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