blob: a3060078a8da0b653f6114c805bf59b715005da0 [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 Axboe88c7b2b2016-08-25 08:07:30 -060025#include <linux/prefetch.h>
Jens Axboe320ae512013-10-24 09:20:05 +010026
27#include <trace/events/block.h>
28
29#include <linux/blk-mq.h>
30#include "blk.h"
31#include "blk-mq.h"
32#include "blk-mq-tag.h"
33
34static DEFINE_MUTEX(all_q_mutex);
35static LIST_HEAD(all_q_list);
36
37static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
38
Jens Axboe320ae512013-10-24 09:20:05 +010039/*
40 * Check if any of the ctx's have pending work in this hardware queue
41 */
42static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
43{
44 unsigned int i;
45
Jens Axboe569fd0c2015-04-17 08:28:50 -060046 for (i = 0; i < hctx->ctx_map.size; i++)
Jens Axboe1429d7c2014-05-19 09:23:55 -060047 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010048 return true;
49
50 return false;
51}
52
Jens Axboe1429d7c2014-05-19 09:23:55 -060053static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
54 struct blk_mq_ctx *ctx)
55{
56 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
57}
58
59#define CTX_TO_BIT(hctx, ctx) \
60 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
61
Jens Axboe320ae512013-10-24 09:20:05 +010062/*
63 * Mark this ctx as having pending work in this hardware queue
64 */
65static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
66 struct blk_mq_ctx *ctx)
67{
Jens Axboe1429d7c2014-05-19 09:23:55 -060068 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
69
70 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
71 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
72}
73
74static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
75 struct blk_mq_ctx *ctx)
76{
77 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
78
79 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010080}
81
Keith Buschb4c6a022014-12-19 17:54:14 -070082void blk_mq_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +080083{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020084 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -040085
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020086 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
87 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -040088 percpu_ref_kill(&q->q_usage_counter);
Mike Snitzerb94ec292015-03-11 23:56:38 -040089 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -040090 }
Tejun Heof3af0202014-11-04 13:52:27 -050091}
Keith Buschb4c6a022014-12-19 17:54:14 -070092EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -050093
94static void blk_mq_freeze_queue_wait(struct request_queue *q)
95{
Dan Williams3ef28e82015-10-21 13:20:12 -040096 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +080097}
98
Tejun Heof3af0202014-11-04 13:52:27 -050099/*
100 * Guarantee no request is in use, so we can change any data structure of
101 * the queue afterward.
102 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400103void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500104{
Dan Williams3ef28e82015-10-21 13:20:12 -0400105 /*
106 * In the !blk_mq case we are only calling this to kill the
107 * q_usage_counter, otherwise this increases the freeze depth
108 * and waits for it to return to zero. For this reason there is
109 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
110 * exported to drivers as the only user for unfreeze is blk_mq.
111 */
Tejun Heof3af0202014-11-04 13:52:27 -0500112 blk_mq_freeze_queue_start(q);
113 blk_mq_freeze_queue_wait(q);
114}
Dan Williams3ef28e82015-10-21 13:20:12 -0400115
116void blk_mq_freeze_queue(struct request_queue *q)
117{
118 /*
119 * ...just an alias to keep freeze and unfreeze actions balanced
120 * in the blk_mq_* namespace
121 */
122 blk_freeze_queue(q);
123}
Jens Axboec761d962015-01-02 15:05:12 -0700124EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500125
Keith Buschb4c6a022014-12-19 17:54:14 -0700126void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100127{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200128 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100129
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200130 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
131 WARN_ON_ONCE(freeze_depth < 0);
132 if (!freeze_depth) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400133 percpu_ref_reinit(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100134 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600135 }
Jens Axboe320ae512013-10-24 09:20:05 +0100136}
Keith Buschb4c6a022014-12-19 17:54:14 -0700137EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100138
Jens Axboeaed3ea92014-12-22 14:04:42 -0700139void blk_mq_wake_waiters(struct request_queue *q)
140{
141 struct blk_mq_hw_ctx *hctx;
142 unsigned int i;
143
144 queue_for_each_hw_ctx(q, hctx, i)
145 if (blk_mq_hw_queue_mapped(hctx))
146 blk_mq_tag_wakeup_all(hctx->tags, true);
Keith Busch3fd59402015-01-08 08:53:56 -0700147
148 /*
149 * If we are called because the queue has now been marked as
150 * dying, we need to ensure that processes currently waiting on
151 * the queue are notified as well.
152 */
153 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700154}
155
Jens Axboe320ae512013-10-24 09:20:05 +0100156bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
157{
158 return blk_mq_has_free_tags(hctx->tags);
159}
160EXPORT_SYMBOL(blk_mq_can_queue);
161
Jens Axboe94eddfb2013-11-19 09:25:07 -0700162static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
Mike Christiecc6e3b12016-06-05 14:32:12 -0500163 struct request *rq, int op,
164 unsigned int op_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100165{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700166 if (blk_queue_io_stat(q))
Mike Christiecc6e3b12016-06-05 14:32:12 -0500167 op_flags |= REQ_IO_STAT;
Jens Axboe94eddfb2013-11-19 09:25:07 -0700168
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200169 INIT_LIST_HEAD(&rq->queuelist);
170 /* csd/requeue_work/fifo_time is initialized before use */
171 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100172 rq->mq_ctx = ctx;
Mike Christiecc6e3b12016-06-05 14:32:12 -0500173 req_set_op_attrs(rq, op, op_flags);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200174 /* do not touch atomic flags, it needs atomic ops against the timer */
175 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200176 INIT_HLIST_NODE(&rq->hash);
177 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200178 rq->rq_disk = NULL;
179 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600180 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200181#ifdef CONFIG_BLK_CGROUP
182 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700183 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200184 rq->io_start_time_ns = 0;
185#endif
186 rq->nr_phys_segments = 0;
187#if defined(CONFIG_BLK_DEV_INTEGRITY)
188 rq->nr_integrity_segments = 0;
189#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200190 rq->special = NULL;
191 /* tag was already set */
192 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200193
Tony Battersby6f4a16262014-08-22 15:53:39 -0400194 rq->cmd = rq->__cmd;
195
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200196 rq->extra_len = 0;
197 rq->sense_len = 0;
198 rq->resid_len = 0;
199 rq->sense = NULL;
200
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200201 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600202 rq->timeout = 0;
203
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200204 rq->end_io = NULL;
205 rq->end_io_data = NULL;
206 rq->next_rq = NULL;
207
Mike Christied9d8c5c2016-06-05 14:32:16 -0500208 ctx->rq_dispatched[rw_is_sync(op, op_flags)]++;
Jens Axboe320ae512013-10-24 09:20:05 +0100209}
210
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200211static struct request *
Mike Christiecc6e3b12016-06-05 14:32:12 -0500212__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int op, int op_flags)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200213{
214 struct request *rq;
215 unsigned int tag;
216
Ming Leicb96a42c2014-06-01 00:43:37 +0800217 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200218 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a42c2014-06-01 00:43:37 +0800219 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200220
Ming Leicb96a42c2014-06-01 00:43:37 +0800221 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200222 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a42c2014-06-01 00:43:37 +0800223 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200224 }
225
226 rq->tag = tag;
Mike Christiecc6e3b12016-06-05 14:32:12 -0500227 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op, op_flags);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200228 return rq;
229 }
230
231 return NULL;
232}
233
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100234struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
235 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100236{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200237 struct blk_mq_ctx *ctx;
238 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100239 struct request *rq;
Ming Leicb96a42c2014-06-01 00:43:37 +0800240 struct blk_mq_alloc_data alloc_data;
Joe Lawrencea492f072014-08-28 08:15:21 -0600241 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100242
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100243 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
Joe Lawrencea492f072014-08-28 08:15:21 -0600244 if (ret)
245 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100246
Christoph Hellwigd8525642014-05-27 20:59:50 +0200247 ctx = blk_mq_get_ctx(q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200248 hctx = blk_mq_map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100249 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200250
Mike Christiecc6e3b12016-06-05 14:32:12 -0500251 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100252 if (!rq && !(flags & BLK_MQ_REQ_NOWAIT)) {
Christoph Hellwigd8525642014-05-27 20:59:50 +0200253 __blk_mq_run_hw_queue(hctx);
254 blk_mq_put_ctx(ctx);
255
256 ctx = blk_mq_get_ctx(q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200257 hctx = blk_mq_map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100258 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -0500259 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
Ming Leicb96a42c2014-06-01 00:43:37 +0800260 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200261 }
262 blk_mq_put_ctx(ctx);
Keith Buschc76541a2014-12-19 17:54:13 -0700263 if (!rq) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400264 blk_queue_exit(q);
Joe Lawrencea492f072014-08-28 08:15:21 -0600265 return ERR_PTR(-EWOULDBLOCK);
Keith Buschc76541a2014-12-19 17:54:13 -0700266 }
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200267
268 rq->__data_len = 0;
269 rq->__sector = (sector_t) -1;
270 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100271 return rq;
272}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600273EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100274
Ming Lin1f5bd332016-06-13 16:45:21 +0200275struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
276 unsigned int flags, unsigned int hctx_idx)
277{
278 struct blk_mq_hw_ctx *hctx;
279 struct blk_mq_ctx *ctx;
280 struct request *rq;
281 struct blk_mq_alloc_data alloc_data;
282 int ret;
283
284 /*
285 * If the tag allocator sleeps we could get an allocation for a
286 * different hardware context. No need to complicate the low level
287 * allocator for this for the rare use case of a command tied to
288 * a specific queue.
289 */
290 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
291 return ERR_PTR(-EINVAL);
292
293 if (hctx_idx >= q->nr_hw_queues)
294 return ERR_PTR(-EIO);
295
296 ret = blk_queue_enter(q, true);
297 if (ret)
298 return ERR_PTR(ret);
299
300 hctx = q->queue_hw_ctx[hctx_idx];
301 ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask));
302
303 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
304 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
305 if (!rq) {
306 blk_queue_exit(q);
307 return ERR_PTR(-EWOULDBLOCK);
308 }
309
310 return rq;
311}
312EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
313
Jens Axboe320ae512013-10-24 09:20:05 +0100314static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
315 struct blk_mq_ctx *ctx, struct request *rq)
316{
317 const int tag = rq->tag;
318 struct request_queue *q = rq->q;
319
Jens Axboe0d2602c2014-05-13 15:10:52 -0600320 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
321 atomic_dec(&hctx->nr_active);
David Hildenbrand683d0e12014-09-18 11:04:31 +0200322 rq->cmd_flags = 0;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600323
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200324 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600325 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Dan Williams3ef28e82015-10-21 13:20:12 -0400326 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100327}
328
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700329void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100330{
331 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700332
333 ctx->rq_completed[rq_is_sync(rq)]++;
334 __blk_mq_free_request(hctx, ctx, rq);
335
336}
337EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
338
339void blk_mq_free_request(struct request *rq)
340{
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200341 blk_mq_free_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100342}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700343EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100344
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700345inline void __blk_mq_end_request(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100346{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700347 blk_account_io_done(rq);
348
Christoph Hellwig91b63632014-04-16 09:44:53 +0200349 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100350 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200351 } else {
352 if (unlikely(blk_bidi_rq(rq)))
353 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100354 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200355 }
Jens Axboe320ae512013-10-24 09:20:05 +0100356}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700357EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200358
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700359void blk_mq_end_request(struct request *rq, int error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200360{
361 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
362 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700363 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200364}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700365EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100366
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800367static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100368{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800369 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100370
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800371 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100372}
373
Jens Axboeed851862014-05-30 21:20:50 -0600374static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100375{
376 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700377 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100378 int cpu;
379
Christoph Hellwig38535202014-04-25 02:32:53 -0700380 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800381 rq->q->softirq_done_fn(rq);
382 return;
383 }
Jens Axboe320ae512013-10-24 09:20:05 +0100384
385 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700386 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
387 shared = cpus_share_cache(cpu, ctx->cpu);
388
389 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800390 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800391 rq->csd.info = rq;
392 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100393 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800394 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800395 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800396 }
Jens Axboe320ae512013-10-24 09:20:05 +0100397 put_cpu();
398}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800399
Jens Axboe1fa8cc52015-11-05 14:32:55 -0700400static void __blk_mq_complete_request(struct request *rq)
Jens Axboeed851862014-05-30 21:20:50 -0600401{
402 struct request_queue *q = rq->q;
403
404 if (!q->softirq_done_fn)
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700405 blk_mq_end_request(rq, rq->errors);
Jens Axboeed851862014-05-30 21:20:50 -0600406 else
407 blk_mq_ipi_complete_request(rq);
408}
409
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800410/**
411 * blk_mq_complete_request - end I/O on a request
412 * @rq: the request being processed
413 *
414 * Description:
415 * Ends all I/O on a request. It does not handle partial completions.
416 * The actual completion happens out-of-order, through a IPI handler.
417 **/
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200418void blk_mq_complete_request(struct request *rq, int error)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800419{
Jens Axboe95f09682014-05-27 17:46:48 -0600420 struct request_queue *q = rq->q;
421
422 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800423 return;
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200424 if (!blk_mark_rq_complete(rq)) {
425 rq->errors = error;
Jens Axboeed851862014-05-30 21:20:50 -0600426 __blk_mq_complete_request(rq);
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200427 }
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800428}
429EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100430
Keith Busch973c0192015-01-07 18:55:43 -0700431int blk_mq_request_started(struct request *rq)
432{
433 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
434}
435EXPORT_SYMBOL_GPL(blk_mq_request_started);
436
Christoph Hellwige2490072014-09-13 16:40:09 -0700437void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100438{
439 struct request_queue *q = rq->q;
440
441 trace_block_rq_issue(q, rq);
442
Christoph Hellwig742ee692014-04-14 10:30:06 +0200443 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200444 if (unlikely(blk_bidi_rq(rq)))
445 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200446
Ming Lei2b8393b2014-06-10 00:16:41 +0800447 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600448
449 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600450 * Ensure that ->deadline is visible before set the started
451 * flag and clear the completed flag.
452 */
453 smp_mb__before_atomic();
454
455 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600456 * Mark us as started and clear complete. Complete might have been
457 * set if requeue raced with timeout, which then marked it as
458 * complete. So be sure to clear complete again when we start
459 * the request, otherwise we'll ignore the completion event.
460 */
Jens Axboe4b570522014-05-29 11:00:11 -0600461 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
462 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
463 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
464 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800465
466 if (q->dma_drain_size && blk_rq_bytes(rq)) {
467 /*
468 * Make sure space for the drain appears. We know we can do
469 * this because max_hw_segments has been adjusted to be one
470 * fewer than the device can handle.
471 */
472 rq->nr_phys_segments++;
473 }
Jens Axboe320ae512013-10-24 09:20:05 +0100474}
Christoph Hellwige2490072014-09-13 16:40:09 -0700475EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100476
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200477static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100478{
479 struct request_queue *q = rq->q;
480
481 trace_block_rq_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800482
Christoph Hellwige2490072014-09-13 16:40:09 -0700483 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
484 if (q->dma_drain_size && blk_rq_bytes(rq))
485 rq->nr_phys_segments--;
486 }
Jens Axboe320ae512013-10-24 09:20:05 +0100487}
488
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200489void blk_mq_requeue_request(struct request *rq)
490{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200491 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200492
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200493 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600494 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200495}
496EXPORT_SYMBOL(blk_mq_requeue_request);
497
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600498static void blk_mq_requeue_work(struct work_struct *work)
499{
500 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400501 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600502 LIST_HEAD(rq_list);
503 struct request *rq, *next;
504 unsigned long flags;
505
506 spin_lock_irqsave(&q->requeue_lock, flags);
507 list_splice_init(&q->requeue_list, &rq_list);
508 spin_unlock_irqrestore(&q->requeue_lock, flags);
509
510 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
511 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
512 continue;
513
514 rq->cmd_flags &= ~REQ_SOFTBARRIER;
515 list_del_init(&rq->queuelist);
516 blk_mq_insert_request(rq, true, false, false);
517 }
518
519 while (!list_empty(&rq_list)) {
520 rq = list_entry(rq_list.next, struct request, queuelist);
521 list_del_init(&rq->queuelist);
522 blk_mq_insert_request(rq, false, false, false);
523 }
524
Jens Axboe8b957412014-09-19 13:10:29 -0600525 /*
526 * Use the start variant of queue running here, so that running
527 * the requeue work will kick stopped queues.
528 */
529 blk_mq_start_hw_queues(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600530}
531
532void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
533{
534 struct request_queue *q = rq->q;
535 unsigned long flags;
536
537 /*
538 * We abuse this flag that is otherwise used by the I/O scheduler to
539 * request head insertation from the workqueue.
540 */
541 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
542
543 spin_lock_irqsave(&q->requeue_lock, flags);
544 if (at_head) {
545 rq->cmd_flags |= REQ_SOFTBARRIER;
546 list_add(&rq->queuelist, &q->requeue_list);
547 } else {
548 list_add_tail(&rq->queuelist, &q->requeue_list);
549 }
550 spin_unlock_irqrestore(&q->requeue_lock, flags);
551}
552EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
553
Keith Buschc68ed592015-01-07 18:55:44 -0700554void blk_mq_cancel_requeue_work(struct request_queue *q)
555{
Mike Snitzer28494502016-09-14 13:28:30 -0400556 cancel_delayed_work_sync(&q->requeue_work);
Keith Buschc68ed592015-01-07 18:55:44 -0700557}
558EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work);
559
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600560void blk_mq_kick_requeue_list(struct request_queue *q)
561{
Mike Snitzer28494502016-09-14 13:28:30 -0400562 kblockd_schedule_delayed_work(&q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600563}
564EXPORT_SYMBOL(blk_mq_kick_requeue_list);
565
Mike Snitzer28494502016-09-14 13:28:30 -0400566void blk_mq_delay_kick_requeue_list(struct request_queue *q,
567 unsigned long msecs)
568{
569 kblockd_schedule_delayed_work(&q->requeue_work,
570 msecs_to_jiffies(msecs));
571}
572EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
573
Jens Axboe1885b242015-01-07 18:55:45 -0700574void blk_mq_abort_requeue_list(struct request_queue *q)
575{
576 unsigned long flags;
577 LIST_HEAD(rq_list);
578
579 spin_lock_irqsave(&q->requeue_lock, flags);
580 list_splice_init(&q->requeue_list, &rq_list);
581 spin_unlock_irqrestore(&q->requeue_lock, flags);
582
583 while (!list_empty(&rq_list)) {
584 struct request *rq;
585
586 rq = list_first_entry(&rq_list, struct request, queuelist);
587 list_del_init(&rq->queuelist);
588 rq->errors = -EIO;
589 blk_mq_end_request(rq, rq->errors);
590 }
591}
592EXPORT_SYMBOL(blk_mq_abort_requeue_list);
593
Jens Axboe0e62f512014-06-04 10:23:49 -0600594struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
595{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600596 if (tag < tags->nr_tags) {
597 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700598 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600599 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700600
601 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600602}
603EXPORT_SYMBOL(blk_mq_tag_to_rq);
604
Jens Axboe320ae512013-10-24 09:20:05 +0100605struct blk_mq_timeout_data {
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700606 unsigned long next;
607 unsigned int next_set;
Jens Axboe320ae512013-10-24 09:20:05 +0100608};
609
Christoph Hellwig90415832014-09-22 10:21:48 -0600610void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100611{
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700612 struct blk_mq_ops *ops = req->q->mq_ops;
613 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600614
615 /*
616 * We know that complete is set at this point. If STARTED isn't set
617 * anymore, then the request isn't active and the "timeout" should
618 * just be ignored. This can happen due to the bitflag ordering.
619 * Timeout first checks if STARTED is set, and if it is, assumes
620 * the request is active. But if we race with completion, then
621 * we both flags will get cleared. So check here again, and ignore
622 * a timeout event with a request that isn't active.
623 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700624 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
625 return;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600626
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700627 if (ops->timeout)
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700628 ret = ops->timeout(req, reserved);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600629
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700630 switch (ret) {
631 case BLK_EH_HANDLED:
632 __blk_mq_complete_request(req);
633 break;
634 case BLK_EH_RESET_TIMER:
635 blk_add_timer(req);
636 blk_clear_rq_complete(req);
637 break;
638 case BLK_EH_NOT_HANDLED:
639 break;
640 default:
641 printk(KERN_ERR "block: bad eh return: %d\n", ret);
642 break;
643 }
Jens Axboe87ee7b12014-04-24 08:51:47 -0600644}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700645
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700646static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
647 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100648{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700649 struct blk_mq_timeout_data *data = priv;
650
Keith Buscheb130db2015-01-08 08:59:53 -0700651 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
652 /*
653 * If a request wasn't started before the queue was
654 * marked dying, kill it here or it'll go unnoticed.
655 */
Keith Buscha59e0f52016-02-11 13:05:38 -0700656 if (unlikely(blk_queue_dying(rq->q))) {
657 rq->errors = -EIO;
658 blk_mq_end_request(rq, rq->errors);
659 }
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700660 return;
Keith Buscheb130db2015-01-08 08:59:53 -0700661 }
Jens Axboe320ae512013-10-24 09:20:05 +0100662
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700663 if (time_after_eq(jiffies, rq->deadline)) {
664 if (!blk_mark_rq_complete(rq))
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700665 blk_mq_rq_timed_out(rq, reserved);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700666 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
667 data->next = rq->deadline;
668 data->next_set = 1;
669 }
Jens Axboe320ae512013-10-24 09:20:05 +0100670}
671
Christoph Hellwig287922e2015-10-30 20:57:30 +0800672static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100673{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800674 struct request_queue *q =
675 container_of(work, struct request_queue, timeout_work);
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700676 struct blk_mq_timeout_data data = {
677 .next = 0,
678 .next_set = 0,
679 };
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700680 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100681
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600682 /* A deadlock might occur if a request is stuck requiring a
683 * timeout at the same time a queue freeze is waiting
684 * completion, since the timeout code would not be able to
685 * acquire the queue reference here.
686 *
687 * That's why we don't use blk_queue_enter here; instead, we use
688 * percpu_ref_tryget directly, because we need to be able to
689 * obtain a reference even in the short window between the queue
690 * starting to freeze, by dropping the first reference in
691 * blk_mq_freeze_queue_start, and the moment the last request is
692 * consumed, marked by the instant q_usage_counter reaches
693 * zero.
694 */
695 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800696 return;
697
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200698 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
Jens Axboe320ae512013-10-24 09:20:05 +0100699
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700700 if (data.next_set) {
701 data.next = blk_rq_timeout(round_jiffies_up(data.next));
702 mod_timer(&q->timeout, data.next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600703 } else {
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200704 struct blk_mq_hw_ctx *hctx;
705
Ming Leif054b562015-04-21 10:00:19 +0800706 queue_for_each_hw_ctx(q, hctx, i) {
707 /* the hctx may be unmapped, so check it here */
708 if (blk_mq_hw_queue_mapped(hctx))
709 blk_mq_tag_idle(hctx);
710 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600711 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800712 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100713}
714
715/*
716 * Reverse check our software queue for entries that we could potentially
717 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
718 * too much time checking for merges.
719 */
720static bool blk_mq_attempt_merge(struct request_queue *q,
721 struct blk_mq_ctx *ctx, struct bio *bio)
722{
723 struct request *rq;
724 int checked = 8;
725
726 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
727 int el_ret;
728
729 if (!checked--)
730 break;
731
732 if (!blk_rq_merge_ok(rq, bio))
733 continue;
734
735 el_ret = blk_try_merge(rq, bio);
736 if (el_ret == ELEVATOR_BACK_MERGE) {
737 if (bio_attempt_back_merge(q, rq, bio)) {
738 ctx->rq_merged++;
739 return true;
740 }
741 break;
742 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
743 if (bio_attempt_front_merge(q, rq, bio)) {
744 ctx->rq_merged++;
745 return true;
746 }
747 break;
748 }
749 }
750
751 return false;
752}
753
Jens Axboe320ae512013-10-24 09:20:05 +0100754/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600755 * Process software queues that have been marked busy, splicing them
756 * to the for-dispatch
757 */
758static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
759{
760 struct blk_mq_ctx *ctx;
761 int i;
762
Jens Axboe569fd0c2015-04-17 08:28:50 -0600763 for (i = 0; i < hctx->ctx_map.size; i++) {
Jens Axboe1429d7c2014-05-19 09:23:55 -0600764 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
765 unsigned int off, bit;
766
767 if (!bm->word)
768 continue;
769
770 bit = 0;
771 off = i * hctx->ctx_map.bits_per_word;
772 do {
773 bit = find_next_bit(&bm->word, bm->depth, bit);
774 if (bit >= bm->depth)
775 break;
776
777 ctx = hctx->ctxs[bit + off];
778 clear_bit(bit, &bm->word);
779 spin_lock(&ctx->lock);
780 list_splice_tail_init(&ctx->rq_list, list);
781 spin_unlock(&ctx->lock);
782
783 bit++;
784 } while (1);
785 }
786}
787
788/*
Jens Axboe320ae512013-10-24 09:20:05 +0100789 * Run this hardware queue, pulling any software queues mapped to it in.
790 * Note that this function currently has various problems around ordering
791 * of IO. In particular, we'd like FIFO behaviour on handling existing
792 * items on the hctx->dispatch list. Ignore that for now.
793 */
794static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
795{
796 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100797 struct request *rq;
798 LIST_HEAD(rq_list);
Jens Axboe74c45052014-10-29 11:14:52 -0600799 LIST_HEAD(driver_list);
800 struct list_head *dptr;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600801 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100802
Jens Axboe5d12f902014-03-19 15:25:02 -0600803 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100804 return;
805
Jens Axboe0e87e582016-08-24 15:38:01 -0600806 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
807 cpu_online(hctx->next_cpu));
808
Jens Axboe320ae512013-10-24 09:20:05 +0100809 hctx->run++;
810
811 /*
812 * Touch any software queue that has pending entries.
813 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600814 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100815
816 /*
817 * If we have previous entries on our dispatch list, grab them
818 * and stuff them at the front for more fair dispatch.
819 */
820 if (!list_empty_careful(&hctx->dispatch)) {
821 spin_lock(&hctx->lock);
822 if (!list_empty(&hctx->dispatch))
823 list_splice_init(&hctx->dispatch, &rq_list);
824 spin_unlock(&hctx->lock);
825 }
826
827 /*
Jens Axboe74c45052014-10-29 11:14:52 -0600828 * Start off with dptr being NULL, so we start the first request
829 * immediately, even if we have more pending.
830 */
831 dptr = NULL;
832
833 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100834 * Now process all the entries, sending them to the driver.
835 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600836 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100837 while (!list_empty(&rq_list)) {
Jens Axboe74c45052014-10-29 11:14:52 -0600838 struct blk_mq_queue_data bd;
Jens Axboe320ae512013-10-24 09:20:05 +0100839 int ret;
840
841 rq = list_first_entry(&rq_list, struct request, queuelist);
842 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100843
Jens Axboe74c45052014-10-29 11:14:52 -0600844 bd.rq = rq;
845 bd.list = dptr;
846 bd.last = list_empty(&rq_list);
847
848 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe320ae512013-10-24 09:20:05 +0100849 switch (ret) {
850 case BLK_MQ_RQ_QUEUE_OK:
851 queued++;
Omar Sandoval52b9c332016-06-08 18:22:20 -0700852 break;
Jens Axboe320ae512013-10-24 09:20:05 +0100853 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100854 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200855 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100856 break;
857 default:
858 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100859 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800860 rq->errors = -EIO;
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700861 blk_mq_end_request(rq, rq->errors);
Jens Axboe320ae512013-10-24 09:20:05 +0100862 break;
863 }
864
865 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
866 break;
Jens Axboe74c45052014-10-29 11:14:52 -0600867
868 /*
869 * We've done the first request. If we have more than 1
870 * left in the list, set dptr to defer issue.
871 */
872 if (!dptr && rq_list.next != rq_list.prev)
873 dptr = &driver_list;
Jens Axboe320ae512013-10-24 09:20:05 +0100874 }
875
876 if (!queued)
877 hctx->dispatched[0]++;
878 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
879 hctx->dispatched[ilog2(queued) + 1]++;
880
881 /*
882 * Any items that need requeuing? Stuff them into hctx->dispatch,
883 * that is where we will continue on next queue run.
884 */
885 if (!list_empty(&rq_list)) {
886 spin_lock(&hctx->lock);
887 list_splice(&rq_list, &hctx->dispatch);
888 spin_unlock(&hctx->lock);
Shaohua Li9ba52e52015-05-04 14:32:48 -0600889 /*
890 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
891 * it's possible the queue is stopped and restarted again
892 * before this. Queue restart will dispatch requests. And since
893 * requests in rq_list aren't added into hctx->dispatch yet,
894 * the requests in rq_list might get lost.
895 *
896 * blk_mq_run_hw_queue() already checks the STOPPED bit
897 **/
898 blk_mq_run_hw_queue(hctx, true);
Jens Axboe320ae512013-10-24 09:20:05 +0100899 }
900}
901
Jens Axboe506e9312014-05-07 10:26:44 -0600902/*
903 * It'd be great if the workqueue API had a way to pass
904 * in a mask and had some smarts for more clever placement.
905 * For now we just round-robin here, switching for every
906 * BLK_MQ_CPU_WORK_BATCH queued items.
907 */
908static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
909{
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100910 if (hctx->queue->nr_hw_queues == 1)
911 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -0600912
913 if (--hctx->next_cpu_batch <= 0) {
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100914 int cpu = hctx->next_cpu, next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600915
916 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
917 if (next_cpu >= nr_cpu_ids)
918 next_cpu = cpumask_first(hctx->cpumask);
919
920 hctx->next_cpu = next_cpu;
921 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100922
923 return cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600924 }
925
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100926 return hctx->next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600927}
928
Jens Axboe320ae512013-10-24 09:20:05 +0100929void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
930{
Ming Lei19c66e52014-12-03 19:38:04 +0800931 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state) ||
932 !blk_mq_hw_queue_mapped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +0100933 return;
934
Paolo Bonzini398205b2014-11-07 23:03:59 +0100935 if (!async) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100936 int cpu = get_cpu();
937 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +0100938 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100939 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +0100940 return;
941 }
Jens Axboee4043dc2014-04-09 10:18:23 -0600942
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100943 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -0600944 }
Paolo Bonzini398205b2014-11-07 23:03:59 +0100945
Jens Axboe27489a32016-08-24 15:54:25 -0600946 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100947}
948
Mike Snitzerb94ec292015-03-11 23:56:38 -0400949void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100950{
951 struct blk_mq_hw_ctx *hctx;
952 int i;
953
954 queue_for_each_hw_ctx(q, hctx, i) {
955 if ((!blk_mq_hctx_has_pending(hctx) &&
956 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600957 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100958 continue;
959
Mike Snitzerb94ec292015-03-11 23:56:38 -0400960 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +0100961 }
962}
Mike Snitzerb94ec292015-03-11 23:56:38 -0400963EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +0100964
965void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
966{
Jens Axboe27489a32016-08-24 15:54:25 -0600967 cancel_work(&hctx->run_work);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600968 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100969 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
970}
971EXPORT_SYMBOL(blk_mq_stop_hw_queue);
972
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100973void blk_mq_stop_hw_queues(struct request_queue *q)
974{
975 struct blk_mq_hw_ctx *hctx;
976 int i;
977
978 queue_for_each_hw_ctx(q, hctx, i)
979 blk_mq_stop_hw_queue(hctx);
980}
981EXPORT_SYMBOL(blk_mq_stop_hw_queues);
982
Jens Axboe320ae512013-10-24 09:20:05 +0100983void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
984{
985 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600986
Jens Axboe0ffbce82014-06-25 08:22:34 -0600987 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100988}
989EXPORT_SYMBOL(blk_mq_start_hw_queue);
990
Christoph Hellwig2f268552014-04-16 09:44:56 +0200991void blk_mq_start_hw_queues(struct request_queue *q)
992{
993 struct blk_mq_hw_ctx *hctx;
994 int i;
995
996 queue_for_each_hw_ctx(q, hctx, i)
997 blk_mq_start_hw_queue(hctx);
998}
999EXPORT_SYMBOL(blk_mq_start_hw_queues);
1000
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001001void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001002{
1003 struct blk_mq_hw_ctx *hctx;
1004 int i;
1005
1006 queue_for_each_hw_ctx(q, hctx, i) {
1007 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
1008 continue;
1009
1010 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001011 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001012 }
1013}
1014EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1015
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001016static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001017{
1018 struct blk_mq_hw_ctx *hctx;
1019
Jens Axboe27489a32016-08-24 15:54:25 -06001020 hctx = container_of(work, struct blk_mq_hw_ctx, run_work);
Jens Axboee4043dc2014-04-09 10:18:23 -06001021
Jens Axboe320ae512013-10-24 09:20:05 +01001022 __blk_mq_run_hw_queue(hctx);
1023}
1024
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001025static void blk_mq_delay_work_fn(struct work_struct *work)
1026{
1027 struct blk_mq_hw_ctx *hctx;
1028
1029 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1030
1031 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1032 __blk_mq_run_hw_queue(hctx);
1033}
1034
1035void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1036{
Ming Lei19c66e52014-12-03 19:38:04 +08001037 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1038 return;
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001039
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001040 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1041 &hctx->delay_work, msecs_to_jiffies(msecs));
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001042}
1043EXPORT_SYMBOL(blk_mq_delay_queue);
1044
Ming Leicfd0c552015-10-20 23:13:57 +08001045static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001046 struct request *rq,
1047 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001048{
Jens Axboee57690f2016-08-24 15:34:35 -06001049 struct blk_mq_ctx *ctx = rq->mq_ctx;
1050
Jens Axboe01b983c2013-11-19 18:59:10 -07001051 trace_block_rq_insert(hctx->queue, rq);
1052
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001053 if (at_head)
1054 list_add(&rq->queuelist, &ctx->rq_list);
1055 else
1056 list_add_tail(&rq->queuelist, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001057}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001058
Ming Leicfd0c552015-10-20 23:13:57 +08001059static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
1060 struct request *rq, bool at_head)
1061{
1062 struct blk_mq_ctx *ctx = rq->mq_ctx;
1063
Jens Axboee57690f2016-08-24 15:34:35 -06001064 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001065 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001066}
1067
Christoph Hellwigeeabc852014-03-21 08:57:37 -06001068void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
Jens Axboee57690f2016-08-24 15:34:35 -06001069 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001070{
Jens Axboee57690f2016-08-24 15:34:35 -06001071 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001072 struct request_queue *q = rq->q;
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001073 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001074
Christoph Hellwiga57a1782014-09-16 14:44:07 -07001075 spin_lock(&ctx->lock);
1076 __blk_mq_insert_request(hctx, rq, at_head);
1077 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001078
Jens Axboe320ae512013-10-24 09:20:05 +01001079 if (run_queue)
1080 blk_mq_run_hw_queue(hctx, async);
1081}
1082
1083static void blk_mq_insert_requests(struct request_queue *q,
1084 struct blk_mq_ctx *ctx,
1085 struct list_head *list,
1086 int depth,
1087 bool from_schedule)
1088
1089{
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001090 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001091
1092 trace_block_unplug(q, depth, !from_schedule);
1093
Jens Axboe320ae512013-10-24 09:20:05 +01001094 /*
1095 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1096 * offline now
1097 */
1098 spin_lock(&ctx->lock);
1099 while (!list_empty(list)) {
1100 struct request *rq;
1101
1102 rq = list_first_entry(list, struct request, queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001103 BUG_ON(rq->mq_ctx != ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001104 list_del_init(&rq->queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001105 __blk_mq_insert_req_list(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001106 }
Ming Leicfd0c552015-10-20 23:13:57 +08001107 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001108 spin_unlock(&ctx->lock);
1109
Jens Axboe320ae512013-10-24 09:20:05 +01001110 blk_mq_run_hw_queue(hctx, from_schedule);
1111}
1112
1113static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1114{
1115 struct request *rqa = container_of(a, struct request, queuelist);
1116 struct request *rqb = container_of(b, struct request, queuelist);
1117
1118 return !(rqa->mq_ctx < rqb->mq_ctx ||
1119 (rqa->mq_ctx == rqb->mq_ctx &&
1120 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1121}
1122
1123void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1124{
1125 struct blk_mq_ctx *this_ctx;
1126 struct request_queue *this_q;
1127 struct request *rq;
1128 LIST_HEAD(list);
1129 LIST_HEAD(ctx_list);
1130 unsigned int depth;
1131
1132 list_splice_init(&plug->mq_list, &list);
1133
1134 list_sort(NULL, &list, plug_ctx_cmp);
1135
1136 this_q = NULL;
1137 this_ctx = NULL;
1138 depth = 0;
1139
1140 while (!list_empty(&list)) {
1141 rq = list_entry_rq(list.next);
1142 list_del_init(&rq->queuelist);
1143 BUG_ON(!rq->q);
1144 if (rq->mq_ctx != this_ctx) {
1145 if (this_ctx) {
1146 blk_mq_insert_requests(this_q, this_ctx,
1147 &ctx_list, depth,
1148 from_schedule);
1149 }
1150
1151 this_ctx = rq->mq_ctx;
1152 this_q = rq->q;
1153 depth = 0;
1154 }
1155
1156 depth++;
1157 list_add_tail(&rq->queuelist, &ctx_list);
1158 }
1159
1160 /*
1161 * If 'this_ctx' is set, we know we have entries to complete
1162 * on 'ctx_list'. Do those.
1163 */
1164 if (this_ctx) {
1165 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1166 from_schedule);
1167 }
1168}
1169
1170static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1171{
1172 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001173
Michael Callahana21f2a32016-05-03 11:12:49 -04001174 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001175}
1176
Jens Axboe274a5842014-08-15 12:44:08 -06001177static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1178{
1179 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1180 !blk_queue_nomerges(hctx->queue);
1181}
1182
Jens Axboe07068d52014-05-22 10:40:51 -06001183static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1184 struct blk_mq_ctx *ctx,
1185 struct request *rq, struct bio *bio)
1186{
Ming Leie18378a2015-10-20 23:13:54 +08001187 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001188 blk_mq_bio_to_request(rq, bio);
1189 spin_lock(&ctx->lock);
1190insert_rq:
1191 __blk_mq_insert_request(hctx, rq, false);
1192 spin_unlock(&ctx->lock);
1193 return false;
1194 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001195 struct request_queue *q = hctx->queue;
1196
Jens Axboe07068d52014-05-22 10:40:51 -06001197 spin_lock(&ctx->lock);
1198 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1199 blk_mq_bio_to_request(rq, bio);
1200 goto insert_rq;
1201 }
1202
1203 spin_unlock(&ctx->lock);
1204 __blk_mq_free_request(hctx, ctx, rq);
1205 return true;
1206 }
1207}
1208
1209struct blk_map_ctx {
1210 struct blk_mq_hw_ctx *hctx;
1211 struct blk_mq_ctx *ctx;
1212};
1213
1214static struct request *blk_mq_map_request(struct request_queue *q,
1215 struct bio *bio,
1216 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001217{
1218 struct blk_mq_hw_ctx *hctx;
1219 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001220 struct request *rq;
Mike Christiecc6e3b12016-06-05 14:32:12 -05001221 int op = bio_data_dir(bio);
1222 int op_flags = 0;
Ming Leicb96a42c2014-06-01 00:43:37 +08001223 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001224
Dan Williams3ef28e82015-10-21 13:20:12 -04001225 blk_queue_enter_live(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001226 ctx = blk_mq_get_ctx(q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001227 hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001228
Jens Axboe1eff9d32016-08-05 15:35:16 -06001229 if (rw_is_sync(bio_op(bio), bio->bi_opf))
Mike Christiecc6e3b12016-06-05 14:32:12 -05001230 op_flags |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001231
Mike Christiecc6e3b12016-06-05 14:32:12 -05001232 trace_block_getrq(q, bio, op);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001233 blk_mq_set_alloc_data(&alloc_data, q, BLK_MQ_REQ_NOWAIT, ctx, hctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -05001234 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001235 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001236 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001237 blk_mq_put_ctx(ctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -05001238 trace_block_sleeprq(q, bio, op);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001239
1240 ctx = blk_mq_get_ctx(q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001241 hctx = blk_mq_map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001242 blk_mq_set_alloc_data(&alloc_data, q, 0, ctx, hctx);
Mike Christiecc6e3b12016-06-05 14:32:12 -05001243 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
Ming Leicb96a42c2014-06-01 00:43:37 +08001244 ctx = alloc_data.ctx;
1245 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001246 }
1247
1248 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001249 data->hctx = hctx;
1250 data->ctx = ctx;
1251 return rq;
1252}
1253
Jens Axboe7b371632015-11-05 10:41:40 -07001254static int blk_mq_direct_issue_request(struct request *rq, blk_qc_t *cookie)
Shaohua Lif984df12015-05-08 10:51:32 -07001255{
1256 int ret;
1257 struct request_queue *q = rq->q;
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001258 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
Shaohua Lif984df12015-05-08 10:51:32 -07001259 struct blk_mq_queue_data bd = {
1260 .rq = rq,
1261 .list = NULL,
1262 .last = 1
1263 };
Jens Axboe7b371632015-11-05 10:41:40 -07001264 blk_qc_t new_cookie = blk_tag_to_qc_t(rq->tag, hctx->queue_num);
Shaohua Lif984df12015-05-08 10:51:32 -07001265
1266 /*
1267 * For OK queue, we are done. For error, kill it. Any other
1268 * error (busy), just add it to our list as we previously
1269 * would have done
1270 */
1271 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe7b371632015-11-05 10:41:40 -07001272 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1273 *cookie = new_cookie;
Shaohua Lif984df12015-05-08 10:51:32 -07001274 return 0;
Shaohua Lif984df12015-05-08 10:51:32 -07001275 }
Jens Axboe7b371632015-11-05 10:41:40 -07001276
1277 __blk_mq_requeue_request(rq);
1278
1279 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1280 *cookie = BLK_QC_T_NONE;
1281 rq->errors = -EIO;
1282 blk_mq_end_request(rq, rq->errors);
1283 return 0;
1284 }
1285
1286 return -1;
Shaohua Lif984df12015-05-08 10:51:32 -07001287}
1288
Jens Axboe07068d52014-05-22 10:40:51 -06001289/*
1290 * Multiple hardware queue variant. This will not use per-process plugs,
1291 * but will attempt to bypass the hctx queueing if we can go straight to
1292 * hardware for SYNC IO.
1293 */
Jens Axboedece1632015-11-05 10:41:16 -07001294static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001295{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001296 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1297 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Jens Axboe07068d52014-05-22 10:40:51 -06001298 struct blk_map_ctx data;
1299 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001300 unsigned int request_count = 0;
1301 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001302 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001303 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001304
1305 blk_queue_bounce(q, &bio);
1306
1307 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001308 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001309 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001310 }
1311
Kent Overstreet54efd502015-04-23 22:37:18 -07001312 blk_queue_split(q, &bio, q->bio_split);
1313
Omar Sandoval87c279e2016-06-01 22:18:48 -07001314 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1315 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1316 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001317
Jens Axboe07068d52014-05-22 10:40:51 -06001318 rq = blk_mq_map_request(q, bio, &data);
1319 if (unlikely(!rq))
Jens Axboedece1632015-11-05 10:41:16 -07001320 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001321
Jens Axboe7b371632015-11-05 10:41:40 -07001322 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
Jens Axboe07068d52014-05-22 10:40:51 -06001323
1324 if (unlikely(is_flush_fua)) {
1325 blk_mq_bio_to_request(rq, bio);
1326 blk_insert_flush(rq);
1327 goto run_queue;
1328 }
1329
Shaohua Lif984df12015-05-08 10:51:32 -07001330 plug = current->plug;
Jens Axboee167dfb2014-10-29 11:18:26 -06001331 /*
1332 * If the driver supports defer issued based on 'last', then
1333 * queue it up like normal since we can potentially save some
1334 * CPU this way.
1335 */
Shaohua Lif984df12015-05-08 10:51:32 -07001336 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1337 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1338 struct request *old_rq = NULL;
Jens Axboe07068d52014-05-22 10:40:51 -06001339
1340 blk_mq_bio_to_request(rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001341
1342 /*
Jens Axboeb094f892015-11-20 20:29:45 -07001343 * We do limited pluging. If the bio can be merged, do that.
Shaohua Lif984df12015-05-08 10:51:32 -07001344 * Otherwise the existing request in the plug list will be
1345 * issued. So the plug list will have one request at most
Jens Axboe07068d52014-05-22 10:40:51 -06001346 */
Shaohua Lif984df12015-05-08 10:51:32 -07001347 if (plug) {
Shaohua Li5b3f3412015-05-08 10:51:33 -07001348 /*
1349 * The plug list might get flushed before this. If that
Jens Axboeb094f892015-11-20 20:29:45 -07001350 * happens, same_queue_rq is invalid and plug list is
1351 * empty
1352 */
Shaohua Li5b3f3412015-05-08 10:51:33 -07001353 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1354 old_rq = same_queue_rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001355 list_del_init(&old_rq->queuelist);
Jens Axboe07068d52014-05-22 10:40:51 -06001356 }
Shaohua Lif984df12015-05-08 10:51:32 -07001357 list_add_tail(&rq->queuelist, &plug->mq_list);
1358 } else /* is_sync */
1359 old_rq = rq;
1360 blk_mq_put_ctx(data.ctx);
1361 if (!old_rq)
Jens Axboe7b371632015-11-05 10:41:40 -07001362 goto done;
1363 if (!blk_mq_direct_issue_request(old_rq, &cookie))
1364 goto done;
Shaohua Lif984df12015-05-08 10:51:32 -07001365 blk_mq_insert_request(old_rq, false, true, true);
Jens Axboe7b371632015-11-05 10:41:40 -07001366 goto done;
Jens Axboe07068d52014-05-22 10:40:51 -06001367 }
1368
1369 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1370 /*
1371 * For a SYNC request, send it to the hardware immediately. For
1372 * an ASYNC request, just ensure that we run it later on. The
1373 * latter allows for merging opportunities and more efficient
1374 * dispatching.
1375 */
1376run_queue:
1377 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1378 }
Jens Axboe07068d52014-05-22 10:40:51 -06001379 blk_mq_put_ctx(data.ctx);
Jens Axboe7b371632015-11-05 10:41:40 -07001380done:
1381 return cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001382}
1383
1384/*
1385 * Single hardware queue variant. This will attempt to use any per-process
1386 * plug for merging and IO deferral.
1387 */
Jens Axboedece1632015-11-05 10:41:16 -07001388static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001389{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001390 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1391 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Jeff Moyere6c44382015-05-08 10:51:30 -07001392 struct blk_plug *plug;
1393 unsigned int request_count = 0;
Jens Axboe07068d52014-05-22 10:40:51 -06001394 struct blk_map_ctx data;
1395 struct request *rq;
Jens Axboe7b371632015-11-05 10:41:40 -07001396 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001397
Jens Axboe07068d52014-05-22 10:40:51 -06001398 blk_queue_bounce(q, &bio);
1399
1400 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001401 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001402 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001403 }
1404
Kent Overstreet54efd502015-04-23 22:37:18 -07001405 blk_queue_split(q, &bio, q->bio_split);
1406
Omar Sandoval87c279e2016-06-01 22:18:48 -07001407 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1408 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1409 return BLK_QC_T_NONE;
1410 } else
1411 request_count = blk_plug_queued_count(q);
Jens Axboe07068d52014-05-22 10:40:51 -06001412
1413 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001414 if (unlikely(!rq))
Jens Axboedece1632015-11-05 10:41:16 -07001415 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01001416
Jens Axboe7b371632015-11-05 10:41:40 -07001417 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
Jens Axboe320ae512013-10-24 09:20:05 +01001418
1419 if (unlikely(is_flush_fua)) {
1420 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001421 blk_insert_flush(rq);
1422 goto run_queue;
1423 }
1424
1425 /*
1426 * A task plug currently exists. Since this is completely lockless,
1427 * utilize that to temporarily store requests until the task is
1428 * either done or scheduled away.
1429 */
Jeff Moyere6c44382015-05-08 10:51:30 -07001430 plug = current->plug;
1431 if (plug) {
1432 blk_mq_bio_to_request(rq, bio);
Ming Lei676d0602015-10-20 23:13:56 +08001433 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001434 trace_block_plug(q);
Jens Axboeb094f892015-11-20 20:29:45 -07001435
1436 blk_mq_put_ctx(data.ctx);
1437
1438 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001439 blk_flush_plug_list(plug, false);
1440 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001441 }
Jens Axboeb094f892015-11-20 20:29:45 -07001442
Jeff Moyere6c44382015-05-08 10:51:30 -07001443 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe7b371632015-11-05 10:41:40 -07001444 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001445 }
1446
Jens Axboe07068d52014-05-22 10:40:51 -06001447 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1448 /*
1449 * For a SYNC request, send it to the hardware immediately. For
1450 * an ASYNC request, just ensure that we run it later on. The
1451 * latter allows for merging opportunities and more efficient
1452 * dispatching.
1453 */
1454run_queue:
1455 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001456 }
1457
Jens Axboe07068d52014-05-22 10:40:51 -06001458 blk_mq_put_ctx(data.ctx);
Jens Axboe7b371632015-11-05 10:41:40 -07001459 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001460}
1461
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001462static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1463 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001464{
1465 struct page *page;
1466
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001467 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001468 int i;
1469
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001470 for (i = 0; i < tags->nr_tags; i++) {
1471 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001472 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001473 set->ops->exit_request(set->driver_data, tags->rqs[i],
1474 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001475 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001476 }
1477 }
1478
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001479 while (!list_empty(&tags->page_list)) {
1480 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001481 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001482 /*
1483 * Remove kmemleak object previously allocated in
1484 * blk_mq_init_rq_map().
1485 */
1486 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01001487 __free_pages(page, page->private);
1488 }
1489
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001490 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001491
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001492 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001493}
1494
1495static size_t order_to_size(unsigned int order)
1496{
Ming Lei4ca08502014-04-19 18:00:18 +08001497 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001498}
1499
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001500static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1501 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001502{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001503 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001504 unsigned int i, j, entries_per_page, max_order = 4;
1505 size_t rq_size, left;
1506
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001507 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
Shaohua Li24391c02015-01-23 14:18:00 -07001508 set->numa_node,
1509 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001510 if (!tags)
1511 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001512
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001513 INIT_LIST_HEAD(&tags->page_list);
1514
Jens Axboea5164402014-09-10 09:02:03 -06001515 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1516 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1517 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001518 if (!tags->rqs) {
1519 blk_mq_free_tags(tags);
1520 return NULL;
1521 }
Jens Axboe320ae512013-10-24 09:20:05 +01001522
1523 /*
1524 * rq_size is the size of the request plus driver payload, rounded
1525 * to the cacheline size
1526 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001527 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001528 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001529 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001530
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001531 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001532 int this_order = max_order;
1533 struct page *page;
1534 int to_do;
1535 void *p;
1536
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06001537 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01001538 this_order--;
1539
1540 do {
Jens Axboea5164402014-09-10 09:02:03 -06001541 page = alloc_pages_node(set->numa_node,
Linus Torvaldsac211172015-04-09 14:12:22 -07001542 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06001543 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001544 if (page)
1545 break;
1546 if (!this_order--)
1547 break;
1548 if (order_to_size(this_order) < rq_size)
1549 break;
1550 } while (1);
1551
1552 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001553 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001554
1555 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001556 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001557
1558 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001559 /*
1560 * Allow kmemleak to scan these pages as they contain pointers
1561 * to additional allocations like via ops->init_request().
1562 */
1563 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL);
Jens Axboe320ae512013-10-24 09:20:05 +01001564 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001565 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001566 left -= to_do * rq_size;
1567 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001568 tags->rqs[i] = p;
1569 if (set->ops->init_request) {
1570 if (set->ops->init_request(set->driver_data,
1571 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001572 set->numa_node)) {
1573 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001574 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001575 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001576 }
1577
Jens Axboe320ae512013-10-24 09:20:05 +01001578 p += rq_size;
1579 i++;
1580 }
1581 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001582 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001583
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001584fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001585 blk_mq_free_rq_map(set, tags, hctx_idx);
1586 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001587}
1588
Jens Axboe1429d7c2014-05-19 09:23:55 -06001589static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1590{
1591 kfree(bitmap->map);
1592}
1593
1594static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1595{
1596 unsigned int bpw = 8, total, num_maps, i;
1597
1598 bitmap->bits_per_word = bpw;
1599
1600 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1601 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1602 GFP_KERNEL, node);
1603 if (!bitmap->map)
1604 return -ENOMEM;
1605
Jens Axboe1429d7c2014-05-19 09:23:55 -06001606 total = nr_cpu_ids;
1607 for (i = 0; i < num_maps; i++) {
1608 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1609 total -= bitmap->map[i].depth;
1610 }
1611
1612 return 0;
1613}
1614
Jens Axboee57690f2016-08-24 15:34:35 -06001615/*
1616 * 'cpu' is going away. splice any existing rq_list entries from this
1617 * software queue to the hw queue dispatch list, and ensure that it
1618 * gets run.
1619 */
Jens Axboe484b4062014-05-21 14:01:15 -06001620static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1621{
Jens Axboe484b4062014-05-21 14:01:15 -06001622 struct blk_mq_ctx *ctx;
1623 LIST_HEAD(tmp);
1624
Jens Axboee57690f2016-08-24 15:34:35 -06001625 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Jens Axboe484b4062014-05-21 14:01:15 -06001626
1627 spin_lock(&ctx->lock);
1628 if (!list_empty(&ctx->rq_list)) {
1629 list_splice_init(&ctx->rq_list, &tmp);
1630 blk_mq_hctx_clear_pending(hctx, ctx);
1631 }
1632 spin_unlock(&ctx->lock);
1633
1634 if (list_empty(&tmp))
1635 return NOTIFY_OK;
1636
Jens Axboee57690f2016-08-24 15:34:35 -06001637 spin_lock(&hctx->lock);
1638 list_splice_tail_init(&tmp, &hctx->dispatch);
1639 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06001640
1641 blk_mq_run_hw_queue(hctx, true);
Jens Axboe484b4062014-05-21 14:01:15 -06001642 return NOTIFY_OK;
1643}
1644
Jens Axboe484b4062014-05-21 14:01:15 -06001645static int blk_mq_hctx_notify(void *data, unsigned long action,
1646 unsigned int cpu)
1647{
1648 struct blk_mq_hw_ctx *hctx = data;
1649
1650 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1651 return blk_mq_hctx_cpu_offline(hctx, cpu);
Ming Lei2a34c082015-04-21 10:00:20 +08001652
1653 /*
1654 * In case of CPU online, tags may be reallocated
1655 * in blk_mq_map_swqueue() after mapping is updated.
1656 */
Jens Axboe484b4062014-05-21 14:01:15 -06001657
1658 return NOTIFY_OK;
1659}
1660
Ming Leic3b4afc2015-06-04 22:25:04 +08001661/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08001662static void blk_mq_exit_hctx(struct request_queue *q,
1663 struct blk_mq_tag_set *set,
1664 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1665{
Ming Leif70ced02014-09-25 23:23:47 +08001666 unsigned flush_start_tag = set->queue_depth;
1667
Ming Lei08e98fc2014-09-25 23:23:38 +08001668 blk_mq_tag_idle(hctx);
1669
Ming Leif70ced02014-09-25 23:23:47 +08001670 if (set->ops->exit_request)
1671 set->ops->exit_request(set->driver_data,
1672 hctx->fq->flush_rq, hctx_idx,
1673 flush_start_tag + hctx_idx);
1674
Ming Lei08e98fc2014-09-25 23:23:38 +08001675 if (set->ops->exit_hctx)
1676 set->ops->exit_hctx(hctx, hctx_idx);
1677
1678 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Ming Leif70ced02014-09-25 23:23:47 +08001679 blk_free_flush_queue(hctx->fq);
Ming Lei08e98fc2014-09-25 23:23:38 +08001680 blk_mq_free_bitmap(&hctx->ctx_map);
1681}
1682
Ming Lei624dbe42014-05-27 23:35:13 +08001683static void blk_mq_exit_hw_queues(struct request_queue *q,
1684 struct blk_mq_tag_set *set, int nr_queue)
1685{
1686 struct blk_mq_hw_ctx *hctx;
1687 unsigned int i;
1688
1689 queue_for_each_hw_ctx(q, hctx, i) {
1690 if (i == nr_queue)
1691 break;
Ming Lei08e98fc2014-09-25 23:23:38 +08001692 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08001693 }
Ming Lei624dbe42014-05-27 23:35:13 +08001694}
1695
1696static void blk_mq_free_hw_queues(struct request_queue *q,
1697 struct blk_mq_tag_set *set)
1698{
1699 struct blk_mq_hw_ctx *hctx;
1700 unsigned int i;
1701
Ming Leie09aae72015-01-29 20:17:27 +08001702 queue_for_each_hw_ctx(q, hctx, i)
Ming Lei624dbe42014-05-27 23:35:13 +08001703 free_cpumask_var(hctx->cpumask);
Ming Lei624dbe42014-05-27 23:35:13 +08001704}
1705
Ming Lei08e98fc2014-09-25 23:23:38 +08001706static int blk_mq_init_hctx(struct request_queue *q,
1707 struct blk_mq_tag_set *set,
1708 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1709{
1710 int node;
Ming Leif70ced02014-09-25 23:23:47 +08001711 unsigned flush_start_tag = set->queue_depth;
Ming Lei08e98fc2014-09-25 23:23:38 +08001712
1713 node = hctx->numa_node;
1714 if (node == NUMA_NO_NODE)
1715 node = hctx->numa_node = set->numa_node;
1716
Jens Axboe27489a32016-08-24 15:54:25 -06001717 INIT_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08001718 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1719 spin_lock_init(&hctx->lock);
1720 INIT_LIST_HEAD(&hctx->dispatch);
1721 hctx->queue = q;
1722 hctx->queue_num = hctx_idx;
Jeff Moyer2404e602015-11-03 10:40:06 -05001723 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08001724
1725 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1726 blk_mq_hctx_notify, hctx);
1727 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1728
1729 hctx->tags = set->tags[hctx_idx];
1730
1731 /*
1732 * Allocate space for all possible cpus to avoid allocation at
1733 * runtime
1734 */
1735 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1736 GFP_KERNEL, node);
1737 if (!hctx->ctxs)
1738 goto unregister_cpu_notifier;
1739
1740 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1741 goto free_ctxs;
1742
1743 hctx->nr_ctx = 0;
1744
1745 if (set->ops->init_hctx &&
1746 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1747 goto free_bitmap;
1748
Ming Leif70ced02014-09-25 23:23:47 +08001749 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1750 if (!hctx->fq)
1751 goto exit_hctx;
1752
1753 if (set->ops->init_request &&
1754 set->ops->init_request(set->driver_data,
1755 hctx->fq->flush_rq, hctx_idx,
1756 flush_start_tag + hctx_idx, node))
1757 goto free_fq;
1758
Ming Lei08e98fc2014-09-25 23:23:38 +08001759 return 0;
1760
Ming Leif70ced02014-09-25 23:23:47 +08001761 free_fq:
1762 kfree(hctx->fq);
1763 exit_hctx:
1764 if (set->ops->exit_hctx)
1765 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001766 free_bitmap:
1767 blk_mq_free_bitmap(&hctx->ctx_map);
1768 free_ctxs:
1769 kfree(hctx->ctxs);
1770 unregister_cpu_notifier:
1771 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1772
1773 return -1;
1774}
1775
Jens Axboe320ae512013-10-24 09:20:05 +01001776static void blk_mq_init_cpu_queues(struct request_queue *q,
1777 unsigned int nr_hw_queues)
1778{
1779 unsigned int i;
1780
1781 for_each_possible_cpu(i) {
1782 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1783 struct blk_mq_hw_ctx *hctx;
1784
1785 memset(__ctx, 0, sizeof(*__ctx));
1786 __ctx->cpu = i;
1787 spin_lock_init(&__ctx->lock);
1788 INIT_LIST_HEAD(&__ctx->rq_list);
1789 __ctx->queue = q;
1790
1791 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001792 if (!cpu_online(i))
1793 continue;
1794
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001795 hctx = blk_mq_map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001796
Jens Axboe320ae512013-10-24 09:20:05 +01001797 /*
1798 * Set local node, IFF we have more than one hw queue. If
1799 * not, we remain on the home node of the device
1800 */
1801 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Raghavendra K Tbffed452015-12-02 16:59:05 +05301802 hctx->numa_node = local_memory_node(cpu_to_node(i));
Jens Axboe320ae512013-10-24 09:20:05 +01001803 }
1804}
1805
Akinobu Mita57783222015-09-27 02:09:23 +09001806static void blk_mq_map_swqueue(struct request_queue *q,
1807 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01001808{
1809 unsigned int i;
1810 struct blk_mq_hw_ctx *hctx;
1811 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08001812 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001813
Akinobu Mita60de0742015-09-27 02:09:25 +09001814 /*
1815 * Avoid others reading imcomplete hctx->cpumask through sysfs
1816 */
1817 mutex_lock(&q->sysfs_lock);
1818
Jens Axboe320ae512013-10-24 09:20:05 +01001819 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001820 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001821 hctx->nr_ctx = 0;
1822 }
1823
1824 /*
1825 * Map software to hardware queues
1826 */
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01001827 for_each_possible_cpu(i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001828 /* If the cpu isn't online, the cpu is mapped to first hctx */
Akinobu Mita57783222015-09-27 02:09:23 +09001829 if (!cpumask_test_cpu(i, online_mask))
Jens Axboee4043dc2014-04-09 10:18:23 -06001830 continue;
1831
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01001832 ctx = per_cpu_ptr(q->queue_ctx, i);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001833 hctx = blk_mq_map_queue(q, i);
Keith Busch868f2f02015-12-17 17:08:14 -07001834
Jens Axboee4043dc2014-04-09 10:18:23 -06001835 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001836 ctx->index_hw = hctx->nr_ctx;
1837 hctx->ctxs[hctx->nr_ctx++] = ctx;
1838 }
Jens Axboe506e9312014-05-07 10:26:44 -06001839
Akinobu Mita60de0742015-09-27 02:09:25 +09001840 mutex_unlock(&q->sysfs_lock);
1841
Jens Axboe506e9312014-05-07 10:26:44 -06001842 queue_for_each_hw_ctx(q, hctx, i) {
Chong Yuan889fa312015-04-15 11:39:29 -06001843 struct blk_mq_ctxmap *map = &hctx->ctx_map;
1844
Jens Axboe484b4062014-05-21 14:01:15 -06001845 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001846 * If no software queues are mapped to this hardware queue,
1847 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06001848 */
1849 if (!hctx->nr_ctx) {
Jens Axboe484b4062014-05-21 14:01:15 -06001850 if (set->tags[i]) {
1851 blk_mq_free_rq_map(set, set->tags[i], i);
1852 set->tags[i] = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001853 }
Ming Lei2a34c082015-04-21 10:00:20 +08001854 hctx->tags = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001855 continue;
1856 }
1857
Ming Lei2a34c082015-04-21 10:00:20 +08001858 /* unmapped hw queue can be remapped after CPU topo changed */
1859 if (!set->tags[i])
1860 set->tags[i] = blk_mq_init_rq_map(set, i);
1861 hctx->tags = set->tags[i];
1862 WARN_ON(!hctx->tags);
1863
Raghavendra K Te0e827b2015-12-02 16:57:06 +05301864 cpumask_copy(hctx->tags->cpumask, hctx->cpumask);
Jens Axboe484b4062014-05-21 14:01:15 -06001865 /*
Chong Yuan889fa312015-04-15 11:39:29 -06001866 * Set the map size to the number of mapped software queues.
1867 * This is more accurate and more efficient than looping
1868 * over all possibly mapped software queues.
1869 */
Jens Axboe569fd0c2015-04-17 08:28:50 -06001870 map->size = DIV_ROUND_UP(hctx->nr_ctx, map->bits_per_word);
Chong Yuan889fa312015-04-15 11:39:29 -06001871
1872 /*
Jens Axboe484b4062014-05-21 14:01:15 -06001873 * Initialize batch roundrobin counts
1874 */
Jens Axboe506e9312014-05-07 10:26:44 -06001875 hctx->next_cpu = cpumask_first(hctx->cpumask);
1876 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1877 }
Jens Axboe320ae512013-10-24 09:20:05 +01001878}
1879
Jeff Moyer2404e602015-11-03 10:40:06 -05001880static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06001881{
1882 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06001883 int i;
1884
Jeff Moyer2404e602015-11-03 10:40:06 -05001885 queue_for_each_hw_ctx(q, hctx, i) {
1886 if (shared)
1887 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1888 else
1889 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1890 }
1891}
1892
1893static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
1894{
1895 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06001896
1897 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1898 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05001899 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001900 blk_mq_unfreeze_queue(q);
1901 }
1902}
1903
1904static void blk_mq_del_queue_tag_set(struct request_queue *q)
1905{
1906 struct blk_mq_tag_set *set = q->tag_set;
1907
Jens Axboe0d2602c2014-05-13 15:10:52 -06001908 mutex_lock(&set->tag_list_lock);
1909 list_del_init(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05001910 if (list_is_singular(&set->tag_list)) {
1911 /* just transitioned to unshared */
1912 set->flags &= ~BLK_MQ_F_TAG_SHARED;
1913 /* update existing queue */
1914 blk_mq_update_tag_set_depth(set, false);
1915 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001916 mutex_unlock(&set->tag_list_lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001917}
1918
1919static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1920 struct request_queue *q)
1921{
1922 q->tag_set = set;
1923
1924 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05001925
1926 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1927 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
1928 set->flags |= BLK_MQ_F_TAG_SHARED;
1929 /* update existing queue */
1930 blk_mq_update_tag_set_depth(set, true);
1931 }
1932 if (set->flags & BLK_MQ_F_TAG_SHARED)
1933 queue_set_hctx_shared(q, true);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001934 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05001935
Jens Axboe0d2602c2014-05-13 15:10:52 -06001936 mutex_unlock(&set->tag_list_lock);
1937}
1938
Ming Leie09aae72015-01-29 20:17:27 +08001939/*
1940 * It is the actual release handler for mq, but we do it from
1941 * request queue's release handler for avoiding use-after-free
1942 * and headache because q->mq_kobj shouldn't have been introduced,
1943 * but we can't group ctx/kctx kobj without it.
1944 */
1945void blk_mq_release(struct request_queue *q)
1946{
1947 struct blk_mq_hw_ctx *hctx;
1948 unsigned int i;
1949
1950 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08001951 queue_for_each_hw_ctx(q, hctx, i) {
1952 if (!hctx)
1953 continue;
1954 kfree(hctx->ctxs);
Ming Leie09aae72015-01-29 20:17:27 +08001955 kfree(hctx);
Ming Leic3b4afc2015-06-04 22:25:04 +08001956 }
Ming Leie09aae72015-01-29 20:17:27 +08001957
Akinobu Mitaa723bab2015-09-27 02:09:21 +09001958 q->mq_map = NULL;
1959
Ming Leie09aae72015-01-29 20:17:27 +08001960 kfree(q->queue_hw_ctx);
1961
1962 /* ctx kobj stays in queue_ctx */
1963 free_percpu(q->queue_ctx);
1964}
1965
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001966struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001967{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04001968 struct request_queue *uninit_q, *q;
1969
1970 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1971 if (!uninit_q)
1972 return ERR_PTR(-ENOMEM);
1973
1974 q = blk_mq_init_allocated_queue(set, uninit_q);
1975 if (IS_ERR(q))
1976 blk_cleanup_queue(uninit_q);
1977
1978 return q;
1979}
1980EXPORT_SYMBOL(blk_mq_init_queue);
1981
Keith Busch868f2f02015-12-17 17:08:14 -07001982static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
1983 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04001984{
Keith Busch868f2f02015-12-17 17:08:14 -07001985 int i, j;
1986 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001987
Keith Busch868f2f02015-12-17 17:08:14 -07001988 blk_mq_sysfs_unregister(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001989 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07001990 int node;
Jens Axboef14bbe72014-05-27 12:06:53 -06001991
Keith Busch868f2f02015-12-17 17:08:14 -07001992 if (hctxs[i])
1993 continue;
1994
1995 node = blk_mq_hw_queue_to_node(q->mq_map, i);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001996 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1997 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001998 if (!hctxs[i])
Keith Busch868f2f02015-12-17 17:08:14 -07001999 break;
Jens Axboe320ae512013-10-24 09:20:05 +01002000
Jens Axboea86073e2014-10-13 15:41:54 -06002001 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
Keith Busch868f2f02015-12-17 17:08:14 -07002002 node)) {
2003 kfree(hctxs[i]);
2004 hctxs[i] = NULL;
2005 break;
2006 }
Jens Axboee4043dc2014-04-09 10:18:23 -06002007
Jens Axboe0d2602c2014-05-13 15:10:52 -06002008 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06002009 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01002010 hctxs[i]->queue_num = i;
Keith Busch868f2f02015-12-17 17:08:14 -07002011
2012 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2013 free_cpumask_var(hctxs[i]->cpumask);
2014 kfree(hctxs[i]);
2015 hctxs[i] = NULL;
2016 break;
2017 }
2018 blk_mq_hctx_kobj_init(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01002019 }
Keith Busch868f2f02015-12-17 17:08:14 -07002020 for (j = i; j < q->nr_hw_queues; j++) {
2021 struct blk_mq_hw_ctx *hctx = hctxs[j];
2022
2023 if (hctx) {
2024 if (hctx->tags) {
2025 blk_mq_free_rq_map(set, hctx->tags, j);
2026 set->tags[j] = NULL;
2027 }
2028 blk_mq_exit_hctx(q, set, hctx, j);
2029 free_cpumask_var(hctx->cpumask);
2030 kobject_put(&hctx->kobj);
2031 kfree(hctx->ctxs);
2032 kfree(hctx);
2033 hctxs[j] = NULL;
2034
2035 }
2036 }
2037 q->nr_hw_queues = i;
2038 blk_mq_sysfs_register(q);
2039}
2040
2041struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2042 struct request_queue *q)
2043{
Ming Lei66841672016-02-12 15:27:00 +08002044 /* mark the queue as mq asap */
2045 q->mq_ops = set->ops;
2046
Keith Busch868f2f02015-12-17 17:08:14 -07002047 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2048 if (!q->queue_ctx)
Ming Linc7de5722016-05-25 23:23:27 -07002049 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002050
2051 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2052 GFP_KERNEL, set->numa_node);
2053 if (!q->queue_hw_ctx)
2054 goto err_percpu;
2055
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002056 q->mq_map = set->mq_map;
Keith Busch868f2f02015-12-17 17:08:14 -07002057
2058 blk_mq_realloc_hw_ctxs(set, q);
2059 if (!q->nr_hw_queues)
2060 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002061
Christoph Hellwig287922e2015-10-30 20:57:30 +08002062 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002063 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002064
2065 q->nr_queues = nr_cpu_ids;
Jens Axboe320ae512013-10-24 09:20:05 +01002066
Jens Axboe94eddfb2013-11-19 09:25:07 -07002067 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002068
Jens Axboe05f1dd52014-05-29 09:53:32 -06002069 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2070 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2071
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002072 q->sg_reserved_size = INT_MAX;
2073
Mike Snitzer28494502016-09-14 13:28:30 -04002074 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002075 INIT_LIST_HEAD(&q->requeue_list);
2076 spin_lock_init(&q->requeue_lock);
2077
Jens Axboe07068d52014-05-22 10:40:51 -06002078 if (q->nr_hw_queues > 1)
2079 blk_queue_make_request(q, blk_mq_make_request);
2080 else
2081 blk_queue_make_request(q, blk_sq_make_request);
2082
Jens Axboeeba71762014-05-20 15:17:27 -06002083 /*
2084 * Do this after blk_queue_make_request() overrides it...
2085 */
2086 q->nr_requests = set->queue_depth;
2087
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002088 if (set->ops->complete)
2089 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08002090
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002091 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002092
Akinobu Mita57783222015-09-27 02:09:23 +09002093 get_online_cpus();
Jens Axboe320ae512013-10-24 09:20:05 +01002094 mutex_lock(&all_q_mutex);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002095
Jens Axboe320ae512013-10-24 09:20:05 +01002096 list_add_tail(&q->all_q_node, &all_q_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002097 blk_mq_add_queue_tag_set(set, q);
Akinobu Mita57783222015-09-27 02:09:23 +09002098 blk_mq_map_swqueue(q, cpu_online_mask);
Jens Axboe484b4062014-05-21 14:01:15 -06002099
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002100 mutex_unlock(&all_q_mutex);
Akinobu Mita57783222015-09-27 02:09:23 +09002101 put_online_cpus();
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002102
Jens Axboe320ae512013-10-24 09:20:05 +01002103 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002104
Jens Axboe320ae512013-10-24 09:20:05 +01002105err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002106 kfree(q->queue_hw_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002107err_percpu:
Keith Busch868f2f02015-12-17 17:08:14 -07002108 free_percpu(q->queue_ctx);
Ming Linc7de5722016-05-25 23:23:27 -07002109err_exit:
2110 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002111 return ERR_PTR(-ENOMEM);
2112}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002113EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002114
2115void blk_mq_free_queue(struct request_queue *q)
2116{
Ming Lei624dbe42014-05-27 23:35:13 +08002117 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002118
Akinobu Mita0e626362015-09-27 02:09:22 +09002119 mutex_lock(&all_q_mutex);
2120 list_del_init(&q->all_q_node);
2121 mutex_unlock(&all_q_mutex);
2122
Jens Axboe0d2602c2014-05-13 15:10:52 -06002123 blk_mq_del_queue_tag_set(q);
2124
Ming Lei624dbe42014-05-27 23:35:13 +08002125 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2126 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01002127}
Jens Axboe320ae512013-10-24 09:20:05 +01002128
2129/* Basically redo blk_mq_init_queue with queue frozen */
Akinobu Mita57783222015-09-27 02:09:23 +09002130static void blk_mq_queue_reinit(struct request_queue *q,
2131 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01002132{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +02002133 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
Jens Axboe320ae512013-10-24 09:20:05 +01002134
Jens Axboe67aec142014-05-30 08:25:36 -06002135 blk_mq_sysfs_unregister(q);
2136
Jens Axboe320ae512013-10-24 09:20:05 +01002137 /*
2138 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2139 * we should change hctx numa_node according to new topology (this
2140 * involves free and re-allocate memory, worthy doing?)
2141 */
2142
Akinobu Mita57783222015-09-27 02:09:23 +09002143 blk_mq_map_swqueue(q, online_mask);
Jens Axboe320ae512013-10-24 09:20:05 +01002144
Jens Axboe67aec142014-05-30 08:25:36 -06002145 blk_mq_sysfs_register(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002146}
2147
Paul Gortmakerf618ef72013-11-14 08:26:02 -07002148static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
2149 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01002150{
2151 struct request_queue *q;
Akinobu Mita57783222015-09-27 02:09:23 +09002152 int cpu = (unsigned long)hcpu;
2153 /*
2154 * New online cpumask which is going to be set in this hotplug event.
2155 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2156 * one-by-one and dynamically allocating this could result in a failure.
2157 */
2158 static struct cpumask online_new;
Jens Axboe320ae512013-10-24 09:20:05 +01002159
2160 /*
Akinobu Mita57783222015-09-27 02:09:23 +09002161 * Before hotadded cpu starts handling requests, new mappings must
2162 * be established. Otherwise, these requests in hw queue might
2163 * never be dispatched.
2164 *
2165 * For example, there is a single hw queue (hctx) and two CPU queues
2166 * (ctx0 for CPU0, and ctx1 for CPU1).
2167 *
2168 * Now CPU1 is just onlined and a request is inserted into
2169 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2170 * still zero.
2171 *
2172 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2173 * set in pending bitmap and tries to retrieve requests in
2174 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2175 * so the request in ctx1->rq_list is ignored.
Jens Axboe320ae512013-10-24 09:20:05 +01002176 */
Akinobu Mita57783222015-09-27 02:09:23 +09002177 switch (action & ~CPU_TASKS_FROZEN) {
2178 case CPU_DEAD:
2179 case CPU_UP_CANCELED:
2180 cpumask_copy(&online_new, cpu_online_mask);
2181 break;
2182 case CPU_UP_PREPARE:
2183 cpumask_copy(&online_new, cpu_online_mask);
2184 cpumask_set_cpu(cpu, &online_new);
2185 break;
2186 default:
Jens Axboe320ae512013-10-24 09:20:05 +01002187 return NOTIFY_OK;
Akinobu Mita57783222015-09-27 02:09:23 +09002188 }
Jens Axboe320ae512013-10-24 09:20:05 +01002189
2190 mutex_lock(&all_q_mutex);
Tejun Heof3af0202014-11-04 13:52:27 -05002191
2192 /*
2193 * We need to freeze and reinit all existing queues. Freezing
2194 * involves synchronous wait for an RCU grace period and doing it
2195 * one by one may take a long time. Start freezing all queues in
2196 * one swoop and then wait for the completions so that freezing can
2197 * take place in parallel.
2198 */
2199 list_for_each_entry(q, &all_q_list, all_q_node)
2200 blk_mq_freeze_queue_start(q);
Ming Leif054b562015-04-21 10:00:19 +08002201 list_for_each_entry(q, &all_q_list, all_q_node) {
Tejun Heof3af0202014-11-04 13:52:27 -05002202 blk_mq_freeze_queue_wait(q);
2203
Ming Leif054b562015-04-21 10:00:19 +08002204 /*
2205 * timeout handler can't touch hw queue during the
2206 * reinitialization
2207 */
2208 del_timer_sync(&q->timeout);
2209 }
2210
Jens Axboe320ae512013-10-24 09:20:05 +01002211 list_for_each_entry(q, &all_q_list, all_q_node)
Akinobu Mita57783222015-09-27 02:09:23 +09002212 blk_mq_queue_reinit(q, &online_new);
Tejun Heof3af0202014-11-04 13:52:27 -05002213
2214 list_for_each_entry(q, &all_q_list, all_q_node)
2215 blk_mq_unfreeze_queue(q);
2216
Jens Axboe320ae512013-10-24 09:20:05 +01002217 mutex_unlock(&all_q_mutex);
2218 return NOTIFY_OK;
2219}
2220
Jens Axboea5164402014-09-10 09:02:03 -06002221static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2222{
2223 int i;
2224
2225 for (i = 0; i < set->nr_hw_queues; i++) {
2226 set->tags[i] = blk_mq_init_rq_map(set, i);
2227 if (!set->tags[i])
2228 goto out_unwind;
2229 }
2230
2231 return 0;
2232
2233out_unwind:
2234 while (--i >= 0)
2235 blk_mq_free_rq_map(set, set->tags[i], i);
2236
Jens Axboea5164402014-09-10 09:02:03 -06002237 return -ENOMEM;
2238}
2239
2240/*
2241 * Allocate the request maps associated with this tag_set. Note that this
2242 * may reduce the depth asked for, if memory is tight. set->queue_depth
2243 * will be updated to reflect the allocated depth.
2244 */
2245static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2246{
2247 unsigned int depth;
2248 int err;
2249
2250 depth = set->queue_depth;
2251 do {
2252 err = __blk_mq_alloc_rq_maps(set);
2253 if (!err)
2254 break;
2255
2256 set->queue_depth >>= 1;
2257 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2258 err = -ENOMEM;
2259 break;
2260 }
2261 } while (set->queue_depth);
2262
2263 if (!set->queue_depth || err) {
2264 pr_err("blk-mq: failed to allocate request map\n");
2265 return -ENOMEM;
2266 }
2267
2268 if (depth != set->queue_depth)
2269 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2270 depth, set->queue_depth);
2271
2272 return 0;
2273}
2274
Keith Buschf26cdc82015-06-01 09:29:53 -06002275struct cpumask *blk_mq_tags_cpumask(struct blk_mq_tags *tags)
2276{
2277 return tags->cpumask;
2278}
2279EXPORT_SYMBOL_GPL(blk_mq_tags_cpumask);
2280
Jens Axboea4391c62014-06-05 15:21:56 -06002281/*
2282 * Alloc a tag set to be associated with one or more request queues.
2283 * May fail with EINVAL for various error conditions. May adjust the
2284 * requested depth down, if if it too large. In that case, the set
2285 * value will be stored in set->queue_depth.
2286 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002287int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2288{
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002289 int ret;
2290
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002291 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2292
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002293 if (!set->nr_hw_queues)
2294 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002295 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002296 return -EINVAL;
2297 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2298 return -EINVAL;
2299
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002300 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002301 return -EINVAL;
2302
Jens Axboea4391c62014-06-05 15:21:56 -06002303 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2304 pr_info("blk-mq: reduced tag depth to %u\n",
2305 BLK_MQ_MAX_DEPTH);
2306 set->queue_depth = BLK_MQ_MAX_DEPTH;
2307 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002308
Shaohua Li6637fad2014-11-30 16:00:58 -08002309 /*
2310 * If a crashdump is active, then we are potentially in a very
2311 * memory constrained environment. Limit us to 1 queue and
2312 * 64 tags to prevent using too much memory.
2313 */
2314 if (is_kdump_kernel()) {
2315 set->nr_hw_queues = 1;
2316 set->queue_depth = min(64U, set->queue_depth);
2317 }
Keith Busch868f2f02015-12-17 17:08:14 -07002318 /*
2319 * There is no use for more h/w queues than cpus.
2320 */
2321 if (set->nr_hw_queues > nr_cpu_ids)
2322 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08002323
Keith Busch868f2f02015-12-17 17:08:14 -07002324 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002325 GFP_KERNEL, set->numa_node);
2326 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002327 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002328
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002329 ret = -ENOMEM;
2330 set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
2331 GFP_KERNEL, set->numa_node);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002332 if (!set->mq_map)
2333 goto out_free_tags;
2334
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002335 if (set->ops->map_queues)
2336 ret = set->ops->map_queues(set);
2337 else
2338 ret = blk_mq_map_queues(set);
2339 if (ret)
2340 goto out_free_mq_map;
2341
2342 ret = blk_mq_alloc_rq_maps(set);
2343 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002344 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002345
Jens Axboe0d2602c2014-05-13 15:10:52 -06002346 mutex_init(&set->tag_list_lock);
2347 INIT_LIST_HEAD(&set->tag_list);
2348
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002349 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002350
2351out_free_mq_map:
2352 kfree(set->mq_map);
2353 set->mq_map = NULL;
2354out_free_tags:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002355 kfree(set->tags);
2356 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002357 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002358}
2359EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2360
2361void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2362{
2363 int i;
2364
Keith Busch868f2f02015-12-17 17:08:14 -07002365 for (i = 0; i < nr_cpu_ids; i++) {
Junichi Nomuraf42d79a2015-10-14 05:02:15 +00002366 if (set->tags[i])
Jens Axboe484b4062014-05-21 14:01:15 -06002367 blk_mq_free_rq_map(set, set->tags[i], i);
2368 }
2369
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002370 kfree(set->mq_map);
2371 set->mq_map = NULL;
2372
Ming Lei981bd182014-04-24 00:07:34 +08002373 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002374 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002375}
2376EXPORT_SYMBOL(blk_mq_free_tag_set);
2377
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002378int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2379{
2380 struct blk_mq_tag_set *set = q->tag_set;
2381 struct blk_mq_hw_ctx *hctx;
2382 int i, ret;
2383
2384 if (!set || nr > set->queue_depth)
2385 return -EINVAL;
2386
2387 ret = 0;
2388 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07002389 if (!hctx->tags)
2390 continue;
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002391 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2392 if (ret)
2393 break;
2394 }
2395
2396 if (!ret)
2397 q->nr_requests = nr;
2398
2399 return ret;
2400}
2401
Keith Busch868f2f02015-12-17 17:08:14 -07002402void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2403{
2404 struct request_queue *q;
2405
2406 if (nr_hw_queues > nr_cpu_ids)
2407 nr_hw_queues = nr_cpu_ids;
2408 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2409 return;
2410
2411 list_for_each_entry(q, &set->tag_list, tag_set_list)
2412 blk_mq_freeze_queue(q);
2413
2414 set->nr_hw_queues = nr_hw_queues;
2415 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2416 blk_mq_realloc_hw_ctxs(set, q);
2417
2418 if (q->nr_hw_queues > 1)
2419 blk_queue_make_request(q, blk_mq_make_request);
2420 else
2421 blk_queue_make_request(q, blk_sq_make_request);
2422
2423 blk_mq_queue_reinit(q, cpu_online_mask);
2424 }
2425
2426 list_for_each_entry(q, &set->tag_list, tag_set_list)
2427 blk_mq_unfreeze_queue(q);
2428}
2429EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2430
Jens Axboe676141e2014-03-20 13:29:18 -06002431void blk_mq_disable_hotplug(void)
2432{
2433 mutex_lock(&all_q_mutex);
2434}
2435
2436void blk_mq_enable_hotplug(void)
2437{
2438 mutex_unlock(&all_q_mutex);
2439}
2440
Jens Axboe320ae512013-10-24 09:20:05 +01002441static int __init blk_mq_init(void)
2442{
Jens Axboe320ae512013-10-24 09:20:05 +01002443 blk_mq_cpu_init();
2444
Tejun Heoadd703f2014-07-01 10:34:38 -06002445 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002446
2447 return 0;
2448}
2449subsys_initcall(blk_mq_init);