blob: e862c4408427eff676e4ddf596e9fa3d31f3a758 [file] [log] [blame]
Jens Axboe320ae512013-10-24 09:20:05 +01001#include <linux/kernel.h>
2#include <linux/module.h>
3#include <linux/backing-dev.h>
4#include <linux/bio.h>
5#include <linux/blkdev.h>
6#include <linux/mm.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/workqueue.h>
10#include <linux/smp.h>
11#include <linux/llist.h>
12#include <linux/list_sort.h>
13#include <linux/cpu.h>
14#include <linux/cache.h>
15#include <linux/sched/sysctl.h>
16#include <linux/delay.h>
17
18#include <trace/events/block.h>
19
20#include <linux/blk-mq.h>
21#include "blk.h"
22#include "blk-mq.h"
23#include "blk-mq-tag.h"
24
25static DEFINE_MUTEX(all_q_mutex);
26static LIST_HEAD(all_q_list);
27
28static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
29
Jens Axboe320ae512013-10-24 09:20:05 +010030static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
31 unsigned int cpu)
32{
33 return per_cpu_ptr(q->queue_ctx, cpu);
34}
35
36/*
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
41 */
42static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
43{
44 return __blk_mq_get_ctx(q, get_cpu());
45}
46
47static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
48{
49 put_cpu();
50}
51
52/*
53 * Check if any of the ctx's have pending work in this hardware queue
54 */
55static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
56{
57 unsigned int i;
58
Jens Axboe1429d7c2014-05-19 09:23:55 -060059 for (i = 0; i < hctx->ctx_map.map_size; i++)
60 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010061 return true;
62
63 return false;
64}
65
Jens Axboe1429d7c2014-05-19 09:23:55 -060066static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
67 struct blk_mq_ctx *ctx)
68{
69 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
70}
71
72#define CTX_TO_BIT(hctx, ctx) \
73 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
74
Jens Axboe320ae512013-10-24 09:20:05 +010075/*
76 * Mark this ctx as having pending work in this hardware queue
77 */
78static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
79 struct blk_mq_ctx *ctx)
80{
Jens Axboe1429d7c2014-05-19 09:23:55 -060081 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
82
83 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
84 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
85}
86
87static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
88 struct blk_mq_ctx *ctx)
89{
90 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
91
92 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010093}
94
Christoph Hellwig081241e2014-02-20 15:32:36 -080095static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
Jens Axboe4bb659b2014-05-09 09:36:49 -060096 struct blk_mq_ctx *ctx,
Christoph Hellwig081241e2014-02-20 15:32:36 -080097 gfp_t gfp, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +010098{
99 struct request *rq;
100 unsigned int tag;
101
Jens Axboe4bb659b2014-05-09 09:36:49 -0600102 tag = blk_mq_get_tag(hctx->tags, hctx, &ctx->last_tag, gfp, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100103 if (tag != BLK_MQ_TAG_FAIL) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600104 rq = hctx->tags->rqs[tag];
Jens Axboe320ae512013-10-24 09:20:05 +0100105 rq->tag = tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100106 return rq;
107 }
108
109 return NULL;
110}
111
112static int blk_mq_queue_enter(struct request_queue *q)
113{
114 int ret;
115
116 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
117 smp_wmb();
118 /* we have problems to freeze the queue if it's initializing */
119 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
120 return 0;
121
122 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
123
124 spin_lock_irq(q->queue_lock);
125 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800126 !blk_queue_bypass(q) || blk_queue_dying(q),
127 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100128 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800129 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100130 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800131 else if (blk_queue_dying(q))
132 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100133 spin_unlock_irq(q->queue_lock);
134
135 return ret;
136}
137
138static void blk_mq_queue_exit(struct request_queue *q)
139{
140 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
141}
142
Ming Lei43a5e4e2013-12-26 21:31:35 +0800143static void __blk_mq_drain_queue(struct request_queue *q)
144{
145 while (true) {
146 s64 count;
147
148 spin_lock_irq(q->queue_lock);
149 count = percpu_counter_sum(&q->mq_usage_counter);
150 spin_unlock_irq(q->queue_lock);
151
152 if (count == 0)
153 break;
154 blk_mq_run_queues(q, false);
155 msleep(10);
156 }
157}
158
Jens Axboe320ae512013-10-24 09:20:05 +0100159/*
160 * Guarantee no request is in use, so we can change any data structure of
161 * the queue afterward.
162 */
163static void blk_mq_freeze_queue(struct request_queue *q)
164{
165 bool drain;
166
167 spin_lock_irq(q->queue_lock);
168 drain = !q->bypass_depth++;
169 queue_flag_set(QUEUE_FLAG_BYPASS, q);
170 spin_unlock_irq(q->queue_lock);
171
Ming Lei43a5e4e2013-12-26 21:31:35 +0800172 if (drain)
173 __blk_mq_drain_queue(q);
174}
Jens Axboe320ae512013-10-24 09:20:05 +0100175
Ming Lei43a5e4e2013-12-26 21:31:35 +0800176void blk_mq_drain_queue(struct request_queue *q)
177{
178 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100179}
180
181static void blk_mq_unfreeze_queue(struct request_queue *q)
182{
183 bool wake = false;
184
185 spin_lock_irq(q->queue_lock);
186 if (!--q->bypass_depth) {
187 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
188 wake = true;
189 }
190 WARN_ON_ONCE(q->bypass_depth < 0);
191 spin_unlock_irq(q->queue_lock);
192 if (wake)
193 wake_up_all(&q->mq_freeze_wq);
194}
195
196bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
197{
198 return blk_mq_has_free_tags(hctx->tags);
199}
200EXPORT_SYMBOL(blk_mq_can_queue);
201
Jens Axboe94eddfb2013-11-19 09:25:07 -0700202static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
203 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100204{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700205 if (blk_queue_io_stat(q))
206 rw_flags |= REQ_IO_STAT;
207
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200208 INIT_LIST_HEAD(&rq->queuelist);
209 /* csd/requeue_work/fifo_time is initialized before use */
210 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100211 rq->mq_ctx = ctx;
212 rq->cmd_flags = rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200213 rq->cmd_type = 0;
214 /* do not touch atomic flags, it needs atomic ops against the timer */
215 rq->cpu = -1;
216 rq->__data_len = 0;
217 rq->__sector = (sector_t) -1;
218 rq->bio = NULL;
219 rq->biotail = NULL;
220 INIT_HLIST_NODE(&rq->hash);
221 RB_CLEAR_NODE(&rq->rb_node);
222 memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv)));
223 rq->rq_disk = NULL;
224 rq->part = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700225 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200226#ifdef CONFIG_BLK_CGROUP
227 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700228 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200229 rq->io_start_time_ns = 0;
230#endif
231 rq->nr_phys_segments = 0;
232#if defined(CONFIG_BLK_DEV_INTEGRITY)
233 rq->nr_integrity_segments = 0;
234#endif
235 rq->ioprio = 0;
236 rq->special = NULL;
237 /* tag was already set */
238 rq->errors = 0;
239 memset(rq->__cmd, 0, sizeof(rq->__cmd));
240 rq->cmd = rq->__cmd;
241 rq->cmd_len = BLK_MAX_CDB;
242
243 rq->extra_len = 0;
244 rq->sense_len = 0;
245 rq->resid_len = 0;
246 rq->sense = NULL;
247
248 rq->deadline = 0;
249 INIT_LIST_HEAD(&rq->timeout_list);
250 rq->timeout = 0;
251 rq->retries = 0;
252 rq->end_io = NULL;
253 rq->end_io_data = NULL;
254 rq->next_rq = NULL;
255
Jens Axboe320ae512013-10-24 09:20:05 +0100256 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
257}
258
Jens Axboe320ae512013-10-24 09:20:05 +0100259static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
260 int rw, gfp_t gfp,
261 bool reserved)
262{
263 struct request *rq;
264
265 do {
266 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
267 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
268
Jens Axboe4bb659b2014-05-09 09:36:49 -0600269 rq = __blk_mq_alloc_request(hctx, ctx, gfp & ~__GFP_WAIT,
270 reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100271 if (rq) {
Jens Axboe94eddfb2013-11-19 09:25:07 -0700272 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100273 break;
Jeff Moyer959a35f2013-12-03 14:23:00 -0700274 }
Jens Axboe320ae512013-10-24 09:20:05 +0100275
Jens Axboee4043dc2014-04-09 10:18:23 -0600276 if (gfp & __GFP_WAIT) {
277 __blk_mq_run_hw_queue(hctx);
278 blk_mq_put_ctx(ctx);
279 } else {
280 blk_mq_put_ctx(ctx);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700281 break;
Jens Axboee4043dc2014-04-09 10:18:23 -0600282 }
Jeff Moyer959a35f2013-12-03 14:23:00 -0700283
Jens Axboe4bb659b2014-05-09 09:36:49 -0600284 blk_mq_wait_for_tags(hctx->tags, hctx, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100285 } while (1);
286
287 return rq;
288}
289
Christoph Hellwig18741982014-02-10 09:29:00 -0700290struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100291{
292 struct request *rq;
293
294 if (blk_mq_queue_enter(q))
295 return NULL;
296
Christoph Hellwig18741982014-02-10 09:29:00 -0700297 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700298 if (rq)
299 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100300 return rq;
301}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600302EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100303
304struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
305 gfp_t gfp)
306{
307 struct request *rq;
308
309 if (blk_mq_queue_enter(q))
310 return NULL;
311
312 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700313 if (rq)
314 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100315 return rq;
316}
317EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
318
Jens Axboe320ae512013-10-24 09:20:05 +0100319static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
320 struct blk_mq_ctx *ctx, struct request *rq)
321{
322 const int tag = rq->tag;
323 struct request_queue *q = rq->q;
324
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200325 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600326 blk_mq_put_tag(hctx->tags, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100327 blk_mq_queue_exit(q);
328}
329
330void blk_mq_free_request(struct request *rq)
331{
332 struct blk_mq_ctx *ctx = rq->mq_ctx;
333 struct blk_mq_hw_ctx *hctx;
334 struct request_queue *q = rq->q;
335
336 ctx->rq_completed[rq_is_sync(rq)]++;
337
338 hctx = q->mq_ops->map_queue(q, ctx->cpu);
339 __blk_mq_free_request(hctx, ctx, rq);
340}
341
Christoph Hellwig8727af42014-04-14 10:30:08 +0200342/*
343 * Clone all relevant state from a request that has been put on hold in
344 * the flush state machine into the preallocated flush request that hangs
345 * off the request queue.
346 *
347 * For a driver the flush request should be invisible, that's why we are
348 * impersonating the original request here.
349 */
350void blk_mq_clone_flush_request(struct request *flush_rq,
351 struct request *orig_rq)
352{
353 struct blk_mq_hw_ctx *hctx =
354 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
355
356 flush_rq->mq_ctx = orig_rq->mq_ctx;
357 flush_rq->tag = orig_rq->tag;
358 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
359 hctx->cmd_size);
360}
361
Christoph Hellwig63151a42014-04-16 09:44:52 +0200362inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100363{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700364 blk_account_io_done(rq);
365
Christoph Hellwig91b63632014-04-16 09:44:53 +0200366 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100367 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200368 } else {
369 if (unlikely(blk_bidi_rq(rq)))
370 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100371 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200372 }
Jens Axboe320ae512013-10-24 09:20:05 +0100373}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200374EXPORT_SYMBOL(__blk_mq_end_io);
375
376void blk_mq_end_io(struct request *rq, int error)
377{
378 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
379 BUG();
380 __blk_mq_end_io(rq, error);
381}
382EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100383
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800384static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100385{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800386 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100387
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800388 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100389}
390
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800391void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100392{
393 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700394 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100395 int cpu;
396
Christoph Hellwig38535202014-04-25 02:32:53 -0700397 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800398 rq->q->softirq_done_fn(rq);
399 return;
400 }
Jens Axboe320ae512013-10-24 09:20:05 +0100401
402 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700403 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
404 shared = cpus_share_cache(cpu, ctx->cpu);
405
406 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800407 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800408 rq->csd.info = rq;
409 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100410 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800411 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800412 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800413 }
Jens Axboe320ae512013-10-24 09:20:05 +0100414 put_cpu();
415}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800416
417/**
418 * blk_mq_complete_request - end I/O on a request
419 * @rq: the request being processed
420 *
421 * Description:
422 * Ends all I/O on a request. It does not handle partial completions.
423 * The actual completion happens out-of-order, through a IPI handler.
424 **/
425void blk_mq_complete_request(struct request *rq)
426{
427 if (unlikely(blk_should_fake_timeout(rq->q)))
428 return;
429 if (!blk_mark_rq_complete(rq))
430 __blk_mq_complete_request(rq);
431}
432EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100433
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800434static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100435{
436 struct request_queue *q = rq->q;
437
438 trace_block_rq_issue(q, rq);
439
Christoph Hellwig742ee692014-04-14 10:30:06 +0200440 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200441 if (unlikely(blk_bidi_rq(rq)))
442 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200443
Jens Axboe320ae512013-10-24 09:20:05 +0100444 /*
445 * Just mark start time and set the started bit. Due to memory
446 * ordering, we know we'll see the correct deadline as long as
447 * REQ_ATOMIC_STARTED is seen.
448 */
449 rq->deadline = jiffies + q->rq_timeout;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600450
451 /*
452 * Mark us as started and clear complete. Complete might have been
453 * set if requeue raced with timeout, which then marked it as
454 * complete. So be sure to clear complete again when we start
455 * the request, otherwise we'll ignore the completion event.
456 */
Jens Axboe320ae512013-10-24 09:20:05 +0100457 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600458 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800459
460 if (q->dma_drain_size && blk_rq_bytes(rq)) {
461 /*
462 * Make sure space for the drain appears. We know we can do
463 * this because max_hw_segments has been adjusted to be one
464 * fewer than the device can handle.
465 */
466 rq->nr_phys_segments++;
467 }
468
469 /*
470 * Flag the last request in the series so that drivers know when IO
471 * should be kicked off, if they don't do it on a per-request basis.
472 *
473 * Note: the flag isn't the only condition drivers should do kick off.
474 * If drive is busy, the last request might not have the bit set.
475 */
476 if (last)
477 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100478}
479
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200480static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100481{
482 struct request_queue *q = rq->q;
483
484 trace_block_rq_requeue(q, rq);
485 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800486
487 rq->cmd_flags &= ~REQ_END;
488
489 if (q->dma_drain_size && blk_rq_bytes(rq))
490 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100491}
492
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200493void blk_mq_requeue_request(struct request *rq)
494{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200495 __blk_mq_requeue_request(rq);
496 blk_clear_rq_complete(rq);
497
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200498 BUG_ON(blk_queued_rq(rq));
499 blk_mq_insert_request(rq, true, true, false);
500}
501EXPORT_SYMBOL(blk_mq_requeue_request);
502
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600503struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
504{
505 return tags->rqs[tag];
506}
507EXPORT_SYMBOL(blk_mq_tag_to_rq);
508
Jens Axboe320ae512013-10-24 09:20:05 +0100509struct blk_mq_timeout_data {
510 struct blk_mq_hw_ctx *hctx;
511 unsigned long *next;
512 unsigned int *next_set;
513};
514
515static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
516{
517 struct blk_mq_timeout_data *data = __data;
518 struct blk_mq_hw_ctx *hctx = data->hctx;
519 unsigned int tag;
520
521 /* It may not be in flight yet (this is where
522 * the REQ_ATOMIC_STARTED flag comes in). The requests are
523 * statically allocated, so we know it's always safe to access the
524 * memory associated with a bit offset into ->rqs[].
525 */
526 tag = 0;
527 do {
528 struct request *rq;
529
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600530 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
531 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100532 break;
533
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600534 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
535 if (rq->q != hctx->queue)
536 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100537 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
538 continue;
539
540 blk_rq_check_expired(rq, data->next, data->next_set);
541 } while (1);
542}
543
544static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
545 unsigned long *next,
546 unsigned int *next_set)
547{
548 struct blk_mq_timeout_data data = {
549 .hctx = hctx,
550 .next = next,
551 .next_set = next_set,
552 };
553
554 /*
555 * Ask the tagging code to iterate busy requests, so we can
556 * check them for timeout.
557 */
558 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
559}
560
Jens Axboe87ee7b12014-04-24 08:51:47 -0600561static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
562{
563 struct request_queue *q = rq->q;
564
565 /*
566 * We know that complete is set at this point. If STARTED isn't set
567 * anymore, then the request isn't active and the "timeout" should
568 * just be ignored. This can happen due to the bitflag ordering.
569 * Timeout first checks if STARTED is set, and if it is, assumes
570 * the request is active. But if we race with completion, then
571 * we both flags will get cleared. So check here again, and ignore
572 * a timeout event with a request that isn't active.
573 */
574 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
575 return BLK_EH_NOT_HANDLED;
576
577 if (!q->mq_ops->timeout)
578 return BLK_EH_RESET_TIMER;
579
580 return q->mq_ops->timeout(rq);
581}
582
Jens Axboe320ae512013-10-24 09:20:05 +0100583static void blk_mq_rq_timer(unsigned long data)
584{
585 struct request_queue *q = (struct request_queue *) data;
586 struct blk_mq_hw_ctx *hctx;
587 unsigned long next = 0;
588 int i, next_set = 0;
589
590 queue_for_each_hw_ctx(q, hctx, i)
591 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
592
593 if (next_set)
594 mod_timer(&q->timeout, round_jiffies_up(next));
595}
596
597/*
598 * Reverse check our software queue for entries that we could potentially
599 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
600 * too much time checking for merges.
601 */
602static bool blk_mq_attempt_merge(struct request_queue *q,
603 struct blk_mq_ctx *ctx, struct bio *bio)
604{
605 struct request *rq;
606 int checked = 8;
607
608 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
609 int el_ret;
610
611 if (!checked--)
612 break;
613
614 if (!blk_rq_merge_ok(rq, bio))
615 continue;
616
617 el_ret = blk_try_merge(rq, bio);
618 if (el_ret == ELEVATOR_BACK_MERGE) {
619 if (bio_attempt_back_merge(q, rq, bio)) {
620 ctx->rq_merged++;
621 return true;
622 }
623 break;
624 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
625 if (bio_attempt_front_merge(q, rq, bio)) {
626 ctx->rq_merged++;
627 return true;
628 }
629 break;
630 }
631 }
632
633 return false;
634}
635
Jens Axboe320ae512013-10-24 09:20:05 +0100636/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600637 * Process software queues that have been marked busy, splicing them
638 * to the for-dispatch
639 */
640static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
641{
642 struct blk_mq_ctx *ctx;
643 int i;
644
645 for (i = 0; i < hctx->ctx_map.map_size; i++) {
646 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
647 unsigned int off, bit;
648
649 if (!bm->word)
650 continue;
651
652 bit = 0;
653 off = i * hctx->ctx_map.bits_per_word;
654 do {
655 bit = find_next_bit(&bm->word, bm->depth, bit);
656 if (bit >= bm->depth)
657 break;
658
659 ctx = hctx->ctxs[bit + off];
660 clear_bit(bit, &bm->word);
661 spin_lock(&ctx->lock);
662 list_splice_tail_init(&ctx->rq_list, list);
663 spin_unlock(&ctx->lock);
664
665 bit++;
666 } while (1);
667 }
668}
669
670/*
Jens Axboe320ae512013-10-24 09:20:05 +0100671 * Run this hardware queue, pulling any software queues mapped to it in.
672 * Note that this function currently has various problems around ordering
673 * of IO. In particular, we'd like FIFO behaviour on handling existing
674 * items on the hctx->dispatch list. Ignore that for now.
675 */
676static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
677{
678 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100679 struct request *rq;
680 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600681 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100682
Jens Axboefd1270d2014-04-16 09:23:48 -0600683 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600684
Jens Axboe5d12f902014-03-19 15:25:02 -0600685 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100686 return;
687
688 hctx->run++;
689
690 /*
691 * Touch any software queue that has pending entries.
692 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600693 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100694
695 /*
696 * If we have previous entries on our dispatch list, grab them
697 * and stuff them at the front for more fair dispatch.
698 */
699 if (!list_empty_careful(&hctx->dispatch)) {
700 spin_lock(&hctx->lock);
701 if (!list_empty(&hctx->dispatch))
702 list_splice_init(&hctx->dispatch, &rq_list);
703 spin_unlock(&hctx->lock);
704 }
705
706 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100707 * Now process all the entries, sending them to the driver.
708 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600709 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100710 while (!list_empty(&rq_list)) {
711 int ret;
712
713 rq = list_first_entry(&rq_list, struct request, queuelist);
714 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100715
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800716 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100717
718 ret = q->mq_ops->queue_rq(hctx, rq);
719 switch (ret) {
720 case BLK_MQ_RQ_QUEUE_OK:
721 queued++;
722 continue;
723 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100724 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200725 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100726 break;
727 default:
728 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100729 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800730 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100731 blk_mq_end_io(rq, rq->errors);
732 break;
733 }
734
735 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
736 break;
737 }
738
739 if (!queued)
740 hctx->dispatched[0]++;
741 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
742 hctx->dispatched[ilog2(queued) + 1]++;
743
744 /*
745 * Any items that need requeuing? Stuff them into hctx->dispatch,
746 * that is where we will continue on next queue run.
747 */
748 if (!list_empty(&rq_list)) {
749 spin_lock(&hctx->lock);
750 list_splice(&rq_list, &hctx->dispatch);
751 spin_unlock(&hctx->lock);
752 }
753}
754
Jens Axboe506e9312014-05-07 10:26:44 -0600755/*
756 * It'd be great if the workqueue API had a way to pass
757 * in a mask and had some smarts for more clever placement.
758 * For now we just round-robin here, switching for every
759 * BLK_MQ_CPU_WORK_BATCH queued items.
760 */
761static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
762{
763 int cpu = hctx->next_cpu;
764
765 if (--hctx->next_cpu_batch <= 0) {
766 int next_cpu;
767
768 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
769 if (next_cpu >= nr_cpu_ids)
770 next_cpu = cpumask_first(hctx->cpumask);
771
772 hctx->next_cpu = next_cpu;
773 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
774 }
775
776 return cpu;
777}
778
Jens Axboe320ae512013-10-24 09:20:05 +0100779void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
780{
Jens Axboe5d12f902014-03-19 15:25:02 -0600781 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100782 return;
783
Jens Axboee4043dc2014-04-09 10:18:23 -0600784 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100785 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600786 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600787 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600788 else {
789 unsigned int cpu;
790
Jens Axboe506e9312014-05-07 10:26:44 -0600791 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600792 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600793 }
Jens Axboe320ae512013-10-24 09:20:05 +0100794}
795
796void blk_mq_run_queues(struct request_queue *q, bool async)
797{
798 struct blk_mq_hw_ctx *hctx;
799 int i;
800
801 queue_for_each_hw_ctx(q, hctx, i) {
802 if ((!blk_mq_hctx_has_pending(hctx) &&
803 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600804 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100805 continue;
806
Jens Axboee4043dc2014-04-09 10:18:23 -0600807 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100808 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600809 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100810 }
811}
812EXPORT_SYMBOL(blk_mq_run_queues);
813
814void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
815{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600816 cancel_delayed_work(&hctx->run_work);
817 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100818 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
819}
820EXPORT_SYMBOL(blk_mq_stop_hw_queue);
821
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100822void blk_mq_stop_hw_queues(struct request_queue *q)
823{
824 struct blk_mq_hw_ctx *hctx;
825 int i;
826
827 queue_for_each_hw_ctx(q, hctx, i)
828 blk_mq_stop_hw_queue(hctx);
829}
830EXPORT_SYMBOL(blk_mq_stop_hw_queues);
831
Jens Axboe320ae512013-10-24 09:20:05 +0100832void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
833{
834 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600835
836 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100837 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600838 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100839}
840EXPORT_SYMBOL(blk_mq_start_hw_queue);
841
Christoph Hellwig2f268552014-04-16 09:44:56 +0200842void blk_mq_start_hw_queues(struct request_queue *q)
843{
844 struct blk_mq_hw_ctx *hctx;
845 int i;
846
847 queue_for_each_hw_ctx(q, hctx, i)
848 blk_mq_start_hw_queue(hctx);
849}
850EXPORT_SYMBOL(blk_mq_start_hw_queues);
851
852
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200853void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100854{
855 struct blk_mq_hw_ctx *hctx;
856 int i;
857
858 queue_for_each_hw_ctx(q, hctx, i) {
859 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
860 continue;
861
862 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600863 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200864 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600865 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100866 }
867}
868EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
869
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600870static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100871{
872 struct blk_mq_hw_ctx *hctx;
873
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600874 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600875
Jens Axboe320ae512013-10-24 09:20:05 +0100876 __blk_mq_run_hw_queue(hctx);
877}
878
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600879static void blk_mq_delay_work_fn(struct work_struct *work)
880{
881 struct blk_mq_hw_ctx *hctx;
882
883 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
884
885 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
886 __blk_mq_run_hw_queue(hctx);
887}
888
889void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
890{
891 unsigned long tmo = msecs_to_jiffies(msecs);
892
893 if (hctx->queue->nr_hw_queues == 1)
894 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
895 else {
896 unsigned int cpu;
897
Jens Axboe506e9312014-05-07 10:26:44 -0600898 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600899 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
900 }
901}
902EXPORT_SYMBOL(blk_mq_delay_queue);
903
Jens Axboe320ae512013-10-24 09:20:05 +0100904static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800905 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100906{
907 struct blk_mq_ctx *ctx = rq->mq_ctx;
908
Jens Axboe01b983c2013-11-19 18:59:10 -0700909 trace_block_rq_insert(hctx->queue, rq);
910
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800911 if (at_head)
912 list_add(&rq->queuelist, &ctx->rq_list);
913 else
914 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600915
Jens Axboe320ae512013-10-24 09:20:05 +0100916 blk_mq_hctx_mark_pending(hctx, ctx);
917
918 /*
919 * We do this early, to ensure we are on the right CPU.
920 */
Jens Axboe87ee7b12014-04-24 08:51:47 -0600921 blk_add_timer(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100922}
923
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600924void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
925 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100926{
927 struct request_queue *q = rq->q;
928 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600929 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100930
931 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600932 if (!cpu_online(ctx->cpu))
933 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100934
Jens Axboe320ae512013-10-24 09:20:05 +0100935 hctx = q->mq_ops->map_queue(q, ctx->cpu);
936
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600937 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
938 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
939 blk_insert_flush(rq);
940 } else {
941 spin_lock(&ctx->lock);
942 __blk_mq_insert_request(hctx, rq, at_head);
943 spin_unlock(&ctx->lock);
944 }
Jens Axboe320ae512013-10-24 09:20:05 +0100945
Jens Axboe320ae512013-10-24 09:20:05 +0100946 if (run_queue)
947 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600948
949 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100950}
951
952static void blk_mq_insert_requests(struct request_queue *q,
953 struct blk_mq_ctx *ctx,
954 struct list_head *list,
955 int depth,
956 bool from_schedule)
957
958{
959 struct blk_mq_hw_ctx *hctx;
960 struct blk_mq_ctx *current_ctx;
961
962 trace_block_unplug(q, depth, !from_schedule);
963
964 current_ctx = blk_mq_get_ctx(q);
965
966 if (!cpu_online(ctx->cpu))
967 ctx = current_ctx;
968 hctx = q->mq_ops->map_queue(q, ctx->cpu);
969
970 /*
971 * preemption doesn't flush plug list, so it's possible ctx->cpu is
972 * offline now
973 */
974 spin_lock(&ctx->lock);
975 while (!list_empty(list)) {
976 struct request *rq;
977
978 rq = list_first_entry(list, struct request, queuelist);
979 list_del_init(&rq->queuelist);
980 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800981 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100982 }
983 spin_unlock(&ctx->lock);
984
Jens Axboe320ae512013-10-24 09:20:05 +0100985 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600986 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100987}
988
989static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
990{
991 struct request *rqa = container_of(a, struct request, queuelist);
992 struct request *rqb = container_of(b, struct request, queuelist);
993
994 return !(rqa->mq_ctx < rqb->mq_ctx ||
995 (rqa->mq_ctx == rqb->mq_ctx &&
996 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
997}
998
999void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1000{
1001 struct blk_mq_ctx *this_ctx;
1002 struct request_queue *this_q;
1003 struct request *rq;
1004 LIST_HEAD(list);
1005 LIST_HEAD(ctx_list);
1006 unsigned int depth;
1007
1008 list_splice_init(&plug->mq_list, &list);
1009
1010 list_sort(NULL, &list, plug_ctx_cmp);
1011
1012 this_q = NULL;
1013 this_ctx = NULL;
1014 depth = 0;
1015
1016 while (!list_empty(&list)) {
1017 rq = list_entry_rq(list.next);
1018 list_del_init(&rq->queuelist);
1019 BUG_ON(!rq->q);
1020 if (rq->mq_ctx != this_ctx) {
1021 if (this_ctx) {
1022 blk_mq_insert_requests(this_q, this_ctx,
1023 &ctx_list, depth,
1024 from_schedule);
1025 }
1026
1027 this_ctx = rq->mq_ctx;
1028 this_q = rq->q;
1029 depth = 0;
1030 }
1031
1032 depth++;
1033 list_add_tail(&rq->queuelist, &ctx_list);
1034 }
1035
1036 /*
1037 * If 'this_ctx' is set, we know we have entries to complete
1038 * on 'ctx_list'. Do those.
1039 */
1040 if (this_ctx) {
1041 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1042 from_schedule);
1043 }
1044}
1045
1046static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1047{
1048 init_request_from_bio(rq, bio);
1049 blk_account_io_start(rq, 1);
1050}
1051
1052static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1053{
1054 struct blk_mq_hw_ctx *hctx;
1055 struct blk_mq_ctx *ctx;
1056 const int is_sync = rw_is_sync(bio->bi_rw);
1057 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1058 int rw = bio_data_dir(bio);
1059 struct request *rq;
1060 unsigned int use_plug, request_count = 0;
1061
1062 /*
1063 * If we have multiple hardware queues, just go directly to
1064 * one of those for sync IO.
1065 */
1066 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
1067
1068 blk_queue_bounce(q, &bio);
1069
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001070 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1071 bio_endio(bio, -EIO);
1072 return;
1073 }
1074
Jens Axboe320ae512013-10-24 09:20:05 +01001075 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
1076 return;
1077
1078 if (blk_mq_queue_enter(q)) {
1079 bio_endio(bio, -EIO);
1080 return;
1081 }
1082
1083 ctx = blk_mq_get_ctx(q);
1084 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1085
Shaohua Li27fbf4e2014-02-19 20:20:21 +08001086 if (is_sync)
1087 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +01001088 trace_block_getrq(q, bio, rw);
Jens Axboe4bb659b2014-05-09 09:36:49 -06001089 rq = __blk_mq_alloc_request(hctx, ctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001090 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -07001091 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +01001092 else {
1093 blk_mq_put_ctx(ctx);
1094 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -07001095 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
1096 false);
Jens Axboe320ae512013-10-24 09:20:05 +01001097 ctx = rq->mq_ctx;
1098 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1099 }
1100
1101 hctx->queued++;
1102
1103 if (unlikely(is_flush_fua)) {
1104 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001105 blk_insert_flush(rq);
1106 goto run_queue;
1107 }
1108
1109 /*
1110 * A task plug currently exists. Since this is completely lockless,
1111 * utilize that to temporarily store requests until the task is
1112 * either done or scheduled away.
1113 */
1114 if (use_plug) {
1115 struct blk_plug *plug = current->plug;
1116
1117 if (plug) {
1118 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001119 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001120 trace_block_plug(q);
1121 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1122 blk_flush_plug_list(plug, false);
1123 trace_block_plug(q);
1124 }
1125 list_add_tail(&rq->queuelist, &plug->mq_list);
1126 blk_mq_put_ctx(ctx);
1127 return;
1128 }
1129 }
1130
Jens Axboec6d600c2014-04-30 13:43:56 -06001131 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
Jens Axboecf4b50a2014-05-09 14:54:08 -06001132 blk_mq_bio_to_request(rq, bio);
Jens Axboec6d600c2014-04-30 13:43:56 -06001133 spin_lock(&ctx->lock);
1134insert_rq:
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001135 __blk_mq_insert_request(hctx, rq, false);
Jens Axboec6d600c2014-04-30 13:43:56 -06001136 spin_unlock(&ctx->lock);
Jens Axboec6d600c2014-04-30 13:43:56 -06001137 } else {
1138 spin_lock(&ctx->lock);
1139 if (!blk_mq_attempt_merge(q, ctx, bio)) {
Jens Axboecf4b50a2014-05-09 14:54:08 -06001140 blk_mq_bio_to_request(rq, bio);
Jens Axboec6d600c2014-04-30 13:43:56 -06001141 goto insert_rq;
1142 }
1143
1144 spin_unlock(&ctx->lock);
1145 __blk_mq_free_request(hctx, ctx, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001146 }
1147
Jens Axboe320ae512013-10-24 09:20:05 +01001148
1149 /*
1150 * For a SYNC request, send it to the hardware immediately. For an
1151 * ASYNC request, just ensure that we run it later on. The latter
1152 * allows for merging opportunities and more efficient dispatching.
1153 */
1154run_queue:
1155 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -06001156 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001157}
1158
1159/*
1160 * Default mapping to a software queue, since we use one per CPU.
1161 */
1162struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1163{
1164 return q->queue_hw_ctx[q->mq_map[cpu]];
1165}
1166EXPORT_SYMBOL(blk_mq_map_queue);
1167
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001168struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
Jens Axboe320ae512013-10-24 09:20:05 +01001169 unsigned int hctx_index)
1170{
Jens Axboe4bb659b2014-05-09 09:36:49 -06001171 return kzalloc_node(sizeof(struct blk_mq_hw_ctx), GFP_KERNEL,
1172 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001173}
1174EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1175
1176void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1177 unsigned int hctx_index)
1178{
1179 kfree(hctx);
1180}
1181EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1182
1183static void blk_mq_hctx_notify(void *data, unsigned long action,
1184 unsigned int cpu)
1185{
1186 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -06001187 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +01001188 struct blk_mq_ctx *ctx;
1189 LIST_HEAD(tmp);
1190
1191 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1192 return;
1193
1194 /*
1195 * Move ctx entries to new CPU, if this one is going away.
1196 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001197 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001198
1199 spin_lock(&ctx->lock);
1200 if (!list_empty(&ctx->rq_list)) {
1201 list_splice_init(&ctx->rq_list, &tmp);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001202 blk_mq_hctx_clear_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001203 }
1204 spin_unlock(&ctx->lock);
1205
1206 if (list_empty(&tmp))
1207 return;
1208
Jens Axboebccb5f72014-04-04 21:34:48 -06001209 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001210 spin_lock(&ctx->lock);
1211
1212 while (!list_empty(&tmp)) {
1213 struct request *rq;
1214
1215 rq = list_first_entry(&tmp, struct request, queuelist);
1216 rq->mq_ctx = ctx;
1217 list_move_tail(&rq->queuelist, &ctx->rq_list);
1218 }
1219
Jens Axboebccb5f72014-04-04 21:34:48 -06001220 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001221 blk_mq_hctx_mark_pending(hctx, ctx);
1222
1223 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001224
1225 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001226 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001227}
1228
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001229static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1230 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001231{
1232 struct page *page;
1233
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001234 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001235 int i;
1236
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001237 for (i = 0; i < tags->nr_tags; i++) {
1238 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001239 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001240 set->ops->exit_request(set->driver_data, tags->rqs[i],
1241 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001242 }
1243 }
1244
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001245 while (!list_empty(&tags->page_list)) {
1246 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001247 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001248 __free_pages(page, page->private);
1249 }
1250
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001251 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001252
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001253 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001254}
1255
1256static size_t order_to_size(unsigned int order)
1257{
Ming Lei4ca08502014-04-19 18:00:18 +08001258 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001259}
1260
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001261static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1262 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001263{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001264 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001265 unsigned int i, j, entries_per_page, max_order = 4;
1266 size_t rq_size, left;
1267
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001268 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1269 set->numa_node);
1270 if (!tags)
1271 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001272
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001273 INIT_LIST_HEAD(&tags->page_list);
1274
1275 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1276 GFP_KERNEL, set->numa_node);
1277 if (!tags->rqs) {
1278 blk_mq_free_tags(tags);
1279 return NULL;
1280 }
Jens Axboe320ae512013-10-24 09:20:05 +01001281
1282 /*
1283 * rq_size is the size of the request plus driver payload, rounded
1284 * to the cacheline size
1285 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001286 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001287 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001288 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001289
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001290 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001291 int this_order = max_order;
1292 struct page *page;
1293 int to_do;
1294 void *p;
1295
1296 while (left < order_to_size(this_order - 1) && this_order)
1297 this_order--;
1298
1299 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001300 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1301 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001302 if (page)
1303 break;
1304 if (!this_order--)
1305 break;
1306 if (order_to_size(this_order) < rq_size)
1307 break;
1308 } while (1);
1309
1310 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001311 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001312
1313 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001314 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001315
1316 p = page_address(page);
1317 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001318 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001319 left -= to_do * rq_size;
1320 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001321 tags->rqs[i] = p;
1322 if (set->ops->init_request) {
1323 if (set->ops->init_request(set->driver_data,
1324 tags->rqs[i], hctx_idx, i,
1325 set->numa_node))
1326 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001327 }
1328
Jens Axboe320ae512013-10-24 09:20:05 +01001329 p += rq_size;
1330 i++;
1331 }
1332 }
1333
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001334 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001335
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001336fail:
1337 pr_warn("%s: failed to allocate requests\n", __func__);
1338 blk_mq_free_rq_map(set, tags, hctx_idx);
1339 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001340}
1341
Jens Axboe1429d7c2014-05-19 09:23:55 -06001342static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1343{
1344 kfree(bitmap->map);
1345}
1346
1347static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1348{
1349 unsigned int bpw = 8, total, num_maps, i;
1350
1351 bitmap->bits_per_word = bpw;
1352
1353 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1354 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1355 GFP_KERNEL, node);
1356 if (!bitmap->map)
1357 return -ENOMEM;
1358
1359 bitmap->map_size = num_maps;
1360
1361 total = nr_cpu_ids;
1362 for (i = 0; i < num_maps; i++) {
1363 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1364 total -= bitmap->map[i].depth;
1365 }
1366
1367 return 0;
1368}
1369
Jens Axboe320ae512013-10-24 09:20:05 +01001370static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001371 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001372{
1373 struct blk_mq_hw_ctx *hctx;
1374 unsigned int i, j;
1375
1376 /*
1377 * Initialize hardware queues
1378 */
1379 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001380 int node;
1381
1382 node = hctx->numa_node;
1383 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001384 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001385
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001386 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1387 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001388 spin_lock_init(&hctx->lock);
1389 INIT_LIST_HEAD(&hctx->dispatch);
1390 hctx->queue = q;
1391 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001392 hctx->flags = set->flags;
1393 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001394
1395 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1396 blk_mq_hctx_notify, hctx);
1397 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1398
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001399 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001400
1401 /*
1402 * Allocate space for all possible cpus to avoid allocation in
1403 * runtime
1404 */
1405 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1406 GFP_KERNEL, node);
1407 if (!hctx->ctxs)
1408 break;
1409
Jens Axboe1429d7c2014-05-19 09:23:55 -06001410 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001411 break;
1412
Jens Axboe320ae512013-10-24 09:20:05 +01001413 hctx->nr_ctx = 0;
1414
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001415 if (set->ops->init_hctx &&
1416 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001417 break;
1418 }
1419
1420 if (i == q->nr_hw_queues)
1421 return 0;
1422
1423 /*
1424 * Init failed
1425 */
1426 queue_for_each_hw_ctx(q, hctx, j) {
1427 if (i == j)
1428 break;
1429
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001430 if (set->ops->exit_hctx)
1431 set->ops->exit_hctx(hctx, j);
Jens Axboe320ae512013-10-24 09:20:05 +01001432
1433 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Jens Axboe320ae512013-10-24 09:20:05 +01001434 kfree(hctx->ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001435 blk_mq_free_bitmap(&hctx->ctx_map);
Jens Axboe320ae512013-10-24 09:20:05 +01001436 }
1437
1438 return 1;
1439}
1440
1441static void blk_mq_init_cpu_queues(struct request_queue *q,
1442 unsigned int nr_hw_queues)
1443{
1444 unsigned int i;
1445
1446 for_each_possible_cpu(i) {
1447 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1448 struct blk_mq_hw_ctx *hctx;
1449
1450 memset(__ctx, 0, sizeof(*__ctx));
1451 __ctx->cpu = i;
1452 spin_lock_init(&__ctx->lock);
1453 INIT_LIST_HEAD(&__ctx->rq_list);
1454 __ctx->queue = q;
1455
1456 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001457 if (!cpu_online(i))
1458 continue;
1459
Jens Axboee4043dc2014-04-09 10:18:23 -06001460 hctx = q->mq_ops->map_queue(q, i);
1461 cpumask_set_cpu(i, hctx->cpumask);
1462 hctx->nr_ctx++;
1463
Jens Axboe320ae512013-10-24 09:20:05 +01001464 /*
1465 * Set local node, IFF we have more than one hw queue. If
1466 * not, we remain on the home node of the device
1467 */
1468 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1469 hctx->numa_node = cpu_to_node(i);
1470 }
1471}
1472
1473static void blk_mq_map_swqueue(struct request_queue *q)
1474{
1475 unsigned int i;
1476 struct blk_mq_hw_ctx *hctx;
1477 struct blk_mq_ctx *ctx;
1478
1479 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001480 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001481 hctx->nr_ctx = 0;
1482 }
1483
1484 /*
1485 * Map software to hardware queues
1486 */
1487 queue_for_each_ctx(q, ctx, i) {
1488 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001489 if (!cpu_online(i))
1490 continue;
1491
Jens Axboe320ae512013-10-24 09:20:05 +01001492 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001493 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001494 ctx->index_hw = hctx->nr_ctx;
1495 hctx->ctxs[hctx->nr_ctx++] = ctx;
1496 }
Jens Axboe506e9312014-05-07 10:26:44 -06001497
1498 queue_for_each_hw_ctx(q, hctx, i) {
1499 hctx->next_cpu = cpumask_first(hctx->cpumask);
1500 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1501 }
Jens Axboe320ae512013-10-24 09:20:05 +01001502}
1503
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001504struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001505{
1506 struct blk_mq_hw_ctx **hctxs;
1507 struct blk_mq_ctx *ctx;
1508 struct request_queue *q;
1509 int i;
1510
Jens Axboe320ae512013-10-24 09:20:05 +01001511 ctx = alloc_percpu(struct blk_mq_ctx);
1512 if (!ctx)
1513 return ERR_PTR(-ENOMEM);
1514
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001515 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1516 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001517
1518 if (!hctxs)
1519 goto err_percpu;
1520
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001521 for (i = 0; i < set->nr_hw_queues; i++) {
1522 hctxs[i] = set->ops->alloc_hctx(set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001523 if (!hctxs[i])
1524 goto err_hctxs;
1525
Jens Axboee4043dc2014-04-09 10:18:23 -06001526 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1527 goto err_hctxs;
1528
Jens Axboe320ae512013-10-24 09:20:05 +01001529 hctxs[i]->numa_node = NUMA_NO_NODE;
1530 hctxs[i]->queue_num = i;
1531 }
1532
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001533 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001534 if (!q)
1535 goto err_hctxs;
1536
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001537 q->mq_map = blk_mq_make_queue_map(set);
Jens Axboe320ae512013-10-24 09:20:05 +01001538 if (!q->mq_map)
1539 goto err_map;
1540
1541 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1542 blk_queue_rq_timeout(q, 30000);
1543
1544 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001545 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboe320ae512013-10-24 09:20:05 +01001546
1547 q->queue_ctx = ctx;
1548 q->queue_hw_ctx = hctxs;
1549
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001550 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001551 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001552
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001553 q->sg_reserved_size = INT_MAX;
1554
Jens Axboe320ae512013-10-24 09:20:05 +01001555 blk_queue_make_request(q, blk_mq_make_request);
Jens Axboe87ee7b12014-04-24 08:51:47 -06001556 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001557 if (set->timeout)
1558 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001559
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001560 if (set->ops->complete)
1561 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001562
Jens Axboe320ae512013-10-24 09:20:05 +01001563 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001564 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001565
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001566 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1567 set->cmd_size, cache_line_size()),
1568 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001569 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001570 goto err_hw;
1571
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001572 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001573 goto err_flush_rq;
1574
Jens Axboe320ae512013-10-24 09:20:05 +01001575 blk_mq_map_swqueue(q);
1576
1577 mutex_lock(&all_q_mutex);
1578 list_add_tail(&q->all_q_node, &all_q_list);
1579 mutex_unlock(&all_q_mutex);
1580
1581 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001582
1583err_flush_rq:
1584 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001585err_hw:
1586 kfree(q->mq_map);
1587err_map:
1588 blk_cleanup_queue(q);
1589err_hctxs:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001590 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001591 if (!hctxs[i])
1592 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001593 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001594 set->ops->free_hctx(hctxs[i], i);
Jens Axboe320ae512013-10-24 09:20:05 +01001595 }
1596 kfree(hctxs);
1597err_percpu:
1598 free_percpu(ctx);
1599 return ERR_PTR(-ENOMEM);
1600}
1601EXPORT_SYMBOL(blk_mq_init_queue);
1602
1603void blk_mq_free_queue(struct request_queue *q)
1604{
1605 struct blk_mq_hw_ctx *hctx;
1606 int i;
1607
1608 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001609 kfree(hctx->ctxs);
Jens Axboe320ae512013-10-24 09:20:05 +01001610 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1611 if (q->mq_ops->exit_hctx)
1612 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001613 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001614 q->mq_ops->free_hctx(hctx, i);
1615 }
1616
1617 free_percpu(q->queue_ctx);
1618 kfree(q->queue_hw_ctx);
1619 kfree(q->mq_map);
1620
1621 q->queue_ctx = NULL;
1622 q->queue_hw_ctx = NULL;
1623 q->mq_map = NULL;
1624
1625 mutex_lock(&all_q_mutex);
1626 list_del_init(&q->all_q_node);
1627 mutex_unlock(&all_q_mutex);
1628}
Jens Axboe320ae512013-10-24 09:20:05 +01001629
1630/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001631static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001632{
1633 blk_mq_freeze_queue(q);
1634
1635 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1636
1637 /*
1638 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1639 * we should change hctx numa_node according to new topology (this
1640 * involves free and re-allocate memory, worthy doing?)
1641 */
1642
1643 blk_mq_map_swqueue(q);
1644
1645 blk_mq_unfreeze_queue(q);
1646}
1647
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001648static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1649 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001650{
1651 struct request_queue *q;
1652
1653 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001654 * Before new mappings are established, hotadded cpu might already
1655 * start handling requests. This doesn't break anything as we map
1656 * offline CPUs to first hardware queue. We will re-init the queue
1657 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001658 */
1659 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1660 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1661 return NOTIFY_OK;
1662
1663 mutex_lock(&all_q_mutex);
1664 list_for_each_entry(q, &all_q_list, all_q_node)
1665 blk_mq_queue_reinit(q);
1666 mutex_unlock(&all_q_mutex);
1667 return NOTIFY_OK;
1668}
1669
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001670int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1671{
1672 int i;
1673
1674 if (!set->nr_hw_queues)
1675 return -EINVAL;
1676 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1677 return -EINVAL;
1678 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1679 return -EINVAL;
1680
1681 if (!set->nr_hw_queues ||
1682 !set->ops->queue_rq || !set->ops->map_queue ||
1683 !set->ops->alloc_hctx || !set->ops->free_hctx)
1684 return -EINVAL;
1685
1686
Ming Lei48479002014-04-19 18:00:17 +08001687 set->tags = kmalloc_node(set->nr_hw_queues *
1688 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001689 GFP_KERNEL, set->numa_node);
1690 if (!set->tags)
1691 goto out;
1692
1693 for (i = 0; i < set->nr_hw_queues; i++) {
1694 set->tags[i] = blk_mq_init_rq_map(set, i);
1695 if (!set->tags[i])
1696 goto out_unwind;
1697 }
1698
1699 return 0;
1700
1701out_unwind:
1702 while (--i >= 0)
1703 blk_mq_free_rq_map(set, set->tags[i], i);
1704out:
1705 return -ENOMEM;
1706}
1707EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1708
1709void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1710{
1711 int i;
1712
1713 for (i = 0; i < set->nr_hw_queues; i++)
1714 blk_mq_free_rq_map(set, set->tags[i], i);
Ming Lei981bd182014-04-24 00:07:34 +08001715 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001716}
1717EXPORT_SYMBOL(blk_mq_free_tag_set);
1718
Jens Axboe676141e2014-03-20 13:29:18 -06001719void blk_mq_disable_hotplug(void)
1720{
1721 mutex_lock(&all_q_mutex);
1722}
1723
1724void blk_mq_enable_hotplug(void)
1725{
1726 mutex_unlock(&all_q_mutex);
1727}
1728
Jens Axboe320ae512013-10-24 09:20:05 +01001729static int __init blk_mq_init(void)
1730{
Jens Axboe320ae512013-10-24 09:20:05 +01001731 blk_mq_cpu_init();
1732
1733 /* Must be called after percpu_counter_hotcpu_callback() */
1734 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1735
1736 return 0;
1737}
1738subsys_initcall(blk_mq_init);