blob: 75fc33f342516c883231ca16b3c2129ff386a624 [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>
12#include <linux/mm.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/workqueue.h>
16#include <linux/smp.h>
17#include <linux/llist.h>
18#include <linux/list_sort.h>
19#include <linux/cpu.h>
20#include <linux/cache.h>
21#include <linux/sched/sysctl.h>
22#include <linux/delay.h>
23
24#include <trace/events/block.h>
25
26#include <linux/blk-mq.h>
27#include "blk.h"
28#include "blk-mq.h"
29#include "blk-mq-tag.h"
30
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
34static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
35
Jens Axboe320ae512013-10-24 09:20:05 +010036/*
37 * Check if any of the ctx's have pending work in this hardware queue
38 */
39static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
40{
41 unsigned int i;
42
Jens Axboe1429d7c2014-05-19 09:23:55 -060043 for (i = 0; i < hctx->ctx_map.map_size; i++)
44 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010045 return true;
46
47 return false;
48}
49
Jens Axboe1429d7c2014-05-19 09:23:55 -060050static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
51 struct blk_mq_ctx *ctx)
52{
53 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
54}
55
56#define CTX_TO_BIT(hctx, ctx) \
57 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
58
Jens Axboe320ae512013-10-24 09:20:05 +010059/*
60 * Mark this ctx as having pending work in this hardware queue
61 */
62static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
63 struct blk_mq_ctx *ctx)
64{
Jens Axboe1429d7c2014-05-19 09:23:55 -060065 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
66
67 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
68 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
69}
70
71static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
72 struct blk_mq_ctx *ctx)
73{
74 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
75
76 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010077}
78
Jens Axboe320ae512013-10-24 09:20:05 +010079static int blk_mq_queue_enter(struct request_queue *q)
80{
81 int ret;
82
83 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
84 smp_wmb();
Keith Busch3b632cf2014-06-06 10:22:07 -060085
86 /* we have problems freezing the queue if it's initializing */
87 if (!blk_queue_dying(q) &&
88 (!blk_queue_bypass(q) || !blk_queue_init_done(q)))
Jens Axboe320ae512013-10-24 09:20:05 +010089 return 0;
90
91 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
92
93 spin_lock_irq(q->queue_lock);
94 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +080095 !blk_queue_bypass(q) || blk_queue_dying(q),
96 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +010097 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +080098 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +010099 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800100 else if (blk_queue_dying(q))
101 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100102 spin_unlock_irq(q->queue_lock);
103
104 return ret;
105}
106
107static void blk_mq_queue_exit(struct request_queue *q)
108{
109 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
110}
111
Ming Lei43a5e4e2013-12-26 21:31:35 +0800112static void __blk_mq_drain_queue(struct request_queue *q)
113{
114 while (true) {
115 s64 count;
116
117 spin_lock_irq(q->queue_lock);
118 count = percpu_counter_sum(&q->mq_usage_counter);
119 spin_unlock_irq(q->queue_lock);
120
121 if (count == 0)
122 break;
123 blk_mq_run_queues(q, false);
124 msleep(10);
125 }
126}
127
Jens Axboe320ae512013-10-24 09:20:05 +0100128/*
129 * Guarantee no request is in use, so we can change any data structure of
130 * the queue afterward.
131 */
132static void blk_mq_freeze_queue(struct request_queue *q)
133{
134 bool drain;
135
136 spin_lock_irq(q->queue_lock);
137 drain = !q->bypass_depth++;
138 queue_flag_set(QUEUE_FLAG_BYPASS, q);
139 spin_unlock_irq(q->queue_lock);
140
Ming Lei43a5e4e2013-12-26 21:31:35 +0800141 if (drain)
142 __blk_mq_drain_queue(q);
143}
Jens Axboe320ae512013-10-24 09:20:05 +0100144
Ming Lei43a5e4e2013-12-26 21:31:35 +0800145void blk_mq_drain_queue(struct request_queue *q)
146{
147 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100148}
149
150static void blk_mq_unfreeze_queue(struct request_queue *q)
151{
152 bool wake = false;
153
154 spin_lock_irq(q->queue_lock);
155 if (!--q->bypass_depth) {
156 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
157 wake = true;
158 }
159 WARN_ON_ONCE(q->bypass_depth < 0);
160 spin_unlock_irq(q->queue_lock);
161 if (wake)
162 wake_up_all(&q->mq_freeze_wq);
163}
164
165bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
166{
167 return blk_mq_has_free_tags(hctx->tags);
168}
169EXPORT_SYMBOL(blk_mq_can_queue);
170
Jens Axboe94eddfb2013-11-19 09:25:07 -0700171static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
172 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100173{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700174 if (blk_queue_io_stat(q))
175 rw_flags |= REQ_IO_STAT;
176
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200177 INIT_LIST_HEAD(&rq->queuelist);
178 /* csd/requeue_work/fifo_time is initialized before use */
179 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100180 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600181 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200182 /* do not touch atomic flags, it needs atomic ops against the timer */
183 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200184 INIT_HLIST_NODE(&rq->hash);
185 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200186 rq->rq_disk = NULL;
187 rq->part = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200188#ifdef CONFIG_BLK_CGROUP
189 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700190 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200191 rq->io_start_time_ns = 0;
192#endif
193 rq->nr_phys_segments = 0;
194#if defined(CONFIG_BLK_DEV_INTEGRITY)
195 rq->nr_integrity_segments = 0;
196#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200197 rq->special = NULL;
198 /* tag was already set */
199 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200200
201 rq->extra_len = 0;
202 rq->sense_len = 0;
203 rq->resid_len = 0;
204 rq->sense = NULL;
205
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200206 INIT_LIST_HEAD(&rq->timeout_list);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200207 rq->end_io = NULL;
208 rq->end_io_data = NULL;
209 rq->next_rq = NULL;
210
Jens Axboe320ae512013-10-24 09:20:05 +0100211 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
212}
213
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200214static struct request *
Ming Leicb96a42c2014-06-01 00:43:37 +0800215__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200216{
217 struct request *rq;
218 unsigned int tag;
219
Ming Leicb96a42c2014-06-01 00:43:37 +0800220 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200221 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a42c2014-06-01 00:43:37 +0800222 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200223
224 rq->cmd_flags = 0;
Ming Leicb96a42c2014-06-01 00:43:37 +0800225 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200226 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a42c2014-06-01 00:43:37 +0800227 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200228 }
229
230 rq->tag = tag;
Ming Leicb96a42c2014-06-01 00:43:37 +0800231 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200232 return rq;
233 }
234
235 return NULL;
236}
237
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200238struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
239 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100240{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200241 struct blk_mq_ctx *ctx;
242 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100243 struct request *rq;
Ming Leicb96a42c2014-06-01 00:43:37 +0800244 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +0100245
246 if (blk_mq_queue_enter(q))
247 return NULL;
248
Christoph Hellwigd8525642014-05-27 20:59:50 +0200249 ctx = blk_mq_get_ctx(q);
250 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +0800251 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
252 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200253
Ming Leicb96a42c2014-06-01 00:43:37 +0800254 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200255 if (!rq && (gfp & __GFP_WAIT)) {
256 __blk_mq_run_hw_queue(hctx);
257 blk_mq_put_ctx(ctx);
258
259 ctx = blk_mq_get_ctx(q);
260 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +0800261 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
262 hctx);
263 rq = __blk_mq_alloc_request(&alloc_data, rw);
264 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200265 }
266 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100267 return rq;
268}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600269EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100270
Jens Axboe320ae512013-10-24 09:20:05 +0100271static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
272 struct blk_mq_ctx *ctx, struct request *rq)
273{
274 const int tag = rq->tag;
275 struct request_queue *q = rq->q;
276
Jens Axboe0d2602c2014-05-13 15:10:52 -0600277 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
278 atomic_dec(&hctx->nr_active);
279
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200280 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600281 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100282 blk_mq_queue_exit(q);
283}
284
285void blk_mq_free_request(struct request *rq)
286{
287 struct blk_mq_ctx *ctx = rq->mq_ctx;
288 struct blk_mq_hw_ctx *hctx;
289 struct request_queue *q = rq->q;
290
291 ctx->rq_completed[rq_is_sync(rq)]++;
292
293 hctx = q->mq_ops->map_queue(q, ctx->cpu);
294 __blk_mq_free_request(hctx, ctx, rq);
295}
296
Christoph Hellwig8727af42014-04-14 10:30:08 +0200297/*
298 * Clone all relevant state from a request that has been put on hold in
299 * the flush state machine into the preallocated flush request that hangs
300 * off the request queue.
301 *
302 * For a driver the flush request should be invisible, that's why we are
303 * impersonating the original request here.
304 */
305void blk_mq_clone_flush_request(struct request *flush_rq,
306 struct request *orig_rq)
307{
308 struct blk_mq_hw_ctx *hctx =
309 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
310
311 flush_rq->mq_ctx = orig_rq->mq_ctx;
312 flush_rq->tag = orig_rq->tag;
313 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
314 hctx->cmd_size);
315}
316
Christoph Hellwig63151a42014-04-16 09:44:52 +0200317inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100318{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700319 blk_account_io_done(rq);
320
Christoph Hellwig91b63632014-04-16 09:44:53 +0200321 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100322 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200323 } else {
324 if (unlikely(blk_bidi_rq(rq)))
325 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100326 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200327 }
Jens Axboe320ae512013-10-24 09:20:05 +0100328}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200329EXPORT_SYMBOL(__blk_mq_end_io);
330
331void blk_mq_end_io(struct request *rq, int error)
332{
333 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
334 BUG();
335 __blk_mq_end_io(rq, error);
336}
337EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100338
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800339static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100340{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800341 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100342
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800343 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100344}
345
Jens Axboeed851862014-05-30 21:20:50 -0600346static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100347{
348 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700349 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100350 int cpu;
351
Christoph Hellwig38535202014-04-25 02:32:53 -0700352 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800353 rq->q->softirq_done_fn(rq);
354 return;
355 }
Jens Axboe320ae512013-10-24 09:20:05 +0100356
357 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700358 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
359 shared = cpus_share_cache(cpu, ctx->cpu);
360
361 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800362 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800363 rq->csd.info = rq;
364 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100365 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800366 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800367 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800368 }
Jens Axboe320ae512013-10-24 09:20:05 +0100369 put_cpu();
370}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800371
Jens Axboeed851862014-05-30 21:20:50 -0600372void __blk_mq_complete_request(struct request *rq)
373{
374 struct request_queue *q = rq->q;
375
376 if (!q->softirq_done_fn)
377 blk_mq_end_io(rq, rq->errors);
378 else
379 blk_mq_ipi_complete_request(rq);
380}
381
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800382/**
383 * blk_mq_complete_request - end I/O on a request
384 * @rq: the request being processed
385 *
386 * Description:
387 * Ends all I/O on a request. It does not handle partial completions.
388 * The actual completion happens out-of-order, through a IPI handler.
389 **/
390void blk_mq_complete_request(struct request *rq)
391{
Jens Axboe95f09682014-05-27 17:46:48 -0600392 struct request_queue *q = rq->q;
393
394 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800395 return;
Jens Axboeed851862014-05-30 21:20:50 -0600396 if (!blk_mark_rq_complete(rq))
397 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800398}
399EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100400
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800401static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100402{
403 struct request_queue *q = rq->q;
404
405 trace_block_rq_issue(q, rq);
406
Christoph Hellwig742ee692014-04-14 10:30:06 +0200407 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200408 if (unlikely(blk_bidi_rq(rq)))
409 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200410
Jens Axboe320ae512013-10-24 09:20:05 +0100411 /*
412 * Just mark start time and set the started bit. Due to memory
413 * ordering, we know we'll see the correct deadline as long as
Jens Axboec22d9d82014-05-23 14:14:57 -0600414 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
415 * unless one has been set in the request.
Jens Axboe320ae512013-10-24 09:20:05 +0100416 */
Jens Axboec22d9d82014-05-23 14:14:57 -0600417 if (!rq->timeout)
418 rq->deadline = jiffies + q->rq_timeout;
419 else
420 rq->deadline = jiffies + rq->timeout;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600421
422 /*
423 * Mark us as started and clear complete. Complete might have been
424 * set if requeue raced with timeout, which then marked it as
425 * complete. So be sure to clear complete again when we start
426 * the request, otherwise we'll ignore the completion event.
427 */
Jens Axboe4b570522014-05-29 11:00:11 -0600428 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
429 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
430 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
431 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800432
433 if (q->dma_drain_size && blk_rq_bytes(rq)) {
434 /*
435 * Make sure space for the drain appears. We know we can do
436 * this because max_hw_segments has been adjusted to be one
437 * fewer than the device can handle.
438 */
439 rq->nr_phys_segments++;
440 }
441
442 /*
443 * Flag the last request in the series so that drivers know when IO
444 * should be kicked off, if they don't do it on a per-request basis.
445 *
446 * Note: the flag isn't the only condition drivers should do kick off.
447 * If drive is busy, the last request might not have the bit set.
448 */
449 if (last)
450 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100451}
452
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200453static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100454{
455 struct request_queue *q = rq->q;
456
457 trace_block_rq_requeue(q, rq);
458 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800459
460 rq->cmd_flags &= ~REQ_END;
461
462 if (q->dma_drain_size && blk_rq_bytes(rq))
463 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100464}
465
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200466void blk_mq_requeue_request(struct request *rq)
467{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200468 __blk_mq_requeue_request(rq);
469 blk_clear_rq_complete(rq);
470
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200471 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600472 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200473}
474EXPORT_SYMBOL(blk_mq_requeue_request);
475
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600476static void blk_mq_requeue_work(struct work_struct *work)
477{
478 struct request_queue *q =
479 container_of(work, struct request_queue, requeue_work);
480 LIST_HEAD(rq_list);
481 struct request *rq, *next;
482 unsigned long flags;
483
484 spin_lock_irqsave(&q->requeue_lock, flags);
485 list_splice_init(&q->requeue_list, &rq_list);
486 spin_unlock_irqrestore(&q->requeue_lock, flags);
487
488 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
489 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
490 continue;
491
492 rq->cmd_flags &= ~REQ_SOFTBARRIER;
493 list_del_init(&rq->queuelist);
494 blk_mq_insert_request(rq, true, false, false);
495 }
496
497 while (!list_empty(&rq_list)) {
498 rq = list_entry(rq_list.next, struct request, queuelist);
499 list_del_init(&rq->queuelist);
500 blk_mq_insert_request(rq, false, false, false);
501 }
502
503 blk_mq_run_queues(q, false);
504}
505
506void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
507{
508 struct request_queue *q = rq->q;
509 unsigned long flags;
510
511 /*
512 * We abuse this flag that is otherwise used by the I/O scheduler to
513 * request head insertation from the workqueue.
514 */
515 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
516
517 spin_lock_irqsave(&q->requeue_lock, flags);
518 if (at_head) {
519 rq->cmd_flags |= REQ_SOFTBARRIER;
520 list_add(&rq->queuelist, &q->requeue_list);
521 } else {
522 list_add_tail(&rq->queuelist, &q->requeue_list);
523 }
524 spin_unlock_irqrestore(&q->requeue_lock, flags);
525}
526EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
527
528void blk_mq_kick_requeue_list(struct request_queue *q)
529{
530 kblockd_schedule_work(&q->requeue_work);
531}
532EXPORT_SYMBOL(blk_mq_kick_requeue_list);
533
Jens Axboe0e62f512014-06-04 10:23:49 -0600534static inline bool is_flush_request(struct request *rq, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600535{
Jens Axboe0e62f512014-06-04 10:23:49 -0600536 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
537 rq->q->flush_rq->tag == tag);
538}
Shaohua Li22302372014-05-30 08:06:42 -0600539
Jens Axboe0e62f512014-06-04 10:23:49 -0600540struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
541{
542 struct request *rq = tags->rqs[tag];
Shaohua Li22302372014-05-30 08:06:42 -0600543
Jens Axboe0e62f512014-06-04 10:23:49 -0600544 if (!is_flush_request(rq, tag))
545 return rq;
546
547 return rq->q->flush_rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600548}
549EXPORT_SYMBOL(blk_mq_tag_to_rq);
550
Jens Axboe320ae512013-10-24 09:20:05 +0100551struct blk_mq_timeout_data {
552 struct blk_mq_hw_ctx *hctx;
553 unsigned long *next;
554 unsigned int *next_set;
555};
556
557static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
558{
559 struct blk_mq_timeout_data *data = __data;
560 struct blk_mq_hw_ctx *hctx = data->hctx;
561 unsigned int tag;
562
563 /* It may not be in flight yet (this is where
564 * the REQ_ATOMIC_STARTED flag comes in). The requests are
565 * statically allocated, so we know it's always safe to access the
566 * memory associated with a bit offset into ->rqs[].
567 */
568 tag = 0;
569 do {
570 struct request *rq;
571
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600572 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
573 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100574 break;
575
Jens Axboe0e62f512014-06-04 10:23:49 -0600576 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600577 if (rq->q != hctx->queue)
578 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100579 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
580 continue;
581
582 blk_rq_check_expired(rq, data->next, data->next_set);
583 } while (1);
584}
585
586static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
587 unsigned long *next,
588 unsigned int *next_set)
589{
590 struct blk_mq_timeout_data data = {
591 .hctx = hctx,
592 .next = next,
593 .next_set = next_set,
594 };
595
596 /*
597 * Ask the tagging code to iterate busy requests, so we can
598 * check them for timeout.
599 */
600 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
601}
602
Jens Axboe87ee7b12014-04-24 08:51:47 -0600603static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
604{
605 struct request_queue *q = rq->q;
606
607 /*
608 * We know that complete is set at this point. If STARTED isn't set
609 * anymore, then the request isn't active and the "timeout" should
610 * just be ignored. This can happen due to the bitflag ordering.
611 * Timeout first checks if STARTED is set, and if it is, assumes
612 * the request is active. But if we race with completion, then
613 * we both flags will get cleared. So check here again, and ignore
614 * a timeout event with a request that isn't active.
615 */
616 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
617 return BLK_EH_NOT_HANDLED;
618
619 if (!q->mq_ops->timeout)
620 return BLK_EH_RESET_TIMER;
621
622 return q->mq_ops->timeout(rq);
623}
624
Jens Axboe320ae512013-10-24 09:20:05 +0100625static void blk_mq_rq_timer(unsigned long data)
626{
627 struct request_queue *q = (struct request_queue *) data;
628 struct blk_mq_hw_ctx *hctx;
629 unsigned long next = 0;
630 int i, next_set = 0;
631
Jens Axboe484b4062014-05-21 14:01:15 -0600632 queue_for_each_hw_ctx(q, hctx, i) {
633 /*
634 * If not software queues are currently mapped to this
635 * hardware queue, there's nothing to check
636 */
637 if (!hctx->nr_ctx || !hctx->tags)
638 continue;
639
Jens Axboe320ae512013-10-24 09:20:05 +0100640 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
Jens Axboe484b4062014-05-21 14:01:15 -0600641 }
Jens Axboe320ae512013-10-24 09:20:05 +0100642
Jens Axboe0d2602c2014-05-13 15:10:52 -0600643 if (next_set) {
644 next = blk_rq_timeout(round_jiffies_up(next));
645 mod_timer(&q->timeout, next);
646 } else {
647 queue_for_each_hw_ctx(q, hctx, i)
648 blk_mq_tag_idle(hctx);
649 }
Jens Axboe320ae512013-10-24 09:20:05 +0100650}
651
652/*
653 * Reverse check our software queue for entries that we could potentially
654 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
655 * too much time checking for merges.
656 */
657static bool blk_mq_attempt_merge(struct request_queue *q,
658 struct blk_mq_ctx *ctx, struct bio *bio)
659{
660 struct request *rq;
661 int checked = 8;
662
663 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
664 int el_ret;
665
666 if (!checked--)
667 break;
668
669 if (!blk_rq_merge_ok(rq, bio))
670 continue;
671
672 el_ret = blk_try_merge(rq, bio);
673 if (el_ret == ELEVATOR_BACK_MERGE) {
674 if (bio_attempt_back_merge(q, rq, bio)) {
675 ctx->rq_merged++;
676 return true;
677 }
678 break;
679 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
680 if (bio_attempt_front_merge(q, rq, bio)) {
681 ctx->rq_merged++;
682 return true;
683 }
684 break;
685 }
686 }
687
688 return false;
689}
690
Jens Axboe320ae512013-10-24 09:20:05 +0100691/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600692 * Process software queues that have been marked busy, splicing them
693 * to the for-dispatch
694 */
695static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
696{
697 struct blk_mq_ctx *ctx;
698 int i;
699
700 for (i = 0; i < hctx->ctx_map.map_size; i++) {
701 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
702 unsigned int off, bit;
703
704 if (!bm->word)
705 continue;
706
707 bit = 0;
708 off = i * hctx->ctx_map.bits_per_word;
709 do {
710 bit = find_next_bit(&bm->word, bm->depth, bit);
711 if (bit >= bm->depth)
712 break;
713
714 ctx = hctx->ctxs[bit + off];
715 clear_bit(bit, &bm->word);
716 spin_lock(&ctx->lock);
717 list_splice_tail_init(&ctx->rq_list, list);
718 spin_unlock(&ctx->lock);
719
720 bit++;
721 } while (1);
722 }
723}
724
725/*
Jens Axboe320ae512013-10-24 09:20:05 +0100726 * Run this hardware queue, pulling any software queues mapped to it in.
727 * Note that this function currently has various problems around ordering
728 * of IO. In particular, we'd like FIFO behaviour on handling existing
729 * items on the hctx->dispatch list. Ignore that for now.
730 */
731static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
732{
733 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100734 struct request *rq;
735 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600736 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100737
Jens Axboefd1270d2014-04-16 09:23:48 -0600738 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600739
Jens Axboe5d12f902014-03-19 15:25:02 -0600740 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100741 return;
742
743 hctx->run++;
744
745 /*
746 * Touch any software queue that has pending entries.
747 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600748 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100749
750 /*
751 * If we have previous entries on our dispatch list, grab them
752 * and stuff them at the front for more fair dispatch.
753 */
754 if (!list_empty_careful(&hctx->dispatch)) {
755 spin_lock(&hctx->lock);
756 if (!list_empty(&hctx->dispatch))
757 list_splice_init(&hctx->dispatch, &rq_list);
758 spin_unlock(&hctx->lock);
759 }
760
761 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100762 * Now process all the entries, sending them to the driver.
763 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600764 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100765 while (!list_empty(&rq_list)) {
766 int ret;
767
768 rq = list_first_entry(&rq_list, struct request, queuelist);
769 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100770
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800771 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100772
773 ret = q->mq_ops->queue_rq(hctx, rq);
774 switch (ret) {
775 case BLK_MQ_RQ_QUEUE_OK:
776 queued++;
777 continue;
778 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100779 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200780 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100781 break;
782 default:
783 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100784 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800785 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100786 blk_mq_end_io(rq, rq->errors);
787 break;
788 }
789
790 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
791 break;
792 }
793
794 if (!queued)
795 hctx->dispatched[0]++;
796 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
797 hctx->dispatched[ilog2(queued) + 1]++;
798
799 /*
800 * Any items that need requeuing? Stuff them into hctx->dispatch,
801 * that is where we will continue on next queue run.
802 */
803 if (!list_empty(&rq_list)) {
804 spin_lock(&hctx->lock);
805 list_splice(&rq_list, &hctx->dispatch);
806 spin_unlock(&hctx->lock);
807 }
808}
809
Jens Axboe506e9312014-05-07 10:26:44 -0600810/*
811 * It'd be great if the workqueue API had a way to pass
812 * in a mask and had some smarts for more clever placement.
813 * For now we just round-robin here, switching for every
814 * BLK_MQ_CPU_WORK_BATCH queued items.
815 */
816static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
817{
818 int cpu = hctx->next_cpu;
819
820 if (--hctx->next_cpu_batch <= 0) {
821 int next_cpu;
822
823 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
824 if (next_cpu >= nr_cpu_ids)
825 next_cpu = cpumask_first(hctx->cpumask);
826
827 hctx->next_cpu = next_cpu;
828 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
829 }
830
831 return cpu;
832}
833
Jens Axboe320ae512013-10-24 09:20:05 +0100834void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
835{
Jens Axboe5d12f902014-03-19 15:25:02 -0600836 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100837 return;
838
Jens Axboee4043dc2014-04-09 10:18:23 -0600839 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100840 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600841 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600842 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600843 else {
844 unsigned int cpu;
845
Jens Axboe506e9312014-05-07 10:26:44 -0600846 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600847 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600848 }
Jens Axboe320ae512013-10-24 09:20:05 +0100849}
850
851void blk_mq_run_queues(struct request_queue *q, bool async)
852{
853 struct blk_mq_hw_ctx *hctx;
854 int i;
855
856 queue_for_each_hw_ctx(q, hctx, i) {
857 if ((!blk_mq_hctx_has_pending(hctx) &&
858 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600859 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100860 continue;
861
Jens Axboee4043dc2014-04-09 10:18:23 -0600862 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100863 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600864 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100865 }
866}
867EXPORT_SYMBOL(blk_mq_run_queues);
868
869void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
870{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600871 cancel_delayed_work(&hctx->run_work);
872 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100873 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
874}
875EXPORT_SYMBOL(blk_mq_stop_hw_queue);
876
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100877void blk_mq_stop_hw_queues(struct request_queue *q)
878{
879 struct blk_mq_hw_ctx *hctx;
880 int i;
881
882 queue_for_each_hw_ctx(q, hctx, i)
883 blk_mq_stop_hw_queue(hctx);
884}
885EXPORT_SYMBOL(blk_mq_stop_hw_queues);
886
Jens Axboe320ae512013-10-24 09:20:05 +0100887void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
888{
889 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600890
891 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100892 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600893 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100894}
895EXPORT_SYMBOL(blk_mq_start_hw_queue);
896
Christoph Hellwig2f268552014-04-16 09:44:56 +0200897void blk_mq_start_hw_queues(struct request_queue *q)
898{
899 struct blk_mq_hw_ctx *hctx;
900 int i;
901
902 queue_for_each_hw_ctx(q, hctx, i)
903 blk_mq_start_hw_queue(hctx);
904}
905EXPORT_SYMBOL(blk_mq_start_hw_queues);
906
907
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200908void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100909{
910 struct blk_mq_hw_ctx *hctx;
911 int i;
912
913 queue_for_each_hw_ctx(q, hctx, i) {
914 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
915 continue;
916
917 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600918 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200919 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600920 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100921 }
922}
923EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
924
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600925static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100926{
927 struct blk_mq_hw_ctx *hctx;
928
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600929 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600930
Jens Axboe320ae512013-10-24 09:20:05 +0100931 __blk_mq_run_hw_queue(hctx);
932}
933
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600934static void blk_mq_delay_work_fn(struct work_struct *work)
935{
936 struct blk_mq_hw_ctx *hctx;
937
938 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
939
940 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
941 __blk_mq_run_hw_queue(hctx);
942}
943
944void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
945{
946 unsigned long tmo = msecs_to_jiffies(msecs);
947
948 if (hctx->queue->nr_hw_queues == 1)
949 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
950 else {
951 unsigned int cpu;
952
Jens Axboe506e9312014-05-07 10:26:44 -0600953 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600954 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
955 }
956}
957EXPORT_SYMBOL(blk_mq_delay_queue);
958
Jens Axboe320ae512013-10-24 09:20:05 +0100959static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800960 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100961{
962 struct blk_mq_ctx *ctx = rq->mq_ctx;
963
Jens Axboe01b983c2013-11-19 18:59:10 -0700964 trace_block_rq_insert(hctx->queue, rq);
965
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800966 if (at_head)
967 list_add(&rq->queuelist, &ctx->rq_list);
968 else
969 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600970
Jens Axboe320ae512013-10-24 09:20:05 +0100971 blk_mq_hctx_mark_pending(hctx, ctx);
972
973 /*
974 * We do this early, to ensure we are on the right CPU.
975 */
Jens Axboe87ee7b12014-04-24 08:51:47 -0600976 blk_add_timer(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100977}
978
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600979void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
980 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100981{
982 struct request_queue *q = rq->q;
983 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600984 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100985
986 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600987 if (!cpu_online(ctx->cpu))
988 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100989
Jens Axboe320ae512013-10-24 09:20:05 +0100990 hctx = q->mq_ops->map_queue(q, ctx->cpu);
991
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600992 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
993 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
994 blk_insert_flush(rq);
995 } else {
996 spin_lock(&ctx->lock);
997 __blk_mq_insert_request(hctx, rq, at_head);
998 spin_unlock(&ctx->lock);
999 }
Jens Axboe320ae512013-10-24 09:20:05 +01001000
Jens Axboe320ae512013-10-24 09:20:05 +01001001 if (run_queue)
1002 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -06001003
1004 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001005}
1006
1007static void blk_mq_insert_requests(struct request_queue *q,
1008 struct blk_mq_ctx *ctx,
1009 struct list_head *list,
1010 int depth,
1011 bool from_schedule)
1012
1013{
1014 struct blk_mq_hw_ctx *hctx;
1015 struct blk_mq_ctx *current_ctx;
1016
1017 trace_block_unplug(q, depth, !from_schedule);
1018
1019 current_ctx = blk_mq_get_ctx(q);
1020
1021 if (!cpu_online(ctx->cpu))
1022 ctx = current_ctx;
1023 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1024
1025 /*
1026 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1027 * offline now
1028 */
1029 spin_lock(&ctx->lock);
1030 while (!list_empty(list)) {
1031 struct request *rq;
1032
1033 rq = list_first_entry(list, struct request, queuelist);
1034 list_del_init(&rq->queuelist);
1035 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001036 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001037 }
1038 spin_unlock(&ctx->lock);
1039
Jens Axboe320ae512013-10-24 09:20:05 +01001040 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001041 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001042}
1043
1044static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1045{
1046 struct request *rqa = container_of(a, struct request, queuelist);
1047 struct request *rqb = container_of(b, struct request, queuelist);
1048
1049 return !(rqa->mq_ctx < rqb->mq_ctx ||
1050 (rqa->mq_ctx == rqb->mq_ctx &&
1051 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1052}
1053
1054void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1055{
1056 struct blk_mq_ctx *this_ctx;
1057 struct request_queue *this_q;
1058 struct request *rq;
1059 LIST_HEAD(list);
1060 LIST_HEAD(ctx_list);
1061 unsigned int depth;
1062
1063 list_splice_init(&plug->mq_list, &list);
1064
1065 list_sort(NULL, &list, plug_ctx_cmp);
1066
1067 this_q = NULL;
1068 this_ctx = NULL;
1069 depth = 0;
1070
1071 while (!list_empty(&list)) {
1072 rq = list_entry_rq(list.next);
1073 list_del_init(&rq->queuelist);
1074 BUG_ON(!rq->q);
1075 if (rq->mq_ctx != this_ctx) {
1076 if (this_ctx) {
1077 blk_mq_insert_requests(this_q, this_ctx,
1078 &ctx_list, depth,
1079 from_schedule);
1080 }
1081
1082 this_ctx = rq->mq_ctx;
1083 this_q = rq->q;
1084 depth = 0;
1085 }
1086
1087 depth++;
1088 list_add_tail(&rq->queuelist, &ctx_list);
1089 }
1090
1091 /*
1092 * If 'this_ctx' is set, we know we have entries to complete
1093 * on 'ctx_list'. Do those.
1094 */
1095 if (this_ctx) {
1096 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1097 from_schedule);
1098 }
1099}
1100
1101static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1102{
1103 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001104
1105 if (blk_do_io_stat(rq)) {
1106 rq->start_time = jiffies;
1107 blk_account_io_start(rq, 1);
1108 }
Jens Axboe320ae512013-10-24 09:20:05 +01001109}
1110
Jens Axboe07068d52014-05-22 10:40:51 -06001111static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1112 struct blk_mq_ctx *ctx,
1113 struct request *rq, struct bio *bio)
1114{
1115 struct request_queue *q = hctx->queue;
1116
1117 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1118 blk_mq_bio_to_request(rq, bio);
1119 spin_lock(&ctx->lock);
1120insert_rq:
1121 __blk_mq_insert_request(hctx, rq, false);
1122 spin_unlock(&ctx->lock);
1123 return false;
1124 } else {
1125 spin_lock(&ctx->lock);
1126 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1127 blk_mq_bio_to_request(rq, bio);
1128 goto insert_rq;
1129 }
1130
1131 spin_unlock(&ctx->lock);
1132 __blk_mq_free_request(hctx, ctx, rq);
1133 return true;
1134 }
1135}
1136
1137struct blk_map_ctx {
1138 struct blk_mq_hw_ctx *hctx;
1139 struct blk_mq_ctx *ctx;
1140};
1141
1142static struct request *blk_mq_map_request(struct request_queue *q,
1143 struct bio *bio,
1144 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001145{
1146 struct blk_mq_hw_ctx *hctx;
1147 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001148 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001149 int rw = bio_data_dir(bio);
Ming Leicb96a42c2014-06-01 00:43:37 +08001150 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001151
Jens Axboe07068d52014-05-22 10:40:51 -06001152 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001153 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001154 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001155 }
1156
1157 ctx = blk_mq_get_ctx(q);
1158 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1159
Jens Axboe07068d52014-05-22 10:40:51 -06001160 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e82014-02-19 20:20:21 +08001161 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001162
Jens Axboe320ae512013-10-24 09:20:05 +01001163 trace_block_getrq(q, bio, rw);
Ming Leicb96a42c2014-06-01 00:43:37 +08001164 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1165 hctx);
1166 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001167 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001168 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001169 blk_mq_put_ctx(ctx);
1170 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001171
1172 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001173 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +08001174 blk_mq_set_alloc_data(&alloc_data, q,
1175 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1176 rq = __blk_mq_alloc_request(&alloc_data, rw);
1177 ctx = alloc_data.ctx;
1178 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001179 }
1180
1181 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001182 data->hctx = hctx;
1183 data->ctx = ctx;
1184 return rq;
1185}
1186
1187/*
1188 * Multiple hardware queue variant. This will not use per-process plugs,
1189 * but will attempt to bypass the hctx queueing if we can go straight to
1190 * hardware for SYNC IO.
1191 */
1192static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1193{
1194 const int is_sync = rw_is_sync(bio->bi_rw);
1195 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1196 struct blk_map_ctx data;
1197 struct request *rq;
1198
1199 blk_queue_bounce(q, &bio);
1200
1201 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1202 bio_endio(bio, -EIO);
1203 return;
1204 }
1205
1206 rq = blk_mq_map_request(q, bio, &data);
1207 if (unlikely(!rq))
1208 return;
1209
1210 if (unlikely(is_flush_fua)) {
1211 blk_mq_bio_to_request(rq, bio);
1212 blk_insert_flush(rq);
1213 goto run_queue;
1214 }
1215
1216 if (is_sync) {
1217 int ret;
1218
1219 blk_mq_bio_to_request(rq, bio);
1220 blk_mq_start_request(rq, true);
Jens Axboefeff6892014-05-30 15:42:56 -06001221 blk_add_timer(rq);
Jens Axboe07068d52014-05-22 10:40:51 -06001222
1223 /*
1224 * For OK queue, we are done. For error, kill it. Any other
1225 * error (busy), just add it to our list as we previously
1226 * would have done
1227 */
1228 ret = q->mq_ops->queue_rq(data.hctx, rq);
1229 if (ret == BLK_MQ_RQ_QUEUE_OK)
1230 goto done;
1231 else {
1232 __blk_mq_requeue_request(rq);
1233
1234 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1235 rq->errors = -EIO;
1236 blk_mq_end_io(rq, rq->errors);
1237 goto done;
1238 }
1239 }
1240 }
1241
1242 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1243 /*
1244 * For a SYNC request, send it to the hardware immediately. For
1245 * an ASYNC request, just ensure that we run it later on. The
1246 * latter allows for merging opportunities and more efficient
1247 * dispatching.
1248 */
1249run_queue:
1250 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1251 }
1252done:
1253 blk_mq_put_ctx(data.ctx);
1254}
1255
1256/*
1257 * Single hardware queue variant. This will attempt to use any per-process
1258 * plug for merging and IO deferral.
1259 */
1260static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1261{
1262 const int is_sync = rw_is_sync(bio->bi_rw);
1263 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1264 unsigned int use_plug, request_count = 0;
1265 struct blk_map_ctx data;
1266 struct request *rq;
1267
1268 /*
1269 * If we have multiple hardware queues, just go directly to
1270 * one of those for sync IO.
1271 */
1272 use_plug = !is_flush_fua && !is_sync;
1273
1274 blk_queue_bounce(q, &bio);
1275
1276 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1277 bio_endio(bio, -EIO);
1278 return;
1279 }
1280
1281 if (use_plug && !blk_queue_nomerges(q) &&
1282 blk_attempt_plug_merge(q, bio, &request_count))
1283 return;
1284
1285 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001286 if (unlikely(!rq))
1287 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001288
1289 if (unlikely(is_flush_fua)) {
1290 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001291 blk_insert_flush(rq);
1292 goto run_queue;
1293 }
1294
1295 /*
1296 * A task plug currently exists. Since this is completely lockless,
1297 * utilize that to temporarily store requests until the task is
1298 * either done or scheduled away.
1299 */
1300 if (use_plug) {
1301 struct blk_plug *plug = current->plug;
1302
1303 if (plug) {
1304 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001305 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001306 trace_block_plug(q);
1307 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1308 blk_flush_plug_list(plug, false);
1309 trace_block_plug(q);
1310 }
1311 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001312 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001313 return;
1314 }
1315 }
1316
Jens Axboe07068d52014-05-22 10:40:51 -06001317 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1318 /*
1319 * For a SYNC request, send it to the hardware immediately. For
1320 * an ASYNC request, just ensure that we run it later on. The
1321 * latter allows for merging opportunities and more efficient
1322 * dispatching.
1323 */
1324run_queue:
1325 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001326 }
1327
Jens Axboe07068d52014-05-22 10:40:51 -06001328 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001329}
1330
1331/*
1332 * Default mapping to a software queue, since we use one per CPU.
1333 */
1334struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1335{
1336 return q->queue_hw_ctx[q->mq_map[cpu]];
1337}
1338EXPORT_SYMBOL(blk_mq_map_queue);
1339
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001340static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1341 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001342{
1343 struct page *page;
1344
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001345 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001346 int i;
1347
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001348 for (i = 0; i < tags->nr_tags; i++) {
1349 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001350 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001351 set->ops->exit_request(set->driver_data, tags->rqs[i],
1352 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001353 }
1354 }
1355
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001356 while (!list_empty(&tags->page_list)) {
1357 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001358 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001359 __free_pages(page, page->private);
1360 }
1361
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001362 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001363
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001364 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001365}
1366
1367static size_t order_to_size(unsigned int order)
1368{
Ming Lei4ca08502014-04-19 18:00:18 +08001369 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001370}
1371
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001372static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1373 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001374{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001375 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001376 unsigned int i, j, entries_per_page, max_order = 4;
1377 size_t rq_size, left;
1378
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001379 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1380 set->numa_node);
1381 if (!tags)
1382 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001383
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001384 INIT_LIST_HEAD(&tags->page_list);
1385
1386 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1387 GFP_KERNEL, set->numa_node);
1388 if (!tags->rqs) {
1389 blk_mq_free_tags(tags);
1390 return NULL;
1391 }
Jens Axboe320ae512013-10-24 09:20:05 +01001392
1393 /*
1394 * rq_size is the size of the request plus driver payload, rounded
1395 * to the cacheline size
1396 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001397 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001398 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001399 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001400
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001401 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001402 int this_order = max_order;
1403 struct page *page;
1404 int to_do;
1405 void *p;
1406
1407 while (left < order_to_size(this_order - 1) && this_order)
1408 this_order--;
1409
1410 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001411 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1412 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001413 if (page)
1414 break;
1415 if (!this_order--)
1416 break;
1417 if (order_to_size(this_order) < rq_size)
1418 break;
1419 } while (1);
1420
1421 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001422 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001423
1424 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001425 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001426
1427 p = page_address(page);
1428 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001429 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001430 left -= to_do * rq_size;
1431 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001432 tags->rqs[i] = p;
1433 if (set->ops->init_request) {
1434 if (set->ops->init_request(set->driver_data,
1435 tags->rqs[i], hctx_idx, i,
1436 set->numa_node))
1437 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001438 }
1439
Jens Axboe320ae512013-10-24 09:20:05 +01001440 p += rq_size;
1441 i++;
1442 }
1443 }
1444
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001445 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001446
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001447fail:
1448 pr_warn("%s: failed to allocate requests\n", __func__);
1449 blk_mq_free_rq_map(set, tags, hctx_idx);
1450 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001451}
1452
Jens Axboe1429d7c2014-05-19 09:23:55 -06001453static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1454{
1455 kfree(bitmap->map);
1456}
1457
1458static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1459{
1460 unsigned int bpw = 8, total, num_maps, i;
1461
1462 bitmap->bits_per_word = bpw;
1463
1464 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1465 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1466 GFP_KERNEL, node);
1467 if (!bitmap->map)
1468 return -ENOMEM;
1469
1470 bitmap->map_size = num_maps;
1471
1472 total = nr_cpu_ids;
1473 for (i = 0; i < num_maps; i++) {
1474 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1475 total -= bitmap->map[i].depth;
1476 }
1477
1478 return 0;
1479}
1480
Jens Axboe484b4062014-05-21 14:01:15 -06001481static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1482{
1483 struct request_queue *q = hctx->queue;
1484 struct blk_mq_ctx *ctx;
1485 LIST_HEAD(tmp);
1486
1487 /*
1488 * Move ctx entries to new CPU, if this one is going away.
1489 */
1490 ctx = __blk_mq_get_ctx(q, cpu);
1491
1492 spin_lock(&ctx->lock);
1493 if (!list_empty(&ctx->rq_list)) {
1494 list_splice_init(&ctx->rq_list, &tmp);
1495 blk_mq_hctx_clear_pending(hctx, ctx);
1496 }
1497 spin_unlock(&ctx->lock);
1498
1499 if (list_empty(&tmp))
1500 return NOTIFY_OK;
1501
1502 ctx = blk_mq_get_ctx(q);
1503 spin_lock(&ctx->lock);
1504
1505 while (!list_empty(&tmp)) {
1506 struct request *rq;
1507
1508 rq = list_first_entry(&tmp, struct request, queuelist);
1509 rq->mq_ctx = ctx;
1510 list_move_tail(&rq->queuelist, &ctx->rq_list);
1511 }
1512
1513 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1514 blk_mq_hctx_mark_pending(hctx, ctx);
1515
1516 spin_unlock(&ctx->lock);
1517
1518 blk_mq_run_hw_queue(hctx, true);
1519 blk_mq_put_ctx(ctx);
1520 return NOTIFY_OK;
1521}
1522
1523static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1524{
1525 struct request_queue *q = hctx->queue;
1526 struct blk_mq_tag_set *set = q->tag_set;
1527
1528 if (set->tags[hctx->queue_num])
1529 return NOTIFY_OK;
1530
1531 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1532 if (!set->tags[hctx->queue_num])
1533 return NOTIFY_STOP;
1534
1535 hctx->tags = set->tags[hctx->queue_num];
1536 return NOTIFY_OK;
1537}
1538
1539static int blk_mq_hctx_notify(void *data, unsigned long action,
1540 unsigned int cpu)
1541{
1542 struct blk_mq_hw_ctx *hctx = data;
1543
1544 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1545 return blk_mq_hctx_cpu_offline(hctx, cpu);
1546 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1547 return blk_mq_hctx_cpu_online(hctx, cpu);
1548
1549 return NOTIFY_OK;
1550}
1551
Ming Lei624dbe42014-05-27 23:35:13 +08001552static void blk_mq_exit_hw_queues(struct request_queue *q,
1553 struct blk_mq_tag_set *set, int nr_queue)
1554{
1555 struct blk_mq_hw_ctx *hctx;
1556 unsigned int i;
1557
1558 queue_for_each_hw_ctx(q, hctx, i) {
1559 if (i == nr_queue)
1560 break;
1561
Jens Axboef899fed2014-06-04 09:11:53 -06001562 blk_mq_tag_idle(hctx);
1563
Ming Lei624dbe42014-05-27 23:35:13 +08001564 if (set->ops->exit_hctx)
1565 set->ops->exit_hctx(hctx, i);
1566
1567 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1568 kfree(hctx->ctxs);
1569 blk_mq_free_bitmap(&hctx->ctx_map);
1570 }
1571
1572}
1573
1574static void blk_mq_free_hw_queues(struct request_queue *q,
1575 struct blk_mq_tag_set *set)
1576{
1577 struct blk_mq_hw_ctx *hctx;
1578 unsigned int i;
1579
1580 queue_for_each_hw_ctx(q, hctx, i) {
1581 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001582 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001583 }
1584}
1585
Jens Axboe320ae512013-10-24 09:20:05 +01001586static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001587 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001588{
1589 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001590 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001591
1592 /*
1593 * Initialize hardware queues
1594 */
1595 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001596 int node;
1597
1598 node = hctx->numa_node;
1599 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001600 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001601
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001602 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1603 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001604 spin_lock_init(&hctx->lock);
1605 INIT_LIST_HEAD(&hctx->dispatch);
1606 hctx->queue = q;
1607 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001608 hctx->flags = set->flags;
1609 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001610
1611 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1612 blk_mq_hctx_notify, hctx);
1613 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1614
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001615 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001616
1617 /*
1618 * Allocate space for all possible cpus to avoid allocation in
1619 * runtime
1620 */
1621 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1622 GFP_KERNEL, node);
1623 if (!hctx->ctxs)
1624 break;
1625
Jens Axboe1429d7c2014-05-19 09:23:55 -06001626 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001627 break;
1628
Jens Axboe320ae512013-10-24 09:20:05 +01001629 hctx->nr_ctx = 0;
1630
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001631 if (set->ops->init_hctx &&
1632 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001633 break;
1634 }
1635
1636 if (i == q->nr_hw_queues)
1637 return 0;
1638
1639 /*
1640 * Init failed
1641 */
Ming Lei624dbe42014-05-27 23:35:13 +08001642 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001643
1644 return 1;
1645}
1646
1647static void blk_mq_init_cpu_queues(struct request_queue *q,
1648 unsigned int nr_hw_queues)
1649{
1650 unsigned int i;
1651
1652 for_each_possible_cpu(i) {
1653 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1654 struct blk_mq_hw_ctx *hctx;
1655
1656 memset(__ctx, 0, sizeof(*__ctx));
1657 __ctx->cpu = i;
1658 spin_lock_init(&__ctx->lock);
1659 INIT_LIST_HEAD(&__ctx->rq_list);
1660 __ctx->queue = q;
1661
1662 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001663 if (!cpu_online(i))
1664 continue;
1665
Jens Axboee4043dc2014-04-09 10:18:23 -06001666 hctx = q->mq_ops->map_queue(q, i);
1667 cpumask_set_cpu(i, hctx->cpumask);
1668 hctx->nr_ctx++;
1669
Jens Axboe320ae512013-10-24 09:20:05 +01001670 /*
1671 * Set local node, IFF we have more than one hw queue. If
1672 * not, we remain on the home node of the device
1673 */
1674 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1675 hctx->numa_node = cpu_to_node(i);
1676 }
1677}
1678
1679static void blk_mq_map_swqueue(struct request_queue *q)
1680{
1681 unsigned int i;
1682 struct blk_mq_hw_ctx *hctx;
1683 struct blk_mq_ctx *ctx;
1684
1685 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001686 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001687 hctx->nr_ctx = 0;
1688 }
1689
1690 /*
1691 * Map software to hardware queues
1692 */
1693 queue_for_each_ctx(q, ctx, i) {
1694 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001695 if (!cpu_online(i))
1696 continue;
1697
Jens Axboe320ae512013-10-24 09:20:05 +01001698 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001699 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001700 ctx->index_hw = hctx->nr_ctx;
1701 hctx->ctxs[hctx->nr_ctx++] = ctx;
1702 }
Jens Axboe506e9312014-05-07 10:26:44 -06001703
1704 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001705 /*
1706 * If not software queues are mapped to this hardware queue,
1707 * disable it and free the request entries
1708 */
1709 if (!hctx->nr_ctx) {
1710 struct blk_mq_tag_set *set = q->tag_set;
1711
1712 if (set->tags[i]) {
1713 blk_mq_free_rq_map(set, set->tags[i], i);
1714 set->tags[i] = NULL;
1715 hctx->tags = NULL;
1716 }
1717 continue;
1718 }
1719
1720 /*
1721 * Initialize batch roundrobin counts
1722 */
Jens Axboe506e9312014-05-07 10:26:44 -06001723 hctx->next_cpu = cpumask_first(hctx->cpumask);
1724 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1725 }
Jens Axboe320ae512013-10-24 09:20:05 +01001726}
1727
Jens Axboe0d2602c2014-05-13 15:10:52 -06001728static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1729{
1730 struct blk_mq_hw_ctx *hctx;
1731 struct request_queue *q;
1732 bool shared;
1733 int i;
1734
1735 if (set->tag_list.next == set->tag_list.prev)
1736 shared = false;
1737 else
1738 shared = true;
1739
1740 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1741 blk_mq_freeze_queue(q);
1742
1743 queue_for_each_hw_ctx(q, hctx, i) {
1744 if (shared)
1745 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1746 else
1747 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1748 }
1749 blk_mq_unfreeze_queue(q);
1750 }
1751}
1752
1753static void blk_mq_del_queue_tag_set(struct request_queue *q)
1754{
1755 struct blk_mq_tag_set *set = q->tag_set;
1756
1757 blk_mq_freeze_queue(q);
1758
1759 mutex_lock(&set->tag_list_lock);
1760 list_del_init(&q->tag_set_list);
1761 blk_mq_update_tag_set_depth(set);
1762 mutex_unlock(&set->tag_list_lock);
1763
1764 blk_mq_unfreeze_queue(q);
1765}
1766
1767static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1768 struct request_queue *q)
1769{
1770 q->tag_set = set;
1771
1772 mutex_lock(&set->tag_list_lock);
1773 list_add_tail(&q->tag_set_list, &set->tag_list);
1774 blk_mq_update_tag_set_depth(set);
1775 mutex_unlock(&set->tag_list_lock);
1776}
1777
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001778struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001779{
1780 struct blk_mq_hw_ctx **hctxs;
Ming Leie6cdb092014-06-03 11:24:06 +08001781 struct blk_mq_ctx __percpu *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001782 struct request_queue *q;
Jens Axboef14bbe72014-05-27 12:06:53 -06001783 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001784 int i;
1785
Jens Axboe320ae512013-10-24 09:20:05 +01001786 ctx = alloc_percpu(struct blk_mq_ctx);
1787 if (!ctx)
1788 return ERR_PTR(-ENOMEM);
1789
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001790 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1791 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001792
1793 if (!hctxs)
1794 goto err_percpu;
1795
Jens Axboef14bbe72014-05-27 12:06:53 -06001796 map = blk_mq_make_queue_map(set);
1797 if (!map)
1798 goto err_map;
1799
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001800 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001801 int node = blk_mq_hw_queue_to_node(map, i);
1802
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001803 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1804 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001805 if (!hctxs[i])
1806 goto err_hctxs;
1807
Jens Axboee4043dc2014-04-09 10:18:23 -06001808 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1809 goto err_hctxs;
1810
Jens Axboe0d2602c2014-05-13 15:10:52 -06001811 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001812 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001813 hctxs[i]->queue_num = i;
1814 }
1815
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001816 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001817 if (!q)
1818 goto err_hctxs;
1819
Ming Lei3d2936f2014-05-27 23:35:14 +08001820 if (percpu_counter_init(&q->mq_usage_counter, 0))
1821 goto err_map;
1822
Jens Axboe320ae512013-10-24 09:20:05 +01001823 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1824 blk_queue_rq_timeout(q, 30000);
1825
1826 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001827 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001828 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001829
1830 q->queue_ctx = ctx;
1831 q->queue_hw_ctx = hctxs;
1832
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001833 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001834 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001835
Jens Axboe05f1dd52014-05-29 09:53:32 -06001836 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1837 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1838
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001839 q->sg_reserved_size = INT_MAX;
1840
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001841 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1842 INIT_LIST_HEAD(&q->requeue_list);
1843 spin_lock_init(&q->requeue_lock);
1844
Jens Axboe07068d52014-05-22 10:40:51 -06001845 if (q->nr_hw_queues > 1)
1846 blk_queue_make_request(q, blk_mq_make_request);
1847 else
1848 blk_queue_make_request(q, blk_sq_make_request);
1849
Jens Axboe87ee7b12014-04-24 08:51:47 -06001850 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001851 if (set->timeout)
1852 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001853
Jens Axboeeba71762014-05-20 15:17:27 -06001854 /*
1855 * Do this after blk_queue_make_request() overrides it...
1856 */
1857 q->nr_requests = set->queue_depth;
1858
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001859 if (set->ops->complete)
1860 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001861
Jens Axboe320ae512013-10-24 09:20:05 +01001862 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001863 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001864
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001865 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1866 set->cmd_size, cache_line_size()),
1867 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001868 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001869 goto err_hw;
1870
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001871 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001872 goto err_flush_rq;
1873
Jens Axboe320ae512013-10-24 09:20:05 +01001874 mutex_lock(&all_q_mutex);
1875 list_add_tail(&q->all_q_node, &all_q_list);
1876 mutex_unlock(&all_q_mutex);
1877
Jens Axboe0d2602c2014-05-13 15:10:52 -06001878 blk_mq_add_queue_tag_set(set, q);
1879
Jens Axboe484b4062014-05-21 14:01:15 -06001880 blk_mq_map_swqueue(q);
1881
Jens Axboe320ae512013-10-24 09:20:05 +01001882 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001883
1884err_flush_rq:
1885 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001886err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001887 blk_cleanup_queue(q);
1888err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001889 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001890 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001891 if (!hctxs[i])
1892 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001893 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001894 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001895 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001896err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001897 kfree(hctxs);
1898err_percpu:
1899 free_percpu(ctx);
1900 return ERR_PTR(-ENOMEM);
1901}
1902EXPORT_SYMBOL(blk_mq_init_queue);
1903
1904void blk_mq_free_queue(struct request_queue *q)
1905{
Ming Lei624dbe42014-05-27 23:35:13 +08001906 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001907
Jens Axboe0d2602c2014-05-13 15:10:52 -06001908 blk_mq_del_queue_tag_set(q);
1909
Ming Lei624dbe42014-05-27 23:35:13 +08001910 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1911 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001912
Ming Lei3d2936f2014-05-27 23:35:14 +08001913 percpu_counter_destroy(&q->mq_usage_counter);
1914
Jens Axboe320ae512013-10-24 09:20:05 +01001915 free_percpu(q->queue_ctx);
1916 kfree(q->queue_hw_ctx);
1917 kfree(q->mq_map);
1918
1919 q->queue_ctx = NULL;
1920 q->queue_hw_ctx = NULL;
1921 q->mq_map = NULL;
1922
1923 mutex_lock(&all_q_mutex);
1924 list_del_init(&q->all_q_node);
1925 mutex_unlock(&all_q_mutex);
1926}
Jens Axboe320ae512013-10-24 09:20:05 +01001927
1928/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001929static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001930{
1931 blk_mq_freeze_queue(q);
1932
Jens Axboe67aec142014-05-30 08:25:36 -06001933 blk_mq_sysfs_unregister(q);
1934
Jens Axboe320ae512013-10-24 09:20:05 +01001935 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1936
1937 /*
1938 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1939 * we should change hctx numa_node according to new topology (this
1940 * involves free and re-allocate memory, worthy doing?)
1941 */
1942
1943 blk_mq_map_swqueue(q);
1944
Jens Axboe67aec142014-05-30 08:25:36 -06001945 blk_mq_sysfs_register(q);
1946
Jens Axboe320ae512013-10-24 09:20:05 +01001947 blk_mq_unfreeze_queue(q);
1948}
1949
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001950static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1951 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001952{
1953 struct request_queue *q;
1954
1955 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001956 * Before new mappings are established, hotadded cpu might already
1957 * start handling requests. This doesn't break anything as we map
1958 * offline CPUs to first hardware queue. We will re-init the queue
1959 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001960 */
1961 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1962 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1963 return NOTIFY_OK;
1964
1965 mutex_lock(&all_q_mutex);
1966 list_for_each_entry(q, &all_q_list, all_q_node)
1967 blk_mq_queue_reinit(q);
1968 mutex_unlock(&all_q_mutex);
1969 return NOTIFY_OK;
1970}
1971
Jens Axboea4391c62014-06-05 15:21:56 -06001972/*
1973 * Alloc a tag set to be associated with one or more request queues.
1974 * May fail with EINVAL for various error conditions. May adjust the
1975 * requested depth down, if if it too large. In that case, the set
1976 * value will be stored in set->queue_depth.
1977 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001978int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1979{
1980 int i;
1981
1982 if (!set->nr_hw_queues)
1983 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06001984 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001985 return -EINVAL;
1986 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1987 return -EINVAL;
1988
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001989 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001990 return -EINVAL;
1991
Jens Axboea4391c62014-06-05 15:21:56 -06001992 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
1993 pr_info("blk-mq: reduced tag depth to %u\n",
1994 BLK_MQ_MAX_DEPTH);
1995 set->queue_depth = BLK_MQ_MAX_DEPTH;
1996 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001997
Ming Lei484790052014-04-19 18:00:17 +08001998 set->tags = kmalloc_node(set->nr_hw_queues *
1999 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002000 GFP_KERNEL, set->numa_node);
2001 if (!set->tags)
2002 goto out;
2003
2004 for (i = 0; i < set->nr_hw_queues; i++) {
2005 set->tags[i] = blk_mq_init_rq_map(set, i);
2006 if (!set->tags[i])
2007 goto out_unwind;
2008 }
2009
Jens Axboe0d2602c2014-05-13 15:10:52 -06002010 mutex_init(&set->tag_list_lock);
2011 INIT_LIST_HEAD(&set->tag_list);
2012
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002013 return 0;
2014
2015out_unwind:
2016 while (--i >= 0)
2017 blk_mq_free_rq_map(set, set->tags[i], i);
2018out:
2019 return -ENOMEM;
2020}
2021EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2022
2023void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2024{
2025 int i;
2026
Jens Axboe484b4062014-05-21 14:01:15 -06002027 for (i = 0; i < set->nr_hw_queues; i++) {
2028 if (set->tags[i])
2029 blk_mq_free_rq_map(set, set->tags[i], i);
2030 }
2031
Ming Lei981bd182014-04-24 00:07:34 +08002032 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002033}
2034EXPORT_SYMBOL(blk_mq_free_tag_set);
2035
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002036int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2037{
2038 struct blk_mq_tag_set *set = q->tag_set;
2039 struct blk_mq_hw_ctx *hctx;
2040 int i, ret;
2041
2042 if (!set || nr > set->queue_depth)
2043 return -EINVAL;
2044
2045 ret = 0;
2046 queue_for_each_hw_ctx(q, hctx, i) {
2047 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2048 if (ret)
2049 break;
2050 }
2051
2052 if (!ret)
2053 q->nr_requests = nr;
2054
2055 return ret;
2056}
2057
Jens Axboe676141e2014-03-20 13:29:18 -06002058void blk_mq_disable_hotplug(void)
2059{
2060 mutex_lock(&all_q_mutex);
2061}
2062
2063void blk_mq_enable_hotplug(void)
2064{
2065 mutex_unlock(&all_q_mutex);
2066}
2067
Jens Axboe320ae512013-10-24 09:20:05 +01002068static int __init blk_mq_init(void)
2069{
Jens Axboe320ae512013-10-24 09:20:05 +01002070 blk_mq_cpu_init();
2071
2072 /* Must be called after percpu_counter_hotcpu_callback() */
2073 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2074
2075 return 0;
2076}
2077subsys_initcall(blk_mq_init);