blob: a13c40ca8230bfce6c953c33294810b7a9b1ea20 [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) {
122 percpu_ref_kill(&q->mq_usage_counter);
123 blk_mq_run_queues(q, false);
124 }
Tejun Heoadd703f2014-07-01 10:34:38 -0600125 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800126}
127
Jens Axboe320ae512013-10-24 09:20:05 +0100128static void blk_mq_unfreeze_queue(struct request_queue *q)
129{
Tejun Heocddd5d12014-08-16 08:02:24 -0400130 bool wake;
Jens Axboe320ae512013-10-24 09:20:05 +0100131
132 spin_lock_irq(q->queue_lock);
Tejun Heo780db202014-07-01 10:31:13 -0600133 wake = !--q->mq_freeze_depth;
134 WARN_ON_ONCE(q->mq_freeze_depth < 0);
Jens Axboe320ae512013-10-24 09:20:05 +0100135 spin_unlock_irq(q->queue_lock);
Tejun Heoadd703f2014-07-01 10:34:38 -0600136 if (wake) {
137 percpu_ref_reinit(&q->mq_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100138 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600139 }
Jens Axboe320ae512013-10-24 09:20:05 +0100140}
141
142bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
143{
144 return blk_mq_has_free_tags(hctx->tags);
145}
146EXPORT_SYMBOL(blk_mq_can_queue);
147
Jens Axboe94eddfb2013-11-19 09:25:07 -0700148static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
149 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100150{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700151 if (blk_queue_io_stat(q))
152 rw_flags |= REQ_IO_STAT;
153
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200154 INIT_LIST_HEAD(&rq->queuelist);
155 /* csd/requeue_work/fifo_time is initialized before use */
156 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100157 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600158 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200159 /* do not touch atomic flags, it needs atomic ops against the timer */
160 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200161 INIT_HLIST_NODE(&rq->hash);
162 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200163 rq->rq_disk = NULL;
164 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600165 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200166#ifdef CONFIG_BLK_CGROUP
167 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700168 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200169 rq->io_start_time_ns = 0;
170#endif
171 rq->nr_phys_segments = 0;
172#if defined(CONFIG_BLK_DEV_INTEGRITY)
173 rq->nr_integrity_segments = 0;
174#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200175 rq->special = NULL;
176 /* tag was already set */
177 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200178
Tony Battersby6f4a16262014-08-22 15:53:39 -0400179 rq->cmd = rq->__cmd;
180
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200181 rq->extra_len = 0;
182 rq->sense_len = 0;
183 rq->resid_len = 0;
184 rq->sense = NULL;
185
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200186 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600187 rq->timeout = 0;
188
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200189 rq->end_io = NULL;
190 rq->end_io_data = NULL;
191 rq->next_rq = NULL;
192
Jens Axboe320ae512013-10-24 09:20:05 +0100193 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
194}
195
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200196static struct request *
Ming Leicb96a422014-06-01 00:43:37 +0800197__blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200198{
199 struct request *rq;
200 unsigned int tag;
201
Ming Leicb96a422014-06-01 00:43:37 +0800202 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200203 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a422014-06-01 00:43:37 +0800204 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200205
206 rq->cmd_flags = 0;
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;
Jens Axboe320ae512013-10-24 09:20:05 +0100227
228 if (blk_mq_queue_enter(q))
229 return NULL;
230
Christoph Hellwigd8525642014-05-27 20:59:50 +0200231 ctx = blk_mq_get_ctx(q);
232 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800233 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
234 reserved, ctx, hctx);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200235
Ming Leicb96a422014-06-01 00:43:37 +0800236 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200237 if (!rq && (gfp & __GFP_WAIT)) {
238 __blk_mq_run_hw_queue(hctx);
239 blk_mq_put_ctx(ctx);
240
241 ctx = blk_mq_get_ctx(q);
242 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +0800243 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
244 hctx);
245 rq = __blk_mq_alloc_request(&alloc_data, rw);
246 ctx = alloc_data.ctx;
Christoph Hellwigd8525642014-05-27 20:59:50 +0200247 }
248 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100249 return rq;
250}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600251EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100252
Jens Axboe320ae512013-10-24 09:20:05 +0100253static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
254 struct blk_mq_ctx *ctx, struct request *rq)
255{
256 const int tag = rq->tag;
257 struct request_queue *q = rq->q;
258
Jens Axboe0d2602c2014-05-13 15:10:52 -0600259 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
260 atomic_dec(&hctx->nr_active);
261
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200262 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600263 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100264 blk_mq_queue_exit(q);
265}
266
267void blk_mq_free_request(struct request *rq)
268{
269 struct blk_mq_ctx *ctx = rq->mq_ctx;
270 struct blk_mq_hw_ctx *hctx;
271 struct request_queue *q = rq->q;
272
273 ctx->rq_completed[rq_is_sync(rq)]++;
274
275 hctx = q->mq_ops->map_queue(q, ctx->cpu);
276 __blk_mq_free_request(hctx, ctx, rq);
277}
278
Christoph Hellwig8727af42014-04-14 10:30:08 +0200279/*
280 * Clone all relevant state from a request that has been put on hold in
281 * the flush state machine into the preallocated flush request that hangs
282 * off the request queue.
283 *
284 * For a driver the flush request should be invisible, that's why we are
285 * impersonating the original request here.
286 */
287void blk_mq_clone_flush_request(struct request *flush_rq,
288 struct request *orig_rq)
289{
290 struct blk_mq_hw_ctx *hctx =
291 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
292
293 flush_rq->mq_ctx = orig_rq->mq_ctx;
294 flush_rq->tag = orig_rq->tag;
295 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
296 hctx->cmd_size);
297}
298
Christoph Hellwig63151a42014-04-16 09:44:52 +0200299inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100300{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700301 blk_account_io_done(rq);
302
Christoph Hellwig91b63632014-04-16 09:44:53 +0200303 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100304 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200305 } else {
306 if (unlikely(blk_bidi_rq(rq)))
307 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100308 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200309 }
Jens Axboe320ae512013-10-24 09:20:05 +0100310}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200311EXPORT_SYMBOL(__blk_mq_end_io);
312
313void blk_mq_end_io(struct request *rq, int error)
314{
315 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
316 BUG();
317 __blk_mq_end_io(rq, error);
318}
319EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100320
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800321static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100322{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800323 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100324
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800325 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100326}
327
Jens Axboeed851862014-05-30 21:20:50 -0600328static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100329{
330 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700331 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100332 int cpu;
333
Christoph Hellwig38535202014-04-25 02:32:53 -0700334 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800335 rq->q->softirq_done_fn(rq);
336 return;
337 }
Jens Axboe320ae512013-10-24 09:20:05 +0100338
339 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700340 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
341 shared = cpus_share_cache(cpu, ctx->cpu);
342
343 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800344 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800345 rq->csd.info = rq;
346 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100347 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800348 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800349 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800350 }
Jens Axboe320ae512013-10-24 09:20:05 +0100351 put_cpu();
352}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800353
Jens Axboeed851862014-05-30 21:20:50 -0600354void __blk_mq_complete_request(struct request *rq)
355{
356 struct request_queue *q = rq->q;
357
358 if (!q->softirq_done_fn)
359 blk_mq_end_io(rq, rq->errors);
360 else
361 blk_mq_ipi_complete_request(rq);
362}
363
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800364/**
365 * blk_mq_complete_request - end I/O on a request
366 * @rq: the request being processed
367 *
368 * Description:
369 * Ends all I/O on a request. It does not handle partial completions.
370 * The actual completion happens out-of-order, through a IPI handler.
371 **/
372void blk_mq_complete_request(struct request *rq)
373{
Jens Axboe95f09682014-05-27 17:46:48 -0600374 struct request_queue *q = rq->q;
375
376 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800377 return;
Jens Axboeed851862014-05-30 21:20:50 -0600378 if (!blk_mark_rq_complete(rq))
379 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800380}
381EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100382
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800383static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100384{
385 struct request_queue *q = rq->q;
386
387 trace_block_rq_issue(q, rq);
388
Christoph Hellwig742ee692014-04-14 10:30:06 +0200389 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200390 if (unlikely(blk_bidi_rq(rq)))
391 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200392
Ming Lei2b8393b2014-06-10 00:16:41 +0800393 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600394
395 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600396 * Ensure that ->deadline is visible before set the started
397 * flag and clear the completed flag.
398 */
399 smp_mb__before_atomic();
400
401 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600402 * Mark us as started and clear complete. Complete might have been
403 * set if requeue raced with timeout, which then marked it as
404 * complete. So be sure to clear complete again when we start
405 * the request, otherwise we'll ignore the completion event.
406 */
Jens Axboe4b570522014-05-29 11:00:11 -0600407 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
408 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
409 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
410 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800411
412 if (q->dma_drain_size && blk_rq_bytes(rq)) {
413 /*
414 * Make sure space for the drain appears. We know we can do
415 * this because max_hw_segments has been adjusted to be one
416 * fewer than the device can handle.
417 */
418 rq->nr_phys_segments++;
419 }
420
421 /*
422 * Flag the last request in the series so that drivers know when IO
423 * should be kicked off, if they don't do it on a per-request basis.
424 *
425 * Note: the flag isn't the only condition drivers should do kick off.
426 * If drive is busy, the last request might not have the bit set.
427 */
428 if (last)
429 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100430}
431
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200432static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100433{
434 struct request_queue *q = rq->q;
435
436 trace_block_rq_requeue(q, rq);
437 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800438
439 rq->cmd_flags &= ~REQ_END;
440
441 if (q->dma_drain_size && blk_rq_bytes(rq))
442 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100443}
444
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200445void blk_mq_requeue_request(struct request *rq)
446{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200447 __blk_mq_requeue_request(rq);
448 blk_clear_rq_complete(rq);
449
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200450 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600451 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200452}
453EXPORT_SYMBOL(blk_mq_requeue_request);
454
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600455static void blk_mq_requeue_work(struct work_struct *work)
456{
457 struct request_queue *q =
458 container_of(work, struct request_queue, requeue_work);
459 LIST_HEAD(rq_list);
460 struct request *rq, *next;
461 unsigned long flags;
462
463 spin_lock_irqsave(&q->requeue_lock, flags);
464 list_splice_init(&q->requeue_list, &rq_list);
465 spin_unlock_irqrestore(&q->requeue_lock, flags);
466
467 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
468 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
469 continue;
470
471 rq->cmd_flags &= ~REQ_SOFTBARRIER;
472 list_del_init(&rq->queuelist);
473 blk_mq_insert_request(rq, true, false, false);
474 }
475
476 while (!list_empty(&rq_list)) {
477 rq = list_entry(rq_list.next, struct request, queuelist);
478 list_del_init(&rq->queuelist);
479 blk_mq_insert_request(rq, false, false, false);
480 }
481
482 blk_mq_run_queues(q, false);
483}
484
485void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
486{
487 struct request_queue *q = rq->q;
488 unsigned long flags;
489
490 /*
491 * We abuse this flag that is otherwise used by the I/O scheduler to
492 * request head insertation from the workqueue.
493 */
494 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
495
496 spin_lock_irqsave(&q->requeue_lock, flags);
497 if (at_head) {
498 rq->cmd_flags |= REQ_SOFTBARRIER;
499 list_add(&rq->queuelist, &q->requeue_list);
500 } else {
501 list_add_tail(&rq->queuelist, &q->requeue_list);
502 }
503 spin_unlock_irqrestore(&q->requeue_lock, flags);
504}
505EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
506
507void blk_mq_kick_requeue_list(struct request_queue *q)
508{
509 kblockd_schedule_work(&q->requeue_work);
510}
511EXPORT_SYMBOL(blk_mq_kick_requeue_list);
512
Jens Axboe0e62f512014-06-04 10:23:49 -0600513static inline bool is_flush_request(struct request *rq, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600514{
Jens Axboe0e62f512014-06-04 10:23:49 -0600515 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
516 rq->q->flush_rq->tag == tag);
517}
Shaohua Li22302372014-05-30 08:06:42 -0600518
Jens Axboe0e62f512014-06-04 10:23:49 -0600519struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
520{
521 struct request *rq = tags->rqs[tag];
Shaohua Li22302372014-05-30 08:06:42 -0600522
Jens Axboe0e62f512014-06-04 10:23:49 -0600523 if (!is_flush_request(rq, tag))
524 return rq;
525
526 return rq->q->flush_rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600527}
528EXPORT_SYMBOL(blk_mq_tag_to_rq);
529
Jens Axboe320ae512013-10-24 09:20:05 +0100530struct blk_mq_timeout_data {
531 struct blk_mq_hw_ctx *hctx;
532 unsigned long *next;
533 unsigned int *next_set;
534};
535
536static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
537{
538 struct blk_mq_timeout_data *data = __data;
539 struct blk_mq_hw_ctx *hctx = data->hctx;
540 unsigned int tag;
541
542 /* It may not be in flight yet (this is where
543 * the REQ_ATOMIC_STARTED flag comes in). The requests are
544 * statically allocated, so we know it's always safe to access the
545 * memory associated with a bit offset into ->rqs[].
546 */
547 tag = 0;
548 do {
549 struct request *rq;
550
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600551 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
552 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100553 break;
554
Jens Axboe0e62f512014-06-04 10:23:49 -0600555 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600556 if (rq->q != hctx->queue)
557 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100558 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
559 continue;
560
561 blk_rq_check_expired(rq, data->next, data->next_set);
562 } while (1);
563}
564
565static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
566 unsigned long *next,
567 unsigned int *next_set)
568{
569 struct blk_mq_timeout_data data = {
570 .hctx = hctx,
571 .next = next,
572 .next_set = next_set,
573 };
574
575 /*
576 * Ask the tagging code to iterate busy requests, so we can
577 * check them for timeout.
578 */
579 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
580}
581
Jens Axboe87ee7b12014-04-24 08:51:47 -0600582static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
583{
584 struct request_queue *q = rq->q;
585
586 /*
587 * We know that complete is set at this point. If STARTED isn't set
588 * anymore, then the request isn't active and the "timeout" should
589 * just be ignored. This can happen due to the bitflag ordering.
590 * Timeout first checks if STARTED is set, and if it is, assumes
591 * the request is active. But if we race with completion, then
592 * we both flags will get cleared. So check here again, and ignore
593 * a timeout event with a request that isn't active.
594 */
595 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
596 return BLK_EH_NOT_HANDLED;
597
598 if (!q->mq_ops->timeout)
599 return BLK_EH_RESET_TIMER;
600
601 return q->mq_ops->timeout(rq);
602}
603
Jens Axboe320ae512013-10-24 09:20:05 +0100604static void blk_mq_rq_timer(unsigned long data)
605{
606 struct request_queue *q = (struct request_queue *) data;
607 struct blk_mq_hw_ctx *hctx;
608 unsigned long next = 0;
609 int i, next_set = 0;
610
Jens Axboe484b4062014-05-21 14:01:15 -0600611 queue_for_each_hw_ctx(q, hctx, i) {
612 /*
613 * If not software queues are currently mapped to this
614 * hardware queue, there's nothing to check
615 */
616 if (!hctx->nr_ctx || !hctx->tags)
617 continue;
618
Jens Axboe320ae512013-10-24 09:20:05 +0100619 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
Jens Axboe484b4062014-05-21 14:01:15 -0600620 }
Jens Axboe320ae512013-10-24 09:20:05 +0100621
Jens Axboe0d2602c2014-05-13 15:10:52 -0600622 if (next_set) {
623 next = blk_rq_timeout(round_jiffies_up(next));
624 mod_timer(&q->timeout, next);
625 } else {
626 queue_for_each_hw_ctx(q, hctx, i)
627 blk_mq_tag_idle(hctx);
628 }
Jens Axboe320ae512013-10-24 09:20:05 +0100629}
630
631/*
632 * Reverse check our software queue for entries that we could potentially
633 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
634 * too much time checking for merges.
635 */
636static bool blk_mq_attempt_merge(struct request_queue *q,
637 struct blk_mq_ctx *ctx, struct bio *bio)
638{
639 struct request *rq;
640 int checked = 8;
641
642 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
643 int el_ret;
644
645 if (!checked--)
646 break;
647
648 if (!blk_rq_merge_ok(rq, bio))
649 continue;
650
651 el_ret = blk_try_merge(rq, bio);
652 if (el_ret == ELEVATOR_BACK_MERGE) {
653 if (bio_attempt_back_merge(q, rq, bio)) {
654 ctx->rq_merged++;
655 return true;
656 }
657 break;
658 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
659 if (bio_attempt_front_merge(q, rq, bio)) {
660 ctx->rq_merged++;
661 return true;
662 }
663 break;
664 }
665 }
666
667 return false;
668}
669
Jens Axboe320ae512013-10-24 09:20:05 +0100670/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600671 * Process software queues that have been marked busy, splicing them
672 * to the for-dispatch
673 */
674static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
675{
676 struct blk_mq_ctx *ctx;
677 int i;
678
679 for (i = 0; i < hctx->ctx_map.map_size; i++) {
680 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
681 unsigned int off, bit;
682
683 if (!bm->word)
684 continue;
685
686 bit = 0;
687 off = i * hctx->ctx_map.bits_per_word;
688 do {
689 bit = find_next_bit(&bm->word, bm->depth, bit);
690 if (bit >= bm->depth)
691 break;
692
693 ctx = hctx->ctxs[bit + off];
694 clear_bit(bit, &bm->word);
695 spin_lock(&ctx->lock);
696 list_splice_tail_init(&ctx->rq_list, list);
697 spin_unlock(&ctx->lock);
698
699 bit++;
700 } while (1);
701 }
702}
703
704/*
Jens Axboe320ae512013-10-24 09:20:05 +0100705 * Run this hardware queue, pulling any software queues mapped to it in.
706 * Note that this function currently has various problems around ordering
707 * of IO. In particular, we'd like FIFO behaviour on handling existing
708 * items on the hctx->dispatch list. Ignore that for now.
709 */
710static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
711{
712 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100713 struct request *rq;
714 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600715 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100716
Jens Axboefd1270d2014-04-16 09:23:48 -0600717 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600718
Jens Axboe5d12f902014-03-19 15:25:02 -0600719 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100720 return;
721
722 hctx->run++;
723
724 /*
725 * Touch any software queue that has pending entries.
726 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600727 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100728
729 /*
730 * If we have previous entries on our dispatch list, grab them
731 * and stuff them at the front for more fair dispatch.
732 */
733 if (!list_empty_careful(&hctx->dispatch)) {
734 spin_lock(&hctx->lock);
735 if (!list_empty(&hctx->dispatch))
736 list_splice_init(&hctx->dispatch, &rq_list);
737 spin_unlock(&hctx->lock);
738 }
739
740 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100741 * Now process all the entries, sending them to the driver.
742 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600743 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100744 while (!list_empty(&rq_list)) {
745 int ret;
746
747 rq = list_first_entry(&rq_list, struct request, queuelist);
748 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100749
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800750 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100751
752 ret = q->mq_ops->queue_rq(hctx, rq);
753 switch (ret) {
754 case BLK_MQ_RQ_QUEUE_OK:
755 queued++;
756 continue;
757 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100758 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200759 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100760 break;
761 default:
762 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100763 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800764 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100765 blk_mq_end_io(rq, rq->errors);
766 break;
767 }
768
769 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
770 break;
771 }
772
773 if (!queued)
774 hctx->dispatched[0]++;
775 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
776 hctx->dispatched[ilog2(queued) + 1]++;
777
778 /*
779 * Any items that need requeuing? Stuff them into hctx->dispatch,
780 * that is where we will continue on next queue run.
781 */
782 if (!list_empty(&rq_list)) {
783 spin_lock(&hctx->lock);
784 list_splice(&rq_list, &hctx->dispatch);
785 spin_unlock(&hctx->lock);
786 }
787}
788
Jens Axboe506e9312014-05-07 10:26:44 -0600789/*
790 * It'd be great if the workqueue API had a way to pass
791 * in a mask and had some smarts for more clever placement.
792 * For now we just round-robin here, switching for every
793 * BLK_MQ_CPU_WORK_BATCH queued items.
794 */
795static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
796{
797 int cpu = hctx->next_cpu;
798
799 if (--hctx->next_cpu_batch <= 0) {
800 int next_cpu;
801
802 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
803 if (next_cpu >= nr_cpu_ids)
804 next_cpu = cpumask_first(hctx->cpumask);
805
806 hctx->next_cpu = next_cpu;
807 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
808 }
809
810 return cpu;
811}
812
Jens Axboe320ae512013-10-24 09:20:05 +0100813void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
814{
Jens Axboe5d12f902014-03-19 15:25:02 -0600815 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100816 return;
817
Jens Axboee4043dc2014-04-09 10:18:23 -0600818 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100819 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600820 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600821 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600822 else {
823 unsigned int cpu;
824
Jens Axboe506e9312014-05-07 10:26:44 -0600825 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600826 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600827 }
Jens Axboe320ae512013-10-24 09:20:05 +0100828}
829
830void blk_mq_run_queues(struct request_queue *q, bool async)
831{
832 struct blk_mq_hw_ctx *hctx;
833 int i;
834
835 queue_for_each_hw_ctx(q, hctx, i) {
836 if ((!blk_mq_hctx_has_pending(hctx) &&
837 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600838 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100839 continue;
840
Jens Axboee4043dc2014-04-09 10:18:23 -0600841 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100842 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600843 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100844 }
845}
846EXPORT_SYMBOL(blk_mq_run_queues);
847
848void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
849{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600850 cancel_delayed_work(&hctx->run_work);
851 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100852 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
853}
854EXPORT_SYMBOL(blk_mq_stop_hw_queue);
855
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100856void blk_mq_stop_hw_queues(struct request_queue *q)
857{
858 struct blk_mq_hw_ctx *hctx;
859 int i;
860
861 queue_for_each_hw_ctx(q, hctx, i)
862 blk_mq_stop_hw_queue(hctx);
863}
864EXPORT_SYMBOL(blk_mq_stop_hw_queues);
865
Jens Axboe320ae512013-10-24 09:20:05 +0100866void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
867{
868 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600869
870 preempt_disable();
Jens Axboe0ffbce82014-06-25 08:22:34 -0600871 blk_mq_run_hw_queue(hctx, false);
Jens Axboee4043dc2014-04-09 10:18:23 -0600872 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100873}
874EXPORT_SYMBOL(blk_mq_start_hw_queue);
875
Christoph Hellwig2f268552014-04-16 09:44:56 +0200876void blk_mq_start_hw_queues(struct request_queue *q)
877{
878 struct blk_mq_hw_ctx *hctx;
879 int i;
880
881 queue_for_each_hw_ctx(q, hctx, i)
882 blk_mq_start_hw_queue(hctx);
883}
884EXPORT_SYMBOL(blk_mq_start_hw_queues);
885
886
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200887void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100888{
889 struct blk_mq_hw_ctx *hctx;
890 int i;
891
892 queue_for_each_hw_ctx(q, hctx, i) {
893 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
894 continue;
895
896 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600897 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200898 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600899 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100900 }
901}
902EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
903
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600904static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100905{
906 struct blk_mq_hw_ctx *hctx;
907
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600908 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600909
Jens Axboe320ae512013-10-24 09:20:05 +0100910 __blk_mq_run_hw_queue(hctx);
911}
912
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600913static void blk_mq_delay_work_fn(struct work_struct *work)
914{
915 struct blk_mq_hw_ctx *hctx;
916
917 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
918
919 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
920 __blk_mq_run_hw_queue(hctx);
921}
922
923void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
924{
925 unsigned long tmo = msecs_to_jiffies(msecs);
926
927 if (hctx->queue->nr_hw_queues == 1)
928 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
929 else {
930 unsigned int cpu;
931
Jens Axboe506e9312014-05-07 10:26:44 -0600932 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600933 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
934 }
935}
936EXPORT_SYMBOL(blk_mq_delay_queue);
937
Jens Axboe320ae512013-10-24 09:20:05 +0100938static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800939 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100940{
941 struct blk_mq_ctx *ctx = rq->mq_ctx;
942
Jens Axboe01b983c2013-11-19 18:59:10 -0700943 trace_block_rq_insert(hctx->queue, rq);
944
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800945 if (at_head)
946 list_add(&rq->queuelist, &ctx->rq_list);
947 else
948 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600949
Jens Axboe320ae512013-10-24 09:20:05 +0100950 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100951}
952
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600953void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
954 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100955{
956 struct request_queue *q = rq->q;
957 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600958 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100959
960 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600961 if (!cpu_online(ctx->cpu))
962 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100963
Jens Axboe320ae512013-10-24 09:20:05 +0100964 hctx = q->mq_ops->map_queue(q, ctx->cpu);
965
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600966 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
967 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
968 blk_insert_flush(rq);
969 } else {
970 spin_lock(&ctx->lock);
971 __blk_mq_insert_request(hctx, rq, at_head);
972 spin_unlock(&ctx->lock);
973 }
Jens Axboe320ae512013-10-24 09:20:05 +0100974
Jens Axboe320ae512013-10-24 09:20:05 +0100975 if (run_queue)
976 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600977
978 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100979}
980
981static void blk_mq_insert_requests(struct request_queue *q,
982 struct blk_mq_ctx *ctx,
983 struct list_head *list,
984 int depth,
985 bool from_schedule)
986
987{
988 struct blk_mq_hw_ctx *hctx;
989 struct blk_mq_ctx *current_ctx;
990
991 trace_block_unplug(q, depth, !from_schedule);
992
993 current_ctx = blk_mq_get_ctx(q);
994
995 if (!cpu_online(ctx->cpu))
996 ctx = current_ctx;
997 hctx = q->mq_ops->map_queue(q, ctx->cpu);
998
999 /*
1000 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1001 * offline now
1002 */
1003 spin_lock(&ctx->lock);
1004 while (!list_empty(list)) {
1005 struct request *rq;
1006
1007 rq = list_first_entry(list, struct request, queuelist);
1008 list_del_init(&rq->queuelist);
1009 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001010 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001011 }
1012 spin_unlock(&ctx->lock);
1013
Jens Axboe320ae512013-10-24 09:20:05 +01001014 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001015 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001016}
1017
1018static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1019{
1020 struct request *rqa = container_of(a, struct request, queuelist);
1021 struct request *rqb = container_of(b, struct request, queuelist);
1022
1023 return !(rqa->mq_ctx < rqb->mq_ctx ||
1024 (rqa->mq_ctx == rqb->mq_ctx &&
1025 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1026}
1027
1028void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1029{
1030 struct blk_mq_ctx *this_ctx;
1031 struct request_queue *this_q;
1032 struct request *rq;
1033 LIST_HEAD(list);
1034 LIST_HEAD(ctx_list);
1035 unsigned int depth;
1036
1037 list_splice_init(&plug->mq_list, &list);
1038
1039 list_sort(NULL, &list, plug_ctx_cmp);
1040
1041 this_q = NULL;
1042 this_ctx = NULL;
1043 depth = 0;
1044
1045 while (!list_empty(&list)) {
1046 rq = list_entry_rq(list.next);
1047 list_del_init(&rq->queuelist);
1048 BUG_ON(!rq->q);
1049 if (rq->mq_ctx != this_ctx) {
1050 if (this_ctx) {
1051 blk_mq_insert_requests(this_q, this_ctx,
1052 &ctx_list, depth,
1053 from_schedule);
1054 }
1055
1056 this_ctx = rq->mq_ctx;
1057 this_q = rq->q;
1058 depth = 0;
1059 }
1060
1061 depth++;
1062 list_add_tail(&rq->queuelist, &ctx_list);
1063 }
1064
1065 /*
1066 * If 'this_ctx' is set, we know we have entries to complete
1067 * on 'ctx_list'. Do those.
1068 */
1069 if (this_ctx) {
1070 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1071 from_schedule);
1072 }
1073}
1074
1075static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1076{
1077 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001078
Jens Axboe3ee32372014-06-09 09:36:53 -06001079 if (blk_do_io_stat(rq))
Jens Axboe4b570522014-05-29 11:00:11 -06001080 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001081}
1082
Jens Axboe274a5842014-08-15 12:44:08 -06001083static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1084{
1085 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1086 !blk_queue_nomerges(hctx->queue);
1087}
1088
Jens Axboe07068d52014-05-22 10:40:51 -06001089static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1090 struct blk_mq_ctx *ctx,
1091 struct request *rq, struct bio *bio)
1092{
Jens Axboe274a5842014-08-15 12:44:08 -06001093 if (!hctx_allow_merges(hctx)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001094 blk_mq_bio_to_request(rq, bio);
1095 spin_lock(&ctx->lock);
1096insert_rq:
1097 __blk_mq_insert_request(hctx, rq, false);
1098 spin_unlock(&ctx->lock);
1099 return false;
1100 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001101 struct request_queue *q = hctx->queue;
1102
Jens Axboe07068d52014-05-22 10:40:51 -06001103 spin_lock(&ctx->lock);
1104 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1105 blk_mq_bio_to_request(rq, bio);
1106 goto insert_rq;
1107 }
1108
1109 spin_unlock(&ctx->lock);
1110 __blk_mq_free_request(hctx, ctx, rq);
1111 return true;
1112 }
1113}
1114
1115struct blk_map_ctx {
1116 struct blk_mq_hw_ctx *hctx;
1117 struct blk_mq_ctx *ctx;
1118};
1119
1120static struct request *blk_mq_map_request(struct request_queue *q,
1121 struct bio *bio,
1122 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001123{
1124 struct blk_mq_hw_ctx *hctx;
1125 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001126 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001127 int rw = bio_data_dir(bio);
Ming Leicb96a422014-06-01 00:43:37 +08001128 struct blk_mq_alloc_data alloc_data;
Jens Axboe320ae512013-10-24 09:20:05 +01001129
Jens Axboe07068d52014-05-22 10:40:51 -06001130 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001131 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001132 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001133 }
1134
1135 ctx = blk_mq_get_ctx(q);
1136 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1137
Jens Axboe07068d52014-05-22 10:40:51 -06001138 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001139 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001140
Jens Axboe320ae512013-10-24 09:20:05 +01001141 trace_block_getrq(q, bio, rw);
Ming Leicb96a422014-06-01 00:43:37 +08001142 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1143 hctx);
1144 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001145 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001146 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001147 blk_mq_put_ctx(ctx);
1148 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001149
1150 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001151 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Ming Leicb96a422014-06-01 00:43:37 +08001152 blk_mq_set_alloc_data(&alloc_data, q,
1153 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1154 rq = __blk_mq_alloc_request(&alloc_data, rw);
1155 ctx = alloc_data.ctx;
1156 hctx = alloc_data.hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001157 }
1158
1159 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001160 data->hctx = hctx;
1161 data->ctx = ctx;
1162 return rq;
1163}
1164
1165/*
1166 * Multiple hardware queue variant. This will not use per-process plugs,
1167 * but will attempt to bypass the hctx queueing if we can go straight to
1168 * hardware for SYNC IO.
1169 */
1170static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1171{
1172 const int is_sync = rw_is_sync(bio->bi_rw);
1173 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1174 struct blk_map_ctx data;
1175 struct request *rq;
1176
1177 blk_queue_bounce(q, &bio);
1178
1179 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1180 bio_endio(bio, -EIO);
1181 return;
1182 }
1183
1184 rq = blk_mq_map_request(q, bio, &data);
1185 if (unlikely(!rq))
1186 return;
1187
1188 if (unlikely(is_flush_fua)) {
1189 blk_mq_bio_to_request(rq, bio);
1190 blk_insert_flush(rq);
1191 goto run_queue;
1192 }
1193
1194 if (is_sync) {
1195 int ret;
1196
1197 blk_mq_bio_to_request(rq, bio);
1198 blk_mq_start_request(rq, true);
1199
1200 /*
1201 * For OK queue, we are done. For error, kill it. Any other
1202 * error (busy), just add it to our list as we previously
1203 * would have done
1204 */
1205 ret = q->mq_ops->queue_rq(data.hctx, rq);
1206 if (ret == BLK_MQ_RQ_QUEUE_OK)
1207 goto done;
1208 else {
1209 __blk_mq_requeue_request(rq);
1210
1211 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1212 rq->errors = -EIO;
1213 blk_mq_end_io(rq, rq->errors);
1214 goto done;
1215 }
1216 }
1217 }
1218
1219 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1220 /*
1221 * For a SYNC request, send it to the hardware immediately. For
1222 * an ASYNC request, just ensure that we run it later on. The
1223 * latter allows for merging opportunities and more efficient
1224 * dispatching.
1225 */
1226run_queue:
1227 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1228 }
1229done:
1230 blk_mq_put_ctx(data.ctx);
1231}
1232
1233/*
1234 * Single hardware queue variant. This will attempt to use any per-process
1235 * plug for merging and IO deferral.
1236 */
1237static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1238{
1239 const int is_sync = rw_is_sync(bio->bi_rw);
1240 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1241 unsigned int use_plug, request_count = 0;
1242 struct blk_map_ctx data;
1243 struct request *rq;
1244
1245 /*
1246 * If we have multiple hardware queues, just go directly to
1247 * one of those for sync IO.
1248 */
1249 use_plug = !is_flush_fua && !is_sync;
1250
1251 blk_queue_bounce(q, &bio);
1252
1253 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1254 bio_endio(bio, -EIO);
1255 return;
1256 }
1257
1258 if (use_plug && !blk_queue_nomerges(q) &&
1259 blk_attempt_plug_merge(q, bio, &request_count))
1260 return;
1261
1262 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001263 if (unlikely(!rq))
1264 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001265
1266 if (unlikely(is_flush_fua)) {
1267 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001268 blk_insert_flush(rq);
1269 goto run_queue;
1270 }
1271
1272 /*
1273 * A task plug currently exists. Since this is completely lockless,
1274 * utilize that to temporarily store requests until the task is
1275 * either done or scheduled away.
1276 */
1277 if (use_plug) {
1278 struct blk_plug *plug = current->plug;
1279
1280 if (plug) {
1281 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001282 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001283 trace_block_plug(q);
1284 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1285 blk_flush_plug_list(plug, false);
1286 trace_block_plug(q);
1287 }
1288 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001289 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001290 return;
1291 }
1292 }
1293
Jens Axboe07068d52014-05-22 10:40:51 -06001294 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1295 /*
1296 * For a SYNC request, send it to the hardware immediately. For
1297 * an ASYNC request, just ensure that we run it later on. The
1298 * latter allows for merging opportunities and more efficient
1299 * dispatching.
1300 */
1301run_queue:
1302 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001303 }
1304
Jens Axboe07068d52014-05-22 10:40:51 -06001305 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001306}
1307
1308/*
1309 * Default mapping to a software queue, since we use one per CPU.
1310 */
1311struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1312{
1313 return q->queue_hw_ctx[q->mq_map[cpu]];
1314}
1315EXPORT_SYMBOL(blk_mq_map_queue);
1316
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001317static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1318 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001319{
1320 struct page *page;
1321
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001322 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001323 int i;
1324
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001325 for (i = 0; i < tags->nr_tags; i++) {
1326 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001327 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001328 set->ops->exit_request(set->driver_data, tags->rqs[i],
1329 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001330 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001331 }
1332 }
1333
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001334 while (!list_empty(&tags->page_list)) {
1335 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001336 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001337 __free_pages(page, page->private);
1338 }
1339
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001340 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001341
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001342 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001343}
1344
1345static size_t order_to_size(unsigned int order)
1346{
Ming Lei4ca08502014-04-19 18:00:18 +08001347 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001348}
1349
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001350static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1351 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001352{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001353 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001354 unsigned int i, j, entries_per_page, max_order = 4;
1355 size_t rq_size, left;
1356
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001357 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1358 set->numa_node);
1359 if (!tags)
1360 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001361
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001362 INIT_LIST_HEAD(&tags->page_list);
1363
Jens Axboea5164402014-09-10 09:02:03 -06001364 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1365 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1366 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001367 if (!tags->rqs) {
1368 blk_mq_free_tags(tags);
1369 return NULL;
1370 }
Jens Axboe320ae512013-10-24 09:20:05 +01001371
1372 /*
1373 * rq_size is the size of the request plus driver payload, rounded
1374 * to the cacheline size
1375 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001376 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001377 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001378 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001379
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001380 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001381 int this_order = max_order;
1382 struct page *page;
1383 int to_do;
1384 void *p;
1385
1386 while (left < order_to_size(this_order - 1) && this_order)
1387 this_order--;
1388
1389 do {
Jens Axboea5164402014-09-10 09:02:03 -06001390 page = alloc_pages_node(set->numa_node,
1391 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1392 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001393 if (page)
1394 break;
1395 if (!this_order--)
1396 break;
1397 if (order_to_size(this_order) < rq_size)
1398 break;
1399 } while (1);
1400
1401 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001402 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001403
1404 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001405 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001406
1407 p = page_address(page);
1408 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001409 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001410 left -= to_do * rq_size;
1411 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001412 tags->rqs[i] = p;
1413 if (set->ops->init_request) {
1414 if (set->ops->init_request(set->driver_data,
1415 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001416 set->numa_node)) {
1417 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001418 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001419 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001420 }
1421
Jens Axboe320ae512013-10-24 09:20:05 +01001422 p += rq_size;
1423 i++;
1424 }
1425 }
1426
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001427 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001428
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001429fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001430 blk_mq_free_rq_map(set, tags, hctx_idx);
1431 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001432}
1433
Jens Axboe1429d7c2014-05-19 09:23:55 -06001434static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1435{
1436 kfree(bitmap->map);
1437}
1438
1439static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1440{
1441 unsigned int bpw = 8, total, num_maps, i;
1442
1443 bitmap->bits_per_word = bpw;
1444
1445 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1446 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1447 GFP_KERNEL, node);
1448 if (!bitmap->map)
1449 return -ENOMEM;
1450
1451 bitmap->map_size = num_maps;
1452
1453 total = nr_cpu_ids;
1454 for (i = 0; i < num_maps; i++) {
1455 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1456 total -= bitmap->map[i].depth;
1457 }
1458
1459 return 0;
1460}
1461
Jens Axboe484b4062014-05-21 14:01:15 -06001462static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1463{
1464 struct request_queue *q = hctx->queue;
1465 struct blk_mq_ctx *ctx;
1466 LIST_HEAD(tmp);
1467
1468 /*
1469 * Move ctx entries to new CPU, if this one is going away.
1470 */
1471 ctx = __blk_mq_get_ctx(q, cpu);
1472
1473 spin_lock(&ctx->lock);
1474 if (!list_empty(&ctx->rq_list)) {
1475 list_splice_init(&ctx->rq_list, &tmp);
1476 blk_mq_hctx_clear_pending(hctx, ctx);
1477 }
1478 spin_unlock(&ctx->lock);
1479
1480 if (list_empty(&tmp))
1481 return NOTIFY_OK;
1482
1483 ctx = blk_mq_get_ctx(q);
1484 spin_lock(&ctx->lock);
1485
1486 while (!list_empty(&tmp)) {
1487 struct request *rq;
1488
1489 rq = list_first_entry(&tmp, struct request, queuelist);
1490 rq->mq_ctx = ctx;
1491 list_move_tail(&rq->queuelist, &ctx->rq_list);
1492 }
1493
1494 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1495 blk_mq_hctx_mark_pending(hctx, ctx);
1496
1497 spin_unlock(&ctx->lock);
1498
1499 blk_mq_run_hw_queue(hctx, true);
1500 blk_mq_put_ctx(ctx);
1501 return NOTIFY_OK;
1502}
1503
1504static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1505{
1506 struct request_queue *q = hctx->queue;
1507 struct blk_mq_tag_set *set = q->tag_set;
1508
1509 if (set->tags[hctx->queue_num])
1510 return NOTIFY_OK;
1511
1512 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1513 if (!set->tags[hctx->queue_num])
1514 return NOTIFY_STOP;
1515
1516 hctx->tags = set->tags[hctx->queue_num];
1517 return NOTIFY_OK;
1518}
1519
1520static int blk_mq_hctx_notify(void *data, unsigned long action,
1521 unsigned int cpu)
1522{
1523 struct blk_mq_hw_ctx *hctx = data;
1524
1525 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1526 return blk_mq_hctx_cpu_offline(hctx, cpu);
1527 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1528 return blk_mq_hctx_cpu_online(hctx, cpu);
1529
1530 return NOTIFY_OK;
1531}
1532
Ming Lei624dbe42014-05-27 23:35:13 +08001533static void blk_mq_exit_hw_queues(struct request_queue *q,
1534 struct blk_mq_tag_set *set, int nr_queue)
1535{
1536 struct blk_mq_hw_ctx *hctx;
1537 unsigned int i;
1538
1539 queue_for_each_hw_ctx(q, hctx, i) {
1540 if (i == nr_queue)
1541 break;
1542
Jens Axboef899fed2014-06-04 09:11:53 -06001543 blk_mq_tag_idle(hctx);
1544
Ming Lei624dbe42014-05-27 23:35:13 +08001545 if (set->ops->exit_hctx)
1546 set->ops->exit_hctx(hctx, i);
1547
1548 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1549 kfree(hctx->ctxs);
1550 blk_mq_free_bitmap(&hctx->ctx_map);
1551 }
1552
1553}
1554
1555static void blk_mq_free_hw_queues(struct request_queue *q,
1556 struct blk_mq_tag_set *set)
1557{
1558 struct blk_mq_hw_ctx *hctx;
1559 unsigned int i;
1560
1561 queue_for_each_hw_ctx(q, hctx, i) {
1562 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001563 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001564 }
1565}
1566
Jens Axboe320ae512013-10-24 09:20:05 +01001567static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001568 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001569{
1570 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001571 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001572
1573 /*
1574 * Initialize hardware queues
1575 */
1576 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001577 int node;
1578
1579 node = hctx->numa_node;
1580 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001581 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001582
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001583 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1584 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001585 spin_lock_init(&hctx->lock);
1586 INIT_LIST_HEAD(&hctx->dispatch);
1587 hctx->queue = q;
1588 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001589 hctx->flags = set->flags;
1590 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001591
1592 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1593 blk_mq_hctx_notify, hctx);
1594 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1595
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001596 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001597
1598 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001599 * Allocate space for all possible cpus to avoid allocation at
Jens Axboe320ae512013-10-24 09:20:05 +01001600 * runtime
1601 */
1602 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1603 GFP_KERNEL, node);
1604 if (!hctx->ctxs)
1605 break;
1606
Jens Axboe1429d7c2014-05-19 09:23:55 -06001607 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001608 break;
1609
Jens Axboe320ae512013-10-24 09:20:05 +01001610 hctx->nr_ctx = 0;
1611
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001612 if (set->ops->init_hctx &&
1613 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001614 break;
1615 }
1616
1617 if (i == q->nr_hw_queues)
1618 return 0;
1619
1620 /*
1621 * Init failed
1622 */
Ming Lei624dbe42014-05-27 23:35:13 +08001623 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001624
1625 return 1;
1626}
1627
1628static void blk_mq_init_cpu_queues(struct request_queue *q,
1629 unsigned int nr_hw_queues)
1630{
1631 unsigned int i;
1632
1633 for_each_possible_cpu(i) {
1634 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1635 struct blk_mq_hw_ctx *hctx;
1636
1637 memset(__ctx, 0, sizeof(*__ctx));
1638 __ctx->cpu = i;
1639 spin_lock_init(&__ctx->lock);
1640 INIT_LIST_HEAD(&__ctx->rq_list);
1641 __ctx->queue = q;
1642
1643 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001644 if (!cpu_online(i))
1645 continue;
1646
Jens Axboee4043dc2014-04-09 10:18:23 -06001647 hctx = q->mq_ops->map_queue(q, i);
1648 cpumask_set_cpu(i, hctx->cpumask);
1649 hctx->nr_ctx++;
1650
Jens Axboe320ae512013-10-24 09:20:05 +01001651 /*
1652 * Set local node, IFF we have more than one hw queue. If
1653 * not, we remain on the home node of the device
1654 */
1655 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1656 hctx->numa_node = cpu_to_node(i);
1657 }
1658}
1659
1660static void blk_mq_map_swqueue(struct request_queue *q)
1661{
1662 unsigned int i;
1663 struct blk_mq_hw_ctx *hctx;
1664 struct blk_mq_ctx *ctx;
1665
1666 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001667 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001668 hctx->nr_ctx = 0;
1669 }
1670
1671 /*
1672 * Map software to hardware queues
1673 */
1674 queue_for_each_ctx(q, ctx, i) {
1675 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001676 if (!cpu_online(i))
1677 continue;
1678
Jens Axboe320ae512013-10-24 09:20:05 +01001679 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001680 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001681 ctx->index_hw = hctx->nr_ctx;
1682 hctx->ctxs[hctx->nr_ctx++] = ctx;
1683 }
Jens Axboe506e9312014-05-07 10:26:44 -06001684
1685 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001686 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001687 * If no software queues are mapped to this hardware queue,
1688 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06001689 */
1690 if (!hctx->nr_ctx) {
1691 struct blk_mq_tag_set *set = q->tag_set;
1692
1693 if (set->tags[i]) {
1694 blk_mq_free_rq_map(set, set->tags[i], i);
1695 set->tags[i] = NULL;
1696 hctx->tags = NULL;
1697 }
1698 continue;
1699 }
1700
1701 /*
1702 * Initialize batch roundrobin counts
1703 */
Jens Axboe506e9312014-05-07 10:26:44 -06001704 hctx->next_cpu = cpumask_first(hctx->cpumask);
1705 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1706 }
Jens Axboe320ae512013-10-24 09:20:05 +01001707}
1708
Jens Axboe0d2602c2014-05-13 15:10:52 -06001709static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1710{
1711 struct blk_mq_hw_ctx *hctx;
1712 struct request_queue *q;
1713 bool shared;
1714 int i;
1715
1716 if (set->tag_list.next == set->tag_list.prev)
1717 shared = false;
1718 else
1719 shared = true;
1720
1721 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1722 blk_mq_freeze_queue(q);
1723
1724 queue_for_each_hw_ctx(q, hctx, i) {
1725 if (shared)
1726 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1727 else
1728 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1729 }
1730 blk_mq_unfreeze_queue(q);
1731 }
1732}
1733
1734static void blk_mq_del_queue_tag_set(struct request_queue *q)
1735{
1736 struct blk_mq_tag_set *set = q->tag_set;
1737
Jens Axboe0d2602c2014-05-13 15:10:52 -06001738 mutex_lock(&set->tag_list_lock);
1739 list_del_init(&q->tag_set_list);
1740 blk_mq_update_tag_set_depth(set);
1741 mutex_unlock(&set->tag_list_lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001742}
1743
1744static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1745 struct request_queue *q)
1746{
1747 q->tag_set = set;
1748
1749 mutex_lock(&set->tag_list_lock);
1750 list_add_tail(&q->tag_set_list, &set->tag_list);
1751 blk_mq_update_tag_set_depth(set);
1752 mutex_unlock(&set->tag_list_lock);
1753}
1754
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001755struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001756{
1757 struct blk_mq_hw_ctx **hctxs;
Ming Leie6cdb092014-06-03 11:24:06 +08001758 struct blk_mq_ctx __percpu *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001759 struct request_queue *q;
Jens Axboef14bbe72014-05-27 12:06:53 -06001760 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001761 int i;
1762
Jens Axboe320ae512013-10-24 09:20:05 +01001763 ctx = alloc_percpu(struct blk_mq_ctx);
1764 if (!ctx)
1765 return ERR_PTR(-ENOMEM);
1766
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001767 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1768 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001769
1770 if (!hctxs)
1771 goto err_percpu;
1772
Jens Axboef14bbe72014-05-27 12:06:53 -06001773 map = blk_mq_make_queue_map(set);
1774 if (!map)
1775 goto err_map;
1776
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001777 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001778 int node = blk_mq_hw_queue_to_node(map, i);
1779
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001780 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1781 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001782 if (!hctxs[i])
1783 goto err_hctxs;
1784
Jens Axboee4043dc2014-04-09 10:18:23 -06001785 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1786 goto err_hctxs;
1787
Jens Axboe0d2602c2014-05-13 15:10:52 -06001788 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001789 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001790 hctxs[i]->queue_num = i;
1791 }
1792
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001793 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001794 if (!q)
1795 goto err_hctxs;
1796
Tejun Heoadd703f2014-07-01 10:34:38 -06001797 if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release))
Ming Lei3d2936f2014-05-27 23:35:14 +08001798 goto err_map;
1799
Jens Axboe320ae512013-10-24 09:20:05 +01001800 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1801 blk_queue_rq_timeout(q, 30000);
1802
1803 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001804 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001805 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001806
1807 q->queue_ctx = ctx;
1808 q->queue_hw_ctx = hctxs;
1809
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001810 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001811 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001812
Jens Axboe05f1dd52014-05-29 09:53:32 -06001813 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1814 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1815
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001816 q->sg_reserved_size = INT_MAX;
1817
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001818 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1819 INIT_LIST_HEAD(&q->requeue_list);
1820 spin_lock_init(&q->requeue_lock);
1821
Jens Axboe07068d52014-05-22 10:40:51 -06001822 if (q->nr_hw_queues > 1)
1823 blk_queue_make_request(q, blk_mq_make_request);
1824 else
1825 blk_queue_make_request(q, blk_sq_make_request);
1826
Jens Axboe87ee7b12014-04-24 08:51:47 -06001827 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001828 if (set->timeout)
1829 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001830
Jens Axboeeba71762014-05-20 15:17:27 -06001831 /*
1832 * Do this after blk_queue_make_request() overrides it...
1833 */
1834 q->nr_requests = set->queue_depth;
1835
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001836 if (set->ops->complete)
1837 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001838
Jens Axboe320ae512013-10-24 09:20:05 +01001839 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001840 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001841
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001842 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1843 set->cmd_size, cache_line_size()),
1844 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001845 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001846 goto err_hw;
1847
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001848 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001849 goto err_flush_rq;
1850
Jens Axboe320ae512013-10-24 09:20:05 +01001851 mutex_lock(&all_q_mutex);
1852 list_add_tail(&q->all_q_node, &all_q_list);
1853 mutex_unlock(&all_q_mutex);
1854
Jens Axboe0d2602c2014-05-13 15:10:52 -06001855 blk_mq_add_queue_tag_set(set, q);
1856
Jens Axboe484b4062014-05-21 14:01:15 -06001857 blk_mq_map_swqueue(q);
1858
Jens Axboe320ae512013-10-24 09:20:05 +01001859 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001860
1861err_flush_rq:
1862 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001863err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001864 blk_cleanup_queue(q);
1865err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001866 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001867 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001868 if (!hctxs[i])
1869 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001870 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001871 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001872 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001873err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001874 kfree(hctxs);
1875err_percpu:
1876 free_percpu(ctx);
1877 return ERR_PTR(-ENOMEM);
1878}
1879EXPORT_SYMBOL(blk_mq_init_queue);
1880
1881void blk_mq_free_queue(struct request_queue *q)
1882{
Ming Lei624dbe42014-05-27 23:35:13 +08001883 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001884
Jens Axboe0d2602c2014-05-13 15:10:52 -06001885 blk_mq_del_queue_tag_set(q);
1886
Ming Lei624dbe42014-05-27 23:35:13 +08001887 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1888 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001889
Tejun Heoadd703f2014-07-01 10:34:38 -06001890 percpu_ref_exit(&q->mq_usage_counter);
Ming Lei3d2936f2014-05-27 23:35:14 +08001891
Jens Axboe320ae512013-10-24 09:20:05 +01001892 free_percpu(q->queue_ctx);
1893 kfree(q->queue_hw_ctx);
1894 kfree(q->mq_map);
1895
1896 q->queue_ctx = NULL;
1897 q->queue_hw_ctx = NULL;
1898 q->mq_map = NULL;
1899
1900 mutex_lock(&all_q_mutex);
1901 list_del_init(&q->all_q_node);
1902 mutex_unlock(&all_q_mutex);
1903}
Jens Axboe320ae512013-10-24 09:20:05 +01001904
1905/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001906static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001907{
1908 blk_mq_freeze_queue(q);
1909
Jens Axboe67aec142014-05-30 08:25:36 -06001910 blk_mq_sysfs_unregister(q);
1911
Jens Axboe320ae512013-10-24 09:20:05 +01001912 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1913
1914 /*
1915 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1916 * we should change hctx numa_node according to new topology (this
1917 * involves free and re-allocate memory, worthy doing?)
1918 */
1919
1920 blk_mq_map_swqueue(q);
1921
Jens Axboe67aec142014-05-30 08:25:36 -06001922 blk_mq_sysfs_register(q);
1923
Jens Axboe320ae512013-10-24 09:20:05 +01001924 blk_mq_unfreeze_queue(q);
1925}
1926
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001927static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1928 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001929{
1930 struct request_queue *q;
1931
1932 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001933 * Before new mappings are established, hotadded cpu might already
1934 * start handling requests. This doesn't break anything as we map
1935 * offline CPUs to first hardware queue. We will re-init the queue
1936 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001937 */
1938 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1939 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1940 return NOTIFY_OK;
1941
1942 mutex_lock(&all_q_mutex);
1943 list_for_each_entry(q, &all_q_list, all_q_node)
1944 blk_mq_queue_reinit(q);
1945 mutex_unlock(&all_q_mutex);
1946 return NOTIFY_OK;
1947}
1948
Jens Axboea5164402014-09-10 09:02:03 -06001949static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1950{
1951 int i;
1952
1953 for (i = 0; i < set->nr_hw_queues; i++) {
1954 set->tags[i] = blk_mq_init_rq_map(set, i);
1955 if (!set->tags[i])
1956 goto out_unwind;
1957 }
1958
1959 return 0;
1960
1961out_unwind:
1962 while (--i >= 0)
1963 blk_mq_free_rq_map(set, set->tags[i], i);
1964
1965 set->tags = NULL;
1966 return -ENOMEM;
1967}
1968
1969/*
1970 * Allocate the request maps associated with this tag_set. Note that this
1971 * may reduce the depth asked for, if memory is tight. set->queue_depth
1972 * will be updated to reflect the allocated depth.
1973 */
1974static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
1975{
1976 unsigned int depth;
1977 int err;
1978
1979 depth = set->queue_depth;
1980 do {
1981 err = __blk_mq_alloc_rq_maps(set);
1982 if (!err)
1983 break;
1984
1985 set->queue_depth >>= 1;
1986 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
1987 err = -ENOMEM;
1988 break;
1989 }
1990 } while (set->queue_depth);
1991
1992 if (!set->queue_depth || err) {
1993 pr_err("blk-mq: failed to allocate request map\n");
1994 return -ENOMEM;
1995 }
1996
1997 if (depth != set->queue_depth)
1998 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
1999 depth, set->queue_depth);
2000
2001 return 0;
2002}
2003
Jens Axboea4391c62014-06-05 15:21:56 -06002004/*
2005 * Alloc a tag set to be associated with one or more request queues.
2006 * May fail with EINVAL for various error conditions. May adjust the
2007 * requested depth down, if if it too large. In that case, the set
2008 * value will be stored in set->queue_depth.
2009 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002010int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2011{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002012 if (!set->nr_hw_queues)
2013 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002014 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002015 return -EINVAL;
2016 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2017 return -EINVAL;
2018
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02002019 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002020 return -EINVAL;
2021
Jens Axboea4391c62014-06-05 15:21:56 -06002022 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2023 pr_info("blk-mq: reduced tag depth to %u\n",
2024 BLK_MQ_MAX_DEPTH);
2025 set->queue_depth = BLK_MQ_MAX_DEPTH;
2026 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002027
Ming Lei48479002014-04-19 18:00:17 +08002028 set->tags = kmalloc_node(set->nr_hw_queues *
2029 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002030 GFP_KERNEL, set->numa_node);
2031 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002032 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002033
Jens Axboea5164402014-09-10 09:02:03 -06002034 if (blk_mq_alloc_rq_maps(set))
2035 goto enomem;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002036
Jens Axboe0d2602c2014-05-13 15:10:52 -06002037 mutex_init(&set->tag_list_lock);
2038 INIT_LIST_HEAD(&set->tag_list);
2039
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002040 return 0;
Jens Axboea5164402014-09-10 09:02:03 -06002041enomem:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002042 kfree(set->tags);
2043 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002044 return -ENOMEM;
2045}
2046EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2047
2048void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2049{
2050 int i;
2051
Jens Axboe484b4062014-05-21 14:01:15 -06002052 for (i = 0; i < set->nr_hw_queues; i++) {
2053 if (set->tags[i])
2054 blk_mq_free_rq_map(set, set->tags[i], i);
2055 }
2056
Ming Lei981bd182014-04-24 00:07:34 +08002057 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002058 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002059}
2060EXPORT_SYMBOL(blk_mq_free_tag_set);
2061
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002062int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2063{
2064 struct blk_mq_tag_set *set = q->tag_set;
2065 struct blk_mq_hw_ctx *hctx;
2066 int i, ret;
2067
2068 if (!set || nr > set->queue_depth)
2069 return -EINVAL;
2070
2071 ret = 0;
2072 queue_for_each_hw_ctx(q, hctx, i) {
2073 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2074 if (ret)
2075 break;
2076 }
2077
2078 if (!ret)
2079 q->nr_requests = nr;
2080
2081 return ret;
2082}
2083
Jens Axboe676141e2014-03-20 13:29:18 -06002084void blk_mq_disable_hotplug(void)
2085{
2086 mutex_lock(&all_q_mutex);
2087}
2088
2089void blk_mq_enable_hotplug(void)
2090{
2091 mutex_unlock(&all_q_mutex);
2092}
2093
Jens Axboe320ae512013-10-24 09:20:05 +01002094static int __init blk_mq_init(void)
2095{
Jens Axboe320ae512013-10-24 09:20:05 +01002096 blk_mq_cpu_init();
2097
Tejun Heoadd703f2014-07-01 10:34:38 -06002098 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002099
2100 return 0;
2101}
2102subsys_initcall(blk_mq_init);