blob: df8e1e09dd172d9ab67dbd0a91e519c5c01919ef [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>
23
24#include <trace/events/block.h>
25
26#include <linux/blk-mq.h>
27#include "blk.h"
28#include "blk-mq.h"
29#include "blk-mq-tag.h"
30
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
34static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
35
Jens Axboe320ae512013-10-24 09:20:05 +010036/*
37 * Check if any of the ctx's have pending work in this hardware queue
38 */
39static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
40{
41 unsigned int i;
42
Jens Axboe1429d7c2014-05-19 09:23:55 -060043 for (i = 0; i < hctx->ctx_map.map_size; i++)
44 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010045 return true;
46
47 return false;
48}
49
Jens Axboe1429d7c2014-05-19 09:23:55 -060050static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
51 struct blk_mq_ctx *ctx)
52{
53 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
54}
55
56#define CTX_TO_BIT(hctx, ctx) \
57 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
58
Jens Axboe320ae512013-10-24 09:20:05 +010059/*
60 * Mark this ctx as having pending work in this hardware queue
61 */
62static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
63 struct blk_mq_ctx *ctx)
64{
Jens Axboe1429d7c2014-05-19 09:23:55 -060065 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
66
67 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
68 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
69}
70
71static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
72 struct blk_mq_ctx *ctx)
73{
74 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
75
76 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010077}
78
Jens Axboe320ae512013-10-24 09:20:05 +010079static int blk_mq_queue_enter(struct request_queue *q)
80{
Tejun Heoadd703f2014-07-01 10:34:38 -060081 while (true) {
82 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +010083
Tejun Heoadd703f2014-07-01 10:34:38 -060084 if (percpu_ref_tryget_live(&q->mq_usage_counter))
85 return 0;
Keith Busch3b632cf2014-06-06 10:22:07 -060086
Tejun Heoadd703f2014-07-01 10:34:38 -060087 ret = wait_event_interruptible(q->mq_freeze_wq,
88 !q->mq_freeze_depth || blk_queue_dying(q));
89 if (blk_queue_dying(q))
90 return -ENODEV;
91 if (ret)
92 return ret;
93 }
Jens Axboe320ae512013-10-24 09:20:05 +010094}
95
96static void blk_mq_queue_exit(struct request_queue *q)
97{
Tejun Heoadd703f2014-07-01 10:34:38 -060098 percpu_ref_put(&q->mq_usage_counter);
99}
100
101static void blk_mq_usage_counter_release(struct percpu_ref *ref)
102{
103 struct request_queue *q =
104 container_of(ref, struct request_queue, mq_usage_counter);
105
106 wake_up_all(&q->mq_freeze_wq);
Jens Axboe320ae512013-10-24 09:20:05 +0100107}
108
Tejun Heo72d6f022014-07-01 10:33:02 -0600109/*
110 * Guarantee no request is in use, so we can change any data structure of
111 * the queue afterward.
112 */
113void blk_mq_freeze_queue(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800114{
Tejun Heocddd5d12014-08-16 08:02:24 -0400115 bool freeze;
116
Tejun Heo72d6f022014-07-01 10:33:02 -0600117 spin_lock_irq(q->queue_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400118 freeze = !q->mq_freeze_depth++;
Tejun Heo72d6f022014-07-01 10:33:02 -0600119 spin_unlock_irq(q->queue_lock);
120
Tejun Heocddd5d12014-08-16 08:02:24 -0400121 if (freeze) {
Tejun Heo0a302882014-09-23 15:24:32 -0400122 /*
123 * XXX: Temporary kludge to work around SCSI blk-mq stall.
124 * SCSI synchronously creates and destroys many queues
125 * back-to-back during probe leading to lengthy stalls.
126 * This will be fixed by keeping ->mq_usage_counter in
127 * atomic mode until genhd registration, but, for now,
128 * let's work around using expedited synchronization.
129 */
130 __percpu_ref_kill_expedited(&q->mq_usage_counter);
131
Tejun Heocddd5d12014-08-16 08:02:24 -0400132 blk_mq_run_queues(q, false);
133 }
Tejun Heoadd703f2014-07-01 10:34:38 -0600134 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800135}
136
Jens Axboe320ae512013-10-24 09:20:05 +0100137static void blk_mq_unfreeze_queue(struct request_queue *q)
138{
Tejun Heocddd5d12014-08-16 08:02:24 -0400139 bool wake;
Jens Axboe320ae512013-10-24 09:20:05 +0100140
141 spin_lock_irq(q->queue_lock);
Tejun Heo780db202014-07-01 10:31:13 -0600142 wake = !--q->mq_freeze_depth;
143 WARN_ON_ONCE(q->mq_freeze_depth < 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100144 spin_unlock_irq(q->queue_lock);
Tejun Heoadd703f2014-07-01 10:34:38 -0600145 if (wake) {
146 percpu_ref_reinit(&q->mq_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100147 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600148 }
Jens Axboe320ae512013-10-24 09:20:05 +0100149}
150
151bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
152{
153 return blk_mq_has_free_tags(hctx->tags);
154}
155EXPORT_SYMBOL(blk_mq_can_queue);
156
Jens Axboe94eddfb2013-11-19 09:25:07 -0700157static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
158 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100159{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700160 if (blk_queue_io_stat(q))
161 rw_flags |= REQ_IO_STAT;
162
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200163 INIT_LIST_HEAD(&rq->queuelist);
164 /* csd/requeue_work/fifo_time is initialized before use */
165 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100166 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600167 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200168 /* do not touch atomic flags, it needs atomic ops against the timer */
169 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200170 INIT_HLIST_NODE(&rq->hash);
171 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200172 rq->rq_disk = NULL;
173 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600174 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200175#ifdef CONFIG_BLK_CGROUP
176 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700177 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200178 rq->io_start_time_ns = 0;
179#endif
180 rq->nr_phys_segments = 0;
181#if defined(CONFIG_BLK_DEV_INTEGRITY)
182 rq->nr_integrity_segments = 0;
183#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200184 rq->special = NULL;
185 /* tag was already set */
186 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200187
Tony Battersby6f4a16262014-08-22 15:53:39 -0400188 rq->cmd = rq->__cmd;
189
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200190 rq->extra_len = 0;
191 rq->sense_len = 0;
192 rq->resid_len = 0;
193 rq->sense = NULL;
194
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200195 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600196 rq->timeout = 0;
197
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200198 rq->end_io = NULL;
199 rq->end_io_data = NULL;
200 rq->next_rq = NULL;
201
Jens Axboe320ae512013-10-24 09:20:05 +0100202 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
203}
204
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200205static struct request *
Ming Leicb96a422014-06-01 00:43:37 +0800206__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200207{
208 struct request *rq;
209 unsigned int tag;
210
Ming Leicb96a422014-06-01 00:43:37 +0800211 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200212 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a422014-06-01 00:43:37 +0800213 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200214
Ming Leicb96a422014-06-01 00:43:37 +0800215 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200216 rq->cmd_flags = REQ_MQ_INFLIGHT;
Ming Leicb96a422014-06-01 00:43:37 +0800217 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200218 }
219
220 rq->tag = tag;
Ming Leicb96a422014-06-01 00:43:37 +0800221 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200222 return rq;
223 }
224
225 return NULL;
226}
227
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200228struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
229 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100230{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200231 struct blk_mq_ctx *ctx;
232 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100233 struct request *rq;
Ming Leicb96a422014-06-01 00:43:37 +0800234 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +0100235
236 if (blk_mq_queue_enter(q))
237 return NULL;
238
Christoph Hellwigd8525642014-05-27 20:59:50 +0200239 ctx = blk_mq_get_ctx(q);
240 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800241 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
242 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200243
Ming Leicb96a422014-06-01 00:43:37 +0800244 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200245 if (!rq && (gfp & __GFP_WAIT)) {
246 __blk_mq_run_hw_queue(hctx);
247 blk_mq_put_ctx(ctx);
248
249 ctx = blk_mq_get_ctx(q);
250 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800251 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
252 hctx);
253 rq = __blk_mq_alloc_request(&alloc_data, rw);
254 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200255 }
256 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100257 return rq;
258}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600259EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100260
Jens Axboe320ae512013-10-24 09:20:05 +0100261static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
262 struct blk_mq_ctx *ctx, struct request *rq)
263{
264 const int tag = rq->tag;
265 struct request_queue *q = rq->q;
266
Jens Axboe0d2602c2014-05-13 15:10:52 -0600267 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
268 atomic_dec(&hctx->nr_active);
David Hildenbrand683d0e12014-09-18 11:04:31 +0200269 rq->cmd_flags = 0;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600270
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200271 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600272 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100273 blk_mq_queue_exit(q);
274}
275
276void blk_mq_free_request(struct request *rq)
277{
278 struct blk_mq_ctx *ctx = rq->mq_ctx;
279 struct blk_mq_hw_ctx *hctx;
280 struct request_queue *q = rq->q;
281
282 ctx->rq_completed[rq_is_sync(rq)]++;
283
284 hctx = q->mq_ops->map_queue(q, ctx->cpu);
285 __blk_mq_free_request(hctx, ctx, rq);
286}
287
Christoph Hellwig8727af42014-04-14 10:30:08 +0200288/*
289 * Clone all relevant state from a request that has been put on hold in
290 * the flush state machine into the preallocated flush request that hangs
291 * off the request queue.
292 *
293 * For a driver the flush request should be invisible, that's why we are
294 * impersonating the original request here.
295 */
296void blk_mq_clone_flush_request(struct request *flush_rq,
297 struct request *orig_rq)
298{
299 struct blk_mq_hw_ctx *hctx =
300 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
301
302 flush_rq->mq_ctx = orig_rq->mq_ctx;
303 flush_rq->tag = orig_rq->tag;
304 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
305 hctx->cmd_size);
306}
307
Christoph Hellwig63151a42014-04-16 09:44:52 +0200308inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100309{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700310 blk_account_io_done(rq);
311
Christoph Hellwig91b63632014-04-16 09:44:53 +0200312 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100313 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200314 } else {
315 if (unlikely(blk_bidi_rq(rq)))
316 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100317 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200318 }
Jens Axboe320ae512013-10-24 09:20:05 +0100319}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200320EXPORT_SYMBOL(__blk_mq_end_io);
321
322void blk_mq_end_io(struct request *rq, int error)
323{
324 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
325 BUG();
326 __blk_mq_end_io(rq, error);
327}
328EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100329
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800330static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100331{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800332 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100333
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800334 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100335}
336
Jens Axboeed851862014-05-30 21:20:50 -0600337static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100338{
339 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700340 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100341 int cpu;
342
Christoph Hellwig38535202014-04-25 02:32:53 -0700343 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800344 rq->q->softirq_done_fn(rq);
345 return;
346 }
Jens Axboe320ae512013-10-24 09:20:05 +0100347
348 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700349 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
350 shared = cpus_share_cache(cpu, ctx->cpu);
351
352 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800353 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800354 rq->csd.info = rq;
355 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100356 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800357 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800358 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800359 }
Jens Axboe320ae512013-10-24 09:20:05 +0100360 put_cpu();
361}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800362
Jens Axboeed851862014-05-30 21:20:50 -0600363void __blk_mq_complete_request(struct request *rq)
364{
365 struct request_queue *q = rq->q;
366
367 if (!q->softirq_done_fn)
368 blk_mq_end_io(rq, rq->errors);
369 else
370 blk_mq_ipi_complete_request(rq);
371}
372
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800373/**
374 * blk_mq_complete_request - end I/O on a request
375 * @rq: the request being processed
376 *
377 * Description:
378 * Ends all I/O on a request. It does not handle partial completions.
379 * The actual completion happens out-of-order, through a IPI handler.
380 **/
381void blk_mq_complete_request(struct request *rq)
382{
Jens Axboe95f09682014-05-27 17:46:48 -0600383 struct request_queue *q = rq->q;
384
385 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800386 return;
Jens Axboeed851862014-05-30 21:20:50 -0600387 if (!blk_mark_rq_complete(rq))
388 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800389}
390EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100391
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800392static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100393{
394 struct request_queue *q = rq->q;
395
396 trace_block_rq_issue(q, rq);
397
Christoph Hellwig742ee692014-04-14 10:30:06 +0200398 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200399 if (unlikely(blk_bidi_rq(rq)))
400 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200401
Ming Lei2b8393b2014-06-10 00:16:41 +0800402 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600403
404 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600405 * Ensure that ->deadline is visible before set the started
406 * flag and clear the completed flag.
407 */
408 smp_mb__before_atomic();
409
410 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600411 * Mark us as started and clear complete. Complete might have been
412 * set if requeue raced with timeout, which then marked it as
413 * complete. So be sure to clear complete again when we start
414 * the request, otherwise we'll ignore the completion event.
415 */
Jens Axboe4b570522014-05-29 11:00:11 -0600416 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
417 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
418 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
419 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800420
421 if (q->dma_drain_size && blk_rq_bytes(rq)) {
422 /*
423 * Make sure space for the drain appears. We know we can do
424 * this because max_hw_segments has been adjusted to be one
425 * fewer than the device can handle.
426 */
427 rq->nr_phys_segments++;
428 }
429
430 /*
431 * Flag the last request in the series so that drivers know when IO
432 * should be kicked off, if they don't do it on a per-request basis.
433 *
434 * Note: the flag isn't the only condition drivers should do kick off.
435 * If drive is busy, the last request might not have the bit set.
436 */
437 if (last)
438 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100439}
440
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200441static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100442{
443 struct request_queue *q = rq->q;
444
445 trace_block_rq_requeue(q, rq);
446 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800447
448 rq->cmd_flags &= ~REQ_END;
449
450 if (q->dma_drain_size && blk_rq_bytes(rq))
451 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100452}
453
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200454void blk_mq_requeue_request(struct request *rq)
455{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200456 __blk_mq_requeue_request(rq);
457 blk_clear_rq_complete(rq);
458
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200459 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600460 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200461}
462EXPORT_SYMBOL(blk_mq_requeue_request);
463
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600464static void blk_mq_requeue_work(struct work_struct *work)
465{
466 struct request_queue *q =
467 container_of(work, struct request_queue, requeue_work);
468 LIST_HEAD(rq_list);
469 struct request *rq, *next;
470 unsigned long flags;
471
472 spin_lock_irqsave(&q->requeue_lock, flags);
473 list_splice_init(&q->requeue_list, &rq_list);
474 spin_unlock_irqrestore(&q->requeue_lock, flags);
475
476 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
477 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
478 continue;
479
480 rq->cmd_flags &= ~REQ_SOFTBARRIER;
481 list_del_init(&rq->queuelist);
482 blk_mq_insert_request(rq, true, false, false);
483 }
484
485 while (!list_empty(&rq_list)) {
486 rq = list_entry(rq_list.next, struct request, queuelist);
487 list_del_init(&rq->queuelist);
488 blk_mq_insert_request(rq, false, false, false);
489 }
490
Jens Axboe8b957412014-09-19 13:10:29 -0600491 /*
492 * Use the start variant of queue running here, so that running
493 * the requeue work will kick stopped queues.
494 */
495 blk_mq_start_hw_queues(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600496}
497
498void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
499{
500 struct request_queue *q = rq->q;
501 unsigned long flags;
502
503 /*
504 * We abuse this flag that is otherwise used by the I/O scheduler to
505 * request head insertation from the workqueue.
506 */
507 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
508
509 spin_lock_irqsave(&q->requeue_lock, flags);
510 if (at_head) {
511 rq->cmd_flags |= REQ_SOFTBARRIER;
512 list_add(&rq->queuelist, &q->requeue_list);
513 } else {
514 list_add_tail(&rq->queuelist, &q->requeue_list);
515 }
516 spin_unlock_irqrestore(&q->requeue_lock, flags);
517}
518EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
519
520void blk_mq_kick_requeue_list(struct request_queue *q)
521{
522 kblockd_schedule_work(&q->requeue_work);
523}
524EXPORT_SYMBOL(blk_mq_kick_requeue_list);
525
Jens Axboe0e62f512014-06-04 10:23:49 -0600526static inline bool is_flush_request(struct request *rq, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600527{
Jens Axboe0e62f512014-06-04 10:23:49 -0600528 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
529 rq->q->flush_rq->tag == tag);
530}
Shaohua Li22302372014-05-30 08:06:42 -0600531
Jens Axboe0e62f512014-06-04 10:23:49 -0600532struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
533{
534 struct request *rq = tags->rqs[tag];
Shaohua Li22302372014-05-30 08:06:42 -0600535
Jens Axboe0e62f512014-06-04 10:23:49 -0600536 if (!is_flush_request(rq, tag))
537 return rq;
538
539 return rq->q->flush_rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600540}
541EXPORT_SYMBOL(blk_mq_tag_to_rq);
542
Jens Axboe320ae512013-10-24 09:20:05 +0100543struct blk_mq_timeout_data {
544 struct blk_mq_hw_ctx *hctx;
545 unsigned long *next;
546 unsigned int *next_set;
547};
548
549static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
550{
551 struct blk_mq_timeout_data *data = __data;
552 struct blk_mq_hw_ctx *hctx = data->hctx;
553 unsigned int tag;
554
555 /* It may not be in flight yet (this is where
556 * the REQ_ATOMIC_STARTED flag comes in). The requests are
557 * statically allocated, so we know it's always safe to access the
558 * memory associated with a bit offset into ->rqs[].
559 */
560 tag = 0;
561 do {
562 struct request *rq;
563
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600564 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
565 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100566 break;
567
Jens Axboe0e62f512014-06-04 10:23:49 -0600568 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600569 if (rq->q != hctx->queue)
570 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100571 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
572 continue;
573
574 blk_rq_check_expired(rq, data->next, data->next_set);
575 } while (1);
576}
577
578static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
579 unsigned long *next,
580 unsigned int *next_set)
581{
582 struct blk_mq_timeout_data data = {
583 .hctx = hctx,
584 .next = next,
585 .next_set = next_set,
586 };
587
588 /*
589 * Ask the tagging code to iterate busy requests, so we can
590 * check them for timeout.
591 */
592 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
593}
594
Jens Axboe87ee7b12014-04-24 08:51:47 -0600595static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
596{
597 struct request_queue *q = rq->q;
598
599 /*
600 * We know that complete is set at this point. If STARTED isn't set
601 * anymore, then the request isn't active and the "timeout" should
602 * just be ignored. This can happen due to the bitflag ordering.
603 * Timeout first checks if STARTED is set, and if it is, assumes
604 * the request is active. But if we race with completion, then
605 * we both flags will get cleared. So check here again, and ignore
606 * a timeout event with a request that isn't active.
607 */
608 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
609 return BLK_EH_NOT_HANDLED;
610
611 if (!q->mq_ops->timeout)
612 return BLK_EH_RESET_TIMER;
613
614 return q->mq_ops->timeout(rq);
615}
616
Jens Axboe320ae512013-10-24 09:20:05 +0100617static void blk_mq_rq_timer(unsigned long data)
618{
619 struct request_queue *q = (struct request_queue *) data;
620 struct blk_mq_hw_ctx *hctx;
621 unsigned long next = 0;
622 int i, next_set = 0;
623
Jens Axboe484b4062014-05-21 14:01:15 -0600624 queue_for_each_hw_ctx(q, hctx, i) {
625 /*
626 * If not software queues are currently mapped to this
627 * hardware queue, there's nothing to check
628 */
629 if (!hctx->nr_ctx || !hctx->tags)
630 continue;
631
Jens Axboe320ae512013-10-24 09:20:05 +0100632 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
Jens Axboe484b4062014-05-21 14:01:15 -0600633 }
Jens Axboe320ae512013-10-24 09:20:05 +0100634
Jens Axboe0d2602c2014-05-13 15:10:52 -0600635 if (next_set) {
636 next = blk_rq_timeout(round_jiffies_up(next));
637 mod_timer(&q->timeout, next);
638 } else {
639 queue_for_each_hw_ctx(q, hctx, i)
640 blk_mq_tag_idle(hctx);
641 }
Jens Axboe320ae512013-10-24 09:20:05 +0100642}
643
644/*
645 * Reverse check our software queue for entries that we could potentially
646 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
647 * too much time checking for merges.
648 */
649static bool blk_mq_attempt_merge(struct request_queue *q,
650 struct blk_mq_ctx *ctx, struct bio *bio)
651{
652 struct request *rq;
653 int checked = 8;
654
655 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
656 int el_ret;
657
658 if (!checked--)
659 break;
660
661 if (!blk_rq_merge_ok(rq, bio))
662 continue;
663
664 el_ret = blk_try_merge(rq, bio);
665 if (el_ret == ELEVATOR_BACK_MERGE) {
666 if (bio_attempt_back_merge(q, rq, bio)) {
667 ctx->rq_merged++;
668 return true;
669 }
670 break;
671 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
672 if (bio_attempt_front_merge(q, rq, bio)) {
673 ctx->rq_merged++;
674 return true;
675 }
676 break;
677 }
678 }
679
680 return false;
681}
682
Jens Axboe320ae512013-10-24 09:20:05 +0100683/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600684 * Process software queues that have been marked busy, splicing them
685 * to the for-dispatch
686 */
687static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
688{
689 struct blk_mq_ctx *ctx;
690 int i;
691
692 for (i = 0; i < hctx->ctx_map.map_size; i++) {
693 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
694 unsigned int off, bit;
695
696 if (!bm->word)
697 continue;
698
699 bit = 0;
700 off = i * hctx->ctx_map.bits_per_word;
701 do {
702 bit = find_next_bit(&bm->word, bm->depth, bit);
703 if (bit >= bm->depth)
704 break;
705
706 ctx = hctx->ctxs[bit + off];
707 clear_bit(bit, &bm->word);
708 spin_lock(&ctx->lock);
709 list_splice_tail_init(&ctx->rq_list, list);
710 spin_unlock(&ctx->lock);
711
712 bit++;
713 } while (1);
714 }
715}
716
717/*
Jens Axboe320ae512013-10-24 09:20:05 +0100718 * Run this hardware queue, pulling any software queues mapped to it in.
719 * Note that this function currently has various problems around ordering
720 * of IO. In particular, we'd like FIFO behaviour on handling existing
721 * items on the hctx->dispatch list. Ignore that for now.
722 */
723static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
724{
725 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100726 struct request *rq;
727 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600728 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100729
Jens Axboefd1270d2014-04-16 09:23:48 -0600730 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600731
Jens Axboe5d12f902014-03-19 15:25:02 -0600732 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100733 return;
734
735 hctx->run++;
736
737 /*
738 * Touch any software queue that has pending entries.
739 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600740 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100741
742 /*
743 * If we have previous entries on our dispatch list, grab them
744 * and stuff them at the front for more fair dispatch.
745 */
746 if (!list_empty_careful(&hctx->dispatch)) {
747 spin_lock(&hctx->lock);
748 if (!list_empty(&hctx->dispatch))
749 list_splice_init(&hctx->dispatch, &rq_list);
750 spin_unlock(&hctx->lock);
751 }
752
753 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100754 * Now process all the entries, sending them to the driver.
755 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600756 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100757 while (!list_empty(&rq_list)) {
758 int ret;
759
760 rq = list_first_entry(&rq_list, struct request, queuelist);
761 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100762
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800763 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100764
765 ret = q->mq_ops->queue_rq(hctx, rq);
766 switch (ret) {
767 case BLK_MQ_RQ_QUEUE_OK:
768 queued++;
769 continue;
770 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100771 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200772 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100773 break;
774 default:
775 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100776 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800777 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100778 blk_mq_end_io(rq, rq->errors);
779 break;
780 }
781
782 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
783 break;
784 }
785
786 if (!queued)
787 hctx->dispatched[0]++;
788 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
789 hctx->dispatched[ilog2(queued) + 1]++;
790
791 /*
792 * Any items that need requeuing? Stuff them into hctx->dispatch,
793 * that is where we will continue on next queue run.
794 */
795 if (!list_empty(&rq_list)) {
796 spin_lock(&hctx->lock);
797 list_splice(&rq_list, &hctx->dispatch);
798 spin_unlock(&hctx->lock);
799 }
800}
801
Jens Axboe506e9312014-05-07 10:26:44 -0600802/*
803 * It'd be great if the workqueue API had a way to pass
804 * in a mask and had some smarts for more clever placement.
805 * For now we just round-robin here, switching for every
806 * BLK_MQ_CPU_WORK_BATCH queued items.
807 */
808static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
809{
810 int cpu = hctx->next_cpu;
811
812 if (--hctx->next_cpu_batch <= 0) {
813 int next_cpu;
814
815 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
816 if (next_cpu >= nr_cpu_ids)
817 next_cpu = cpumask_first(hctx->cpumask);
818
819 hctx->next_cpu = next_cpu;
820 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
821 }
822
823 return cpu;
824}
825
Jens Axboe320ae512013-10-24 09:20:05 +0100826void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
827{
Jens Axboe5d12f902014-03-19 15:25:02 -0600828 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100829 return;
830
Jens Axboee4043dc2014-04-09 10:18:23 -0600831 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100832 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600833 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600834 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600835 else {
836 unsigned int cpu;
837
Jens Axboe506e9312014-05-07 10:26:44 -0600838 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600839 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600840 }
Jens Axboe320ae512013-10-24 09:20:05 +0100841}
842
843void blk_mq_run_queues(struct request_queue *q, bool async)
844{
845 struct blk_mq_hw_ctx *hctx;
846 int i;
847
848 queue_for_each_hw_ctx(q, hctx, i) {
849 if ((!blk_mq_hctx_has_pending(hctx) &&
850 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600851 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100852 continue;
853
Jens Axboee4043dc2014-04-09 10:18:23 -0600854 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100855 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600856 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100857 }
858}
859EXPORT_SYMBOL(blk_mq_run_queues);
860
861void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
862{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600863 cancel_delayed_work(&hctx->run_work);
864 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100865 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
866}
867EXPORT_SYMBOL(blk_mq_stop_hw_queue);
868
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100869void blk_mq_stop_hw_queues(struct request_queue *q)
870{
871 struct blk_mq_hw_ctx *hctx;
872 int i;
873
874 queue_for_each_hw_ctx(q, hctx, i)
875 blk_mq_stop_hw_queue(hctx);
876}
877EXPORT_SYMBOL(blk_mq_stop_hw_queues);
878
Jens Axboe320ae512013-10-24 09:20:05 +0100879void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
880{
881 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600882
883 preempt_disable();
Jens Axboe0ffbce82014-06-25 08:22:34 -0600884 blk_mq_run_hw_queue(hctx, false);
Jens Axboee4043dc2014-04-09 10:18:23 -0600885 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100886}
887EXPORT_SYMBOL(blk_mq_start_hw_queue);
888
Christoph Hellwig2f268552014-04-16 09:44:56 +0200889void blk_mq_start_hw_queues(struct request_queue *q)
890{
891 struct blk_mq_hw_ctx *hctx;
892 int i;
893
894 queue_for_each_hw_ctx(q, hctx, i)
895 blk_mq_start_hw_queue(hctx);
896}
897EXPORT_SYMBOL(blk_mq_start_hw_queues);
898
899
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200900void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100901{
902 struct blk_mq_hw_ctx *hctx;
903 int i;
904
905 queue_for_each_hw_ctx(q, hctx, i) {
906 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
907 continue;
908
909 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600910 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200911 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600912 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100913 }
914}
915EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
916
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600917static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100918{
919 struct blk_mq_hw_ctx *hctx;
920
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600921 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600922
Jens Axboe320ae512013-10-24 09:20:05 +0100923 __blk_mq_run_hw_queue(hctx);
924}
925
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600926static void blk_mq_delay_work_fn(struct work_struct *work)
927{
928 struct blk_mq_hw_ctx *hctx;
929
930 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
931
932 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
933 __blk_mq_run_hw_queue(hctx);
934}
935
936void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
937{
938 unsigned long tmo = msecs_to_jiffies(msecs);
939
940 if (hctx->queue->nr_hw_queues == 1)
941 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
942 else {
943 unsigned int cpu;
944
Jens Axboe506e9312014-05-07 10:26:44 -0600945 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600946 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
947 }
948}
949EXPORT_SYMBOL(blk_mq_delay_queue);
950
Jens Axboe320ae512013-10-24 09:20:05 +0100951static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800952 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100953{
954 struct blk_mq_ctx *ctx = rq->mq_ctx;
955
Jens Axboe01b983c2013-11-19 18:59:10 -0700956 trace_block_rq_insert(hctx->queue, rq);
957
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800958 if (at_head)
959 list_add(&rq->queuelist, &ctx->rq_list);
960 else
961 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600962
Jens Axboe320ae512013-10-24 09:20:05 +0100963 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100964}
965
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600966void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
967 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100968{
969 struct request_queue *q = rq->q;
970 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600971 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100972
973 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600974 if (!cpu_online(ctx->cpu))
975 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100976
Jens Axboe320ae512013-10-24 09:20:05 +0100977 hctx = q->mq_ops->map_queue(q, ctx->cpu);
978
Christoph Hellwiga57a1782014-09-16 14:44:07 -0700979 spin_lock(&ctx->lock);
980 __blk_mq_insert_request(hctx, rq, at_head);
981 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100982
Jens Axboe320ae512013-10-24 09:20:05 +0100983 if (run_queue)
984 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600985
986 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100987}
988
989static void blk_mq_insert_requests(struct request_queue *q,
990 struct blk_mq_ctx *ctx,
991 struct list_head *list,
992 int depth,
993 bool from_schedule)
994
995{
996 struct blk_mq_hw_ctx *hctx;
997 struct blk_mq_ctx *current_ctx;
998
999 trace_block_unplug(q, depth, !from_schedule);
1000
1001 current_ctx = blk_mq_get_ctx(q);
1002
1003 if (!cpu_online(ctx->cpu))
1004 ctx = current_ctx;
1005 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1006
1007 /*
1008 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1009 * offline now
1010 */
1011 spin_lock(&ctx->lock);
1012 while (!list_empty(list)) {
1013 struct request *rq;
1014
1015 rq = list_first_entry(list, struct request, queuelist);
1016 list_del_init(&rq->queuelist);
1017 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001018 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001019 }
1020 spin_unlock(&ctx->lock);
1021
Jens Axboe320ae512013-10-24 09:20:05 +01001022 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001023 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001024}
1025
1026static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1027{
1028 struct request *rqa = container_of(a, struct request, queuelist);
1029 struct request *rqb = container_of(b, struct request, queuelist);
1030
1031 return !(rqa->mq_ctx < rqb->mq_ctx ||
1032 (rqa->mq_ctx == rqb->mq_ctx &&
1033 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1034}
1035
1036void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1037{
1038 struct blk_mq_ctx *this_ctx;
1039 struct request_queue *this_q;
1040 struct request *rq;
1041 LIST_HEAD(list);
1042 LIST_HEAD(ctx_list);
1043 unsigned int depth;
1044
1045 list_splice_init(&plug->mq_list, &list);
1046
1047 list_sort(NULL, &list, plug_ctx_cmp);
1048
1049 this_q = NULL;
1050 this_ctx = NULL;
1051 depth = 0;
1052
1053 while (!list_empty(&list)) {
1054 rq = list_entry_rq(list.next);
1055 list_del_init(&rq->queuelist);
1056 BUG_ON(!rq->q);
1057 if (rq->mq_ctx != this_ctx) {
1058 if (this_ctx) {
1059 blk_mq_insert_requests(this_q, this_ctx,
1060 &ctx_list, depth,
1061 from_schedule);
1062 }
1063
1064 this_ctx = rq->mq_ctx;
1065 this_q = rq->q;
1066 depth = 0;
1067 }
1068
1069 depth++;
1070 list_add_tail(&rq->queuelist, &ctx_list);
1071 }
1072
1073 /*
1074 * If 'this_ctx' is set, we know we have entries to complete
1075 * on 'ctx_list'. Do those.
1076 */
1077 if (this_ctx) {
1078 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1079 from_schedule);
1080 }
1081}
1082
1083static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1084{
1085 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001086
Jens Axboe3ee32372014-06-09 09:36:53 -06001087 if (blk_do_io_stat(rq))
Jens Axboe4b570522014-05-29 11:00:11 -06001088 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001089}
1090
Jens Axboe274a5842014-08-15 12:44:08 -06001091static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1092{
1093 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1094 !blk_queue_nomerges(hctx->queue);
1095}
1096
Jens Axboe07068d52014-05-22 10:40:51 -06001097static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1098 struct blk_mq_ctx *ctx,
1099 struct request *rq, struct bio *bio)
1100{
Jens Axboe274a5842014-08-15 12:44:08 -06001101 if (!hctx_allow_merges(hctx)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001102 blk_mq_bio_to_request(rq, bio);
1103 spin_lock(&ctx->lock);
1104insert_rq:
1105 __blk_mq_insert_request(hctx, rq, false);
1106 spin_unlock(&ctx->lock);
1107 return false;
1108 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001109 struct request_queue *q = hctx->queue;
1110
Jens Axboe07068d52014-05-22 10:40:51 -06001111 spin_lock(&ctx->lock);
1112 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1113 blk_mq_bio_to_request(rq, bio);
1114 goto insert_rq;
1115 }
1116
1117 spin_unlock(&ctx->lock);
1118 __blk_mq_free_request(hctx, ctx, rq);
1119 return true;
1120 }
1121}
1122
1123struct blk_map_ctx {
1124 struct blk_mq_hw_ctx *hctx;
1125 struct blk_mq_ctx *ctx;
1126};
1127
1128static struct request *blk_mq_map_request(struct request_queue *q,
1129 struct bio *bio,
1130 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001131{
1132 struct blk_mq_hw_ctx *hctx;
1133 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001134 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001135 int rw = bio_data_dir(bio);
Ming Leicb96a422014-06-01 00:43:37 +08001136 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001137
Jens Axboe07068d52014-05-22 10:40:51 -06001138 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001139 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001140 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001141 }
1142
1143 ctx = blk_mq_get_ctx(q);
1144 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1145
Jens Axboe07068d52014-05-22 10:40:51 -06001146 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001147 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001148
Jens Axboe320ae512013-10-24 09:20:05 +01001149 trace_block_getrq(q, bio, rw);
Ming Leicb96a422014-06-01 00:43:37 +08001150 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1151 hctx);
1152 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001153 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001154 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001155 blk_mq_put_ctx(ctx);
1156 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001157
1158 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001159 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +08001160 blk_mq_set_alloc_data(&alloc_data, q,
1161 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1162 rq = __blk_mq_alloc_request(&alloc_data, rw);
1163 ctx = alloc_data.ctx;
1164 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001165 }
1166
1167 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001168 data->hctx = hctx;
1169 data->ctx = ctx;
1170 return rq;
1171}
1172
1173/*
1174 * Multiple hardware queue variant. This will not use per-process plugs,
1175 * but will attempt to bypass the hctx queueing if we can go straight to
1176 * hardware for SYNC IO.
1177 */
1178static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1179{
1180 const int is_sync = rw_is_sync(bio->bi_rw);
1181 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1182 struct blk_map_ctx data;
1183 struct request *rq;
1184
1185 blk_queue_bounce(q, &bio);
1186
1187 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1188 bio_endio(bio, -EIO);
1189 return;
1190 }
1191
1192 rq = blk_mq_map_request(q, bio, &data);
1193 if (unlikely(!rq))
1194 return;
1195
1196 if (unlikely(is_flush_fua)) {
1197 blk_mq_bio_to_request(rq, bio);
1198 blk_insert_flush(rq);
1199 goto run_queue;
1200 }
1201
1202 if (is_sync) {
1203 int ret;
1204
1205 blk_mq_bio_to_request(rq, bio);
1206 blk_mq_start_request(rq, true);
1207
1208 /*
1209 * For OK queue, we are done. For error, kill it. Any other
1210 * error (busy), just add it to our list as we previously
1211 * would have done
1212 */
1213 ret = q->mq_ops->queue_rq(data.hctx, rq);
1214 if (ret == BLK_MQ_RQ_QUEUE_OK)
1215 goto done;
1216 else {
1217 __blk_mq_requeue_request(rq);
1218
1219 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1220 rq->errors = -EIO;
1221 blk_mq_end_io(rq, rq->errors);
1222 goto done;
1223 }
1224 }
1225 }
1226
1227 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1228 /*
1229 * For a SYNC request, send it to the hardware immediately. For
1230 * an ASYNC request, just ensure that we run it later on. The
1231 * latter allows for merging opportunities and more efficient
1232 * dispatching.
1233 */
1234run_queue:
1235 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1236 }
1237done:
1238 blk_mq_put_ctx(data.ctx);
1239}
1240
1241/*
1242 * Single hardware queue variant. This will attempt to use any per-process
1243 * plug for merging and IO deferral.
1244 */
1245static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1246{
1247 const int is_sync = rw_is_sync(bio->bi_rw);
1248 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1249 unsigned int use_plug, request_count = 0;
1250 struct blk_map_ctx data;
1251 struct request *rq;
1252
1253 /*
1254 * If we have multiple hardware queues, just go directly to
1255 * one of those for sync IO.
1256 */
1257 use_plug = !is_flush_fua && !is_sync;
1258
1259 blk_queue_bounce(q, &bio);
1260
1261 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1262 bio_endio(bio, -EIO);
1263 return;
1264 }
1265
1266 if (use_plug && !blk_queue_nomerges(q) &&
1267 blk_attempt_plug_merge(q, bio, &request_count))
1268 return;
1269
1270 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001271 if (unlikely(!rq))
1272 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001273
1274 if (unlikely(is_flush_fua)) {
1275 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001276 blk_insert_flush(rq);
1277 goto run_queue;
1278 }
1279
1280 /*
1281 * A task plug currently exists. Since this is completely lockless,
1282 * utilize that to temporarily store requests until the task is
1283 * either done or scheduled away.
1284 */
1285 if (use_plug) {
1286 struct blk_plug *plug = current->plug;
1287
1288 if (plug) {
1289 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001290 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001291 trace_block_plug(q);
1292 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1293 blk_flush_plug_list(plug, false);
1294 trace_block_plug(q);
1295 }
1296 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001297 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001298 return;
1299 }
1300 }
1301
Jens Axboe07068d52014-05-22 10:40:51 -06001302 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1303 /*
1304 * For a SYNC request, send it to the hardware immediately. For
1305 * an ASYNC request, just ensure that we run it later on. The
1306 * latter allows for merging opportunities and more efficient
1307 * dispatching.
1308 */
1309run_queue:
1310 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001311 }
1312
Jens Axboe07068d52014-05-22 10:40:51 -06001313 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001314}
1315
1316/*
1317 * Default mapping to a software queue, since we use one per CPU.
1318 */
1319struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1320{
1321 return q->queue_hw_ctx[q->mq_map[cpu]];
1322}
1323EXPORT_SYMBOL(blk_mq_map_queue);
1324
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001325static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1326 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001327{
1328 struct page *page;
1329
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001330 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001331 int i;
1332
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001333 for (i = 0; i < tags->nr_tags; i++) {
1334 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001335 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001336 set->ops->exit_request(set->driver_data, tags->rqs[i],
1337 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001338 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001339 }
1340 }
1341
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001342 while (!list_empty(&tags->page_list)) {
1343 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001344 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001345 __free_pages(page, page->private);
1346 }
1347
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001348 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001349
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001350 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001351}
1352
1353static size_t order_to_size(unsigned int order)
1354{
Ming Lei4ca08502014-04-19 18:00:18 +08001355 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001356}
1357
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001358static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1359 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001360{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001361 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001362 unsigned int i, j, entries_per_page, max_order = 4;
1363 size_t rq_size, left;
1364
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001365 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1366 set->numa_node);
1367 if (!tags)
1368 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001369
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001370 INIT_LIST_HEAD(&tags->page_list);
1371
Jens Axboea5164402014-09-10 09:02:03 -06001372 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1373 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1374 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001375 if (!tags->rqs) {
1376 blk_mq_free_tags(tags);
1377 return NULL;
1378 }
Jens Axboe320ae512013-10-24 09:20:05 +01001379
1380 /*
1381 * rq_size is the size of the request plus driver payload, rounded
1382 * to the cacheline size
1383 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001384 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001385 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001386 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001387
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001388 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001389 int this_order = max_order;
1390 struct page *page;
1391 int to_do;
1392 void *p;
1393
1394 while (left < order_to_size(this_order - 1) && this_order)
1395 this_order--;
1396
1397 do {
Jens Axboea5164402014-09-10 09:02:03 -06001398 page = alloc_pages_node(set->numa_node,
1399 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1400 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001401 if (page)
1402 break;
1403 if (!this_order--)
1404 break;
1405 if (order_to_size(this_order) < rq_size)
1406 break;
1407 } while (1);
1408
1409 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001410 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001411
1412 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001413 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001414
1415 p = page_address(page);
1416 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001417 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001418 left -= to_do * rq_size;
1419 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001420 tags->rqs[i] = p;
David Hildenbrand683d0e12014-09-18 11:04:31 +02001421 tags->rqs[i]->atomic_flags = 0;
1422 tags->rqs[i]->cmd_flags = 0;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001423 if (set->ops->init_request) {
1424 if (set->ops->init_request(set->driver_data,
1425 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001426 set->numa_node)) {
1427 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001428 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001429 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001430 }
1431
Jens Axboe320ae512013-10-24 09:20:05 +01001432 p += rq_size;
1433 i++;
1434 }
1435 }
1436
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001437 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001438
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001439fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001440 blk_mq_free_rq_map(set, tags, hctx_idx);
1441 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001442}
1443
Jens Axboe1429d7c2014-05-19 09:23:55 -06001444static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1445{
1446 kfree(bitmap->map);
1447}
1448
1449static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1450{
1451 unsigned int bpw = 8, total, num_maps, i;
1452
1453 bitmap->bits_per_word = bpw;
1454
1455 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1456 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1457 GFP_KERNEL, node);
1458 if (!bitmap->map)
1459 return -ENOMEM;
1460
1461 bitmap->map_size = num_maps;
1462
1463 total = nr_cpu_ids;
1464 for (i = 0; i < num_maps; i++) {
1465 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1466 total -= bitmap->map[i].depth;
1467 }
1468
1469 return 0;
1470}
1471
Jens Axboe484b4062014-05-21 14:01:15 -06001472static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1473{
1474 struct request_queue *q = hctx->queue;
1475 struct blk_mq_ctx *ctx;
1476 LIST_HEAD(tmp);
1477
1478 /*
1479 * Move ctx entries to new CPU, if this one is going away.
1480 */
1481 ctx = __blk_mq_get_ctx(q, cpu);
1482
1483 spin_lock(&ctx->lock);
1484 if (!list_empty(&ctx->rq_list)) {
1485 list_splice_init(&ctx->rq_list, &tmp);
1486 blk_mq_hctx_clear_pending(hctx, ctx);
1487 }
1488 spin_unlock(&ctx->lock);
1489
1490 if (list_empty(&tmp))
1491 return NOTIFY_OK;
1492
1493 ctx = blk_mq_get_ctx(q);
1494 spin_lock(&ctx->lock);
1495
1496 while (!list_empty(&tmp)) {
1497 struct request *rq;
1498
1499 rq = list_first_entry(&tmp, struct request, queuelist);
1500 rq->mq_ctx = ctx;
1501 list_move_tail(&rq->queuelist, &ctx->rq_list);
1502 }
1503
1504 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1505 blk_mq_hctx_mark_pending(hctx, ctx);
1506
1507 spin_unlock(&ctx->lock);
1508
1509 blk_mq_run_hw_queue(hctx, true);
1510 blk_mq_put_ctx(ctx);
1511 return NOTIFY_OK;
1512}
1513
1514static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1515{
1516 struct request_queue *q = hctx->queue;
1517 struct blk_mq_tag_set *set = q->tag_set;
1518
1519 if (set->tags[hctx->queue_num])
1520 return NOTIFY_OK;
1521
1522 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1523 if (!set->tags[hctx->queue_num])
1524 return NOTIFY_STOP;
1525
1526 hctx->tags = set->tags[hctx->queue_num];
1527 return NOTIFY_OK;
1528}
1529
1530static int blk_mq_hctx_notify(void *data, unsigned long action,
1531 unsigned int cpu)
1532{
1533 struct blk_mq_hw_ctx *hctx = data;
1534
1535 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1536 return blk_mq_hctx_cpu_offline(hctx, cpu);
1537 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1538 return blk_mq_hctx_cpu_online(hctx, cpu);
1539
1540 return NOTIFY_OK;
1541}
1542
Ming Lei624dbe42014-05-27 23:35:13 +08001543static void blk_mq_exit_hw_queues(struct request_queue *q,
1544 struct blk_mq_tag_set *set, int nr_queue)
1545{
1546 struct blk_mq_hw_ctx *hctx;
1547 unsigned int i;
1548
1549 queue_for_each_hw_ctx(q, hctx, i) {
1550 if (i == nr_queue)
1551 break;
1552
Jens Axboef899fed2014-06-04 09:11:53 -06001553 blk_mq_tag_idle(hctx);
1554
Ming Lei624dbe42014-05-27 23:35:13 +08001555 if (set->ops->exit_hctx)
1556 set->ops->exit_hctx(hctx, i);
1557
1558 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1559 kfree(hctx->ctxs);
1560 blk_mq_free_bitmap(&hctx->ctx_map);
1561 }
1562
1563}
1564
1565static void blk_mq_free_hw_queues(struct request_queue *q,
1566 struct blk_mq_tag_set *set)
1567{
1568 struct blk_mq_hw_ctx *hctx;
1569 unsigned int i;
1570
1571 queue_for_each_hw_ctx(q, hctx, i) {
1572 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001573 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001574 }
1575}
1576
Jens Axboe320ae512013-10-24 09:20:05 +01001577static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001578 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001579{
1580 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001581 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001582
1583 /*
1584 * Initialize hardware queues
1585 */
1586 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001587 int node;
1588
1589 node = hctx->numa_node;
1590 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001591 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001592
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001593 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1594 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001595 spin_lock_init(&hctx->lock);
1596 INIT_LIST_HEAD(&hctx->dispatch);
1597 hctx->queue = q;
1598 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001599 hctx->flags = set->flags;
1600 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001601
1602 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1603 blk_mq_hctx_notify, hctx);
1604 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1605
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001606 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001607
1608 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001609 * Allocate space for all possible cpus to avoid allocation at
Jens Axboe320ae512013-10-24 09:20:05 +01001610 * runtime
1611 */
1612 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1613 GFP_KERNEL, node);
1614 if (!hctx->ctxs)
1615 break;
1616
Jens Axboe1429d7c2014-05-19 09:23:55 -06001617 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001618 break;
1619
Jens Axboe320ae512013-10-24 09:20:05 +01001620 hctx->nr_ctx = 0;
1621
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001622 if (set->ops->init_hctx &&
1623 set->ops->init_hctx(hctx, set->driver_data, 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
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001777 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1778 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001779
1780 if (!hctxs)
1781 goto err_percpu;
1782
Jens Axboef14bbe72014-05-27 12:06:53 -06001783 map = blk_mq_make_queue_map(set);
1784 if (!map)
1785 goto err_map;
1786
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001787 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001788 int node = blk_mq_hw_queue_to_node(map, i);
1789
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001790 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1791 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001792 if (!hctxs[i])
1793 goto err_hctxs;
1794
Jens Axboee4043dc2014-04-09 10:18:23 -06001795 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1796 goto err_hctxs;
1797
Jens Axboe0d2602c2014-05-13 15:10:52 -06001798 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001799 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001800 hctxs[i]->queue_num = i;
1801 }
1802
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001803 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001804 if (!q)
1805 goto err_hctxs;
1806
Tejun Heoadd703f2014-07-01 10:34:38 -06001807 if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release))
Ming Lei3d2936f2014-05-27 23:35:14 +08001808 goto err_map;
1809
Jens Axboe320ae512013-10-24 09:20:05 +01001810 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1811 blk_queue_rq_timeout(q, 30000);
1812
1813 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001814 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001815 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001816
1817 q->queue_ctx = ctx;
1818 q->queue_hw_ctx = hctxs;
1819
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001820 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001821 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001822
Jens Axboe05f1dd52014-05-29 09:53:32 -06001823 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1824 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1825
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001826 q->sg_reserved_size = INT_MAX;
1827
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001828 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1829 INIT_LIST_HEAD(&q->requeue_list);
1830 spin_lock_init(&q->requeue_lock);
1831
Jens Axboe07068d52014-05-22 10:40:51 -06001832 if (q->nr_hw_queues > 1)
1833 blk_queue_make_request(q, blk_mq_make_request);
1834 else
1835 blk_queue_make_request(q, blk_sq_make_request);
1836
Jens Axboe87ee7b12014-04-24 08:51:47 -06001837 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001838 if (set->timeout)
1839 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001840
Jens Axboeeba71762014-05-20 15:17:27 -06001841 /*
1842 * Do this after blk_queue_make_request() overrides it...
1843 */
1844 q->nr_requests = set->queue_depth;
1845
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001846 if (set->ops->complete)
1847 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001848
Jens Axboe320ae512013-10-24 09:20:05 +01001849 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001850 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001851
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001852 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1853 set->cmd_size, cache_line_size()),
1854 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001855 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001856 goto err_hw;
1857
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001858 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001859 goto err_flush_rq;
1860
Jens Axboe320ae512013-10-24 09:20:05 +01001861 mutex_lock(&all_q_mutex);
1862 list_add_tail(&q->all_q_node, &all_q_list);
1863 mutex_unlock(&all_q_mutex);
1864
Jens Axboe0d2602c2014-05-13 15:10:52 -06001865 blk_mq_add_queue_tag_set(set, q);
1866
Jens Axboe484b4062014-05-21 14:01:15 -06001867 blk_mq_map_swqueue(q);
1868
Jens Axboe320ae512013-10-24 09:20:05 +01001869 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001870
1871err_flush_rq:
1872 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001873err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001874 blk_cleanup_queue(q);
1875err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001876 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001877 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001878 if (!hctxs[i])
1879 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001880 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001881 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001882 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001883err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001884 kfree(hctxs);
1885err_percpu:
1886 free_percpu(ctx);
1887 return ERR_PTR(-ENOMEM);
1888}
1889EXPORT_SYMBOL(blk_mq_init_queue);
1890
1891void blk_mq_free_queue(struct request_queue *q)
1892{
Ming Lei624dbe42014-05-27 23:35:13 +08001893 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001894
Jens Axboe0d2602c2014-05-13 15:10:52 -06001895 blk_mq_del_queue_tag_set(q);
1896
Ming Lei624dbe42014-05-27 23:35:13 +08001897 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1898 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001899
Tejun Heoadd703f2014-07-01 10:34:38 -06001900 percpu_ref_exit(&q->mq_usage_counter);
Ming Lei3d2936f2014-05-27 23:35:14 +08001901
Jens Axboe320ae512013-10-24 09:20:05 +01001902 free_percpu(q->queue_ctx);
1903 kfree(q->queue_hw_ctx);
1904 kfree(q->mq_map);
1905
1906 q->queue_ctx = NULL;
1907 q->queue_hw_ctx = NULL;
1908 q->mq_map = NULL;
1909
1910 mutex_lock(&all_q_mutex);
1911 list_del_init(&q->all_q_node);
1912 mutex_unlock(&all_q_mutex);
1913}
Jens Axboe320ae512013-10-24 09:20:05 +01001914
1915/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001916static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001917{
1918 blk_mq_freeze_queue(q);
1919
Jens Axboe67aec142014-05-30 08:25:36 -06001920 blk_mq_sysfs_unregister(q);
1921
Jens Axboe320ae512013-10-24 09:20:05 +01001922 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1923
1924 /*
1925 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1926 * we should change hctx numa_node according to new topology (this
1927 * involves free and re-allocate memory, worthy doing?)
1928 */
1929
1930 blk_mq_map_swqueue(q);
1931
Jens Axboe67aec142014-05-30 08:25:36 -06001932 blk_mq_sysfs_register(q);
1933
Jens Axboe320ae512013-10-24 09:20:05 +01001934 blk_mq_unfreeze_queue(q);
1935}
1936
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001937static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1938 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001939{
1940 struct request_queue *q;
1941
1942 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001943 * Before new mappings are established, hotadded cpu might already
1944 * start handling requests. This doesn't break anything as we map
1945 * offline CPUs to first hardware queue. We will re-init the queue
1946 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001947 */
1948 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1949 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1950 return NOTIFY_OK;
1951
1952 mutex_lock(&all_q_mutex);
1953 list_for_each_entry(q, &all_q_list, all_q_node)
1954 blk_mq_queue_reinit(q);
1955 mutex_unlock(&all_q_mutex);
1956 return NOTIFY_OK;
1957}
1958
Jens Axboea5164402014-09-10 09:02:03 -06001959static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1960{
1961 int i;
1962
1963 for (i = 0; i < set->nr_hw_queues; i++) {
1964 set->tags[i] = blk_mq_init_rq_map(set, i);
1965 if (!set->tags[i])
1966 goto out_unwind;
1967 }
1968
1969 return 0;
1970
1971out_unwind:
1972 while (--i >= 0)
1973 blk_mq_free_rq_map(set, set->tags[i], i);
1974
Jens Axboea5164402014-09-10 09:02:03 -06001975 return -ENOMEM;
1976}
1977
1978/*
1979 * Allocate the request maps associated with this tag_set. Note that this
1980 * may reduce the depth asked for, if memory is tight. set->queue_depth
1981 * will be updated to reflect the allocated depth.
1982 */
1983static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1984{
1985 unsigned int depth;
1986 int err;
1987
1988 depth = set->queue_depth;
1989 do {
1990 err = __blk_mq_alloc_rq_maps(set);
1991 if (!err)
1992 break;
1993
1994 set->queue_depth >>= 1;
1995 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
1996 err = -ENOMEM;
1997 break;
1998 }
1999 } while (set->queue_depth);
2000
2001 if (!set->queue_depth || err) {
2002 pr_err("blk-mq: failed to allocate request map\n");
2003 return -ENOMEM;
2004 }
2005
2006 if (depth != set->queue_depth)
2007 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2008 depth, set->queue_depth);
2009
2010 return 0;
2011}
2012
Jens Axboea4391c62014-06-05 15:21:56 -06002013/*
2014 * Alloc a tag set to be associated with one or more request queues.
2015 * May fail with EINVAL for various error conditions. May adjust the
2016 * requested depth down, if if it too large. In that case, the set
2017 * value will be stored in set->queue_depth.
2018 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002019int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2020{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002021 if (!set->nr_hw_queues)
2022 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002023 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002024 return -EINVAL;
2025 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2026 return -EINVAL;
2027
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02002028 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002029 return -EINVAL;
2030
Jens Axboea4391c62014-06-05 15:21:56 -06002031 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2032 pr_info("blk-mq: reduced tag depth to %u\n",
2033 BLK_MQ_MAX_DEPTH);
2034 set->queue_depth = BLK_MQ_MAX_DEPTH;
2035 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002036
Ming Lei48479002014-04-19 18:00:17 +08002037 set->tags = kmalloc_node(set->nr_hw_queues *
2038 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002039 GFP_KERNEL, set->numa_node);
2040 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002041 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002042
Jens Axboea5164402014-09-10 09:02:03 -06002043 if (blk_mq_alloc_rq_maps(set))
2044 goto enomem;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002045
Jens Axboe0d2602c2014-05-13 15:10:52 -06002046 mutex_init(&set->tag_list_lock);
2047 INIT_LIST_HEAD(&set->tag_list);
2048
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002049 return 0;
Jens Axboea5164402014-09-10 09:02:03 -06002050enomem:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002051 kfree(set->tags);
2052 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002053 return -ENOMEM;
2054}
2055EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2056
2057void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2058{
2059 int i;
2060
Jens Axboe484b4062014-05-21 14:01:15 -06002061 for (i = 0; i < set->nr_hw_queues; i++) {
2062 if (set->tags[i])
2063 blk_mq_free_rq_map(set, set->tags[i], i);
2064 }
2065
Ming Lei981bd182014-04-24 00:07:34 +08002066 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002067 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002068}
2069EXPORT_SYMBOL(blk_mq_free_tag_set);
2070
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002071int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2072{
2073 struct blk_mq_tag_set *set = q->tag_set;
2074 struct blk_mq_hw_ctx *hctx;
2075 int i, ret;
2076
2077 if (!set || nr > set->queue_depth)
2078 return -EINVAL;
2079
2080 ret = 0;
2081 queue_for_each_hw_ctx(q, hctx, i) {
2082 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2083 if (ret)
2084 break;
2085 }
2086
2087 if (!ret)
2088 q->nr_requests = nr;
2089
2090 return ret;
2091}
2092
Jens Axboe676141e2014-03-20 13:29:18 -06002093void blk_mq_disable_hotplug(void)
2094{
2095 mutex_lock(&all_q_mutex);
2096}
2097
2098void blk_mq_enable_hotplug(void)
2099{
2100 mutex_unlock(&all_q_mutex);
2101}
2102
Jens Axboe320ae512013-10-24 09:20:05 +01002103static int __init blk_mq_init(void)
2104{
Jens Axboe320ae512013-10-24 09:20:05 +01002105 blk_mq_cpu_init();
2106
Tejun Heoadd703f2014-07-01 10:34:38 -06002107 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002108
2109 return 0;
2110}
2111subsys_initcall(blk_mq_init);