blob: d38371160019ca17468478d30a5c8f1792f5e109 [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
Jens Axboe320ae512013-10-24 09:20:05 +01007#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/backing-dev.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
Catalin Marinasf75782e2015-09-14 18:16:02 +010012#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010013#include <linux/mm.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/workqueue.h>
17#include <linux/smp.h>
18#include <linux/llist.h>
19#include <linux/list_sort.h>
20#include <linux/cpu.h>
21#include <linux/cache.h>
22#include <linux/sched/sysctl.h>
23#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060024#include <linux/crash_dump.h>
Jens Axboe320ae512013-10-24 09:20:05 +010025
26#include <trace/events/block.h>
27
28#include <linux/blk-mq.h>
29#include "blk.h"
30#include "blk-mq.h"
31#include "blk-mq-tag.h"
32
33static DEFINE_MUTEX(all_q_mutex);
34static LIST_HEAD(all_q_list);
35
36static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
37
Jens Axboe320ae512013-10-24 09:20:05 +010038/*
39 * Check if any of the ctx's have pending work in this hardware queue
40 */
41static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
42{
43 unsigned int i;
44
Jens Axboe569fd0c2015-04-17 08:28:50 -060045 for (i = 0; i < hctx->ctx_map.size; i++)
Jens Axboe1429d7c2014-05-19 09:23:55 -060046 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010047 return true;
48
49 return false;
50}
51
Jens Axboe1429d7c2014-05-19 09:23:55 -060052static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
53 struct blk_mq_ctx *ctx)
54{
55 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
56}
57
58#define CTX_TO_BIT(hctx, ctx) \
59 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
60
Jens Axboe320ae512013-10-24 09:20:05 +010061/*
62 * Mark this ctx as having pending work in this hardware queue
63 */
64static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
65 struct blk_mq_ctx *ctx)
66{
Jens Axboe1429d7c2014-05-19 09:23:55 -060067 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
68
69 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
70 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
71}
72
73static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
74 struct blk_mq_ctx *ctx)
75{
76 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
77
78 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010079}
80
Keith Buschbfd343a2015-03-11 23:56:39 -040081static int blk_mq_queue_enter(struct request_queue *q, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +010082{
Tejun Heoadd703f2014-07-01 10:34:38 -060083 while (true) {
84 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +010085
Tejun Heoadd703f2014-07-01 10:34:38 -060086 if (percpu_ref_tryget_live(&q->mq_usage_counter))
87 return 0;
Keith Busch3b632cf2014-06-06 10:22:07 -060088
Keith Buschbfd343a2015-03-11 23:56:39 -040089 if (!(gfp & __GFP_WAIT))
90 return -EBUSY;
91
Tejun Heoadd703f2014-07-01 10:34:38 -060092 ret = wait_event_interruptible(q->mq_freeze_wq,
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020093 !atomic_read(&q->mq_freeze_depth) ||
94 blk_queue_dying(q));
Tejun Heoadd703f2014-07-01 10:34:38 -060095 if (blk_queue_dying(q))
96 return -ENODEV;
97 if (ret)
98 return ret;
99 }
Jens Axboe320ae512013-10-24 09:20:05 +0100100}
101
102static void blk_mq_queue_exit(struct request_queue *q)
103{
Tejun Heoadd703f2014-07-01 10:34:38 -0600104 percpu_ref_put(&q->mq_usage_counter);
105}
106
107static void blk_mq_usage_counter_release(struct percpu_ref *ref)
108{
109 struct request_queue *q =
110 container_of(ref, struct request_queue, mq_usage_counter);
111
112 wake_up_all(&q->mq_freeze_wq);
Jens Axboe320ae512013-10-24 09:20:05 +0100113}
114
Keith Buschb4c6a022014-12-19 17:54:14 -0700115void blk_mq_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800116{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200117 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -0400118
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200119 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
120 if (freeze_depth == 1) {
Tejun Heo9eca8042014-09-24 13:07:33 -0400121 percpu_ref_kill(&q->mq_usage_counter);
Mike Snitzerb94ec292015-03-11 23:56:38 -0400122 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -0400123 }
Tejun Heof3af0202014-11-04 13:52:27 -0500124}
Keith Buschb4c6a022014-12-19 17:54:14 -0700125EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500126
127static void blk_mq_freeze_queue_wait(struct request_queue *q)
128{
Tejun Heoadd703f2014-07-01 10:34:38 -0600129 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800130}
131
Tejun Heof3af0202014-11-04 13:52:27 -0500132/*
133 * Guarantee no request is in use, so we can change any data structure of
134 * the queue afterward.
135 */
136void blk_mq_freeze_queue(struct request_queue *q)
137{
138 blk_mq_freeze_queue_start(q);
139 blk_mq_freeze_queue_wait(q);
140}
Jens Axboec761d962015-01-02 15:05:12 -0700141EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500142
Keith Buschb4c6a022014-12-19 17:54:14 -0700143void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100144{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200145 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100146
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200147 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
148 WARN_ON_ONCE(freeze_depth < 0);
149 if (!freeze_depth) {
Tejun Heoadd703f2014-07-01 10:34:38 -0600150 percpu_ref_reinit(&q->mq_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100151 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600152 }
Jens Axboe320ae512013-10-24 09:20:05 +0100153}
Keith Buschb4c6a022014-12-19 17:54:14 -0700154EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100155
Jens Axboeaed3ea92014-12-22 14:04:42 -0700156void blk_mq_wake_waiters(struct request_queue *q)
157{
158 struct blk_mq_hw_ctx *hctx;
159 unsigned int i;
160
161 queue_for_each_hw_ctx(q, hctx, i)
162 if (blk_mq_hw_queue_mapped(hctx))
163 blk_mq_tag_wakeup_all(hctx->tags, true);
Keith Busch3fd59402015-01-08 08:53:56 -0700164
165 /*
166 * If we are called because the queue has now been marked as
167 * dying, we need to ensure that processes currently waiting on
168 * the queue are notified as well.
169 */
170 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700171}
172
Jens Axboe320ae512013-10-24 09:20:05 +0100173bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
174{
175 return blk_mq_has_free_tags(hctx->tags);
176}
177EXPORT_SYMBOL(blk_mq_can_queue);
178
Jens Axboe94eddfb2013-11-19 09:25:07 -0700179static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
180 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100181{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700182 if (blk_queue_io_stat(q))
183 rw_flags |= REQ_IO_STAT;
184
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200185 INIT_LIST_HEAD(&rq->queuelist);
186 /* csd/requeue_work/fifo_time is initialized before use */
187 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100188 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600189 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200190 /* do not touch atomic flags, it needs atomic ops against the timer */
191 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200192 INIT_HLIST_NODE(&rq->hash);
193 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200194 rq->rq_disk = NULL;
195 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600196 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200197#ifdef CONFIG_BLK_CGROUP
198 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700199 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200200 rq->io_start_time_ns = 0;
201#endif
202 rq->nr_phys_segments = 0;
203#if defined(CONFIG_BLK_DEV_INTEGRITY)
204 rq->nr_integrity_segments = 0;
205#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200206 rq->special = NULL;
207 /* tag was already set */
208 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200209
Tony Battersby6f4a16262014-08-22 15:53:39 -0400210 rq->cmd = rq->__cmd;
211
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200212 rq->extra_len = 0;
213 rq->sense_len = 0;
214 rq->resid_len = 0;
215 rq->sense = NULL;
216
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200217 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600218 rq->timeout = 0;
219
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200220 rq->end_io = NULL;
221 rq->end_io_data = NULL;
222 rq->next_rq = NULL;
223
Jens Axboe320ae512013-10-24 09:20:05 +0100224 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
225}
226
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200227static struct request *
Ming Leicb96a422014-06-01 00:43:37 +0800228__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200229{
230 struct request *rq;
231 unsigned int tag;
232
Ming Leicb96a422014-06-01 00:43:37 +0800233 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200234 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a422014-06-01 00:43:37 +0800235 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200236
Ming Leicb96a422014-06-01 00:43:37 +0800237 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200238 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a422014-06-01 00:43:37 +0800239 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200240 }
241
242 rq->tag = tag;
Ming Leicb96a422014-06-01 00:43:37 +0800243 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200244 return rq;
245 }
246
247 return NULL;
248}
249
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200250struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
251 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100252{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200253 struct blk_mq_ctx *ctx;
254 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100255 struct request *rq;
Ming Leicb96a422014-06-01 00:43:37 +0800256 struct blk_mq_alloc_data alloc_data;
Joe Lawrencea492f072014-08-28 08:15:21 -0600257 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100258
Keith Buschbfd343a2015-03-11 23:56:39 -0400259 ret = blk_mq_queue_enter(q, gfp);
Joe Lawrencea492f072014-08-28 08:15:21 -0600260 if (ret)
261 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100262
Christoph Hellwigd8525642014-05-27 20:59:50 +0200263 ctx = blk_mq_get_ctx(q);
264 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800265 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
266 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200267
Ming Leicb96a422014-06-01 00:43:37 +0800268 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200269 if (!rq && (gfp & __GFP_WAIT)) {
270 __blk_mq_run_hw_queue(hctx);
271 blk_mq_put_ctx(ctx);
272
273 ctx = blk_mq_get_ctx(q);
274 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800275 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
276 hctx);
277 rq = __blk_mq_alloc_request(&alloc_data, rw);
278 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200279 }
280 blk_mq_put_ctx(ctx);
Keith Buschc76541a2014-12-19 17:54:13 -0700281 if (!rq) {
282 blk_mq_queue_exit(q);
Joe Lawrencea492f072014-08-28 08:15:21 -0600283 return ERR_PTR(-EWOULDBLOCK);
Keith Buschc76541a2014-12-19 17:54:13 -0700284 }
Jens Axboe320ae512013-10-24 09:20:05 +0100285 return rq;
286}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600287EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100288
Jens Axboe320ae512013-10-24 09:20:05 +0100289static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
290 struct blk_mq_ctx *ctx, struct request *rq)
291{
292 const int tag = rq->tag;
293 struct request_queue *q = rq->q;
294
Jens Axboe0d2602c2014-05-13 15:10:52 -0600295 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
296 atomic_dec(&hctx->nr_active);
David Hildenbrand683d0e12014-09-18 11:04:31 +0200297 rq->cmd_flags = 0;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600298
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200299 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600300 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100301 blk_mq_queue_exit(q);
302}
303
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700304void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100305{
306 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700307
308 ctx->rq_completed[rq_is_sync(rq)]++;
309 __blk_mq_free_request(hctx, ctx, rq);
310
311}
312EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
313
314void blk_mq_free_request(struct request *rq)
315{
Jens Axboe320ae512013-10-24 09:20:05 +0100316 struct blk_mq_hw_ctx *hctx;
317 struct request_queue *q = rq->q;
318
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700319 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
320 blk_mq_free_hctx_request(hctx, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100321}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700322EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100323
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700324inline void __blk_mq_end_request(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100325{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700326 blk_account_io_done(rq);
327
Christoph Hellwig91b63632014-04-16 09:44:53 +0200328 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100329 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200330 } else {
331 if (unlikely(blk_bidi_rq(rq)))
332 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100333 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200334 }
Jens Axboe320ae512013-10-24 09:20:05 +0100335}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700336EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200337
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700338void blk_mq_end_request(struct request *rq, int error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200339{
340 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
341 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700342 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200343}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700344EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100345
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800346static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100347{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800348 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100349
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800350 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100351}
352
Jens Axboeed851862014-05-30 21:20:50 -0600353static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100354{
355 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700356 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100357 int cpu;
358
Christoph Hellwig38535202014-04-25 02:32:53 -0700359 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800360 rq->q->softirq_done_fn(rq);
361 return;
362 }
Jens Axboe320ae512013-10-24 09:20:05 +0100363
364 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700365 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
366 shared = cpus_share_cache(cpu, ctx->cpu);
367
368 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800369 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800370 rq->csd.info = rq;
371 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100372 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800373 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800374 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800375 }
Jens Axboe320ae512013-10-24 09:20:05 +0100376 put_cpu();
377}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800378
Jens Axboeed851862014-05-30 21:20:50 -0600379void __blk_mq_complete_request(struct request *rq)
380{
381 struct request_queue *q = rq->q;
382
383 if (!q->softirq_done_fn)
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700384 blk_mq_end_request(rq, rq->errors);
Jens Axboeed851862014-05-30 21:20:50 -0600385 else
386 blk_mq_ipi_complete_request(rq);
387}
388
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800389/**
390 * blk_mq_complete_request - end I/O on a request
391 * @rq: the request being processed
392 *
393 * Description:
394 * Ends all I/O on a request. It does not handle partial completions.
395 * The actual completion happens out-of-order, through a IPI handler.
396 **/
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200397void blk_mq_complete_request(struct request *rq, int error)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800398{
Jens Axboe95f09682014-05-27 17:46:48 -0600399 struct request_queue *q = rq->q;
400
401 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800402 return;
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200403 if (!blk_mark_rq_complete(rq)) {
404 rq->errors = error;
Jens Axboeed851862014-05-30 21:20:50 -0600405 __blk_mq_complete_request(rq);
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200406 }
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800407}
408EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100409
Keith Busch973c0192015-01-07 18:55:43 -0700410int blk_mq_request_started(struct request *rq)
411{
412 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
413}
414EXPORT_SYMBOL_GPL(blk_mq_request_started);
415
Christoph Hellwige2490072014-09-13 16:40:09 -0700416void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100417{
418 struct request_queue *q = rq->q;
419
420 trace_block_rq_issue(q, rq);
421
Christoph Hellwig742ee692014-04-14 10:30:06 +0200422 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200423 if (unlikely(blk_bidi_rq(rq)))
424 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200425
Ming Lei2b8393b2014-06-10 00:16:41 +0800426 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600427
428 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600429 * Ensure that ->deadline is visible before set the started
430 * flag and clear the completed flag.
431 */
432 smp_mb__before_atomic();
433
434 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600435 * Mark us as started and clear complete. Complete might have been
436 * set if requeue raced with timeout, which then marked it as
437 * complete. So be sure to clear complete again when we start
438 * the request, otherwise we'll ignore the completion event.
439 */
Jens Axboe4b570522014-05-29 11:00:11 -0600440 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
441 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
442 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
443 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800444
445 if (q->dma_drain_size && blk_rq_bytes(rq)) {
446 /*
447 * Make sure space for the drain appears. We know we can do
448 * this because max_hw_segments has been adjusted to be one
449 * fewer than the device can handle.
450 */
451 rq->nr_phys_segments++;
452 }
Jens Axboe320ae512013-10-24 09:20:05 +0100453}
Christoph Hellwige2490072014-09-13 16:40:09 -0700454EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100455
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200456static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100457{
458 struct request_queue *q = rq->q;
459
460 trace_block_rq_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800461
Christoph Hellwige2490072014-09-13 16:40:09 -0700462 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
463 if (q->dma_drain_size && blk_rq_bytes(rq))
464 rq->nr_phys_segments--;
465 }
Jens Axboe320ae512013-10-24 09:20:05 +0100466}
467
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200468void blk_mq_requeue_request(struct request *rq)
469{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200470 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200471
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200472 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600473 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200474}
475EXPORT_SYMBOL(blk_mq_requeue_request);
476
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600477static void blk_mq_requeue_work(struct work_struct *work)
478{
479 struct request_queue *q =
480 container_of(work, struct request_queue, requeue_work);
481 LIST_HEAD(rq_list);
482 struct request *rq, *next;
483 unsigned long flags;
484
485 spin_lock_irqsave(&q->requeue_lock, flags);
486 list_splice_init(&q->requeue_list, &rq_list);
487 spin_unlock_irqrestore(&q->requeue_lock, flags);
488
489 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
490 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
491 continue;
492
493 rq->cmd_flags &= ~REQ_SOFTBARRIER;
494 list_del_init(&rq->queuelist);
495 blk_mq_insert_request(rq, true, false, false);
496 }
497
498 while (!list_empty(&rq_list)) {
499 rq = list_entry(rq_list.next, struct request, queuelist);
500 list_del_init(&rq->queuelist);
501 blk_mq_insert_request(rq, false, false, false);
502 }
503
Jens Axboe8b957412014-09-19 13:10:29 -0600504 /*
505 * Use the start variant of queue running here, so that running
506 * the requeue work will kick stopped queues.
507 */
508 blk_mq_start_hw_queues(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600509}
510
511void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
512{
513 struct request_queue *q = rq->q;
514 unsigned long flags;
515
516 /*
517 * We abuse this flag that is otherwise used by the I/O scheduler to
518 * request head insertation from the workqueue.
519 */
520 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
521
522 spin_lock_irqsave(&q->requeue_lock, flags);
523 if (at_head) {
524 rq->cmd_flags |= REQ_SOFTBARRIER;
525 list_add(&rq->queuelist, &q->requeue_list);
526 } else {
527 list_add_tail(&rq->queuelist, &q->requeue_list);
528 }
529 spin_unlock_irqrestore(&q->requeue_lock, flags);
530}
531EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
532
Keith Buschc68ed592015-01-07 18:55:44 -0700533void blk_mq_cancel_requeue_work(struct request_queue *q)
534{
535 cancel_work_sync(&q->requeue_work);
536}
537EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work);
538
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600539void blk_mq_kick_requeue_list(struct request_queue *q)
540{
541 kblockd_schedule_work(&q->requeue_work);
542}
543EXPORT_SYMBOL(blk_mq_kick_requeue_list);
544
Jens Axboe1885b242015-01-07 18:55:45 -0700545void blk_mq_abort_requeue_list(struct request_queue *q)
546{
547 unsigned long flags;
548 LIST_HEAD(rq_list);
549
550 spin_lock_irqsave(&q->requeue_lock, flags);
551 list_splice_init(&q->requeue_list, &rq_list);
552 spin_unlock_irqrestore(&q->requeue_lock, flags);
553
554 while (!list_empty(&rq_list)) {
555 struct request *rq;
556
557 rq = list_first_entry(&rq_list, struct request, queuelist);
558 list_del_init(&rq->queuelist);
559 rq->errors = -EIO;
560 blk_mq_end_request(rq, rq->errors);
561 }
562}
563EXPORT_SYMBOL(blk_mq_abort_requeue_list);
564
Jens Axboe0e62f512014-06-04 10:23:49 -0600565struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
566{
Ming Lei0048b482015-08-09 03:41:51 -0400567 return tags->rqs[tag];
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600568}
569EXPORT_SYMBOL(blk_mq_tag_to_rq);
570
Jens Axboe320ae512013-10-24 09:20:05 +0100571struct blk_mq_timeout_data {
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700572 unsigned long next;
573 unsigned int next_set;
Jens Axboe320ae512013-10-24 09:20:05 +0100574};
575
Christoph Hellwig90415832014-09-22 10:21:48 -0600576void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100577{
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700578 struct blk_mq_ops *ops = req->q->mq_ops;
579 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600580
581 /*
582 * We know that complete is set at this point. If STARTED isn't set
583 * anymore, then the request isn't active and the "timeout" should
584 * just be ignored. This can happen due to the bitflag ordering.
585 * Timeout first checks if STARTED is set, and if it is, assumes
586 * the request is active. But if we race with completion, then
587 * we both flags will get cleared. So check here again, and ignore
588 * a timeout event with a request that isn't active.
589 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700590 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
591 return;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600592
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700593 if (ops->timeout)
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700594 ret = ops->timeout(req, reserved);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600595
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700596 switch (ret) {
597 case BLK_EH_HANDLED:
598 __blk_mq_complete_request(req);
599 break;
600 case BLK_EH_RESET_TIMER:
601 blk_add_timer(req);
602 blk_clear_rq_complete(req);
603 break;
604 case BLK_EH_NOT_HANDLED:
605 break;
606 default:
607 printk(KERN_ERR "block: bad eh return: %d\n", ret);
608 break;
609 }
Jens Axboe87ee7b12014-04-24 08:51:47 -0600610}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700611
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700612static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
613 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100614{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700615 struct blk_mq_timeout_data *data = priv;
616
Keith Buscheb130db2015-01-08 08:59:53 -0700617 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
618 /*
619 * If a request wasn't started before the queue was
620 * marked dying, kill it here or it'll go unnoticed.
621 */
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200622 if (unlikely(blk_queue_dying(rq->q)))
623 blk_mq_complete_request(rq, -EIO);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700624 return;
Keith Buscheb130db2015-01-08 08:59:53 -0700625 }
Keith Busch5b3f25f2015-01-07 18:55:46 -0700626 if (rq->cmd_flags & REQ_NO_TIMEOUT)
627 return;
Jens Axboe320ae512013-10-24 09:20:05 +0100628
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700629 if (time_after_eq(jiffies, rq->deadline)) {
630 if (!blk_mark_rq_complete(rq))
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700631 blk_mq_rq_timed_out(rq, reserved);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700632 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
633 data->next = rq->deadline;
634 data->next_set = 1;
635 }
Jens Axboe320ae512013-10-24 09:20:05 +0100636}
637
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700638static void blk_mq_rq_timer(unsigned long priv)
Jens Axboe320ae512013-10-24 09:20:05 +0100639{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700640 struct request_queue *q = (struct request_queue *)priv;
641 struct blk_mq_timeout_data data = {
642 .next = 0,
643 .next_set = 0,
644 };
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700645 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100646
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200647 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
Jens Axboe320ae512013-10-24 09:20:05 +0100648
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700649 if (data.next_set) {
650 data.next = blk_rq_timeout(round_jiffies_up(data.next));
651 mod_timer(&q->timeout, data.next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600652 } else {
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200653 struct blk_mq_hw_ctx *hctx;
654
Ming Leif054b562015-04-21 10:00:19 +0800655 queue_for_each_hw_ctx(q, hctx, i) {
656 /* the hctx may be unmapped, so check it here */
657 if (blk_mq_hw_queue_mapped(hctx))
658 blk_mq_tag_idle(hctx);
659 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600660 }
Jens Axboe320ae512013-10-24 09:20:05 +0100661}
662
663/*
664 * Reverse check our software queue for entries that we could potentially
665 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
666 * too much time checking for merges.
667 */
668static bool blk_mq_attempt_merge(struct request_queue *q,
669 struct blk_mq_ctx *ctx, struct bio *bio)
670{
671 struct request *rq;
672 int checked = 8;
673
674 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
675 int el_ret;
676
677 if (!checked--)
678 break;
679
680 if (!blk_rq_merge_ok(rq, bio))
681 continue;
682
683 el_ret = blk_try_merge(rq, bio);
684 if (el_ret == ELEVATOR_BACK_MERGE) {
685 if (bio_attempt_back_merge(q, rq, bio)) {
686 ctx->rq_merged++;
687 return true;
688 }
689 break;
690 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
691 if (bio_attempt_front_merge(q, rq, bio)) {
692 ctx->rq_merged++;
693 return true;
694 }
695 break;
696 }
697 }
698
699 return false;
700}
701
Jens Axboe320ae512013-10-24 09:20:05 +0100702/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600703 * Process software queues that have been marked busy, splicing them
704 * to the for-dispatch
705 */
706static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
707{
708 struct blk_mq_ctx *ctx;
709 int i;
710
Jens Axboe569fd0c2015-04-17 08:28:50 -0600711 for (i = 0; i < hctx->ctx_map.size; i++) {
Jens Axboe1429d7c2014-05-19 09:23:55 -0600712 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
713 unsigned int off, bit;
714
715 if (!bm->word)
716 continue;
717
718 bit = 0;
719 off = i * hctx->ctx_map.bits_per_word;
720 do {
721 bit = find_next_bit(&bm->word, bm->depth, bit);
722 if (bit >= bm->depth)
723 break;
724
725 ctx = hctx->ctxs[bit + off];
726 clear_bit(bit, &bm->word);
727 spin_lock(&ctx->lock);
728 list_splice_tail_init(&ctx->rq_list, list);
729 spin_unlock(&ctx->lock);
730
731 bit++;
732 } while (1);
733 }
734}
735
736/*
Jens Axboe320ae512013-10-24 09:20:05 +0100737 * Run this hardware queue, pulling any software queues mapped to it in.
738 * Note that this function currently has various problems around ordering
739 * of IO. In particular, we'd like FIFO behaviour on handling existing
740 * items on the hctx->dispatch list. Ignore that for now.
741 */
742static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
743{
744 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100745 struct request *rq;
746 LIST_HEAD(rq_list);
Jens Axboe74c45052014-10-29 11:14:52 -0600747 LIST_HEAD(driver_list);
748 struct list_head *dptr;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600749 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100750
Jens Axboefd1270d2014-04-16 09:23:48 -0600751 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600752
Jens Axboe5d12f902014-03-19 15:25:02 -0600753 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100754 return;
755
756 hctx->run++;
757
758 /*
759 * Touch any software queue that has pending entries.
760 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600761 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100762
763 /*
764 * If we have previous entries on our dispatch list, grab them
765 * and stuff them at the front for more fair dispatch.
766 */
767 if (!list_empty_careful(&hctx->dispatch)) {
768 spin_lock(&hctx->lock);
769 if (!list_empty(&hctx->dispatch))
770 list_splice_init(&hctx->dispatch, &rq_list);
771 spin_unlock(&hctx->lock);
772 }
773
774 /*
Jens Axboe74c45052014-10-29 11:14:52 -0600775 * Start off with dptr being NULL, so we start the first request
776 * immediately, even if we have more pending.
777 */
778 dptr = NULL;
779
780 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100781 * Now process all the entries, sending them to the driver.
782 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600783 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100784 while (!list_empty(&rq_list)) {
Jens Axboe74c45052014-10-29 11:14:52 -0600785 struct blk_mq_queue_data bd;
Jens Axboe320ae512013-10-24 09:20:05 +0100786 int ret;
787
788 rq = list_first_entry(&rq_list, struct request, queuelist);
789 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100790
Jens Axboe74c45052014-10-29 11:14:52 -0600791 bd.rq = rq;
792 bd.list = dptr;
793 bd.last = list_empty(&rq_list);
794
795 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe320ae512013-10-24 09:20:05 +0100796 switch (ret) {
797 case BLK_MQ_RQ_QUEUE_OK:
798 queued++;
799 continue;
800 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100801 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200802 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100803 break;
804 default:
805 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100806 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800807 rq->errors = -EIO;
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700808 blk_mq_end_request(rq, rq->errors);
Jens Axboe320ae512013-10-24 09:20:05 +0100809 break;
810 }
811
812 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
813 break;
Jens Axboe74c45052014-10-29 11:14:52 -0600814
815 /*
816 * We've done the first request. If we have more than 1
817 * left in the list, set dptr to defer issue.
818 */
819 if (!dptr && rq_list.next != rq_list.prev)
820 dptr = &driver_list;
Jens Axboe320ae512013-10-24 09:20:05 +0100821 }
822
823 if (!queued)
824 hctx->dispatched[0]++;
825 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
826 hctx->dispatched[ilog2(queued) + 1]++;
827
828 /*
829 * Any items that need requeuing? Stuff them into hctx->dispatch,
830 * that is where we will continue on next queue run.
831 */
832 if (!list_empty(&rq_list)) {
833 spin_lock(&hctx->lock);
834 list_splice(&rq_list, &hctx->dispatch);
835 spin_unlock(&hctx->lock);
Shaohua Li9ba52e52015-05-04 14:32:48 -0600836 /*
837 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
838 * it's possible the queue is stopped and restarted again
839 * before this. Queue restart will dispatch requests. And since
840 * requests in rq_list aren't added into hctx->dispatch yet,
841 * the requests in rq_list might get lost.
842 *
843 * blk_mq_run_hw_queue() already checks the STOPPED bit
844 **/
845 blk_mq_run_hw_queue(hctx, true);
Jens Axboe320ae512013-10-24 09:20:05 +0100846 }
847}
848
Jens Axboe506e9312014-05-07 10:26:44 -0600849/*
850 * It'd be great if the workqueue API had a way to pass
851 * in a mask and had some smarts for more clever placement.
852 * For now we just round-robin here, switching for every
853 * BLK_MQ_CPU_WORK_BATCH queued items.
854 */
855static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
856{
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100857 if (hctx->queue->nr_hw_queues == 1)
858 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -0600859
860 if (--hctx->next_cpu_batch <= 0) {
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100861 int cpu = hctx->next_cpu, next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600862
863 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
864 if (next_cpu >= nr_cpu_ids)
865 next_cpu = cpumask_first(hctx->cpumask);
866
867 hctx->next_cpu = next_cpu;
868 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100869
870 return cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600871 }
872
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100873 return hctx->next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600874}
875
Jens Axboe320ae512013-10-24 09:20:05 +0100876void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
877{
Ming Lei19c66e52014-12-03 19:38:04 +0800878 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state) ||
879 !blk_mq_hw_queue_mapped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +0100880 return;
881
Paolo Bonzini398205b2014-11-07 23:03:59 +0100882 if (!async) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100883 int cpu = get_cpu();
884 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +0100885 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100886 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +0100887 return;
888 }
Jens Axboee4043dc2014-04-09 10:18:23 -0600889
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100890 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -0600891 }
Paolo Bonzini398205b2014-11-07 23:03:59 +0100892
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100893 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
894 &hctx->run_work, 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100895}
896
Mike Snitzerb94ec292015-03-11 23:56:38 -0400897void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100898{
899 struct blk_mq_hw_ctx *hctx;
900 int i;
901
902 queue_for_each_hw_ctx(q, hctx, i) {
903 if ((!blk_mq_hctx_has_pending(hctx) &&
904 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600905 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100906 continue;
907
Mike Snitzerb94ec292015-03-11 23:56:38 -0400908 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +0100909 }
910}
Mike Snitzerb94ec292015-03-11 23:56:38 -0400911EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +0100912
913void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
914{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600915 cancel_delayed_work(&hctx->run_work);
916 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100917 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
918}
919EXPORT_SYMBOL(blk_mq_stop_hw_queue);
920
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100921void blk_mq_stop_hw_queues(struct request_queue *q)
922{
923 struct blk_mq_hw_ctx *hctx;
924 int i;
925
926 queue_for_each_hw_ctx(q, hctx, i)
927 blk_mq_stop_hw_queue(hctx);
928}
929EXPORT_SYMBOL(blk_mq_stop_hw_queues);
930
Jens Axboe320ae512013-10-24 09:20:05 +0100931void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
932{
933 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600934
Jens Axboe0ffbce82014-06-25 08:22:34 -0600935 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100936}
937EXPORT_SYMBOL(blk_mq_start_hw_queue);
938
Christoph Hellwig2f268552014-04-16 09:44:56 +0200939void blk_mq_start_hw_queues(struct request_queue *q)
940{
941 struct blk_mq_hw_ctx *hctx;
942 int i;
943
944 queue_for_each_hw_ctx(q, hctx, i)
945 blk_mq_start_hw_queue(hctx);
946}
947EXPORT_SYMBOL(blk_mq_start_hw_queues);
948
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200949void blk_mq_start_stopped_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 (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
956 continue;
957
958 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200959 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +0100960 }
961}
962EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
963
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600964static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100965{
966 struct blk_mq_hw_ctx *hctx;
967
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600968 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600969
Jens Axboe320ae512013-10-24 09:20:05 +0100970 __blk_mq_run_hw_queue(hctx);
971}
972
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600973static void blk_mq_delay_work_fn(struct work_struct *work)
974{
975 struct blk_mq_hw_ctx *hctx;
976
977 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
978
979 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
980 __blk_mq_run_hw_queue(hctx);
981}
982
983void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
984{
Ming Lei19c66e52014-12-03 19:38:04 +0800985 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
986 return;
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600987
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100988 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
989 &hctx->delay_work, msecs_to_jiffies(msecs));
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600990}
991EXPORT_SYMBOL(blk_mq_delay_queue);
992
Jens Axboe320ae512013-10-24 09:20:05 +0100993static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800994 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100995{
996 struct blk_mq_ctx *ctx = rq->mq_ctx;
997
Jens Axboe01b983c2013-11-19 18:59:10 -0700998 trace_block_rq_insert(hctx->queue, rq);
999
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001000 if (at_head)
1001 list_add(&rq->queuelist, &ctx->rq_list);
1002 else
1003 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -06001004
Jens Axboe320ae512013-10-24 09:20:05 +01001005 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001006}
1007
Christoph Hellwigeeabc852014-03-21 08:57:37 -06001008void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
1009 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001010{
1011 struct request_queue *q = rq->q;
1012 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -06001013 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001014
1015 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -06001016 if (!cpu_online(ctx->cpu))
1017 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001018
Jens Axboe320ae512013-10-24 09:20:05 +01001019 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1020
Christoph Hellwiga57a1782014-09-16 14:44:07 -07001021 spin_lock(&ctx->lock);
1022 __blk_mq_insert_request(hctx, rq, at_head);
1023 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001024
Jens Axboe320ae512013-10-24 09:20:05 +01001025 if (run_queue)
1026 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -06001027
1028 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001029}
1030
1031static void blk_mq_insert_requests(struct request_queue *q,
1032 struct blk_mq_ctx *ctx,
1033 struct list_head *list,
1034 int depth,
1035 bool from_schedule)
1036
1037{
1038 struct blk_mq_hw_ctx *hctx;
1039 struct blk_mq_ctx *current_ctx;
1040
1041 trace_block_unplug(q, depth, !from_schedule);
1042
1043 current_ctx = blk_mq_get_ctx(q);
1044
1045 if (!cpu_online(ctx->cpu))
1046 ctx = current_ctx;
1047 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1048
1049 /*
1050 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1051 * offline now
1052 */
1053 spin_lock(&ctx->lock);
1054 while (!list_empty(list)) {
1055 struct request *rq;
1056
1057 rq = list_first_entry(list, struct request, queuelist);
1058 list_del_init(&rq->queuelist);
1059 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001060 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001061 }
1062 spin_unlock(&ctx->lock);
1063
Jens Axboe320ae512013-10-24 09:20:05 +01001064 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001065 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001066}
1067
1068static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1069{
1070 struct request *rqa = container_of(a, struct request, queuelist);
1071 struct request *rqb = container_of(b, struct request, queuelist);
1072
1073 return !(rqa->mq_ctx < rqb->mq_ctx ||
1074 (rqa->mq_ctx == rqb->mq_ctx &&
1075 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1076}
1077
1078void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1079{
1080 struct blk_mq_ctx *this_ctx;
1081 struct request_queue *this_q;
1082 struct request *rq;
1083 LIST_HEAD(list);
1084 LIST_HEAD(ctx_list);
1085 unsigned int depth;
1086
1087 list_splice_init(&plug->mq_list, &list);
1088
1089 list_sort(NULL, &list, plug_ctx_cmp);
1090
1091 this_q = NULL;
1092 this_ctx = NULL;
1093 depth = 0;
1094
1095 while (!list_empty(&list)) {
1096 rq = list_entry_rq(list.next);
1097 list_del_init(&rq->queuelist);
1098 BUG_ON(!rq->q);
1099 if (rq->mq_ctx != this_ctx) {
1100 if (this_ctx) {
1101 blk_mq_insert_requests(this_q, this_ctx,
1102 &ctx_list, depth,
1103 from_schedule);
1104 }
1105
1106 this_ctx = rq->mq_ctx;
1107 this_q = rq->q;
1108 depth = 0;
1109 }
1110
1111 depth++;
1112 list_add_tail(&rq->queuelist, &ctx_list);
1113 }
1114
1115 /*
1116 * If 'this_ctx' is set, we know we have entries to complete
1117 * on 'ctx_list'. Do those.
1118 */
1119 if (this_ctx) {
1120 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1121 from_schedule);
1122 }
1123}
1124
1125static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1126{
1127 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001128
Jens Axboe3ee32372014-06-09 09:36:53 -06001129 if (blk_do_io_stat(rq))
Jens Axboe4b570522014-05-29 11:00:11 -06001130 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001131}
1132
Jens Axboe274a5842014-08-15 12:44:08 -06001133static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1134{
1135 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1136 !blk_queue_nomerges(hctx->queue);
1137}
1138
Jens Axboe07068d52014-05-22 10:40:51 -06001139static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1140 struct blk_mq_ctx *ctx,
1141 struct request *rq, struct bio *bio)
1142{
Ming Leie18378a2015-10-20 23:13:54 +08001143 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001144 blk_mq_bio_to_request(rq, bio);
1145 spin_lock(&ctx->lock);
1146insert_rq:
1147 __blk_mq_insert_request(hctx, rq, false);
1148 spin_unlock(&ctx->lock);
1149 return false;
1150 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001151 struct request_queue *q = hctx->queue;
1152
Jens Axboe07068d52014-05-22 10:40:51 -06001153 spin_lock(&ctx->lock);
1154 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1155 blk_mq_bio_to_request(rq, bio);
1156 goto insert_rq;
1157 }
1158
1159 spin_unlock(&ctx->lock);
1160 __blk_mq_free_request(hctx, ctx, rq);
1161 return true;
1162 }
1163}
1164
1165struct blk_map_ctx {
1166 struct blk_mq_hw_ctx *hctx;
1167 struct blk_mq_ctx *ctx;
1168};
1169
1170static struct request *blk_mq_map_request(struct request_queue *q,
1171 struct bio *bio,
1172 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001173{
1174 struct blk_mq_hw_ctx *hctx;
1175 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001176 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001177 int rw = bio_data_dir(bio);
Ming Leicb96a422014-06-01 00:43:37 +08001178 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001179
Keith Buschbfd343a2015-03-11 23:56:39 -04001180 if (unlikely(blk_mq_queue_enter(q, GFP_KERNEL))) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001181 bio_io_error(bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001182 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001183 }
1184
1185 ctx = blk_mq_get_ctx(q);
1186 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1187
Jens Axboe07068d52014-05-22 10:40:51 -06001188 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001189 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001190
Jens Axboe320ae512013-10-24 09:20:05 +01001191 trace_block_getrq(q, bio, rw);
Ming Leicb96a422014-06-01 00:43:37 +08001192 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1193 hctx);
1194 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001195 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001196 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001197 blk_mq_put_ctx(ctx);
1198 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001199
1200 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001201 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +08001202 blk_mq_set_alloc_data(&alloc_data, q,
1203 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1204 rq = __blk_mq_alloc_request(&alloc_data, rw);
1205 ctx = alloc_data.ctx;
1206 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001207 }
1208
1209 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001210 data->hctx = hctx;
1211 data->ctx = ctx;
1212 return rq;
1213}
1214
Shaohua Lif984df12015-05-08 10:51:32 -07001215static int blk_mq_direct_issue_request(struct request *rq)
1216{
1217 int ret;
1218 struct request_queue *q = rq->q;
1219 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q,
1220 rq->mq_ctx->cpu);
1221 struct blk_mq_queue_data bd = {
1222 .rq = rq,
1223 .list = NULL,
1224 .last = 1
1225 };
1226
1227 /*
1228 * For OK queue, we are done. For error, kill it. Any other
1229 * error (busy), just add it to our list as we previously
1230 * would have done
1231 */
1232 ret = q->mq_ops->queue_rq(hctx, &bd);
1233 if (ret == BLK_MQ_RQ_QUEUE_OK)
1234 return 0;
1235 else {
1236 __blk_mq_requeue_request(rq);
1237
1238 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1239 rq->errors = -EIO;
1240 blk_mq_end_request(rq, rq->errors);
1241 return 0;
1242 }
1243 return -1;
1244 }
1245}
1246
Jens Axboe07068d52014-05-22 10:40:51 -06001247/*
1248 * Multiple hardware queue variant. This will not use per-process plugs,
1249 * but will attempt to bypass the hctx queueing if we can go straight to
1250 * hardware for SYNC IO.
1251 */
1252static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1253{
1254 const int is_sync = rw_is_sync(bio->bi_rw);
1255 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1256 struct blk_map_ctx data;
1257 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001258 unsigned int request_count = 0;
1259 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001260 struct request *same_queue_rq = NULL;
Jens Axboe07068d52014-05-22 10:40:51 -06001261
1262 blk_queue_bounce(q, &bio);
1263
1264 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001265 bio_io_error(bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001266 return;
1267 }
1268
Kent Overstreet54efd502015-04-23 22:37:18 -07001269 blk_queue_split(q, &bio, q->bio_split);
1270
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001271 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1272 if (blk_attempt_plug_merge(q, bio, &request_count,
1273 &same_queue_rq))
1274 return;
1275 } else
1276 request_count = blk_plug_queued_count(q);
Shaohua Lif984df12015-05-08 10:51:32 -07001277
Jens Axboe07068d52014-05-22 10:40:51 -06001278 rq = blk_mq_map_request(q, bio, &data);
1279 if (unlikely(!rq))
1280 return;
1281
1282 if (unlikely(is_flush_fua)) {
1283 blk_mq_bio_to_request(rq, bio);
1284 blk_insert_flush(rq);
1285 goto run_queue;
1286 }
1287
Shaohua Lif984df12015-05-08 10:51:32 -07001288 plug = current->plug;
Jens Axboee167dfb2014-10-29 11:18:26 -06001289 /*
1290 * If the driver supports defer issued based on 'last', then
1291 * queue it up like normal since we can potentially save some
1292 * CPU this way.
1293 */
Shaohua Lif984df12015-05-08 10:51:32 -07001294 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1295 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1296 struct request *old_rq = NULL;
Jens Axboe07068d52014-05-22 10:40:51 -06001297
1298 blk_mq_bio_to_request(rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001299
1300 /*
Shaohua Lif984df12015-05-08 10:51:32 -07001301 * we do limited pluging. If bio can be merged, do merge.
1302 * Otherwise the existing request in the plug list will be
1303 * issued. So the plug list will have one request at most
Jens Axboe07068d52014-05-22 10:40:51 -06001304 */
Shaohua Lif984df12015-05-08 10:51:32 -07001305 if (plug) {
Shaohua Li5b3f3412015-05-08 10:51:33 -07001306 /*
1307 * The plug list might get flushed before this. If that
1308 * happens, same_queue_rq is invalid and plug list is empty
1309 **/
1310 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1311 old_rq = same_queue_rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001312 list_del_init(&old_rq->queuelist);
Jens Axboe07068d52014-05-22 10:40:51 -06001313 }
Shaohua Lif984df12015-05-08 10:51:32 -07001314 list_add_tail(&rq->queuelist, &plug->mq_list);
1315 } else /* is_sync */
1316 old_rq = rq;
1317 blk_mq_put_ctx(data.ctx);
1318 if (!old_rq)
Shaohua Li239ad212015-05-08 10:51:31 -07001319 return;
Shaohua Lif984df12015-05-08 10:51:32 -07001320 if (!blk_mq_direct_issue_request(old_rq))
1321 return;
1322 blk_mq_insert_request(old_rq, false, true, true);
1323 return;
Jens Axboe07068d52014-05-22 10:40:51 -06001324 }
1325
1326 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1327 /*
1328 * For a SYNC request, send it to the hardware immediately. For
1329 * an ASYNC request, just ensure that we run it later on. The
1330 * latter allows for merging opportunities and more efficient
1331 * dispatching.
1332 */
1333run_queue:
1334 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1335 }
Jens Axboe07068d52014-05-22 10:40:51 -06001336 blk_mq_put_ctx(data.ctx);
1337}
1338
1339/*
1340 * Single hardware queue variant. This will attempt to use any per-process
1341 * plug for merging and IO deferral.
1342 */
1343static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1344{
1345 const int is_sync = rw_is_sync(bio->bi_rw);
1346 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
Jeff Moyere6c44382015-05-08 10:51:30 -07001347 struct blk_plug *plug;
1348 unsigned int request_count = 0;
Jens Axboe07068d52014-05-22 10:40:51 -06001349 struct blk_map_ctx data;
1350 struct request *rq;
1351
Jens Axboe07068d52014-05-22 10:40:51 -06001352 blk_queue_bounce(q, &bio);
1353
1354 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001355 bio_io_error(bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001356 return;
1357 }
1358
Kent Overstreet54efd502015-04-23 22:37:18 -07001359 blk_queue_split(q, &bio, q->bio_split);
1360
Jeff Moyere6c44382015-05-08 10:51:30 -07001361 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Shaohua Li5b3f3412015-05-08 10:51:33 -07001362 blk_attempt_plug_merge(q, bio, &request_count, NULL))
Jens Axboe07068d52014-05-22 10:40:51 -06001363 return;
1364
1365 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001366 if (unlikely(!rq))
1367 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001368
1369 if (unlikely(is_flush_fua)) {
1370 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001371 blk_insert_flush(rq);
1372 goto run_queue;
1373 }
1374
1375 /*
1376 * A task plug currently exists. Since this is completely lockless,
1377 * utilize that to temporarily store requests until the task is
1378 * either done or scheduled away.
1379 */
Jeff Moyere6c44382015-05-08 10:51:30 -07001380 plug = current->plug;
1381 if (plug) {
1382 blk_mq_bio_to_request(rq, bio);
1383 if (list_empty(&plug->mq_list))
1384 trace_block_plug(q);
1385 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1386 blk_flush_plug_list(plug, false);
1387 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001388 }
Jeff Moyere6c44382015-05-08 10:51:30 -07001389 list_add_tail(&rq->queuelist, &plug->mq_list);
1390 blk_mq_put_ctx(data.ctx);
1391 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001392 }
1393
Jens Axboe07068d52014-05-22 10:40:51 -06001394 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1395 /*
1396 * For a SYNC request, send it to the hardware immediately. For
1397 * an ASYNC request, just ensure that we run it later on. The
1398 * latter allows for merging opportunities and more efficient
1399 * dispatching.
1400 */
1401run_queue:
1402 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001403 }
1404
Jens Axboe07068d52014-05-22 10:40:51 -06001405 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001406}
1407
1408/*
1409 * Default mapping to a software queue, since we use one per CPU.
1410 */
1411struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1412{
1413 return q->queue_hw_ctx[q->mq_map[cpu]];
1414}
1415EXPORT_SYMBOL(blk_mq_map_queue);
1416
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001417static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1418 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001419{
1420 struct page *page;
1421
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001422 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001423 int i;
1424
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001425 for (i = 0; i < tags->nr_tags; i++) {
1426 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001427 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001428 set->ops->exit_request(set->driver_data, tags->rqs[i],
1429 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001430 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001431 }
1432 }
1433
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001434 while (!list_empty(&tags->page_list)) {
1435 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001436 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001437 /*
1438 * Remove kmemleak object previously allocated in
1439 * blk_mq_init_rq_map().
1440 */
1441 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01001442 __free_pages(page, page->private);
1443 }
1444
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001445 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001446
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001447 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001448}
1449
1450static size_t order_to_size(unsigned int order)
1451{
Ming Lei4ca08502014-04-19 18:00:18 +08001452 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001453}
1454
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001455static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1456 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001457{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001458 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001459 unsigned int i, j, entries_per_page, max_order = 4;
1460 size_t rq_size, left;
1461
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001462 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
Shaohua Li24391c02015-01-23 14:18:00 -07001463 set->numa_node,
1464 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001465 if (!tags)
1466 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001467
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001468 INIT_LIST_HEAD(&tags->page_list);
1469
Jens Axboea5164402014-09-10 09:02:03 -06001470 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1471 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1472 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001473 if (!tags->rqs) {
1474 blk_mq_free_tags(tags);
1475 return NULL;
1476 }
Jens Axboe320ae512013-10-24 09:20:05 +01001477
1478 /*
1479 * rq_size is the size of the request plus driver payload, rounded
1480 * to the cacheline size
1481 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001482 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001483 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001484 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001485
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001486 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001487 int this_order = max_order;
1488 struct page *page;
1489 int to_do;
1490 void *p;
1491
1492 while (left < order_to_size(this_order - 1) && this_order)
1493 this_order--;
1494
1495 do {
Jens Axboea5164402014-09-10 09:02:03 -06001496 page = alloc_pages_node(set->numa_node,
Linus Torvaldsac211172015-04-09 14:12:22 -07001497 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06001498 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001499 if (page)
1500 break;
1501 if (!this_order--)
1502 break;
1503 if (order_to_size(this_order) < rq_size)
1504 break;
1505 } while (1);
1506
1507 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001508 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001509
1510 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001511 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001512
1513 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001514 /*
1515 * Allow kmemleak to scan these pages as they contain pointers
1516 * to additional allocations like via ops->init_request().
1517 */
1518 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL);
Jens Axboe320ae512013-10-24 09:20:05 +01001519 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001520 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001521 left -= to_do * rq_size;
1522 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001523 tags->rqs[i] = p;
1524 if (set->ops->init_request) {
1525 if (set->ops->init_request(set->driver_data,
1526 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001527 set->numa_node)) {
1528 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001529 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001530 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001531 }
1532
Jens Axboe320ae512013-10-24 09:20:05 +01001533 p += rq_size;
1534 i++;
1535 }
1536 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001537 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001538
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001539fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001540 blk_mq_free_rq_map(set, tags, hctx_idx);
1541 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001542}
1543
Jens Axboe1429d7c2014-05-19 09:23:55 -06001544static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1545{
1546 kfree(bitmap->map);
1547}
1548
1549static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1550{
1551 unsigned int bpw = 8, total, num_maps, i;
1552
1553 bitmap->bits_per_word = bpw;
1554
1555 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1556 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1557 GFP_KERNEL, node);
1558 if (!bitmap->map)
1559 return -ENOMEM;
1560
Jens Axboe1429d7c2014-05-19 09:23:55 -06001561 total = nr_cpu_ids;
1562 for (i = 0; i < num_maps; i++) {
1563 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1564 total -= bitmap->map[i].depth;
1565 }
1566
1567 return 0;
1568}
1569
Jens Axboe484b4062014-05-21 14:01:15 -06001570static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1571{
1572 struct request_queue *q = hctx->queue;
1573 struct blk_mq_ctx *ctx;
1574 LIST_HEAD(tmp);
1575
1576 /*
1577 * Move ctx entries to new CPU, if this one is going away.
1578 */
1579 ctx = __blk_mq_get_ctx(q, cpu);
1580
1581 spin_lock(&ctx->lock);
1582 if (!list_empty(&ctx->rq_list)) {
1583 list_splice_init(&ctx->rq_list, &tmp);
1584 blk_mq_hctx_clear_pending(hctx, ctx);
1585 }
1586 spin_unlock(&ctx->lock);
1587
1588 if (list_empty(&tmp))
1589 return NOTIFY_OK;
1590
1591 ctx = blk_mq_get_ctx(q);
1592 spin_lock(&ctx->lock);
1593
1594 while (!list_empty(&tmp)) {
1595 struct request *rq;
1596
1597 rq = list_first_entry(&tmp, struct request, queuelist);
1598 rq->mq_ctx = ctx;
1599 list_move_tail(&rq->queuelist, &ctx->rq_list);
1600 }
1601
1602 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1603 blk_mq_hctx_mark_pending(hctx, ctx);
1604
1605 spin_unlock(&ctx->lock);
1606
1607 blk_mq_run_hw_queue(hctx, true);
1608 blk_mq_put_ctx(ctx);
1609 return NOTIFY_OK;
1610}
1611
Jens Axboe484b4062014-05-21 14:01:15 -06001612static int blk_mq_hctx_notify(void *data, unsigned long action,
1613 unsigned int cpu)
1614{
1615 struct blk_mq_hw_ctx *hctx = data;
1616
1617 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1618 return blk_mq_hctx_cpu_offline(hctx, cpu);
Ming Lei2a34c082015-04-21 10:00:20 +08001619
1620 /*
1621 * In case of CPU online, tags may be reallocated
1622 * in blk_mq_map_swqueue() after mapping is updated.
1623 */
Jens Axboe484b4062014-05-21 14:01:15 -06001624
1625 return NOTIFY_OK;
1626}
1627
Ming Leic3b4afc2015-06-04 22:25:04 +08001628/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08001629static void blk_mq_exit_hctx(struct request_queue *q,
1630 struct blk_mq_tag_set *set,
1631 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1632{
Ming Leif70ced02014-09-25 23:23:47 +08001633 unsigned flush_start_tag = set->queue_depth;
1634
Ming Lei08e98fc2014-09-25 23:23:38 +08001635 blk_mq_tag_idle(hctx);
1636
Ming Leif70ced02014-09-25 23:23:47 +08001637 if (set->ops->exit_request)
1638 set->ops->exit_request(set->driver_data,
1639 hctx->fq->flush_rq, hctx_idx,
1640 flush_start_tag + hctx_idx);
1641
Ming Lei08e98fc2014-09-25 23:23:38 +08001642 if (set->ops->exit_hctx)
1643 set->ops->exit_hctx(hctx, hctx_idx);
1644
1645 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Ming Leif70ced02014-09-25 23:23:47 +08001646 blk_free_flush_queue(hctx->fq);
Ming Lei08e98fc2014-09-25 23:23:38 +08001647 blk_mq_free_bitmap(&hctx->ctx_map);
1648}
1649
Ming Lei624dbe42014-05-27 23:35:13 +08001650static void blk_mq_exit_hw_queues(struct request_queue *q,
1651 struct blk_mq_tag_set *set, int nr_queue)
1652{
1653 struct blk_mq_hw_ctx *hctx;
1654 unsigned int i;
1655
1656 queue_for_each_hw_ctx(q, hctx, i) {
1657 if (i == nr_queue)
1658 break;
Ming Lei08e98fc2014-09-25 23:23:38 +08001659 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08001660 }
Ming Lei624dbe42014-05-27 23:35:13 +08001661}
1662
1663static void blk_mq_free_hw_queues(struct request_queue *q,
1664 struct blk_mq_tag_set *set)
1665{
1666 struct blk_mq_hw_ctx *hctx;
1667 unsigned int i;
1668
Ming Leie09aae72015-01-29 20:17:27 +08001669 queue_for_each_hw_ctx(q, hctx, i)
Ming Lei624dbe42014-05-27 23:35:13 +08001670 free_cpumask_var(hctx->cpumask);
Ming Lei624dbe42014-05-27 23:35:13 +08001671}
1672
Ming Lei08e98fc2014-09-25 23:23:38 +08001673static int blk_mq_init_hctx(struct request_queue *q,
1674 struct blk_mq_tag_set *set,
1675 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1676{
1677 int node;
Ming Leif70ced02014-09-25 23:23:47 +08001678 unsigned flush_start_tag = set->queue_depth;
Ming Lei08e98fc2014-09-25 23:23:38 +08001679
1680 node = hctx->numa_node;
1681 if (node == NUMA_NO_NODE)
1682 node = hctx->numa_node = set->numa_node;
1683
1684 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1685 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1686 spin_lock_init(&hctx->lock);
1687 INIT_LIST_HEAD(&hctx->dispatch);
1688 hctx->queue = q;
1689 hctx->queue_num = hctx_idx;
1690 hctx->flags = set->flags;
Ming Lei08e98fc2014-09-25 23:23:38 +08001691
1692 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1693 blk_mq_hctx_notify, hctx);
1694 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1695
1696 hctx->tags = set->tags[hctx_idx];
1697
1698 /*
1699 * Allocate space for all possible cpus to avoid allocation at
1700 * runtime
1701 */
1702 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1703 GFP_KERNEL, node);
1704 if (!hctx->ctxs)
1705 goto unregister_cpu_notifier;
1706
1707 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1708 goto free_ctxs;
1709
1710 hctx->nr_ctx = 0;
1711
1712 if (set->ops->init_hctx &&
1713 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1714 goto free_bitmap;
1715
Ming Leif70ced02014-09-25 23:23:47 +08001716 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1717 if (!hctx->fq)
1718 goto exit_hctx;
1719
1720 if (set->ops->init_request &&
1721 set->ops->init_request(set->driver_data,
1722 hctx->fq->flush_rq, hctx_idx,
1723 flush_start_tag + hctx_idx, node))
1724 goto free_fq;
1725
Ming Lei08e98fc2014-09-25 23:23:38 +08001726 return 0;
1727
Ming Leif70ced02014-09-25 23:23:47 +08001728 free_fq:
1729 kfree(hctx->fq);
1730 exit_hctx:
1731 if (set->ops->exit_hctx)
1732 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001733 free_bitmap:
1734 blk_mq_free_bitmap(&hctx->ctx_map);
1735 free_ctxs:
1736 kfree(hctx->ctxs);
1737 unregister_cpu_notifier:
1738 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1739
1740 return -1;
1741}
1742
Jens Axboe320ae512013-10-24 09:20:05 +01001743static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001744 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001745{
1746 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001747 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001748
1749 /*
1750 * Initialize hardware queues
1751 */
1752 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei08e98fc2014-09-25 23:23:38 +08001753 if (blk_mq_init_hctx(q, set, hctx, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001754 break;
1755 }
1756
1757 if (i == q->nr_hw_queues)
1758 return 0;
1759
1760 /*
1761 * Init failed
1762 */
Ming Lei624dbe42014-05-27 23:35:13 +08001763 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001764
1765 return 1;
1766}
1767
1768static void blk_mq_init_cpu_queues(struct request_queue *q,
1769 unsigned int nr_hw_queues)
1770{
1771 unsigned int i;
1772
1773 for_each_possible_cpu(i) {
1774 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1775 struct blk_mq_hw_ctx *hctx;
1776
1777 memset(__ctx, 0, sizeof(*__ctx));
1778 __ctx->cpu = i;
1779 spin_lock_init(&__ctx->lock);
1780 INIT_LIST_HEAD(&__ctx->rq_list);
1781 __ctx->queue = q;
1782
1783 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001784 if (!cpu_online(i))
1785 continue;
1786
Jens Axboee4043dc2014-04-09 10:18:23 -06001787 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001788
Jens Axboe320ae512013-10-24 09:20:05 +01001789 /*
1790 * Set local node, IFF we have more than one hw queue. If
1791 * not, we remain on the home node of the device
1792 */
1793 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1794 hctx->numa_node = cpu_to_node(i);
1795 }
1796}
1797
Akinobu Mita57783222015-09-27 02:09:23 +09001798static void blk_mq_map_swqueue(struct request_queue *q,
1799 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01001800{
1801 unsigned int i;
1802 struct blk_mq_hw_ctx *hctx;
1803 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08001804 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001805
Akinobu Mita60de0742015-09-27 02:09:25 +09001806 /*
1807 * Avoid others reading imcomplete hctx->cpumask through sysfs
1808 */
1809 mutex_lock(&q->sysfs_lock);
1810
Jens Axboe320ae512013-10-24 09:20:05 +01001811 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001812 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001813 hctx->nr_ctx = 0;
1814 }
1815
1816 /*
1817 * Map software to hardware queues
1818 */
1819 queue_for_each_ctx(q, ctx, i) {
1820 /* If the cpu isn't online, the cpu is mapped to first hctx */
Akinobu Mita57783222015-09-27 02:09:23 +09001821 if (!cpumask_test_cpu(i, online_mask))
Jens Axboee4043dc2014-04-09 10:18:23 -06001822 continue;
1823
Jens Axboe320ae512013-10-24 09:20:05 +01001824 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001825 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001826 ctx->index_hw = hctx->nr_ctx;
1827 hctx->ctxs[hctx->nr_ctx++] = ctx;
1828 }
Jens Axboe506e9312014-05-07 10:26:44 -06001829
Akinobu Mita60de0742015-09-27 02:09:25 +09001830 mutex_unlock(&q->sysfs_lock);
1831
Jens Axboe506e9312014-05-07 10:26:44 -06001832 queue_for_each_hw_ctx(q, hctx, i) {
Chong Yuan889fa312015-04-15 11:39:29 -06001833 struct blk_mq_ctxmap *map = &hctx->ctx_map;
1834
Jens Axboe484b4062014-05-21 14:01:15 -06001835 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001836 * If no software queues are mapped to this hardware queue,
1837 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06001838 */
1839 if (!hctx->nr_ctx) {
Jens Axboe484b4062014-05-21 14:01:15 -06001840 if (set->tags[i]) {
1841 blk_mq_free_rq_map(set, set->tags[i], i);
1842 set->tags[i] = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001843 }
Ming Lei2a34c082015-04-21 10:00:20 +08001844 hctx->tags = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001845 continue;
1846 }
1847
Ming Lei2a34c082015-04-21 10:00:20 +08001848 /* unmapped hw queue can be remapped after CPU topo changed */
1849 if (!set->tags[i])
1850 set->tags[i] = blk_mq_init_rq_map(set, i);
1851 hctx->tags = set->tags[i];
1852 WARN_ON(!hctx->tags);
1853
Jens Axboe484b4062014-05-21 14:01:15 -06001854 /*
Chong Yuan889fa312015-04-15 11:39:29 -06001855 * Set the map size to the number of mapped software queues.
1856 * This is more accurate and more efficient than looping
1857 * over all possibly mapped software queues.
1858 */
Jens Axboe569fd0c2015-04-17 08:28:50 -06001859 map->size = DIV_ROUND_UP(hctx->nr_ctx, map->bits_per_word);
Chong Yuan889fa312015-04-15 11:39:29 -06001860
1861 /*
Jens Axboe484b4062014-05-21 14:01:15 -06001862 * Initialize batch roundrobin counts
1863 */
Jens Axboe506e9312014-05-07 10:26:44 -06001864 hctx->next_cpu = cpumask_first(hctx->cpumask);
1865 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1866 }
Akinobu Mita1356aae2015-09-27 02:09:19 +09001867
1868 queue_for_each_ctx(q, ctx, i) {
Akinobu Mita57783222015-09-27 02:09:23 +09001869 if (!cpumask_test_cpu(i, online_mask))
Akinobu Mita1356aae2015-09-27 02:09:19 +09001870 continue;
1871
1872 hctx = q->mq_ops->map_queue(q, i);
1873 cpumask_set_cpu(i, hctx->tags->cpumask);
1874 }
Jens Axboe320ae512013-10-24 09:20:05 +01001875}
1876
Jens Axboe0d2602c2014-05-13 15:10:52 -06001877static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1878{
1879 struct blk_mq_hw_ctx *hctx;
1880 struct request_queue *q;
1881 bool shared;
1882 int i;
1883
1884 if (set->tag_list.next == set->tag_list.prev)
1885 shared = false;
1886 else
1887 shared = true;
1888
1889 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1890 blk_mq_freeze_queue(q);
1891
1892 queue_for_each_hw_ctx(q, hctx, i) {
1893 if (shared)
1894 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1895 else
1896 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1897 }
1898 blk_mq_unfreeze_queue(q);
1899 }
1900}
1901
1902static void blk_mq_del_queue_tag_set(struct request_queue *q)
1903{
1904 struct blk_mq_tag_set *set = q->tag_set;
1905
Jens Axboe0d2602c2014-05-13 15:10:52 -06001906 mutex_lock(&set->tag_list_lock);
1907 list_del_init(&q->tag_set_list);
1908 blk_mq_update_tag_set_depth(set);
1909 mutex_unlock(&set->tag_list_lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001910}
1911
1912static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1913 struct request_queue *q)
1914{
1915 q->tag_set = set;
1916
1917 mutex_lock(&set->tag_list_lock);
1918 list_add_tail(&q->tag_set_list, &set->tag_list);
1919 blk_mq_update_tag_set_depth(set);
1920 mutex_unlock(&set->tag_list_lock);
1921}
1922
Ming Leie09aae72015-01-29 20:17:27 +08001923/*
1924 * It is the actual release handler for mq, but we do it from
1925 * request queue's release handler for avoiding use-after-free
1926 * and headache because q->mq_kobj shouldn't have been introduced,
1927 * but we can't group ctx/kctx kobj without it.
1928 */
1929void blk_mq_release(struct request_queue *q)
1930{
1931 struct blk_mq_hw_ctx *hctx;
1932 unsigned int i;
1933
1934 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08001935 queue_for_each_hw_ctx(q, hctx, i) {
1936 if (!hctx)
1937 continue;
1938 kfree(hctx->ctxs);
Ming Leie09aae72015-01-29 20:17:27 +08001939 kfree(hctx);
Ming Leic3b4afc2015-06-04 22:25:04 +08001940 }
Ming Leie09aae72015-01-29 20:17:27 +08001941
Akinobu Mitaa723bab2015-09-27 02:09:21 +09001942 kfree(q->mq_map);
1943 q->mq_map = NULL;
1944
Ming Leie09aae72015-01-29 20:17:27 +08001945 kfree(q->queue_hw_ctx);
1946
1947 /* ctx kobj stays in queue_ctx */
1948 free_percpu(q->queue_ctx);
1949}
1950
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001951struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001952{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04001953 struct request_queue *uninit_q, *q;
1954
1955 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1956 if (!uninit_q)
1957 return ERR_PTR(-ENOMEM);
1958
1959 q = blk_mq_init_allocated_queue(set, uninit_q);
1960 if (IS_ERR(q))
1961 blk_cleanup_queue(uninit_q);
1962
1963 return q;
1964}
1965EXPORT_SYMBOL(blk_mq_init_queue);
1966
1967struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
1968 struct request_queue *q)
1969{
Jens Axboe320ae512013-10-24 09:20:05 +01001970 struct blk_mq_hw_ctx **hctxs;
Ming Leie6cdb092014-06-03 11:24:06 +08001971 struct blk_mq_ctx __percpu *ctx;
Jens Axboef14bbe72014-05-27 12:06:53 -06001972 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001973 int i;
1974
Jens Axboe320ae512013-10-24 09:20:05 +01001975 ctx = alloc_percpu(struct blk_mq_ctx);
1976 if (!ctx)
1977 return ERR_PTR(-ENOMEM);
1978
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001979 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1980 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001981
1982 if (!hctxs)
1983 goto err_percpu;
1984
Jens Axboef14bbe72014-05-27 12:06:53 -06001985 map = blk_mq_make_queue_map(set);
1986 if (!map)
1987 goto err_map;
1988
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001989 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001990 int node = blk_mq_hw_queue_to_node(map, i);
1991
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001992 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1993 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001994 if (!hctxs[i])
1995 goto err_hctxs;
1996
Jens Axboea86073e2014-10-13 15:41:54 -06001997 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
1998 node))
Jens Axboee4043dc2014-04-09 10:18:23 -06001999 goto err_hctxs;
2000
Jens Axboe0d2602c2014-05-13 15:10:52 -06002001 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06002002 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01002003 hctxs[i]->queue_num = i;
2004 }
2005
Tejun Heo17497ac2014-09-24 13:31:50 -04002006 /*
2007 * Init percpu_ref in atomic mode so that it's faster to shutdown.
2008 * See blk_register_queue() for details.
2009 */
Tejun Heoa34375e2014-09-08 09:51:30 +09002010 if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release,
Tejun Heo17497ac2014-09-24 13:31:50 -04002011 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002012 goto err_hctxs;
Ming Lei3d2936f2014-05-27 23:35:14 +08002013
Jens Axboe320ae512013-10-24 09:20:05 +01002014 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
Ming Leie56f6982015-07-16 19:53:22 +08002015 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002016
2017 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002018 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06002019 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01002020
2021 q->queue_ctx = ctx;
2022 q->queue_hw_ctx = hctxs;
2023
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002024 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07002025 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002026
Jens Axboe05f1dd52014-05-29 09:53:32 -06002027 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2028 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2029
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002030 q->sg_reserved_size = INT_MAX;
2031
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002032 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
2033 INIT_LIST_HEAD(&q->requeue_list);
2034 spin_lock_init(&q->requeue_lock);
2035
Jens Axboe07068d52014-05-22 10:40:51 -06002036 if (q->nr_hw_queues > 1)
2037 blk_queue_make_request(q, blk_mq_make_request);
2038 else
2039 blk_queue_make_request(q, blk_sq_make_request);
2040
Jens Axboeeba71762014-05-20 15:17:27 -06002041 /*
2042 * Do this after blk_queue_make_request() overrides it...
2043 */
2044 q->nr_requests = set->queue_depth;
2045
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002046 if (set->ops->complete)
2047 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08002048
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002049 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002050
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002051 if (blk_mq_init_hw_queues(q, set))
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002052 goto err_hctxs;
Christoph Hellwig18741982014-02-10 09:29:00 -07002053
Akinobu Mita57783222015-09-27 02:09:23 +09002054 get_online_cpus();
Jens Axboe320ae512013-10-24 09:20:05 +01002055 mutex_lock(&all_q_mutex);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002056
Jens Axboe320ae512013-10-24 09:20:05 +01002057 list_add_tail(&q->all_q_node, &all_q_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002058 blk_mq_add_queue_tag_set(set, q);
Akinobu Mita57783222015-09-27 02:09:23 +09002059 blk_mq_map_swqueue(q, cpu_online_mask);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002060
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002061 mutex_unlock(&all_q_mutex);
Akinobu Mita57783222015-09-27 02:09:23 +09002062 put_online_cpus();
Jens Axboe484b4062014-05-21 14:01:15 -06002063
Jens Axboe320ae512013-10-24 09:20:05 +01002064 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002065
Jens Axboe320ae512013-10-24 09:20:05 +01002066err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06002067 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002068 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01002069 if (!hctxs[i])
2070 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06002071 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02002072 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01002073 }
Jens Axboef14bbe72014-05-27 12:06:53 -06002074err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01002075 kfree(hctxs);
2076err_percpu:
2077 free_percpu(ctx);
2078 return ERR_PTR(-ENOMEM);
2079}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002080EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002081
2082void blk_mq_free_queue(struct request_queue *q)
2083{
Ming Lei624dbe42014-05-27 23:35:13 +08002084 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002085
Akinobu Mita0e626362015-09-27 02:09:22 +09002086 mutex_lock(&all_q_mutex);
2087 list_del_init(&q->all_q_node);
2088 mutex_unlock(&all_q_mutex);
2089
Jens Axboe0d2602c2014-05-13 15:10:52 -06002090 blk_mq_del_queue_tag_set(q);
2091
Ming Lei624dbe42014-05-27 23:35:13 +08002092 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2093 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01002094
Tejun Heoadd703f2014-07-01 10:34:38 -06002095 percpu_ref_exit(&q->mq_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +01002096}
Jens Axboe320ae512013-10-24 09:20:05 +01002097
2098/* Basically redo blk_mq_init_queue with queue frozen */
Akinobu Mita57783222015-09-27 02:09:23 +09002099static void blk_mq_queue_reinit(struct request_queue *q,
2100 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01002101{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +02002102 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
Jens Axboe320ae512013-10-24 09:20:05 +01002103
Jens Axboe67aec142014-05-30 08:25:36 -06002104 blk_mq_sysfs_unregister(q);
2105
Akinobu Mita57783222015-09-27 02:09:23 +09002106 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues, online_mask);
Jens Axboe320ae512013-10-24 09:20:05 +01002107
2108 /*
2109 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2110 * we should change hctx numa_node according to new topology (this
2111 * involves free and re-allocate memory, worthy doing?)
2112 */
2113
Akinobu Mita57783222015-09-27 02:09:23 +09002114 blk_mq_map_swqueue(q, online_mask);
Jens Axboe320ae512013-10-24 09:20:05 +01002115
Jens Axboe67aec142014-05-30 08:25:36 -06002116 blk_mq_sysfs_register(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002117}
2118
Paul Gortmakerf618ef72013-11-14 08:26:02 -07002119static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
2120 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01002121{
2122 struct request_queue *q;
Akinobu Mita57783222015-09-27 02:09:23 +09002123 int cpu = (unsigned long)hcpu;
2124 /*
2125 * New online cpumask which is going to be set in this hotplug event.
2126 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2127 * one-by-one and dynamically allocating this could result in a failure.
2128 */
2129 static struct cpumask online_new;
Jens Axboe320ae512013-10-24 09:20:05 +01002130
2131 /*
Akinobu Mita57783222015-09-27 02:09:23 +09002132 * Before hotadded cpu starts handling requests, new mappings must
2133 * be established. Otherwise, these requests in hw queue might
2134 * never be dispatched.
2135 *
2136 * For example, there is a single hw queue (hctx) and two CPU queues
2137 * (ctx0 for CPU0, and ctx1 for CPU1).
2138 *
2139 * Now CPU1 is just onlined and a request is inserted into
2140 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2141 * still zero.
2142 *
2143 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2144 * set in pending bitmap and tries to retrieve requests in
2145 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2146 * so the request in ctx1->rq_list is ignored.
Jens Axboe320ae512013-10-24 09:20:05 +01002147 */
Akinobu Mita57783222015-09-27 02:09:23 +09002148 switch (action & ~CPU_TASKS_FROZEN) {
2149 case CPU_DEAD:
2150 case CPU_UP_CANCELED:
2151 cpumask_copy(&online_new, cpu_online_mask);
2152 break;
2153 case CPU_UP_PREPARE:
2154 cpumask_copy(&online_new, cpu_online_mask);
2155 cpumask_set_cpu(cpu, &online_new);
2156 break;
2157 default:
Jens Axboe320ae512013-10-24 09:20:05 +01002158 return NOTIFY_OK;
Akinobu Mita57783222015-09-27 02:09:23 +09002159 }
Jens Axboe320ae512013-10-24 09:20:05 +01002160
2161 mutex_lock(&all_q_mutex);
Tejun Heof3af0202014-11-04 13:52:27 -05002162
2163 /*
2164 * We need to freeze and reinit all existing queues. Freezing
2165 * involves synchronous wait for an RCU grace period and doing it
2166 * one by one may take a long time. Start freezing all queues in
2167 * one swoop and then wait for the completions so that freezing can
2168 * take place in parallel.
2169 */
2170 list_for_each_entry(q, &all_q_list, all_q_node)
2171 blk_mq_freeze_queue_start(q);
Ming Leif054b562015-04-21 10:00:19 +08002172 list_for_each_entry(q, &all_q_list, all_q_node) {
Tejun Heof3af0202014-11-04 13:52:27 -05002173 blk_mq_freeze_queue_wait(q);
2174
Ming Leif054b562015-04-21 10:00:19 +08002175 /*
2176 * timeout handler can't touch hw queue during the
2177 * reinitialization
2178 */
2179 del_timer_sync(&q->timeout);
2180 }
2181
Jens Axboe320ae512013-10-24 09:20:05 +01002182 list_for_each_entry(q, &all_q_list, all_q_node)
Akinobu Mita57783222015-09-27 02:09:23 +09002183 blk_mq_queue_reinit(q, &online_new);
Tejun Heof3af0202014-11-04 13:52:27 -05002184
2185 list_for_each_entry(q, &all_q_list, all_q_node)
2186 blk_mq_unfreeze_queue(q);
2187
Jens Axboe320ae512013-10-24 09:20:05 +01002188 mutex_unlock(&all_q_mutex);
2189 return NOTIFY_OK;
2190}
2191
Jens Axboea5164402014-09-10 09:02:03 -06002192static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2193{
2194 int i;
2195
2196 for (i = 0; i < set->nr_hw_queues; i++) {
2197 set->tags[i] = blk_mq_init_rq_map(set, i);
2198 if (!set->tags[i])
2199 goto out_unwind;
2200 }
2201
2202 return 0;
2203
2204out_unwind:
2205 while (--i >= 0)
2206 blk_mq_free_rq_map(set, set->tags[i], i);
2207
Jens Axboea5164402014-09-10 09:02:03 -06002208 return -ENOMEM;
2209}
2210
2211/*
2212 * Allocate the request maps associated with this tag_set. Note that this
2213 * may reduce the depth asked for, if memory is tight. set->queue_depth
2214 * will be updated to reflect the allocated depth.
2215 */
2216static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2217{
2218 unsigned int depth;
2219 int err;
2220
2221 depth = set->queue_depth;
2222 do {
2223 err = __blk_mq_alloc_rq_maps(set);
2224 if (!err)
2225 break;
2226
2227 set->queue_depth >>= 1;
2228 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2229 err = -ENOMEM;
2230 break;
2231 }
2232 } while (set->queue_depth);
2233
2234 if (!set->queue_depth || err) {
2235 pr_err("blk-mq: failed to allocate request map\n");
2236 return -ENOMEM;
2237 }
2238
2239 if (depth != set->queue_depth)
2240 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2241 depth, set->queue_depth);
2242
2243 return 0;
2244}
2245
Keith Buschf26cdc82015-06-01 09:29:53 -06002246struct cpumask *blk_mq_tags_cpumask(struct blk_mq_tags *tags)
2247{
2248 return tags->cpumask;
2249}
2250EXPORT_SYMBOL_GPL(blk_mq_tags_cpumask);
2251
Jens Axboea4391c62014-06-05 15:21:56 -06002252/*
2253 * Alloc a tag set to be associated with one or more request queues.
2254 * May fail with EINVAL for various error conditions. May adjust the
2255 * requested depth down, if if it too large. In that case, the set
2256 * value will be stored in set->queue_depth.
2257 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002258int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2259{
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002260 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2261
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002262 if (!set->nr_hw_queues)
2263 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002264 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002265 return -EINVAL;
2266 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2267 return -EINVAL;
2268
Xiaoguang Wangf9018ac2015-03-30 13:19:14 +08002269 if (!set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002270 return -EINVAL;
2271
Jens Axboea4391c62014-06-05 15:21:56 -06002272 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2273 pr_info("blk-mq: reduced tag depth to %u\n",
2274 BLK_MQ_MAX_DEPTH);
2275 set->queue_depth = BLK_MQ_MAX_DEPTH;
2276 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002277
Shaohua Li6637fad2014-11-30 16:00:58 -08002278 /*
2279 * If a crashdump is active, then we are potentially in a very
2280 * memory constrained environment. Limit us to 1 queue and
2281 * 64 tags to prevent using too much memory.
2282 */
2283 if (is_kdump_kernel()) {
2284 set->nr_hw_queues = 1;
2285 set->queue_depth = min(64U, set->queue_depth);
2286 }
2287
Ming Lei48479002014-04-19 18:00:17 +08002288 set->tags = kmalloc_node(set->nr_hw_queues *
2289 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002290 GFP_KERNEL, set->numa_node);
2291 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002292 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002293
Jens Axboea5164402014-09-10 09:02:03 -06002294 if (blk_mq_alloc_rq_maps(set))
2295 goto enomem;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002296
Jens Axboe0d2602c2014-05-13 15:10:52 -06002297 mutex_init(&set->tag_list_lock);
2298 INIT_LIST_HEAD(&set->tag_list);
2299
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002300 return 0;
Jens Axboea5164402014-09-10 09:02:03 -06002301enomem:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002302 kfree(set->tags);
2303 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002304 return -ENOMEM;
2305}
2306EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2307
2308void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2309{
2310 int i;
2311
Jens Axboe484b4062014-05-21 14:01:15 -06002312 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Buschf26cdc82015-06-01 09:29:53 -06002313 if (set->tags[i]) {
Jens Axboe484b4062014-05-21 14:01:15 -06002314 blk_mq_free_rq_map(set, set->tags[i], i);
Keith Buschf26cdc82015-06-01 09:29:53 -06002315 free_cpumask_var(set->tags[i]->cpumask);
2316 }
Jens Axboe484b4062014-05-21 14:01:15 -06002317 }
2318
Ming Lei981bd182014-04-24 00:07:34 +08002319 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002320 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002321}
2322EXPORT_SYMBOL(blk_mq_free_tag_set);
2323
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002324int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2325{
2326 struct blk_mq_tag_set *set = q->tag_set;
2327 struct blk_mq_hw_ctx *hctx;
2328 int i, ret;
2329
2330 if (!set || nr > set->queue_depth)
2331 return -EINVAL;
2332
2333 ret = 0;
2334 queue_for_each_hw_ctx(q, hctx, i) {
2335 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2336 if (ret)
2337 break;
2338 }
2339
2340 if (!ret)
2341 q->nr_requests = nr;
2342
2343 return ret;
2344}
2345
Jens Axboe676141e2014-03-20 13:29:18 -06002346void blk_mq_disable_hotplug(void)
2347{
2348 mutex_lock(&all_q_mutex);
2349}
2350
2351void blk_mq_enable_hotplug(void)
2352{
2353 mutex_unlock(&all_q_mutex);
2354}
2355
Jens Axboe320ae512013-10-24 09:20:05 +01002356static int __init blk_mq_init(void)
2357{
Jens Axboe320ae512013-10-24 09:20:05 +01002358 blk_mq_cpu_init();
2359
Tejun Heoadd703f2014-07-01 10:34:38 -06002360 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002361
2362 return 0;
2363}
2364subsys_initcall(blk_mq_init);