Merge branch 'drm-fixes-4.8' of git://people.freedesktop.org/~agd5f/linux into drm...
[deliverable/linux.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>
23#include <linux/delay.h>
aedcd72f 24#include <linux/crash_dump.h>
320ae51f
JA
25
26#include <trace/events/block.h>
27
28#include <linux/blk-mq.h>
29#include "blk.h"
30#include "blk-mq.h"
31#include "blk-mq-tag.h"
32
33static DEFINE_MUTEX(all_q_mutex);
34static LIST_HEAD(all_q_list);
35
36static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
37
320ae51f
JA
38/*
39 * Check if any of the ctx's have pending work in this hardware queue
40 */
41static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
42{
43 unsigned int i;
44
569fd0ce 45 for (i = 0; i < hctx->ctx_map.size; i++)
1429d7c9 46 if (hctx->ctx_map.map[i].word)
320ae51f
JA
47 return true;
48
49 return false;
50}
51
1429d7c9
JA
52static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
53 struct blk_mq_ctx *ctx)
54{
55 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
56}
57
58#define CTX_TO_BIT(hctx, ctx) \
59 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
60
320ae51f
JA
61/*
62 * Mark this ctx as having pending work in this hardware queue
63 */
64static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
65 struct blk_mq_ctx *ctx)
66{
1429d7c9
JA
67 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
68
69 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
70 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
71}
72
73static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
74 struct blk_mq_ctx *ctx)
75{
76 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
77
78 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
320ae51f
JA
79}
80
b4c6a028 81void blk_mq_freeze_queue_start(struct request_queue *q)
43a5e4e2 82{
4ecd4fef 83 int freeze_depth;
cddd5d17 84
4ecd4fef
CH
85 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
86 if (freeze_depth == 1) {
3ef28e83 87 percpu_ref_kill(&q->q_usage_counter);
b94ec296 88 blk_mq_run_hw_queues(q, false);
cddd5d17 89 }
f3af020b 90}
b4c6a028 91EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
f3af020b
TH
92
93static void blk_mq_freeze_queue_wait(struct request_queue *q)
94{
3ef28e83 95 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
43a5e4e2
ML
96}
97
f3af020b
TH
98/*
99 * Guarantee no request is in use, so we can change any data structure of
100 * the queue afterward.
101 */
3ef28e83 102void blk_freeze_queue(struct request_queue *q)
f3af020b 103{
3ef28e83
DW
104 /*
105 * In the !blk_mq case we are only calling this to kill the
106 * q_usage_counter, otherwise this increases the freeze depth
107 * and waits for it to return to zero. For this reason there is
108 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
109 * exported to drivers as the only user for unfreeze is blk_mq.
110 */
f3af020b
TH
111 blk_mq_freeze_queue_start(q);
112 blk_mq_freeze_queue_wait(q);
113}
3ef28e83
DW
114
115void blk_mq_freeze_queue(struct request_queue *q)
116{
117 /*
118 * ...just an alias to keep freeze and unfreeze actions balanced
119 * in the blk_mq_* namespace
120 */
121 blk_freeze_queue(q);
122}
c761d96b 123EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
f3af020b 124
b4c6a028 125void blk_mq_unfreeze_queue(struct request_queue *q)
320ae51f 126{
4ecd4fef 127 int freeze_depth;
320ae51f 128
4ecd4fef
CH
129 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
130 WARN_ON_ONCE(freeze_depth < 0);
131 if (!freeze_depth) {
3ef28e83 132 percpu_ref_reinit(&q->q_usage_counter);
320ae51f 133 wake_up_all(&q->mq_freeze_wq);
add703fd 134 }
320ae51f 135}
b4c6a028 136EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
320ae51f 137
aed3ea94
JA
138void blk_mq_wake_waiters(struct request_queue *q)
139{
140 struct blk_mq_hw_ctx *hctx;
141 unsigned int i;
142
143 queue_for_each_hw_ctx(q, hctx, i)
144 if (blk_mq_hw_queue_mapped(hctx))
145 blk_mq_tag_wakeup_all(hctx->tags, true);
3fd5940c
KB
146
147 /*
148 * If we are called because the queue has now been marked as
149 * dying, we need to ensure that processes currently waiting on
150 * the queue are notified as well.
151 */
152 wake_up_all(&q->mq_freeze_wq);
aed3ea94
JA
153}
154
320ae51f
JA
155bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
156{
157 return blk_mq_has_free_tags(hctx->tags);
158}
159EXPORT_SYMBOL(blk_mq_can_queue);
160
94eddfbe 161static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
cc6e3b10
MC
162 struct request *rq, int op,
163 unsigned int op_flags)
320ae51f 164{
94eddfbe 165 if (blk_queue_io_stat(q))
cc6e3b10 166 op_flags |= REQ_IO_STAT;
94eddfbe 167
af76e555
CH
168 INIT_LIST_HEAD(&rq->queuelist);
169 /* csd/requeue_work/fifo_time is initialized before use */
170 rq->q = q;
320ae51f 171 rq->mq_ctx = ctx;
cc6e3b10 172 req_set_op_attrs(rq, op, op_flags);
af76e555
CH
173 /* do not touch atomic flags, it needs atomic ops against the timer */
174 rq->cpu = -1;
af76e555
CH
175 INIT_HLIST_NODE(&rq->hash);
176 RB_CLEAR_NODE(&rq->rb_node);
af76e555
CH
177 rq->rq_disk = NULL;
178 rq->part = NULL;
3ee32372 179 rq->start_time = jiffies;
af76e555
CH
180#ifdef CONFIG_BLK_CGROUP
181 rq->rl = NULL;
0fec08b4 182 set_start_time_ns(rq);
af76e555
CH
183 rq->io_start_time_ns = 0;
184#endif
185 rq->nr_phys_segments = 0;
186#if defined(CONFIG_BLK_DEV_INTEGRITY)
187 rq->nr_integrity_segments = 0;
188#endif
af76e555
CH
189 rq->special = NULL;
190 /* tag was already set */
191 rq->errors = 0;
af76e555 192
6f4a1626
TB
193 rq->cmd = rq->__cmd;
194
af76e555
CH
195 rq->extra_len = 0;
196 rq->sense_len = 0;
197 rq->resid_len = 0;
198 rq->sense = NULL;
199
af76e555 200 INIT_LIST_HEAD(&rq->timeout_list);
f6be4fb4
JA
201 rq->timeout = 0;
202
af76e555
CH
203 rq->end_io = NULL;
204 rq->end_io_data = NULL;
205 rq->next_rq = NULL;
206
d9d8c5c4 207 ctx->rq_dispatched[rw_is_sync(op, op_flags)]++;
320ae51f
JA
208}
209
5dee8577 210static struct request *
cc6e3b10 211__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int op, int op_flags)
5dee8577
CH
212{
213 struct request *rq;
214 unsigned int tag;
215
cb96a42c 216 tag = blk_mq_get_tag(data);
5dee8577 217 if (tag != BLK_MQ_TAG_FAIL) {
cb96a42c 218 rq = data->hctx->tags->rqs[tag];
5dee8577 219
cb96a42c 220 if (blk_mq_tag_busy(data->hctx)) {
5dee8577 221 rq->cmd_flags = REQ_MQ_INFLIGHT;
cb96a42c 222 atomic_inc(&data->hctx->nr_active);
5dee8577
CH
223 }
224
225 rq->tag = tag;
cc6e3b10 226 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op, op_flags);
5dee8577
CH
227 return rq;
228 }
229
230 return NULL;
231}
232
6f3b0e8b
CH
233struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
234 unsigned int flags)
320ae51f 235{
d852564f
CH
236 struct blk_mq_ctx *ctx;
237 struct blk_mq_hw_ctx *hctx;
320ae51f 238 struct request *rq;
cb96a42c 239 struct blk_mq_alloc_data alloc_data;
a492f075 240 int ret;
320ae51f 241
6f3b0e8b 242 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
a492f075
JL
243 if (ret)
244 return ERR_PTR(ret);
320ae51f 245
d852564f
CH
246 ctx = blk_mq_get_ctx(q);
247 hctx = q->mq_ops->map_queue(q, ctx->cpu);
6f3b0e8b 248 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
d852564f 249
cc6e3b10 250 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
6f3b0e8b 251 if (!rq && !(flags & BLK_MQ_REQ_NOWAIT)) {
d852564f
CH
252 __blk_mq_run_hw_queue(hctx);
253 blk_mq_put_ctx(ctx);
254
255 ctx = blk_mq_get_ctx(q);
256 hctx = q->mq_ops->map_queue(q, ctx->cpu);
6f3b0e8b 257 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
cc6e3b10 258 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
cb96a42c 259 ctx = alloc_data.ctx;
d852564f
CH
260 }
261 blk_mq_put_ctx(ctx);
c76541a9 262 if (!rq) {
3ef28e83 263 blk_queue_exit(q);
a492f075 264 return ERR_PTR(-EWOULDBLOCK);
c76541a9 265 }
0c4de0f3
CH
266
267 rq->__data_len = 0;
268 rq->__sector = (sector_t) -1;
269 rq->bio = rq->biotail = NULL;
320ae51f
JA
270 return rq;
271}
4bb659b1 272EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f 273
1f5bd336
ML
274struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
275 unsigned int flags, unsigned int hctx_idx)
276{
277 struct blk_mq_hw_ctx *hctx;
278 struct blk_mq_ctx *ctx;
279 struct request *rq;
280 struct blk_mq_alloc_data alloc_data;
281 int ret;
282
283 /*
284 * If the tag allocator sleeps we could get an allocation for a
285 * different hardware context. No need to complicate the low level
286 * allocator for this for the rare use case of a command tied to
287 * a specific queue.
288 */
289 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
290 return ERR_PTR(-EINVAL);
291
292 if (hctx_idx >= q->nr_hw_queues)
293 return ERR_PTR(-EIO);
294
295 ret = blk_queue_enter(q, true);
296 if (ret)
297 return ERR_PTR(ret);
298
c8712c6a
CH
299 /*
300 * Check if the hardware context is actually mapped to anything.
301 * If not tell the caller that it should skip this queue.
302 */
1f5bd336 303 hctx = q->queue_hw_ctx[hctx_idx];
c8712c6a
CH
304 if (!blk_mq_hw_queue_mapped(hctx)) {
305 ret = -EXDEV;
306 goto out_queue_exit;
307 }
1f5bd336
ML
308 ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask));
309
310 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
311 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
312 if (!rq) {
c8712c6a
CH
313 ret = -EWOULDBLOCK;
314 goto out_queue_exit;
1f5bd336
ML
315 }
316
317 return rq;
c8712c6a
CH
318
319out_queue_exit:
320 blk_queue_exit(q);
321 return ERR_PTR(ret);
1f5bd336
ML
322}
323EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
324
320ae51f
JA
325static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
326 struct blk_mq_ctx *ctx, struct request *rq)
327{
328 const int tag = rq->tag;
329 struct request_queue *q = rq->q;
330
0d2602ca
JA
331 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
332 atomic_dec(&hctx->nr_active);
683d0e12 333 rq->cmd_flags = 0;
0d2602ca 334
af76e555 335 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
0d2602ca 336 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
3ef28e83 337 blk_queue_exit(q);
320ae51f
JA
338}
339
7c7f2f2b 340void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
320ae51f
JA
341{
342 struct blk_mq_ctx *ctx = rq->mq_ctx;
320ae51f
JA
343
344 ctx->rq_completed[rq_is_sync(rq)]++;
320ae51f 345 __blk_mq_free_request(hctx, ctx, rq);
7c7f2f2b
JA
346
347}
348EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
349
350void blk_mq_free_request(struct request *rq)
351{
352 struct blk_mq_hw_ctx *hctx;
353 struct request_queue *q = rq->q;
354
355 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
356 blk_mq_free_hctx_request(hctx, rq);
320ae51f 357}
1a3b595a 358EXPORT_SYMBOL_GPL(blk_mq_free_request);
320ae51f 359
c8a446ad 360inline void __blk_mq_end_request(struct request *rq, int error)
320ae51f 361{
0d11e6ac
ML
362 blk_account_io_done(rq);
363
91b63639 364 if (rq->end_io) {
320ae51f 365 rq->end_io(rq, error);
91b63639
CH
366 } else {
367 if (unlikely(blk_bidi_rq(rq)))
368 blk_mq_free_request(rq->next_rq);
320ae51f 369 blk_mq_free_request(rq);
91b63639 370 }
320ae51f 371}
c8a446ad 372EXPORT_SYMBOL(__blk_mq_end_request);
63151a44 373
c8a446ad 374void blk_mq_end_request(struct request *rq, int error)
63151a44
CH
375{
376 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
377 BUG();
c8a446ad 378 __blk_mq_end_request(rq, error);
63151a44 379}
c8a446ad 380EXPORT_SYMBOL(blk_mq_end_request);
320ae51f 381
30a91cb4 382static void __blk_mq_complete_request_remote(void *data)
320ae51f 383{
3d6efbf6 384 struct request *rq = data;
320ae51f 385
30a91cb4 386 rq->q->softirq_done_fn(rq);
320ae51f 387}
320ae51f 388
ed851860 389static void blk_mq_ipi_complete_request(struct request *rq)
320ae51f
JA
390{
391 struct blk_mq_ctx *ctx = rq->mq_ctx;
38535201 392 bool shared = false;
320ae51f
JA
393 int cpu;
394
38535201 395 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
30a91cb4
CH
396 rq->q->softirq_done_fn(rq);
397 return;
398 }
320ae51f
JA
399
400 cpu = get_cpu();
38535201
CH
401 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
402 shared = cpus_share_cache(cpu, ctx->cpu);
403
404 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 405 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
406 rq->csd.info = rq;
407 rq->csd.flags = 0;
c46fff2a 408 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 409 } else {
30a91cb4 410 rq->q->softirq_done_fn(rq);
3d6efbf6 411 }
320ae51f
JA
412 put_cpu();
413}
30a91cb4 414
1fa8cc52 415static void __blk_mq_complete_request(struct request *rq)
ed851860
JA
416{
417 struct request_queue *q = rq->q;
418
419 if (!q->softirq_done_fn)
c8a446ad 420 blk_mq_end_request(rq, rq->errors);
ed851860
JA
421 else
422 blk_mq_ipi_complete_request(rq);
423}
424
30a91cb4
CH
425/**
426 * blk_mq_complete_request - end I/O on a request
427 * @rq: the request being processed
428 *
429 * Description:
430 * Ends all I/O on a request. It does not handle partial completions.
431 * The actual completion happens out-of-order, through a IPI handler.
432 **/
f4829a9b 433void blk_mq_complete_request(struct request *rq, int error)
30a91cb4 434{
95f09684
JA
435 struct request_queue *q = rq->q;
436
437 if (unlikely(blk_should_fake_timeout(q)))
30a91cb4 438 return;
f4829a9b
CH
439 if (!blk_mark_rq_complete(rq)) {
440 rq->errors = error;
ed851860 441 __blk_mq_complete_request(rq);
f4829a9b 442 }
30a91cb4
CH
443}
444EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 445
973c0191
KB
446int blk_mq_request_started(struct request *rq)
447{
448 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
449}
450EXPORT_SYMBOL_GPL(blk_mq_request_started);
451
e2490073 452void blk_mq_start_request(struct request *rq)
320ae51f
JA
453{
454 struct request_queue *q = rq->q;
455
456 trace_block_rq_issue(q, rq);
457
742ee69b 458 rq->resid_len = blk_rq_bytes(rq);
91b63639
CH
459 if (unlikely(blk_bidi_rq(rq)))
460 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
742ee69b 461
2b8393b4 462 blk_add_timer(rq);
87ee7b11 463
538b7534
JA
464 /*
465 * Ensure that ->deadline is visible before set the started
466 * flag and clear the completed flag.
467 */
468 smp_mb__before_atomic();
469
87ee7b11
JA
470 /*
471 * Mark us as started and clear complete. Complete might have been
472 * set if requeue raced with timeout, which then marked it as
473 * complete. So be sure to clear complete again when we start
474 * the request, otherwise we'll ignore the completion event.
475 */
4b570521
JA
476 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
477 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
478 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
479 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
49f5baa5
CH
480
481 if (q->dma_drain_size && blk_rq_bytes(rq)) {
482 /*
483 * Make sure space for the drain appears. We know we can do
484 * this because max_hw_segments has been adjusted to be one
485 * fewer than the device can handle.
486 */
487 rq->nr_phys_segments++;
488 }
320ae51f 489}
e2490073 490EXPORT_SYMBOL(blk_mq_start_request);
320ae51f 491
ed0791b2 492static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
493{
494 struct request_queue *q = rq->q;
495
496 trace_block_rq_requeue(q, rq);
49f5baa5 497
e2490073
CH
498 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
499 if (q->dma_drain_size && blk_rq_bytes(rq))
500 rq->nr_phys_segments--;
501 }
320ae51f
JA
502}
503
ed0791b2
CH
504void blk_mq_requeue_request(struct request *rq)
505{
ed0791b2 506 __blk_mq_requeue_request(rq);
ed0791b2 507
ed0791b2 508 BUG_ON(blk_queued_rq(rq));
6fca6a61 509 blk_mq_add_to_requeue_list(rq, true);
ed0791b2
CH
510}
511EXPORT_SYMBOL(blk_mq_requeue_request);
512
6fca6a61
CH
513static void blk_mq_requeue_work(struct work_struct *work)
514{
515 struct request_queue *q =
516 container_of(work, struct request_queue, requeue_work);
517 LIST_HEAD(rq_list);
518 struct request *rq, *next;
519 unsigned long flags;
520
521 spin_lock_irqsave(&q->requeue_lock, flags);
522 list_splice_init(&q->requeue_list, &rq_list);
523 spin_unlock_irqrestore(&q->requeue_lock, flags);
524
525 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
526 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
527 continue;
528
529 rq->cmd_flags &= ~REQ_SOFTBARRIER;
530 list_del_init(&rq->queuelist);
531 blk_mq_insert_request(rq, true, false, false);
532 }
533
534 while (!list_empty(&rq_list)) {
535 rq = list_entry(rq_list.next, struct request, queuelist);
536 list_del_init(&rq->queuelist);
537 blk_mq_insert_request(rq, false, false, false);
538 }
539
8b957415
JA
540 /*
541 * Use the start variant of queue running here, so that running
542 * the requeue work will kick stopped queues.
543 */
544 blk_mq_start_hw_queues(q);
6fca6a61
CH
545}
546
547void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
548{
549 struct request_queue *q = rq->q;
550 unsigned long flags;
551
552 /*
553 * We abuse this flag that is otherwise used by the I/O scheduler to
554 * request head insertation from the workqueue.
555 */
556 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
557
558 spin_lock_irqsave(&q->requeue_lock, flags);
559 if (at_head) {
560 rq->cmd_flags |= REQ_SOFTBARRIER;
561 list_add(&rq->queuelist, &q->requeue_list);
562 } else {
563 list_add_tail(&rq->queuelist, &q->requeue_list);
564 }
565 spin_unlock_irqrestore(&q->requeue_lock, flags);
566}
567EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
568
c68ed59f
KB
569void blk_mq_cancel_requeue_work(struct request_queue *q)
570{
571 cancel_work_sync(&q->requeue_work);
572}
573EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work);
574
6fca6a61
CH
575void blk_mq_kick_requeue_list(struct request_queue *q)
576{
577 kblockd_schedule_work(&q->requeue_work);
578}
579EXPORT_SYMBOL(blk_mq_kick_requeue_list);
580
1885b24d
JA
581void blk_mq_abort_requeue_list(struct request_queue *q)
582{
583 unsigned long flags;
584 LIST_HEAD(rq_list);
585
586 spin_lock_irqsave(&q->requeue_lock, flags);
587 list_splice_init(&q->requeue_list, &rq_list);
588 spin_unlock_irqrestore(&q->requeue_lock, flags);
589
590 while (!list_empty(&rq_list)) {
591 struct request *rq;
592
593 rq = list_first_entry(&rq_list, struct request, queuelist);
594 list_del_init(&rq->queuelist);
595 rq->errors = -EIO;
596 blk_mq_end_request(rq, rq->errors);
597 }
598}
599EXPORT_SYMBOL(blk_mq_abort_requeue_list);
600
0e62f51f
JA
601struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
602{
4ee86bab
HR
603 if (tag < tags->nr_tags)
604 return tags->rqs[tag];
605
606 return NULL;
24d2f903
CH
607}
608EXPORT_SYMBOL(blk_mq_tag_to_rq);
609
320ae51f 610struct blk_mq_timeout_data {
46f92d42
CH
611 unsigned long next;
612 unsigned int next_set;
320ae51f
JA
613};
614
90415837 615void blk_mq_rq_timed_out(struct request *req, bool reserved)
320ae51f 616{
46f92d42
CH
617 struct blk_mq_ops *ops = req->q->mq_ops;
618 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
87ee7b11
JA
619
620 /*
621 * We know that complete is set at this point. If STARTED isn't set
622 * anymore, then the request isn't active and the "timeout" should
623 * just be ignored. This can happen due to the bitflag ordering.
624 * Timeout first checks if STARTED is set, and if it is, assumes
625 * the request is active. But if we race with completion, then
626 * we both flags will get cleared. So check here again, and ignore
627 * a timeout event with a request that isn't active.
628 */
46f92d42
CH
629 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
630 return;
87ee7b11 631
46f92d42 632 if (ops->timeout)
0152fb6b 633 ret = ops->timeout(req, reserved);
46f92d42
CH
634
635 switch (ret) {
636 case BLK_EH_HANDLED:
637 __blk_mq_complete_request(req);
638 break;
639 case BLK_EH_RESET_TIMER:
640 blk_add_timer(req);
641 blk_clear_rq_complete(req);
642 break;
643 case BLK_EH_NOT_HANDLED:
644 break;
645 default:
646 printk(KERN_ERR "block: bad eh return: %d\n", ret);
647 break;
648 }
87ee7b11 649}
5b3f25fc 650
81481eb4
CH
651static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
652 struct request *rq, void *priv, bool reserved)
653{
654 struct blk_mq_timeout_data *data = priv;
87ee7b11 655
eb130dbf
KB
656 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
657 /*
658 * If a request wasn't started before the queue was
659 * marked dying, kill it here or it'll go unnoticed.
660 */
a59e0f57
KB
661 if (unlikely(blk_queue_dying(rq->q))) {
662 rq->errors = -EIO;
663 blk_mq_end_request(rq, rq->errors);
664 }
46f92d42 665 return;
eb130dbf 666 }
87ee7b11 667
46f92d42
CH
668 if (time_after_eq(jiffies, rq->deadline)) {
669 if (!blk_mark_rq_complete(rq))
0152fb6b 670 blk_mq_rq_timed_out(rq, reserved);
46f92d42
CH
671 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
672 data->next = rq->deadline;
673 data->next_set = 1;
674 }
87ee7b11
JA
675}
676
287922eb 677static void blk_mq_timeout_work(struct work_struct *work)
320ae51f 678{
287922eb
CH
679 struct request_queue *q =
680 container_of(work, struct request_queue, timeout_work);
81481eb4
CH
681 struct blk_mq_timeout_data data = {
682 .next = 0,
683 .next_set = 0,
684 };
81481eb4 685 int i;
320ae51f 686
71f79fb3
GKB
687 /* A deadlock might occur if a request is stuck requiring a
688 * timeout at the same time a queue freeze is waiting
689 * completion, since the timeout code would not be able to
690 * acquire the queue reference here.
691 *
692 * That's why we don't use blk_queue_enter here; instead, we use
693 * percpu_ref_tryget directly, because we need to be able to
694 * obtain a reference even in the short window between the queue
695 * starting to freeze, by dropping the first reference in
696 * blk_mq_freeze_queue_start, and the moment the last request is
697 * consumed, marked by the instant q_usage_counter reaches
698 * zero.
699 */
700 if (!percpu_ref_tryget(&q->q_usage_counter))
287922eb
CH
701 return;
702
0bf6cd5b 703 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
320ae51f 704
81481eb4
CH
705 if (data.next_set) {
706 data.next = blk_rq_timeout(round_jiffies_up(data.next));
707 mod_timer(&q->timeout, data.next);
0d2602ca 708 } else {
0bf6cd5b
CH
709 struct blk_mq_hw_ctx *hctx;
710
f054b56c
ML
711 queue_for_each_hw_ctx(q, hctx, i) {
712 /* the hctx may be unmapped, so check it here */
713 if (blk_mq_hw_queue_mapped(hctx))
714 blk_mq_tag_idle(hctx);
715 }
0d2602ca 716 }
287922eb 717 blk_queue_exit(q);
320ae51f
JA
718}
719
720/*
721 * Reverse check our software queue for entries that we could potentially
722 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
723 * too much time checking for merges.
724 */
725static bool blk_mq_attempt_merge(struct request_queue *q,
726 struct blk_mq_ctx *ctx, struct bio *bio)
727{
728 struct request *rq;
729 int checked = 8;
730
731 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
732 int el_ret;
733
734 if (!checked--)
735 break;
736
737 if (!blk_rq_merge_ok(rq, bio))
738 continue;
739
740 el_ret = blk_try_merge(rq, bio);
741 if (el_ret == ELEVATOR_BACK_MERGE) {
742 if (bio_attempt_back_merge(q, rq, bio)) {
743 ctx->rq_merged++;
744 return true;
745 }
746 break;
747 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
748 if (bio_attempt_front_merge(q, rq, bio)) {
749 ctx->rq_merged++;
750 return true;
751 }
752 break;
753 }
754 }
755
756 return false;
757}
758
1429d7c9
JA
759/*
760 * Process software queues that have been marked busy, splicing them
761 * to the for-dispatch
762 */
763static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
764{
765 struct blk_mq_ctx *ctx;
766 int i;
767
569fd0ce 768 for (i = 0; i < hctx->ctx_map.size; i++) {
1429d7c9
JA
769 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
770 unsigned int off, bit;
771
772 if (!bm->word)
773 continue;
774
775 bit = 0;
776 off = i * hctx->ctx_map.bits_per_word;
777 do {
778 bit = find_next_bit(&bm->word, bm->depth, bit);
779 if (bit >= bm->depth)
780 break;
781
782 ctx = hctx->ctxs[bit + off];
783 clear_bit(bit, &bm->word);
784 spin_lock(&ctx->lock);
785 list_splice_tail_init(&ctx->rq_list, list);
786 spin_unlock(&ctx->lock);
787
788 bit++;
789 } while (1);
790 }
791}
792
320ae51f
JA
793/*
794 * Run this hardware queue, pulling any software queues mapped to it in.
795 * Note that this function currently has various problems around ordering
796 * of IO. In particular, we'd like FIFO behaviour on handling existing
797 * items on the hctx->dispatch list. Ignore that for now.
798 */
799static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
800{
801 struct request_queue *q = hctx->queue;
320ae51f
JA
802 struct request *rq;
803 LIST_HEAD(rq_list);
74c45052
JA
804 LIST_HEAD(driver_list);
805 struct list_head *dptr;
1429d7c9 806 int queued;
320ae51f 807
5d12f905 808 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
809 return;
810
0e87e58b
JA
811 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
812 cpu_online(hctx->next_cpu));
813
320ae51f
JA
814 hctx->run++;
815
816 /*
817 * Touch any software queue that has pending entries.
818 */
1429d7c9 819 flush_busy_ctxs(hctx, &rq_list);
320ae51f
JA
820
821 /*
822 * If we have previous entries on our dispatch list, grab them
823 * and stuff them at the front for more fair dispatch.
824 */
825 if (!list_empty_careful(&hctx->dispatch)) {
826 spin_lock(&hctx->lock);
827 if (!list_empty(&hctx->dispatch))
828 list_splice_init(&hctx->dispatch, &rq_list);
829 spin_unlock(&hctx->lock);
830 }
831
74c45052
JA
832 /*
833 * Start off with dptr being NULL, so we start the first request
834 * immediately, even if we have more pending.
835 */
836 dptr = NULL;
837
320ae51f
JA
838 /*
839 * Now process all the entries, sending them to the driver.
840 */
1429d7c9 841 queued = 0;
320ae51f 842 while (!list_empty(&rq_list)) {
74c45052 843 struct blk_mq_queue_data bd;
320ae51f
JA
844 int ret;
845
846 rq = list_first_entry(&rq_list, struct request, queuelist);
847 list_del_init(&rq->queuelist);
320ae51f 848
74c45052
JA
849 bd.rq = rq;
850 bd.list = dptr;
851 bd.last = list_empty(&rq_list);
852
853 ret = q->mq_ops->queue_rq(hctx, &bd);
320ae51f
JA
854 switch (ret) {
855 case BLK_MQ_RQ_QUEUE_OK:
856 queued++;
52b9c330 857 break;
320ae51f 858 case BLK_MQ_RQ_QUEUE_BUSY:
320ae51f 859 list_add(&rq->queuelist, &rq_list);
ed0791b2 860 __blk_mq_requeue_request(rq);
320ae51f
JA
861 break;
862 default:
863 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 864 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 865 rq->errors = -EIO;
c8a446ad 866 blk_mq_end_request(rq, rq->errors);
320ae51f
JA
867 break;
868 }
869
870 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
871 break;
74c45052
JA
872
873 /*
874 * We've done the first request. If we have more than 1
875 * left in the list, set dptr to defer issue.
876 */
877 if (!dptr && rq_list.next != rq_list.prev)
878 dptr = &driver_list;
320ae51f
JA
879 }
880
881 if (!queued)
882 hctx->dispatched[0]++;
883 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
884 hctx->dispatched[ilog2(queued) + 1]++;
885
886 /*
887 * Any items that need requeuing? Stuff them into hctx->dispatch,
888 * that is where we will continue on next queue run.
889 */
890 if (!list_empty(&rq_list)) {
891 spin_lock(&hctx->lock);
892 list_splice(&rq_list, &hctx->dispatch);
893 spin_unlock(&hctx->lock);
9ba52e58
SL
894 /*
895 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
896 * it's possible the queue is stopped and restarted again
897 * before this. Queue restart will dispatch requests. And since
898 * requests in rq_list aren't added into hctx->dispatch yet,
899 * the requests in rq_list might get lost.
900 *
901 * blk_mq_run_hw_queue() already checks the STOPPED bit
902 **/
903 blk_mq_run_hw_queue(hctx, true);
320ae51f
JA
904 }
905}
906
506e931f
JA
907/*
908 * It'd be great if the workqueue API had a way to pass
909 * in a mask and had some smarts for more clever placement.
910 * For now we just round-robin here, switching for every
911 * BLK_MQ_CPU_WORK_BATCH queued items.
912 */
913static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
914{
b657d7e6
CH
915 if (hctx->queue->nr_hw_queues == 1)
916 return WORK_CPU_UNBOUND;
506e931f
JA
917
918 if (--hctx->next_cpu_batch <= 0) {
b657d7e6 919 int cpu = hctx->next_cpu, next_cpu;
506e931f
JA
920
921 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
922 if (next_cpu >= nr_cpu_ids)
923 next_cpu = cpumask_first(hctx->cpumask);
924
925 hctx->next_cpu = next_cpu;
926 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
b657d7e6
CH
927
928 return cpu;
506e931f
JA
929 }
930
b657d7e6 931 return hctx->next_cpu;
506e931f
JA
932}
933
320ae51f
JA
934void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
935{
19c66e59
ML
936 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state) ||
937 !blk_mq_hw_queue_mapped(hctx)))
320ae51f
JA
938 return;
939
398205b8 940 if (!async) {
2a90d4aa
PB
941 int cpu = get_cpu();
942 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
398205b8 943 __blk_mq_run_hw_queue(hctx);
2a90d4aa 944 put_cpu();
398205b8
PB
945 return;
946 }
e4043dcf 947
2a90d4aa 948 put_cpu();
e4043dcf 949 }
398205b8 950
b657d7e6
CH
951 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
952 &hctx->run_work, 0);
320ae51f
JA
953}
954
b94ec296 955void blk_mq_run_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
956{
957 struct blk_mq_hw_ctx *hctx;
958 int i;
959
960 queue_for_each_hw_ctx(q, hctx, i) {
961 if ((!blk_mq_hctx_has_pending(hctx) &&
962 list_empty_careful(&hctx->dispatch)) ||
5d12f905 963 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
964 continue;
965
b94ec296 966 blk_mq_run_hw_queue(hctx, async);
320ae51f
JA
967 }
968}
b94ec296 969EXPORT_SYMBOL(blk_mq_run_hw_queues);
320ae51f
JA
970
971void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
972{
70f4db63
CH
973 cancel_delayed_work(&hctx->run_work);
974 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
975 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
976}
977EXPORT_SYMBOL(blk_mq_stop_hw_queue);
978
280d45f6
CH
979void blk_mq_stop_hw_queues(struct request_queue *q)
980{
981 struct blk_mq_hw_ctx *hctx;
982 int i;
983
984 queue_for_each_hw_ctx(q, hctx, i)
985 blk_mq_stop_hw_queue(hctx);
986}
987EXPORT_SYMBOL(blk_mq_stop_hw_queues);
988
320ae51f
JA
989void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
990{
991 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 992
0ffbce80 993 blk_mq_run_hw_queue(hctx, false);
320ae51f
JA
994}
995EXPORT_SYMBOL(blk_mq_start_hw_queue);
996
2f268556
CH
997void blk_mq_start_hw_queues(struct request_queue *q)
998{
999 struct blk_mq_hw_ctx *hctx;
1000 int i;
1001
1002 queue_for_each_hw_ctx(q, hctx, i)
1003 blk_mq_start_hw_queue(hctx);
1004}
1005EXPORT_SYMBOL(blk_mq_start_hw_queues);
1006
1b4a3258 1007void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
1008{
1009 struct blk_mq_hw_ctx *hctx;
1010 int i;
1011
1012 queue_for_each_hw_ctx(q, hctx, i) {
1013 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
1014 continue;
1015
1016 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1b4a3258 1017 blk_mq_run_hw_queue(hctx, async);
320ae51f
JA
1018 }
1019}
1020EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1021
70f4db63 1022static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
1023{
1024 struct blk_mq_hw_ctx *hctx;
1025
70f4db63 1026 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
e4043dcf 1027
320ae51f
JA
1028 __blk_mq_run_hw_queue(hctx);
1029}
1030
70f4db63
CH
1031static void blk_mq_delay_work_fn(struct work_struct *work)
1032{
1033 struct blk_mq_hw_ctx *hctx;
1034
1035 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1036
1037 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1038 __blk_mq_run_hw_queue(hctx);
1039}
1040
1041void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1042{
19c66e59
ML
1043 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1044 return;
70f4db63 1045
b657d7e6
CH
1046 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1047 &hctx->delay_work, msecs_to_jiffies(msecs));
70f4db63
CH
1048}
1049EXPORT_SYMBOL(blk_mq_delay_queue);
1050
cfd0c552 1051static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
cfd0c552
ML
1052 struct request *rq,
1053 bool at_head)
320ae51f 1054{
e57690fe
JA
1055 struct blk_mq_ctx *ctx = rq->mq_ctx;
1056
01b983c9
JA
1057 trace_block_rq_insert(hctx->queue, rq);
1058
72a0a36e
CH
1059 if (at_head)
1060 list_add(&rq->queuelist, &ctx->rq_list);
1061 else
1062 list_add_tail(&rq->queuelist, &ctx->rq_list);
cfd0c552 1063}
4bb659b1 1064
cfd0c552
ML
1065static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
1066 struct request *rq, bool at_head)
1067{
1068 struct blk_mq_ctx *ctx = rq->mq_ctx;
1069
e57690fe 1070 __blk_mq_insert_req_list(hctx, rq, at_head);
320ae51f 1071 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
1072}
1073
eeabc850 1074void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
e57690fe 1075 bool async)
320ae51f 1076{
e57690fe 1077 struct blk_mq_ctx *ctx = rq->mq_ctx;
eeabc850 1078 struct request_queue *q = rq->q;
320ae51f 1079 struct blk_mq_hw_ctx *hctx;
320ae51f 1080
320ae51f
JA
1081 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1082
a57a178a
CH
1083 spin_lock(&ctx->lock);
1084 __blk_mq_insert_request(hctx, rq, at_head);
1085 spin_unlock(&ctx->lock);
320ae51f 1086
320ae51f
JA
1087 if (run_queue)
1088 blk_mq_run_hw_queue(hctx, async);
1089}
1090
1091static void blk_mq_insert_requests(struct request_queue *q,
1092 struct blk_mq_ctx *ctx,
1093 struct list_head *list,
1094 int depth,
1095 bool from_schedule)
1096
1097{
1098 struct blk_mq_hw_ctx *hctx;
320ae51f
JA
1099
1100 trace_block_unplug(q, depth, !from_schedule);
1101
320ae51f
JA
1102 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1103
1104 /*
1105 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1106 * offline now
1107 */
1108 spin_lock(&ctx->lock);
1109 while (!list_empty(list)) {
1110 struct request *rq;
1111
1112 rq = list_first_entry(list, struct request, queuelist);
e57690fe 1113 BUG_ON(rq->mq_ctx != ctx);
320ae51f 1114 list_del_init(&rq->queuelist);
e57690fe 1115 __blk_mq_insert_req_list(hctx, rq, false);
320ae51f 1116 }
cfd0c552 1117 blk_mq_hctx_mark_pending(hctx, ctx);
320ae51f
JA
1118 spin_unlock(&ctx->lock);
1119
320ae51f
JA
1120 blk_mq_run_hw_queue(hctx, from_schedule);
1121}
1122
1123static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1124{
1125 struct request *rqa = container_of(a, struct request, queuelist);
1126 struct request *rqb = container_of(b, struct request, queuelist);
1127
1128 return !(rqa->mq_ctx < rqb->mq_ctx ||
1129 (rqa->mq_ctx == rqb->mq_ctx &&
1130 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1131}
1132
1133void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1134{
1135 struct blk_mq_ctx *this_ctx;
1136 struct request_queue *this_q;
1137 struct request *rq;
1138 LIST_HEAD(list);
1139 LIST_HEAD(ctx_list);
1140 unsigned int depth;
1141
1142 list_splice_init(&plug->mq_list, &list);
1143
1144 list_sort(NULL, &list, plug_ctx_cmp);
1145
1146 this_q = NULL;
1147 this_ctx = NULL;
1148 depth = 0;
1149
1150 while (!list_empty(&list)) {
1151 rq = list_entry_rq(list.next);
1152 list_del_init(&rq->queuelist);
1153 BUG_ON(!rq->q);
1154 if (rq->mq_ctx != this_ctx) {
1155 if (this_ctx) {
1156 blk_mq_insert_requests(this_q, this_ctx,
1157 &ctx_list, depth,
1158 from_schedule);
1159 }
1160
1161 this_ctx = rq->mq_ctx;
1162 this_q = rq->q;
1163 depth = 0;
1164 }
1165
1166 depth++;
1167 list_add_tail(&rq->queuelist, &ctx_list);
1168 }
1169
1170 /*
1171 * If 'this_ctx' is set, we know we have entries to complete
1172 * on 'ctx_list'. Do those.
1173 */
1174 if (this_ctx) {
1175 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1176 from_schedule);
1177 }
1178}
1179
1180static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1181{
1182 init_request_from_bio(rq, bio);
4b570521 1183
a21f2a3e 1184 blk_account_io_start(rq, 1);
320ae51f
JA
1185}
1186
274a5843
JA
1187static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1188{
1189 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1190 !blk_queue_nomerges(hctx->queue);
1191}
1192
07068d5b
JA
1193static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1194 struct blk_mq_ctx *ctx,
1195 struct request *rq, struct bio *bio)
320ae51f 1196{
e18378a6 1197 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
07068d5b
JA
1198 blk_mq_bio_to_request(rq, bio);
1199 spin_lock(&ctx->lock);
1200insert_rq:
1201 __blk_mq_insert_request(hctx, rq, false);
1202 spin_unlock(&ctx->lock);
1203 return false;
1204 } else {
274a5843
JA
1205 struct request_queue *q = hctx->queue;
1206
07068d5b
JA
1207 spin_lock(&ctx->lock);
1208 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1209 blk_mq_bio_to_request(rq, bio);
1210 goto insert_rq;
1211 }
320ae51f 1212
07068d5b
JA
1213 spin_unlock(&ctx->lock);
1214 __blk_mq_free_request(hctx, ctx, rq);
1215 return true;
14ec77f3 1216 }
07068d5b 1217}
14ec77f3 1218
07068d5b
JA
1219struct blk_map_ctx {
1220 struct blk_mq_hw_ctx *hctx;
1221 struct blk_mq_ctx *ctx;
1222};
1223
1224static struct request *blk_mq_map_request(struct request_queue *q,
1225 struct bio *bio,
1226 struct blk_map_ctx *data)
1227{
1228 struct blk_mq_hw_ctx *hctx;
1229 struct blk_mq_ctx *ctx;
1230 struct request *rq;
cc6e3b10
MC
1231 int op = bio_data_dir(bio);
1232 int op_flags = 0;
cb96a42c 1233 struct blk_mq_alloc_data alloc_data;
320ae51f 1234
3ef28e83 1235 blk_queue_enter_live(q);
320ae51f
JA
1236 ctx = blk_mq_get_ctx(q);
1237 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1238
1eff9d32 1239 if (rw_is_sync(bio_op(bio), bio->bi_opf))
cc6e3b10 1240 op_flags |= REQ_SYNC;
07068d5b 1241
cc6e3b10 1242 trace_block_getrq(q, bio, op);
6f3b0e8b 1243 blk_mq_set_alloc_data(&alloc_data, q, BLK_MQ_REQ_NOWAIT, ctx, hctx);
cc6e3b10 1244 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
5dee8577 1245 if (unlikely(!rq)) {
793597a6 1246 __blk_mq_run_hw_queue(hctx);
320ae51f 1247 blk_mq_put_ctx(ctx);
cc6e3b10 1248 trace_block_sleeprq(q, bio, op);
793597a6
CH
1249
1250 ctx = blk_mq_get_ctx(q);
320ae51f 1251 hctx = q->mq_ops->map_queue(q, ctx->cpu);
6f3b0e8b 1252 blk_mq_set_alloc_data(&alloc_data, q, 0, ctx, hctx);
cc6e3b10 1253 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
cb96a42c
ML
1254 ctx = alloc_data.ctx;
1255 hctx = alloc_data.hctx;
320ae51f
JA
1256 }
1257
1258 hctx->queued++;
07068d5b
JA
1259 data->hctx = hctx;
1260 data->ctx = ctx;
1261 return rq;
1262}
1263
7b371636 1264static int blk_mq_direct_issue_request(struct request *rq, blk_qc_t *cookie)
f984df1f
SL
1265{
1266 int ret;
1267 struct request_queue *q = rq->q;
1268 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q,
1269 rq->mq_ctx->cpu);
1270 struct blk_mq_queue_data bd = {
1271 .rq = rq,
1272 .list = NULL,
1273 .last = 1
1274 };
7b371636 1275 blk_qc_t new_cookie = blk_tag_to_qc_t(rq->tag, hctx->queue_num);
f984df1f
SL
1276
1277 /*
1278 * For OK queue, we are done. For error, kill it. Any other
1279 * error (busy), just add it to our list as we previously
1280 * would have done
1281 */
1282 ret = q->mq_ops->queue_rq(hctx, &bd);
7b371636
JA
1283 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1284 *cookie = new_cookie;
f984df1f 1285 return 0;
7b371636 1286 }
f984df1f 1287
7b371636
JA
1288 __blk_mq_requeue_request(rq);
1289
1290 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1291 *cookie = BLK_QC_T_NONE;
1292 rq->errors = -EIO;
1293 blk_mq_end_request(rq, rq->errors);
1294 return 0;
f984df1f 1295 }
7b371636
JA
1296
1297 return -1;
f984df1f
SL
1298}
1299
07068d5b
JA
1300/*
1301 * Multiple hardware queue variant. This will not use per-process plugs,
1302 * but will attempt to bypass the hctx queueing if we can go straight to
1303 * hardware for SYNC IO.
1304 */
dece1635 1305static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 1306{
1eff9d32
JA
1307 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1308 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
07068d5b
JA
1309 struct blk_map_ctx data;
1310 struct request *rq;
f984df1f
SL
1311 unsigned int request_count = 0;
1312 struct blk_plug *plug;
5b3f341f 1313 struct request *same_queue_rq = NULL;
7b371636 1314 blk_qc_t cookie;
07068d5b
JA
1315
1316 blk_queue_bounce(q, &bio);
1317
1318 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6 1319 bio_io_error(bio);
dece1635 1320 return BLK_QC_T_NONE;
07068d5b
JA
1321 }
1322
54efd50b
KO
1323 blk_queue_split(q, &bio, q->bio_split);
1324
87c279e6
OS
1325 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1326 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1327 return BLK_QC_T_NONE;
f984df1f 1328
07068d5b
JA
1329 rq = blk_mq_map_request(q, bio, &data);
1330 if (unlikely(!rq))
dece1635 1331 return BLK_QC_T_NONE;
07068d5b 1332
7b371636 1333 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
07068d5b
JA
1334
1335 if (unlikely(is_flush_fua)) {
1336 blk_mq_bio_to_request(rq, bio);
1337 blk_insert_flush(rq);
1338 goto run_queue;
1339 }
1340
f984df1f 1341 plug = current->plug;
e167dfb5
JA
1342 /*
1343 * If the driver supports defer issued based on 'last', then
1344 * queue it up like normal since we can potentially save some
1345 * CPU this way.
1346 */
f984df1f
SL
1347 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1348 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1349 struct request *old_rq = NULL;
07068d5b
JA
1350
1351 blk_mq_bio_to_request(rq, bio);
07068d5b
JA
1352
1353 /*
b094f89c 1354 * We do limited pluging. If the bio can be merged, do that.
f984df1f
SL
1355 * Otherwise the existing request in the plug list will be
1356 * issued. So the plug list will have one request at most
07068d5b 1357 */
f984df1f 1358 if (plug) {
5b3f341f
SL
1359 /*
1360 * The plug list might get flushed before this. If that
b094f89c
JA
1361 * happens, same_queue_rq is invalid and plug list is
1362 * empty
1363 */
5b3f341f
SL
1364 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1365 old_rq = same_queue_rq;
f984df1f 1366 list_del_init(&old_rq->queuelist);
07068d5b 1367 }
f984df1f
SL
1368 list_add_tail(&rq->queuelist, &plug->mq_list);
1369 } else /* is_sync */
1370 old_rq = rq;
1371 blk_mq_put_ctx(data.ctx);
1372 if (!old_rq)
7b371636
JA
1373 goto done;
1374 if (!blk_mq_direct_issue_request(old_rq, &cookie))
1375 goto done;
f984df1f 1376 blk_mq_insert_request(old_rq, false, true, true);
7b371636 1377 goto done;
07068d5b
JA
1378 }
1379
1380 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1381 /*
1382 * For a SYNC request, send it to the hardware immediately. For
1383 * an ASYNC request, just ensure that we run it later on. The
1384 * latter allows for merging opportunities and more efficient
1385 * dispatching.
1386 */
1387run_queue:
1388 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1389 }
07068d5b 1390 blk_mq_put_ctx(data.ctx);
7b371636
JA
1391done:
1392 return cookie;
07068d5b
JA
1393}
1394
1395/*
1396 * Single hardware queue variant. This will attempt to use any per-process
1397 * plug for merging and IO deferral.
1398 */
dece1635 1399static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
07068d5b 1400{
1eff9d32
JA
1401 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1402 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
e6c4438b
JM
1403 struct blk_plug *plug;
1404 unsigned int request_count = 0;
07068d5b
JA
1405 struct blk_map_ctx data;
1406 struct request *rq;
7b371636 1407 blk_qc_t cookie;
07068d5b 1408
07068d5b
JA
1409 blk_queue_bounce(q, &bio);
1410
1411 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6 1412 bio_io_error(bio);
dece1635 1413 return BLK_QC_T_NONE;
07068d5b
JA
1414 }
1415
54efd50b
KO
1416 blk_queue_split(q, &bio, q->bio_split);
1417
87c279e6
OS
1418 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1419 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1420 return BLK_QC_T_NONE;
1421 } else
1422 request_count = blk_plug_queued_count(q);
07068d5b
JA
1423
1424 rq = blk_mq_map_request(q, bio, &data);
ff87bcec 1425 if (unlikely(!rq))
dece1635 1426 return BLK_QC_T_NONE;
320ae51f 1427
7b371636 1428 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
320ae51f
JA
1429
1430 if (unlikely(is_flush_fua)) {
1431 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
1432 blk_insert_flush(rq);
1433 goto run_queue;
1434 }
1435
1436 /*
1437 * A task plug currently exists. Since this is completely lockless,
1438 * utilize that to temporarily store requests until the task is
1439 * either done or scheduled away.
1440 */
e6c4438b
JM
1441 plug = current->plug;
1442 if (plug) {
1443 blk_mq_bio_to_request(rq, bio);
676d0607 1444 if (!request_count)
e6c4438b 1445 trace_block_plug(q);
b094f89c
JA
1446
1447 blk_mq_put_ctx(data.ctx);
1448
1449 if (request_count >= BLK_MAX_REQUEST_COUNT) {
e6c4438b
JM
1450 blk_flush_plug_list(plug, false);
1451 trace_block_plug(q);
320ae51f 1452 }
b094f89c 1453
e6c4438b 1454 list_add_tail(&rq->queuelist, &plug->mq_list);
7b371636 1455 return cookie;
320ae51f
JA
1456 }
1457
07068d5b
JA
1458 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1459 /*
1460 * For a SYNC request, send it to the hardware immediately. For
1461 * an ASYNC request, just ensure that we run it later on. The
1462 * latter allows for merging opportunities and more efficient
1463 * dispatching.
1464 */
1465run_queue:
1466 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1467 }
1468
07068d5b 1469 blk_mq_put_ctx(data.ctx);
7b371636 1470 return cookie;
320ae51f
JA
1471}
1472
1473/*
1474 * Default mapping to a software queue, since we use one per CPU.
1475 */
1476struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1477{
1478 return q->queue_hw_ctx[q->mq_map[cpu]];
1479}
1480EXPORT_SYMBOL(blk_mq_map_queue);
1481
24d2f903
CH
1482static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1483 struct blk_mq_tags *tags, unsigned int hctx_idx)
95363efd 1484{
e9b267d9 1485 struct page *page;
320ae51f 1486
24d2f903 1487 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1488 int i;
320ae51f 1489
24d2f903
CH
1490 for (i = 0; i < tags->nr_tags; i++) {
1491 if (!tags->rqs[i])
e9b267d9 1492 continue;
24d2f903
CH
1493 set->ops->exit_request(set->driver_data, tags->rqs[i],
1494 hctx_idx, i);
a5164405 1495 tags->rqs[i] = NULL;
e9b267d9 1496 }
320ae51f 1497 }
320ae51f 1498
24d2f903
CH
1499 while (!list_empty(&tags->page_list)) {
1500 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1501 list_del_init(&page->lru);
f75782e4
CM
1502 /*
1503 * Remove kmemleak object previously allocated in
1504 * blk_mq_init_rq_map().
1505 */
1506 kmemleak_free(page_address(page));
320ae51f
JA
1507 __free_pages(page, page->private);
1508 }
1509
24d2f903 1510 kfree(tags->rqs);
320ae51f 1511
24d2f903 1512 blk_mq_free_tags(tags);
320ae51f
JA
1513}
1514
1515static size_t order_to_size(unsigned int order)
1516{
4ca08500 1517 return (size_t)PAGE_SIZE << order;
320ae51f
JA
1518}
1519
24d2f903
CH
1520static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1521 unsigned int hctx_idx)
320ae51f 1522{
24d2f903 1523 struct blk_mq_tags *tags;
320ae51f
JA
1524 unsigned int i, j, entries_per_page, max_order = 4;
1525 size_t rq_size, left;
1526
24d2f903 1527 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
24391c0d
SL
1528 set->numa_node,
1529 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
24d2f903
CH
1530 if (!tags)
1531 return NULL;
320ae51f 1532
24d2f903
CH
1533 INIT_LIST_HEAD(&tags->page_list);
1534
a5164405
JA
1535 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1536 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1537 set->numa_node);
24d2f903
CH
1538 if (!tags->rqs) {
1539 blk_mq_free_tags(tags);
1540 return NULL;
1541 }
320ae51f
JA
1542
1543 /*
1544 * rq_size is the size of the request plus driver payload, rounded
1545 * to the cacheline size
1546 */
24d2f903 1547 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1548 cache_line_size());
24d2f903 1549 left = rq_size * set->queue_depth;
320ae51f 1550
24d2f903 1551 for (i = 0; i < set->queue_depth; ) {
320ae51f
JA
1552 int this_order = max_order;
1553 struct page *page;
1554 int to_do;
1555 void *p;
1556
b3a834b1 1557 while (this_order && left < order_to_size(this_order - 1))
320ae51f
JA
1558 this_order--;
1559
1560 do {
a5164405 1561 page = alloc_pages_node(set->numa_node,
ac211175 1562 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
a5164405 1563 this_order);
320ae51f
JA
1564 if (page)
1565 break;
1566 if (!this_order--)
1567 break;
1568 if (order_to_size(this_order) < rq_size)
1569 break;
1570 } while (1);
1571
1572 if (!page)
24d2f903 1573 goto fail;
320ae51f
JA
1574
1575 page->private = this_order;
24d2f903 1576 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1577
1578 p = page_address(page);
f75782e4
CM
1579 /*
1580 * Allow kmemleak to scan these pages as they contain pointers
1581 * to additional allocations like via ops->init_request().
1582 */
1583 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL);
320ae51f 1584 entries_per_page = order_to_size(this_order) / rq_size;
24d2f903 1585 to_do = min(entries_per_page, set->queue_depth - i);
320ae51f
JA
1586 left -= to_do * rq_size;
1587 for (j = 0; j < to_do; j++) {
24d2f903
CH
1588 tags->rqs[i] = p;
1589 if (set->ops->init_request) {
1590 if (set->ops->init_request(set->driver_data,
1591 tags->rqs[i], hctx_idx, i,
a5164405
JA
1592 set->numa_node)) {
1593 tags->rqs[i] = NULL;
24d2f903 1594 goto fail;
a5164405 1595 }
e9b267d9
CH
1596 }
1597
320ae51f
JA
1598 p += rq_size;
1599 i++;
1600 }
1601 }
24d2f903 1602 return tags;
320ae51f 1603
24d2f903 1604fail:
24d2f903
CH
1605 blk_mq_free_rq_map(set, tags, hctx_idx);
1606 return NULL;
320ae51f
JA
1607}
1608
1429d7c9
JA
1609static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1610{
1611 kfree(bitmap->map);
1612}
1613
1614static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1615{
1616 unsigned int bpw = 8, total, num_maps, i;
1617
1618 bitmap->bits_per_word = bpw;
1619
1620 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1621 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1622 GFP_KERNEL, node);
1623 if (!bitmap->map)
1624 return -ENOMEM;
1625
1429d7c9
JA
1626 total = nr_cpu_ids;
1627 for (i = 0; i < num_maps; i++) {
1628 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1629 total -= bitmap->map[i].depth;
1630 }
1631
1632 return 0;
1633}
1634
e57690fe
JA
1635/*
1636 * 'cpu' is going away. splice any existing rq_list entries from this
1637 * software queue to the hw queue dispatch list, and ensure that it
1638 * gets run.
1639 */
484b4061
JA
1640static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1641{
484b4061
JA
1642 struct blk_mq_ctx *ctx;
1643 LIST_HEAD(tmp);
1644
e57690fe 1645 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
484b4061
JA
1646
1647 spin_lock(&ctx->lock);
1648 if (!list_empty(&ctx->rq_list)) {
1649 list_splice_init(&ctx->rq_list, &tmp);
1650 blk_mq_hctx_clear_pending(hctx, ctx);
1651 }
1652 spin_unlock(&ctx->lock);
1653
1654 if (list_empty(&tmp))
1655 return NOTIFY_OK;
1656
e57690fe
JA
1657 spin_lock(&hctx->lock);
1658 list_splice_tail_init(&tmp, &hctx->dispatch);
1659 spin_unlock(&hctx->lock);
484b4061
JA
1660
1661 blk_mq_run_hw_queue(hctx, true);
484b4061
JA
1662 return NOTIFY_OK;
1663}
1664
484b4061
JA
1665static int blk_mq_hctx_notify(void *data, unsigned long action,
1666 unsigned int cpu)
1667{
1668 struct blk_mq_hw_ctx *hctx = data;
1669
1670 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1671 return blk_mq_hctx_cpu_offline(hctx, cpu);
2a34c087
ML
1672
1673 /*
1674 * In case of CPU online, tags may be reallocated
1675 * in blk_mq_map_swqueue() after mapping is updated.
1676 */
484b4061
JA
1677
1678 return NOTIFY_OK;
1679}
1680
c3b4afca 1681/* hctx->ctxs will be freed in queue's release handler */
08e98fc6
ML
1682static void blk_mq_exit_hctx(struct request_queue *q,
1683 struct blk_mq_tag_set *set,
1684 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1685{
f70ced09
ML
1686 unsigned flush_start_tag = set->queue_depth;
1687
08e98fc6
ML
1688 blk_mq_tag_idle(hctx);
1689
f70ced09
ML
1690 if (set->ops->exit_request)
1691 set->ops->exit_request(set->driver_data,
1692 hctx->fq->flush_rq, hctx_idx,
1693 flush_start_tag + hctx_idx);
1694
08e98fc6
ML
1695 if (set->ops->exit_hctx)
1696 set->ops->exit_hctx(hctx, hctx_idx);
1697
1698 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
f70ced09 1699 blk_free_flush_queue(hctx->fq);
08e98fc6
ML
1700 blk_mq_free_bitmap(&hctx->ctx_map);
1701}
1702
624dbe47
ML
1703static void blk_mq_exit_hw_queues(struct request_queue *q,
1704 struct blk_mq_tag_set *set, int nr_queue)
1705{
1706 struct blk_mq_hw_ctx *hctx;
1707 unsigned int i;
1708
1709 queue_for_each_hw_ctx(q, hctx, i) {
1710 if (i == nr_queue)
1711 break;
08e98fc6 1712 blk_mq_exit_hctx(q, set, hctx, i);
624dbe47 1713 }
624dbe47
ML
1714}
1715
1716static void blk_mq_free_hw_queues(struct request_queue *q,
1717 struct blk_mq_tag_set *set)
1718{
1719 struct blk_mq_hw_ctx *hctx;
1720 unsigned int i;
1721
e09aae7e 1722 queue_for_each_hw_ctx(q, hctx, i)
624dbe47 1723 free_cpumask_var(hctx->cpumask);
624dbe47
ML
1724}
1725
08e98fc6
ML
1726static int blk_mq_init_hctx(struct request_queue *q,
1727 struct blk_mq_tag_set *set,
1728 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
320ae51f 1729{
08e98fc6 1730 int node;
f70ced09 1731 unsigned flush_start_tag = set->queue_depth;
08e98fc6
ML
1732
1733 node = hctx->numa_node;
1734 if (node == NUMA_NO_NODE)
1735 node = hctx->numa_node = set->numa_node;
1736
1737 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1738 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1739 spin_lock_init(&hctx->lock);
1740 INIT_LIST_HEAD(&hctx->dispatch);
1741 hctx->queue = q;
1742 hctx->queue_num = hctx_idx;
2404e607 1743 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
08e98fc6
ML
1744
1745 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1746 blk_mq_hctx_notify, hctx);
1747 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1748
1749 hctx->tags = set->tags[hctx_idx];
320ae51f
JA
1750
1751 /*
08e98fc6
ML
1752 * Allocate space for all possible cpus to avoid allocation at
1753 * runtime
320ae51f 1754 */
08e98fc6
ML
1755 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1756 GFP_KERNEL, node);
1757 if (!hctx->ctxs)
1758 goto unregister_cpu_notifier;
320ae51f 1759
08e98fc6
ML
1760 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1761 goto free_ctxs;
320ae51f 1762
08e98fc6 1763 hctx->nr_ctx = 0;
320ae51f 1764
08e98fc6
ML
1765 if (set->ops->init_hctx &&
1766 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1767 goto free_bitmap;
320ae51f 1768
f70ced09
ML
1769 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1770 if (!hctx->fq)
1771 goto exit_hctx;
320ae51f 1772
f70ced09
ML
1773 if (set->ops->init_request &&
1774 set->ops->init_request(set->driver_data,
1775 hctx->fq->flush_rq, hctx_idx,
1776 flush_start_tag + hctx_idx, node))
1777 goto free_fq;
320ae51f 1778
08e98fc6 1779 return 0;
320ae51f 1780
f70ced09
ML
1781 free_fq:
1782 kfree(hctx->fq);
1783 exit_hctx:
1784 if (set->ops->exit_hctx)
1785 set->ops->exit_hctx(hctx, hctx_idx);
08e98fc6
ML
1786 free_bitmap:
1787 blk_mq_free_bitmap(&hctx->ctx_map);
1788 free_ctxs:
1789 kfree(hctx->ctxs);
1790 unregister_cpu_notifier:
1791 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
320ae51f 1792
08e98fc6
ML
1793 return -1;
1794}
320ae51f 1795
320ae51f
JA
1796static void blk_mq_init_cpu_queues(struct request_queue *q,
1797 unsigned int nr_hw_queues)
1798{
1799 unsigned int i;
1800
1801 for_each_possible_cpu(i) {
1802 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1803 struct blk_mq_hw_ctx *hctx;
1804
1805 memset(__ctx, 0, sizeof(*__ctx));
1806 __ctx->cpu = i;
1807 spin_lock_init(&__ctx->lock);
1808 INIT_LIST_HEAD(&__ctx->rq_list);
1809 __ctx->queue = q;
1810
1811 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1812 if (!cpu_online(i))
1813 continue;
1814
e4043dcf 1815 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1816
320ae51f
JA
1817 /*
1818 * Set local node, IFF we have more than one hw queue. If
1819 * not, we remain on the home node of the device
1820 */
1821 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
bffed457 1822 hctx->numa_node = local_memory_node(cpu_to_node(i));
320ae51f
JA
1823 }
1824}
1825
5778322e
AM
1826static void blk_mq_map_swqueue(struct request_queue *q,
1827 const struct cpumask *online_mask)
320ae51f
JA
1828{
1829 unsigned int i;
1830 struct blk_mq_hw_ctx *hctx;
1831 struct blk_mq_ctx *ctx;
2a34c087 1832 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 1833
60de074b
AM
1834 /*
1835 * Avoid others reading imcomplete hctx->cpumask through sysfs
1836 */
1837 mutex_lock(&q->sysfs_lock);
1838
320ae51f 1839 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1840 cpumask_clear(hctx->cpumask);
320ae51f
JA
1841 hctx->nr_ctx = 0;
1842 }
1843
1844 /*
1845 * Map software to hardware queues
1846 */
897bb0c7 1847 for_each_possible_cpu(i) {
320ae51f 1848 /* If the cpu isn't online, the cpu is mapped to first hctx */
5778322e 1849 if (!cpumask_test_cpu(i, online_mask))
e4043dcf
JA
1850 continue;
1851
897bb0c7 1852 ctx = per_cpu_ptr(q->queue_ctx, i);
320ae51f 1853 hctx = q->mq_ops->map_queue(q, i);
868f2f0b 1854
e4043dcf 1855 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1856 ctx->index_hw = hctx->nr_ctx;
1857 hctx->ctxs[hctx->nr_ctx++] = ctx;
1858 }
506e931f 1859
60de074b
AM
1860 mutex_unlock(&q->sysfs_lock);
1861
506e931f 1862 queue_for_each_hw_ctx(q, hctx, i) {
889fa31f
CY
1863 struct blk_mq_ctxmap *map = &hctx->ctx_map;
1864
484b4061 1865 /*
a68aafa5
JA
1866 * If no software queues are mapped to this hardware queue,
1867 * disable it and free the request entries.
484b4061
JA
1868 */
1869 if (!hctx->nr_ctx) {
484b4061
JA
1870 if (set->tags[i]) {
1871 blk_mq_free_rq_map(set, set->tags[i], i);
1872 set->tags[i] = NULL;
484b4061 1873 }
2a34c087 1874 hctx->tags = NULL;
484b4061
JA
1875 continue;
1876 }
1877
2a34c087
ML
1878 /* unmapped hw queue can be remapped after CPU topo changed */
1879 if (!set->tags[i])
1880 set->tags[i] = blk_mq_init_rq_map(set, i);
1881 hctx->tags = set->tags[i];
1882 WARN_ON(!hctx->tags);
1883
e0e827b9 1884 cpumask_copy(hctx->tags->cpumask, hctx->cpumask);
889fa31f
CY
1885 /*
1886 * Set the map size to the number of mapped software queues.
1887 * This is more accurate and more efficient than looping
1888 * over all possibly mapped software queues.
1889 */
569fd0ce 1890 map->size = DIV_ROUND_UP(hctx->nr_ctx, map->bits_per_word);
889fa31f 1891
484b4061
JA
1892 /*
1893 * Initialize batch roundrobin counts
1894 */
506e931f
JA
1895 hctx->next_cpu = cpumask_first(hctx->cpumask);
1896 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1897 }
320ae51f
JA
1898}
1899
2404e607 1900static void queue_set_hctx_shared(struct request_queue *q, bool shared)
0d2602ca
JA
1901{
1902 struct blk_mq_hw_ctx *hctx;
0d2602ca
JA
1903 int i;
1904
2404e607
JM
1905 queue_for_each_hw_ctx(q, hctx, i) {
1906 if (shared)
1907 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1908 else
1909 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1910 }
1911}
1912
1913static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
1914{
1915 struct request_queue *q;
0d2602ca
JA
1916
1917 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1918 blk_mq_freeze_queue(q);
2404e607 1919 queue_set_hctx_shared(q, shared);
0d2602ca
JA
1920 blk_mq_unfreeze_queue(q);
1921 }
1922}
1923
1924static void blk_mq_del_queue_tag_set(struct request_queue *q)
1925{
1926 struct blk_mq_tag_set *set = q->tag_set;
1927
0d2602ca
JA
1928 mutex_lock(&set->tag_list_lock);
1929 list_del_init(&q->tag_set_list);
2404e607
JM
1930 if (list_is_singular(&set->tag_list)) {
1931 /* just transitioned to unshared */
1932 set->flags &= ~BLK_MQ_F_TAG_SHARED;
1933 /* update existing queue */
1934 blk_mq_update_tag_set_depth(set, false);
1935 }
0d2602ca 1936 mutex_unlock(&set->tag_list_lock);
0d2602ca
JA
1937}
1938
1939static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1940 struct request_queue *q)
1941{
1942 q->tag_set = set;
1943
1944 mutex_lock(&set->tag_list_lock);
2404e607
JM
1945
1946 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1947 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
1948 set->flags |= BLK_MQ_F_TAG_SHARED;
1949 /* update existing queue */
1950 blk_mq_update_tag_set_depth(set, true);
1951 }
1952 if (set->flags & BLK_MQ_F_TAG_SHARED)
1953 queue_set_hctx_shared(q, true);
0d2602ca 1954 list_add_tail(&q->tag_set_list, &set->tag_list);
2404e607 1955
0d2602ca
JA
1956 mutex_unlock(&set->tag_list_lock);
1957}
1958
e09aae7e
ML
1959/*
1960 * It is the actual release handler for mq, but we do it from
1961 * request queue's release handler for avoiding use-after-free
1962 * and headache because q->mq_kobj shouldn't have been introduced,
1963 * but we can't group ctx/kctx kobj without it.
1964 */
1965void blk_mq_release(struct request_queue *q)
1966{
1967 struct blk_mq_hw_ctx *hctx;
1968 unsigned int i;
1969
1970 /* hctx kobj stays in hctx */
c3b4afca
ML
1971 queue_for_each_hw_ctx(q, hctx, i) {
1972 if (!hctx)
1973 continue;
1974 kfree(hctx->ctxs);
e09aae7e 1975 kfree(hctx);
c3b4afca 1976 }
e09aae7e 1977
a723bab3
AM
1978 kfree(q->mq_map);
1979 q->mq_map = NULL;
1980
e09aae7e
ML
1981 kfree(q->queue_hw_ctx);
1982
1983 /* ctx kobj stays in queue_ctx */
1984 free_percpu(q->queue_ctx);
1985}
1986
24d2f903 1987struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
b62c21b7
MS
1988{
1989 struct request_queue *uninit_q, *q;
1990
1991 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1992 if (!uninit_q)
1993 return ERR_PTR(-ENOMEM);
1994
1995 q = blk_mq_init_allocated_queue(set, uninit_q);
1996 if (IS_ERR(q))
1997 blk_cleanup_queue(uninit_q);
1998
1999 return q;
2000}
2001EXPORT_SYMBOL(blk_mq_init_queue);
2002
868f2f0b
KB
2003static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2004 struct request_queue *q)
320ae51f 2005{
868f2f0b
KB
2006 int i, j;
2007 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
f14bbe77 2008
868f2f0b 2009 blk_mq_sysfs_unregister(q);
24d2f903 2010 for (i = 0; i < set->nr_hw_queues; i++) {
868f2f0b 2011 int node;
f14bbe77 2012
868f2f0b
KB
2013 if (hctxs[i])
2014 continue;
2015
2016 node = blk_mq_hw_queue_to_node(q->mq_map, i);
cdef54dd
CH
2017 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
2018 GFP_KERNEL, node);
320ae51f 2019 if (!hctxs[i])
868f2f0b 2020 break;
320ae51f 2021
a86073e4 2022 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
868f2f0b
KB
2023 node)) {
2024 kfree(hctxs[i]);
2025 hctxs[i] = NULL;
2026 break;
2027 }
e4043dcf 2028
0d2602ca 2029 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 2030 hctxs[i]->numa_node = node;
320ae51f 2031 hctxs[i]->queue_num = i;
868f2f0b
KB
2032
2033 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2034 free_cpumask_var(hctxs[i]->cpumask);
2035 kfree(hctxs[i]);
2036 hctxs[i] = NULL;
2037 break;
2038 }
2039 blk_mq_hctx_kobj_init(hctxs[i]);
320ae51f 2040 }
868f2f0b
KB
2041 for (j = i; j < q->nr_hw_queues; j++) {
2042 struct blk_mq_hw_ctx *hctx = hctxs[j];
2043
2044 if (hctx) {
2045 if (hctx->tags) {
2046 blk_mq_free_rq_map(set, hctx->tags, j);
2047 set->tags[j] = NULL;
2048 }
2049 blk_mq_exit_hctx(q, set, hctx, j);
2050 free_cpumask_var(hctx->cpumask);
2051 kobject_put(&hctx->kobj);
2052 kfree(hctx->ctxs);
2053 kfree(hctx);
2054 hctxs[j] = NULL;
2055
2056 }
2057 }
2058 q->nr_hw_queues = i;
2059 blk_mq_sysfs_register(q);
2060}
2061
2062struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2063 struct request_queue *q)
2064{
66841672
ML
2065 /* mark the queue as mq asap */
2066 q->mq_ops = set->ops;
2067
868f2f0b
KB
2068 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2069 if (!q->queue_ctx)
c7de5726 2070 goto err_exit;
868f2f0b
KB
2071
2072 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2073 GFP_KERNEL, set->numa_node);
2074 if (!q->queue_hw_ctx)
2075 goto err_percpu;
2076
2077 q->mq_map = blk_mq_make_queue_map(set);
2078 if (!q->mq_map)
2079 goto err_map;
2080
2081 blk_mq_realloc_hw_ctxs(set, q);
2082 if (!q->nr_hw_queues)
2083 goto err_hctxs;
320ae51f 2084
287922eb 2085 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
e56f698b 2086 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
320ae51f
JA
2087
2088 q->nr_queues = nr_cpu_ids;
320ae51f 2089
94eddfbe 2090 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 2091
05f1dd53
JA
2092 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2093 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2094
1be036e9
CH
2095 q->sg_reserved_size = INT_MAX;
2096
6fca6a61
CH
2097 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
2098 INIT_LIST_HEAD(&q->requeue_list);
2099 spin_lock_init(&q->requeue_lock);
2100
07068d5b
JA
2101 if (q->nr_hw_queues > 1)
2102 blk_queue_make_request(q, blk_mq_make_request);
2103 else
2104 blk_queue_make_request(q, blk_sq_make_request);
2105
eba71768
JA
2106 /*
2107 * Do this after blk_queue_make_request() overrides it...
2108 */
2109 q->nr_requests = set->queue_depth;
2110
24d2f903
CH
2111 if (set->ops->complete)
2112 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 2113
24d2f903 2114 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 2115
5778322e 2116 get_online_cpus();
320ae51f 2117 mutex_lock(&all_q_mutex);
320ae51f 2118
4593fdbe 2119 list_add_tail(&q->all_q_node, &all_q_list);
0d2602ca 2120 blk_mq_add_queue_tag_set(set, q);
5778322e 2121 blk_mq_map_swqueue(q, cpu_online_mask);
484b4061 2122
4593fdbe 2123 mutex_unlock(&all_q_mutex);
5778322e 2124 put_online_cpus();
4593fdbe 2125
320ae51f 2126 return q;
18741986 2127
320ae51f 2128err_hctxs:
868f2f0b 2129 kfree(q->mq_map);
f14bbe77 2130err_map:
868f2f0b 2131 kfree(q->queue_hw_ctx);
320ae51f 2132err_percpu:
868f2f0b 2133 free_percpu(q->queue_ctx);
c7de5726
ML
2134err_exit:
2135 q->mq_ops = NULL;
320ae51f
JA
2136 return ERR_PTR(-ENOMEM);
2137}
b62c21b7 2138EXPORT_SYMBOL(blk_mq_init_allocated_queue);
320ae51f
JA
2139
2140void blk_mq_free_queue(struct request_queue *q)
2141{
624dbe47 2142 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 2143
0e626368
AM
2144 mutex_lock(&all_q_mutex);
2145 list_del_init(&q->all_q_node);
2146 mutex_unlock(&all_q_mutex);
2147
0d2602ca
JA
2148 blk_mq_del_queue_tag_set(q);
2149
624dbe47
ML
2150 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2151 blk_mq_free_hw_queues(q, set);
320ae51f 2152}
320ae51f
JA
2153
2154/* Basically redo blk_mq_init_queue with queue frozen */
5778322e
AM
2155static void blk_mq_queue_reinit(struct request_queue *q,
2156 const struct cpumask *online_mask)
320ae51f 2157{
4ecd4fef 2158 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
320ae51f 2159
67aec14c
JA
2160 blk_mq_sysfs_unregister(q);
2161
5778322e 2162 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues, online_mask);
320ae51f
JA
2163
2164 /*
2165 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2166 * we should change hctx numa_node according to new topology (this
2167 * involves free and re-allocate memory, worthy doing?)
2168 */
2169
5778322e 2170 blk_mq_map_swqueue(q, online_mask);
320ae51f 2171
67aec14c 2172 blk_mq_sysfs_register(q);
320ae51f
JA
2173}
2174
f618ef7c
PG
2175static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
2176 unsigned long action, void *hcpu)
320ae51f
JA
2177{
2178 struct request_queue *q;
5778322e
AM
2179 int cpu = (unsigned long)hcpu;
2180 /*
2181 * New online cpumask which is going to be set in this hotplug event.
2182 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2183 * one-by-one and dynamically allocating this could result in a failure.
2184 */
2185 static struct cpumask online_new;
320ae51f
JA
2186
2187 /*
5778322e
AM
2188 * Before hotadded cpu starts handling requests, new mappings must
2189 * be established. Otherwise, these requests in hw queue might
2190 * never be dispatched.
2191 *
2192 * For example, there is a single hw queue (hctx) and two CPU queues
2193 * (ctx0 for CPU0, and ctx1 for CPU1).
2194 *
2195 * Now CPU1 is just onlined and a request is inserted into
2196 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2197 * still zero.
2198 *
2199 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2200 * set in pending bitmap and tries to retrieve requests in
2201 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2202 * so the request in ctx1->rq_list is ignored.
320ae51f 2203 */
5778322e
AM
2204 switch (action & ~CPU_TASKS_FROZEN) {
2205 case CPU_DEAD:
2206 case CPU_UP_CANCELED:
2207 cpumask_copy(&online_new, cpu_online_mask);
2208 break;
2209 case CPU_UP_PREPARE:
2210 cpumask_copy(&online_new, cpu_online_mask);
2211 cpumask_set_cpu(cpu, &online_new);
2212 break;
2213 default:
320ae51f 2214 return NOTIFY_OK;
5778322e 2215 }
320ae51f
JA
2216
2217 mutex_lock(&all_q_mutex);
f3af020b
TH
2218
2219 /*
2220 * We need to freeze and reinit all existing queues. Freezing
2221 * involves synchronous wait for an RCU grace period and doing it
2222 * one by one may take a long time. Start freezing all queues in
2223 * one swoop and then wait for the completions so that freezing can
2224 * take place in parallel.
2225 */
2226 list_for_each_entry(q, &all_q_list, all_q_node)
2227 blk_mq_freeze_queue_start(q);
f054b56c 2228 list_for_each_entry(q, &all_q_list, all_q_node) {
f3af020b
TH
2229 blk_mq_freeze_queue_wait(q);
2230
f054b56c
ML
2231 /*
2232 * timeout handler can't touch hw queue during the
2233 * reinitialization
2234 */
2235 del_timer_sync(&q->timeout);
2236 }
2237
320ae51f 2238 list_for_each_entry(q, &all_q_list, all_q_node)
5778322e 2239 blk_mq_queue_reinit(q, &online_new);
f3af020b
TH
2240
2241 list_for_each_entry(q, &all_q_list, all_q_node)
2242 blk_mq_unfreeze_queue(q);
2243
320ae51f
JA
2244 mutex_unlock(&all_q_mutex);
2245 return NOTIFY_OK;
2246}
2247
a5164405
JA
2248static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2249{
2250 int i;
2251
2252 for (i = 0; i < set->nr_hw_queues; i++) {
2253 set->tags[i] = blk_mq_init_rq_map(set, i);
2254 if (!set->tags[i])
2255 goto out_unwind;
2256 }
2257
2258 return 0;
2259
2260out_unwind:
2261 while (--i >= 0)
2262 blk_mq_free_rq_map(set, set->tags[i], i);
2263
a5164405
JA
2264 return -ENOMEM;
2265}
2266
2267/*
2268 * Allocate the request maps associated with this tag_set. Note that this
2269 * may reduce the depth asked for, if memory is tight. set->queue_depth
2270 * will be updated to reflect the allocated depth.
2271 */
2272static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2273{
2274 unsigned int depth;
2275 int err;
2276
2277 depth = set->queue_depth;
2278 do {
2279 err = __blk_mq_alloc_rq_maps(set);
2280 if (!err)
2281 break;
2282
2283 set->queue_depth >>= 1;
2284 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2285 err = -ENOMEM;
2286 break;
2287 }
2288 } while (set->queue_depth);
2289
2290 if (!set->queue_depth || err) {
2291 pr_err("blk-mq: failed to allocate request map\n");
2292 return -ENOMEM;
2293 }
2294
2295 if (depth != set->queue_depth)
2296 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2297 depth, set->queue_depth);
2298
2299 return 0;
2300}
2301
f26cdc85
KB
2302struct cpumask *blk_mq_tags_cpumask(struct blk_mq_tags *tags)
2303{
2304 return tags->cpumask;
2305}
2306EXPORT_SYMBOL_GPL(blk_mq_tags_cpumask);
2307
a4391c64
JA
2308/*
2309 * Alloc a tag set to be associated with one or more request queues.
2310 * May fail with EINVAL for various error conditions. May adjust the
2311 * requested depth down, if if it too large. In that case, the set
2312 * value will be stored in set->queue_depth.
2313 */
24d2f903
CH
2314int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2315{
205fb5f5
BVA
2316 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2317
24d2f903
CH
2318 if (!set->nr_hw_queues)
2319 return -EINVAL;
a4391c64 2320 if (!set->queue_depth)
24d2f903
CH
2321 return -EINVAL;
2322 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2323 return -EINVAL;
2324
f9018ac9 2325 if (!set->ops->queue_rq || !set->ops->map_queue)
24d2f903
CH
2326 return -EINVAL;
2327
a4391c64
JA
2328 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2329 pr_info("blk-mq: reduced tag depth to %u\n",
2330 BLK_MQ_MAX_DEPTH);
2331 set->queue_depth = BLK_MQ_MAX_DEPTH;
2332 }
24d2f903 2333
6637fadf
SL
2334 /*
2335 * If a crashdump is active, then we are potentially in a very
2336 * memory constrained environment. Limit us to 1 queue and
2337 * 64 tags to prevent using too much memory.
2338 */
2339 if (is_kdump_kernel()) {
2340 set->nr_hw_queues = 1;
2341 set->queue_depth = min(64U, set->queue_depth);
2342 }
868f2f0b
KB
2343 /*
2344 * There is no use for more h/w queues than cpus.
2345 */
2346 if (set->nr_hw_queues > nr_cpu_ids)
2347 set->nr_hw_queues = nr_cpu_ids;
6637fadf 2348
868f2f0b 2349 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
24d2f903
CH
2350 GFP_KERNEL, set->numa_node);
2351 if (!set->tags)
a5164405 2352 return -ENOMEM;
24d2f903 2353
a5164405
JA
2354 if (blk_mq_alloc_rq_maps(set))
2355 goto enomem;
24d2f903 2356
0d2602ca
JA
2357 mutex_init(&set->tag_list_lock);
2358 INIT_LIST_HEAD(&set->tag_list);
2359
24d2f903 2360 return 0;
a5164405 2361enomem:
5676e7b6
RE
2362 kfree(set->tags);
2363 set->tags = NULL;
24d2f903
CH
2364 return -ENOMEM;
2365}
2366EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2367
2368void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2369{
2370 int i;
2371
868f2f0b 2372 for (i = 0; i < nr_cpu_ids; i++) {
f42d79ab 2373 if (set->tags[i])
484b4061
JA
2374 blk_mq_free_rq_map(set, set->tags[i], i);
2375 }
2376
981bd189 2377 kfree(set->tags);
5676e7b6 2378 set->tags = NULL;
24d2f903
CH
2379}
2380EXPORT_SYMBOL(blk_mq_free_tag_set);
2381
e3a2b3f9
JA
2382int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2383{
2384 struct blk_mq_tag_set *set = q->tag_set;
2385 struct blk_mq_hw_ctx *hctx;
2386 int i, ret;
2387
2388 if (!set || nr > set->queue_depth)
2389 return -EINVAL;
2390
2391 ret = 0;
2392 queue_for_each_hw_ctx(q, hctx, i) {
e9137d4b
KB
2393 if (!hctx->tags)
2394 continue;
e3a2b3f9
JA
2395 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2396 if (ret)
2397 break;
2398 }
2399
2400 if (!ret)
2401 q->nr_requests = nr;
2402
2403 return ret;
2404}
2405
868f2f0b
KB
2406void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2407{
2408 struct request_queue *q;
2409
2410 if (nr_hw_queues > nr_cpu_ids)
2411 nr_hw_queues = nr_cpu_ids;
2412 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2413 return;
2414
2415 list_for_each_entry(q, &set->tag_list, tag_set_list)
2416 blk_mq_freeze_queue(q);
2417
2418 set->nr_hw_queues = nr_hw_queues;
2419 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2420 blk_mq_realloc_hw_ctxs(set, q);
2421
2422 if (q->nr_hw_queues > 1)
2423 blk_queue_make_request(q, blk_mq_make_request);
2424 else
2425 blk_queue_make_request(q, blk_sq_make_request);
2426
2427 blk_mq_queue_reinit(q, cpu_online_mask);
2428 }
2429
2430 list_for_each_entry(q, &set->tag_list, tag_set_list)
2431 blk_mq_unfreeze_queue(q);
2432}
2433EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2434
676141e4
JA
2435void blk_mq_disable_hotplug(void)
2436{
2437 mutex_lock(&all_q_mutex);
2438}
2439
2440void blk_mq_enable_hotplug(void)
2441{
2442 mutex_unlock(&all_q_mutex);
2443}
2444
320ae51f
JA
2445static int __init blk_mq_init(void)
2446{
320ae51f
JA
2447 blk_mq_cpu_init();
2448
add703fd 2449 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
320ae51f
JA
2450
2451 return 0;
2452}
2453subsys_initcall(blk_mq_init);
This page took 0.260355 seconds and 5 git commands to generate.