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