blob: 79aa11b3efa524e6575bede9199d0ca6585fe45b [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
Jens Axboe320ae512013-10-24 09:20:05 +01007#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>
Jens Axboeaedcd722014-09-17 08:27:03 -060023#include <linux/crash_dump.h>
Jens Axboe320ae512013-10-24 09:20:05 +010024
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
32static DEFINE_MUTEX(all_q_mutex);
33static LIST_HEAD(all_q_list);
34
35static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
36
Jens Axboe320ae512013-10-24 09:20:05 +010037/*
38 * Check if any of the ctx's have pending work in this hardware queue
39 */
40static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
41{
42 unsigned int i;
43
Jens Axboe1429d7c2014-05-19 09:23:55 -060044 for (i = 0; i < hctx->ctx_map.map_size; i++)
45 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010046 return true;
47
48 return false;
49}
50
Jens Axboe1429d7c2014-05-19 09:23:55 -060051static 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
Jens Axboe320ae512013-10-24 09:20:05 +010060/*
61 * Mark this ctx as having pending work in this hardware queue
62 */
63static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
64 struct blk_mq_ctx *ctx)
65{
Jens Axboe1429d7c2014-05-19 09:23:55 -060066 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
72static 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);
Jens Axboe320ae512013-10-24 09:20:05 +010078}
79
Jens Axboe320ae512013-10-24 09:20:05 +010080static int blk_mq_queue_enter(struct request_queue *q)
81{
Tejun Heoadd703f2014-07-01 10:34:38 -060082 while (true) {
83 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +010084
Tejun Heoadd703f2014-07-01 10:34:38 -060085 if (percpu_ref_tryget_live(&q->mq_usage_counter))
86 return 0;
Keith Busch3b632cf2014-06-06 10:22:07 -060087
Tejun Heoadd703f2014-07-01 10:34:38 -060088 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 }
Jens Axboe320ae512013-10-24 09:20:05 +010095}
96
97static void blk_mq_queue_exit(struct request_queue *q)
98{
Tejun Heoadd703f2014-07-01 10:34:38 -060099 percpu_ref_put(&q->mq_usage_counter);
100}
101
102static 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);
Jens Axboe320ae512013-10-24 09:20:05 +0100108}
109
Tejun Heo72d6f022014-07-01 10:33:02 -0600110/*
111 * Guarantee no request is in use, so we can change any data structure of
112 * the queue afterward.
113 */
114void blk_mq_freeze_queue(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800115{
Tejun Heocddd5d12014-08-16 08:02:24 -0400116 bool freeze;
117
Tejun Heo72d6f022014-07-01 10:33:02 -0600118 spin_lock_irq(q->queue_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400119 freeze = !q->mq_freeze_depth++;
Tejun Heo72d6f022014-07-01 10:33:02 -0600120 spin_unlock_irq(q->queue_lock);
121
Tejun Heocddd5d12014-08-16 08:02:24 -0400122 if (freeze) {
123 percpu_ref_kill(&q->mq_usage_counter);
124 blk_mq_run_queues(q, false);
125 }
Tejun Heoadd703f2014-07-01 10:34:38 -0600126 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800127}
128
Jens Axboe320ae512013-10-24 09:20:05 +0100129static void blk_mq_unfreeze_queue(struct request_queue *q)
130{
Tejun Heocddd5d12014-08-16 08:02:24 -0400131 bool wake;
Jens Axboe320ae512013-10-24 09:20:05 +0100132
133 spin_lock_irq(q->queue_lock);
Tejun Heo780db202014-07-01 10:31:13 -0600134 wake = !--q->mq_freeze_depth;
135 WARN_ON_ONCE(q->mq_freeze_depth < 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100136 spin_unlock_irq(q->queue_lock);
Tejun Heoadd703f2014-07-01 10:34:38 -0600137 if (wake) {
138 percpu_ref_reinit(&q->mq_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100139 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600140 }
Jens Axboe320ae512013-10-24 09:20:05 +0100141}
142
143bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
144{
145 return blk_mq_has_free_tags(hctx->tags);
146}
147EXPORT_SYMBOL(blk_mq_can_queue);
148
Jens Axboe94eddfb2013-11-19 09:25:07 -0700149static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
150 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100151{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700152 if (blk_queue_io_stat(q))
153 rw_flags |= REQ_IO_STAT;
154
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200155 INIT_LIST_HEAD(&rq->queuelist);
156 /* csd/requeue_work/fifo_time is initialized before use */
157 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100158 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600159 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200160 /* do not touch atomic flags, it needs atomic ops against the timer */
161 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200162 INIT_HLIST_NODE(&rq->hash);
163 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200164 rq->rq_disk = NULL;
165 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600166 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200167#ifdef CONFIG_BLK_CGROUP
168 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700169 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200170 rq->io_start_time_ns = 0;
171#endif
172 rq->nr_phys_segments = 0;
173#if defined(CONFIG_BLK_DEV_INTEGRITY)
174 rq->nr_integrity_segments = 0;
175#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200176 rq->special = NULL;
177 /* tag was already set */
178 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200179
Tony Battersby6f4a16262014-08-22 15:53:39 -0400180 rq->cmd = rq->__cmd;
181
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200182 rq->extra_len = 0;
183 rq->sense_len = 0;
184 rq->resid_len = 0;
185 rq->sense = NULL;
186
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200187 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600188 rq->timeout = 0;
189
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200190 rq->end_io = NULL;
191 rq->end_io_data = NULL;
192 rq->next_rq = NULL;
193
Jens Axboe320ae512013-10-24 09:20:05 +0100194 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195}
196
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200197static struct request *
Ming Leicb96a422014-06-01 00:43:37 +0800198__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200199{
200 struct request *rq;
201 unsigned int tag;
202
Ming Leicb96a422014-06-01 00:43:37 +0800203 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200204 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a422014-06-01 00:43:37 +0800205 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200206
Ming Leicb96a422014-06-01 00:43:37 +0800207 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200208 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a422014-06-01 00:43:37 +0800209 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200210 }
211
212 rq->tag = tag;
Ming Leicb96a422014-06-01 00:43:37 +0800213 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200214 return rq;
215 }
216
217 return NULL;
218}
219
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200220struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
221 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100222{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200223 struct blk_mq_ctx *ctx;
224 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100225 struct request *rq;
Ming Leicb96a422014-06-01 00:43:37 +0800226 struct blk_mq_alloc_data alloc_data;
Joe Lawrencea492f072014-08-28 08:15:21 -0600227 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100228
Joe Lawrencea492f072014-08-28 08:15:21 -0600229 ret = blk_mq_queue_enter(q);
230 if (ret)
231 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100232
Christoph Hellwigd8525642014-05-27 20:59:50 +0200233 ctx = blk_mq_get_ctx(q);
234 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800235 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
236 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200237
Ming Leicb96a422014-06-01 00:43:37 +0800238 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200239 if (!rq && (gfp & __GFP_WAIT)) {
240 __blk_mq_run_hw_queue(hctx);
241 blk_mq_put_ctx(ctx);
242
243 ctx = blk_mq_get_ctx(q);
244 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800245 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
246 hctx);
247 rq = __blk_mq_alloc_request(&alloc_data, rw);
248 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200249 }
250 blk_mq_put_ctx(ctx);
Joe Lawrencea492f072014-08-28 08:15:21 -0600251 if (!rq)
252 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100253 return rq;
254}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600255EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100256
Jens Axboe320ae512013-10-24 09:20:05 +0100257static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
258 struct blk_mq_ctx *ctx, struct request *rq)
259{
260 const int tag = rq->tag;
261 struct request_queue *q = rq->q;
262
Jens Axboe0d2602c2014-05-13 15:10:52 -0600263 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
264 atomic_dec(&hctx->nr_active);
David Hildenbrand683d0e12014-09-18 11:04:31 +0200265 rq->cmd_flags = 0;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600266
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200267 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600268 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100269 blk_mq_queue_exit(q);
270}
271
272void blk_mq_free_request(struct request *rq)
273{
274 struct blk_mq_ctx *ctx = rq->mq_ctx;
275 struct blk_mq_hw_ctx *hctx;
276 struct request_queue *q = rq->q;
277
278 ctx->rq_completed[rq_is_sync(rq)]++;
279
280 hctx = q->mq_ops->map_queue(q, ctx->cpu);
281 __blk_mq_free_request(hctx, ctx, rq);
282}
283
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700284inline void __blk_mq_end_request(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100285{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700286 blk_account_io_done(rq);
287
Christoph Hellwig91b63632014-04-16 09:44:53 +0200288 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100289 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200290 } else {
291 if (unlikely(blk_bidi_rq(rq)))
292 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100293 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200294 }
Jens Axboe320ae512013-10-24 09:20:05 +0100295}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700296EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200297
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700298void blk_mq_end_request(struct request *rq, int error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200299{
300 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
301 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700302 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200303}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700304EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100305
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800306static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100307{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800308 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100309
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800310 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100311}
312
Jens Axboeed851862014-05-30 21:20:50 -0600313static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100314{
315 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700316 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100317 int cpu;
318
Christoph Hellwig38535202014-04-25 02:32:53 -0700319 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800320 rq->q->softirq_done_fn(rq);
321 return;
322 }
Jens Axboe320ae512013-10-24 09:20:05 +0100323
324 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700325 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
326 shared = cpus_share_cache(cpu, ctx->cpu);
327
328 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800329 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800330 rq->csd.info = rq;
331 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100332 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800333 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800334 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800335 }
Jens Axboe320ae512013-10-24 09:20:05 +0100336 put_cpu();
337}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800338
Jens Axboeed851862014-05-30 21:20:50 -0600339void __blk_mq_complete_request(struct request *rq)
340{
341 struct request_queue *q = rq->q;
342
343 if (!q->softirq_done_fn)
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700344 blk_mq_end_request(rq, rq->errors);
Jens Axboeed851862014-05-30 21:20:50 -0600345 else
346 blk_mq_ipi_complete_request(rq);
347}
348
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800349/**
350 * blk_mq_complete_request - end I/O on a request
351 * @rq: the request being processed
352 *
353 * Description:
354 * Ends all I/O on a request. It does not handle partial completions.
355 * The actual completion happens out-of-order, through a IPI handler.
356 **/
357void blk_mq_complete_request(struct request *rq)
358{
Jens Axboe95f09682014-05-27 17:46:48 -0600359 struct request_queue *q = rq->q;
360
361 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800362 return;
Jens Axboeed851862014-05-30 21:20:50 -0600363 if (!blk_mark_rq_complete(rq))
364 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800365}
366EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100367
Christoph Hellwige2490072014-09-13 16:40:09 -0700368void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100369{
370 struct request_queue *q = rq->q;
371
372 trace_block_rq_issue(q, rq);
373
Christoph Hellwig742ee692014-04-14 10:30:06 +0200374 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200375 if (unlikely(blk_bidi_rq(rq)))
376 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200377
Ming Lei2b8393b2014-06-10 00:16:41 +0800378 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600379
380 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600381 * Ensure that ->deadline is visible before set the started
382 * flag and clear the completed flag.
383 */
384 smp_mb__before_atomic();
385
386 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600387 * 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 */
Jens Axboe4b570522014-05-29 11:00:11 -0600392 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
393 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
394 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
395 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800396
397 if (q->dma_drain_size && blk_rq_bytes(rq)) {
398 /*
399 * Make sure space for the drain appears. We know we can do
400 * this because max_hw_segments has been adjusted to be one
401 * fewer than the device can handle.
402 */
403 rq->nr_phys_segments++;
404 }
Jens Axboe320ae512013-10-24 09:20:05 +0100405}
Christoph Hellwige2490072014-09-13 16:40:09 -0700406EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100407
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200408static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100409{
410 struct request_queue *q = rq->q;
411
412 trace_block_rq_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800413
Christoph Hellwige2490072014-09-13 16:40:09 -0700414 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
415 if (q->dma_drain_size && blk_rq_bytes(rq))
416 rq->nr_phys_segments--;
417 }
Jens Axboe320ae512013-10-24 09:20:05 +0100418}
419
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200420void blk_mq_requeue_request(struct request *rq)
421{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200422 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200423
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200424 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600425 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200426}
427EXPORT_SYMBOL(blk_mq_requeue_request);
428
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600429static void blk_mq_requeue_work(struct work_struct *work)
430{
431 struct request_queue *q =
432 container_of(work, struct request_queue, requeue_work);
433 LIST_HEAD(rq_list);
434 struct request *rq, *next;
435 unsigned long flags;
436
437 spin_lock_irqsave(&q->requeue_lock, flags);
438 list_splice_init(&q->requeue_list, &rq_list);
439 spin_unlock_irqrestore(&q->requeue_lock, flags);
440
441 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
442 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
443 continue;
444
445 rq->cmd_flags &= ~REQ_SOFTBARRIER;
446 list_del_init(&rq->queuelist);
447 blk_mq_insert_request(rq, true, false, false);
448 }
449
450 while (!list_empty(&rq_list)) {
451 rq = list_entry(rq_list.next, struct request, queuelist);
452 list_del_init(&rq->queuelist);
453 blk_mq_insert_request(rq, false, false, false);
454 }
455
Jens Axboe8b957412014-09-19 13:10:29 -0600456 /*
457 * Use the start variant of queue running here, so that running
458 * the requeue work will kick stopped queues.
459 */
460 blk_mq_start_hw_queues(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600461}
462
463void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
464{
465 struct request_queue *q = rq->q;
466 unsigned long flags;
467
468 /*
469 * We abuse this flag that is otherwise used by the I/O scheduler to
470 * request head insertation from the workqueue.
471 */
472 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
473
474 spin_lock_irqsave(&q->requeue_lock, flags);
475 if (at_head) {
476 rq->cmd_flags |= REQ_SOFTBARRIER;
477 list_add(&rq->queuelist, &q->requeue_list);
478 } else {
479 list_add_tail(&rq->queuelist, &q->requeue_list);
480 }
481 spin_unlock_irqrestore(&q->requeue_lock, flags);
482}
483EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
484
485void blk_mq_kick_requeue_list(struct request_queue *q)
486{
487 kblockd_schedule_work(&q->requeue_work);
488}
489EXPORT_SYMBOL(blk_mq_kick_requeue_list);
490
Ming Lei7c94e1c2014-09-25 23:23:43 +0800491static inline bool is_flush_request(struct request *rq,
492 struct blk_flush_queue *fq, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600493{
Jens Axboe0e62f512014-06-04 10:23:49 -0600494 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
Ming Lei7c94e1c2014-09-25 23:23:43 +0800495 fq->flush_rq->tag == tag);
Jens Axboe0e62f512014-06-04 10:23:49 -0600496}
Shaohua Li22302372014-05-30 08:06:42 -0600497
Jens Axboe0e62f512014-06-04 10:23:49 -0600498struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
499{
500 struct request *rq = tags->rqs[tag];
Ming Leie97c2932014-09-25 23:23:46 +0800501 /* mq_ctx of flush rq is always cloned from the corresponding req */
502 struct blk_flush_queue *fq = blk_get_flush_queue(rq->q, rq->mq_ctx);
Shaohua Li22302372014-05-30 08:06:42 -0600503
Ming Lei7c94e1c2014-09-25 23:23:43 +0800504 if (!is_flush_request(rq, fq, tag))
Jens Axboe0e62f512014-06-04 10:23:49 -0600505 return rq;
506
Ming Lei7c94e1c2014-09-25 23:23:43 +0800507 return fq->flush_rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600508}
509EXPORT_SYMBOL(blk_mq_tag_to_rq);
510
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700511struct blk_mq_timeout_data {
512 unsigned long next;
513 unsigned int next_set;
514};
515
Christoph Hellwig90415832014-09-22 10:21:48 -0600516void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe87ee7b12014-04-24 08:51:47 -0600517{
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700518 struct blk_mq_ops *ops = req->q->mq_ops;
519 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600520
521 /*
522 * We know that complete is set at this point. If STARTED isn't set
523 * anymore, then the request isn't active and the "timeout" should
524 * just be ignored. This can happen due to the bitflag ordering.
525 * Timeout first checks if STARTED is set, and if it is, assumes
526 * the request is active. But if we race with completion, then
527 * we both flags will get cleared. So check here again, and ignore
528 * a timeout event with a request that isn't active.
529 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700530 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
531 return;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600532
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700533 if (ops->timeout)
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700534 ret = ops->timeout(req, reserved);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600535
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700536 switch (ret) {
537 case BLK_EH_HANDLED:
538 __blk_mq_complete_request(req);
539 break;
540 case BLK_EH_RESET_TIMER:
541 blk_add_timer(req);
542 blk_clear_rq_complete(req);
543 break;
544 case BLK_EH_NOT_HANDLED:
545 break;
546 default:
547 printk(KERN_ERR "block: bad eh return: %d\n", ret);
548 break;
549 }
Jens Axboe87ee7b12014-04-24 08:51:47 -0600550}
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700551
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700552static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
553 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100554{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700555 struct blk_mq_timeout_data *data = priv;
556
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700557 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
558 return;
559
560 if (time_after_eq(jiffies, rq->deadline)) {
561 if (!blk_mark_rq_complete(rq))
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700562 blk_mq_rq_timed_out(rq, reserved);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700563 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
564 data->next = rq->deadline;
565 data->next_set = 1;
566 }
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700567}
568
569static void blk_mq_rq_timer(unsigned long priv)
570{
571 struct request_queue *q = (struct request_queue *)priv;
572 struct blk_mq_timeout_data data = {
573 .next = 0,
574 .next_set = 0,
575 };
Jens Axboe320ae512013-10-24 09:20:05 +0100576 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700577 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100578
Jens Axboe484b4062014-05-21 14:01:15 -0600579 queue_for_each_hw_ctx(q, hctx, i) {
580 /*
581 * If not software queues are currently mapped to this
582 * hardware queue, there's nothing to check
583 */
584 if (!hctx->nr_ctx || !hctx->tags)
585 continue;
586
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700587 blk_mq_tag_busy_iter(hctx, blk_mq_check_expired, &data);
Jens Axboe484b4062014-05-21 14:01:15 -0600588 }
Jens Axboe320ae512013-10-24 09:20:05 +0100589
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700590 if (data.next_set) {
591 data.next = blk_rq_timeout(round_jiffies_up(data.next));
592 mod_timer(&q->timeout, data.next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600593 } else {
594 queue_for_each_hw_ctx(q, hctx, i)
595 blk_mq_tag_idle(hctx);
596 }
Jens Axboe320ae512013-10-24 09:20:05 +0100597}
598
599/*
600 * Reverse check our software queue for entries that we could potentially
601 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
602 * too much time checking for merges.
603 */
604static bool blk_mq_attempt_merge(struct request_queue *q,
605 struct blk_mq_ctx *ctx, struct bio *bio)
606{
607 struct request *rq;
608 int checked = 8;
609
610 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
611 int el_ret;
612
613 if (!checked--)
614 break;
615
616 if (!blk_rq_merge_ok(rq, bio))
617 continue;
618
619 el_ret = blk_try_merge(rq, bio);
620 if (el_ret == ELEVATOR_BACK_MERGE) {
621 if (bio_attempt_back_merge(q, rq, bio)) {
622 ctx->rq_merged++;
623 return true;
624 }
625 break;
626 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
627 if (bio_attempt_front_merge(q, rq, bio)) {
628 ctx->rq_merged++;
629 return true;
630 }
631 break;
632 }
633 }
634
635 return false;
636}
637
Jens Axboe320ae512013-10-24 09:20:05 +0100638/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600639 * Process software queues that have been marked busy, splicing them
640 * to the for-dispatch
641 */
642static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
643{
644 struct blk_mq_ctx *ctx;
645 int i;
646
647 for (i = 0; i < hctx->ctx_map.map_size; i++) {
648 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
649 unsigned int off, bit;
650
651 if (!bm->word)
652 continue;
653
654 bit = 0;
655 off = i * hctx->ctx_map.bits_per_word;
656 do {
657 bit = find_next_bit(&bm->word, bm->depth, bit);
658 if (bit >= bm->depth)
659 break;
660
661 ctx = hctx->ctxs[bit + off];
662 clear_bit(bit, &bm->word);
663 spin_lock(&ctx->lock);
664 list_splice_tail_init(&ctx->rq_list, list);
665 spin_unlock(&ctx->lock);
666
667 bit++;
668 } while (1);
669 }
670}
671
672/*
Jens Axboe320ae512013-10-24 09:20:05 +0100673 * Run this hardware queue, pulling any software queues mapped to it in.
674 * Note that this function currently has various problems around ordering
675 * of IO. In particular, we'd like FIFO behaviour on handling existing
676 * items on the hctx->dispatch list. Ignore that for now.
677 */
678static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
679{
680 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100681 struct request *rq;
682 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600683 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100684
Jens Axboefd1270d2014-04-16 09:23:48 -0600685 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600686
Jens Axboe5d12f902014-03-19 15:25:02 -0600687 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100688 return;
689
690 hctx->run++;
691
692 /*
693 * Touch any software queue that has pending entries.
694 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600695 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100696
697 /*
698 * If we have previous entries on our dispatch list, grab them
699 * and stuff them at the front for more fair dispatch.
700 */
701 if (!list_empty_careful(&hctx->dispatch)) {
702 spin_lock(&hctx->lock);
703 if (!list_empty(&hctx->dispatch))
704 list_splice_init(&hctx->dispatch, &rq_list);
705 spin_unlock(&hctx->lock);
706 }
707
708 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100709 * Now process all the entries, sending them to the driver.
710 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600711 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100712 while (!list_empty(&rq_list)) {
713 int ret;
714
715 rq = list_first_entry(&rq_list, struct request, queuelist);
716 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100717
Christoph Hellwigbf572292014-09-13 16:40:08 -0700718 ret = q->mq_ops->queue_rq(hctx, rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100719 switch (ret) {
720 case BLK_MQ_RQ_QUEUE_OK:
721 queued++;
722 continue;
723 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100724 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200725 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100726 break;
727 default:
728 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100729 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800730 rq->errors = -EIO;
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700731 blk_mq_end_request(rq, rq->errors);
Jens Axboe320ae512013-10-24 09:20:05 +0100732 break;
733 }
734
735 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
736 break;
737 }
738
739 if (!queued)
740 hctx->dispatched[0]++;
741 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
742 hctx->dispatched[ilog2(queued) + 1]++;
743
744 /*
745 * Any items that need requeuing? Stuff them into hctx->dispatch,
746 * that is where we will continue on next queue run.
747 */
748 if (!list_empty(&rq_list)) {
749 spin_lock(&hctx->lock);
750 list_splice(&rq_list, &hctx->dispatch);
751 spin_unlock(&hctx->lock);
752 }
753}
754
Jens Axboe506e9312014-05-07 10:26:44 -0600755/*
756 * It'd be great if the workqueue API had a way to pass
757 * in a mask and had some smarts for more clever placement.
758 * For now we just round-robin here, switching for every
759 * BLK_MQ_CPU_WORK_BATCH queued items.
760 */
761static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
762{
763 int cpu = hctx->next_cpu;
764
765 if (--hctx->next_cpu_batch <= 0) {
766 int next_cpu;
767
768 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
769 if (next_cpu >= nr_cpu_ids)
770 next_cpu = cpumask_first(hctx->cpumask);
771
772 hctx->next_cpu = next_cpu;
773 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
774 }
775
776 return cpu;
777}
778
Jens Axboe320ae512013-10-24 09:20:05 +0100779void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
780{
Jens Axboe5d12f902014-03-19 15:25:02 -0600781 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100782 return;
783
Jens Axboee4043dc2014-04-09 10:18:23 -0600784 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100785 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600786 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600787 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600788 else {
789 unsigned int cpu;
790
Jens Axboe506e9312014-05-07 10:26:44 -0600791 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600792 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600793 }
Jens Axboe320ae512013-10-24 09:20:05 +0100794}
795
796void blk_mq_run_queues(struct request_queue *q, bool async)
797{
798 struct blk_mq_hw_ctx *hctx;
799 int i;
800
801 queue_for_each_hw_ctx(q, hctx, i) {
802 if ((!blk_mq_hctx_has_pending(hctx) &&
803 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600804 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100805 continue;
806
Jens Axboee4043dc2014-04-09 10:18:23 -0600807 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100808 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600809 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100810 }
811}
812EXPORT_SYMBOL(blk_mq_run_queues);
813
814void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
815{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600816 cancel_delayed_work(&hctx->run_work);
817 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100818 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
819}
820EXPORT_SYMBOL(blk_mq_stop_hw_queue);
821
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100822void blk_mq_stop_hw_queues(struct request_queue *q)
823{
824 struct blk_mq_hw_ctx *hctx;
825 int i;
826
827 queue_for_each_hw_ctx(q, hctx, i)
828 blk_mq_stop_hw_queue(hctx);
829}
830EXPORT_SYMBOL(blk_mq_stop_hw_queues);
831
Jens Axboe320ae512013-10-24 09:20:05 +0100832void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
833{
834 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600835
836 preempt_disable();
Jens Axboe0ffbce82014-06-25 08:22:34 -0600837 blk_mq_run_hw_queue(hctx, false);
Jens Axboee4043dc2014-04-09 10:18:23 -0600838 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100839}
840EXPORT_SYMBOL(blk_mq_start_hw_queue);
841
Christoph Hellwig2f268552014-04-16 09:44:56 +0200842void blk_mq_start_hw_queues(struct request_queue *q)
843{
844 struct blk_mq_hw_ctx *hctx;
845 int i;
846
847 queue_for_each_hw_ctx(q, hctx, i)
848 blk_mq_start_hw_queue(hctx);
849}
850EXPORT_SYMBOL(blk_mq_start_hw_queues);
851
852
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200853void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100854{
855 struct blk_mq_hw_ctx *hctx;
856 int i;
857
858 queue_for_each_hw_ctx(q, hctx, i) {
859 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
860 continue;
861
862 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600863 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200864 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600865 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100866 }
867}
868EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
869
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600870static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100871{
872 struct blk_mq_hw_ctx *hctx;
873
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600874 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600875
Jens Axboe320ae512013-10-24 09:20:05 +0100876 __blk_mq_run_hw_queue(hctx);
877}
878
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600879static void blk_mq_delay_work_fn(struct work_struct *work)
880{
881 struct blk_mq_hw_ctx *hctx;
882
883 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
884
885 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
886 __blk_mq_run_hw_queue(hctx);
887}
888
889void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
890{
891 unsigned long tmo = msecs_to_jiffies(msecs);
892
893 if (hctx->queue->nr_hw_queues == 1)
894 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
895 else {
896 unsigned int cpu;
897
Jens Axboe506e9312014-05-07 10:26:44 -0600898 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600899 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
900 }
901}
902EXPORT_SYMBOL(blk_mq_delay_queue);
903
Jens Axboe320ae512013-10-24 09:20:05 +0100904static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800905 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100906{
907 struct blk_mq_ctx *ctx = rq->mq_ctx;
908
Jens Axboe01b983c2013-11-19 18:59:10 -0700909 trace_block_rq_insert(hctx->queue, rq);
910
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800911 if (at_head)
912 list_add(&rq->queuelist, &ctx->rq_list);
913 else
914 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600915
Jens Axboe320ae512013-10-24 09:20:05 +0100916 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100917}
918
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600919void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
920 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100921{
922 struct request_queue *q = rq->q;
923 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600924 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100925
926 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600927 if (!cpu_online(ctx->cpu))
928 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100929
Jens Axboe320ae512013-10-24 09:20:05 +0100930 hctx = q->mq_ops->map_queue(q, ctx->cpu);
931
Christoph Hellwiga57a1782014-09-16 14:44:07 -0700932 spin_lock(&ctx->lock);
933 __blk_mq_insert_request(hctx, rq, at_head);
934 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100935
Jens Axboe320ae512013-10-24 09:20:05 +0100936 if (run_queue)
937 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600938
939 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100940}
941
942static void blk_mq_insert_requests(struct request_queue *q,
943 struct blk_mq_ctx *ctx,
944 struct list_head *list,
945 int depth,
946 bool from_schedule)
947
948{
949 struct blk_mq_hw_ctx *hctx;
950 struct blk_mq_ctx *current_ctx;
951
952 trace_block_unplug(q, depth, !from_schedule);
953
954 current_ctx = blk_mq_get_ctx(q);
955
956 if (!cpu_online(ctx->cpu))
957 ctx = current_ctx;
958 hctx = q->mq_ops->map_queue(q, ctx->cpu);
959
960 /*
961 * preemption doesn't flush plug list, so it's possible ctx->cpu is
962 * offline now
963 */
964 spin_lock(&ctx->lock);
965 while (!list_empty(list)) {
966 struct request *rq;
967
968 rq = list_first_entry(list, struct request, queuelist);
969 list_del_init(&rq->queuelist);
970 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800971 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100972 }
973 spin_unlock(&ctx->lock);
974
Jens Axboe320ae512013-10-24 09:20:05 +0100975 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600976 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100977}
978
979static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
980{
981 struct request *rqa = container_of(a, struct request, queuelist);
982 struct request *rqb = container_of(b, struct request, queuelist);
983
984 return !(rqa->mq_ctx < rqb->mq_ctx ||
985 (rqa->mq_ctx == rqb->mq_ctx &&
986 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
987}
988
989void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
990{
991 struct blk_mq_ctx *this_ctx;
992 struct request_queue *this_q;
993 struct request *rq;
994 LIST_HEAD(list);
995 LIST_HEAD(ctx_list);
996 unsigned int depth;
997
998 list_splice_init(&plug->mq_list, &list);
999
1000 list_sort(NULL, &list, plug_ctx_cmp);
1001
1002 this_q = NULL;
1003 this_ctx = NULL;
1004 depth = 0;
1005
1006 while (!list_empty(&list)) {
1007 rq = list_entry_rq(list.next);
1008 list_del_init(&rq->queuelist);
1009 BUG_ON(!rq->q);
1010 if (rq->mq_ctx != this_ctx) {
1011 if (this_ctx) {
1012 blk_mq_insert_requests(this_q, this_ctx,
1013 &ctx_list, depth,
1014 from_schedule);
1015 }
1016
1017 this_ctx = rq->mq_ctx;
1018 this_q = rq->q;
1019 depth = 0;
1020 }
1021
1022 depth++;
1023 list_add_tail(&rq->queuelist, &ctx_list);
1024 }
1025
1026 /*
1027 * If 'this_ctx' is set, we know we have entries to complete
1028 * on 'ctx_list'. Do those.
1029 */
1030 if (this_ctx) {
1031 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1032 from_schedule);
1033 }
1034}
1035
1036static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1037{
1038 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001039
Jens Axboe3ee32372014-06-09 09:36:53 -06001040 if (blk_do_io_stat(rq))
Jens Axboe4b570522014-05-29 11:00:11 -06001041 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001042}
1043
Jens Axboe274a5842014-08-15 12:44:08 -06001044static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1045{
1046 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1047 !blk_queue_nomerges(hctx->queue);
1048}
1049
Jens Axboe07068d52014-05-22 10:40:51 -06001050static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1051 struct blk_mq_ctx *ctx,
1052 struct request *rq, struct bio *bio)
1053{
Jens Axboe274a5842014-08-15 12:44:08 -06001054 if (!hctx_allow_merges(hctx)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001055 blk_mq_bio_to_request(rq, bio);
1056 spin_lock(&ctx->lock);
1057insert_rq:
1058 __blk_mq_insert_request(hctx, rq, false);
1059 spin_unlock(&ctx->lock);
1060 return false;
1061 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001062 struct request_queue *q = hctx->queue;
1063
Jens Axboe07068d52014-05-22 10:40:51 -06001064 spin_lock(&ctx->lock);
1065 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1066 blk_mq_bio_to_request(rq, bio);
1067 goto insert_rq;
1068 }
1069
1070 spin_unlock(&ctx->lock);
1071 __blk_mq_free_request(hctx, ctx, rq);
1072 return true;
1073 }
1074}
1075
1076struct blk_map_ctx {
1077 struct blk_mq_hw_ctx *hctx;
1078 struct blk_mq_ctx *ctx;
1079};
1080
1081static struct request *blk_mq_map_request(struct request_queue *q,
1082 struct bio *bio,
1083 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001084{
1085 struct blk_mq_hw_ctx *hctx;
1086 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001087 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001088 int rw = bio_data_dir(bio);
Ming Leicb96a422014-06-01 00:43:37 +08001089 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001090
Jens Axboe07068d52014-05-22 10:40:51 -06001091 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001092 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001093 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001094 }
1095
1096 ctx = blk_mq_get_ctx(q);
1097 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1098
Jens Axboe07068d52014-05-22 10:40:51 -06001099 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001100 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001101
Jens Axboe320ae512013-10-24 09:20:05 +01001102 trace_block_getrq(q, bio, rw);
Ming Leicb96a422014-06-01 00:43:37 +08001103 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1104 hctx);
1105 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001106 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001107 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001108 blk_mq_put_ctx(ctx);
1109 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001110
1111 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001112 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +08001113 blk_mq_set_alloc_data(&alloc_data, q,
1114 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1115 rq = __blk_mq_alloc_request(&alloc_data, rw);
1116 ctx = alloc_data.ctx;
1117 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001118 }
1119
1120 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001121 data->hctx = hctx;
1122 data->ctx = ctx;
1123 return rq;
1124}
1125
1126/*
1127 * Multiple hardware queue variant. This will not use per-process plugs,
1128 * but will attempt to bypass the hctx queueing if we can go straight to
1129 * hardware for SYNC IO.
1130 */
1131static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1132{
1133 const int is_sync = rw_is_sync(bio->bi_rw);
1134 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1135 struct blk_map_ctx data;
1136 struct request *rq;
1137
1138 blk_queue_bounce(q, &bio);
1139
1140 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1141 bio_endio(bio, -EIO);
1142 return;
1143 }
1144
1145 rq = blk_mq_map_request(q, bio, &data);
1146 if (unlikely(!rq))
1147 return;
1148
1149 if (unlikely(is_flush_fua)) {
1150 blk_mq_bio_to_request(rq, bio);
1151 blk_insert_flush(rq);
1152 goto run_queue;
1153 }
1154
1155 if (is_sync) {
1156 int ret;
1157
1158 blk_mq_bio_to_request(rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001159
1160 /*
1161 * For OK queue, we are done. For error, kill it. Any other
1162 * error (busy), just add it to our list as we previously
1163 * would have done
1164 */
Christoph Hellwigbf572292014-09-13 16:40:08 -07001165 ret = q->mq_ops->queue_rq(data.hctx, rq, true);
Jens Axboe07068d52014-05-22 10:40:51 -06001166 if (ret == BLK_MQ_RQ_QUEUE_OK)
1167 goto done;
1168 else {
1169 __blk_mq_requeue_request(rq);
1170
1171 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1172 rq->errors = -EIO;
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07001173 blk_mq_end_request(rq, rq->errors);
Jens Axboe07068d52014-05-22 10:40:51 -06001174 goto done;
1175 }
1176 }
1177 }
1178
1179 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1180 /*
1181 * For a SYNC request, send it to the hardware immediately. For
1182 * an ASYNC request, just ensure that we run it later on. The
1183 * latter allows for merging opportunities and more efficient
1184 * dispatching.
1185 */
1186run_queue:
1187 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1188 }
1189done:
1190 blk_mq_put_ctx(data.ctx);
1191}
1192
1193/*
1194 * Single hardware queue variant. This will attempt to use any per-process
1195 * plug for merging and IO deferral.
1196 */
1197static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1198{
1199 const int is_sync = rw_is_sync(bio->bi_rw);
1200 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1201 unsigned int use_plug, request_count = 0;
1202 struct blk_map_ctx data;
1203 struct request *rq;
1204
1205 /*
1206 * If we have multiple hardware queues, just go directly to
1207 * one of those for sync IO.
1208 */
1209 use_plug = !is_flush_fua && !is_sync;
1210
1211 blk_queue_bounce(q, &bio);
1212
1213 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1214 bio_endio(bio, -EIO);
1215 return;
1216 }
1217
1218 if (use_plug && !blk_queue_nomerges(q) &&
1219 blk_attempt_plug_merge(q, bio, &request_count))
1220 return;
1221
1222 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001223 if (unlikely(!rq))
1224 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001225
1226 if (unlikely(is_flush_fua)) {
1227 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001228 blk_insert_flush(rq);
1229 goto run_queue;
1230 }
1231
1232 /*
1233 * A task plug currently exists. Since this is completely lockless,
1234 * utilize that to temporarily store requests until the task is
1235 * either done or scheduled away.
1236 */
1237 if (use_plug) {
1238 struct blk_plug *plug = current->plug;
1239
1240 if (plug) {
1241 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001242 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001243 trace_block_plug(q);
1244 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1245 blk_flush_plug_list(plug, false);
1246 trace_block_plug(q);
1247 }
1248 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001249 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001250 return;
1251 }
1252 }
1253
Jens Axboe07068d52014-05-22 10:40:51 -06001254 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1255 /*
1256 * For a SYNC request, send it to the hardware immediately. For
1257 * an ASYNC request, just ensure that we run it later on. The
1258 * latter allows for merging opportunities and more efficient
1259 * dispatching.
1260 */
1261run_queue:
1262 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001263 }
1264
Jens Axboe07068d52014-05-22 10:40:51 -06001265 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001266}
1267
1268/*
1269 * Default mapping to a software queue, since we use one per CPU.
1270 */
1271struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1272{
1273 return q->queue_hw_ctx[q->mq_map[cpu]];
1274}
1275EXPORT_SYMBOL(blk_mq_map_queue);
1276
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001277static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1278 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001279{
1280 struct page *page;
1281
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001282 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001283 int i;
1284
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001285 for (i = 0; i < tags->nr_tags; i++) {
1286 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001287 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001288 set->ops->exit_request(set->driver_data, tags->rqs[i],
1289 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001290 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001291 }
1292 }
1293
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001294 while (!list_empty(&tags->page_list)) {
1295 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001296 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001297 __free_pages(page, page->private);
1298 }
1299
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001300 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001301
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001302 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001303}
1304
1305static size_t order_to_size(unsigned int order)
1306{
Ming Lei4ca08502014-04-19 18:00:18 +08001307 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001308}
1309
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001310static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1311 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001312{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001313 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001314 unsigned int i, j, entries_per_page, max_order = 4;
1315 size_t rq_size, left;
1316
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001317 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1318 set->numa_node);
1319 if (!tags)
1320 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001321
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001322 INIT_LIST_HEAD(&tags->page_list);
1323
Jens Axboea5164402014-09-10 09:02:03 -06001324 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1325 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1326 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001327 if (!tags->rqs) {
1328 blk_mq_free_tags(tags);
1329 return NULL;
1330 }
Jens Axboe320ae512013-10-24 09:20:05 +01001331
1332 /*
1333 * rq_size is the size of the request plus driver payload, rounded
1334 * to the cacheline size
1335 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001336 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001337 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001338 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001339
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001340 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001341 int this_order = max_order;
1342 struct page *page;
1343 int to_do;
1344 void *p;
1345
1346 while (left < order_to_size(this_order - 1) && this_order)
1347 this_order--;
1348
1349 do {
Jens Axboea5164402014-09-10 09:02:03 -06001350 page = alloc_pages_node(set->numa_node,
1351 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1352 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001353 if (page)
1354 break;
1355 if (!this_order--)
1356 break;
1357 if (order_to_size(this_order) < rq_size)
1358 break;
1359 } while (1);
1360
1361 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001362 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001363
1364 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001365 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001366
1367 p = page_address(page);
1368 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001369 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001370 left -= to_do * rq_size;
1371 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001372 tags->rqs[i] = p;
David Hildenbrand683d0e12014-09-18 11:04:31 +02001373 tags->rqs[i]->atomic_flags = 0;
1374 tags->rqs[i]->cmd_flags = 0;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001375 if (set->ops->init_request) {
1376 if (set->ops->init_request(set->driver_data,
1377 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001378 set->numa_node)) {
1379 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001380 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001381 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001382 }
1383
Jens Axboe320ae512013-10-24 09:20:05 +01001384 p += rq_size;
1385 i++;
1386 }
1387 }
1388
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001389 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001390
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001391fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001392 blk_mq_free_rq_map(set, tags, hctx_idx);
1393 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001394}
1395
Jens Axboe1429d7c2014-05-19 09:23:55 -06001396static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1397{
1398 kfree(bitmap->map);
1399}
1400
1401static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1402{
1403 unsigned int bpw = 8, total, num_maps, i;
1404
1405 bitmap->bits_per_word = bpw;
1406
1407 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1408 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1409 GFP_KERNEL, node);
1410 if (!bitmap->map)
1411 return -ENOMEM;
1412
1413 bitmap->map_size = num_maps;
1414
1415 total = nr_cpu_ids;
1416 for (i = 0; i < num_maps; i++) {
1417 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1418 total -= bitmap->map[i].depth;
1419 }
1420
1421 return 0;
1422}
1423
Jens Axboe484b4062014-05-21 14:01:15 -06001424static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1425{
1426 struct request_queue *q = hctx->queue;
1427 struct blk_mq_ctx *ctx;
1428 LIST_HEAD(tmp);
1429
1430 /*
1431 * Move ctx entries to new CPU, if this one is going away.
1432 */
1433 ctx = __blk_mq_get_ctx(q, cpu);
1434
1435 spin_lock(&ctx->lock);
1436 if (!list_empty(&ctx->rq_list)) {
1437 list_splice_init(&ctx->rq_list, &tmp);
1438 blk_mq_hctx_clear_pending(hctx, ctx);
1439 }
1440 spin_unlock(&ctx->lock);
1441
1442 if (list_empty(&tmp))
1443 return NOTIFY_OK;
1444
1445 ctx = blk_mq_get_ctx(q);
1446 spin_lock(&ctx->lock);
1447
1448 while (!list_empty(&tmp)) {
1449 struct request *rq;
1450
1451 rq = list_first_entry(&tmp, struct request, queuelist);
1452 rq->mq_ctx = ctx;
1453 list_move_tail(&rq->queuelist, &ctx->rq_list);
1454 }
1455
1456 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1457 blk_mq_hctx_mark_pending(hctx, ctx);
1458
1459 spin_unlock(&ctx->lock);
1460
1461 blk_mq_run_hw_queue(hctx, true);
1462 blk_mq_put_ctx(ctx);
1463 return NOTIFY_OK;
1464}
1465
1466static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1467{
1468 struct request_queue *q = hctx->queue;
1469 struct blk_mq_tag_set *set = q->tag_set;
1470
1471 if (set->tags[hctx->queue_num])
1472 return NOTIFY_OK;
1473
1474 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1475 if (!set->tags[hctx->queue_num])
1476 return NOTIFY_STOP;
1477
1478 hctx->tags = set->tags[hctx->queue_num];
1479 return NOTIFY_OK;
1480}
1481
1482static int blk_mq_hctx_notify(void *data, unsigned long action,
1483 unsigned int cpu)
1484{
1485 struct blk_mq_hw_ctx *hctx = data;
1486
1487 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1488 return blk_mq_hctx_cpu_offline(hctx, cpu);
1489 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1490 return blk_mq_hctx_cpu_online(hctx, cpu);
1491
1492 return NOTIFY_OK;
1493}
1494
Ming Lei08e98fc2014-09-25 23:23:38 +08001495static void blk_mq_exit_hctx(struct request_queue *q,
1496 struct blk_mq_tag_set *set,
1497 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1498{
Ming Leif70ced02014-09-25 23:23:47 +08001499 unsigned flush_start_tag = set->queue_depth;
1500
Ming Lei08e98fc2014-09-25 23:23:38 +08001501 blk_mq_tag_idle(hctx);
1502
Ming Leif70ced02014-09-25 23:23:47 +08001503 if (set->ops->exit_request)
1504 set->ops->exit_request(set->driver_data,
1505 hctx->fq->flush_rq, hctx_idx,
1506 flush_start_tag + hctx_idx);
1507
Ming Lei08e98fc2014-09-25 23:23:38 +08001508 if (set->ops->exit_hctx)
1509 set->ops->exit_hctx(hctx, hctx_idx);
1510
1511 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Ming Leif70ced02014-09-25 23:23:47 +08001512 blk_free_flush_queue(hctx->fq);
Ming Lei08e98fc2014-09-25 23:23:38 +08001513 kfree(hctx->ctxs);
1514 blk_mq_free_bitmap(&hctx->ctx_map);
1515}
1516
Ming Lei624dbe42014-05-27 23:35:13 +08001517static void blk_mq_exit_hw_queues(struct request_queue *q,
1518 struct blk_mq_tag_set *set, int nr_queue)
1519{
1520 struct blk_mq_hw_ctx *hctx;
1521 unsigned int i;
1522
1523 queue_for_each_hw_ctx(q, hctx, i) {
1524 if (i == nr_queue)
1525 break;
Ming Lei08e98fc2014-09-25 23:23:38 +08001526 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08001527 }
Ming Lei624dbe42014-05-27 23:35:13 +08001528}
1529
1530static void blk_mq_free_hw_queues(struct request_queue *q,
1531 struct blk_mq_tag_set *set)
1532{
1533 struct blk_mq_hw_ctx *hctx;
1534 unsigned int i;
1535
1536 queue_for_each_hw_ctx(q, hctx, i) {
1537 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001538 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001539 }
1540}
1541
Ming Lei08e98fc2014-09-25 23:23:38 +08001542static int blk_mq_init_hctx(struct request_queue *q,
1543 struct blk_mq_tag_set *set,
1544 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1545{
1546 int node;
Ming Leif70ced02014-09-25 23:23:47 +08001547 unsigned flush_start_tag = set->queue_depth;
Ming Lei08e98fc2014-09-25 23:23:38 +08001548
1549 node = hctx->numa_node;
1550 if (node == NUMA_NO_NODE)
1551 node = hctx->numa_node = set->numa_node;
1552
1553 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1554 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1555 spin_lock_init(&hctx->lock);
1556 INIT_LIST_HEAD(&hctx->dispatch);
1557 hctx->queue = q;
1558 hctx->queue_num = hctx_idx;
1559 hctx->flags = set->flags;
1560 hctx->cmd_size = set->cmd_size;
1561
1562 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1563 blk_mq_hctx_notify, hctx);
1564 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1565
1566 hctx->tags = set->tags[hctx_idx];
1567
1568 /*
1569 * Allocate space for all possible cpus to avoid allocation at
1570 * runtime
1571 */
1572 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1573 GFP_KERNEL, node);
1574 if (!hctx->ctxs)
1575 goto unregister_cpu_notifier;
1576
1577 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1578 goto free_ctxs;
1579
1580 hctx->nr_ctx = 0;
1581
1582 if (set->ops->init_hctx &&
1583 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1584 goto free_bitmap;
1585
Ming Leif70ced02014-09-25 23:23:47 +08001586 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1587 if (!hctx->fq)
1588 goto exit_hctx;
1589
1590 if (set->ops->init_request &&
1591 set->ops->init_request(set->driver_data,
1592 hctx->fq->flush_rq, hctx_idx,
1593 flush_start_tag + hctx_idx, node))
1594 goto free_fq;
1595
Ming Lei08e98fc2014-09-25 23:23:38 +08001596 return 0;
1597
Ming Leif70ced02014-09-25 23:23:47 +08001598 free_fq:
1599 kfree(hctx->fq);
1600 exit_hctx:
1601 if (set->ops->exit_hctx)
1602 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001603 free_bitmap:
1604 blk_mq_free_bitmap(&hctx->ctx_map);
1605 free_ctxs:
1606 kfree(hctx->ctxs);
1607 unregister_cpu_notifier:
1608 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1609
1610 return -1;
1611}
1612
Jens Axboe320ae512013-10-24 09:20:05 +01001613static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001614 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001615{
1616 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001617 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001618
1619 /*
1620 * Initialize hardware queues
1621 */
1622 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei08e98fc2014-09-25 23:23:38 +08001623 if (blk_mq_init_hctx(q, set, hctx, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001624 break;
1625 }
1626
1627 if (i == q->nr_hw_queues)
1628 return 0;
1629
1630 /*
1631 * Init failed
1632 */
Ming Lei624dbe42014-05-27 23:35:13 +08001633 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001634
1635 return 1;
1636}
1637
1638static void blk_mq_init_cpu_queues(struct request_queue *q,
1639 unsigned int nr_hw_queues)
1640{
1641 unsigned int i;
1642
1643 for_each_possible_cpu(i) {
1644 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1645 struct blk_mq_hw_ctx *hctx;
1646
1647 memset(__ctx, 0, sizeof(*__ctx));
1648 __ctx->cpu = i;
1649 spin_lock_init(&__ctx->lock);
1650 INIT_LIST_HEAD(&__ctx->rq_list);
1651 __ctx->queue = q;
1652
1653 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001654 if (!cpu_online(i))
1655 continue;
1656
Jens Axboee4043dc2014-04-09 10:18:23 -06001657 hctx = q->mq_ops->map_queue(q, i);
1658 cpumask_set_cpu(i, hctx->cpumask);
1659 hctx->nr_ctx++;
1660
Jens Axboe320ae512013-10-24 09:20:05 +01001661 /*
1662 * Set local node, IFF we have more than one hw queue. If
1663 * not, we remain on the home node of the device
1664 */
1665 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1666 hctx->numa_node = cpu_to_node(i);
1667 }
1668}
1669
1670static void blk_mq_map_swqueue(struct request_queue *q)
1671{
1672 unsigned int i;
1673 struct blk_mq_hw_ctx *hctx;
1674 struct blk_mq_ctx *ctx;
1675
1676 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001677 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001678 hctx->nr_ctx = 0;
1679 }
1680
1681 /*
1682 * Map software to hardware queues
1683 */
1684 queue_for_each_ctx(q, ctx, i) {
1685 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001686 if (!cpu_online(i))
1687 continue;
1688
Jens Axboe320ae512013-10-24 09:20:05 +01001689 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001690 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001691 ctx->index_hw = hctx->nr_ctx;
1692 hctx->ctxs[hctx->nr_ctx++] = ctx;
1693 }
Jens Axboe506e9312014-05-07 10:26:44 -06001694
1695 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001696 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001697 * If no software queues are mapped to this hardware queue,
1698 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06001699 */
1700 if (!hctx->nr_ctx) {
1701 struct blk_mq_tag_set *set = q->tag_set;
1702
1703 if (set->tags[i]) {
1704 blk_mq_free_rq_map(set, set->tags[i], i);
1705 set->tags[i] = NULL;
1706 hctx->tags = NULL;
1707 }
1708 continue;
1709 }
1710
1711 /*
1712 * Initialize batch roundrobin counts
1713 */
Jens Axboe506e9312014-05-07 10:26:44 -06001714 hctx->next_cpu = cpumask_first(hctx->cpumask);
1715 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1716 }
Jens Axboe320ae512013-10-24 09:20:05 +01001717}
1718
Jens Axboe0d2602c2014-05-13 15:10:52 -06001719static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1720{
1721 struct blk_mq_hw_ctx *hctx;
1722 struct request_queue *q;
1723 bool shared;
1724 int i;
1725
1726 if (set->tag_list.next == set->tag_list.prev)
1727 shared = false;
1728 else
1729 shared = true;
1730
1731 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1732 blk_mq_freeze_queue(q);
1733
1734 queue_for_each_hw_ctx(q, hctx, i) {
1735 if (shared)
1736 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1737 else
1738 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1739 }
1740 blk_mq_unfreeze_queue(q);
1741 }
1742}
1743
1744static void blk_mq_del_queue_tag_set(struct request_queue *q)
1745{
1746 struct blk_mq_tag_set *set = q->tag_set;
1747
Jens Axboe0d2602c2014-05-13 15:10:52 -06001748 mutex_lock(&set->tag_list_lock);
1749 list_del_init(&q->tag_set_list);
1750 blk_mq_update_tag_set_depth(set);
1751 mutex_unlock(&set->tag_list_lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001752}
1753
1754static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1755 struct request_queue *q)
1756{
1757 q->tag_set = set;
1758
1759 mutex_lock(&set->tag_list_lock);
1760 list_add_tail(&q->tag_set_list, &set->tag_list);
1761 blk_mq_update_tag_set_depth(set);
1762 mutex_unlock(&set->tag_list_lock);
1763}
1764
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001765struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001766{
1767 struct blk_mq_hw_ctx **hctxs;
Ming Leie6cdb092014-06-03 11:24:06 +08001768 struct blk_mq_ctx __percpu *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001769 struct request_queue *q;
Jens Axboef14bbe72014-05-27 12:06:53 -06001770 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001771 int i;
1772
Jens Axboe320ae512013-10-24 09:20:05 +01001773 ctx = alloc_percpu(struct blk_mq_ctx);
1774 if (!ctx)
1775 return ERR_PTR(-ENOMEM);
1776
Jens Axboeaedcd722014-09-17 08:27:03 -06001777 /*
1778 * If a crashdump is active, then we are potentially in a very
1779 * memory constrained environment. Limit us to 1 queue and
1780 * 64 tags to prevent using too much memory.
1781 */
1782 if (is_kdump_kernel()) {
1783 set->nr_hw_queues = 1;
1784 set->queue_depth = min(64U, set->queue_depth);
1785 }
1786
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001787 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1788 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001789
1790 if (!hctxs)
1791 goto err_percpu;
1792
Jens Axboef14bbe72014-05-27 12:06:53 -06001793 map = blk_mq_make_queue_map(set);
1794 if (!map)
1795 goto err_map;
1796
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001797 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001798 int node = blk_mq_hw_queue_to_node(map, i);
1799
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001800 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1801 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001802 if (!hctxs[i])
1803 goto err_hctxs;
1804
Jens Axboea86073e2014-10-13 15:41:54 -06001805 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
1806 node))
Jens Axboee4043dc2014-04-09 10:18:23 -06001807 goto err_hctxs;
1808
Jens Axboe0d2602c2014-05-13 15:10:52 -06001809 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001810 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001811 hctxs[i]->queue_num = i;
1812 }
1813
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001814 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001815 if (!q)
1816 goto err_hctxs;
1817
Tejun Heoadd703f2014-07-01 10:34:38 -06001818 if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release))
Ming Lei3d2936f2014-05-27 23:35:14 +08001819 goto err_map;
1820
Jens Axboe320ae512013-10-24 09:20:05 +01001821 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1822 blk_queue_rq_timeout(q, 30000);
1823
1824 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001825 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001826 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001827
1828 q->queue_ctx = ctx;
1829 q->queue_hw_ctx = hctxs;
1830
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001831 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001832 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001833
Jens Axboe05f1dd52014-05-29 09:53:32 -06001834 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1835 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1836
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001837 q->sg_reserved_size = INT_MAX;
1838
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001839 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1840 INIT_LIST_HEAD(&q->requeue_list);
1841 spin_lock_init(&q->requeue_lock);
1842
Jens Axboe07068d52014-05-22 10:40:51 -06001843 if (q->nr_hw_queues > 1)
1844 blk_queue_make_request(q, blk_mq_make_request);
1845 else
1846 blk_queue_make_request(q, blk_sq_make_request);
1847
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001848 if (set->timeout)
1849 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001850
Jens Axboeeba71762014-05-20 15:17:27 -06001851 /*
1852 * Do this after blk_queue_make_request() overrides it...
1853 */
1854 q->nr_requests = set->queue_depth;
1855
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001856 if (set->ops->complete)
1857 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001858
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001859 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001860
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001861 if (blk_mq_init_hw_queues(q, set))
Ming Lei1bcb1ea2014-09-25 23:23:39 +08001862 goto err_hw;
Christoph Hellwig18741982014-02-10 09:29:00 -07001863
Jens Axboe320ae512013-10-24 09:20:05 +01001864 mutex_lock(&all_q_mutex);
1865 list_add_tail(&q->all_q_node, &all_q_list);
1866 mutex_unlock(&all_q_mutex);
1867
Jens Axboe0d2602c2014-05-13 15:10:52 -06001868 blk_mq_add_queue_tag_set(set, q);
1869
Jens Axboe484b4062014-05-21 14:01:15 -06001870 blk_mq_map_swqueue(q);
1871
Jens Axboe320ae512013-10-24 09:20:05 +01001872 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001873
Jens Axboe320ae512013-10-24 09:20:05 +01001874err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001875 blk_cleanup_queue(q);
1876err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001877 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001878 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001879 if (!hctxs[i])
1880 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001881 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001882 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001883 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001884err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001885 kfree(hctxs);
1886err_percpu:
1887 free_percpu(ctx);
1888 return ERR_PTR(-ENOMEM);
1889}
1890EXPORT_SYMBOL(blk_mq_init_queue);
1891
1892void blk_mq_free_queue(struct request_queue *q)
1893{
Ming Lei624dbe42014-05-27 23:35:13 +08001894 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001895
Jens Axboe0d2602c2014-05-13 15:10:52 -06001896 blk_mq_del_queue_tag_set(q);
1897
Ming Lei624dbe42014-05-27 23:35:13 +08001898 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1899 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001900
Tejun Heoadd703f2014-07-01 10:34:38 -06001901 percpu_ref_exit(&q->mq_usage_counter);
Ming Lei3d2936f2014-05-27 23:35:14 +08001902
Jens Axboe320ae512013-10-24 09:20:05 +01001903 free_percpu(q->queue_ctx);
1904 kfree(q->queue_hw_ctx);
1905 kfree(q->mq_map);
1906
1907 q->queue_ctx = NULL;
1908 q->queue_hw_ctx = NULL;
1909 q->mq_map = NULL;
1910
1911 mutex_lock(&all_q_mutex);
1912 list_del_init(&q->all_q_node);
1913 mutex_unlock(&all_q_mutex);
1914}
Jens Axboe320ae512013-10-24 09:20:05 +01001915
1916/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001917static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001918{
1919 blk_mq_freeze_queue(q);
1920
Jens Axboe67aec142014-05-30 08:25:36 -06001921 blk_mq_sysfs_unregister(q);
1922
Jens Axboe320ae512013-10-24 09:20:05 +01001923 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1924
1925 /*
1926 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1927 * we should change hctx numa_node according to new topology (this
1928 * involves free and re-allocate memory, worthy doing?)
1929 */
1930
1931 blk_mq_map_swqueue(q);
1932
Jens Axboe67aec142014-05-30 08:25:36 -06001933 blk_mq_sysfs_register(q);
1934
Jens Axboe320ae512013-10-24 09:20:05 +01001935 blk_mq_unfreeze_queue(q);
1936}
1937
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001938static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1939 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001940{
1941 struct request_queue *q;
1942
1943 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001944 * Before new mappings are established, hotadded cpu might already
1945 * start handling requests. This doesn't break anything as we map
1946 * offline CPUs to first hardware queue. We will re-init the queue
1947 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001948 */
1949 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1950 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1951 return NOTIFY_OK;
1952
1953 mutex_lock(&all_q_mutex);
1954 list_for_each_entry(q, &all_q_list, all_q_node)
1955 blk_mq_queue_reinit(q);
1956 mutex_unlock(&all_q_mutex);
1957 return NOTIFY_OK;
1958}
1959
Jens Axboea5164402014-09-10 09:02:03 -06001960static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1961{
1962 int i;
1963
1964 for (i = 0; i < set->nr_hw_queues; i++) {
1965 set->tags[i] = blk_mq_init_rq_map(set, i);
1966 if (!set->tags[i])
1967 goto out_unwind;
1968 }
1969
1970 return 0;
1971
1972out_unwind:
1973 while (--i >= 0)
1974 blk_mq_free_rq_map(set, set->tags[i], i);
1975
Jens Axboea5164402014-09-10 09:02:03 -06001976 return -ENOMEM;
1977}
1978
1979/*
1980 * Allocate the request maps associated with this tag_set. Note that this
1981 * may reduce the depth asked for, if memory is tight. set->queue_depth
1982 * will be updated to reflect the allocated depth.
1983 */
1984static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1985{
1986 unsigned int depth;
1987 int err;
1988
1989 depth = set->queue_depth;
1990 do {
1991 err = __blk_mq_alloc_rq_maps(set);
1992 if (!err)
1993 break;
1994
1995 set->queue_depth >>= 1;
1996 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
1997 err = -ENOMEM;
1998 break;
1999 }
2000 } while (set->queue_depth);
2001
2002 if (!set->queue_depth || err) {
2003 pr_err("blk-mq: failed to allocate request map\n");
2004 return -ENOMEM;
2005 }
2006
2007 if (depth != set->queue_depth)
2008 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2009 depth, set->queue_depth);
2010
2011 return 0;
2012}
2013
Jens Axboea4391c62014-06-05 15:21:56 -06002014/*
2015 * Alloc a tag set to be associated with one or more request queues.
2016 * May fail with EINVAL for various error conditions. May adjust the
2017 * requested depth down, if if it too large. In that case, the set
2018 * value will be stored in set->queue_depth.
2019 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002020int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2021{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002022 if (!set->nr_hw_queues)
2023 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002024 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002025 return -EINVAL;
2026 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2027 return -EINVAL;
2028
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02002029 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002030 return -EINVAL;
2031
Jens Axboea4391c62014-06-05 15:21:56 -06002032 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2033 pr_info("blk-mq: reduced tag depth to %u\n",
2034 BLK_MQ_MAX_DEPTH);
2035 set->queue_depth = BLK_MQ_MAX_DEPTH;
2036 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002037
Ming Lei48479002014-04-19 18:00:17 +08002038 set->tags = kmalloc_node(set->nr_hw_queues *
2039 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002040 GFP_KERNEL, set->numa_node);
2041 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002042 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002043
Jens Axboea5164402014-09-10 09:02:03 -06002044 if (blk_mq_alloc_rq_maps(set))
2045 goto enomem;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002046
Jens Axboe0d2602c2014-05-13 15:10:52 -06002047 mutex_init(&set->tag_list_lock);
2048 INIT_LIST_HEAD(&set->tag_list);
2049
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002050 return 0;
Jens Axboea5164402014-09-10 09:02:03 -06002051enomem:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002052 kfree(set->tags);
2053 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002054 return -ENOMEM;
2055}
2056EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2057
2058void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2059{
2060 int i;
2061
Jens Axboe484b4062014-05-21 14:01:15 -06002062 for (i = 0; i < set->nr_hw_queues; i++) {
2063 if (set->tags[i])
2064 blk_mq_free_rq_map(set, set->tags[i], i);
2065 }
2066
Ming Lei981bd182014-04-24 00:07:34 +08002067 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002068 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002069}
2070EXPORT_SYMBOL(blk_mq_free_tag_set);
2071
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002072int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2073{
2074 struct blk_mq_tag_set *set = q->tag_set;
2075 struct blk_mq_hw_ctx *hctx;
2076 int i, ret;
2077
2078 if (!set || nr > set->queue_depth)
2079 return -EINVAL;
2080
2081 ret = 0;
2082 queue_for_each_hw_ctx(q, hctx, i) {
2083 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2084 if (ret)
2085 break;
2086 }
2087
2088 if (!ret)
2089 q->nr_requests = nr;
2090
2091 return ret;
2092}
2093
Jens Axboe676141e2014-03-20 13:29:18 -06002094void blk_mq_disable_hotplug(void)
2095{
2096 mutex_lock(&all_q_mutex);
2097}
2098
2099void blk_mq_enable_hotplug(void)
2100{
2101 mutex_unlock(&all_q_mutex);
2102}
2103
Jens Axboe320ae512013-10-24 09:20:05 +01002104static int __init blk_mq_init(void)
2105{
Jens Axboe320ae512013-10-24 09:20:05 +01002106 blk_mq_cpu_init();
2107
Tejun Heoadd703f2014-07-01 10:34:38 -06002108 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002109
2110 return 0;
2111}
2112subsys_initcall(blk_mq_init);