summaryrefslogtreecommitdiff
path: root/block/blk-mq.c
blob: 22949ab087ec90a3882d42f8bc34873a2442b218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/backing-dev.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/smp.h>
#include <linux/llist.h>
#include <linux/cpu.h>

#define CREATE_TRACE_POINTS
#include <trace/events/block.h>

#include <linux/blk-mq.h>
#include "blk.h"

static DEFINE_PER_CPU(struct llist_head, ipi_lists);

/*
 * Check if any of the ctx's have pending work in this hardware queue
 */
static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
{
	unsigned int i;

	for (i = 0; i < hctx->nr_ctx_map; i++)
		if (hctx->ctx_map[i])
			return true;

	return false;
}

/*
 * Mark this ctx as having pending work in this hardware queue
 */
static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
				     struct blk_mq_ctx *ctx)
{
	if (!test_bit(ctx->index, hctx->ctx_map))
		set_bit(ctx->index, hctx->ctx_map);
}

static struct request *__blk_mq_alloc_rq_nowait(struct blk_mq_hw_ctx *hctx)
{
	struct request *rq;
	unsigned int tag;

	do {
		tag = find_first_zero_bit(hctx->rq_map, hctx->queue_depth);
		if (tag >= hctx->queue_depth)
			return NULL;
	} while (test_and_set_bit_lock(tag, hctx->rq_map));

	rq = &hctx->rqs[tag];
	rq->tag = tag;

	return rq;
}

static struct request *__blk_mq_alloc_request(struct request_queue *q,
					      struct blk_mq_ctx *ctx,
					      unsigned int rw_flags, gfp_t gfp,
					      bool has_lock)
{
	struct blk_mq_hw_ctx *hctx;
	struct request *rq = NULL;
	DEFINE_WAIT(wait);

	hctx = q->mq_ops->map_queue(q, ctx);

	rq = __blk_mq_alloc_rq_nowait(hctx);
	if (rq) {
got_rq:
		rq->mq_ctx = ctx;
		rq->cmd_flags = rw_flags;
		ctx->rq_dispatched[rw_is_sync(rw_flags)]++;

		return rq;
	}

	if (!(gfp & __GFP_WAIT))
		return NULL;

	if (has_lock)
		spin_unlock(&ctx->lock);

	do {
		prepare_to_wait(&hctx->rq_wait, &wait, TASK_UNINTERRUPTIBLE);
		rq = __blk_mq_alloc_rq_nowait(hctx);
		if (rq)
			break;

		trace_block_sleeprq(q, NULL, rw_flags & 1);
		io_schedule();
	} while (!rq);

	if (has_lock)
		spin_lock(&ctx->lock);

	finish_wait(&hctx->rq_wait, &wait);
	goto got_rq;
}

struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
{
	struct blk_mq_ctx *ctx;
	struct request *rq;

	ctx = per_cpu_ptr(q->queue_ctx, get_cpu());
	rq = __blk_mq_alloc_request(q, ctx, rw, gfp, false);
	put_cpu();
	return rq;
}

static void blk_mq_free_request(struct request *rq)
{
	struct blk_mq_ctx *ctx = rq->mq_ctx;
	struct blk_mq_hw_ctx *hctx;
	struct request_queue *q = rq->q;
	const int tag = rq->tag;

	ctx->rq_completed[rq_is_sync(rq)]++;

	hctx = q->mq_ops->map_queue(q, ctx);
	blk_rq_init(hctx->queue, rq);
	clear_bit_unlock(tag, hctx->rq_map);

	if (waitqueue_active(&hctx->rq_wait))
		wake_up(&hctx->rq_wait);
}

static void __blk_mq_end_io(struct request *rq, int error)
{
	struct bio *bio = rq->bio;

	if (blk_mark_rq_complete(rq))
		return;

	trace_block_rq_complete(rq->q, rq);

	while (bio) {
		struct bio *next = bio->bi_next;

		bio->bi_next = NULL;
		bio_endio(bio, error);
		bio = next;
	}

	blk_mq_free_request(rq);
}

/*
 * Called with interrupts disabled.
 */
static void ipi_end_io(void *data)
{
	struct llist_head *list = &per_cpu(ipi_lists, smp_processor_id());
	struct llist_node *entry;
	struct request *rq;

	while ((entry = llist_del_first(list)) != NULL) {
		rq = llist_entry(entry, struct request, ll_list);

		__blk_mq_end_io(rq, rq->errors);
	}
}

/*
 * End IO on this request on a multiqueue enabled driver. We'll either do
 * it directly inline, or punt to a local IPI handler on the matching
 * remote CPU.
 */
void blk_mq_end_io(struct blk_mq_hw_ctx *hctx, struct request *rq, int error)
{
	struct blk_mq_ctx *ctx = rq->mq_ctx;
	int cpu;

	if (!ctx->ipi_redirect)
		return __blk_mq_end_io(rq, error);

	cpu = get_cpu();
	if (cpu == ctx->index)
		__blk_mq_end_io(rq, error);
	else {
		struct call_single_data *data = &rq->csd;

		rq->errors = error;
		rq->ll_list.next = NULL;

		if (llist_add(&rq->ll_list, &per_cpu(ipi_lists, cpu))) {
			data->func = ipi_end_io;
			data->flags = 0;
			__smp_call_function_single(cpu, data, 0);
		}
	}

	put_cpu();
}
EXPORT_SYMBOL(blk_mq_end_io);

/*
 * FIXME: we currently don't add requests to the ctx timeout list
 */
static void blk_mq_start_request(struct request *rq)
{
	struct request_queue *q = rq->q;

	trace_block_rq_issue(q, rq);

	rq->deadline = jiffies + q->rq_timeout;
	set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
}

static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
					unsigned long *next,
					unsigned int *next_set)
{
	unsigned int i;

	for_each_set_bit(i, hctx->rq_map, hctx->queue_depth) {
		struct request *rq = &hctx->rqs[i];

		if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
			continue;

		blk_rq_check_expired(rq, next, next_set);
	}
}

static void blk_mq_rq_timer(unsigned long data)
{
	struct request_queue *q = (struct request_queue *) data;
	struct blk_mq_hw_ctx *hctx;
	unsigned long next = 0;
	int i, next_set = 0;

	queue_for_each_hw_ctx(q, hctx, i)
		blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);

	if (next_set)
		mod_timer(&q->timeout, round_jiffies_up(next));
}

/*
 * Reverse check our software queue for entries that we could potentially
 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
 * too much time checking for merges.
 */
static bool blk_mq_attempt_merge(struct request_queue *q,
				 struct blk_mq_ctx *ctx, struct bio *bio)
{
	struct request *rq;
	int checked = 8;

	list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
		int el_ret;

		if (!checked--)
			break;

		if (!blk_rq_merge_ok(rq, bio))
			continue;

		el_ret = blk_try_merge(rq, bio);
		if (el_ret == ELEVATOR_BACK_MERGE) {
			if (bio_attempt_back_merge(q, rq, bio)) {
				ctx->rq_merged++;
				return true;
			}
			break;
		} else if (el_ret == ELEVATOR_FRONT_MERGE) {
			if (bio_attempt_front_merge(q, rq, bio)) {
				ctx->rq_merged++;
				return true;
			}
			break;
		}
	}

	return false;
}

static void blk_mq_add_timer(struct request *rq)
{
	__blk_add_timer(rq, NULL);
}

/*
 * Run this hardware queue, pulling any software queues mapped to it in.
 * Note that this function currently has various problems around ordering
 * of IO. In particular, we'd like FIFO behaviour on handling existing
 * items on the hctx->dispatch list. Ignore that for now.
 */
void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
{
	struct request_queue *q = hctx->queue;
	struct blk_mq_ctx *ctx;
	struct request *rq;
	struct llist_node *first, *last = NULL;
	unsigned long flags = 0;
	LLIST_HEAD(rq_list);
	LIST_HEAD(tmp);
	int bit, queued;

	hctx->run++;

	/*
	 * Touch any software queue that has pending entries.
	 */
	for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
		clear_bit(bit, hctx->ctx_map);
		ctx = hctx->ctxs[bit];
		BUG_ON(bit != ctx->index);

		spin_lock(&ctx->lock);
		list_splice_tail_init(&ctx->rq_list, &tmp);
		spin_unlock(&ctx->lock);

		/*
		 * Reverse-add the entries to a lockless list, using the
		 * non-cmpxchg variant for adding the entries. It's a local
		 * list on the stack, so we need not care about others
		 * fiddling with it.
		 */
		while (!list_empty(&tmp)) {
			rq = list_entry(tmp.prev, struct request, queuelist);
			list_del(&rq->queuelist);
			rq->ll_list.next = NULL;
			__llist_add(&rq->ll_list, &rq_list);

			if (!last)
				last = &rq->ll_list;
		}
	}

	/*
	 * If we found entries above, batch add them to the dispatch list.
	 */
	if (!llist_empty(&rq_list))
		llist_add_batch(rq_list.first, last, &hctx->dispatch);

	/*
	 * Delete and return all entries from our dispatch list
	 */
	queued = 0;
	first = llist_del_all(&hctx->dispatch);

	/*
	 * Now process all the entries, sending them to the driver.
	 */
	if (hctx->flags & BLK_MQ_F_SHOULD_LOCK)
		spin_lock_irqsave(hctx->lock, flags);

	while (first) {
		struct llist_node *entry;
		int ret;

		entry = first;
		first = first->next;

		rq = llist_entry(entry, struct request, ll_list);
		blk_mq_start_request(rq);

		if (!first)
			rq->cmd_flags |= REQ_END;

		ret = q->mq_ops->queue_rq(hctx, rq);
		switch (ret) {
		case BLK_MQ_RQ_QUEUE_OK:
			queued++;
			continue;
		case BLK_MQ_RQ_QUEUE_BUSY:
			break;
		default:
			pr_err("blk-mq: bad return on queue: %d\n", ret);
			rq->errors = -EIO;
		case BLK_MQ_RQ_QUEUE_ERROR:
			blk_mq_end_io(hctx, rq, rq->errors);
			break;
		}
	}

	if (hctx->flags & BLK_MQ_F_SHOULD_LOCK)
		spin_unlock_irqrestore(hctx->lock, flags);

	if (!queued)
		hctx->dispatched[0]++;
	else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
		hctx->dispatched[ilog2(queued) + 1]++;

	/*
	 * Any items that need requeuing? Find last entry, batch re-add.
	 */
	if (first) {
		last = NULL;
		while (first->next)
			last = first->next;

		if (last)
			llist_add_batch(first, last, &hctx->dispatch);
		else
			llist_add(first, &hctx->dispatch);
	}
}

void blk_mq_run_queues(struct request_queue *q, bool async)
{
	struct blk_mq_hw_ctx *hctx;
	int i;

	queue_for_each_hw_ctx(q, hctx, i) {
		if (!blk_mq_hctx_has_pending(hctx) &&
		    llist_empty(&hctx->dispatch))
			continue;

		if (!async)
			blk_mq_run_hw_queue(hctx);
		else
			kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
	}
}

static void blk_mq_work_fn(struct work_struct *work)
{
	struct blk_mq_hw_ctx *hctx;

	hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
	blk_mq_run_hw_queue(hctx);
}

void blk_mq_flush_plug(struct request_queue *q, bool from_schedule)
{
	blk_mq_run_queues(q, from_schedule);
}

static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
				    struct blk_mq_ctx *ctx,
				    struct request *rq)
{
	list_add_tail(&rq->queuelist, &ctx->rq_list);
	blk_mq_hctx_mark_pending(hctx, ctx);

	/*
	 * We do this early, to ensure we are on the right CPU.
	 */
	blk_mq_add_timer(rq);
}

void blk_mq_insert_request(struct request_queue *q, struct request *rq)
{
	struct blk_mq_hw_ctx *hctx;
	struct blk_mq_ctx *ctx;

	ctx = rq->mq_ctx;
	hctx = q->mq_ops->map_queue(q, ctx);

	spin_lock(&ctx->lock);
	__blk_mq_insert_request(hctx, ctx, rq);
	spin_unlock(&ctx->lock);
}

void blk_mq_insert_requests(struct request_queue *q, struct list_head *list)
{
	struct blk_mq_hw_ctx *hctx;
	struct blk_mq_ctx *ctx;

	if (list_empty(list))
		return;

	ctx = per_cpu_ptr(q->queue_ctx, smp_processor_id());
	hctx = q->mq_ops->map_queue(q, ctx);
	
	spin_lock(&ctx->lock);
	while (!list_empty(list)) {
		struct request *rq;

		rq = list_entry(list->next, struct request, queuelist);
		list_del_init(&rq->queuelist);
		__blk_mq_insert_request(hctx, ctx, rq);
	}
	spin_unlock(&ctx->lock);

	blk_mq_run_hw_queue(hctx);
}

static struct request *blk_mq_bio_to_request(struct request_queue *q,
					     struct blk_mq_ctx *ctx,
					     struct bio *bio, bool has_lock)
{
	unsigned int rw_flags;
	struct request *rq;

	rw_flags = bio_data_dir(bio);
	if (rw_is_sync(bio->bi_rw))
		rw_flags |= REQ_SYNC;

	trace_block_getrq(q, bio, rw_flags & 1);

	rq = __blk_mq_alloc_request(q, ctx, rw_flags, GFP_ATOMIC | __GFP_WAIT, has_lock);
	if (rq)
		init_request_from_bio(rq, bio);

	return rq;
}

static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
{
	struct blk_mq_hw_ctx *hctx;
	struct blk_mq_ctx *ctx;
	int is_sync = rw_is_sync(bio->bi_rw);
	struct request *rq;
	unsigned int request_count = 0;
	struct blk_plug *plug;

	blk_queue_bounce(q, &bio);

	if (blk_attempt_plug_merge(q, bio, &request_count))
		return;

	ctx = per_cpu_ptr(q->queue_ctx, raw_smp_processor_id());
	hctx = q->mq_ops->map_queue(q, ctx);

	hctx->queued++;

	plug = current->plug;
	if (plug) {
		rq = blk_mq_bio_to_request(q, ctx, bio, false);
		if (list_empty(&plug->list))
			trace_block_plug(q);
		list_add_tail(&rq->queuelist, &plug->list);
		return;
	}

	spin_lock(&ctx->lock);

	if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) ||
	    !blk_mq_attempt_merge(q, ctx, bio)) {
		rq = blk_mq_bio_to_request(q, ctx, bio, true);
		__blk_mq_insert_request(hctx, ctx, rq);
	}

	spin_unlock(&ctx->lock);

	if (is_sync)
		blk_mq_run_hw_queue(hctx);
	else
		kblockd_schedule_delayed_work(q, &hctx->delayed_work, 2);
}

/*
 * Default mapping to a software queue, since we use one per CPU
 */
struct blk_mq_hw_ctx *blk_mq_map_single_queue(struct request_queue *q,
						 struct blk_mq_ctx *ctx)
{
	return &q->queue_hw_ctx[0];
}
EXPORT_SYMBOL(blk_mq_map_single_queue);

static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx)
{
	unsigned int num_maps;
	int i;

	hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request),
					GFP_KERNEL, hctx->numa_node);
	if (!hctx->rqs)
		return -ENOMEM;

	num_maps = ALIGN(hctx->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
	hctx->rq_map = kzalloc_node(num_maps * sizeof(unsigned long),
					GFP_KERNEL, hctx->numa_node);
	if (!hctx->rq_map) {
		kfree(hctx->rqs);
		return -ENOMEM;
	}

	for (i = 0; i < hctx->queue_depth; i++)
		blk_rq_init(hctx->queue, &hctx->rqs[i]);

	init_waitqueue_head(&hctx->rq_wait);
	return 0;
}

struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
					spinlock_t *lock)
{
	struct blk_mq_hw_ctx *hctx;
	struct blk_mq_ctx *ctx;
	struct request_queue *q;
	int i;

	if (!reg->nr_hw_queues || !reg->ops->queue_rq || !reg->ops->map_queue)
		return ERR_PTR(-EINVAL);

	if (!reg->queue_depth)
		reg->queue_depth = BLK_MQ_MAX_DEPTH;
	else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
		printk(KERN_ERR "blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
		reg->queue_depth = BLK_MQ_MAX_DEPTH;
	}

	ctx = alloc_percpu(struct blk_mq_ctx);
	if (!ctx)
		return ERR_PTR(-ENOMEM);

	hctx = kmalloc_node(reg->nr_hw_queues * sizeof(*hctx), GFP_KERNEL,
				reg->numa_node);
	if (!hctx) {
		free_percpu(ctx);
		return ERR_PTR(-ENOMEM);
	}
	memset(hctx, 0, reg->nr_hw_queues * sizeof(*hctx));

	q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
	if (!q) {
		free_percpu(ctx);
		return ERR_PTR(-ENOMEM);
	}

	setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
	blk_queue_rq_timeout(q, 30000);

	q->queue_ctx = ctx;
	q->queue_hw_ctx = hctx;

	blk_queue_make_request(q, blk_mq_make_request);
	blk_queue_rq_timed_out(q, reg->ops->timeout);
	blk_queue_rq_timeout(q, reg->timeout);
	q->mq_ops = reg->ops;

	for_each_possible_cpu(i) {
		struct blk_mq_ctx *__ctx = per_cpu_ptr(ctx, i);

		hctx = q->mq_ops->map_queue(q, ctx);
		hctx->nr_ctx++;

		memset(__ctx, 0, sizeof(*__ctx));
		spin_lock_init(&__ctx->lock);
		__ctx->index = i;
		INIT_LIST_HEAD(&__ctx->rq_list);
	}

	q->nr_queues = nr_cpu_ids;
	q->nr_hw_queues = reg->nr_hw_queues;

	/*
	 * Initialize hardware queues
	 */
	queue_for_each_hw_ctx(q, hctx, i) {
		unsigned int num_maps;

		INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
		spin_lock_init(&hctx->__lock);
		init_llist_head(&hctx->dispatch);
		hctx->queue = q;
		hctx->flags = reg->flags;
		hctx->queue_depth = reg->queue_depth;
		hctx->numa_node = reg->numa_node;

		if (!lock)
			hctx->lock = &hctx->__lock;
		else
			hctx->lock = lock;

		/* FIXME: alloc failure handling */
		blk_mq_init_rq_map(hctx);

		/* FIXME: alloc failure handling */
		hctx->ctxs = kmalloc_node(hctx->nr_ctx *
				sizeof(void *), GFP_KERNEL, reg->numa_node);

		num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
		hctx->ctx_map = kmalloc_node(num_maps * sizeof(unsigned long),
						GFP_KERNEL, reg->numa_node);
		hctx->nr_ctx_map = num_maps;

		hctx->nr_ctx = 0;
	}

	/*
	 * Map software to hardware queues
	 */
	queue_for_each_ctx(q, ctx, i) {
		hctx = q->mq_ops->map_queue(q, ctx);
		hctx->ctxs[hctx->nr_ctx++] = ctx;
	}

	return q;
}
EXPORT_SYMBOL(blk_mq_init_queue);

void blk_mq_free_queue(struct request_queue *q)
{
	struct blk_mq_hw_ctx *hctx;
	int i;

	queue_for_each_hw_ctx(q, hctx, i) {
		cancel_delayed_work_sync(&hctx->delayed_work);
		kfree(hctx->ctx_map);
		kfree(hctx->ctxs);
		kfree(hctx->rqs);
		kfree(hctx->rq_map);
	}

	free_percpu(q->queue_ctx);
	kfree(q->queue_hw_ctx);

	q->queue_ctx = NULL;
	q->queue_hw_ctx = NULL;
}
EXPORT_SYMBOL(blk_mq_free_queue);

static int __cpuinit blk_mq_cpu_notify(struct notifier_block *self,
				       unsigned long action, void *hcpu)
{
	/*
	 * If the CPU goes away, ensure that we run any pending completions.
	 */
	if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
		int cpu = (unsigned long) hcpu;
		struct llist_node *node;
		struct request *rq;

		local_irq_disable();

		node = llist_del_first(&per_cpu(ipi_lists, cpu));
		llist_for_each_entry(rq, node, ll_list)
			__blk_mq_end_io(rq, rq->errors);

		local_irq_enable();
	}

	return NOTIFY_OK;
}

static struct notifier_block __cpuinitdata blk_mq_cpu_notifier = {
	.notifier_call	= blk_mq_cpu_notify,
};

int __init blk_mq_init(void)
{
	unsigned int i;

	for_each_possible_cpu(i)
		init_llist_head(&per_cpu(ipi_lists, i));

	register_hotcpu_notifier(&blk_mq_cpu_notifier);
	return 0;
}