blob: b59a8d027dff956ec2c45b59b9f61008fb1e483c [file] [log] [blame]
Jens Axboe320ae512013-10-24 09:20:05 +01001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/backing-dev.h>
4#include <linux/bio.h>
5#include <linux/blkdev.h>
6#include <linux/mm.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/workqueue.h>
10#include <linux/smp.h>
11#include <linux/llist.h>
12#include <linux/list_sort.h>
13#include <linux/cpu.h>
14#include <linux/cache.h>
15#include <linux/sched/sysctl.h>
16#include <linux/delay.h>
17
18#include <trace/events/block.h>
19
20#include <linux/blk-mq.h>
21#include "blk.h"
22#include "blk-mq.h"
23#include "blk-mq-tag.h"
24
25static DEFINE_MUTEX(all_q_mutex);
26static LIST_HEAD(all_q_list);
27
28static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
29
Jens Axboe320ae512013-10-24 09:20:05 +010030static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
31 unsigned int cpu)
32{
33 return per_cpu_ptr(q->queue_ctx, cpu);
34}
35
36/*
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
41 */
42static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
43{
44 return __blk_mq_get_ctx(q, get_cpu());
45}
46
47static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
48{
49 put_cpu();
50}
51
52/*
53 * Check if any of the ctx's have pending work in this hardware queue
54 */
55static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
56{
57 unsigned int i;
58
59 for (i = 0; i < hctx->nr_ctx_map; i++)
60 if (hctx->ctx_map[i])
61 return true;
62
63 return false;
64}
65
66/*
67 * Mark this ctx as having pending work in this hardware queue
68 */
69static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70 struct blk_mq_ctx *ctx)
71{
72 if (!test_bit(ctx->index_hw, hctx->ctx_map))
73 set_bit(ctx->index_hw, hctx->ctx_map);
74}
75
Christoph Hellwig081241e2014-02-20 15:32:36 -080076static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +010078{
79 struct request *rq;
80 unsigned int tag;
81
82 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83 if (tag != BLK_MQ_TAG_FAIL) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -060084 rq = hctx->tags->rqs[tag];
Christoph Hellwiged448322014-04-14 10:30:10 +020085 blk_rq_init(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +010086 rq->tag = tag;
87
88 return rq;
89 }
90
91 return NULL;
92}
93
94static int blk_mq_queue_enter(struct request_queue *q)
95{
96 int ret;
97
98 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
99 smp_wmb();
100 /* we have problems to freeze the queue if it's initializing */
101 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
102 return 0;
103
104 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
105
106 spin_lock_irq(q->queue_lock);
107 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800108 !blk_queue_bypass(q) || blk_queue_dying(q),
109 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100110 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800111 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100112 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800113 else if (blk_queue_dying(q))
114 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100115 spin_unlock_irq(q->queue_lock);
116
117 return ret;
118}
119
120static void blk_mq_queue_exit(struct request_queue *q)
121{
122 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
123}
124
Ming Lei43a5e4e2013-12-26 21:31:35 +0800125static void __blk_mq_drain_queue(struct request_queue *q)
126{
127 while (true) {
128 s64 count;
129
130 spin_lock_irq(q->queue_lock);
131 count = percpu_counter_sum(&q->mq_usage_counter);
132 spin_unlock_irq(q->queue_lock);
133
134 if (count == 0)
135 break;
136 blk_mq_run_queues(q, false);
137 msleep(10);
138 }
139}
140
Jens Axboe320ae512013-10-24 09:20:05 +0100141/*
142 * Guarantee no request is in use, so we can change any data structure of
143 * the queue afterward.
144 */
145static void blk_mq_freeze_queue(struct request_queue *q)
146{
147 bool drain;
148
149 spin_lock_irq(q->queue_lock);
150 drain = !q->bypass_depth++;
151 queue_flag_set(QUEUE_FLAG_BYPASS, q);
152 spin_unlock_irq(q->queue_lock);
153
Ming Lei43a5e4e2013-12-26 21:31:35 +0800154 if (drain)
155 __blk_mq_drain_queue(q);
156}
Jens Axboe320ae512013-10-24 09:20:05 +0100157
Ming Lei43a5e4e2013-12-26 21:31:35 +0800158void blk_mq_drain_queue(struct request_queue *q)
159{
160 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100161}
162
163static void blk_mq_unfreeze_queue(struct request_queue *q)
164{
165 bool wake = false;
166
167 spin_lock_irq(q->queue_lock);
168 if (!--q->bypass_depth) {
169 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
170 wake = true;
171 }
172 WARN_ON_ONCE(q->bypass_depth < 0);
173 spin_unlock_irq(q->queue_lock);
174 if (wake)
175 wake_up_all(&q->mq_freeze_wq);
176}
177
178bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
179{
180 return blk_mq_has_free_tags(hctx->tags);
181}
182EXPORT_SYMBOL(blk_mq_can_queue);
183
Jens Axboe94eddfb2013-11-19 09:25:07 -0700184static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
185 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100186{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700187 if (blk_queue_io_stat(q))
188 rw_flags |= REQ_IO_STAT;
189
Jens Axboe320ae512013-10-24 09:20:05 +0100190 rq->mq_ctx = ctx;
191 rq->cmd_flags = rw_flags;
Ming Lei0fec08b2014-01-03 10:00:08 -0700192 rq->start_time = jiffies;
193 set_start_time_ns(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100194 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195}
196
Jens Axboe320ae512013-10-24 09:20:05 +0100197static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
198 int rw, gfp_t gfp,
199 bool reserved)
200{
201 struct request *rq;
202
203 do {
204 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
205 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
206
Christoph Hellwig18741982014-02-10 09:29:00 -0700207 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100208 if (rq) {
Jens Axboe94eddfb2013-11-19 09:25:07 -0700209 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100210 break;
Jeff Moyer959a35f2013-12-03 14:23:00 -0700211 }
Jens Axboe320ae512013-10-24 09:20:05 +0100212
Jens Axboee4043dc2014-04-09 10:18:23 -0600213 if (gfp & __GFP_WAIT) {
214 __blk_mq_run_hw_queue(hctx);
215 blk_mq_put_ctx(ctx);
216 } else {
217 blk_mq_put_ctx(ctx);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700218 break;
Jens Axboee4043dc2014-04-09 10:18:23 -0600219 }
Jeff Moyer959a35f2013-12-03 14:23:00 -0700220
Jens Axboe320ae512013-10-24 09:20:05 +0100221 blk_mq_wait_for_tags(hctx->tags);
222 } while (1);
223
224 return rq;
225}
226
Christoph Hellwig18741982014-02-10 09:29:00 -0700227struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100228{
229 struct request *rq;
230
231 if (blk_mq_queue_enter(q))
232 return NULL;
233
Christoph Hellwig18741982014-02-10 09:29:00 -0700234 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700235 if (rq)
236 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100237 return rq;
238}
239
240struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
241 gfp_t gfp)
242{
243 struct request *rq;
244
245 if (blk_mq_queue_enter(q))
246 return NULL;
247
248 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700249 if (rq)
250 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100251 return rq;
252}
253EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
254
Jens Axboe320ae512013-10-24 09:20:05 +0100255static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
256 struct blk_mq_ctx *ctx, struct request *rq)
257{
258 const int tag = rq->tag;
259 struct request_queue *q = rq->q;
260
Jens Axboe320ae512013-10-24 09:20:05 +0100261 blk_mq_put_tag(hctx->tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100262 blk_mq_queue_exit(q);
263}
264
265void blk_mq_free_request(struct request *rq)
266{
267 struct blk_mq_ctx *ctx = rq->mq_ctx;
268 struct blk_mq_hw_ctx *hctx;
269 struct request_queue *q = rq->q;
270
271 ctx->rq_completed[rq_is_sync(rq)]++;
272
273 hctx = q->mq_ops->map_queue(q, ctx->cpu);
274 __blk_mq_free_request(hctx, ctx, rq);
275}
276
Christoph Hellwig8727af42014-04-14 10:30:08 +0200277/*
278 * Clone all relevant state from a request that has been put on hold in
279 * the flush state machine into the preallocated flush request that hangs
280 * off the request queue.
281 *
282 * For a driver the flush request should be invisible, that's why we are
283 * impersonating the original request here.
284 */
285void blk_mq_clone_flush_request(struct request *flush_rq,
286 struct request *orig_rq)
287{
288 struct blk_mq_hw_ctx *hctx =
289 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
290
291 flush_rq->mq_ctx = orig_rq->mq_ctx;
292 flush_rq->tag = orig_rq->tag;
293 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
294 hctx->cmd_size);
295}
296
Christoph 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
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600407struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
408{
409 return tags->rqs[tag];
410}
411EXPORT_SYMBOL(blk_mq_tag_to_rq);
412
Jens Axboe320ae512013-10-24 09:20:05 +0100413struct blk_mq_timeout_data {
414 struct blk_mq_hw_ctx *hctx;
415 unsigned long *next;
416 unsigned int *next_set;
417};
418
419static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
420{
421 struct blk_mq_timeout_data *data = __data;
422 struct blk_mq_hw_ctx *hctx = data->hctx;
423 unsigned int tag;
424
425 /* It may not be in flight yet (this is where
426 * the REQ_ATOMIC_STARTED flag comes in). The requests are
427 * statically allocated, so we know it's always safe to access the
428 * memory associated with a bit offset into ->rqs[].
429 */
430 tag = 0;
431 do {
432 struct request *rq;
433
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600434 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
435 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100436 break;
437
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600438 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
439 if (rq->q != hctx->queue)
440 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100441 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
442 continue;
443
444 blk_rq_check_expired(rq, data->next, data->next_set);
445 } while (1);
446}
447
448static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
449 unsigned long *next,
450 unsigned int *next_set)
451{
452 struct blk_mq_timeout_data data = {
453 .hctx = hctx,
454 .next = next,
455 .next_set = next_set,
456 };
457
458 /*
459 * Ask the tagging code to iterate busy requests, so we can
460 * check them for timeout.
461 */
462 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
463}
464
465static void blk_mq_rq_timer(unsigned long data)
466{
467 struct request_queue *q = (struct request_queue *) data;
468 struct blk_mq_hw_ctx *hctx;
469 unsigned long next = 0;
470 int i, next_set = 0;
471
472 queue_for_each_hw_ctx(q, hctx, i)
473 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
474
475 if (next_set)
476 mod_timer(&q->timeout, round_jiffies_up(next));
477}
478
479/*
480 * Reverse check our software queue for entries that we could potentially
481 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
482 * too much time checking for merges.
483 */
484static bool blk_mq_attempt_merge(struct request_queue *q,
485 struct blk_mq_ctx *ctx, struct bio *bio)
486{
487 struct request *rq;
488 int checked = 8;
489
490 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
491 int el_ret;
492
493 if (!checked--)
494 break;
495
496 if (!blk_rq_merge_ok(rq, bio))
497 continue;
498
499 el_ret = blk_try_merge(rq, bio);
500 if (el_ret == ELEVATOR_BACK_MERGE) {
501 if (bio_attempt_back_merge(q, rq, bio)) {
502 ctx->rq_merged++;
503 return true;
504 }
505 break;
506 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
507 if (bio_attempt_front_merge(q, rq, bio)) {
508 ctx->rq_merged++;
509 return true;
510 }
511 break;
512 }
513 }
514
515 return false;
516}
517
518void blk_mq_add_timer(struct request *rq)
519{
520 __blk_add_timer(rq, NULL);
521}
522
523/*
524 * Run this hardware queue, pulling any software queues mapped to it in.
525 * Note that this function currently has various problems around ordering
526 * of IO. In particular, we'd like FIFO behaviour on handling existing
527 * items on the hctx->dispatch list. Ignore that for now.
528 */
529static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
530{
531 struct request_queue *q = hctx->queue;
532 struct blk_mq_ctx *ctx;
533 struct request *rq;
534 LIST_HEAD(rq_list);
535 int bit, queued;
536
Jens Axboefd1270d2014-04-16 09:23:48 -0600537 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600538
Jens Axboe5d12f902014-03-19 15:25:02 -0600539 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100540 return;
541
542 hctx->run++;
543
544 /*
545 * Touch any software queue that has pending entries.
546 */
547 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
548 clear_bit(bit, hctx->ctx_map);
549 ctx = hctx->ctxs[bit];
550 BUG_ON(bit != ctx->index_hw);
551
552 spin_lock(&ctx->lock);
553 list_splice_tail_init(&ctx->rq_list, &rq_list);
554 spin_unlock(&ctx->lock);
555 }
556
557 /*
558 * If we have previous entries on our dispatch list, grab them
559 * and stuff them at the front for more fair dispatch.
560 */
561 if (!list_empty_careful(&hctx->dispatch)) {
562 spin_lock(&hctx->lock);
563 if (!list_empty(&hctx->dispatch))
564 list_splice_init(&hctx->dispatch, &rq_list);
565 spin_unlock(&hctx->lock);
566 }
567
568 /*
569 * Delete and return all entries from our dispatch list
570 */
571 queued = 0;
572
573 /*
574 * Now process all the entries, sending them to the driver.
575 */
576 while (!list_empty(&rq_list)) {
577 int ret;
578
579 rq = list_first_entry(&rq_list, struct request, queuelist);
580 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100581
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800582 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100583
584 ret = q->mq_ops->queue_rq(hctx, rq);
585 switch (ret) {
586 case BLK_MQ_RQ_QUEUE_OK:
587 queued++;
588 continue;
589 case BLK_MQ_RQ_QUEUE_BUSY:
590 /*
591 * FIXME: we should have a mechanism to stop the queue
592 * like blk_stop_queue, otherwise we will waste cpu
593 * time
594 */
595 list_add(&rq->queuelist, &rq_list);
596 blk_mq_requeue_request(rq);
597 break;
598 default:
599 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100600 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800601 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100602 blk_mq_end_io(rq, rq->errors);
603 break;
604 }
605
606 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
607 break;
608 }
609
610 if (!queued)
611 hctx->dispatched[0]++;
612 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
613 hctx->dispatched[ilog2(queued) + 1]++;
614
615 /*
616 * Any items that need requeuing? Stuff them into hctx->dispatch,
617 * that is where we will continue on next queue run.
618 */
619 if (!list_empty(&rq_list)) {
620 spin_lock(&hctx->lock);
621 list_splice(&rq_list, &hctx->dispatch);
622 spin_unlock(&hctx->lock);
623 }
624}
625
626void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
627{
Jens Axboe5d12f902014-03-19 15:25:02 -0600628 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100629 return;
630
Jens Axboee4043dc2014-04-09 10:18:23 -0600631 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100632 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600633 else if (hctx->queue->nr_hw_queues == 1)
Jens Axboe59c3d452014-04-08 09:15:35 -0600634 kblockd_schedule_delayed_work(&hctx->delayed_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600635 else {
636 unsigned int cpu;
637
638 /*
639 * It'd be great if the workqueue API had a way to pass
640 * in a mask and had some smarts for more clever placement
641 * than the first CPU. Or we could round-robin here. For now,
642 * just queue on the first CPU.
643 */
644 cpu = cpumask_first(hctx->cpumask);
645 kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0);
646 }
Jens Axboe320ae512013-10-24 09:20:05 +0100647}
648
649void blk_mq_run_queues(struct request_queue *q, bool async)
650{
651 struct blk_mq_hw_ctx *hctx;
652 int i;
653
654 queue_for_each_hw_ctx(q, hctx, i) {
655 if ((!blk_mq_hctx_has_pending(hctx) &&
656 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600657 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100658 continue;
659
Jens Axboee4043dc2014-04-09 10:18:23 -0600660 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100661 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600662 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100663 }
664}
665EXPORT_SYMBOL(blk_mq_run_queues);
666
667void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
668{
669 cancel_delayed_work(&hctx->delayed_work);
670 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
671}
672EXPORT_SYMBOL(blk_mq_stop_hw_queue);
673
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100674void blk_mq_stop_hw_queues(struct request_queue *q)
675{
676 struct blk_mq_hw_ctx *hctx;
677 int i;
678
679 queue_for_each_hw_ctx(q, hctx, i)
680 blk_mq_stop_hw_queue(hctx);
681}
682EXPORT_SYMBOL(blk_mq_stop_hw_queues);
683
Jens Axboe320ae512013-10-24 09:20:05 +0100684void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
685{
686 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600687
688 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100689 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600690 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100691}
692EXPORT_SYMBOL(blk_mq_start_hw_queue);
693
694void blk_mq_start_stopped_hw_queues(struct request_queue *q)
695{
696 struct blk_mq_hw_ctx *hctx;
697 int i;
698
699 queue_for_each_hw_ctx(q, hctx, i) {
700 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
701 continue;
702
703 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600704 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100705 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -0600706 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100707 }
708}
709EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
710
711static void blk_mq_work_fn(struct work_struct *work)
712{
713 struct blk_mq_hw_ctx *hctx;
714
715 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600716
Jens Axboe320ae512013-10-24 09:20:05 +0100717 __blk_mq_run_hw_queue(hctx);
718}
719
720static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800721 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100722{
723 struct blk_mq_ctx *ctx = rq->mq_ctx;
724
Jens Axboe01b983c2013-11-19 18:59:10 -0700725 trace_block_rq_insert(hctx->queue, rq);
726
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800727 if (at_head)
728 list_add(&rq->queuelist, &ctx->rq_list);
729 else
730 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100731 blk_mq_hctx_mark_pending(hctx, ctx);
732
733 /*
734 * We do this early, to ensure we are on the right CPU.
735 */
736 blk_mq_add_timer(rq);
737}
738
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600739void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
740 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100741{
742 struct request_queue *q = rq->q;
743 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600744 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100745
746 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600747 if (!cpu_online(ctx->cpu))
748 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100749
Jens Axboe320ae512013-10-24 09:20:05 +0100750 hctx = q->mq_ops->map_queue(q, ctx->cpu);
751
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600752 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
753 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
754 blk_insert_flush(rq);
755 } else {
756 spin_lock(&ctx->lock);
757 __blk_mq_insert_request(hctx, rq, at_head);
758 spin_unlock(&ctx->lock);
759 }
Jens Axboe320ae512013-10-24 09:20:05 +0100760
Jens Axboe320ae512013-10-24 09:20:05 +0100761 if (run_queue)
762 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600763
764 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100765}
766
767static void blk_mq_insert_requests(struct request_queue *q,
768 struct blk_mq_ctx *ctx,
769 struct list_head *list,
770 int depth,
771 bool from_schedule)
772
773{
774 struct blk_mq_hw_ctx *hctx;
775 struct blk_mq_ctx *current_ctx;
776
777 trace_block_unplug(q, depth, !from_schedule);
778
779 current_ctx = blk_mq_get_ctx(q);
780
781 if (!cpu_online(ctx->cpu))
782 ctx = current_ctx;
783 hctx = q->mq_ops->map_queue(q, ctx->cpu);
784
785 /*
786 * preemption doesn't flush plug list, so it's possible ctx->cpu is
787 * offline now
788 */
789 spin_lock(&ctx->lock);
790 while (!list_empty(list)) {
791 struct request *rq;
792
793 rq = list_first_entry(list, struct request, queuelist);
794 list_del_init(&rq->queuelist);
795 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800796 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100797 }
798 spin_unlock(&ctx->lock);
799
Jens Axboe320ae512013-10-24 09:20:05 +0100800 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600801 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100802}
803
804static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
805{
806 struct request *rqa = container_of(a, struct request, queuelist);
807 struct request *rqb = container_of(b, struct request, queuelist);
808
809 return !(rqa->mq_ctx < rqb->mq_ctx ||
810 (rqa->mq_ctx == rqb->mq_ctx &&
811 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
812}
813
814void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
815{
816 struct blk_mq_ctx *this_ctx;
817 struct request_queue *this_q;
818 struct request *rq;
819 LIST_HEAD(list);
820 LIST_HEAD(ctx_list);
821 unsigned int depth;
822
823 list_splice_init(&plug->mq_list, &list);
824
825 list_sort(NULL, &list, plug_ctx_cmp);
826
827 this_q = NULL;
828 this_ctx = NULL;
829 depth = 0;
830
831 while (!list_empty(&list)) {
832 rq = list_entry_rq(list.next);
833 list_del_init(&rq->queuelist);
834 BUG_ON(!rq->q);
835 if (rq->mq_ctx != this_ctx) {
836 if (this_ctx) {
837 blk_mq_insert_requests(this_q, this_ctx,
838 &ctx_list, depth,
839 from_schedule);
840 }
841
842 this_ctx = rq->mq_ctx;
843 this_q = rq->q;
844 depth = 0;
845 }
846
847 depth++;
848 list_add_tail(&rq->queuelist, &ctx_list);
849 }
850
851 /*
852 * If 'this_ctx' is set, we know we have entries to complete
853 * on 'ctx_list'. Do those.
854 */
855 if (this_ctx) {
856 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
857 from_schedule);
858 }
859}
860
861static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
862{
863 init_request_from_bio(rq, bio);
864 blk_account_io_start(rq, 1);
865}
866
867static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
868{
869 struct blk_mq_hw_ctx *hctx;
870 struct blk_mq_ctx *ctx;
871 const int is_sync = rw_is_sync(bio->bi_rw);
872 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
873 int rw = bio_data_dir(bio);
874 struct request *rq;
875 unsigned int use_plug, request_count = 0;
876
877 /*
878 * If we have multiple hardware queues, just go directly to
879 * one of those for sync IO.
880 */
881 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
882
883 blk_queue_bounce(q, &bio);
884
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -0700885 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
886 bio_endio(bio, -EIO);
887 return;
888 }
889
Jens Axboe320ae512013-10-24 09:20:05 +0100890 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
891 return;
892
893 if (blk_mq_queue_enter(q)) {
894 bio_endio(bio, -EIO);
895 return;
896 }
897
898 ctx = blk_mq_get_ctx(q);
899 hctx = q->mq_ops->map_queue(q, ctx->cpu);
900
Shaohua Li27fbf4e2014-02-19 20:20:21 +0800901 if (is_sync)
902 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +0100903 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700904 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100905 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -0700906 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100907 else {
908 blk_mq_put_ctx(ctx);
909 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700910 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
911 false);
Jens Axboe320ae512013-10-24 09:20:05 +0100912 ctx = rq->mq_ctx;
913 hctx = q->mq_ops->map_queue(q, ctx->cpu);
914 }
915
916 hctx->queued++;
917
918 if (unlikely(is_flush_fua)) {
919 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +0100920 blk_insert_flush(rq);
921 goto run_queue;
922 }
923
924 /*
925 * A task plug currently exists. Since this is completely lockless,
926 * utilize that to temporarily store requests until the task is
927 * either done or scheduled away.
928 */
929 if (use_plug) {
930 struct blk_plug *plug = current->plug;
931
932 if (plug) {
933 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -0600934 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +0100935 trace_block_plug(q);
936 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
937 blk_flush_plug_list(plug, false);
938 trace_block_plug(q);
939 }
940 list_add_tail(&rq->queuelist, &plug->mq_list);
941 blk_mq_put_ctx(ctx);
942 return;
943 }
944 }
945
946 spin_lock(&ctx->lock);
947
948 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
949 blk_mq_attempt_merge(q, ctx, bio))
950 __blk_mq_free_request(hctx, ctx, rq);
951 else {
952 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800953 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100954 }
955
956 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100957
958 /*
959 * For a SYNC request, send it to the hardware immediately. For an
960 * ASYNC request, just ensure that we run it later on. The latter
961 * allows for merging opportunities and more efficient dispatching.
962 */
963run_queue:
964 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -0600965 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100966}
967
968/*
969 * Default mapping to a software queue, since we use one per CPU.
970 */
971struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
972{
973 return q->queue_hw_ctx[q->mq_map[cpu]];
974}
975EXPORT_SYMBOL(blk_mq_map_queue);
976
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600977struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
Jens Axboe320ae512013-10-24 09:20:05 +0100978 unsigned int hctx_index)
979{
980 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600981 GFP_KERNEL | __GFP_ZERO, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +0100982}
983EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
984
985void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
986 unsigned int hctx_index)
987{
988 kfree(hctx);
989}
990EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
991
992static void blk_mq_hctx_notify(void *data, unsigned long action,
993 unsigned int cpu)
994{
995 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -0600996 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100997 struct blk_mq_ctx *ctx;
998 LIST_HEAD(tmp);
999
1000 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1001 return;
1002
1003 /*
1004 * Move ctx entries to new CPU, if this one is going away.
1005 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001006 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001007
1008 spin_lock(&ctx->lock);
1009 if (!list_empty(&ctx->rq_list)) {
1010 list_splice_init(&ctx->rq_list, &tmp);
1011 clear_bit(ctx->index_hw, hctx->ctx_map);
1012 }
1013 spin_unlock(&ctx->lock);
1014
1015 if (list_empty(&tmp))
1016 return;
1017
Jens Axboebccb5f72014-04-04 21:34:48 -06001018 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001019 spin_lock(&ctx->lock);
1020
1021 while (!list_empty(&tmp)) {
1022 struct request *rq;
1023
1024 rq = list_first_entry(&tmp, struct request, queuelist);
1025 rq->mq_ctx = ctx;
1026 list_move_tail(&rq->queuelist, &ctx->rq_list);
1027 }
1028
Jens Axboebccb5f72014-04-04 21:34:48 -06001029 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001030 blk_mq_hctx_mark_pending(hctx, ctx);
1031
1032 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001033
1034 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001035 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001036}
1037
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001038static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1039 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001040{
1041 struct page *page;
1042
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001043 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001044 int i;
1045
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001046 for (i = 0; i < tags->nr_tags; i++) {
1047 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001048 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001049 set->ops->exit_request(set->driver_data, tags->rqs[i],
1050 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001051 }
1052 }
1053
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001054 while (!list_empty(&tags->page_list)) {
1055 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001056 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001057 __free_pages(page, page->private);
1058 }
1059
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001060 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001061
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001062 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001063}
1064
1065static size_t order_to_size(unsigned int order)
1066{
1067 size_t ret = PAGE_SIZE;
1068
1069 while (order--)
1070 ret *= 2;
1071
1072 return ret;
1073}
1074
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001075static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1076 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001077{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001078 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001079 unsigned int i, j, entries_per_page, max_order = 4;
1080 size_t rq_size, left;
1081
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001082 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1083 set->numa_node);
1084 if (!tags)
1085 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001086
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001087 INIT_LIST_HEAD(&tags->page_list);
1088
1089 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1090 GFP_KERNEL, set->numa_node);
1091 if (!tags->rqs) {
1092 blk_mq_free_tags(tags);
1093 return NULL;
1094 }
Jens Axboe320ae512013-10-24 09:20:05 +01001095
1096 /*
1097 * rq_size is the size of the request plus driver payload, rounded
1098 * to the cacheline size
1099 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001100 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001101 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001102 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001103
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001104 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001105 int this_order = max_order;
1106 struct page *page;
1107 int to_do;
1108 void *p;
1109
1110 while (left < order_to_size(this_order - 1) && this_order)
1111 this_order--;
1112
1113 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001114 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1115 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001116 if (page)
1117 break;
1118 if (!this_order--)
1119 break;
1120 if (order_to_size(this_order) < rq_size)
1121 break;
1122 } while (1);
1123
1124 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001125 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001126
1127 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001128 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001129
1130 p = page_address(page);
1131 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001132 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001133 left -= to_do * rq_size;
1134 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001135 tags->rqs[i] = p;
1136 if (set->ops->init_request) {
1137 if (set->ops->init_request(set->driver_data,
1138 tags->rqs[i], hctx_idx, i,
1139 set->numa_node))
1140 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001141 }
1142
Jens Axboe320ae512013-10-24 09:20:05 +01001143 p += rq_size;
1144 i++;
1145 }
1146 }
1147
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001148 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001149
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001150fail:
1151 pr_warn("%s: failed to allocate requests\n", __func__);
1152 blk_mq_free_rq_map(set, tags, hctx_idx);
1153 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001154}
1155
1156static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001157 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001158{
1159 struct blk_mq_hw_ctx *hctx;
1160 unsigned int i, j;
1161
1162 /*
1163 * Initialize hardware queues
1164 */
1165 queue_for_each_hw_ctx(q, hctx, i) {
1166 unsigned int num_maps;
1167 int node;
1168
1169 node = hctx->numa_node;
1170 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001171 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001172
1173 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1174 spin_lock_init(&hctx->lock);
1175 INIT_LIST_HEAD(&hctx->dispatch);
1176 hctx->queue = q;
1177 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001178 hctx->flags = set->flags;
1179 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001180
1181 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1182 blk_mq_hctx_notify, hctx);
1183 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1184
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001185 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001186
1187 /*
1188 * Allocate space for all possible cpus to avoid allocation in
1189 * runtime
1190 */
1191 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1192 GFP_KERNEL, node);
1193 if (!hctx->ctxs)
1194 break;
1195
1196 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1197 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1198 GFP_KERNEL, node);
1199 if (!hctx->ctx_map)
1200 break;
1201
1202 hctx->nr_ctx_map = num_maps;
1203 hctx->nr_ctx = 0;
1204
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001205 if (set->ops->init_hctx &&
1206 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001207 break;
1208 }
1209
1210 if (i == q->nr_hw_queues)
1211 return 0;
1212
1213 /*
1214 * Init failed
1215 */
1216 queue_for_each_hw_ctx(q, hctx, j) {
1217 if (i == j)
1218 break;
1219
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001220 if (set->ops->exit_hctx)
1221 set->ops->exit_hctx(hctx, j);
Jens Axboe320ae512013-10-24 09:20:05 +01001222
1223 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Jens Axboe320ae512013-10-24 09:20:05 +01001224 kfree(hctx->ctxs);
1225 }
1226
1227 return 1;
1228}
1229
1230static void blk_mq_init_cpu_queues(struct request_queue *q,
1231 unsigned int nr_hw_queues)
1232{
1233 unsigned int i;
1234
1235 for_each_possible_cpu(i) {
1236 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1237 struct blk_mq_hw_ctx *hctx;
1238
1239 memset(__ctx, 0, sizeof(*__ctx));
1240 __ctx->cpu = i;
1241 spin_lock_init(&__ctx->lock);
1242 INIT_LIST_HEAD(&__ctx->rq_list);
1243 __ctx->queue = q;
1244
1245 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001246 if (!cpu_online(i))
1247 continue;
1248
Jens Axboee4043dc2014-04-09 10:18:23 -06001249 hctx = q->mq_ops->map_queue(q, i);
1250 cpumask_set_cpu(i, hctx->cpumask);
1251 hctx->nr_ctx++;
1252
Jens Axboe320ae512013-10-24 09:20:05 +01001253 /*
1254 * Set local node, IFF we have more than one hw queue. If
1255 * not, we remain on the home node of the device
1256 */
1257 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1258 hctx->numa_node = cpu_to_node(i);
1259 }
1260}
1261
1262static void blk_mq_map_swqueue(struct request_queue *q)
1263{
1264 unsigned int i;
1265 struct blk_mq_hw_ctx *hctx;
1266 struct blk_mq_ctx *ctx;
1267
1268 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001269 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001270 hctx->nr_ctx = 0;
1271 }
1272
1273 /*
1274 * Map software to hardware queues
1275 */
1276 queue_for_each_ctx(q, ctx, i) {
1277 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001278 if (!cpu_online(i))
1279 continue;
1280
Jens Axboe320ae512013-10-24 09:20:05 +01001281 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001282 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001283 ctx->index_hw = hctx->nr_ctx;
1284 hctx->ctxs[hctx->nr_ctx++] = ctx;
1285 }
1286}
1287
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001288struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001289{
1290 struct blk_mq_hw_ctx **hctxs;
1291 struct blk_mq_ctx *ctx;
1292 struct request_queue *q;
1293 int i;
1294
Jens Axboe320ae512013-10-24 09:20:05 +01001295 ctx = alloc_percpu(struct blk_mq_ctx);
1296 if (!ctx)
1297 return ERR_PTR(-ENOMEM);
1298
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001299 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1300 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001301
1302 if (!hctxs)
1303 goto err_percpu;
1304
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001305 for (i = 0; i < set->nr_hw_queues; i++) {
1306 hctxs[i] = set->ops->alloc_hctx(set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001307 if (!hctxs[i])
1308 goto err_hctxs;
1309
Jens Axboee4043dc2014-04-09 10:18:23 -06001310 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1311 goto err_hctxs;
1312
Jens Axboe320ae512013-10-24 09:20:05 +01001313 hctxs[i]->numa_node = NUMA_NO_NODE;
1314 hctxs[i]->queue_num = i;
1315 }
1316
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001317 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001318 if (!q)
1319 goto err_hctxs;
1320
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001321 q->mq_map = blk_mq_make_queue_map(set);
Jens Axboe320ae512013-10-24 09:20:05 +01001322 if (!q->mq_map)
1323 goto err_map;
1324
1325 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1326 blk_queue_rq_timeout(q, 30000);
1327
1328 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001329 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboe320ae512013-10-24 09:20:05 +01001330
1331 q->queue_ctx = ctx;
1332 q->queue_hw_ctx = hctxs;
1333
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001334 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001335 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001336
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001337 q->sg_reserved_size = INT_MAX;
1338
Jens Axboe320ae512013-10-24 09:20:05 +01001339 blk_queue_make_request(q, blk_mq_make_request);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001340 blk_queue_rq_timed_out(q, set->ops->timeout);
1341 if (set->timeout)
1342 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001343
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001344 if (set->ops->complete)
1345 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001346
Jens Axboe320ae512013-10-24 09:20:05 +01001347 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001348 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001349
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001350 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1351 set->cmd_size, cache_line_size()),
1352 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001353 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001354 goto err_hw;
1355
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001356 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001357 goto err_flush_rq;
1358
Jens Axboe320ae512013-10-24 09:20:05 +01001359 blk_mq_map_swqueue(q);
1360
1361 mutex_lock(&all_q_mutex);
1362 list_add_tail(&q->all_q_node, &all_q_list);
1363 mutex_unlock(&all_q_mutex);
1364
1365 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001366
1367err_flush_rq:
1368 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001369err_hw:
1370 kfree(q->mq_map);
1371err_map:
1372 blk_cleanup_queue(q);
1373err_hctxs:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001374 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001375 if (!hctxs[i])
1376 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001377 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001378 set->ops->free_hctx(hctxs[i], i);
Jens Axboe320ae512013-10-24 09:20:05 +01001379 }
1380 kfree(hctxs);
1381err_percpu:
1382 free_percpu(ctx);
1383 return ERR_PTR(-ENOMEM);
1384}
1385EXPORT_SYMBOL(blk_mq_init_queue);
1386
1387void blk_mq_free_queue(struct request_queue *q)
1388{
1389 struct blk_mq_hw_ctx *hctx;
1390 int i;
1391
1392 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001393 kfree(hctx->ctx_map);
1394 kfree(hctx->ctxs);
Jens Axboe320ae512013-10-24 09:20:05 +01001395 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1396 if (q->mq_ops->exit_hctx)
1397 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001398 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001399 q->mq_ops->free_hctx(hctx, i);
1400 }
1401
1402 free_percpu(q->queue_ctx);
1403 kfree(q->queue_hw_ctx);
1404 kfree(q->mq_map);
1405
1406 q->queue_ctx = NULL;
1407 q->queue_hw_ctx = NULL;
1408 q->mq_map = NULL;
1409
1410 mutex_lock(&all_q_mutex);
1411 list_del_init(&q->all_q_node);
1412 mutex_unlock(&all_q_mutex);
1413}
Jens Axboe320ae512013-10-24 09:20:05 +01001414
1415/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001416static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001417{
1418 blk_mq_freeze_queue(q);
1419
1420 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1421
1422 /*
1423 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1424 * we should change hctx numa_node according to new topology (this
1425 * involves free and re-allocate memory, worthy doing?)
1426 */
1427
1428 blk_mq_map_swqueue(q);
1429
1430 blk_mq_unfreeze_queue(q);
1431}
1432
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001433static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1434 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001435{
1436 struct request_queue *q;
1437
1438 /*
1439 * Before new mapping is established, hotadded cpu might already start
1440 * handling requests. This doesn't break anything as we map offline
1441 * CPUs to first hardware queue. We will re-init queue below to get
1442 * optimal settings.
1443 */
1444 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1445 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1446 return NOTIFY_OK;
1447
1448 mutex_lock(&all_q_mutex);
1449 list_for_each_entry(q, &all_q_list, all_q_node)
1450 blk_mq_queue_reinit(q);
1451 mutex_unlock(&all_q_mutex);
1452 return NOTIFY_OK;
1453}
1454
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001455int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1456{
1457 int i;
1458
1459 if (!set->nr_hw_queues)
1460 return -EINVAL;
1461 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1462 return -EINVAL;
1463 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1464 return -EINVAL;
1465
1466 if (!set->nr_hw_queues ||
1467 !set->ops->queue_rq || !set->ops->map_queue ||
1468 !set->ops->alloc_hctx || !set->ops->free_hctx)
1469 return -EINVAL;
1470
1471
1472 set->tags = kmalloc_node(set->nr_hw_queues * sizeof(struct blk_mq_tags),
1473 GFP_KERNEL, set->numa_node);
1474 if (!set->tags)
1475 goto out;
1476
1477 for (i = 0; i < set->nr_hw_queues; i++) {
1478 set->tags[i] = blk_mq_init_rq_map(set, i);
1479 if (!set->tags[i])
1480 goto out_unwind;
1481 }
1482
1483 return 0;
1484
1485out_unwind:
1486 while (--i >= 0)
1487 blk_mq_free_rq_map(set, set->tags[i], i);
1488out:
1489 return -ENOMEM;
1490}
1491EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1492
1493void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1494{
1495 int i;
1496
1497 for (i = 0; i < set->nr_hw_queues; i++)
1498 blk_mq_free_rq_map(set, set->tags[i], i);
1499}
1500EXPORT_SYMBOL(blk_mq_free_tag_set);
1501
Jens Axboe676141e2014-03-20 13:29:18 -06001502void blk_mq_disable_hotplug(void)
1503{
1504 mutex_lock(&all_q_mutex);
1505}
1506
1507void blk_mq_enable_hotplug(void)
1508{
1509 mutex_unlock(&all_q_mutex);
1510}
1511
Jens Axboe320ae512013-10-24 09:20:05 +01001512static int __init blk_mq_init(void)
1513{
Jens Axboe320ae512013-10-24 09:20:05 +01001514 blk_mq_cpu_init();
1515
1516 /* Must be called after percpu_counter_hotcpu_callback() */
1517 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1518
1519 return 0;
1520}
1521subsys_initcall(blk_mq_init);