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