blob: cea1bd87a4162093d04b0b28ef89b39dd9e648b7 [file] [log] [blame]
Jens Axboe320ae512013-10-24 09:20:05 +01001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/backing-dev.h>
4#include <linux/bio.h>
5#include <linux/blkdev.h>
6#include <linux/mm.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/workqueue.h>
10#include <linux/smp.h>
11#include <linux/llist.h>
12#include <linux/list_sort.h>
13#include <linux/cpu.h>
14#include <linux/cache.h>
15#include <linux/sched/sysctl.h>
16#include <linux/delay.h>
17
18#include <trace/events/block.h>
19
20#include <linux/blk-mq.h>
21#include "blk.h"
22#include "blk-mq.h"
23#include "blk-mq-tag.h"
24
25static DEFINE_MUTEX(all_q_mutex);
26static LIST_HEAD(all_q_list);
27
28static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
29
Jens Axboe320ae512013-10-24 09:20:05 +010030static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
31 unsigned int cpu)
32{
33 return per_cpu_ptr(q->queue_ctx, cpu);
34}
35
36/*
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
41 */
42static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
43{
44 return __blk_mq_get_ctx(q, get_cpu());
45}
46
47static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
48{
49 put_cpu();
50}
51
52/*
53 * Check if any of the ctx's have pending work in this hardware queue
54 */
55static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
56{
57 unsigned int i;
58
59 for (i = 0; i < hctx->nr_ctx_map; i++)
60 if (hctx->ctx_map[i])
61 return true;
62
63 return false;
64}
65
66/*
67 * Mark this ctx as having pending work in this hardware queue
68 */
69static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70 struct blk_mq_ctx *ctx)
71{
72 if (!test_bit(ctx->index_hw, hctx->ctx_map))
73 set_bit(ctx->index_hw, hctx->ctx_map);
74}
75
Christoph Hellwig081241e2014-02-20 15:32:36 -080076static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +010078{
79 struct request *rq;
80 unsigned int tag;
81
82 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83 if (tag != BLK_MQ_TAG_FAIL) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -060084 rq = hctx->tags->rqs[tag];
Jens Axboe320ae512013-10-24 09:20:05 +010085 rq->tag = tag;
86
87 return rq;
88 }
89
90 return NULL;
91}
92
93static int blk_mq_queue_enter(struct request_queue *q)
94{
95 int ret;
96
97 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
98 smp_wmb();
99 /* we have problems to freeze the queue if it's initializing */
100 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
101 return 0;
102
103 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
104
105 spin_lock_irq(q->queue_lock);
106 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800107 !blk_queue_bypass(q) || blk_queue_dying(q),
108 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100109 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800110 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100111 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800112 else if (blk_queue_dying(q))
113 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100114 spin_unlock_irq(q->queue_lock);
115
116 return ret;
117}
118
119static void blk_mq_queue_exit(struct request_queue *q)
120{
121 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
122}
123
Ming Lei43a5e4e2013-12-26 21:31:35 +0800124static void __blk_mq_drain_queue(struct request_queue *q)
125{
126 while (true) {
127 s64 count;
128
129 spin_lock_irq(q->queue_lock);
130 count = percpu_counter_sum(&q->mq_usage_counter);
131 spin_unlock_irq(q->queue_lock);
132
133 if (count == 0)
134 break;
135 blk_mq_run_queues(q, false);
136 msleep(10);
137 }
138}
139
Jens Axboe320ae512013-10-24 09:20:05 +0100140/*
141 * Guarantee no request is in use, so we can change any data structure of
142 * the queue afterward.
143 */
144static void blk_mq_freeze_queue(struct request_queue *q)
145{
146 bool drain;
147
148 spin_lock_irq(q->queue_lock);
149 drain = !q->bypass_depth++;
150 queue_flag_set(QUEUE_FLAG_BYPASS, q);
151 spin_unlock_irq(q->queue_lock);
152
Ming Lei43a5e4e2013-12-26 21:31:35 +0800153 if (drain)
154 __blk_mq_drain_queue(q);
155}
Jens Axboe320ae512013-10-24 09:20:05 +0100156
Ming Lei43a5e4e2013-12-26 21:31:35 +0800157void blk_mq_drain_queue(struct request_queue *q)
158{
159 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100160}
161
162static void blk_mq_unfreeze_queue(struct request_queue *q)
163{
164 bool wake = false;
165
166 spin_lock_irq(q->queue_lock);
167 if (!--q->bypass_depth) {
168 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
169 wake = true;
170 }
171 WARN_ON_ONCE(q->bypass_depth < 0);
172 spin_unlock_irq(q->queue_lock);
173 if (wake)
174 wake_up_all(&q->mq_freeze_wq);
175}
176
177bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
178{
179 return blk_mq_has_free_tags(hctx->tags);
180}
181EXPORT_SYMBOL(blk_mq_can_queue);
182
Jens Axboe94eddfb2013-11-19 09:25:07 -0700183static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
184 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100185{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700186 if (blk_queue_io_stat(q))
187 rw_flags |= REQ_IO_STAT;
188
Ming Lei6a3c8a32014-04-19 18:00:19 +0800189 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100190 rq->mq_ctx = ctx;
191 rq->cmd_flags = rw_flags;
Ming Lei0fec08b2014-01-03 10:00:08 -0700192 rq->start_time = jiffies;
193 set_start_time_ns(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100194 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195}
196
Jens Axboe320ae512013-10-24 09:20:05 +0100197static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
198 int rw, gfp_t gfp,
199 bool reserved)
200{
201 struct request *rq;
202
203 do {
204 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
205 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
206
Christoph Hellwig18741982014-02-10 09:29:00 -0700207 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100208 if (rq) {
Jens Axboe94eddfb2013-11-19 09:25:07 -0700209 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100210 break;
Jeff Moyer959a35f2013-12-03 14:23:00 -0700211 }
Jens Axboe320ae512013-10-24 09:20:05 +0100212
Jens Axboee4043dc2014-04-09 10:18:23 -0600213 if (gfp & __GFP_WAIT) {
214 __blk_mq_run_hw_queue(hctx);
215 blk_mq_put_ctx(ctx);
216 } else {
217 blk_mq_put_ctx(ctx);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700218 break;
Jens Axboee4043dc2014-04-09 10:18:23 -0600219 }
Jeff Moyer959a35f2013-12-03 14:23:00 -0700220
Jens Axboe320ae512013-10-24 09:20:05 +0100221 blk_mq_wait_for_tags(hctx->tags);
222 } while (1);
223
224 return rq;
225}
226
Christoph Hellwig18741982014-02-10 09:29:00 -0700227struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100228{
229 struct request *rq;
230
231 if (blk_mq_queue_enter(q))
232 return NULL;
233
Christoph Hellwig18741982014-02-10 09:29:00 -0700234 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700235 if (rq)
236 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100237 return rq;
238}
239
240struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
241 gfp_t gfp)
242{
243 struct request *rq;
244
245 if (blk_mq_queue_enter(q))
246 return NULL;
247
248 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700249 if (rq)
250 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100251 return rq;
252}
253EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
254
Jens Axboe320ae512013-10-24 09:20:05 +0100255static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
256 struct blk_mq_ctx *ctx, struct request *rq)
257{
258 const int tag = rq->tag;
259 struct request_queue *q = rq->q;
260
Ming Lei6a3c8a32014-04-19 18:00:19 +0800261 blk_rq_init(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100262 blk_mq_put_tag(hctx->tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100263 blk_mq_queue_exit(q);
264}
265
266void blk_mq_free_request(struct request *rq)
267{
268 struct blk_mq_ctx *ctx = rq->mq_ctx;
269 struct blk_mq_hw_ctx *hctx;
270 struct request_queue *q = rq->q;
271
272 ctx->rq_completed[rq_is_sync(rq)]++;
273
274 hctx = q->mq_ops->map_queue(q, ctx->cpu);
275 __blk_mq_free_request(hctx, ctx, rq);
276}
277
Christoph Hellwig8727af42014-04-14 10:30:08 +0200278/*
279 * Clone all relevant state from a request that has been put on hold in
280 * the flush state machine into the preallocated flush request that hangs
281 * off the request queue.
282 *
283 * For a driver the flush request should be invisible, that's why we are
284 * impersonating the original request here.
285 */
286void blk_mq_clone_flush_request(struct request *flush_rq,
287 struct request *orig_rq)
288{
289 struct blk_mq_hw_ctx *hctx =
290 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
291
292 flush_rq->mq_ctx = orig_rq->mq_ctx;
293 flush_rq->tag = orig_rq->tag;
294 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
295 hctx->cmd_size);
296}
297
Christoph Hellwig63151a42014-04-16 09:44:52 +0200298inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100299{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700300 blk_account_io_done(rq);
301
Christoph Hellwig91b63632014-04-16 09:44:53 +0200302 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100303 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200304 } else {
305 if (unlikely(blk_bidi_rq(rq)))
306 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100307 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200308 }
Jens Axboe320ae512013-10-24 09:20:05 +0100309}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200310EXPORT_SYMBOL(__blk_mq_end_io);
311
312void blk_mq_end_io(struct request *rq, int error)
313{
314 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
315 BUG();
316 __blk_mq_end_io(rq, error);
317}
318EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100319
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800320static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100321{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800322 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100323
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800324 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100325}
326
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800327void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100328{
329 struct blk_mq_ctx *ctx = rq->mq_ctx;
330 int cpu;
331
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800332 if (!ctx->ipi_redirect) {
333 rq->q->softirq_done_fn(rq);
334 return;
335 }
Jens Axboe320ae512013-10-24 09:20:05 +0100336
337 cpu = get_cpu();
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800338 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800339 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800340 rq->csd.info = rq;
341 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100342 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800343 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800344 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800345 }
Jens Axboe320ae512013-10-24 09:20:05 +0100346 put_cpu();
347}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800348
349/**
350 * blk_mq_complete_request - end I/O on a request
351 * @rq: the request being processed
352 *
353 * Description:
354 * Ends all I/O on a request. It does not handle partial completions.
355 * The actual completion happens out-of-order, through a IPI handler.
356 **/
357void blk_mq_complete_request(struct request *rq)
358{
359 if (unlikely(blk_should_fake_timeout(rq->q)))
360 return;
361 if (!blk_mark_rq_complete(rq))
362 __blk_mq_complete_request(rq);
363}
364EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100365
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800366static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100367{
368 struct request_queue *q = rq->q;
369
370 trace_block_rq_issue(q, rq);
371
Christoph Hellwig742ee692014-04-14 10:30:06 +0200372 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200373 if (unlikely(blk_bidi_rq(rq)))
374 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200375
Jens Axboe320ae512013-10-24 09:20:05 +0100376 /*
377 * Just mark start time and set the started bit. Due to memory
378 * ordering, we know we'll see the correct deadline as long as
379 * REQ_ATOMIC_STARTED is seen.
380 */
381 rq->deadline = jiffies + q->rq_timeout;
382 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800383
384 if (q->dma_drain_size && blk_rq_bytes(rq)) {
385 /*
386 * Make sure space for the drain appears. We know we can do
387 * this because max_hw_segments has been adjusted to be one
388 * fewer than the device can handle.
389 */
390 rq->nr_phys_segments++;
391 }
392
393 /*
394 * Flag the last request in the series so that drivers know when IO
395 * should be kicked off, if they don't do it on a per-request basis.
396 *
397 * Note: the flag isn't the only condition drivers should do kick off.
398 * If drive is busy, the last request might not have the bit set.
399 */
400 if (last)
401 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100402}
403
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200404static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100405{
406 struct request_queue *q = rq->q;
407
408 trace_block_rq_requeue(q, rq);
409 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800410
411 rq->cmd_flags &= ~REQ_END;
412
413 if (q->dma_drain_size && blk_rq_bytes(rq))
414 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100415}
416
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200417void blk_mq_requeue_request(struct request *rq)
418{
419 struct request_queue *q = rq->q;
420
421 __blk_mq_requeue_request(rq);
422 blk_clear_rq_complete(rq);
423
424 trace_block_rq_requeue(q, rq);
425
426 BUG_ON(blk_queued_rq(rq));
427 blk_mq_insert_request(rq, true, true, false);
428}
429EXPORT_SYMBOL(blk_mq_requeue_request);
430
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600431struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
432{
433 return tags->rqs[tag];
434}
435EXPORT_SYMBOL(blk_mq_tag_to_rq);
436
Jens Axboe320ae512013-10-24 09:20:05 +0100437struct blk_mq_timeout_data {
438 struct blk_mq_hw_ctx *hctx;
439 unsigned long *next;
440 unsigned int *next_set;
441};
442
443static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
444{
445 struct blk_mq_timeout_data *data = __data;
446 struct blk_mq_hw_ctx *hctx = data->hctx;
447 unsigned int tag;
448
449 /* It may not be in flight yet (this is where
450 * the REQ_ATOMIC_STARTED flag comes in). The requests are
451 * statically allocated, so we know it's always safe to access the
452 * memory associated with a bit offset into ->rqs[].
453 */
454 tag = 0;
455 do {
456 struct request *rq;
457
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600458 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
459 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100460 break;
461
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600462 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
463 if (rq->q != hctx->queue)
464 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100465 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
466 continue;
467
468 blk_rq_check_expired(rq, data->next, data->next_set);
469 } while (1);
470}
471
472static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
473 unsigned long *next,
474 unsigned int *next_set)
475{
476 struct blk_mq_timeout_data data = {
477 .hctx = hctx,
478 .next = next,
479 .next_set = next_set,
480 };
481
482 /*
483 * Ask the tagging code to iterate busy requests, so we can
484 * check them for timeout.
485 */
486 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
487}
488
489static void blk_mq_rq_timer(unsigned long data)
490{
491 struct request_queue *q = (struct request_queue *) data;
492 struct blk_mq_hw_ctx *hctx;
493 unsigned long next = 0;
494 int i, next_set = 0;
495
496 queue_for_each_hw_ctx(q, hctx, i)
497 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
498
499 if (next_set)
500 mod_timer(&q->timeout, round_jiffies_up(next));
501}
502
503/*
504 * Reverse check our software queue for entries that we could potentially
505 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
506 * too much time checking for merges.
507 */
508static bool blk_mq_attempt_merge(struct request_queue *q,
509 struct blk_mq_ctx *ctx, struct bio *bio)
510{
511 struct request *rq;
512 int checked = 8;
513
514 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
515 int el_ret;
516
517 if (!checked--)
518 break;
519
520 if (!blk_rq_merge_ok(rq, bio))
521 continue;
522
523 el_ret = blk_try_merge(rq, bio);
524 if (el_ret == ELEVATOR_BACK_MERGE) {
525 if (bio_attempt_back_merge(q, rq, bio)) {
526 ctx->rq_merged++;
527 return true;
528 }
529 break;
530 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
531 if (bio_attempt_front_merge(q, rq, bio)) {
532 ctx->rq_merged++;
533 return true;
534 }
535 break;
536 }
537 }
538
539 return false;
540}
541
542void blk_mq_add_timer(struct request *rq)
543{
544 __blk_add_timer(rq, NULL);
545}
546
547/*
548 * Run this hardware queue, pulling any software queues mapped to it in.
549 * Note that this function currently has various problems around ordering
550 * of IO. In particular, we'd like FIFO behaviour on handling existing
551 * items on the hctx->dispatch list. Ignore that for now.
552 */
553static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
554{
555 struct request_queue *q = hctx->queue;
556 struct blk_mq_ctx *ctx;
557 struct request *rq;
558 LIST_HEAD(rq_list);
559 int bit, queued;
560
Jens Axboefd1270d2014-04-16 09:23:48 -0600561 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600562
Jens Axboe5d12f902014-03-19 15:25:02 -0600563 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100564 return;
565
566 hctx->run++;
567
568 /*
569 * Touch any software queue that has pending entries.
570 */
571 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
572 clear_bit(bit, hctx->ctx_map);
573 ctx = hctx->ctxs[bit];
574 BUG_ON(bit != ctx->index_hw);
575
576 spin_lock(&ctx->lock);
577 list_splice_tail_init(&ctx->rq_list, &rq_list);
578 spin_unlock(&ctx->lock);
579 }
580
581 /*
582 * If we have previous entries on our dispatch list, grab them
583 * and stuff them at the front for more fair dispatch.
584 */
585 if (!list_empty_careful(&hctx->dispatch)) {
586 spin_lock(&hctx->lock);
587 if (!list_empty(&hctx->dispatch))
588 list_splice_init(&hctx->dispatch, &rq_list);
589 spin_unlock(&hctx->lock);
590 }
591
592 /*
593 * Delete and return all entries from our dispatch list
594 */
595 queued = 0;
596
597 /*
598 * Now process all the entries, sending them to the driver.
599 */
600 while (!list_empty(&rq_list)) {
601 int ret;
602
603 rq = list_first_entry(&rq_list, struct request, queuelist);
604 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100605
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800606 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100607
608 ret = q->mq_ops->queue_rq(hctx, rq);
609 switch (ret) {
610 case BLK_MQ_RQ_QUEUE_OK:
611 queued++;
612 continue;
613 case BLK_MQ_RQ_QUEUE_BUSY:
614 /*
615 * FIXME: we should have a mechanism to stop the queue
616 * like blk_stop_queue, otherwise we will waste cpu
617 * time
618 */
619 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200620 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100621 break;
622 default:
623 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100624 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800625 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100626 blk_mq_end_io(rq, rq->errors);
627 break;
628 }
629
630 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
631 break;
632 }
633
634 if (!queued)
635 hctx->dispatched[0]++;
636 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
637 hctx->dispatched[ilog2(queued) + 1]++;
638
639 /*
640 * Any items that need requeuing? Stuff them into hctx->dispatch,
641 * that is where we will continue on next queue run.
642 */
643 if (!list_empty(&rq_list)) {
644 spin_lock(&hctx->lock);
645 list_splice(&rq_list, &hctx->dispatch);
646 spin_unlock(&hctx->lock);
647 }
648}
649
650void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
651{
Jens Axboe5d12f902014-03-19 15:25:02 -0600652 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100653 return;
654
Jens Axboee4043dc2014-04-09 10:18:23 -0600655 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100656 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600657 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600658 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600659 else {
660 unsigned int cpu;
661
662 /*
663 * It'd be great if the workqueue API had a way to pass
664 * in a mask and had some smarts for more clever placement
665 * than the first CPU. Or we could round-robin here. For now,
666 * just queue on the first CPU.
667 */
668 cpu = cpumask_first(hctx->cpumask);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600669 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600670 }
Jens Axboe320ae512013-10-24 09:20:05 +0100671}
672
673void blk_mq_run_queues(struct request_queue *q, bool async)
674{
675 struct blk_mq_hw_ctx *hctx;
676 int i;
677
678 queue_for_each_hw_ctx(q, hctx, i) {
679 if ((!blk_mq_hctx_has_pending(hctx) &&
680 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600681 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100682 continue;
683
Jens Axboee4043dc2014-04-09 10:18:23 -0600684 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100685 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600686 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100687 }
688}
689EXPORT_SYMBOL(blk_mq_run_queues);
690
691void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
692{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600693 cancel_delayed_work(&hctx->run_work);
694 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100695 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
696}
697EXPORT_SYMBOL(blk_mq_stop_hw_queue);
698
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100699void blk_mq_stop_hw_queues(struct request_queue *q)
700{
701 struct blk_mq_hw_ctx *hctx;
702 int i;
703
704 queue_for_each_hw_ctx(q, hctx, i)
705 blk_mq_stop_hw_queue(hctx);
706}
707EXPORT_SYMBOL(blk_mq_stop_hw_queues);
708
Jens Axboe320ae512013-10-24 09:20:05 +0100709void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
710{
711 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600712
713 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100714 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600715 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100716}
717EXPORT_SYMBOL(blk_mq_start_hw_queue);
718
Christoph Hellwig2f268552014-04-16 09:44:56 +0200719void blk_mq_start_hw_queues(struct request_queue *q)
720{
721 struct blk_mq_hw_ctx *hctx;
722 int i;
723
724 queue_for_each_hw_ctx(q, hctx, i)
725 blk_mq_start_hw_queue(hctx);
726}
727EXPORT_SYMBOL(blk_mq_start_hw_queues);
728
729
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200730void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100731{
732 struct blk_mq_hw_ctx *hctx;
733 int i;
734
735 queue_for_each_hw_ctx(q, hctx, i) {
736 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
737 continue;
738
739 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600740 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200741 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600742 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100743 }
744}
745EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
746
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600747static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100748{
749 struct blk_mq_hw_ctx *hctx;
750
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600751 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600752
Jens Axboe320ae512013-10-24 09:20:05 +0100753 __blk_mq_run_hw_queue(hctx);
754}
755
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600756static void blk_mq_delay_work_fn(struct work_struct *work)
757{
758 struct blk_mq_hw_ctx *hctx;
759
760 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
761
762 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
763 __blk_mq_run_hw_queue(hctx);
764}
765
766void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
767{
768 unsigned long tmo = msecs_to_jiffies(msecs);
769
770 if (hctx->queue->nr_hw_queues == 1)
771 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
772 else {
773 unsigned int cpu;
774
775 /*
776 * It'd be great if the workqueue API had a way to pass
777 * in a mask and had some smarts for more clever placement
778 * than the first CPU. Or we could round-robin here. For now,
779 * just queue on the first CPU.
780 */
781 cpu = cpumask_first(hctx->cpumask);
782 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
783 }
784}
785EXPORT_SYMBOL(blk_mq_delay_queue);
786
Jens Axboe320ae512013-10-24 09:20:05 +0100787static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800788 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100789{
790 struct blk_mq_ctx *ctx = rq->mq_ctx;
791
Jens Axboe01b983c2013-11-19 18:59:10 -0700792 trace_block_rq_insert(hctx->queue, rq);
793
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800794 if (at_head)
795 list_add(&rq->queuelist, &ctx->rq_list);
796 else
797 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100798 blk_mq_hctx_mark_pending(hctx, ctx);
799
800 /*
801 * We do this early, to ensure we are on the right CPU.
802 */
803 blk_mq_add_timer(rq);
804}
805
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600806void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
807 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100808{
809 struct request_queue *q = rq->q;
810 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600811 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100812
813 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600814 if (!cpu_online(ctx->cpu))
815 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100816
Jens Axboe320ae512013-10-24 09:20:05 +0100817 hctx = q->mq_ops->map_queue(q, ctx->cpu);
818
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600819 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
820 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
821 blk_insert_flush(rq);
822 } else {
823 spin_lock(&ctx->lock);
824 __blk_mq_insert_request(hctx, rq, at_head);
825 spin_unlock(&ctx->lock);
826 }
Jens Axboe320ae512013-10-24 09:20:05 +0100827
Jens Axboe320ae512013-10-24 09:20:05 +0100828 if (run_queue)
829 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600830
831 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100832}
833
834static void blk_mq_insert_requests(struct request_queue *q,
835 struct blk_mq_ctx *ctx,
836 struct list_head *list,
837 int depth,
838 bool from_schedule)
839
840{
841 struct blk_mq_hw_ctx *hctx;
842 struct blk_mq_ctx *current_ctx;
843
844 trace_block_unplug(q, depth, !from_schedule);
845
846 current_ctx = blk_mq_get_ctx(q);
847
848 if (!cpu_online(ctx->cpu))
849 ctx = current_ctx;
850 hctx = q->mq_ops->map_queue(q, ctx->cpu);
851
852 /*
853 * preemption doesn't flush plug list, so it's possible ctx->cpu is
854 * offline now
855 */
856 spin_lock(&ctx->lock);
857 while (!list_empty(list)) {
858 struct request *rq;
859
860 rq = list_first_entry(list, struct request, queuelist);
861 list_del_init(&rq->queuelist);
862 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800863 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100864 }
865 spin_unlock(&ctx->lock);
866
Jens Axboe320ae512013-10-24 09:20:05 +0100867 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600868 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100869}
870
871static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
872{
873 struct request *rqa = container_of(a, struct request, queuelist);
874 struct request *rqb = container_of(b, struct request, queuelist);
875
876 return !(rqa->mq_ctx < rqb->mq_ctx ||
877 (rqa->mq_ctx == rqb->mq_ctx &&
878 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
879}
880
881void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
882{
883 struct blk_mq_ctx *this_ctx;
884 struct request_queue *this_q;
885 struct request *rq;
886 LIST_HEAD(list);
887 LIST_HEAD(ctx_list);
888 unsigned int depth;
889
890 list_splice_init(&plug->mq_list, &list);
891
892 list_sort(NULL, &list, plug_ctx_cmp);
893
894 this_q = NULL;
895 this_ctx = NULL;
896 depth = 0;
897
898 while (!list_empty(&list)) {
899 rq = list_entry_rq(list.next);
900 list_del_init(&rq->queuelist);
901 BUG_ON(!rq->q);
902 if (rq->mq_ctx != this_ctx) {
903 if (this_ctx) {
904 blk_mq_insert_requests(this_q, this_ctx,
905 &ctx_list, depth,
906 from_schedule);
907 }
908
909 this_ctx = rq->mq_ctx;
910 this_q = rq->q;
911 depth = 0;
912 }
913
914 depth++;
915 list_add_tail(&rq->queuelist, &ctx_list);
916 }
917
918 /*
919 * If 'this_ctx' is set, we know we have entries to complete
920 * on 'ctx_list'. Do those.
921 */
922 if (this_ctx) {
923 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
924 from_schedule);
925 }
926}
927
928static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
929{
930 init_request_from_bio(rq, bio);
931 blk_account_io_start(rq, 1);
932}
933
934static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
935{
936 struct blk_mq_hw_ctx *hctx;
937 struct blk_mq_ctx *ctx;
938 const int is_sync = rw_is_sync(bio->bi_rw);
939 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
940 int rw = bio_data_dir(bio);
941 struct request *rq;
942 unsigned int use_plug, request_count = 0;
943
944 /*
945 * If we have multiple hardware queues, just go directly to
946 * one of those for sync IO.
947 */
948 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
949
950 blk_queue_bounce(q, &bio);
951
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -0700952 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
953 bio_endio(bio, -EIO);
954 return;
955 }
956
Jens Axboe320ae512013-10-24 09:20:05 +0100957 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
958 return;
959
960 if (blk_mq_queue_enter(q)) {
961 bio_endio(bio, -EIO);
962 return;
963 }
964
965 ctx = blk_mq_get_ctx(q);
966 hctx = q->mq_ops->map_queue(q, ctx->cpu);
967
Shaohua Li27fbf4e2014-02-19 20:20:21 +0800968 if (is_sync)
969 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +0100970 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700971 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100972 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -0700973 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100974 else {
975 blk_mq_put_ctx(ctx);
976 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700977 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
978 false);
Jens Axboe320ae512013-10-24 09:20:05 +0100979 ctx = rq->mq_ctx;
980 hctx = q->mq_ops->map_queue(q, ctx->cpu);
981 }
982
983 hctx->queued++;
984
985 if (unlikely(is_flush_fua)) {
986 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +0100987 blk_insert_flush(rq);
988 goto run_queue;
989 }
990
991 /*
992 * A task plug currently exists. Since this is completely lockless,
993 * utilize that to temporarily store requests until the task is
994 * either done or scheduled away.
995 */
996 if (use_plug) {
997 struct blk_plug *plug = current->plug;
998
999 if (plug) {
1000 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001001 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001002 trace_block_plug(q);
1003 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1004 blk_flush_plug_list(plug, false);
1005 trace_block_plug(q);
1006 }
1007 list_add_tail(&rq->queuelist, &plug->mq_list);
1008 blk_mq_put_ctx(ctx);
1009 return;
1010 }
1011 }
1012
1013 spin_lock(&ctx->lock);
1014
1015 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1016 blk_mq_attempt_merge(q, ctx, bio))
1017 __blk_mq_free_request(hctx, ctx, rq);
1018 else {
1019 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001020 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001021 }
1022
1023 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001024
1025 /*
1026 * For a SYNC request, send it to the hardware immediately. For an
1027 * ASYNC request, just ensure that we run it later on. The latter
1028 * allows for merging opportunities and more efficient dispatching.
1029 */
1030run_queue:
1031 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -06001032 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001033}
1034
1035/*
1036 * Default mapping to a software queue, since we use one per CPU.
1037 */
1038struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1039{
1040 return q->queue_hw_ctx[q->mq_map[cpu]];
1041}
1042EXPORT_SYMBOL(blk_mq_map_queue);
1043
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001044struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
Jens Axboe320ae512013-10-24 09:20:05 +01001045 unsigned int hctx_index)
1046{
1047 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001048 GFP_KERNEL | __GFP_ZERO, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001049}
1050EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1051
1052void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1053 unsigned int hctx_index)
1054{
1055 kfree(hctx);
1056}
1057EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1058
1059static void blk_mq_hctx_notify(void *data, unsigned long action,
1060 unsigned int cpu)
1061{
1062 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -06001063 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +01001064 struct blk_mq_ctx *ctx;
1065 LIST_HEAD(tmp);
1066
1067 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1068 return;
1069
1070 /*
1071 * Move ctx entries to new CPU, if this one is going away.
1072 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001073 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001074
1075 spin_lock(&ctx->lock);
1076 if (!list_empty(&ctx->rq_list)) {
1077 list_splice_init(&ctx->rq_list, &tmp);
1078 clear_bit(ctx->index_hw, hctx->ctx_map);
1079 }
1080 spin_unlock(&ctx->lock);
1081
1082 if (list_empty(&tmp))
1083 return;
1084
Jens Axboebccb5f72014-04-04 21:34:48 -06001085 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001086 spin_lock(&ctx->lock);
1087
1088 while (!list_empty(&tmp)) {
1089 struct request *rq;
1090
1091 rq = list_first_entry(&tmp, struct request, queuelist);
1092 rq->mq_ctx = ctx;
1093 list_move_tail(&rq->queuelist, &ctx->rq_list);
1094 }
1095
Jens Axboebccb5f72014-04-04 21:34:48 -06001096 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001097 blk_mq_hctx_mark_pending(hctx, ctx);
1098
1099 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001100
1101 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001102 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001103}
1104
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001105static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1106 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001107{
1108 struct page *page;
1109
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001110 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001111 int i;
1112
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001113 for (i = 0; i < tags->nr_tags; i++) {
1114 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001115 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001116 set->ops->exit_request(set->driver_data, tags->rqs[i],
1117 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001118 }
1119 }
1120
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001121 while (!list_empty(&tags->page_list)) {
1122 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001123 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001124 __free_pages(page, page->private);
1125 }
1126
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001127 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001128
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001129 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001130}
1131
1132static size_t order_to_size(unsigned int order)
1133{
Ming Lei4ca08502014-04-19 18:00:18 +08001134 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001135}
1136
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001137static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1138 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001139{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001140 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001141 unsigned int i, j, entries_per_page, max_order = 4;
1142 size_t rq_size, left;
1143
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001144 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1145 set->numa_node);
1146 if (!tags)
1147 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001148
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001149 INIT_LIST_HEAD(&tags->page_list);
1150
1151 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1152 GFP_KERNEL, set->numa_node);
1153 if (!tags->rqs) {
1154 blk_mq_free_tags(tags);
1155 return NULL;
1156 }
Jens Axboe320ae512013-10-24 09:20:05 +01001157
1158 /*
1159 * rq_size is the size of the request plus driver payload, rounded
1160 * to the cacheline size
1161 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001162 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001163 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001164 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001165
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001166 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001167 int this_order = max_order;
1168 struct page *page;
1169 int to_do;
1170 void *p;
1171
1172 while (left < order_to_size(this_order - 1) && this_order)
1173 this_order--;
1174
1175 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001176 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1177 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001178 if (page)
1179 break;
1180 if (!this_order--)
1181 break;
1182 if (order_to_size(this_order) < rq_size)
1183 break;
1184 } while (1);
1185
1186 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001187 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001188
1189 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001190 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001191
1192 p = page_address(page);
1193 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001194 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001195 left -= to_do * rq_size;
1196 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001197 tags->rqs[i] = p;
Ming Lei6a3c8a32014-04-19 18:00:19 +08001198 blk_rq_init(NULL, tags->rqs[i]);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001199 if (set->ops->init_request) {
1200 if (set->ops->init_request(set->driver_data,
1201 tags->rqs[i], hctx_idx, i,
1202 set->numa_node))
1203 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001204 }
1205
Jens Axboe320ae512013-10-24 09:20:05 +01001206 p += rq_size;
1207 i++;
1208 }
1209 }
1210
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001211 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001212
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001213fail:
1214 pr_warn("%s: failed to allocate requests\n", __func__);
1215 blk_mq_free_rq_map(set, tags, hctx_idx);
1216 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001217}
1218
1219static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001220 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001221{
1222 struct blk_mq_hw_ctx *hctx;
1223 unsigned int i, j;
1224
1225 /*
1226 * Initialize hardware queues
1227 */
1228 queue_for_each_hw_ctx(q, hctx, i) {
1229 unsigned int num_maps;
1230 int node;
1231
1232 node = hctx->numa_node;
1233 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001234 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001235
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001236 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1237 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001238 spin_lock_init(&hctx->lock);
1239 INIT_LIST_HEAD(&hctx->dispatch);
1240 hctx->queue = q;
1241 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001242 hctx->flags = set->flags;
1243 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001244
1245 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1246 blk_mq_hctx_notify, hctx);
1247 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1248
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001249 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001250
1251 /*
1252 * Allocate space for all possible cpus to avoid allocation in
1253 * runtime
1254 */
1255 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1256 GFP_KERNEL, node);
1257 if (!hctx->ctxs)
1258 break;
1259
1260 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1261 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1262 GFP_KERNEL, node);
1263 if (!hctx->ctx_map)
1264 break;
1265
1266 hctx->nr_ctx_map = num_maps;
1267 hctx->nr_ctx = 0;
1268
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001269 if (set->ops->init_hctx &&
1270 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001271 break;
1272 }
1273
1274 if (i == q->nr_hw_queues)
1275 return 0;
1276
1277 /*
1278 * Init failed
1279 */
1280 queue_for_each_hw_ctx(q, hctx, j) {
1281 if (i == j)
1282 break;
1283
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001284 if (set->ops->exit_hctx)
1285 set->ops->exit_hctx(hctx, j);
Jens Axboe320ae512013-10-24 09:20:05 +01001286
1287 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Jens Axboe320ae512013-10-24 09:20:05 +01001288 kfree(hctx->ctxs);
Ming Lei11471e02014-04-19 18:00:16 +08001289 kfree(hctx->ctx_map);
Jens Axboe320ae512013-10-24 09:20:05 +01001290 }
1291
1292 return 1;
1293}
1294
1295static void blk_mq_init_cpu_queues(struct request_queue *q,
1296 unsigned int nr_hw_queues)
1297{
1298 unsigned int i;
1299
1300 for_each_possible_cpu(i) {
1301 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1302 struct blk_mq_hw_ctx *hctx;
1303
1304 memset(__ctx, 0, sizeof(*__ctx));
1305 __ctx->cpu = i;
1306 spin_lock_init(&__ctx->lock);
1307 INIT_LIST_HEAD(&__ctx->rq_list);
1308 __ctx->queue = q;
1309
1310 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001311 if (!cpu_online(i))
1312 continue;
1313
Jens Axboee4043dc2014-04-09 10:18:23 -06001314 hctx = q->mq_ops->map_queue(q, i);
1315 cpumask_set_cpu(i, hctx->cpumask);
1316 hctx->nr_ctx++;
1317
Jens Axboe320ae512013-10-24 09:20:05 +01001318 /*
1319 * Set local node, IFF we have more than one hw queue. If
1320 * not, we remain on the home node of the device
1321 */
1322 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1323 hctx->numa_node = cpu_to_node(i);
1324 }
1325}
1326
1327static void blk_mq_map_swqueue(struct request_queue *q)
1328{
1329 unsigned int i;
1330 struct blk_mq_hw_ctx *hctx;
1331 struct blk_mq_ctx *ctx;
1332
1333 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001334 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001335 hctx->nr_ctx = 0;
1336 }
1337
1338 /*
1339 * Map software to hardware queues
1340 */
1341 queue_for_each_ctx(q, ctx, i) {
1342 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001343 if (!cpu_online(i))
1344 continue;
1345
Jens Axboe320ae512013-10-24 09:20:05 +01001346 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001347 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001348 ctx->index_hw = hctx->nr_ctx;
1349 hctx->ctxs[hctx->nr_ctx++] = ctx;
1350 }
1351}
1352
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001353struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001354{
1355 struct blk_mq_hw_ctx **hctxs;
1356 struct blk_mq_ctx *ctx;
1357 struct request_queue *q;
1358 int i;
1359
Jens Axboe320ae512013-10-24 09:20:05 +01001360 ctx = alloc_percpu(struct blk_mq_ctx);
1361 if (!ctx)
1362 return ERR_PTR(-ENOMEM);
1363
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001364 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1365 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001366
1367 if (!hctxs)
1368 goto err_percpu;
1369
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001370 for (i = 0; i < set->nr_hw_queues; i++) {
1371 hctxs[i] = set->ops->alloc_hctx(set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001372 if (!hctxs[i])
1373 goto err_hctxs;
1374
Jens Axboee4043dc2014-04-09 10:18:23 -06001375 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1376 goto err_hctxs;
1377
Jens Axboe320ae512013-10-24 09:20:05 +01001378 hctxs[i]->numa_node = NUMA_NO_NODE;
1379 hctxs[i]->queue_num = i;
1380 }
1381
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001382 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001383 if (!q)
1384 goto err_hctxs;
1385
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001386 q->mq_map = blk_mq_make_queue_map(set);
Jens Axboe320ae512013-10-24 09:20:05 +01001387 if (!q->mq_map)
1388 goto err_map;
1389
1390 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1391 blk_queue_rq_timeout(q, 30000);
1392
1393 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001394 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboe320ae512013-10-24 09:20:05 +01001395
1396 q->queue_ctx = ctx;
1397 q->queue_hw_ctx = hctxs;
1398
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001399 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001400 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001401
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001402 q->sg_reserved_size = INT_MAX;
1403
Jens Axboe320ae512013-10-24 09:20:05 +01001404 blk_queue_make_request(q, blk_mq_make_request);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001405 blk_queue_rq_timed_out(q, set->ops->timeout);
1406 if (set->timeout)
1407 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001408
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001409 if (set->ops->complete)
1410 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001411
Jens Axboe320ae512013-10-24 09:20:05 +01001412 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001413 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001414
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001415 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1416 set->cmd_size, cache_line_size()),
1417 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001418 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001419 goto err_hw;
1420
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001421 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001422 goto err_flush_rq;
1423
Jens Axboe320ae512013-10-24 09:20:05 +01001424 blk_mq_map_swqueue(q);
1425
1426 mutex_lock(&all_q_mutex);
1427 list_add_tail(&q->all_q_node, &all_q_list);
1428 mutex_unlock(&all_q_mutex);
1429
1430 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001431
1432err_flush_rq:
1433 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001434err_hw:
1435 kfree(q->mq_map);
1436err_map:
1437 blk_cleanup_queue(q);
1438err_hctxs:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001439 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001440 if (!hctxs[i])
1441 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001442 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001443 set->ops->free_hctx(hctxs[i], i);
Jens Axboe320ae512013-10-24 09:20:05 +01001444 }
1445 kfree(hctxs);
1446err_percpu:
1447 free_percpu(ctx);
1448 return ERR_PTR(-ENOMEM);
1449}
1450EXPORT_SYMBOL(blk_mq_init_queue);
1451
1452void blk_mq_free_queue(struct request_queue *q)
1453{
1454 struct blk_mq_hw_ctx *hctx;
1455 int i;
1456
1457 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001458 kfree(hctx->ctx_map);
1459 kfree(hctx->ctxs);
Jens Axboe320ae512013-10-24 09:20:05 +01001460 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1461 if (q->mq_ops->exit_hctx)
1462 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001463 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001464 q->mq_ops->free_hctx(hctx, i);
1465 }
1466
1467 free_percpu(q->queue_ctx);
1468 kfree(q->queue_hw_ctx);
1469 kfree(q->mq_map);
1470
1471 q->queue_ctx = NULL;
1472 q->queue_hw_ctx = NULL;
1473 q->mq_map = NULL;
1474
1475 mutex_lock(&all_q_mutex);
1476 list_del_init(&q->all_q_node);
1477 mutex_unlock(&all_q_mutex);
1478}
Jens Axboe320ae512013-10-24 09:20:05 +01001479
1480/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001481static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001482{
1483 blk_mq_freeze_queue(q);
1484
1485 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1486
1487 /*
1488 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1489 * we should change hctx numa_node according to new topology (this
1490 * involves free and re-allocate memory, worthy doing?)
1491 */
1492
1493 blk_mq_map_swqueue(q);
1494
1495 blk_mq_unfreeze_queue(q);
1496}
1497
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001498static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1499 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001500{
1501 struct request_queue *q;
1502
1503 /*
1504 * Before new mapping is established, hotadded cpu might already start
1505 * handling requests. This doesn't break anything as we map offline
1506 * CPUs to first hardware queue. We will re-init queue below to get
1507 * optimal settings.
1508 */
1509 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1510 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1511 return NOTIFY_OK;
1512
1513 mutex_lock(&all_q_mutex);
1514 list_for_each_entry(q, &all_q_list, all_q_node)
1515 blk_mq_queue_reinit(q);
1516 mutex_unlock(&all_q_mutex);
1517 return NOTIFY_OK;
1518}
1519
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001520int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1521{
1522 int i;
1523
1524 if (!set->nr_hw_queues)
1525 return -EINVAL;
1526 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1527 return -EINVAL;
1528 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1529 return -EINVAL;
1530
1531 if (!set->nr_hw_queues ||
1532 !set->ops->queue_rq || !set->ops->map_queue ||
1533 !set->ops->alloc_hctx || !set->ops->free_hctx)
1534 return -EINVAL;
1535
1536
Ming Lei48479002014-04-19 18:00:17 +08001537 set->tags = kmalloc_node(set->nr_hw_queues *
1538 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001539 GFP_KERNEL, set->numa_node);
1540 if (!set->tags)
1541 goto out;
1542
1543 for (i = 0; i < set->nr_hw_queues; i++) {
1544 set->tags[i] = blk_mq_init_rq_map(set, i);
1545 if (!set->tags[i])
1546 goto out_unwind;
1547 }
1548
1549 return 0;
1550
1551out_unwind:
1552 while (--i >= 0)
1553 blk_mq_free_rq_map(set, set->tags[i], i);
1554out:
1555 return -ENOMEM;
1556}
1557EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1558
1559void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1560{
1561 int i;
1562
1563 for (i = 0; i < set->nr_hw_queues; i++)
1564 blk_mq_free_rq_map(set, set->tags[i], i);
Ming Lei981bd182014-04-24 00:07:34 +08001565 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001566}
1567EXPORT_SYMBOL(blk_mq_free_tag_set);
1568
Jens Axboe676141e2014-03-20 13:29:18 -06001569void blk_mq_disable_hotplug(void)
1570{
1571 mutex_lock(&all_q_mutex);
1572}
1573
1574void blk_mq_enable_hotplug(void)
1575{
1576 mutex_unlock(&all_q_mutex);
1577}
1578
Jens Axboe320ae512013-10-24 09:20:05 +01001579static int __init blk_mq_init(void)
1580{
Jens Axboe320ae512013-10-24 09:20:05 +01001581 blk_mq_cpu_init();
1582
1583 /* Must be called after percpu_counter_hotcpu_callback() */
1584 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1585
1586 return 0;
1587}
1588subsys_initcall(blk_mq_init);