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