blob: 3a560a4db0b4047f1d5c295208aaeb02fb69b328 [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];
Christoph Hellwiged448322014-04-14 10:30:10 +020085 blk_rq_init(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +010086 rq->tag = tag;
87
88 return rq;
89 }
90
91 return NULL;
92}
93
94static int blk_mq_queue_enter(struct request_queue *q)
95{
96 int ret;
97
98 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
99 smp_wmb();
100 /* we have problems to freeze the queue if it's initializing */
101 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
102 return 0;
103
104 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
105
106 spin_lock_irq(q->queue_lock);
107 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800108 !blk_queue_bypass(q) || blk_queue_dying(q),
109 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100110 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800111 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100112 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800113 else if (blk_queue_dying(q))
114 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100115 spin_unlock_irq(q->queue_lock);
116
117 return ret;
118}
119
120static void blk_mq_queue_exit(struct request_queue *q)
121{
122 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
123}
124
Ming Lei43a5e4e2013-12-26 21:31:35 +0800125static void __blk_mq_drain_queue(struct request_queue *q)
126{
127 while (true) {
128 s64 count;
129
130 spin_lock_irq(q->queue_lock);
131 count = percpu_counter_sum(&q->mq_usage_counter);
132 spin_unlock_irq(q->queue_lock);
133
134 if (count == 0)
135 break;
136 blk_mq_run_queues(q, false);
137 msleep(10);
138 }
139}
140
Jens Axboe320ae512013-10-24 09:20:05 +0100141/*
142 * Guarantee no request is in use, so we can change any data structure of
143 * the queue afterward.
144 */
145static void blk_mq_freeze_queue(struct request_queue *q)
146{
147 bool drain;
148
149 spin_lock_irq(q->queue_lock);
150 drain = !q->bypass_depth++;
151 queue_flag_set(QUEUE_FLAG_BYPASS, q);
152 spin_unlock_irq(q->queue_lock);
153
Ming Lei43a5e4e2013-12-26 21:31:35 +0800154 if (drain)
155 __blk_mq_drain_queue(q);
156}
Jens Axboe320ae512013-10-24 09:20:05 +0100157
Ming Lei43a5e4e2013-12-26 21:31:35 +0800158void blk_mq_drain_queue(struct request_queue *q)
159{
160 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100161}
162
163static void blk_mq_unfreeze_queue(struct request_queue *q)
164{
165 bool wake = false;
166
167 spin_lock_irq(q->queue_lock);
168 if (!--q->bypass_depth) {
169 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
170 wake = true;
171 }
172 WARN_ON_ONCE(q->bypass_depth < 0);
173 spin_unlock_irq(q->queue_lock);
174 if (wake)
175 wake_up_all(&q->mq_freeze_wq);
176}
177
178bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
179{
180 return blk_mq_has_free_tags(hctx->tags);
181}
182EXPORT_SYMBOL(blk_mq_can_queue);
183
Jens Axboe94eddfb2013-11-19 09:25:07 -0700184static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
185 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100186{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700187 if (blk_queue_io_stat(q))
188 rw_flags |= REQ_IO_STAT;
189
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
Jens Axboe320ae512013-10-24 09:20:05 +0100261 blk_mq_put_tag(hctx->tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100262 blk_mq_queue_exit(q);
263}
264
265void blk_mq_free_request(struct request *rq)
266{
267 struct blk_mq_ctx *ctx = rq->mq_ctx;
268 struct blk_mq_hw_ctx *hctx;
269 struct request_queue *q = rq->q;
270
271 ctx->rq_completed[rq_is_sync(rq)]++;
272
273 hctx = q->mq_ops->map_queue(q, ctx->cpu);
274 __blk_mq_free_request(hctx, ctx, rq);
275}
276
Christoph Hellwig8727af42014-04-14 10:30:08 +0200277/*
278 * Clone all relevant state from a request that has been put on hold in
279 * the flush state machine into the preallocated flush request that hangs
280 * off the request queue.
281 *
282 * For a driver the flush request should be invisible, that's why we are
283 * impersonating the original request here.
284 */
285void blk_mq_clone_flush_request(struct request *flush_rq,
286 struct request *orig_rq)
287{
288 struct blk_mq_hw_ctx *hctx =
289 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
290
291 flush_rq->mq_ctx = orig_rq->mq_ctx;
292 flush_rq->tag = orig_rq->tag;
293 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
294 hctx->cmd_size);
295}
296
Christoph Hellwig63151a42014-04-16 09:44:52 +0200297inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100298{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700299 blk_account_io_done(rq);
300
Christoph Hellwig91b63632014-04-16 09:44:53 +0200301 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100302 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200303 } else {
304 if (unlikely(blk_bidi_rq(rq)))
305 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100306 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200307 }
Jens Axboe320ae512013-10-24 09:20:05 +0100308}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200309EXPORT_SYMBOL(__blk_mq_end_io);
310
311void blk_mq_end_io(struct request *rq, int error)
312{
313 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
314 BUG();
315 __blk_mq_end_io(rq, error);
316}
317EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100318
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800319static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100320{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800321 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100322
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800323 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100324}
325
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800326void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100327{
328 struct blk_mq_ctx *ctx = rq->mq_ctx;
329 int cpu;
330
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800331 if (!ctx->ipi_redirect) {
332 rq->q->softirq_done_fn(rq);
333 return;
334 }
Jens Axboe320ae512013-10-24 09:20:05 +0100335
336 cpu = get_cpu();
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800337 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800338 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800339 rq->csd.info = rq;
340 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100341 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800342 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800343 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800344 }
Jens Axboe320ae512013-10-24 09:20:05 +0100345 put_cpu();
346}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800347
348/**
349 * blk_mq_complete_request - end I/O on a request
350 * @rq: the request being processed
351 *
352 * Description:
353 * Ends all I/O on a request. It does not handle partial completions.
354 * The actual completion happens out-of-order, through a IPI handler.
355 **/
356void blk_mq_complete_request(struct request *rq)
357{
358 if (unlikely(blk_should_fake_timeout(rq->q)))
359 return;
360 if (!blk_mark_rq_complete(rq))
361 __blk_mq_complete_request(rq);
362}
363EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100364
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800365static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100366{
367 struct request_queue *q = rq->q;
368
369 trace_block_rq_issue(q, rq);
370
Christoph Hellwig742ee692014-04-14 10:30:06 +0200371 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200372 if (unlikely(blk_bidi_rq(rq)))
373 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200374
Jens Axboe320ae512013-10-24 09:20:05 +0100375 /*
376 * Just mark start time and set the started bit. Due to memory
377 * ordering, we know we'll see the correct deadline as long as
378 * REQ_ATOMIC_STARTED is seen.
379 */
380 rq->deadline = jiffies + q->rq_timeout;
381 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800382
383 if (q->dma_drain_size && blk_rq_bytes(rq)) {
384 /*
385 * Make sure space for the drain appears. We know we can do
386 * this because max_hw_segments has been adjusted to be one
387 * fewer than the device can handle.
388 */
389 rq->nr_phys_segments++;
390 }
391
392 /*
393 * Flag the last request in the series so that drivers know when IO
394 * should be kicked off, if they don't do it on a per-request basis.
395 *
396 * Note: the flag isn't the only condition drivers should do kick off.
397 * If drive is busy, the last request might not have the bit set.
398 */
399 if (last)
400 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100401}
402
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200403static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100404{
405 struct request_queue *q = rq->q;
406
407 trace_block_rq_requeue(q, rq);
408 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800409
410 rq->cmd_flags &= ~REQ_END;
411
412 if (q->dma_drain_size && blk_rq_bytes(rq))
413 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100414}
415
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200416void blk_mq_requeue_request(struct request *rq)
417{
418 struct request_queue *q = rq->q;
419
420 __blk_mq_requeue_request(rq);
421 blk_clear_rq_complete(rq);
422
423 trace_block_rq_requeue(q, rq);
424
425 BUG_ON(blk_queued_rq(rq));
426 blk_mq_insert_request(rq, true, true, false);
427}
428EXPORT_SYMBOL(blk_mq_requeue_request);
429
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600430struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
431{
432 return tags->rqs[tag];
433}
434EXPORT_SYMBOL(blk_mq_tag_to_rq);
435
Jens Axboe320ae512013-10-24 09:20:05 +0100436struct blk_mq_timeout_data {
437 struct blk_mq_hw_ctx *hctx;
438 unsigned long *next;
439 unsigned int *next_set;
440};
441
442static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
443{
444 struct blk_mq_timeout_data *data = __data;
445 struct blk_mq_hw_ctx *hctx = data->hctx;
446 unsigned int tag;
447
448 /* It may not be in flight yet (this is where
449 * the REQ_ATOMIC_STARTED flag comes in). The requests are
450 * statically allocated, so we know it's always safe to access the
451 * memory associated with a bit offset into ->rqs[].
452 */
453 tag = 0;
454 do {
455 struct request *rq;
456
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600457 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
458 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100459 break;
460
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600461 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
462 if (rq->q != hctx->queue)
463 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100464 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
465 continue;
466
467 blk_rq_check_expired(rq, data->next, data->next_set);
468 } while (1);
469}
470
471static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
472 unsigned long *next,
473 unsigned int *next_set)
474{
475 struct blk_mq_timeout_data data = {
476 .hctx = hctx,
477 .next = next,
478 .next_set = next_set,
479 };
480
481 /*
482 * Ask the tagging code to iterate busy requests, so we can
483 * check them for timeout.
484 */
485 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
486}
487
488static void blk_mq_rq_timer(unsigned long data)
489{
490 struct request_queue *q = (struct request_queue *) data;
491 struct blk_mq_hw_ctx *hctx;
492 unsigned long next = 0;
493 int i, next_set = 0;
494
495 queue_for_each_hw_ctx(q, hctx, i)
496 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
497
498 if (next_set)
499 mod_timer(&q->timeout, round_jiffies_up(next));
500}
501
502/*
503 * Reverse check our software queue for entries that we could potentially
504 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
505 * too much time checking for merges.
506 */
507static bool blk_mq_attempt_merge(struct request_queue *q,
508 struct blk_mq_ctx *ctx, struct bio *bio)
509{
510 struct request *rq;
511 int checked = 8;
512
513 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
514 int el_ret;
515
516 if (!checked--)
517 break;
518
519 if (!blk_rq_merge_ok(rq, bio))
520 continue;
521
522 el_ret = blk_try_merge(rq, bio);
523 if (el_ret == ELEVATOR_BACK_MERGE) {
524 if (bio_attempt_back_merge(q, rq, bio)) {
525 ctx->rq_merged++;
526 return true;
527 }
528 break;
529 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
530 if (bio_attempt_front_merge(q, rq, bio)) {
531 ctx->rq_merged++;
532 return true;
533 }
534 break;
535 }
536 }
537
538 return false;
539}
540
541void blk_mq_add_timer(struct request *rq)
542{
543 __blk_add_timer(rq, NULL);
544}
545
546/*
547 * Run this hardware queue, pulling any software queues mapped to it in.
548 * Note that this function currently has various problems around ordering
549 * of IO. In particular, we'd like FIFO behaviour on handling existing
550 * items on the hctx->dispatch list. Ignore that for now.
551 */
552static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
553{
554 struct request_queue *q = hctx->queue;
555 struct blk_mq_ctx *ctx;
556 struct request *rq;
557 LIST_HEAD(rq_list);
558 int bit, queued;
559
Jens Axboefd1270d2014-04-16 09:23:48 -0600560 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600561
Jens Axboe5d12f902014-03-19 15:25:02 -0600562 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100563 return;
564
565 hctx->run++;
566
567 /*
568 * Touch any software queue that has pending entries.
569 */
570 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
571 clear_bit(bit, hctx->ctx_map);
572 ctx = hctx->ctxs[bit];
573 BUG_ON(bit != ctx->index_hw);
574
575 spin_lock(&ctx->lock);
576 list_splice_tail_init(&ctx->rq_list, &rq_list);
577 spin_unlock(&ctx->lock);
578 }
579
580 /*
581 * If we have previous entries on our dispatch list, grab them
582 * and stuff them at the front for more fair dispatch.
583 */
584 if (!list_empty_careful(&hctx->dispatch)) {
585 spin_lock(&hctx->lock);
586 if (!list_empty(&hctx->dispatch))
587 list_splice_init(&hctx->dispatch, &rq_list);
588 spin_unlock(&hctx->lock);
589 }
590
591 /*
592 * Delete and return all entries from our dispatch list
593 */
594 queued = 0;
595
596 /*
597 * Now process all the entries, sending them to the driver.
598 */
599 while (!list_empty(&rq_list)) {
600 int ret;
601
602 rq = list_first_entry(&rq_list, struct request, queuelist);
603 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100604
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800605 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100606
607 ret = q->mq_ops->queue_rq(hctx, rq);
608 switch (ret) {
609 case BLK_MQ_RQ_QUEUE_OK:
610 queued++;
611 continue;
612 case BLK_MQ_RQ_QUEUE_BUSY:
613 /*
614 * FIXME: we should have a mechanism to stop the queue
615 * like blk_stop_queue, otherwise we will waste cpu
616 * time
617 */
618 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200619 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100620 break;
621 default:
622 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100623 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800624 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100625 blk_mq_end_io(rq, rq->errors);
626 break;
627 }
628
629 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
630 break;
631 }
632
633 if (!queued)
634 hctx->dispatched[0]++;
635 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
636 hctx->dispatched[ilog2(queued) + 1]++;
637
638 /*
639 * Any items that need requeuing? Stuff them into hctx->dispatch,
640 * that is where we will continue on next queue run.
641 */
642 if (!list_empty(&rq_list)) {
643 spin_lock(&hctx->lock);
644 list_splice(&rq_list, &hctx->dispatch);
645 spin_unlock(&hctx->lock);
646 }
647}
648
649void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
650{
Jens Axboe5d12f902014-03-19 15:25:02 -0600651 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100652 return;
653
Jens Axboee4043dc2014-04-09 10:18:23 -0600654 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100655 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600656 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600657 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600658 else {
659 unsigned int cpu;
660
661 /*
662 * It'd be great if the workqueue API had a way to pass
663 * in a mask and had some smarts for more clever placement
664 * than the first CPU. Or we could round-robin here. For now,
665 * just queue on the first CPU.
666 */
667 cpu = cpumask_first(hctx->cpumask);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600668 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600669 }
Jens Axboe320ae512013-10-24 09:20:05 +0100670}
671
672void blk_mq_run_queues(struct request_queue *q, bool async)
673{
674 struct blk_mq_hw_ctx *hctx;
675 int i;
676
677 queue_for_each_hw_ctx(q, hctx, i) {
678 if ((!blk_mq_hctx_has_pending(hctx) &&
679 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600680 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100681 continue;
682
Jens Axboee4043dc2014-04-09 10:18:23 -0600683 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100684 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600685 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100686 }
687}
688EXPORT_SYMBOL(blk_mq_run_queues);
689
690void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
691{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600692 cancel_delayed_work(&hctx->run_work);
693 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100694 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
695}
696EXPORT_SYMBOL(blk_mq_stop_hw_queue);
697
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100698void blk_mq_stop_hw_queues(struct request_queue *q)
699{
700 struct blk_mq_hw_ctx *hctx;
701 int i;
702
703 queue_for_each_hw_ctx(q, hctx, i)
704 blk_mq_stop_hw_queue(hctx);
705}
706EXPORT_SYMBOL(blk_mq_stop_hw_queues);
707
Jens Axboe320ae512013-10-24 09:20:05 +0100708void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
709{
710 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600711
712 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100713 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600714 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100715}
716EXPORT_SYMBOL(blk_mq_start_hw_queue);
717
Christoph Hellwig2f268552014-04-16 09:44:56 +0200718void blk_mq_start_hw_queues(struct request_queue *q)
719{
720 struct blk_mq_hw_ctx *hctx;
721 int i;
722
723 queue_for_each_hw_ctx(q, hctx, i)
724 blk_mq_start_hw_queue(hctx);
725}
726EXPORT_SYMBOL(blk_mq_start_hw_queues);
727
728
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200729void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100730{
731 struct blk_mq_hw_ctx *hctx;
732 int i;
733
734 queue_for_each_hw_ctx(q, hctx, i) {
735 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
736 continue;
737
738 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600739 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200740 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600741 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100742 }
743}
744EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
745
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600746static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100747{
748 struct blk_mq_hw_ctx *hctx;
749
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600750 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600751
Jens Axboe320ae512013-10-24 09:20:05 +0100752 __blk_mq_run_hw_queue(hctx);
753}
754
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600755static void blk_mq_delay_work_fn(struct work_struct *work)
756{
757 struct blk_mq_hw_ctx *hctx;
758
759 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
760
761 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
762 __blk_mq_run_hw_queue(hctx);
763}
764
765void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
766{
767 unsigned long tmo = msecs_to_jiffies(msecs);
768
769 if (hctx->queue->nr_hw_queues == 1)
770 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
771 else {
772 unsigned int cpu;
773
774 /*
775 * It'd be great if the workqueue API had a way to pass
776 * in a mask and had some smarts for more clever placement
777 * than the first CPU. Or we could round-robin here. For now,
778 * just queue on the first CPU.
779 */
780 cpu = cpumask_first(hctx->cpumask);
781 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
782 }
783}
784EXPORT_SYMBOL(blk_mq_delay_queue);
785
Jens Axboe320ae512013-10-24 09:20:05 +0100786static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800787 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100788{
789 struct blk_mq_ctx *ctx = rq->mq_ctx;
790
Jens Axboe01b983c2013-11-19 18:59:10 -0700791 trace_block_rq_insert(hctx->queue, rq);
792
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800793 if (at_head)
794 list_add(&rq->queuelist, &ctx->rq_list);
795 else
796 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100797 blk_mq_hctx_mark_pending(hctx, ctx);
798
799 /*
800 * We do this early, to ensure we are on the right CPU.
801 */
802 blk_mq_add_timer(rq);
803}
804
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600805void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
806 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100807{
808 struct request_queue *q = rq->q;
809 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600810 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100811
812 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600813 if (!cpu_online(ctx->cpu))
814 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100815
Jens Axboe320ae512013-10-24 09:20:05 +0100816 hctx = q->mq_ops->map_queue(q, ctx->cpu);
817
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600818 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
819 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
820 blk_insert_flush(rq);
821 } else {
822 spin_lock(&ctx->lock);
823 __blk_mq_insert_request(hctx, rq, at_head);
824 spin_unlock(&ctx->lock);
825 }
Jens Axboe320ae512013-10-24 09:20:05 +0100826
Jens Axboe320ae512013-10-24 09:20:05 +0100827 if (run_queue)
828 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600829
830 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100831}
832
833static void blk_mq_insert_requests(struct request_queue *q,
834 struct blk_mq_ctx *ctx,
835 struct list_head *list,
836 int depth,
837 bool from_schedule)
838
839{
840 struct blk_mq_hw_ctx *hctx;
841 struct blk_mq_ctx *current_ctx;
842
843 trace_block_unplug(q, depth, !from_schedule);
844
845 current_ctx = blk_mq_get_ctx(q);
846
847 if (!cpu_online(ctx->cpu))
848 ctx = current_ctx;
849 hctx = q->mq_ops->map_queue(q, ctx->cpu);
850
851 /*
852 * preemption doesn't flush plug list, so it's possible ctx->cpu is
853 * offline now
854 */
855 spin_lock(&ctx->lock);
856 while (!list_empty(list)) {
857 struct request *rq;
858
859 rq = list_first_entry(list, struct request, queuelist);
860 list_del_init(&rq->queuelist);
861 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800862 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100863 }
864 spin_unlock(&ctx->lock);
865
Jens Axboe320ae512013-10-24 09:20:05 +0100866 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600867 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100868}
869
870static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
871{
872 struct request *rqa = container_of(a, struct request, queuelist);
873 struct request *rqb = container_of(b, struct request, queuelist);
874
875 return !(rqa->mq_ctx < rqb->mq_ctx ||
876 (rqa->mq_ctx == rqb->mq_ctx &&
877 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
878}
879
880void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
881{
882 struct blk_mq_ctx *this_ctx;
883 struct request_queue *this_q;
884 struct request *rq;
885 LIST_HEAD(list);
886 LIST_HEAD(ctx_list);
887 unsigned int depth;
888
889 list_splice_init(&plug->mq_list, &list);
890
891 list_sort(NULL, &list, plug_ctx_cmp);
892
893 this_q = NULL;
894 this_ctx = NULL;
895 depth = 0;
896
897 while (!list_empty(&list)) {
898 rq = list_entry_rq(list.next);
899 list_del_init(&rq->queuelist);
900 BUG_ON(!rq->q);
901 if (rq->mq_ctx != this_ctx) {
902 if (this_ctx) {
903 blk_mq_insert_requests(this_q, this_ctx,
904 &ctx_list, depth,
905 from_schedule);
906 }
907
908 this_ctx = rq->mq_ctx;
909 this_q = rq->q;
910 depth = 0;
911 }
912
913 depth++;
914 list_add_tail(&rq->queuelist, &ctx_list);
915 }
916
917 /*
918 * If 'this_ctx' is set, we know we have entries to complete
919 * on 'ctx_list'. Do those.
920 */
921 if (this_ctx) {
922 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
923 from_schedule);
924 }
925}
926
927static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
928{
929 init_request_from_bio(rq, bio);
930 blk_account_io_start(rq, 1);
931}
932
933static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
934{
935 struct blk_mq_hw_ctx *hctx;
936 struct blk_mq_ctx *ctx;
937 const int is_sync = rw_is_sync(bio->bi_rw);
938 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
939 int rw = bio_data_dir(bio);
940 struct request *rq;
941 unsigned int use_plug, request_count = 0;
942
943 /*
944 * If we have multiple hardware queues, just go directly to
945 * one of those for sync IO.
946 */
947 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
948
949 blk_queue_bounce(q, &bio);
950
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -0700951 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
952 bio_endio(bio, -EIO);
953 return;
954 }
955
Jens Axboe320ae512013-10-24 09:20:05 +0100956 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
957 return;
958
959 if (blk_mq_queue_enter(q)) {
960 bio_endio(bio, -EIO);
961 return;
962 }
963
964 ctx = blk_mq_get_ctx(q);
965 hctx = q->mq_ops->map_queue(q, ctx->cpu);
966
Shaohua Li27fbf4e82014-02-19 20:20:21 +0800967 if (is_sync)
968 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +0100969 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700970 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100971 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -0700972 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100973 else {
974 blk_mq_put_ctx(ctx);
975 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700976 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
977 false);
Jens Axboe320ae512013-10-24 09:20:05 +0100978 ctx = rq->mq_ctx;
979 hctx = q->mq_ops->map_queue(q, ctx->cpu);
980 }
981
982 hctx->queued++;
983
984 if (unlikely(is_flush_fua)) {
985 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +0100986 blk_insert_flush(rq);
987 goto run_queue;
988 }
989
990 /*
991 * A task plug currently exists. Since this is completely lockless,
992 * utilize that to temporarily store requests until the task is
993 * either done or scheduled away.
994 */
995 if (use_plug) {
996 struct blk_plug *plug = current->plug;
997
998 if (plug) {
999 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001000 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001001 trace_block_plug(q);
1002 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1003 blk_flush_plug_list(plug, false);
1004 trace_block_plug(q);
1005 }
1006 list_add_tail(&rq->queuelist, &plug->mq_list);
1007 blk_mq_put_ctx(ctx);
1008 return;
1009 }
1010 }
1011
1012 spin_lock(&ctx->lock);
1013
1014 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1015 blk_mq_attempt_merge(q, ctx, bio))
1016 __blk_mq_free_request(hctx, ctx, rq);
1017 else {
1018 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001019 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001020 }
1021
1022 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001023
1024 /*
1025 * For a SYNC request, send it to the hardware immediately. For an
1026 * ASYNC request, just ensure that we run it later on. The latter
1027 * allows for merging opportunities and more efficient dispatching.
1028 */
1029run_queue:
1030 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -06001031 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001032}
1033
1034/*
1035 * Default mapping to a software queue, since we use one per CPU.
1036 */
1037struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1038{
1039 return q->queue_hw_ctx[q->mq_map[cpu]];
1040}
1041EXPORT_SYMBOL(blk_mq_map_queue);
1042
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001043struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
Jens Axboe320ae512013-10-24 09:20:05 +01001044 unsigned int hctx_index)
1045{
1046 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001047 GFP_KERNEL | __GFP_ZERO, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001048}
1049EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1050
1051void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1052 unsigned int hctx_index)
1053{
1054 kfree(hctx);
1055}
1056EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1057
1058static void blk_mq_hctx_notify(void *data, unsigned long action,
1059 unsigned int cpu)
1060{
1061 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -06001062 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +01001063 struct blk_mq_ctx *ctx;
1064 LIST_HEAD(tmp);
1065
1066 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1067 return;
1068
1069 /*
1070 * Move ctx entries to new CPU, if this one is going away.
1071 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001072 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001073
1074 spin_lock(&ctx->lock);
1075 if (!list_empty(&ctx->rq_list)) {
1076 list_splice_init(&ctx->rq_list, &tmp);
1077 clear_bit(ctx->index_hw, hctx->ctx_map);
1078 }
1079 spin_unlock(&ctx->lock);
1080
1081 if (list_empty(&tmp))
1082 return;
1083
Jens Axboebccb5f72014-04-04 21:34:48 -06001084 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001085 spin_lock(&ctx->lock);
1086
1087 while (!list_empty(&tmp)) {
1088 struct request *rq;
1089
1090 rq = list_first_entry(&tmp, struct request, queuelist);
1091 rq->mq_ctx = ctx;
1092 list_move_tail(&rq->queuelist, &ctx->rq_list);
1093 }
1094
Jens Axboebccb5f72014-04-04 21:34:48 -06001095 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001096 blk_mq_hctx_mark_pending(hctx, ctx);
1097
1098 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001099
1100 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001101 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001102}
1103
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001104static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1105 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001106{
1107 struct page *page;
1108
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001109 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001110 int i;
1111
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001112 for (i = 0; i < tags->nr_tags; i++) {
1113 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001114 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001115 set->ops->exit_request(set->driver_data, tags->rqs[i],
1116 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001117 }
1118 }
1119
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001120 while (!list_empty(&tags->page_list)) {
1121 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001122 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001123 __free_pages(page, page->private);
1124 }
1125
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001126 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001127
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001128 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001129}
1130
1131static size_t order_to_size(unsigned int order)
1132{
Ming Lei4ca08502014-04-19 18:00:18 +08001133 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001134}
1135
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001136static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1137 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001138{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001139 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001140 unsigned int i, j, entries_per_page, max_order = 4;
1141 size_t rq_size, left;
1142
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001143 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1144 set->numa_node);
1145 if (!tags)
1146 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001147
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001148 INIT_LIST_HEAD(&tags->page_list);
1149
1150 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1151 GFP_KERNEL, set->numa_node);
1152 if (!tags->rqs) {
1153 blk_mq_free_tags(tags);
1154 return NULL;
1155 }
Jens Axboe320ae512013-10-24 09:20:05 +01001156
1157 /*
1158 * rq_size is the size of the request plus driver payload, rounded
1159 * to the cacheline size
1160 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001161 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001162 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001163 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001164
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001165 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001166 int this_order = max_order;
1167 struct page *page;
1168 int to_do;
1169 void *p;
1170
1171 while (left < order_to_size(this_order - 1) && this_order)
1172 this_order--;
1173
1174 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001175 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1176 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001177 if (page)
1178 break;
1179 if (!this_order--)
1180 break;
1181 if (order_to_size(this_order) < rq_size)
1182 break;
1183 } while (1);
1184
1185 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001186 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001187
1188 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001189 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001190
1191 p = page_address(page);
1192 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001193 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001194 left -= to_do * rq_size;
1195 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001196 tags->rqs[i] = p;
1197 if (set->ops->init_request) {
1198 if (set->ops->init_request(set->driver_data,
1199 tags->rqs[i], hctx_idx, i,
1200 set->numa_node))
1201 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001202 }
1203
Jens Axboe320ae512013-10-24 09:20:05 +01001204 p += rq_size;
1205 i++;
1206 }
1207 }
1208
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001209 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001210
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001211fail:
1212 pr_warn("%s: failed to allocate requests\n", __func__);
1213 blk_mq_free_rq_map(set, tags, hctx_idx);
1214 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001215}
1216
1217static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001218 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001219{
1220 struct blk_mq_hw_ctx *hctx;
1221 unsigned int i, j;
1222
1223 /*
1224 * Initialize hardware queues
1225 */
1226 queue_for_each_hw_ctx(q, hctx, i) {
1227 unsigned int num_maps;
1228 int node;
1229
1230 node = hctx->numa_node;
1231 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001232 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001233
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001234 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1235 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001236 spin_lock_init(&hctx->lock);
1237 INIT_LIST_HEAD(&hctx->dispatch);
1238 hctx->queue = q;
1239 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001240 hctx->flags = set->flags;
1241 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001242
1243 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1244 blk_mq_hctx_notify, hctx);
1245 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1246
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001247 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001248
1249 /*
1250 * Allocate space for all possible cpus to avoid allocation in
1251 * runtime
1252 */
1253 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1254 GFP_KERNEL, node);
1255 if (!hctx->ctxs)
1256 break;
1257
1258 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1259 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1260 GFP_KERNEL, node);
1261 if (!hctx->ctx_map)
1262 break;
1263
1264 hctx->nr_ctx_map = num_maps;
1265 hctx->nr_ctx = 0;
1266
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001267 if (set->ops->init_hctx &&
1268 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001269 break;
1270 }
1271
1272 if (i == q->nr_hw_queues)
1273 return 0;
1274
1275 /*
1276 * Init failed
1277 */
1278 queue_for_each_hw_ctx(q, hctx, j) {
1279 if (i == j)
1280 break;
1281
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001282 if (set->ops->exit_hctx)
1283 set->ops->exit_hctx(hctx, j);
Jens Axboe320ae512013-10-24 09:20:05 +01001284
1285 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Jens Axboe320ae512013-10-24 09:20:05 +01001286 kfree(hctx->ctxs);
Ming Lei11471e02014-04-19 18:00:16 +08001287 kfree(hctx->ctx_map);
Jens Axboe320ae512013-10-24 09:20:05 +01001288 }
1289
1290 return 1;
1291}
1292
1293static void blk_mq_init_cpu_queues(struct request_queue *q,
1294 unsigned int nr_hw_queues)
1295{
1296 unsigned int i;
1297
1298 for_each_possible_cpu(i) {
1299 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1300 struct blk_mq_hw_ctx *hctx;
1301
1302 memset(__ctx, 0, sizeof(*__ctx));
1303 __ctx->cpu = i;
1304 spin_lock_init(&__ctx->lock);
1305 INIT_LIST_HEAD(&__ctx->rq_list);
1306 __ctx->queue = q;
1307
1308 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001309 if (!cpu_online(i))
1310 continue;
1311
Jens Axboee4043dc2014-04-09 10:18:23 -06001312 hctx = q->mq_ops->map_queue(q, i);
1313 cpumask_set_cpu(i, hctx->cpumask);
1314 hctx->nr_ctx++;
1315
Jens Axboe320ae512013-10-24 09:20:05 +01001316 /*
1317 * Set local node, IFF we have more than one hw queue. If
1318 * not, we remain on the home node of the device
1319 */
1320 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1321 hctx->numa_node = cpu_to_node(i);
1322 }
1323}
1324
1325static void blk_mq_map_swqueue(struct request_queue *q)
1326{
1327 unsigned int i;
1328 struct blk_mq_hw_ctx *hctx;
1329 struct blk_mq_ctx *ctx;
1330
1331 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001332 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001333 hctx->nr_ctx = 0;
1334 }
1335
1336 /*
1337 * Map software to hardware queues
1338 */
1339 queue_for_each_ctx(q, ctx, i) {
1340 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001341 if (!cpu_online(i))
1342 continue;
1343
Jens Axboe320ae512013-10-24 09:20:05 +01001344 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001345 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001346 ctx->index_hw = hctx->nr_ctx;
1347 hctx->ctxs[hctx->nr_ctx++] = ctx;
1348 }
1349}
1350
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001351struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001352{
1353 struct blk_mq_hw_ctx **hctxs;
1354 struct blk_mq_ctx *ctx;
1355 struct request_queue *q;
1356 int i;
1357
Jens Axboe320ae512013-10-24 09:20:05 +01001358 ctx = alloc_percpu(struct blk_mq_ctx);
1359 if (!ctx)
1360 return ERR_PTR(-ENOMEM);
1361
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001362 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1363 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001364
1365 if (!hctxs)
1366 goto err_percpu;
1367
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001368 for (i = 0; i < set->nr_hw_queues; i++) {
1369 hctxs[i] = set->ops->alloc_hctx(set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001370 if (!hctxs[i])
1371 goto err_hctxs;
1372
Jens Axboee4043dc2014-04-09 10:18:23 -06001373 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1374 goto err_hctxs;
1375
Jens Axboe320ae512013-10-24 09:20:05 +01001376 hctxs[i]->numa_node = NUMA_NO_NODE;
1377 hctxs[i]->queue_num = i;
1378 }
1379
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001380 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001381 if (!q)
1382 goto err_hctxs;
1383
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001384 q->mq_map = blk_mq_make_queue_map(set);
Jens Axboe320ae512013-10-24 09:20:05 +01001385 if (!q->mq_map)
1386 goto err_map;
1387
1388 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1389 blk_queue_rq_timeout(q, 30000);
1390
1391 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001392 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboe320ae512013-10-24 09:20:05 +01001393
1394 q->queue_ctx = ctx;
1395 q->queue_hw_ctx = hctxs;
1396
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001397 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001398 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001399
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001400 q->sg_reserved_size = INT_MAX;
1401
Jens Axboe320ae512013-10-24 09:20:05 +01001402 blk_queue_make_request(q, blk_mq_make_request);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001403 blk_queue_rq_timed_out(q, set->ops->timeout);
1404 if (set->timeout)
1405 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001406
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001407 if (set->ops->complete)
1408 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001409
Jens Axboe320ae512013-10-24 09:20:05 +01001410 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001411 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001412
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001413 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1414 set->cmd_size, cache_line_size()),
1415 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001416 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001417 goto err_hw;
1418
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001419 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001420 goto err_flush_rq;
1421
Jens Axboe320ae512013-10-24 09:20:05 +01001422 blk_mq_map_swqueue(q);
1423
1424 mutex_lock(&all_q_mutex);
1425 list_add_tail(&q->all_q_node, &all_q_list);
1426 mutex_unlock(&all_q_mutex);
1427
1428 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001429
1430err_flush_rq:
1431 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001432err_hw:
1433 kfree(q->mq_map);
1434err_map:
1435 blk_cleanup_queue(q);
1436err_hctxs:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001437 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001438 if (!hctxs[i])
1439 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001440 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001441 set->ops->free_hctx(hctxs[i], i);
Jens Axboe320ae512013-10-24 09:20:05 +01001442 }
1443 kfree(hctxs);
1444err_percpu:
1445 free_percpu(ctx);
1446 return ERR_PTR(-ENOMEM);
1447}
1448EXPORT_SYMBOL(blk_mq_init_queue);
1449
1450void blk_mq_free_queue(struct request_queue *q)
1451{
1452 struct blk_mq_hw_ctx *hctx;
1453 int i;
1454
1455 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001456 kfree(hctx->ctx_map);
1457 kfree(hctx->ctxs);
Jens Axboe320ae512013-10-24 09:20:05 +01001458 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1459 if (q->mq_ops->exit_hctx)
1460 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001461 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001462 q->mq_ops->free_hctx(hctx, i);
1463 }
1464
1465 free_percpu(q->queue_ctx);
1466 kfree(q->queue_hw_ctx);
1467 kfree(q->mq_map);
1468
1469 q->queue_ctx = NULL;
1470 q->queue_hw_ctx = NULL;
1471 q->mq_map = NULL;
1472
1473 mutex_lock(&all_q_mutex);
1474 list_del_init(&q->all_q_node);
1475 mutex_unlock(&all_q_mutex);
1476}
Jens Axboe320ae512013-10-24 09:20:05 +01001477
1478/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001479static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001480{
1481 blk_mq_freeze_queue(q);
1482
1483 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1484
1485 /*
1486 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1487 * we should change hctx numa_node according to new topology (this
1488 * involves free and re-allocate memory, worthy doing?)
1489 */
1490
1491 blk_mq_map_swqueue(q);
1492
1493 blk_mq_unfreeze_queue(q);
1494}
1495
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001496static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1497 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001498{
1499 struct request_queue *q;
1500
1501 /*
1502 * Before new mapping is established, hotadded cpu might already start
1503 * handling requests. This doesn't break anything as we map offline
1504 * CPUs to first hardware queue. We will re-init queue below to get
1505 * optimal settings.
1506 */
1507 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1508 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1509 return NOTIFY_OK;
1510
1511 mutex_lock(&all_q_mutex);
1512 list_for_each_entry(q, &all_q_list, all_q_node)
1513 blk_mq_queue_reinit(q);
1514 mutex_unlock(&all_q_mutex);
1515 return NOTIFY_OK;
1516}
1517
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001518int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1519{
1520 int i;
1521
1522 if (!set->nr_hw_queues)
1523 return -EINVAL;
1524 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1525 return -EINVAL;
1526 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1527 return -EINVAL;
1528
1529 if (!set->nr_hw_queues ||
1530 !set->ops->queue_rq || !set->ops->map_queue ||
1531 !set->ops->alloc_hctx || !set->ops->free_hctx)
1532 return -EINVAL;
1533
1534
Ming Lei484790052014-04-19 18:00:17 +08001535 set->tags = kmalloc_node(set->nr_hw_queues *
1536 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001537 GFP_KERNEL, set->numa_node);
1538 if (!set->tags)
1539 goto out;
1540
1541 for (i = 0; i < set->nr_hw_queues; i++) {
1542 set->tags[i] = blk_mq_init_rq_map(set, i);
1543 if (!set->tags[i])
1544 goto out_unwind;
1545 }
1546
1547 return 0;
1548
1549out_unwind:
1550 while (--i >= 0)
1551 blk_mq_free_rq_map(set, set->tags[i], i);
1552out:
1553 return -ENOMEM;
1554}
1555EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1556
1557void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1558{
1559 int i;
1560
1561 for (i = 0; i < set->nr_hw_queues; i++)
1562 blk_mq_free_rq_map(set, set->tags[i], i);
1563}
1564EXPORT_SYMBOL(blk_mq_free_tag_set);
1565
Jens Axboe676141e2014-03-20 13:29:18 -06001566void blk_mq_disable_hotplug(void)
1567{
1568 mutex_lock(&all_q_mutex);
1569}
1570
1571void blk_mq_enable_hotplug(void)
1572{
1573 mutex_unlock(&all_q_mutex);
1574}
1575
Jens Axboe320ae512013-10-24 09:20:05 +01001576static int __init blk_mq_init(void)
1577{
Jens Axboe320ae512013-10-24 09:20:05 +01001578 blk_mq_cpu_init();
1579
1580 /* Must be called after percpu_counter_hotcpu_callback() */
1581 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1582
1583 return 0;
1584}
1585subsys_initcall(blk_mq_init);