blob: e09d7f5000777723f7be3dc4dfb7cd76fee362b6 [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
Jens Axboe0a1b8b82018-11-26 08:24:43 -070041static int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, bool spin);
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{
Jens Axboef31967f2018-10-29 13:13:29 -060078 const int bit = ctx->index_hw[hctx->type];
79
80 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
81 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -060082}
83
84static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
85 struct blk_mq_ctx *ctx)
86{
Jens Axboef31967f2018-10-29 13:13:29 -060087 const int bit = ctx->index_hw[hctx->type];
88
89 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +010090}
91
Jens Axboef299b7c2017-08-08 17:51:45 -060092struct mq_inflight {
93 struct hd_struct *part;
94 unsigned int *inflight;
95};
96
Jens Axboe7baa8572018-11-08 10:24:07 -070097static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -060098 struct request *rq, void *priv,
99 bool reserved)
100{
101 struct mq_inflight *mi = priv;
102
Omar Sandoval61318372018-04-26 00:21:58 -0700103 /*
104 * index[0] counts the specific partition that was asked for. index[1]
105 * counts the ones that are active on the whole device, so increment
106 * that if mi->part is indeed a partition, and not a whole device.
107 */
108 if (rq->part == mi->part)
109 mi->inflight[0]++;
110 if (mi->part->partno)
111 mi->inflight[1]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700112
113 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600114}
115
116void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
117 unsigned int inflight[2])
118{
119 struct mq_inflight mi = { .part = part, .inflight = inflight, };
120
Jens Axboeb8d62b32017-08-08 17:53:33 -0600121 inflight[0] = inflight[1] = 0;
Jens Axboef299b7c2017-08-08 17:51:45 -0600122 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
123}
124
Jens Axboe7baa8572018-11-08 10:24:07 -0700125static bool blk_mq_check_inflight_rw(struct blk_mq_hw_ctx *hctx,
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700126 struct request *rq, void *priv,
127 bool reserved)
128{
129 struct mq_inflight *mi = priv;
130
131 if (rq->part == mi->part)
132 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700133
134 return true;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700135}
136
137void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
138 unsigned int inflight[2])
139{
140 struct mq_inflight mi = { .part = part, .inflight = inflight, };
141
142 inflight[0] = inflight[1] = 0;
143 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight_rw, &mi);
144}
145
Ming Lei1671d522017-03-27 20:06:57 +0800146void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800147{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200148 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -0400149
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200150 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
151 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400152 percpu_ref_kill(&q->q_usage_counter);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700153 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800154 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -0400155 }
Tejun Heof3af0202014-11-04 13:52:27 -0500156}
Ming Lei1671d522017-03-27 20:06:57 +0800157EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500158
Keith Busch6bae363e2017-03-01 14:22:10 -0500159void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500160{
Dan Williams3ef28e82015-10-21 13:20:12 -0400161 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800162}
Keith Busch6bae363e2017-03-01 14:22:10 -0500163EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800164
Keith Buschf91328c2017-03-01 14:22:11 -0500165int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
166 unsigned long timeout)
167{
168 return wait_event_timeout(q->mq_freeze_wq,
169 percpu_ref_is_zero(&q->q_usage_counter),
170 timeout);
171}
172EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100173
Tejun Heof3af0202014-11-04 13:52:27 -0500174/*
175 * Guarantee no request is in use, so we can change any data structure of
176 * the queue afterward.
177 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400178void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500179{
Dan Williams3ef28e82015-10-21 13:20:12 -0400180 /*
181 * In the !blk_mq case we are only calling this to kill the
182 * q_usage_counter, otherwise this increases the freeze depth
183 * and waits for it to return to zero. For this reason there is
184 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
185 * exported to drivers as the only user for unfreeze is blk_mq.
186 */
Ming Lei1671d522017-03-27 20:06:57 +0800187 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500188 blk_mq_freeze_queue_wait(q);
189}
Dan Williams3ef28e82015-10-21 13:20:12 -0400190
191void blk_mq_freeze_queue(struct request_queue *q)
192{
193 /*
194 * ...just an alias to keep freeze and unfreeze actions balanced
195 * in the blk_mq_* namespace
196 */
197 blk_freeze_queue(q);
198}
Jens Axboec761d962015-01-02 15:05:12 -0700199EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500200
Keith Buschb4c6a022014-12-19 17:54:14 -0700201void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100202{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200203 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100204
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200205 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
206 WARN_ON_ONCE(freeze_depth < 0);
207 if (!freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700208 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100209 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600210 }
Jens Axboe320ae512013-10-24 09:20:05 +0100211}
Keith Buschb4c6a022014-12-19 17:54:14 -0700212EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100213
Bart Van Assche852ec802017-06-21 10:55:47 -0700214/*
215 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
216 * mpt3sas driver such that this function can be removed.
217 */
218void blk_mq_quiesce_queue_nowait(struct request_queue *q)
219{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800220 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700221}
222EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
223
Bart Van Assche6a83e742016-11-02 10:09:51 -0600224/**
Ming Lei69e07c42017-06-06 23:22:07 +0800225 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600226 * @q: request queue.
227 *
228 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800229 * callback function is invoked. Once this function is returned, we make
230 * sure no dispatch can happen until the queue is unquiesced via
231 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600232 */
233void blk_mq_quiesce_queue(struct request_queue *q)
234{
235 struct blk_mq_hw_ctx *hctx;
236 unsigned int i;
237 bool rcu = false;
238
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800239 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600240
Bart Van Assche6a83e742016-11-02 10:09:51 -0600241 queue_for_each_hw_ctx(q, hctx, i) {
242 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800243 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600244 else
245 rcu = true;
246 }
247 if (rcu)
248 synchronize_rcu();
249}
250EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
251
Ming Leie4e73912017-06-06 23:22:03 +0800252/*
253 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
254 * @q: request queue.
255 *
256 * This function recovers queue into the state before quiescing
257 * which is done by blk_mq_quiesce_queue.
258 */
259void blk_mq_unquiesce_queue(struct request_queue *q)
260{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800261 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600262
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800263 /* dispatch requests which are inserted during quiescing */
264 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800265}
266EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
267
Jens Axboeaed3ea92014-12-22 14:04:42 -0700268void blk_mq_wake_waiters(struct request_queue *q)
269{
270 struct blk_mq_hw_ctx *hctx;
271 unsigned int i;
272
273 queue_for_each_hw_ctx(q, hctx, i)
274 if (blk_mq_hw_queue_mapped(hctx))
275 blk_mq_tag_wakeup_all(hctx->tags, true);
276}
277
Jens Axboe320ae512013-10-24 09:20:05 +0100278bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
279{
280 return blk_mq_has_free_tags(hctx->tags);
281}
282EXPORT_SYMBOL(blk_mq_can_queue);
283
Jens Axboefe1f4522018-11-28 10:50:07 -0700284/*
285 * Only need start/end time stamping if we have stats enabled, or using
286 * an IO scheduler.
287 */
288static inline bool blk_mq_need_time_stamp(struct request *rq)
289{
290 return (rq->rq_flags & RQF_IO_STAT) || rq->q->elevator;
291}
292
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200293static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
294 unsigned int tag, unsigned int op)
Jens Axboe320ae512013-10-24 09:20:05 +0100295{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200296 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
297 struct request *rq = tags->static_rqs[tag];
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700298 req_flags_t rq_flags = 0;
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700299
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200300 if (data->flags & BLK_MQ_REQ_INTERNAL) {
301 rq->tag = -1;
302 rq->internal_tag = tag;
303 } else {
Jianchao Wangd263ed92018-08-09 08:34:17 -0600304 if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) {
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700305 rq_flags = RQF_MQ_INFLIGHT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200306 atomic_inc(&data->hctx->nr_active);
307 }
308 rq->tag = tag;
309 rq->internal_tag = -1;
310 data->hctx->tags->rqs[rq->tag] = rq;
311 }
312
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200313 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200314 rq->q = data->q;
315 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600316 rq->mq_hctx = data->hctx;
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700317 rq->rq_flags = rq_flags;
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600318 rq->cmd_flags = op;
Bart Van Assche1b6d65a2017-11-09 10:49:55 -0800319 if (data->flags & BLK_MQ_REQ_PREEMPT)
320 rq->rq_flags |= RQF_PREEMPT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200321 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200322 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700323 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200324 INIT_HLIST_NODE(&rq->hash);
325 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200326 rq->rq_disk = NULL;
327 rq->part = NULL;
Jens Axboefe1f4522018-11-28 10:50:07 -0700328 if (blk_mq_need_time_stamp(rq))
329 rq->start_time_ns = ktime_get_ns();
330 else
331 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700332 rq->io_start_time_ns = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200333 rq->nr_phys_segments = 0;
334#if defined(CONFIG_BLK_DEV_INTEGRITY)
335 rq->nr_integrity_segments = 0;
336#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200337 rq->special = NULL;
338 /* tag was already set */
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200339 rq->extra_len = 0;
Christoph Hellwig079076b2018-11-14 17:02:05 +0100340 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200341
Jens Axboef6be4fb2014-06-06 11:03:48 -0600342 rq->timeout = 0;
343
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200344 rq->end_io = NULL;
345 rq->end_io_data = NULL;
346 rq->next_rq = NULL;
347
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200348 data->ctx->rq_dispatched[op_is_sync(op)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200349 refcount_set(&rq->ref, 1);
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200350 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100351}
352
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200353static struct request *blk_mq_get_request(struct request_queue *q,
Jens Axboef9afca42018-10-29 13:11:38 -0600354 struct bio *bio,
355 struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200356{
357 struct elevator_queue *e = q->elevator;
358 struct request *rq;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200359 unsigned int tag;
Bart Van Assche21e768b2017-10-16 16:32:26 -0700360 bool put_ctx_on_error = false;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200361
362 blk_queue_enter_live(q);
363 data->q = q;
Bart Van Assche21e768b2017-10-16 16:32:26 -0700364 if (likely(!data->ctx)) {
365 data->ctx = blk_mq_get_ctx(q);
366 put_ctx_on_error = true;
367 }
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200368 if (likely(!data->hctx))
Jens Axboef9afca42018-10-29 13:11:38 -0600369 data->hctx = blk_mq_map_queue(q, data->cmd_flags,
370 data->ctx->cpu);
371 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500372 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200373
374 if (e) {
375 data->flags |= BLK_MQ_REQ_INTERNAL;
376
377 /*
378 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600379 * dispatch list. Don't include reserved tags in the
380 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200381 */
Jens Axboef9afca42018-10-29 13:11:38 -0600382 if (!op_is_flush(data->cmd_flags) &&
383 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600384 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600385 e->type->ops.limit_depth(data->cmd_flags, data);
Jianchao Wangd263ed92018-08-09 08:34:17 -0600386 } else {
387 blk_mq_tag_busy(data->hctx);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200388 }
389
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200390 tag = blk_mq_get_tag(data);
391 if (tag == BLK_MQ_TAG_FAIL) {
Bart Van Assche21e768b2017-10-16 16:32:26 -0700392 if (put_ctx_on_error) {
393 blk_mq_put_ctx(data->ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800394 data->ctx = NULL;
395 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200396 blk_queue_exit(q);
397 return NULL;
398 }
399
Jens Axboef9afca42018-10-29 13:11:38 -0600400 rq = blk_mq_rq_ctx_init(data, tag, data->cmd_flags);
401 if (!op_is_flush(data->cmd_flags)) {
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200402 rq->elv.icq = NULL;
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600403 if (e && e->type->ops.prepare_request) {
Damien Le Moale2b3fa52018-11-20 10:52:34 +0900404 if (e->type->icq_cache)
405 blk_mq_sched_assign_ioc(rq);
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200406
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600407 e->type->ops.prepare_request(rq, bio);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200408 rq->rq_flags |= RQF_ELVPRIV;
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200409 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200410 }
411 data->hctx->queued++;
412 return rq;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200413}
414
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700415struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800416 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100417{
Jens Axboef9afca42018-10-29 13:11:38 -0600418 struct blk_mq_alloc_data alloc_data = { .flags = flags, .cmd_flags = op };
Jens Axboebd166ef2017-01-17 06:03:22 -0700419 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600420 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100421
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800422 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600423 if (ret)
424 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100425
Jens Axboef9afca42018-10-29 13:11:38 -0600426 rq = blk_mq_get_request(q, NULL, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400427 blk_queue_exit(q);
Jens Axboe841bac22016-09-21 10:08:43 -0600428
Jens Axboebd166ef2017-01-17 06:03:22 -0700429 if (!rq)
Joe Lawrencea492f072014-08-28 08:15:21 -0600430 return ERR_PTR(-EWOULDBLOCK);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200431
Ming Lei1ad43c02017-08-02 08:01:45 +0800432 blk_mq_put_ctx(alloc_data.ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800433
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200434 rq->__data_len = 0;
435 rq->__sector = (sector_t) -1;
436 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100437 return rq;
438}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600439EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100440
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700441struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800442 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200443{
Jens Axboef9afca42018-10-29 13:11:38 -0600444 struct blk_mq_alloc_data alloc_data = { .flags = flags, .cmd_flags = op };
Ming Lin1f5bd332016-06-13 16:45:21 +0200445 struct request *rq;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800446 unsigned int cpu;
Ming Lin1f5bd332016-06-13 16:45:21 +0200447 int ret;
448
449 /*
450 * If the tag allocator sleeps we could get an allocation for a
451 * different hardware context. No need to complicate the low level
452 * allocator for this for the rare use case of a command tied to
453 * a specific queue.
454 */
455 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
456 return ERR_PTR(-EINVAL);
457
458 if (hctx_idx >= q->nr_hw_queues)
459 return ERR_PTR(-EIO);
460
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800461 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200462 if (ret)
463 return ERR_PTR(ret);
464
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600465 /*
466 * Check if the hardware context is actually mapped to anything.
467 * If not tell the caller that it should skip this queue.
468 */
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800469 alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
470 if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
471 blk_queue_exit(q);
472 return ERR_PTR(-EXDEV);
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600473 }
Christoph Hellwig20e4d8132018-01-12 10:53:06 +0800474 cpu = cpumask_first_and(alloc_data.hctx->cpumask, cpu_online_mask);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800475 alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200476
Jens Axboef9afca42018-10-29 13:11:38 -0600477 rq = blk_mq_get_request(q, NULL, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400478 blk_queue_exit(q);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800479
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800480 if (!rq)
481 return ERR_PTR(-EWOULDBLOCK);
Ming Lin1f5bd332016-06-13 16:45:21 +0200482
483 return rq;
484}
485EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
486
Keith Busch12f5b932018-05-29 15:52:28 +0200487static void __blk_mq_free_request(struct request *rq)
488{
489 struct request_queue *q = rq->q;
490 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600491 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200492 const int sched_tag = rq->internal_tag;
493
Bart Van Assche986d4132018-09-26 14:01:10 -0700494 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600495 rq->mq_hctx = NULL;
Keith Busch12f5b932018-05-29 15:52:28 +0200496 if (rq->tag != -1)
497 blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
498 if (sched_tag != -1)
499 blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
500 blk_mq_sched_restart(hctx);
501 blk_queue_exit(q);
502}
503
Christoph Hellwig6af54052017-06-16 18:15:22 +0200504void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100505{
Jens Axboe320ae512013-10-24 09:20:05 +0100506 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200507 struct elevator_queue *e = q->elevator;
508 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600509 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100510
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200511 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600512 if (e && e->type->ops.finish_request)
513 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200514 if (rq->elv.icq) {
515 put_io_context(rq->elv.icq->ioc);
516 rq->elv.icq = NULL;
517 }
518 }
519
520 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200521 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600522 atomic_dec(&hctx->nr_active);
Jens Axboe87760e52016-11-09 12:38:14 -0700523
Jens Axboe7beb2f82017-09-30 02:08:24 -0600524 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
525 laptop_io_completion(q->backing_dev_info);
526
Josef Bacika7905042018-07-03 09:32:35 -0600527 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600528
Keith Busch12f5b932018-05-29 15:52:28 +0200529 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
530 if (refcount_dec_and_test(&rq->ref))
531 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100532}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700533EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100534
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200535inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100536{
Jens Axboefe1f4522018-11-28 10:50:07 -0700537 u64 now = 0;
538
539 if (blk_mq_need_time_stamp(rq))
540 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700541
Omar Sandoval4bc63392018-05-09 02:08:52 -0700542 if (rq->rq_flags & RQF_STATS) {
543 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700544 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700545 }
546
Omar Sandovaled886602018-09-27 15:55:51 -0700547 if (rq->internal_tag != -1)
548 blk_mq_sched_completed_request(rq, now);
549
Omar Sandoval522a7772018-05-09 02:08:53 -0700550 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700551
Christoph Hellwig91b63632014-04-16 09:44:53 +0200552 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600553 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100554 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200555 } else {
556 if (unlikely(blk_bidi_rq(rq)))
557 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100558 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200559 }
Jens Axboe320ae512013-10-24 09:20:05 +0100560}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700561EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200562
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200563void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200564{
565 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
566 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700567 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200568}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700569EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100570
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800571static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100572{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800573 struct request *rq = data;
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600574 struct request_queue *q = rq->q;
Jens Axboe320ae512013-10-24 09:20:05 +0100575
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600576 q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100577}
578
Christoph Hellwig453f8342017-04-20 16:03:10 +0200579static void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100580{
581 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600582 struct request_queue *q = rq->q;
Christoph Hellwig38535202014-04-25 02:32:53 -0700583 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100584 int cpu;
585
Keith Buschaf78ff72018-11-26 09:54:30 -0700586 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800587 /*
588 * Most of single queue controllers, there is only one irq vector
589 * for handling IO completion, and the only irq's affinity is set
590 * as all possible CPUs. On most of ARCHs, this affinity means the
591 * irq is handled on one specific CPU.
592 *
593 * So complete IO reqeust in softirq context in case of single queue
594 * for not degrading IO performance by irqsoff latency.
595 */
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600596 if (q->nr_hw_queues == 1) {
Ming Lei36e76532018-09-28 16:42:20 +0800597 __blk_complete_request(rq);
598 return;
599 }
600
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700601 /*
602 * For a polled request, always complete locallly, it's pointless
603 * to redirect the completion.
604 */
605 if ((rq->cmd_flags & REQ_HIPRI) ||
606 !test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags)) {
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600607 q->mq_ops->complete(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800608 return;
609 }
Jens Axboe320ae512013-10-24 09:20:05 +0100610
611 cpu = get_cpu();
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600612 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
Christoph Hellwig38535202014-04-25 02:32:53 -0700613 shared = cpus_share_cache(cpu, ctx->cpu);
614
615 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800616 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800617 rq->csd.info = rq;
618 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100619 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800620 } else {
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600621 q->mq_ops->complete(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800622 }
Jens Axboe320ae512013-10-24 09:20:05 +0100623 put_cpu();
624}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800625
Jens Axboe04ced152018-01-09 08:29:46 -0800626static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800627 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800628{
629 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
630 rcu_read_unlock();
631 else
Tejun Heo05707b62018-01-09 08:29:53 -0800632 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800633}
634
635static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800636 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800637{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700638 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
639 /* shut up gcc false positive */
640 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800641 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700642 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800643 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800644}
645
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800646/**
647 * blk_mq_complete_request - end I/O on a request
648 * @rq: the request being processed
649 *
650 * Description:
651 * Ends all I/O on a request. It does not handle partial completions.
652 * The actual completion happens out-of-order, through a IPI handler.
653 **/
Keith Busch16c15eb2018-11-26 09:54:28 -0700654bool blk_mq_complete_request(struct request *rq)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800655{
Keith Busch12f5b932018-05-29 15:52:28 +0200656 if (unlikely(blk_should_fake_timeout(rq->q)))
Keith Busch16c15eb2018-11-26 09:54:28 -0700657 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200658 __blk_mq_complete_request(rq);
Keith Busch16c15eb2018-11-26 09:54:28 -0700659 return true;
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800660}
661EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100662
Keith Busch973c0192015-01-07 18:55:43 -0700663int blk_mq_request_started(struct request *rq)
664{
Tejun Heo5a61c362018-01-09 08:29:52 -0800665 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
Keith Busch973c0192015-01-07 18:55:43 -0700666}
667EXPORT_SYMBOL_GPL(blk_mq_request_started);
668
Christoph Hellwige2490072014-09-13 16:40:09 -0700669void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100670{
671 struct request_queue *q = rq->q;
672
Jens Axboebd166ef2017-01-17 06:03:22 -0700673 blk_mq_sched_started_request(rq);
674
Jens Axboe320ae512013-10-24 09:20:05 +0100675 trace_block_rq_issue(q, rq);
676
Jens Axboecf43e6b2016-11-07 21:32:37 -0700677 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700678 rq->io_start_time_ns = ktime_get_ns();
679#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
680 rq->throtl_size = blk_rq_sectors(rq);
681#endif
Jens Axboecf43e6b2016-11-07 21:32:37 -0700682 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600683 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700684 }
685
Tejun Heo1d9bd512018-01-09 08:29:48 -0800686 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600687
Tejun Heo1d9bd512018-01-09 08:29:48 -0800688 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200689 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800690
691 if (q->dma_drain_size && blk_rq_bytes(rq)) {
692 /*
693 * Make sure space for the drain appears. We know we can do
694 * this because max_hw_segments has been adjusted to be one
695 * fewer than the device can handle.
696 */
697 rq->nr_phys_segments++;
698 }
Jens Axboe320ae512013-10-24 09:20:05 +0100699}
Christoph Hellwige2490072014-09-13 16:40:09 -0700700EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100701
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200702static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100703{
704 struct request_queue *q = rq->q;
705
Ming Lei923218f2017-11-02 23:24:38 +0800706 blk_mq_put_driver_tag(rq);
707
Jens Axboe320ae512013-10-24 09:20:05 +0100708 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600709 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800710
Keith Busch12f5b932018-05-29 15:52:28 +0200711 if (blk_mq_request_started(rq)) {
712 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200713 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700714 if (q->dma_drain_size && blk_rq_bytes(rq))
715 rq->nr_phys_segments--;
716 }
Jens Axboe320ae512013-10-24 09:20:05 +0100717}
718
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700719void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200720{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200721 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200722
Ming Lei105976f2018-02-23 23:36:56 +0800723 /* this request will be re-inserted to io scheduler queue */
724 blk_mq_sched_requeue_request(rq);
725
Jens Axboe7d692332018-10-24 10:48:12 -0600726 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700727 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200728}
729EXPORT_SYMBOL(blk_mq_requeue_request);
730
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600731static void blk_mq_requeue_work(struct work_struct *work)
732{
733 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400734 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600735 LIST_HEAD(rq_list);
736 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600737
Jens Axboe18e97812017-07-27 08:03:57 -0600738 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600739 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600740 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600741
742 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200743 if (!(rq->rq_flags & RQF_SOFTBARRIER))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600744 continue;
745
Christoph Hellwige8064022016-10-20 15:12:13 +0200746 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600747 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500748 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600749 }
750
751 while (!list_empty(&rq_list)) {
752 rq = list_entry(rq_list.next, struct request, queuelist);
753 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500754 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600755 }
756
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700757 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600758}
759
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700760void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
761 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600762{
763 struct request_queue *q = rq->q;
764 unsigned long flags;
765
766 /*
767 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700768 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600769 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200770 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600771
772 spin_lock_irqsave(&q->requeue_lock, flags);
773 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200774 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600775 list_add(&rq->queuelist, &q->requeue_list);
776 } else {
777 list_add_tail(&rq->queuelist, &q->requeue_list);
778 }
779 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700780
781 if (kick_requeue_list)
782 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600783}
784EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
785
786void blk_mq_kick_requeue_list(struct request_queue *q)
787{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800788 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600789}
790EXPORT_SYMBOL(blk_mq_kick_requeue_list);
791
Mike Snitzer28494502016-09-14 13:28:30 -0400792void blk_mq_delay_kick_requeue_list(struct request_queue *q,
793 unsigned long msecs)
794{
Bart Van Assched4acf362017-08-09 11:28:06 -0700795 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
796 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400797}
798EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
799
Jens Axboe0e62f512014-06-04 10:23:49 -0600800struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
801{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600802 if (tag < tags->nr_tags) {
803 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700804 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600805 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700806
807 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600808}
809EXPORT_SYMBOL(blk_mq_tag_to_rq);
810
Jens Axboeae879912018-11-08 09:03:51 -0700811static bool blk_mq_check_busy(struct blk_mq_hw_ctx *hctx, struct request *rq,
812 void *priv, bool reserved)
813{
814 /*
815 * If we find a request, we know the queue is busy. Return false
816 * to stop the iteration.
817 */
818 if (rq->q == hctx->queue) {
819 bool *busy = priv;
820
821 *busy = true;
822 return false;
823 }
824
825 return true;
826}
827
828bool blk_mq_queue_busy(struct request_queue *q)
829{
830 bool busy = false;
831
832 blk_mq_queue_tag_busy_iter(q, blk_mq_check_busy, &busy);
833 return busy;
834}
835EXPORT_SYMBOL_GPL(blk_mq_queue_busy);
836
Tejun Heo358f70d2018-01-09 08:29:50 -0800837static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100838{
Christoph Hellwigda661262018-06-14 13:58:45 +0200839 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200840 if (req->q->mq_ops->timeout) {
841 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600842
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200843 ret = req->q->mq_ops->timeout(req, reserved);
844 if (ret == BLK_EH_DONE)
845 return;
846 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700847 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200848
849 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600850}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700851
Keith Busch12f5b932018-05-29 15:52:28 +0200852static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
853{
854 unsigned long deadline;
855
856 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
857 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200858 if (rq->rq_flags & RQF_TIMED_OUT)
859 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200860
Christoph Hellwig079076b2018-11-14 17:02:05 +0100861 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200862 if (time_after_eq(jiffies, deadline))
863 return true;
864
865 if (*next == 0)
866 *next = deadline;
867 else if (time_after(*next, deadline))
868 *next = deadline;
869 return false;
870}
871
Jens Axboe7baa8572018-11-08 10:24:07 -0700872static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700873 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100874{
Keith Busch12f5b932018-05-29 15:52:28 +0200875 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700876
Keith Busch12f5b932018-05-29 15:52:28 +0200877 /*
878 * Just do a quick check if it is expired before locking the request in
879 * so we're not unnecessarilly synchronizing across CPUs.
880 */
881 if (!blk_mq_req_expired(rq, next))
Jens Axboe7baa8572018-11-08 10:24:07 -0700882 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100883
Tejun Heo1d9bd512018-01-09 08:29:48 -0800884 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200885 * We have reason to believe the request may be expired. Take a
886 * reference on the request to lock this request lifetime into its
887 * currently allocated context to prevent it from being reallocated in
888 * the event the completion by-passes this timeout handler.
889 *
890 * If the reference was already released, then the driver beat the
891 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800892 */
Keith Busch12f5b932018-05-29 15:52:28 +0200893 if (!refcount_inc_not_zero(&rq->ref))
Jens Axboe7baa8572018-11-08 10:24:07 -0700894 return true;
Keith Busch12f5b932018-05-29 15:52:28 +0200895
896 /*
897 * The request is now locked and cannot be reallocated underneath the
898 * timeout handler's processing. Re-verify this exact request is truly
899 * expired; if it is not expired, then the request was completed and
900 * reallocated as a new request.
901 */
902 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800903 blk_mq_rq_timed_out(rq, reserved);
Keith Busch12f5b932018-05-29 15:52:28 +0200904 if (refcount_dec_and_test(&rq->ref))
905 __blk_mq_free_request(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700906
907 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800908}
909
Christoph Hellwig287922e2015-10-30 20:57:30 +0800910static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100911{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800912 struct request_queue *q =
913 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200914 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800915 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700916 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100917
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600918 /* A deadlock might occur if a request is stuck requiring a
919 * timeout at the same time a queue freeze is waiting
920 * completion, since the timeout code would not be able to
921 * acquire the queue reference here.
922 *
923 * That's why we don't use blk_queue_enter here; instead, we use
924 * percpu_ref_tryget directly, because we need to be able to
925 * obtain a reference even in the short window between the queue
926 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800927 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600928 * consumed, marked by the instant q_usage_counter reaches
929 * zero.
930 */
931 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800932 return;
933
Keith Busch12f5b932018-05-29 15:52:28 +0200934 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +0100935
Keith Busch12f5b932018-05-29 15:52:28 +0200936 if (next != 0) {
937 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600938 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -0800939 /*
940 * Request timeouts are handled as a forward rolling timer. If
941 * we end up here it means that no requests are pending and
942 * also that no request has been pending for a while. Mark
943 * each hctx as idle.
944 */
Ming Leif054b562015-04-21 10:00:19 +0800945 queue_for_each_hw_ctx(q, hctx, i) {
946 /* the hctx may be unmapped, so check it here */
947 if (blk_mq_hw_queue_mapped(hctx))
948 blk_mq_tag_idle(hctx);
949 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600950 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800951 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100952}
953
Omar Sandoval88459642016-09-17 08:38:44 -0600954struct flush_busy_ctx_data {
955 struct blk_mq_hw_ctx *hctx;
956 struct list_head *list;
957};
958
959static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
960{
961 struct flush_busy_ctx_data *flush_data = data;
962 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
963 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
964
Omar Sandoval88459642016-09-17 08:38:44 -0600965 spin_lock(&ctx->lock);
966 list_splice_tail_init(&ctx->rq_list, flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -0800967 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -0600968 spin_unlock(&ctx->lock);
969 return true;
970}
971
Jens Axboe320ae512013-10-24 09:20:05 +0100972/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600973 * Process software queues that have been marked busy, splicing them
974 * to the for-dispatch
975 */
Jens Axboe2c3ad662016-12-14 14:34:47 -0700976void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -0600977{
Omar Sandoval88459642016-09-17 08:38:44 -0600978 struct flush_busy_ctx_data data = {
979 .hctx = hctx,
980 .list = list,
981 };
Jens Axboe1429d7c2014-05-19 09:23:55 -0600982
Omar Sandoval88459642016-09-17 08:38:44 -0600983 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600984}
Jens Axboe2c3ad662016-12-14 14:34:47 -0700985EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600986
Ming Leib3476892017-10-14 17:22:30 +0800987struct dispatch_rq_data {
988 struct blk_mq_hw_ctx *hctx;
989 struct request *rq;
990};
991
992static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
993 void *data)
994{
995 struct dispatch_rq_data *dispatch_data = data;
996 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
997 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
998
999 spin_lock(&ctx->lock);
huhaib4f6f382018-05-22 17:39:34 +08001000 if (!list_empty(&ctx->rq_list)) {
Ming Leib3476892017-10-14 17:22:30 +08001001 dispatch_data->rq = list_entry_rq(ctx->rq_list.next);
1002 list_del_init(&dispatch_data->rq->queuelist);
1003 if (list_empty(&ctx->rq_list))
1004 sbitmap_clear_bit(sb, bitnr);
1005 }
1006 spin_unlock(&ctx->lock);
1007
1008 return !dispatch_data->rq;
1009}
1010
1011struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1012 struct blk_mq_ctx *start)
1013{
Jens Axboef31967f2018-10-29 13:13:29 -06001014 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001015 struct dispatch_rq_data data = {
1016 .hctx = hctx,
1017 .rq = NULL,
1018 };
1019
1020 __sbitmap_for_each_set(&hctx->ctx_map, off,
1021 dispatch_rq_from_ctx, &data);
1022
1023 return data.rq;
1024}
1025
Jens Axboe703fd1c2016-09-16 13:59:14 -06001026static inline unsigned int queued_to_index(unsigned int queued)
1027{
1028 if (!queued)
1029 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001030
Jens Axboe703fd1c2016-09-16 13:59:14 -06001031 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001032}
1033
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001034bool blk_mq_get_driver_tag(struct request *rq)
Jens Axboebd166ef2017-01-17 06:03:22 -07001035{
1036 struct blk_mq_alloc_data data = {
1037 .q = rq->q,
Jens Axboeea4f9952018-10-29 15:06:13 -06001038 .hctx = rq->mq_hctx,
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001039 .flags = BLK_MQ_REQ_NOWAIT,
Jens Axboef9afca42018-10-29 13:11:38 -06001040 .cmd_flags = rq->cmd_flags,
Jens Axboebd166ef2017-01-17 06:03:22 -07001041 };
Jianchao Wangd263ed92018-08-09 08:34:17 -06001042 bool shared;
Jens Axboe5feeacd2017-04-20 17:23:13 -06001043
Omar Sandoval81380ca2017-04-07 08:56:26 -06001044 if (rq->tag != -1)
1045 goto done;
Jens Axboebd166ef2017-01-17 06:03:22 -07001046
Sagi Grimberg415b8062017-02-27 10:04:39 -07001047 if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
1048 data.flags |= BLK_MQ_REQ_RESERVED;
1049
Jianchao Wangd263ed92018-08-09 08:34:17 -06001050 shared = blk_mq_tag_busy(data.hctx);
Jens Axboebd166ef2017-01-17 06:03:22 -07001051 rq->tag = blk_mq_get_tag(&data);
1052 if (rq->tag >= 0) {
Jianchao Wangd263ed92018-08-09 08:34:17 -06001053 if (shared) {
Jens Axboe200e86b2017-01-25 08:11:38 -07001054 rq->rq_flags |= RQF_MQ_INFLIGHT;
1055 atomic_inc(&data.hctx->nr_active);
1056 }
Jens Axboebd166ef2017-01-17 06:03:22 -07001057 data.hctx->tags->rqs[rq->tag] = rq;
Jens Axboebd166ef2017-01-17 06:03:22 -07001058 }
1059
Omar Sandoval81380ca2017-04-07 08:56:26 -06001060done:
Omar Sandoval81380ca2017-04-07 08:56:26 -06001061 return rq->tag != -1;
Jens Axboebd166ef2017-01-17 06:03:22 -07001062}
1063
Jens Axboeeb619fd2017-11-09 08:32:43 -07001064static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1065 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001066{
1067 struct blk_mq_hw_ctx *hctx;
1068
1069 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1070
Ming Lei5815839b2018-06-25 19:31:47 +08001071 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001072 list_del_init(&wait->entry);
Ming Lei5815839b2018-06-25 19:31:47 +08001073 spin_unlock(&hctx->dispatch_wait_lock);
1074
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001075 blk_mq_run_hw_queue(hctx, true);
1076 return 1;
1077}
1078
Jens Axboef906a6a2017-11-09 16:10:13 -07001079/*
1080 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001081 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1082 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001083 * marking us as waiting.
1084 */
Ming Lei2278d692018-06-25 19:31:46 +08001085static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001086 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001087{
Ming Lei5815839b2018-06-25 19:31:47 +08001088 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001089 wait_queue_entry_t *wait;
1090 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001091
Ming Lei2278d692018-06-25 19:31:46 +08001092 if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
1093 if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
1094 set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001095
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001096 /*
1097 * It's possible that a tag was freed in the window between the
1098 * allocation failure and adding the hardware queue to the wait
1099 * queue.
1100 *
1101 * Don't clear RESTART here, someone else could have set it.
1102 * At most this will cost an extra queue run.
1103 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001104 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001105 }
1106
Ming Lei2278d692018-06-25 19:31:46 +08001107 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001108 if (!list_empty_careful(&wait->entry))
1109 return false;
1110
Ming Lei5815839b2018-06-25 19:31:47 +08001111 wq = &bt_wait_ptr(&hctx->tags->bitmap_tags, hctx)->wait;
1112
1113 spin_lock_irq(&wq->lock);
1114 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001115 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001116 spin_unlock(&hctx->dispatch_wait_lock);
1117 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001118 return false;
1119 }
1120
Ming Lei5815839b2018-06-25 19:31:47 +08001121 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1122 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001123
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001124 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001125 * It's possible that a tag was freed in the window between the
1126 * allocation failure and adding the hardware queue to the wait
1127 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001128 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001129 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001130 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001131 spin_unlock(&hctx->dispatch_wait_lock);
1132 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001133 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001134 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001135
1136 /*
1137 * We got a tag, remove ourselves from the wait queue to ensure
1138 * someone else gets the wakeup.
1139 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001140 list_del_init(&wait->entry);
Ming Lei5815839b2018-06-25 19:31:47 +08001141 spin_unlock(&hctx->dispatch_wait_lock);
1142 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001143
1144 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001145}
1146
Ming Lei6e7687172018-07-03 09:03:16 -06001147#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1148#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1149/*
1150 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1151 * - EWMA is one simple way to compute running average value
1152 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1153 * - take 4 as factor for avoiding to get too small(0) result, and this
1154 * factor doesn't matter because EWMA decreases exponentially
1155 */
1156static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1157{
1158 unsigned int ewma;
1159
1160 if (hctx->queue->elevator)
1161 return;
1162
1163 ewma = hctx->dispatch_busy;
1164
1165 if (!ewma && !busy)
1166 return;
1167
1168 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1169 if (busy)
1170 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1171 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1172
1173 hctx->dispatch_busy = ewma;
1174}
1175
Ming Lei86ff7c22018-01-30 22:04:57 -05001176#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1177
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001178/*
1179 * Returns true if we did some work AND can potentially do more.
1180 */
Ming Leide148292017-10-14 17:22:29 +08001181bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
Jens Axboeeb619fd2017-11-09 08:32:43 -07001182 bool got_budget)
Jens Axboef04c3df2016-12-07 08:41:17 -07001183{
Omar Sandoval81380ca2017-04-07 08:56:26 -06001184 struct blk_mq_hw_ctx *hctx;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001185 struct request *rq, *nxt;
Jens Axboeeb619fd2017-11-09 08:32:43 -07001186 bool no_tag = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001187 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001188 blk_status_t ret = BLK_STS_OK;
Jens Axboef04c3df2016-12-07 08:41:17 -07001189
Omar Sandoval81380ca2017-04-07 08:56:26 -06001190 if (list_empty(list))
1191 return false;
1192
Ming Leide148292017-10-14 17:22:29 +08001193 WARN_ON(!list_is_singular(list) && got_budget);
1194
Jens Axboef04c3df2016-12-07 08:41:17 -07001195 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001196 * Now process all the entries, sending them to the driver.
1197 */
Jens Axboe93efe982017-03-24 12:04:19 -06001198 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001199 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001200 struct blk_mq_queue_data bd;
1201
1202 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001203
Jens Axboeea4f9952018-10-29 15:06:13 -06001204 hctx = rq->mq_hctx;
Ming Lei0bca7992018-04-05 00:35:21 +08001205 if (!got_budget && !blk_mq_get_dispatch_budget(hctx))
1206 break;
1207
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001208 if (!blk_mq_get_driver_tag(rq)) {
Jens Axboe3c782d62017-01-26 12:50:36 -07001209 /*
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001210 * The initial allocation attempt failed, so we need to
Jens Axboeeb619fd2017-11-09 08:32:43 -07001211 * rerun the hardware queue when a tag is freed. The
1212 * waitqueue takes care of that. If the queue is run
1213 * before we add this entry back on the dispatch list,
1214 * we'll re-run it below.
Jens Axboe3c782d62017-01-26 12:50:36 -07001215 */
Ming Lei2278d692018-06-25 19:31:46 +08001216 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001217 blk_mq_put_dispatch_budget(hctx);
Jens Axboef906a6a2017-11-09 16:10:13 -07001218 /*
1219 * For non-shared tags, the RESTART check
1220 * will suffice.
1221 */
1222 if (hctx->flags & BLK_MQ_F_TAG_SHARED)
1223 no_tag = true;
Omar Sandoval807b1042017-04-05 12:01:35 -07001224 break;
Ming Leide148292017-10-14 17:22:29 +08001225 }
1226 }
1227
Jens Axboef04c3df2016-12-07 08:41:17 -07001228 list_del_init(&rq->queuelist);
1229
1230 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001231
1232 /*
1233 * Flag last if we have no more requests, or if we have more
1234 * but can't assign a driver tag to it.
1235 */
1236 if (list_empty(list))
1237 bd.last = true;
1238 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001239 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001240 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001241 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001242
1243 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei86ff7c22018-01-30 22:04:57 -05001244 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001245 /*
1246 * If an I/O scheduler has been configured and we got a
Jens Axboeff821d22017-11-10 22:05:12 -07001247 * driver tag for the next request already, free it
1248 * again.
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001249 */
1250 if (!list_empty(list)) {
1251 nxt = list_first_entry(list, struct request, queuelist);
1252 blk_mq_put_driver_tag(nxt);
1253 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001254 list_add(&rq->queuelist, list);
1255 __blk_mq_requeue_request(rq);
1256 break;
Jens Axboef04c3df2016-12-07 08:41:17 -07001257 }
1258
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001259 if (unlikely(ret != BLK_STS_OK)) {
1260 errors++;
1261 blk_mq_end_request(rq, BLK_STS_IOERR);
1262 continue;
1263 }
1264
1265 queued++;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001266 } while (!list_empty(list));
Jens Axboef04c3df2016-12-07 08:41:17 -07001267
1268 hctx->dispatched[queued_to_index(queued)]++;
1269
1270 /*
1271 * Any items that need requeuing? Stuff them into hctx->dispatch,
1272 * that is where we will continue on next queue run.
1273 */
1274 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001275 bool needs_restart;
1276
Jens Axboed666ba92018-11-27 17:02:25 -07001277 /*
1278 * If we didn't flush the entire list, we could have told
1279 * the driver there was more coming, but that turned out to
1280 * be a lie.
1281 */
1282 if (q->mq_ops->commit_rqs)
1283 q->mq_ops->commit_rqs(hctx);
1284
Jens Axboef04c3df2016-12-07 08:41:17 -07001285 spin_lock(&hctx->lock);
Jens Axboec13660a2017-01-26 12:40:07 -07001286 list_splice_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001287 spin_unlock(&hctx->lock);
1288
1289 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001290 * If SCHED_RESTART was set by the caller of this function and
1291 * it is no longer set that means that it was cleared by another
1292 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001293 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001294 * If 'no_tag' is set, that means that we failed getting
1295 * a driver tag with an I/O scheduler attached. If our dispatch
1296 * waitqueue is no longer active, ensure that we run the queue
1297 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001298 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001299 * If no I/O scheduler has been configured it is possible that
1300 * the hardware queue got stopped and restarted before requests
1301 * were pushed back onto the dispatch list. Rerun the queue to
1302 * avoid starvation. Notes:
1303 * - blk_mq_run_hw_queue() checks whether or not a queue has
1304 * been stopped before rerunning a queue.
1305 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001306 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001307 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001308 *
1309 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1310 * bit is set, run queue after a delay to avoid IO stalls
1311 * that could otherwise occur if the queue is idle.
Jens Axboebd166ef2017-01-17 06:03:22 -07001312 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001313 needs_restart = blk_mq_sched_needs_restart(hctx);
1314 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001315 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001316 blk_mq_run_hw_queue(hctx, true);
Ming Lei86ff7c22018-01-30 22:04:57 -05001317 else if (needs_restart && (ret == BLK_STS_RESOURCE))
1318 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001319
Ming Lei6e7687172018-07-03 09:03:16 -06001320 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001321 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001322 } else
1323 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001324
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001325 /*
1326 * If the host/device is unable to accept more work, inform the
1327 * caller of that.
1328 */
1329 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
1330 return false;
1331
Jens Axboe93efe982017-03-24 12:04:19 -06001332 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001333}
1334
Bart Van Assche6a83e742016-11-02 10:09:51 -06001335static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1336{
1337 int srcu_idx;
1338
Jens Axboeb7a71e62017-08-01 09:28:24 -06001339 /*
1340 * We should be running this queue from one of the CPUs that
1341 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001342 *
1343 * There are at least two related races now between setting
1344 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1345 * __blk_mq_run_hw_queue():
1346 *
1347 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1348 * but later it becomes online, then this warning is harmless
1349 * at all
1350 *
1351 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1352 * but later it becomes offline, then the warning can't be
1353 * triggered, and we depend on blk-mq timeout handler to
1354 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001355 */
Ming Lei7df938f2018-01-18 00:41:52 +08001356 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1357 cpu_online(hctx->next_cpu)) {
1358 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1359 raw_smp_processor_id(),
1360 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1361 dump_stack();
1362 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001363
Jens Axboeb7a71e62017-08-01 09:28:24 -06001364 /*
1365 * We can't run the queue inline with ints disabled. Ensure that
1366 * we catch bad users of this early.
1367 */
1368 WARN_ON_ONCE(in_interrupt());
1369
Jens Axboe04ced152018-01-09 08:29:46 -08001370 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001371
Jens Axboe04ced152018-01-09 08:29:46 -08001372 hctx_lock(hctx, &srcu_idx);
1373 blk_mq_sched_dispatch_requests(hctx);
1374 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001375}
1376
Ming Leif82ddf12018-04-08 17:48:10 +08001377static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1378{
1379 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1380
1381 if (cpu >= nr_cpu_ids)
1382 cpu = cpumask_first(hctx->cpumask);
1383 return cpu;
1384}
1385
Jens Axboe506e9312014-05-07 10:26:44 -06001386/*
1387 * It'd be great if the workqueue API had a way to pass
1388 * in a mask and had some smarts for more clever placement.
1389 * For now we just round-robin here, switching for every
1390 * BLK_MQ_CPU_WORK_BATCH queued items.
1391 */
1392static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1393{
Ming Lei7bed4592018-01-18 00:41:51 +08001394 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001395 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001396
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001397 if (hctx->queue->nr_hw_queues == 1)
1398 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001399
1400 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001401select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001402 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001403 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001404 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001405 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001406 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1407 }
1408
Ming Lei7bed4592018-01-18 00:41:51 +08001409 /*
1410 * Do unbound schedule if we can't find a online CPU for this hctx,
1411 * and it should only happen in the path of handling CPU DEAD.
1412 */
Ming Lei476f8c92018-04-08 17:48:09 +08001413 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001414 if (!tried) {
1415 tried = true;
1416 goto select_cpu;
1417 }
1418
1419 /*
1420 * Make sure to re-select CPU next time once after CPUs
1421 * in hctx->cpumask become online again.
1422 */
Ming Lei476f8c92018-04-08 17:48:09 +08001423 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001424 hctx->next_cpu_batch = 1;
1425 return WORK_CPU_UNBOUND;
1426 }
Ming Lei476f8c92018-04-08 17:48:09 +08001427
1428 hctx->next_cpu = next_cpu;
1429 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001430}
1431
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001432static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1433 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001434{
Bart Van Assche5435c022017-06-20 11:15:49 -07001435 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001436 return;
1437
Jens Axboe1b792f22016-09-21 10:12:13 -06001438 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001439 int cpu = get_cpu();
1440 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001441 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001442 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001443 return;
1444 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001445
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001446 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001447 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001448
Bart Van Asscheae943d22018-01-19 08:58:55 -08001449 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1450 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001451}
1452
1453void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1454{
1455 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1456}
1457EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1458
Jens Axboe79f720a2017-11-10 09:13:21 -07001459bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001460{
Ming Lei24f5a902018-01-06 16:27:38 +08001461 int srcu_idx;
1462 bool need_run;
1463
1464 /*
1465 * When queue is quiesced, we may be switching io scheduler, or
1466 * updating nr_hw_queues, or other things, and we can't run queue
1467 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1468 *
1469 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1470 * quiesced.
1471 */
Jens Axboe04ced152018-01-09 08:29:46 -08001472 hctx_lock(hctx, &srcu_idx);
1473 need_run = !blk_queue_quiesced(hctx->queue) &&
1474 blk_mq_hctx_has_pending(hctx);
1475 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001476
1477 if (need_run) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001478 __blk_mq_delay_run_hw_queue(hctx, async, 0);
1479 return true;
1480 }
1481
1482 return false;
Jens Axboe320ae512013-10-24 09:20:05 +01001483}
Omar Sandoval5b727272017-04-14 01:00:00 -07001484EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001485
Mike Snitzerb94ec292015-03-11 23:56:38 -04001486void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001487{
1488 struct blk_mq_hw_ctx *hctx;
1489 int i;
1490
1491 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001492 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001493 continue;
1494
Mike Snitzerb94ec292015-03-11 23:56:38 -04001495 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001496 }
1497}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001498EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001499
Bart Van Asschefd001442016-10-28 17:19:37 -07001500/**
1501 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1502 * @q: request queue.
1503 *
1504 * The caller is responsible for serializing this function against
1505 * blk_mq_{start,stop}_hw_queue().
1506 */
1507bool blk_mq_queue_stopped(struct request_queue *q)
1508{
1509 struct blk_mq_hw_ctx *hctx;
1510 int i;
1511
1512 queue_for_each_hw_ctx(q, hctx, i)
1513 if (blk_mq_hctx_stopped(hctx))
1514 return true;
1515
1516 return false;
1517}
1518EXPORT_SYMBOL(blk_mq_queue_stopped);
1519
Ming Lei39a70c72017-06-06 23:22:09 +08001520/*
1521 * This function is often used for pausing .queue_rq() by driver when
1522 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001523 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001524 *
1525 * We do not guarantee that dispatch can be drained or blocked
1526 * after blk_mq_stop_hw_queue() returns. Please use
1527 * blk_mq_quiesce_queue() for that requirement.
1528 */
Jens Axboe320ae512013-10-24 09:20:05 +01001529void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1530{
Ming Lei641a9ed2017-06-06 23:22:10 +08001531 cancel_delayed_work(&hctx->run_work);
1532
1533 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001534}
1535EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1536
Ming Lei39a70c72017-06-06 23:22:09 +08001537/*
1538 * This function is often used for pausing .queue_rq() by driver when
1539 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001540 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001541 *
1542 * We do not guarantee that dispatch can be drained or blocked
1543 * after blk_mq_stop_hw_queues() returns. Please use
1544 * blk_mq_quiesce_queue() for that requirement.
1545 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001546void blk_mq_stop_hw_queues(struct request_queue *q)
1547{
Ming Lei641a9ed2017-06-06 23:22:10 +08001548 struct blk_mq_hw_ctx *hctx;
1549 int i;
1550
1551 queue_for_each_hw_ctx(q, hctx, i)
1552 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001553}
1554EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1555
Jens Axboe320ae512013-10-24 09:20:05 +01001556void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1557{
1558 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001559
Jens Axboe0ffbce82014-06-25 08:22:34 -06001560 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001561}
1562EXPORT_SYMBOL(blk_mq_start_hw_queue);
1563
Christoph Hellwig2f268552014-04-16 09:44:56 +02001564void blk_mq_start_hw_queues(struct request_queue *q)
1565{
1566 struct blk_mq_hw_ctx *hctx;
1567 int i;
1568
1569 queue_for_each_hw_ctx(q, hctx, i)
1570 blk_mq_start_hw_queue(hctx);
1571}
1572EXPORT_SYMBOL(blk_mq_start_hw_queues);
1573
Jens Axboeae911c52016-12-08 13:19:30 -07001574void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1575{
1576 if (!blk_mq_hctx_stopped(hctx))
1577 return;
1578
1579 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1580 blk_mq_run_hw_queue(hctx, async);
1581}
1582EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1583
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001584void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001585{
1586 struct blk_mq_hw_ctx *hctx;
1587 int i;
1588
Jens Axboeae911c52016-12-08 13:19:30 -07001589 queue_for_each_hw_ctx(q, hctx, i)
1590 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001591}
1592EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1593
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001594static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001595{
1596 struct blk_mq_hw_ctx *hctx;
1597
Jens Axboe9f993732017-04-10 09:54:54 -06001598 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001599
1600 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001601 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001602 */
Ming Lei15fe8a902018-04-08 17:48:11 +08001603 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001604 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001605
Jens Axboe320ae512013-10-24 09:20:05 +01001606 __blk_mq_run_hw_queue(hctx);
1607}
1608
Ming Leicfd0c552015-10-20 23:13:57 +08001609static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001610 struct request *rq,
1611 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001612{
Jens Axboee57690f2016-08-24 15:34:35 -06001613 struct blk_mq_ctx *ctx = rq->mq_ctx;
1614
Bart Van Assche7b607812017-06-20 11:15:47 -07001615 lockdep_assert_held(&ctx->lock);
1616
Jens Axboe01b983c2013-11-19 18:59:10 -07001617 trace_block_rq_insert(hctx->queue, rq);
1618
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001619 if (at_head)
1620 list_add(&rq->queuelist, &ctx->rq_list);
1621 else
1622 list_add_tail(&rq->queuelist, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001623}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001624
Jens Axboe2c3ad662016-12-14 14:34:47 -07001625void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1626 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001627{
1628 struct blk_mq_ctx *ctx = rq->mq_ctx;
1629
Bart Van Assche7b607812017-06-20 11:15:47 -07001630 lockdep_assert_held(&ctx->lock);
1631
Jens Axboee57690f2016-08-24 15:34:35 -06001632 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001633 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001634}
1635
Jens Axboe157f3772017-09-11 16:43:57 -06001636/*
1637 * Should only be used carefully, when the caller knows we want to
1638 * bypass a potential IO scheduler on the target device.
1639 */
Ming Leib0850292017-11-02 23:24:34 +08001640void blk_mq_request_bypass_insert(struct request *rq, bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001641{
Jens Axboeea4f9952018-10-29 15:06:13 -06001642 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001643
1644 spin_lock(&hctx->lock);
1645 list_add_tail(&rq->queuelist, &hctx->dispatch);
1646 spin_unlock(&hctx->lock);
1647
Ming Leib0850292017-11-02 23:24:34 +08001648 if (run_queue)
1649 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001650}
1651
Jens Axboebd166ef2017-01-17 06:03:22 -07001652void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1653 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001654
1655{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001656 struct request *rq;
1657
Jens Axboe320ae512013-10-24 09:20:05 +01001658 /*
1659 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1660 * offline now
1661 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001662 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001663 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001664 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001665 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001666
1667 spin_lock(&ctx->lock);
1668 list_splice_tail_init(list, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001669 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001670 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001671}
1672
Jens Axboe3110fc72018-10-30 12:24:04 -06001673static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001674{
1675 struct request *rqa = container_of(a, struct request, queuelist);
1676 struct request *rqb = container_of(b, struct request, queuelist);
1677
Jens Axboe3110fc72018-10-30 12:24:04 -06001678 if (rqa->mq_ctx < rqb->mq_ctx)
1679 return -1;
1680 else if (rqa->mq_ctx > rqb->mq_ctx)
1681 return 1;
1682 else if (rqa->mq_hctx < rqb->mq_hctx)
1683 return -1;
1684 else if (rqa->mq_hctx > rqb->mq_hctx)
1685 return 1;
1686
1687 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001688}
1689
1690void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1691{
Jens Axboe67cae4c2018-10-30 11:31:51 -06001692 struct blk_mq_hw_ctx *this_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001693 struct blk_mq_ctx *this_ctx;
1694 struct request_queue *this_q;
1695 struct request *rq;
1696 LIST_HEAD(list);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001697 LIST_HEAD(rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001698 unsigned int depth;
1699
1700 list_splice_init(&plug->mq_list, &list);
Jens Axboe5f0ed772018-11-23 22:04:33 -07001701 plug->rq_count = 0;
Jens Axboe320ae512013-10-24 09:20:05 +01001702
Jens Axboece5b0092018-11-27 17:13:56 -07001703 if (plug->rq_count > 2 && plug->multiple_queues)
1704 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001705
1706 this_q = NULL;
Jens Axboe67cae4c2018-10-30 11:31:51 -06001707 this_hctx = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001708 this_ctx = NULL;
1709 depth = 0;
1710
1711 while (!list_empty(&list)) {
1712 rq = list_entry_rq(list.next);
1713 list_del_init(&rq->queuelist);
1714 BUG_ON(!rq->q);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001715 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx) {
1716 if (this_hctx) {
Ilya Dryomov587562d2018-09-26 14:35:50 +02001717 trace_block_unplug(this_q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001718 blk_mq_sched_insert_requests(this_hctx, this_ctx,
1719 &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001720 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001721 }
1722
Jens Axboe320ae512013-10-24 09:20:05 +01001723 this_q = rq->q;
Jens Axboe67cae4c2018-10-30 11:31:51 -06001724 this_ctx = rq->mq_ctx;
1725 this_hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001726 depth = 0;
1727 }
1728
1729 depth++;
Jens Axboe67cae4c2018-10-30 11:31:51 -06001730 list_add_tail(&rq->queuelist, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001731 }
1732
1733 /*
Jens Axboe67cae4c2018-10-30 11:31:51 -06001734 * If 'this_hctx' is set, we know we have entries to complete
1735 * on 'rq_list'. Do those.
Jens Axboe320ae512013-10-24 09:20:05 +01001736 */
Jens Axboe67cae4c2018-10-30 11:31:51 -06001737 if (this_hctx) {
Ilya Dryomov587562d2018-09-26 14:35:50 +02001738 trace_block_unplug(this_q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001739 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001740 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001741 }
1742}
1743
1744static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1745{
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001746 blk_init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001747
Jens Axboe6e85eaf2016-12-02 20:00:14 -07001748 blk_account_io_start(rq, true);
Jens Axboe320ae512013-10-24 09:20:05 +01001749}
1750
Jens Axboefd2d3322017-01-12 10:04:45 -07001751static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
1752{
Jens Axboebd166ef2017-01-17 06:03:22 -07001753 if (rq->tag != -1)
1754 return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
1755
1756 return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
Jens Axboefd2d3322017-01-12 10:04:45 -07001757}
1758
Mike Snitzer0f955492018-01-17 11:25:56 -05001759static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1760 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001761 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07001762{
Shaohua Lif984df12015-05-08 10:51:32 -07001763 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001764 struct blk_mq_queue_data bd = {
1765 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001766 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07001767 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001768 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001769 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001770
1771 new_cookie = request_to_qc_t(hctx, rq);
1772
1773 /*
1774 * For OK queue, we are done. For error, caller may kill it.
1775 * Any other error (busy), just add it to our list as we
1776 * previously would have done.
1777 */
1778 ret = q->mq_ops->queue_rq(hctx, &bd);
1779 switch (ret) {
1780 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001781 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001782 *cookie = new_cookie;
1783 break;
1784 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05001785 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001786 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05001787 __blk_mq_requeue_request(rq);
1788 break;
1789 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001790 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001791 *cookie = BLK_QC_T_NONE;
1792 break;
1793 }
1794
1795 return ret;
1796}
1797
Mike Snitzer0f955492018-01-17 11:25:56 -05001798static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1799 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05001800 blk_qc_t *cookie,
Jens Axboebe94f052018-11-24 10:15:46 -07001801 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05001802{
1803 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08001804 bool run_queue = true;
1805
Ming Lei23d4ee12018-01-18 12:06:59 +08001806 /*
1807 * RCU or SRCU read lock is needed before checking quiesced flag.
1808 *
1809 * When queue is stopped or quiesced, ignore 'bypass_insert' from
Bart Van Asschec77ff7f2018-01-19 08:58:54 -08001810 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
Ming Lei23d4ee12018-01-18 12:06:59 +08001811 * and avoid driver to try to dispatch again.
1812 */
Ming Leif4560ff2017-06-18 14:24:27 -06001813 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08001814 run_queue = false;
Ming Lei23d4ee12018-01-18 12:06:59 +08001815 bypass_insert = false;
Ming Leid964f042017-06-06 23:22:00 +08001816 goto insert;
1817 }
Shaohua Lif984df12015-05-08 10:51:32 -07001818
Ming Lei396eaf22018-01-17 11:25:57 -05001819 if (q->elevator && !bypass_insert)
Bart Van Assche2253efc2016-10-28 17:20:02 -07001820 goto insert;
1821
Ming Lei0bca7992018-04-05 00:35:21 +08001822 if (!blk_mq_get_dispatch_budget(hctx))
Jens Axboebd166ef2017-01-17 06:03:22 -07001823 goto insert;
1824
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001825 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001826 blk_mq_put_dispatch_budget(hctx);
Ming Leide148292017-10-14 17:22:29 +08001827 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08001828 }
Ming Leide148292017-10-14 17:22:29 +08001829
Jens Axboebe94f052018-11-24 10:15:46 -07001830 return __blk_mq_issue_directly(hctx, rq, cookie, last);
Bart Van Assche2253efc2016-10-28 17:20:02 -07001831insert:
Ming Lei396eaf22018-01-17 11:25:57 -05001832 if (bypass_insert)
1833 return BLK_STS_RESOURCE;
Mike Snitzer0f955492018-01-17 11:25:56 -05001834
Ming Lei23d4ee12018-01-18 12:06:59 +08001835 blk_mq_sched_insert_request(rq, false, run_queue, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001836 return BLK_STS_OK;
Shaohua Lif984df12015-05-08 10:51:32 -07001837}
1838
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001839static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1840 struct request *rq, blk_qc_t *cookie)
1841{
Mike Snitzer0f955492018-01-17 11:25:56 -05001842 blk_status_t ret;
Jens Axboe04ced152018-01-09 08:29:46 -08001843 int srcu_idx;
Jens Axboebf4907c2017-03-30 12:30:39 -06001844
Jens Axboe04ced152018-01-09 08:29:46 -08001845 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001846
Jens Axboe04ced152018-01-09 08:29:46 -08001847 hctx_lock(hctx, &srcu_idx);
Mike Snitzer0f955492018-01-17 11:25:56 -05001848
Jens Axboebe94f052018-11-24 10:15:46 -07001849 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
Ming Lei86ff7c22018-01-30 22:04:57 -05001850 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei23d4ee12018-01-18 12:06:59 +08001851 blk_mq_sched_insert_request(rq, false, true, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001852 else if (ret != BLK_STS_OK)
1853 blk_mq_end_request(rq, ret);
1854
Jens Axboe04ced152018-01-09 08:29:46 -08001855 hctx_unlock(hctx, srcu_idx);
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001856}
1857
Jens Axboebe94f052018-11-24 10:15:46 -07001858blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
Ming Lei396eaf22018-01-17 11:25:57 -05001859{
1860 blk_status_t ret;
1861 int srcu_idx;
1862 blk_qc_t unused_cookie;
Jens Axboeea4f9952018-10-29 15:06:13 -06001863 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Ming Lei396eaf22018-01-17 11:25:57 -05001864
1865 hctx_lock(hctx, &srcu_idx);
Jens Axboebe94f052018-11-24 10:15:46 -07001866 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
Ming Lei396eaf22018-01-17 11:25:57 -05001867 hctx_unlock(hctx, srcu_idx);
1868
1869 return ret;
Jens Axboe07068d52014-05-22 10:40:51 -06001870}
1871
Ming Lei6ce3dd62018-07-10 09:03:31 +08001872void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
1873 struct list_head *list)
1874{
1875 while (!list_empty(list)) {
1876 blk_status_t ret;
1877 struct request *rq = list_first_entry(list, struct request,
1878 queuelist);
1879
1880 list_del_init(&rq->queuelist);
Jens Axboebe94f052018-11-24 10:15:46 -07001881 ret = blk_mq_request_issue_directly(rq, list_empty(list));
Ming Lei6ce3dd62018-07-10 09:03:31 +08001882 if (ret != BLK_STS_OK) {
Ming Lei8824f622018-07-22 14:10:15 +08001883 if (ret == BLK_STS_RESOURCE ||
1884 ret == BLK_STS_DEV_RESOURCE) {
1885 list_add(&rq->queuelist, list);
1886 break;
1887 }
1888 blk_mq_end_request(rq, ret);
Ming Lei6ce3dd62018-07-10 09:03:31 +08001889 }
1890 }
Jens Axboed666ba92018-11-27 17:02:25 -07001891
1892 /*
1893 * If we didn't flush the entire list, we could have told
1894 * the driver there was more coming, but that turned out to
1895 * be a lie.
1896 */
1897 if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs)
1898 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08001899}
1900
Jens Axboece5b0092018-11-27 17:13:56 -07001901static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
1902{
1903 list_add_tail(&rq->queuelist, &plug->mq_list);
1904 plug->rq_count++;
1905 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
1906 struct request *tmp;
1907
1908 tmp = list_first_entry(&plug->mq_list, struct request,
1909 queuelist);
1910 if (tmp->q != rq->q)
1911 plug->multiple_queues = true;
1912 }
1913}
1914
Jens Axboedece1632015-11-05 10:41:16 -07001915static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001916{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001917 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001918 const int is_flush_fua = op_is_flush(bio->bi_opf);
Jens Axboef9afca42018-10-29 13:11:38 -06001919 struct blk_mq_alloc_data data = { .flags = 0, .cmd_flags = bio->bi_opf };
Jens Axboe07068d52014-05-22 10:40:51 -06001920 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001921 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001922 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001923 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001924
1925 blk_queue_bounce(q, &bio);
1926
NeilBrownaf67c312017-06-18 14:38:57 +10001927 blk_queue_split(q, &bio);
Wen Xiongf36ea502017-05-10 08:54:11 -05001928
Dmitry Monakhove23947b2017-06-29 11:31:11 -07001929 if (!bio_integrity_prep(bio))
Jens Axboedece1632015-11-05 10:41:16 -07001930 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001931
Omar Sandoval87c279e2016-06-01 22:18:48 -07001932 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Jens Axboe5f0ed772018-11-23 22:04:33 -07001933 blk_attempt_plug_merge(q, bio, &same_queue_rq))
Omar Sandoval87c279e2016-06-01 22:18:48 -07001934 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001935
Jens Axboebd166ef2017-01-17 06:03:22 -07001936 if (blk_mq_sched_bio_merge(q, bio))
1937 return BLK_QC_T_NONE;
1938
Christoph Hellwigd5337562018-11-14 17:02:09 +01001939 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07001940
Jens Axboef9afca42018-10-29 13:11:38 -06001941 rq = blk_mq_get_request(q, bio, &data);
Jens Axboe87760e52016-11-09 12:38:14 -07001942 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04001943 rq_qos_cleanup(q, bio);
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05001944 if (bio->bi_opf & REQ_NOWAIT)
1945 bio_wouldblock_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001946 return BLK_QC_T_NONE;
Jens Axboe87760e52016-11-09 12:38:14 -07001947 }
1948
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08001949 trace_block_getrq(q, bio, bio->bi_opf);
1950
Josef Bacikc1c80382018-07-03 11:14:59 -04001951 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001952
Jens Axboefd2d3322017-01-12 10:04:45 -07001953 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06001954
Shaohua Lif984df12015-05-08 10:51:32 -07001955 plug = current->plug;
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001956 if (unlikely(is_flush_fua)) {
Shaohua Lif984df12015-05-08 10:51:32 -07001957 blk_mq_put_ctx(data.ctx);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001958 blk_mq_bio_to_request(rq, bio);
Ming Lei923218f2017-11-02 23:24:38 +08001959
1960 /* bypass scheduler for flush rq */
1961 blk_insert_flush(rq);
1962 blk_mq_run_hw_queue(data.hctx, true);
Jens Axboeb2c5d162018-11-29 10:03:42 -07001963 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs)) {
1964 /*
1965 * Use plugging if we have a ->commit_rqs() hook as well, as
1966 * we know the driver uses bd->last in a smart fashion.
1967 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07001968 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07001969 struct request *last = NULL;
1970
Jens Axboeb00c53e2017-04-20 16:40:36 -06001971 blk_mq_put_ctx(data.ctx);
Jeff Moyere6c44382015-05-08 10:51:30 -07001972 blk_mq_bio_to_request(rq, bio);
Ming Lei0a6219a2016-11-16 18:07:05 +08001973
Ming Lei676d0602015-10-20 23:13:56 +08001974 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001975 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07001976 else
1977 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07001978
Shaohua Li600271d2016-11-03 17:03:54 -07001979 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
1980 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001981 blk_flush_plug_list(plug, false);
1982 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001983 }
Jens Axboeb094f892015-11-20 20:29:45 -07001984
Jens Axboece5b0092018-11-27 17:13:56 -07001985 blk_add_rq_to_plug(plug, rq);
Christoph Hellwig22997222017-03-22 15:01:52 -04001986 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01001987 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001988
Jens Axboe320ae512013-10-24 09:20:05 +01001989 /*
1990 * We do limited plugging. If the bio can be merged, do that.
1991 * Otherwise the existing request in the plug list will be
1992 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04001993 * The plug list might get flushed before this. If that happens,
1994 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01001995 */
Christoph Hellwig22997222017-03-22 15:01:52 -04001996 if (list_empty(&plug->mq_list))
1997 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07001998 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04001999 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002000 plug->rq_count--;
2001 }
Jens Axboece5b0092018-11-27 17:13:56 -07002002 blk_add_rq_to_plug(plug, rq);
Christoph Hellwig22997222017-03-22 15:01:52 -04002003
Jens Axboebf4907c2017-03-30 12:30:39 -06002004 blk_mq_put_ctx(data.ctx);
2005
Ming Leidad7a3b2017-06-06 23:21:59 +08002006 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002007 data.hctx = same_queue_rq->mq_hctx;
Christoph Hellwig22997222017-03-22 15:01:52 -04002008 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
2009 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002010 }
Ming Lei6ce3dd62018-07-10 09:03:31 +08002011 } else if ((q->nr_hw_queues > 1 && is_sync) || (!q->elevator &&
2012 !data.hctx->dispatch_busy)) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002013 blk_mq_put_ctx(data.ctx);
2014 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig22997222017-03-22 15:01:52 -04002015 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002016 } else {
Jens Axboeb00c53e2017-04-20 16:40:36 -06002017 blk_mq_put_ctx(data.ctx);
Ming Leiab42f352017-05-26 19:53:19 +08002018 blk_mq_bio_to_request(rq, bio);
huhai8fa9f552018-05-16 08:21:21 -06002019 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002020 }
Jens Axboe320ae512013-10-24 09:20:05 +01002021
Jens Axboe7b371632015-11-05 10:41:40 -07002022 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01002023}
2024
Jens Axboecc71a6f2017-01-11 14:29:56 -07002025void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2026 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002027{
2028 struct page *page;
2029
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002030 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002031 int i;
2032
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002033 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002034 struct request *rq = tags->static_rqs[i];
2035
2036 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002037 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002038 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002039 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002040 }
2041 }
2042
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002043 while (!list_empty(&tags->page_list)) {
2044 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002045 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002046 /*
2047 * Remove kmemleak object previously allocated in
2048 * blk_mq_init_rq_map().
2049 */
2050 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002051 __free_pages(page, page->private);
2052 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002053}
Jens Axboe320ae512013-10-24 09:20:05 +01002054
Jens Axboecc71a6f2017-01-11 14:29:56 -07002055void blk_mq_free_rq_map(struct blk_mq_tags *tags)
2056{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002057 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002058 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002059 kfree(tags->static_rqs);
2060 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002061
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002062 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002063}
2064
Jens Axboecc71a6f2017-01-11 14:29:56 -07002065struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2066 unsigned int hctx_idx,
2067 unsigned int nr_tags,
2068 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002069{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002070 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002071 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002072
Jens Axboeed76e322018-10-29 13:06:14 -06002073 node = blk_mq_hw_queue_to_node(&set->map[0], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002074 if (node == NUMA_NO_NODE)
2075 node = set->numa_node;
2076
2077 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
Shaohua Li24391c02015-01-23 14:18:00 -07002078 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002079 if (!tags)
2080 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002081
Kees Cook590b5b72018-06-12 14:04:20 -07002082 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002083 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002084 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002085 if (!tags->rqs) {
2086 blk_mq_free_tags(tags);
2087 return NULL;
2088 }
Jens Axboe320ae512013-10-24 09:20:05 +01002089
Kees Cook590b5b72018-06-12 14:04:20 -07002090 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2091 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2092 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002093 if (!tags->static_rqs) {
2094 kfree(tags->rqs);
2095 blk_mq_free_tags(tags);
2096 return NULL;
2097 }
2098
Jens Axboecc71a6f2017-01-11 14:29:56 -07002099 return tags;
2100}
2101
2102static size_t order_to_size(unsigned int order)
2103{
2104 return (size_t)PAGE_SIZE << order;
2105}
2106
Tejun Heo1d9bd512018-01-09 08:29:48 -08002107static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2108 unsigned int hctx_idx, int node)
2109{
2110 int ret;
2111
2112 if (set->ops->init_request) {
2113 ret = set->ops->init_request(set, rq, hctx_idx, node);
2114 if (ret)
2115 return ret;
2116 }
2117
Keith Busch12f5b932018-05-29 15:52:28 +02002118 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002119 return 0;
2120}
2121
Jens Axboecc71a6f2017-01-11 14:29:56 -07002122int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2123 unsigned int hctx_idx, unsigned int depth)
2124{
2125 unsigned int i, j, entries_per_page, max_order = 4;
2126 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002127 int node;
2128
Jens Axboeed76e322018-10-29 13:06:14 -06002129 node = blk_mq_hw_queue_to_node(&set->map[0], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002130 if (node == NUMA_NO_NODE)
2131 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002132
2133 INIT_LIST_HEAD(&tags->page_list);
2134
Jens Axboe320ae512013-10-24 09:20:05 +01002135 /*
2136 * rq_size is the size of the request plus driver payload, rounded
2137 * to the cacheline size
2138 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002139 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002140 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002141 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002142
Jens Axboecc71a6f2017-01-11 14:29:56 -07002143 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002144 int this_order = max_order;
2145 struct page *page;
2146 int to_do;
2147 void *p;
2148
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002149 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002150 this_order--;
2151
2152 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002153 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002154 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002155 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002156 if (page)
2157 break;
2158 if (!this_order--)
2159 break;
2160 if (order_to_size(this_order) < rq_size)
2161 break;
2162 } while (1);
2163
2164 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002165 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002166
2167 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002168 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002169
2170 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002171 /*
2172 * Allow kmemleak to scan these pages as they contain pointers
2173 * to additional allocations like via ops->init_request().
2174 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002175 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002176 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002177 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002178 left -= to_do * rq_size;
2179 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002180 struct request *rq = p;
2181
2182 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002183 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2184 tags->static_rqs[i] = NULL;
2185 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002186 }
2187
Jens Axboe320ae512013-10-24 09:20:05 +01002188 p += rq_size;
2189 i++;
2190 }
2191 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002192 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002193
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002194fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002195 blk_mq_free_rqs(set, tags, hctx_idx);
2196 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002197}
2198
Jens Axboee57690f2016-08-24 15:34:35 -06002199/*
2200 * 'cpu' is going away. splice any existing rq_list entries from this
2201 * software queue to the hw queue dispatch list, and ensure that it
2202 * gets run.
2203 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002204static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002205{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002206 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002207 struct blk_mq_ctx *ctx;
2208 LIST_HEAD(tmp);
2209
Thomas Gleixner9467f852016-09-22 08:05:17 -06002210 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Jens Axboee57690f2016-08-24 15:34:35 -06002211 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Jens Axboe484b4062014-05-21 14:01:15 -06002212
2213 spin_lock(&ctx->lock);
2214 if (!list_empty(&ctx->rq_list)) {
2215 list_splice_init(&ctx->rq_list, &tmp);
2216 blk_mq_hctx_clear_pending(hctx, ctx);
2217 }
2218 spin_unlock(&ctx->lock);
2219
2220 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002221 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002222
Jens Axboee57690f2016-08-24 15:34:35 -06002223 spin_lock(&hctx->lock);
2224 list_splice_tail_init(&tmp, &hctx->dispatch);
2225 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002226
2227 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002228 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002229}
2230
Thomas Gleixner9467f852016-09-22 08:05:17 -06002231static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002232{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002233 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2234 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002235}
2236
Ming Leic3b4afc2015-06-04 22:25:04 +08002237/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002238static void blk_mq_exit_hctx(struct request_queue *q,
2239 struct blk_mq_tag_set *set,
2240 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2241{
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002242 if (blk_mq_hw_queue_mapped(hctx))
2243 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002244
Ming Leif70ced02014-09-25 23:23:47 +08002245 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002246 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002247
Ming Lei08e98fc2014-09-25 23:23:38 +08002248 if (set->ops->exit_hctx)
2249 set->ops->exit_hctx(hctx, hctx_idx);
2250
Bart Van Assche6a83e742016-11-02 10:09:51 -06002251 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002252 cleanup_srcu_struct(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002253
Thomas Gleixner9467f852016-09-22 08:05:17 -06002254 blk_mq_remove_cpuhp(hctx);
Ming Leif70ced02014-09-25 23:23:47 +08002255 blk_free_flush_queue(hctx->fq);
Omar Sandoval88459642016-09-17 08:38:44 -06002256 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002257}
2258
Ming Lei624dbe42014-05-27 23:35:13 +08002259static void blk_mq_exit_hw_queues(struct request_queue *q,
2260 struct blk_mq_tag_set *set, int nr_queue)
2261{
2262 struct blk_mq_hw_ctx *hctx;
2263 unsigned int i;
2264
2265 queue_for_each_hw_ctx(q, hctx, i) {
2266 if (i == nr_queue)
2267 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002268 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002269 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002270 }
Ming Lei624dbe42014-05-27 23:35:13 +08002271}
2272
Ming Lei08e98fc2014-09-25 23:23:38 +08002273static int blk_mq_init_hctx(struct request_queue *q,
2274 struct blk_mq_tag_set *set,
2275 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2276{
2277 int node;
2278
2279 node = hctx->numa_node;
2280 if (node == NUMA_NO_NODE)
2281 node = hctx->numa_node = set->numa_node;
2282
Jens Axboe9f993732017-04-10 09:54:54 -06002283 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002284 spin_lock_init(&hctx->lock);
2285 INIT_LIST_HEAD(&hctx->dispatch);
2286 hctx->queue = q;
Jeff Moyer2404e602015-11-03 10:40:06 -05002287 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002288
Thomas Gleixner9467f852016-09-22 08:05:17 -06002289 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
Ming Lei08e98fc2014-09-25 23:23:38 +08002290
2291 hctx->tags = set->tags[hctx_idx];
2292
2293 /*
2294 * Allocate space for all possible cpus to avoid allocation at
2295 * runtime
2296 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002297 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Jianchao Wang5b202852018-10-12 18:07:26 +08002298 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002299 if (!hctx->ctxs)
2300 goto unregister_cpu_notifier;
2301
Jianchao Wang5b202852018-10-12 18:07:26 +08002302 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
2303 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002304 goto free_ctxs;
2305
2306 hctx->nr_ctx = 0;
2307
Ming Lei5815839b2018-06-25 19:31:47 +08002308 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002309 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2310 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2311
Ming Lei08e98fc2014-09-25 23:23:38 +08002312 if (set->ops->init_hctx &&
2313 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2314 goto free_bitmap;
2315
Jianchao Wang5b202852018-10-12 18:07:26 +08002316 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size,
2317 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
Ming Leif70ced02014-09-25 23:23:47 +08002318 if (!hctx->fq)
Jianchao Wangd48ece22018-08-21 15:15:03 +08002319 goto exit_hctx;
Ming Leif70ced02014-09-25 23:23:47 +08002320
Tejun Heo1d9bd512018-01-09 08:29:48 -08002321 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, node))
Ming Leif70ced02014-09-25 23:23:47 +08002322 goto free_fq;
2323
Bart Van Assche6a83e742016-11-02 10:09:51 -06002324 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002325 init_srcu_struct(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002326
Ming Lei08e98fc2014-09-25 23:23:38 +08002327 return 0;
2328
Ming Leif70ced02014-09-25 23:23:47 +08002329 free_fq:
2330 kfree(hctx->fq);
2331 exit_hctx:
2332 if (set->ops->exit_hctx)
2333 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002334 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002335 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002336 free_ctxs:
2337 kfree(hctx->ctxs);
2338 unregister_cpu_notifier:
Thomas Gleixner9467f852016-09-22 08:05:17 -06002339 blk_mq_remove_cpuhp(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002340 return -1;
2341}
2342
Jens Axboe320ae512013-10-24 09:20:05 +01002343static void blk_mq_init_cpu_queues(struct request_queue *q,
2344 unsigned int nr_hw_queues)
2345{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002346 struct blk_mq_tag_set *set = q->tag_set;
2347 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002348
2349 for_each_possible_cpu(i) {
2350 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2351 struct blk_mq_hw_ctx *hctx;
2352
Jens Axboe320ae512013-10-24 09:20:05 +01002353 __ctx->cpu = i;
2354 spin_lock_init(&__ctx->lock);
2355 INIT_LIST_HEAD(&__ctx->rq_list);
2356 __ctx->queue = q;
2357
Jens Axboe320ae512013-10-24 09:20:05 +01002358 /*
2359 * Set local node, IFF we have more than one hw queue. If
2360 * not, we remain on the home node of the device
2361 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002362 for (j = 0; j < set->nr_maps; j++) {
2363 hctx = blk_mq_map_queue_type(q, j, i);
2364 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2365 hctx->numa_node = local_memory_node(cpu_to_node(i));
2366 }
Jens Axboe320ae512013-10-24 09:20:05 +01002367 }
2368}
2369
Jens Axboecc71a6f2017-01-11 14:29:56 -07002370static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
2371{
2372 int ret = 0;
2373
2374 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2375 set->queue_depth, set->reserved_tags);
2376 if (!set->tags[hctx_idx])
2377 return false;
2378
2379 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2380 set->queue_depth);
2381 if (!ret)
2382 return true;
2383
2384 blk_mq_free_rq_map(set->tags[hctx_idx]);
2385 set->tags[hctx_idx] = NULL;
2386 return false;
2387}
2388
2389static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2390 unsigned int hctx_idx)
2391{
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002392 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002393 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2394 blk_mq_free_rq_map(set->tags[hctx_idx]);
2395 set->tags[hctx_idx] = NULL;
2396 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002397}
2398
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002399static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002400{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002401 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002402 struct blk_mq_hw_ctx *hctx;
2403 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002404 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002405
Akinobu Mita60de0742015-09-27 02:09:25 +09002406 /*
2407 * Avoid others reading imcomplete hctx->cpumask through sysfs
2408 */
2409 mutex_lock(&q->sysfs_lock);
2410
Jens Axboe320ae512013-10-24 09:20:05 +01002411 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002412 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002413 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002414 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002415 }
2416
2417 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002418 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002419 *
2420 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002421 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002422 for_each_possible_cpu(i) {
Jens Axboeed76e322018-10-29 13:06:14 -06002423 hctx_idx = set->map[0].mq_map[i];
Ming Lei4412efe2018-04-25 04:01:44 +08002424 /* unmapped hw queue can be remapped after CPU topo changed */
2425 if (!set->tags[hctx_idx] &&
2426 !__blk_mq_alloc_rq_map(set, hctx_idx)) {
2427 /*
2428 * If tags initialization fail for some hctx,
2429 * that hctx won't be brought online. In this
2430 * case, remap the current ctx to hctx[0] which
2431 * is guaranteed to always have tags allocated
2432 */
Jens Axboeed76e322018-10-29 13:06:14 -06002433 set->map[0].mq_map[i] = 0;
Ming Lei4412efe2018-04-25 04:01:44 +08002434 }
2435
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002436 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002437 for (j = 0; j < set->nr_maps; j++) {
2438 hctx = blk_mq_map_queue_type(q, j, i);
Jens Axboef31967f2018-10-29 13:13:29 -06002439
Jens Axboeb3c661b2018-10-30 10:36:06 -06002440 /*
2441 * If the CPU is already set in the mask, then we've
2442 * mapped this one already. This can happen if
2443 * devices share queues across queue maps.
2444 */
2445 if (cpumask_test_cpu(i, hctx->cpumask))
2446 continue;
2447
2448 cpumask_set_cpu(i, hctx->cpumask);
2449 hctx->type = j;
2450 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2451 hctx->ctxs[hctx->nr_ctx++] = ctx;
2452
2453 /*
2454 * If the nr_ctx type overflows, we have exceeded the
2455 * amount of sw queues we can support.
2456 */
2457 BUG_ON(!hctx->nr_ctx);
2458 }
Jens Axboe320ae512013-10-24 09:20:05 +01002459 }
Jens Axboe506e9312014-05-07 10:26:44 -06002460
Akinobu Mita60de0742015-09-27 02:09:25 +09002461 mutex_unlock(&q->sysfs_lock);
2462
Jens Axboe506e9312014-05-07 10:26:44 -06002463 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002464 /*
2465 * If no software queues are mapped to this hardware queue,
2466 * disable it and free the request entries.
2467 */
2468 if (!hctx->nr_ctx) {
2469 /* Never unmap queue 0. We need it as a
2470 * fallback in case of a new remap fails
2471 * allocation
2472 */
2473 if (i && set->tags[i])
2474 blk_mq_free_map_and_requests(set, i);
2475
2476 hctx->tags = NULL;
2477 continue;
2478 }
Jens Axboe484b4062014-05-21 14:01:15 -06002479
Ming Lei2a34c082015-04-21 10:00:20 +08002480 hctx->tags = set->tags[i];
2481 WARN_ON(!hctx->tags);
2482
Jens Axboe484b4062014-05-21 14:01:15 -06002483 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002484 * Set the map size to the number of mapped software queues.
2485 * This is more accurate and more efficient than looping
2486 * over all possibly mapped software queues.
2487 */
Omar Sandoval88459642016-09-17 08:38:44 -06002488 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002489
2490 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002491 * Initialize batch roundrobin counts
2492 */
Ming Leif82ddf12018-04-08 17:48:10 +08002493 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06002494 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2495 }
Jens Axboe320ae512013-10-24 09:20:05 +01002496}
2497
Jens Axboe8e8320c2017-06-20 17:56:13 -06002498/*
2499 * Caller needs to ensure that we're either frozen/quiesced, or that
2500 * the queue isn't live yet.
2501 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002502static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002503{
2504 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002505 int i;
2506
Jeff Moyer2404e602015-11-03 10:40:06 -05002507 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08002508 if (shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002509 hctx->flags |= BLK_MQ_F_TAG_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08002510 else
Jeff Moyer2404e602015-11-03 10:40:06 -05002511 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2512 }
2513}
2514
Jens Axboe8e8320c2017-06-20 17:56:13 -06002515static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2516 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002517{
2518 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002519
Bart Van Assche705cda92017-04-07 11:16:49 -07002520 lockdep_assert_held(&set->tag_list_lock);
2521
Jens Axboe0d2602c2014-05-13 15:10:52 -06002522 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2523 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002524 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002525 blk_mq_unfreeze_queue(q);
2526 }
2527}
2528
2529static void blk_mq_del_queue_tag_set(struct request_queue *q)
2530{
2531 struct blk_mq_tag_set *set = q->tag_set;
2532
Jens Axboe0d2602c2014-05-13 15:10:52 -06002533 mutex_lock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002534 list_del_rcu(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002535 if (list_is_singular(&set->tag_list)) {
2536 /* just transitioned to unshared */
2537 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2538 /* update existing queue */
2539 blk_mq_update_tag_set_depth(set, false);
2540 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002541 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02002542 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002543}
2544
2545static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2546 struct request_queue *q)
2547{
Jens Axboe0d2602c2014-05-13 15:10:52 -06002548 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002549
Jens Axboeff821d22017-11-10 22:05:12 -07002550 /*
2551 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2552 */
2553 if (!list_empty(&set->tag_list) &&
2554 !(set->flags & BLK_MQ_F_TAG_SHARED)) {
Jeff Moyer2404e602015-11-03 10:40:06 -05002555 set->flags |= BLK_MQ_F_TAG_SHARED;
2556 /* update existing queue */
2557 blk_mq_update_tag_set_depth(set, true);
2558 }
2559 if (set->flags & BLK_MQ_F_TAG_SHARED)
2560 queue_set_hctx_shared(q, true);
Bart Van Assche705cda92017-04-07 11:16:49 -07002561 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002562
Jens Axboe0d2602c2014-05-13 15:10:52 -06002563 mutex_unlock(&set->tag_list_lock);
2564}
2565
Ming Lei1db49092018-11-20 09:44:35 +08002566/* All allocations will be freed in release handler of q->mq_kobj */
2567static int blk_mq_alloc_ctxs(struct request_queue *q)
2568{
2569 struct blk_mq_ctxs *ctxs;
2570 int cpu;
2571
2572 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
2573 if (!ctxs)
2574 return -ENOMEM;
2575
2576 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2577 if (!ctxs->queue_ctx)
2578 goto fail;
2579
2580 for_each_possible_cpu(cpu) {
2581 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
2582 ctx->ctxs = ctxs;
2583 }
2584
2585 q->mq_kobj = &ctxs->kobj;
2586 q->queue_ctx = ctxs->queue_ctx;
2587
2588 return 0;
2589 fail:
2590 kfree(ctxs);
2591 return -ENOMEM;
2592}
2593
Ming Leie09aae72015-01-29 20:17:27 +08002594/*
2595 * It is the actual release handler for mq, but we do it from
2596 * request queue's release handler for avoiding use-after-free
2597 * and headache because q->mq_kobj shouldn't have been introduced,
2598 * but we can't group ctx/kctx kobj without it.
2599 */
2600void blk_mq_release(struct request_queue *q)
2601{
2602 struct blk_mq_hw_ctx *hctx;
2603 unsigned int i;
2604
2605 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08002606 queue_for_each_hw_ctx(q, hctx, i) {
2607 if (!hctx)
2608 continue;
Ming Lei6c8b2322017-02-22 18:14:01 +08002609 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002610 }
Ming Leie09aae72015-01-29 20:17:27 +08002611
2612 kfree(q->queue_hw_ctx);
2613
Ming Lei7ea5fe32017-02-22 18:14:00 +08002614 /*
2615 * release .mq_kobj and sw queue's kobject now because
2616 * both share lifetime with request queue.
2617 */
2618 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08002619}
2620
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002621struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01002622{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002623 struct request_queue *uninit_q, *q;
2624
Christoph Hellwig6d469642018-11-14 17:02:18 +01002625 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002626 if (!uninit_q)
2627 return ERR_PTR(-ENOMEM);
2628
2629 q = blk_mq_init_allocated_queue(set, uninit_q);
2630 if (IS_ERR(q))
2631 blk_cleanup_queue(uninit_q);
2632
2633 return q;
2634}
2635EXPORT_SYMBOL(blk_mq_init_queue);
2636
Jens Axboe9316a9e2018-10-15 08:40:37 -06002637/*
2638 * Helper for setting up a queue with mq ops, given queue depth, and
2639 * the passed in mq ops flags.
2640 */
2641struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
2642 const struct blk_mq_ops *ops,
2643 unsigned int queue_depth,
2644 unsigned int set_flags)
2645{
2646 struct request_queue *q;
2647 int ret;
2648
2649 memset(set, 0, sizeof(*set));
2650 set->ops = ops;
2651 set->nr_hw_queues = 1;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002652 set->nr_maps = 1;
Jens Axboe9316a9e2018-10-15 08:40:37 -06002653 set->queue_depth = queue_depth;
2654 set->numa_node = NUMA_NO_NODE;
2655 set->flags = set_flags;
2656
2657 ret = blk_mq_alloc_tag_set(set);
2658 if (ret)
2659 return ERR_PTR(ret);
2660
2661 q = blk_mq_init_queue(set);
2662 if (IS_ERR(q)) {
2663 blk_mq_free_tag_set(set);
2664 return q;
2665 }
2666
2667 return q;
2668}
2669EXPORT_SYMBOL(blk_mq_init_sq_queue);
2670
Bart Van Assche07319672017-06-20 11:15:38 -07002671static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2672{
2673 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2674
Tejun Heo05707b62018-01-09 08:29:53 -08002675 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
Bart Van Assche07319672017-06-20 11:15:38 -07002676 __alignof__(struct blk_mq_hw_ctx)) !=
2677 sizeof(struct blk_mq_hw_ctx));
2678
2679 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2680 hw_ctx_size += sizeof(struct srcu_struct);
2681
2682 return hw_ctx_size;
2683}
2684
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002685static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
2686 struct blk_mq_tag_set *set, struct request_queue *q,
2687 int hctx_idx, int node)
2688{
2689 struct blk_mq_hw_ctx *hctx;
2690
2691 hctx = kzalloc_node(blk_mq_hw_ctx_size(set),
2692 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2693 node);
2694 if (!hctx)
2695 return NULL;
2696
2697 if (!zalloc_cpumask_var_node(&hctx->cpumask,
2698 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2699 node)) {
2700 kfree(hctx);
2701 return NULL;
2702 }
2703
2704 atomic_set(&hctx->nr_active, 0);
2705 hctx->numa_node = node;
2706 hctx->queue_num = hctx_idx;
2707
2708 if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) {
2709 free_cpumask_var(hctx->cpumask);
2710 kfree(hctx);
2711 return NULL;
2712 }
2713 blk_mq_hctx_kobj_init(hctx);
2714
2715 return hctx;
2716}
2717
Keith Busch868f2f02015-12-17 17:08:14 -07002718static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2719 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002720{
Jianchao Wange01ad462018-10-12 18:07:28 +08002721 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07002722 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01002723
Ming Leifb350e02018-01-06 16:27:40 +08002724 /* protect against switching io scheduler */
2725 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002726 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002727 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002728 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07002729
Jens Axboeed76e322018-10-29 13:06:14 -06002730 node = blk_mq_hw_queue_to_node(&set->map[0], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002731 /*
2732 * If the hw queue has been mapped to another numa node,
2733 * we need to realloc the hctx. If allocation fails, fallback
2734 * to use the previous one.
2735 */
2736 if (hctxs[i] && (hctxs[i]->numa_node == node))
2737 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01002738
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002739 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
2740 if (hctx) {
2741 if (hctxs[i]) {
2742 blk_mq_exit_hctx(q, set, hctxs[i], i);
2743 kobject_put(&hctxs[i]->kobj);
2744 }
2745 hctxs[i] = hctx;
2746 } else {
2747 if (hctxs[i])
2748 pr_warn("Allocate new hctx on node %d fails,\
2749 fallback to previous one on node %d\n",
2750 node, hctxs[i]->numa_node);
2751 else
2752 break;
Keith Busch868f2f02015-12-17 17:08:14 -07002753 }
Jens Axboe320ae512013-10-24 09:20:05 +01002754 }
Jianchao Wange01ad462018-10-12 18:07:28 +08002755 /*
2756 * Increasing nr_hw_queues fails. Free the newly allocated
2757 * hctxs and keep the previous q->nr_hw_queues.
2758 */
2759 if (i != set->nr_hw_queues) {
2760 j = q->nr_hw_queues;
2761 end = i;
2762 } else {
2763 j = i;
2764 end = q->nr_hw_queues;
2765 q->nr_hw_queues = set->nr_hw_queues;
2766 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002767
Jianchao Wange01ad462018-10-12 18:07:28 +08002768 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002769 struct blk_mq_hw_ctx *hctx = hctxs[j];
2770
2771 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07002772 if (hctx->tags)
2773 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002774 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002775 kobject_put(&hctx->kobj);
Keith Busch868f2f02015-12-17 17:08:14 -07002776 hctxs[j] = NULL;
2777
2778 }
2779 }
Ming Leifb350e02018-01-06 16:27:40 +08002780 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07002781}
2782
Jens Axboe392546a2018-10-29 13:25:27 -06002783/*
2784 * Maximum number of hardware queues we support. For single sets, we'll never
2785 * have more than the CPUs (software queues). For multiple sets, the tag_set
2786 * user may have set ->nr_hw_queues larger.
2787 */
2788static unsigned int nr_hw_queues(struct blk_mq_tag_set *set)
2789{
2790 if (set->nr_maps == 1)
2791 return nr_cpu_ids;
2792
2793 return max(set->nr_hw_queues, nr_cpu_ids);
2794}
2795
Keith Busch868f2f02015-12-17 17:08:14 -07002796struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2797 struct request_queue *q)
2798{
Ming Lei66841672016-02-12 15:27:00 +08002799 /* mark the queue as mq asap */
2800 q->mq_ops = set->ops;
2801
Omar Sandoval34dbad52017-03-21 08:56:08 -07002802 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06002803 blk_mq_poll_stats_bkt,
2804 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07002805 if (!q->poll_cb)
2806 goto err_exit;
2807
Ming Lei1db49092018-11-20 09:44:35 +08002808 if (blk_mq_alloc_ctxs(q))
Ming Linc7de5722016-05-25 23:23:27 -07002809 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002810
Ming Lei737f98c2017-02-22 18:13:59 +08002811 /* init q->mq_kobj and sw queues' kobjects */
2812 blk_mq_sysfs_init(q);
2813
Jens Axboe392546a2018-10-29 13:25:27 -06002814 q->nr_queues = nr_hw_queues(set);
2815 q->queue_hw_ctx = kcalloc_node(q->nr_queues, sizeof(*(q->queue_hw_ctx)),
Keith Busch868f2f02015-12-17 17:08:14 -07002816 GFP_KERNEL, set->numa_node);
2817 if (!q->queue_hw_ctx)
Ming Lei1db49092018-11-20 09:44:35 +08002818 goto err_sys_init;
Keith Busch868f2f02015-12-17 17:08:14 -07002819
Keith Busch868f2f02015-12-17 17:08:14 -07002820 blk_mq_realloc_hw_ctxs(set, q);
2821 if (!q->nr_hw_queues)
2822 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002823
Christoph Hellwig287922e2015-10-30 20:57:30 +08002824 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002825 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002826
Jens Axboea8908932018-10-16 14:23:06 -06002827 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01002828
Jens Axboe94eddfb2013-11-19 09:25:07 -07002829 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002830
Jens Axboe05f1dd52014-05-29 09:53:32 -06002831 if (!(set->flags & BLK_MQ_F_SG_MERGE))
Christoph Hellwig57d74df2018-11-14 17:02:07 +01002832 blk_queue_flag_set(QUEUE_FLAG_NO_SG_MERGE, q);
Jens Axboe05f1dd52014-05-29 09:53:32 -06002833
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002834 q->sg_reserved_size = INT_MAX;
2835
Mike Snitzer28494502016-09-14 13:28:30 -04002836 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002837 INIT_LIST_HEAD(&q->requeue_list);
2838 spin_lock_init(&q->requeue_lock);
2839
Christoph Hellwig254d2592017-03-22 15:01:50 -04002840 blk_queue_make_request(q, blk_mq_make_request);
Christoph Hellwigea435e12017-11-02 21:29:54 +03002841 if (q->mq_ops->poll)
2842 q->poll_fn = blk_mq_poll;
Jens Axboe07068d52014-05-22 10:40:51 -06002843
Jens Axboeeba71762014-05-20 15:17:27 -06002844 /*
2845 * Do this after blk_queue_make_request() overrides it...
2846 */
2847 q->nr_requests = set->queue_depth;
2848
Jens Axboe64f1c212016-11-14 13:03:03 -07002849 /*
2850 * Default to classic polling
2851 */
2852 q->poll_nsec = -1;
2853
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002854 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002855 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002856 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002857
Jens Axboed3484992017-01-13 14:43:58 -07002858 if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
2859 int ret;
2860
Christoph Hellwig131d08e2018-05-31 19:11:40 +02002861 ret = elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07002862 if (ret)
2863 return ERR_PTR(ret);
2864 }
2865
Jens Axboe320ae512013-10-24 09:20:05 +01002866 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002867
Jens Axboe320ae512013-10-24 09:20:05 +01002868err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002869 kfree(q->queue_hw_ctx);
Ming Lei1db49092018-11-20 09:44:35 +08002870err_sys_init:
2871 blk_mq_sysfs_deinit(q);
Ming Linc7de5722016-05-25 23:23:27 -07002872err_exit:
2873 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002874 return ERR_PTR(-ENOMEM);
2875}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002876EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002877
2878void blk_mq_free_queue(struct request_queue *q)
2879{
Ming Lei624dbe42014-05-27 23:35:13 +08002880 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002881
Jens Axboe0d2602c2014-05-13 15:10:52 -06002882 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08002883 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002884}
Jens Axboe320ae512013-10-24 09:20:05 +01002885
Jens Axboea5164402014-09-10 09:02:03 -06002886static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2887{
2888 int i;
2889
Jens Axboecc71a6f2017-01-11 14:29:56 -07002890 for (i = 0; i < set->nr_hw_queues; i++)
2891 if (!__blk_mq_alloc_rq_map(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06002892 goto out_unwind;
Jens Axboea5164402014-09-10 09:02:03 -06002893
2894 return 0;
2895
2896out_unwind:
2897 while (--i >= 0)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002898 blk_mq_free_rq_map(set->tags[i]);
Jens Axboea5164402014-09-10 09:02:03 -06002899
Jens Axboea5164402014-09-10 09:02:03 -06002900 return -ENOMEM;
2901}
2902
2903/*
2904 * Allocate the request maps associated with this tag_set. Note that this
2905 * may reduce the depth asked for, if memory is tight. set->queue_depth
2906 * will be updated to reflect the allocated depth.
2907 */
2908static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2909{
2910 unsigned int depth;
2911 int err;
2912
2913 depth = set->queue_depth;
2914 do {
2915 err = __blk_mq_alloc_rq_maps(set);
2916 if (!err)
2917 break;
2918
2919 set->queue_depth >>= 1;
2920 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2921 err = -ENOMEM;
2922 break;
2923 }
2924 } while (set->queue_depth);
2925
2926 if (!set->queue_depth || err) {
2927 pr_err("blk-mq: failed to allocate request map\n");
2928 return -ENOMEM;
2929 }
2930
2931 if (depth != set->queue_depth)
2932 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2933 depth, set->queue_depth);
2934
2935 return 0;
2936}
2937
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002938static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
2939{
Ming Lei7d4901a2018-01-06 16:27:39 +08002940 if (set->ops->map_queues) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06002941 int i;
2942
Ming Lei7d4901a2018-01-06 16:27:39 +08002943 /*
2944 * transport .map_queues is usually done in the following
2945 * way:
2946 *
2947 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
2948 * mask = get_cpu_mask(queue)
2949 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06002950 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08002951 * }
2952 *
2953 * When we need to remap, the table has to be cleared for
2954 * killing stale mapping since one CPU may not be mapped
2955 * to any hw queue.
2956 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002957 for (i = 0; i < set->nr_maps; i++)
2958 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08002959
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002960 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002961 } else {
2962 BUG_ON(set->nr_maps > 1);
Jens Axboeed76e322018-10-29 13:06:14 -06002963 return blk_mq_map_queues(&set->map[0]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002964 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002965}
2966
Jens Axboea4391c62014-06-05 15:21:56 -06002967/*
2968 * Alloc a tag set to be associated with one or more request queues.
2969 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09002970 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06002971 * value will be stored in set->queue_depth.
2972 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002973int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2974{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002975 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002976
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002977 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2978
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002979 if (!set->nr_hw_queues)
2980 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002981 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002982 return -EINVAL;
2983 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2984 return -EINVAL;
2985
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002986 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002987 return -EINVAL;
2988
Ming Leide148292017-10-14 17:22:29 +08002989 if (!set->ops->get_budget ^ !set->ops->put_budget)
2990 return -EINVAL;
2991
Jens Axboea4391c62014-06-05 15:21:56 -06002992 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2993 pr_info("blk-mq: reduced tag depth to %u\n",
2994 BLK_MQ_MAX_DEPTH);
2995 set->queue_depth = BLK_MQ_MAX_DEPTH;
2996 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002997
Jens Axboeb3c661b2018-10-30 10:36:06 -06002998 if (!set->nr_maps)
2999 set->nr_maps = 1;
3000 else if (set->nr_maps > HCTX_MAX_TYPES)
3001 return -EINVAL;
3002
Shaohua Li6637fad2014-11-30 16:00:58 -08003003 /*
3004 * If a crashdump is active, then we are potentially in a very
3005 * memory constrained environment. Limit us to 1 queue and
3006 * 64 tags to prevent using too much memory.
3007 */
3008 if (is_kdump_kernel()) {
3009 set->nr_hw_queues = 1;
3010 set->queue_depth = min(64U, set->queue_depth);
3011 }
Keith Busch868f2f02015-12-17 17:08:14 -07003012 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003013 * There is no use for more h/w queues than cpus if we just have
3014 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003015 */
Jens Axboe392546a2018-10-29 13:25:27 -06003016 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003017 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003018
Jens Axboe392546a2018-10-29 13:25:27 -06003019 set->tags = kcalloc_node(nr_hw_queues(set), sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003020 GFP_KERNEL, set->numa_node);
3021 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06003022 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003023
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003024 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003025 for (i = 0; i < set->nr_maps; i++) {
3026 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
3027 sizeof(struct blk_mq_queue_map),
3028 GFP_KERNEL, set->numa_node);
3029 if (!set->map[i].mq_map)
3030 goto out_free_mq_map;
3031 set->map[i].nr_queues = set->nr_hw_queues;
3032 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003033
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003034 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003035 if (ret)
3036 goto out_free_mq_map;
3037
3038 ret = blk_mq_alloc_rq_maps(set);
3039 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003040 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003041
Jens Axboe0d2602c2014-05-13 15:10:52 -06003042 mutex_init(&set->tag_list_lock);
3043 INIT_LIST_HEAD(&set->tag_list);
3044
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003045 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003046
3047out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003048 for (i = 0; i < set->nr_maps; i++) {
3049 kfree(set->map[i].mq_map);
3050 set->map[i].mq_map = NULL;
3051 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003052 kfree(set->tags);
3053 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003054 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003055}
3056EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3057
3058void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3059{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003060 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003061
Jens Axboe392546a2018-10-29 13:25:27 -06003062 for (i = 0; i < nr_hw_queues(set); i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003063 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003064
Jens Axboeb3c661b2018-10-30 10:36:06 -06003065 for (j = 0; j < set->nr_maps; j++) {
3066 kfree(set->map[j].mq_map);
3067 set->map[j].mq_map = NULL;
3068 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003069
Ming Lei981bd182014-04-24 00:07:34 +08003070 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003071 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003072}
3073EXPORT_SYMBOL(blk_mq_free_tag_set);
3074
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003075int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3076{
3077 struct blk_mq_tag_set *set = q->tag_set;
3078 struct blk_mq_hw_ctx *hctx;
3079 int i, ret;
3080
Jens Axboebd166ef2017-01-17 06:03:22 -07003081 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003082 return -EINVAL;
3083
Jens Axboe70f36b62017-01-19 10:59:07 -07003084 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003085 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003086
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003087 ret = 0;
3088 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003089 if (!hctx->tags)
3090 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003091 /*
3092 * If we're using an MQ scheduler, just update the scheduler
3093 * queue depth. This is similar to what the old code would do.
3094 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003095 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003096 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003097 false);
3098 } else {
3099 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3100 nr, true);
3101 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003102 if (ret)
3103 break;
3104 }
3105
3106 if (!ret)
3107 q->nr_requests = nr;
3108
Ming Lei24f5a902018-01-06 16:27:38 +08003109 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003110 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003111
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003112 return ret;
3113}
3114
Jianchao Wangd48ece22018-08-21 15:15:03 +08003115/*
3116 * request_queue and elevator_type pair.
3117 * It is just used by __blk_mq_update_nr_hw_queues to cache
3118 * the elevator_type associated with a request_queue.
3119 */
3120struct blk_mq_qe_pair {
3121 struct list_head node;
3122 struct request_queue *q;
3123 struct elevator_type *type;
3124};
3125
3126/*
3127 * Cache the elevator_type in qe pair list and switch the
3128 * io scheduler to 'none'
3129 */
3130static bool blk_mq_elv_switch_none(struct list_head *head,
3131 struct request_queue *q)
3132{
3133 struct blk_mq_qe_pair *qe;
3134
3135 if (!q->elevator)
3136 return true;
3137
3138 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3139 if (!qe)
3140 return false;
3141
3142 INIT_LIST_HEAD(&qe->node);
3143 qe->q = q;
3144 qe->type = q->elevator->type;
3145 list_add(&qe->node, head);
3146
3147 mutex_lock(&q->sysfs_lock);
3148 /*
3149 * After elevator_switch_mq, the previous elevator_queue will be
3150 * released by elevator_release. The reference of the io scheduler
3151 * module get by elevator_get will also be put. So we need to get
3152 * a reference of the io scheduler module here to prevent it to be
3153 * removed.
3154 */
3155 __module_get(qe->type->elevator_owner);
3156 elevator_switch_mq(q, NULL);
3157 mutex_unlock(&q->sysfs_lock);
3158
3159 return true;
3160}
3161
3162static void blk_mq_elv_switch_back(struct list_head *head,
3163 struct request_queue *q)
3164{
3165 struct blk_mq_qe_pair *qe;
3166 struct elevator_type *t = NULL;
3167
3168 list_for_each_entry(qe, head, node)
3169 if (qe->q == q) {
3170 t = qe->type;
3171 break;
3172 }
3173
3174 if (!t)
3175 return;
3176
3177 list_del(&qe->node);
3178 kfree(qe);
3179
3180 mutex_lock(&q->sysfs_lock);
3181 elevator_switch_mq(q, t);
3182 mutex_unlock(&q->sysfs_lock);
3183}
3184
Keith Busche4dc2b32017-05-30 14:39:11 -04003185static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3186 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003187{
3188 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003189 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003190 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003191
Bart Van Assche705cda92017-04-07 11:16:49 -07003192 lockdep_assert_held(&set->tag_list_lock);
3193
Jens Axboe392546a2018-10-29 13:25:27 -06003194 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003195 nr_hw_queues = nr_cpu_ids;
3196 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
3197 return;
3198
3199 list_for_each_entry(q, &set->tag_list, tag_set_list)
3200 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003201 /*
Jianchao Wangf5bbbbe2018-08-21 15:15:04 +08003202 * Sync with blk_mq_queue_tag_busy_iter.
3203 */
3204 synchronize_rcu();
3205 /*
Jianchao Wangd48ece22018-08-21 15:15:03 +08003206 * Switch IO scheduler to 'none', cleaning up the data associated
3207 * with the previous scheduler. We will switch back once we are done
3208 * updating the new sw to hw queue mappings.
3209 */
3210 list_for_each_entry(q, &set->tag_list, tag_set_list)
3211 if (!blk_mq_elv_switch_none(&head, q))
3212 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003213
Jianchao Wang477e19d2018-10-12 18:07:25 +08003214 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3215 blk_mq_debugfs_unregister_hctxs(q);
3216 blk_mq_sysfs_unregister(q);
3217 }
3218
Jianchao Wange01ad462018-10-12 18:07:28 +08003219 prev_nr_hw_queues = set->nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003220 set->nr_hw_queues = nr_hw_queues;
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003221 blk_mq_update_queue_map(set);
Jianchao Wange01ad462018-10-12 18:07:28 +08003222fallback:
Keith Busch868f2f02015-12-17 17:08:14 -07003223 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3224 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003225 if (q->nr_hw_queues != set->nr_hw_queues) {
3226 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3227 nr_hw_queues, prev_nr_hw_queues);
3228 set->nr_hw_queues = prev_nr_hw_queues;
Jens Axboeed76e322018-10-29 13:06:14 -06003229 blk_mq_map_queues(&set->map[0]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003230 goto fallback;
3231 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003232 blk_mq_map_swqueue(q);
3233 }
3234
3235 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3236 blk_mq_sysfs_register(q);
3237 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003238 }
3239
Jianchao Wangd48ece22018-08-21 15:15:03 +08003240switch_back:
3241 list_for_each_entry(q, &set->tag_list, tag_set_list)
3242 blk_mq_elv_switch_back(&head, q);
3243
Keith Busch868f2f02015-12-17 17:08:14 -07003244 list_for_each_entry(q, &set->tag_list, tag_set_list)
3245 blk_mq_unfreeze_queue(q);
3246}
Keith Busche4dc2b32017-05-30 14:39:11 -04003247
3248void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3249{
3250 mutex_lock(&set->tag_list_lock);
3251 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3252 mutex_unlock(&set->tag_list_lock);
3253}
Keith Busch868f2f02015-12-17 17:08:14 -07003254EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3255
Omar Sandoval34dbad52017-03-21 08:56:08 -07003256/* Enable polling stats and return whether they were already enabled. */
3257static bool blk_poll_stats_enable(struct request_queue *q)
3258{
3259 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003260 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003261 return true;
3262 blk_stat_add_callback(q, q->poll_cb);
3263 return false;
3264}
3265
3266static void blk_mq_poll_stats_start(struct request_queue *q)
3267{
3268 /*
3269 * We don't arm the callback if polling stats are not enabled or the
3270 * callback is already active.
3271 */
3272 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3273 blk_stat_is_active(q->poll_cb))
3274 return;
3275
3276 blk_stat_activate_msecs(q->poll_cb, 100);
3277}
3278
3279static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3280{
3281 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003282 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003283
Stephen Bates720b8cc2017-04-07 06:24:03 -06003284 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3285 if (cb->stat[bucket].nr_samples)
3286 q->poll_stat[bucket] = cb->stat[bucket];
3287 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003288}
3289
Jens Axboe64f1c212016-11-14 13:03:03 -07003290static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
3291 struct blk_mq_hw_ctx *hctx,
3292 struct request *rq)
3293{
Jens Axboe64f1c212016-11-14 13:03:03 -07003294 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003295 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003296
3297 /*
3298 * If stats collection isn't on, don't sleep but turn it on for
3299 * future users
3300 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003301 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003302 return 0;
3303
3304 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003305 * As an optimistic guess, use half of the mean service time
3306 * for this type of request. We can (and should) make this smarter.
3307 * For instance, if the completion latencies are tight, we can
3308 * get closer than just half the mean. This is especially
3309 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003310 * than ~10 usec. We do use the stats for the relevant IO size
3311 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003312 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003313 bucket = blk_mq_poll_stats_bkt(rq);
3314 if (bucket < 0)
3315 return ret;
3316
3317 if (q->poll_stat[bucket].nr_samples)
3318 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003319
3320 return ret;
3321}
3322
Jens Axboe06426ad2016-11-14 13:01:59 -07003323static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003324 struct blk_mq_hw_ctx *hctx,
Jens Axboe06426ad2016-11-14 13:01:59 -07003325 struct request *rq)
3326{
3327 struct hrtimer_sleeper hs;
3328 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003329 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003330 ktime_t kt;
3331
Jens Axboe76a86f92018-01-10 11:30:56 -07003332 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003333 return false;
3334
3335 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003336 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003337 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003338 * 0: use half of prev avg
3339 * >0: use this specific value
3340 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003341 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003342 nsecs = q->poll_nsec;
3343 else
3344 nsecs = blk_mq_poll_nsecs(q, hctx, rq);
3345
3346 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003347 return false;
3348
Jens Axboe76a86f92018-01-10 11:30:56 -07003349 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003350
3351 /*
3352 * This will be replaced with the stats tracking code, using
3353 * 'avg_completion_time / 2' as the pre-sleep target.
3354 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003355 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003356
3357 mode = HRTIMER_MODE_REL;
3358 hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
3359 hrtimer_set_expires(&hs.timer, kt);
3360
3361 hrtimer_init_sleeper(&hs, current);
3362 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003363 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003364 break;
3365 set_current_state(TASK_UNINTERRUPTIBLE);
3366 hrtimer_start_expires(&hs.timer, mode);
3367 if (hs.task)
3368 io_schedule();
3369 hrtimer_cancel(&hs.timer);
3370 mode = HRTIMER_MODE_ABS;
3371 } while (hs.task && !signal_pending(current));
3372
3373 __set_current_state(TASK_RUNNING);
3374 destroy_hrtimer_on_stack(&hs.timer);
3375 return true;
3376}
3377
Jens Axboe1052b8a2018-11-26 08:21:49 -07003378static bool blk_mq_poll_hybrid(struct request_queue *q,
3379 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003380{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003381 struct request *rq;
3382
3383 if (q->poll_nsec == -1)
3384 return false;
3385
3386 if (!blk_qc_t_is_internal(cookie))
3387 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3388 else {
3389 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3390 /*
3391 * With scheduling, if the request has completed, we'll
3392 * get a NULL return here, as we clear the sched tag when
3393 * that happens. The request still remains valid, like always,
3394 * so we should be safe with just the NULL check.
3395 */
3396 if (!rq)
3397 return false;
3398 }
3399
3400 return blk_mq_poll_hybrid_sleep(q, hctx, rq);
3401}
3402
Jens Axboe0a1b8b82018-11-26 08:24:43 -07003403static int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003404{
3405 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003406 long state;
3407
Jens Axboe1052b8a2018-11-26 08:21:49 -07003408 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
3409 return 0;
3410
3411 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3412
Jens Axboe06426ad2016-11-14 13:01:59 -07003413 /*
3414 * If we sleep, have the caller restart the poll loop to reset
3415 * the state. Like for the other success return cases, the
3416 * caller is responsible for checking if the IO completed. If
3417 * the IO isn't complete, we'll get called again and will go
3418 * straight to the busy poll loop.
3419 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003420 if (blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003421 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07003422
Jens Axboebbd7bb72016-11-04 09:34:34 -06003423 hctx->poll_considered++;
3424
3425 state = current->state;
Jens Axboeaa61bec2018-11-13 21:32:10 -07003426 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06003427 int ret;
3428
3429 hctx->poll_invoked++;
3430
Jens Axboe97431392018-11-16 09:48:21 -07003431 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003432 if (ret > 0) {
3433 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07003434 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003435 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003436 }
3437
3438 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07003439 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003440
3441 if (current->state == TASK_RUNNING)
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003442 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07003443 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003444 break;
3445 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07003446 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06003447
Nitesh Shetty67b41102018-02-13 21:18:12 +05303448 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003449 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003450}
3451
Jens Axboe9cf2bab2018-10-31 17:01:22 -06003452unsigned int blk_mq_rq_cpu(struct request *rq)
3453{
3454 return rq->mq_ctx->cpu;
3455}
3456EXPORT_SYMBOL(blk_mq_rq_cpu);
3457
Jens Axboe320ae512013-10-24 09:20:05 +01003458static int __init blk_mq_init(void)
3459{
Thomas Gleixner9467f852016-09-22 08:05:17 -06003460 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3461 blk_mq_hctx_notify_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01003462 return 0;
3463}
3464subsys_initcall(blk_mq_init);