]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-mq.c
blk-mq: Export blk_mq_freeze_queue_wait
[mirror_ubuntu-artful-kernel.git] / block / blk-mq.c
CommitLineData
75bb4625
JA
1/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
320ae51f
JA
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/backing-dev.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
f75782e4 12#include <linux/kmemleak.h>
320ae51f
JA
13#include <linux/mm.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/workqueue.h>
17#include <linux/smp.h>
18#include <linux/llist.h>
19#include <linux/list_sort.h>
20#include <linux/cpu.h>
21#include <linux/cache.h>
22#include <linux/sched/sysctl.h>
23#include <linux/delay.h>
aedcd72f 24#include <linux/crash_dump.h>
88c7b2b7 25#include <linux/prefetch.h>
320ae51f
JA
26
27#include <trace/events/block.h>
28
29#include <linux/blk-mq.h>
30#include "blk.h"
31#include "blk-mq.h"
32#include "blk-mq-tag.h"
cf43e6be 33#include "blk-stat.h"
87760e5e 34#include "blk-wbt.h"
bd166ef1 35#include "blk-mq-sched.h"
320ae51f
JA
36
37static DEFINE_MUTEX(all_q_mutex);
38static LIST_HEAD(all_q_list);
39
320ae51f
JA
40/*
41 * Check if any of the ctx's have pending work in this hardware queue
42 */
50e1dab8 43bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
320ae51f 44{
bd166ef1
JA
45 return sbitmap_any_bit_set(&hctx->ctx_map) ||
46 !list_empty_careful(&hctx->dispatch) ||
47 blk_mq_sched_has_work(hctx);
1429d7c9
JA
48}
49
320ae51f
JA
50/*
51 * Mark this ctx as having pending work in this hardware queue
52 */
53static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
54 struct blk_mq_ctx *ctx)
55{
88459642
OS
56 if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
57 sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
1429d7c9
JA
58}
59
60static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
61 struct blk_mq_ctx *ctx)
62{
88459642 63 sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
320ae51f
JA
64}
65
b4c6a028 66void blk_mq_freeze_queue_start(struct request_queue *q)
43a5e4e2 67{
4ecd4fef 68 int freeze_depth;
cddd5d17 69
4ecd4fef
CH
70 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
71 if (freeze_depth == 1) {
3ef28e83 72 percpu_ref_kill(&q->q_usage_counter);
b94ec296 73 blk_mq_run_hw_queues(q, false);
cddd5d17 74 }
f3af020b 75}
b4c6a028 76EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
f3af020b 77
6bae363e 78void blk_mq_freeze_queue_wait(struct request_queue *q)
f3af020b 79{
3ef28e83 80 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
43a5e4e2 81}
6bae363e 82EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
43a5e4e2 83
f3af020b
TH
84/*
85 * Guarantee no request is in use, so we can change any data structure of
86 * the queue afterward.
87 */
3ef28e83 88void blk_freeze_queue(struct request_queue *q)
f3af020b 89{
3ef28e83
DW
90 /*
91 * In the !blk_mq case we are only calling this to kill the
92 * q_usage_counter, otherwise this increases the freeze depth
93 * and waits for it to return to zero. For this reason there is
94 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
95 * exported to drivers as the only user for unfreeze is blk_mq.
96 */
f3af020b
TH
97 blk_mq_freeze_queue_start(q);
98 blk_mq_freeze_queue_wait(q);
99}
3ef28e83
DW
100
101void blk_mq_freeze_queue(struct request_queue *q)
102{
103 /*
104 * ...just an alias to keep freeze and unfreeze actions balanced
105 * in the blk_mq_* namespace
106 */
107 blk_freeze_queue(q);
108}
c761d96b 109EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
f3af020b 110
b4c6a028 111void blk_mq_unfreeze_queue(struct request_queue *q)
320ae51f 112{
4ecd4fef 113 int freeze_depth;
320ae51f 114
4ecd4fef
CH
115 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
116 WARN_ON_ONCE(freeze_depth < 0);
117 if (!freeze_depth) {
3ef28e83 118 percpu_ref_reinit(&q->q_usage_counter);
320ae51f 119 wake_up_all(&q->mq_freeze_wq);
add703fd 120 }
320ae51f 121}
b4c6a028 122EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
320ae51f 123
6a83e74d
BVA
124/**
125 * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished
126 * @q: request queue.
127 *
128 * Note: this function does not prevent that the struct request end_io()
129 * callback function is invoked. Additionally, it is not prevented that
130 * new queue_rq() calls occur unless the queue has been stopped first.
131 */
132void blk_mq_quiesce_queue(struct request_queue *q)
133{
134 struct blk_mq_hw_ctx *hctx;
135 unsigned int i;
136 bool rcu = false;
137
138 blk_mq_stop_hw_queues(q);
139
140 queue_for_each_hw_ctx(q, hctx, i) {
141 if (hctx->flags & BLK_MQ_F_BLOCKING)
142 synchronize_srcu(&hctx->queue_rq_srcu);
143 else
144 rcu = true;
145 }
146 if (rcu)
147 synchronize_rcu();
148}
149EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
150
aed3ea94
JA
151void blk_mq_wake_waiters(struct request_queue *q)
152{
153 struct blk_mq_hw_ctx *hctx;
154 unsigned int i;
155
156 queue_for_each_hw_ctx(q, hctx, i)
157 if (blk_mq_hw_queue_mapped(hctx))
158 blk_mq_tag_wakeup_all(hctx->tags, true);
3fd5940c
KB
159
160 /*
161 * If we are called because the queue has now been marked as
162 * dying, we need to ensure that processes currently waiting on
163 * the queue are notified as well.
164 */
165 wake_up_all(&q->mq_freeze_wq);
aed3ea94
JA
166}
167
320ae51f
JA
168bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
169{
170 return blk_mq_has_free_tags(hctx->tags);
171}
172EXPORT_SYMBOL(blk_mq_can_queue);
173
2c3ad667
JA
174void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
175 struct request *rq, unsigned int op)
320ae51f 176{
af76e555
CH
177 INIT_LIST_HEAD(&rq->queuelist);
178 /* csd/requeue_work/fifo_time is initialized before use */
179 rq->q = q;
320ae51f 180 rq->mq_ctx = ctx;
ef295ecf 181 rq->cmd_flags = op;
e8064021
CH
182 if (blk_queue_io_stat(q))
183 rq->rq_flags |= RQF_IO_STAT;
af76e555
CH
184 /* do not touch atomic flags, it needs atomic ops against the timer */
185 rq->cpu = -1;
af76e555
CH
186 INIT_HLIST_NODE(&rq->hash);
187 RB_CLEAR_NODE(&rq->rb_node);
af76e555
CH
188 rq->rq_disk = NULL;
189 rq->part = NULL;
3ee32372 190 rq->start_time = jiffies;
af76e555
CH
191#ifdef CONFIG_BLK_CGROUP
192 rq->rl = NULL;
0fec08b4 193 set_start_time_ns(rq);
af76e555
CH
194 rq->io_start_time_ns = 0;
195#endif
196 rq->nr_phys_segments = 0;
197#if defined(CONFIG_BLK_DEV_INTEGRITY)
198 rq->nr_integrity_segments = 0;
199#endif
af76e555
CH
200 rq->special = NULL;
201 /* tag was already set */
202 rq->errors = 0;
af76e555 203 rq->extra_len = 0;
af76e555 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 213}
2c3ad667 214EXPORT_SYMBOL_GPL(blk_mq_rq_ctx_init);
320ae51f 215
2c3ad667
JA
216struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data,
217 unsigned int op)
5dee8577
CH
218{
219 struct request *rq;
220 unsigned int tag;
221
cb96a42c 222 tag = blk_mq_get_tag(data);
5dee8577 223 if (tag != BLK_MQ_TAG_FAIL) {
bd166ef1
JA
224 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
225
226 rq = tags->static_rqs[tag];
5dee8577 227
bd166ef1
JA
228 if (data->flags & BLK_MQ_REQ_INTERNAL) {
229 rq->tag = -1;
230 rq->internal_tag = tag;
231 } else {
200e86b3
JA
232 if (blk_mq_tag_busy(data->hctx)) {
233 rq->rq_flags = RQF_MQ_INFLIGHT;
234 atomic_inc(&data->hctx->nr_active);
235 }
bd166ef1
JA
236 rq->tag = tag;
237 rq->internal_tag = -1;
562bef42 238 data->hctx->tags->rqs[rq->tag] = rq;
bd166ef1
JA
239 }
240
ef295ecf 241 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op);
5dee8577
CH
242 return rq;
243 }
244
245 return NULL;
246}
2c3ad667 247EXPORT_SYMBOL_GPL(__blk_mq_alloc_request);
5dee8577 248
6f3b0e8b
CH
249struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
250 unsigned int flags)
320ae51f 251{
5a797e00 252 struct blk_mq_alloc_data alloc_data = { .flags = flags };
bd166ef1 253 struct request *rq;
a492f075 254 int ret;
320ae51f 255
6f3b0e8b 256 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
a492f075
JL
257 if (ret)
258 return ERR_PTR(ret);
320ae51f 259
bd166ef1 260 rq = blk_mq_sched_get_request(q, NULL, rw, &alloc_data);
841bac2c 261
bd166ef1
JA
262 blk_mq_put_ctx(alloc_data.ctx);
263 blk_queue_exit(q);
264
265 if (!rq)
a492f075 266 return ERR_PTR(-EWOULDBLOCK);
0c4de0f3
CH
267
268 rq->__data_len = 0;
269 rq->__sector = (sector_t) -1;
270 rq->bio = rq->biotail = NULL;
320ae51f
JA
271 return rq;
272}
4bb659b1 273EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f 274
1f5bd336
ML
275struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
276 unsigned int flags, unsigned int hctx_idx)
277{
6d2809d5 278 struct blk_mq_alloc_data alloc_data = { .flags = flags };
1f5bd336 279 struct request *rq;
6d2809d5 280 unsigned int cpu;
1f5bd336
ML
281 int ret;
282
283 /*
284 * If the tag allocator sleeps we could get an allocation for a
285 * different hardware context. No need to complicate the low level
286 * allocator for this for the rare use case of a command tied to
287 * a specific queue.
288 */
289 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
290 return ERR_PTR(-EINVAL);
291
292 if (hctx_idx >= q->nr_hw_queues)
293 return ERR_PTR(-EIO);
294
295 ret = blk_queue_enter(q, true);
296 if (ret)
297 return ERR_PTR(ret);
298
c8712c6a
CH
299 /*
300 * Check if the hardware context is actually mapped to anything.
301 * If not tell the caller that it should skip this queue.
302 */
6d2809d5
OS
303 alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
304 if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
305 blk_queue_exit(q);
306 return ERR_PTR(-EXDEV);
1f5bd336 307 }
6d2809d5
OS
308 cpu = cpumask_first(alloc_data.hctx->cpumask);
309 alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
1f5bd336 310
6d2809d5 311 rq = blk_mq_sched_get_request(q, NULL, rw, &alloc_data);
c8712c6a 312
6d2809d5 313 blk_mq_put_ctx(alloc_data.ctx);
c8712c6a 314 blk_queue_exit(q);
6d2809d5
OS
315
316 if (!rq)
317 return ERR_PTR(-EWOULDBLOCK);
318
319 return rq;
1f5bd336
ML
320}
321EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
322
bd166ef1
JA
323void __blk_mq_finish_request(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
324 struct request *rq)
320ae51f 325{
bd166ef1 326 const int sched_tag = rq->internal_tag;
320ae51f
JA
327 struct request_queue *q = rq->q;
328
e8064021 329 if (rq->rq_flags & RQF_MQ_INFLIGHT)
0d2602ca 330 atomic_dec(&hctx->nr_active);
87760e5e
JA
331
332 wbt_done(q->rq_wb, &rq->issue_stat);
e8064021 333 rq->rq_flags = 0;
0d2602ca 334
af76e555 335 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
06426adf 336 clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
bd166ef1
JA
337 if (rq->tag != -1)
338 blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
339 if (sched_tag != -1)
340 blk_mq_sched_completed_request(hctx, rq);
50e1dab8 341 blk_mq_sched_restart_queues(hctx);
3ef28e83 342 blk_queue_exit(q);
320ae51f
JA
343}
344
bd166ef1 345static void blk_mq_finish_hctx_request(struct blk_mq_hw_ctx *hctx,
16a3c2a7 346 struct request *rq)
320ae51f
JA
347{
348 struct blk_mq_ctx *ctx = rq->mq_ctx;
320ae51f
JA
349
350 ctx->rq_completed[rq_is_sync(rq)]++;
bd166ef1
JA
351 __blk_mq_finish_request(hctx, ctx, rq);
352}
353
354void blk_mq_finish_request(struct request *rq)
355{
356 blk_mq_finish_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq);
7c7f2f2b 357}
7c7f2f2b
JA
358
359void blk_mq_free_request(struct request *rq)
360{
bd166ef1 361 blk_mq_sched_put_request(rq);
320ae51f 362}
1a3b595a 363EXPORT_SYMBOL_GPL(blk_mq_free_request);
320ae51f 364
c8a446ad 365inline void __blk_mq_end_request(struct request *rq, int error)
320ae51f 366{
0d11e6ac
ML
367 blk_account_io_done(rq);
368
91b63639 369 if (rq->end_io) {
87760e5e 370 wbt_done(rq->q->rq_wb, &rq->issue_stat);
320ae51f 371 rq->end_io(rq, error);
91b63639
CH
372 } else {
373 if (unlikely(blk_bidi_rq(rq)))
374 blk_mq_free_request(rq->next_rq);
320ae51f 375 blk_mq_free_request(rq);
91b63639 376 }
320ae51f 377}
c8a446ad 378EXPORT_SYMBOL(__blk_mq_end_request);
63151a44 379
c8a446ad 380void blk_mq_end_request(struct request *rq, int error)
63151a44
CH
381{
382 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
383 BUG();
c8a446ad 384 __blk_mq_end_request(rq, error);
63151a44 385}
c8a446ad 386EXPORT_SYMBOL(blk_mq_end_request);
320ae51f 387
30a91cb4 388static void __blk_mq_complete_request_remote(void *data)
320ae51f 389{
3d6efbf6 390 struct request *rq = data;
320ae51f 391
30a91cb4 392 rq->q->softirq_done_fn(rq);
320ae51f 393}
320ae51f 394
ed851860 395static void blk_mq_ipi_complete_request(struct request *rq)
320ae51f
JA
396{
397 struct blk_mq_ctx *ctx = rq->mq_ctx;
38535201 398 bool shared = false;
320ae51f
JA
399 int cpu;
400
38535201 401 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
30a91cb4
CH
402 rq->q->softirq_done_fn(rq);
403 return;
404 }
320ae51f
JA
405
406 cpu = get_cpu();
38535201
CH
407 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
408 shared = cpus_share_cache(cpu, ctx->cpu);
409
410 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 411 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
412 rq->csd.info = rq;
413 rq->csd.flags = 0;
c46fff2a 414 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 415 } else {
30a91cb4 416 rq->q->softirq_done_fn(rq);
3d6efbf6 417 }
320ae51f
JA
418 put_cpu();
419}
30a91cb4 420
cf43e6be
JA
421static void blk_mq_stat_add(struct request *rq)
422{
423 if (rq->rq_flags & RQF_STATS) {
424 /*
425 * We could rq->mq_ctx here, but there's less of a risk
426 * of races if we have the completion event add the stats
427 * to the local software queue.
428 */
429 struct blk_mq_ctx *ctx;
430
431 ctx = __blk_mq_get_ctx(rq->q, raw_smp_processor_id());
432 blk_stat_add(&ctx->stat[rq_data_dir(rq)], rq);
433 }
434}
435
1fa8cc52 436static void __blk_mq_complete_request(struct request *rq)
ed851860
JA
437{
438 struct request_queue *q = rq->q;
439
cf43e6be
JA
440 blk_mq_stat_add(rq);
441
ed851860 442 if (!q->softirq_done_fn)
c8a446ad 443 blk_mq_end_request(rq, rq->errors);
ed851860
JA
444 else
445 blk_mq_ipi_complete_request(rq);
446}
447
30a91cb4
CH
448/**
449 * blk_mq_complete_request - end I/O on a request
450 * @rq: the request being processed
451 *
452 * Description:
453 * Ends all I/O on a request. It does not handle partial completions.
454 * The actual completion happens out-of-order, through a IPI handler.
455 **/
f4829a9b 456void blk_mq_complete_request(struct request *rq, int error)
30a91cb4 457{
95f09684
JA
458 struct request_queue *q = rq->q;
459
460 if (unlikely(blk_should_fake_timeout(q)))
30a91cb4 461 return;
f4829a9b
CH
462 if (!blk_mark_rq_complete(rq)) {
463 rq->errors = error;
ed851860 464 __blk_mq_complete_request(rq);
f4829a9b 465 }
30a91cb4
CH
466}
467EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 468
973c0191
KB
469int blk_mq_request_started(struct request *rq)
470{
471 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
472}
473EXPORT_SYMBOL_GPL(blk_mq_request_started);
474
e2490073 475void blk_mq_start_request(struct request *rq)
320ae51f
JA
476{
477 struct request_queue *q = rq->q;
478
bd166ef1
JA
479 blk_mq_sched_started_request(rq);
480
320ae51f
JA
481 trace_block_rq_issue(q, rq);
482
cf43e6be
JA
483 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
484 blk_stat_set_issue_time(&rq->issue_stat);
485 rq->rq_flags |= RQF_STATS;
87760e5e 486 wbt_issue(q->rq_wb, &rq->issue_stat);
cf43e6be
JA
487 }
488
2b8393b4 489 blk_add_timer(rq);
87ee7b11 490
538b7534
JA
491 /*
492 * Ensure that ->deadline is visible before set the started
493 * flag and clear the completed flag.
494 */
495 smp_mb__before_atomic();
496
87ee7b11
JA
497 /*
498 * Mark us as started and clear complete. Complete might have been
499 * set if requeue raced with timeout, which then marked it as
500 * complete. So be sure to clear complete again when we start
501 * the request, otherwise we'll ignore the completion event.
502 */
4b570521
JA
503 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
504 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
505 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
506 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
49f5baa5
CH
507
508 if (q->dma_drain_size && blk_rq_bytes(rq)) {
509 /*
510 * Make sure space for the drain appears. We know we can do
511 * this because max_hw_segments has been adjusted to be one
512 * fewer than the device can handle.
513 */
514 rq->nr_phys_segments++;
515 }
320ae51f 516}
e2490073 517EXPORT_SYMBOL(blk_mq_start_request);
320ae51f 518
ed0791b2 519static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
520{
521 struct request_queue *q = rq->q;
522
523 trace_block_rq_requeue(q, rq);
87760e5e 524 wbt_requeue(q->rq_wb, &rq->issue_stat);
bd166ef1 525 blk_mq_sched_requeue_request(rq);
49f5baa5 526
e2490073
CH
527 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
528 if (q->dma_drain_size && blk_rq_bytes(rq))
529 rq->nr_phys_segments--;
530 }
320ae51f
JA
531}
532
2b053aca 533void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
ed0791b2 534{
ed0791b2 535 __blk_mq_requeue_request(rq);
ed0791b2 536
ed0791b2 537 BUG_ON(blk_queued_rq(rq));
2b053aca 538 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
ed0791b2
CH
539}
540EXPORT_SYMBOL(blk_mq_requeue_request);
541
6fca6a61
CH
542static void blk_mq_requeue_work(struct work_struct *work)
543{
544 struct request_queue *q =
2849450a 545 container_of(work, struct request_queue, requeue_work.work);
6fca6a61
CH
546 LIST_HEAD(rq_list);
547 struct request *rq, *next;
548 unsigned long flags;
549
550 spin_lock_irqsave(&q->requeue_lock, flags);
551 list_splice_init(&q->requeue_list, &rq_list);
552 spin_unlock_irqrestore(&q->requeue_lock, flags);
553
554 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
e8064021 555 if (!(rq->rq_flags & RQF_SOFTBARRIER))
6fca6a61
CH
556 continue;
557
e8064021 558 rq->rq_flags &= ~RQF_SOFTBARRIER;
6fca6a61 559 list_del_init(&rq->queuelist);
bd6737f1 560 blk_mq_sched_insert_request(rq, true, false, false, true);
6fca6a61
CH
561 }
562
563 while (!list_empty(&rq_list)) {
564 rq = list_entry(rq_list.next, struct request, queuelist);
565 list_del_init(&rq->queuelist);
bd6737f1 566 blk_mq_sched_insert_request(rq, false, false, false, true);
6fca6a61
CH
567 }
568
52d7f1b5 569 blk_mq_run_hw_queues(q, false);
6fca6a61
CH
570}
571
2b053aca
BVA
572void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
573 bool kick_requeue_list)
6fca6a61
CH
574{
575 struct request_queue *q = rq->q;
576 unsigned long flags;
577
578 /*
579 * We abuse this flag that is otherwise used by the I/O scheduler to
580 * request head insertation from the workqueue.
581 */
e8064021 582 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
6fca6a61
CH
583
584 spin_lock_irqsave(&q->requeue_lock, flags);
585 if (at_head) {
e8064021 586 rq->rq_flags |= RQF_SOFTBARRIER;
6fca6a61
CH
587 list_add(&rq->queuelist, &q->requeue_list);
588 } else {
589 list_add_tail(&rq->queuelist, &q->requeue_list);
590 }
591 spin_unlock_irqrestore(&q->requeue_lock, flags);
2b053aca
BVA
592
593 if (kick_requeue_list)
594 blk_mq_kick_requeue_list(q);
6fca6a61
CH
595}
596EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
597
598void blk_mq_kick_requeue_list(struct request_queue *q)
599{
2849450a 600 kblockd_schedule_delayed_work(&q->requeue_work, 0);
6fca6a61
CH
601}
602EXPORT_SYMBOL(blk_mq_kick_requeue_list);
603
2849450a
MS
604void blk_mq_delay_kick_requeue_list(struct request_queue *q,
605 unsigned long msecs)
606{
607 kblockd_schedule_delayed_work(&q->requeue_work,
608 msecs_to_jiffies(msecs));
609}
610EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
611
1885b24d
JA
612void blk_mq_abort_requeue_list(struct request_queue *q)
613{
614 unsigned long flags;
615 LIST_HEAD(rq_list);
616
617 spin_lock_irqsave(&q->requeue_lock, flags);
618 list_splice_init(&q->requeue_list, &rq_list);
619 spin_unlock_irqrestore(&q->requeue_lock, flags);
620
621 while (!list_empty(&rq_list)) {
622 struct request *rq;
623
624 rq = list_first_entry(&rq_list, struct request, queuelist);
625 list_del_init(&rq->queuelist);
626 rq->errors = -EIO;
627 blk_mq_end_request(rq, rq->errors);
628 }
629}
630EXPORT_SYMBOL(blk_mq_abort_requeue_list);
631
0e62f51f
JA
632struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
633{
88c7b2b7
JA
634 if (tag < tags->nr_tags) {
635 prefetch(tags->rqs[tag]);
4ee86bab 636 return tags->rqs[tag];
88c7b2b7 637 }
4ee86bab
HR
638
639 return NULL;
24d2f903
CH
640}
641EXPORT_SYMBOL(blk_mq_tag_to_rq);
642
320ae51f 643struct blk_mq_timeout_data {
46f92d42
CH
644 unsigned long next;
645 unsigned int next_set;
320ae51f
JA
646};
647
90415837 648void blk_mq_rq_timed_out(struct request *req, bool reserved)
320ae51f 649{
f8a5b122 650 const struct blk_mq_ops *ops = req->q->mq_ops;
46f92d42 651 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
87ee7b11
JA
652
653 /*
654 * We know that complete is set at this point. If STARTED isn't set
655 * anymore, then the request isn't active and the "timeout" should
656 * just be ignored. This can happen due to the bitflag ordering.
657 * Timeout first checks if STARTED is set, and if it is, assumes
658 * the request is active. But if we race with completion, then
659 * we both flags will get cleared. So check here again, and ignore
660 * a timeout event with a request that isn't active.
661 */
46f92d42
CH
662 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
663 return;
87ee7b11 664
46f92d42 665 if (ops->timeout)
0152fb6b 666 ret = ops->timeout(req, reserved);
46f92d42
CH
667
668 switch (ret) {
669 case BLK_EH_HANDLED:
670 __blk_mq_complete_request(req);
671 break;
672 case BLK_EH_RESET_TIMER:
673 blk_add_timer(req);
674 blk_clear_rq_complete(req);
675 break;
676 case BLK_EH_NOT_HANDLED:
677 break;
678 default:
679 printk(KERN_ERR "block: bad eh return: %d\n", ret);
680 break;
681 }
87ee7b11 682}
5b3f25fc 683
81481eb4
CH
684static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
685 struct request *rq, void *priv, bool reserved)
686{
687 struct blk_mq_timeout_data *data = priv;
87ee7b11 688
eb130dbf
KB
689 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
690 /*
691 * If a request wasn't started before the queue was
692 * marked dying, kill it here or it'll go unnoticed.
693 */
a59e0f57
KB
694 if (unlikely(blk_queue_dying(rq->q))) {
695 rq->errors = -EIO;
696 blk_mq_end_request(rq, rq->errors);
697 }
46f92d42 698 return;
eb130dbf 699 }
87ee7b11 700
46f92d42
CH
701 if (time_after_eq(jiffies, rq->deadline)) {
702 if (!blk_mark_rq_complete(rq))
0152fb6b 703 blk_mq_rq_timed_out(rq, reserved);
46f92d42
CH
704 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
705 data->next = rq->deadline;
706 data->next_set = 1;
707 }
87ee7b11
JA
708}
709
287922eb 710static void blk_mq_timeout_work(struct work_struct *work)
320ae51f 711{
287922eb
CH
712 struct request_queue *q =
713 container_of(work, struct request_queue, timeout_work);
81481eb4
CH
714 struct blk_mq_timeout_data data = {
715 .next = 0,
716 .next_set = 0,
717 };
81481eb4 718 int i;
320ae51f 719
71f79fb3
GKB
720 /* A deadlock might occur if a request is stuck requiring a
721 * timeout at the same time a queue freeze is waiting
722 * completion, since the timeout code would not be able to
723 * acquire the queue reference here.
724 *
725 * That's why we don't use blk_queue_enter here; instead, we use
726 * percpu_ref_tryget directly, because we need to be able to
727 * obtain a reference even in the short window between the queue
728 * starting to freeze, by dropping the first reference in
729 * blk_mq_freeze_queue_start, and the moment the last request is
730 * consumed, marked by the instant q_usage_counter reaches
731 * zero.
732 */
733 if (!percpu_ref_tryget(&q->q_usage_counter))
287922eb
CH
734 return;
735
0bf6cd5b 736 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
320ae51f 737
81481eb4
CH
738 if (data.next_set) {
739 data.next = blk_rq_timeout(round_jiffies_up(data.next));
740 mod_timer(&q->timeout, data.next);
0d2602ca 741 } else {
0bf6cd5b
CH
742 struct blk_mq_hw_ctx *hctx;
743
f054b56c
ML
744 queue_for_each_hw_ctx(q, hctx, i) {
745 /* the hctx may be unmapped, so check it here */
746 if (blk_mq_hw_queue_mapped(hctx))
747 blk_mq_tag_idle(hctx);
748 }
0d2602ca 749 }
287922eb 750 blk_queue_exit(q);
320ae51f
JA
751}
752
753/*
754 * Reverse check our software queue for entries that we could potentially
755 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
756 * too much time checking for merges.
757 */
758static bool blk_mq_attempt_merge(struct request_queue *q,
759 struct blk_mq_ctx *ctx, struct bio *bio)
760{
761 struct request *rq;
762 int checked = 8;
763
764 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
34fe7c05 765 bool merged = false;
320ae51f
JA
766
767 if (!checked--)
768 break;
769
770 if (!blk_rq_merge_ok(rq, bio))
771 continue;
772
34fe7c05
CH
773 switch (blk_try_merge(rq, bio)) {
774 case ELEVATOR_BACK_MERGE:
775 if (blk_mq_sched_allow_merge(q, rq, bio))
776 merged = bio_attempt_back_merge(q, rq, bio);
bd166ef1 777 break;
34fe7c05
CH
778 case ELEVATOR_FRONT_MERGE:
779 if (blk_mq_sched_allow_merge(q, rq, bio))
780 merged = bio_attempt_front_merge(q, rq, bio);
320ae51f 781 break;
1e739730
CH
782 case ELEVATOR_DISCARD_MERGE:
783 merged = bio_attempt_discard_merge(q, rq, bio);
320ae51f 784 break;
34fe7c05
CH
785 default:
786 continue;
320ae51f 787 }
34fe7c05
CH
788
789 if (merged)
790 ctx->rq_merged++;
791 return merged;
320ae51f
JA
792 }
793
794 return false;
795}
796
88459642
OS
797struct flush_busy_ctx_data {
798 struct blk_mq_hw_ctx *hctx;
799 struct list_head *list;
800};
801
802static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
803{
804 struct flush_busy_ctx_data *flush_data = data;
805 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
806 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
807
808 sbitmap_clear_bit(sb, bitnr);
809 spin_lock(&ctx->lock);
810 list_splice_tail_init(&ctx->rq_list, flush_data->list);
811 spin_unlock(&ctx->lock);
812 return true;
813}
814
1429d7c9
JA
815/*
816 * Process software queues that have been marked busy, splicing them
817 * to the for-dispatch
818 */
2c3ad667 819void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
1429d7c9 820{
88459642
OS
821 struct flush_busy_ctx_data data = {
822 .hctx = hctx,
823 .list = list,
824 };
1429d7c9 825
88459642 826 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
1429d7c9 827}
2c3ad667 828EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
1429d7c9 829
703fd1c0
JA
830static inline unsigned int queued_to_index(unsigned int queued)
831{
832 if (!queued)
833 return 0;
1429d7c9 834
703fd1c0 835 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
1429d7c9
JA
836}
837
bd6737f1
JA
838bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx,
839 bool wait)
bd166ef1
JA
840{
841 struct blk_mq_alloc_data data = {
842 .q = rq->q,
bd166ef1
JA
843 .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
844 .flags = wait ? 0 : BLK_MQ_REQ_NOWAIT,
845 };
846
bd166ef1
JA
847 if (rq->tag != -1) {
848done:
849 if (hctx)
850 *hctx = data.hctx;
851 return true;
852 }
853
415b806d
SG
854 if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
855 data.flags |= BLK_MQ_REQ_RESERVED;
856
bd166ef1
JA
857 rq->tag = blk_mq_get_tag(&data);
858 if (rq->tag >= 0) {
200e86b3
JA
859 if (blk_mq_tag_busy(data.hctx)) {
860 rq->rq_flags |= RQF_MQ_INFLIGHT;
861 atomic_inc(&data.hctx->nr_active);
862 }
bd166ef1
JA
863 data.hctx->tags->rqs[rq->tag] = rq;
864 goto done;
865 }
866
867 return false;
868}
869
99cf1dc5
JA
870static void blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
871 struct request *rq)
872{
873 if (rq->tag == -1 || rq->internal_tag == -1)
874 return;
875
876 blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag);
877 rq->tag = -1;
878
879 if (rq->rq_flags & RQF_MQ_INFLIGHT) {
880 rq->rq_flags &= ~RQF_MQ_INFLIGHT;
881 atomic_dec(&hctx->nr_active);
882 }
883}
884
bd166ef1
JA
885/*
886 * If we fail getting a driver tag because all the driver tags are already
887 * assigned and on the dispatch list, BUT the first entry does not have a
888 * tag, then we could deadlock. For that case, move entries with assigned
889 * driver tags to the front, leaving the set of tagged requests in the
890 * same order, and the untagged set in the same order.
891 */
892static bool reorder_tags_to_front(struct list_head *list)
893{
894 struct request *rq, *tmp, *first = NULL;
895
896 list_for_each_entry_safe_reverse(rq, tmp, list, queuelist) {
897 if (rq == first)
898 break;
899 if (rq->tag != -1) {
900 list_move(&rq->queuelist, list);
901 if (!first)
902 first = rq;
903 }
904 }
905
906 return first != NULL;
907}
908
da55f2cc
OS
909static int blk_mq_dispatch_wake(wait_queue_t *wait, unsigned mode, int flags,
910 void *key)
911{
912 struct blk_mq_hw_ctx *hctx;
913
914 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
915
916 list_del(&wait->task_list);
917 clear_bit_unlock(BLK_MQ_S_TAG_WAITING, &hctx->state);
918 blk_mq_run_hw_queue(hctx, true);
919 return 1;
920}
921
922static bool blk_mq_dispatch_wait_add(struct blk_mq_hw_ctx *hctx)
923{
924 struct sbq_wait_state *ws;
925
926 /*
927 * The TAG_WAITING bit serves as a lock protecting hctx->dispatch_wait.
928 * The thread which wins the race to grab this bit adds the hardware
929 * queue to the wait queue.
930 */
931 if (test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state) ||
932 test_and_set_bit_lock(BLK_MQ_S_TAG_WAITING, &hctx->state))
933 return false;
934
935 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
936 ws = bt_wait_ptr(&hctx->tags->bitmap_tags, hctx);
937
938 /*
939 * As soon as this returns, it's no longer safe to fiddle with
940 * hctx->dispatch_wait, since a completion can wake up the wait queue
941 * and unlock the bit.
942 */
943 add_wait_queue(&ws->wait, &hctx->dispatch_wait);
944 return true;
945}
946
f04c3df3 947bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list)
320ae51f
JA
948{
949 struct request_queue *q = hctx->queue;
320ae51f 950 struct request *rq;
74c45052
JA
951 LIST_HEAD(driver_list);
952 struct list_head *dptr;
f04c3df3 953 int queued, ret = BLK_MQ_RQ_QUEUE_OK;
320ae51f 954
74c45052
JA
955 /*
956 * Start off with dptr being NULL, so we start the first request
957 * immediately, even if we have more pending.
958 */
959 dptr = NULL;
960
320ae51f
JA
961 /*
962 * Now process all the entries, sending them to the driver.
963 */
1429d7c9 964 queued = 0;
f04c3df3 965 while (!list_empty(list)) {
74c45052 966 struct blk_mq_queue_data bd;
320ae51f 967
f04c3df3 968 rq = list_first_entry(list, struct request, queuelist);
bd166ef1
JA
969 if (!blk_mq_get_driver_tag(rq, &hctx, false)) {
970 if (!queued && reorder_tags_to_front(list))
971 continue;
3c782d67
JA
972
973 /*
da55f2cc
OS
974 * The initial allocation attempt failed, so we need to
975 * rerun the hardware queue when a tag is freed.
3c782d67 976 */
da55f2cc
OS
977 if (blk_mq_dispatch_wait_add(hctx)) {
978 /*
979 * It's possible that a tag was freed in the
980 * window between the allocation failure and
981 * adding the hardware queue to the wait queue.
982 */
983 if (!blk_mq_get_driver_tag(rq, &hctx, false))
984 break;
985 } else {
3c782d67 986 break;
da55f2cc 987 }
bd166ef1 988 }
da55f2cc 989
320ae51f 990 list_del_init(&rq->queuelist);
320ae51f 991
74c45052
JA
992 bd.rq = rq;
993 bd.list = dptr;
f04c3df3 994 bd.last = list_empty(list);
74c45052
JA
995
996 ret = q->mq_ops->queue_rq(hctx, &bd);
320ae51f
JA
997 switch (ret) {
998 case BLK_MQ_RQ_QUEUE_OK:
999 queued++;
52b9c330 1000 break;
320ae51f 1001 case BLK_MQ_RQ_QUEUE_BUSY:
99cf1dc5 1002 blk_mq_put_driver_tag(hctx, rq);
f04c3df3 1003 list_add(&rq->queuelist, list);
ed0791b2 1004 __blk_mq_requeue_request(rq);
320ae51f
JA
1005 break;
1006 default:
1007 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 1008 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 1009 rq->errors = -EIO;
c8a446ad 1010 blk_mq_end_request(rq, rq->errors);
320ae51f
JA
1011 break;
1012 }
1013
1014 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
1015 break;
74c45052
JA
1016
1017 /*
1018 * We've done the first request. If we have more than 1
1019 * left in the list, set dptr to defer issue.
1020 */
f04c3df3 1021 if (!dptr && list->next != list->prev)
74c45052 1022 dptr = &driver_list;
320ae51f
JA
1023 }
1024
703fd1c0 1025 hctx->dispatched[queued_to_index(queued)]++;
320ae51f
JA
1026
1027 /*
1028 * Any items that need requeuing? Stuff them into hctx->dispatch,
1029 * that is where we will continue on next queue run.
1030 */
f04c3df3 1031 if (!list_empty(list)) {
320ae51f 1032 spin_lock(&hctx->lock);
c13660a0 1033 list_splice_init(list, &hctx->dispatch);
320ae51f 1034 spin_unlock(&hctx->lock);
f04c3df3 1035
9ba52e58
SL
1036 /*
1037 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
1038 * it's possible the queue is stopped and restarted again
1039 * before this. Queue restart will dispatch requests. And since
1040 * requests in rq_list aren't added into hctx->dispatch yet,
1041 * the requests in rq_list might get lost.
1042 *
1043 * blk_mq_run_hw_queue() already checks the STOPPED bit
bd166ef1 1044 *
da55f2cc
OS
1045 * If RESTART or TAG_WAITING is set, then let completion restart
1046 * the queue instead of potentially looping here.
bd166ef1 1047 */
da55f2cc
OS
1048 if (!blk_mq_sched_needs_restart(hctx) &&
1049 !test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state))
bd166ef1 1050 blk_mq_run_hw_queue(hctx, true);
320ae51f 1051 }
f04c3df3 1052
2aa0f21d 1053 return queued != 0;
f04c3df3
JA
1054}
1055
6a83e74d
BVA
1056static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1057{
1058 int srcu_idx;
1059
1060 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1061 cpu_online(hctx->next_cpu));
1062
1063 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1064 rcu_read_lock();
bd166ef1 1065 blk_mq_sched_dispatch_requests(hctx);
6a83e74d
BVA
1066 rcu_read_unlock();
1067 } else {
1068 srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu);
bd166ef1 1069 blk_mq_sched_dispatch_requests(hctx);
6a83e74d
BVA
1070 srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx);
1071 }
1072}
1073
506e931f
JA
1074/*
1075 * It'd be great if the workqueue API had a way to pass
1076 * in a mask and had some smarts for more clever placement.
1077 * For now we just round-robin here, switching for every
1078 * BLK_MQ_CPU_WORK_BATCH queued items.
1079 */
1080static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1081{
b657d7e6
CH
1082 if (hctx->queue->nr_hw_queues == 1)
1083 return WORK_CPU_UNBOUND;
506e931f
JA
1084
1085 if (--hctx->next_cpu_batch <= 0) {
c02ebfdd 1086 int next_cpu;
506e931f
JA
1087
1088 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
1089 if (next_cpu >= nr_cpu_ids)
1090 next_cpu = cpumask_first(hctx->cpumask);
1091
1092 hctx->next_cpu = next_cpu;
1093 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1094 }
1095
b657d7e6 1096 return hctx->next_cpu;
506e931f
JA
1097}
1098
320ae51f
JA
1099void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1100{
5d1b25c1
BVA
1101 if (unlikely(blk_mq_hctx_stopped(hctx) ||
1102 !blk_mq_hw_queue_mapped(hctx)))
320ae51f
JA
1103 return;
1104
1b792f2f 1105 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
2a90d4aa
PB
1106 int cpu = get_cpu();
1107 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
398205b8 1108 __blk_mq_run_hw_queue(hctx);
2a90d4aa 1109 put_cpu();
398205b8
PB
1110 return;
1111 }
e4043dcf 1112
2a90d4aa 1113 put_cpu();
e4043dcf 1114 }
398205b8 1115
27489a3c 1116 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work);
320ae51f
JA
1117}
1118
b94ec296 1119void blk_mq_run_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1120{
1121 struct blk_mq_hw_ctx *hctx;
1122 int i;
1123
1124 queue_for_each_hw_ctx(q, hctx, i) {
bd166ef1 1125 if (!blk_mq_hctx_has_pending(hctx) ||
5d1b25c1 1126 blk_mq_hctx_stopped(hctx))
320ae51f
JA
1127 continue;
1128
b94ec296 1129 blk_mq_run_hw_queue(hctx, async);
320ae51f
JA
1130 }
1131}
b94ec296 1132EXPORT_SYMBOL(blk_mq_run_hw_queues);
320ae51f 1133
fd001443
BVA
1134/**
1135 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1136 * @q: request queue.
1137 *
1138 * The caller is responsible for serializing this function against
1139 * blk_mq_{start,stop}_hw_queue().
1140 */
1141bool blk_mq_queue_stopped(struct request_queue *q)
1142{
1143 struct blk_mq_hw_ctx *hctx;
1144 int i;
1145
1146 queue_for_each_hw_ctx(q, hctx, i)
1147 if (blk_mq_hctx_stopped(hctx))
1148 return true;
1149
1150 return false;
1151}
1152EXPORT_SYMBOL(blk_mq_queue_stopped);
1153
320ae51f
JA
1154void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1155{
27489a3c 1156 cancel_work(&hctx->run_work);
70f4db63 1157 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
1158 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
1159}
1160EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1161
280d45f6
CH
1162void blk_mq_stop_hw_queues(struct request_queue *q)
1163{
1164 struct blk_mq_hw_ctx *hctx;
1165 int i;
1166
1167 queue_for_each_hw_ctx(q, hctx, i)
1168 blk_mq_stop_hw_queue(hctx);
1169}
1170EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1171
320ae51f
JA
1172void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1173{
1174 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 1175
0ffbce80 1176 blk_mq_run_hw_queue(hctx, false);
320ae51f
JA
1177}
1178EXPORT_SYMBOL(blk_mq_start_hw_queue);
1179
2f268556
CH
1180void blk_mq_start_hw_queues(struct request_queue *q)
1181{
1182 struct blk_mq_hw_ctx *hctx;
1183 int i;
1184
1185 queue_for_each_hw_ctx(q, hctx, i)
1186 blk_mq_start_hw_queue(hctx);
1187}
1188EXPORT_SYMBOL(blk_mq_start_hw_queues);
1189
ae911c5e
JA
1190void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1191{
1192 if (!blk_mq_hctx_stopped(hctx))
1193 return;
1194
1195 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1196 blk_mq_run_hw_queue(hctx, async);
1197}
1198EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1199
1b4a3258 1200void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1201{
1202 struct blk_mq_hw_ctx *hctx;
1203 int i;
1204
ae911c5e
JA
1205 queue_for_each_hw_ctx(q, hctx, i)
1206 blk_mq_start_stopped_hw_queue(hctx, async);
320ae51f
JA
1207}
1208EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1209
70f4db63 1210static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
1211{
1212 struct blk_mq_hw_ctx *hctx;
1213
27489a3c 1214 hctx = container_of(work, struct blk_mq_hw_ctx, run_work);
e4043dcf 1215
320ae51f
JA
1216 __blk_mq_run_hw_queue(hctx);
1217}
1218
70f4db63
CH
1219static void blk_mq_delay_work_fn(struct work_struct *work)
1220{
1221 struct blk_mq_hw_ctx *hctx;
1222
1223 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1224
1225 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1226 __blk_mq_run_hw_queue(hctx);
1227}
1228
1229void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1230{
19c66e59
ML
1231 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1232 return;
70f4db63 1233
7e79dadc 1234 blk_mq_stop_hw_queue(hctx);
b657d7e6
CH
1235 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1236 &hctx->delay_work, msecs_to_jiffies(msecs));
70f4db63
CH
1237}
1238EXPORT_SYMBOL(blk_mq_delay_queue);
1239
cfd0c552 1240static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
cfd0c552
ML
1241 struct request *rq,
1242 bool at_head)
320ae51f 1243{
e57690fe
JA
1244 struct blk_mq_ctx *ctx = rq->mq_ctx;
1245
01b983c9
JA
1246 trace_block_rq_insert(hctx->queue, rq);
1247
72a0a36e
CH
1248 if (at_head)
1249 list_add(&rq->queuelist, &ctx->rq_list);
1250 else
1251 list_add_tail(&rq->queuelist, &ctx->rq_list);
cfd0c552 1252}
4bb659b1 1253
2c3ad667
JA
1254void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1255 bool at_head)
cfd0c552
ML
1256{
1257 struct blk_mq_ctx *ctx = rq->mq_ctx;
1258
e57690fe 1259 __blk_mq_insert_req_list(hctx, rq, at_head);
320ae51f 1260 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
1261}
1262
bd166ef1
JA
1263void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1264 struct list_head *list)
320ae51f
JA
1265
1266{
320ae51f
JA
1267 /*
1268 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1269 * offline now
1270 */
1271 spin_lock(&ctx->lock);
1272 while (!list_empty(list)) {
1273 struct request *rq;
1274
1275 rq = list_first_entry(list, struct request, queuelist);
e57690fe 1276 BUG_ON(rq->mq_ctx != ctx);
320ae51f 1277 list_del_init(&rq->queuelist);
e57690fe 1278 __blk_mq_insert_req_list(hctx, rq, false);
320ae51f 1279 }
cfd0c552 1280 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f 1281 spin_unlock(&ctx->lock);
320ae51f
JA
1282}
1283
1284static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1285{
1286 struct request *rqa = container_of(a, struct request, queuelist);
1287 struct request *rqb = container_of(b, struct request, queuelist);
1288
1289 return !(rqa->mq_ctx < rqb->mq_ctx ||
1290 (rqa->mq_ctx == rqb->mq_ctx &&
1291 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1292}
1293
1294void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1295{
1296 struct blk_mq_ctx *this_ctx;
1297 struct request_queue *this_q;
1298 struct request *rq;
1299 LIST_HEAD(list);
1300 LIST_HEAD(ctx_list);
1301 unsigned int depth;
1302
1303 list_splice_init(&plug->mq_list, &list);
1304
1305 list_sort(NULL, &list, plug_ctx_cmp);
1306
1307 this_q = NULL;
1308 this_ctx = NULL;
1309 depth = 0;
1310
1311 while (!list_empty(&list)) {
1312 rq = list_entry_rq(list.next);
1313 list_del_init(&rq->queuelist);
1314 BUG_ON(!rq->q);
1315 if (rq->mq_ctx != this_ctx) {
1316 if (this_ctx) {
bd166ef1
JA
1317 trace_block_unplug(this_q, depth, from_schedule);
1318 blk_mq_sched_insert_requests(this_q, this_ctx,
1319 &ctx_list,
1320 from_schedule);
320ae51f
JA
1321 }
1322
1323 this_ctx = rq->mq_ctx;
1324 this_q = rq->q;
1325 depth = 0;
1326 }
1327
1328 depth++;
1329 list_add_tail(&rq->queuelist, &ctx_list);
1330 }
1331
1332 /*
1333 * If 'this_ctx' is set, we know we have entries to complete
1334 * on 'ctx_list'. Do those.
1335 */
1336 if (this_ctx) {
bd166ef1
JA
1337 trace_block_unplug(this_q, depth, from_schedule);
1338 blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list,
1339 from_schedule);
320ae51f
JA
1340 }
1341}
1342
1343static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1344{
1345 init_request_from_bio(rq, bio);
4b570521 1346
6e85eaf3 1347 blk_account_io_start(rq, true);
320ae51f
JA
1348}
1349
274a5843
JA
1350static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1351{
1352 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1353 !blk_queue_nomerges(hctx->queue);
1354}
1355
07068d5b
JA
1356static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1357 struct blk_mq_ctx *ctx,
1358 struct request *rq, struct bio *bio)
320ae51f 1359{
e18378a6 1360 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
07068d5b
JA
1361 blk_mq_bio_to_request(rq, bio);
1362 spin_lock(&ctx->lock);
1363insert_rq:
1364 __blk_mq_insert_request(hctx, rq, false);
1365 spin_unlock(&ctx->lock);
1366 return false;
1367 } else {
274a5843
JA
1368 struct request_queue *q = hctx->queue;
1369
07068d5b
JA
1370 spin_lock(&ctx->lock);
1371 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1372 blk_mq_bio_to_request(rq, bio);
1373 goto insert_rq;
1374 }
320ae51f 1375
07068d5b 1376 spin_unlock(&ctx->lock);
bd166ef1 1377 __blk_mq_finish_request(hctx, ctx, rq);
07068d5b 1378 return true;
14ec77f3 1379 }
07068d5b 1380}
14ec77f3 1381
fd2d3326
JA
1382static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
1383{
bd166ef1
JA
1384 if (rq->tag != -1)
1385 return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
1386
1387 return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
fd2d3326
JA
1388}
1389
066a4a73 1390static void blk_mq_try_issue_directly(struct request *rq, blk_qc_t *cookie)
f984df1f 1391{
f984df1f 1392 struct request_queue *q = rq->q;
f984df1f
SL
1393 struct blk_mq_queue_data bd = {
1394 .rq = rq,
1395 .list = NULL,
1396 .last = 1
1397 };
bd166ef1
JA
1398 struct blk_mq_hw_ctx *hctx;
1399 blk_qc_t new_cookie;
1400 int ret;
f984df1f 1401
bd166ef1 1402 if (q->elevator)
2253efc8
BVA
1403 goto insert;
1404
bd166ef1
JA
1405 if (!blk_mq_get_driver_tag(rq, &hctx, false))
1406 goto insert;
1407
1408 new_cookie = request_to_qc_t(hctx, rq);
1409
f984df1f
SL
1410 /*
1411 * For OK queue, we are done. For error, kill it. Any other
1412 * error (busy), just add it to our list as we previously
1413 * would have done
1414 */
1415 ret = q->mq_ops->queue_rq(hctx, &bd);
7b371636
JA
1416 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1417 *cookie = new_cookie;
2253efc8 1418 return;
7b371636 1419 }
f984df1f 1420
7b371636
JA
1421 __blk_mq_requeue_request(rq);
1422
1423 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1424 *cookie = BLK_QC_T_NONE;
1425 rq->errors = -EIO;
1426 blk_mq_end_request(rq, rq->errors);
2253efc8 1427 return;
f984df1f 1428 }
7b371636 1429
2253efc8 1430insert:
bd6737f1 1431 blk_mq_sched_insert_request(rq, false, true, true, false);
f984df1f
SL
1432}
1433
07068d5b
JA
1434/*
1435 * Multiple hardware queue variant. This will not use per-process plugs,
1436 * but will attempt to bypass the hctx queueing if we can go straight to
1437 * hardware for SYNC IO.
1438 */
dece1635 1439static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 1440{
ef295ecf 1441 const int is_sync = op_is_sync(bio->bi_opf);
f73f44eb 1442 const int is_flush_fua = op_is_flush(bio->bi_opf);
5a797e00 1443 struct blk_mq_alloc_data data = { .flags = 0 };
07068d5b 1444 struct request *rq;
6a83e74d 1445 unsigned int request_count = 0, srcu_idx;
f984df1f 1446 struct blk_plug *plug;
5b3f341f 1447 struct request *same_queue_rq = NULL;
7b371636 1448 blk_qc_t cookie;
87760e5e 1449 unsigned int wb_acct;
07068d5b
JA
1450
1451 blk_queue_bounce(q, &bio);
1452
1453 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6 1454 bio_io_error(bio);
dece1635 1455 return BLK_QC_T_NONE;
07068d5b
JA
1456 }
1457
54efd50b
KO
1458 blk_queue_split(q, &bio, q->bio_split);
1459
87c279e6
OS
1460 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1461 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1462 return BLK_QC_T_NONE;
f984df1f 1463
bd166ef1
JA
1464 if (blk_mq_sched_bio_merge(q, bio))
1465 return BLK_QC_T_NONE;
1466
87760e5e
JA
1467 wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1468
bd166ef1
JA
1469 trace_block_getrq(q, bio, bio->bi_opf);
1470
1471 rq = blk_mq_sched_get_request(q, bio, bio->bi_opf, &data);
87760e5e
JA
1472 if (unlikely(!rq)) {
1473 __wbt_done(q->rq_wb, wb_acct);
dece1635 1474 return BLK_QC_T_NONE;
87760e5e
JA
1475 }
1476
1477 wbt_track(&rq->issue_stat, wb_acct);
07068d5b 1478
fd2d3326 1479 cookie = request_to_qc_t(data.hctx, rq);
07068d5b
JA
1480
1481 if (unlikely(is_flush_fua)) {
0c2a6fe4
JA
1482 if (q->elevator)
1483 goto elv_insert;
07068d5b
JA
1484 blk_mq_bio_to_request(rq, bio);
1485 blk_insert_flush(rq);
0c2a6fe4 1486 goto run_queue;
07068d5b
JA
1487 }
1488
f984df1f 1489 plug = current->plug;
e167dfb5
JA
1490 /*
1491 * If the driver supports defer issued based on 'last', then
1492 * queue it up like normal since we can potentially save some
1493 * CPU this way.
1494 */
f984df1f
SL
1495 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1496 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1497 struct request *old_rq = NULL;
07068d5b
JA
1498
1499 blk_mq_bio_to_request(rq, bio);
07068d5b
JA
1500
1501 /*
6a83e74d 1502 * We do limited plugging. If the bio can be merged, do that.
f984df1f
SL
1503 * Otherwise the existing request in the plug list will be
1504 * issued. So the plug list will have one request at most
07068d5b 1505 */
f984df1f 1506 if (plug) {
5b3f341f
SL
1507 /*
1508 * The plug list might get flushed before this. If that
b094f89c
JA
1509 * happens, same_queue_rq is invalid and plug list is
1510 * empty
1511 */
5b3f341f
SL
1512 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1513 old_rq = same_queue_rq;
f984df1f 1514 list_del_init(&old_rq->queuelist);
07068d5b 1515 }
f984df1f
SL
1516 list_add_tail(&rq->queuelist, &plug->mq_list);
1517 } else /* is_sync */
1518 old_rq = rq;
1519 blk_mq_put_ctx(data.ctx);
1520 if (!old_rq)
7b371636 1521 goto done;
6a83e74d
BVA
1522
1523 if (!(data.hctx->flags & BLK_MQ_F_BLOCKING)) {
1524 rcu_read_lock();
066a4a73 1525 blk_mq_try_issue_directly(old_rq, &cookie);
6a83e74d
BVA
1526 rcu_read_unlock();
1527 } else {
1528 srcu_idx = srcu_read_lock(&data.hctx->queue_rq_srcu);
066a4a73 1529 blk_mq_try_issue_directly(old_rq, &cookie);
6a83e74d
BVA
1530 srcu_read_unlock(&data.hctx->queue_rq_srcu, srcu_idx);
1531 }
7b371636 1532 goto done;
07068d5b
JA
1533 }
1534
bd166ef1 1535 if (q->elevator) {
0c2a6fe4 1536elv_insert:
bd166ef1
JA
1537 blk_mq_put_ctx(data.ctx);
1538 blk_mq_bio_to_request(rq, bio);
0abad774 1539 blk_mq_sched_insert_request(rq, false, true,
bd6737f1 1540 !is_sync || is_flush_fua, true);
bd166ef1
JA
1541 goto done;
1542 }
07068d5b
JA
1543 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1544 /*
1545 * For a SYNC request, send it to the hardware immediately. For
1546 * an ASYNC request, just ensure that we run it later on. The
1547 * latter allows for merging opportunities and more efficient
1548 * dispatching.
1549 */
0c2a6fe4 1550run_queue:
07068d5b
JA
1551 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1552 }
07068d5b 1553 blk_mq_put_ctx(data.ctx);
7b371636
JA
1554done:
1555 return cookie;
07068d5b
JA
1556}
1557
1558/*
1559 * Single hardware queue variant. This will attempt to use any per-process
1560 * plug for merging and IO deferral.
1561 */
dece1635 1562static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 1563{
ef295ecf 1564 const int is_sync = op_is_sync(bio->bi_opf);
f73f44eb 1565 const int is_flush_fua = op_is_flush(bio->bi_opf);
e6c4438b
JM
1566 struct blk_plug *plug;
1567 unsigned int request_count = 0;
5a797e00 1568 struct blk_mq_alloc_data data = { .flags = 0 };
07068d5b 1569 struct request *rq;
7b371636 1570 blk_qc_t cookie;
87760e5e 1571 unsigned int wb_acct;
07068d5b 1572
07068d5b
JA
1573 blk_queue_bounce(q, &bio);
1574
1575 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6 1576 bio_io_error(bio);
dece1635 1577 return BLK_QC_T_NONE;
07068d5b
JA
1578 }
1579
54efd50b
KO
1580 blk_queue_split(q, &bio, q->bio_split);
1581
87c279e6
OS
1582 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1583 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1584 return BLK_QC_T_NONE;
1585 } else
1586 request_count = blk_plug_queued_count(q);
07068d5b 1587
bd166ef1
JA
1588 if (blk_mq_sched_bio_merge(q, bio))
1589 return BLK_QC_T_NONE;
1590
87760e5e
JA
1591 wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1592
bd166ef1
JA
1593 trace_block_getrq(q, bio, bio->bi_opf);
1594
1595 rq = blk_mq_sched_get_request(q, bio, bio->bi_opf, &data);
87760e5e
JA
1596 if (unlikely(!rq)) {
1597 __wbt_done(q->rq_wb, wb_acct);
dece1635 1598 return BLK_QC_T_NONE;
87760e5e
JA
1599 }
1600
1601 wbt_track(&rq->issue_stat, wb_acct);
320ae51f 1602
fd2d3326 1603 cookie = request_to_qc_t(data.hctx, rq);
320ae51f
JA
1604
1605 if (unlikely(is_flush_fua)) {
0c2a6fe4
JA
1606 if (q->elevator)
1607 goto elv_insert;
320ae51f 1608 blk_mq_bio_to_request(rq, bio);
320ae51f 1609 blk_insert_flush(rq);
0c2a6fe4 1610 goto run_queue;
320ae51f
JA
1611 }
1612
1613 /*
1614 * A task plug currently exists. Since this is completely lockless,
1615 * utilize that to temporarily store requests until the task is
1616 * either done or scheduled away.
1617 */
e6c4438b
JM
1618 plug = current->plug;
1619 if (plug) {
600271d9
SL
1620 struct request *last = NULL;
1621
e6c4438b 1622 blk_mq_bio_to_request(rq, bio);
0a6219a9
ML
1623
1624 /*
1625 * @request_count may become stale because of schedule
1626 * out, so check the list again.
1627 */
1628 if (list_empty(&plug->mq_list))
1629 request_count = 0;
676d0607 1630 if (!request_count)
e6c4438b 1631 trace_block_plug(q);
600271d9
SL
1632 else
1633 last = list_entry_rq(plug->mq_list.prev);
b094f89c
JA
1634
1635 blk_mq_put_ctx(data.ctx);
1636
600271d9
SL
1637 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
1638 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
e6c4438b
JM
1639 blk_flush_plug_list(plug, false);
1640 trace_block_plug(q);
320ae51f 1641 }
b094f89c 1642
e6c4438b 1643 list_add_tail(&rq->queuelist, &plug->mq_list);
7b371636 1644 return cookie;
320ae51f
JA
1645 }
1646
bd166ef1 1647 if (q->elevator) {
0c2a6fe4 1648elv_insert:
bd166ef1
JA
1649 blk_mq_put_ctx(data.ctx);
1650 blk_mq_bio_to_request(rq, bio);
0abad774 1651 blk_mq_sched_insert_request(rq, false, true,
bd6737f1 1652 !is_sync || is_flush_fua, true);
bd166ef1
JA
1653 goto done;
1654 }
07068d5b
JA
1655 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1656 /*
1657 * For a SYNC request, send it to the hardware immediately. For
1658 * an ASYNC request, just ensure that we run it later on. The
1659 * latter allows for merging opportunities and more efficient
1660 * dispatching.
1661 */
0c2a6fe4 1662run_queue:
07068d5b 1663 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1664 }
1665
07068d5b 1666 blk_mq_put_ctx(data.ctx);
bd166ef1 1667done:
7b371636 1668 return cookie;
320ae51f
JA
1669}
1670
cc71a6f4
JA
1671void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1672 unsigned int hctx_idx)
95363efd 1673{
e9b267d9 1674 struct page *page;
320ae51f 1675
24d2f903 1676 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1677 int i;
320ae51f 1678
24d2f903 1679 for (i = 0; i < tags->nr_tags; i++) {
2af8cbe3
JA
1680 struct request *rq = tags->static_rqs[i];
1681
1682 if (!rq)
e9b267d9 1683 continue;
2af8cbe3 1684 set->ops->exit_request(set->driver_data, rq,
24d2f903 1685 hctx_idx, i);
2af8cbe3 1686 tags->static_rqs[i] = NULL;
e9b267d9 1687 }
320ae51f 1688 }
320ae51f 1689
24d2f903
CH
1690 while (!list_empty(&tags->page_list)) {
1691 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1692 list_del_init(&page->lru);
f75782e4
CM
1693 /*
1694 * Remove kmemleak object previously allocated in
1695 * blk_mq_init_rq_map().
1696 */
1697 kmemleak_free(page_address(page));
320ae51f
JA
1698 __free_pages(page, page->private);
1699 }
cc71a6f4 1700}
320ae51f 1701
cc71a6f4
JA
1702void blk_mq_free_rq_map(struct blk_mq_tags *tags)
1703{
24d2f903 1704 kfree(tags->rqs);
cc71a6f4 1705 tags->rqs = NULL;
2af8cbe3
JA
1706 kfree(tags->static_rqs);
1707 tags->static_rqs = NULL;
320ae51f 1708
24d2f903 1709 blk_mq_free_tags(tags);
320ae51f
JA
1710}
1711
cc71a6f4
JA
1712struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
1713 unsigned int hctx_idx,
1714 unsigned int nr_tags,
1715 unsigned int reserved_tags)
320ae51f 1716{
24d2f903 1717 struct blk_mq_tags *tags;
59f082e4 1718 int node;
320ae51f 1719
59f082e4
SL
1720 node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
1721 if (node == NUMA_NO_NODE)
1722 node = set->numa_node;
1723
1724 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
24391c0d 1725 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
24d2f903
CH
1726 if (!tags)
1727 return NULL;
320ae51f 1728
cc71a6f4 1729 tags->rqs = kzalloc_node(nr_tags * sizeof(struct request *),
36e1f3d1 1730 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
59f082e4 1731 node);
24d2f903
CH
1732 if (!tags->rqs) {
1733 blk_mq_free_tags(tags);
1734 return NULL;
1735 }
320ae51f 1736
2af8cbe3
JA
1737 tags->static_rqs = kzalloc_node(nr_tags * sizeof(struct request *),
1738 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
59f082e4 1739 node);
2af8cbe3
JA
1740 if (!tags->static_rqs) {
1741 kfree(tags->rqs);
1742 blk_mq_free_tags(tags);
1743 return NULL;
1744 }
1745
cc71a6f4
JA
1746 return tags;
1747}
1748
1749static size_t order_to_size(unsigned int order)
1750{
1751 return (size_t)PAGE_SIZE << order;
1752}
1753
1754int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1755 unsigned int hctx_idx, unsigned int depth)
1756{
1757 unsigned int i, j, entries_per_page, max_order = 4;
1758 size_t rq_size, left;
59f082e4
SL
1759 int node;
1760
1761 node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
1762 if (node == NUMA_NO_NODE)
1763 node = set->numa_node;
cc71a6f4
JA
1764
1765 INIT_LIST_HEAD(&tags->page_list);
1766
320ae51f
JA
1767 /*
1768 * rq_size is the size of the request plus driver payload, rounded
1769 * to the cacheline size
1770 */
24d2f903 1771 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1772 cache_line_size());
cc71a6f4 1773 left = rq_size * depth;
320ae51f 1774
cc71a6f4 1775 for (i = 0; i < depth; ) {
320ae51f
JA
1776 int this_order = max_order;
1777 struct page *page;
1778 int to_do;
1779 void *p;
1780
b3a834b1 1781 while (this_order && left < order_to_size(this_order - 1))
320ae51f
JA
1782 this_order--;
1783
1784 do {
59f082e4 1785 page = alloc_pages_node(node,
36e1f3d1 1786 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
a5164405 1787 this_order);
320ae51f
JA
1788 if (page)
1789 break;
1790 if (!this_order--)
1791 break;
1792 if (order_to_size(this_order) < rq_size)
1793 break;
1794 } while (1);
1795
1796 if (!page)
24d2f903 1797 goto fail;
320ae51f
JA
1798
1799 page->private = this_order;
24d2f903 1800 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1801
1802 p = page_address(page);
f75782e4
CM
1803 /*
1804 * Allow kmemleak to scan these pages as they contain pointers
1805 * to additional allocations like via ops->init_request().
1806 */
36e1f3d1 1807 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
320ae51f 1808 entries_per_page = order_to_size(this_order) / rq_size;
cc71a6f4 1809 to_do = min(entries_per_page, depth - i);
320ae51f
JA
1810 left -= to_do * rq_size;
1811 for (j = 0; j < to_do; j++) {
2af8cbe3
JA
1812 struct request *rq = p;
1813
1814 tags->static_rqs[i] = rq;
24d2f903
CH
1815 if (set->ops->init_request) {
1816 if (set->ops->init_request(set->driver_data,
2af8cbe3 1817 rq, hctx_idx, i,
59f082e4 1818 node)) {
2af8cbe3 1819 tags->static_rqs[i] = NULL;
24d2f903 1820 goto fail;
a5164405 1821 }
e9b267d9
CH
1822 }
1823
320ae51f
JA
1824 p += rq_size;
1825 i++;
1826 }
1827 }
cc71a6f4 1828 return 0;
320ae51f 1829
24d2f903 1830fail:
cc71a6f4
JA
1831 blk_mq_free_rqs(set, tags, hctx_idx);
1832 return -ENOMEM;
320ae51f
JA
1833}
1834
e57690fe
JA
1835/*
1836 * 'cpu' is going away. splice any existing rq_list entries from this
1837 * software queue to the hw queue dispatch list, and ensure that it
1838 * gets run.
1839 */
9467f859 1840static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
484b4061 1841{
9467f859 1842 struct blk_mq_hw_ctx *hctx;
484b4061
JA
1843 struct blk_mq_ctx *ctx;
1844 LIST_HEAD(tmp);
1845
9467f859 1846 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
e57690fe 1847 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
484b4061
JA
1848
1849 spin_lock(&ctx->lock);
1850 if (!list_empty(&ctx->rq_list)) {
1851 list_splice_init(&ctx->rq_list, &tmp);
1852 blk_mq_hctx_clear_pending(hctx, ctx);
1853 }
1854 spin_unlock(&ctx->lock);
1855
1856 if (list_empty(&tmp))
9467f859 1857 return 0;
484b4061 1858
e57690fe
JA
1859 spin_lock(&hctx->lock);
1860 list_splice_tail_init(&tmp, &hctx->dispatch);
1861 spin_unlock(&hctx->lock);
484b4061
JA
1862
1863 blk_mq_run_hw_queue(hctx, true);
9467f859 1864 return 0;
484b4061
JA
1865}
1866
9467f859 1867static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
484b4061 1868{
9467f859
TG
1869 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
1870 &hctx->cpuhp_dead);
484b4061
JA
1871}
1872
c3b4afca 1873/* hctx->ctxs will be freed in queue's release handler */
08e98fc6
ML
1874static void blk_mq_exit_hctx(struct request_queue *q,
1875 struct blk_mq_tag_set *set,
1876 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1877{
f70ced09
ML
1878 unsigned flush_start_tag = set->queue_depth;
1879
08e98fc6
ML
1880 blk_mq_tag_idle(hctx);
1881
f70ced09
ML
1882 if (set->ops->exit_request)
1883 set->ops->exit_request(set->driver_data,
1884 hctx->fq->flush_rq, hctx_idx,
1885 flush_start_tag + hctx_idx);
1886
08e98fc6
ML
1887 if (set->ops->exit_hctx)
1888 set->ops->exit_hctx(hctx, hctx_idx);
1889
6a83e74d
BVA
1890 if (hctx->flags & BLK_MQ_F_BLOCKING)
1891 cleanup_srcu_struct(&hctx->queue_rq_srcu);
1892
9467f859 1893 blk_mq_remove_cpuhp(hctx);
f70ced09 1894 blk_free_flush_queue(hctx->fq);
88459642 1895 sbitmap_free(&hctx->ctx_map);
08e98fc6
ML
1896}
1897
624dbe47
ML
1898static void blk_mq_exit_hw_queues(struct request_queue *q,
1899 struct blk_mq_tag_set *set, int nr_queue)
1900{
1901 struct blk_mq_hw_ctx *hctx;
1902 unsigned int i;
1903
1904 queue_for_each_hw_ctx(q, hctx, i) {
1905 if (i == nr_queue)
1906 break;
08e98fc6 1907 blk_mq_exit_hctx(q, set, hctx, i);
624dbe47 1908 }
624dbe47
ML
1909}
1910
1911static void blk_mq_free_hw_queues(struct request_queue *q,
1912 struct blk_mq_tag_set *set)
1913{
1914 struct blk_mq_hw_ctx *hctx;
1915 unsigned int i;
1916
e09aae7e 1917 queue_for_each_hw_ctx(q, hctx, i)
624dbe47 1918 free_cpumask_var(hctx->cpumask);
624dbe47
ML
1919}
1920
08e98fc6
ML
1921static int blk_mq_init_hctx(struct request_queue *q,
1922 struct blk_mq_tag_set *set,
1923 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
320ae51f 1924{
08e98fc6 1925 int node;
f70ced09 1926 unsigned flush_start_tag = set->queue_depth;
08e98fc6
ML
1927
1928 node = hctx->numa_node;
1929 if (node == NUMA_NO_NODE)
1930 node = hctx->numa_node = set->numa_node;
1931
27489a3c 1932 INIT_WORK(&hctx->run_work, blk_mq_run_work_fn);
08e98fc6
ML
1933 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1934 spin_lock_init(&hctx->lock);
1935 INIT_LIST_HEAD(&hctx->dispatch);
1936 hctx->queue = q;
1937 hctx->queue_num = hctx_idx;
2404e607 1938 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
08e98fc6 1939
9467f859 1940 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
08e98fc6
ML
1941
1942 hctx->tags = set->tags[hctx_idx];
320ae51f
JA
1943
1944 /*
08e98fc6
ML
1945 * Allocate space for all possible cpus to avoid allocation at
1946 * runtime
320ae51f 1947 */
08e98fc6
ML
1948 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1949 GFP_KERNEL, node);
1950 if (!hctx->ctxs)
1951 goto unregister_cpu_notifier;
320ae51f 1952
88459642
OS
1953 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL,
1954 node))
08e98fc6 1955 goto free_ctxs;
320ae51f 1956
08e98fc6 1957 hctx->nr_ctx = 0;
320ae51f 1958
08e98fc6
ML
1959 if (set->ops->init_hctx &&
1960 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1961 goto free_bitmap;
320ae51f 1962
f70ced09
ML
1963 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1964 if (!hctx->fq)
1965 goto exit_hctx;
320ae51f 1966
f70ced09
ML
1967 if (set->ops->init_request &&
1968 set->ops->init_request(set->driver_data,
1969 hctx->fq->flush_rq, hctx_idx,
1970 flush_start_tag + hctx_idx, node))
1971 goto free_fq;
320ae51f 1972
6a83e74d
BVA
1973 if (hctx->flags & BLK_MQ_F_BLOCKING)
1974 init_srcu_struct(&hctx->queue_rq_srcu);
1975
08e98fc6 1976 return 0;
320ae51f 1977
f70ced09
ML
1978 free_fq:
1979 kfree(hctx->fq);
1980 exit_hctx:
1981 if (set->ops->exit_hctx)
1982 set->ops->exit_hctx(hctx, hctx_idx);
08e98fc6 1983 free_bitmap:
88459642 1984 sbitmap_free(&hctx->ctx_map);
08e98fc6
ML
1985 free_ctxs:
1986 kfree(hctx->ctxs);
1987 unregister_cpu_notifier:
9467f859 1988 blk_mq_remove_cpuhp(hctx);
08e98fc6
ML
1989 return -1;
1990}
320ae51f 1991
320ae51f
JA
1992static void blk_mq_init_cpu_queues(struct request_queue *q,
1993 unsigned int nr_hw_queues)
1994{
1995 unsigned int i;
1996
1997 for_each_possible_cpu(i) {
1998 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1999 struct blk_mq_hw_ctx *hctx;
2000
2001 memset(__ctx, 0, sizeof(*__ctx));
2002 __ctx->cpu = i;
2003 spin_lock_init(&__ctx->lock);
2004 INIT_LIST_HEAD(&__ctx->rq_list);
2005 __ctx->queue = q;
cf43e6be
JA
2006 blk_stat_init(&__ctx->stat[BLK_STAT_READ]);
2007 blk_stat_init(&__ctx->stat[BLK_STAT_WRITE]);
320ae51f
JA
2008
2009 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
2010 if (!cpu_online(i))
2011 continue;
2012
7d7e0f90 2013 hctx = blk_mq_map_queue(q, i);
e4043dcf 2014
320ae51f
JA
2015 /*
2016 * Set local node, IFF we have more than one hw queue. If
2017 * not, we remain on the home node of the device
2018 */
2019 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
bffed457 2020 hctx->numa_node = local_memory_node(cpu_to_node(i));
320ae51f
JA
2021 }
2022}
2023
cc71a6f4
JA
2024static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
2025{
2026 int ret = 0;
2027
2028 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2029 set->queue_depth, set->reserved_tags);
2030 if (!set->tags[hctx_idx])
2031 return false;
2032
2033 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2034 set->queue_depth);
2035 if (!ret)
2036 return true;
2037
2038 blk_mq_free_rq_map(set->tags[hctx_idx]);
2039 set->tags[hctx_idx] = NULL;
2040 return false;
2041}
2042
2043static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2044 unsigned int hctx_idx)
2045{
bd166ef1
JA
2046 if (set->tags[hctx_idx]) {
2047 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2048 blk_mq_free_rq_map(set->tags[hctx_idx]);
2049 set->tags[hctx_idx] = NULL;
2050 }
cc71a6f4
JA
2051}
2052
5778322e
AM
2053static void blk_mq_map_swqueue(struct request_queue *q,
2054 const struct cpumask *online_mask)
320ae51f 2055{
d1b1cea1 2056 unsigned int i, hctx_idx;
320ae51f
JA
2057 struct blk_mq_hw_ctx *hctx;
2058 struct blk_mq_ctx *ctx;
2a34c087 2059 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 2060
60de074b
AM
2061 /*
2062 * Avoid others reading imcomplete hctx->cpumask through sysfs
2063 */
2064 mutex_lock(&q->sysfs_lock);
2065
320ae51f 2066 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 2067 cpumask_clear(hctx->cpumask);
320ae51f
JA
2068 hctx->nr_ctx = 0;
2069 }
2070
2071 /*
2072 * Map software to hardware queues
2073 */
897bb0c7 2074 for_each_possible_cpu(i) {
320ae51f 2075 /* If the cpu isn't online, the cpu is mapped to first hctx */
5778322e 2076 if (!cpumask_test_cpu(i, online_mask))
e4043dcf
JA
2077 continue;
2078
d1b1cea1
GKB
2079 hctx_idx = q->mq_map[i];
2080 /* unmapped hw queue can be remapped after CPU topo changed */
cc71a6f4
JA
2081 if (!set->tags[hctx_idx] &&
2082 !__blk_mq_alloc_rq_map(set, hctx_idx)) {
d1b1cea1
GKB
2083 /*
2084 * If tags initialization fail for some hctx,
2085 * that hctx won't be brought online. In this
2086 * case, remap the current ctx to hctx[0] which
2087 * is guaranteed to always have tags allocated
2088 */
cc71a6f4 2089 q->mq_map[i] = 0;
d1b1cea1
GKB
2090 }
2091
897bb0c7 2092 ctx = per_cpu_ptr(q->queue_ctx, i);
7d7e0f90 2093 hctx = blk_mq_map_queue(q, i);
868f2f0b 2094
e4043dcf 2095 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
2096 ctx->index_hw = hctx->nr_ctx;
2097 hctx->ctxs[hctx->nr_ctx++] = ctx;
2098 }
506e931f 2099
60de074b
AM
2100 mutex_unlock(&q->sysfs_lock);
2101
506e931f 2102 queue_for_each_hw_ctx(q, hctx, i) {
484b4061 2103 /*
a68aafa5
JA
2104 * If no software queues are mapped to this hardware queue,
2105 * disable it and free the request entries.
484b4061
JA
2106 */
2107 if (!hctx->nr_ctx) {
d1b1cea1
GKB
2108 /* Never unmap queue 0. We need it as a
2109 * fallback in case of a new remap fails
2110 * allocation
2111 */
cc71a6f4
JA
2112 if (i && set->tags[i])
2113 blk_mq_free_map_and_requests(set, i);
2114
2a34c087 2115 hctx->tags = NULL;
484b4061
JA
2116 continue;
2117 }
2118
2a34c087
ML
2119 hctx->tags = set->tags[i];
2120 WARN_ON(!hctx->tags);
2121
889fa31f
CY
2122 /*
2123 * Set the map size to the number of mapped software queues.
2124 * This is more accurate and more efficient than looping
2125 * over all possibly mapped software queues.
2126 */
88459642 2127 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
889fa31f 2128
484b4061
JA
2129 /*
2130 * Initialize batch roundrobin counts
2131 */
506e931f
JA
2132 hctx->next_cpu = cpumask_first(hctx->cpumask);
2133 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2134 }
320ae51f
JA
2135}
2136
2404e607 2137static void queue_set_hctx_shared(struct request_queue *q, bool shared)
0d2602ca
JA
2138{
2139 struct blk_mq_hw_ctx *hctx;
0d2602ca
JA
2140 int i;
2141
2404e607
JM
2142 queue_for_each_hw_ctx(q, hctx, i) {
2143 if (shared)
2144 hctx->flags |= BLK_MQ_F_TAG_SHARED;
2145 else
2146 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2147 }
2148}
2149
2150static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
2151{
2152 struct request_queue *q;
0d2602ca
JA
2153
2154 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2155 blk_mq_freeze_queue(q);
2404e607 2156 queue_set_hctx_shared(q, shared);
0d2602ca
JA
2157 blk_mq_unfreeze_queue(q);
2158 }
2159}
2160
2161static void blk_mq_del_queue_tag_set(struct request_queue *q)
2162{
2163 struct blk_mq_tag_set *set = q->tag_set;
2164
0d2602ca
JA
2165 mutex_lock(&set->tag_list_lock);
2166 list_del_init(&q->tag_set_list);
2404e607
JM
2167 if (list_is_singular(&set->tag_list)) {
2168 /* just transitioned to unshared */
2169 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2170 /* update existing queue */
2171 blk_mq_update_tag_set_depth(set, false);
2172 }
0d2602ca 2173 mutex_unlock(&set->tag_list_lock);
0d2602ca
JA
2174}
2175
2176static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2177 struct request_queue *q)
2178{
2179 q->tag_set = set;
2180
2181 mutex_lock(&set->tag_list_lock);
2404e607
JM
2182
2183 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
2184 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
2185 set->flags |= BLK_MQ_F_TAG_SHARED;
2186 /* update existing queue */
2187 blk_mq_update_tag_set_depth(set, true);
2188 }
2189 if (set->flags & BLK_MQ_F_TAG_SHARED)
2190 queue_set_hctx_shared(q, true);
0d2602ca 2191 list_add_tail(&q->tag_set_list, &set->tag_list);
2404e607 2192
0d2602ca
JA
2193 mutex_unlock(&set->tag_list_lock);
2194}
2195
e09aae7e
ML
2196/*
2197 * It is the actual release handler for mq, but we do it from
2198 * request queue's release handler for avoiding use-after-free
2199 * and headache because q->mq_kobj shouldn't have been introduced,
2200 * but we can't group ctx/kctx kobj without it.
2201 */
2202void blk_mq_release(struct request_queue *q)
2203{
2204 struct blk_mq_hw_ctx *hctx;
2205 unsigned int i;
2206
bd166ef1
JA
2207 blk_mq_sched_teardown(q);
2208
e09aae7e 2209 /* hctx kobj stays in hctx */
c3b4afca
ML
2210 queue_for_each_hw_ctx(q, hctx, i) {
2211 if (!hctx)
2212 continue;
2213 kfree(hctx->ctxs);
e09aae7e 2214 kfree(hctx);
c3b4afca 2215 }
e09aae7e 2216
a723bab3
AM
2217 q->mq_map = NULL;
2218
e09aae7e
ML
2219 kfree(q->queue_hw_ctx);
2220
2221 /* ctx kobj stays in queue_ctx */
2222 free_percpu(q->queue_ctx);
2223}
2224
24d2f903 2225struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
b62c21b7
MS
2226{
2227 struct request_queue *uninit_q, *q;
2228
2229 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
2230 if (!uninit_q)
2231 return ERR_PTR(-ENOMEM);
2232
2233 q = blk_mq_init_allocated_queue(set, uninit_q);
2234 if (IS_ERR(q))
2235 blk_cleanup_queue(uninit_q);
2236
2237 return q;
2238}
2239EXPORT_SYMBOL(blk_mq_init_queue);
2240
868f2f0b
KB
2241static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2242 struct request_queue *q)
320ae51f 2243{
868f2f0b
KB
2244 int i, j;
2245 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
f14bbe77 2246
868f2f0b 2247 blk_mq_sysfs_unregister(q);
24d2f903 2248 for (i = 0; i < set->nr_hw_queues; i++) {
868f2f0b 2249 int node;
f14bbe77 2250
868f2f0b
KB
2251 if (hctxs[i])
2252 continue;
2253
2254 node = blk_mq_hw_queue_to_node(q->mq_map, i);
cdef54dd
CH
2255 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
2256 GFP_KERNEL, node);
320ae51f 2257 if (!hctxs[i])
868f2f0b 2258 break;
320ae51f 2259
a86073e4 2260 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
868f2f0b
KB
2261 node)) {
2262 kfree(hctxs[i]);
2263 hctxs[i] = NULL;
2264 break;
2265 }
e4043dcf 2266
0d2602ca 2267 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 2268 hctxs[i]->numa_node = node;
320ae51f 2269 hctxs[i]->queue_num = i;
868f2f0b
KB
2270
2271 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2272 free_cpumask_var(hctxs[i]->cpumask);
2273 kfree(hctxs[i]);
2274 hctxs[i] = NULL;
2275 break;
2276 }
2277 blk_mq_hctx_kobj_init(hctxs[i]);
320ae51f 2278 }
868f2f0b
KB
2279 for (j = i; j < q->nr_hw_queues; j++) {
2280 struct blk_mq_hw_ctx *hctx = hctxs[j];
2281
2282 if (hctx) {
cc71a6f4
JA
2283 if (hctx->tags)
2284 blk_mq_free_map_and_requests(set, j);
868f2f0b
KB
2285 blk_mq_exit_hctx(q, set, hctx, j);
2286 free_cpumask_var(hctx->cpumask);
2287 kobject_put(&hctx->kobj);
2288 kfree(hctx->ctxs);
2289 kfree(hctx);
2290 hctxs[j] = NULL;
2291
2292 }
2293 }
2294 q->nr_hw_queues = i;
2295 blk_mq_sysfs_register(q);
2296}
2297
2298struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2299 struct request_queue *q)
2300{
66841672
ML
2301 /* mark the queue as mq asap */
2302 q->mq_ops = set->ops;
2303
868f2f0b
KB
2304 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2305 if (!q->queue_ctx)
c7de5726 2306 goto err_exit;
868f2f0b
KB
2307
2308 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2309 GFP_KERNEL, set->numa_node);
2310 if (!q->queue_hw_ctx)
2311 goto err_percpu;
2312
bdd17e75 2313 q->mq_map = set->mq_map;
868f2f0b
KB
2314
2315 blk_mq_realloc_hw_ctxs(set, q);
2316 if (!q->nr_hw_queues)
2317 goto err_hctxs;
320ae51f 2318
287922eb 2319 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
e56f698b 2320 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
320ae51f
JA
2321
2322 q->nr_queues = nr_cpu_ids;
320ae51f 2323
94eddfbe 2324 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 2325
05f1dd53
JA
2326 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2327 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2328
1be036e9
CH
2329 q->sg_reserved_size = INT_MAX;
2330
2849450a 2331 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
6fca6a61
CH
2332 INIT_LIST_HEAD(&q->requeue_list);
2333 spin_lock_init(&q->requeue_lock);
2334
07068d5b
JA
2335 if (q->nr_hw_queues > 1)
2336 blk_queue_make_request(q, blk_mq_make_request);
2337 else
2338 blk_queue_make_request(q, blk_sq_make_request);
2339
eba71768
JA
2340 /*
2341 * Do this after blk_queue_make_request() overrides it...
2342 */
2343 q->nr_requests = set->queue_depth;
2344
64f1c21e
JA
2345 /*
2346 * Default to classic polling
2347 */
2348 q->poll_nsec = -1;
2349
24d2f903
CH
2350 if (set->ops->complete)
2351 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 2352
24d2f903 2353 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 2354
5778322e 2355 get_online_cpus();
320ae51f 2356 mutex_lock(&all_q_mutex);
320ae51f 2357
4593fdbe 2358 list_add_tail(&q->all_q_node, &all_q_list);
0d2602ca 2359 blk_mq_add_queue_tag_set(set, q);
5778322e 2360 blk_mq_map_swqueue(q, cpu_online_mask);
484b4061 2361
4593fdbe 2362 mutex_unlock(&all_q_mutex);
5778322e 2363 put_online_cpus();
4593fdbe 2364
d3484991
JA
2365 if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
2366 int ret;
2367
2368 ret = blk_mq_sched_init(q);
2369 if (ret)
2370 return ERR_PTR(ret);
2371 }
2372
320ae51f 2373 return q;
18741986 2374
320ae51f 2375err_hctxs:
868f2f0b 2376 kfree(q->queue_hw_ctx);
320ae51f 2377err_percpu:
868f2f0b 2378 free_percpu(q->queue_ctx);
c7de5726
ML
2379err_exit:
2380 q->mq_ops = NULL;
320ae51f
JA
2381 return ERR_PTR(-ENOMEM);
2382}
b62c21b7 2383EXPORT_SYMBOL(blk_mq_init_allocated_queue);
320ae51f
JA
2384
2385void blk_mq_free_queue(struct request_queue *q)
2386{
624dbe47 2387 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 2388
0e626368
AM
2389 mutex_lock(&all_q_mutex);
2390 list_del_init(&q->all_q_node);
2391 mutex_unlock(&all_q_mutex);
2392
87760e5e
JA
2393 wbt_exit(q);
2394
0d2602ca
JA
2395 blk_mq_del_queue_tag_set(q);
2396
624dbe47
ML
2397 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2398 blk_mq_free_hw_queues(q, set);
320ae51f 2399}
320ae51f
JA
2400
2401/* Basically redo blk_mq_init_queue with queue frozen */
5778322e
AM
2402static void blk_mq_queue_reinit(struct request_queue *q,
2403 const struct cpumask *online_mask)
320ae51f 2404{
4ecd4fef 2405 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
320ae51f 2406
67aec14c
JA
2407 blk_mq_sysfs_unregister(q);
2408
320ae51f
JA
2409 /*
2410 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2411 * we should change hctx numa_node according to new topology (this
2412 * involves free and re-allocate memory, worthy doing?)
2413 */
2414
5778322e 2415 blk_mq_map_swqueue(q, online_mask);
320ae51f 2416
67aec14c 2417 blk_mq_sysfs_register(q);
320ae51f
JA
2418}
2419
65d5291e
SAS
2420/*
2421 * New online cpumask which is going to be set in this hotplug event.
2422 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2423 * one-by-one and dynamically allocating this could result in a failure.
2424 */
2425static struct cpumask cpuhp_online_new;
2426
2427static void blk_mq_queue_reinit_work(void)
320ae51f
JA
2428{
2429 struct request_queue *q;
320ae51f
JA
2430
2431 mutex_lock(&all_q_mutex);
f3af020b
TH
2432 /*
2433 * We need to freeze and reinit all existing queues. Freezing
2434 * involves synchronous wait for an RCU grace period and doing it
2435 * one by one may take a long time. Start freezing all queues in
2436 * one swoop and then wait for the completions so that freezing can
2437 * take place in parallel.
2438 */
2439 list_for_each_entry(q, &all_q_list, all_q_node)
2440 blk_mq_freeze_queue_start(q);
415d3dab 2441 list_for_each_entry(q, &all_q_list, all_q_node)
f3af020b
TH
2442 blk_mq_freeze_queue_wait(q);
2443
320ae51f 2444 list_for_each_entry(q, &all_q_list, all_q_node)
65d5291e 2445 blk_mq_queue_reinit(q, &cpuhp_online_new);
f3af020b
TH
2446
2447 list_for_each_entry(q, &all_q_list, all_q_node)
2448 blk_mq_unfreeze_queue(q);
2449
320ae51f 2450 mutex_unlock(&all_q_mutex);
65d5291e
SAS
2451}
2452
2453static int blk_mq_queue_reinit_dead(unsigned int cpu)
2454{
97a32864 2455 cpumask_copy(&cpuhp_online_new, cpu_online_mask);
65d5291e
SAS
2456 blk_mq_queue_reinit_work();
2457 return 0;
2458}
2459
2460/*
2461 * Before hotadded cpu starts handling requests, new mappings must be
2462 * established. Otherwise, these requests in hw queue might never be
2463 * dispatched.
2464 *
2465 * For example, there is a single hw queue (hctx) and two CPU queues (ctx0
2466 * for CPU0, and ctx1 for CPU1).
2467 *
2468 * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list
2469 * and set bit0 in pending bitmap as ctx1->index_hw is still zero.
2470 *
2c3ad667
JA
2471 * And then while running hw queue, blk_mq_flush_busy_ctxs() finds bit0 is set
2472 * in pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list.
2473 * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list is
2474 * ignored.
65d5291e
SAS
2475 */
2476static int blk_mq_queue_reinit_prepare(unsigned int cpu)
2477{
2478 cpumask_copy(&cpuhp_online_new, cpu_online_mask);
2479 cpumask_set_cpu(cpu, &cpuhp_online_new);
2480 blk_mq_queue_reinit_work();
2481 return 0;
320ae51f
JA
2482}
2483
a5164405
JA
2484static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2485{
2486 int i;
2487
cc71a6f4
JA
2488 for (i = 0; i < set->nr_hw_queues; i++)
2489 if (!__blk_mq_alloc_rq_map(set, i))
a5164405 2490 goto out_unwind;
a5164405
JA
2491
2492 return 0;
2493
2494out_unwind:
2495 while (--i >= 0)
cc71a6f4 2496 blk_mq_free_rq_map(set->tags[i]);
a5164405 2497
a5164405
JA
2498 return -ENOMEM;
2499}
2500
2501/*
2502 * Allocate the request maps associated with this tag_set. Note that this
2503 * may reduce the depth asked for, if memory is tight. set->queue_depth
2504 * will be updated to reflect the allocated depth.
2505 */
2506static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2507{
2508 unsigned int depth;
2509 int err;
2510
2511 depth = set->queue_depth;
2512 do {
2513 err = __blk_mq_alloc_rq_maps(set);
2514 if (!err)
2515 break;
2516
2517 set->queue_depth >>= 1;
2518 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2519 err = -ENOMEM;
2520 break;
2521 }
2522 } while (set->queue_depth);
2523
2524 if (!set->queue_depth || err) {
2525 pr_err("blk-mq: failed to allocate request map\n");
2526 return -ENOMEM;
2527 }
2528
2529 if (depth != set->queue_depth)
2530 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2531 depth, set->queue_depth);
2532
2533 return 0;
2534}
2535
a4391c64
JA
2536/*
2537 * Alloc a tag set to be associated with one or more request queues.
2538 * May fail with EINVAL for various error conditions. May adjust the
2539 * requested depth down, if if it too large. In that case, the set
2540 * value will be stored in set->queue_depth.
2541 */
24d2f903
CH
2542int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2543{
da695ba2
CH
2544 int ret;
2545
205fb5f5
BVA
2546 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2547
24d2f903
CH
2548 if (!set->nr_hw_queues)
2549 return -EINVAL;
a4391c64 2550 if (!set->queue_depth)
24d2f903
CH
2551 return -EINVAL;
2552 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2553 return -EINVAL;
2554
7d7e0f90 2555 if (!set->ops->queue_rq)
24d2f903
CH
2556 return -EINVAL;
2557
a4391c64
JA
2558 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2559 pr_info("blk-mq: reduced tag depth to %u\n",
2560 BLK_MQ_MAX_DEPTH);
2561 set->queue_depth = BLK_MQ_MAX_DEPTH;
2562 }
24d2f903 2563
6637fadf
SL
2564 /*
2565 * If a crashdump is active, then we are potentially in a very
2566 * memory constrained environment. Limit us to 1 queue and
2567 * 64 tags to prevent using too much memory.
2568 */
2569 if (is_kdump_kernel()) {
2570 set->nr_hw_queues = 1;
2571 set->queue_depth = min(64U, set->queue_depth);
2572 }
868f2f0b
KB
2573 /*
2574 * There is no use for more h/w queues than cpus.
2575 */
2576 if (set->nr_hw_queues > nr_cpu_ids)
2577 set->nr_hw_queues = nr_cpu_ids;
6637fadf 2578
868f2f0b 2579 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
24d2f903
CH
2580 GFP_KERNEL, set->numa_node);
2581 if (!set->tags)
a5164405 2582 return -ENOMEM;
24d2f903 2583
da695ba2
CH
2584 ret = -ENOMEM;
2585 set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
2586 GFP_KERNEL, set->numa_node);
bdd17e75
CH
2587 if (!set->mq_map)
2588 goto out_free_tags;
2589
da695ba2
CH
2590 if (set->ops->map_queues)
2591 ret = set->ops->map_queues(set);
2592 else
2593 ret = blk_mq_map_queues(set);
2594 if (ret)
2595 goto out_free_mq_map;
2596
2597 ret = blk_mq_alloc_rq_maps(set);
2598 if (ret)
bdd17e75 2599 goto out_free_mq_map;
24d2f903 2600
0d2602ca
JA
2601 mutex_init(&set->tag_list_lock);
2602 INIT_LIST_HEAD(&set->tag_list);
2603
24d2f903 2604 return 0;
bdd17e75
CH
2605
2606out_free_mq_map:
2607 kfree(set->mq_map);
2608 set->mq_map = NULL;
2609out_free_tags:
5676e7b6
RE
2610 kfree(set->tags);
2611 set->tags = NULL;
da695ba2 2612 return ret;
24d2f903
CH
2613}
2614EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2615
2616void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2617{
2618 int i;
2619
cc71a6f4
JA
2620 for (i = 0; i < nr_cpu_ids; i++)
2621 blk_mq_free_map_and_requests(set, i);
484b4061 2622
bdd17e75
CH
2623 kfree(set->mq_map);
2624 set->mq_map = NULL;
2625
981bd189 2626 kfree(set->tags);
5676e7b6 2627 set->tags = NULL;
24d2f903
CH
2628}
2629EXPORT_SYMBOL(blk_mq_free_tag_set);
2630
e3a2b3f9
JA
2631int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2632{
2633 struct blk_mq_tag_set *set = q->tag_set;
2634 struct blk_mq_hw_ctx *hctx;
2635 int i, ret;
2636
bd166ef1 2637 if (!set)
e3a2b3f9
JA
2638 return -EINVAL;
2639
70f36b60
JA
2640 blk_mq_freeze_queue(q);
2641 blk_mq_quiesce_queue(q);
2642
e3a2b3f9
JA
2643 ret = 0;
2644 queue_for_each_hw_ctx(q, hctx, i) {
e9137d4b
KB
2645 if (!hctx->tags)
2646 continue;
bd166ef1
JA
2647 /*
2648 * If we're using an MQ scheduler, just update the scheduler
2649 * queue depth. This is similar to what the old code would do.
2650 */
70f36b60
JA
2651 if (!hctx->sched_tags) {
2652 ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
2653 min(nr, set->queue_depth),
2654 false);
2655 } else {
2656 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
2657 nr, true);
2658 }
e3a2b3f9
JA
2659 if (ret)
2660 break;
2661 }
2662
2663 if (!ret)
2664 q->nr_requests = nr;
2665
70f36b60
JA
2666 blk_mq_unfreeze_queue(q);
2667 blk_mq_start_stopped_hw_queues(q, true);
2668
e3a2b3f9
JA
2669 return ret;
2670}
2671
868f2f0b
KB
2672void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2673{
2674 struct request_queue *q;
2675
2676 if (nr_hw_queues > nr_cpu_ids)
2677 nr_hw_queues = nr_cpu_ids;
2678 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2679 return;
2680
2681 list_for_each_entry(q, &set->tag_list, tag_set_list)
2682 blk_mq_freeze_queue(q);
2683
2684 set->nr_hw_queues = nr_hw_queues;
2685 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2686 blk_mq_realloc_hw_ctxs(set, q);
2687
f6f94300
JB
2688 /*
2689 * Manually set the make_request_fn as blk_queue_make_request
2690 * resets a lot of the queue settings.
2691 */
868f2f0b 2692 if (q->nr_hw_queues > 1)
f6f94300 2693 q->make_request_fn = blk_mq_make_request;
868f2f0b 2694 else
f6f94300 2695 q->make_request_fn = blk_sq_make_request;
868f2f0b
KB
2696
2697 blk_mq_queue_reinit(q, cpu_online_mask);
2698 }
2699
2700 list_for_each_entry(q, &set->tag_list, tag_set_list)
2701 blk_mq_unfreeze_queue(q);
2702}
2703EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2704
64f1c21e
JA
2705static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
2706 struct blk_mq_hw_ctx *hctx,
2707 struct request *rq)
2708{
2709 struct blk_rq_stat stat[2];
2710 unsigned long ret = 0;
2711
2712 /*
2713 * If stats collection isn't on, don't sleep but turn it on for
2714 * future users
2715 */
2716 if (!blk_stat_enable(q))
2717 return 0;
2718
2719 /*
2720 * We don't have to do this once per IO, should optimize this
2721 * to just use the current window of stats until it changes
2722 */
2723 memset(&stat, 0, sizeof(stat));
2724 blk_hctx_stat_get(hctx, stat);
2725
2726 /*
2727 * As an optimistic guess, use half of the mean service time
2728 * for this type of request. We can (and should) make this smarter.
2729 * For instance, if the completion latencies are tight, we can
2730 * get closer than just half the mean. This is especially
2731 * important on devices where the completion latencies are longer
2732 * than ~10 usec.
2733 */
2734 if (req_op(rq) == REQ_OP_READ && stat[BLK_STAT_READ].nr_samples)
2735 ret = (stat[BLK_STAT_READ].mean + 1) / 2;
2736 else if (req_op(rq) == REQ_OP_WRITE && stat[BLK_STAT_WRITE].nr_samples)
2737 ret = (stat[BLK_STAT_WRITE].mean + 1) / 2;
2738
2739 return ret;
2740}
2741
06426adf 2742static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
64f1c21e 2743 struct blk_mq_hw_ctx *hctx,
06426adf
JA
2744 struct request *rq)
2745{
2746 struct hrtimer_sleeper hs;
2747 enum hrtimer_mode mode;
64f1c21e 2748 unsigned int nsecs;
06426adf
JA
2749 ktime_t kt;
2750
64f1c21e
JA
2751 if (test_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags))
2752 return false;
2753
2754 /*
2755 * poll_nsec can be:
2756 *
2757 * -1: don't ever hybrid sleep
2758 * 0: use half of prev avg
2759 * >0: use this specific value
2760 */
2761 if (q->poll_nsec == -1)
2762 return false;
2763 else if (q->poll_nsec > 0)
2764 nsecs = q->poll_nsec;
2765 else
2766 nsecs = blk_mq_poll_nsecs(q, hctx, rq);
2767
2768 if (!nsecs)
06426adf
JA
2769 return false;
2770
2771 set_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
2772
2773 /*
2774 * This will be replaced with the stats tracking code, using
2775 * 'avg_completion_time / 2' as the pre-sleep target.
2776 */
8b0e1953 2777 kt = nsecs;
06426adf
JA
2778
2779 mode = HRTIMER_MODE_REL;
2780 hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
2781 hrtimer_set_expires(&hs.timer, kt);
2782
2783 hrtimer_init_sleeper(&hs, current);
2784 do {
2785 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
2786 break;
2787 set_current_state(TASK_UNINTERRUPTIBLE);
2788 hrtimer_start_expires(&hs.timer, mode);
2789 if (hs.task)
2790 io_schedule();
2791 hrtimer_cancel(&hs.timer);
2792 mode = HRTIMER_MODE_ABS;
2793 } while (hs.task && !signal_pending(current));
2794
2795 __set_current_state(TASK_RUNNING);
2796 destroy_hrtimer_on_stack(&hs.timer);
2797 return true;
2798}
2799
bbd7bb70
JA
2800static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
2801{
2802 struct request_queue *q = hctx->queue;
2803 long state;
2804
06426adf
JA
2805 /*
2806 * If we sleep, have the caller restart the poll loop to reset
2807 * the state. Like for the other success return cases, the
2808 * caller is responsible for checking if the IO completed. If
2809 * the IO isn't complete, we'll get called again and will go
2810 * straight to the busy poll loop.
2811 */
64f1c21e 2812 if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
06426adf
JA
2813 return true;
2814
bbd7bb70
JA
2815 hctx->poll_considered++;
2816
2817 state = current->state;
2818 while (!need_resched()) {
2819 int ret;
2820
2821 hctx->poll_invoked++;
2822
2823 ret = q->mq_ops->poll(hctx, rq->tag);
2824 if (ret > 0) {
2825 hctx->poll_success++;
2826 set_current_state(TASK_RUNNING);
2827 return true;
2828 }
2829
2830 if (signal_pending_state(state, current))
2831 set_current_state(TASK_RUNNING);
2832
2833 if (current->state == TASK_RUNNING)
2834 return true;
2835 if (ret < 0)
2836 break;
2837 cpu_relax();
2838 }
2839
2840 return false;
2841}
2842
2843bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
2844{
2845 struct blk_mq_hw_ctx *hctx;
2846 struct blk_plug *plug;
2847 struct request *rq;
2848
2849 if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
2850 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
2851 return false;
2852
2853 plug = current->plug;
2854 if (plug)
2855 blk_flush_plug_list(plug, false);
2856
2857 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
bd166ef1
JA
2858 if (!blk_qc_t_is_internal(cookie))
2859 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
2860 else
2861 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
bbd7bb70
JA
2862
2863 return __blk_mq_poll(hctx, rq);
2864}
2865EXPORT_SYMBOL_GPL(blk_mq_poll);
2866
676141e4
JA
2867void blk_mq_disable_hotplug(void)
2868{
2869 mutex_lock(&all_q_mutex);
2870}
2871
2872void blk_mq_enable_hotplug(void)
2873{
2874 mutex_unlock(&all_q_mutex);
2875}
2876
320ae51f
JA
2877static int __init blk_mq_init(void)
2878{
9467f859
TG
2879 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
2880 blk_mq_hctx_notify_dead);
320ae51f 2881
65d5291e
SAS
2882 cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE, "block/mq:prepare",
2883 blk_mq_queue_reinit_prepare,
2884 blk_mq_queue_reinit_dead);
320ae51f
JA
2885 return 0;
2886}
2887subsys_initcall(blk_mq_init);