]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-mq.c
blk-mq: always initialize request->start_time
[mirror_ubuntu-artful-kernel.git] / block / blk-mq.c
CommitLineData
75bb4625
JA
1/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
320ae51f
JA
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/backing-dev.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
12#include <linux/mm.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/workqueue.h>
16#include <linux/smp.h>
17#include <linux/llist.h>
18#include <linux/list_sort.h>
19#include <linux/cpu.h>
20#include <linux/cache.h>
21#include <linux/sched/sysctl.h>
22#include <linux/delay.h>
23
24#include <trace/events/block.h>
25
26#include <linux/blk-mq.h>
27#include "blk.h"
28#include "blk-mq.h"
29#include "blk-mq-tag.h"
30
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
34static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
35
320ae51f
JA
36/*
37 * Check if any of the ctx's have pending work in this hardware queue
38 */
39static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
40{
41 unsigned int i;
42
1429d7c9
JA
43 for (i = 0; i < hctx->ctx_map.map_size; i++)
44 if (hctx->ctx_map.map[i].word)
320ae51f
JA
45 return true;
46
47 return false;
48}
49
1429d7c9
JA
50static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
51 struct blk_mq_ctx *ctx)
52{
53 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
54}
55
56#define CTX_TO_BIT(hctx, ctx) \
57 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
58
320ae51f
JA
59/*
60 * Mark this ctx as having pending work in this hardware queue
61 */
62static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
63 struct blk_mq_ctx *ctx)
64{
1429d7c9
JA
65 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
66
67 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
68 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
69}
70
71static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
72 struct blk_mq_ctx *ctx)
73{
74 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
75
76 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
320ae51f
JA
77}
78
320ae51f
JA
79static int blk_mq_queue_enter(struct request_queue *q)
80{
81 int ret;
82
83 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
84 smp_wmb();
3b632cf0
KB
85
86 /* we have problems freezing the queue if it's initializing */
87 if (!blk_queue_dying(q) &&
88 (!blk_queue_bypass(q) || !blk_queue_init_done(q)))
320ae51f
JA
89 return 0;
90
91 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
92
93 spin_lock_irq(q->queue_lock);
94 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
43a5e4e2
ML
95 !blk_queue_bypass(q) || blk_queue_dying(q),
96 *q->queue_lock);
320ae51f 97 /* inc usage with lock hold to avoid freeze_queue runs here */
43a5e4e2 98 if (!ret && !blk_queue_dying(q))
320ae51f 99 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
43a5e4e2
ML
100 else if (blk_queue_dying(q))
101 ret = -ENODEV;
320ae51f
JA
102 spin_unlock_irq(q->queue_lock);
103
104 return ret;
105}
106
107static void blk_mq_queue_exit(struct request_queue *q)
108{
109 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
110}
111
43a5e4e2
ML
112static void __blk_mq_drain_queue(struct request_queue *q)
113{
114 while (true) {
115 s64 count;
116
117 spin_lock_irq(q->queue_lock);
118 count = percpu_counter_sum(&q->mq_usage_counter);
119 spin_unlock_irq(q->queue_lock);
120
121 if (count == 0)
122 break;
123 blk_mq_run_queues(q, false);
124 msleep(10);
125 }
126}
127
320ae51f
JA
128/*
129 * Guarantee no request is in use, so we can change any data structure of
130 * the queue afterward.
131 */
132static void blk_mq_freeze_queue(struct request_queue *q)
133{
134 bool drain;
135
136 spin_lock_irq(q->queue_lock);
137 drain = !q->bypass_depth++;
138 queue_flag_set(QUEUE_FLAG_BYPASS, q);
139 spin_unlock_irq(q->queue_lock);
140
43a5e4e2
ML
141 if (drain)
142 __blk_mq_drain_queue(q);
143}
320ae51f 144
43a5e4e2
ML
145void blk_mq_drain_queue(struct request_queue *q)
146{
147 __blk_mq_drain_queue(q);
320ae51f
JA
148}
149
150static void blk_mq_unfreeze_queue(struct request_queue *q)
151{
152 bool wake = false;
153
154 spin_lock_irq(q->queue_lock);
155 if (!--q->bypass_depth) {
156 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
157 wake = true;
158 }
159 WARN_ON_ONCE(q->bypass_depth < 0);
160 spin_unlock_irq(q->queue_lock);
161 if (wake)
162 wake_up_all(&q->mq_freeze_wq);
163}
164
165bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
166{
167 return blk_mq_has_free_tags(hctx->tags);
168}
169EXPORT_SYMBOL(blk_mq_can_queue);
170
94eddfbe
JA
171static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
172 struct request *rq, unsigned int rw_flags)
320ae51f 173{
94eddfbe
JA
174 if (blk_queue_io_stat(q))
175 rw_flags |= REQ_IO_STAT;
176
af76e555
CH
177 INIT_LIST_HEAD(&rq->queuelist);
178 /* csd/requeue_work/fifo_time is initialized before use */
179 rq->q = q;
320ae51f 180 rq->mq_ctx = ctx;
0d2602ca 181 rq->cmd_flags |= rw_flags;
af76e555
CH
182 /* do not touch atomic flags, it needs atomic ops against the timer */
183 rq->cpu = -1;
af76e555
CH
184 INIT_HLIST_NODE(&rq->hash);
185 RB_CLEAR_NODE(&rq->rb_node);
af76e555
CH
186 rq->rq_disk = NULL;
187 rq->part = NULL;
3ee32372 188 rq->start_time = jiffies;
af76e555
CH
189#ifdef CONFIG_BLK_CGROUP
190 rq->rl = NULL;
0fec08b4 191 set_start_time_ns(rq);
af76e555
CH
192 rq->io_start_time_ns = 0;
193#endif
194 rq->nr_phys_segments = 0;
195#if defined(CONFIG_BLK_DEV_INTEGRITY)
196 rq->nr_integrity_segments = 0;
197#endif
af76e555
CH
198 rq->special = NULL;
199 /* tag was already set */
200 rq->errors = 0;
af76e555
CH
201
202 rq->extra_len = 0;
203 rq->sense_len = 0;
204 rq->resid_len = 0;
205 rq->sense = NULL;
206
af76e555 207 INIT_LIST_HEAD(&rq->timeout_list);
f6be4fb4
JA
208 rq->timeout = 0;
209
af76e555
CH
210 rq->end_io = NULL;
211 rq->end_io_data = NULL;
212 rq->next_rq = NULL;
213
320ae51f
JA
214 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
215}
216
5dee8577 217static struct request *
cb96a42c 218__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
5dee8577
CH
219{
220 struct request *rq;
221 unsigned int tag;
222
cb96a42c 223 tag = blk_mq_get_tag(data);
5dee8577 224 if (tag != BLK_MQ_TAG_FAIL) {
cb96a42c 225 rq = data->hctx->tags->rqs[tag];
5dee8577
CH
226
227 rq->cmd_flags = 0;
cb96a42c 228 if (blk_mq_tag_busy(data->hctx)) {
5dee8577 229 rq->cmd_flags = REQ_MQ_INFLIGHT;
cb96a42c 230 atomic_inc(&data->hctx->nr_active);
5dee8577
CH
231 }
232
233 rq->tag = tag;
cb96a42c 234 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
5dee8577
CH
235 return rq;
236 }
237
238 return NULL;
239}
240
4ce01dd1
CH
241struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
242 bool reserved)
320ae51f 243{
d852564f
CH
244 struct blk_mq_ctx *ctx;
245 struct blk_mq_hw_ctx *hctx;
320ae51f 246 struct request *rq;
cb96a42c 247 struct blk_mq_alloc_data alloc_data;
320ae51f
JA
248
249 if (blk_mq_queue_enter(q))
250 return NULL;
251
d852564f
CH
252 ctx = blk_mq_get_ctx(q);
253 hctx = q->mq_ops->map_queue(q, ctx->cpu);
cb96a42c
ML
254 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
255 reserved, ctx, hctx);
d852564f 256
cb96a42c 257 rq = __blk_mq_alloc_request(&alloc_data, rw);
d852564f
CH
258 if (!rq && (gfp & __GFP_WAIT)) {
259 __blk_mq_run_hw_queue(hctx);
260 blk_mq_put_ctx(ctx);
261
262 ctx = blk_mq_get_ctx(q);
263 hctx = q->mq_ops->map_queue(q, ctx->cpu);
cb96a42c
ML
264 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
265 hctx);
266 rq = __blk_mq_alloc_request(&alloc_data, rw);
267 ctx = alloc_data.ctx;
d852564f
CH
268 }
269 blk_mq_put_ctx(ctx);
320ae51f
JA
270 return rq;
271}
4bb659b1 272EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f 273
320ae51f
JA
274static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
275 struct blk_mq_ctx *ctx, struct request *rq)
276{
277 const int tag = rq->tag;
278 struct request_queue *q = rq->q;
279
0d2602ca
JA
280 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
281 atomic_dec(&hctx->nr_active);
282
af76e555 283 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
0d2602ca 284 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
320ae51f
JA
285 blk_mq_queue_exit(q);
286}
287
288void blk_mq_free_request(struct request *rq)
289{
290 struct blk_mq_ctx *ctx = rq->mq_ctx;
291 struct blk_mq_hw_ctx *hctx;
292 struct request_queue *q = rq->q;
293
294 ctx->rq_completed[rq_is_sync(rq)]++;
295
296 hctx = q->mq_ops->map_queue(q, ctx->cpu);
297 __blk_mq_free_request(hctx, ctx, rq);
298}
299
8727af4b
CH
300/*
301 * Clone all relevant state from a request that has been put on hold in
302 * the flush state machine into the preallocated flush request that hangs
303 * off the request queue.
304 *
305 * For a driver the flush request should be invisible, that's why we are
306 * impersonating the original request here.
307 */
308void blk_mq_clone_flush_request(struct request *flush_rq,
309 struct request *orig_rq)
310{
311 struct blk_mq_hw_ctx *hctx =
312 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
313
314 flush_rq->mq_ctx = orig_rq->mq_ctx;
315 flush_rq->tag = orig_rq->tag;
316 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
317 hctx->cmd_size);
318}
319
63151a44 320inline void __blk_mq_end_io(struct request *rq, int error)
320ae51f 321{
0d11e6ac
ML
322 blk_account_io_done(rq);
323
91b63639 324 if (rq->end_io) {
320ae51f 325 rq->end_io(rq, error);
91b63639
CH
326 } else {
327 if (unlikely(blk_bidi_rq(rq)))
328 blk_mq_free_request(rq->next_rq);
320ae51f 329 blk_mq_free_request(rq);
91b63639 330 }
320ae51f 331}
63151a44
CH
332EXPORT_SYMBOL(__blk_mq_end_io);
333
334void blk_mq_end_io(struct request *rq, int error)
335{
336 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
337 BUG();
338 __blk_mq_end_io(rq, error);
339}
340EXPORT_SYMBOL(blk_mq_end_io);
320ae51f 341
30a91cb4 342static void __blk_mq_complete_request_remote(void *data)
320ae51f 343{
3d6efbf6 344 struct request *rq = data;
320ae51f 345
30a91cb4 346 rq->q->softirq_done_fn(rq);
320ae51f 347}
320ae51f 348
ed851860 349static void blk_mq_ipi_complete_request(struct request *rq)
320ae51f
JA
350{
351 struct blk_mq_ctx *ctx = rq->mq_ctx;
38535201 352 bool shared = false;
320ae51f
JA
353 int cpu;
354
38535201 355 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
30a91cb4
CH
356 rq->q->softirq_done_fn(rq);
357 return;
358 }
320ae51f
JA
359
360 cpu = get_cpu();
38535201
CH
361 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
362 shared = cpus_share_cache(cpu, ctx->cpu);
363
364 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 365 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
366 rq->csd.info = rq;
367 rq->csd.flags = 0;
c46fff2a 368 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 369 } else {
30a91cb4 370 rq->q->softirq_done_fn(rq);
3d6efbf6 371 }
320ae51f
JA
372 put_cpu();
373}
30a91cb4 374
ed851860
JA
375void __blk_mq_complete_request(struct request *rq)
376{
377 struct request_queue *q = rq->q;
378
379 if (!q->softirq_done_fn)
380 blk_mq_end_io(rq, rq->errors);
381 else
382 blk_mq_ipi_complete_request(rq);
383}
384
30a91cb4
CH
385/**
386 * blk_mq_complete_request - end I/O on a request
387 * @rq: the request being processed
388 *
389 * Description:
390 * Ends all I/O on a request. It does not handle partial completions.
391 * The actual completion happens out-of-order, through a IPI handler.
392 **/
393void blk_mq_complete_request(struct request *rq)
394{
95f09684
JA
395 struct request_queue *q = rq->q;
396
397 if (unlikely(blk_should_fake_timeout(q)))
30a91cb4 398 return;
ed851860
JA
399 if (!blk_mark_rq_complete(rq))
400 __blk_mq_complete_request(rq);
30a91cb4
CH
401}
402EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 403
49f5baa5 404static void blk_mq_start_request(struct request *rq, bool last)
320ae51f
JA
405{
406 struct request_queue *q = rq->q;
407
408 trace_block_rq_issue(q, rq);
409
742ee69b 410 rq->resid_len = blk_rq_bytes(rq);
91b63639
CH
411 if (unlikely(blk_bidi_rq(rq)))
412 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
742ee69b 413
320ae51f
JA
414 /*
415 * Just mark start time and set the started bit. Due to memory
416 * ordering, we know we'll see the correct deadline as long as
c22d9d8a
JA
417 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
418 * unless one has been set in the request.
320ae51f 419 */
c22d9d8a
JA
420 if (!rq->timeout)
421 rq->deadline = jiffies + q->rq_timeout;
422 else
423 rq->deadline = jiffies + rq->timeout;
87ee7b11
JA
424
425 /*
426 * Mark us as started and clear complete. Complete might have been
427 * set if requeue raced with timeout, which then marked it as
428 * complete. So be sure to clear complete again when we start
429 * the request, otherwise we'll ignore the completion event.
430 */
4b570521
JA
431 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
432 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
433 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
434 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
49f5baa5
CH
435
436 if (q->dma_drain_size && blk_rq_bytes(rq)) {
437 /*
438 * Make sure space for the drain appears. We know we can do
439 * this because max_hw_segments has been adjusted to be one
440 * fewer than the device can handle.
441 */
442 rq->nr_phys_segments++;
443 }
444
445 /*
446 * Flag the last request in the series so that drivers know when IO
447 * should be kicked off, if they don't do it on a per-request basis.
448 *
449 * Note: the flag isn't the only condition drivers should do kick off.
450 * If drive is busy, the last request might not have the bit set.
451 */
452 if (last)
453 rq->cmd_flags |= REQ_END;
320ae51f
JA
454}
455
ed0791b2 456static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
457{
458 struct request_queue *q = rq->q;
459
460 trace_block_rq_requeue(q, rq);
461 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
462
463 rq->cmd_flags &= ~REQ_END;
464
465 if (q->dma_drain_size && blk_rq_bytes(rq))
466 rq->nr_phys_segments--;
320ae51f
JA
467}
468
ed0791b2
CH
469void blk_mq_requeue_request(struct request *rq)
470{
ed0791b2
CH
471 __blk_mq_requeue_request(rq);
472 blk_clear_rq_complete(rq);
473
ed0791b2 474 BUG_ON(blk_queued_rq(rq));
6fca6a61 475 blk_mq_add_to_requeue_list(rq, true);
ed0791b2
CH
476}
477EXPORT_SYMBOL(blk_mq_requeue_request);
478
6fca6a61
CH
479static void blk_mq_requeue_work(struct work_struct *work)
480{
481 struct request_queue *q =
482 container_of(work, struct request_queue, requeue_work);
483 LIST_HEAD(rq_list);
484 struct request *rq, *next;
485 unsigned long flags;
486
487 spin_lock_irqsave(&q->requeue_lock, flags);
488 list_splice_init(&q->requeue_list, &rq_list);
489 spin_unlock_irqrestore(&q->requeue_lock, flags);
490
491 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
492 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
493 continue;
494
495 rq->cmd_flags &= ~REQ_SOFTBARRIER;
496 list_del_init(&rq->queuelist);
497 blk_mq_insert_request(rq, true, false, false);
498 }
499
500 while (!list_empty(&rq_list)) {
501 rq = list_entry(rq_list.next, struct request, queuelist);
502 list_del_init(&rq->queuelist);
503 blk_mq_insert_request(rq, false, false, false);
504 }
505
506 blk_mq_run_queues(q, false);
507}
508
509void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
510{
511 struct request_queue *q = rq->q;
512 unsigned long flags;
513
514 /*
515 * We abuse this flag that is otherwise used by the I/O scheduler to
516 * request head insertation from the workqueue.
517 */
518 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
519
520 spin_lock_irqsave(&q->requeue_lock, flags);
521 if (at_head) {
522 rq->cmd_flags |= REQ_SOFTBARRIER;
523 list_add(&rq->queuelist, &q->requeue_list);
524 } else {
525 list_add_tail(&rq->queuelist, &q->requeue_list);
526 }
527 spin_unlock_irqrestore(&q->requeue_lock, flags);
528}
529EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
530
531void blk_mq_kick_requeue_list(struct request_queue *q)
532{
533 kblockd_schedule_work(&q->requeue_work);
534}
535EXPORT_SYMBOL(blk_mq_kick_requeue_list);
536
0e62f51f 537static inline bool is_flush_request(struct request *rq, unsigned int tag)
24d2f903 538{
0e62f51f
JA
539 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
540 rq->q->flush_rq->tag == tag);
541}
542
543struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
544{
545 struct request *rq = tags->rqs[tag];
22302375 546
0e62f51f
JA
547 if (!is_flush_request(rq, tag))
548 return rq;
22302375 549
0e62f51f 550 return rq->q->flush_rq;
24d2f903
CH
551}
552EXPORT_SYMBOL(blk_mq_tag_to_rq);
553
320ae51f
JA
554struct blk_mq_timeout_data {
555 struct blk_mq_hw_ctx *hctx;
556 unsigned long *next;
557 unsigned int *next_set;
558};
559
560static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
561{
562 struct blk_mq_timeout_data *data = __data;
563 struct blk_mq_hw_ctx *hctx = data->hctx;
564 unsigned int tag;
565
566 /* It may not be in flight yet (this is where
567 * the REQ_ATOMIC_STARTED flag comes in). The requests are
568 * statically allocated, so we know it's always safe to access the
569 * memory associated with a bit offset into ->rqs[].
570 */
571 tag = 0;
572 do {
573 struct request *rq;
574
24d2f903
CH
575 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
576 if (tag >= hctx->tags->nr_tags)
320ae51f
JA
577 break;
578
0e62f51f 579 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
24d2f903
CH
580 if (rq->q != hctx->queue)
581 continue;
320ae51f
JA
582 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
583 continue;
584
585 blk_rq_check_expired(rq, data->next, data->next_set);
586 } while (1);
587}
588
589static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
590 unsigned long *next,
591 unsigned int *next_set)
592{
593 struct blk_mq_timeout_data data = {
594 .hctx = hctx,
595 .next = next,
596 .next_set = next_set,
597 };
598
599 /*
600 * Ask the tagging code to iterate busy requests, so we can
601 * check them for timeout.
602 */
603 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
604}
605
87ee7b11
JA
606static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
607{
608 struct request_queue *q = rq->q;
609
610 /*
611 * We know that complete is set at this point. If STARTED isn't set
612 * anymore, then the request isn't active and the "timeout" should
613 * just be ignored. This can happen due to the bitflag ordering.
614 * Timeout first checks if STARTED is set, and if it is, assumes
615 * the request is active. But if we race with completion, then
616 * we both flags will get cleared. So check here again, and ignore
617 * a timeout event with a request that isn't active.
618 */
619 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
620 return BLK_EH_NOT_HANDLED;
621
622 if (!q->mq_ops->timeout)
623 return BLK_EH_RESET_TIMER;
624
625 return q->mq_ops->timeout(rq);
626}
627
320ae51f
JA
628static void blk_mq_rq_timer(unsigned long data)
629{
630 struct request_queue *q = (struct request_queue *) data;
631 struct blk_mq_hw_ctx *hctx;
632 unsigned long next = 0;
633 int i, next_set = 0;
634
484b4061
JA
635 queue_for_each_hw_ctx(q, hctx, i) {
636 /*
637 * If not software queues are currently mapped to this
638 * hardware queue, there's nothing to check
639 */
640 if (!hctx->nr_ctx || !hctx->tags)
641 continue;
642
320ae51f 643 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
484b4061 644 }
320ae51f 645
0d2602ca
JA
646 if (next_set) {
647 next = blk_rq_timeout(round_jiffies_up(next));
648 mod_timer(&q->timeout, next);
649 } else {
650 queue_for_each_hw_ctx(q, hctx, i)
651 blk_mq_tag_idle(hctx);
652 }
320ae51f
JA
653}
654
655/*
656 * Reverse check our software queue for entries that we could potentially
657 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
658 * too much time checking for merges.
659 */
660static bool blk_mq_attempt_merge(struct request_queue *q,
661 struct blk_mq_ctx *ctx, struct bio *bio)
662{
663 struct request *rq;
664 int checked = 8;
665
666 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
667 int el_ret;
668
669 if (!checked--)
670 break;
671
672 if (!blk_rq_merge_ok(rq, bio))
673 continue;
674
675 el_ret = blk_try_merge(rq, bio);
676 if (el_ret == ELEVATOR_BACK_MERGE) {
677 if (bio_attempt_back_merge(q, rq, bio)) {
678 ctx->rq_merged++;
679 return true;
680 }
681 break;
682 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
683 if (bio_attempt_front_merge(q, rq, bio)) {
684 ctx->rq_merged++;
685 return true;
686 }
687 break;
688 }
689 }
690
691 return false;
692}
693
1429d7c9
JA
694/*
695 * Process software queues that have been marked busy, splicing them
696 * to the for-dispatch
697 */
698static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
699{
700 struct blk_mq_ctx *ctx;
701 int i;
702
703 for (i = 0; i < hctx->ctx_map.map_size; i++) {
704 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
705 unsigned int off, bit;
706
707 if (!bm->word)
708 continue;
709
710 bit = 0;
711 off = i * hctx->ctx_map.bits_per_word;
712 do {
713 bit = find_next_bit(&bm->word, bm->depth, bit);
714 if (bit >= bm->depth)
715 break;
716
717 ctx = hctx->ctxs[bit + off];
718 clear_bit(bit, &bm->word);
719 spin_lock(&ctx->lock);
720 list_splice_tail_init(&ctx->rq_list, list);
721 spin_unlock(&ctx->lock);
722
723 bit++;
724 } while (1);
725 }
726}
727
320ae51f
JA
728/*
729 * Run this hardware queue, pulling any software queues mapped to it in.
730 * Note that this function currently has various problems around ordering
731 * of IO. In particular, we'd like FIFO behaviour on handling existing
732 * items on the hctx->dispatch list. Ignore that for now.
733 */
734static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
735{
736 struct request_queue *q = hctx->queue;
320ae51f
JA
737 struct request *rq;
738 LIST_HEAD(rq_list);
1429d7c9 739 int queued;
320ae51f 740
fd1270d5 741 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
e4043dcf 742
5d12f905 743 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
744 return;
745
746 hctx->run++;
747
748 /*
749 * Touch any software queue that has pending entries.
750 */
1429d7c9 751 flush_busy_ctxs(hctx, &rq_list);
320ae51f
JA
752
753 /*
754 * If we have previous entries on our dispatch list, grab them
755 * and stuff them at the front for more fair dispatch.
756 */
757 if (!list_empty_careful(&hctx->dispatch)) {
758 spin_lock(&hctx->lock);
759 if (!list_empty(&hctx->dispatch))
760 list_splice_init(&hctx->dispatch, &rq_list);
761 spin_unlock(&hctx->lock);
762 }
763
320ae51f
JA
764 /*
765 * Now process all the entries, sending them to the driver.
766 */
1429d7c9 767 queued = 0;
320ae51f
JA
768 while (!list_empty(&rq_list)) {
769 int ret;
770
771 rq = list_first_entry(&rq_list, struct request, queuelist);
772 list_del_init(&rq->queuelist);
320ae51f 773
49f5baa5 774 blk_mq_start_request(rq, list_empty(&rq_list));
320ae51f
JA
775
776 ret = q->mq_ops->queue_rq(hctx, rq);
777 switch (ret) {
778 case BLK_MQ_RQ_QUEUE_OK:
779 queued++;
780 continue;
781 case BLK_MQ_RQ_QUEUE_BUSY:
320ae51f 782 list_add(&rq->queuelist, &rq_list);
ed0791b2 783 __blk_mq_requeue_request(rq);
320ae51f
JA
784 break;
785 default:
786 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 787 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 788 rq->errors = -EIO;
320ae51f
JA
789 blk_mq_end_io(rq, rq->errors);
790 break;
791 }
792
793 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
794 break;
795 }
796
797 if (!queued)
798 hctx->dispatched[0]++;
799 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
800 hctx->dispatched[ilog2(queued) + 1]++;
801
802 /*
803 * Any items that need requeuing? Stuff them into hctx->dispatch,
804 * that is where we will continue on next queue run.
805 */
806 if (!list_empty(&rq_list)) {
807 spin_lock(&hctx->lock);
808 list_splice(&rq_list, &hctx->dispatch);
809 spin_unlock(&hctx->lock);
810 }
811}
812
506e931f
JA
813/*
814 * It'd be great if the workqueue API had a way to pass
815 * in a mask and had some smarts for more clever placement.
816 * For now we just round-robin here, switching for every
817 * BLK_MQ_CPU_WORK_BATCH queued items.
818 */
819static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
820{
821 int cpu = hctx->next_cpu;
822
823 if (--hctx->next_cpu_batch <= 0) {
824 int next_cpu;
825
826 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
827 if (next_cpu >= nr_cpu_ids)
828 next_cpu = cpumask_first(hctx->cpumask);
829
830 hctx->next_cpu = next_cpu;
831 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
832 }
833
834 return cpu;
835}
836
320ae51f
JA
837void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
838{
5d12f905 839 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
840 return;
841
e4043dcf 842 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
320ae51f 843 __blk_mq_run_hw_queue(hctx);
e4043dcf 844 else if (hctx->queue->nr_hw_queues == 1)
70f4db63 845 kblockd_schedule_delayed_work(&hctx->run_work, 0);
e4043dcf
JA
846 else {
847 unsigned int cpu;
848
506e931f 849 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63 850 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
e4043dcf 851 }
320ae51f
JA
852}
853
854void blk_mq_run_queues(struct request_queue *q, bool async)
855{
856 struct blk_mq_hw_ctx *hctx;
857 int i;
858
859 queue_for_each_hw_ctx(q, hctx, i) {
860 if ((!blk_mq_hctx_has_pending(hctx) &&
861 list_empty_careful(&hctx->dispatch)) ||
5d12f905 862 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
863 continue;
864
e4043dcf 865 preempt_disable();
320ae51f 866 blk_mq_run_hw_queue(hctx, async);
e4043dcf 867 preempt_enable();
320ae51f
JA
868 }
869}
870EXPORT_SYMBOL(blk_mq_run_queues);
871
872void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
873{
70f4db63
CH
874 cancel_delayed_work(&hctx->run_work);
875 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
876 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
877}
878EXPORT_SYMBOL(blk_mq_stop_hw_queue);
879
280d45f6
CH
880void blk_mq_stop_hw_queues(struct request_queue *q)
881{
882 struct blk_mq_hw_ctx *hctx;
883 int i;
884
885 queue_for_each_hw_ctx(q, hctx, i)
886 blk_mq_stop_hw_queue(hctx);
887}
888EXPORT_SYMBOL(blk_mq_stop_hw_queues);
889
320ae51f
JA
890void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
891{
892 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf
JA
893
894 preempt_disable();
320ae51f 895 __blk_mq_run_hw_queue(hctx);
e4043dcf 896 preempt_enable();
320ae51f
JA
897}
898EXPORT_SYMBOL(blk_mq_start_hw_queue);
899
2f268556
CH
900void blk_mq_start_hw_queues(struct request_queue *q)
901{
902 struct blk_mq_hw_ctx *hctx;
903 int i;
904
905 queue_for_each_hw_ctx(q, hctx, i)
906 blk_mq_start_hw_queue(hctx);
907}
908EXPORT_SYMBOL(blk_mq_start_hw_queues);
909
910
1b4a3258 911void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
912{
913 struct blk_mq_hw_ctx *hctx;
914 int i;
915
916 queue_for_each_hw_ctx(q, hctx, i) {
917 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
918 continue;
919
920 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 921 preempt_disable();
1b4a3258 922 blk_mq_run_hw_queue(hctx, async);
e4043dcf 923 preempt_enable();
320ae51f
JA
924 }
925}
926EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
927
70f4db63 928static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
929{
930 struct blk_mq_hw_ctx *hctx;
931
70f4db63 932 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
e4043dcf 933
320ae51f
JA
934 __blk_mq_run_hw_queue(hctx);
935}
936
70f4db63
CH
937static void blk_mq_delay_work_fn(struct work_struct *work)
938{
939 struct blk_mq_hw_ctx *hctx;
940
941 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
942
943 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
944 __blk_mq_run_hw_queue(hctx);
945}
946
947void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
948{
949 unsigned long tmo = msecs_to_jiffies(msecs);
950
951 if (hctx->queue->nr_hw_queues == 1)
952 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
953 else {
954 unsigned int cpu;
955
506e931f 956 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63
CH
957 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
958 }
959}
960EXPORT_SYMBOL(blk_mq_delay_queue);
961
320ae51f 962static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
72a0a36e 963 struct request *rq, bool at_head)
320ae51f
JA
964{
965 struct blk_mq_ctx *ctx = rq->mq_ctx;
966
01b983c9
JA
967 trace_block_rq_insert(hctx->queue, rq);
968
72a0a36e
CH
969 if (at_head)
970 list_add(&rq->queuelist, &ctx->rq_list);
971 else
972 list_add_tail(&rq->queuelist, &ctx->rq_list);
4bb659b1 973
320ae51f
JA
974 blk_mq_hctx_mark_pending(hctx, ctx);
975
976 /*
977 * We do this early, to ensure we are on the right CPU.
978 */
87ee7b11 979 blk_add_timer(rq);
320ae51f
JA
980}
981
eeabc850
CH
982void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
983 bool async)
320ae51f 984{
eeabc850 985 struct request_queue *q = rq->q;
320ae51f 986 struct blk_mq_hw_ctx *hctx;
eeabc850
CH
987 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
988
989 current_ctx = blk_mq_get_ctx(q);
990 if (!cpu_online(ctx->cpu))
991 rq->mq_ctx = ctx = current_ctx;
320ae51f 992
320ae51f
JA
993 hctx = q->mq_ops->map_queue(q, ctx->cpu);
994
eeabc850
CH
995 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
996 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
320ae51f
JA
997 blk_insert_flush(rq);
998 } else {
320ae51f 999 spin_lock(&ctx->lock);
72a0a36e 1000 __blk_mq_insert_request(hctx, rq, at_head);
320ae51f 1001 spin_unlock(&ctx->lock);
320ae51f
JA
1002 }
1003
320ae51f
JA
1004 if (run_queue)
1005 blk_mq_run_hw_queue(hctx, async);
e4043dcf
JA
1006
1007 blk_mq_put_ctx(current_ctx);
320ae51f
JA
1008}
1009
1010static void blk_mq_insert_requests(struct request_queue *q,
1011 struct blk_mq_ctx *ctx,
1012 struct list_head *list,
1013 int depth,
1014 bool from_schedule)
1015
1016{
1017 struct blk_mq_hw_ctx *hctx;
1018 struct blk_mq_ctx *current_ctx;
1019
1020 trace_block_unplug(q, depth, !from_schedule);
1021
1022 current_ctx = blk_mq_get_ctx(q);
1023
1024 if (!cpu_online(ctx->cpu))
1025 ctx = current_ctx;
1026 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1027
1028 /*
1029 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1030 * offline now
1031 */
1032 spin_lock(&ctx->lock);
1033 while (!list_empty(list)) {
1034 struct request *rq;
1035
1036 rq = list_first_entry(list, struct request, queuelist);
1037 list_del_init(&rq->queuelist);
1038 rq->mq_ctx = ctx;
72a0a36e 1039 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
1040 }
1041 spin_unlock(&ctx->lock);
1042
320ae51f 1043 blk_mq_run_hw_queue(hctx, from_schedule);
e4043dcf 1044 blk_mq_put_ctx(current_ctx);
320ae51f
JA
1045}
1046
1047static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1048{
1049 struct request *rqa = container_of(a, struct request, queuelist);
1050 struct request *rqb = container_of(b, struct request, queuelist);
1051
1052 return !(rqa->mq_ctx < rqb->mq_ctx ||
1053 (rqa->mq_ctx == rqb->mq_ctx &&
1054 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1055}
1056
1057void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1058{
1059 struct blk_mq_ctx *this_ctx;
1060 struct request_queue *this_q;
1061 struct request *rq;
1062 LIST_HEAD(list);
1063 LIST_HEAD(ctx_list);
1064 unsigned int depth;
1065
1066 list_splice_init(&plug->mq_list, &list);
1067
1068 list_sort(NULL, &list, plug_ctx_cmp);
1069
1070 this_q = NULL;
1071 this_ctx = NULL;
1072 depth = 0;
1073
1074 while (!list_empty(&list)) {
1075 rq = list_entry_rq(list.next);
1076 list_del_init(&rq->queuelist);
1077 BUG_ON(!rq->q);
1078 if (rq->mq_ctx != this_ctx) {
1079 if (this_ctx) {
1080 blk_mq_insert_requests(this_q, this_ctx,
1081 &ctx_list, depth,
1082 from_schedule);
1083 }
1084
1085 this_ctx = rq->mq_ctx;
1086 this_q = rq->q;
1087 depth = 0;
1088 }
1089
1090 depth++;
1091 list_add_tail(&rq->queuelist, &ctx_list);
1092 }
1093
1094 /*
1095 * If 'this_ctx' is set, we know we have entries to complete
1096 * on 'ctx_list'. Do those.
1097 */
1098 if (this_ctx) {
1099 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1100 from_schedule);
1101 }
1102}
1103
1104static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1105{
1106 init_request_from_bio(rq, bio);
4b570521 1107
3ee32372 1108 if (blk_do_io_stat(rq))
4b570521 1109 blk_account_io_start(rq, 1);
320ae51f
JA
1110}
1111
07068d5b
JA
1112static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1113 struct blk_mq_ctx *ctx,
1114 struct request *rq, struct bio *bio)
320ae51f 1115{
07068d5b 1116 struct request_queue *q = hctx->queue;
320ae51f 1117
07068d5b
JA
1118 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1119 blk_mq_bio_to_request(rq, bio);
1120 spin_lock(&ctx->lock);
1121insert_rq:
1122 __blk_mq_insert_request(hctx, rq, false);
1123 spin_unlock(&ctx->lock);
1124 return false;
1125 } else {
1126 spin_lock(&ctx->lock);
1127 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1128 blk_mq_bio_to_request(rq, bio);
1129 goto insert_rq;
1130 }
320ae51f 1131
07068d5b
JA
1132 spin_unlock(&ctx->lock);
1133 __blk_mq_free_request(hctx, ctx, rq);
1134 return true;
14ec77f3 1135 }
07068d5b 1136}
14ec77f3 1137
07068d5b
JA
1138struct blk_map_ctx {
1139 struct blk_mq_hw_ctx *hctx;
1140 struct blk_mq_ctx *ctx;
1141};
1142
1143static struct request *blk_mq_map_request(struct request_queue *q,
1144 struct bio *bio,
1145 struct blk_map_ctx *data)
1146{
1147 struct blk_mq_hw_ctx *hctx;
1148 struct blk_mq_ctx *ctx;
1149 struct request *rq;
1150 int rw = bio_data_dir(bio);
cb96a42c 1151 struct blk_mq_alloc_data alloc_data;
320ae51f 1152
07068d5b 1153 if (unlikely(blk_mq_queue_enter(q))) {
320ae51f 1154 bio_endio(bio, -EIO);
07068d5b 1155 return NULL;
320ae51f
JA
1156 }
1157
1158 ctx = blk_mq_get_ctx(q);
1159 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1160
07068d5b 1161 if (rw_is_sync(bio->bi_rw))
27fbf4e8 1162 rw |= REQ_SYNC;
07068d5b 1163
320ae51f 1164 trace_block_getrq(q, bio, rw);
cb96a42c
ML
1165 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1166 hctx);
1167 rq = __blk_mq_alloc_request(&alloc_data, rw);
5dee8577 1168 if (unlikely(!rq)) {
793597a6 1169 __blk_mq_run_hw_queue(hctx);
320ae51f
JA
1170 blk_mq_put_ctx(ctx);
1171 trace_block_sleeprq(q, bio, rw);
793597a6
CH
1172
1173 ctx = blk_mq_get_ctx(q);
320ae51f 1174 hctx = q->mq_ops->map_queue(q, ctx->cpu);
cb96a42c
ML
1175 blk_mq_set_alloc_data(&alloc_data, q,
1176 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1177 rq = __blk_mq_alloc_request(&alloc_data, rw);
1178 ctx = alloc_data.ctx;
1179 hctx = alloc_data.hctx;
320ae51f
JA
1180 }
1181
1182 hctx->queued++;
07068d5b
JA
1183 data->hctx = hctx;
1184 data->ctx = ctx;
1185 return rq;
1186}
1187
1188/*
1189 * Multiple hardware queue variant. This will not use per-process plugs,
1190 * but will attempt to bypass the hctx queueing if we can go straight to
1191 * hardware for SYNC IO.
1192 */
1193static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1194{
1195 const int is_sync = rw_is_sync(bio->bi_rw);
1196 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1197 struct blk_map_ctx data;
1198 struct request *rq;
1199
1200 blk_queue_bounce(q, &bio);
1201
1202 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1203 bio_endio(bio, -EIO);
1204 return;
1205 }
1206
1207 rq = blk_mq_map_request(q, bio, &data);
1208 if (unlikely(!rq))
1209 return;
1210
1211 if (unlikely(is_flush_fua)) {
1212 blk_mq_bio_to_request(rq, bio);
1213 blk_insert_flush(rq);
1214 goto run_queue;
1215 }
1216
1217 if (is_sync) {
1218 int ret;
1219
1220 blk_mq_bio_to_request(rq, bio);
1221 blk_mq_start_request(rq, true);
feff6894 1222 blk_add_timer(rq);
07068d5b
JA
1223
1224 /*
1225 * For OK queue, we are done. For error, kill it. Any other
1226 * error (busy), just add it to our list as we previously
1227 * would have done
1228 */
1229 ret = q->mq_ops->queue_rq(data.hctx, rq);
1230 if (ret == BLK_MQ_RQ_QUEUE_OK)
1231 goto done;
1232 else {
1233 __blk_mq_requeue_request(rq);
1234
1235 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1236 rq->errors = -EIO;
1237 blk_mq_end_io(rq, rq->errors);
1238 goto done;
1239 }
1240 }
1241 }
1242
1243 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1244 /*
1245 * For a SYNC request, send it to the hardware immediately. For
1246 * an ASYNC request, just ensure that we run it later on. The
1247 * latter allows for merging opportunities and more efficient
1248 * dispatching.
1249 */
1250run_queue:
1251 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1252 }
1253done:
1254 blk_mq_put_ctx(data.ctx);
1255}
1256
1257/*
1258 * Single hardware queue variant. This will attempt to use any per-process
1259 * plug for merging and IO deferral.
1260 */
1261static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1262{
1263 const int is_sync = rw_is_sync(bio->bi_rw);
1264 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1265 unsigned int use_plug, request_count = 0;
1266 struct blk_map_ctx data;
1267 struct request *rq;
1268
1269 /*
1270 * If we have multiple hardware queues, just go directly to
1271 * one of those for sync IO.
1272 */
1273 use_plug = !is_flush_fua && !is_sync;
1274
1275 blk_queue_bounce(q, &bio);
1276
1277 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1278 bio_endio(bio, -EIO);
1279 return;
1280 }
1281
1282 if (use_plug && !blk_queue_nomerges(q) &&
1283 blk_attempt_plug_merge(q, bio, &request_count))
1284 return;
1285
1286 rq = blk_mq_map_request(q, bio, &data);
ff87bcec
JA
1287 if (unlikely(!rq))
1288 return;
320ae51f
JA
1289
1290 if (unlikely(is_flush_fua)) {
1291 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
1292 blk_insert_flush(rq);
1293 goto run_queue;
1294 }
1295
1296 /*
1297 * A task plug currently exists. Since this is completely lockless,
1298 * utilize that to temporarily store requests until the task is
1299 * either done or scheduled away.
1300 */
1301 if (use_plug) {
1302 struct blk_plug *plug = current->plug;
1303
1304 if (plug) {
1305 blk_mq_bio_to_request(rq, bio);
92f399c7 1306 if (list_empty(&plug->mq_list))
320ae51f
JA
1307 trace_block_plug(q);
1308 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1309 blk_flush_plug_list(plug, false);
1310 trace_block_plug(q);
1311 }
1312 list_add_tail(&rq->queuelist, &plug->mq_list);
07068d5b 1313 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1314 return;
1315 }
1316 }
1317
07068d5b
JA
1318 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1319 /*
1320 * For a SYNC request, send it to the hardware immediately. For
1321 * an ASYNC request, just ensure that we run it later on. The
1322 * latter allows for merging opportunities and more efficient
1323 * dispatching.
1324 */
1325run_queue:
1326 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1327 }
1328
07068d5b 1329 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1330}
1331
1332/*
1333 * Default mapping to a software queue, since we use one per CPU.
1334 */
1335struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1336{
1337 return q->queue_hw_ctx[q->mq_map[cpu]];
1338}
1339EXPORT_SYMBOL(blk_mq_map_queue);
1340
24d2f903
CH
1341static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1342 struct blk_mq_tags *tags, unsigned int hctx_idx)
95363efd 1343{
e9b267d9 1344 struct page *page;
320ae51f 1345
24d2f903 1346 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1347 int i;
320ae51f 1348
24d2f903
CH
1349 for (i = 0; i < tags->nr_tags; i++) {
1350 if (!tags->rqs[i])
e9b267d9 1351 continue;
24d2f903
CH
1352 set->ops->exit_request(set->driver_data, tags->rqs[i],
1353 hctx_idx, i);
e9b267d9 1354 }
320ae51f 1355 }
320ae51f 1356
24d2f903
CH
1357 while (!list_empty(&tags->page_list)) {
1358 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1359 list_del_init(&page->lru);
320ae51f
JA
1360 __free_pages(page, page->private);
1361 }
1362
24d2f903 1363 kfree(tags->rqs);
320ae51f 1364
24d2f903 1365 blk_mq_free_tags(tags);
320ae51f
JA
1366}
1367
1368static size_t order_to_size(unsigned int order)
1369{
4ca08500 1370 return (size_t)PAGE_SIZE << order;
320ae51f
JA
1371}
1372
24d2f903
CH
1373static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1374 unsigned int hctx_idx)
320ae51f 1375{
24d2f903 1376 struct blk_mq_tags *tags;
320ae51f
JA
1377 unsigned int i, j, entries_per_page, max_order = 4;
1378 size_t rq_size, left;
1379
24d2f903
CH
1380 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1381 set->numa_node);
1382 if (!tags)
1383 return NULL;
320ae51f 1384
24d2f903
CH
1385 INIT_LIST_HEAD(&tags->page_list);
1386
1387 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1388 GFP_KERNEL, set->numa_node);
1389 if (!tags->rqs) {
1390 blk_mq_free_tags(tags);
1391 return NULL;
1392 }
320ae51f
JA
1393
1394 /*
1395 * rq_size is the size of the request plus driver payload, rounded
1396 * to the cacheline size
1397 */
24d2f903 1398 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1399 cache_line_size());
24d2f903 1400 left = rq_size * set->queue_depth;
320ae51f 1401
24d2f903 1402 for (i = 0; i < set->queue_depth; ) {
320ae51f
JA
1403 int this_order = max_order;
1404 struct page *page;
1405 int to_do;
1406 void *p;
1407
1408 while (left < order_to_size(this_order - 1) && this_order)
1409 this_order--;
1410
1411 do {
24d2f903
CH
1412 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1413 this_order);
320ae51f
JA
1414 if (page)
1415 break;
1416 if (!this_order--)
1417 break;
1418 if (order_to_size(this_order) < rq_size)
1419 break;
1420 } while (1);
1421
1422 if (!page)
24d2f903 1423 goto fail;
320ae51f
JA
1424
1425 page->private = this_order;
24d2f903 1426 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1427
1428 p = page_address(page);
1429 entries_per_page = order_to_size(this_order) / rq_size;
24d2f903 1430 to_do = min(entries_per_page, set->queue_depth - i);
320ae51f
JA
1431 left -= to_do * rq_size;
1432 for (j = 0; j < to_do; j++) {
24d2f903
CH
1433 tags->rqs[i] = p;
1434 if (set->ops->init_request) {
1435 if (set->ops->init_request(set->driver_data,
1436 tags->rqs[i], hctx_idx, i,
1437 set->numa_node))
1438 goto fail;
e9b267d9
CH
1439 }
1440
320ae51f
JA
1441 p += rq_size;
1442 i++;
1443 }
1444 }
1445
24d2f903 1446 return tags;
320ae51f 1447
24d2f903
CH
1448fail:
1449 pr_warn("%s: failed to allocate requests\n", __func__);
1450 blk_mq_free_rq_map(set, tags, hctx_idx);
1451 return NULL;
320ae51f
JA
1452}
1453
1429d7c9
JA
1454static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1455{
1456 kfree(bitmap->map);
1457}
1458
1459static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1460{
1461 unsigned int bpw = 8, total, num_maps, i;
1462
1463 bitmap->bits_per_word = bpw;
1464
1465 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1466 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1467 GFP_KERNEL, node);
1468 if (!bitmap->map)
1469 return -ENOMEM;
1470
1471 bitmap->map_size = num_maps;
1472
1473 total = nr_cpu_ids;
1474 for (i = 0; i < num_maps; i++) {
1475 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1476 total -= bitmap->map[i].depth;
1477 }
1478
1479 return 0;
1480}
1481
484b4061
JA
1482static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1483{
1484 struct request_queue *q = hctx->queue;
1485 struct blk_mq_ctx *ctx;
1486 LIST_HEAD(tmp);
1487
1488 /*
1489 * Move ctx entries to new CPU, if this one is going away.
1490 */
1491 ctx = __blk_mq_get_ctx(q, cpu);
1492
1493 spin_lock(&ctx->lock);
1494 if (!list_empty(&ctx->rq_list)) {
1495 list_splice_init(&ctx->rq_list, &tmp);
1496 blk_mq_hctx_clear_pending(hctx, ctx);
1497 }
1498 spin_unlock(&ctx->lock);
1499
1500 if (list_empty(&tmp))
1501 return NOTIFY_OK;
1502
1503 ctx = blk_mq_get_ctx(q);
1504 spin_lock(&ctx->lock);
1505
1506 while (!list_empty(&tmp)) {
1507 struct request *rq;
1508
1509 rq = list_first_entry(&tmp, struct request, queuelist);
1510 rq->mq_ctx = ctx;
1511 list_move_tail(&rq->queuelist, &ctx->rq_list);
1512 }
1513
1514 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1515 blk_mq_hctx_mark_pending(hctx, ctx);
1516
1517 spin_unlock(&ctx->lock);
1518
1519 blk_mq_run_hw_queue(hctx, true);
1520 blk_mq_put_ctx(ctx);
1521 return NOTIFY_OK;
1522}
1523
1524static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1525{
1526 struct request_queue *q = hctx->queue;
1527 struct blk_mq_tag_set *set = q->tag_set;
1528
1529 if (set->tags[hctx->queue_num])
1530 return NOTIFY_OK;
1531
1532 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1533 if (!set->tags[hctx->queue_num])
1534 return NOTIFY_STOP;
1535
1536 hctx->tags = set->tags[hctx->queue_num];
1537 return NOTIFY_OK;
1538}
1539
1540static int blk_mq_hctx_notify(void *data, unsigned long action,
1541 unsigned int cpu)
1542{
1543 struct blk_mq_hw_ctx *hctx = data;
1544
1545 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1546 return blk_mq_hctx_cpu_offline(hctx, cpu);
1547 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1548 return blk_mq_hctx_cpu_online(hctx, cpu);
1549
1550 return NOTIFY_OK;
1551}
1552
624dbe47
ML
1553static void blk_mq_exit_hw_queues(struct request_queue *q,
1554 struct blk_mq_tag_set *set, int nr_queue)
1555{
1556 struct blk_mq_hw_ctx *hctx;
1557 unsigned int i;
1558
1559 queue_for_each_hw_ctx(q, hctx, i) {
1560 if (i == nr_queue)
1561 break;
1562
f899fed4
JA
1563 blk_mq_tag_idle(hctx);
1564
624dbe47
ML
1565 if (set->ops->exit_hctx)
1566 set->ops->exit_hctx(hctx, i);
1567
1568 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1569 kfree(hctx->ctxs);
1570 blk_mq_free_bitmap(&hctx->ctx_map);
1571 }
1572
1573}
1574
1575static void blk_mq_free_hw_queues(struct request_queue *q,
1576 struct blk_mq_tag_set *set)
1577{
1578 struct blk_mq_hw_ctx *hctx;
1579 unsigned int i;
1580
1581 queue_for_each_hw_ctx(q, hctx, i) {
1582 free_cpumask_var(hctx->cpumask);
cdef54dd 1583 kfree(hctx);
624dbe47
ML
1584 }
1585}
1586
320ae51f 1587static int blk_mq_init_hw_queues(struct request_queue *q,
24d2f903 1588 struct blk_mq_tag_set *set)
320ae51f
JA
1589{
1590 struct blk_mq_hw_ctx *hctx;
624dbe47 1591 unsigned int i;
320ae51f
JA
1592
1593 /*
1594 * Initialize hardware queues
1595 */
1596 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1597 int node;
1598
1599 node = hctx->numa_node;
1600 if (node == NUMA_NO_NODE)
24d2f903 1601 node = hctx->numa_node = set->numa_node;
320ae51f 1602
70f4db63
CH
1603 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1604 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
320ae51f
JA
1605 spin_lock_init(&hctx->lock);
1606 INIT_LIST_HEAD(&hctx->dispatch);
1607 hctx->queue = q;
1608 hctx->queue_num = i;
24d2f903
CH
1609 hctx->flags = set->flags;
1610 hctx->cmd_size = set->cmd_size;
320ae51f
JA
1611
1612 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1613 blk_mq_hctx_notify, hctx);
1614 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1615
24d2f903 1616 hctx->tags = set->tags[i];
320ae51f
JA
1617
1618 /*
1619 * Allocate space for all possible cpus to avoid allocation in
1620 * runtime
1621 */
1622 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1623 GFP_KERNEL, node);
1624 if (!hctx->ctxs)
1625 break;
1626
1429d7c9 1627 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
320ae51f
JA
1628 break;
1629
320ae51f
JA
1630 hctx->nr_ctx = 0;
1631
24d2f903
CH
1632 if (set->ops->init_hctx &&
1633 set->ops->init_hctx(hctx, set->driver_data, i))
320ae51f
JA
1634 break;
1635 }
1636
1637 if (i == q->nr_hw_queues)
1638 return 0;
1639
1640 /*
1641 * Init failed
1642 */
624dbe47 1643 blk_mq_exit_hw_queues(q, set, i);
320ae51f
JA
1644
1645 return 1;
1646}
1647
1648static void blk_mq_init_cpu_queues(struct request_queue *q,
1649 unsigned int nr_hw_queues)
1650{
1651 unsigned int i;
1652
1653 for_each_possible_cpu(i) {
1654 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1655 struct blk_mq_hw_ctx *hctx;
1656
1657 memset(__ctx, 0, sizeof(*__ctx));
1658 __ctx->cpu = i;
1659 spin_lock_init(&__ctx->lock);
1660 INIT_LIST_HEAD(&__ctx->rq_list);
1661 __ctx->queue = q;
1662
1663 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1664 if (!cpu_online(i))
1665 continue;
1666
e4043dcf
JA
1667 hctx = q->mq_ops->map_queue(q, i);
1668 cpumask_set_cpu(i, hctx->cpumask);
1669 hctx->nr_ctx++;
1670
320ae51f
JA
1671 /*
1672 * Set local node, IFF we have more than one hw queue. If
1673 * not, we remain on the home node of the device
1674 */
1675 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1676 hctx->numa_node = cpu_to_node(i);
1677 }
1678}
1679
1680static void blk_mq_map_swqueue(struct request_queue *q)
1681{
1682 unsigned int i;
1683 struct blk_mq_hw_ctx *hctx;
1684 struct blk_mq_ctx *ctx;
1685
1686 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1687 cpumask_clear(hctx->cpumask);
320ae51f
JA
1688 hctx->nr_ctx = 0;
1689 }
1690
1691 /*
1692 * Map software to hardware queues
1693 */
1694 queue_for_each_ctx(q, ctx, i) {
1695 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1696 if (!cpu_online(i))
1697 continue;
1698
320ae51f 1699 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1700 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1701 ctx->index_hw = hctx->nr_ctx;
1702 hctx->ctxs[hctx->nr_ctx++] = ctx;
1703 }
506e931f
JA
1704
1705 queue_for_each_hw_ctx(q, hctx, i) {
484b4061
JA
1706 /*
1707 * If not software queues are mapped to this hardware queue,
1708 * disable it and free the request entries
1709 */
1710 if (!hctx->nr_ctx) {
1711 struct blk_mq_tag_set *set = q->tag_set;
1712
1713 if (set->tags[i]) {
1714 blk_mq_free_rq_map(set, set->tags[i], i);
1715 set->tags[i] = NULL;
1716 hctx->tags = NULL;
1717 }
1718 continue;
1719 }
1720
1721 /*
1722 * Initialize batch roundrobin counts
1723 */
506e931f
JA
1724 hctx->next_cpu = cpumask_first(hctx->cpumask);
1725 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1726 }
320ae51f
JA
1727}
1728
0d2602ca
JA
1729static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1730{
1731 struct blk_mq_hw_ctx *hctx;
1732 struct request_queue *q;
1733 bool shared;
1734 int i;
1735
1736 if (set->tag_list.next == set->tag_list.prev)
1737 shared = false;
1738 else
1739 shared = true;
1740
1741 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1742 blk_mq_freeze_queue(q);
1743
1744 queue_for_each_hw_ctx(q, hctx, i) {
1745 if (shared)
1746 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1747 else
1748 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1749 }
1750 blk_mq_unfreeze_queue(q);
1751 }
1752}
1753
1754static void blk_mq_del_queue_tag_set(struct request_queue *q)
1755{
1756 struct blk_mq_tag_set *set = q->tag_set;
1757
1758 blk_mq_freeze_queue(q);
1759
1760 mutex_lock(&set->tag_list_lock);
1761 list_del_init(&q->tag_set_list);
1762 blk_mq_update_tag_set_depth(set);
1763 mutex_unlock(&set->tag_list_lock);
1764
1765 blk_mq_unfreeze_queue(q);
1766}
1767
1768static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1769 struct request_queue *q)
1770{
1771 q->tag_set = set;
1772
1773 mutex_lock(&set->tag_list_lock);
1774 list_add_tail(&q->tag_set_list, &set->tag_list);
1775 blk_mq_update_tag_set_depth(set);
1776 mutex_unlock(&set->tag_list_lock);
1777}
1778
24d2f903 1779struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
320ae51f
JA
1780{
1781 struct blk_mq_hw_ctx **hctxs;
e6cdb092 1782 struct blk_mq_ctx __percpu *ctx;
320ae51f 1783 struct request_queue *q;
f14bbe77 1784 unsigned int *map;
320ae51f
JA
1785 int i;
1786
320ae51f
JA
1787 ctx = alloc_percpu(struct blk_mq_ctx);
1788 if (!ctx)
1789 return ERR_PTR(-ENOMEM);
1790
24d2f903
CH
1791 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1792 set->numa_node);
320ae51f
JA
1793
1794 if (!hctxs)
1795 goto err_percpu;
1796
f14bbe77
JA
1797 map = blk_mq_make_queue_map(set);
1798 if (!map)
1799 goto err_map;
1800
24d2f903 1801 for (i = 0; i < set->nr_hw_queues; i++) {
f14bbe77
JA
1802 int node = blk_mq_hw_queue_to_node(map, i);
1803
cdef54dd
CH
1804 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1805 GFP_KERNEL, node);
320ae51f
JA
1806 if (!hctxs[i])
1807 goto err_hctxs;
1808
e4043dcf
JA
1809 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1810 goto err_hctxs;
1811
0d2602ca 1812 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 1813 hctxs[i]->numa_node = node;
320ae51f
JA
1814 hctxs[i]->queue_num = i;
1815 }
1816
24d2f903 1817 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
320ae51f
JA
1818 if (!q)
1819 goto err_hctxs;
1820
3d2936f4
ML
1821 if (percpu_counter_init(&q->mq_usage_counter, 0))
1822 goto err_map;
1823
320ae51f
JA
1824 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1825 blk_queue_rq_timeout(q, 30000);
1826
1827 q->nr_queues = nr_cpu_ids;
24d2f903 1828 q->nr_hw_queues = set->nr_hw_queues;
f14bbe77 1829 q->mq_map = map;
320ae51f
JA
1830
1831 q->queue_ctx = ctx;
1832 q->queue_hw_ctx = hctxs;
1833
24d2f903 1834 q->mq_ops = set->ops;
94eddfbe 1835 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1836
05f1dd53
JA
1837 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1838 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1839
1be036e9
CH
1840 q->sg_reserved_size = INT_MAX;
1841
6fca6a61
CH
1842 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1843 INIT_LIST_HEAD(&q->requeue_list);
1844 spin_lock_init(&q->requeue_lock);
1845
07068d5b
JA
1846 if (q->nr_hw_queues > 1)
1847 blk_queue_make_request(q, blk_mq_make_request);
1848 else
1849 blk_queue_make_request(q, blk_sq_make_request);
1850
87ee7b11 1851 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
24d2f903
CH
1852 if (set->timeout)
1853 blk_queue_rq_timeout(q, set->timeout);
320ae51f 1854
eba71768
JA
1855 /*
1856 * Do this after blk_queue_make_request() overrides it...
1857 */
1858 q->nr_requests = set->queue_depth;
1859
24d2f903
CH
1860 if (set->ops->complete)
1861 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 1862
320ae51f 1863 blk_mq_init_flush(q);
24d2f903 1864 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 1865
24d2f903
CH
1866 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1867 set->cmd_size, cache_line_size()),
1868 GFP_KERNEL);
18741986 1869 if (!q->flush_rq)
320ae51f
JA
1870 goto err_hw;
1871
24d2f903 1872 if (blk_mq_init_hw_queues(q, set))
18741986
CH
1873 goto err_flush_rq;
1874
320ae51f
JA
1875 mutex_lock(&all_q_mutex);
1876 list_add_tail(&q->all_q_node, &all_q_list);
1877 mutex_unlock(&all_q_mutex);
1878
0d2602ca
JA
1879 blk_mq_add_queue_tag_set(set, q);
1880
484b4061
JA
1881 blk_mq_map_swqueue(q);
1882
320ae51f 1883 return q;
18741986
CH
1884
1885err_flush_rq:
1886 kfree(q->flush_rq);
320ae51f 1887err_hw:
320ae51f
JA
1888 blk_cleanup_queue(q);
1889err_hctxs:
f14bbe77 1890 kfree(map);
24d2f903 1891 for (i = 0; i < set->nr_hw_queues; i++) {
320ae51f
JA
1892 if (!hctxs[i])
1893 break;
e4043dcf 1894 free_cpumask_var(hctxs[i]->cpumask);
cdef54dd 1895 kfree(hctxs[i]);
320ae51f 1896 }
f14bbe77 1897err_map:
320ae51f
JA
1898 kfree(hctxs);
1899err_percpu:
1900 free_percpu(ctx);
1901 return ERR_PTR(-ENOMEM);
1902}
1903EXPORT_SYMBOL(blk_mq_init_queue);
1904
1905void blk_mq_free_queue(struct request_queue *q)
1906{
624dbe47 1907 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 1908
0d2602ca
JA
1909 blk_mq_del_queue_tag_set(q);
1910
624dbe47
ML
1911 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1912 blk_mq_free_hw_queues(q, set);
320ae51f 1913
3d2936f4
ML
1914 percpu_counter_destroy(&q->mq_usage_counter);
1915
320ae51f
JA
1916 free_percpu(q->queue_ctx);
1917 kfree(q->queue_hw_ctx);
1918 kfree(q->mq_map);
1919
1920 q->queue_ctx = NULL;
1921 q->queue_hw_ctx = NULL;
1922 q->mq_map = NULL;
1923
1924 mutex_lock(&all_q_mutex);
1925 list_del_init(&q->all_q_node);
1926 mutex_unlock(&all_q_mutex);
1927}
320ae51f
JA
1928
1929/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1930static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1931{
1932 blk_mq_freeze_queue(q);
1933
67aec14c
JA
1934 blk_mq_sysfs_unregister(q);
1935
320ae51f
JA
1936 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1937
1938 /*
1939 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1940 * we should change hctx numa_node according to new topology (this
1941 * involves free and re-allocate memory, worthy doing?)
1942 */
1943
1944 blk_mq_map_swqueue(q);
1945
67aec14c
JA
1946 blk_mq_sysfs_register(q);
1947
320ae51f
JA
1948 blk_mq_unfreeze_queue(q);
1949}
1950
f618ef7c
PG
1951static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1952 unsigned long action, void *hcpu)
320ae51f
JA
1953{
1954 struct request_queue *q;
1955
1956 /*
9fccfed8
JA
1957 * Before new mappings are established, hotadded cpu might already
1958 * start handling requests. This doesn't break anything as we map
1959 * offline CPUs to first hardware queue. We will re-init the queue
1960 * below to get optimal settings.
320ae51f
JA
1961 */
1962 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1963 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1964 return NOTIFY_OK;
1965
1966 mutex_lock(&all_q_mutex);
1967 list_for_each_entry(q, &all_q_list, all_q_node)
1968 blk_mq_queue_reinit(q);
1969 mutex_unlock(&all_q_mutex);
1970 return NOTIFY_OK;
1971}
1972
a4391c64
JA
1973/*
1974 * Alloc a tag set to be associated with one or more request queues.
1975 * May fail with EINVAL for various error conditions. May adjust the
1976 * requested depth down, if if it too large. In that case, the set
1977 * value will be stored in set->queue_depth.
1978 */
24d2f903
CH
1979int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1980{
1981 int i;
1982
1983 if (!set->nr_hw_queues)
1984 return -EINVAL;
a4391c64 1985 if (!set->queue_depth)
24d2f903
CH
1986 return -EINVAL;
1987 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1988 return -EINVAL;
1989
cdef54dd 1990 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
24d2f903
CH
1991 return -EINVAL;
1992
a4391c64
JA
1993 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
1994 pr_info("blk-mq: reduced tag depth to %u\n",
1995 BLK_MQ_MAX_DEPTH);
1996 set->queue_depth = BLK_MQ_MAX_DEPTH;
1997 }
24d2f903 1998
48479005
ML
1999 set->tags = kmalloc_node(set->nr_hw_queues *
2000 sizeof(struct blk_mq_tags *),
24d2f903
CH
2001 GFP_KERNEL, set->numa_node);
2002 if (!set->tags)
2003 goto out;
2004
2005 for (i = 0; i < set->nr_hw_queues; i++) {
2006 set->tags[i] = blk_mq_init_rq_map(set, i);
2007 if (!set->tags[i])
2008 goto out_unwind;
2009 }
2010
0d2602ca
JA
2011 mutex_init(&set->tag_list_lock);
2012 INIT_LIST_HEAD(&set->tag_list);
2013
24d2f903
CH
2014 return 0;
2015
2016out_unwind:
2017 while (--i >= 0)
2018 blk_mq_free_rq_map(set, set->tags[i], i);
2019out:
2020 return -ENOMEM;
2021}
2022EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2023
2024void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2025{
2026 int i;
2027
484b4061
JA
2028 for (i = 0; i < set->nr_hw_queues; i++) {
2029 if (set->tags[i])
2030 blk_mq_free_rq_map(set, set->tags[i], i);
2031 }
2032
981bd189 2033 kfree(set->tags);
24d2f903
CH
2034}
2035EXPORT_SYMBOL(blk_mq_free_tag_set);
2036
e3a2b3f9
JA
2037int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2038{
2039 struct blk_mq_tag_set *set = q->tag_set;
2040 struct blk_mq_hw_ctx *hctx;
2041 int i, ret;
2042
2043 if (!set || nr > set->queue_depth)
2044 return -EINVAL;
2045
2046 ret = 0;
2047 queue_for_each_hw_ctx(q, hctx, i) {
2048 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2049 if (ret)
2050 break;
2051 }
2052
2053 if (!ret)
2054 q->nr_requests = nr;
2055
2056 return ret;
2057}
2058
676141e4
JA
2059void blk_mq_disable_hotplug(void)
2060{
2061 mutex_lock(&all_q_mutex);
2062}
2063
2064void blk_mq_enable_hotplug(void)
2065{
2066 mutex_unlock(&all_q_mutex);
2067}
2068
320ae51f
JA
2069static int __init blk_mq_init(void)
2070{
320ae51f
JA
2071 blk_mq_cpu_init();
2072
2073 /* Must be called after percpu_counter_hotcpu_callback() */
2074 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2075
2076 return 0;
2077}
2078subsys_initcall(blk_mq_init);