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