blob: c207fa9870ebcaaed6bfb02efa70b5f7333954f8 [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>
23#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060024#include <linux/crash_dump.h>
Jens Axboe320ae512013-10-24 09:20:05 +010025
26#include <trace/events/block.h>
27
28#include <linux/blk-mq.h>
29#include "blk.h"
30#include "blk-mq.h"
31#include "blk-mq-tag.h"
32
33static DEFINE_MUTEX(all_q_mutex);
34static LIST_HEAD(all_q_list);
35
36static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
37
Jens Axboe320ae512013-10-24 09:20:05 +010038/*
39 * Check if any of the ctx's have pending work in this hardware queue
40 */
41static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
42{
43 unsigned int i;
44
Jens Axboe569fd0c2015-04-17 08:28:50 -060045 for (i = 0; i < hctx->ctx_map.size; i++)
Jens Axboe1429d7c2014-05-19 09:23:55 -060046 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010047 return true;
48
49 return false;
50}
51
Jens Axboe1429d7c2014-05-19 09:23:55 -060052static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
53 struct blk_mq_ctx *ctx)
54{
55 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
56}
57
58#define CTX_TO_BIT(hctx, ctx) \
59 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
60
Jens Axboe320ae512013-10-24 09:20:05 +010061/*
62 * Mark this ctx as having pending work in this hardware queue
63 */
64static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
65 struct blk_mq_ctx *ctx)
66{
Jens Axboe1429d7c2014-05-19 09:23:55 -060067 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
68
69 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
70 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
71}
72
73static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
74 struct blk_mq_ctx *ctx)
75{
76 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
77
78 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010079}
80
Keith Buschb4c6a022014-12-19 17:54:14 -070081void blk_mq_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +080082{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020083 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -040084
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020085 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
86 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -040087 percpu_ref_kill(&q->q_usage_counter);
Mike Snitzerb94ec292015-03-11 23:56:38 -040088 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -040089 }
Tejun Heof3af0202014-11-04 13:52:27 -050090}
Keith Buschb4c6a022014-12-19 17:54:14 -070091EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -050092
93static void blk_mq_freeze_queue_wait(struct request_queue *q)
94{
Dan Williams3ef28e82015-10-21 13:20:12 -040095 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +080096}
97
Tejun Heof3af0202014-11-04 13:52:27 -050098/*
99 * Guarantee no request is in use, so we can change any data structure of
100 * the queue afterward.
101 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400102void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500103{
Dan Williams3ef28e82015-10-21 13:20:12 -0400104 /*
105 * In the !blk_mq case we are only calling this to kill the
106 * q_usage_counter, otherwise this increases the freeze depth
107 * and waits for it to return to zero. For this reason there is
108 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
109 * exported to drivers as the only user for unfreeze is blk_mq.
110 */
Tejun Heof3af0202014-11-04 13:52:27 -0500111 blk_mq_freeze_queue_start(q);
112 blk_mq_freeze_queue_wait(q);
113}
Dan Williams3ef28e82015-10-21 13:20:12 -0400114
115void blk_mq_freeze_queue(struct request_queue *q)
116{
117 /*
118 * ...just an alias to keep freeze and unfreeze actions balanced
119 * in the blk_mq_* namespace
120 */
121 blk_freeze_queue(q);
122}
Jens Axboec761d962015-01-02 15:05:12 -0700123EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500124
Keith Buschb4c6a022014-12-19 17:54:14 -0700125void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100126{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200127 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100128
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200129 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
130 WARN_ON_ONCE(freeze_depth < 0);
131 if (!freeze_depth) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400132 percpu_ref_reinit(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100133 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600134 }
Jens Axboe320ae512013-10-24 09:20:05 +0100135}
Keith Buschb4c6a022014-12-19 17:54:14 -0700136EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100137
Jens Axboeaed3ea92014-12-22 14:04:42 -0700138void blk_mq_wake_waiters(struct request_queue *q)
139{
140 struct blk_mq_hw_ctx *hctx;
141 unsigned int i;
142
143 queue_for_each_hw_ctx(q, hctx, i)
144 if (blk_mq_hw_queue_mapped(hctx))
145 blk_mq_tag_wakeup_all(hctx->tags, true);
Keith Busch3fd59402015-01-08 08:53:56 -0700146
147 /*
148 * If we are called because the queue has now been marked as
149 * dying, we need to ensure that processes currently waiting on
150 * the queue are notified as well.
151 */
152 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700153}
154
Jens Axboe320ae512013-10-24 09:20:05 +0100155bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
156{
157 return blk_mq_has_free_tags(hctx->tags);
158}
159EXPORT_SYMBOL(blk_mq_can_queue);
160
Jens Axboe94eddfb2013-11-19 09:25:07 -0700161static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
Mike Christiecc6e3b12016-06-05 14:32:12 -0500162 struct request *rq, int op,
163 unsigned int op_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100164{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700165 if (blk_queue_io_stat(q))
Mike Christiecc6e3b12016-06-05 14:32:12 -0500166 op_flags |= REQ_IO_STAT;
Jens Axboe94eddfb2013-11-19 09:25:07 -0700167
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200168 INIT_LIST_HEAD(&rq->queuelist);
169 /* csd/requeue_work/fifo_time is initialized before use */
170 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100171 rq->mq_ctx = ctx;
Mike Christiecc6e3b12016-06-05 14:32:12 -0500172 req_set_op_attrs(rq, op, op_flags);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200173 /* do not touch atomic flags, it needs atomic ops against the timer */
174 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200175 INIT_HLIST_NODE(&rq->hash);
176 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200177 rq->rq_disk = NULL;
178 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600179 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200180#ifdef CONFIG_BLK_CGROUP
181 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700182 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200183 rq->io_start_time_ns = 0;
184#endif
185 rq->nr_phys_segments = 0;
186#if defined(CONFIG_BLK_DEV_INTEGRITY)
187 rq->nr_integrity_segments = 0;
188#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200189 rq->special = NULL;
190 /* tag was already set */
191 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200192
Tony Battersby6f4a16262014-08-22 15:53:39 -0400193 rq->cmd = rq->__cmd;
194
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200195 rq->extra_len = 0;
196 rq->sense_len = 0;
197 rq->resid_len = 0;
198 rq->sense = NULL;
199
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200200 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600201 rq->timeout = 0;
202
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200203 rq->end_io = NULL;
204 rq->end_io_data = NULL;
205 rq->next_rq = NULL;
206
Mike Christied9d8c5c2016-06-05 14:32:16 -0500207 ctx->rq_dispatched[rw_is_sync(op, op_flags)]++;
Jens Axboe320ae512013-10-24 09:20:05 +0100208}
209
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200210static struct request *
Mike Christiecc6e3b12016-06-05 14:32:12 -0500211__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int op, int op_flags)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200212{
213 struct request *rq;
214 unsigned int tag;
215
Ming Leicb96a42c2014-06-01 00:43:37 +0800216 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200217 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a42c2014-06-01 00:43:37 +0800218 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200219
Ming Leicb96a42c2014-06-01 00:43:37 +0800220 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200221 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a42c2014-06-01 00:43:37 +0800222 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200223 }
224
225 rq->tag = tag;
Mike Christiecc6e3b12016-06-05 14:32:12 -0500226 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op, op_flags);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200227 return rq;
228 }
229
230 return NULL;
231}
232
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100233struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
234 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100235{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200236 struct blk_mq_ctx *ctx;
237 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100238 struct request *rq;
Ming Leicb96a42c2014-06-01 00:43:37 +0800239 struct blk_mq_alloc_data alloc_data;
Joe Lawrencea492f072014-08-28 08:15:21 -0600240 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100241
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100242 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
Joe Lawrencea492f072014-08-28 08:15:21 -0600243 if (ret)
244 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100245
Christoph Hellwigd8525642014-05-27 20:59:50 +0200246 ctx = blk_mq_get_ctx(q);
247 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100248 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200249
Mike Christiecc6e3b12016-06-05 14:32:12 -0500250 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100251 if (!rq && !(flags & BLK_MQ_REQ_NOWAIT)) {
Christoph Hellwigd8525642014-05-27 20:59:50 +0200252 __blk_mq_run_hw_queue(hctx);
253 blk_mq_put_ctx(ctx);
254
255 ctx = blk_mq_get_ctx(q);
256 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100257 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -0500258 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
Ming Leicb96a42c2014-06-01 00:43:37 +0800259 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200260 }
261 blk_mq_put_ctx(ctx);
Keith Buschc76541a2014-12-19 17:54:13 -0700262 if (!rq) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400263 blk_queue_exit(q);
Joe Lawrencea492f072014-08-28 08:15:21 -0600264 return ERR_PTR(-EWOULDBLOCK);
Keith Buschc76541a2014-12-19 17:54:13 -0700265 }
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200266
267 rq->__data_len = 0;
268 rq->__sector = (sector_t) -1;
269 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100270 return rq;
271}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600272EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100273
Ming Lin1f5bd332016-06-13 16:45:21 +0200274struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
275 unsigned int flags, unsigned int hctx_idx)
276{
277 struct blk_mq_hw_ctx *hctx;
278 struct blk_mq_ctx *ctx;
279 struct request *rq;
280 struct blk_mq_alloc_data alloc_data;
281 int ret;
282
283 /*
284 * If the tag allocator sleeps we could get an allocation for a
285 * different hardware context. No need to complicate the low level
286 * allocator for this for the rare use case of a command tied to
287 * a specific queue.
288 */
289 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
290 return ERR_PTR(-EINVAL);
291
292 if (hctx_idx >= q->nr_hw_queues)
293 return ERR_PTR(-EIO);
294
295 ret = blk_queue_enter(q, true);
296 if (ret)
297 return ERR_PTR(ret);
298
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600299 /*
300 * Check if the hardware context is actually mapped to anything.
301 * If not tell the caller that it should skip this queue.
302 */
Ming Lin1f5bd332016-06-13 16:45:21 +0200303 hctx = q->queue_hw_ctx[hctx_idx];
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600304 if (!blk_mq_hw_queue_mapped(hctx)) {
305 ret = -EXDEV;
306 goto out_queue_exit;
307 }
Ming Lin1f5bd332016-06-13 16:45:21 +0200308 ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask));
309
310 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
311 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
312 if (!rq) {
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600313 ret = -EWOULDBLOCK;
314 goto out_queue_exit;
Ming Lin1f5bd332016-06-13 16:45:21 +0200315 }
316
317 return rq;
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600318
319out_queue_exit:
320 blk_queue_exit(q);
321 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200322}
323EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
324
Jens Axboe320ae512013-10-24 09:20:05 +0100325static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
326 struct blk_mq_ctx *ctx, struct request *rq)
327{
328 const int tag = rq->tag;
329 struct request_queue *q = rq->q;
330
Jens Axboe0d2602c2014-05-13 15:10:52 -0600331 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
332 atomic_dec(&hctx->nr_active);
David Hildenbrand683d0e12014-09-18 11:04:31 +0200333 rq->cmd_flags = 0;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600334
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200335 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600336 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Dan Williams3ef28e82015-10-21 13:20:12 -0400337 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100338}
339
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700340void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100341{
342 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700343
344 ctx->rq_completed[rq_is_sync(rq)]++;
345 __blk_mq_free_request(hctx, ctx, rq);
346
347}
348EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
349
350void blk_mq_free_request(struct request *rq)
351{
Jens Axboe320ae512013-10-24 09:20:05 +0100352 struct blk_mq_hw_ctx *hctx;
353 struct request_queue *q = rq->q;
354
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700355 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
356 blk_mq_free_hctx_request(hctx, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100357}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700358EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100359
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700360inline void __blk_mq_end_request(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100361{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700362 blk_account_io_done(rq);
363
Christoph Hellwig91b63632014-04-16 09:44:53 +0200364 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100365 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200366 } else {
367 if (unlikely(blk_bidi_rq(rq)))
368 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100369 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200370 }
Jens Axboe320ae512013-10-24 09:20:05 +0100371}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700372EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200373
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700374void blk_mq_end_request(struct request *rq, int error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200375{
376 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
377 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700378 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200379}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700380EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100381
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800382static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100383{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800384 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100385
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800386 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100387}
388
Jens Axboeed851862014-05-30 21:20:50 -0600389static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100390{
391 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700392 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100393 int cpu;
394
Christoph Hellwig38535202014-04-25 02:32:53 -0700395 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800396 rq->q->softirq_done_fn(rq);
397 return;
398 }
Jens Axboe320ae512013-10-24 09:20:05 +0100399
400 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700401 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
402 shared = cpus_share_cache(cpu, ctx->cpu);
403
404 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800405 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800406 rq->csd.info = rq;
407 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100408 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800409 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800410 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800411 }
Jens Axboe320ae512013-10-24 09:20:05 +0100412 put_cpu();
413}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800414
Jens Axboe1fa8cc52015-11-05 14:32:55 -0700415static void __blk_mq_complete_request(struct request *rq)
Jens Axboeed851862014-05-30 21:20:50 -0600416{
417 struct request_queue *q = rq->q;
418
419 if (!q->softirq_done_fn)
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700420 blk_mq_end_request(rq, rq->errors);
Jens Axboeed851862014-05-30 21:20:50 -0600421 else
422 blk_mq_ipi_complete_request(rq);
423}
424
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800425/**
426 * blk_mq_complete_request - end I/O on a request
427 * @rq: the request being processed
428 *
429 * Description:
430 * Ends all I/O on a request. It does not handle partial completions.
431 * The actual completion happens out-of-order, through a IPI handler.
432 **/
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200433void blk_mq_complete_request(struct request *rq, int error)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800434{
Jens Axboe95f09682014-05-27 17:46:48 -0600435 struct request_queue *q = rq->q;
436
437 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800438 return;
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200439 if (!blk_mark_rq_complete(rq)) {
440 rq->errors = error;
Jens Axboeed851862014-05-30 21:20:50 -0600441 __blk_mq_complete_request(rq);
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200442 }
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800443}
444EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100445
Keith Busch973c0192015-01-07 18:55:43 -0700446int blk_mq_request_started(struct request *rq)
447{
448 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
449}
450EXPORT_SYMBOL_GPL(blk_mq_request_started);
451
Christoph Hellwige2490072014-09-13 16:40:09 -0700452void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100453{
454 struct request_queue *q = rq->q;
455
456 trace_block_rq_issue(q, rq);
457
Christoph Hellwig742ee692014-04-14 10:30:06 +0200458 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200459 if (unlikely(blk_bidi_rq(rq)))
460 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200461
Ming Lei2b8393b2014-06-10 00:16:41 +0800462 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600463
464 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600465 * Ensure that ->deadline is visible before set the started
466 * flag and clear the completed flag.
467 */
468 smp_mb__before_atomic();
469
470 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600471 * Mark us as started and clear complete. Complete might have been
472 * set if requeue raced with timeout, which then marked it as
473 * complete. So be sure to clear complete again when we start
474 * the request, otherwise we'll ignore the completion event.
475 */
Jens Axboe4b570522014-05-29 11:00:11 -0600476 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
477 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
478 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
479 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800480
481 if (q->dma_drain_size && blk_rq_bytes(rq)) {
482 /*
483 * Make sure space for the drain appears. We know we can do
484 * this because max_hw_segments has been adjusted to be one
485 * fewer than the device can handle.
486 */
487 rq->nr_phys_segments++;
488 }
Jens Axboe320ae512013-10-24 09:20:05 +0100489}
Christoph Hellwige2490072014-09-13 16:40:09 -0700490EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100491
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200492static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100493{
494 struct request_queue *q = rq->q;
495
496 trace_block_rq_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800497
Christoph Hellwige2490072014-09-13 16:40:09 -0700498 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
499 if (q->dma_drain_size && blk_rq_bytes(rq))
500 rq->nr_phys_segments--;
501 }
Jens Axboe320ae512013-10-24 09:20:05 +0100502}
503
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200504void blk_mq_requeue_request(struct request *rq)
505{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200506 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200507
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200508 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600509 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200510}
511EXPORT_SYMBOL(blk_mq_requeue_request);
512
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600513static void blk_mq_requeue_work(struct work_struct *work)
514{
515 struct request_queue *q =
516 container_of(work, struct request_queue, requeue_work);
517 LIST_HEAD(rq_list);
518 struct request *rq, *next;
519 unsigned long flags;
520
521 spin_lock_irqsave(&q->requeue_lock, flags);
522 list_splice_init(&q->requeue_list, &rq_list);
523 spin_unlock_irqrestore(&q->requeue_lock, flags);
524
525 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
526 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
527 continue;
528
529 rq->cmd_flags &= ~REQ_SOFTBARRIER;
530 list_del_init(&rq->queuelist);
531 blk_mq_insert_request(rq, true, false, false);
532 }
533
534 while (!list_empty(&rq_list)) {
535 rq = list_entry(rq_list.next, struct request, queuelist);
536 list_del_init(&rq->queuelist);
537 blk_mq_insert_request(rq, false, false, false);
538 }
539
Jens Axboe8b957412014-09-19 13:10:29 -0600540 /*
541 * Use the start variant of queue running here, so that running
542 * the requeue work will kick stopped queues.
543 */
544 blk_mq_start_hw_queues(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600545}
546
547void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
548{
549 struct request_queue *q = rq->q;
550 unsigned long flags;
551
552 /*
553 * We abuse this flag that is otherwise used by the I/O scheduler to
554 * request head insertation from the workqueue.
555 */
556 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
557
558 spin_lock_irqsave(&q->requeue_lock, flags);
559 if (at_head) {
560 rq->cmd_flags |= REQ_SOFTBARRIER;
561 list_add(&rq->queuelist, &q->requeue_list);
562 } else {
563 list_add_tail(&rq->queuelist, &q->requeue_list);
564 }
565 spin_unlock_irqrestore(&q->requeue_lock, flags);
566}
567EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
568
Keith Buschc68ed592015-01-07 18:55:44 -0700569void blk_mq_cancel_requeue_work(struct request_queue *q)
570{
571 cancel_work_sync(&q->requeue_work);
572}
573EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work);
574
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600575void blk_mq_kick_requeue_list(struct request_queue *q)
576{
577 kblockd_schedule_work(&q->requeue_work);
578}
579EXPORT_SYMBOL(blk_mq_kick_requeue_list);
580
Jens Axboe1885b242015-01-07 18:55:45 -0700581void blk_mq_abort_requeue_list(struct request_queue *q)
582{
583 unsigned long flags;
584 LIST_HEAD(rq_list);
585
586 spin_lock_irqsave(&q->requeue_lock, flags);
587 list_splice_init(&q->requeue_list, &rq_list);
588 spin_unlock_irqrestore(&q->requeue_lock, flags);
589
590 while (!list_empty(&rq_list)) {
591 struct request *rq;
592
593 rq = list_first_entry(&rq_list, struct request, queuelist);
594 list_del_init(&rq->queuelist);
595 rq->errors = -EIO;
596 blk_mq_end_request(rq, rq->errors);
597 }
598}
599EXPORT_SYMBOL(blk_mq_abort_requeue_list);
600
Jens Axboe0e62f512014-06-04 10:23:49 -0600601struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
602{
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700603 if (tag < tags->nr_tags)
604 return tags->rqs[tag];
605
606 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600607}
608EXPORT_SYMBOL(blk_mq_tag_to_rq);
609
Jens Axboe320ae512013-10-24 09:20:05 +0100610struct blk_mq_timeout_data {
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700611 unsigned long next;
612 unsigned int next_set;
Jens Axboe320ae512013-10-24 09:20:05 +0100613};
614
Christoph Hellwig90415832014-09-22 10:21:48 -0600615void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100616{
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700617 struct blk_mq_ops *ops = req->q->mq_ops;
618 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600619
620 /*
621 * We know that complete is set at this point. If STARTED isn't set
622 * anymore, then the request isn't active and the "timeout" should
623 * just be ignored. This can happen due to the bitflag ordering.
624 * Timeout first checks if STARTED is set, and if it is, assumes
625 * the request is active. But if we race with completion, then
626 * we both flags will get cleared. So check here again, and ignore
627 * a timeout event with a request that isn't active.
628 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700629 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
630 return;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600631
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700632 if (ops->timeout)
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700633 ret = ops->timeout(req, reserved);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600634
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700635 switch (ret) {
636 case BLK_EH_HANDLED:
637 __blk_mq_complete_request(req);
638 break;
639 case BLK_EH_RESET_TIMER:
640 blk_add_timer(req);
641 blk_clear_rq_complete(req);
642 break;
643 case BLK_EH_NOT_HANDLED:
644 break;
645 default:
646 printk(KERN_ERR "block: bad eh return: %d\n", ret);
647 break;
648 }
Jens Axboe87ee7b12014-04-24 08:51:47 -0600649}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700650
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700651static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
652 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100653{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700654 struct blk_mq_timeout_data *data = priv;
655
Keith Buscheb130db2015-01-08 08:59:53 -0700656 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
657 /*
658 * If a request wasn't started before the queue was
659 * marked dying, kill it here or it'll go unnoticed.
660 */
Keith Buscha59e0f52016-02-11 13:05:38 -0700661 if (unlikely(blk_queue_dying(rq->q))) {
662 rq->errors = -EIO;
663 blk_mq_end_request(rq, rq->errors);
664 }
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700665 return;
Keith Buscheb130db2015-01-08 08:59:53 -0700666 }
Jens Axboe320ae512013-10-24 09:20:05 +0100667
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700668 if (time_after_eq(jiffies, rq->deadline)) {
669 if (!blk_mark_rq_complete(rq))
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700670 blk_mq_rq_timed_out(rq, reserved);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700671 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
672 data->next = rq->deadline;
673 data->next_set = 1;
674 }
Jens Axboe320ae512013-10-24 09:20:05 +0100675}
676
Christoph Hellwig287922e2015-10-30 20:57:30 +0800677static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100678{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800679 struct request_queue *q =
680 container_of(work, struct request_queue, timeout_work);
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700681 struct blk_mq_timeout_data data = {
682 .next = 0,
683 .next_set = 0,
684 };
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700685 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100686
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600687 /* A deadlock might occur if a request is stuck requiring a
688 * timeout at the same time a queue freeze is waiting
689 * completion, since the timeout code would not be able to
690 * acquire the queue reference here.
691 *
692 * That's why we don't use blk_queue_enter here; instead, we use
693 * percpu_ref_tryget directly, because we need to be able to
694 * obtain a reference even in the short window between the queue
695 * starting to freeze, by dropping the first reference in
696 * blk_mq_freeze_queue_start, and the moment the last request is
697 * consumed, marked by the instant q_usage_counter reaches
698 * zero.
699 */
700 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800701 return;
702
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200703 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
Jens Axboe320ae512013-10-24 09:20:05 +0100704
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700705 if (data.next_set) {
706 data.next = blk_rq_timeout(round_jiffies_up(data.next));
707 mod_timer(&q->timeout, data.next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600708 } else {
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200709 struct blk_mq_hw_ctx *hctx;
710
Ming Leif054b562015-04-21 10:00:19 +0800711 queue_for_each_hw_ctx(q, hctx, i) {
712 /* the hctx may be unmapped, so check it here */
713 if (blk_mq_hw_queue_mapped(hctx))
714 blk_mq_tag_idle(hctx);
715 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600716 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800717 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100718}
719
720/*
721 * Reverse check our software queue for entries that we could potentially
722 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
723 * too much time checking for merges.
724 */
725static bool blk_mq_attempt_merge(struct request_queue *q,
726 struct blk_mq_ctx *ctx, struct bio *bio)
727{
728 struct request *rq;
729 int checked = 8;
730
731 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
732 int el_ret;
733
734 if (!checked--)
735 break;
736
737 if (!blk_rq_merge_ok(rq, bio))
738 continue;
739
740 el_ret = blk_try_merge(rq, bio);
741 if (el_ret == ELEVATOR_BACK_MERGE) {
742 if (bio_attempt_back_merge(q, rq, bio)) {
743 ctx->rq_merged++;
744 return true;
745 }
746 break;
747 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
748 if (bio_attempt_front_merge(q, rq, bio)) {
749 ctx->rq_merged++;
750 return true;
751 }
752 break;
753 }
754 }
755
756 return false;
757}
758
Jens Axboe320ae512013-10-24 09:20:05 +0100759/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600760 * Process software queues that have been marked busy, splicing them
761 * to the for-dispatch
762 */
763static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
764{
765 struct blk_mq_ctx *ctx;
766 int i;
767
Jens Axboe569fd0c2015-04-17 08:28:50 -0600768 for (i = 0; i < hctx->ctx_map.size; i++) {
Jens Axboe1429d7c2014-05-19 09:23:55 -0600769 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
770 unsigned int off, bit;
771
772 if (!bm->word)
773 continue;
774
775 bit = 0;
776 off = i * hctx->ctx_map.bits_per_word;
777 do {
778 bit = find_next_bit(&bm->word, bm->depth, bit);
779 if (bit >= bm->depth)
780 break;
781
782 ctx = hctx->ctxs[bit + off];
783 clear_bit(bit, &bm->word);
784 spin_lock(&ctx->lock);
785 list_splice_tail_init(&ctx->rq_list, list);
786 spin_unlock(&ctx->lock);
787
788 bit++;
789 } while (1);
790 }
791}
792
793/*
Jens Axboe320ae512013-10-24 09:20:05 +0100794 * Run this hardware queue, pulling any software queues mapped to it in.
795 * Note that this function currently has various problems around ordering
796 * of IO. In particular, we'd like FIFO behaviour on handling existing
797 * items on the hctx->dispatch list. Ignore that for now.
798 */
799static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
800{
801 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100802 struct request *rq;
803 LIST_HEAD(rq_list);
Jens Axboe74c45052014-10-29 11:14:52 -0600804 LIST_HEAD(driver_list);
805 struct list_head *dptr;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600806 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100807
Jens Axboe5d12f902014-03-19 15:25:02 -0600808 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100809 return;
810
Jens Axboe0e87e582016-08-24 15:38:01 -0600811 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
812 cpu_online(hctx->next_cpu));
813
Jens Axboe320ae512013-10-24 09:20:05 +0100814 hctx->run++;
815
816 /*
817 * Touch any software queue that has pending entries.
818 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600819 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100820
821 /*
822 * If we have previous entries on our dispatch list, grab them
823 * and stuff them at the front for more fair dispatch.
824 */
825 if (!list_empty_careful(&hctx->dispatch)) {
826 spin_lock(&hctx->lock);
827 if (!list_empty(&hctx->dispatch))
828 list_splice_init(&hctx->dispatch, &rq_list);
829 spin_unlock(&hctx->lock);
830 }
831
832 /*
Jens Axboe74c45052014-10-29 11:14:52 -0600833 * Start off with dptr being NULL, so we start the first request
834 * immediately, even if we have more pending.
835 */
836 dptr = NULL;
837
838 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100839 * Now process all the entries, sending them to the driver.
840 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600841 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100842 while (!list_empty(&rq_list)) {
Jens Axboe74c45052014-10-29 11:14:52 -0600843 struct blk_mq_queue_data bd;
Jens Axboe320ae512013-10-24 09:20:05 +0100844 int ret;
845
846 rq = list_first_entry(&rq_list, struct request, queuelist);
847 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100848
Jens Axboe74c45052014-10-29 11:14:52 -0600849 bd.rq = rq;
850 bd.list = dptr;
851 bd.last = list_empty(&rq_list);
852
853 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe320ae512013-10-24 09:20:05 +0100854 switch (ret) {
855 case BLK_MQ_RQ_QUEUE_OK:
856 queued++;
Omar Sandoval52b9c332016-06-08 18:22:20 -0700857 break;
Jens Axboe320ae512013-10-24 09:20:05 +0100858 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100859 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200860 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100861 break;
862 default:
863 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100864 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800865 rq->errors = -EIO;
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700866 blk_mq_end_request(rq, rq->errors);
Jens Axboe320ae512013-10-24 09:20:05 +0100867 break;
868 }
869
870 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
871 break;
Jens Axboe74c45052014-10-29 11:14:52 -0600872
873 /*
874 * We've done the first request. If we have more than 1
875 * left in the list, set dptr to defer issue.
876 */
877 if (!dptr && rq_list.next != rq_list.prev)
878 dptr = &driver_list;
Jens Axboe320ae512013-10-24 09:20:05 +0100879 }
880
881 if (!queued)
882 hctx->dispatched[0]++;
883 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
884 hctx->dispatched[ilog2(queued) + 1]++;
885
886 /*
887 * Any items that need requeuing? Stuff them into hctx->dispatch,
888 * that is where we will continue on next queue run.
889 */
890 if (!list_empty(&rq_list)) {
891 spin_lock(&hctx->lock);
892 list_splice(&rq_list, &hctx->dispatch);
893 spin_unlock(&hctx->lock);
Shaohua Li9ba52e52015-05-04 14:32:48 -0600894 /*
895 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
896 * it's possible the queue is stopped and restarted again
897 * before this. Queue restart will dispatch requests. And since
898 * requests in rq_list aren't added into hctx->dispatch yet,
899 * the requests in rq_list might get lost.
900 *
901 * blk_mq_run_hw_queue() already checks the STOPPED bit
902 **/
903 blk_mq_run_hw_queue(hctx, true);
Jens Axboe320ae512013-10-24 09:20:05 +0100904 }
905}
906
Jens Axboe506e9312014-05-07 10:26:44 -0600907/*
908 * It'd be great if the workqueue API had a way to pass
909 * in a mask and had some smarts for more clever placement.
910 * For now we just round-robin here, switching for every
911 * BLK_MQ_CPU_WORK_BATCH queued items.
912 */
913static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
914{
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100915 if (hctx->queue->nr_hw_queues == 1)
916 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -0600917
918 if (--hctx->next_cpu_batch <= 0) {
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100919 int cpu = hctx->next_cpu, next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600920
921 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
922 if (next_cpu >= nr_cpu_ids)
923 next_cpu = cpumask_first(hctx->cpumask);
924
925 hctx->next_cpu = next_cpu;
926 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100927
928 return cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600929 }
930
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100931 return hctx->next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600932}
933
Jens Axboe320ae512013-10-24 09:20:05 +0100934void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
935{
Ming Lei19c66e52014-12-03 19:38:04 +0800936 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state) ||
937 !blk_mq_hw_queue_mapped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +0100938 return;
939
Paolo Bonzini398205b2014-11-07 23:03:59 +0100940 if (!async) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100941 int cpu = get_cpu();
942 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +0100943 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100944 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +0100945 return;
946 }
Jens Axboee4043dc2014-04-09 10:18:23 -0600947
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100948 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -0600949 }
Paolo Bonzini398205b2014-11-07 23:03:59 +0100950
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100951 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
952 &hctx->run_work, 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100953}
954
Mike Snitzerb94ec292015-03-11 23:56:38 -0400955void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100956{
957 struct blk_mq_hw_ctx *hctx;
958 int i;
959
960 queue_for_each_hw_ctx(q, hctx, i) {
961 if ((!blk_mq_hctx_has_pending(hctx) &&
962 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600963 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100964 continue;
965
Mike Snitzerb94ec292015-03-11 23:56:38 -0400966 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +0100967 }
968}
Mike Snitzerb94ec292015-03-11 23:56:38 -0400969EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +0100970
971void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
972{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600973 cancel_delayed_work(&hctx->run_work);
974 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100975 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
976}
977EXPORT_SYMBOL(blk_mq_stop_hw_queue);
978
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100979void blk_mq_stop_hw_queues(struct request_queue *q)
980{
981 struct blk_mq_hw_ctx *hctx;
982 int i;
983
984 queue_for_each_hw_ctx(q, hctx, i)
985 blk_mq_stop_hw_queue(hctx);
986}
987EXPORT_SYMBOL(blk_mq_stop_hw_queues);
988
Jens Axboe320ae512013-10-24 09:20:05 +0100989void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
990{
991 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600992
Jens Axboe0ffbce82014-06-25 08:22:34 -0600993 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100994}
995EXPORT_SYMBOL(blk_mq_start_hw_queue);
996
Christoph Hellwig2f268552014-04-16 09:44:56 +0200997void blk_mq_start_hw_queues(struct request_queue *q)
998{
999 struct blk_mq_hw_ctx *hctx;
1000 int i;
1001
1002 queue_for_each_hw_ctx(q, hctx, i)
1003 blk_mq_start_hw_queue(hctx);
1004}
1005EXPORT_SYMBOL(blk_mq_start_hw_queues);
1006
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001007void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001008{
1009 struct blk_mq_hw_ctx *hctx;
1010 int i;
1011
1012 queue_for_each_hw_ctx(q, hctx, i) {
1013 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
1014 continue;
1015
1016 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001017 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001018 }
1019}
1020EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1021
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001022static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001023{
1024 struct blk_mq_hw_ctx *hctx;
1025
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001026 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -06001027
Jens Axboe320ae512013-10-24 09:20:05 +01001028 __blk_mq_run_hw_queue(hctx);
1029}
1030
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001031static void blk_mq_delay_work_fn(struct work_struct *work)
1032{
1033 struct blk_mq_hw_ctx *hctx;
1034
1035 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1036
1037 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1038 __blk_mq_run_hw_queue(hctx);
1039}
1040
1041void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1042{
Ming Lei19c66e52014-12-03 19:38:04 +08001043 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1044 return;
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001045
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001046 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1047 &hctx->delay_work, msecs_to_jiffies(msecs));
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001048}
1049EXPORT_SYMBOL(blk_mq_delay_queue);
1050
Ming Leicfd0c552015-10-20 23:13:57 +08001051static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001052 struct request *rq,
1053 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001054{
Jens Axboee57690f2016-08-24 15:34:35 -06001055 struct blk_mq_ctx *ctx = rq->mq_ctx;
1056
Jens Axboe01b983c2013-11-19 18:59:10 -07001057 trace_block_rq_insert(hctx->queue, rq);
1058
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001059 if (at_head)
1060 list_add(&rq->queuelist, &ctx->rq_list);
1061 else
1062 list_add_tail(&rq->queuelist, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001063}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001064
Ming Leicfd0c552015-10-20 23:13:57 +08001065static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
1066 struct request *rq, bool at_head)
1067{
1068 struct blk_mq_ctx *ctx = rq->mq_ctx;
1069
Jens Axboee57690f2016-08-24 15:34:35 -06001070 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001071 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001072}
1073
Christoph Hellwigeeabc852014-03-21 08:57:37 -06001074void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
Jens Axboee57690f2016-08-24 15:34:35 -06001075 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001076{
Jens Axboee57690f2016-08-24 15:34:35 -06001077 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001078 struct request_queue *q = rq->q;
1079 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001080
Jens Axboe320ae512013-10-24 09:20:05 +01001081 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1082
Christoph Hellwiga57a1782014-09-16 14:44:07 -07001083 spin_lock(&ctx->lock);
1084 __blk_mq_insert_request(hctx, rq, at_head);
1085 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001086
Jens Axboe320ae512013-10-24 09:20:05 +01001087 if (run_queue)
1088 blk_mq_run_hw_queue(hctx, async);
1089}
1090
1091static void blk_mq_insert_requests(struct request_queue *q,
1092 struct blk_mq_ctx *ctx,
1093 struct list_head *list,
1094 int depth,
1095 bool from_schedule)
1096
1097{
1098 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001099
1100 trace_block_unplug(q, depth, !from_schedule);
1101
Jens Axboe320ae512013-10-24 09:20:05 +01001102 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1103
1104 /*
1105 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1106 * offline now
1107 */
1108 spin_lock(&ctx->lock);
1109 while (!list_empty(list)) {
1110 struct request *rq;
1111
1112 rq = list_first_entry(list, struct request, queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001113 BUG_ON(rq->mq_ctx != ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001114 list_del_init(&rq->queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001115 __blk_mq_insert_req_list(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001116 }
Ming Leicfd0c552015-10-20 23:13:57 +08001117 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001118 spin_unlock(&ctx->lock);
1119
Jens Axboe320ae512013-10-24 09:20:05 +01001120 blk_mq_run_hw_queue(hctx, from_schedule);
1121}
1122
1123static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1124{
1125 struct request *rqa = container_of(a, struct request, queuelist);
1126 struct request *rqb = container_of(b, struct request, queuelist);
1127
1128 return !(rqa->mq_ctx < rqb->mq_ctx ||
1129 (rqa->mq_ctx == rqb->mq_ctx &&
1130 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1131}
1132
1133void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1134{
1135 struct blk_mq_ctx *this_ctx;
1136 struct request_queue *this_q;
1137 struct request *rq;
1138 LIST_HEAD(list);
1139 LIST_HEAD(ctx_list);
1140 unsigned int depth;
1141
1142 list_splice_init(&plug->mq_list, &list);
1143
1144 list_sort(NULL, &list, plug_ctx_cmp);
1145
1146 this_q = NULL;
1147 this_ctx = NULL;
1148 depth = 0;
1149
1150 while (!list_empty(&list)) {
1151 rq = list_entry_rq(list.next);
1152 list_del_init(&rq->queuelist);
1153 BUG_ON(!rq->q);
1154 if (rq->mq_ctx != this_ctx) {
1155 if (this_ctx) {
1156 blk_mq_insert_requests(this_q, this_ctx,
1157 &ctx_list, depth,
1158 from_schedule);
1159 }
1160
1161 this_ctx = rq->mq_ctx;
1162 this_q = rq->q;
1163 depth = 0;
1164 }
1165
1166 depth++;
1167 list_add_tail(&rq->queuelist, &ctx_list);
1168 }
1169
1170 /*
1171 * If 'this_ctx' is set, we know we have entries to complete
1172 * on 'ctx_list'. Do those.
1173 */
1174 if (this_ctx) {
1175 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1176 from_schedule);
1177 }
1178}
1179
1180static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1181{
1182 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001183
Michael Callahana21f2a32016-05-03 11:12:49 -04001184 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001185}
1186
Jens Axboe274a5842014-08-15 12:44:08 -06001187static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1188{
1189 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1190 !blk_queue_nomerges(hctx->queue);
1191}
1192
Jens Axboe07068d52014-05-22 10:40:51 -06001193static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1194 struct blk_mq_ctx *ctx,
1195 struct request *rq, struct bio *bio)
1196{
Ming Leie18378a2015-10-20 23:13:54 +08001197 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001198 blk_mq_bio_to_request(rq, bio);
1199 spin_lock(&ctx->lock);
1200insert_rq:
1201 __blk_mq_insert_request(hctx, rq, false);
1202 spin_unlock(&ctx->lock);
1203 return false;
1204 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001205 struct request_queue *q = hctx->queue;
1206
Jens Axboe07068d52014-05-22 10:40:51 -06001207 spin_lock(&ctx->lock);
1208 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1209 blk_mq_bio_to_request(rq, bio);
1210 goto insert_rq;
1211 }
1212
1213 spin_unlock(&ctx->lock);
1214 __blk_mq_free_request(hctx, ctx, rq);
1215 return true;
1216 }
1217}
1218
1219struct blk_map_ctx {
1220 struct blk_mq_hw_ctx *hctx;
1221 struct blk_mq_ctx *ctx;
1222};
1223
1224static struct request *blk_mq_map_request(struct request_queue *q,
1225 struct bio *bio,
1226 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001227{
1228 struct blk_mq_hw_ctx *hctx;
1229 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001230 struct request *rq;
Mike Christiecc6e3b12016-06-05 14:32:12 -05001231 int op = bio_data_dir(bio);
1232 int op_flags = 0;
Ming Leicb96a42c2014-06-01 00:43:37 +08001233 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001234
Dan Williams3ef28e82015-10-21 13:20:12 -04001235 blk_queue_enter_live(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001236 ctx = blk_mq_get_ctx(q);
1237 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1238
Jens Axboe1eff9d32016-08-05 15:35:16 -06001239 if (rw_is_sync(bio_op(bio), bio->bi_opf))
Mike Christiecc6e3b12016-06-05 14:32:12 -05001240 op_flags |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001241
Mike Christiecc6e3b12016-06-05 14:32:12 -05001242 trace_block_getrq(q, bio, op);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001243 blk_mq_set_alloc_data(&alloc_data, q, BLK_MQ_REQ_NOWAIT, ctx, hctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -05001244 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001245 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001246 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001247 blk_mq_put_ctx(ctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -05001248 trace_block_sleeprq(q, bio, op);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001249
1250 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001251 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001252 blk_mq_set_alloc_data(&alloc_data, q, 0, ctx, hctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -05001253 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
Ming Leicb96a42c2014-06-01 00:43:37 +08001254 ctx = alloc_data.ctx;
1255 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001256 }
1257
1258 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001259 data->hctx = hctx;
1260 data->ctx = ctx;
1261 return rq;
1262}
1263
Jens Axboe7b371632015-11-05 10:41:40 -07001264static int blk_mq_direct_issue_request(struct request *rq, blk_qc_t *cookie)
Shaohua Lif984df12015-05-08 10:51:32 -07001265{
1266 int ret;
1267 struct request_queue *q = rq->q;
1268 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q,
1269 rq->mq_ctx->cpu);
1270 struct blk_mq_queue_data bd = {
1271 .rq = rq,
1272 .list = NULL,
1273 .last = 1
1274 };
Jens Axboe7b371632015-11-05 10:41:40 -07001275 blk_qc_t new_cookie = blk_tag_to_qc_t(rq->tag, hctx->queue_num);
Shaohua Lif984df12015-05-08 10:51:32 -07001276
1277 /*
1278 * For OK queue, we are done. For error, kill it. Any other
1279 * error (busy), just add it to our list as we previously
1280 * would have done
1281 */
1282 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe7b371632015-11-05 10:41:40 -07001283 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1284 *cookie = new_cookie;
Shaohua Lif984df12015-05-08 10:51:32 -07001285 return 0;
Shaohua Lif984df12015-05-08 10:51:32 -07001286 }
Jens Axboe7b371632015-11-05 10:41:40 -07001287
1288 __blk_mq_requeue_request(rq);
1289
1290 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1291 *cookie = BLK_QC_T_NONE;
1292 rq->errors = -EIO;
1293 blk_mq_end_request(rq, rq->errors);
1294 return 0;
1295 }
1296
1297 return -1;
Shaohua Lif984df12015-05-08 10:51:32 -07001298}
1299
Jens Axboe07068d52014-05-22 10:40:51 -06001300/*
1301 * Multiple hardware queue variant. This will not use per-process plugs,
1302 * but will attempt to bypass the hctx queueing if we can go straight to
1303 * hardware for SYNC IO.
1304 */
Jens Axboedece1632015-11-05 10:41:16 -07001305static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001306{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001307 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1308 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Jens Axboe07068d52014-05-22 10:40:51 -06001309 struct blk_map_ctx data;
1310 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001311 unsigned int request_count = 0;
1312 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001313 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001314 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001315
1316 blk_queue_bounce(q, &bio);
1317
1318 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001319 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001320 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001321 }
1322
Kent Overstreet54efd502015-04-23 22:37:18 -07001323 blk_queue_split(q, &bio, q->bio_split);
1324
Omar Sandoval87c279e2016-06-01 22:18:48 -07001325 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1326 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1327 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001328
Jens Axboe07068d52014-05-22 10:40:51 -06001329 rq = blk_mq_map_request(q, bio, &data);
1330 if (unlikely(!rq))
Jens Axboedece1632015-11-05 10:41:16 -07001331 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001332
Jens Axboe7b371632015-11-05 10:41:40 -07001333 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
Jens Axboe07068d52014-05-22 10:40:51 -06001334
1335 if (unlikely(is_flush_fua)) {
1336 blk_mq_bio_to_request(rq, bio);
1337 blk_insert_flush(rq);
1338 goto run_queue;
1339 }
1340
Shaohua Lif984df12015-05-08 10:51:32 -07001341 plug = current->plug;
Jens Axboee167dfb2014-10-29 11:18:26 -06001342 /*
1343 * If the driver supports defer issued based on 'last', then
1344 * queue it up like normal since we can potentially save some
1345 * CPU this way.
1346 */
Shaohua Lif984df12015-05-08 10:51:32 -07001347 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1348 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1349 struct request *old_rq = NULL;
Jens Axboe07068d52014-05-22 10:40:51 -06001350
1351 blk_mq_bio_to_request(rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001352
1353 /*
Jens Axboeb094f892015-11-20 20:29:45 -07001354 * We do limited pluging. If the bio can be merged, do that.
Shaohua Lif984df12015-05-08 10:51:32 -07001355 * Otherwise the existing request in the plug list will be
1356 * issued. So the plug list will have one request at most
Jens Axboe07068d52014-05-22 10:40:51 -06001357 */
Shaohua Lif984df12015-05-08 10:51:32 -07001358 if (plug) {
Shaohua Li5b3f3412015-05-08 10:51:33 -07001359 /*
1360 * The plug list might get flushed before this. If that
Jens Axboeb094f892015-11-20 20:29:45 -07001361 * happens, same_queue_rq is invalid and plug list is
1362 * empty
1363 */
Shaohua Li5b3f3412015-05-08 10:51:33 -07001364 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1365 old_rq = same_queue_rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001366 list_del_init(&old_rq->queuelist);
Jens Axboe07068d52014-05-22 10:40:51 -06001367 }
Shaohua Lif984df12015-05-08 10:51:32 -07001368 list_add_tail(&rq->queuelist, &plug->mq_list);
1369 } else /* is_sync */
1370 old_rq = rq;
1371 blk_mq_put_ctx(data.ctx);
1372 if (!old_rq)
Jens Axboe7b371632015-11-05 10:41:40 -07001373 goto done;
1374 if (!blk_mq_direct_issue_request(old_rq, &cookie))
1375 goto done;
Shaohua Lif984df12015-05-08 10:51:32 -07001376 blk_mq_insert_request(old_rq, false, true, true);
Jens Axboe7b371632015-11-05 10:41:40 -07001377 goto done;
Jens Axboe07068d52014-05-22 10:40:51 -06001378 }
1379
1380 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1381 /*
1382 * For a SYNC request, send it to the hardware immediately. For
1383 * an ASYNC request, just ensure that we run it later on. The
1384 * latter allows for merging opportunities and more efficient
1385 * dispatching.
1386 */
1387run_queue:
1388 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1389 }
Jens Axboe07068d52014-05-22 10:40:51 -06001390 blk_mq_put_ctx(data.ctx);
Jens Axboe7b371632015-11-05 10:41:40 -07001391done:
1392 return cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001393}
1394
1395/*
1396 * Single hardware queue variant. This will attempt to use any per-process
1397 * plug for merging and IO deferral.
1398 */
Jens Axboedece1632015-11-05 10:41:16 -07001399static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001400{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001401 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1402 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Jeff Moyere6c44382015-05-08 10:51:30 -07001403 struct blk_plug *plug;
1404 unsigned int request_count = 0;
Jens Axboe07068d52014-05-22 10:40:51 -06001405 struct blk_map_ctx data;
1406 struct request *rq;
Jens Axboe7b371632015-11-05 10:41:40 -07001407 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001408
Jens Axboe07068d52014-05-22 10:40:51 -06001409 blk_queue_bounce(q, &bio);
1410
1411 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001412 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001413 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001414 }
1415
Kent Overstreet54efd502015-04-23 22:37:18 -07001416 blk_queue_split(q, &bio, q->bio_split);
1417
Omar Sandoval87c279e2016-06-01 22:18:48 -07001418 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1419 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1420 return BLK_QC_T_NONE;
1421 } else
1422 request_count = blk_plug_queued_count(q);
Jens Axboe07068d52014-05-22 10:40:51 -06001423
1424 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001425 if (unlikely(!rq))
Jens Axboedece1632015-11-05 10:41:16 -07001426 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01001427
Jens Axboe7b371632015-11-05 10:41:40 -07001428 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
Jens Axboe320ae512013-10-24 09:20:05 +01001429
1430 if (unlikely(is_flush_fua)) {
1431 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001432 blk_insert_flush(rq);
1433 goto run_queue;
1434 }
1435
1436 /*
1437 * A task plug currently exists. Since this is completely lockless,
1438 * utilize that to temporarily store requests until the task is
1439 * either done or scheduled away.
1440 */
Jeff Moyere6c44382015-05-08 10:51:30 -07001441 plug = current->plug;
1442 if (plug) {
1443 blk_mq_bio_to_request(rq, bio);
Ming Lei676d0602015-10-20 23:13:56 +08001444 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001445 trace_block_plug(q);
Jens Axboeb094f892015-11-20 20:29:45 -07001446
1447 blk_mq_put_ctx(data.ctx);
1448
1449 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001450 blk_flush_plug_list(plug, false);
1451 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001452 }
Jens Axboeb094f892015-11-20 20:29:45 -07001453
Jeff Moyere6c44382015-05-08 10:51:30 -07001454 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe7b371632015-11-05 10:41:40 -07001455 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001456 }
1457
Jens Axboe07068d52014-05-22 10:40:51 -06001458 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1459 /*
1460 * For a SYNC request, send it to the hardware immediately. For
1461 * an ASYNC request, just ensure that we run it later on. The
1462 * latter allows for merging opportunities and more efficient
1463 * dispatching.
1464 */
1465run_queue:
1466 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001467 }
1468
Jens Axboe07068d52014-05-22 10:40:51 -06001469 blk_mq_put_ctx(data.ctx);
Jens Axboe7b371632015-11-05 10:41:40 -07001470 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001471}
1472
1473/*
1474 * Default mapping to a software queue, since we use one per CPU.
1475 */
1476struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1477{
1478 return q->queue_hw_ctx[q->mq_map[cpu]];
1479}
1480EXPORT_SYMBOL(blk_mq_map_queue);
1481
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001482static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1483 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001484{
1485 struct page *page;
1486
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001487 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001488 int i;
1489
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001490 for (i = 0; i < tags->nr_tags; i++) {
1491 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001492 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001493 set->ops->exit_request(set->driver_data, tags->rqs[i],
1494 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001495 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001496 }
1497 }
1498
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001499 while (!list_empty(&tags->page_list)) {
1500 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001501 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001502 /*
1503 * Remove kmemleak object previously allocated in
1504 * blk_mq_init_rq_map().
1505 */
1506 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01001507 __free_pages(page, page->private);
1508 }
1509
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001510 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001511
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001512 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001513}
1514
1515static size_t order_to_size(unsigned int order)
1516{
Ming Lei4ca08502014-04-19 18:00:18 +08001517 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001518}
1519
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001520static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1521 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001522{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001523 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001524 unsigned int i, j, entries_per_page, max_order = 4;
1525 size_t rq_size, left;
1526
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001527 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
Shaohua Li24391c02015-01-23 14:18:00 -07001528 set->numa_node,
1529 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001530 if (!tags)
1531 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001532
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001533 INIT_LIST_HEAD(&tags->page_list);
1534
Jens Axboea5164402014-09-10 09:02:03 -06001535 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1536 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1537 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001538 if (!tags->rqs) {
1539 blk_mq_free_tags(tags);
1540 return NULL;
1541 }
Jens Axboe320ae512013-10-24 09:20:05 +01001542
1543 /*
1544 * rq_size is the size of the request plus driver payload, rounded
1545 * to the cacheline size
1546 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001547 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001548 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001549 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001550
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001551 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001552 int this_order = max_order;
1553 struct page *page;
1554 int to_do;
1555 void *p;
1556
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06001557 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01001558 this_order--;
1559
1560 do {
Jens Axboea5164402014-09-10 09:02:03 -06001561 page = alloc_pages_node(set->numa_node,
Linus Torvaldsac211172015-04-09 14:12:22 -07001562 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06001563 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001564 if (page)
1565 break;
1566 if (!this_order--)
1567 break;
1568 if (order_to_size(this_order) < rq_size)
1569 break;
1570 } while (1);
1571
1572 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001573 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001574
1575 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001576 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001577
1578 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001579 /*
1580 * Allow kmemleak to scan these pages as they contain pointers
1581 * to additional allocations like via ops->init_request().
1582 */
1583 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL);
Jens Axboe320ae512013-10-24 09:20:05 +01001584 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001585 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001586 left -= to_do * rq_size;
1587 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001588 tags->rqs[i] = p;
1589 if (set->ops->init_request) {
1590 if (set->ops->init_request(set->driver_data,
1591 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001592 set->numa_node)) {
1593 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001594 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001595 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001596 }
1597
Jens Axboe320ae512013-10-24 09:20:05 +01001598 p += rq_size;
1599 i++;
1600 }
1601 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001602 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001603
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001604fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001605 blk_mq_free_rq_map(set, tags, hctx_idx);
1606 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001607}
1608
Jens Axboe1429d7c2014-05-19 09:23:55 -06001609static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1610{
1611 kfree(bitmap->map);
1612}
1613
1614static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1615{
1616 unsigned int bpw = 8, total, num_maps, i;
1617
1618 bitmap->bits_per_word = bpw;
1619
1620 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1621 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1622 GFP_KERNEL, node);
1623 if (!bitmap->map)
1624 return -ENOMEM;
1625
Jens Axboe1429d7c2014-05-19 09:23:55 -06001626 total = nr_cpu_ids;
1627 for (i = 0; i < num_maps; i++) {
1628 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1629 total -= bitmap->map[i].depth;
1630 }
1631
1632 return 0;
1633}
1634
Jens Axboee57690f2016-08-24 15:34:35 -06001635/*
1636 * 'cpu' is going away. splice any existing rq_list entries from this
1637 * software queue to the hw queue dispatch list, and ensure that it
1638 * gets run.
1639 */
Jens Axboe484b4062014-05-21 14:01:15 -06001640static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1641{
Jens Axboe484b4062014-05-21 14:01:15 -06001642 struct blk_mq_ctx *ctx;
1643 LIST_HEAD(tmp);
1644
Jens Axboee57690f2016-08-24 15:34:35 -06001645 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Jens Axboe484b4062014-05-21 14:01:15 -06001646
1647 spin_lock(&ctx->lock);
1648 if (!list_empty(&ctx->rq_list)) {
1649 list_splice_init(&ctx->rq_list, &tmp);
1650 blk_mq_hctx_clear_pending(hctx, ctx);
1651 }
1652 spin_unlock(&ctx->lock);
1653
1654 if (list_empty(&tmp))
1655 return NOTIFY_OK;
1656
Jens Axboee57690f2016-08-24 15:34:35 -06001657 spin_lock(&hctx->lock);
1658 list_splice_tail_init(&tmp, &hctx->dispatch);
1659 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06001660
1661 blk_mq_run_hw_queue(hctx, true);
Jens Axboe484b4062014-05-21 14:01:15 -06001662 return NOTIFY_OK;
1663}
1664
Jens Axboe484b4062014-05-21 14:01:15 -06001665static int blk_mq_hctx_notify(void *data, unsigned long action,
1666 unsigned int cpu)
1667{
1668 struct blk_mq_hw_ctx *hctx = data;
1669
1670 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1671 return blk_mq_hctx_cpu_offline(hctx, cpu);
Ming Lei2a34c082015-04-21 10:00:20 +08001672
1673 /*
1674 * In case of CPU online, tags may be reallocated
1675 * in blk_mq_map_swqueue() after mapping is updated.
1676 */
Jens Axboe484b4062014-05-21 14:01:15 -06001677
1678 return NOTIFY_OK;
1679}
1680
Ming Leic3b4afc2015-06-04 22:25:04 +08001681/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08001682static void blk_mq_exit_hctx(struct request_queue *q,
1683 struct blk_mq_tag_set *set,
1684 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1685{
Ming Leif70ced02014-09-25 23:23:47 +08001686 unsigned flush_start_tag = set->queue_depth;
1687
Ming Lei08e98fc2014-09-25 23:23:38 +08001688 blk_mq_tag_idle(hctx);
1689
Ming Leif70ced02014-09-25 23:23:47 +08001690 if (set->ops->exit_request)
1691 set->ops->exit_request(set->driver_data,
1692 hctx->fq->flush_rq, hctx_idx,
1693 flush_start_tag + hctx_idx);
1694
Ming Lei08e98fc2014-09-25 23:23:38 +08001695 if (set->ops->exit_hctx)
1696 set->ops->exit_hctx(hctx, hctx_idx);
1697
1698 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Ming Leif70ced02014-09-25 23:23:47 +08001699 blk_free_flush_queue(hctx->fq);
Ming Lei08e98fc2014-09-25 23:23:38 +08001700 blk_mq_free_bitmap(&hctx->ctx_map);
1701}
1702
Ming Lei624dbe42014-05-27 23:35:13 +08001703static void blk_mq_exit_hw_queues(struct request_queue *q,
1704 struct blk_mq_tag_set *set, int nr_queue)
1705{
1706 struct blk_mq_hw_ctx *hctx;
1707 unsigned int i;
1708
1709 queue_for_each_hw_ctx(q, hctx, i) {
1710 if (i == nr_queue)
1711 break;
Ming Lei08e98fc2014-09-25 23:23:38 +08001712 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08001713 }
Ming Lei624dbe42014-05-27 23:35:13 +08001714}
1715
1716static void blk_mq_free_hw_queues(struct request_queue *q,
1717 struct blk_mq_tag_set *set)
1718{
1719 struct blk_mq_hw_ctx *hctx;
1720 unsigned int i;
1721
Ming Leie09aae72015-01-29 20:17:27 +08001722 queue_for_each_hw_ctx(q, hctx, i)
Ming Lei624dbe42014-05-27 23:35:13 +08001723 free_cpumask_var(hctx->cpumask);
Ming Lei624dbe42014-05-27 23:35:13 +08001724}
1725
Ming Lei08e98fc2014-09-25 23:23:38 +08001726static int blk_mq_init_hctx(struct request_queue *q,
1727 struct blk_mq_tag_set *set,
1728 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1729{
1730 int node;
Ming Leif70ced02014-09-25 23:23:47 +08001731 unsigned flush_start_tag = set->queue_depth;
Ming Lei08e98fc2014-09-25 23:23:38 +08001732
1733 node = hctx->numa_node;
1734 if (node == NUMA_NO_NODE)
1735 node = hctx->numa_node = set->numa_node;
1736
1737 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1738 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1739 spin_lock_init(&hctx->lock);
1740 INIT_LIST_HEAD(&hctx->dispatch);
1741 hctx->queue = q;
1742 hctx->queue_num = hctx_idx;
Jeff Moyer2404e602015-11-03 10:40:06 -05001743 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08001744
1745 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1746 blk_mq_hctx_notify, hctx);
1747 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1748
1749 hctx->tags = set->tags[hctx_idx];
1750
1751 /*
1752 * Allocate space for all possible cpus to avoid allocation at
1753 * runtime
1754 */
1755 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1756 GFP_KERNEL, node);
1757 if (!hctx->ctxs)
1758 goto unregister_cpu_notifier;
1759
1760 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1761 goto free_ctxs;
1762
1763 hctx->nr_ctx = 0;
1764
1765 if (set->ops->init_hctx &&
1766 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1767 goto free_bitmap;
1768
Ming Leif70ced02014-09-25 23:23:47 +08001769 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1770 if (!hctx->fq)
1771 goto exit_hctx;
1772
1773 if (set->ops->init_request &&
1774 set->ops->init_request(set->driver_data,
1775 hctx->fq->flush_rq, hctx_idx,
1776 flush_start_tag + hctx_idx, node))
1777 goto free_fq;
1778
Ming Lei08e98fc2014-09-25 23:23:38 +08001779 return 0;
1780
Ming Leif70ced02014-09-25 23:23:47 +08001781 free_fq:
1782 kfree(hctx->fq);
1783 exit_hctx:
1784 if (set->ops->exit_hctx)
1785 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001786 free_bitmap:
1787 blk_mq_free_bitmap(&hctx->ctx_map);
1788 free_ctxs:
1789 kfree(hctx->ctxs);
1790 unregister_cpu_notifier:
1791 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1792
1793 return -1;
1794}
1795
Jens Axboe320ae512013-10-24 09:20:05 +01001796static void blk_mq_init_cpu_queues(struct request_queue *q,
1797 unsigned int nr_hw_queues)
1798{
1799 unsigned int i;
1800
1801 for_each_possible_cpu(i) {
1802 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1803 struct blk_mq_hw_ctx *hctx;
1804
1805 memset(__ctx, 0, sizeof(*__ctx));
1806 __ctx->cpu = i;
1807 spin_lock_init(&__ctx->lock);
1808 INIT_LIST_HEAD(&__ctx->rq_list);
1809 __ctx->queue = q;
1810
1811 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001812 if (!cpu_online(i))
1813 continue;
1814
Jens Axboee4043dc2014-04-09 10:18:23 -06001815 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001816
Jens Axboe320ae512013-10-24 09:20:05 +01001817 /*
1818 * Set local node, IFF we have more than one hw queue. If
1819 * not, we remain on the home node of the device
1820 */
1821 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Raghavendra K Tbffed452015-12-02 16:59:05 +05301822 hctx->numa_node = local_memory_node(cpu_to_node(i));
Jens Axboe320ae512013-10-24 09:20:05 +01001823 }
1824}
1825
Akinobu Mita57783222015-09-27 02:09:23 +09001826static void blk_mq_map_swqueue(struct request_queue *q,
1827 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01001828{
1829 unsigned int i;
1830 struct blk_mq_hw_ctx *hctx;
1831 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08001832 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001833
Akinobu Mita60de0742015-09-27 02:09:25 +09001834 /*
1835 * Avoid others reading imcomplete hctx->cpumask through sysfs
1836 */
1837 mutex_lock(&q->sysfs_lock);
1838
Jens Axboe320ae512013-10-24 09:20:05 +01001839 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001840 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001841 hctx->nr_ctx = 0;
1842 }
1843
1844 /*
1845 * Map software to hardware queues
1846 */
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01001847 for_each_possible_cpu(i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001848 /* If the cpu isn't online, the cpu is mapped to first hctx */
Akinobu Mita57783222015-09-27 02:09:23 +09001849 if (!cpumask_test_cpu(i, online_mask))
Jens Axboee4043dc2014-04-09 10:18:23 -06001850 continue;
1851
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01001852 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001853 hctx = q->mq_ops->map_queue(q, i);
Keith Busch868f2f02015-12-17 17:08:14 -07001854
Jens Axboee4043dc2014-04-09 10:18:23 -06001855 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001856 ctx->index_hw = hctx->nr_ctx;
1857 hctx->ctxs[hctx->nr_ctx++] = ctx;
1858 }
Jens Axboe506e9312014-05-07 10:26:44 -06001859
Akinobu Mita60de0742015-09-27 02:09:25 +09001860 mutex_unlock(&q->sysfs_lock);
1861
Jens Axboe506e9312014-05-07 10:26:44 -06001862 queue_for_each_hw_ctx(q, hctx, i) {
Chong Yuan889fa312015-04-15 11:39:29 -06001863 struct blk_mq_ctxmap *map = &hctx->ctx_map;
1864
Jens Axboe484b4062014-05-21 14:01:15 -06001865 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001866 * If no software queues are mapped to this hardware queue,
1867 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06001868 */
1869 if (!hctx->nr_ctx) {
Jens Axboe484b4062014-05-21 14:01:15 -06001870 if (set->tags[i]) {
1871 blk_mq_free_rq_map(set, set->tags[i], i);
1872 set->tags[i] = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001873 }
Ming Lei2a34c082015-04-21 10:00:20 +08001874 hctx->tags = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001875 continue;
1876 }
1877
Ming Lei2a34c082015-04-21 10:00:20 +08001878 /* unmapped hw queue can be remapped after CPU topo changed */
1879 if (!set->tags[i])
1880 set->tags[i] = blk_mq_init_rq_map(set, i);
1881 hctx->tags = set->tags[i];
1882 WARN_ON(!hctx->tags);
1883
Raghavendra K Te0e827b2015-12-02 16:57:06 +05301884 cpumask_copy(hctx->tags->cpumask, hctx->cpumask);
Jens Axboe484b4062014-05-21 14:01:15 -06001885 /*
Chong Yuan889fa312015-04-15 11:39:29 -06001886 * Set the map size to the number of mapped software queues.
1887 * This is more accurate and more efficient than looping
1888 * over all possibly mapped software queues.
1889 */
Jens Axboe569fd0c2015-04-17 08:28:50 -06001890 map->size = DIV_ROUND_UP(hctx->nr_ctx, map->bits_per_word);
Chong Yuan889fa312015-04-15 11:39:29 -06001891
1892 /*
Jens Axboe484b4062014-05-21 14:01:15 -06001893 * Initialize batch roundrobin counts
1894 */
Jens Axboe506e9312014-05-07 10:26:44 -06001895 hctx->next_cpu = cpumask_first(hctx->cpumask);
1896 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1897 }
Jens Axboe320ae512013-10-24 09:20:05 +01001898}
1899
Jeff Moyer2404e602015-11-03 10:40:06 -05001900static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06001901{
1902 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06001903 int i;
1904
Jeff Moyer2404e602015-11-03 10:40:06 -05001905 queue_for_each_hw_ctx(q, hctx, i) {
1906 if (shared)
1907 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1908 else
1909 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1910 }
1911}
1912
1913static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
1914{
1915 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06001916
1917 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1918 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05001919 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001920 blk_mq_unfreeze_queue(q);
1921 }
1922}
1923
1924static void blk_mq_del_queue_tag_set(struct request_queue *q)
1925{
1926 struct blk_mq_tag_set *set = q->tag_set;
1927
Jens Axboe0d2602c2014-05-13 15:10:52 -06001928 mutex_lock(&set->tag_list_lock);
1929 list_del_init(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05001930 if (list_is_singular(&set->tag_list)) {
1931 /* just transitioned to unshared */
1932 set->flags &= ~BLK_MQ_F_TAG_SHARED;
1933 /* update existing queue */
1934 blk_mq_update_tag_set_depth(set, false);
1935 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001936 mutex_unlock(&set->tag_list_lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001937}
1938
1939static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1940 struct request_queue *q)
1941{
1942 q->tag_set = set;
1943
1944 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05001945
1946 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1947 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
1948 set->flags |= BLK_MQ_F_TAG_SHARED;
1949 /* update existing queue */
1950 blk_mq_update_tag_set_depth(set, true);
1951 }
1952 if (set->flags & BLK_MQ_F_TAG_SHARED)
1953 queue_set_hctx_shared(q, true);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001954 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05001955
Jens Axboe0d2602c2014-05-13 15:10:52 -06001956 mutex_unlock(&set->tag_list_lock);
1957}
1958
Ming Leie09aae72015-01-29 20:17:27 +08001959/*
1960 * It is the actual release handler for mq, but we do it from
1961 * request queue's release handler for avoiding use-after-free
1962 * and headache because q->mq_kobj shouldn't have been introduced,
1963 * but we can't group ctx/kctx kobj without it.
1964 */
1965void blk_mq_release(struct request_queue *q)
1966{
1967 struct blk_mq_hw_ctx *hctx;
1968 unsigned int i;
1969
1970 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08001971 queue_for_each_hw_ctx(q, hctx, i) {
1972 if (!hctx)
1973 continue;
1974 kfree(hctx->ctxs);
Ming Leie09aae72015-01-29 20:17:27 +08001975 kfree(hctx);
Ming Leic3b4afc2015-06-04 22:25:04 +08001976 }
Ming Leie09aae72015-01-29 20:17:27 +08001977
Akinobu Mitaa723bab2015-09-27 02:09:21 +09001978 kfree(q->mq_map);
1979 q->mq_map = NULL;
1980
Ming Leie09aae72015-01-29 20:17:27 +08001981 kfree(q->queue_hw_ctx);
1982
1983 /* ctx kobj stays in queue_ctx */
1984 free_percpu(q->queue_ctx);
1985}
1986
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001987struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001988{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04001989 struct request_queue *uninit_q, *q;
1990
1991 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1992 if (!uninit_q)
1993 return ERR_PTR(-ENOMEM);
1994
1995 q = blk_mq_init_allocated_queue(set, uninit_q);
1996 if (IS_ERR(q))
1997 blk_cleanup_queue(uninit_q);
1998
1999 return q;
2000}
2001EXPORT_SYMBOL(blk_mq_init_queue);
2002
Keith Busch868f2f02015-12-17 17:08:14 -07002003static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2004 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002005{
Keith Busch868f2f02015-12-17 17:08:14 -07002006 int i, j;
2007 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01002008
Keith Busch868f2f02015-12-17 17:08:14 -07002009 blk_mq_sysfs_unregister(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002010 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002011 int node;
Jens Axboef14bbe72014-05-27 12:06:53 -06002012
Keith Busch868f2f02015-12-17 17:08:14 -07002013 if (hctxs[i])
2014 continue;
2015
2016 node = blk_mq_hw_queue_to_node(q->mq_map, i);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02002017 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
2018 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01002019 if (!hctxs[i])
Keith Busch868f2f02015-12-17 17:08:14 -07002020 break;
Jens Axboe320ae512013-10-24 09:20:05 +01002021
Jens Axboea86073e2014-10-13 15:41:54 -06002022 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
Keith Busch868f2f02015-12-17 17:08:14 -07002023 node)) {
2024 kfree(hctxs[i]);
2025 hctxs[i] = NULL;
2026 break;
2027 }
Jens Axboee4043dc2014-04-09 10:18:23 -06002028
Jens Axboe0d2602c2014-05-13 15:10:52 -06002029 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06002030 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01002031 hctxs[i]->queue_num = i;
Keith Busch868f2f02015-12-17 17:08:14 -07002032
2033 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2034 free_cpumask_var(hctxs[i]->cpumask);
2035 kfree(hctxs[i]);
2036 hctxs[i] = NULL;
2037 break;
2038 }
2039 blk_mq_hctx_kobj_init(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01002040 }
Keith Busch868f2f02015-12-17 17:08:14 -07002041 for (j = i; j < q->nr_hw_queues; j++) {
2042 struct blk_mq_hw_ctx *hctx = hctxs[j];
2043
2044 if (hctx) {
2045 if (hctx->tags) {
2046 blk_mq_free_rq_map(set, hctx->tags, j);
2047 set->tags[j] = NULL;
2048 }
2049 blk_mq_exit_hctx(q, set, hctx, j);
2050 free_cpumask_var(hctx->cpumask);
2051 kobject_put(&hctx->kobj);
2052 kfree(hctx->ctxs);
2053 kfree(hctx);
2054 hctxs[j] = NULL;
2055
2056 }
2057 }
2058 q->nr_hw_queues = i;
2059 blk_mq_sysfs_register(q);
2060}
2061
2062struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2063 struct request_queue *q)
2064{
Ming Lei66841672016-02-12 15:27:00 +08002065 /* mark the queue as mq asap */
2066 q->mq_ops = set->ops;
2067
Keith Busch868f2f02015-12-17 17:08:14 -07002068 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2069 if (!q->queue_ctx)
Ming Linc7de5722016-05-25 23:23:27 -07002070 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002071
2072 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2073 GFP_KERNEL, set->numa_node);
2074 if (!q->queue_hw_ctx)
2075 goto err_percpu;
2076
2077 q->mq_map = blk_mq_make_queue_map(set);
2078 if (!q->mq_map)
2079 goto err_map;
2080
2081 blk_mq_realloc_hw_ctxs(set, q);
2082 if (!q->nr_hw_queues)
2083 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002084
Christoph Hellwig287922e2015-10-30 20:57:30 +08002085 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002086 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002087
2088 q->nr_queues = nr_cpu_ids;
Jens Axboe320ae512013-10-24 09:20:05 +01002089
Jens Axboe94eddfb2013-11-19 09:25:07 -07002090 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002091
Jens Axboe05f1dd52014-05-29 09:53:32 -06002092 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2093 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2094
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002095 q->sg_reserved_size = INT_MAX;
2096
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002097 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
2098 INIT_LIST_HEAD(&q->requeue_list);
2099 spin_lock_init(&q->requeue_lock);
2100
Jens Axboe07068d52014-05-22 10:40:51 -06002101 if (q->nr_hw_queues > 1)
2102 blk_queue_make_request(q, blk_mq_make_request);
2103 else
2104 blk_queue_make_request(q, blk_sq_make_request);
2105
Jens Axboeeba71762014-05-20 15:17:27 -06002106 /*
2107 * Do this after blk_queue_make_request() overrides it...
2108 */
2109 q->nr_requests = set->queue_depth;
2110
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002111 if (set->ops->complete)
2112 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08002113
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002114 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002115
Akinobu Mita57783222015-09-27 02:09:23 +09002116 get_online_cpus();
Jens Axboe320ae512013-10-24 09:20:05 +01002117 mutex_lock(&all_q_mutex);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002118
Jens Axboe320ae512013-10-24 09:20:05 +01002119 list_add_tail(&q->all_q_node, &all_q_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002120 blk_mq_add_queue_tag_set(set, q);
Akinobu Mita57783222015-09-27 02:09:23 +09002121 blk_mq_map_swqueue(q, cpu_online_mask);
Jens Axboe484b4062014-05-21 14:01:15 -06002122
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002123 mutex_unlock(&all_q_mutex);
Akinobu Mita57783222015-09-27 02:09:23 +09002124 put_online_cpus();
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002125
Jens Axboe320ae512013-10-24 09:20:05 +01002126 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002127
Jens Axboe320ae512013-10-24 09:20:05 +01002128err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002129 kfree(q->mq_map);
Jens Axboef14bbe72014-05-27 12:06:53 -06002130err_map:
Keith Busch868f2f02015-12-17 17:08:14 -07002131 kfree(q->queue_hw_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002132err_percpu:
Keith Busch868f2f02015-12-17 17:08:14 -07002133 free_percpu(q->queue_ctx);
Ming Linc7de5722016-05-25 23:23:27 -07002134err_exit:
2135 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002136 return ERR_PTR(-ENOMEM);
2137}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002138EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002139
2140void blk_mq_free_queue(struct request_queue *q)
2141{
Ming Lei624dbe42014-05-27 23:35:13 +08002142 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002143
Akinobu Mita0e626362015-09-27 02:09:22 +09002144 mutex_lock(&all_q_mutex);
2145 list_del_init(&q->all_q_node);
2146 mutex_unlock(&all_q_mutex);
2147
Jens Axboe0d2602c2014-05-13 15:10:52 -06002148 blk_mq_del_queue_tag_set(q);
2149
Ming Lei624dbe42014-05-27 23:35:13 +08002150 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2151 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01002152}
Jens Axboe320ae512013-10-24 09:20:05 +01002153
2154/* Basically redo blk_mq_init_queue with queue frozen */
Akinobu Mita57783222015-09-27 02:09:23 +09002155static void blk_mq_queue_reinit(struct request_queue *q,
2156 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01002157{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +02002158 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
Jens Axboe320ae512013-10-24 09:20:05 +01002159
Jens Axboe67aec142014-05-30 08:25:36 -06002160 blk_mq_sysfs_unregister(q);
2161
Akinobu Mita57783222015-09-27 02:09:23 +09002162 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues, online_mask);
Jens Axboe320ae512013-10-24 09:20:05 +01002163
2164 /*
2165 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2166 * we should change hctx numa_node according to new topology (this
2167 * involves free and re-allocate memory, worthy doing?)
2168 */
2169
Akinobu Mita57783222015-09-27 02:09:23 +09002170 blk_mq_map_swqueue(q, online_mask);
Jens Axboe320ae512013-10-24 09:20:05 +01002171
Jens Axboe67aec142014-05-30 08:25:36 -06002172 blk_mq_sysfs_register(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002173}
2174
Paul Gortmakerf618ef72013-11-14 08:26:02 -07002175static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
2176 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01002177{
2178 struct request_queue *q;
Akinobu Mita57783222015-09-27 02:09:23 +09002179 int cpu = (unsigned long)hcpu;
2180 /*
2181 * New online cpumask which is going to be set in this hotplug event.
2182 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2183 * one-by-one and dynamically allocating this could result in a failure.
2184 */
2185 static struct cpumask online_new;
Jens Axboe320ae512013-10-24 09:20:05 +01002186
2187 /*
Akinobu Mita57783222015-09-27 02:09:23 +09002188 * Before hotadded cpu starts handling requests, new mappings must
2189 * be established. Otherwise, these requests in hw queue might
2190 * never be dispatched.
2191 *
2192 * For example, there is a single hw queue (hctx) and two CPU queues
2193 * (ctx0 for CPU0, and ctx1 for CPU1).
2194 *
2195 * Now CPU1 is just onlined and a request is inserted into
2196 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2197 * still zero.
2198 *
2199 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2200 * set in pending bitmap and tries to retrieve requests in
2201 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2202 * so the request in ctx1->rq_list is ignored.
Jens Axboe320ae512013-10-24 09:20:05 +01002203 */
Akinobu Mita57783222015-09-27 02:09:23 +09002204 switch (action & ~CPU_TASKS_FROZEN) {
2205 case CPU_DEAD:
2206 case CPU_UP_CANCELED:
2207 cpumask_copy(&online_new, cpu_online_mask);
2208 break;
2209 case CPU_UP_PREPARE:
2210 cpumask_copy(&online_new, cpu_online_mask);
2211 cpumask_set_cpu(cpu, &online_new);
2212 break;
2213 default:
Jens Axboe320ae512013-10-24 09:20:05 +01002214 return NOTIFY_OK;
Akinobu Mita57783222015-09-27 02:09:23 +09002215 }
Jens Axboe320ae512013-10-24 09:20:05 +01002216
2217 mutex_lock(&all_q_mutex);
Tejun Heof3af0202014-11-04 13:52:27 -05002218
2219 /*
2220 * We need to freeze and reinit all existing queues. Freezing
2221 * involves synchronous wait for an RCU grace period and doing it
2222 * one by one may take a long time. Start freezing all queues in
2223 * one swoop and then wait for the completions so that freezing can
2224 * take place in parallel.
2225 */
2226 list_for_each_entry(q, &all_q_list, all_q_node)
2227 blk_mq_freeze_queue_start(q);
Ming Leif054b562015-04-21 10:00:19 +08002228 list_for_each_entry(q, &all_q_list, all_q_node) {
Tejun Heof3af0202014-11-04 13:52:27 -05002229 blk_mq_freeze_queue_wait(q);
2230
Ming Leif054b562015-04-21 10:00:19 +08002231 /*
2232 * timeout handler can't touch hw queue during the
2233 * reinitialization
2234 */
2235 del_timer_sync(&q->timeout);
2236 }
2237
Jens Axboe320ae512013-10-24 09:20:05 +01002238 list_for_each_entry(q, &all_q_list, all_q_node)
Akinobu Mita57783222015-09-27 02:09:23 +09002239 blk_mq_queue_reinit(q, &online_new);
Tejun Heof3af0202014-11-04 13:52:27 -05002240
2241 list_for_each_entry(q, &all_q_list, all_q_node)
2242 blk_mq_unfreeze_queue(q);
2243
Jens Axboe320ae512013-10-24 09:20:05 +01002244 mutex_unlock(&all_q_mutex);
2245 return NOTIFY_OK;
2246}
2247
Jens Axboea5164402014-09-10 09:02:03 -06002248static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2249{
2250 int i;
2251
2252 for (i = 0; i < set->nr_hw_queues; i++) {
2253 set->tags[i] = blk_mq_init_rq_map(set, i);
2254 if (!set->tags[i])
2255 goto out_unwind;
2256 }
2257
2258 return 0;
2259
2260out_unwind:
2261 while (--i >= 0)
2262 blk_mq_free_rq_map(set, set->tags[i], i);
2263
Jens Axboea5164402014-09-10 09:02:03 -06002264 return -ENOMEM;
2265}
2266
2267/*
2268 * Allocate the request maps associated with this tag_set. Note that this
2269 * may reduce the depth asked for, if memory is tight. set->queue_depth
2270 * will be updated to reflect the allocated depth.
2271 */
2272static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2273{
2274 unsigned int depth;
2275 int err;
2276
2277 depth = set->queue_depth;
2278 do {
2279 err = __blk_mq_alloc_rq_maps(set);
2280 if (!err)
2281 break;
2282
2283 set->queue_depth >>= 1;
2284 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2285 err = -ENOMEM;
2286 break;
2287 }
2288 } while (set->queue_depth);
2289
2290 if (!set->queue_depth || err) {
2291 pr_err("blk-mq: failed to allocate request map\n");
2292 return -ENOMEM;
2293 }
2294
2295 if (depth != set->queue_depth)
2296 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2297 depth, set->queue_depth);
2298
2299 return 0;
2300}
2301
Keith Buschf26cdc82015-06-01 09:29:53 -06002302struct cpumask *blk_mq_tags_cpumask(struct blk_mq_tags *tags)
2303{
2304 return tags->cpumask;
2305}
2306EXPORT_SYMBOL_GPL(blk_mq_tags_cpumask);
2307
Jens Axboea4391c62014-06-05 15:21:56 -06002308/*
2309 * Alloc a tag set to be associated with one or more request queues.
2310 * May fail with EINVAL for various error conditions. May adjust the
2311 * requested depth down, if if it too large. In that case, the set
2312 * value will be stored in set->queue_depth.
2313 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002314int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2315{
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002316 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2317
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002318 if (!set->nr_hw_queues)
2319 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002320 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002321 return -EINVAL;
2322 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2323 return -EINVAL;
2324
Xiaoguang Wangf9018ac2015-03-30 13:19:14 +08002325 if (!set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002326 return -EINVAL;
2327
Jens Axboea4391c62014-06-05 15:21:56 -06002328 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2329 pr_info("blk-mq: reduced tag depth to %u\n",
2330 BLK_MQ_MAX_DEPTH);
2331 set->queue_depth = BLK_MQ_MAX_DEPTH;
2332 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002333
Shaohua Li6637fad2014-11-30 16:00:58 -08002334 /*
2335 * If a crashdump is active, then we are potentially in a very
2336 * memory constrained environment. Limit us to 1 queue and
2337 * 64 tags to prevent using too much memory.
2338 */
2339 if (is_kdump_kernel()) {
2340 set->nr_hw_queues = 1;
2341 set->queue_depth = min(64U, set->queue_depth);
2342 }
Keith Busch868f2f02015-12-17 17:08:14 -07002343 /*
2344 * There is no use for more h/w queues than cpus.
2345 */
2346 if (set->nr_hw_queues > nr_cpu_ids)
2347 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08002348
Keith Busch868f2f02015-12-17 17:08:14 -07002349 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002350 GFP_KERNEL, set->numa_node);
2351 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002352 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002353
Jens Axboea5164402014-09-10 09:02:03 -06002354 if (blk_mq_alloc_rq_maps(set))
2355 goto enomem;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002356
Jens Axboe0d2602c2014-05-13 15:10:52 -06002357 mutex_init(&set->tag_list_lock);
2358 INIT_LIST_HEAD(&set->tag_list);
2359
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002360 return 0;
Jens Axboea5164402014-09-10 09:02:03 -06002361enomem:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002362 kfree(set->tags);
2363 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002364 return -ENOMEM;
2365}
2366EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2367
2368void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2369{
2370 int i;
2371
Keith Busch868f2f02015-12-17 17:08:14 -07002372 for (i = 0; i < nr_cpu_ids; i++) {
Junichi Nomuraf42d79a2015-10-14 05:02:15 +00002373 if (set->tags[i])
Jens Axboe484b4062014-05-21 14:01:15 -06002374 blk_mq_free_rq_map(set, set->tags[i], i);
2375 }
2376
Ming Lei981bd182014-04-24 00:07:34 +08002377 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002378 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002379}
2380EXPORT_SYMBOL(blk_mq_free_tag_set);
2381
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002382int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2383{
2384 struct blk_mq_tag_set *set = q->tag_set;
2385 struct blk_mq_hw_ctx *hctx;
2386 int i, ret;
2387
2388 if (!set || nr > set->queue_depth)
2389 return -EINVAL;
2390
2391 ret = 0;
2392 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07002393 if (!hctx->tags)
2394 continue;
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002395 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2396 if (ret)
2397 break;
2398 }
2399
2400 if (!ret)
2401 q->nr_requests = nr;
2402
2403 return ret;
2404}
2405
Keith Busch868f2f02015-12-17 17:08:14 -07002406void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2407{
2408 struct request_queue *q;
2409
2410 if (nr_hw_queues > nr_cpu_ids)
2411 nr_hw_queues = nr_cpu_ids;
2412 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2413 return;
2414
2415 list_for_each_entry(q, &set->tag_list, tag_set_list)
2416 blk_mq_freeze_queue(q);
2417
2418 set->nr_hw_queues = nr_hw_queues;
2419 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2420 blk_mq_realloc_hw_ctxs(set, q);
2421
2422 if (q->nr_hw_queues > 1)
2423 blk_queue_make_request(q, blk_mq_make_request);
2424 else
2425 blk_queue_make_request(q, blk_sq_make_request);
2426
2427 blk_mq_queue_reinit(q, cpu_online_mask);
2428 }
2429
2430 list_for_each_entry(q, &set->tag_list, tag_set_list)
2431 blk_mq_unfreeze_queue(q);
2432}
2433EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2434
Jens Axboe676141e2014-03-20 13:29:18 -06002435void blk_mq_disable_hotplug(void)
2436{
2437 mutex_lock(&all_q_mutex);
2438}
2439
2440void blk_mq_enable_hotplug(void)
2441{
2442 mutex_unlock(&all_q_mutex);
2443}
2444
Jens Axboe320ae512013-10-24 09:20:05 +01002445static int __init blk_mq_init(void)
2446{
Jens Axboe320ae512013-10-24 09:20:05 +01002447 blk_mq_cpu_init();
2448
Tejun Heoadd703f2014-07-01 10:34:38 -06002449 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002450
2451 return 0;
2452}
2453subsys_initcall(blk_mq_init);