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