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