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