blob: 22682fb4be6552886cb491f0ee687e7ab7ad6359 [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);
Tejun Heo531ed622014-06-18 11:21:08 -040084 smp_mb();
Keith Busch3b632cf2014-06-06 10:22:07 -060085
86 /* we have problems freezing the queue if it's initializing */
Tejun Heo780db202014-07-01 10:31:13 -060087 if (!q->mq_freeze_depth)
Jens Axboe320ae512013-10-24 09:20:05 +010088 return 0;
89
90 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
91
92 spin_lock_irq(q->queue_lock);
93 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Tejun Heo780db202014-07-01 10:31:13 -060094 !q->mq_freeze_depth || blk_queue_dying(q),
Ming Lei43a5e4e2013-12-26 21:31:35 +080095 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +010096 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +080097 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +010098 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +080099 else if (blk_queue_dying(q))
100 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100101 spin_unlock_irq(q->queue_lock);
102
103 return ret;
104}
105
106static void blk_mq_queue_exit(struct request_queue *q)
107{
108 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
109}
110
Tejun Heo72d6f022014-07-01 10:33:02 -0600111/*
112 * Guarantee no request is in use, so we can change any data structure of
113 * the queue afterward.
114 */
115void blk_mq_freeze_queue(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800116{
Tejun Heo72d6f022014-07-01 10:33:02 -0600117 spin_lock_irq(q->queue_lock);
118 q->mq_freeze_depth++;
119 spin_unlock_irq(q->queue_lock);
120
Ming Lei43a5e4e2013-12-26 21:31:35 +0800121 while (true) {
122 s64 count;
123
124 spin_lock_irq(q->queue_lock);
125 count = percpu_counter_sum(&q->mq_usage_counter);
126 spin_unlock_irq(q->queue_lock);
127
128 if (count == 0)
129 break;
Christoph Hellwig8f5280f2014-06-13 19:43:04 +0200130 blk_mq_start_hw_queues(q);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800131 msleep(10);
132 }
133}
134
Jens Axboe320ae512013-10-24 09:20:05 +0100135static void blk_mq_unfreeze_queue(struct request_queue *q)
136{
137 bool wake = false;
138
139 spin_lock_irq(q->queue_lock);
Tejun Heo780db202014-07-01 10:31:13 -0600140 wake = !--q->mq_freeze_depth;
141 WARN_ON_ONCE(q->mq_freeze_depth < 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100142 spin_unlock_irq(q->queue_lock);
143 if (wake)
144 wake_up_all(&q->mq_freeze_wq);
145}
146
147bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
148{
149 return blk_mq_has_free_tags(hctx->tags);
150}
151EXPORT_SYMBOL(blk_mq_can_queue);
152
Jens Axboe94eddfb2013-11-19 09:25:07 -0700153static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
154 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100155{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700156 if (blk_queue_io_stat(q))
157 rw_flags |= REQ_IO_STAT;
158
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200159 INIT_LIST_HEAD(&rq->queuelist);
160 /* csd/requeue_work/fifo_time is initialized before use */
161 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100162 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600163 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200164 /* do not touch atomic flags, it needs atomic ops against the timer */
165 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200166 INIT_HLIST_NODE(&rq->hash);
167 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200168 rq->rq_disk = NULL;
169 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600170 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200171#ifdef CONFIG_BLK_CGROUP
172 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700173 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200174 rq->io_start_time_ns = 0;
175#endif
176 rq->nr_phys_segments = 0;
177#if defined(CONFIG_BLK_DEV_INTEGRITY)
178 rq->nr_integrity_segments = 0;
179#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200180 rq->special = NULL;
181 /* tag was already set */
182 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200183
184 rq->extra_len = 0;
185 rq->sense_len = 0;
186 rq->resid_len = 0;
187 rq->sense = NULL;
188
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200189 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600190 rq->timeout = 0;
191
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200192 rq->end_io = NULL;
193 rq->end_io_data = NULL;
194 rq->next_rq = NULL;
195
Jens Axboe320ae512013-10-24 09:20:05 +0100196 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
197}
198
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200199static struct request *
Ming Leicb96a42c2014-06-01 00:43:37 +0800200__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200201{
202 struct request *rq;
203 unsigned int tag;
204
Ming Leicb96a42c2014-06-01 00:43:37 +0800205 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200206 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a42c2014-06-01 00:43:37 +0800207 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200208
209 rq->cmd_flags = 0;
Ming Leicb96a42c2014-06-01 00:43:37 +0800210 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200211 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a42c2014-06-01 00:43:37 +0800212 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200213 }
214
215 rq->tag = tag;
Ming Leicb96a42c2014-06-01 00:43:37 +0800216 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200217 return rq;
218 }
219
220 return NULL;
221}
222
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200223struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
224 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100225{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200226 struct blk_mq_ctx *ctx;
227 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100228 struct request *rq;
Ming Leicb96a42c2014-06-01 00:43:37 +0800229 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +0100230
231 if (blk_mq_queue_enter(q))
232 return NULL;
233
Christoph Hellwigd8525642014-05-27 20:59:50 +0200234 ctx = blk_mq_get_ctx(q);
235 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +0800236 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
237 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200238
Ming Leicb96a42c2014-06-01 00:43:37 +0800239 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200240 if (!rq && (gfp & __GFP_WAIT)) {
241 __blk_mq_run_hw_queue(hctx);
242 blk_mq_put_ctx(ctx);
243
244 ctx = blk_mq_get_ctx(q);
245 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +0800246 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
247 hctx);
248 rq = __blk_mq_alloc_request(&alloc_data, rw);
249 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200250 }
251 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100252 return rq;
253}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600254EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100255
Jens Axboe320ae512013-10-24 09:20:05 +0100256static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
257 struct blk_mq_ctx *ctx, struct request *rq)
258{
259 const int tag = rq->tag;
260 struct request_queue *q = rq->q;
261
Jens Axboe0d2602c2014-05-13 15:10:52 -0600262 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
263 atomic_dec(&hctx->nr_active);
264
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200265 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600266 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100267 blk_mq_queue_exit(q);
268}
269
270void blk_mq_free_request(struct request *rq)
271{
272 struct blk_mq_ctx *ctx = rq->mq_ctx;
273 struct blk_mq_hw_ctx *hctx;
274 struct request_queue *q = rq->q;
275
276 ctx->rq_completed[rq_is_sync(rq)]++;
277
278 hctx = q->mq_ops->map_queue(q, ctx->cpu);
279 __blk_mq_free_request(hctx, ctx, rq);
280}
281
Christoph Hellwig8727af42014-04-14 10:30:08 +0200282/*
283 * Clone all relevant state from a request that has been put on hold in
284 * the flush state machine into the preallocated flush request that hangs
285 * off the request queue.
286 *
287 * For a driver the flush request should be invisible, that's why we are
288 * impersonating the original request here.
289 */
290void blk_mq_clone_flush_request(struct request *flush_rq,
291 struct request *orig_rq)
292{
293 struct blk_mq_hw_ctx *hctx =
294 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
295
296 flush_rq->mq_ctx = orig_rq->mq_ctx;
297 flush_rq->tag = orig_rq->tag;
298 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
299 hctx->cmd_size);
300}
301
Christoph Hellwig63151a42014-04-16 09:44:52 +0200302inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100303{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700304 blk_account_io_done(rq);
305
Christoph Hellwig91b63632014-04-16 09:44:53 +0200306 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100307 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200308 } else {
309 if (unlikely(blk_bidi_rq(rq)))
310 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100311 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200312 }
Jens Axboe320ae512013-10-24 09:20:05 +0100313}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200314EXPORT_SYMBOL(__blk_mq_end_io);
315
316void blk_mq_end_io(struct request *rq, int error)
317{
318 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
319 BUG();
320 __blk_mq_end_io(rq, error);
321}
322EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100323
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800324static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100325{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800326 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100327
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800328 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100329}
330
Jens Axboeed851862014-05-30 21:20:50 -0600331static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100332{
333 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700334 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100335 int cpu;
336
Christoph Hellwig38535202014-04-25 02:32:53 -0700337 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800338 rq->q->softirq_done_fn(rq);
339 return;
340 }
Jens Axboe320ae512013-10-24 09:20:05 +0100341
342 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700343 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
344 shared = cpus_share_cache(cpu, ctx->cpu);
345
346 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800347 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800348 rq->csd.info = rq;
349 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100350 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800351 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800352 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800353 }
Jens Axboe320ae512013-10-24 09:20:05 +0100354 put_cpu();
355}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800356
Jens Axboeed851862014-05-30 21:20:50 -0600357void __blk_mq_complete_request(struct request *rq)
358{
359 struct request_queue *q = rq->q;
360
361 if (!q->softirq_done_fn)
362 blk_mq_end_io(rq, rq->errors);
363 else
364 blk_mq_ipi_complete_request(rq);
365}
366
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800367/**
368 * blk_mq_complete_request - end I/O on a request
369 * @rq: the request being processed
370 *
371 * Description:
372 * Ends all I/O on a request. It does not handle partial completions.
373 * The actual completion happens out-of-order, through a IPI handler.
374 **/
375void blk_mq_complete_request(struct request *rq)
376{
Jens Axboe95f09682014-05-27 17:46:48 -0600377 struct request_queue *q = rq->q;
378
379 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800380 return;
Jens Axboeed851862014-05-30 21:20:50 -0600381 if (!blk_mark_rq_complete(rq))
382 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800383}
384EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100385
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800386static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100387{
388 struct request_queue *q = rq->q;
389
390 trace_block_rq_issue(q, rq);
391
Christoph Hellwig742ee692014-04-14 10:30:06 +0200392 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200393 if (unlikely(blk_bidi_rq(rq)))
394 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200395
Ming Lei2b8393b2014-06-10 00:16:41 +0800396 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600397
398 /*
399 * Mark us as started and clear complete. Complete might have been
400 * set if requeue raced with timeout, which then marked it as
401 * complete. So be sure to clear complete again when we start
402 * the request, otherwise we'll ignore the completion event.
403 */
Jens Axboe4b570522014-05-29 11:00:11 -0600404 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
405 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
406 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
407 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800408
409 if (q->dma_drain_size && blk_rq_bytes(rq)) {
410 /*
411 * Make sure space for the drain appears. We know we can do
412 * this because max_hw_segments has been adjusted to be one
413 * fewer than the device can handle.
414 */
415 rq->nr_phys_segments++;
416 }
417
418 /*
419 * Flag the last request in the series so that drivers know when IO
420 * should be kicked off, if they don't do it on a per-request basis.
421 *
422 * Note: the flag isn't the only condition drivers should do kick off.
423 * If drive is busy, the last request might not have the bit set.
424 */
425 if (last)
426 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100427}
428
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200429static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100430{
431 struct request_queue *q = rq->q;
432
433 trace_block_rq_requeue(q, rq);
434 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800435
436 rq->cmd_flags &= ~REQ_END;
437
438 if (q->dma_drain_size && blk_rq_bytes(rq))
439 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100440}
441
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200442void blk_mq_requeue_request(struct request *rq)
443{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200444 __blk_mq_requeue_request(rq);
445 blk_clear_rq_complete(rq);
446
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200447 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600448 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200449}
450EXPORT_SYMBOL(blk_mq_requeue_request);
451
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600452static void blk_mq_requeue_work(struct work_struct *work)
453{
454 struct request_queue *q =
455 container_of(work, struct request_queue, requeue_work);
456 LIST_HEAD(rq_list);
457 struct request *rq, *next;
458 unsigned long flags;
459
460 spin_lock_irqsave(&q->requeue_lock, flags);
461 list_splice_init(&q->requeue_list, &rq_list);
462 spin_unlock_irqrestore(&q->requeue_lock, flags);
463
464 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
465 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
466 continue;
467
468 rq->cmd_flags &= ~REQ_SOFTBARRIER;
469 list_del_init(&rq->queuelist);
470 blk_mq_insert_request(rq, true, false, false);
471 }
472
473 while (!list_empty(&rq_list)) {
474 rq = list_entry(rq_list.next, struct request, queuelist);
475 list_del_init(&rq->queuelist);
476 blk_mq_insert_request(rq, false, false, false);
477 }
478
479 blk_mq_run_queues(q, false);
480}
481
482void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
483{
484 struct request_queue *q = rq->q;
485 unsigned long flags;
486
487 /*
488 * We abuse this flag that is otherwise used by the I/O scheduler to
489 * request head insertation from the workqueue.
490 */
491 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
492
493 spin_lock_irqsave(&q->requeue_lock, flags);
494 if (at_head) {
495 rq->cmd_flags |= REQ_SOFTBARRIER;
496 list_add(&rq->queuelist, &q->requeue_list);
497 } else {
498 list_add_tail(&rq->queuelist, &q->requeue_list);
499 }
500 spin_unlock_irqrestore(&q->requeue_lock, flags);
501}
502EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
503
504void blk_mq_kick_requeue_list(struct request_queue *q)
505{
506 kblockd_schedule_work(&q->requeue_work);
507}
508EXPORT_SYMBOL(blk_mq_kick_requeue_list);
509
Jens Axboe0e62f512014-06-04 10:23:49 -0600510static inline bool is_flush_request(struct request *rq, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600511{
Jens Axboe0e62f512014-06-04 10:23:49 -0600512 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
513 rq->q->flush_rq->tag == tag);
514}
Shaohua Li22302372014-05-30 08:06:42 -0600515
Jens Axboe0e62f512014-06-04 10:23:49 -0600516struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
517{
518 struct request *rq = tags->rqs[tag];
Shaohua Li22302372014-05-30 08:06:42 -0600519
Jens Axboe0e62f512014-06-04 10:23:49 -0600520 if (!is_flush_request(rq, tag))
521 return rq;
522
523 return rq->q->flush_rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600524}
525EXPORT_SYMBOL(blk_mq_tag_to_rq);
526
Jens Axboe320ae512013-10-24 09:20:05 +0100527struct blk_mq_timeout_data {
528 struct blk_mq_hw_ctx *hctx;
529 unsigned long *next;
530 unsigned int *next_set;
531};
532
533static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
534{
535 struct blk_mq_timeout_data *data = __data;
536 struct blk_mq_hw_ctx *hctx = data->hctx;
537 unsigned int tag;
538
539 /* It may not be in flight yet (this is where
540 * the REQ_ATOMIC_STARTED flag comes in). The requests are
541 * statically allocated, so we know it's always safe to access the
542 * memory associated with a bit offset into ->rqs[].
543 */
544 tag = 0;
545 do {
546 struct request *rq;
547
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600548 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
549 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100550 break;
551
Jens Axboe0e62f512014-06-04 10:23:49 -0600552 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600553 if (rq->q != hctx->queue)
554 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100555 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
556 continue;
557
558 blk_rq_check_expired(rq, data->next, data->next_set);
559 } while (1);
560}
561
562static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
563 unsigned long *next,
564 unsigned int *next_set)
565{
566 struct blk_mq_timeout_data data = {
567 .hctx = hctx,
568 .next = next,
569 .next_set = next_set,
570 };
571
572 /*
573 * Ask the tagging code to iterate busy requests, so we can
574 * check them for timeout.
575 */
576 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
577}
578
Jens Axboe87ee7b12014-04-24 08:51:47 -0600579static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
580{
581 struct request_queue *q = rq->q;
582
583 /*
584 * We know that complete is set at this point. If STARTED isn't set
585 * anymore, then the request isn't active and the "timeout" should
586 * just be ignored. This can happen due to the bitflag ordering.
587 * Timeout first checks if STARTED is set, and if it is, assumes
588 * the request is active. But if we race with completion, then
589 * we both flags will get cleared. So check here again, and ignore
590 * a timeout event with a request that isn't active.
591 */
592 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
593 return BLK_EH_NOT_HANDLED;
594
595 if (!q->mq_ops->timeout)
596 return BLK_EH_RESET_TIMER;
597
598 return q->mq_ops->timeout(rq);
599}
600
Jens Axboe320ae512013-10-24 09:20:05 +0100601static void blk_mq_rq_timer(unsigned long data)
602{
603 struct request_queue *q = (struct request_queue *) data;
604 struct blk_mq_hw_ctx *hctx;
605 unsigned long next = 0;
606 int i, next_set = 0;
607
Jens Axboe484b4062014-05-21 14:01:15 -0600608 queue_for_each_hw_ctx(q, hctx, i) {
609 /*
610 * If not software queues are currently mapped to this
611 * hardware queue, there's nothing to check
612 */
613 if (!hctx->nr_ctx || !hctx->tags)
614 continue;
615
Jens Axboe320ae512013-10-24 09:20:05 +0100616 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
Jens Axboe484b4062014-05-21 14:01:15 -0600617 }
Jens Axboe320ae512013-10-24 09:20:05 +0100618
Jens Axboe0d2602c2014-05-13 15:10:52 -0600619 if (next_set) {
620 next = blk_rq_timeout(round_jiffies_up(next));
621 mod_timer(&q->timeout, next);
622 } else {
623 queue_for_each_hw_ctx(q, hctx, i)
624 blk_mq_tag_idle(hctx);
625 }
Jens Axboe320ae512013-10-24 09:20:05 +0100626}
627
628/*
629 * Reverse check our software queue for entries that we could potentially
630 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
631 * too much time checking for merges.
632 */
633static bool blk_mq_attempt_merge(struct request_queue *q,
634 struct blk_mq_ctx *ctx, struct bio *bio)
635{
636 struct request *rq;
637 int checked = 8;
638
639 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
640 int el_ret;
641
642 if (!checked--)
643 break;
644
645 if (!blk_rq_merge_ok(rq, bio))
646 continue;
647
648 el_ret = blk_try_merge(rq, bio);
649 if (el_ret == ELEVATOR_BACK_MERGE) {
650 if (bio_attempt_back_merge(q, rq, bio)) {
651 ctx->rq_merged++;
652 return true;
653 }
654 break;
655 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
656 if (bio_attempt_front_merge(q, rq, bio)) {
657 ctx->rq_merged++;
658 return true;
659 }
660 break;
661 }
662 }
663
664 return false;
665}
666
Jens Axboe320ae512013-10-24 09:20:05 +0100667/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600668 * Process software queues that have been marked busy, splicing them
669 * to the for-dispatch
670 */
671static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
672{
673 struct blk_mq_ctx *ctx;
674 int i;
675
676 for (i = 0; i < hctx->ctx_map.map_size; i++) {
677 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
678 unsigned int off, bit;
679
680 if (!bm->word)
681 continue;
682
683 bit = 0;
684 off = i * hctx->ctx_map.bits_per_word;
685 do {
686 bit = find_next_bit(&bm->word, bm->depth, bit);
687 if (bit >= bm->depth)
688 break;
689
690 ctx = hctx->ctxs[bit + off];
691 clear_bit(bit, &bm->word);
692 spin_lock(&ctx->lock);
693 list_splice_tail_init(&ctx->rq_list, list);
694 spin_unlock(&ctx->lock);
695
696 bit++;
697 } while (1);
698 }
699}
700
701/*
Jens Axboe320ae512013-10-24 09:20:05 +0100702 * Run this hardware queue, pulling any software queues mapped to it in.
703 * Note that this function currently has various problems around ordering
704 * of IO. In particular, we'd like FIFO behaviour on handling existing
705 * items on the hctx->dispatch list. Ignore that for now.
706 */
707static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
708{
709 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100710 struct request *rq;
711 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600712 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100713
Jens Axboefd1270d2014-04-16 09:23:48 -0600714 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600715
Jens Axboe5d12f902014-03-19 15:25:02 -0600716 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100717 return;
718
719 hctx->run++;
720
721 /*
722 * Touch any software queue that has pending entries.
723 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600724 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100725
726 /*
727 * If we have previous entries on our dispatch list, grab them
728 * and stuff them at the front for more fair dispatch.
729 */
730 if (!list_empty_careful(&hctx->dispatch)) {
731 spin_lock(&hctx->lock);
732 if (!list_empty(&hctx->dispatch))
733 list_splice_init(&hctx->dispatch, &rq_list);
734 spin_unlock(&hctx->lock);
735 }
736
737 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100738 * Now process all the entries, sending them to the driver.
739 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600740 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100741 while (!list_empty(&rq_list)) {
742 int ret;
743
744 rq = list_first_entry(&rq_list, struct request, queuelist);
745 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100746
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800747 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100748
749 ret = q->mq_ops->queue_rq(hctx, rq);
750 switch (ret) {
751 case BLK_MQ_RQ_QUEUE_OK:
752 queued++;
753 continue;
754 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100755 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200756 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100757 break;
758 default:
759 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100760 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800761 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100762 blk_mq_end_io(rq, rq->errors);
763 break;
764 }
765
766 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
767 break;
768 }
769
770 if (!queued)
771 hctx->dispatched[0]++;
772 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
773 hctx->dispatched[ilog2(queued) + 1]++;
774
775 /*
776 * Any items that need requeuing? Stuff them into hctx->dispatch,
777 * that is where we will continue on next queue run.
778 */
779 if (!list_empty(&rq_list)) {
780 spin_lock(&hctx->lock);
781 list_splice(&rq_list, &hctx->dispatch);
782 spin_unlock(&hctx->lock);
783 }
784}
785
Jens Axboe506e9312014-05-07 10:26:44 -0600786/*
787 * It'd be great if the workqueue API had a way to pass
788 * in a mask and had some smarts for more clever placement.
789 * For now we just round-robin here, switching for every
790 * BLK_MQ_CPU_WORK_BATCH queued items.
791 */
792static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
793{
794 int cpu = hctx->next_cpu;
795
796 if (--hctx->next_cpu_batch <= 0) {
797 int next_cpu;
798
799 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
800 if (next_cpu >= nr_cpu_ids)
801 next_cpu = cpumask_first(hctx->cpumask);
802
803 hctx->next_cpu = next_cpu;
804 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
805 }
806
807 return cpu;
808}
809
Jens Axboe320ae512013-10-24 09:20:05 +0100810void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
811{
Jens Axboe5d12f902014-03-19 15:25:02 -0600812 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100813 return;
814
Jens Axboee4043dc2014-04-09 10:18:23 -0600815 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100816 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600817 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600818 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600819 else {
820 unsigned int cpu;
821
Jens Axboe506e9312014-05-07 10:26:44 -0600822 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600823 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600824 }
Jens Axboe320ae512013-10-24 09:20:05 +0100825}
826
827void blk_mq_run_queues(struct request_queue *q, bool async)
828{
829 struct blk_mq_hw_ctx *hctx;
830 int i;
831
832 queue_for_each_hw_ctx(q, hctx, i) {
833 if ((!blk_mq_hctx_has_pending(hctx) &&
834 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600835 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100836 continue;
837
Jens Axboee4043dc2014-04-09 10:18:23 -0600838 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100839 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600840 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100841 }
842}
843EXPORT_SYMBOL(blk_mq_run_queues);
844
845void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
846{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600847 cancel_delayed_work(&hctx->run_work);
848 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100849 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
850}
851EXPORT_SYMBOL(blk_mq_stop_hw_queue);
852
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100853void blk_mq_stop_hw_queues(struct request_queue *q)
854{
855 struct blk_mq_hw_ctx *hctx;
856 int i;
857
858 queue_for_each_hw_ctx(q, hctx, i)
859 blk_mq_stop_hw_queue(hctx);
860}
861EXPORT_SYMBOL(blk_mq_stop_hw_queues);
862
Jens Axboe320ae512013-10-24 09:20:05 +0100863void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
864{
865 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600866
867 preempt_disable();
Jens Axboe0ffbce82014-06-25 08:22:34 -0600868 blk_mq_run_hw_queue(hctx, false);
Jens Axboee4043dc2014-04-09 10:18:23 -0600869 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100870}
871EXPORT_SYMBOL(blk_mq_start_hw_queue);
872
Christoph Hellwig2f268552014-04-16 09:44:56 +0200873void blk_mq_start_hw_queues(struct request_queue *q)
874{
875 struct blk_mq_hw_ctx *hctx;
876 int i;
877
878 queue_for_each_hw_ctx(q, hctx, i)
879 blk_mq_start_hw_queue(hctx);
880}
881EXPORT_SYMBOL(blk_mq_start_hw_queues);
882
883
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200884void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100885{
886 struct blk_mq_hw_ctx *hctx;
887 int i;
888
889 queue_for_each_hw_ctx(q, hctx, i) {
890 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
891 continue;
892
893 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600894 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200895 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600896 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100897 }
898}
899EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
900
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600901static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100902{
903 struct blk_mq_hw_ctx *hctx;
904
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600905 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600906
Jens Axboe320ae512013-10-24 09:20:05 +0100907 __blk_mq_run_hw_queue(hctx);
908}
909
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600910static void blk_mq_delay_work_fn(struct work_struct *work)
911{
912 struct blk_mq_hw_ctx *hctx;
913
914 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
915
916 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
917 __blk_mq_run_hw_queue(hctx);
918}
919
920void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
921{
922 unsigned long tmo = msecs_to_jiffies(msecs);
923
924 if (hctx->queue->nr_hw_queues == 1)
925 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
926 else {
927 unsigned int cpu;
928
Jens Axboe506e9312014-05-07 10:26:44 -0600929 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600930 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
931 }
932}
933EXPORT_SYMBOL(blk_mq_delay_queue);
934
Jens Axboe320ae512013-10-24 09:20:05 +0100935static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800936 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100937{
938 struct blk_mq_ctx *ctx = rq->mq_ctx;
939
Jens Axboe01b983c2013-11-19 18:59:10 -0700940 trace_block_rq_insert(hctx->queue, rq);
941
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800942 if (at_head)
943 list_add(&rq->queuelist, &ctx->rq_list);
944 else
945 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600946
Jens Axboe320ae512013-10-24 09:20:05 +0100947 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100948}
949
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600950void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
951 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100952{
953 struct request_queue *q = rq->q;
954 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600955 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100956
957 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600958 if (!cpu_online(ctx->cpu))
959 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100960
Jens Axboe320ae512013-10-24 09:20:05 +0100961 hctx = q->mq_ops->map_queue(q, ctx->cpu);
962
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600963 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
964 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
965 blk_insert_flush(rq);
966 } else {
967 spin_lock(&ctx->lock);
968 __blk_mq_insert_request(hctx, rq, at_head);
969 spin_unlock(&ctx->lock);
970 }
Jens Axboe320ae512013-10-24 09:20:05 +0100971
Jens Axboe320ae512013-10-24 09:20:05 +0100972 if (run_queue)
973 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600974
975 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100976}
977
978static void blk_mq_insert_requests(struct request_queue *q,
979 struct blk_mq_ctx *ctx,
980 struct list_head *list,
981 int depth,
982 bool from_schedule)
983
984{
985 struct blk_mq_hw_ctx *hctx;
986 struct blk_mq_ctx *current_ctx;
987
988 trace_block_unplug(q, depth, !from_schedule);
989
990 current_ctx = blk_mq_get_ctx(q);
991
992 if (!cpu_online(ctx->cpu))
993 ctx = current_ctx;
994 hctx = q->mq_ops->map_queue(q, ctx->cpu);
995
996 /*
997 * preemption doesn't flush plug list, so it's possible ctx->cpu is
998 * offline now
999 */
1000 spin_lock(&ctx->lock);
1001 while (!list_empty(list)) {
1002 struct request *rq;
1003
1004 rq = list_first_entry(list, struct request, queuelist);
1005 list_del_init(&rq->queuelist);
1006 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001007 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001008 }
1009 spin_unlock(&ctx->lock);
1010
Jens Axboe320ae512013-10-24 09:20:05 +01001011 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001012 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001013}
1014
1015static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1016{
1017 struct request *rqa = container_of(a, struct request, queuelist);
1018 struct request *rqb = container_of(b, struct request, queuelist);
1019
1020 return !(rqa->mq_ctx < rqb->mq_ctx ||
1021 (rqa->mq_ctx == rqb->mq_ctx &&
1022 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1023}
1024
1025void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1026{
1027 struct blk_mq_ctx *this_ctx;
1028 struct request_queue *this_q;
1029 struct request *rq;
1030 LIST_HEAD(list);
1031 LIST_HEAD(ctx_list);
1032 unsigned int depth;
1033
1034 list_splice_init(&plug->mq_list, &list);
1035
1036 list_sort(NULL, &list, plug_ctx_cmp);
1037
1038 this_q = NULL;
1039 this_ctx = NULL;
1040 depth = 0;
1041
1042 while (!list_empty(&list)) {
1043 rq = list_entry_rq(list.next);
1044 list_del_init(&rq->queuelist);
1045 BUG_ON(!rq->q);
1046 if (rq->mq_ctx != this_ctx) {
1047 if (this_ctx) {
1048 blk_mq_insert_requests(this_q, this_ctx,
1049 &ctx_list, depth,
1050 from_schedule);
1051 }
1052
1053 this_ctx = rq->mq_ctx;
1054 this_q = rq->q;
1055 depth = 0;
1056 }
1057
1058 depth++;
1059 list_add_tail(&rq->queuelist, &ctx_list);
1060 }
1061
1062 /*
1063 * If 'this_ctx' is set, we know we have entries to complete
1064 * on 'ctx_list'. Do those.
1065 */
1066 if (this_ctx) {
1067 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1068 from_schedule);
1069 }
1070}
1071
1072static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1073{
1074 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001075
Jens Axboe3ee32372014-06-09 09:36:53 -06001076 if (blk_do_io_stat(rq))
Jens Axboe4b570522014-05-29 11:00:11 -06001077 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001078}
1079
Jens Axboe07068d52014-05-22 10:40:51 -06001080static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1081 struct blk_mq_ctx *ctx,
1082 struct request *rq, struct bio *bio)
1083{
1084 struct request_queue *q = hctx->queue;
1085
1086 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1087 blk_mq_bio_to_request(rq, bio);
1088 spin_lock(&ctx->lock);
1089insert_rq:
1090 __blk_mq_insert_request(hctx, rq, false);
1091 spin_unlock(&ctx->lock);
1092 return false;
1093 } else {
1094 spin_lock(&ctx->lock);
1095 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1096 blk_mq_bio_to_request(rq, bio);
1097 goto insert_rq;
1098 }
1099
1100 spin_unlock(&ctx->lock);
1101 __blk_mq_free_request(hctx, ctx, rq);
1102 return true;
1103 }
1104}
1105
1106struct blk_map_ctx {
1107 struct blk_mq_hw_ctx *hctx;
1108 struct blk_mq_ctx *ctx;
1109};
1110
1111static struct request *blk_mq_map_request(struct request_queue *q,
1112 struct bio *bio,
1113 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001114{
1115 struct blk_mq_hw_ctx *hctx;
1116 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001117 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001118 int rw = bio_data_dir(bio);
Ming Leicb96a42c2014-06-01 00:43:37 +08001119 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001120
Jens Axboe07068d52014-05-22 10:40:51 -06001121 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001122 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001123 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001124 }
1125
1126 ctx = blk_mq_get_ctx(q);
1127 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1128
Jens Axboe07068d52014-05-22 10:40:51 -06001129 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001130 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001131
Jens Axboe320ae512013-10-24 09:20:05 +01001132 trace_block_getrq(q, bio, rw);
Ming Leicb96a42c2014-06-01 00:43:37 +08001133 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1134 hctx);
1135 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001136 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001137 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001138 blk_mq_put_ctx(ctx);
1139 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001140
1141 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001142 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a42c2014-06-01 00:43:37 +08001143 blk_mq_set_alloc_data(&alloc_data, q,
1144 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1145 rq = __blk_mq_alloc_request(&alloc_data, rw);
1146 ctx = alloc_data.ctx;
1147 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001148 }
1149
1150 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001151 data->hctx = hctx;
1152 data->ctx = ctx;
1153 return rq;
1154}
1155
1156/*
1157 * Multiple hardware queue variant. This will not use per-process plugs,
1158 * but will attempt to bypass the hctx queueing if we can go straight to
1159 * hardware for SYNC IO.
1160 */
1161static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1162{
1163 const int is_sync = rw_is_sync(bio->bi_rw);
1164 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1165 struct blk_map_ctx data;
1166 struct request *rq;
1167
1168 blk_queue_bounce(q, &bio);
1169
1170 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1171 bio_endio(bio, -EIO);
1172 return;
1173 }
1174
1175 rq = blk_mq_map_request(q, bio, &data);
1176 if (unlikely(!rq))
1177 return;
1178
1179 if (unlikely(is_flush_fua)) {
1180 blk_mq_bio_to_request(rq, bio);
1181 blk_insert_flush(rq);
1182 goto run_queue;
1183 }
1184
1185 if (is_sync) {
1186 int ret;
1187
1188 blk_mq_bio_to_request(rq, bio);
1189 blk_mq_start_request(rq, true);
1190
1191 /*
1192 * For OK queue, we are done. For error, kill it. Any other
1193 * error (busy), just add it to our list as we previously
1194 * would have done
1195 */
1196 ret = q->mq_ops->queue_rq(data.hctx, rq);
1197 if (ret == BLK_MQ_RQ_QUEUE_OK)
1198 goto done;
1199 else {
1200 __blk_mq_requeue_request(rq);
1201
1202 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1203 rq->errors = -EIO;
1204 blk_mq_end_io(rq, rq->errors);
1205 goto done;
1206 }
1207 }
1208 }
1209
1210 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1211 /*
1212 * For a SYNC request, send it to the hardware immediately. For
1213 * an ASYNC request, just ensure that we run it later on. The
1214 * latter allows for merging opportunities and more efficient
1215 * dispatching.
1216 */
1217run_queue:
1218 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1219 }
1220done:
1221 blk_mq_put_ctx(data.ctx);
1222}
1223
1224/*
1225 * Single hardware queue variant. This will attempt to use any per-process
1226 * plug for merging and IO deferral.
1227 */
1228static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1229{
1230 const int is_sync = rw_is_sync(bio->bi_rw);
1231 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1232 unsigned int use_plug, request_count = 0;
1233 struct blk_map_ctx data;
1234 struct request *rq;
1235
1236 /*
1237 * If we have multiple hardware queues, just go directly to
1238 * one of those for sync IO.
1239 */
1240 use_plug = !is_flush_fua && !is_sync;
1241
1242 blk_queue_bounce(q, &bio);
1243
1244 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1245 bio_endio(bio, -EIO);
1246 return;
1247 }
1248
1249 if (use_plug && !blk_queue_nomerges(q) &&
1250 blk_attempt_plug_merge(q, bio, &request_count))
1251 return;
1252
1253 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001254 if (unlikely(!rq))
1255 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001256
1257 if (unlikely(is_flush_fua)) {
1258 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001259 blk_insert_flush(rq);
1260 goto run_queue;
1261 }
1262
1263 /*
1264 * A task plug currently exists. Since this is completely lockless,
1265 * utilize that to temporarily store requests until the task is
1266 * either done or scheduled away.
1267 */
1268 if (use_plug) {
1269 struct blk_plug *plug = current->plug;
1270
1271 if (plug) {
1272 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001273 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001274 trace_block_plug(q);
1275 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1276 blk_flush_plug_list(plug, false);
1277 trace_block_plug(q);
1278 }
1279 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001280 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001281 return;
1282 }
1283 }
1284
Jens Axboe07068d52014-05-22 10:40:51 -06001285 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1286 /*
1287 * For a SYNC request, send it to the hardware immediately. For
1288 * an ASYNC request, just ensure that we run it later on. The
1289 * latter allows for merging opportunities and more efficient
1290 * dispatching.
1291 */
1292run_queue:
1293 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001294 }
1295
Jens Axboe07068d52014-05-22 10:40:51 -06001296 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001297}
1298
1299/*
1300 * Default mapping to a software queue, since we use one per CPU.
1301 */
1302struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1303{
1304 return q->queue_hw_ctx[q->mq_map[cpu]];
1305}
1306EXPORT_SYMBOL(blk_mq_map_queue);
1307
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001308static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1309 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001310{
1311 struct page *page;
1312
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001313 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001314 int i;
1315
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001316 for (i = 0; i < tags->nr_tags; i++) {
1317 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001318 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001319 set->ops->exit_request(set->driver_data, tags->rqs[i],
1320 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001321 }
1322 }
1323
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001324 while (!list_empty(&tags->page_list)) {
1325 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001326 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001327 __free_pages(page, page->private);
1328 }
1329
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001330 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001331
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001332 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001333}
1334
1335static size_t order_to_size(unsigned int order)
1336{
Ming Lei4ca08502014-04-19 18:00:18 +08001337 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001338}
1339
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001340static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1341 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001342{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001343 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001344 unsigned int i, j, entries_per_page, max_order = 4;
1345 size_t rq_size, left;
1346
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001347 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1348 set->numa_node);
1349 if (!tags)
1350 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001351
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001352 INIT_LIST_HEAD(&tags->page_list);
1353
1354 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1355 GFP_KERNEL, set->numa_node);
1356 if (!tags->rqs) {
1357 blk_mq_free_tags(tags);
1358 return NULL;
1359 }
Jens Axboe320ae512013-10-24 09:20:05 +01001360
1361 /*
1362 * rq_size is the size of the request plus driver payload, rounded
1363 * to the cacheline size
1364 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001365 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001366 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001367 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001368
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001369 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001370 int this_order = max_order;
1371 struct page *page;
1372 int to_do;
1373 void *p;
1374
1375 while (left < order_to_size(this_order - 1) && this_order)
1376 this_order--;
1377
1378 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001379 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1380 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001381 if (page)
1382 break;
1383 if (!this_order--)
1384 break;
1385 if (order_to_size(this_order) < rq_size)
1386 break;
1387 } while (1);
1388
1389 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001390 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001391
1392 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001393 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001394
1395 p = page_address(page);
1396 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001397 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001398 left -= to_do * rq_size;
1399 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001400 tags->rqs[i] = p;
1401 if (set->ops->init_request) {
1402 if (set->ops->init_request(set->driver_data,
1403 tags->rqs[i], hctx_idx, i,
1404 set->numa_node))
1405 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001406 }
1407
Jens Axboe320ae512013-10-24 09:20:05 +01001408 p += rq_size;
1409 i++;
1410 }
1411 }
1412
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001413 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001414
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001415fail:
1416 pr_warn("%s: failed to allocate requests\n", __func__);
1417 blk_mq_free_rq_map(set, tags, hctx_idx);
1418 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001419}
1420
Jens Axboe1429d7c2014-05-19 09:23:55 -06001421static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1422{
1423 kfree(bitmap->map);
1424}
1425
1426static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1427{
1428 unsigned int bpw = 8, total, num_maps, i;
1429
1430 bitmap->bits_per_word = bpw;
1431
1432 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1433 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1434 GFP_KERNEL, node);
1435 if (!bitmap->map)
1436 return -ENOMEM;
1437
1438 bitmap->map_size = num_maps;
1439
1440 total = nr_cpu_ids;
1441 for (i = 0; i < num_maps; i++) {
1442 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1443 total -= bitmap->map[i].depth;
1444 }
1445
1446 return 0;
1447}
1448
Jens Axboe484b4062014-05-21 14:01:15 -06001449static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1450{
1451 struct request_queue *q = hctx->queue;
1452 struct blk_mq_ctx *ctx;
1453 LIST_HEAD(tmp);
1454
1455 /*
1456 * Move ctx entries to new CPU, if this one is going away.
1457 */
1458 ctx = __blk_mq_get_ctx(q, cpu);
1459
1460 spin_lock(&ctx->lock);
1461 if (!list_empty(&ctx->rq_list)) {
1462 list_splice_init(&ctx->rq_list, &tmp);
1463 blk_mq_hctx_clear_pending(hctx, ctx);
1464 }
1465 spin_unlock(&ctx->lock);
1466
1467 if (list_empty(&tmp))
1468 return NOTIFY_OK;
1469
1470 ctx = blk_mq_get_ctx(q);
1471 spin_lock(&ctx->lock);
1472
1473 while (!list_empty(&tmp)) {
1474 struct request *rq;
1475
1476 rq = list_first_entry(&tmp, struct request, queuelist);
1477 rq->mq_ctx = ctx;
1478 list_move_tail(&rq->queuelist, &ctx->rq_list);
1479 }
1480
1481 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1482 blk_mq_hctx_mark_pending(hctx, ctx);
1483
1484 spin_unlock(&ctx->lock);
1485
1486 blk_mq_run_hw_queue(hctx, true);
1487 blk_mq_put_ctx(ctx);
1488 return NOTIFY_OK;
1489}
1490
1491static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1492{
1493 struct request_queue *q = hctx->queue;
1494 struct blk_mq_tag_set *set = q->tag_set;
1495
1496 if (set->tags[hctx->queue_num])
1497 return NOTIFY_OK;
1498
1499 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1500 if (!set->tags[hctx->queue_num])
1501 return NOTIFY_STOP;
1502
1503 hctx->tags = set->tags[hctx->queue_num];
1504 return NOTIFY_OK;
1505}
1506
1507static int blk_mq_hctx_notify(void *data, unsigned long action,
1508 unsigned int cpu)
1509{
1510 struct blk_mq_hw_ctx *hctx = data;
1511
1512 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1513 return blk_mq_hctx_cpu_offline(hctx, cpu);
1514 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1515 return blk_mq_hctx_cpu_online(hctx, cpu);
1516
1517 return NOTIFY_OK;
1518}
1519
Ming Lei624dbe42014-05-27 23:35:13 +08001520static void blk_mq_exit_hw_queues(struct request_queue *q,
1521 struct blk_mq_tag_set *set, int nr_queue)
1522{
1523 struct blk_mq_hw_ctx *hctx;
1524 unsigned int i;
1525
1526 queue_for_each_hw_ctx(q, hctx, i) {
1527 if (i == nr_queue)
1528 break;
1529
Jens Axboef899fed2014-06-04 09:11:53 -06001530 blk_mq_tag_idle(hctx);
1531
Ming Lei624dbe42014-05-27 23:35:13 +08001532 if (set->ops->exit_hctx)
1533 set->ops->exit_hctx(hctx, i);
1534
1535 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1536 kfree(hctx->ctxs);
1537 blk_mq_free_bitmap(&hctx->ctx_map);
1538 }
1539
1540}
1541
1542static void blk_mq_free_hw_queues(struct request_queue *q,
1543 struct blk_mq_tag_set *set)
1544{
1545 struct blk_mq_hw_ctx *hctx;
1546 unsigned int i;
1547
1548 queue_for_each_hw_ctx(q, hctx, i) {
1549 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001550 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001551 }
1552}
1553
Jens Axboe320ae512013-10-24 09:20:05 +01001554static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001555 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001556{
1557 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001558 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001559
1560 /*
1561 * Initialize hardware queues
1562 */
1563 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001564 int node;
1565
1566 node = hctx->numa_node;
1567 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001568 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001569
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001570 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1571 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001572 spin_lock_init(&hctx->lock);
1573 INIT_LIST_HEAD(&hctx->dispatch);
1574 hctx->queue = q;
1575 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001576 hctx->flags = set->flags;
1577 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001578
1579 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1580 blk_mq_hctx_notify, hctx);
1581 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1582
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001583 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001584
1585 /*
1586 * Allocate space for all possible cpus to avoid allocation in
1587 * runtime
1588 */
1589 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1590 GFP_KERNEL, node);
1591 if (!hctx->ctxs)
1592 break;
1593
Jens Axboe1429d7c2014-05-19 09:23:55 -06001594 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001595 break;
1596
Jens Axboe320ae512013-10-24 09:20:05 +01001597 hctx->nr_ctx = 0;
1598
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001599 if (set->ops->init_hctx &&
1600 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001601 break;
1602 }
1603
1604 if (i == q->nr_hw_queues)
1605 return 0;
1606
1607 /*
1608 * Init failed
1609 */
Ming Lei624dbe42014-05-27 23:35:13 +08001610 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001611
1612 return 1;
1613}
1614
1615static void blk_mq_init_cpu_queues(struct request_queue *q,
1616 unsigned int nr_hw_queues)
1617{
1618 unsigned int i;
1619
1620 for_each_possible_cpu(i) {
1621 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1622 struct blk_mq_hw_ctx *hctx;
1623
1624 memset(__ctx, 0, sizeof(*__ctx));
1625 __ctx->cpu = i;
1626 spin_lock_init(&__ctx->lock);
1627 INIT_LIST_HEAD(&__ctx->rq_list);
1628 __ctx->queue = q;
1629
1630 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001631 if (!cpu_online(i))
1632 continue;
1633
Jens Axboee4043dc2014-04-09 10:18:23 -06001634 hctx = q->mq_ops->map_queue(q, i);
1635 cpumask_set_cpu(i, hctx->cpumask);
1636 hctx->nr_ctx++;
1637
Jens Axboe320ae512013-10-24 09:20:05 +01001638 /*
1639 * Set local node, IFF we have more than one hw queue. If
1640 * not, we remain on the home node of the device
1641 */
1642 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1643 hctx->numa_node = cpu_to_node(i);
1644 }
1645}
1646
1647static void blk_mq_map_swqueue(struct request_queue *q)
1648{
1649 unsigned int i;
1650 struct blk_mq_hw_ctx *hctx;
1651 struct blk_mq_ctx *ctx;
1652
1653 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001654 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001655 hctx->nr_ctx = 0;
1656 }
1657
1658 /*
1659 * Map software to hardware queues
1660 */
1661 queue_for_each_ctx(q, ctx, i) {
1662 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001663 if (!cpu_online(i))
1664 continue;
1665
Jens Axboe320ae512013-10-24 09:20:05 +01001666 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001667 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001668 ctx->index_hw = hctx->nr_ctx;
1669 hctx->ctxs[hctx->nr_ctx++] = ctx;
1670 }
Jens Axboe506e9312014-05-07 10:26:44 -06001671
1672 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001673 /*
1674 * If not software queues are mapped to this hardware queue,
1675 * disable it and free the request entries
1676 */
1677 if (!hctx->nr_ctx) {
1678 struct blk_mq_tag_set *set = q->tag_set;
1679
1680 if (set->tags[i]) {
1681 blk_mq_free_rq_map(set, set->tags[i], i);
1682 set->tags[i] = NULL;
1683 hctx->tags = NULL;
1684 }
1685 continue;
1686 }
1687
1688 /*
1689 * Initialize batch roundrobin counts
1690 */
Jens Axboe506e9312014-05-07 10:26:44 -06001691 hctx->next_cpu = cpumask_first(hctx->cpumask);
1692 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1693 }
Jens Axboe320ae512013-10-24 09:20:05 +01001694}
1695
Jens Axboe0d2602c2014-05-13 15:10:52 -06001696static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1697{
1698 struct blk_mq_hw_ctx *hctx;
1699 struct request_queue *q;
1700 bool shared;
1701 int i;
1702
1703 if (set->tag_list.next == set->tag_list.prev)
1704 shared = false;
1705 else
1706 shared = true;
1707
1708 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1709 blk_mq_freeze_queue(q);
1710
1711 queue_for_each_hw_ctx(q, hctx, i) {
1712 if (shared)
1713 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1714 else
1715 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1716 }
1717 blk_mq_unfreeze_queue(q);
1718 }
1719}
1720
1721static void blk_mq_del_queue_tag_set(struct request_queue *q)
1722{
1723 struct blk_mq_tag_set *set = q->tag_set;
1724
1725 blk_mq_freeze_queue(q);
1726
1727 mutex_lock(&set->tag_list_lock);
1728 list_del_init(&q->tag_set_list);
1729 blk_mq_update_tag_set_depth(set);
1730 mutex_unlock(&set->tag_list_lock);
1731
1732 blk_mq_unfreeze_queue(q);
1733}
1734
1735static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1736 struct request_queue *q)
1737{
1738 q->tag_set = set;
1739
1740 mutex_lock(&set->tag_list_lock);
1741 list_add_tail(&q->tag_set_list, &set->tag_list);
1742 blk_mq_update_tag_set_depth(set);
1743 mutex_unlock(&set->tag_list_lock);
1744}
1745
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001746struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001747{
1748 struct blk_mq_hw_ctx **hctxs;
Ming Leie6cdb092014-06-03 11:24:06 +08001749 struct blk_mq_ctx __percpu *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001750 struct request_queue *q;
Jens Axboef14bbe72014-05-27 12:06:53 -06001751 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001752 int i;
1753
Jens Axboe320ae512013-10-24 09:20:05 +01001754 ctx = alloc_percpu(struct blk_mq_ctx);
1755 if (!ctx)
1756 return ERR_PTR(-ENOMEM);
1757
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001758 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1759 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001760
1761 if (!hctxs)
1762 goto err_percpu;
1763
Jens Axboef14bbe72014-05-27 12:06:53 -06001764 map = blk_mq_make_queue_map(set);
1765 if (!map)
1766 goto err_map;
1767
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001768 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001769 int node = blk_mq_hw_queue_to_node(map, i);
1770
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001771 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1772 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001773 if (!hctxs[i])
1774 goto err_hctxs;
1775
Jens Axboee4043dc2014-04-09 10:18:23 -06001776 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1777 goto err_hctxs;
1778
Jens Axboe0d2602c2014-05-13 15:10:52 -06001779 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001780 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001781 hctxs[i]->queue_num = i;
1782 }
1783
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001784 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001785 if (!q)
1786 goto err_hctxs;
1787
Ming Lei3d2936f2014-05-27 23:35:14 +08001788 if (percpu_counter_init(&q->mq_usage_counter, 0))
1789 goto err_map;
1790
Jens Axboe320ae512013-10-24 09:20:05 +01001791 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1792 blk_queue_rq_timeout(q, 30000);
1793
1794 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001795 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001796 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001797
1798 q->queue_ctx = ctx;
1799 q->queue_hw_ctx = hctxs;
1800
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001801 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001802 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001803
Jens Axboe05f1dd52014-05-29 09:53:32 -06001804 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1805 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1806
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001807 q->sg_reserved_size = INT_MAX;
1808
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001809 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1810 INIT_LIST_HEAD(&q->requeue_list);
1811 spin_lock_init(&q->requeue_lock);
1812
Jens Axboe07068d52014-05-22 10:40:51 -06001813 if (q->nr_hw_queues > 1)
1814 blk_queue_make_request(q, blk_mq_make_request);
1815 else
1816 blk_queue_make_request(q, blk_sq_make_request);
1817
Jens Axboe87ee7b12014-04-24 08:51:47 -06001818 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001819 if (set->timeout)
1820 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001821
Jens Axboeeba71762014-05-20 15:17:27 -06001822 /*
1823 * Do this after blk_queue_make_request() overrides it...
1824 */
1825 q->nr_requests = set->queue_depth;
1826
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001827 if (set->ops->complete)
1828 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001829
Jens Axboe320ae512013-10-24 09:20:05 +01001830 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001831 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001832
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001833 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1834 set->cmd_size, cache_line_size()),
1835 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001836 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001837 goto err_hw;
1838
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001839 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001840 goto err_flush_rq;
1841
Jens Axboe320ae512013-10-24 09:20:05 +01001842 mutex_lock(&all_q_mutex);
1843 list_add_tail(&q->all_q_node, &all_q_list);
1844 mutex_unlock(&all_q_mutex);
1845
Jens Axboe0d2602c2014-05-13 15:10:52 -06001846 blk_mq_add_queue_tag_set(set, q);
1847
Jens Axboe484b4062014-05-21 14:01:15 -06001848 blk_mq_map_swqueue(q);
1849
Jens Axboe320ae512013-10-24 09:20:05 +01001850 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001851
1852err_flush_rq:
1853 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001854err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001855 blk_cleanup_queue(q);
1856err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001857 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001858 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001859 if (!hctxs[i])
1860 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001861 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001862 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001863 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001864err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001865 kfree(hctxs);
1866err_percpu:
1867 free_percpu(ctx);
1868 return ERR_PTR(-ENOMEM);
1869}
1870EXPORT_SYMBOL(blk_mq_init_queue);
1871
1872void blk_mq_free_queue(struct request_queue *q)
1873{
Ming Lei624dbe42014-05-27 23:35:13 +08001874 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001875
Jens Axboe0d2602c2014-05-13 15:10:52 -06001876 blk_mq_del_queue_tag_set(q);
1877
Ming Lei624dbe42014-05-27 23:35:13 +08001878 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1879 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001880
Ming Lei3d2936f2014-05-27 23:35:14 +08001881 percpu_counter_destroy(&q->mq_usage_counter);
1882
Jens Axboe320ae512013-10-24 09:20:05 +01001883 free_percpu(q->queue_ctx);
1884 kfree(q->queue_hw_ctx);
1885 kfree(q->mq_map);
1886
1887 q->queue_ctx = NULL;
1888 q->queue_hw_ctx = NULL;
1889 q->mq_map = NULL;
1890
1891 mutex_lock(&all_q_mutex);
1892 list_del_init(&q->all_q_node);
1893 mutex_unlock(&all_q_mutex);
1894}
Jens Axboe320ae512013-10-24 09:20:05 +01001895
1896/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001897static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001898{
1899 blk_mq_freeze_queue(q);
1900
Jens Axboe67aec142014-05-30 08:25:36 -06001901 blk_mq_sysfs_unregister(q);
1902
Jens Axboe320ae512013-10-24 09:20:05 +01001903 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1904
1905 /*
1906 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1907 * we should change hctx numa_node according to new topology (this
1908 * involves free and re-allocate memory, worthy doing?)
1909 */
1910
1911 blk_mq_map_swqueue(q);
1912
Jens Axboe67aec142014-05-30 08:25:36 -06001913 blk_mq_sysfs_register(q);
1914
Jens Axboe320ae512013-10-24 09:20:05 +01001915 blk_mq_unfreeze_queue(q);
1916}
1917
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001918static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1919 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001920{
1921 struct request_queue *q;
1922
1923 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001924 * Before new mappings are established, hotadded cpu might already
1925 * start handling requests. This doesn't break anything as we map
1926 * offline CPUs to first hardware queue. We will re-init the queue
1927 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001928 */
1929 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1930 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1931 return NOTIFY_OK;
1932
1933 mutex_lock(&all_q_mutex);
1934 list_for_each_entry(q, &all_q_list, all_q_node)
1935 blk_mq_queue_reinit(q);
1936 mutex_unlock(&all_q_mutex);
1937 return NOTIFY_OK;
1938}
1939
Jens Axboea4391c62014-06-05 15:21:56 -06001940/*
1941 * Alloc a tag set to be associated with one or more request queues.
1942 * May fail with EINVAL for various error conditions. May adjust the
1943 * requested depth down, if if it too large. In that case, the set
1944 * value will be stored in set->queue_depth.
1945 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001946int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1947{
1948 int i;
1949
1950 if (!set->nr_hw_queues)
1951 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06001952 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001953 return -EINVAL;
1954 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1955 return -EINVAL;
1956
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001957 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001958 return -EINVAL;
1959
Jens Axboea4391c62014-06-05 15:21:56 -06001960 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
1961 pr_info("blk-mq: reduced tag depth to %u\n",
1962 BLK_MQ_MAX_DEPTH);
1963 set->queue_depth = BLK_MQ_MAX_DEPTH;
1964 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001965
Ming Lei484790052014-04-19 18:00:17 +08001966 set->tags = kmalloc_node(set->nr_hw_queues *
1967 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001968 GFP_KERNEL, set->numa_node);
1969 if (!set->tags)
1970 goto out;
1971
1972 for (i = 0; i < set->nr_hw_queues; i++) {
1973 set->tags[i] = blk_mq_init_rq_map(set, i);
1974 if (!set->tags[i])
1975 goto out_unwind;
1976 }
1977
Jens Axboe0d2602c2014-05-13 15:10:52 -06001978 mutex_init(&set->tag_list_lock);
1979 INIT_LIST_HEAD(&set->tag_list);
1980
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001981 return 0;
1982
1983out_unwind:
1984 while (--i >= 0)
1985 blk_mq_free_rq_map(set, set->tags[i], i);
1986out:
1987 return -ENOMEM;
1988}
1989EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1990
1991void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1992{
1993 int i;
1994
Jens Axboe484b4062014-05-21 14:01:15 -06001995 for (i = 0; i < set->nr_hw_queues; i++) {
1996 if (set->tags[i])
1997 blk_mq_free_rq_map(set, set->tags[i], i);
1998 }
1999
Ming Lei981bd182014-04-24 00:07:34 +08002000 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002001}
2002EXPORT_SYMBOL(blk_mq_free_tag_set);
2003
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002004int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2005{
2006 struct blk_mq_tag_set *set = q->tag_set;
2007 struct blk_mq_hw_ctx *hctx;
2008 int i, ret;
2009
2010 if (!set || nr > set->queue_depth)
2011 return -EINVAL;
2012
2013 ret = 0;
2014 queue_for_each_hw_ctx(q, hctx, i) {
2015 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2016 if (ret)
2017 break;
2018 }
2019
2020 if (!ret)
2021 q->nr_requests = nr;
2022
2023 return ret;
2024}
2025
Jens Axboe676141e2014-03-20 13:29:18 -06002026void blk_mq_disable_hotplug(void)
2027{
2028 mutex_lock(&all_q_mutex);
2029}
2030
2031void blk_mq_enable_hotplug(void)
2032{
2033 mutex_unlock(&all_q_mutex);
2034}
2035
Jens Axboe320ae512013-10-24 09:20:05 +01002036static int __init blk_mq_init(void)
2037{
Jens Axboe320ae512013-10-24 09:20:05 +01002038 blk_mq_cpu_init();
2039
2040 /* Must be called after percpu_counter_hotcpu_callback() */
2041 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2042
2043 return 0;
2044}
2045subsys_initcall(blk_mq_init);