blob: 2a5a0fed10a33dcaf8095ee668ac69a58d5e21dc [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];
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 Hellwig7237c742014-02-20 15:32:38 -0800297bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes)
Jens Axboe320ae512013-10-24 09:20:05 +0100298{
Christoph Hellwig7237c742014-02-20 15:32:38 -0800299 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
300 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100301
Ming Lei0d11e6a2013-12-05 10:50:39 -0700302 blk_account_io_done(rq);
303
Jens Axboe320ae512013-10-24 09:20:05 +0100304 if (rq->end_io)
305 rq->end_io(rq, error);
306 else
307 blk_mq_free_request(rq);
Christoph Hellwig7237c742014-02-20 15:32:38 -0800308 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100309}
Christoph Hellwig7237c742014-02-20 15:32:38 -0800310EXPORT_SYMBOL(blk_mq_end_io_partial);
Jens Axboe320ae512013-10-24 09:20:05 +0100311
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800312static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100313{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800314 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100315
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800316 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100317}
318
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800319void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100320{
321 struct blk_mq_ctx *ctx = rq->mq_ctx;
322 int cpu;
323
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800324 if (!ctx->ipi_redirect) {
325 rq->q->softirq_done_fn(rq);
326 return;
327 }
Jens Axboe320ae512013-10-24 09:20:05 +0100328
329 cpu = get_cpu();
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800330 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800331 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800332 rq->csd.info = rq;
333 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100334 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800335 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800336 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800337 }
Jens Axboe320ae512013-10-24 09:20:05 +0100338 put_cpu();
339}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800340
341/**
342 * blk_mq_complete_request - end I/O on a request
343 * @rq: the request being processed
344 *
345 * Description:
346 * Ends all I/O on a request. It does not handle partial completions.
347 * The actual completion happens out-of-order, through a IPI handler.
348 **/
349void blk_mq_complete_request(struct request *rq)
350{
351 if (unlikely(blk_should_fake_timeout(rq->q)))
352 return;
353 if (!blk_mark_rq_complete(rq))
354 __blk_mq_complete_request(rq);
355}
356EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100357
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800358static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100359{
360 struct request_queue *q = rq->q;
361
362 trace_block_rq_issue(q, rq);
363
Christoph Hellwig742ee692014-04-14 10:30:06 +0200364 rq->resid_len = blk_rq_bytes(rq);
365
Jens Axboe320ae512013-10-24 09:20:05 +0100366 /*
367 * Just mark start time and set the started bit. Due to memory
368 * ordering, we know we'll see the correct deadline as long as
369 * REQ_ATOMIC_STARTED is seen.
370 */
371 rq->deadline = jiffies + q->rq_timeout;
372 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800373
374 if (q->dma_drain_size && blk_rq_bytes(rq)) {
375 /*
376 * Make sure space for the drain appears. We know we can do
377 * this because max_hw_segments has been adjusted to be one
378 * fewer than the device can handle.
379 */
380 rq->nr_phys_segments++;
381 }
382
383 /*
384 * Flag the last request in the series so that drivers know when IO
385 * should be kicked off, if they don't do it on a per-request basis.
386 *
387 * Note: the flag isn't the only condition drivers should do kick off.
388 * If drive is busy, the last request might not have the bit set.
389 */
390 if (last)
391 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100392}
393
394static void blk_mq_requeue_request(struct request *rq)
395{
396 struct request_queue *q = rq->q;
397
398 trace_block_rq_requeue(q, rq);
399 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800400
401 rq->cmd_flags &= ~REQ_END;
402
403 if (q->dma_drain_size && blk_rq_bytes(rq))
404 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100405}
406
407struct blk_mq_timeout_data {
408 struct blk_mq_hw_ctx *hctx;
409 unsigned long *next;
410 unsigned int *next_set;
411};
412
413static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
414{
415 struct blk_mq_timeout_data *data = __data;
416 struct blk_mq_hw_ctx *hctx = data->hctx;
417 unsigned int tag;
418
419 /* It may not be in flight yet (this is where
420 * the REQ_ATOMIC_STARTED flag comes in). The requests are
421 * statically allocated, so we know it's always safe to access the
422 * memory associated with a bit offset into ->rqs[].
423 */
424 tag = 0;
425 do {
426 struct request *rq;
427
428 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
429 if (tag >= hctx->queue_depth)
430 break;
431
432 rq = hctx->rqs[tag++];
433
434 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
435 continue;
436
437 blk_rq_check_expired(rq, data->next, data->next_set);
438 } while (1);
439}
440
441static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
442 unsigned long *next,
443 unsigned int *next_set)
444{
445 struct blk_mq_timeout_data data = {
446 .hctx = hctx,
447 .next = next,
448 .next_set = next_set,
449 };
450
451 /*
452 * Ask the tagging code to iterate busy requests, so we can
453 * check them for timeout.
454 */
455 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
456}
457
458static void blk_mq_rq_timer(unsigned long data)
459{
460 struct request_queue *q = (struct request_queue *) data;
461 struct blk_mq_hw_ctx *hctx;
462 unsigned long next = 0;
463 int i, next_set = 0;
464
465 queue_for_each_hw_ctx(q, hctx, i)
466 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
467
468 if (next_set)
469 mod_timer(&q->timeout, round_jiffies_up(next));
470}
471
472/*
473 * Reverse check our software queue for entries that we could potentially
474 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
475 * too much time checking for merges.
476 */
477static bool blk_mq_attempt_merge(struct request_queue *q,
478 struct blk_mq_ctx *ctx, struct bio *bio)
479{
480 struct request *rq;
481 int checked = 8;
482
483 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
484 int el_ret;
485
486 if (!checked--)
487 break;
488
489 if (!blk_rq_merge_ok(rq, bio))
490 continue;
491
492 el_ret = blk_try_merge(rq, bio);
493 if (el_ret == ELEVATOR_BACK_MERGE) {
494 if (bio_attempt_back_merge(q, rq, bio)) {
495 ctx->rq_merged++;
496 return true;
497 }
498 break;
499 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
500 if (bio_attempt_front_merge(q, rq, bio)) {
501 ctx->rq_merged++;
502 return true;
503 }
504 break;
505 }
506 }
507
508 return false;
509}
510
511void blk_mq_add_timer(struct request *rq)
512{
513 __blk_add_timer(rq, NULL);
514}
515
516/*
517 * Run this hardware queue, pulling any software queues mapped to it in.
518 * Note that this function currently has various problems around ordering
519 * of IO. In particular, we'd like FIFO behaviour on handling existing
520 * items on the hctx->dispatch list. Ignore that for now.
521 */
522static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
523{
524 struct request_queue *q = hctx->queue;
525 struct blk_mq_ctx *ctx;
526 struct request *rq;
527 LIST_HEAD(rq_list);
528 int bit, queued;
529
Jens Axboee4043dc2014-04-09 10:18:23 -0600530 WARN_ON(!preempt_count());
531
Jens Axboe5d12f902014-03-19 15:25:02 -0600532 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100533 return;
534
535 hctx->run++;
536
537 /*
538 * Touch any software queue that has pending entries.
539 */
540 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
541 clear_bit(bit, hctx->ctx_map);
542 ctx = hctx->ctxs[bit];
543 BUG_ON(bit != ctx->index_hw);
544
545 spin_lock(&ctx->lock);
546 list_splice_tail_init(&ctx->rq_list, &rq_list);
547 spin_unlock(&ctx->lock);
548 }
549
550 /*
551 * If we have previous entries on our dispatch list, grab them
552 * and stuff them at the front for more fair dispatch.
553 */
554 if (!list_empty_careful(&hctx->dispatch)) {
555 spin_lock(&hctx->lock);
556 if (!list_empty(&hctx->dispatch))
557 list_splice_init(&hctx->dispatch, &rq_list);
558 spin_unlock(&hctx->lock);
559 }
560
561 /*
562 * Delete and return all entries from our dispatch list
563 */
564 queued = 0;
565
566 /*
567 * Now process all the entries, sending them to the driver.
568 */
569 while (!list_empty(&rq_list)) {
570 int ret;
571
572 rq = list_first_entry(&rq_list, struct request, queuelist);
573 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100574
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800575 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100576
577 ret = q->mq_ops->queue_rq(hctx, rq);
578 switch (ret) {
579 case BLK_MQ_RQ_QUEUE_OK:
580 queued++;
581 continue;
582 case BLK_MQ_RQ_QUEUE_BUSY:
583 /*
584 * FIXME: we should have a mechanism to stop the queue
585 * like blk_stop_queue, otherwise we will waste cpu
586 * time
587 */
588 list_add(&rq->queuelist, &rq_list);
589 blk_mq_requeue_request(rq);
590 break;
591 default:
592 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100593 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800594 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100595 blk_mq_end_io(rq, rq->errors);
596 break;
597 }
598
599 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
600 break;
601 }
602
603 if (!queued)
604 hctx->dispatched[0]++;
605 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
606 hctx->dispatched[ilog2(queued) + 1]++;
607
608 /*
609 * Any items that need requeuing? Stuff them into hctx->dispatch,
610 * that is where we will continue on next queue run.
611 */
612 if (!list_empty(&rq_list)) {
613 spin_lock(&hctx->lock);
614 list_splice(&rq_list, &hctx->dispatch);
615 spin_unlock(&hctx->lock);
616 }
617}
618
619void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
620{
Jens Axboe5d12f902014-03-19 15:25:02 -0600621 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100622 return;
623
Jens Axboee4043dc2014-04-09 10:18:23 -0600624 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100625 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600626 else if (hctx->queue->nr_hw_queues == 1)
Jens Axboe59c3d452014-04-08 09:15:35 -0600627 kblockd_schedule_delayed_work(&hctx->delayed_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600628 else {
629 unsigned int cpu;
630
631 /*
632 * It'd be great if the workqueue API had a way to pass
633 * in a mask and had some smarts for more clever placement
634 * than the first CPU. Or we could round-robin here. For now,
635 * just queue on the first CPU.
636 */
637 cpu = cpumask_first(hctx->cpumask);
638 kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0);
639 }
Jens Axboe320ae512013-10-24 09:20:05 +0100640}
641
642void blk_mq_run_queues(struct request_queue *q, bool async)
643{
644 struct blk_mq_hw_ctx *hctx;
645 int i;
646
647 queue_for_each_hw_ctx(q, hctx, i) {
648 if ((!blk_mq_hctx_has_pending(hctx) &&
649 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600650 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100651 continue;
652
Jens Axboee4043dc2014-04-09 10:18:23 -0600653 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100654 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600655 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100656 }
657}
658EXPORT_SYMBOL(blk_mq_run_queues);
659
660void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
661{
662 cancel_delayed_work(&hctx->delayed_work);
663 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
664}
665EXPORT_SYMBOL(blk_mq_stop_hw_queue);
666
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100667void blk_mq_stop_hw_queues(struct request_queue *q)
668{
669 struct blk_mq_hw_ctx *hctx;
670 int i;
671
672 queue_for_each_hw_ctx(q, hctx, i)
673 blk_mq_stop_hw_queue(hctx);
674}
675EXPORT_SYMBOL(blk_mq_stop_hw_queues);
676
Jens Axboe320ae512013-10-24 09:20:05 +0100677void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
678{
679 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600680
681 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100682 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600683 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100684}
685EXPORT_SYMBOL(blk_mq_start_hw_queue);
686
687void blk_mq_start_stopped_hw_queues(struct request_queue *q)
688{
689 struct blk_mq_hw_ctx *hctx;
690 int i;
691
692 queue_for_each_hw_ctx(q, hctx, i) {
693 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
694 continue;
695
696 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600697 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100698 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -0600699 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100700 }
701}
702EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
703
704static void blk_mq_work_fn(struct work_struct *work)
705{
706 struct blk_mq_hw_ctx *hctx;
707
708 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600709
710 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100711 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600712 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100713}
714
715static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800716 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100717{
718 struct blk_mq_ctx *ctx = rq->mq_ctx;
719
Jens Axboe01b983c2013-11-19 18:59:10 -0700720 trace_block_rq_insert(hctx->queue, rq);
721
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800722 if (at_head)
723 list_add(&rq->queuelist, &ctx->rq_list);
724 else
725 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100726 blk_mq_hctx_mark_pending(hctx, ctx);
727
728 /*
729 * We do this early, to ensure we are on the right CPU.
730 */
731 blk_mq_add_timer(rq);
732}
733
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600734void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
735 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100736{
737 struct request_queue *q = rq->q;
738 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600739 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100740
741 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600742 if (!cpu_online(ctx->cpu))
743 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100744
Jens Axboe320ae512013-10-24 09:20:05 +0100745 hctx = q->mq_ops->map_queue(q, ctx->cpu);
746
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600747 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
748 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
749 blk_insert_flush(rq);
750 } else {
751 spin_lock(&ctx->lock);
752 __blk_mq_insert_request(hctx, rq, at_head);
753 spin_unlock(&ctx->lock);
754 }
Jens Axboe320ae512013-10-24 09:20:05 +0100755
Jens Axboe320ae512013-10-24 09:20:05 +0100756 if (run_queue)
757 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600758
759 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100760}
761
762static void blk_mq_insert_requests(struct request_queue *q,
763 struct blk_mq_ctx *ctx,
764 struct list_head *list,
765 int depth,
766 bool from_schedule)
767
768{
769 struct blk_mq_hw_ctx *hctx;
770 struct blk_mq_ctx *current_ctx;
771
772 trace_block_unplug(q, depth, !from_schedule);
773
774 current_ctx = blk_mq_get_ctx(q);
775
776 if (!cpu_online(ctx->cpu))
777 ctx = current_ctx;
778 hctx = q->mq_ops->map_queue(q, ctx->cpu);
779
780 /*
781 * preemption doesn't flush plug list, so it's possible ctx->cpu is
782 * offline now
783 */
784 spin_lock(&ctx->lock);
785 while (!list_empty(list)) {
786 struct request *rq;
787
788 rq = list_first_entry(list, struct request, queuelist);
789 list_del_init(&rq->queuelist);
790 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800791 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100792 }
793 spin_unlock(&ctx->lock);
794
Jens Axboe320ae512013-10-24 09:20:05 +0100795 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600796 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100797}
798
799static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
800{
801 struct request *rqa = container_of(a, struct request, queuelist);
802 struct request *rqb = container_of(b, struct request, queuelist);
803
804 return !(rqa->mq_ctx < rqb->mq_ctx ||
805 (rqa->mq_ctx == rqb->mq_ctx &&
806 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
807}
808
809void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
810{
811 struct blk_mq_ctx *this_ctx;
812 struct request_queue *this_q;
813 struct request *rq;
814 LIST_HEAD(list);
815 LIST_HEAD(ctx_list);
816 unsigned int depth;
817
818 list_splice_init(&plug->mq_list, &list);
819
820 list_sort(NULL, &list, plug_ctx_cmp);
821
822 this_q = NULL;
823 this_ctx = NULL;
824 depth = 0;
825
826 while (!list_empty(&list)) {
827 rq = list_entry_rq(list.next);
828 list_del_init(&rq->queuelist);
829 BUG_ON(!rq->q);
830 if (rq->mq_ctx != this_ctx) {
831 if (this_ctx) {
832 blk_mq_insert_requests(this_q, this_ctx,
833 &ctx_list, depth,
834 from_schedule);
835 }
836
837 this_ctx = rq->mq_ctx;
838 this_q = rq->q;
839 depth = 0;
840 }
841
842 depth++;
843 list_add_tail(&rq->queuelist, &ctx_list);
844 }
845
846 /*
847 * If 'this_ctx' is set, we know we have entries to complete
848 * on 'ctx_list'. Do those.
849 */
850 if (this_ctx) {
851 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
852 from_schedule);
853 }
854}
855
856static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
857{
858 init_request_from_bio(rq, bio);
859 blk_account_io_start(rq, 1);
860}
861
862static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
863{
864 struct blk_mq_hw_ctx *hctx;
865 struct blk_mq_ctx *ctx;
866 const int is_sync = rw_is_sync(bio->bi_rw);
867 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
868 int rw = bio_data_dir(bio);
869 struct request *rq;
870 unsigned int use_plug, request_count = 0;
871
872 /*
873 * If we have multiple hardware queues, just go directly to
874 * one of those for sync IO.
875 */
876 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
877
878 blk_queue_bounce(q, &bio);
879
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -0700880 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
881 bio_endio(bio, -EIO);
882 return;
883 }
884
Jens Axboe320ae512013-10-24 09:20:05 +0100885 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
886 return;
887
888 if (blk_mq_queue_enter(q)) {
889 bio_endio(bio, -EIO);
890 return;
891 }
892
893 ctx = blk_mq_get_ctx(q);
894 hctx = q->mq_ops->map_queue(q, ctx->cpu);
895
Shaohua Li27fbf4e82014-02-19 20:20:21 +0800896 if (is_sync)
897 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +0100898 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700899 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100900 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -0700901 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100902 else {
903 blk_mq_put_ctx(ctx);
904 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700905 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
906 false);
Jens Axboe320ae512013-10-24 09:20:05 +0100907 ctx = rq->mq_ctx;
908 hctx = q->mq_ops->map_queue(q, ctx->cpu);
909 }
910
911 hctx->queued++;
912
913 if (unlikely(is_flush_fua)) {
914 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +0100915 blk_insert_flush(rq);
916 goto run_queue;
917 }
918
919 /*
920 * A task plug currently exists. Since this is completely lockless,
921 * utilize that to temporarily store requests until the task is
922 * either done or scheduled away.
923 */
924 if (use_plug) {
925 struct blk_plug *plug = current->plug;
926
927 if (plug) {
928 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -0600929 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +0100930 trace_block_plug(q);
931 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
932 blk_flush_plug_list(plug, false);
933 trace_block_plug(q);
934 }
935 list_add_tail(&rq->queuelist, &plug->mq_list);
936 blk_mq_put_ctx(ctx);
937 return;
938 }
939 }
940
941 spin_lock(&ctx->lock);
942
943 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
944 blk_mq_attempt_merge(q, ctx, bio))
945 __blk_mq_free_request(hctx, ctx, rq);
946 else {
947 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800948 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100949 }
950
951 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100952
953 /*
954 * For a SYNC request, send it to the hardware immediately. For an
955 * ASYNC request, just ensure that we run it later on. The latter
956 * allows for merging opportunities and more efficient dispatching.
957 */
958run_queue:
959 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -0600960 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100961}
962
963/*
964 * Default mapping to a software queue, since we use one per CPU.
965 */
966struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
967{
968 return q->queue_hw_ctx[q->mq_map[cpu]];
969}
970EXPORT_SYMBOL(blk_mq_map_queue);
971
972struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
973 unsigned int hctx_index)
974{
975 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
976 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
977}
978EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
979
980void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
981 unsigned int hctx_index)
982{
983 kfree(hctx);
984}
985EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
986
987static void blk_mq_hctx_notify(void *data, unsigned long action,
988 unsigned int cpu)
989{
990 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -0600991 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100992 struct blk_mq_ctx *ctx;
993 LIST_HEAD(tmp);
994
995 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
996 return;
997
998 /*
999 * Move ctx entries to new CPU, if this one is going away.
1000 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001001 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001002
1003 spin_lock(&ctx->lock);
1004 if (!list_empty(&ctx->rq_list)) {
1005 list_splice_init(&ctx->rq_list, &tmp);
1006 clear_bit(ctx->index_hw, hctx->ctx_map);
1007 }
1008 spin_unlock(&ctx->lock);
1009
1010 if (list_empty(&tmp))
1011 return;
1012
Jens Axboebccb5f72014-04-04 21:34:48 -06001013 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001014 spin_lock(&ctx->lock);
1015
1016 while (!list_empty(&tmp)) {
1017 struct request *rq;
1018
1019 rq = list_first_entry(&tmp, struct request, queuelist);
1020 rq->mq_ctx = ctx;
1021 list_move_tail(&rq->queuelist, &ctx->rq_list);
1022 }
1023
Jens Axboebccb5f72014-04-04 21:34:48 -06001024 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001025 blk_mq_hctx_mark_pending(hctx, ctx);
1026
1027 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001028
1029 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001030 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001031}
1032
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001033static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx, void *driver_data)
Jens Axboe320ae512013-10-24 09:20:05 +01001034{
1035 struct page *page;
1036
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001037 if (hctx->rqs && hctx->queue->mq_ops->exit_request) {
1038 int i;
1039
1040 for (i = 0; i < hctx->queue_depth; i++) {
1041 if (!hctx->rqs[i])
1042 continue;
1043 hctx->queue->mq_ops->exit_request(driver_data, hctx,
1044 hctx->rqs[i], i);
1045 }
1046 }
1047
Jens Axboe320ae512013-10-24 09:20:05 +01001048 while (!list_empty(&hctx->page_list)) {
Dave Hansen67534712014-01-08 20:17:46 -07001049 page = list_first_entry(&hctx->page_list, struct page, lru);
1050 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001051 __free_pages(page, page->private);
1052 }
1053
1054 kfree(hctx->rqs);
1055
1056 if (hctx->tags)
1057 blk_mq_free_tags(hctx->tags);
1058}
1059
1060static size_t order_to_size(unsigned int order)
1061{
1062 size_t ret = PAGE_SIZE;
1063
1064 while (order--)
1065 ret *= 2;
1066
1067 return ret;
1068}
1069
1070static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001071 struct blk_mq_reg *reg, void *driver_data, int node)
Jens Axboe320ae512013-10-24 09:20:05 +01001072{
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001073 unsigned int reserved_tags = reg->reserved_tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001074 unsigned int i, j, entries_per_page, max_order = 4;
1075 size_t rq_size, left;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001076 int error;
Jens Axboe320ae512013-10-24 09:20:05 +01001077
1078 INIT_LIST_HEAD(&hctx->page_list);
1079
1080 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1081 GFP_KERNEL, node);
1082 if (!hctx->rqs)
1083 return -ENOMEM;
1084
1085 /*
1086 * rq_size is the size of the request plus driver payload, rounded
1087 * to the cacheline size
1088 */
1089 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1090 cache_line_size());
1091 left = rq_size * hctx->queue_depth;
1092
1093 for (i = 0; i < hctx->queue_depth;) {
1094 int this_order = max_order;
1095 struct page *page;
1096 int to_do;
1097 void *p;
1098
1099 while (left < order_to_size(this_order - 1) && this_order)
1100 this_order--;
1101
1102 do {
1103 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1104 if (page)
1105 break;
1106 if (!this_order--)
1107 break;
1108 if (order_to_size(this_order) < rq_size)
1109 break;
1110 } while (1);
1111
1112 if (!page)
1113 break;
1114
1115 page->private = this_order;
Dave Hansen67534712014-01-08 20:17:46 -07001116 list_add_tail(&page->lru, &hctx->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001117
1118 p = page_address(page);
1119 entries_per_page = order_to_size(this_order) / rq_size;
1120 to_do = min(entries_per_page, hctx->queue_depth - i);
1121 left -= to_do * rq_size;
1122 for (j = 0; j < to_do; j++) {
1123 hctx->rqs[i] = p;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001124 if (reg->ops->init_request) {
1125 error = reg->ops->init_request(driver_data,
1126 hctx, hctx->rqs[i], i);
1127 if (error)
1128 goto err_rq_map;
1129 }
1130
Jens Axboe320ae512013-10-24 09:20:05 +01001131 p += rq_size;
1132 i++;
1133 }
1134 }
1135
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001136 if (i < (reserved_tags + BLK_MQ_TAG_MIN)) {
1137 error = -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01001138 goto err_rq_map;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001139 }
1140 if (i != hctx->queue_depth) {
Jens Axboe320ae512013-10-24 09:20:05 +01001141 hctx->queue_depth = i;
1142 pr_warn("%s: queue depth set to %u because of low memory\n",
1143 __func__, i);
1144 }
1145
1146 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1147 if (!hctx->tags) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001148 error = -ENOMEM;
1149 goto err_rq_map;
Jens Axboe320ae512013-10-24 09:20:05 +01001150 }
1151
1152 return 0;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001153err_rq_map:
1154 blk_mq_free_rq_map(hctx, driver_data);
1155 return error;
Jens Axboe320ae512013-10-24 09:20:05 +01001156}
1157
1158static int blk_mq_init_hw_queues(struct request_queue *q,
1159 struct blk_mq_reg *reg, void *driver_data)
1160{
1161 struct blk_mq_hw_ctx *hctx;
1162 unsigned int i, j;
1163
1164 /*
1165 * Initialize hardware queues
1166 */
1167 queue_for_each_hw_ctx(q, hctx, i) {
1168 unsigned int num_maps;
1169 int node;
1170
1171 node = hctx->numa_node;
1172 if (node == NUMA_NO_NODE)
1173 node = hctx->numa_node = reg->numa_node;
1174
1175 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1176 spin_lock_init(&hctx->lock);
1177 INIT_LIST_HEAD(&hctx->dispatch);
1178 hctx->queue = q;
1179 hctx->queue_num = i;
1180 hctx->flags = reg->flags;
1181 hctx->queue_depth = reg->queue_depth;
1182 hctx->cmd_size = reg->cmd_size;
1183
1184 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1185 blk_mq_hctx_notify, hctx);
1186 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1187
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001188 if (blk_mq_init_rq_map(hctx, reg, driver_data, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001189 break;
1190
1191 /*
1192 * Allocate space for all possible cpus to avoid allocation in
1193 * runtime
1194 */
1195 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1196 GFP_KERNEL, node);
1197 if (!hctx->ctxs)
1198 break;
1199
1200 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1201 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1202 GFP_KERNEL, node);
1203 if (!hctx->ctx_map)
1204 break;
1205
1206 hctx->nr_ctx_map = num_maps;
1207 hctx->nr_ctx = 0;
1208
1209 if (reg->ops->init_hctx &&
1210 reg->ops->init_hctx(hctx, driver_data, i))
1211 break;
1212 }
1213
1214 if (i == q->nr_hw_queues)
1215 return 0;
1216
1217 /*
1218 * Init failed
1219 */
1220 queue_for_each_hw_ctx(q, hctx, j) {
1221 if (i == j)
1222 break;
1223
1224 if (reg->ops->exit_hctx)
1225 reg->ops->exit_hctx(hctx, j);
1226
1227 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001228 blk_mq_free_rq_map(hctx, driver_data);
Jens Axboe320ae512013-10-24 09:20:05 +01001229 kfree(hctx->ctxs);
1230 }
1231
1232 return 1;
1233}
1234
1235static void blk_mq_init_cpu_queues(struct request_queue *q,
1236 unsigned int nr_hw_queues)
1237{
1238 unsigned int i;
1239
1240 for_each_possible_cpu(i) {
1241 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1242 struct blk_mq_hw_ctx *hctx;
1243
1244 memset(__ctx, 0, sizeof(*__ctx));
1245 __ctx->cpu = i;
1246 spin_lock_init(&__ctx->lock);
1247 INIT_LIST_HEAD(&__ctx->rq_list);
1248 __ctx->queue = q;
1249
1250 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001251 if (!cpu_online(i))
1252 continue;
1253
Jens Axboee4043dc2014-04-09 10:18:23 -06001254 hctx = q->mq_ops->map_queue(q, i);
1255 cpumask_set_cpu(i, hctx->cpumask);
1256 hctx->nr_ctx++;
1257
Jens Axboe320ae512013-10-24 09:20:05 +01001258 /*
1259 * Set local node, IFF we have more than one hw queue. If
1260 * not, we remain on the home node of the device
1261 */
1262 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1263 hctx->numa_node = cpu_to_node(i);
1264 }
1265}
1266
1267static void blk_mq_map_swqueue(struct request_queue *q)
1268{
1269 unsigned int i;
1270 struct blk_mq_hw_ctx *hctx;
1271 struct blk_mq_ctx *ctx;
1272
1273 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001274 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001275 hctx->nr_ctx = 0;
1276 }
1277
1278 /*
1279 * Map software to hardware queues
1280 */
1281 queue_for_each_ctx(q, ctx, i) {
1282 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001283 if (!cpu_online(i))
1284 continue;
1285
Jens Axboe320ae512013-10-24 09:20:05 +01001286 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001287 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001288 ctx->index_hw = hctx->nr_ctx;
1289 hctx->ctxs[hctx->nr_ctx++] = ctx;
1290 }
1291}
1292
1293struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1294 void *driver_data)
1295{
1296 struct blk_mq_hw_ctx **hctxs;
1297 struct blk_mq_ctx *ctx;
1298 struct request_queue *q;
1299 int i;
1300
1301 if (!reg->nr_hw_queues ||
1302 !reg->ops->queue_rq || !reg->ops->map_queue ||
1303 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1304 return ERR_PTR(-EINVAL);
1305
1306 if (!reg->queue_depth)
1307 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1308 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1309 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1310 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1311 }
1312
Jens Axboe320ae512013-10-24 09:20:05 +01001313 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1314 return ERR_PTR(-EINVAL);
1315
1316 ctx = alloc_percpu(struct blk_mq_ctx);
1317 if (!ctx)
1318 return ERR_PTR(-ENOMEM);
1319
1320 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1321 reg->numa_node);
1322
1323 if (!hctxs)
1324 goto err_percpu;
1325
1326 for (i = 0; i < reg->nr_hw_queues; i++) {
1327 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1328 if (!hctxs[i])
1329 goto err_hctxs;
1330
Jens Axboee4043dc2014-04-09 10:18:23 -06001331 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1332 goto err_hctxs;
1333
Jens Axboe320ae512013-10-24 09:20:05 +01001334 hctxs[i]->numa_node = NUMA_NO_NODE;
1335 hctxs[i]->queue_num = i;
1336 }
1337
1338 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1339 if (!q)
1340 goto err_hctxs;
1341
1342 q->mq_map = blk_mq_make_queue_map(reg);
1343 if (!q->mq_map)
1344 goto err_map;
1345
1346 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1347 blk_queue_rq_timeout(q, 30000);
1348
1349 q->nr_queues = nr_cpu_ids;
1350 q->nr_hw_queues = reg->nr_hw_queues;
1351
1352 q->queue_ctx = ctx;
1353 q->queue_hw_ctx = hctxs;
1354
1355 q->mq_ops = reg->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001356 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001357
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001358 q->sg_reserved_size = INT_MAX;
1359
Jens Axboe320ae512013-10-24 09:20:05 +01001360 blk_queue_make_request(q, blk_mq_make_request);
1361 blk_queue_rq_timed_out(q, reg->ops->timeout);
1362 if (reg->timeout)
1363 blk_queue_rq_timeout(q, reg->timeout);
1364
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001365 if (reg->ops->complete)
1366 blk_queue_softirq_done(q, reg->ops->complete);
1367
Jens Axboe320ae512013-10-24 09:20:05 +01001368 blk_mq_init_flush(q);
1369 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1370
Christoph Hellwig18741982014-02-10 09:29:00 -07001371 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1372 cache_line_size()), GFP_KERNEL);
1373 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001374 goto err_hw;
1375
Christoph Hellwig18741982014-02-10 09:29:00 -07001376 if (blk_mq_init_hw_queues(q, reg, driver_data))
1377 goto err_flush_rq;
1378
Jens Axboe320ae512013-10-24 09:20:05 +01001379 blk_mq_map_swqueue(q);
1380
1381 mutex_lock(&all_q_mutex);
1382 list_add_tail(&q->all_q_node, &all_q_list);
1383 mutex_unlock(&all_q_mutex);
1384
1385 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001386
1387err_flush_rq:
1388 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001389err_hw:
1390 kfree(q->mq_map);
1391err_map:
1392 blk_cleanup_queue(q);
1393err_hctxs:
1394 for (i = 0; i < reg->nr_hw_queues; i++) {
1395 if (!hctxs[i])
1396 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001397 free_cpumask_var(hctxs[i]->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001398 reg->ops->free_hctx(hctxs[i], i);
1399 }
1400 kfree(hctxs);
1401err_percpu:
1402 free_percpu(ctx);
1403 return ERR_PTR(-ENOMEM);
1404}
1405EXPORT_SYMBOL(blk_mq_init_queue);
1406
1407void blk_mq_free_queue(struct request_queue *q)
1408{
1409 struct blk_mq_hw_ctx *hctx;
1410 int i;
1411
1412 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001413 kfree(hctx->ctx_map);
1414 kfree(hctx->ctxs);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001415 blk_mq_free_rq_map(hctx, q->queuedata);
Jens Axboe320ae512013-10-24 09:20:05 +01001416 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1417 if (q->mq_ops->exit_hctx)
1418 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001419 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001420 q->mq_ops->free_hctx(hctx, i);
1421 }
1422
1423 free_percpu(q->queue_ctx);
1424 kfree(q->queue_hw_ctx);
1425 kfree(q->mq_map);
1426
1427 q->queue_ctx = NULL;
1428 q->queue_hw_ctx = NULL;
1429 q->mq_map = NULL;
1430
1431 mutex_lock(&all_q_mutex);
1432 list_del_init(&q->all_q_node);
1433 mutex_unlock(&all_q_mutex);
1434}
Jens Axboe320ae512013-10-24 09:20:05 +01001435
1436/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001437static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001438{
1439 blk_mq_freeze_queue(q);
1440
1441 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1442
1443 /*
1444 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1445 * we should change hctx numa_node according to new topology (this
1446 * involves free and re-allocate memory, worthy doing?)
1447 */
1448
1449 blk_mq_map_swqueue(q);
1450
1451 blk_mq_unfreeze_queue(q);
1452}
1453
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001454static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1455 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001456{
1457 struct request_queue *q;
1458
1459 /*
1460 * Before new mapping is established, hotadded cpu might already start
1461 * handling requests. This doesn't break anything as we map offline
1462 * CPUs to first hardware queue. We will re-init queue below to get
1463 * optimal settings.
1464 */
1465 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1466 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1467 return NOTIFY_OK;
1468
1469 mutex_lock(&all_q_mutex);
1470 list_for_each_entry(q, &all_q_list, all_q_node)
1471 blk_mq_queue_reinit(q);
1472 mutex_unlock(&all_q_mutex);
1473 return NOTIFY_OK;
1474}
1475
Jens Axboe676141e2014-03-20 13:29:18 -06001476void blk_mq_disable_hotplug(void)
1477{
1478 mutex_lock(&all_q_mutex);
1479}
1480
1481void blk_mq_enable_hotplug(void)
1482{
1483 mutex_unlock(&all_q_mutex);
1484}
1485
Jens Axboe320ae512013-10-24 09:20:05 +01001486static int __init blk_mq_init(void)
1487{
Jens Axboe320ae512013-10-24 09:20:05 +01001488 blk_mq_cpu_init();
1489
1490 /* Must be called after percpu_counter_hotcpu_callback() */
1491 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1492
1493 return 0;
1494}
1495subsys_initcall(blk_mq_init);