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