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