blob: e644feec068c258b7b1fce66a34f1821209543e5 [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) {
84 rq = hctx->rqs[tag];
85 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
Jens Axboe320ae512013-10-24 09:20:05 +0100189 rq->mq_ctx = ctx;
190 rq->cmd_flags = rw_flags;
Ming Lei0fec08b2014-01-03 10:00:08 -0700191 rq->start_time = jiffies;
192 set_start_time_ns(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100193 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
194}
195
Jens Axboe320ae512013-10-24 09:20:05 +0100196static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
197 int rw, gfp_t gfp,
198 bool reserved)
199{
200 struct request *rq;
201
202 do {
203 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
204 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
205
Christoph Hellwig18741982014-02-10 09:29:00 -0700206 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100207 if (rq) {
Jens Axboe94eddfb2013-11-19 09:25:07 -0700208 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100209 break;
Jeff Moyer959a35f2013-12-03 14:23:00 -0700210 }
Jens Axboe320ae512013-10-24 09:20:05 +0100211
Jens Axboee4043dc2014-04-09 10:18:23 -0600212 if (gfp & __GFP_WAIT) {
213 __blk_mq_run_hw_queue(hctx);
214 blk_mq_put_ctx(ctx);
215 } else {
216 blk_mq_put_ctx(ctx);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700217 break;
Jens Axboee4043dc2014-04-09 10:18:23 -0600218 }
Jeff Moyer959a35f2013-12-03 14:23:00 -0700219
Jens Axboe320ae512013-10-24 09:20:05 +0100220 blk_mq_wait_for_tags(hctx->tags);
221 } while (1);
222
223 return rq;
224}
225
Christoph Hellwig18741982014-02-10 09:29:00 -0700226struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100227{
228 struct request *rq;
229
230 if (blk_mq_queue_enter(q))
231 return NULL;
232
Christoph Hellwig18741982014-02-10 09:29:00 -0700233 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700234 if (rq)
235 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100236 return rq;
237}
238
239struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
240 gfp_t gfp)
241{
242 struct request *rq;
243
244 if (blk_mq_queue_enter(q))
245 return NULL;
246
247 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700248 if (rq)
249 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100250 return rq;
251}
252EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
253
Jens Axboe320ae512013-10-24 09:20:05 +0100254static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
255 struct blk_mq_ctx *ctx, struct request *rq)
256{
257 const int tag = rq->tag;
258 struct request_queue *q = rq->q;
259
Christoph Hellwig9d74e252014-04-14 10:30:07 +0200260 blk_rq_init(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100261 blk_mq_put_tag(hctx->tags, tag);
262
263 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 Hellwig7237c742014-02-20 15:32:38 -0800298bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes)
Jens Axboe320ae512013-10-24 09:20:05 +0100299{
Christoph Hellwig7237c742014-02-20 15:32:38 -0800300 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
301 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100302
Ming Lei0d11e6a2013-12-05 10:50:39 -0700303 blk_account_io_done(rq);
304
Jens Axboe320ae512013-10-24 09:20:05 +0100305 if (rq->end_io)
306 rq->end_io(rq, error);
307 else
308 blk_mq_free_request(rq);
Christoph Hellwig7237c742014-02-20 15:32:38 -0800309 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100310}
Christoph Hellwig7237c742014-02-20 15:32:38 -0800311EXPORT_SYMBOL(blk_mq_end_io_partial);
Jens Axboe320ae512013-10-24 09:20:05 +0100312
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800313static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100314{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800315 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100316
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800317 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100318}
319
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800320void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100321{
322 struct blk_mq_ctx *ctx = rq->mq_ctx;
323 int cpu;
324
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800325 if (!ctx->ipi_redirect) {
326 rq->q->softirq_done_fn(rq);
327 return;
328 }
Jens Axboe320ae512013-10-24 09:20:05 +0100329
330 cpu = get_cpu();
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800331 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800332 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800333 rq->csd.info = rq;
334 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100335 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800336 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800337 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800338 }
Jens Axboe320ae512013-10-24 09:20:05 +0100339 put_cpu();
340}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800341
342/**
343 * blk_mq_complete_request - end I/O on a request
344 * @rq: the request being processed
345 *
346 * Description:
347 * Ends all I/O on a request. It does not handle partial completions.
348 * The actual completion happens out-of-order, through a IPI handler.
349 **/
350void blk_mq_complete_request(struct request *rq)
351{
352 if (unlikely(blk_should_fake_timeout(rq->q)))
353 return;
354 if (!blk_mark_rq_complete(rq))
355 __blk_mq_complete_request(rq);
356}
357EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100358
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800359static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100360{
361 struct request_queue *q = rq->q;
362
363 trace_block_rq_issue(q, rq);
364
Christoph Hellwig742ee692014-04-14 10:30:06 +0200365 rq->resid_len = blk_rq_bytes(rq);
366
Jens Axboe320ae512013-10-24 09:20:05 +0100367 /*
368 * Just mark start time and set the started bit. Due to memory
369 * ordering, we know we'll see the correct deadline as long as
370 * REQ_ATOMIC_STARTED is seen.
371 */
372 rq->deadline = jiffies + q->rq_timeout;
373 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800374
375 if (q->dma_drain_size && blk_rq_bytes(rq)) {
376 /*
377 * Make sure space for the drain appears. We know we can do
378 * this because max_hw_segments has been adjusted to be one
379 * fewer than the device can handle.
380 */
381 rq->nr_phys_segments++;
382 }
383
384 /*
385 * Flag the last request in the series so that drivers know when IO
386 * should be kicked off, if they don't do it on a per-request basis.
387 *
388 * Note: the flag isn't the only condition drivers should do kick off.
389 * If drive is busy, the last request might not have the bit set.
390 */
391 if (last)
392 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100393}
394
395static void blk_mq_requeue_request(struct request *rq)
396{
397 struct request_queue *q = rq->q;
398
399 trace_block_rq_requeue(q, rq);
400 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800401
402 rq->cmd_flags &= ~REQ_END;
403
404 if (q->dma_drain_size && blk_rq_bytes(rq))
405 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100406}
407
408struct blk_mq_timeout_data {
409 struct blk_mq_hw_ctx *hctx;
410 unsigned long *next;
411 unsigned int *next_set;
412};
413
414static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
415{
416 struct blk_mq_timeout_data *data = __data;
417 struct blk_mq_hw_ctx *hctx = data->hctx;
418 unsigned int tag;
419
420 /* It may not be in flight yet (this is where
421 * the REQ_ATOMIC_STARTED flag comes in). The requests are
422 * statically allocated, so we know it's always safe to access the
423 * memory associated with a bit offset into ->rqs[].
424 */
425 tag = 0;
426 do {
427 struct request *rq;
428
429 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
430 if (tag >= hctx->queue_depth)
431 break;
432
433 rq = hctx->rqs[tag++];
434
435 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
436 continue;
437
438 blk_rq_check_expired(rq, data->next, data->next_set);
439 } while (1);
440}
441
442static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
443 unsigned long *next,
444 unsigned int *next_set)
445{
446 struct blk_mq_timeout_data data = {
447 .hctx = hctx,
448 .next = next,
449 .next_set = next_set,
450 };
451
452 /*
453 * Ask the tagging code to iterate busy requests, so we can
454 * check them for timeout.
455 */
456 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
457}
458
459static void blk_mq_rq_timer(unsigned long data)
460{
461 struct request_queue *q = (struct request_queue *) data;
462 struct blk_mq_hw_ctx *hctx;
463 unsigned long next = 0;
464 int i, next_set = 0;
465
466 queue_for_each_hw_ctx(q, hctx, i)
467 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
468
469 if (next_set)
470 mod_timer(&q->timeout, round_jiffies_up(next));
471}
472
473/*
474 * Reverse check our software queue for entries that we could potentially
475 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
476 * too much time checking for merges.
477 */
478static bool blk_mq_attempt_merge(struct request_queue *q,
479 struct blk_mq_ctx *ctx, struct bio *bio)
480{
481 struct request *rq;
482 int checked = 8;
483
484 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
485 int el_ret;
486
487 if (!checked--)
488 break;
489
490 if (!blk_rq_merge_ok(rq, bio))
491 continue;
492
493 el_ret = blk_try_merge(rq, bio);
494 if (el_ret == ELEVATOR_BACK_MERGE) {
495 if (bio_attempt_back_merge(q, rq, bio)) {
496 ctx->rq_merged++;
497 return true;
498 }
499 break;
500 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
501 if (bio_attempt_front_merge(q, rq, bio)) {
502 ctx->rq_merged++;
503 return true;
504 }
505 break;
506 }
507 }
508
509 return false;
510}
511
512void blk_mq_add_timer(struct request *rq)
513{
514 __blk_add_timer(rq, NULL);
515}
516
517/*
518 * Run this hardware queue, pulling any software queues mapped to it in.
519 * Note that this function currently has various problems around ordering
520 * of IO. In particular, we'd like FIFO behaviour on handling existing
521 * items on the hctx->dispatch list. Ignore that for now.
522 */
523static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
524{
525 struct request_queue *q = hctx->queue;
526 struct blk_mq_ctx *ctx;
527 struct request *rq;
528 LIST_HEAD(rq_list);
529 int bit, queued;
530
Jens Axboee4043dc2014-04-09 10:18:23 -0600531 WARN_ON(!preempt_count());
532
Jens Axboe5d12f902014-03-19 15:25:02 -0600533 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100534 return;
535
536 hctx->run++;
537
538 /*
539 * Touch any software queue that has pending entries.
540 */
541 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
542 clear_bit(bit, hctx->ctx_map);
543 ctx = hctx->ctxs[bit];
544 BUG_ON(bit != ctx->index_hw);
545
546 spin_lock(&ctx->lock);
547 list_splice_tail_init(&ctx->rq_list, &rq_list);
548 spin_unlock(&ctx->lock);
549 }
550
551 /*
552 * If we have previous entries on our dispatch list, grab them
553 * and stuff them at the front for more fair dispatch.
554 */
555 if (!list_empty_careful(&hctx->dispatch)) {
556 spin_lock(&hctx->lock);
557 if (!list_empty(&hctx->dispatch))
558 list_splice_init(&hctx->dispatch, &rq_list);
559 spin_unlock(&hctx->lock);
560 }
561
562 /*
563 * Delete and return all entries from our dispatch list
564 */
565 queued = 0;
566
567 /*
568 * Now process all the entries, sending them to the driver.
569 */
570 while (!list_empty(&rq_list)) {
571 int ret;
572
573 rq = list_first_entry(&rq_list, struct request, queuelist);
574 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100575
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800576 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100577
578 ret = q->mq_ops->queue_rq(hctx, rq);
579 switch (ret) {
580 case BLK_MQ_RQ_QUEUE_OK:
581 queued++;
582 continue;
583 case BLK_MQ_RQ_QUEUE_BUSY:
584 /*
585 * FIXME: we should have a mechanism to stop the queue
586 * like blk_stop_queue, otherwise we will waste cpu
587 * time
588 */
589 list_add(&rq->queuelist, &rq_list);
590 blk_mq_requeue_request(rq);
591 break;
592 default:
593 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100594 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800595 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100596 blk_mq_end_io(rq, rq->errors);
597 break;
598 }
599
600 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
601 break;
602 }
603
604 if (!queued)
605 hctx->dispatched[0]++;
606 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
607 hctx->dispatched[ilog2(queued) + 1]++;
608
609 /*
610 * Any items that need requeuing? Stuff them into hctx->dispatch,
611 * that is where we will continue on next queue run.
612 */
613 if (!list_empty(&rq_list)) {
614 spin_lock(&hctx->lock);
615 list_splice(&rq_list, &hctx->dispatch);
616 spin_unlock(&hctx->lock);
617 }
618}
619
620void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
621{
Jens Axboe5d12f902014-03-19 15:25:02 -0600622 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100623 return;
624
Jens Axboee4043dc2014-04-09 10:18:23 -0600625 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100626 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600627 else if (hctx->queue->nr_hw_queues == 1)
Jens Axboe59c3d452014-04-08 09:15:35 -0600628 kblockd_schedule_delayed_work(&hctx->delayed_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600629 else {
630 unsigned int cpu;
631
632 /*
633 * It'd be great if the workqueue API had a way to pass
634 * in a mask and had some smarts for more clever placement
635 * than the first CPU. Or we could round-robin here. For now,
636 * just queue on the first CPU.
637 */
638 cpu = cpumask_first(hctx->cpumask);
639 kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0);
640 }
Jens Axboe320ae512013-10-24 09:20:05 +0100641}
642
643void blk_mq_run_queues(struct request_queue *q, bool async)
644{
645 struct blk_mq_hw_ctx *hctx;
646 int i;
647
648 queue_for_each_hw_ctx(q, hctx, i) {
649 if ((!blk_mq_hctx_has_pending(hctx) &&
650 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600651 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100652 continue;
653
Jens Axboee4043dc2014-04-09 10:18:23 -0600654 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100655 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600656 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100657 }
658}
659EXPORT_SYMBOL(blk_mq_run_queues);
660
661void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
662{
663 cancel_delayed_work(&hctx->delayed_work);
664 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
665}
666EXPORT_SYMBOL(blk_mq_stop_hw_queue);
667
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100668void blk_mq_stop_hw_queues(struct request_queue *q)
669{
670 struct blk_mq_hw_ctx *hctx;
671 int i;
672
673 queue_for_each_hw_ctx(q, hctx, i)
674 blk_mq_stop_hw_queue(hctx);
675}
676EXPORT_SYMBOL(blk_mq_stop_hw_queues);
677
Jens Axboe320ae512013-10-24 09:20:05 +0100678void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
679{
680 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600681
682 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100683 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600684 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100685}
686EXPORT_SYMBOL(blk_mq_start_hw_queue);
687
688void blk_mq_start_stopped_hw_queues(struct request_queue *q)
689{
690 struct blk_mq_hw_ctx *hctx;
691 int i;
692
693 queue_for_each_hw_ctx(q, hctx, i) {
694 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
695 continue;
696
697 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600698 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100699 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -0600700 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100701 }
702}
703EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
704
705static void blk_mq_work_fn(struct work_struct *work)
706{
707 struct blk_mq_hw_ctx *hctx;
708
709 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600710
711 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100712 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600713 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100714}
715
716static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800717 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100718{
719 struct blk_mq_ctx *ctx = rq->mq_ctx;
720
Jens Axboe01b983c2013-11-19 18:59:10 -0700721 trace_block_rq_insert(hctx->queue, rq);
722
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800723 if (at_head)
724 list_add(&rq->queuelist, &ctx->rq_list);
725 else
726 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100727 blk_mq_hctx_mark_pending(hctx, ctx);
728
729 /*
730 * We do this early, to ensure we are on the right CPU.
731 */
732 blk_mq_add_timer(rq);
733}
734
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600735void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
736 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100737{
738 struct request_queue *q = rq->q;
739 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600740 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100741
742 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600743 if (!cpu_online(ctx->cpu))
744 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100745
Jens Axboe320ae512013-10-24 09:20:05 +0100746 hctx = q->mq_ops->map_queue(q, ctx->cpu);
747
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600748 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
749 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
750 blk_insert_flush(rq);
751 } else {
752 spin_lock(&ctx->lock);
753 __blk_mq_insert_request(hctx, rq, at_head);
754 spin_unlock(&ctx->lock);
755 }
Jens Axboe320ae512013-10-24 09:20:05 +0100756
Jens Axboe320ae512013-10-24 09:20:05 +0100757 if (run_queue)
758 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600759
760 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100761}
762
763static void blk_mq_insert_requests(struct request_queue *q,
764 struct blk_mq_ctx *ctx,
765 struct list_head *list,
766 int depth,
767 bool from_schedule)
768
769{
770 struct blk_mq_hw_ctx *hctx;
771 struct blk_mq_ctx *current_ctx;
772
773 trace_block_unplug(q, depth, !from_schedule);
774
775 current_ctx = blk_mq_get_ctx(q);
776
777 if (!cpu_online(ctx->cpu))
778 ctx = current_ctx;
779 hctx = q->mq_ops->map_queue(q, ctx->cpu);
780
781 /*
782 * preemption doesn't flush plug list, so it's possible ctx->cpu is
783 * offline now
784 */
785 spin_lock(&ctx->lock);
786 while (!list_empty(list)) {
787 struct request *rq;
788
789 rq = list_first_entry(list, struct request, queuelist);
790 list_del_init(&rq->queuelist);
791 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800792 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100793 }
794 spin_unlock(&ctx->lock);
795
Jens Axboe320ae512013-10-24 09:20:05 +0100796 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600797 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100798}
799
800static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
801{
802 struct request *rqa = container_of(a, struct request, queuelist);
803 struct request *rqb = container_of(b, struct request, queuelist);
804
805 return !(rqa->mq_ctx < rqb->mq_ctx ||
806 (rqa->mq_ctx == rqb->mq_ctx &&
807 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
808}
809
810void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
811{
812 struct blk_mq_ctx *this_ctx;
813 struct request_queue *this_q;
814 struct request *rq;
815 LIST_HEAD(list);
816 LIST_HEAD(ctx_list);
817 unsigned int depth;
818
819 list_splice_init(&plug->mq_list, &list);
820
821 list_sort(NULL, &list, plug_ctx_cmp);
822
823 this_q = NULL;
824 this_ctx = NULL;
825 depth = 0;
826
827 while (!list_empty(&list)) {
828 rq = list_entry_rq(list.next);
829 list_del_init(&rq->queuelist);
830 BUG_ON(!rq->q);
831 if (rq->mq_ctx != this_ctx) {
832 if (this_ctx) {
833 blk_mq_insert_requests(this_q, this_ctx,
834 &ctx_list, depth,
835 from_schedule);
836 }
837
838 this_ctx = rq->mq_ctx;
839 this_q = rq->q;
840 depth = 0;
841 }
842
843 depth++;
844 list_add_tail(&rq->queuelist, &ctx_list);
845 }
846
847 /*
848 * If 'this_ctx' is set, we know we have entries to complete
849 * on 'ctx_list'. Do those.
850 */
851 if (this_ctx) {
852 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
853 from_schedule);
854 }
855}
856
857static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
858{
859 init_request_from_bio(rq, bio);
860 blk_account_io_start(rq, 1);
861}
862
863static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
864{
865 struct blk_mq_hw_ctx *hctx;
866 struct blk_mq_ctx *ctx;
867 const int is_sync = rw_is_sync(bio->bi_rw);
868 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
869 int rw = bio_data_dir(bio);
870 struct request *rq;
871 unsigned int use_plug, request_count = 0;
872
873 /*
874 * If we have multiple hardware queues, just go directly to
875 * one of those for sync IO.
876 */
877 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
878
879 blk_queue_bounce(q, &bio);
880
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -0700881 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
882 bio_endio(bio, -EIO);
883 return;
884 }
885
Jens Axboe320ae512013-10-24 09:20:05 +0100886 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
887 return;
888
889 if (blk_mq_queue_enter(q)) {
890 bio_endio(bio, -EIO);
891 return;
892 }
893
894 ctx = blk_mq_get_ctx(q);
895 hctx = q->mq_ops->map_queue(q, ctx->cpu);
896
Shaohua Li27fbf4e2014-02-19 20:20:21 +0800897 if (is_sync)
898 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +0100899 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700900 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100901 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -0700902 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100903 else {
904 blk_mq_put_ctx(ctx);
905 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700906 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
907 false);
Jens Axboe320ae512013-10-24 09:20:05 +0100908 ctx = rq->mq_ctx;
909 hctx = q->mq_ops->map_queue(q, ctx->cpu);
910 }
911
912 hctx->queued++;
913
914 if (unlikely(is_flush_fua)) {
915 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +0100916 blk_insert_flush(rq);
917 goto run_queue;
918 }
919
920 /*
921 * A task plug currently exists. Since this is completely lockless,
922 * utilize that to temporarily store requests until the task is
923 * either done or scheduled away.
924 */
925 if (use_plug) {
926 struct blk_plug *plug = current->plug;
927
928 if (plug) {
929 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -0600930 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +0100931 trace_block_plug(q);
932 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
933 blk_flush_plug_list(plug, false);
934 trace_block_plug(q);
935 }
936 list_add_tail(&rq->queuelist, &plug->mq_list);
937 blk_mq_put_ctx(ctx);
938 return;
939 }
940 }
941
942 spin_lock(&ctx->lock);
943
944 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
945 blk_mq_attempt_merge(q, ctx, bio))
946 __blk_mq_free_request(hctx, ctx, rq);
947 else {
948 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800949 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100950 }
951
952 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100953
954 /*
955 * For a SYNC request, send it to the hardware immediately. For an
956 * ASYNC request, just ensure that we run it later on. The latter
957 * allows for merging opportunities and more efficient dispatching.
958 */
959run_queue:
960 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -0600961 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100962}
963
964/*
965 * Default mapping to a software queue, since we use one per CPU.
966 */
967struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
968{
969 return q->queue_hw_ctx[q->mq_map[cpu]];
970}
971EXPORT_SYMBOL(blk_mq_map_queue);
972
973struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
974 unsigned int hctx_index)
975{
976 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
977 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
978}
979EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
980
981void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
982 unsigned int hctx_index)
983{
984 kfree(hctx);
985}
986EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
987
988static void blk_mq_hctx_notify(void *data, unsigned long action,
989 unsigned int cpu)
990{
991 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -0600992 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100993 struct blk_mq_ctx *ctx;
994 LIST_HEAD(tmp);
995
996 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
997 return;
998
999 /*
1000 * Move ctx entries to new CPU, if this one is going away.
1001 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001002 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001003
1004 spin_lock(&ctx->lock);
1005 if (!list_empty(&ctx->rq_list)) {
1006 list_splice_init(&ctx->rq_list, &tmp);
1007 clear_bit(ctx->index_hw, hctx->ctx_map);
1008 }
1009 spin_unlock(&ctx->lock);
1010
1011 if (list_empty(&tmp))
1012 return;
1013
Jens Axboebccb5f72014-04-04 21:34:48 -06001014 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001015 spin_lock(&ctx->lock);
1016
1017 while (!list_empty(&tmp)) {
1018 struct request *rq;
1019
1020 rq = list_first_entry(&tmp, struct request, queuelist);
1021 rq->mq_ctx = ctx;
1022 list_move_tail(&rq->queuelist, &ctx->rq_list);
1023 }
1024
Jens Axboebccb5f72014-04-04 21:34:48 -06001025 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001026 blk_mq_hctx_mark_pending(hctx, ctx);
1027
1028 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001029
1030 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001031 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001032}
1033
Jens Axboe95363ef2014-03-14 10:43:15 -06001034static int blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1035 int (*init)(void *, struct blk_mq_hw_ctx *,
1036 struct request *, unsigned int),
1037 void *data)
1038{
1039 unsigned int i;
1040 int ret = 0;
1041
1042 for (i = 0; i < hctx->queue_depth; i++) {
1043 struct request *rq = hctx->rqs[i];
1044
1045 ret = init(data, hctx, rq, i);
1046 if (ret)
1047 break;
1048 }
1049
1050 return ret;
1051}
1052
1053int blk_mq_init_commands(struct request_queue *q,
1054 int (*init)(void *, struct blk_mq_hw_ctx *,
1055 struct request *, unsigned int),
1056 void *data)
1057{
1058 struct blk_mq_hw_ctx *hctx;
1059 unsigned int i;
1060 int ret = 0;
1061
1062 queue_for_each_hw_ctx(q, hctx, i) {
1063 ret = blk_mq_init_hw_commands(hctx, init, data);
1064 if (ret)
1065 break;
1066 }
1067
1068 return ret;
1069}
1070EXPORT_SYMBOL(blk_mq_init_commands);
1071
1072static void blk_mq_free_hw_commands(struct blk_mq_hw_ctx *hctx,
1073 void (*free)(void *, struct blk_mq_hw_ctx *,
Jens Axboe320ae512013-10-24 09:20:05 +01001074 struct request *, unsigned int),
1075 void *data)
1076{
1077 unsigned int i;
1078
1079 for (i = 0; i < hctx->queue_depth; i++) {
1080 struct request *rq = hctx->rqs[i];
1081
Jens Axboe95363ef2014-03-14 10:43:15 -06001082 free(data, hctx, rq, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001083 }
1084}
1085
Jens Axboe95363ef2014-03-14 10:43:15 -06001086void blk_mq_free_commands(struct request_queue *q,
1087 void (*free)(void *, struct blk_mq_hw_ctx *,
Jens Axboe320ae512013-10-24 09:20:05 +01001088 struct request *, unsigned int),
1089 void *data)
1090{
1091 struct blk_mq_hw_ctx *hctx;
1092 unsigned int i;
1093
1094 queue_for_each_hw_ctx(q, hctx, i)
Jens Axboe95363ef2014-03-14 10:43:15 -06001095 blk_mq_free_hw_commands(hctx, free, data);
Jens Axboe320ae512013-10-24 09:20:05 +01001096}
Jens Axboe95363ef2014-03-14 10:43:15 -06001097EXPORT_SYMBOL(blk_mq_free_commands);
Jens Axboe320ae512013-10-24 09:20:05 +01001098
1099static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1100{
1101 struct page *page;
1102
1103 while (!list_empty(&hctx->page_list)) {
Dave Hansen67534712014-01-08 20:17:46 -07001104 page = list_first_entry(&hctx->page_list, struct page, lru);
1105 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001106 __free_pages(page, page->private);
1107 }
1108
1109 kfree(hctx->rqs);
1110
1111 if (hctx->tags)
1112 blk_mq_free_tags(hctx->tags);
1113}
1114
1115static size_t order_to_size(unsigned int order)
1116{
1117 size_t ret = PAGE_SIZE;
1118
1119 while (order--)
1120 ret *= 2;
1121
1122 return ret;
1123}
1124
1125static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1126 unsigned int reserved_tags, int node)
1127{
1128 unsigned int i, j, entries_per_page, max_order = 4;
1129 size_t rq_size, left;
1130
1131 INIT_LIST_HEAD(&hctx->page_list);
1132
1133 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1134 GFP_KERNEL, node);
1135 if (!hctx->rqs)
1136 return -ENOMEM;
1137
1138 /*
1139 * rq_size is the size of the request plus driver payload, rounded
1140 * to the cacheline size
1141 */
1142 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1143 cache_line_size());
1144 left = rq_size * hctx->queue_depth;
1145
1146 for (i = 0; i < hctx->queue_depth;) {
1147 int this_order = max_order;
1148 struct page *page;
1149 int to_do;
1150 void *p;
1151
1152 while (left < order_to_size(this_order - 1) && this_order)
1153 this_order--;
1154
1155 do {
1156 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1157 if (page)
1158 break;
1159 if (!this_order--)
1160 break;
1161 if (order_to_size(this_order) < rq_size)
1162 break;
1163 } while (1);
1164
1165 if (!page)
1166 break;
1167
1168 page->private = this_order;
Dave Hansen67534712014-01-08 20:17:46 -07001169 list_add_tail(&page->lru, &hctx->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001170
1171 p = page_address(page);
1172 entries_per_page = order_to_size(this_order) / rq_size;
1173 to_do = min(entries_per_page, hctx->queue_depth - i);
1174 left -= to_do * rq_size;
1175 for (j = 0; j < to_do; j++) {
1176 hctx->rqs[i] = p;
Christoph Hellwig9d74e252014-04-14 10:30:07 +02001177 blk_rq_init(hctx->queue, hctx->rqs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001178 p += rq_size;
1179 i++;
1180 }
1181 }
1182
1183 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1184 goto err_rq_map;
1185 else if (i != hctx->queue_depth) {
1186 hctx->queue_depth = i;
1187 pr_warn("%s: queue depth set to %u because of low memory\n",
1188 __func__, i);
1189 }
1190
1191 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1192 if (!hctx->tags) {
1193err_rq_map:
1194 blk_mq_free_rq_map(hctx);
1195 return -ENOMEM;
1196 }
1197
1198 return 0;
1199}
1200
1201static int blk_mq_init_hw_queues(struct request_queue *q,
1202 struct blk_mq_reg *reg, void *driver_data)
1203{
1204 struct blk_mq_hw_ctx *hctx;
1205 unsigned int i, j;
1206
1207 /*
1208 * Initialize hardware queues
1209 */
1210 queue_for_each_hw_ctx(q, hctx, i) {
1211 unsigned int num_maps;
1212 int node;
1213
1214 node = hctx->numa_node;
1215 if (node == NUMA_NO_NODE)
1216 node = hctx->numa_node = reg->numa_node;
1217
1218 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1219 spin_lock_init(&hctx->lock);
1220 INIT_LIST_HEAD(&hctx->dispatch);
1221 hctx->queue = q;
1222 hctx->queue_num = i;
1223 hctx->flags = reg->flags;
1224 hctx->queue_depth = reg->queue_depth;
1225 hctx->cmd_size = reg->cmd_size;
1226
1227 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1228 blk_mq_hctx_notify, hctx);
1229 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1230
1231 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1232 break;
1233
1234 /*
1235 * Allocate space for all possible cpus to avoid allocation in
1236 * runtime
1237 */
1238 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1239 GFP_KERNEL, node);
1240 if (!hctx->ctxs)
1241 break;
1242
1243 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1244 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1245 GFP_KERNEL, node);
1246 if (!hctx->ctx_map)
1247 break;
1248
1249 hctx->nr_ctx_map = num_maps;
1250 hctx->nr_ctx = 0;
1251
1252 if (reg->ops->init_hctx &&
1253 reg->ops->init_hctx(hctx, driver_data, i))
1254 break;
1255 }
1256
1257 if (i == q->nr_hw_queues)
1258 return 0;
1259
1260 /*
1261 * Init failed
1262 */
1263 queue_for_each_hw_ctx(q, hctx, j) {
1264 if (i == j)
1265 break;
1266
1267 if (reg->ops->exit_hctx)
1268 reg->ops->exit_hctx(hctx, j);
1269
1270 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1271 blk_mq_free_rq_map(hctx);
1272 kfree(hctx->ctxs);
1273 }
1274
1275 return 1;
1276}
1277
1278static void blk_mq_init_cpu_queues(struct request_queue *q,
1279 unsigned int nr_hw_queues)
1280{
1281 unsigned int i;
1282
1283 for_each_possible_cpu(i) {
1284 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1285 struct blk_mq_hw_ctx *hctx;
1286
1287 memset(__ctx, 0, sizeof(*__ctx));
1288 __ctx->cpu = i;
1289 spin_lock_init(&__ctx->lock);
1290 INIT_LIST_HEAD(&__ctx->rq_list);
1291 __ctx->queue = q;
1292
1293 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001294 if (!cpu_online(i))
1295 continue;
1296
Jens Axboee4043dc2014-04-09 10:18:23 -06001297 hctx = q->mq_ops->map_queue(q, i);
1298 cpumask_set_cpu(i, hctx->cpumask);
1299 hctx->nr_ctx++;
1300
Jens Axboe320ae512013-10-24 09:20:05 +01001301 /*
1302 * Set local node, IFF we have more than one hw queue. If
1303 * not, we remain on the home node of the device
1304 */
1305 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1306 hctx->numa_node = cpu_to_node(i);
1307 }
1308}
1309
1310static void blk_mq_map_swqueue(struct request_queue *q)
1311{
1312 unsigned int i;
1313 struct blk_mq_hw_ctx *hctx;
1314 struct blk_mq_ctx *ctx;
1315
1316 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001317 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001318 hctx->nr_ctx = 0;
1319 }
1320
1321 /*
1322 * Map software to hardware queues
1323 */
1324 queue_for_each_ctx(q, ctx, i) {
1325 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001326 if (!cpu_online(i))
1327 continue;
1328
Jens Axboe320ae512013-10-24 09:20:05 +01001329 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001330 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001331 ctx->index_hw = hctx->nr_ctx;
1332 hctx->ctxs[hctx->nr_ctx++] = ctx;
1333 }
1334}
1335
1336struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1337 void *driver_data)
1338{
1339 struct blk_mq_hw_ctx **hctxs;
1340 struct blk_mq_ctx *ctx;
1341 struct request_queue *q;
1342 int i;
1343
1344 if (!reg->nr_hw_queues ||
1345 !reg->ops->queue_rq || !reg->ops->map_queue ||
1346 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1347 return ERR_PTR(-EINVAL);
1348
1349 if (!reg->queue_depth)
1350 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1351 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1352 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1353 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1354 }
1355
Jens Axboe320ae512013-10-24 09:20:05 +01001356 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1357 return ERR_PTR(-EINVAL);
1358
1359 ctx = alloc_percpu(struct blk_mq_ctx);
1360 if (!ctx)
1361 return ERR_PTR(-ENOMEM);
1362
1363 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1364 reg->numa_node);
1365
1366 if (!hctxs)
1367 goto err_percpu;
1368
1369 for (i = 0; i < reg->nr_hw_queues; i++) {
1370 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1371 if (!hctxs[i])
1372 goto err_hctxs;
1373
Jens Axboee4043dc2014-04-09 10:18:23 -06001374 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1375 goto err_hctxs;
1376
Jens Axboe320ae512013-10-24 09:20:05 +01001377 hctxs[i]->numa_node = NUMA_NO_NODE;
1378 hctxs[i]->queue_num = i;
1379 }
1380
1381 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1382 if (!q)
1383 goto err_hctxs;
1384
1385 q->mq_map = blk_mq_make_queue_map(reg);
1386 if (!q->mq_map)
1387 goto err_map;
1388
1389 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1390 blk_queue_rq_timeout(q, 30000);
1391
1392 q->nr_queues = nr_cpu_ids;
1393 q->nr_hw_queues = reg->nr_hw_queues;
1394
1395 q->queue_ctx = ctx;
1396 q->queue_hw_ctx = hctxs;
1397
1398 q->mq_ops = reg->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001399 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001400
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001401 q->sg_reserved_size = INT_MAX;
1402
Jens Axboe320ae512013-10-24 09:20:05 +01001403 blk_queue_make_request(q, blk_mq_make_request);
1404 blk_queue_rq_timed_out(q, reg->ops->timeout);
1405 if (reg->timeout)
1406 blk_queue_rq_timeout(q, reg->timeout);
1407
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001408 if (reg->ops->complete)
1409 blk_queue_softirq_done(q, reg->ops->complete);
1410
Jens Axboe320ae512013-10-24 09:20:05 +01001411 blk_mq_init_flush(q);
1412 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1413
Christoph Hellwig18741982014-02-10 09:29:00 -07001414 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1415 cache_line_size()), GFP_KERNEL);
1416 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001417 goto err_hw;
1418
Christoph Hellwig18741982014-02-10 09:29:00 -07001419 if (blk_mq_init_hw_queues(q, reg, driver_data))
1420 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:
1437 for (i = 0; i < reg->nr_hw_queues; i++) {
1438 if (!hctxs[i])
1439 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001440 free_cpumask_var(hctxs[i]->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001441 reg->ops->free_hctx(hctxs[i], i);
1442 }
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);
1458 blk_mq_free_rq_map(hctx);
1459 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1460 if (q->mq_ops->exit_hctx)
1461 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001462 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001463 q->mq_ops->free_hctx(hctx, i);
1464 }
1465
1466 free_percpu(q->queue_ctx);
1467 kfree(q->queue_hw_ctx);
1468 kfree(q->mq_map);
1469
1470 q->queue_ctx = NULL;
1471 q->queue_hw_ctx = NULL;
1472 q->mq_map = NULL;
1473
1474 mutex_lock(&all_q_mutex);
1475 list_del_init(&q->all_q_node);
1476 mutex_unlock(&all_q_mutex);
1477}
Jens Axboe320ae512013-10-24 09:20:05 +01001478
1479/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001480static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001481{
1482 blk_mq_freeze_queue(q);
1483
1484 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1485
1486 /*
1487 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1488 * we should change hctx numa_node according to new topology (this
1489 * involves free and re-allocate memory, worthy doing?)
1490 */
1491
1492 blk_mq_map_swqueue(q);
1493
1494 blk_mq_unfreeze_queue(q);
1495}
1496
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001497static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1498 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001499{
1500 struct request_queue *q;
1501
1502 /*
1503 * Before new mapping is established, hotadded cpu might already start
1504 * handling requests. This doesn't break anything as we map offline
1505 * CPUs to first hardware queue. We will re-init queue below to get
1506 * optimal settings.
1507 */
1508 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1509 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1510 return NOTIFY_OK;
1511
1512 mutex_lock(&all_q_mutex);
1513 list_for_each_entry(q, &all_q_list, all_q_node)
1514 blk_mq_queue_reinit(q);
1515 mutex_unlock(&all_q_mutex);
1516 return NOTIFY_OK;
1517}
1518
Jens Axboe676141e2014-03-20 13:29:18 -06001519void blk_mq_disable_hotplug(void)
1520{
1521 mutex_lock(&all_q_mutex);
1522}
1523
1524void blk_mq_enable_hotplug(void)
1525{
1526 mutex_unlock(&all_q_mutex);
1527}
1528
Jens Axboe320ae512013-10-24 09:20:05 +01001529static int __init blk_mq_init(void)
1530{
Jens Axboe320ae512013-10-24 09:20:05 +01001531 blk_mq_cpu_init();
1532
1533 /* Must be called after percpu_counter_hotcpu_callback() */
1534 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1535
1536 return 0;
1537}
1538subsys_initcall(blk_mq_init);