]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq.c
blk-mq: remove stale comment for blk_mq_complete_request()
[mirror_ubuntu-bionic-kernel.git] / block / blk-mq.c
CommitLineData
320ae51f
JA
1#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/backing-dev.h>
4#include <linux/bio.h>
5#include <linux/blkdev.h>
6#include <linux/mm.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/workqueue.h>
10#include <linux/smp.h>
11#include <linux/llist.h>
12#include <linux/list_sort.h>
13#include <linux/cpu.h>
14#include <linux/cache.h>
15#include <linux/sched/sysctl.h>
16#include <linux/delay.h>
17
18#include <trace/events/block.h>
19
20#include <linux/blk-mq.h>
21#include "blk.h"
22#include "blk-mq.h"
23#include "blk-mq-tag.h"
24
25static DEFINE_MUTEX(all_q_mutex);
26static LIST_HEAD(all_q_list);
27
28static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
29
320ae51f
JA
30static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
31 unsigned int cpu)
32{
33 return per_cpu_ptr(q->queue_ctx, cpu);
34}
35
36/*
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
41 */
42static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
43{
44 return __blk_mq_get_ctx(q, get_cpu());
45}
46
47static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
48{
49 put_cpu();
50}
51
52/*
53 * Check if any of the ctx's have pending work in this hardware queue
54 */
55static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
56{
57 unsigned int i;
58
1429d7c9
JA
59 for (i = 0; i < hctx->ctx_map.map_size; i++)
60 if (hctx->ctx_map.map[i].word)
320ae51f
JA
61 return true;
62
63 return false;
64}
65
1429d7c9
JA
66static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
67 struct blk_mq_ctx *ctx)
68{
69 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
70}
71
72#define CTX_TO_BIT(hctx, ctx) \
73 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
74
320ae51f
JA
75/*
76 * Mark this ctx as having pending work in this hardware queue
77 */
78static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
79 struct blk_mq_ctx *ctx)
80{
1429d7c9
JA
81 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
82
83 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
84 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
85}
86
87static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
88 struct blk_mq_ctx *ctx)
89{
90 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
91
92 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
320ae51f
JA
93}
94
081241e5 95static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
4bb659b1 96 struct blk_mq_ctx *ctx,
081241e5 97 gfp_t gfp, bool reserved)
320ae51f
JA
98{
99 struct request *rq;
100 unsigned int tag;
101
0d2602ca 102 tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
320ae51f 103 if (tag != BLK_MQ_TAG_FAIL) {
24d2f903 104 rq = hctx->tags->rqs[tag];
0d2602ca
JA
105
106 rq->cmd_flags = 0;
107 if (blk_mq_tag_busy(hctx)) {
108 rq->cmd_flags = REQ_MQ_INFLIGHT;
109 atomic_inc(&hctx->nr_active);
110 }
111
320ae51f 112 rq->tag = tag;
320ae51f
JA
113 return rq;
114 }
115
116 return NULL;
117}
118
119static int blk_mq_queue_enter(struct request_queue *q)
120{
121 int ret;
122
123 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
124 smp_wmb();
125 /* we have problems to freeze the queue if it's initializing */
126 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
127 return 0;
128
129 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
130
131 spin_lock_irq(q->queue_lock);
132 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
43a5e4e2
ML
133 !blk_queue_bypass(q) || blk_queue_dying(q),
134 *q->queue_lock);
320ae51f 135 /* inc usage with lock hold to avoid freeze_queue runs here */
43a5e4e2 136 if (!ret && !blk_queue_dying(q))
320ae51f 137 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
43a5e4e2
ML
138 else if (blk_queue_dying(q))
139 ret = -ENODEV;
320ae51f
JA
140 spin_unlock_irq(q->queue_lock);
141
142 return ret;
143}
144
145static void blk_mq_queue_exit(struct request_queue *q)
146{
147 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
148}
149
43a5e4e2
ML
150static void __blk_mq_drain_queue(struct request_queue *q)
151{
152 while (true) {
153 s64 count;
154
155 spin_lock_irq(q->queue_lock);
156 count = percpu_counter_sum(&q->mq_usage_counter);
157 spin_unlock_irq(q->queue_lock);
158
159 if (count == 0)
160 break;
161 blk_mq_run_queues(q, false);
162 msleep(10);
163 }
164}
165
320ae51f
JA
166/*
167 * Guarantee no request is in use, so we can change any data structure of
168 * the queue afterward.
169 */
170static void blk_mq_freeze_queue(struct request_queue *q)
171{
172 bool drain;
173
174 spin_lock_irq(q->queue_lock);
175 drain = !q->bypass_depth++;
176 queue_flag_set(QUEUE_FLAG_BYPASS, q);
177 spin_unlock_irq(q->queue_lock);
178
43a5e4e2
ML
179 if (drain)
180 __blk_mq_drain_queue(q);
181}
320ae51f 182
43a5e4e2
ML
183void blk_mq_drain_queue(struct request_queue *q)
184{
185 __blk_mq_drain_queue(q);
320ae51f
JA
186}
187
188static void blk_mq_unfreeze_queue(struct request_queue *q)
189{
190 bool wake = false;
191
192 spin_lock_irq(q->queue_lock);
193 if (!--q->bypass_depth) {
194 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
195 wake = true;
196 }
197 WARN_ON_ONCE(q->bypass_depth < 0);
198 spin_unlock_irq(q->queue_lock);
199 if (wake)
200 wake_up_all(&q->mq_freeze_wq);
201}
202
203bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
204{
205 return blk_mq_has_free_tags(hctx->tags);
206}
207EXPORT_SYMBOL(blk_mq_can_queue);
208
94eddfbe
JA
209static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
210 struct request *rq, unsigned int rw_flags)
320ae51f 211{
94eddfbe
JA
212 if (blk_queue_io_stat(q))
213 rw_flags |= REQ_IO_STAT;
214
af76e555
CH
215 INIT_LIST_HEAD(&rq->queuelist);
216 /* csd/requeue_work/fifo_time is initialized before use */
217 rq->q = q;
320ae51f 218 rq->mq_ctx = ctx;
0d2602ca 219 rq->cmd_flags |= rw_flags;
af76e555
CH
220 rq->cmd_type = 0;
221 /* do not touch atomic flags, it needs atomic ops against the timer */
222 rq->cpu = -1;
223 rq->__data_len = 0;
224 rq->__sector = (sector_t) -1;
225 rq->bio = NULL;
226 rq->biotail = NULL;
227 INIT_HLIST_NODE(&rq->hash);
228 RB_CLEAR_NODE(&rq->rb_node);
229 memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv)));
230 rq->rq_disk = NULL;
231 rq->part = NULL;
0fec08b4 232 rq->start_time = jiffies;
af76e555
CH
233#ifdef CONFIG_BLK_CGROUP
234 rq->rl = NULL;
0fec08b4 235 set_start_time_ns(rq);
af76e555
CH
236 rq->io_start_time_ns = 0;
237#endif
238 rq->nr_phys_segments = 0;
239#if defined(CONFIG_BLK_DEV_INTEGRITY)
240 rq->nr_integrity_segments = 0;
241#endif
242 rq->ioprio = 0;
243 rq->special = NULL;
244 /* tag was already set */
245 rq->errors = 0;
246 memset(rq->__cmd, 0, sizeof(rq->__cmd));
247 rq->cmd = rq->__cmd;
248 rq->cmd_len = BLK_MAX_CDB;
249
250 rq->extra_len = 0;
251 rq->sense_len = 0;
252 rq->resid_len = 0;
253 rq->sense = NULL;
254
255 rq->deadline = 0;
256 INIT_LIST_HEAD(&rq->timeout_list);
257 rq->timeout = 0;
258 rq->retries = 0;
259 rq->end_io = NULL;
260 rq->end_io_data = NULL;
261 rq->next_rq = NULL;
262
320ae51f
JA
263 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
264}
265
320ae51f
JA
266static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
267 int rw, gfp_t gfp,
268 bool reserved)
269{
270 struct request *rq;
271
272 do {
273 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
274 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
275
4bb659b1
JA
276 rq = __blk_mq_alloc_request(hctx, ctx, gfp & ~__GFP_WAIT,
277 reserved);
320ae51f 278 if (rq) {
94eddfbe 279 blk_mq_rq_ctx_init(q, ctx, rq, rw);
320ae51f 280 break;
959a35f1 281 }
320ae51f 282
e4043dcf
JA
283 if (gfp & __GFP_WAIT) {
284 __blk_mq_run_hw_queue(hctx);
285 blk_mq_put_ctx(ctx);
286 } else {
287 blk_mq_put_ctx(ctx);
959a35f1 288 break;
e4043dcf 289 }
959a35f1 290
0d2602ca 291 blk_mq_wait_for_tags(hctx, reserved);
320ae51f
JA
292 } while (1);
293
294 return rq;
295}
296
18741986 297struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
320ae51f
JA
298{
299 struct request *rq;
300
301 if (blk_mq_queue_enter(q))
302 return NULL;
303
18741986 304 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
959a35f1
JM
305 if (rq)
306 blk_mq_put_ctx(rq->mq_ctx);
320ae51f
JA
307 return rq;
308}
4bb659b1 309EXPORT_SYMBOL(blk_mq_alloc_request);
320ae51f
JA
310
311struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
312 gfp_t gfp)
313{
314 struct request *rq;
315
316 if (blk_mq_queue_enter(q))
317 return NULL;
318
319 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
959a35f1
JM
320 if (rq)
321 blk_mq_put_ctx(rq->mq_ctx);
320ae51f
JA
322 return rq;
323}
324EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
325
320ae51f
JA
326static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
327 struct blk_mq_ctx *ctx, struct request *rq)
328{
329 const int tag = rq->tag;
330 struct request_queue *q = rq->q;
331
0d2602ca
JA
332 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
333 atomic_dec(&hctx->nr_active);
334
af76e555 335 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
0d2602ca 336 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
320ae51f
JA
337 blk_mq_queue_exit(q);
338}
339
340void blk_mq_free_request(struct request *rq)
341{
342 struct blk_mq_ctx *ctx = rq->mq_ctx;
343 struct blk_mq_hw_ctx *hctx;
344 struct request_queue *q = rq->q;
345
346 ctx->rq_completed[rq_is_sync(rq)]++;
347
348 hctx = q->mq_ops->map_queue(q, ctx->cpu);
349 __blk_mq_free_request(hctx, ctx, rq);
350}
351
8727af4b
CH
352/*
353 * Clone all relevant state from a request that has been put on hold in
354 * the flush state machine into the preallocated flush request that hangs
355 * off the request queue.
356 *
357 * For a driver the flush request should be invisible, that's why we are
358 * impersonating the original request here.
359 */
360void blk_mq_clone_flush_request(struct request *flush_rq,
361 struct request *orig_rq)
362{
363 struct blk_mq_hw_ctx *hctx =
364 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
365
366 flush_rq->mq_ctx = orig_rq->mq_ctx;
367 flush_rq->tag = orig_rq->tag;
368 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
369 hctx->cmd_size);
370}
371
63151a44 372inline void __blk_mq_end_io(struct request *rq, int error)
320ae51f 373{
0d11e6ac
ML
374 blk_account_io_done(rq);
375
91b63639 376 if (rq->end_io) {
320ae51f 377 rq->end_io(rq, error);
91b63639
CH
378 } else {
379 if (unlikely(blk_bidi_rq(rq)))
380 blk_mq_free_request(rq->next_rq);
320ae51f 381 blk_mq_free_request(rq);
91b63639 382 }
320ae51f 383}
63151a44
CH
384EXPORT_SYMBOL(__blk_mq_end_io);
385
386void blk_mq_end_io(struct request *rq, int error)
387{
388 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
389 BUG();
390 __blk_mq_end_io(rq, error);
391}
392EXPORT_SYMBOL(blk_mq_end_io);
320ae51f 393
30a91cb4 394static void __blk_mq_complete_request_remote(void *data)
320ae51f 395{
3d6efbf6 396 struct request *rq = data;
320ae51f 397
30a91cb4 398 rq->q->softirq_done_fn(rq);
320ae51f 399}
320ae51f 400
30a91cb4 401void __blk_mq_complete_request(struct request *rq)
320ae51f
JA
402{
403 struct blk_mq_ctx *ctx = rq->mq_ctx;
38535201 404 bool shared = false;
320ae51f
JA
405 int cpu;
406
38535201 407 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
30a91cb4
CH
408 rq->q->softirq_done_fn(rq);
409 return;
410 }
320ae51f
JA
411
412 cpu = get_cpu();
38535201
CH
413 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
414 shared = cpus_share_cache(cpu, ctx->cpu);
415
416 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
30a91cb4 417 rq->csd.func = __blk_mq_complete_request_remote;
3d6efbf6
CH
418 rq->csd.info = rq;
419 rq->csd.flags = 0;
c46fff2a 420 smp_call_function_single_async(ctx->cpu, &rq->csd);
3d6efbf6 421 } else {
30a91cb4 422 rq->q->softirq_done_fn(rq);
3d6efbf6 423 }
320ae51f
JA
424 put_cpu();
425}
30a91cb4
CH
426
427/**
428 * blk_mq_complete_request - end I/O on a request
429 * @rq: the request being processed
430 *
431 * Description:
432 * Ends all I/O on a request. It does not handle partial completions.
433 * The actual completion happens out-of-order, through a IPI handler.
434 **/
435void blk_mq_complete_request(struct request *rq)
436{
95f09684
JA
437 struct request_queue *q = rq->q;
438
439 if (unlikely(blk_should_fake_timeout(q)))
30a91cb4 440 return;
95f09684
JA
441 if (!blk_mark_rq_complete(rq)) {
442 if (q->softirq_done_fn)
443 __blk_mq_complete_request(rq);
444 else
445 blk_mq_end_io(rq, rq->errors);
446 }
30a91cb4
CH
447}
448EXPORT_SYMBOL(blk_mq_complete_request);
320ae51f 449
49f5baa5 450static void blk_mq_start_request(struct request *rq, bool last)
320ae51f
JA
451{
452 struct request_queue *q = rq->q;
453
454 trace_block_rq_issue(q, rq);
455
742ee69b 456 rq->resid_len = blk_rq_bytes(rq);
91b63639
CH
457 if (unlikely(blk_bidi_rq(rq)))
458 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
742ee69b 459
320ae51f
JA
460 /*
461 * Just mark start time and set the started bit. Due to memory
462 * ordering, we know we'll see the correct deadline as long as
c22d9d8a
JA
463 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
464 * unless one has been set in the request.
320ae51f 465 */
c22d9d8a
JA
466 if (!rq->timeout)
467 rq->deadline = jiffies + q->rq_timeout;
468 else
469 rq->deadline = jiffies + rq->timeout;
87ee7b11
JA
470
471 /*
472 * Mark us as started and clear complete. Complete might have been
473 * set if requeue raced with timeout, which then marked it as
474 * complete. So be sure to clear complete again when we start
475 * the request, otherwise we'll ignore the completion event.
476 */
320ae51f 477 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
87ee7b11 478 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
49f5baa5
CH
479
480 if (q->dma_drain_size && blk_rq_bytes(rq)) {
481 /*
482 * Make sure space for the drain appears. We know we can do
483 * this because max_hw_segments has been adjusted to be one
484 * fewer than the device can handle.
485 */
486 rq->nr_phys_segments++;
487 }
488
489 /*
490 * Flag the last request in the series so that drivers know when IO
491 * should be kicked off, if they don't do it on a per-request basis.
492 *
493 * Note: the flag isn't the only condition drivers should do kick off.
494 * If drive is busy, the last request might not have the bit set.
495 */
496 if (last)
497 rq->cmd_flags |= REQ_END;
320ae51f
JA
498}
499
ed0791b2 500static void __blk_mq_requeue_request(struct request *rq)
320ae51f
JA
501{
502 struct request_queue *q = rq->q;
503
504 trace_block_rq_requeue(q, rq);
505 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
49f5baa5
CH
506
507 rq->cmd_flags &= ~REQ_END;
508
509 if (q->dma_drain_size && blk_rq_bytes(rq))
510 rq->nr_phys_segments--;
320ae51f
JA
511}
512
ed0791b2
CH
513void blk_mq_requeue_request(struct request *rq)
514{
ed0791b2
CH
515 __blk_mq_requeue_request(rq);
516 blk_clear_rq_complete(rq);
517
ed0791b2
CH
518 BUG_ON(blk_queued_rq(rq));
519 blk_mq_insert_request(rq, true, true, false);
520}
521EXPORT_SYMBOL(blk_mq_requeue_request);
522
24d2f903
CH
523struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
524{
525 return tags->rqs[tag];
526}
527EXPORT_SYMBOL(blk_mq_tag_to_rq);
528
320ae51f
JA
529struct blk_mq_timeout_data {
530 struct blk_mq_hw_ctx *hctx;
531 unsigned long *next;
532 unsigned int *next_set;
533};
534
535static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
536{
537 struct blk_mq_timeout_data *data = __data;
538 struct blk_mq_hw_ctx *hctx = data->hctx;
539 unsigned int tag;
540
541 /* It may not be in flight yet (this is where
542 * the REQ_ATOMIC_STARTED flag comes in). The requests are
543 * statically allocated, so we know it's always safe to access the
544 * memory associated with a bit offset into ->rqs[].
545 */
546 tag = 0;
547 do {
548 struct request *rq;
549
24d2f903
CH
550 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
551 if (tag >= hctx->tags->nr_tags)
320ae51f
JA
552 break;
553
24d2f903
CH
554 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
555 if (rq->q != hctx->queue)
556 continue;
320ae51f
JA
557 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
558 continue;
559
560 blk_rq_check_expired(rq, data->next, data->next_set);
561 } while (1);
562}
563
564static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
565 unsigned long *next,
566 unsigned int *next_set)
567{
568 struct blk_mq_timeout_data data = {
569 .hctx = hctx,
570 .next = next,
571 .next_set = next_set,
572 };
573
574 /*
575 * Ask the tagging code to iterate busy requests, so we can
576 * check them for timeout.
577 */
578 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
579}
580
87ee7b11
JA
581static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
582{
583 struct request_queue *q = rq->q;
584
585 /*
586 * We know that complete is set at this point. If STARTED isn't set
587 * anymore, then the request isn't active and the "timeout" should
588 * just be ignored. This can happen due to the bitflag ordering.
589 * Timeout first checks if STARTED is set, and if it is, assumes
590 * the request is active. But if we race with completion, then
591 * we both flags will get cleared. So check here again, and ignore
592 * a timeout event with a request that isn't active.
593 */
594 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
595 return BLK_EH_NOT_HANDLED;
596
597 if (!q->mq_ops->timeout)
598 return BLK_EH_RESET_TIMER;
599
600 return q->mq_ops->timeout(rq);
601}
602
320ae51f
JA
603static void blk_mq_rq_timer(unsigned long data)
604{
605 struct request_queue *q = (struct request_queue *) data;
606 struct blk_mq_hw_ctx *hctx;
607 unsigned long next = 0;
608 int i, next_set = 0;
609
484b4061
JA
610 queue_for_each_hw_ctx(q, hctx, i) {
611 /*
612 * If not software queues are currently mapped to this
613 * hardware queue, there's nothing to check
614 */
615 if (!hctx->nr_ctx || !hctx->tags)
616 continue;
617
320ae51f 618 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
484b4061 619 }
320ae51f 620
0d2602ca
JA
621 if (next_set) {
622 next = blk_rq_timeout(round_jiffies_up(next));
623 mod_timer(&q->timeout, next);
624 } else {
625 queue_for_each_hw_ctx(q, hctx, i)
626 blk_mq_tag_idle(hctx);
627 }
320ae51f
JA
628}
629
630/*
631 * Reverse check our software queue for entries that we could potentially
632 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
633 * too much time checking for merges.
634 */
635static bool blk_mq_attempt_merge(struct request_queue *q,
636 struct blk_mq_ctx *ctx, struct bio *bio)
637{
638 struct request *rq;
639 int checked = 8;
640
641 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
642 int el_ret;
643
644 if (!checked--)
645 break;
646
647 if (!blk_rq_merge_ok(rq, bio))
648 continue;
649
650 el_ret = blk_try_merge(rq, bio);
651 if (el_ret == ELEVATOR_BACK_MERGE) {
652 if (bio_attempt_back_merge(q, rq, bio)) {
653 ctx->rq_merged++;
654 return true;
655 }
656 break;
657 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
658 if (bio_attempt_front_merge(q, rq, bio)) {
659 ctx->rq_merged++;
660 return true;
661 }
662 break;
663 }
664 }
665
666 return false;
667}
668
1429d7c9
JA
669/*
670 * Process software queues that have been marked busy, splicing them
671 * to the for-dispatch
672 */
673static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
674{
675 struct blk_mq_ctx *ctx;
676 int i;
677
678 for (i = 0; i < hctx->ctx_map.map_size; i++) {
679 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
680 unsigned int off, bit;
681
682 if (!bm->word)
683 continue;
684
685 bit = 0;
686 off = i * hctx->ctx_map.bits_per_word;
687 do {
688 bit = find_next_bit(&bm->word, bm->depth, bit);
689 if (bit >= bm->depth)
690 break;
691
692 ctx = hctx->ctxs[bit + off];
693 clear_bit(bit, &bm->word);
694 spin_lock(&ctx->lock);
695 list_splice_tail_init(&ctx->rq_list, list);
696 spin_unlock(&ctx->lock);
697
698 bit++;
699 } while (1);
700 }
701}
702
320ae51f
JA
703/*
704 * Run this hardware queue, pulling any software queues mapped to it in.
705 * Note that this function currently has various problems around ordering
706 * of IO. In particular, we'd like FIFO behaviour on handling existing
707 * items on the hctx->dispatch list. Ignore that for now.
708 */
709static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
710{
711 struct request_queue *q = hctx->queue;
320ae51f
JA
712 struct request *rq;
713 LIST_HEAD(rq_list);
1429d7c9 714 int queued;
320ae51f 715
fd1270d5 716 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
e4043dcf 717
5d12f905 718 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
719 return;
720
721 hctx->run++;
722
723 /*
724 * Touch any software queue that has pending entries.
725 */
1429d7c9 726 flush_busy_ctxs(hctx, &rq_list);
320ae51f
JA
727
728 /*
729 * If we have previous entries on our dispatch list, grab them
730 * and stuff them at the front for more fair dispatch.
731 */
732 if (!list_empty_careful(&hctx->dispatch)) {
733 spin_lock(&hctx->lock);
734 if (!list_empty(&hctx->dispatch))
735 list_splice_init(&hctx->dispatch, &rq_list);
736 spin_unlock(&hctx->lock);
737 }
738
320ae51f
JA
739 /*
740 * Now process all the entries, sending them to the driver.
741 */
1429d7c9 742 queued = 0;
320ae51f
JA
743 while (!list_empty(&rq_list)) {
744 int ret;
745
746 rq = list_first_entry(&rq_list, struct request, queuelist);
747 list_del_init(&rq->queuelist);
320ae51f 748
49f5baa5 749 blk_mq_start_request(rq, list_empty(&rq_list));
320ae51f
JA
750
751 ret = q->mq_ops->queue_rq(hctx, rq);
752 switch (ret) {
753 case BLK_MQ_RQ_QUEUE_OK:
754 queued++;
755 continue;
756 case BLK_MQ_RQ_QUEUE_BUSY:
320ae51f 757 list_add(&rq->queuelist, &rq_list);
ed0791b2 758 __blk_mq_requeue_request(rq);
320ae51f
JA
759 break;
760 default:
761 pr_err("blk-mq: bad return on queue: %d\n", ret);
320ae51f 762 case BLK_MQ_RQ_QUEUE_ERROR:
1e93b8c2 763 rq->errors = -EIO;
320ae51f
JA
764 blk_mq_end_io(rq, rq->errors);
765 break;
766 }
767
768 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
769 break;
770 }
771
772 if (!queued)
773 hctx->dispatched[0]++;
774 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
775 hctx->dispatched[ilog2(queued) + 1]++;
776
777 /*
778 * Any items that need requeuing? Stuff them into hctx->dispatch,
779 * that is where we will continue on next queue run.
780 */
781 if (!list_empty(&rq_list)) {
782 spin_lock(&hctx->lock);
783 list_splice(&rq_list, &hctx->dispatch);
784 spin_unlock(&hctx->lock);
785 }
786}
787
506e931f
JA
788/*
789 * It'd be great if the workqueue API had a way to pass
790 * in a mask and had some smarts for more clever placement.
791 * For now we just round-robin here, switching for every
792 * BLK_MQ_CPU_WORK_BATCH queued items.
793 */
794static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
795{
796 int cpu = hctx->next_cpu;
797
798 if (--hctx->next_cpu_batch <= 0) {
799 int next_cpu;
800
801 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
802 if (next_cpu >= nr_cpu_ids)
803 next_cpu = cpumask_first(hctx->cpumask);
804
805 hctx->next_cpu = next_cpu;
806 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
807 }
808
809 return cpu;
810}
811
320ae51f
JA
812void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
813{
5d12f905 814 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
320ae51f
JA
815 return;
816
e4043dcf 817 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
320ae51f 818 __blk_mq_run_hw_queue(hctx);
e4043dcf 819 else if (hctx->queue->nr_hw_queues == 1)
70f4db63 820 kblockd_schedule_delayed_work(&hctx->run_work, 0);
e4043dcf
JA
821 else {
822 unsigned int cpu;
823
506e931f 824 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63 825 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
e4043dcf 826 }
320ae51f
JA
827}
828
829void blk_mq_run_queues(struct request_queue *q, bool async)
830{
831 struct blk_mq_hw_ctx *hctx;
832 int i;
833
834 queue_for_each_hw_ctx(q, hctx, i) {
835 if ((!blk_mq_hctx_has_pending(hctx) &&
836 list_empty_careful(&hctx->dispatch)) ||
5d12f905 837 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
320ae51f
JA
838 continue;
839
e4043dcf 840 preempt_disable();
320ae51f 841 blk_mq_run_hw_queue(hctx, async);
e4043dcf 842 preempt_enable();
320ae51f
JA
843 }
844}
845EXPORT_SYMBOL(blk_mq_run_queues);
846
847void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
848{
70f4db63
CH
849 cancel_delayed_work(&hctx->run_work);
850 cancel_delayed_work(&hctx->delay_work);
320ae51f
JA
851 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
852}
853EXPORT_SYMBOL(blk_mq_stop_hw_queue);
854
280d45f6
CH
855void blk_mq_stop_hw_queues(struct request_queue *q)
856{
857 struct blk_mq_hw_ctx *hctx;
858 int i;
859
860 queue_for_each_hw_ctx(q, hctx, i)
861 blk_mq_stop_hw_queue(hctx);
862}
863EXPORT_SYMBOL(blk_mq_stop_hw_queues);
864
320ae51f
JA
865void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
866{
867 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf
JA
868
869 preempt_disable();
320ae51f 870 __blk_mq_run_hw_queue(hctx);
e4043dcf 871 preempt_enable();
320ae51f
JA
872}
873EXPORT_SYMBOL(blk_mq_start_hw_queue);
874
2f268556
CH
875void blk_mq_start_hw_queues(struct request_queue *q)
876{
877 struct blk_mq_hw_ctx *hctx;
878 int i;
879
880 queue_for_each_hw_ctx(q, hctx, i)
881 blk_mq_start_hw_queue(hctx);
882}
883EXPORT_SYMBOL(blk_mq_start_hw_queues);
884
885
1b4a3258 886void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
320ae51f
JA
887{
888 struct blk_mq_hw_ctx *hctx;
889 int i;
890
891 queue_for_each_hw_ctx(q, hctx, i) {
892 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
893 continue;
894
895 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
e4043dcf 896 preempt_disable();
1b4a3258 897 blk_mq_run_hw_queue(hctx, async);
e4043dcf 898 preempt_enable();
320ae51f
JA
899 }
900}
901EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
902
70f4db63 903static void blk_mq_run_work_fn(struct work_struct *work)
320ae51f
JA
904{
905 struct blk_mq_hw_ctx *hctx;
906
70f4db63 907 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
e4043dcf 908
320ae51f
JA
909 __blk_mq_run_hw_queue(hctx);
910}
911
70f4db63
CH
912static void blk_mq_delay_work_fn(struct work_struct *work)
913{
914 struct blk_mq_hw_ctx *hctx;
915
916 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
917
918 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
919 __blk_mq_run_hw_queue(hctx);
920}
921
922void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
923{
924 unsigned long tmo = msecs_to_jiffies(msecs);
925
926 if (hctx->queue->nr_hw_queues == 1)
927 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
928 else {
929 unsigned int cpu;
930
506e931f 931 cpu = blk_mq_hctx_next_cpu(hctx);
70f4db63
CH
932 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
933 }
934}
935EXPORT_SYMBOL(blk_mq_delay_queue);
936
320ae51f 937static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
72a0a36e 938 struct request *rq, bool at_head)
320ae51f
JA
939{
940 struct blk_mq_ctx *ctx = rq->mq_ctx;
941
01b983c9
JA
942 trace_block_rq_insert(hctx->queue, rq);
943
72a0a36e
CH
944 if (at_head)
945 list_add(&rq->queuelist, &ctx->rq_list);
946 else
947 list_add_tail(&rq->queuelist, &ctx->rq_list);
4bb659b1 948
320ae51f
JA
949 blk_mq_hctx_mark_pending(hctx, ctx);
950
951 /*
952 * We do this early, to ensure we are on the right CPU.
953 */
87ee7b11 954 blk_add_timer(rq);
320ae51f
JA
955}
956
eeabc850
CH
957void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
958 bool async)
320ae51f 959{
eeabc850 960 struct request_queue *q = rq->q;
320ae51f 961 struct blk_mq_hw_ctx *hctx;
eeabc850
CH
962 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
963
964 current_ctx = blk_mq_get_ctx(q);
965 if (!cpu_online(ctx->cpu))
966 rq->mq_ctx = ctx = current_ctx;
320ae51f 967
320ae51f
JA
968 hctx = q->mq_ops->map_queue(q, ctx->cpu);
969
eeabc850
CH
970 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
971 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
320ae51f
JA
972 blk_insert_flush(rq);
973 } else {
320ae51f 974 spin_lock(&ctx->lock);
72a0a36e 975 __blk_mq_insert_request(hctx, rq, at_head);
320ae51f 976 spin_unlock(&ctx->lock);
320ae51f
JA
977 }
978
320ae51f
JA
979 if (run_queue)
980 blk_mq_run_hw_queue(hctx, async);
e4043dcf
JA
981
982 blk_mq_put_ctx(current_ctx);
320ae51f
JA
983}
984
985static void blk_mq_insert_requests(struct request_queue *q,
986 struct blk_mq_ctx *ctx,
987 struct list_head *list,
988 int depth,
989 bool from_schedule)
990
991{
992 struct blk_mq_hw_ctx *hctx;
993 struct blk_mq_ctx *current_ctx;
994
995 trace_block_unplug(q, depth, !from_schedule);
996
997 current_ctx = blk_mq_get_ctx(q);
998
999 if (!cpu_online(ctx->cpu))
1000 ctx = current_ctx;
1001 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1002
1003 /*
1004 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1005 * offline now
1006 */
1007 spin_lock(&ctx->lock);
1008 while (!list_empty(list)) {
1009 struct request *rq;
1010
1011 rq = list_first_entry(list, struct request, queuelist);
1012 list_del_init(&rq->queuelist);
1013 rq->mq_ctx = ctx;
72a0a36e 1014 __blk_mq_insert_request(hctx, rq, false);
320ae51f
JA
1015 }
1016 spin_unlock(&ctx->lock);
1017
320ae51f 1018 blk_mq_run_hw_queue(hctx, from_schedule);
e4043dcf 1019 blk_mq_put_ctx(current_ctx);
320ae51f
JA
1020}
1021
1022static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1023{
1024 struct request *rqa = container_of(a, struct request, queuelist);
1025 struct request *rqb = container_of(b, struct request, queuelist);
1026
1027 return !(rqa->mq_ctx < rqb->mq_ctx ||
1028 (rqa->mq_ctx == rqb->mq_ctx &&
1029 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1030}
1031
1032void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1033{
1034 struct blk_mq_ctx *this_ctx;
1035 struct request_queue *this_q;
1036 struct request *rq;
1037 LIST_HEAD(list);
1038 LIST_HEAD(ctx_list);
1039 unsigned int depth;
1040
1041 list_splice_init(&plug->mq_list, &list);
1042
1043 list_sort(NULL, &list, plug_ctx_cmp);
1044
1045 this_q = NULL;
1046 this_ctx = NULL;
1047 depth = 0;
1048
1049 while (!list_empty(&list)) {
1050 rq = list_entry_rq(list.next);
1051 list_del_init(&rq->queuelist);
1052 BUG_ON(!rq->q);
1053 if (rq->mq_ctx != this_ctx) {
1054 if (this_ctx) {
1055 blk_mq_insert_requests(this_q, this_ctx,
1056 &ctx_list, depth,
1057 from_schedule);
1058 }
1059
1060 this_ctx = rq->mq_ctx;
1061 this_q = rq->q;
1062 depth = 0;
1063 }
1064
1065 depth++;
1066 list_add_tail(&rq->queuelist, &ctx_list);
1067 }
1068
1069 /*
1070 * If 'this_ctx' is set, we know we have entries to complete
1071 * on 'ctx_list'. Do those.
1072 */
1073 if (this_ctx) {
1074 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1075 from_schedule);
1076 }
1077}
1078
1079static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1080{
1081 init_request_from_bio(rq, bio);
1082 blk_account_io_start(rq, 1);
1083}
1084
07068d5b
JA
1085static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1086 struct blk_mq_ctx *ctx,
1087 struct request *rq, struct bio *bio)
320ae51f 1088{
07068d5b 1089 struct request_queue *q = hctx->queue;
320ae51f 1090
07068d5b
JA
1091 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1092 blk_mq_bio_to_request(rq, bio);
1093 spin_lock(&ctx->lock);
1094insert_rq:
1095 __blk_mq_insert_request(hctx, rq, false);
1096 spin_unlock(&ctx->lock);
1097 return false;
1098 } else {
1099 spin_lock(&ctx->lock);
1100 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1101 blk_mq_bio_to_request(rq, bio);
1102 goto insert_rq;
1103 }
320ae51f 1104
07068d5b
JA
1105 spin_unlock(&ctx->lock);
1106 __blk_mq_free_request(hctx, ctx, rq);
1107 return true;
14ec77f3 1108 }
07068d5b 1109}
14ec77f3 1110
07068d5b
JA
1111struct blk_map_ctx {
1112 struct blk_mq_hw_ctx *hctx;
1113 struct blk_mq_ctx *ctx;
1114};
1115
1116static struct request *blk_mq_map_request(struct request_queue *q,
1117 struct bio *bio,
1118 struct blk_map_ctx *data)
1119{
1120 struct blk_mq_hw_ctx *hctx;
1121 struct blk_mq_ctx *ctx;
1122 struct request *rq;
1123 int rw = bio_data_dir(bio);
320ae51f 1124
07068d5b 1125 if (unlikely(blk_mq_queue_enter(q))) {
320ae51f 1126 bio_endio(bio, -EIO);
07068d5b 1127 return NULL;
320ae51f
JA
1128 }
1129
1130 ctx = blk_mq_get_ctx(q);
1131 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1132
07068d5b 1133 if (rw_is_sync(bio->bi_rw))
27fbf4e8 1134 rw |= REQ_SYNC;
07068d5b 1135
320ae51f 1136 trace_block_getrq(q, bio, rw);
4bb659b1 1137 rq = __blk_mq_alloc_request(hctx, ctx, GFP_ATOMIC, false);
320ae51f 1138 if (likely(rq))
18741986 1139 blk_mq_rq_ctx_init(q, ctx, rq, rw);
320ae51f
JA
1140 else {
1141 blk_mq_put_ctx(ctx);
1142 trace_block_sleeprq(q, bio, rw);
18741986
CH
1143 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
1144 false);
320ae51f
JA
1145 ctx = rq->mq_ctx;
1146 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1147 }
1148
1149 hctx->queued++;
07068d5b
JA
1150 data->hctx = hctx;
1151 data->ctx = ctx;
1152 return rq;
1153}
1154
1155/*
1156 * Multiple hardware queue variant. This will not use per-process plugs,
1157 * but will attempt to bypass the hctx queueing if we can go straight to
1158 * hardware for SYNC IO.
1159 */
1160static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1161{
1162 const int is_sync = rw_is_sync(bio->bi_rw);
1163 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1164 struct blk_map_ctx data;
1165 struct request *rq;
1166
1167 blk_queue_bounce(q, &bio);
1168
1169 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1170 bio_endio(bio, -EIO);
1171 return;
1172 }
1173
1174 rq = blk_mq_map_request(q, bio, &data);
1175 if (unlikely(!rq))
1176 return;
1177
1178 if (unlikely(is_flush_fua)) {
1179 blk_mq_bio_to_request(rq, bio);
1180 blk_insert_flush(rq);
1181 goto run_queue;
1182 }
1183
1184 if (is_sync) {
1185 int ret;
1186
1187 blk_mq_bio_to_request(rq, bio);
1188 blk_mq_start_request(rq, true);
1189
1190 /*
1191 * For OK queue, we are done. For error, kill it. Any other
1192 * error (busy), just add it to our list as we previously
1193 * would have done
1194 */
1195 ret = q->mq_ops->queue_rq(data.hctx, rq);
1196 if (ret == BLK_MQ_RQ_QUEUE_OK)
1197 goto done;
1198 else {
1199 __blk_mq_requeue_request(rq);
1200
1201 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1202 rq->errors = -EIO;
1203 blk_mq_end_io(rq, rq->errors);
1204 goto done;
1205 }
1206 }
1207 }
1208
1209 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1210 /*
1211 * For a SYNC request, send it to the hardware immediately. For
1212 * an ASYNC request, just ensure that we run it later on. The
1213 * latter allows for merging opportunities and more efficient
1214 * dispatching.
1215 */
1216run_queue:
1217 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1218 }
1219done:
1220 blk_mq_put_ctx(data.ctx);
1221}
1222
1223/*
1224 * Single hardware queue variant. This will attempt to use any per-process
1225 * plug for merging and IO deferral.
1226 */
1227static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1228{
1229 const int is_sync = rw_is_sync(bio->bi_rw);
1230 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1231 unsigned int use_plug, request_count = 0;
1232 struct blk_map_ctx data;
1233 struct request *rq;
1234
1235 /*
1236 * If we have multiple hardware queues, just go directly to
1237 * one of those for sync IO.
1238 */
1239 use_plug = !is_flush_fua && !is_sync;
1240
1241 blk_queue_bounce(q, &bio);
1242
1243 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1244 bio_endio(bio, -EIO);
1245 return;
1246 }
1247
1248 if (use_plug && !blk_queue_nomerges(q) &&
1249 blk_attempt_plug_merge(q, bio, &request_count))
1250 return;
1251
1252 rq = blk_mq_map_request(q, bio, &data);
320ae51f
JA
1253
1254 if (unlikely(is_flush_fua)) {
1255 blk_mq_bio_to_request(rq, bio);
320ae51f
JA
1256 blk_insert_flush(rq);
1257 goto run_queue;
1258 }
1259
1260 /*
1261 * A task plug currently exists. Since this is completely lockless,
1262 * utilize that to temporarily store requests until the task is
1263 * either done or scheduled away.
1264 */
1265 if (use_plug) {
1266 struct blk_plug *plug = current->plug;
1267
1268 if (plug) {
1269 blk_mq_bio_to_request(rq, bio);
92f399c7 1270 if (list_empty(&plug->mq_list))
320ae51f
JA
1271 trace_block_plug(q);
1272 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1273 blk_flush_plug_list(plug, false);
1274 trace_block_plug(q);
1275 }
1276 list_add_tail(&rq->queuelist, &plug->mq_list);
07068d5b 1277 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1278 return;
1279 }
1280 }
1281
07068d5b
JA
1282 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1283 /*
1284 * For a SYNC request, send it to the hardware immediately. For
1285 * an ASYNC request, just ensure that we run it later on. The
1286 * latter allows for merging opportunities and more efficient
1287 * dispatching.
1288 */
1289run_queue:
1290 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
320ae51f
JA
1291 }
1292
07068d5b 1293 blk_mq_put_ctx(data.ctx);
320ae51f
JA
1294}
1295
1296/*
1297 * Default mapping to a software queue, since we use one per CPU.
1298 */
1299struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1300{
1301 return q->queue_hw_ctx[q->mq_map[cpu]];
1302}
1303EXPORT_SYMBOL(blk_mq_map_queue);
1304
24d2f903 1305struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
f14bbe77
JA
1306 unsigned int hctx_index,
1307 int node)
320ae51f 1308{
f14bbe77 1309 return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL, node);
320ae51f
JA
1310}
1311EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1312
1313void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1314 unsigned int hctx_index)
1315{
1316 kfree(hctx);
1317}
1318EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1319
24d2f903
CH
1320static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1321 struct blk_mq_tags *tags, unsigned int hctx_idx)
95363efd 1322{
e9b267d9 1323 struct page *page;
320ae51f 1324
24d2f903 1325 if (tags->rqs && set->ops->exit_request) {
e9b267d9 1326 int i;
320ae51f 1327
24d2f903
CH
1328 for (i = 0; i < tags->nr_tags; i++) {
1329 if (!tags->rqs[i])
e9b267d9 1330 continue;
24d2f903
CH
1331 set->ops->exit_request(set->driver_data, tags->rqs[i],
1332 hctx_idx, i);
e9b267d9 1333 }
320ae51f 1334 }
320ae51f 1335
24d2f903
CH
1336 while (!list_empty(&tags->page_list)) {
1337 page = list_first_entry(&tags->page_list, struct page, lru);
6753471c 1338 list_del_init(&page->lru);
320ae51f
JA
1339 __free_pages(page, page->private);
1340 }
1341
24d2f903 1342 kfree(tags->rqs);
320ae51f 1343
24d2f903 1344 blk_mq_free_tags(tags);
320ae51f
JA
1345}
1346
1347static size_t order_to_size(unsigned int order)
1348{
4ca08500 1349 return (size_t)PAGE_SIZE << order;
320ae51f
JA
1350}
1351
24d2f903
CH
1352static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1353 unsigned int hctx_idx)
320ae51f 1354{
24d2f903 1355 struct blk_mq_tags *tags;
320ae51f
JA
1356 unsigned int i, j, entries_per_page, max_order = 4;
1357 size_t rq_size, left;
1358
24d2f903
CH
1359 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1360 set->numa_node);
1361 if (!tags)
1362 return NULL;
320ae51f 1363
24d2f903
CH
1364 INIT_LIST_HEAD(&tags->page_list);
1365
1366 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1367 GFP_KERNEL, set->numa_node);
1368 if (!tags->rqs) {
1369 blk_mq_free_tags(tags);
1370 return NULL;
1371 }
320ae51f
JA
1372
1373 /*
1374 * rq_size is the size of the request plus driver payload, rounded
1375 * to the cacheline size
1376 */
24d2f903 1377 rq_size = round_up(sizeof(struct request) + set->cmd_size,
320ae51f 1378 cache_line_size());
24d2f903 1379 left = rq_size * set->queue_depth;
320ae51f 1380
24d2f903 1381 for (i = 0; i < set->queue_depth; ) {
320ae51f
JA
1382 int this_order = max_order;
1383 struct page *page;
1384 int to_do;
1385 void *p;
1386
1387 while (left < order_to_size(this_order - 1) && this_order)
1388 this_order--;
1389
1390 do {
24d2f903
CH
1391 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1392 this_order);
320ae51f
JA
1393 if (page)
1394 break;
1395 if (!this_order--)
1396 break;
1397 if (order_to_size(this_order) < rq_size)
1398 break;
1399 } while (1);
1400
1401 if (!page)
24d2f903 1402 goto fail;
320ae51f
JA
1403
1404 page->private = this_order;
24d2f903 1405 list_add_tail(&page->lru, &tags->page_list);
320ae51f
JA
1406
1407 p = page_address(page);
1408 entries_per_page = order_to_size(this_order) / rq_size;
24d2f903 1409 to_do = min(entries_per_page, set->queue_depth - i);
320ae51f
JA
1410 left -= to_do * rq_size;
1411 for (j = 0; j < to_do; j++) {
24d2f903
CH
1412 tags->rqs[i] = p;
1413 if (set->ops->init_request) {
1414 if (set->ops->init_request(set->driver_data,
1415 tags->rqs[i], hctx_idx, i,
1416 set->numa_node))
1417 goto fail;
e9b267d9
CH
1418 }
1419
320ae51f
JA
1420 p += rq_size;
1421 i++;
1422 }
1423 }
1424
24d2f903 1425 return tags;
320ae51f 1426
24d2f903
CH
1427fail:
1428 pr_warn("%s: failed to allocate requests\n", __func__);
1429 blk_mq_free_rq_map(set, tags, hctx_idx);
1430 return NULL;
320ae51f
JA
1431}
1432
1429d7c9
JA
1433static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1434{
1435 kfree(bitmap->map);
1436}
1437
1438static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1439{
1440 unsigned int bpw = 8, total, num_maps, i;
1441
1442 bitmap->bits_per_word = bpw;
1443
1444 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1445 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1446 GFP_KERNEL, node);
1447 if (!bitmap->map)
1448 return -ENOMEM;
1449
1450 bitmap->map_size = num_maps;
1451
1452 total = nr_cpu_ids;
1453 for (i = 0; i < num_maps; i++) {
1454 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1455 total -= bitmap->map[i].depth;
1456 }
1457
1458 return 0;
1459}
1460
484b4061
JA
1461static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1462{
1463 struct request_queue *q = hctx->queue;
1464 struct blk_mq_ctx *ctx;
1465 LIST_HEAD(tmp);
1466
1467 /*
1468 * Move ctx entries to new CPU, if this one is going away.
1469 */
1470 ctx = __blk_mq_get_ctx(q, cpu);
1471
1472 spin_lock(&ctx->lock);
1473 if (!list_empty(&ctx->rq_list)) {
1474 list_splice_init(&ctx->rq_list, &tmp);
1475 blk_mq_hctx_clear_pending(hctx, ctx);
1476 }
1477 spin_unlock(&ctx->lock);
1478
1479 if (list_empty(&tmp))
1480 return NOTIFY_OK;
1481
1482 ctx = blk_mq_get_ctx(q);
1483 spin_lock(&ctx->lock);
1484
1485 while (!list_empty(&tmp)) {
1486 struct request *rq;
1487
1488 rq = list_first_entry(&tmp, struct request, queuelist);
1489 rq->mq_ctx = ctx;
1490 list_move_tail(&rq->queuelist, &ctx->rq_list);
1491 }
1492
1493 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1494 blk_mq_hctx_mark_pending(hctx, ctx);
1495
1496 spin_unlock(&ctx->lock);
1497
1498 blk_mq_run_hw_queue(hctx, true);
1499 blk_mq_put_ctx(ctx);
1500 return NOTIFY_OK;
1501}
1502
1503static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1504{
1505 struct request_queue *q = hctx->queue;
1506 struct blk_mq_tag_set *set = q->tag_set;
1507
1508 if (set->tags[hctx->queue_num])
1509 return NOTIFY_OK;
1510
1511 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1512 if (!set->tags[hctx->queue_num])
1513 return NOTIFY_STOP;
1514
1515 hctx->tags = set->tags[hctx->queue_num];
1516 return NOTIFY_OK;
1517}
1518
1519static int blk_mq_hctx_notify(void *data, unsigned long action,
1520 unsigned int cpu)
1521{
1522 struct blk_mq_hw_ctx *hctx = data;
1523
1524 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1525 return blk_mq_hctx_cpu_offline(hctx, cpu);
1526 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1527 return blk_mq_hctx_cpu_online(hctx, cpu);
1528
1529 return NOTIFY_OK;
1530}
1531
624dbe47
ML
1532static void blk_mq_exit_hw_queues(struct request_queue *q,
1533 struct blk_mq_tag_set *set, int nr_queue)
1534{
1535 struct blk_mq_hw_ctx *hctx;
1536 unsigned int i;
1537
1538 queue_for_each_hw_ctx(q, hctx, i) {
1539 if (i == nr_queue)
1540 break;
1541
1542 if (set->ops->exit_hctx)
1543 set->ops->exit_hctx(hctx, i);
1544
1545 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1546 kfree(hctx->ctxs);
1547 blk_mq_free_bitmap(&hctx->ctx_map);
1548 }
1549
1550}
1551
1552static void blk_mq_free_hw_queues(struct request_queue *q,
1553 struct blk_mq_tag_set *set)
1554{
1555 struct blk_mq_hw_ctx *hctx;
1556 unsigned int i;
1557
1558 queue_for_each_hw_ctx(q, hctx, i) {
1559 free_cpumask_var(hctx->cpumask);
1560 set->ops->free_hctx(hctx, i);
1561 }
1562}
1563
320ae51f 1564static int blk_mq_init_hw_queues(struct request_queue *q,
24d2f903 1565 struct blk_mq_tag_set *set)
320ae51f
JA
1566{
1567 struct blk_mq_hw_ctx *hctx;
624dbe47 1568 unsigned int i;
320ae51f
JA
1569
1570 /*
1571 * Initialize hardware queues
1572 */
1573 queue_for_each_hw_ctx(q, hctx, i) {
320ae51f
JA
1574 int node;
1575
1576 node = hctx->numa_node;
1577 if (node == NUMA_NO_NODE)
24d2f903 1578 node = hctx->numa_node = set->numa_node;
320ae51f 1579
70f4db63
CH
1580 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1581 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
320ae51f
JA
1582 spin_lock_init(&hctx->lock);
1583 INIT_LIST_HEAD(&hctx->dispatch);
1584 hctx->queue = q;
1585 hctx->queue_num = i;
24d2f903
CH
1586 hctx->flags = set->flags;
1587 hctx->cmd_size = set->cmd_size;
320ae51f
JA
1588
1589 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1590 blk_mq_hctx_notify, hctx);
1591 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1592
24d2f903 1593 hctx->tags = set->tags[i];
320ae51f
JA
1594
1595 /*
1596 * Allocate space for all possible cpus to avoid allocation in
1597 * runtime
1598 */
1599 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1600 GFP_KERNEL, node);
1601 if (!hctx->ctxs)
1602 break;
1603
1429d7c9 1604 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
320ae51f
JA
1605 break;
1606
320ae51f
JA
1607 hctx->nr_ctx = 0;
1608
24d2f903
CH
1609 if (set->ops->init_hctx &&
1610 set->ops->init_hctx(hctx, set->driver_data, i))
320ae51f
JA
1611 break;
1612 }
1613
1614 if (i == q->nr_hw_queues)
1615 return 0;
1616
1617 /*
1618 * Init failed
1619 */
624dbe47 1620 blk_mq_exit_hw_queues(q, set, i);
320ae51f
JA
1621
1622 return 1;
1623}
1624
1625static void blk_mq_init_cpu_queues(struct request_queue *q,
1626 unsigned int nr_hw_queues)
1627{
1628 unsigned int i;
1629
1630 for_each_possible_cpu(i) {
1631 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1632 struct blk_mq_hw_ctx *hctx;
1633
1634 memset(__ctx, 0, sizeof(*__ctx));
1635 __ctx->cpu = i;
1636 spin_lock_init(&__ctx->lock);
1637 INIT_LIST_HEAD(&__ctx->rq_list);
1638 __ctx->queue = q;
1639
1640 /* If the cpu isn't online, the cpu is mapped to first hctx */
320ae51f
JA
1641 if (!cpu_online(i))
1642 continue;
1643
e4043dcf
JA
1644 hctx = q->mq_ops->map_queue(q, i);
1645 cpumask_set_cpu(i, hctx->cpumask);
1646 hctx->nr_ctx++;
1647
320ae51f
JA
1648 /*
1649 * Set local node, IFF we have more than one hw queue. If
1650 * not, we remain on the home node of the device
1651 */
1652 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1653 hctx->numa_node = cpu_to_node(i);
1654 }
1655}
1656
1657static void blk_mq_map_swqueue(struct request_queue *q)
1658{
1659 unsigned int i;
1660 struct blk_mq_hw_ctx *hctx;
1661 struct blk_mq_ctx *ctx;
1662
1663 queue_for_each_hw_ctx(q, hctx, i) {
e4043dcf 1664 cpumask_clear(hctx->cpumask);
320ae51f
JA
1665 hctx->nr_ctx = 0;
1666 }
1667
1668 /*
1669 * Map software to hardware queues
1670 */
1671 queue_for_each_ctx(q, ctx, i) {
1672 /* If the cpu isn't online, the cpu is mapped to first hctx */
e4043dcf
JA
1673 if (!cpu_online(i))
1674 continue;
1675
320ae51f 1676 hctx = q->mq_ops->map_queue(q, i);
e4043dcf 1677 cpumask_set_cpu(i, hctx->cpumask);
320ae51f
JA
1678 ctx->index_hw = hctx->nr_ctx;
1679 hctx->ctxs[hctx->nr_ctx++] = ctx;
1680 }
506e931f
JA
1681
1682 queue_for_each_hw_ctx(q, hctx, i) {
484b4061
JA
1683 /*
1684 * If not software queues are mapped to this hardware queue,
1685 * disable it and free the request entries
1686 */
1687 if (!hctx->nr_ctx) {
1688 struct blk_mq_tag_set *set = q->tag_set;
1689
1690 if (set->tags[i]) {
1691 blk_mq_free_rq_map(set, set->tags[i], i);
1692 set->tags[i] = NULL;
1693 hctx->tags = NULL;
1694 }
1695 continue;
1696 }
1697
1698 /*
1699 * Initialize batch roundrobin counts
1700 */
506e931f
JA
1701 hctx->next_cpu = cpumask_first(hctx->cpumask);
1702 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1703 }
320ae51f
JA
1704}
1705
0d2602ca
JA
1706static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1707{
1708 struct blk_mq_hw_ctx *hctx;
1709 struct request_queue *q;
1710 bool shared;
1711 int i;
1712
1713 if (set->tag_list.next == set->tag_list.prev)
1714 shared = false;
1715 else
1716 shared = true;
1717
1718 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1719 blk_mq_freeze_queue(q);
1720
1721 queue_for_each_hw_ctx(q, hctx, i) {
1722 if (shared)
1723 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1724 else
1725 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1726 }
1727 blk_mq_unfreeze_queue(q);
1728 }
1729}
1730
1731static void blk_mq_del_queue_tag_set(struct request_queue *q)
1732{
1733 struct blk_mq_tag_set *set = q->tag_set;
1734
1735 blk_mq_freeze_queue(q);
1736
1737 mutex_lock(&set->tag_list_lock);
1738 list_del_init(&q->tag_set_list);
1739 blk_mq_update_tag_set_depth(set);
1740 mutex_unlock(&set->tag_list_lock);
1741
1742 blk_mq_unfreeze_queue(q);
1743}
1744
1745static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1746 struct request_queue *q)
1747{
1748 q->tag_set = set;
1749
1750 mutex_lock(&set->tag_list_lock);
1751 list_add_tail(&q->tag_set_list, &set->tag_list);
1752 blk_mq_update_tag_set_depth(set);
1753 mutex_unlock(&set->tag_list_lock);
1754}
1755
24d2f903 1756struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
320ae51f
JA
1757{
1758 struct blk_mq_hw_ctx **hctxs;
1759 struct blk_mq_ctx *ctx;
1760 struct request_queue *q;
f14bbe77 1761 unsigned int *map;
320ae51f
JA
1762 int i;
1763
320ae51f
JA
1764 ctx = alloc_percpu(struct blk_mq_ctx);
1765 if (!ctx)
1766 return ERR_PTR(-ENOMEM);
1767
24d2f903
CH
1768 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1769 set->numa_node);
320ae51f
JA
1770
1771 if (!hctxs)
1772 goto err_percpu;
1773
f14bbe77
JA
1774 map = blk_mq_make_queue_map(set);
1775 if (!map)
1776 goto err_map;
1777
24d2f903 1778 for (i = 0; i < set->nr_hw_queues; i++) {
f14bbe77
JA
1779 int node = blk_mq_hw_queue_to_node(map, i);
1780
1781 hctxs[i] = set->ops->alloc_hctx(set, i, node);
320ae51f
JA
1782 if (!hctxs[i])
1783 goto err_hctxs;
1784
e4043dcf
JA
1785 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1786 goto err_hctxs;
1787
0d2602ca 1788 atomic_set(&hctxs[i]->nr_active, 0);
f14bbe77 1789 hctxs[i]->numa_node = node;
320ae51f
JA
1790 hctxs[i]->queue_num = i;
1791 }
1792
24d2f903 1793 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
320ae51f
JA
1794 if (!q)
1795 goto err_hctxs;
1796
3d2936f4
ML
1797 if (percpu_counter_init(&q->mq_usage_counter, 0))
1798 goto err_map;
1799
320ae51f
JA
1800 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1801 blk_queue_rq_timeout(q, 30000);
1802
1803 q->nr_queues = nr_cpu_ids;
24d2f903 1804 q->nr_hw_queues = set->nr_hw_queues;
f14bbe77 1805 q->mq_map = map;
320ae51f
JA
1806
1807 q->queue_ctx = ctx;
1808 q->queue_hw_ctx = hctxs;
1809
24d2f903 1810 q->mq_ops = set->ops;
94eddfbe 1811 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
320ae51f 1812
1be036e9
CH
1813 q->sg_reserved_size = INT_MAX;
1814
07068d5b
JA
1815 if (q->nr_hw_queues > 1)
1816 blk_queue_make_request(q, blk_mq_make_request);
1817 else
1818 blk_queue_make_request(q, blk_sq_make_request);
1819
87ee7b11 1820 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
24d2f903
CH
1821 if (set->timeout)
1822 blk_queue_rq_timeout(q, set->timeout);
320ae51f 1823
eba71768
JA
1824 /*
1825 * Do this after blk_queue_make_request() overrides it...
1826 */
1827 q->nr_requests = set->queue_depth;
1828
24d2f903
CH
1829 if (set->ops->complete)
1830 blk_queue_softirq_done(q, set->ops->complete);
30a91cb4 1831
320ae51f 1832 blk_mq_init_flush(q);
24d2f903 1833 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
320ae51f 1834
24d2f903
CH
1835 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1836 set->cmd_size, cache_line_size()),
1837 GFP_KERNEL);
18741986 1838 if (!q->flush_rq)
320ae51f
JA
1839 goto err_hw;
1840
24d2f903 1841 if (blk_mq_init_hw_queues(q, set))
18741986
CH
1842 goto err_flush_rq;
1843
320ae51f
JA
1844 mutex_lock(&all_q_mutex);
1845 list_add_tail(&q->all_q_node, &all_q_list);
1846 mutex_unlock(&all_q_mutex);
1847
0d2602ca
JA
1848 blk_mq_add_queue_tag_set(set, q);
1849
484b4061
JA
1850 blk_mq_map_swqueue(q);
1851
320ae51f 1852 return q;
18741986
CH
1853
1854err_flush_rq:
1855 kfree(q->flush_rq);
320ae51f 1856err_hw:
320ae51f
JA
1857 blk_cleanup_queue(q);
1858err_hctxs:
f14bbe77 1859 kfree(map);
24d2f903 1860 for (i = 0; i < set->nr_hw_queues; i++) {
320ae51f
JA
1861 if (!hctxs[i])
1862 break;
e4043dcf 1863 free_cpumask_var(hctxs[i]->cpumask);
24d2f903 1864 set->ops->free_hctx(hctxs[i], i);
320ae51f 1865 }
f14bbe77 1866err_map:
320ae51f
JA
1867 kfree(hctxs);
1868err_percpu:
1869 free_percpu(ctx);
1870 return ERR_PTR(-ENOMEM);
1871}
1872EXPORT_SYMBOL(blk_mq_init_queue);
1873
1874void blk_mq_free_queue(struct request_queue *q)
1875{
624dbe47 1876 struct blk_mq_tag_set *set = q->tag_set;
320ae51f 1877
0d2602ca
JA
1878 blk_mq_del_queue_tag_set(q);
1879
624dbe47
ML
1880 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1881 blk_mq_free_hw_queues(q, set);
320ae51f 1882
3d2936f4
ML
1883 percpu_counter_destroy(&q->mq_usage_counter);
1884
320ae51f
JA
1885 free_percpu(q->queue_ctx);
1886 kfree(q->queue_hw_ctx);
1887 kfree(q->mq_map);
1888
1889 q->queue_ctx = NULL;
1890 q->queue_hw_ctx = NULL;
1891 q->mq_map = NULL;
1892
1893 mutex_lock(&all_q_mutex);
1894 list_del_init(&q->all_q_node);
1895 mutex_unlock(&all_q_mutex);
1896}
320ae51f
JA
1897
1898/* Basically redo blk_mq_init_queue with queue frozen */
f618ef7c 1899static void blk_mq_queue_reinit(struct request_queue *q)
320ae51f
JA
1900{
1901 blk_mq_freeze_queue(q);
1902
1903 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1904
1905 /*
1906 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1907 * we should change hctx numa_node according to new topology (this
1908 * involves free and re-allocate memory, worthy doing?)
1909 */
1910
1911 blk_mq_map_swqueue(q);
1912
1913 blk_mq_unfreeze_queue(q);
1914}
1915
f618ef7c
PG
1916static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1917 unsigned long action, void *hcpu)
320ae51f
JA
1918{
1919 struct request_queue *q;
1920
1921 /*
9fccfed8
JA
1922 * Before new mappings are established, hotadded cpu might already
1923 * start handling requests. This doesn't break anything as we map
1924 * offline CPUs to first hardware queue. We will re-init the queue
1925 * below to get optimal settings.
320ae51f
JA
1926 */
1927 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1928 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1929 return NOTIFY_OK;
1930
1931 mutex_lock(&all_q_mutex);
1932 list_for_each_entry(q, &all_q_list, all_q_node)
1933 blk_mq_queue_reinit(q);
1934 mutex_unlock(&all_q_mutex);
1935 return NOTIFY_OK;
1936}
1937
24d2f903
CH
1938int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1939{
1940 int i;
1941
1942 if (!set->nr_hw_queues)
1943 return -EINVAL;
1944 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1945 return -EINVAL;
1946 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1947 return -EINVAL;
1948
1949 if (!set->nr_hw_queues ||
1950 !set->ops->queue_rq || !set->ops->map_queue ||
1951 !set->ops->alloc_hctx || !set->ops->free_hctx)
1952 return -EINVAL;
1953
1954
48479005
ML
1955 set->tags = kmalloc_node(set->nr_hw_queues *
1956 sizeof(struct blk_mq_tags *),
24d2f903
CH
1957 GFP_KERNEL, set->numa_node);
1958 if (!set->tags)
1959 goto out;
1960
1961 for (i = 0; i < set->nr_hw_queues; i++) {
1962 set->tags[i] = blk_mq_init_rq_map(set, i);
1963 if (!set->tags[i])
1964 goto out_unwind;
1965 }
1966
0d2602ca
JA
1967 mutex_init(&set->tag_list_lock);
1968 INIT_LIST_HEAD(&set->tag_list);
1969
24d2f903
CH
1970 return 0;
1971
1972out_unwind:
1973 while (--i >= 0)
1974 blk_mq_free_rq_map(set, set->tags[i], i);
1975out:
1976 return -ENOMEM;
1977}
1978EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1979
1980void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1981{
1982 int i;
1983
484b4061
JA
1984 for (i = 0; i < set->nr_hw_queues; i++) {
1985 if (set->tags[i])
1986 blk_mq_free_rq_map(set, set->tags[i], i);
1987 }
1988
981bd189 1989 kfree(set->tags);
24d2f903
CH
1990}
1991EXPORT_SYMBOL(blk_mq_free_tag_set);
1992
e3a2b3f9
JA
1993int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
1994{
1995 struct blk_mq_tag_set *set = q->tag_set;
1996 struct blk_mq_hw_ctx *hctx;
1997 int i, ret;
1998
1999 if (!set || nr > set->queue_depth)
2000 return -EINVAL;
2001
2002 ret = 0;
2003 queue_for_each_hw_ctx(q, hctx, i) {
2004 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2005 if (ret)
2006 break;
2007 }
2008
2009 if (!ret)
2010 q->nr_requests = nr;
2011
2012 return ret;
2013}
2014
676141e4
JA
2015void blk_mq_disable_hotplug(void)
2016{
2017 mutex_lock(&all_q_mutex);
2018}
2019
2020void blk_mq_enable_hotplug(void)
2021{
2022 mutex_unlock(&all_q_mutex);
2023}
2024
320ae51f
JA
2025static int __init blk_mq_init(void)
2026{
320ae51f
JA
2027 blk_mq_cpu_init();
2028
2029 /* Must be called after percpu_counter_hotcpu_callback() */
2030 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2031
2032 return 0;
2033}
2034subsys_initcall(blk_mq_init);