blob: a5ea37d7e82013d8947fd2016ad8a010f9cb6b72 [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;
Jens Axboe3ee32372014-06-09 09:36:53 -0600188 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200189#ifdef CONFIG_BLK_CGROUP
190 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700191 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200192 rq->io_start_time_ns = 0;
193#endif
194 rq->nr_phys_segments = 0;
195#if defined(CONFIG_BLK_DEV_INTEGRITY)
196 rq->nr_integrity_segments = 0;
197#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200198 rq->special = NULL;
199 /* tag was already set */
200 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200201
202 rq->extra_len = 0;
203 rq->sense_len = 0;
204 rq->resid_len = 0;
205 rq->sense = NULL;
206
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200207 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600208 rq->timeout = 0;
209
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200210 rq->end_io = NULL;
211 rq->end_io_data = NULL;
212 rq->next_rq = NULL;
213
Jens Axboe320ae512013-10-24 09:20:05 +0100214 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
215}
216
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200217static struct request *
Ming Leicb96a42c2014-06-01 00:43:37 +0800218__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200219{
220 struct request *rq;
221 unsigned int tag;
222
Ming Leicb96a42c2014-06-01 00:43:37 +0800223 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200224 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a42c2014-06-01 00:43:37 +0800225 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200226
227 rq->cmd_flags = 0;
Ming Leicb96a42c2014-06-01 00:43:37 +0800228 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200229 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a42c2014-06-01 00:43:37 +0800230 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200231 }
232
233 rq->tag = tag;
Ming Leicb96a42c2014-06-01 00:43:37 +0800234 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200235 return rq;
236 }
237
238 return NULL;
239}
240
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200241struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
242 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100243{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200244 struct blk_mq_ctx *ctx;
245 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100246 struct request *rq;
Ming Leicb96a42c2014-06-01 00:43:37 +0800247 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +0100248
249 if (blk_mq_queue_enter(q))
250 return NULL;
251
Christoph Hellwigd8525642014-05-27 20:59:50 +0200252 ctx = blk_mq_get_ctx(q);
253 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +0800254 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
255 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200256
Ming Leicb96a42c2014-06-01 00:43:37 +0800257 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200258 if (!rq && (gfp & __GFP_WAIT)) {
259 __blk_mq_run_hw_queue(hctx);
260 blk_mq_put_ctx(ctx);
261
262 ctx = blk_mq_get_ctx(q);
263 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +0800264 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
265 hctx);
266 rq = __blk_mq_alloc_request(&alloc_data, rw);
267 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200268 }
269 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100270 return rq;
271}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600272EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100273
Jens Axboe320ae512013-10-24 09:20:05 +0100274static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
275 struct blk_mq_ctx *ctx, struct request *rq)
276{
277 const int tag = rq->tag;
278 struct request_queue *q = rq->q;
279
Jens Axboe0d2602c2014-05-13 15:10:52 -0600280 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
281 atomic_dec(&hctx->nr_active);
282
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200283 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600284 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100285 blk_mq_queue_exit(q);
286}
287
288void blk_mq_free_request(struct request *rq)
289{
290 struct blk_mq_ctx *ctx = rq->mq_ctx;
291 struct blk_mq_hw_ctx *hctx;
292 struct request_queue *q = rq->q;
293
294 ctx->rq_completed[rq_is_sync(rq)]++;
295
296 hctx = q->mq_ops->map_queue(q, ctx->cpu);
297 __blk_mq_free_request(hctx, ctx, rq);
298}
299
Christoph Hellwig8727af42014-04-14 10:30:08 +0200300/*
301 * Clone all relevant state from a request that has been put on hold in
302 * the flush state machine into the preallocated flush request that hangs
303 * off the request queue.
304 *
305 * For a driver the flush request should be invisible, that's why we are
306 * impersonating the original request here.
307 */
308void blk_mq_clone_flush_request(struct request *flush_rq,
309 struct request *orig_rq)
310{
311 struct blk_mq_hw_ctx *hctx =
312 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
313
314 flush_rq->mq_ctx = orig_rq->mq_ctx;
315 flush_rq->tag = orig_rq->tag;
316 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
317 hctx->cmd_size);
318}
319
Christoph Hellwig63151a42014-04-16 09:44:52 +0200320inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100321{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700322 blk_account_io_done(rq);
323
Christoph Hellwig91b63632014-04-16 09:44:53 +0200324 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100325 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200326 } else {
327 if (unlikely(blk_bidi_rq(rq)))
328 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100329 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200330 }
Jens Axboe320ae512013-10-24 09:20:05 +0100331}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200332EXPORT_SYMBOL(__blk_mq_end_io);
333
334void blk_mq_end_io(struct request *rq, int error)
335{
336 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
337 BUG();
338 __blk_mq_end_io(rq, error);
339}
340EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100341
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800342static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100343{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800344 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100345
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800346 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100347}
348
Jens Axboeed851862014-05-30 21:20:50 -0600349static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100350{
351 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700352 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100353 int cpu;
354
Christoph Hellwig38535202014-04-25 02:32:53 -0700355 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800356 rq->q->softirq_done_fn(rq);
357 return;
358 }
Jens Axboe320ae512013-10-24 09:20:05 +0100359
360 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700361 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
362 shared = cpus_share_cache(cpu, ctx->cpu);
363
364 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800365 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800366 rq->csd.info = rq;
367 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100368 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800369 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800370 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800371 }
Jens Axboe320ae512013-10-24 09:20:05 +0100372 put_cpu();
373}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800374
Jens Axboeed851862014-05-30 21:20:50 -0600375void __blk_mq_complete_request(struct request *rq)
376{
377 struct request_queue *q = rq->q;
378
379 if (!q->softirq_done_fn)
380 blk_mq_end_io(rq, rq->errors);
381 else
382 blk_mq_ipi_complete_request(rq);
383}
384
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800385/**
386 * blk_mq_complete_request - end I/O on a request
387 * @rq: the request being processed
388 *
389 * Description:
390 * Ends all I/O on a request. It does not handle partial completions.
391 * The actual completion happens out-of-order, through a IPI handler.
392 **/
393void blk_mq_complete_request(struct request *rq)
394{
Jens Axboe95f09682014-05-27 17:46:48 -0600395 struct request_queue *q = rq->q;
396
397 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800398 return;
Jens Axboeed851862014-05-30 21:20:50 -0600399 if (!blk_mark_rq_complete(rq))
400 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800401}
402EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100403
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800404static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100405{
406 struct request_queue *q = rq->q;
407
408 trace_block_rq_issue(q, rq);
409
Christoph Hellwig742ee692014-04-14 10:30:06 +0200410 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200411 if (unlikely(blk_bidi_rq(rq)))
412 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200413
Jens Axboe320ae512013-10-24 09:20:05 +0100414 /*
415 * Just mark start time and set the started bit. Due to memory
416 * ordering, we know we'll see the correct deadline as long as
Jens Axboec22d9d82014-05-23 14:14:57 -0600417 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
418 * unless one has been set in the request.
Jens Axboe320ae512013-10-24 09:20:05 +0100419 */
Jens Axboec22d9d82014-05-23 14:14:57 -0600420 if (!rq->timeout)
421 rq->deadline = jiffies + q->rq_timeout;
422 else
423 rq->deadline = jiffies + rq->timeout;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600424
425 /*
426 * Mark us as started and clear complete. Complete might have been
427 * set if requeue raced with timeout, which then marked it as
428 * complete. So be sure to clear complete again when we start
429 * the request, otherwise we'll ignore the completion event.
430 */
Jens Axboe4b570522014-05-29 11:00:11 -0600431 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
432 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
433 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
434 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800435
436 if (q->dma_drain_size && blk_rq_bytes(rq)) {
437 /*
438 * Make sure space for the drain appears. We know we can do
439 * this because max_hw_segments has been adjusted to be one
440 * fewer than the device can handle.
441 */
442 rq->nr_phys_segments++;
443 }
444
445 /*
446 * Flag the last request in the series so that drivers know when IO
447 * should be kicked off, if they don't do it on a per-request basis.
448 *
449 * Note: the flag isn't the only condition drivers should do kick off.
450 * If drive is busy, the last request might not have the bit set.
451 */
452 if (last)
453 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100454}
455
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);
461 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800462
463 rq->cmd_flags &= ~REQ_END;
464
465 if (q->dma_drain_size && blk_rq_bytes(rq))
466 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100467}
468
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200469void blk_mq_requeue_request(struct request *rq)
470{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200471 __blk_mq_requeue_request(rq);
472 blk_clear_rq_complete(rq);
473
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200474 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600475 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200476}
477EXPORT_SYMBOL(blk_mq_requeue_request);
478
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600479static void blk_mq_requeue_work(struct work_struct *work)
480{
481 struct request_queue *q =
482 container_of(work, struct request_queue, requeue_work);
483 LIST_HEAD(rq_list);
484 struct request *rq, *next;
485 unsigned long flags;
486
487 spin_lock_irqsave(&q->requeue_lock, flags);
488 list_splice_init(&q->requeue_list, &rq_list);
489 spin_unlock_irqrestore(&q->requeue_lock, flags);
490
491 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
492 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
493 continue;
494
495 rq->cmd_flags &= ~REQ_SOFTBARRIER;
496 list_del_init(&rq->queuelist);
497 blk_mq_insert_request(rq, true, false, false);
498 }
499
500 while (!list_empty(&rq_list)) {
501 rq = list_entry(rq_list.next, struct request, queuelist);
502 list_del_init(&rq->queuelist);
503 blk_mq_insert_request(rq, false, false, false);
504 }
505
506 blk_mq_run_queues(q, false);
507}
508
509void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
510{
511 struct request_queue *q = rq->q;
512 unsigned long flags;
513
514 /*
515 * We abuse this flag that is otherwise used by the I/O scheduler to
516 * request head insertation from the workqueue.
517 */
518 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
519
520 spin_lock_irqsave(&q->requeue_lock, flags);
521 if (at_head) {
522 rq->cmd_flags |= REQ_SOFTBARRIER;
523 list_add(&rq->queuelist, &q->requeue_list);
524 } else {
525 list_add_tail(&rq->queuelist, &q->requeue_list);
526 }
527 spin_unlock_irqrestore(&q->requeue_lock, flags);
528}
529EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
530
531void blk_mq_kick_requeue_list(struct request_queue *q)
532{
533 kblockd_schedule_work(&q->requeue_work);
534}
535EXPORT_SYMBOL(blk_mq_kick_requeue_list);
536
Jens Axboe0e62f512014-06-04 10:23:49 -0600537static inline bool is_flush_request(struct request *rq, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600538{
Jens Axboe0e62f512014-06-04 10:23:49 -0600539 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
540 rq->q->flush_rq->tag == tag);
541}
Shaohua Li22302372014-05-30 08:06:42 -0600542
Jens Axboe0e62f512014-06-04 10:23:49 -0600543struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
544{
545 struct request *rq = tags->rqs[tag];
Shaohua Li22302372014-05-30 08:06:42 -0600546
Jens Axboe0e62f512014-06-04 10:23:49 -0600547 if (!is_flush_request(rq, tag))
548 return rq;
549
550 return rq->q->flush_rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600551}
552EXPORT_SYMBOL(blk_mq_tag_to_rq);
553
Jens Axboe320ae512013-10-24 09:20:05 +0100554struct blk_mq_timeout_data {
555 struct blk_mq_hw_ctx *hctx;
556 unsigned long *next;
557 unsigned int *next_set;
558};
559
560static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
561{
562 struct blk_mq_timeout_data *data = __data;
563 struct blk_mq_hw_ctx *hctx = data->hctx;
564 unsigned int tag;
565
566 /* It may not be in flight yet (this is where
567 * the REQ_ATOMIC_STARTED flag comes in). The requests are
568 * statically allocated, so we know it's always safe to access the
569 * memory associated with a bit offset into ->rqs[].
570 */
571 tag = 0;
572 do {
573 struct request *rq;
574
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600575 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
576 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100577 break;
578
Jens Axboe0e62f512014-06-04 10:23:49 -0600579 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600580 if (rq->q != hctx->queue)
581 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100582 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
583 continue;
584
585 blk_rq_check_expired(rq, data->next, data->next_set);
586 } while (1);
587}
588
589static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
590 unsigned long *next,
591 unsigned int *next_set)
592{
593 struct blk_mq_timeout_data data = {
594 .hctx = hctx,
595 .next = next,
596 .next_set = next_set,
597 };
598
599 /*
600 * Ask the tagging code to iterate busy requests, so we can
601 * check them for timeout.
602 */
603 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
604}
605
Jens Axboe87ee7b12014-04-24 08:51:47 -0600606static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
607{
608 struct request_queue *q = rq->q;
609
610 /*
611 * We know that complete is set at this point. If STARTED isn't set
612 * anymore, then the request isn't active and the "timeout" should
613 * just be ignored. This can happen due to the bitflag ordering.
614 * Timeout first checks if STARTED is set, and if it is, assumes
615 * the request is active. But if we race with completion, then
616 * we both flags will get cleared. So check here again, and ignore
617 * a timeout event with a request that isn't active.
618 */
619 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
620 return BLK_EH_NOT_HANDLED;
621
622 if (!q->mq_ops->timeout)
623 return BLK_EH_RESET_TIMER;
624
625 return q->mq_ops->timeout(rq);
626}
627
Jens Axboe320ae512013-10-24 09:20:05 +0100628static void blk_mq_rq_timer(unsigned long data)
629{
630 struct request_queue *q = (struct request_queue *) data;
631 struct blk_mq_hw_ctx *hctx;
632 unsigned long next = 0;
633 int i, next_set = 0;
634
Jens Axboe484b4062014-05-21 14:01:15 -0600635 queue_for_each_hw_ctx(q, hctx, i) {
636 /*
637 * If not software queues are currently mapped to this
638 * hardware queue, there's nothing to check
639 */
640 if (!hctx->nr_ctx || !hctx->tags)
641 continue;
642
Jens Axboe320ae512013-10-24 09:20:05 +0100643 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
Jens Axboe484b4062014-05-21 14:01:15 -0600644 }
Jens Axboe320ae512013-10-24 09:20:05 +0100645
Jens Axboe0d2602c2014-05-13 15:10:52 -0600646 if (next_set) {
647 next = blk_rq_timeout(round_jiffies_up(next));
648 mod_timer(&q->timeout, next);
649 } else {
650 queue_for_each_hw_ctx(q, hctx, i)
651 blk_mq_tag_idle(hctx);
652 }
Jens Axboe320ae512013-10-24 09:20:05 +0100653}
654
655/*
656 * Reverse check our software queue for entries that we could potentially
657 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
658 * too much time checking for merges.
659 */
660static bool blk_mq_attempt_merge(struct request_queue *q,
661 struct blk_mq_ctx *ctx, struct bio *bio)
662{
663 struct request *rq;
664 int checked = 8;
665
666 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
667 int el_ret;
668
669 if (!checked--)
670 break;
671
672 if (!blk_rq_merge_ok(rq, bio))
673 continue;
674
675 el_ret = blk_try_merge(rq, bio);
676 if (el_ret == ELEVATOR_BACK_MERGE) {
677 if (bio_attempt_back_merge(q, rq, bio)) {
678 ctx->rq_merged++;
679 return true;
680 }
681 break;
682 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
683 if (bio_attempt_front_merge(q, rq, bio)) {
684 ctx->rq_merged++;
685 return true;
686 }
687 break;
688 }
689 }
690
691 return false;
692}
693
Jens Axboe320ae512013-10-24 09:20:05 +0100694/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600695 * Process software queues that have been marked busy, splicing them
696 * to the for-dispatch
697 */
698static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
699{
700 struct blk_mq_ctx *ctx;
701 int i;
702
703 for (i = 0; i < hctx->ctx_map.map_size; i++) {
704 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
705 unsigned int off, bit;
706
707 if (!bm->word)
708 continue;
709
710 bit = 0;
711 off = i * hctx->ctx_map.bits_per_word;
712 do {
713 bit = find_next_bit(&bm->word, bm->depth, bit);
714 if (bit >= bm->depth)
715 break;
716
717 ctx = hctx->ctxs[bit + off];
718 clear_bit(bit, &bm->word);
719 spin_lock(&ctx->lock);
720 list_splice_tail_init(&ctx->rq_list, list);
721 spin_unlock(&ctx->lock);
722
723 bit++;
724 } while (1);
725 }
726}
727
728/*
Jens Axboe320ae512013-10-24 09:20:05 +0100729 * Run this hardware queue, pulling any software queues mapped to it in.
730 * Note that this function currently has various problems around ordering
731 * of IO. In particular, we'd like FIFO behaviour on handling existing
732 * items on the hctx->dispatch list. Ignore that for now.
733 */
734static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
735{
736 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100737 struct request *rq;
738 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600739 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100740
Jens Axboefd1270d2014-04-16 09:23:48 -0600741 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600742
Jens Axboe5d12f902014-03-19 15:25:02 -0600743 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100744 return;
745
746 hctx->run++;
747
748 /*
749 * Touch any software queue that has pending entries.
750 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600751 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100752
753 /*
754 * If we have previous entries on our dispatch list, grab them
755 * and stuff them at the front for more fair dispatch.
756 */
757 if (!list_empty_careful(&hctx->dispatch)) {
758 spin_lock(&hctx->lock);
759 if (!list_empty(&hctx->dispatch))
760 list_splice_init(&hctx->dispatch, &rq_list);
761 spin_unlock(&hctx->lock);
762 }
763
764 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100765 * Now process all the entries, sending them to the driver.
766 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600767 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100768 while (!list_empty(&rq_list)) {
769 int ret;
770
771 rq = list_first_entry(&rq_list, struct request, queuelist);
772 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100773
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800774 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100775
776 ret = q->mq_ops->queue_rq(hctx, rq);
777 switch (ret) {
778 case BLK_MQ_RQ_QUEUE_OK:
779 queued++;
780 continue;
781 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100782 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200783 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100784 break;
785 default:
786 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100787 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800788 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100789 blk_mq_end_io(rq, rq->errors);
790 break;
791 }
792
793 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
794 break;
795 }
796
797 if (!queued)
798 hctx->dispatched[0]++;
799 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
800 hctx->dispatched[ilog2(queued) + 1]++;
801
802 /*
803 * Any items that need requeuing? Stuff them into hctx->dispatch,
804 * that is where we will continue on next queue run.
805 */
806 if (!list_empty(&rq_list)) {
807 spin_lock(&hctx->lock);
808 list_splice(&rq_list, &hctx->dispatch);
809 spin_unlock(&hctx->lock);
810 }
811}
812
Jens Axboe506e9312014-05-07 10:26:44 -0600813/*
814 * It'd be great if the workqueue API had a way to pass
815 * in a mask and had some smarts for more clever placement.
816 * For now we just round-robin here, switching for every
817 * BLK_MQ_CPU_WORK_BATCH queued items.
818 */
819static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
820{
821 int cpu = hctx->next_cpu;
822
823 if (--hctx->next_cpu_batch <= 0) {
824 int next_cpu;
825
826 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
827 if (next_cpu >= nr_cpu_ids)
828 next_cpu = cpumask_first(hctx->cpumask);
829
830 hctx->next_cpu = next_cpu;
831 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
832 }
833
834 return cpu;
835}
836
Jens Axboe320ae512013-10-24 09:20:05 +0100837void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
838{
Jens Axboe5d12f902014-03-19 15:25:02 -0600839 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100840 return;
841
Jens Axboee4043dc2014-04-09 10:18:23 -0600842 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100843 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600844 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600845 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600846 else {
847 unsigned int cpu;
848
Jens Axboe506e9312014-05-07 10:26:44 -0600849 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600850 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600851 }
Jens Axboe320ae512013-10-24 09:20:05 +0100852}
853
854void blk_mq_run_queues(struct request_queue *q, bool async)
855{
856 struct blk_mq_hw_ctx *hctx;
857 int i;
858
859 queue_for_each_hw_ctx(q, hctx, i) {
860 if ((!blk_mq_hctx_has_pending(hctx) &&
861 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600862 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100863 continue;
864
Jens Axboee4043dc2014-04-09 10:18:23 -0600865 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100866 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600867 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100868 }
869}
870EXPORT_SYMBOL(blk_mq_run_queues);
871
872void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
873{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600874 cancel_delayed_work(&hctx->run_work);
875 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100876 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
877}
878EXPORT_SYMBOL(blk_mq_stop_hw_queue);
879
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100880void blk_mq_stop_hw_queues(struct request_queue *q)
881{
882 struct blk_mq_hw_ctx *hctx;
883 int i;
884
885 queue_for_each_hw_ctx(q, hctx, i)
886 blk_mq_stop_hw_queue(hctx);
887}
888EXPORT_SYMBOL(blk_mq_stop_hw_queues);
889
Jens Axboe320ae512013-10-24 09:20:05 +0100890void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
891{
892 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600893
894 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100895 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600896 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100897}
898EXPORT_SYMBOL(blk_mq_start_hw_queue);
899
Christoph Hellwig2f268552014-04-16 09:44:56 +0200900void blk_mq_start_hw_queues(struct request_queue *q)
901{
902 struct blk_mq_hw_ctx *hctx;
903 int i;
904
905 queue_for_each_hw_ctx(q, hctx, i)
906 blk_mq_start_hw_queue(hctx);
907}
908EXPORT_SYMBOL(blk_mq_start_hw_queues);
909
910
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200911void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100912{
913 struct blk_mq_hw_ctx *hctx;
914 int i;
915
916 queue_for_each_hw_ctx(q, hctx, i) {
917 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
918 continue;
919
920 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600921 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200922 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600923 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100924 }
925}
926EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
927
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600928static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100929{
930 struct blk_mq_hw_ctx *hctx;
931
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600932 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600933
Jens Axboe320ae512013-10-24 09:20:05 +0100934 __blk_mq_run_hw_queue(hctx);
935}
936
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600937static void blk_mq_delay_work_fn(struct work_struct *work)
938{
939 struct blk_mq_hw_ctx *hctx;
940
941 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
942
943 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
944 __blk_mq_run_hw_queue(hctx);
945}
946
947void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
948{
949 unsigned long tmo = msecs_to_jiffies(msecs);
950
951 if (hctx->queue->nr_hw_queues == 1)
952 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
953 else {
954 unsigned int cpu;
955
Jens Axboe506e9312014-05-07 10:26:44 -0600956 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600957 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
958 }
959}
960EXPORT_SYMBOL(blk_mq_delay_queue);
961
Jens Axboe320ae512013-10-24 09:20:05 +0100962static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800963 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100964{
965 struct blk_mq_ctx *ctx = rq->mq_ctx;
966
Jens Axboe01b983c2013-11-19 18:59:10 -0700967 trace_block_rq_insert(hctx->queue, rq);
968
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800969 if (at_head)
970 list_add(&rq->queuelist, &ctx->rq_list);
971 else
972 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600973
Jens Axboe320ae512013-10-24 09:20:05 +0100974 blk_mq_hctx_mark_pending(hctx, ctx);
975
976 /*
977 * We do this early, to ensure we are on the right CPU.
978 */
Jens Axboe87ee7b12014-04-24 08:51:47 -0600979 blk_add_timer(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100980}
981
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600982void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
983 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100984{
985 struct request_queue *q = rq->q;
986 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600987 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100988
989 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600990 if (!cpu_online(ctx->cpu))
991 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100992
Jens Axboe320ae512013-10-24 09:20:05 +0100993 hctx = q->mq_ops->map_queue(q, ctx->cpu);
994
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600995 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
996 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
997 blk_insert_flush(rq);
998 } else {
999 spin_lock(&ctx->lock);
1000 __blk_mq_insert_request(hctx, rq, at_head);
1001 spin_unlock(&ctx->lock);
1002 }
Jens Axboe320ae512013-10-24 09:20:05 +01001003
Jens Axboe320ae512013-10-24 09:20:05 +01001004 if (run_queue)
1005 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -06001006
1007 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001008}
1009
1010static void blk_mq_insert_requests(struct request_queue *q,
1011 struct blk_mq_ctx *ctx,
1012 struct list_head *list,
1013 int depth,
1014 bool from_schedule)
1015
1016{
1017 struct blk_mq_hw_ctx *hctx;
1018 struct blk_mq_ctx *current_ctx;
1019
1020 trace_block_unplug(q, depth, !from_schedule);
1021
1022 current_ctx = blk_mq_get_ctx(q);
1023
1024 if (!cpu_online(ctx->cpu))
1025 ctx = current_ctx;
1026 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1027
1028 /*
1029 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1030 * offline now
1031 */
1032 spin_lock(&ctx->lock);
1033 while (!list_empty(list)) {
1034 struct request *rq;
1035
1036 rq = list_first_entry(list, struct request, queuelist);
1037 list_del_init(&rq->queuelist);
1038 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001039 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001040 }
1041 spin_unlock(&ctx->lock);
1042
Jens Axboe320ae512013-10-24 09:20:05 +01001043 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001044 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001045}
1046
1047static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1048{
1049 struct request *rqa = container_of(a, struct request, queuelist);
1050 struct request *rqb = container_of(b, struct request, queuelist);
1051
1052 return !(rqa->mq_ctx < rqb->mq_ctx ||
1053 (rqa->mq_ctx == rqb->mq_ctx &&
1054 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1055}
1056
1057void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1058{
1059 struct blk_mq_ctx *this_ctx;
1060 struct request_queue *this_q;
1061 struct request *rq;
1062 LIST_HEAD(list);
1063 LIST_HEAD(ctx_list);
1064 unsigned int depth;
1065
1066 list_splice_init(&plug->mq_list, &list);
1067
1068 list_sort(NULL, &list, plug_ctx_cmp);
1069
1070 this_q = NULL;
1071 this_ctx = NULL;
1072 depth = 0;
1073
1074 while (!list_empty(&list)) {
1075 rq = list_entry_rq(list.next);
1076 list_del_init(&rq->queuelist);
1077 BUG_ON(!rq->q);
1078 if (rq->mq_ctx != this_ctx) {
1079 if (this_ctx) {
1080 blk_mq_insert_requests(this_q, this_ctx,
1081 &ctx_list, depth,
1082 from_schedule);
1083 }
1084
1085 this_ctx = rq->mq_ctx;
1086 this_q = rq->q;
1087 depth = 0;
1088 }
1089
1090 depth++;
1091 list_add_tail(&rq->queuelist, &ctx_list);
1092 }
1093
1094 /*
1095 * If 'this_ctx' is set, we know we have entries to complete
1096 * on 'ctx_list'. Do those.
1097 */
1098 if (this_ctx) {
1099 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1100 from_schedule);
1101 }
1102}
1103
1104static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1105{
1106 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001107
Jens Axboe3ee32372014-06-09 09:36:53 -06001108 if (blk_do_io_stat(rq))
Jens Axboe4b570522014-05-29 11:00:11 -06001109 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001110}
1111
Jens Axboe07068d52014-05-22 10:40:51 -06001112static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1113 struct blk_mq_ctx *ctx,
1114 struct request *rq, struct bio *bio)
1115{
1116 struct request_queue *q = hctx->queue;
1117
1118 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1119 blk_mq_bio_to_request(rq, bio);
1120 spin_lock(&ctx->lock);
1121insert_rq:
1122 __blk_mq_insert_request(hctx, rq, false);
1123 spin_unlock(&ctx->lock);
1124 return false;
1125 } else {
1126 spin_lock(&ctx->lock);
1127 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1128 blk_mq_bio_to_request(rq, bio);
1129 goto insert_rq;
1130 }
1131
1132 spin_unlock(&ctx->lock);
1133 __blk_mq_free_request(hctx, ctx, rq);
1134 return true;
1135 }
1136}
1137
1138struct blk_map_ctx {
1139 struct blk_mq_hw_ctx *hctx;
1140 struct blk_mq_ctx *ctx;
1141};
1142
1143static struct request *blk_mq_map_request(struct request_queue *q,
1144 struct bio *bio,
1145 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001146{
1147 struct blk_mq_hw_ctx *hctx;
1148 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001149 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001150 int rw = bio_data_dir(bio);
Ming Leicb96a42c2014-06-01 00:43:37 +08001151 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001152
Jens Axboe07068d52014-05-22 10:40:51 -06001153 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001154 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001155 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001156 }
1157
1158 ctx = blk_mq_get_ctx(q);
1159 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1160
Jens Axboe07068d52014-05-22 10:40:51 -06001161 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001162 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001163
Jens Axboe320ae512013-10-24 09:20:05 +01001164 trace_block_getrq(q, bio, rw);
Ming Leicb96a42c2014-06-01 00:43:37 +08001165 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1166 hctx);
1167 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001168 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001169 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001170 blk_mq_put_ctx(ctx);
1171 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001172
1173 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001174 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +08001175 blk_mq_set_alloc_data(&alloc_data, q,
1176 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1177 rq = __blk_mq_alloc_request(&alloc_data, rw);
1178 ctx = alloc_data.ctx;
1179 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001180 }
1181
1182 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001183 data->hctx = hctx;
1184 data->ctx = ctx;
1185 return rq;
1186}
1187
1188/*
1189 * Multiple hardware queue variant. This will not use per-process plugs,
1190 * but will attempt to bypass the hctx queueing if we can go straight to
1191 * hardware for SYNC IO.
1192 */
1193static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1194{
1195 const int is_sync = rw_is_sync(bio->bi_rw);
1196 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1197 struct blk_map_ctx data;
1198 struct request *rq;
1199
1200 blk_queue_bounce(q, &bio);
1201
1202 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1203 bio_endio(bio, -EIO);
1204 return;
1205 }
1206
1207 rq = blk_mq_map_request(q, bio, &data);
1208 if (unlikely(!rq))
1209 return;
1210
1211 if (unlikely(is_flush_fua)) {
1212 blk_mq_bio_to_request(rq, bio);
1213 blk_insert_flush(rq);
1214 goto run_queue;
1215 }
1216
1217 if (is_sync) {
1218 int ret;
1219
1220 blk_mq_bio_to_request(rq, bio);
1221 blk_mq_start_request(rq, true);
Jens Axboefeff6892014-05-30 15:42:56 -06001222 blk_add_timer(rq);
Jens Axboe07068d52014-05-22 10:40:51 -06001223
1224 /*
1225 * For OK queue, we are done. For error, kill it. Any other
1226 * error (busy), just add it to our list as we previously
1227 * would have done
1228 */
1229 ret = q->mq_ops->queue_rq(data.hctx, rq);
1230 if (ret == BLK_MQ_RQ_QUEUE_OK)
1231 goto done;
1232 else {
1233 __blk_mq_requeue_request(rq);
1234
1235 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1236 rq->errors = -EIO;
1237 blk_mq_end_io(rq, rq->errors);
1238 goto done;
1239 }
1240 }
1241 }
1242
1243 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1244 /*
1245 * For a SYNC request, send it to the hardware immediately. For
1246 * an ASYNC request, just ensure that we run it later on. The
1247 * latter allows for merging opportunities and more efficient
1248 * dispatching.
1249 */
1250run_queue:
1251 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1252 }
1253done:
1254 blk_mq_put_ctx(data.ctx);
1255}
1256
1257/*
1258 * Single hardware queue variant. This will attempt to use any per-process
1259 * plug for merging and IO deferral.
1260 */
1261static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1262{
1263 const int is_sync = rw_is_sync(bio->bi_rw);
1264 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1265 unsigned int use_plug, request_count = 0;
1266 struct blk_map_ctx data;
1267 struct request *rq;
1268
1269 /*
1270 * If we have multiple hardware queues, just go directly to
1271 * one of those for sync IO.
1272 */
1273 use_plug = !is_flush_fua && !is_sync;
1274
1275 blk_queue_bounce(q, &bio);
1276
1277 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1278 bio_endio(bio, -EIO);
1279 return;
1280 }
1281
1282 if (use_plug && !blk_queue_nomerges(q) &&
1283 blk_attempt_plug_merge(q, bio, &request_count))
1284 return;
1285
1286 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001287 if (unlikely(!rq))
1288 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001289
1290 if (unlikely(is_flush_fua)) {
1291 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001292 blk_insert_flush(rq);
1293 goto run_queue;
1294 }
1295
1296 /*
1297 * A task plug currently exists. Since this is completely lockless,
1298 * utilize that to temporarily store requests until the task is
1299 * either done or scheduled away.
1300 */
1301 if (use_plug) {
1302 struct blk_plug *plug = current->plug;
1303
1304 if (plug) {
1305 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001306 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001307 trace_block_plug(q);
1308 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1309 blk_flush_plug_list(plug, false);
1310 trace_block_plug(q);
1311 }
1312 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001313 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001314 return;
1315 }
1316 }
1317
Jens Axboe07068d52014-05-22 10:40:51 -06001318 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1319 /*
1320 * For a SYNC request, send it to the hardware immediately. For
1321 * an ASYNC request, just ensure that we run it later on. The
1322 * latter allows for merging opportunities and more efficient
1323 * dispatching.
1324 */
1325run_queue:
1326 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001327 }
1328
Jens Axboe07068d52014-05-22 10:40:51 -06001329 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001330}
1331
1332/*
1333 * Default mapping to a software queue, since we use one per CPU.
1334 */
1335struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1336{
1337 return q->queue_hw_ctx[q->mq_map[cpu]];
1338}
1339EXPORT_SYMBOL(blk_mq_map_queue);
1340
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001341static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1342 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001343{
1344 struct page *page;
1345
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001346 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001347 int i;
1348
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001349 for (i = 0; i < tags->nr_tags; i++) {
1350 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001351 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001352 set->ops->exit_request(set->driver_data, tags->rqs[i],
1353 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001354 }
1355 }
1356
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001357 while (!list_empty(&tags->page_list)) {
1358 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001359 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001360 __free_pages(page, page->private);
1361 }
1362
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001363 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001364
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001365 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001366}
1367
1368static size_t order_to_size(unsigned int order)
1369{
Ming Lei4ca08502014-04-19 18:00:18 +08001370 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001371}
1372
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001373static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1374 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001375{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001376 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001377 unsigned int i, j, entries_per_page, max_order = 4;
1378 size_t rq_size, left;
1379
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001380 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1381 set->numa_node);
1382 if (!tags)
1383 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001384
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001385 INIT_LIST_HEAD(&tags->page_list);
1386
1387 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1388 GFP_KERNEL, set->numa_node);
1389 if (!tags->rqs) {
1390 blk_mq_free_tags(tags);
1391 return NULL;
1392 }
Jens Axboe320ae512013-10-24 09:20:05 +01001393
1394 /*
1395 * rq_size is the size of the request plus driver payload, rounded
1396 * to the cacheline size
1397 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001398 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001399 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001400 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001401
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001402 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001403 int this_order = max_order;
1404 struct page *page;
1405 int to_do;
1406 void *p;
1407
1408 while (left < order_to_size(this_order - 1) && this_order)
1409 this_order--;
1410
1411 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001412 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1413 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001414 if (page)
1415 break;
1416 if (!this_order--)
1417 break;
1418 if (order_to_size(this_order) < rq_size)
1419 break;
1420 } while (1);
1421
1422 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001423 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001424
1425 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001426 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001427
1428 p = page_address(page);
1429 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001430 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001431 left -= to_do * rq_size;
1432 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001433 tags->rqs[i] = p;
1434 if (set->ops->init_request) {
1435 if (set->ops->init_request(set->driver_data,
1436 tags->rqs[i], hctx_idx, i,
1437 set->numa_node))
1438 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001439 }
1440
Jens Axboe320ae512013-10-24 09:20:05 +01001441 p += rq_size;
1442 i++;
1443 }
1444 }
1445
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001446 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001447
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001448fail:
1449 pr_warn("%s: failed to allocate requests\n", __func__);
1450 blk_mq_free_rq_map(set, tags, hctx_idx);
1451 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001452}
1453
Jens Axboe1429d7c2014-05-19 09:23:55 -06001454static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1455{
1456 kfree(bitmap->map);
1457}
1458
1459static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1460{
1461 unsigned int bpw = 8, total, num_maps, i;
1462
1463 bitmap->bits_per_word = bpw;
1464
1465 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1466 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1467 GFP_KERNEL, node);
1468 if (!bitmap->map)
1469 return -ENOMEM;
1470
1471 bitmap->map_size = num_maps;
1472
1473 total = nr_cpu_ids;
1474 for (i = 0; i < num_maps; i++) {
1475 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1476 total -= bitmap->map[i].depth;
1477 }
1478
1479 return 0;
1480}
1481
Jens Axboe484b4062014-05-21 14:01:15 -06001482static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1483{
1484 struct request_queue *q = hctx->queue;
1485 struct blk_mq_ctx *ctx;
1486 LIST_HEAD(tmp);
1487
1488 /*
1489 * Move ctx entries to new CPU, if this one is going away.
1490 */
1491 ctx = __blk_mq_get_ctx(q, cpu);
1492
1493 spin_lock(&ctx->lock);
1494 if (!list_empty(&ctx->rq_list)) {
1495 list_splice_init(&ctx->rq_list, &tmp);
1496 blk_mq_hctx_clear_pending(hctx, ctx);
1497 }
1498 spin_unlock(&ctx->lock);
1499
1500 if (list_empty(&tmp))
1501 return NOTIFY_OK;
1502
1503 ctx = blk_mq_get_ctx(q);
1504 spin_lock(&ctx->lock);
1505
1506 while (!list_empty(&tmp)) {
1507 struct request *rq;
1508
1509 rq = list_first_entry(&tmp, struct request, queuelist);
1510 rq->mq_ctx = ctx;
1511 list_move_tail(&rq->queuelist, &ctx->rq_list);
1512 }
1513
1514 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1515 blk_mq_hctx_mark_pending(hctx, ctx);
1516
1517 spin_unlock(&ctx->lock);
1518
1519 blk_mq_run_hw_queue(hctx, true);
1520 blk_mq_put_ctx(ctx);
1521 return NOTIFY_OK;
1522}
1523
1524static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1525{
1526 struct request_queue *q = hctx->queue;
1527 struct blk_mq_tag_set *set = q->tag_set;
1528
1529 if (set->tags[hctx->queue_num])
1530 return NOTIFY_OK;
1531
1532 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1533 if (!set->tags[hctx->queue_num])
1534 return NOTIFY_STOP;
1535
1536 hctx->tags = set->tags[hctx->queue_num];
1537 return NOTIFY_OK;
1538}
1539
1540static int blk_mq_hctx_notify(void *data, unsigned long action,
1541 unsigned int cpu)
1542{
1543 struct blk_mq_hw_ctx *hctx = data;
1544
1545 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1546 return blk_mq_hctx_cpu_offline(hctx, cpu);
1547 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1548 return blk_mq_hctx_cpu_online(hctx, cpu);
1549
1550 return NOTIFY_OK;
1551}
1552
Ming Lei624dbe42014-05-27 23:35:13 +08001553static void blk_mq_exit_hw_queues(struct request_queue *q,
1554 struct blk_mq_tag_set *set, int nr_queue)
1555{
1556 struct blk_mq_hw_ctx *hctx;
1557 unsigned int i;
1558
1559 queue_for_each_hw_ctx(q, hctx, i) {
1560 if (i == nr_queue)
1561 break;
1562
Jens Axboef899fed2014-06-04 09:11:53 -06001563 blk_mq_tag_idle(hctx);
1564
Ming Lei624dbe42014-05-27 23:35:13 +08001565 if (set->ops->exit_hctx)
1566 set->ops->exit_hctx(hctx, i);
1567
1568 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1569 kfree(hctx->ctxs);
1570 blk_mq_free_bitmap(&hctx->ctx_map);
1571 }
1572
1573}
1574
1575static void blk_mq_free_hw_queues(struct request_queue *q,
1576 struct blk_mq_tag_set *set)
1577{
1578 struct blk_mq_hw_ctx *hctx;
1579 unsigned int i;
1580
1581 queue_for_each_hw_ctx(q, hctx, i) {
1582 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001583 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001584 }
1585}
1586
Jens Axboe320ae512013-10-24 09:20:05 +01001587static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001588 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001589{
1590 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001591 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001592
1593 /*
1594 * Initialize hardware queues
1595 */
1596 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001597 int node;
1598
1599 node = hctx->numa_node;
1600 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001601 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001602
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001603 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1604 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001605 spin_lock_init(&hctx->lock);
1606 INIT_LIST_HEAD(&hctx->dispatch);
1607 hctx->queue = q;
1608 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001609 hctx->flags = set->flags;
1610 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001611
1612 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1613 blk_mq_hctx_notify, hctx);
1614 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1615
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001616 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001617
1618 /*
1619 * Allocate space for all possible cpus to avoid allocation in
1620 * runtime
1621 */
1622 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1623 GFP_KERNEL, node);
1624 if (!hctx->ctxs)
1625 break;
1626
Jens Axboe1429d7c2014-05-19 09:23:55 -06001627 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001628 break;
1629
Jens Axboe320ae512013-10-24 09:20:05 +01001630 hctx->nr_ctx = 0;
1631
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001632 if (set->ops->init_hctx &&
1633 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001634 break;
1635 }
1636
1637 if (i == q->nr_hw_queues)
1638 return 0;
1639
1640 /*
1641 * Init failed
1642 */
Ming Lei624dbe42014-05-27 23:35:13 +08001643 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001644
1645 return 1;
1646}
1647
1648static void blk_mq_init_cpu_queues(struct request_queue *q,
1649 unsigned int nr_hw_queues)
1650{
1651 unsigned int i;
1652
1653 for_each_possible_cpu(i) {
1654 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1655 struct blk_mq_hw_ctx *hctx;
1656
1657 memset(__ctx, 0, sizeof(*__ctx));
1658 __ctx->cpu = i;
1659 spin_lock_init(&__ctx->lock);
1660 INIT_LIST_HEAD(&__ctx->rq_list);
1661 __ctx->queue = q;
1662
1663 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001664 if (!cpu_online(i))
1665 continue;
1666
Jens Axboee4043dc2014-04-09 10:18:23 -06001667 hctx = q->mq_ops->map_queue(q, i);
1668 cpumask_set_cpu(i, hctx->cpumask);
1669 hctx->nr_ctx++;
1670
Jens Axboe320ae512013-10-24 09:20:05 +01001671 /*
1672 * Set local node, IFF we have more than one hw queue. If
1673 * not, we remain on the home node of the device
1674 */
1675 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1676 hctx->numa_node = cpu_to_node(i);
1677 }
1678}
1679
1680static void blk_mq_map_swqueue(struct request_queue *q)
1681{
1682 unsigned int i;
1683 struct blk_mq_hw_ctx *hctx;
1684 struct blk_mq_ctx *ctx;
1685
1686 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001687 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001688 hctx->nr_ctx = 0;
1689 }
1690
1691 /*
1692 * Map software to hardware queues
1693 */
1694 queue_for_each_ctx(q, ctx, i) {
1695 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001696 if (!cpu_online(i))
1697 continue;
1698
Jens Axboe320ae512013-10-24 09:20:05 +01001699 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001700 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001701 ctx->index_hw = hctx->nr_ctx;
1702 hctx->ctxs[hctx->nr_ctx++] = ctx;
1703 }
Jens Axboe506e9312014-05-07 10:26:44 -06001704
1705 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001706 /*
1707 * If not software queues are mapped to this hardware queue,
1708 * disable it and free the request entries
1709 */
1710 if (!hctx->nr_ctx) {
1711 struct blk_mq_tag_set *set = q->tag_set;
1712
1713 if (set->tags[i]) {
1714 blk_mq_free_rq_map(set, set->tags[i], i);
1715 set->tags[i] = NULL;
1716 hctx->tags = NULL;
1717 }
1718 continue;
1719 }
1720
1721 /*
1722 * Initialize batch roundrobin counts
1723 */
Jens Axboe506e9312014-05-07 10:26:44 -06001724 hctx->next_cpu = cpumask_first(hctx->cpumask);
1725 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1726 }
Jens Axboe320ae512013-10-24 09:20:05 +01001727}
1728
Jens Axboe0d2602c2014-05-13 15:10:52 -06001729static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1730{
1731 struct blk_mq_hw_ctx *hctx;
1732 struct request_queue *q;
1733 bool shared;
1734 int i;
1735
1736 if (set->tag_list.next == set->tag_list.prev)
1737 shared = false;
1738 else
1739 shared = true;
1740
1741 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1742 blk_mq_freeze_queue(q);
1743
1744 queue_for_each_hw_ctx(q, hctx, i) {
1745 if (shared)
1746 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1747 else
1748 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1749 }
1750 blk_mq_unfreeze_queue(q);
1751 }
1752}
1753
1754static void blk_mq_del_queue_tag_set(struct request_queue *q)
1755{
1756 struct blk_mq_tag_set *set = q->tag_set;
1757
1758 blk_mq_freeze_queue(q);
1759
1760 mutex_lock(&set->tag_list_lock);
1761 list_del_init(&q->tag_set_list);
1762 blk_mq_update_tag_set_depth(set);
1763 mutex_unlock(&set->tag_list_lock);
1764
1765 blk_mq_unfreeze_queue(q);
1766}
1767
1768static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1769 struct request_queue *q)
1770{
1771 q->tag_set = set;
1772
1773 mutex_lock(&set->tag_list_lock);
1774 list_add_tail(&q->tag_set_list, &set->tag_list);
1775 blk_mq_update_tag_set_depth(set);
1776 mutex_unlock(&set->tag_list_lock);
1777}
1778
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001779struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001780{
1781 struct blk_mq_hw_ctx **hctxs;
Ming Leie6cdb092014-06-03 11:24:06 +08001782 struct blk_mq_ctx __percpu *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001783 struct request_queue *q;
Jens Axboef14bbe72014-05-27 12:06:53 -06001784 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001785 int i;
1786
Jens Axboe320ae512013-10-24 09:20:05 +01001787 ctx = alloc_percpu(struct blk_mq_ctx);
1788 if (!ctx)
1789 return ERR_PTR(-ENOMEM);
1790
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001791 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1792 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001793
1794 if (!hctxs)
1795 goto err_percpu;
1796
Jens Axboef14bbe72014-05-27 12:06:53 -06001797 map = blk_mq_make_queue_map(set);
1798 if (!map)
1799 goto err_map;
1800
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001801 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001802 int node = blk_mq_hw_queue_to_node(map, i);
1803
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001804 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1805 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001806 if (!hctxs[i])
1807 goto err_hctxs;
1808
Jens Axboee4043dc2014-04-09 10:18:23 -06001809 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1810 goto err_hctxs;
1811
Jens Axboe0d2602c2014-05-13 15:10:52 -06001812 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001813 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001814 hctxs[i]->queue_num = i;
1815 }
1816
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001817 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001818 if (!q)
1819 goto err_hctxs;
1820
Ming Lei3d2936f2014-05-27 23:35:14 +08001821 if (percpu_counter_init(&q->mq_usage_counter, 0))
1822 goto err_map;
1823
Jens Axboe320ae512013-10-24 09:20:05 +01001824 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1825 blk_queue_rq_timeout(q, 30000);
1826
1827 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001828 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001829 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001830
1831 q->queue_ctx = ctx;
1832 q->queue_hw_ctx = hctxs;
1833
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001834 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001835 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001836
Jens Axboe05f1dd52014-05-29 09:53:32 -06001837 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1838 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1839
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001840 q->sg_reserved_size = INT_MAX;
1841
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001842 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1843 INIT_LIST_HEAD(&q->requeue_list);
1844 spin_lock_init(&q->requeue_lock);
1845
Jens Axboe07068d52014-05-22 10:40:51 -06001846 if (q->nr_hw_queues > 1)
1847 blk_queue_make_request(q, blk_mq_make_request);
1848 else
1849 blk_queue_make_request(q, blk_sq_make_request);
1850
Jens Axboe87ee7b12014-04-24 08:51:47 -06001851 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001852 if (set->timeout)
1853 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001854
Jens Axboeeba71762014-05-20 15:17:27 -06001855 /*
1856 * Do this after blk_queue_make_request() overrides it...
1857 */
1858 q->nr_requests = set->queue_depth;
1859
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001860 if (set->ops->complete)
1861 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001862
Jens Axboe320ae512013-10-24 09:20:05 +01001863 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001864 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001865
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001866 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1867 set->cmd_size, cache_line_size()),
1868 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001869 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001870 goto err_hw;
1871
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001872 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001873 goto err_flush_rq;
1874
Jens Axboe320ae512013-10-24 09:20:05 +01001875 mutex_lock(&all_q_mutex);
1876 list_add_tail(&q->all_q_node, &all_q_list);
1877 mutex_unlock(&all_q_mutex);
1878
Jens Axboe0d2602c2014-05-13 15:10:52 -06001879 blk_mq_add_queue_tag_set(set, q);
1880
Jens Axboe484b4062014-05-21 14:01:15 -06001881 blk_mq_map_swqueue(q);
1882
Jens Axboe320ae512013-10-24 09:20:05 +01001883 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001884
1885err_flush_rq:
1886 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001887err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001888 blk_cleanup_queue(q);
1889err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001890 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001891 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001892 if (!hctxs[i])
1893 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001894 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001895 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001896 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001897err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001898 kfree(hctxs);
1899err_percpu:
1900 free_percpu(ctx);
1901 return ERR_PTR(-ENOMEM);
1902}
1903EXPORT_SYMBOL(blk_mq_init_queue);
1904
1905void blk_mq_free_queue(struct request_queue *q)
1906{
Ming Lei624dbe42014-05-27 23:35:13 +08001907 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001908
Jens Axboe0d2602c2014-05-13 15:10:52 -06001909 blk_mq_del_queue_tag_set(q);
1910
Ming Lei624dbe42014-05-27 23:35:13 +08001911 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1912 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001913
Ming Lei3d2936f2014-05-27 23:35:14 +08001914 percpu_counter_destroy(&q->mq_usage_counter);
1915
Jens Axboe320ae512013-10-24 09:20:05 +01001916 free_percpu(q->queue_ctx);
1917 kfree(q->queue_hw_ctx);
1918 kfree(q->mq_map);
1919
1920 q->queue_ctx = NULL;
1921 q->queue_hw_ctx = NULL;
1922 q->mq_map = NULL;
1923
1924 mutex_lock(&all_q_mutex);
1925 list_del_init(&q->all_q_node);
1926 mutex_unlock(&all_q_mutex);
1927}
Jens Axboe320ae512013-10-24 09:20:05 +01001928
1929/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001930static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001931{
1932 blk_mq_freeze_queue(q);
1933
Jens Axboe67aec142014-05-30 08:25:36 -06001934 blk_mq_sysfs_unregister(q);
1935
Jens Axboe320ae512013-10-24 09:20:05 +01001936 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1937
1938 /*
1939 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1940 * we should change hctx numa_node according to new topology (this
1941 * involves free and re-allocate memory, worthy doing?)
1942 */
1943
1944 blk_mq_map_swqueue(q);
1945
Jens Axboe67aec142014-05-30 08:25:36 -06001946 blk_mq_sysfs_register(q);
1947
Jens Axboe320ae512013-10-24 09:20:05 +01001948 blk_mq_unfreeze_queue(q);
1949}
1950
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001951static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1952 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001953{
1954 struct request_queue *q;
1955
1956 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001957 * Before new mappings are established, hotadded cpu might already
1958 * start handling requests. This doesn't break anything as we map
1959 * offline CPUs to first hardware queue. We will re-init the queue
1960 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001961 */
1962 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1963 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1964 return NOTIFY_OK;
1965
1966 mutex_lock(&all_q_mutex);
1967 list_for_each_entry(q, &all_q_list, all_q_node)
1968 blk_mq_queue_reinit(q);
1969 mutex_unlock(&all_q_mutex);
1970 return NOTIFY_OK;
1971}
1972
Jens Axboea4391c62014-06-05 15:21:56 -06001973/*
1974 * Alloc a tag set to be associated with one or more request queues.
1975 * May fail with EINVAL for various error conditions. May adjust the
1976 * requested depth down, if if it too large. In that case, the set
1977 * value will be stored in set->queue_depth.
1978 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001979int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1980{
1981 int i;
1982
1983 if (!set->nr_hw_queues)
1984 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06001985 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001986 return -EINVAL;
1987 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1988 return -EINVAL;
1989
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001990 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001991 return -EINVAL;
1992
Jens Axboea4391c62014-06-05 15:21:56 -06001993 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
1994 pr_info("blk-mq: reduced tag depth to %u\n",
1995 BLK_MQ_MAX_DEPTH);
1996 set->queue_depth = BLK_MQ_MAX_DEPTH;
1997 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001998
Ming Lei484790052014-04-19 18:00:17 +08001999 set->tags = kmalloc_node(set->nr_hw_queues *
2000 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002001 GFP_KERNEL, set->numa_node);
2002 if (!set->tags)
2003 goto out;
2004
2005 for (i = 0; i < set->nr_hw_queues; i++) {
2006 set->tags[i] = blk_mq_init_rq_map(set, i);
2007 if (!set->tags[i])
2008 goto out_unwind;
2009 }
2010
Jens Axboe0d2602c2014-05-13 15:10:52 -06002011 mutex_init(&set->tag_list_lock);
2012 INIT_LIST_HEAD(&set->tag_list);
2013
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002014 return 0;
2015
2016out_unwind:
2017 while (--i >= 0)
2018 blk_mq_free_rq_map(set, set->tags[i], i);
2019out:
2020 return -ENOMEM;
2021}
2022EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2023
2024void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2025{
2026 int i;
2027
Jens Axboe484b4062014-05-21 14:01:15 -06002028 for (i = 0; i < set->nr_hw_queues; i++) {
2029 if (set->tags[i])
2030 blk_mq_free_rq_map(set, set->tags[i], i);
2031 }
2032
Ming Lei981bd182014-04-24 00:07:34 +08002033 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002034}
2035EXPORT_SYMBOL(blk_mq_free_tag_set);
2036
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002037int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2038{
2039 struct blk_mq_tag_set *set = q->tag_set;
2040 struct blk_mq_hw_ctx *hctx;
2041 int i, ret;
2042
2043 if (!set || nr > set->queue_depth)
2044 return -EINVAL;
2045
2046 ret = 0;
2047 queue_for_each_hw_ctx(q, hctx, i) {
2048 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2049 if (ret)
2050 break;
2051 }
2052
2053 if (!ret)
2054 q->nr_requests = nr;
2055
2056 return ret;
2057}
2058
Jens Axboe676141e2014-03-20 13:29:18 -06002059void blk_mq_disable_hotplug(void)
2060{
2061 mutex_lock(&all_q_mutex);
2062}
2063
2064void blk_mq_enable_hotplug(void)
2065{
2066 mutex_unlock(&all_q_mutex);
2067}
2068
Jens Axboe320ae512013-10-24 09:20:05 +01002069static int __init blk_mq_init(void)
2070{
Jens Axboe320ae512013-10-24 09:20:05 +01002071 blk_mq_cpu_init();
2072
2073 /* Must be called after percpu_counter_hotcpu_callback() */
2074 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2075
2076 return 0;
2077}
2078subsys_initcall(blk_mq_init);