blk-mq: add blk_mq_delay_queue
[linux-2.6-block.git] / block / blk-mq.c
CommitLineData
320ae51f
JA
1#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
320ae51f
JA
30static 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
081241e5
CH
76static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
320ae51f
JA
78{
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) {
24d2f903 84 rq = hctx->tags->rqs[tag];
ed44832d 85 blk_rq_init(hctx->queue, rq);
320ae51f
JA
86 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,
43a5e4e2
ML
108 !blk_queue_bypass(q) || blk_queue_dying(q),
109 *q->queue_lock);
320ae51f 110 /* inc usage with lock hold to avoid freeze_queue runs here */
43a5e4e2 111 if (!ret && !blk_queue_dying(q))
320ae51f 112 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
43a5e4e2
ML
113 else if (blk_queue_dying(q))
114 ret = -ENODEV;
320ae51f
JA
115 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
43a5e4e2
ML
125static 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
320ae51f
JA
141/*
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
43a5e4e2
ML
154 if (drain)
155 __blk_mq_drain_queue(q);
156}
320ae51f 157
43a5e4e2
ML
158void blk_mq_drain_queue(struct request_queue *q)
159{
160 __blk_mq_drain_queue(q);
320ae51f
JA
161}
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
94eddfbe
JA
184static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
185 struct request *rq, unsigned int rw_flags)
320ae51f 186{
94eddfbe
JA
187 if (blk_queue_io_stat(q))
188 rw_flags |= REQ_IO_STAT;
189
320ae51f
JA
190 rq->mq_ctx = ctx;
191 rq->cmd_flags = rw_flags;
0fec08b4
ML
192 rq->start_time = jiffies;
193 set_start_time_ns(rq);
320ae51f
JA
194 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195}
196
320ae51f
JA
197static 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
18741986 207 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
320ae51f 208 if (rq) {
94eddfbe 209 blk_mq_rq_ctx_init(q, ctx, rq, rw);
320ae51f 210 break;
959a35f1 211 }
320ae51f 212
e4043dcf
JA
213 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);
959a35f1 218 break;
e4043dcf 219 }
959a35f1 220
320ae51f
JA
221 blk_mq_wait_for_tags(hctx->tags);
222 } while (1);
223
224 return rq;
225}
226
18741986 227struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
320ae51f
JA
228{
229 struct request *rq;
230
231 if (blk_mq_queue_enter(q))
232 return NULL;
233
18741986 234 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
959a35f1
JM
235 if (rq)
236 blk_mq_put_ctx(rq->mq_ctx);
320ae51f
JA
237 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);
959a35f1
JM
249 if (rq)
250 blk_mq_put_ctx(rq->mq_ctx);
320ae51f
JA
251 return rq;
252}
253EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
254
320ae51f
JA
255static 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
320ae51f 261 blk_mq_put_tag(hctx->tags, tag);
320ae51f
JA
262 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
8727af4b
CH
277/*
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
63151a44 297inline void __blk_mq_end_io(struct request *rq, int error)
320ae51f 298{
0d11e6ac
ML
299 blk_account_io_done(rq);
300
91b63639 301 if (rq->end_io) {
320ae51f 302 rq->end_io(rq, error);
91b63639
CH
303 } else {
304 if (unlikely(blk_bidi_rq(rq)))
305 blk_mq_free_request(rq->next_rq);
320ae51f 306 blk_mq_free_request(rq);
91b63639 307 }
320ae51f 308}
63151a44
CH
309EXPORT_SYMBOL(__blk_mq_end_io);
310
311void blk_mq_end_io(struct request *rq, int error)
312{
313 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
314 BUG();
315 __blk_mq_end_io(rq, error);
316}
317EXPORT_SYMBOL(blk_mq_end_io);
320ae51f 318
30a91cb4 319static void __blk_mq_complete_request_remote(void *data)
320ae51f 320{
3d6efbf6 321 struct request *rq = data;
320ae51f 322
30a91cb4 323 rq->q->softirq_done_fn(rq);
320ae51f 324}
320ae51f 325
30a91cb4 326void __blk_mq_complete_request(struct request *rq)
320ae51f
JA
327{
328 struct blk_mq_ctx *ctx = rq->mq_ctx;
329 int cpu;
330
30a91cb4
CH
331 if (!ctx->ipi_redirect) {
332 rq->q->softirq_done_fn(rq);
333 return;
334 }
320ae51f
JA
335
336 cpu = get_cpu();
3d6efbf6 337 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
30a91cb4 338 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
339 rq->csd.info = rq;
340 rq->csd.flags = 0;
c46fff2a 341 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 342 } else {
30a91cb4 343 rq->q->softirq_done_fn(rq);
3d6efbf6 344 }
320ae51f
JA
345 put_cpu();
346}
30a91cb4
CH
347
348/**
349 * blk_mq_complete_request - end I/O on a request
350 * @rq: the request being processed
351 *
352 * Description:
353 * Ends all I/O on a request. It does not handle partial completions.
354 * The actual completion happens out-of-order, through a IPI handler.
355 **/
356void blk_mq_complete_request(struct request *rq)
357{
358 if (unlikely(blk_should_fake_timeout(rq->q)))
359 return;
360 if (!blk_mark_rq_complete(rq))
361 __blk_mq_complete_request(rq);
362}
363EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 364
49f5baa5 365static void blk_mq_start_request(struct request *rq, bool last)
320ae51f
JA
366{
367 struct request_queue *q = rq->q;
368
369 trace_block_rq_issue(q, rq);
370
742ee69b 371 rq->resid_len = blk_rq_bytes(rq);
91b63639
CH
372 if (unlikely(blk_bidi_rq(rq)))
373 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
742ee69b 374
320ae51f
JA
375 /*
376 * Just mark start time and set the started bit. Due to memory
377 * ordering, we know we'll see the correct deadline as long as
378 * REQ_ATOMIC_STARTED is seen.
379 */
380 rq->deadline = jiffies + q->rq_timeout;
381 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
382
383 if (q->dma_drain_size && blk_rq_bytes(rq)) {
384 /*
385 * Make sure space for the drain appears. We know we can do
386 * this because max_hw_segments has been adjusted to be one
387 * fewer than the device can handle.
388 */
389 rq->nr_phys_segments++;
390 }
391
392 /*
393 * Flag the last request in the series so that drivers know when IO
394 * should be kicked off, if they don't do it on a per-request basis.
395 *
396 * Note: the flag isn't the only condition drivers should do kick off.
397 * If drive is busy, the last request might not have the bit set.
398 */
399 if (last)
400 rq->cmd_flags |= REQ_END;
320ae51f
JA
401}
402
403static void blk_mq_requeue_request(struct request *rq)
404{
405 struct request_queue *q = rq->q;
406
407 trace_block_rq_requeue(q, rq);
408 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
409
410 rq->cmd_flags &= ~REQ_END;
411
412 if (q->dma_drain_size && blk_rq_bytes(rq))
413 rq->nr_phys_segments--;
320ae51f
JA
414}
415
24d2f903
CH
416struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
417{
418 return tags->rqs[tag];
419}
420EXPORT_SYMBOL(blk_mq_tag_to_rq);
421
320ae51f
JA
422struct blk_mq_timeout_data {
423 struct blk_mq_hw_ctx *hctx;
424 unsigned long *next;
425 unsigned int *next_set;
426};
427
428static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
429{
430 struct blk_mq_timeout_data *data = __data;
431 struct blk_mq_hw_ctx *hctx = data->hctx;
432 unsigned int tag;
433
434 /* It may not be in flight yet (this is where
435 * the REQ_ATOMIC_STARTED flag comes in). The requests are
436 * statically allocated, so we know it's always safe to access the
437 * memory associated with a bit offset into ->rqs[].
438 */
439 tag = 0;
440 do {
441 struct request *rq;
442
24d2f903
CH
443 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
444 if (tag >= hctx->tags->nr_tags)
320ae51f
JA
445 break;
446
24d2f903
CH
447 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
448 if (rq->q != hctx->queue)
449 continue;
320ae51f
JA
450 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
451 continue;
452
453 blk_rq_check_expired(rq, data->next, data->next_set);
454 } while (1);
455}
456
457static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
458 unsigned long *next,
459 unsigned int *next_set)
460{
461 struct blk_mq_timeout_data data = {
462 .hctx = hctx,
463 .next = next,
464 .next_set = next_set,
465 };
466
467 /*
468 * Ask the tagging code to iterate busy requests, so we can
469 * check them for timeout.
470 */
471 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
472}
473
474static void blk_mq_rq_timer(unsigned long data)
475{
476 struct request_queue *q = (struct request_queue *) data;
477 struct blk_mq_hw_ctx *hctx;
478 unsigned long next = 0;
479 int i, next_set = 0;
480
481 queue_for_each_hw_ctx(q, hctx, i)
482 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
483
484 if (next_set)
485 mod_timer(&q->timeout, round_jiffies_up(next));
486}
487
488/*
489 * Reverse check our software queue for entries that we could potentially
490 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
491 * too much time checking for merges.
492 */
493static bool blk_mq_attempt_merge(struct request_queue *q,
494 struct blk_mq_ctx *ctx, struct bio *bio)
495{
496 struct request *rq;
497 int checked = 8;
498
499 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
500 int el_ret;
501
502 if (!checked--)
503 break;
504
505 if (!blk_rq_merge_ok(rq, bio))
506 continue;
507
508 el_ret = blk_try_merge(rq, bio);
509 if (el_ret == ELEVATOR_BACK_MERGE) {
510 if (bio_attempt_back_merge(q, rq, bio)) {
511 ctx->rq_merged++;
512 return true;
513 }
514 break;
515 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
516 if (bio_attempt_front_merge(q, rq, bio)) {
517 ctx->rq_merged++;
518 return true;
519 }
520 break;
521 }
522 }
523
524 return false;
525}
526
527void blk_mq_add_timer(struct request *rq)
528{
529 __blk_add_timer(rq, NULL);
530}
531
532/*
533 * Run this hardware queue, pulling any software queues mapped to it in.
534 * Note that this function currently has various problems around ordering
535 * of IO. In particular, we'd like FIFO behaviour on handling existing
536 * items on the hctx->dispatch list. Ignore that for now.
537 */
538static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
539{
540 struct request_queue *q = hctx->queue;
541 struct blk_mq_ctx *ctx;
542 struct request *rq;
543 LIST_HEAD(rq_list);
544 int bit, queued;
545
fd1270d5 546 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
e4043dcf 547
5d12f905 548 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
549 return;
550
551 hctx->run++;
552
553 /*
554 * Touch any software queue that has pending entries.
555 */
556 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
557 clear_bit(bit, hctx->ctx_map);
558 ctx = hctx->ctxs[bit];
559 BUG_ON(bit != ctx->index_hw);
560
561 spin_lock(&ctx->lock);
562 list_splice_tail_init(&ctx->rq_list, &rq_list);
563 spin_unlock(&ctx->lock);
564 }
565
566 /*
567 * If we have previous entries on our dispatch list, grab them
568 * and stuff them at the front for more fair dispatch.
569 */
570 if (!list_empty_careful(&hctx->dispatch)) {
571 spin_lock(&hctx->lock);
572 if (!list_empty(&hctx->dispatch))
573 list_splice_init(&hctx->dispatch, &rq_list);
574 spin_unlock(&hctx->lock);
575 }
576
577 /*
578 * Delete and return all entries from our dispatch list
579 */
580 queued = 0;
581
582 /*
583 * Now process all the entries, sending them to the driver.
584 */
585 while (!list_empty(&rq_list)) {
586 int ret;
587
588 rq = list_first_entry(&rq_list, struct request, queuelist);
589 list_del_init(&rq->queuelist);
320ae51f 590
49f5baa5 591 blk_mq_start_request(rq, list_empty(&rq_list));
320ae51f
JA
592
593 ret = q->mq_ops->queue_rq(hctx, rq);
594 switch (ret) {
595 case BLK_MQ_RQ_QUEUE_OK:
596 queued++;
597 continue;
598 case BLK_MQ_RQ_QUEUE_BUSY:
599 /*
600 * FIXME: we should have a mechanism to stop the queue
601 * like blk_stop_queue, otherwise we will waste cpu
602 * time
603 */
604 list_add(&rq->queuelist, &rq_list);
605 blk_mq_requeue_request(rq);
606 break;
607 default:
608 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 609 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 610 rq->errors = -EIO;
320ae51f
JA
611 blk_mq_end_io(rq, rq->errors);
612 break;
613 }
614
615 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
616 break;
617 }
618
619 if (!queued)
620 hctx->dispatched[0]++;
621 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
622 hctx->dispatched[ilog2(queued) + 1]++;
623
624 /*
625 * Any items that need requeuing? Stuff them into hctx->dispatch,
626 * that is where we will continue on next queue run.
627 */
628 if (!list_empty(&rq_list)) {
629 spin_lock(&hctx->lock);
630 list_splice(&rq_list, &hctx->dispatch);
631 spin_unlock(&hctx->lock);
632 }
633}
634
635void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
636{
5d12f905 637 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
638 return;
639
e4043dcf 640 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
320ae51f 641 __blk_mq_run_hw_queue(hctx);
e4043dcf 642 else if (hctx->queue->nr_hw_queues == 1)
70f4db63 643 kblockd_schedule_delayed_work(&hctx->run_work, 0);
e4043dcf
JA
644 else {
645 unsigned int cpu;
646
647 /*
648 * It'd be great if the workqueue API had a way to pass
649 * in a mask and had some smarts for more clever placement
650 * than the first CPU. Or we could round-robin here. For now,
651 * just queue on the first CPU.
652 */
653 cpu = cpumask_first(hctx->cpumask);
70f4db63 654 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
e4043dcf 655 }
320ae51f
JA
656}
657
658void blk_mq_run_queues(struct request_queue *q, bool async)
659{
660 struct blk_mq_hw_ctx *hctx;
661 int i;
662
663 queue_for_each_hw_ctx(q, hctx, i) {
664 if ((!blk_mq_hctx_has_pending(hctx) &&
665 list_empty_careful(&hctx->dispatch)) ||
5d12f905 666 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
667 continue;
668
e4043dcf 669 preempt_disable();
320ae51f 670 blk_mq_run_hw_queue(hctx, async);
e4043dcf 671 preempt_enable();
320ae51f
JA
672 }
673}
674EXPORT_SYMBOL(blk_mq_run_queues);
675
676void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
677{
70f4db63
CH
678 cancel_delayed_work(&hctx->run_work);
679 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
680 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
681}
682EXPORT_SYMBOL(blk_mq_stop_hw_queue);
683
280d45f6
CH
684void blk_mq_stop_hw_queues(struct request_queue *q)
685{
686 struct blk_mq_hw_ctx *hctx;
687 int i;
688
689 queue_for_each_hw_ctx(q, hctx, i)
690 blk_mq_stop_hw_queue(hctx);
691}
692EXPORT_SYMBOL(blk_mq_stop_hw_queues);
693
320ae51f
JA
694void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
695{
696 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf
JA
697
698 preempt_disable();
320ae51f 699 __blk_mq_run_hw_queue(hctx);
e4043dcf 700 preempt_enable();
320ae51f
JA
701}
702EXPORT_SYMBOL(blk_mq_start_hw_queue);
703
1b4a3258 704void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
705{
706 struct blk_mq_hw_ctx *hctx;
707 int i;
708
709 queue_for_each_hw_ctx(q, hctx, i) {
710 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
711 continue;
712
713 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 714 preempt_disable();
1b4a3258 715 blk_mq_run_hw_queue(hctx, async);
e4043dcf 716 preempt_enable();
320ae51f
JA
717 }
718}
719EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
720
70f4db63 721static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
722{
723 struct blk_mq_hw_ctx *hctx;
724
70f4db63 725 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
e4043dcf 726
320ae51f
JA
727 __blk_mq_run_hw_queue(hctx);
728}
729
70f4db63
CH
730static void blk_mq_delay_work_fn(struct work_struct *work)
731{
732 struct blk_mq_hw_ctx *hctx;
733
734 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
735
736 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
737 __blk_mq_run_hw_queue(hctx);
738}
739
740void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
741{
742 unsigned long tmo = msecs_to_jiffies(msecs);
743
744 if (hctx->queue->nr_hw_queues == 1)
745 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
746 else {
747 unsigned int cpu;
748
749 /*
750 * It'd be great if the workqueue API had a way to pass
751 * in a mask and had some smarts for more clever placement
752 * than the first CPU. Or we could round-robin here. For now,
753 * just queue on the first CPU.
754 */
755 cpu = cpumask_first(hctx->cpumask);
756 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
757 }
758}
759EXPORT_SYMBOL(blk_mq_delay_queue);
760
320ae51f 761static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
72a0a36e 762 struct request *rq, bool at_head)
320ae51f
JA
763{
764 struct blk_mq_ctx *ctx = rq->mq_ctx;
765
01b983c9
JA
766 trace_block_rq_insert(hctx->queue, rq);
767
72a0a36e
CH
768 if (at_head)
769 list_add(&rq->queuelist, &ctx->rq_list);
770 else
771 list_add_tail(&rq->queuelist, &ctx->rq_list);
320ae51f
JA
772 blk_mq_hctx_mark_pending(hctx, ctx);
773
774 /*
775 * We do this early, to ensure we are on the right CPU.
776 */
777 blk_mq_add_timer(rq);
778}
779
eeabc850
CH
780void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
781 bool async)
320ae51f 782{
eeabc850 783 struct request_queue *q = rq->q;
320ae51f 784 struct blk_mq_hw_ctx *hctx;
eeabc850
CH
785 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
786
787 current_ctx = blk_mq_get_ctx(q);
788 if (!cpu_online(ctx->cpu))
789 rq->mq_ctx = ctx = current_ctx;
320ae51f 790
320ae51f
JA
791 hctx = q->mq_ops->map_queue(q, ctx->cpu);
792
eeabc850
CH
793 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
794 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
320ae51f
JA
795 blk_insert_flush(rq);
796 } else {
320ae51f 797 spin_lock(&ctx->lock);
72a0a36e 798 __blk_mq_insert_request(hctx, rq, at_head);
320ae51f 799 spin_unlock(&ctx->lock);
320ae51f
JA
800 }
801
320ae51f
JA
802 if (run_queue)
803 blk_mq_run_hw_queue(hctx, async);
e4043dcf
JA
804
805 blk_mq_put_ctx(current_ctx);
320ae51f
JA
806}
807
808static void blk_mq_insert_requests(struct request_queue *q,
809 struct blk_mq_ctx *ctx,
810 struct list_head *list,
811 int depth,
812 bool from_schedule)
813
814{
815 struct blk_mq_hw_ctx *hctx;
816 struct blk_mq_ctx *current_ctx;
817
818 trace_block_unplug(q, depth, !from_schedule);
819
820 current_ctx = blk_mq_get_ctx(q);
821
822 if (!cpu_online(ctx->cpu))
823 ctx = current_ctx;
824 hctx = q->mq_ops->map_queue(q, ctx->cpu);
825
826 /*
827 * preemption doesn't flush plug list, so it's possible ctx->cpu is
828 * offline now
829 */
830 spin_lock(&ctx->lock);
831 while (!list_empty(list)) {
832 struct request *rq;
833
834 rq = list_first_entry(list, struct request, queuelist);
835 list_del_init(&rq->queuelist);
836 rq->mq_ctx = ctx;
72a0a36e 837 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
838 }
839 spin_unlock(&ctx->lock);
840
320ae51f 841 blk_mq_run_hw_queue(hctx, from_schedule);
e4043dcf 842 blk_mq_put_ctx(current_ctx);
320ae51f
JA
843}
844
845static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
846{
847 struct request *rqa = container_of(a, struct request, queuelist);
848 struct request *rqb = container_of(b, struct request, queuelist);
849
850 return !(rqa->mq_ctx < rqb->mq_ctx ||
851 (rqa->mq_ctx == rqb->mq_ctx &&
852 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
853}
854
855void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
856{
857 struct blk_mq_ctx *this_ctx;
858 struct request_queue *this_q;
859 struct request *rq;
860 LIST_HEAD(list);
861 LIST_HEAD(ctx_list);
862 unsigned int depth;
863
864 list_splice_init(&plug->mq_list, &list);
865
866 list_sort(NULL, &list, plug_ctx_cmp);
867
868 this_q = NULL;
869 this_ctx = NULL;
870 depth = 0;
871
872 while (!list_empty(&list)) {
873 rq = list_entry_rq(list.next);
874 list_del_init(&rq->queuelist);
875 BUG_ON(!rq->q);
876 if (rq->mq_ctx != this_ctx) {
877 if (this_ctx) {
878 blk_mq_insert_requests(this_q, this_ctx,
879 &ctx_list, depth,
880 from_schedule);
881 }
882
883 this_ctx = rq->mq_ctx;
884 this_q = rq->q;
885 depth = 0;
886 }
887
888 depth++;
889 list_add_tail(&rq->queuelist, &ctx_list);
890 }
891
892 /*
893 * If 'this_ctx' is set, we know we have entries to complete
894 * on 'ctx_list'. Do those.
895 */
896 if (this_ctx) {
897 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
898 from_schedule);
899 }
900}
901
902static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
903{
904 init_request_from_bio(rq, bio);
905 blk_account_io_start(rq, 1);
906}
907
908static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
909{
910 struct blk_mq_hw_ctx *hctx;
911 struct blk_mq_ctx *ctx;
912 const int is_sync = rw_is_sync(bio->bi_rw);
913 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
914 int rw = bio_data_dir(bio);
915 struct request *rq;
916 unsigned int use_plug, request_count = 0;
917
918 /*
919 * If we have multiple hardware queues, just go directly to
920 * one of those for sync IO.
921 */
922 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
923
924 blk_queue_bounce(q, &bio);
925
14ec77f3
NB
926 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
927 bio_endio(bio, -EIO);
928 return;
929 }
930
320ae51f
JA
931 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
932 return;
933
934 if (blk_mq_queue_enter(q)) {
935 bio_endio(bio, -EIO);
936 return;
937 }
938
939 ctx = blk_mq_get_ctx(q);
940 hctx = q->mq_ops->map_queue(q, ctx->cpu);
941
27fbf4e8
SL
942 if (is_sync)
943 rw |= REQ_SYNC;
320ae51f 944 trace_block_getrq(q, bio, rw);
18741986 945 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
320ae51f 946 if (likely(rq))
18741986 947 blk_mq_rq_ctx_init(q, ctx, rq, rw);
320ae51f
JA
948 else {
949 blk_mq_put_ctx(ctx);
950 trace_block_sleeprq(q, bio, rw);
18741986
CH
951 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
952 false);
320ae51f
JA
953 ctx = rq->mq_ctx;
954 hctx = q->mq_ops->map_queue(q, ctx->cpu);
955 }
956
957 hctx->queued++;
958
959 if (unlikely(is_flush_fua)) {
960 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
961 blk_insert_flush(rq);
962 goto run_queue;
963 }
964
965 /*
966 * A task plug currently exists. Since this is completely lockless,
967 * utilize that to temporarily store requests until the task is
968 * either done or scheduled away.
969 */
970 if (use_plug) {
971 struct blk_plug *plug = current->plug;
972
973 if (plug) {
974 blk_mq_bio_to_request(rq, bio);
92f399c7 975 if (list_empty(&plug->mq_list))
320ae51f
JA
976 trace_block_plug(q);
977 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
978 blk_flush_plug_list(plug, false);
979 trace_block_plug(q);
980 }
981 list_add_tail(&rq->queuelist, &plug->mq_list);
982 blk_mq_put_ctx(ctx);
983 return;
984 }
985 }
986
987 spin_lock(&ctx->lock);
988
989 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
990 blk_mq_attempt_merge(q, ctx, bio))
991 __blk_mq_free_request(hctx, ctx, rq);
992 else {
993 blk_mq_bio_to_request(rq, bio);
72a0a36e 994 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
995 }
996
997 spin_unlock(&ctx->lock);
320ae51f
JA
998
999 /*
1000 * For a SYNC request, send it to the hardware immediately. For an
1001 * ASYNC request, just ensure that we run it later on. The latter
1002 * allows for merging opportunities and more efficient dispatching.
1003 */
1004run_queue:
1005 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
e4043dcf 1006 blk_mq_put_ctx(ctx);
320ae51f
JA
1007}
1008
1009/*
1010 * Default mapping to a software queue, since we use one per CPU.
1011 */
1012struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1013{
1014 return q->queue_hw_ctx[q->mq_map[cpu]];
1015}
1016EXPORT_SYMBOL(blk_mq_map_queue);
1017
24d2f903 1018struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
320ae51f
JA
1019 unsigned int hctx_index)
1020{
1021 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
24d2f903 1022 GFP_KERNEL | __GFP_ZERO, set->numa_node);
320ae51f
JA
1023}
1024EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1025
1026void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1027 unsigned int hctx_index)
1028{
1029 kfree(hctx);
1030}
1031EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1032
1033static void blk_mq_hctx_notify(void *data, unsigned long action,
1034 unsigned int cpu)
1035{
1036 struct blk_mq_hw_ctx *hctx = data;
bccb5f7c 1037 struct request_queue *q = hctx->queue;
320ae51f
JA
1038 struct blk_mq_ctx *ctx;
1039 LIST_HEAD(tmp);
1040
1041 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1042 return;
1043
1044 /*
1045 * Move ctx entries to new CPU, if this one is going away.
1046 */
bccb5f7c 1047 ctx = __blk_mq_get_ctx(q, cpu);
320ae51f
JA
1048
1049 spin_lock(&ctx->lock);
1050 if (!list_empty(&ctx->rq_list)) {
1051 list_splice_init(&ctx->rq_list, &tmp);
1052 clear_bit(ctx->index_hw, hctx->ctx_map);
1053 }
1054 spin_unlock(&ctx->lock);
1055
1056 if (list_empty(&tmp))
1057 return;
1058
bccb5f7c 1059 ctx = blk_mq_get_ctx(q);
320ae51f
JA
1060 spin_lock(&ctx->lock);
1061
1062 while (!list_empty(&tmp)) {
1063 struct request *rq;
1064
1065 rq = list_first_entry(&tmp, struct request, queuelist);
1066 rq->mq_ctx = ctx;
1067 list_move_tail(&rq->queuelist, &ctx->rq_list);
1068 }
1069
bccb5f7c 1070 hctx = q->mq_ops->map_queue(q, ctx->cpu);
320ae51f
JA
1071 blk_mq_hctx_mark_pending(hctx, ctx);
1072
1073 spin_unlock(&ctx->lock);
bccb5f7c
JA
1074
1075 blk_mq_run_hw_queue(hctx, true);
e4043dcf 1076 blk_mq_put_ctx(ctx);
320ae51f
JA
1077}
1078
24d2f903
CH
1079static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1080 struct blk_mq_tags *tags, unsigned int hctx_idx)
95363efd 1081{
e9b267d9 1082 struct page *page;
320ae51f 1083
24d2f903 1084 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1085 int i;
320ae51f 1086
24d2f903
CH
1087 for (i = 0; i < tags->nr_tags; i++) {
1088 if (!tags->rqs[i])
e9b267d9 1089 continue;
24d2f903
CH
1090 set->ops->exit_request(set->driver_data, tags->rqs[i],
1091 hctx_idx, i);
e9b267d9 1092 }
320ae51f 1093 }
320ae51f 1094
24d2f903
CH
1095 while (!list_empty(&tags->page_list)) {
1096 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1097 list_del_init(&page->lru);
320ae51f
JA
1098 __free_pages(page, page->private);
1099 }
1100
24d2f903 1101 kfree(tags->rqs);
320ae51f 1102
24d2f903 1103 blk_mq_free_tags(tags);
320ae51f
JA
1104}
1105
1106static size_t order_to_size(unsigned int order)
1107{
1108 size_t ret = PAGE_SIZE;
1109
1110 while (order--)
1111 ret *= 2;
1112
1113 return ret;
1114}
1115
24d2f903
CH
1116static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1117 unsigned int hctx_idx)
320ae51f 1118{
24d2f903 1119 struct blk_mq_tags *tags;
320ae51f
JA
1120 unsigned int i, j, entries_per_page, max_order = 4;
1121 size_t rq_size, left;
1122
24d2f903
CH
1123 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1124 set->numa_node);
1125 if (!tags)
1126 return NULL;
320ae51f 1127
24d2f903
CH
1128 INIT_LIST_HEAD(&tags->page_list);
1129
1130 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1131 GFP_KERNEL, set->numa_node);
1132 if (!tags->rqs) {
1133 blk_mq_free_tags(tags);
1134 return NULL;
1135 }
320ae51f
JA
1136
1137 /*
1138 * rq_size is the size of the request plus driver payload, rounded
1139 * to the cacheline size
1140 */
24d2f903 1141 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1142 cache_line_size());
24d2f903 1143 left = rq_size * set->queue_depth;
320ae51f 1144
24d2f903 1145 for (i = 0; i < set->queue_depth; ) {
320ae51f
JA
1146 int this_order = max_order;
1147 struct page *page;
1148 int to_do;
1149 void *p;
1150
1151 while (left < order_to_size(this_order - 1) && this_order)
1152 this_order--;
1153
1154 do {
24d2f903
CH
1155 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1156 this_order);
320ae51f
JA
1157 if (page)
1158 break;
1159 if (!this_order--)
1160 break;
1161 if (order_to_size(this_order) < rq_size)
1162 break;
1163 } while (1);
1164
1165 if (!page)
24d2f903 1166 goto fail;
320ae51f
JA
1167
1168 page->private = this_order;
24d2f903 1169 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1170
1171 p = page_address(page);
1172 entries_per_page = order_to_size(this_order) / rq_size;
24d2f903 1173 to_do = min(entries_per_page, set->queue_depth - i);
320ae51f
JA
1174 left -= to_do * rq_size;
1175 for (j = 0; j < to_do; j++) {
24d2f903
CH
1176 tags->rqs[i] = p;
1177 if (set->ops->init_request) {
1178 if (set->ops->init_request(set->driver_data,
1179 tags->rqs[i], hctx_idx, i,
1180 set->numa_node))
1181 goto fail;
e9b267d9
CH
1182 }
1183
320ae51f
JA
1184 p += rq_size;
1185 i++;
1186 }
1187 }
1188
24d2f903 1189 return tags;
320ae51f 1190
24d2f903
CH
1191fail:
1192 pr_warn("%s: failed to allocate requests\n", __func__);
1193 blk_mq_free_rq_map(set, tags, hctx_idx);
1194 return NULL;
320ae51f
JA
1195}
1196
1197static int blk_mq_init_hw_queues(struct request_queue *q,
24d2f903 1198 struct blk_mq_tag_set *set)
320ae51f
JA
1199{
1200 struct blk_mq_hw_ctx *hctx;
1201 unsigned int i, j;
1202
1203 /*
1204 * Initialize hardware queues
1205 */
1206 queue_for_each_hw_ctx(q, hctx, i) {
1207 unsigned int num_maps;
1208 int node;
1209
1210 node = hctx->numa_node;
1211 if (node == NUMA_NO_NODE)
24d2f903 1212 node = hctx->numa_node = set->numa_node;
320ae51f 1213
70f4db63
CH
1214 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1215 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
320ae51f
JA
1216 spin_lock_init(&hctx->lock);
1217 INIT_LIST_HEAD(&hctx->dispatch);
1218 hctx->queue = q;
1219 hctx->queue_num = i;
24d2f903
CH
1220 hctx->flags = set->flags;
1221 hctx->cmd_size = set->cmd_size;
320ae51f
JA
1222
1223 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1224 blk_mq_hctx_notify, hctx);
1225 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1226
24d2f903 1227 hctx->tags = set->tags[i];
320ae51f
JA
1228
1229 /*
1230 * Allocate space for all possible cpus to avoid allocation in
1231 * runtime
1232 */
1233 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1234 GFP_KERNEL, node);
1235 if (!hctx->ctxs)
1236 break;
1237
1238 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1239 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1240 GFP_KERNEL, node);
1241 if (!hctx->ctx_map)
1242 break;
1243
1244 hctx->nr_ctx_map = num_maps;
1245 hctx->nr_ctx = 0;
1246
24d2f903
CH
1247 if (set->ops->init_hctx &&
1248 set->ops->init_hctx(hctx, set->driver_data, i))
320ae51f
JA
1249 break;
1250 }
1251
1252 if (i == q->nr_hw_queues)
1253 return 0;
1254
1255 /*
1256 * Init failed
1257 */
1258 queue_for_each_hw_ctx(q, hctx, j) {
1259 if (i == j)
1260 break;
1261
24d2f903
CH
1262 if (set->ops->exit_hctx)
1263 set->ops->exit_hctx(hctx, j);
320ae51f
JA
1264
1265 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
320ae51f
JA
1266 kfree(hctx->ctxs);
1267 }
1268
1269 return 1;
1270}
1271
1272static void blk_mq_init_cpu_queues(struct request_queue *q,
1273 unsigned int nr_hw_queues)
1274{
1275 unsigned int i;
1276
1277 for_each_possible_cpu(i) {
1278 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1279 struct blk_mq_hw_ctx *hctx;
1280
1281 memset(__ctx, 0, sizeof(*__ctx));
1282 __ctx->cpu = i;
1283 spin_lock_init(&__ctx->lock);
1284 INIT_LIST_HEAD(&__ctx->rq_list);
1285 __ctx->queue = q;
1286
1287 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1288 if (!cpu_online(i))
1289 continue;
1290
e4043dcf
JA
1291 hctx = q->mq_ops->map_queue(q, i);
1292 cpumask_set_cpu(i, hctx->cpumask);
1293 hctx->nr_ctx++;
1294
320ae51f
JA
1295 /*
1296 * Set local node, IFF we have more than one hw queue. If
1297 * not, we remain on the home node of the device
1298 */
1299 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1300 hctx->numa_node = cpu_to_node(i);
1301 }
1302}
1303
1304static void blk_mq_map_swqueue(struct request_queue *q)
1305{
1306 unsigned int i;
1307 struct blk_mq_hw_ctx *hctx;
1308 struct blk_mq_ctx *ctx;
1309
1310 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1311 cpumask_clear(hctx->cpumask);
320ae51f
JA
1312 hctx->nr_ctx = 0;
1313 }
1314
1315 /*
1316 * Map software to hardware queues
1317 */
1318 queue_for_each_ctx(q, ctx, i) {
1319 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1320 if (!cpu_online(i))
1321 continue;
1322
320ae51f 1323 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1324 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1325 ctx->index_hw = hctx->nr_ctx;
1326 hctx->ctxs[hctx->nr_ctx++] = ctx;
1327 }
1328}
1329
24d2f903 1330struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
320ae51f
JA
1331{
1332 struct blk_mq_hw_ctx **hctxs;
1333 struct blk_mq_ctx *ctx;
1334 struct request_queue *q;
1335 int i;
1336
320ae51f
JA
1337 ctx = alloc_percpu(struct blk_mq_ctx);
1338 if (!ctx)
1339 return ERR_PTR(-ENOMEM);
1340
24d2f903
CH
1341 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1342 set->numa_node);
320ae51f
JA
1343
1344 if (!hctxs)
1345 goto err_percpu;
1346
24d2f903
CH
1347 for (i = 0; i < set->nr_hw_queues; i++) {
1348 hctxs[i] = set->ops->alloc_hctx(set, i);
320ae51f
JA
1349 if (!hctxs[i])
1350 goto err_hctxs;
1351
e4043dcf
JA
1352 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1353 goto err_hctxs;
1354
320ae51f
JA
1355 hctxs[i]->numa_node = NUMA_NO_NODE;
1356 hctxs[i]->queue_num = i;
1357 }
1358
24d2f903 1359 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
320ae51f
JA
1360 if (!q)
1361 goto err_hctxs;
1362
24d2f903 1363 q->mq_map = blk_mq_make_queue_map(set);
320ae51f
JA
1364 if (!q->mq_map)
1365 goto err_map;
1366
1367 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1368 blk_queue_rq_timeout(q, 30000);
1369
1370 q->nr_queues = nr_cpu_ids;
24d2f903 1371 q->nr_hw_queues = set->nr_hw_queues;
320ae51f
JA
1372
1373 q->queue_ctx = ctx;
1374 q->queue_hw_ctx = hctxs;
1375
24d2f903 1376 q->mq_ops = set->ops;
94eddfbe 1377 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1378
1be036e9
CH
1379 q->sg_reserved_size = INT_MAX;
1380
320ae51f 1381 blk_queue_make_request(q, blk_mq_make_request);
24d2f903
CH
1382 blk_queue_rq_timed_out(q, set->ops->timeout);
1383 if (set->timeout)
1384 blk_queue_rq_timeout(q, set->timeout);
320ae51f 1385
24d2f903
CH
1386 if (set->ops->complete)
1387 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 1388
320ae51f 1389 blk_mq_init_flush(q);
24d2f903 1390 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 1391
24d2f903
CH
1392 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1393 set->cmd_size, cache_line_size()),
1394 GFP_KERNEL);
18741986 1395 if (!q->flush_rq)
320ae51f
JA
1396 goto err_hw;
1397
24d2f903 1398 if (blk_mq_init_hw_queues(q, set))
18741986
CH
1399 goto err_flush_rq;
1400
320ae51f
JA
1401 blk_mq_map_swqueue(q);
1402
1403 mutex_lock(&all_q_mutex);
1404 list_add_tail(&q->all_q_node, &all_q_list);
1405 mutex_unlock(&all_q_mutex);
1406
1407 return q;
18741986
CH
1408
1409err_flush_rq:
1410 kfree(q->flush_rq);
320ae51f
JA
1411err_hw:
1412 kfree(q->mq_map);
1413err_map:
1414 blk_cleanup_queue(q);
1415err_hctxs:
24d2f903 1416 for (i = 0; i < set->nr_hw_queues; i++) {
320ae51f
JA
1417 if (!hctxs[i])
1418 break;
e4043dcf 1419 free_cpumask_var(hctxs[i]->cpumask);
24d2f903 1420 set->ops->free_hctx(hctxs[i], i);
320ae51f
JA
1421 }
1422 kfree(hctxs);
1423err_percpu:
1424 free_percpu(ctx);
1425 return ERR_PTR(-ENOMEM);
1426}
1427EXPORT_SYMBOL(blk_mq_init_queue);
1428
1429void blk_mq_free_queue(struct request_queue *q)
1430{
1431 struct blk_mq_hw_ctx *hctx;
1432 int i;
1433
1434 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1435 kfree(hctx->ctx_map);
1436 kfree(hctx->ctxs);
320ae51f
JA
1437 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1438 if (q->mq_ops->exit_hctx)
1439 q->mq_ops->exit_hctx(hctx, i);
e4043dcf 1440 free_cpumask_var(hctx->cpumask);
320ae51f
JA
1441 q->mq_ops->free_hctx(hctx, i);
1442 }
1443
1444 free_percpu(q->queue_ctx);
1445 kfree(q->queue_hw_ctx);
1446 kfree(q->mq_map);
1447
1448 q->queue_ctx = NULL;
1449 q->queue_hw_ctx = NULL;
1450 q->mq_map = NULL;
1451
1452 mutex_lock(&all_q_mutex);
1453 list_del_init(&q->all_q_node);
1454 mutex_unlock(&all_q_mutex);
1455}
320ae51f
JA
1456
1457/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1458static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1459{
1460 blk_mq_freeze_queue(q);
1461
1462 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1463
1464 /*
1465 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1466 * we should change hctx numa_node according to new topology (this
1467 * involves free and re-allocate memory, worthy doing?)
1468 */
1469
1470 blk_mq_map_swqueue(q);
1471
1472 blk_mq_unfreeze_queue(q);
1473}
1474
f618ef7c
PG
1475static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1476 unsigned long action, void *hcpu)
320ae51f
JA
1477{
1478 struct request_queue *q;
1479
1480 /*
1481 * Before new mapping is established, hotadded cpu might already start
1482 * handling requests. This doesn't break anything as we map offline
1483 * CPUs to first hardware queue. We will re-init queue below to get
1484 * optimal settings.
1485 */
1486 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1487 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1488 return NOTIFY_OK;
1489
1490 mutex_lock(&all_q_mutex);
1491 list_for_each_entry(q, &all_q_list, all_q_node)
1492 blk_mq_queue_reinit(q);
1493 mutex_unlock(&all_q_mutex);
1494 return NOTIFY_OK;
1495}
1496
24d2f903
CH
1497int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1498{
1499 int i;
1500
1501 if (!set->nr_hw_queues)
1502 return -EINVAL;
1503 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1504 return -EINVAL;
1505 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1506 return -EINVAL;
1507
1508 if (!set->nr_hw_queues ||
1509 !set->ops->queue_rq || !set->ops->map_queue ||
1510 !set->ops->alloc_hctx || !set->ops->free_hctx)
1511 return -EINVAL;
1512
1513
1514 set->tags = kmalloc_node(set->nr_hw_queues * sizeof(struct blk_mq_tags),
1515 GFP_KERNEL, set->numa_node);
1516 if (!set->tags)
1517 goto out;
1518
1519 for (i = 0; i < set->nr_hw_queues; i++) {
1520 set->tags[i] = blk_mq_init_rq_map(set, i);
1521 if (!set->tags[i])
1522 goto out_unwind;
1523 }
1524
1525 return 0;
1526
1527out_unwind:
1528 while (--i >= 0)
1529 blk_mq_free_rq_map(set, set->tags[i], i);
1530out:
1531 return -ENOMEM;
1532}
1533EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1534
1535void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1536{
1537 int i;
1538
1539 for (i = 0; i < set->nr_hw_queues; i++)
1540 blk_mq_free_rq_map(set, set->tags[i], i);
1541}
1542EXPORT_SYMBOL(blk_mq_free_tag_set);
1543
676141e4
JA
1544void blk_mq_disable_hotplug(void)
1545{
1546 mutex_lock(&all_q_mutex);
1547}
1548
1549void blk_mq_enable_hotplug(void)
1550{
1551 mutex_unlock(&all_q_mutex);
1552}
1553
320ae51f
JA
1554static int __init blk_mq_init(void)
1555{
320ae51f
JA
1556 blk_mq_cpu_init();
1557
1558 /* Must be called after percpu_counter_hotcpu_callback() */
1559 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1560
1561 return 0;
1562}
1563subsys_initcall(blk_mq_init);