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