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