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