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