]> Git Repo - linux.git/blob - block/blk-mq.c
blk-mq: refactor request insertion/merging
[linux.git] / block / blk-mq.c
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
25 static DEFINE_MUTEX(all_q_mutex);
26 static LIST_HEAD(all_q_list);
27
28 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
29
30 static 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  */
42 static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
43 {
44         return __blk_mq_get_ctx(q, get_cpu());
45 }
46
47 static 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  */
55 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
56 {
57         unsigned int i;
58
59         for (i = 0; i < hctx->nr_ctx_map; i++)
60                 if (hctx->ctx_map[i])
61                         return true;
62
63         return false;
64 }
65
66 /*
67  * Mark this ctx as having pending work in this hardware queue
68  */
69 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70                                      struct blk_mq_ctx *ctx)
71 {
72         if (!test_bit(ctx->index_hw, hctx->ctx_map))
73                 set_bit(ctx->index_hw, hctx->ctx_map);
74 }
75
76 static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77                                               gfp_t gfp, bool reserved)
78 {
79         struct request *rq;
80         unsigned int tag;
81
82         tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83         if (tag != BLK_MQ_TAG_FAIL) {
84                 rq = hctx->tags->rqs[tag];
85                 blk_rq_init(hctx->queue, rq);
86                 rq->tag = tag;
87
88                 return rq;
89         }
90
91         return NULL;
92 }
93
94 static int blk_mq_queue_enter(struct request_queue *q)
95 {
96         int ret;
97
98         __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
99         smp_wmb();
100         /* we have problems to freeze the queue if it's initializing */
101         if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
102                 return 0;
103
104         __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
105
106         spin_lock_irq(q->queue_lock);
107         ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
108                 !blk_queue_bypass(q) || blk_queue_dying(q),
109                 *q->queue_lock);
110         /* inc usage with lock hold to avoid freeze_queue runs here */
111         if (!ret && !blk_queue_dying(q))
112                 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
113         else if (blk_queue_dying(q))
114                 ret = -ENODEV;
115         spin_unlock_irq(q->queue_lock);
116
117         return ret;
118 }
119
120 static void blk_mq_queue_exit(struct request_queue *q)
121 {
122         __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
123 }
124
125 static void __blk_mq_drain_queue(struct request_queue *q)
126 {
127         while (true) {
128                 s64 count;
129
130                 spin_lock_irq(q->queue_lock);
131                 count = percpu_counter_sum(&q->mq_usage_counter);
132                 spin_unlock_irq(q->queue_lock);
133
134                 if (count == 0)
135                         break;
136                 blk_mq_run_queues(q, false);
137                 msleep(10);
138         }
139 }
140
141 /*
142  * Guarantee no request is in use, so we can change any data structure of
143  * the queue afterward.
144  */
145 static void blk_mq_freeze_queue(struct request_queue *q)
146 {
147         bool drain;
148
149         spin_lock_irq(q->queue_lock);
150         drain = !q->bypass_depth++;
151         queue_flag_set(QUEUE_FLAG_BYPASS, q);
152         spin_unlock_irq(q->queue_lock);
153
154         if (drain)
155                 __blk_mq_drain_queue(q);
156 }
157
158 void blk_mq_drain_queue(struct request_queue *q)
159 {
160         __blk_mq_drain_queue(q);
161 }
162
163 static void blk_mq_unfreeze_queue(struct request_queue *q)
164 {
165         bool wake = false;
166
167         spin_lock_irq(q->queue_lock);
168         if (!--q->bypass_depth) {
169                 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
170                 wake = true;
171         }
172         WARN_ON_ONCE(q->bypass_depth < 0);
173         spin_unlock_irq(q->queue_lock);
174         if (wake)
175                 wake_up_all(&q->mq_freeze_wq);
176 }
177
178 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
179 {
180         return blk_mq_has_free_tags(hctx->tags);
181 }
182 EXPORT_SYMBOL(blk_mq_can_queue);
183
184 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
185                                struct request *rq, unsigned int rw_flags)
186 {
187         if (blk_queue_io_stat(q))
188                 rw_flags |= REQ_IO_STAT;
189
190         rq->mq_ctx = ctx;
191         rq->cmd_flags = rw_flags;
192         rq->start_time = jiffies;
193         set_start_time_ns(rq);
194         ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195 }
196
197 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
198                                                    int rw, gfp_t gfp,
199                                                    bool reserved)
200 {
201         struct request *rq;
202
203         do {
204                 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
205                 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
206
207                 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
208                 if (rq) {
209                         blk_mq_rq_ctx_init(q, ctx, rq, rw);
210                         break;
211                 }
212
213                 if (gfp & __GFP_WAIT) {
214                         __blk_mq_run_hw_queue(hctx);
215                         blk_mq_put_ctx(ctx);
216                 } else {
217                         blk_mq_put_ctx(ctx);
218                         break;
219                 }
220
221                 blk_mq_wait_for_tags(hctx->tags, reserved);
222         } while (1);
223
224         return rq;
225 }
226
227 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
228 {
229         struct request *rq;
230
231         if (blk_mq_queue_enter(q))
232                 return NULL;
233
234         rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
235         if (rq)
236                 blk_mq_put_ctx(rq->mq_ctx);
237         return rq;
238 }
239
240 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
241                                               gfp_t gfp)
242 {
243         struct request *rq;
244
245         if (blk_mq_queue_enter(q))
246                 return NULL;
247
248         rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
249         if (rq)
250                 blk_mq_put_ctx(rq->mq_ctx);
251         return rq;
252 }
253 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
254
255 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
256                                   struct blk_mq_ctx *ctx, struct request *rq)
257 {
258         const int tag = rq->tag;
259         struct request_queue *q = rq->q;
260
261         blk_mq_put_tag(hctx->tags, tag);
262         blk_mq_queue_exit(q);
263 }
264
265 void blk_mq_free_request(struct request *rq)
266 {
267         struct blk_mq_ctx *ctx = rq->mq_ctx;
268         struct blk_mq_hw_ctx *hctx;
269         struct request_queue *q = rq->q;
270
271         ctx->rq_completed[rq_is_sync(rq)]++;
272
273         hctx = q->mq_ops->map_queue(q, ctx->cpu);
274         __blk_mq_free_request(hctx, ctx, rq);
275 }
276
277 /*
278  * Clone all relevant state from a request that has been put on hold in
279  * the flush state machine into the preallocated flush request that hangs
280  * off the request queue.
281  *
282  * For a driver the flush request should be invisible, that's why we are
283  * impersonating the original request here.
284  */
285 void blk_mq_clone_flush_request(struct request *flush_rq,
286                 struct request *orig_rq)
287 {
288         struct blk_mq_hw_ctx *hctx =
289                 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
290
291         flush_rq->mq_ctx = orig_rq->mq_ctx;
292         flush_rq->tag = orig_rq->tag;
293         memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
294                 hctx->cmd_size);
295 }
296
297 inline void __blk_mq_end_io(struct request *rq, int error)
298 {
299         blk_account_io_done(rq);
300
301         if (rq->end_io) {
302                 rq->end_io(rq, error);
303         } else {
304                 if (unlikely(blk_bidi_rq(rq)))
305                         blk_mq_free_request(rq->next_rq);
306                 blk_mq_free_request(rq);
307         }
308 }
309 EXPORT_SYMBOL(__blk_mq_end_io);
310
311 void blk_mq_end_io(struct request *rq, int error)
312 {
313         if (blk_update_request(rq, error, blk_rq_bytes(rq)))
314                 BUG();
315         __blk_mq_end_io(rq, error);
316 }
317 EXPORT_SYMBOL(blk_mq_end_io);
318
319 static void __blk_mq_complete_request_remote(void *data)
320 {
321         struct request *rq = data;
322
323         rq->q->softirq_done_fn(rq);
324 }
325
326 void __blk_mq_complete_request(struct request *rq)
327 {
328         struct blk_mq_ctx *ctx = rq->mq_ctx;
329         bool shared = false;
330         int cpu;
331
332         if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
333                 rq->q->softirq_done_fn(rq);
334                 return;
335         }
336
337         cpu = get_cpu();
338         if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
339                 shared = cpus_share_cache(cpu, ctx->cpu);
340
341         if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
342                 rq->csd.func = __blk_mq_complete_request_remote;
343                 rq->csd.info = rq;
344                 rq->csd.flags = 0;
345                 smp_call_function_single_async(ctx->cpu, &rq->csd);
346         } else {
347                 rq->q->softirq_done_fn(rq);
348         }
349         put_cpu();
350 }
351
352 /**
353  * blk_mq_complete_request - end I/O on a request
354  * @rq:         the request being processed
355  *
356  * Description:
357  *      Ends all I/O on a request. It does not handle partial completions.
358  *      The actual completion happens out-of-order, through a IPI handler.
359  **/
360 void blk_mq_complete_request(struct request *rq)
361 {
362         if (unlikely(blk_should_fake_timeout(rq->q)))
363                 return;
364         if (!blk_mark_rq_complete(rq))
365                 __blk_mq_complete_request(rq);
366 }
367 EXPORT_SYMBOL(blk_mq_complete_request);
368
369 static void blk_mq_start_request(struct request *rq, bool last)
370 {
371         struct request_queue *q = rq->q;
372
373         trace_block_rq_issue(q, rq);
374
375         rq->resid_len = blk_rq_bytes(rq);
376         if (unlikely(blk_bidi_rq(rq)))
377                 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
378
379         /*
380          * Just mark start time and set the started bit. Due to memory
381          * ordering, we know we'll see the correct deadline as long as
382          * REQ_ATOMIC_STARTED is seen.
383          */
384         rq->deadline = jiffies + q->rq_timeout;
385
386         /*
387          * Mark us as started and clear complete. Complete might have been
388          * set if requeue raced with timeout, which then marked it as
389          * complete. So be sure to clear complete again when we start
390          * the request, otherwise we'll ignore the completion event.
391          */
392         set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
393         clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
394
395         if (q->dma_drain_size && blk_rq_bytes(rq)) {
396                 /*
397                  * Make sure space for the drain appears.  We know we can do
398                  * this because max_hw_segments has been adjusted to be one
399                  * fewer than the device can handle.
400                  */
401                 rq->nr_phys_segments++;
402         }
403
404         /*
405          * Flag the last request in the series so that drivers know when IO
406          * should be kicked off, if they don't do it on a per-request basis.
407          *
408          * Note: the flag isn't the only condition drivers should do kick off.
409          * If drive is busy, the last request might not have the bit set.
410          */
411         if (last)
412                 rq->cmd_flags |= REQ_END;
413 }
414
415 static void __blk_mq_requeue_request(struct request *rq)
416 {
417         struct request_queue *q = rq->q;
418
419         trace_block_rq_requeue(q, rq);
420         clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
421
422         rq->cmd_flags &= ~REQ_END;
423
424         if (q->dma_drain_size && blk_rq_bytes(rq))
425                 rq->nr_phys_segments--;
426 }
427
428 void blk_mq_requeue_request(struct request *rq)
429 {
430         struct request_queue *q = rq->q;
431
432         __blk_mq_requeue_request(rq);
433         blk_clear_rq_complete(rq);
434
435         trace_block_rq_requeue(q, rq);
436
437         BUG_ON(blk_queued_rq(rq));
438         blk_mq_insert_request(rq, true, true, false);
439 }
440 EXPORT_SYMBOL(blk_mq_requeue_request);
441
442 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
443 {
444         return tags->rqs[tag];
445 }
446 EXPORT_SYMBOL(blk_mq_tag_to_rq);
447
448 struct blk_mq_timeout_data {
449         struct blk_mq_hw_ctx *hctx;
450         unsigned long *next;
451         unsigned int *next_set;
452 };
453
454 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
455 {
456         struct blk_mq_timeout_data *data = __data;
457         struct blk_mq_hw_ctx *hctx = data->hctx;
458         unsigned int tag;
459
460          /* It may not be in flight yet (this is where
461          * the REQ_ATOMIC_STARTED flag comes in). The requests are
462          * statically allocated, so we know it's always safe to access the
463          * memory associated with a bit offset into ->rqs[].
464          */
465         tag = 0;
466         do {
467                 struct request *rq;
468
469                 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
470                 if (tag >= hctx->tags->nr_tags)
471                         break;
472
473                 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
474                 if (rq->q != hctx->queue)
475                         continue;
476                 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
477                         continue;
478
479                 blk_rq_check_expired(rq, data->next, data->next_set);
480         } while (1);
481 }
482
483 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
484                                         unsigned long *next,
485                                         unsigned int *next_set)
486 {
487         struct blk_mq_timeout_data data = {
488                 .hctx           = hctx,
489                 .next           = next,
490                 .next_set       = next_set,
491         };
492
493         /*
494          * Ask the tagging code to iterate busy requests, so we can
495          * check them for timeout.
496          */
497         blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
498 }
499
500 static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
501 {
502         struct request_queue *q = rq->q;
503
504         /*
505          * We know that complete is set at this point. If STARTED isn't set
506          * anymore, then the request isn't active and the "timeout" should
507          * just be ignored. This can happen due to the bitflag ordering.
508          * Timeout first checks if STARTED is set, and if it is, assumes
509          * the request is active. But if we race with completion, then
510          * we both flags will get cleared. So check here again, and ignore
511          * a timeout event with a request that isn't active.
512          */
513         if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
514                 return BLK_EH_NOT_HANDLED;
515
516         if (!q->mq_ops->timeout)
517                 return BLK_EH_RESET_TIMER;
518
519         return q->mq_ops->timeout(rq);
520 }
521
522 static void blk_mq_rq_timer(unsigned long data)
523 {
524         struct request_queue *q = (struct request_queue *) data;
525         struct blk_mq_hw_ctx *hctx;
526         unsigned long next = 0;
527         int i, next_set = 0;
528
529         queue_for_each_hw_ctx(q, hctx, i)
530                 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
531
532         if (next_set)
533                 mod_timer(&q->timeout, round_jiffies_up(next));
534 }
535
536 /*
537  * Reverse check our software queue for entries that we could potentially
538  * merge with. Currently includes a hand-wavy stop count of 8, to not spend
539  * too much time checking for merges.
540  */
541 static bool blk_mq_attempt_merge(struct request_queue *q,
542                                  struct blk_mq_ctx *ctx, struct bio *bio)
543 {
544         struct request *rq;
545         int checked = 8;
546
547         list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
548                 int el_ret;
549
550                 if (!checked--)
551                         break;
552
553                 if (!blk_rq_merge_ok(rq, bio))
554                         continue;
555
556                 el_ret = blk_try_merge(rq, bio);
557                 if (el_ret == ELEVATOR_BACK_MERGE) {
558                         if (bio_attempt_back_merge(q, rq, bio)) {
559                                 ctx->rq_merged++;
560                                 return true;
561                         }
562                         break;
563                 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
564                         if (bio_attempt_front_merge(q, rq, bio)) {
565                                 ctx->rq_merged++;
566                                 return true;
567                         }
568                         break;
569                 }
570         }
571
572         return false;
573 }
574
575 /*
576  * Run this hardware queue, pulling any software queues mapped to it in.
577  * Note that this function currently has various problems around ordering
578  * of IO. In particular, we'd like FIFO behaviour on handling existing
579  * items on the hctx->dispatch list. Ignore that for now.
580  */
581 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
582 {
583         struct request_queue *q = hctx->queue;
584         struct blk_mq_ctx *ctx;
585         struct request *rq;
586         LIST_HEAD(rq_list);
587         int bit, queued;
588
589         WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
590
591         if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
592                 return;
593
594         hctx->run++;
595
596         /*
597          * Touch any software queue that has pending entries.
598          */
599         for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
600                 clear_bit(bit, hctx->ctx_map);
601                 ctx = hctx->ctxs[bit];
602
603                 spin_lock(&ctx->lock);
604                 list_splice_tail_init(&ctx->rq_list, &rq_list);
605                 spin_unlock(&ctx->lock);
606         }
607
608         /*
609          * If we have previous entries on our dispatch list, grab them
610          * and stuff them at the front for more fair dispatch.
611          */
612         if (!list_empty_careful(&hctx->dispatch)) {
613                 spin_lock(&hctx->lock);
614                 if (!list_empty(&hctx->dispatch))
615                         list_splice_init(&hctx->dispatch, &rq_list);
616                 spin_unlock(&hctx->lock);
617         }
618
619         /*
620          * Delete and return all entries from our dispatch list
621          */
622         queued = 0;
623
624         /*
625          * Now process all the entries, sending them to the driver.
626          */
627         while (!list_empty(&rq_list)) {
628                 int ret;
629
630                 rq = list_first_entry(&rq_list, struct request, queuelist);
631                 list_del_init(&rq->queuelist);
632
633                 blk_mq_start_request(rq, list_empty(&rq_list));
634
635                 ret = q->mq_ops->queue_rq(hctx, rq);
636                 switch (ret) {
637                 case BLK_MQ_RQ_QUEUE_OK:
638                         queued++;
639                         continue;
640                 case BLK_MQ_RQ_QUEUE_BUSY:
641                         /*
642                          * FIXME: we should have a mechanism to stop the queue
643                          * like blk_stop_queue, otherwise we will waste cpu
644                          * time
645                          */
646                         list_add(&rq->queuelist, &rq_list);
647                         __blk_mq_requeue_request(rq);
648                         break;
649                 default:
650                         pr_err("blk-mq: bad return on queue: %d\n", ret);
651                 case BLK_MQ_RQ_QUEUE_ERROR:
652                         rq->errors = -EIO;
653                         blk_mq_end_io(rq, rq->errors);
654                         break;
655                 }
656
657                 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
658                         break;
659         }
660
661         if (!queued)
662                 hctx->dispatched[0]++;
663         else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
664                 hctx->dispatched[ilog2(queued) + 1]++;
665
666         /*
667          * Any items that need requeuing? Stuff them into hctx->dispatch,
668          * that is where we will continue on next queue run.
669          */
670         if (!list_empty(&rq_list)) {
671                 spin_lock(&hctx->lock);
672                 list_splice(&rq_list, &hctx->dispatch);
673                 spin_unlock(&hctx->lock);
674         }
675 }
676
677 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
678 {
679         if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
680                 return;
681
682         if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
683                 __blk_mq_run_hw_queue(hctx);
684         else if (hctx->queue->nr_hw_queues == 1)
685                 kblockd_schedule_delayed_work(&hctx->run_work, 0);
686         else {
687                 unsigned int cpu;
688
689                 /*
690                  * It'd be great if the workqueue API had a way to pass
691                  * in a mask and had some smarts for more clever placement
692                  * than the first CPU. Or we could round-robin here. For now,
693                  * just queue on the first CPU.
694                  */
695                 cpu = cpumask_first(hctx->cpumask);
696                 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
697         }
698 }
699
700 void blk_mq_run_queues(struct request_queue *q, bool async)
701 {
702         struct blk_mq_hw_ctx *hctx;
703         int i;
704
705         queue_for_each_hw_ctx(q, hctx, i) {
706                 if ((!blk_mq_hctx_has_pending(hctx) &&
707                     list_empty_careful(&hctx->dispatch)) ||
708                     test_bit(BLK_MQ_S_STOPPED, &hctx->state))
709                         continue;
710
711                 preempt_disable();
712                 blk_mq_run_hw_queue(hctx, async);
713                 preempt_enable();
714         }
715 }
716 EXPORT_SYMBOL(blk_mq_run_queues);
717
718 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
719 {
720         cancel_delayed_work(&hctx->run_work);
721         cancel_delayed_work(&hctx->delay_work);
722         set_bit(BLK_MQ_S_STOPPED, &hctx->state);
723 }
724 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
725
726 void blk_mq_stop_hw_queues(struct request_queue *q)
727 {
728         struct blk_mq_hw_ctx *hctx;
729         int i;
730
731         queue_for_each_hw_ctx(q, hctx, i)
732                 blk_mq_stop_hw_queue(hctx);
733 }
734 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
735
736 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
737 {
738         clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
739
740         preempt_disable();
741         __blk_mq_run_hw_queue(hctx);
742         preempt_enable();
743 }
744 EXPORT_SYMBOL(blk_mq_start_hw_queue);
745
746 void blk_mq_start_hw_queues(struct request_queue *q)
747 {
748         struct blk_mq_hw_ctx *hctx;
749         int i;
750
751         queue_for_each_hw_ctx(q, hctx, i)
752                 blk_mq_start_hw_queue(hctx);
753 }
754 EXPORT_SYMBOL(blk_mq_start_hw_queues);
755
756
757 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
758 {
759         struct blk_mq_hw_ctx *hctx;
760         int i;
761
762         queue_for_each_hw_ctx(q, hctx, i) {
763                 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
764                         continue;
765
766                 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
767                 preempt_disable();
768                 blk_mq_run_hw_queue(hctx, async);
769                 preempt_enable();
770         }
771 }
772 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
773
774 static void blk_mq_run_work_fn(struct work_struct *work)
775 {
776         struct blk_mq_hw_ctx *hctx;
777
778         hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
779
780         __blk_mq_run_hw_queue(hctx);
781 }
782
783 static void blk_mq_delay_work_fn(struct work_struct *work)
784 {
785         struct blk_mq_hw_ctx *hctx;
786
787         hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
788
789         if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
790                 __blk_mq_run_hw_queue(hctx);
791 }
792
793 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
794 {
795         unsigned long tmo = msecs_to_jiffies(msecs);
796
797         if (hctx->queue->nr_hw_queues == 1)
798                 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
799         else {
800                 unsigned int cpu;
801
802                 /*
803                  * It'd be great if the workqueue API had a way to pass
804                  * in a mask and had some smarts for more clever placement
805                  * than the first CPU. Or we could round-robin here. For now,
806                  * just queue on the first CPU.
807                  */
808                 cpu = cpumask_first(hctx->cpumask);
809                 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
810         }
811 }
812 EXPORT_SYMBOL(blk_mq_delay_queue);
813
814 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
815                                     struct request *rq, bool at_head)
816 {
817         struct blk_mq_ctx *ctx = rq->mq_ctx;
818
819         trace_block_rq_insert(hctx->queue, rq);
820
821         if (at_head)
822                 list_add(&rq->queuelist, &ctx->rq_list);
823         else
824                 list_add_tail(&rq->queuelist, &ctx->rq_list);
825         blk_mq_hctx_mark_pending(hctx, ctx);
826
827         /*
828          * We do this early, to ensure we are on the right CPU.
829          */
830         blk_add_timer(rq);
831 }
832
833 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
834                 bool async)
835 {
836         struct request_queue *q = rq->q;
837         struct blk_mq_hw_ctx *hctx;
838         struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
839
840         current_ctx = blk_mq_get_ctx(q);
841         if (!cpu_online(ctx->cpu))
842                 rq->mq_ctx = ctx = current_ctx;
843
844         hctx = q->mq_ops->map_queue(q, ctx->cpu);
845
846         if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
847             !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
848                 blk_insert_flush(rq);
849         } else {
850                 spin_lock(&ctx->lock);
851                 __blk_mq_insert_request(hctx, rq, at_head);
852                 spin_unlock(&ctx->lock);
853         }
854
855         if (run_queue)
856                 blk_mq_run_hw_queue(hctx, async);
857
858         blk_mq_put_ctx(current_ctx);
859 }
860
861 static void blk_mq_insert_requests(struct request_queue *q,
862                                      struct blk_mq_ctx *ctx,
863                                      struct list_head *list,
864                                      int depth,
865                                      bool from_schedule)
866
867 {
868         struct blk_mq_hw_ctx *hctx;
869         struct blk_mq_ctx *current_ctx;
870
871         trace_block_unplug(q, depth, !from_schedule);
872
873         current_ctx = blk_mq_get_ctx(q);
874
875         if (!cpu_online(ctx->cpu))
876                 ctx = current_ctx;
877         hctx = q->mq_ops->map_queue(q, ctx->cpu);
878
879         /*
880          * preemption doesn't flush plug list, so it's possible ctx->cpu is
881          * offline now
882          */
883         spin_lock(&ctx->lock);
884         while (!list_empty(list)) {
885                 struct request *rq;
886
887                 rq = list_first_entry(list, struct request, queuelist);
888                 list_del_init(&rq->queuelist);
889                 rq->mq_ctx = ctx;
890                 __blk_mq_insert_request(hctx, rq, false);
891         }
892         spin_unlock(&ctx->lock);
893
894         blk_mq_run_hw_queue(hctx, from_schedule);
895         blk_mq_put_ctx(current_ctx);
896 }
897
898 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
899 {
900         struct request *rqa = container_of(a, struct request, queuelist);
901         struct request *rqb = container_of(b, struct request, queuelist);
902
903         return !(rqa->mq_ctx < rqb->mq_ctx ||
904                  (rqa->mq_ctx == rqb->mq_ctx &&
905                   blk_rq_pos(rqa) < blk_rq_pos(rqb)));
906 }
907
908 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
909 {
910         struct blk_mq_ctx *this_ctx;
911         struct request_queue *this_q;
912         struct request *rq;
913         LIST_HEAD(list);
914         LIST_HEAD(ctx_list);
915         unsigned int depth;
916
917         list_splice_init(&plug->mq_list, &list);
918
919         list_sort(NULL, &list, plug_ctx_cmp);
920
921         this_q = NULL;
922         this_ctx = NULL;
923         depth = 0;
924
925         while (!list_empty(&list)) {
926                 rq = list_entry_rq(list.next);
927                 list_del_init(&rq->queuelist);
928                 BUG_ON(!rq->q);
929                 if (rq->mq_ctx != this_ctx) {
930                         if (this_ctx) {
931                                 blk_mq_insert_requests(this_q, this_ctx,
932                                                         &ctx_list, depth,
933                                                         from_schedule);
934                         }
935
936                         this_ctx = rq->mq_ctx;
937                         this_q = rq->q;
938                         depth = 0;
939                 }
940
941                 depth++;
942                 list_add_tail(&rq->queuelist, &ctx_list);
943         }
944
945         /*
946          * If 'this_ctx' is set, we know we have entries to complete
947          * on 'ctx_list'. Do those.
948          */
949         if (this_ctx) {
950                 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
951                                        from_schedule);
952         }
953 }
954
955 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
956 {
957         init_request_from_bio(rq, bio);
958         blk_account_io_start(rq, 1);
959 }
960
961 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
962 {
963         struct blk_mq_hw_ctx *hctx;
964         struct blk_mq_ctx *ctx;
965         const int is_sync = rw_is_sync(bio->bi_rw);
966         const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
967         int rw = bio_data_dir(bio);
968         struct request *rq;
969         unsigned int use_plug, request_count = 0;
970
971         /*
972          * If we have multiple hardware queues, just go directly to
973          * one of those for sync IO.
974          */
975         use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
976
977         blk_queue_bounce(q, &bio);
978
979         if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
980                 bio_endio(bio, -EIO);
981                 return;
982         }
983
984         if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
985                 return;
986
987         if (blk_mq_queue_enter(q)) {
988                 bio_endio(bio, -EIO);
989                 return;
990         }
991
992         ctx = blk_mq_get_ctx(q);
993         hctx = q->mq_ops->map_queue(q, ctx->cpu);
994
995         if (is_sync)
996                 rw |= REQ_SYNC;
997         trace_block_getrq(q, bio, rw);
998         rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
999         if (likely(rq))
1000                 blk_mq_rq_ctx_init(q, ctx, rq, rw);
1001         else {
1002                 blk_mq_put_ctx(ctx);
1003                 trace_block_sleeprq(q, bio, rw);
1004                 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
1005                                                         false);
1006                 ctx = rq->mq_ctx;
1007                 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1008         }
1009
1010         hctx->queued++;
1011
1012         if (unlikely(is_flush_fua)) {
1013                 blk_mq_bio_to_request(rq, bio);
1014                 blk_insert_flush(rq);
1015                 goto run_queue;
1016         }
1017
1018         /*
1019          * A task plug currently exists. Since this is completely lockless,
1020          * utilize that to temporarily store requests until the task is
1021          * either done or scheduled away.
1022          */
1023         if (use_plug) {
1024                 struct blk_plug *plug = current->plug;
1025
1026                 if (plug) {
1027                         blk_mq_bio_to_request(rq, bio);
1028                         if (list_empty(&plug->mq_list))
1029                                 trace_block_plug(q);
1030                         else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1031                                 blk_flush_plug_list(plug, false);
1032                                 trace_block_plug(q);
1033                         }
1034                         list_add_tail(&rq->queuelist, &plug->mq_list);
1035                         blk_mq_put_ctx(ctx);
1036                         return;
1037                 }
1038         }
1039
1040         if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1041                 init_request_from_bio(rq, bio);
1042
1043                 spin_lock(&ctx->lock);
1044 insert_rq:
1045                 __blk_mq_insert_request(hctx, rq, false);
1046                 spin_unlock(&ctx->lock);
1047                 blk_account_io_start(rq, 1);
1048         } else {
1049                 spin_lock(&ctx->lock);
1050                 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1051                         init_request_from_bio(rq, bio);
1052                         goto insert_rq;
1053                 }
1054
1055                 spin_unlock(&ctx->lock);
1056                 __blk_mq_free_request(hctx, ctx, rq);
1057         }
1058
1059
1060         /*
1061          * For a SYNC request, send it to the hardware immediately. For an
1062          * ASYNC request, just ensure that we run it later on. The latter
1063          * allows for merging opportunities and more efficient dispatching.
1064          */
1065 run_queue:
1066         blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
1067         blk_mq_put_ctx(ctx);
1068 }
1069
1070 /*
1071  * Default mapping to a software queue, since we use one per CPU.
1072  */
1073 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1074 {
1075         return q->queue_hw_ctx[q->mq_map[cpu]];
1076 }
1077 EXPORT_SYMBOL(blk_mq_map_queue);
1078
1079 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
1080                                                    unsigned int hctx_index)
1081 {
1082         return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
1083                                 GFP_KERNEL | __GFP_ZERO, set->numa_node);
1084 }
1085 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1086
1087 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1088                                  unsigned int hctx_index)
1089 {
1090         kfree(hctx);
1091 }
1092 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1093
1094 static void blk_mq_hctx_notify(void *data, unsigned long action,
1095                                unsigned int cpu)
1096 {
1097         struct blk_mq_hw_ctx *hctx = data;
1098         struct request_queue *q = hctx->queue;
1099         struct blk_mq_ctx *ctx;
1100         LIST_HEAD(tmp);
1101
1102         if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1103                 return;
1104
1105         /*
1106          * Move ctx entries to new CPU, if this one is going away.
1107          */
1108         ctx = __blk_mq_get_ctx(q, cpu);
1109
1110         spin_lock(&ctx->lock);
1111         if (!list_empty(&ctx->rq_list)) {
1112                 list_splice_init(&ctx->rq_list, &tmp);
1113                 clear_bit(ctx->index_hw, hctx->ctx_map);
1114         }
1115         spin_unlock(&ctx->lock);
1116
1117         if (list_empty(&tmp))
1118                 return;
1119
1120         ctx = blk_mq_get_ctx(q);
1121         spin_lock(&ctx->lock);
1122
1123         while (!list_empty(&tmp)) {
1124                 struct request *rq;
1125
1126                 rq = list_first_entry(&tmp, struct request, queuelist);
1127                 rq->mq_ctx = ctx;
1128                 list_move_tail(&rq->queuelist, &ctx->rq_list);
1129         }
1130
1131         hctx = q->mq_ops->map_queue(q, ctx->cpu);
1132         blk_mq_hctx_mark_pending(hctx, ctx);
1133
1134         spin_unlock(&ctx->lock);
1135
1136         blk_mq_run_hw_queue(hctx, true);
1137         blk_mq_put_ctx(ctx);
1138 }
1139
1140 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1141                 struct blk_mq_tags *tags, unsigned int hctx_idx)
1142 {
1143         struct page *page;
1144
1145         if (tags->rqs && set->ops->exit_request) {
1146                 int i;
1147
1148                 for (i = 0; i < tags->nr_tags; i++) {
1149                         if (!tags->rqs[i])
1150                                 continue;
1151                         set->ops->exit_request(set->driver_data, tags->rqs[i],
1152                                                 hctx_idx, i);
1153                 }
1154         }
1155
1156         while (!list_empty(&tags->page_list)) {
1157                 page = list_first_entry(&tags->page_list, struct page, lru);
1158                 list_del_init(&page->lru);
1159                 __free_pages(page, page->private);
1160         }
1161
1162         kfree(tags->rqs);
1163
1164         blk_mq_free_tags(tags);
1165 }
1166
1167 static size_t order_to_size(unsigned int order)
1168 {
1169         return (size_t)PAGE_SIZE << order;
1170 }
1171
1172 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1173                 unsigned int hctx_idx)
1174 {
1175         struct blk_mq_tags *tags;
1176         unsigned int i, j, entries_per_page, max_order = 4;
1177         size_t rq_size, left;
1178
1179         tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1180                                 set->numa_node);
1181         if (!tags)
1182                 return NULL;
1183
1184         INIT_LIST_HEAD(&tags->page_list);
1185
1186         tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1187                                         GFP_KERNEL, set->numa_node);
1188         if (!tags->rqs) {
1189                 blk_mq_free_tags(tags);
1190                 return NULL;
1191         }
1192
1193         /*
1194          * rq_size is the size of the request plus driver payload, rounded
1195          * to the cacheline size
1196          */
1197         rq_size = round_up(sizeof(struct request) + set->cmd_size,
1198                                 cache_line_size());
1199         left = rq_size * set->queue_depth;
1200
1201         for (i = 0; i < set->queue_depth; ) {
1202                 int this_order = max_order;
1203                 struct page *page;
1204                 int to_do;
1205                 void *p;
1206
1207                 while (left < order_to_size(this_order - 1) && this_order)
1208                         this_order--;
1209
1210                 do {
1211                         page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1212                                                 this_order);
1213                         if (page)
1214                                 break;
1215                         if (!this_order--)
1216                                 break;
1217                         if (order_to_size(this_order) < rq_size)
1218                                 break;
1219                 } while (1);
1220
1221                 if (!page)
1222                         goto fail;
1223
1224                 page->private = this_order;
1225                 list_add_tail(&page->lru, &tags->page_list);
1226
1227                 p = page_address(page);
1228                 entries_per_page = order_to_size(this_order) / rq_size;
1229                 to_do = min(entries_per_page, set->queue_depth - i);
1230                 left -= to_do * rq_size;
1231                 for (j = 0; j < to_do; j++) {
1232                         tags->rqs[i] = p;
1233                         if (set->ops->init_request) {
1234                                 if (set->ops->init_request(set->driver_data,
1235                                                 tags->rqs[i], hctx_idx, i,
1236                                                 set->numa_node))
1237                                         goto fail;
1238                         }
1239
1240                         p += rq_size;
1241                         i++;
1242                 }
1243         }
1244
1245         return tags;
1246
1247 fail:
1248         pr_warn("%s: failed to allocate requests\n", __func__);
1249         blk_mq_free_rq_map(set, tags, hctx_idx);
1250         return NULL;
1251 }
1252
1253 static int blk_mq_init_hw_queues(struct request_queue *q,
1254                 struct blk_mq_tag_set *set)
1255 {
1256         struct blk_mq_hw_ctx *hctx;
1257         unsigned int i, j;
1258
1259         /*
1260          * Initialize hardware queues
1261          */
1262         queue_for_each_hw_ctx(q, hctx, i) {
1263                 unsigned int num_maps;
1264                 int node;
1265
1266                 node = hctx->numa_node;
1267                 if (node == NUMA_NO_NODE)
1268                         node = hctx->numa_node = set->numa_node;
1269
1270                 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1271                 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1272                 spin_lock_init(&hctx->lock);
1273                 INIT_LIST_HEAD(&hctx->dispatch);
1274                 hctx->queue = q;
1275                 hctx->queue_num = i;
1276                 hctx->flags = set->flags;
1277                 hctx->cmd_size = set->cmd_size;
1278
1279                 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1280                                                 blk_mq_hctx_notify, hctx);
1281                 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1282
1283                 hctx->tags = set->tags[i];
1284
1285                 /*
1286                  * Allocate space for all possible cpus to avoid allocation in
1287                  * runtime
1288                  */
1289                 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1290                                                 GFP_KERNEL, node);
1291                 if (!hctx->ctxs)
1292                         break;
1293
1294                 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1295                 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1296                                                 GFP_KERNEL, node);
1297                 if (!hctx->ctx_map)
1298                         break;
1299
1300                 hctx->nr_ctx_map = num_maps;
1301                 hctx->nr_ctx = 0;
1302
1303                 if (set->ops->init_hctx &&
1304                     set->ops->init_hctx(hctx, set->driver_data, i))
1305                         break;
1306         }
1307
1308         if (i == q->nr_hw_queues)
1309                 return 0;
1310
1311         /*
1312          * Init failed
1313          */
1314         queue_for_each_hw_ctx(q, hctx, j) {
1315                 if (i == j)
1316                         break;
1317
1318                 if (set->ops->exit_hctx)
1319                         set->ops->exit_hctx(hctx, j);
1320
1321                 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1322                 kfree(hctx->ctxs);
1323                 kfree(hctx->ctx_map);
1324         }
1325
1326         return 1;
1327 }
1328
1329 static void blk_mq_init_cpu_queues(struct request_queue *q,
1330                                    unsigned int nr_hw_queues)
1331 {
1332         unsigned int i;
1333
1334         for_each_possible_cpu(i) {
1335                 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1336                 struct blk_mq_hw_ctx *hctx;
1337
1338                 memset(__ctx, 0, sizeof(*__ctx));
1339                 __ctx->cpu = i;
1340                 spin_lock_init(&__ctx->lock);
1341                 INIT_LIST_HEAD(&__ctx->rq_list);
1342                 __ctx->queue = q;
1343
1344                 /* If the cpu isn't online, the cpu is mapped to first hctx */
1345                 if (!cpu_online(i))
1346                         continue;
1347
1348                 hctx = q->mq_ops->map_queue(q, i);
1349                 cpumask_set_cpu(i, hctx->cpumask);
1350                 hctx->nr_ctx++;
1351
1352                 /*
1353                  * Set local node, IFF we have more than one hw queue. If
1354                  * not, we remain on the home node of the device
1355                  */
1356                 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1357                         hctx->numa_node = cpu_to_node(i);
1358         }
1359 }
1360
1361 static void blk_mq_map_swqueue(struct request_queue *q)
1362 {
1363         unsigned int i;
1364         struct blk_mq_hw_ctx *hctx;
1365         struct blk_mq_ctx *ctx;
1366
1367         queue_for_each_hw_ctx(q, hctx, i) {
1368                 cpumask_clear(hctx->cpumask);
1369                 hctx->nr_ctx = 0;
1370         }
1371
1372         /*
1373          * Map software to hardware queues
1374          */
1375         queue_for_each_ctx(q, ctx, i) {
1376                 /* If the cpu isn't online, the cpu is mapped to first hctx */
1377                 if (!cpu_online(i))
1378                         continue;
1379
1380                 hctx = q->mq_ops->map_queue(q, i);
1381                 cpumask_set_cpu(i, hctx->cpumask);
1382                 ctx->index_hw = hctx->nr_ctx;
1383                 hctx->ctxs[hctx->nr_ctx++] = ctx;
1384         }
1385 }
1386
1387 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1388 {
1389         struct blk_mq_hw_ctx **hctxs;
1390         struct blk_mq_ctx *ctx;
1391         struct request_queue *q;
1392         int i;
1393
1394         ctx = alloc_percpu(struct blk_mq_ctx);
1395         if (!ctx)
1396                 return ERR_PTR(-ENOMEM);
1397
1398         hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1399                         set->numa_node);
1400
1401         if (!hctxs)
1402                 goto err_percpu;
1403
1404         for (i = 0; i < set->nr_hw_queues; i++) {
1405                 hctxs[i] = set->ops->alloc_hctx(set, i);
1406                 if (!hctxs[i])
1407                         goto err_hctxs;
1408
1409                 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1410                         goto err_hctxs;
1411
1412                 hctxs[i]->numa_node = NUMA_NO_NODE;
1413                 hctxs[i]->queue_num = i;
1414         }
1415
1416         q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1417         if (!q)
1418                 goto err_hctxs;
1419
1420         q->mq_map = blk_mq_make_queue_map(set);
1421         if (!q->mq_map)
1422                 goto err_map;
1423
1424         setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1425         blk_queue_rq_timeout(q, 30000);
1426
1427         q->nr_queues = nr_cpu_ids;
1428         q->nr_hw_queues = set->nr_hw_queues;
1429
1430         q->queue_ctx = ctx;
1431         q->queue_hw_ctx = hctxs;
1432
1433         q->mq_ops = set->ops;
1434         q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1435
1436         q->sg_reserved_size = INT_MAX;
1437
1438         blk_queue_make_request(q, blk_mq_make_request);
1439         blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
1440         if (set->timeout)
1441                 blk_queue_rq_timeout(q, set->timeout);
1442
1443         if (set->ops->complete)
1444                 blk_queue_softirq_done(q, set->ops->complete);
1445
1446         blk_mq_init_flush(q);
1447         blk_mq_init_cpu_queues(q, set->nr_hw_queues);
1448
1449         q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1450                                 set->cmd_size, cache_line_size()),
1451                                 GFP_KERNEL);
1452         if (!q->flush_rq)
1453                 goto err_hw;
1454
1455         if (blk_mq_init_hw_queues(q, set))
1456                 goto err_flush_rq;
1457
1458         blk_mq_map_swqueue(q);
1459
1460         mutex_lock(&all_q_mutex);
1461         list_add_tail(&q->all_q_node, &all_q_list);
1462         mutex_unlock(&all_q_mutex);
1463
1464         return q;
1465
1466 err_flush_rq:
1467         kfree(q->flush_rq);
1468 err_hw:
1469         kfree(q->mq_map);
1470 err_map:
1471         blk_cleanup_queue(q);
1472 err_hctxs:
1473         for (i = 0; i < set->nr_hw_queues; i++) {
1474                 if (!hctxs[i])
1475                         break;
1476                 free_cpumask_var(hctxs[i]->cpumask);
1477                 set->ops->free_hctx(hctxs[i], i);
1478         }
1479         kfree(hctxs);
1480 err_percpu:
1481         free_percpu(ctx);
1482         return ERR_PTR(-ENOMEM);
1483 }
1484 EXPORT_SYMBOL(blk_mq_init_queue);
1485
1486 void blk_mq_free_queue(struct request_queue *q)
1487 {
1488         struct blk_mq_hw_ctx *hctx;
1489         int i;
1490
1491         queue_for_each_hw_ctx(q, hctx, i) {
1492                 kfree(hctx->ctx_map);
1493                 kfree(hctx->ctxs);
1494                 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1495                 if (q->mq_ops->exit_hctx)
1496                         q->mq_ops->exit_hctx(hctx, i);
1497                 free_cpumask_var(hctx->cpumask);
1498                 q->mq_ops->free_hctx(hctx, i);
1499         }
1500
1501         free_percpu(q->queue_ctx);
1502         kfree(q->queue_hw_ctx);
1503         kfree(q->mq_map);
1504
1505         q->queue_ctx = NULL;
1506         q->queue_hw_ctx = NULL;
1507         q->mq_map = NULL;
1508
1509         mutex_lock(&all_q_mutex);
1510         list_del_init(&q->all_q_node);
1511         mutex_unlock(&all_q_mutex);
1512 }
1513
1514 /* Basically redo blk_mq_init_queue with queue frozen */
1515 static void blk_mq_queue_reinit(struct request_queue *q)
1516 {
1517         blk_mq_freeze_queue(q);
1518
1519         blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1520
1521         /*
1522          * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1523          * we should change hctx numa_node according to new topology (this
1524          * involves free and re-allocate memory, worthy doing?)
1525          */
1526
1527         blk_mq_map_swqueue(q);
1528
1529         blk_mq_unfreeze_queue(q);
1530 }
1531
1532 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1533                                       unsigned long action, void *hcpu)
1534 {
1535         struct request_queue *q;
1536
1537         /*
1538          * Before new mapping is established, hotadded cpu might already start
1539          * handling requests. This doesn't break anything as we map offline
1540          * CPUs to first hardware queue. We will re-init queue below to get
1541          * optimal settings.
1542          */
1543         if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1544             action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1545                 return NOTIFY_OK;
1546
1547         mutex_lock(&all_q_mutex);
1548         list_for_each_entry(q, &all_q_list, all_q_node)
1549                 blk_mq_queue_reinit(q);
1550         mutex_unlock(&all_q_mutex);
1551         return NOTIFY_OK;
1552 }
1553
1554 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1555 {
1556         int i;
1557
1558         if (!set->nr_hw_queues)
1559                 return -EINVAL;
1560         if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1561                 return -EINVAL;
1562         if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1563                 return -EINVAL;
1564
1565         if (!set->nr_hw_queues ||
1566             !set->ops->queue_rq || !set->ops->map_queue ||
1567             !set->ops->alloc_hctx || !set->ops->free_hctx)
1568                 return -EINVAL;
1569
1570
1571         set->tags = kmalloc_node(set->nr_hw_queues *
1572                                  sizeof(struct blk_mq_tags *),
1573                                  GFP_KERNEL, set->numa_node);
1574         if (!set->tags)
1575                 goto out;
1576
1577         for (i = 0; i < set->nr_hw_queues; i++) {
1578                 set->tags[i] = blk_mq_init_rq_map(set, i);
1579                 if (!set->tags[i])
1580                         goto out_unwind;
1581         }
1582
1583         return 0;
1584
1585 out_unwind:
1586         while (--i >= 0)
1587                 blk_mq_free_rq_map(set, set->tags[i], i);
1588 out:
1589         return -ENOMEM;
1590 }
1591 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1592
1593 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1594 {
1595         int i;
1596
1597         for (i = 0; i < set->nr_hw_queues; i++)
1598                 blk_mq_free_rq_map(set, set->tags[i], i);
1599         kfree(set->tags);
1600 }
1601 EXPORT_SYMBOL(blk_mq_free_tag_set);
1602
1603 void blk_mq_disable_hotplug(void)
1604 {
1605         mutex_lock(&all_q_mutex);
1606 }
1607
1608 void blk_mq_enable_hotplug(void)
1609 {
1610         mutex_unlock(&all_q_mutex);
1611 }
1612
1613 static int __init blk_mq_init(void)
1614 {
1615         blk_mq_cpu_init();
1616
1617         /* Must be called after percpu_counter_hotcpu_callback() */
1618         hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1619
1620         return 0;
1621 }
1622 subsys_initcall(blk_mq_init);
This page took 0.121972 seconds and 4 git commands to generate.