]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-mq.c
block: remove dead code in scsi_ioctl:blk_verify_command
[mirror_ubuntu-artful-kernel.git] / block / blk-mq.c
CommitLineData
75bb4625
JA
1/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
320ae51f
JA
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/backing-dev.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
12#include <linux/mm.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/workqueue.h>
16#include <linux/smp.h>
17#include <linux/llist.h>
18#include <linux/list_sort.h>
19#include <linux/cpu.h>
20#include <linux/cache.h>
21#include <linux/sched/sysctl.h>
22#include <linux/delay.h>
23
24#include <trace/events/block.h>
25
26#include <linux/blk-mq.h>
27#include "blk.h"
28#include "blk-mq.h"
29#include "blk-mq-tag.h"
30
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
34static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
35
320ae51f
JA
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
24d2f903
CH
544struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
545{
546 return tags->rqs[tag];
547}
548EXPORT_SYMBOL(blk_mq_tag_to_rq);
549
320ae51f
JA
550struct blk_mq_timeout_data {
551 struct blk_mq_hw_ctx *hctx;
552 unsigned long *next;
553 unsigned int *next_set;
554};
555
556static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
557{
558 struct blk_mq_timeout_data *data = __data;
559 struct blk_mq_hw_ctx *hctx = data->hctx;
560 unsigned int tag;
561
562 /* It may not be in flight yet (this is where
563 * the REQ_ATOMIC_STARTED flag comes in). The requests are
564 * statically allocated, so we know it's always safe to access the
565 * memory associated with a bit offset into ->rqs[].
566 */
567 tag = 0;
568 do {
569 struct request *rq;
570
24d2f903
CH
571 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
572 if (tag >= hctx->tags->nr_tags)
320ae51f
JA
573 break;
574
24d2f903
CH
575 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
576 if (rq->q != hctx->queue)
577 continue;
320ae51f
JA
578 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
579 continue;
580
581 blk_rq_check_expired(rq, data->next, data->next_set);
582 } while (1);
583}
584
585static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
586 unsigned long *next,
587 unsigned int *next_set)
588{
589 struct blk_mq_timeout_data data = {
590 .hctx = hctx,
591 .next = next,
592 .next_set = next_set,
593 };
594
595 /*
596 * Ask the tagging code to iterate busy requests, so we can
597 * check them for timeout.
598 */
599 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
600}
601
87ee7b11
JA
602static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
603{
604 struct request_queue *q = rq->q;
605
606 /*
607 * We know that complete is set at this point. If STARTED isn't set
608 * anymore, then the request isn't active and the "timeout" should
609 * just be ignored. This can happen due to the bitflag ordering.
610 * Timeout first checks if STARTED is set, and if it is, assumes
611 * the request is active. But if we race with completion, then
612 * we both flags will get cleared. So check here again, and ignore
613 * a timeout event with a request that isn't active.
614 */
615 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
616 return BLK_EH_NOT_HANDLED;
617
618 if (!q->mq_ops->timeout)
619 return BLK_EH_RESET_TIMER;
620
621 return q->mq_ops->timeout(rq);
622}
623
320ae51f
JA
624static void blk_mq_rq_timer(unsigned long data)
625{
626 struct request_queue *q = (struct request_queue *) data;
627 struct blk_mq_hw_ctx *hctx;
628 unsigned long next = 0;
629 int i, next_set = 0;
630
484b4061
JA
631 queue_for_each_hw_ctx(q, hctx, i) {
632 /*
633 * If not software queues are currently mapped to this
634 * hardware queue, there's nothing to check
635 */
636 if (!hctx->nr_ctx || !hctx->tags)
637 continue;
638
320ae51f 639 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
484b4061 640 }
320ae51f 641
0d2602ca
JA
642 if (next_set) {
643 next = blk_rq_timeout(round_jiffies_up(next));
644 mod_timer(&q->timeout, next);
645 } else {
646 queue_for_each_hw_ctx(q, hctx, i)
647 blk_mq_tag_idle(hctx);
648 }
320ae51f
JA
649}
650
651/*
652 * Reverse check our software queue for entries that we could potentially
653 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
654 * too much time checking for merges.
655 */
656static bool blk_mq_attempt_merge(struct request_queue *q,
657 struct blk_mq_ctx *ctx, struct bio *bio)
658{
659 struct request *rq;
660 int checked = 8;
661
662 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
663 int el_ret;
664
665 if (!checked--)
666 break;
667
668 if (!blk_rq_merge_ok(rq, bio))
669 continue;
670
671 el_ret = blk_try_merge(rq, bio);
672 if (el_ret == ELEVATOR_BACK_MERGE) {
673 if (bio_attempt_back_merge(q, rq, bio)) {
674 ctx->rq_merged++;
675 return true;
676 }
677 break;
678 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
679 if (bio_attempt_front_merge(q, rq, bio)) {
680 ctx->rq_merged++;
681 return true;
682 }
683 break;
684 }
685 }
686
687 return false;
688}
689
1429d7c9
JA
690/*
691 * Process software queues that have been marked busy, splicing them
692 * to the for-dispatch
693 */
694static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
695{
696 struct blk_mq_ctx *ctx;
697 int i;
698
699 for (i = 0; i < hctx->ctx_map.map_size; i++) {
700 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
701 unsigned int off, bit;
702
703 if (!bm->word)
704 continue;
705
706 bit = 0;
707 off = i * hctx->ctx_map.bits_per_word;
708 do {
709 bit = find_next_bit(&bm->word, bm->depth, bit);
710 if (bit >= bm->depth)
711 break;
712
713 ctx = hctx->ctxs[bit + off];
714 clear_bit(bit, &bm->word);
715 spin_lock(&ctx->lock);
716 list_splice_tail_init(&ctx->rq_list, list);
717 spin_unlock(&ctx->lock);
718
719 bit++;
720 } while (1);
721 }
722}
723
320ae51f
JA
724/*
725 * Run this hardware queue, pulling any software queues mapped to it in.
726 * Note that this function currently has various problems around ordering
727 * of IO. In particular, we'd like FIFO behaviour on handling existing
728 * items on the hctx->dispatch list. Ignore that for now.
729 */
730static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
731{
732 struct request_queue *q = hctx->queue;
320ae51f
JA
733 struct request *rq;
734 LIST_HEAD(rq_list);
1429d7c9 735 int queued;
320ae51f 736
fd1270d5 737 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
e4043dcf 738
5d12f905 739 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
740 return;
741
742 hctx->run++;
743
744 /*
745 * Touch any software queue that has pending entries.
746 */
1429d7c9 747 flush_busy_ctxs(hctx, &rq_list);
320ae51f
JA
748
749 /*
750 * If we have previous entries on our dispatch list, grab them
751 * and stuff them at the front for more fair dispatch.
752 */
753 if (!list_empty_careful(&hctx->dispatch)) {
754 spin_lock(&hctx->lock);
755 if (!list_empty(&hctx->dispatch))
756 list_splice_init(&hctx->dispatch, &rq_list);
757 spin_unlock(&hctx->lock);
758 }
759
320ae51f
JA
760 /*
761 * Now process all the entries, sending them to the driver.
762 */
1429d7c9 763 queued = 0;
320ae51f
JA
764 while (!list_empty(&rq_list)) {
765 int ret;
766
767 rq = list_first_entry(&rq_list, struct request, queuelist);
768 list_del_init(&rq->queuelist);
320ae51f 769
49f5baa5 770 blk_mq_start_request(rq, list_empty(&rq_list));
320ae51f
JA
771
772 ret = q->mq_ops->queue_rq(hctx, rq);
773 switch (ret) {
774 case BLK_MQ_RQ_QUEUE_OK:
775 queued++;
776 continue;
777 case BLK_MQ_RQ_QUEUE_BUSY:
320ae51f 778 list_add(&rq->queuelist, &rq_list);
ed0791b2 779 __blk_mq_requeue_request(rq);
320ae51f
JA
780 break;
781 default:
782 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 783 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 784 rq->errors = -EIO;
320ae51f
JA
785 blk_mq_end_io(rq, rq->errors);
786 break;
787 }
788
789 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
790 break;
791 }
792
793 if (!queued)
794 hctx->dispatched[0]++;
795 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
796 hctx->dispatched[ilog2(queued) + 1]++;
797
798 /*
799 * Any items that need requeuing? Stuff them into hctx->dispatch,
800 * that is where we will continue on next queue run.
801 */
802 if (!list_empty(&rq_list)) {
803 spin_lock(&hctx->lock);
804 list_splice(&rq_list, &hctx->dispatch);
805 spin_unlock(&hctx->lock);
806 }
807}
808
506e931f
JA
809/*
810 * It'd be great if the workqueue API had a way to pass
811 * in a mask and had some smarts for more clever placement.
812 * For now we just round-robin here, switching for every
813 * BLK_MQ_CPU_WORK_BATCH queued items.
814 */
815static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
816{
817 int cpu = hctx->next_cpu;
818
819 if (--hctx->next_cpu_batch <= 0) {
820 int next_cpu;
821
822 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
823 if (next_cpu >= nr_cpu_ids)
824 next_cpu = cpumask_first(hctx->cpumask);
825
826 hctx->next_cpu = next_cpu;
827 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
828 }
829
830 return cpu;
831}
832
320ae51f
JA
833void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
834{
5d12f905 835 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
836 return;
837
e4043dcf 838 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
320ae51f 839 __blk_mq_run_hw_queue(hctx);
e4043dcf 840 else if (hctx->queue->nr_hw_queues == 1)
70f4db63 841 kblockd_schedule_delayed_work(&hctx->run_work, 0);
e4043dcf
JA
842 else {
843 unsigned int cpu;
844
506e931f 845 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63 846 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
e4043dcf 847 }
320ae51f
JA
848}
849
850void blk_mq_run_queues(struct request_queue *q, bool async)
851{
852 struct blk_mq_hw_ctx *hctx;
853 int i;
854
855 queue_for_each_hw_ctx(q, hctx, i) {
856 if ((!blk_mq_hctx_has_pending(hctx) &&
857 list_empty_careful(&hctx->dispatch)) ||
5d12f905 858 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
859 continue;
860
e4043dcf 861 preempt_disable();
320ae51f 862 blk_mq_run_hw_queue(hctx, async);
e4043dcf 863 preempt_enable();
320ae51f
JA
864 }
865}
866EXPORT_SYMBOL(blk_mq_run_queues);
867
868void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
869{
70f4db63
CH
870 cancel_delayed_work(&hctx->run_work);
871 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
872 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
873}
874EXPORT_SYMBOL(blk_mq_stop_hw_queue);
875
280d45f6
CH
876void blk_mq_stop_hw_queues(struct request_queue *q)
877{
878 struct blk_mq_hw_ctx *hctx;
879 int i;
880
881 queue_for_each_hw_ctx(q, hctx, i)
882 blk_mq_stop_hw_queue(hctx);
883}
884EXPORT_SYMBOL(blk_mq_stop_hw_queues);
885
320ae51f
JA
886void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
887{
888 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf
JA
889
890 preempt_disable();
320ae51f 891 __blk_mq_run_hw_queue(hctx);
e4043dcf 892 preempt_enable();
320ae51f
JA
893}
894EXPORT_SYMBOL(blk_mq_start_hw_queue);
895
2f268556
CH
896void blk_mq_start_hw_queues(struct request_queue *q)
897{
898 struct blk_mq_hw_ctx *hctx;
899 int i;
900
901 queue_for_each_hw_ctx(q, hctx, i)
902 blk_mq_start_hw_queue(hctx);
903}
904EXPORT_SYMBOL(blk_mq_start_hw_queues);
905
906
1b4a3258 907void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
908{
909 struct blk_mq_hw_ctx *hctx;
910 int i;
911
912 queue_for_each_hw_ctx(q, hctx, i) {
913 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
914 continue;
915
916 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 917 preempt_disable();
1b4a3258 918 blk_mq_run_hw_queue(hctx, async);
e4043dcf 919 preempt_enable();
320ae51f
JA
920 }
921}
922EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
923
70f4db63 924static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
925{
926 struct blk_mq_hw_ctx *hctx;
927
70f4db63 928 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
e4043dcf 929
320ae51f
JA
930 __blk_mq_run_hw_queue(hctx);
931}
932
70f4db63
CH
933static void blk_mq_delay_work_fn(struct work_struct *work)
934{
935 struct blk_mq_hw_ctx *hctx;
936
937 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
938
939 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
940 __blk_mq_run_hw_queue(hctx);
941}
942
943void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
944{
945 unsigned long tmo = msecs_to_jiffies(msecs);
946
947 if (hctx->queue->nr_hw_queues == 1)
948 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
949 else {
950 unsigned int cpu;
951
506e931f 952 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63
CH
953 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
954 }
955}
956EXPORT_SYMBOL(blk_mq_delay_queue);
957
320ae51f 958static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
72a0a36e 959 struct request *rq, bool at_head)
320ae51f
JA
960{
961 struct blk_mq_ctx *ctx = rq->mq_ctx;
962
01b983c9
JA
963 trace_block_rq_insert(hctx->queue, rq);
964
72a0a36e
CH
965 if (at_head)
966 list_add(&rq->queuelist, &ctx->rq_list);
967 else
968 list_add_tail(&rq->queuelist, &ctx->rq_list);
4bb659b1 969
320ae51f
JA
970 blk_mq_hctx_mark_pending(hctx, ctx);
971
972 /*
973 * We do this early, to ensure we are on the right CPU.
974 */
87ee7b11 975 blk_add_timer(rq);
320ae51f
JA
976}
977
eeabc850
CH
978void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
979 bool async)
320ae51f 980{
eeabc850 981 struct request_queue *q = rq->q;
320ae51f 982 struct blk_mq_hw_ctx *hctx;
eeabc850
CH
983 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
984
985 current_ctx = blk_mq_get_ctx(q);
986 if (!cpu_online(ctx->cpu))
987 rq->mq_ctx = ctx = current_ctx;
320ae51f 988
320ae51f
JA
989 hctx = q->mq_ops->map_queue(q, ctx->cpu);
990
eeabc850
CH
991 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
992 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
320ae51f
JA
993 blk_insert_flush(rq);
994 } else {
320ae51f 995 spin_lock(&ctx->lock);
72a0a36e 996 __blk_mq_insert_request(hctx, rq, at_head);
320ae51f 997 spin_unlock(&ctx->lock);
320ae51f
JA
998 }
999
320ae51f
JA
1000 if (run_queue)
1001 blk_mq_run_hw_queue(hctx, async);
e4043dcf
JA
1002
1003 blk_mq_put_ctx(current_ctx);
320ae51f
JA
1004}
1005
1006static void blk_mq_insert_requests(struct request_queue *q,
1007 struct blk_mq_ctx *ctx,
1008 struct list_head *list,
1009 int depth,
1010 bool from_schedule)
1011
1012{
1013 struct blk_mq_hw_ctx *hctx;
1014 struct blk_mq_ctx *current_ctx;
1015
1016 trace_block_unplug(q, depth, !from_schedule);
1017
1018 current_ctx = blk_mq_get_ctx(q);
1019
1020 if (!cpu_online(ctx->cpu))
1021 ctx = current_ctx;
1022 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1023
1024 /*
1025 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1026 * offline now
1027 */
1028 spin_lock(&ctx->lock);
1029 while (!list_empty(list)) {
1030 struct request *rq;
1031
1032 rq = list_first_entry(list, struct request, queuelist);
1033 list_del_init(&rq->queuelist);
1034 rq->mq_ctx = ctx;
72a0a36e 1035 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
1036 }
1037 spin_unlock(&ctx->lock);
1038
320ae51f 1039 blk_mq_run_hw_queue(hctx, from_schedule);
e4043dcf 1040 blk_mq_put_ctx(current_ctx);
320ae51f
JA
1041}
1042
1043static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1044{
1045 struct request *rqa = container_of(a, struct request, queuelist);
1046 struct request *rqb = container_of(b, struct request, queuelist);
1047
1048 return !(rqa->mq_ctx < rqb->mq_ctx ||
1049 (rqa->mq_ctx == rqb->mq_ctx &&
1050 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1051}
1052
1053void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1054{
1055 struct blk_mq_ctx *this_ctx;
1056 struct request_queue *this_q;
1057 struct request *rq;
1058 LIST_HEAD(list);
1059 LIST_HEAD(ctx_list);
1060 unsigned int depth;
1061
1062 list_splice_init(&plug->mq_list, &list);
1063
1064 list_sort(NULL, &list, plug_ctx_cmp);
1065
1066 this_q = NULL;
1067 this_ctx = NULL;
1068 depth = 0;
1069
1070 while (!list_empty(&list)) {
1071 rq = list_entry_rq(list.next);
1072 list_del_init(&rq->queuelist);
1073 BUG_ON(!rq->q);
1074 if (rq->mq_ctx != this_ctx) {
1075 if (this_ctx) {
1076 blk_mq_insert_requests(this_q, this_ctx,
1077 &ctx_list, depth,
1078 from_schedule);
1079 }
1080
1081 this_ctx = rq->mq_ctx;
1082 this_q = rq->q;
1083 depth = 0;
1084 }
1085
1086 depth++;
1087 list_add_tail(&rq->queuelist, &ctx_list);
1088 }
1089
1090 /*
1091 * If 'this_ctx' is set, we know we have entries to complete
1092 * on 'ctx_list'. Do those.
1093 */
1094 if (this_ctx) {
1095 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1096 from_schedule);
1097 }
1098}
1099
1100static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1101{
1102 init_request_from_bio(rq, bio);
4b570521
JA
1103
1104 if (blk_do_io_stat(rq)) {
1105 rq->start_time = jiffies;
1106 blk_account_io_start(rq, 1);
1107 }
320ae51f
JA
1108}
1109
07068d5b
JA
1110static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1111 struct blk_mq_ctx *ctx,
1112 struct request *rq, struct bio *bio)
320ae51f 1113{
07068d5b 1114 struct request_queue *q = hctx->queue;
320ae51f 1115
07068d5b
JA
1116 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1117 blk_mq_bio_to_request(rq, bio);
1118 spin_lock(&ctx->lock);
1119insert_rq:
1120 __blk_mq_insert_request(hctx, rq, false);
1121 spin_unlock(&ctx->lock);
1122 return false;
1123 } else {
1124 spin_lock(&ctx->lock);
1125 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1126 blk_mq_bio_to_request(rq, bio);
1127 goto insert_rq;
1128 }
320ae51f 1129
07068d5b
JA
1130 spin_unlock(&ctx->lock);
1131 __blk_mq_free_request(hctx, ctx, rq);
1132 return true;
14ec77f3 1133 }
07068d5b 1134}
14ec77f3 1135
07068d5b
JA
1136struct blk_map_ctx {
1137 struct blk_mq_hw_ctx *hctx;
1138 struct blk_mq_ctx *ctx;
1139};
1140
1141static struct request *blk_mq_map_request(struct request_queue *q,
1142 struct bio *bio,
1143 struct blk_map_ctx *data)
1144{
1145 struct blk_mq_hw_ctx *hctx;
1146 struct blk_mq_ctx *ctx;
1147 struct request *rq;
1148 int rw = bio_data_dir(bio);
320ae51f 1149
07068d5b 1150 if (unlikely(blk_mq_queue_enter(q))) {
320ae51f 1151 bio_endio(bio, -EIO);
07068d5b 1152 return NULL;
320ae51f
JA
1153 }
1154
1155 ctx = blk_mq_get_ctx(q);
1156 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1157
07068d5b 1158 if (rw_is_sync(bio->bi_rw))
27fbf4e8 1159 rw |= REQ_SYNC;
07068d5b 1160
320ae51f 1161 trace_block_getrq(q, bio, rw);
5dee8577
CH
1162 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, GFP_ATOMIC, false);
1163 if (unlikely(!rq)) {
793597a6 1164 __blk_mq_run_hw_queue(hctx);
320ae51f
JA
1165 blk_mq_put_ctx(ctx);
1166 trace_block_sleeprq(q, bio, rw);
793597a6
CH
1167
1168 ctx = blk_mq_get_ctx(q);
320ae51f 1169 hctx = q->mq_ops->map_queue(q, ctx->cpu);
793597a6
CH
1170 rq = __blk_mq_alloc_request(q, hctx, ctx, rw,
1171 __GFP_WAIT|GFP_ATOMIC, false);
320ae51f
JA
1172 }
1173
1174 hctx->queued++;
07068d5b
JA
1175 data->hctx = hctx;
1176 data->ctx = ctx;
1177 return rq;
1178}
1179
1180/*
1181 * Multiple hardware queue variant. This will not use per-process plugs,
1182 * but will attempt to bypass the hctx queueing if we can go straight to
1183 * hardware for SYNC IO.
1184 */
1185static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1186{
1187 const int is_sync = rw_is_sync(bio->bi_rw);
1188 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1189 struct blk_map_ctx data;
1190 struct request *rq;
1191
1192 blk_queue_bounce(q, &bio);
1193
1194 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1195 bio_endio(bio, -EIO);
1196 return;
1197 }
1198
1199 rq = blk_mq_map_request(q, bio, &data);
1200 if (unlikely(!rq))
1201 return;
1202
1203 if (unlikely(is_flush_fua)) {
1204 blk_mq_bio_to_request(rq, bio);
1205 blk_insert_flush(rq);
1206 goto run_queue;
1207 }
1208
1209 if (is_sync) {
1210 int ret;
1211
1212 blk_mq_bio_to_request(rq, bio);
1213 blk_mq_start_request(rq, true);
1214
1215 /*
1216 * For OK queue, we are done. For error, kill it. Any other
1217 * error (busy), just add it to our list as we previously
1218 * would have done
1219 */
1220 ret = q->mq_ops->queue_rq(data.hctx, rq);
1221 if (ret == BLK_MQ_RQ_QUEUE_OK)
1222 goto done;
1223 else {
1224 __blk_mq_requeue_request(rq);
1225
1226 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1227 rq->errors = -EIO;
1228 blk_mq_end_io(rq, rq->errors);
1229 goto done;
1230 }
1231 }
1232 }
1233
1234 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1235 /*
1236 * For a SYNC request, send it to the hardware immediately. For
1237 * an ASYNC request, just ensure that we run it later on. The
1238 * latter allows for merging opportunities and more efficient
1239 * dispatching.
1240 */
1241run_queue:
1242 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1243 }
1244done:
1245 blk_mq_put_ctx(data.ctx);
1246}
1247
1248/*
1249 * Single hardware queue variant. This will attempt to use any per-process
1250 * plug for merging and IO deferral.
1251 */
1252static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1253{
1254 const int is_sync = rw_is_sync(bio->bi_rw);
1255 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1256 unsigned int use_plug, request_count = 0;
1257 struct blk_map_ctx data;
1258 struct request *rq;
1259
1260 /*
1261 * If we have multiple hardware queues, just go directly to
1262 * one of those for sync IO.
1263 */
1264 use_plug = !is_flush_fua && !is_sync;
1265
1266 blk_queue_bounce(q, &bio);
1267
1268 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1269 bio_endio(bio, -EIO);
1270 return;
1271 }
1272
1273 if (use_plug && !blk_queue_nomerges(q) &&
1274 blk_attempt_plug_merge(q, bio, &request_count))
1275 return;
1276
1277 rq = blk_mq_map_request(q, bio, &data);
320ae51f
JA
1278
1279 if (unlikely(is_flush_fua)) {
1280 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
1281 blk_insert_flush(rq);
1282 goto run_queue;
1283 }
1284
1285 /*
1286 * A task plug currently exists. Since this is completely lockless,
1287 * utilize that to temporarily store requests until the task is
1288 * either done or scheduled away.
1289 */
1290 if (use_plug) {
1291 struct blk_plug *plug = current->plug;
1292
1293 if (plug) {
1294 blk_mq_bio_to_request(rq, bio);
92f399c7 1295 if (list_empty(&plug->mq_list))
320ae51f
JA
1296 trace_block_plug(q);
1297 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1298 blk_flush_plug_list(plug, false);
1299 trace_block_plug(q);
1300 }
1301 list_add_tail(&rq->queuelist, &plug->mq_list);
07068d5b 1302 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1303 return;
1304 }
1305 }
1306
07068d5b
JA
1307 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1308 /*
1309 * For a SYNC request, send it to the hardware immediately. For
1310 * an ASYNC request, just ensure that we run it later on. The
1311 * latter allows for merging opportunities and more efficient
1312 * dispatching.
1313 */
1314run_queue:
1315 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1316 }
1317
07068d5b 1318 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1319}
1320
1321/*
1322 * Default mapping to a software queue, since we use one per CPU.
1323 */
1324struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1325{
1326 return q->queue_hw_ctx[q->mq_map[cpu]];
1327}
1328EXPORT_SYMBOL(blk_mq_map_queue);
1329
24d2f903
CH
1330static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1331 struct blk_mq_tags *tags, unsigned int hctx_idx)
95363efd 1332{
e9b267d9 1333 struct page *page;
320ae51f 1334
24d2f903 1335 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1336 int i;
320ae51f 1337
24d2f903
CH
1338 for (i = 0; i < tags->nr_tags; i++) {
1339 if (!tags->rqs[i])
e9b267d9 1340 continue;
24d2f903
CH
1341 set->ops->exit_request(set->driver_data, tags->rqs[i],
1342 hctx_idx, i);
e9b267d9 1343 }
320ae51f 1344 }
320ae51f 1345
24d2f903
CH
1346 while (!list_empty(&tags->page_list)) {
1347 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1348 list_del_init(&page->lru);
320ae51f
JA
1349 __free_pages(page, page->private);
1350 }
1351
24d2f903 1352 kfree(tags->rqs);
320ae51f 1353
24d2f903 1354 blk_mq_free_tags(tags);
320ae51f
JA
1355}
1356
1357static size_t order_to_size(unsigned int order)
1358{
4ca08500 1359 return (size_t)PAGE_SIZE << order;
320ae51f
JA
1360}
1361
24d2f903
CH
1362static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1363 unsigned int hctx_idx)
320ae51f 1364{
24d2f903 1365 struct blk_mq_tags *tags;
320ae51f
JA
1366 unsigned int i, j, entries_per_page, max_order = 4;
1367 size_t rq_size, left;
1368
24d2f903
CH
1369 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1370 set->numa_node);
1371 if (!tags)
1372 return NULL;
320ae51f 1373
24d2f903
CH
1374 INIT_LIST_HEAD(&tags->page_list);
1375
1376 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1377 GFP_KERNEL, set->numa_node);
1378 if (!tags->rqs) {
1379 blk_mq_free_tags(tags);
1380 return NULL;
1381 }
320ae51f
JA
1382
1383 /*
1384 * rq_size is the size of the request plus driver payload, rounded
1385 * to the cacheline size
1386 */
24d2f903 1387 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1388 cache_line_size());
24d2f903 1389 left = rq_size * set->queue_depth;
320ae51f 1390
24d2f903 1391 for (i = 0; i < set->queue_depth; ) {
320ae51f
JA
1392 int this_order = max_order;
1393 struct page *page;
1394 int to_do;
1395 void *p;
1396
1397 while (left < order_to_size(this_order - 1) && this_order)
1398 this_order--;
1399
1400 do {
24d2f903
CH
1401 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1402 this_order);
320ae51f
JA
1403 if (page)
1404 break;
1405 if (!this_order--)
1406 break;
1407 if (order_to_size(this_order) < rq_size)
1408 break;
1409 } while (1);
1410
1411 if (!page)
24d2f903 1412 goto fail;
320ae51f
JA
1413
1414 page->private = this_order;
24d2f903 1415 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1416
1417 p = page_address(page);
1418 entries_per_page = order_to_size(this_order) / rq_size;
24d2f903 1419 to_do = min(entries_per_page, set->queue_depth - i);
320ae51f
JA
1420 left -= to_do * rq_size;
1421 for (j = 0; j < to_do; j++) {
24d2f903
CH
1422 tags->rqs[i] = p;
1423 if (set->ops->init_request) {
1424 if (set->ops->init_request(set->driver_data,
1425 tags->rqs[i], hctx_idx, i,
1426 set->numa_node))
1427 goto fail;
e9b267d9
CH
1428 }
1429
320ae51f
JA
1430 p += rq_size;
1431 i++;
1432 }
1433 }
1434
24d2f903 1435 return tags;
320ae51f 1436
24d2f903
CH
1437fail:
1438 pr_warn("%s: failed to allocate requests\n", __func__);
1439 blk_mq_free_rq_map(set, tags, hctx_idx);
1440 return NULL;
320ae51f
JA
1441}
1442
1429d7c9
JA
1443static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1444{
1445 kfree(bitmap->map);
1446}
1447
1448static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1449{
1450 unsigned int bpw = 8, total, num_maps, i;
1451
1452 bitmap->bits_per_word = bpw;
1453
1454 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1455 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1456 GFP_KERNEL, node);
1457 if (!bitmap->map)
1458 return -ENOMEM;
1459
1460 bitmap->map_size = num_maps;
1461
1462 total = nr_cpu_ids;
1463 for (i = 0; i < num_maps; i++) {
1464 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1465 total -= bitmap->map[i].depth;
1466 }
1467
1468 return 0;
1469}
1470
484b4061
JA
1471static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1472{
1473 struct request_queue *q = hctx->queue;
1474 struct blk_mq_ctx *ctx;
1475 LIST_HEAD(tmp);
1476
1477 /*
1478 * Move ctx entries to new CPU, if this one is going away.
1479 */
1480 ctx = __blk_mq_get_ctx(q, cpu);
1481
1482 spin_lock(&ctx->lock);
1483 if (!list_empty(&ctx->rq_list)) {
1484 list_splice_init(&ctx->rq_list, &tmp);
1485 blk_mq_hctx_clear_pending(hctx, ctx);
1486 }
1487 spin_unlock(&ctx->lock);
1488
1489 if (list_empty(&tmp))
1490 return NOTIFY_OK;
1491
1492 ctx = blk_mq_get_ctx(q);
1493 spin_lock(&ctx->lock);
1494
1495 while (!list_empty(&tmp)) {
1496 struct request *rq;
1497
1498 rq = list_first_entry(&tmp, struct request, queuelist);
1499 rq->mq_ctx = ctx;
1500 list_move_tail(&rq->queuelist, &ctx->rq_list);
1501 }
1502
1503 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1504 blk_mq_hctx_mark_pending(hctx, ctx);
1505
1506 spin_unlock(&ctx->lock);
1507
1508 blk_mq_run_hw_queue(hctx, true);
1509 blk_mq_put_ctx(ctx);
1510 return NOTIFY_OK;
1511}
1512
1513static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1514{
1515 struct request_queue *q = hctx->queue;
1516 struct blk_mq_tag_set *set = q->tag_set;
1517
1518 if (set->tags[hctx->queue_num])
1519 return NOTIFY_OK;
1520
1521 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1522 if (!set->tags[hctx->queue_num])
1523 return NOTIFY_STOP;
1524
1525 hctx->tags = set->tags[hctx->queue_num];
1526 return NOTIFY_OK;
1527}
1528
1529static int blk_mq_hctx_notify(void *data, unsigned long action,
1530 unsigned int cpu)
1531{
1532 struct blk_mq_hw_ctx *hctx = data;
1533
1534 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1535 return blk_mq_hctx_cpu_offline(hctx, cpu);
1536 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1537 return blk_mq_hctx_cpu_online(hctx, cpu);
1538
1539 return NOTIFY_OK;
1540}
1541
624dbe47
ML
1542static void blk_mq_exit_hw_queues(struct request_queue *q,
1543 struct blk_mq_tag_set *set, int nr_queue)
1544{
1545 struct blk_mq_hw_ctx *hctx;
1546 unsigned int i;
1547
1548 queue_for_each_hw_ctx(q, hctx, i) {
1549 if (i == nr_queue)
1550 break;
1551
1552 if (set->ops->exit_hctx)
1553 set->ops->exit_hctx(hctx, i);
1554
1555 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1556 kfree(hctx->ctxs);
1557 blk_mq_free_bitmap(&hctx->ctx_map);
1558 }
1559
1560}
1561
1562static void blk_mq_free_hw_queues(struct request_queue *q,
1563 struct blk_mq_tag_set *set)
1564{
1565 struct blk_mq_hw_ctx *hctx;
1566 unsigned int i;
1567
1568 queue_for_each_hw_ctx(q, hctx, i) {
1569 free_cpumask_var(hctx->cpumask);
cdef54dd 1570 kfree(hctx);
624dbe47
ML
1571 }
1572}
1573
320ae51f 1574static int blk_mq_init_hw_queues(struct request_queue *q,
24d2f903 1575 struct blk_mq_tag_set *set)
320ae51f
JA
1576{
1577 struct blk_mq_hw_ctx *hctx;
624dbe47 1578 unsigned int i;
320ae51f
JA
1579
1580 /*
1581 * Initialize hardware queues
1582 */
1583 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1584 int node;
1585
1586 node = hctx->numa_node;
1587 if (node == NUMA_NO_NODE)
24d2f903 1588 node = hctx->numa_node = set->numa_node;
320ae51f 1589
70f4db63
CH
1590 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1591 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
320ae51f
JA
1592 spin_lock_init(&hctx->lock);
1593 INIT_LIST_HEAD(&hctx->dispatch);
1594 hctx->queue = q;
1595 hctx->queue_num = i;
24d2f903
CH
1596 hctx->flags = set->flags;
1597 hctx->cmd_size = set->cmd_size;
320ae51f
JA
1598
1599 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1600 blk_mq_hctx_notify, hctx);
1601 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1602
24d2f903 1603 hctx->tags = set->tags[i];
320ae51f
JA
1604
1605 /*
1606 * Allocate space for all possible cpus to avoid allocation in
1607 * runtime
1608 */
1609 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1610 GFP_KERNEL, node);
1611 if (!hctx->ctxs)
1612 break;
1613
1429d7c9 1614 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
320ae51f
JA
1615 break;
1616
320ae51f
JA
1617 hctx->nr_ctx = 0;
1618
24d2f903
CH
1619 if (set->ops->init_hctx &&
1620 set->ops->init_hctx(hctx, set->driver_data, i))
320ae51f
JA
1621 break;
1622 }
1623
1624 if (i == q->nr_hw_queues)
1625 return 0;
1626
1627 /*
1628 * Init failed
1629 */
624dbe47 1630 blk_mq_exit_hw_queues(q, set, i);
320ae51f
JA
1631
1632 return 1;
1633}
1634
1635static void blk_mq_init_cpu_queues(struct request_queue *q,
1636 unsigned int nr_hw_queues)
1637{
1638 unsigned int i;
1639
1640 for_each_possible_cpu(i) {
1641 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1642 struct blk_mq_hw_ctx *hctx;
1643
1644 memset(__ctx, 0, sizeof(*__ctx));
1645 __ctx->cpu = i;
1646 spin_lock_init(&__ctx->lock);
1647 INIT_LIST_HEAD(&__ctx->rq_list);
1648 __ctx->queue = q;
1649
1650 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1651 if (!cpu_online(i))
1652 continue;
1653
e4043dcf
JA
1654 hctx = q->mq_ops->map_queue(q, i);
1655 cpumask_set_cpu(i, hctx->cpumask);
1656 hctx->nr_ctx++;
1657
320ae51f
JA
1658 /*
1659 * Set local node, IFF we have more than one hw queue. If
1660 * not, we remain on the home node of the device
1661 */
1662 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1663 hctx->numa_node = cpu_to_node(i);
1664 }
1665}
1666
1667static void blk_mq_map_swqueue(struct request_queue *q)
1668{
1669 unsigned int i;
1670 struct blk_mq_hw_ctx *hctx;
1671 struct blk_mq_ctx *ctx;
1672
1673 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1674 cpumask_clear(hctx->cpumask);
320ae51f
JA
1675 hctx->nr_ctx = 0;
1676 }
1677
1678 /*
1679 * Map software to hardware queues
1680 */
1681 queue_for_each_ctx(q, ctx, i) {
1682 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1683 if (!cpu_online(i))
1684 continue;
1685
320ae51f 1686 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1687 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1688 ctx->index_hw = hctx->nr_ctx;
1689 hctx->ctxs[hctx->nr_ctx++] = ctx;
1690 }
506e931f
JA
1691
1692 queue_for_each_hw_ctx(q, hctx, i) {
484b4061
JA
1693 /*
1694 * If not software queues are mapped to this hardware queue,
1695 * disable it and free the request entries
1696 */
1697 if (!hctx->nr_ctx) {
1698 struct blk_mq_tag_set *set = q->tag_set;
1699
1700 if (set->tags[i]) {
1701 blk_mq_free_rq_map(set, set->tags[i], i);
1702 set->tags[i] = NULL;
1703 hctx->tags = NULL;
1704 }
1705 continue;
1706 }
1707
1708 /*
1709 * Initialize batch roundrobin counts
1710 */
506e931f
JA
1711 hctx->next_cpu = cpumask_first(hctx->cpumask);
1712 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1713 }
320ae51f
JA
1714}
1715
0d2602ca
JA
1716static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1717{
1718 struct blk_mq_hw_ctx *hctx;
1719 struct request_queue *q;
1720 bool shared;
1721 int i;
1722
1723 if (set->tag_list.next == set->tag_list.prev)
1724 shared = false;
1725 else
1726 shared = true;
1727
1728 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1729 blk_mq_freeze_queue(q);
1730
1731 queue_for_each_hw_ctx(q, hctx, i) {
1732 if (shared)
1733 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1734 else
1735 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1736 }
1737 blk_mq_unfreeze_queue(q);
1738 }
1739}
1740
1741static void blk_mq_del_queue_tag_set(struct request_queue *q)
1742{
1743 struct blk_mq_tag_set *set = q->tag_set;
1744
1745 blk_mq_freeze_queue(q);
1746
1747 mutex_lock(&set->tag_list_lock);
1748 list_del_init(&q->tag_set_list);
1749 blk_mq_update_tag_set_depth(set);
1750 mutex_unlock(&set->tag_list_lock);
1751
1752 blk_mq_unfreeze_queue(q);
1753}
1754
1755static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1756 struct request_queue *q)
1757{
1758 q->tag_set = set;
1759
1760 mutex_lock(&set->tag_list_lock);
1761 list_add_tail(&q->tag_set_list, &set->tag_list);
1762 blk_mq_update_tag_set_depth(set);
1763 mutex_unlock(&set->tag_list_lock);
1764}
1765
24d2f903 1766struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
320ae51f
JA
1767{
1768 struct blk_mq_hw_ctx **hctxs;
1769 struct blk_mq_ctx *ctx;
1770 struct request_queue *q;
f14bbe77 1771 unsigned int *map;
320ae51f
JA
1772 int i;
1773
320ae51f
JA
1774 ctx = alloc_percpu(struct blk_mq_ctx);
1775 if (!ctx)
1776 return ERR_PTR(-ENOMEM);
1777
24d2f903
CH
1778 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1779 set->numa_node);
320ae51f
JA
1780
1781 if (!hctxs)
1782 goto err_percpu;
1783
f14bbe77
JA
1784 map = blk_mq_make_queue_map(set);
1785 if (!map)
1786 goto err_map;
1787
24d2f903 1788 for (i = 0; i < set->nr_hw_queues; i++) {
f14bbe77
JA
1789 int node = blk_mq_hw_queue_to_node(map, i);
1790
cdef54dd
CH
1791 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1792 GFP_KERNEL, node);
320ae51f
JA
1793 if (!hctxs[i])
1794 goto err_hctxs;
1795
e4043dcf
JA
1796 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1797 goto err_hctxs;
1798
0d2602ca 1799 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 1800 hctxs[i]->numa_node = node;
320ae51f
JA
1801 hctxs[i]->queue_num = i;
1802 }
1803
24d2f903 1804 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
320ae51f
JA
1805 if (!q)
1806 goto err_hctxs;
1807
3d2936f4
ML
1808 if (percpu_counter_init(&q->mq_usage_counter, 0))
1809 goto err_map;
1810
320ae51f
JA
1811 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1812 blk_queue_rq_timeout(q, 30000);
1813
1814 q->nr_queues = nr_cpu_ids;
24d2f903 1815 q->nr_hw_queues = set->nr_hw_queues;
f14bbe77 1816 q->mq_map = map;
320ae51f
JA
1817
1818 q->queue_ctx = ctx;
1819 q->queue_hw_ctx = hctxs;
1820
24d2f903 1821 q->mq_ops = set->ops;
94eddfbe 1822 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1823
05f1dd53
JA
1824 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1825 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1826
1be036e9
CH
1827 q->sg_reserved_size = INT_MAX;
1828
6fca6a61
CH
1829 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1830 INIT_LIST_HEAD(&q->requeue_list);
1831 spin_lock_init(&q->requeue_lock);
1832
07068d5b
JA
1833 if (q->nr_hw_queues > 1)
1834 blk_queue_make_request(q, blk_mq_make_request);
1835 else
1836 blk_queue_make_request(q, blk_sq_make_request);
1837
87ee7b11 1838 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
24d2f903
CH
1839 if (set->timeout)
1840 blk_queue_rq_timeout(q, set->timeout);
320ae51f 1841
eba71768
JA
1842 /*
1843 * Do this after blk_queue_make_request() overrides it...
1844 */
1845 q->nr_requests = set->queue_depth;
1846
24d2f903
CH
1847 if (set->ops->complete)
1848 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 1849
320ae51f 1850 blk_mq_init_flush(q);
24d2f903 1851 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 1852
24d2f903
CH
1853 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1854 set->cmd_size, cache_line_size()),
1855 GFP_KERNEL);
18741986 1856 if (!q->flush_rq)
320ae51f
JA
1857 goto err_hw;
1858
24d2f903 1859 if (blk_mq_init_hw_queues(q, set))
18741986
CH
1860 goto err_flush_rq;
1861
320ae51f
JA
1862 mutex_lock(&all_q_mutex);
1863 list_add_tail(&q->all_q_node, &all_q_list);
1864 mutex_unlock(&all_q_mutex);
1865
0d2602ca
JA
1866 blk_mq_add_queue_tag_set(set, q);
1867
484b4061
JA
1868 blk_mq_map_swqueue(q);
1869
320ae51f 1870 return q;
18741986
CH
1871
1872err_flush_rq:
1873 kfree(q->flush_rq);
320ae51f 1874err_hw:
320ae51f
JA
1875 blk_cleanup_queue(q);
1876err_hctxs:
f14bbe77 1877 kfree(map);
24d2f903 1878 for (i = 0; i < set->nr_hw_queues; i++) {
320ae51f
JA
1879 if (!hctxs[i])
1880 break;
e4043dcf 1881 free_cpumask_var(hctxs[i]->cpumask);
cdef54dd 1882 kfree(hctxs[i]);
320ae51f 1883 }
f14bbe77 1884err_map:
320ae51f
JA
1885 kfree(hctxs);
1886err_percpu:
1887 free_percpu(ctx);
1888 return ERR_PTR(-ENOMEM);
1889}
1890EXPORT_SYMBOL(blk_mq_init_queue);
1891
1892void blk_mq_free_queue(struct request_queue *q)
1893{
624dbe47 1894 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 1895
0d2602ca
JA
1896 blk_mq_del_queue_tag_set(q);
1897
624dbe47
ML
1898 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1899 blk_mq_free_hw_queues(q, set);
320ae51f 1900
3d2936f4
ML
1901 percpu_counter_destroy(&q->mq_usage_counter);
1902
320ae51f
JA
1903 free_percpu(q->queue_ctx);
1904 kfree(q->queue_hw_ctx);
1905 kfree(q->mq_map);
1906
1907 q->queue_ctx = NULL;
1908 q->queue_hw_ctx = NULL;
1909 q->mq_map = NULL;
1910
1911 mutex_lock(&all_q_mutex);
1912 list_del_init(&q->all_q_node);
1913 mutex_unlock(&all_q_mutex);
1914}
320ae51f
JA
1915
1916/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1917static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1918{
1919 blk_mq_freeze_queue(q);
1920
1921 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1922
1923 /*
1924 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1925 * we should change hctx numa_node according to new topology (this
1926 * involves free and re-allocate memory, worthy doing?)
1927 */
1928
1929 blk_mq_map_swqueue(q);
1930
1931 blk_mq_unfreeze_queue(q);
1932}
1933
f618ef7c
PG
1934static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1935 unsigned long action, void *hcpu)
320ae51f
JA
1936{
1937 struct request_queue *q;
1938
1939 /*
9fccfed8
JA
1940 * Before new mappings are established, hotadded cpu might already
1941 * start handling requests. This doesn't break anything as we map
1942 * offline CPUs to first hardware queue. We will re-init the queue
1943 * below to get optimal settings.
320ae51f
JA
1944 */
1945 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1946 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1947 return NOTIFY_OK;
1948
1949 mutex_lock(&all_q_mutex);
1950 list_for_each_entry(q, &all_q_list, all_q_node)
1951 blk_mq_queue_reinit(q);
1952 mutex_unlock(&all_q_mutex);
1953 return NOTIFY_OK;
1954}
1955
24d2f903
CH
1956int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1957{
1958 int i;
1959
1960 if (!set->nr_hw_queues)
1961 return -EINVAL;
1962 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1963 return -EINVAL;
1964 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1965 return -EINVAL;
1966
cdef54dd 1967 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
24d2f903
CH
1968 return -EINVAL;
1969
1970
48479005
ML
1971 set->tags = kmalloc_node(set->nr_hw_queues *
1972 sizeof(struct blk_mq_tags *),
24d2f903
CH
1973 GFP_KERNEL, set->numa_node);
1974 if (!set->tags)
1975 goto out;
1976
1977 for (i = 0; i < set->nr_hw_queues; i++) {
1978 set->tags[i] = blk_mq_init_rq_map(set, i);
1979 if (!set->tags[i])
1980 goto out_unwind;
1981 }
1982
0d2602ca
JA
1983 mutex_init(&set->tag_list_lock);
1984 INIT_LIST_HEAD(&set->tag_list);
1985
24d2f903
CH
1986 return 0;
1987
1988out_unwind:
1989 while (--i >= 0)
1990 blk_mq_free_rq_map(set, set->tags[i], i);
1991out:
1992 return -ENOMEM;
1993}
1994EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1995
1996void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1997{
1998 int i;
1999
484b4061
JA
2000 for (i = 0; i < set->nr_hw_queues; i++) {
2001 if (set->tags[i])
2002 blk_mq_free_rq_map(set, set->tags[i], i);
2003 }
2004
981bd189 2005 kfree(set->tags);
24d2f903
CH
2006}
2007EXPORT_SYMBOL(blk_mq_free_tag_set);
2008
e3a2b3f9
JA
2009int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2010{
2011 struct blk_mq_tag_set *set = q->tag_set;
2012 struct blk_mq_hw_ctx *hctx;
2013 int i, ret;
2014
2015 if (!set || nr > set->queue_depth)
2016 return -EINVAL;
2017
2018 ret = 0;
2019 queue_for_each_hw_ctx(q, hctx, i) {
2020 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2021 if (ret)
2022 break;
2023 }
2024
2025 if (!ret)
2026 q->nr_requests = nr;
2027
2028 return ret;
2029}
2030
676141e4
JA
2031void blk_mq_disable_hotplug(void)
2032{
2033 mutex_lock(&all_q_mutex);
2034}
2035
2036void blk_mq_enable_hotplug(void)
2037{
2038 mutex_unlock(&all_q_mutex);
2039}
2040
320ae51f
JA
2041static int __init blk_mq_init(void)
2042{
320ae51f
JA
2043 blk_mq_cpu_init();
2044
2045 /* Must be called after percpu_counter_hotcpu_callback() */
2046 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2047
2048 return 0;
2049}
2050subsys_initcall(blk_mq_init);