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