blob: 492f49f9645911db1a55c4ae541fa974d8a299d3 [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
59 for (i = 0; i < hctx->nr_ctx_map; i++)
60 if (hctx->ctx_map[i])
61 return true;
62
63 return false;
64}
65
66/*
67 * Mark this ctx as having pending work in this hardware queue
68 */
69static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70 struct blk_mq_ctx *ctx)
71{
72 if (!test_bit(ctx->index_hw, hctx->ctx_map))
73 set_bit(ctx->index_hw, hctx->ctx_map);
74}
75
Christoph Hellwig081241e2014-02-20 15:32:36 -080076static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +010078{
79 struct request *rq;
80 unsigned int tag;
81
82 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83 if (tag != BLK_MQ_TAG_FAIL) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -060084 rq = hctx->tags->rqs[tag];
Jens Axboe320ae512013-10-24 09:20:05 +010085 rq->tag = tag;
Jens Axboe320ae512013-10-24 09:20:05 +010086 return rq;
87 }
88
89 return NULL;
90}
91
92static int blk_mq_queue_enter(struct request_queue *q)
93{
94 int ret;
95
96 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
97 smp_wmb();
98 /* we have problems to freeze the queue if it's initializing */
99 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
100 return 0;
101
102 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
103
104 spin_lock_irq(q->queue_lock);
105 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800106 !blk_queue_bypass(q) || blk_queue_dying(q),
107 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100108 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800109 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100110 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800111 else if (blk_queue_dying(q))
112 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100113 spin_unlock_irq(q->queue_lock);
114
115 return ret;
116}
117
118static void blk_mq_queue_exit(struct request_queue *q)
119{
120 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
121}
122
Ming Lei43a5e4e2013-12-26 21:31:35 +0800123static void __blk_mq_drain_queue(struct request_queue *q)
124{
125 while (true) {
126 s64 count;
127
128 spin_lock_irq(q->queue_lock);
129 count = percpu_counter_sum(&q->mq_usage_counter);
130 spin_unlock_irq(q->queue_lock);
131
132 if (count == 0)
133 break;
134 blk_mq_run_queues(q, false);
135 msleep(10);
136 }
137}
138
Jens Axboe320ae512013-10-24 09:20:05 +0100139/*
140 * Guarantee no request is in use, so we can change any data structure of
141 * the queue afterward.
142 */
143static void blk_mq_freeze_queue(struct request_queue *q)
144{
145 bool drain;
146
147 spin_lock_irq(q->queue_lock);
148 drain = !q->bypass_depth++;
149 queue_flag_set(QUEUE_FLAG_BYPASS, q);
150 spin_unlock_irq(q->queue_lock);
151
Ming Lei43a5e4e2013-12-26 21:31:35 +0800152 if (drain)
153 __blk_mq_drain_queue(q);
154}
Jens Axboe320ae512013-10-24 09:20:05 +0100155
Ming Lei43a5e4e2013-12-26 21:31:35 +0800156void blk_mq_drain_queue(struct request_queue *q)
157{
158 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100159}
160
161static void blk_mq_unfreeze_queue(struct request_queue *q)
162{
163 bool wake = false;
164
165 spin_lock_irq(q->queue_lock);
166 if (!--q->bypass_depth) {
167 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
168 wake = true;
169 }
170 WARN_ON_ONCE(q->bypass_depth < 0);
171 spin_unlock_irq(q->queue_lock);
172 if (wake)
173 wake_up_all(&q->mq_freeze_wq);
174}
175
176bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
177{
178 return blk_mq_has_free_tags(hctx->tags);
179}
180EXPORT_SYMBOL(blk_mq_can_queue);
181
Jens Axboe94eddfb2013-11-19 09:25:07 -0700182static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
183 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100184{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700185 if (blk_queue_io_stat(q))
186 rw_flags |= REQ_IO_STAT;
187
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200188 INIT_LIST_HEAD(&rq->queuelist);
189 /* csd/requeue_work/fifo_time is initialized before use */
190 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100191 rq->mq_ctx = ctx;
192 rq->cmd_flags = rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200193 rq->cmd_type = 0;
194 /* do not touch atomic flags, it needs atomic ops against the timer */
195 rq->cpu = -1;
196 rq->__data_len = 0;
197 rq->__sector = (sector_t) -1;
198 rq->bio = NULL;
199 rq->biotail = NULL;
200 INIT_HLIST_NODE(&rq->hash);
201 RB_CLEAR_NODE(&rq->rb_node);
202 memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv)));
203 rq->rq_disk = NULL;
204 rq->part = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700205 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200206#ifdef CONFIG_BLK_CGROUP
207 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700208 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200209 rq->io_start_time_ns = 0;
210#endif
211 rq->nr_phys_segments = 0;
212#if defined(CONFIG_BLK_DEV_INTEGRITY)
213 rq->nr_integrity_segments = 0;
214#endif
215 rq->ioprio = 0;
216 rq->special = NULL;
217 /* tag was already set */
218 rq->errors = 0;
219 memset(rq->__cmd, 0, sizeof(rq->__cmd));
220 rq->cmd = rq->__cmd;
221 rq->cmd_len = BLK_MAX_CDB;
222
223 rq->extra_len = 0;
224 rq->sense_len = 0;
225 rq->resid_len = 0;
226 rq->sense = NULL;
227
228 rq->deadline = 0;
229 INIT_LIST_HEAD(&rq->timeout_list);
230 rq->timeout = 0;
231 rq->retries = 0;
232 rq->end_io = NULL;
233 rq->end_io_data = NULL;
234 rq->next_rq = NULL;
235
Jens Axboe320ae512013-10-24 09:20:05 +0100236 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
237}
238
Jens Axboe320ae512013-10-24 09:20:05 +0100239static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
240 int rw, gfp_t gfp,
241 bool reserved)
242{
243 struct request *rq;
244
245 do {
246 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
247 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
248
Christoph Hellwig18741982014-02-10 09:29:00 -0700249 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100250 if (rq) {
Jens Axboe94eddfb2013-11-19 09:25:07 -0700251 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100252 break;
Jeff Moyer959a35f2013-12-03 14:23:00 -0700253 }
Jens Axboe320ae512013-10-24 09:20:05 +0100254
Jens Axboee4043dc2014-04-09 10:18:23 -0600255 if (gfp & __GFP_WAIT) {
256 __blk_mq_run_hw_queue(hctx);
257 blk_mq_put_ctx(ctx);
258 } else {
259 blk_mq_put_ctx(ctx);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700260 break;
Jens Axboee4043dc2014-04-09 10:18:23 -0600261 }
Jeff Moyer959a35f2013-12-03 14:23:00 -0700262
Jens Axboe5810d902014-04-29 20:49:48 -0600263 blk_mq_wait_for_tags(hctx->tags, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100264 } while (1);
265
266 return rq;
267}
268
Christoph Hellwig18741982014-02-10 09:29:00 -0700269struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100270{
271 struct request *rq;
272
273 if (blk_mq_queue_enter(q))
274 return NULL;
275
Christoph Hellwig18741982014-02-10 09:29:00 -0700276 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700277 if (rq)
278 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100279 return rq;
280}
281
282struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
283 gfp_t gfp)
284{
285 struct request *rq;
286
287 if (blk_mq_queue_enter(q))
288 return NULL;
289
290 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700291 if (rq)
292 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100293 return rq;
294}
295EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
296
Jens Axboe320ae512013-10-24 09:20:05 +0100297static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
298 struct blk_mq_ctx *ctx, struct request *rq)
299{
300 const int tag = rq->tag;
301 struct request_queue *q = rq->q;
302
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200303 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe320ae512013-10-24 09:20:05 +0100304 blk_mq_put_tag(hctx->tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100305 blk_mq_queue_exit(q);
306}
307
308void blk_mq_free_request(struct request *rq)
309{
310 struct blk_mq_ctx *ctx = rq->mq_ctx;
311 struct blk_mq_hw_ctx *hctx;
312 struct request_queue *q = rq->q;
313
314 ctx->rq_completed[rq_is_sync(rq)]++;
315
316 hctx = q->mq_ops->map_queue(q, ctx->cpu);
317 __blk_mq_free_request(hctx, ctx, rq);
318}
319
Christoph Hellwig8727af42014-04-14 10:30:08 +0200320/*
321 * Clone all relevant state from a request that has been put on hold in
322 * the flush state machine into the preallocated flush request that hangs
323 * off the request queue.
324 *
325 * For a driver the flush request should be invisible, that's why we are
326 * impersonating the original request here.
327 */
328void blk_mq_clone_flush_request(struct request *flush_rq,
329 struct request *orig_rq)
330{
331 struct blk_mq_hw_ctx *hctx =
332 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
333
334 flush_rq->mq_ctx = orig_rq->mq_ctx;
335 flush_rq->tag = orig_rq->tag;
336 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
337 hctx->cmd_size);
338}
339
Christoph Hellwig63151a42014-04-16 09:44:52 +0200340inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100341{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700342 blk_account_io_done(rq);
343
Christoph Hellwig91b63632014-04-16 09:44:53 +0200344 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100345 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200346 } else {
347 if (unlikely(blk_bidi_rq(rq)))
348 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100349 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200350 }
Jens Axboe320ae512013-10-24 09:20:05 +0100351}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200352EXPORT_SYMBOL(__blk_mq_end_io);
353
354void blk_mq_end_io(struct request *rq, int error)
355{
356 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
357 BUG();
358 __blk_mq_end_io(rq, error);
359}
360EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100361
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800362static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100363{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800364 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100365
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800366 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100367}
368
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800369void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100370{
371 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700372 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100373 int cpu;
374
Christoph Hellwig38535202014-04-25 02:32:53 -0700375 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800376 rq->q->softirq_done_fn(rq);
377 return;
378 }
Jens Axboe320ae512013-10-24 09:20:05 +0100379
380 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700381 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
382 shared = cpus_share_cache(cpu, ctx->cpu);
383
384 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800385 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800386 rq->csd.info = rq;
387 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100388 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800389 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800390 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800391 }
Jens Axboe320ae512013-10-24 09:20:05 +0100392 put_cpu();
393}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800394
395/**
396 * blk_mq_complete_request - end I/O on a request
397 * @rq: the request being processed
398 *
399 * Description:
400 * Ends all I/O on a request. It does not handle partial completions.
401 * The actual completion happens out-of-order, through a IPI handler.
402 **/
403void blk_mq_complete_request(struct request *rq)
404{
405 if (unlikely(blk_should_fake_timeout(rq->q)))
406 return;
407 if (!blk_mark_rq_complete(rq))
408 __blk_mq_complete_request(rq);
409}
410EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100411
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800412static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100413{
414 struct request_queue *q = rq->q;
415
416 trace_block_rq_issue(q, rq);
417
Christoph Hellwig742ee692014-04-14 10:30:06 +0200418 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200419 if (unlikely(blk_bidi_rq(rq)))
420 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200421
Jens Axboe320ae512013-10-24 09:20:05 +0100422 /*
423 * Just mark start time and set the started bit. Due to memory
424 * ordering, we know we'll see the correct deadline as long as
425 * REQ_ATOMIC_STARTED is seen.
426 */
427 rq->deadline = jiffies + q->rq_timeout;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600428
429 /*
430 * Mark us as started and clear complete. Complete might have been
431 * set if requeue raced with timeout, which then marked it as
432 * complete. So be sure to clear complete again when we start
433 * the request, otherwise we'll ignore the completion event.
434 */
Jens Axboe320ae512013-10-24 09:20:05 +0100435 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600436 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800437
438 if (q->dma_drain_size && blk_rq_bytes(rq)) {
439 /*
440 * Make sure space for the drain appears. We know we can do
441 * this because max_hw_segments has been adjusted to be one
442 * fewer than the device can handle.
443 */
444 rq->nr_phys_segments++;
445 }
446
447 /*
448 * Flag the last request in the series so that drivers know when IO
449 * should be kicked off, if they don't do it on a per-request basis.
450 *
451 * Note: the flag isn't the only condition drivers should do kick off.
452 * If drive is busy, the last request might not have the bit set.
453 */
454 if (last)
455 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100456}
457
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200458static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100459{
460 struct request_queue *q = rq->q;
461
462 trace_block_rq_requeue(q, rq);
463 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800464
465 rq->cmd_flags &= ~REQ_END;
466
467 if (q->dma_drain_size && blk_rq_bytes(rq))
468 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100469}
470
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200471void blk_mq_requeue_request(struct request *rq)
472{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200473 __blk_mq_requeue_request(rq);
474 blk_clear_rq_complete(rq);
475
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200476 BUG_ON(blk_queued_rq(rq));
477 blk_mq_insert_request(rq, true, true, false);
478}
479EXPORT_SYMBOL(blk_mq_requeue_request);
480
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600481struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
482{
483 return tags->rqs[tag];
484}
485EXPORT_SYMBOL(blk_mq_tag_to_rq);
486
Jens Axboe320ae512013-10-24 09:20:05 +0100487struct blk_mq_timeout_data {
488 struct blk_mq_hw_ctx *hctx;
489 unsigned long *next;
490 unsigned int *next_set;
491};
492
493static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
494{
495 struct blk_mq_timeout_data *data = __data;
496 struct blk_mq_hw_ctx *hctx = data->hctx;
497 unsigned int tag;
498
499 /* It may not be in flight yet (this is where
500 * the REQ_ATOMIC_STARTED flag comes in). The requests are
501 * statically allocated, so we know it's always safe to access the
502 * memory associated with a bit offset into ->rqs[].
503 */
504 tag = 0;
505 do {
506 struct request *rq;
507
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600508 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
509 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100510 break;
511
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600512 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
513 if (rq->q != hctx->queue)
514 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100515 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
516 continue;
517
518 blk_rq_check_expired(rq, data->next, data->next_set);
519 } while (1);
520}
521
522static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
523 unsigned long *next,
524 unsigned int *next_set)
525{
526 struct blk_mq_timeout_data data = {
527 .hctx = hctx,
528 .next = next,
529 .next_set = next_set,
530 };
531
532 /*
533 * Ask the tagging code to iterate busy requests, so we can
534 * check them for timeout.
535 */
536 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
537}
538
Jens Axboe87ee7b12014-04-24 08:51:47 -0600539static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
540{
541 struct request_queue *q = rq->q;
542
543 /*
544 * We know that complete is set at this point. If STARTED isn't set
545 * anymore, then the request isn't active and the "timeout" should
546 * just be ignored. This can happen due to the bitflag ordering.
547 * Timeout first checks if STARTED is set, and if it is, assumes
548 * the request is active. But if we race with completion, then
549 * we both flags will get cleared. So check here again, and ignore
550 * a timeout event with a request that isn't active.
551 */
552 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
553 return BLK_EH_NOT_HANDLED;
554
555 if (!q->mq_ops->timeout)
556 return BLK_EH_RESET_TIMER;
557
558 return q->mq_ops->timeout(rq);
559}
560
Jens Axboe320ae512013-10-24 09:20:05 +0100561static void blk_mq_rq_timer(unsigned long data)
562{
563 struct request_queue *q = (struct request_queue *) data;
564 struct blk_mq_hw_ctx *hctx;
565 unsigned long next = 0;
566 int i, next_set = 0;
567
568 queue_for_each_hw_ctx(q, hctx, i)
569 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
570
571 if (next_set)
572 mod_timer(&q->timeout, round_jiffies_up(next));
573}
574
575/*
576 * Reverse check our software queue for entries that we could potentially
577 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
578 * too much time checking for merges.
579 */
580static bool blk_mq_attempt_merge(struct request_queue *q,
581 struct blk_mq_ctx *ctx, struct bio *bio)
582{
583 struct request *rq;
584 int checked = 8;
585
586 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
587 int el_ret;
588
589 if (!checked--)
590 break;
591
592 if (!blk_rq_merge_ok(rq, bio))
593 continue;
594
595 el_ret = blk_try_merge(rq, bio);
596 if (el_ret == ELEVATOR_BACK_MERGE) {
597 if (bio_attempt_back_merge(q, rq, bio)) {
598 ctx->rq_merged++;
599 return true;
600 }
601 break;
602 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
603 if (bio_attempt_front_merge(q, rq, bio)) {
604 ctx->rq_merged++;
605 return true;
606 }
607 break;
608 }
609 }
610
611 return false;
612}
613
Jens Axboe320ae512013-10-24 09:20:05 +0100614/*
615 * Run this hardware queue, pulling any software queues mapped to it in.
616 * Note that this function currently has various problems around ordering
617 * of IO. In particular, we'd like FIFO behaviour on handling existing
618 * items on the hctx->dispatch list. Ignore that for now.
619 */
620static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
621{
622 struct request_queue *q = hctx->queue;
623 struct blk_mq_ctx *ctx;
624 struct request *rq;
625 LIST_HEAD(rq_list);
626 int bit, queued;
627
Jens Axboefd1270d2014-04-16 09:23:48 -0600628 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600629
Jens Axboe5d12f902014-03-19 15:25:02 -0600630 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100631 return;
632
633 hctx->run++;
634
635 /*
636 * Touch any software queue that has pending entries.
637 */
638 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
639 clear_bit(bit, hctx->ctx_map);
640 ctx = hctx->ctxs[bit];
Jens Axboe320ae512013-10-24 09:20:05 +0100641
642 spin_lock(&ctx->lock);
643 list_splice_tail_init(&ctx->rq_list, &rq_list);
644 spin_unlock(&ctx->lock);
645 }
646
647 /*
648 * If we have previous entries on our dispatch list, grab them
649 * and stuff them at the front for more fair dispatch.
650 */
651 if (!list_empty_careful(&hctx->dispatch)) {
652 spin_lock(&hctx->lock);
653 if (!list_empty(&hctx->dispatch))
654 list_splice_init(&hctx->dispatch, &rq_list);
655 spin_unlock(&hctx->lock);
656 }
657
658 /*
659 * Delete and return all entries from our dispatch list
660 */
661 queued = 0;
662
663 /*
664 * Now process all the entries, sending them to the driver.
665 */
666 while (!list_empty(&rq_list)) {
667 int ret;
668
669 rq = list_first_entry(&rq_list, struct request, queuelist);
670 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100671
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800672 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100673
674 ret = q->mq_ops->queue_rq(hctx, rq);
675 switch (ret) {
676 case BLK_MQ_RQ_QUEUE_OK:
677 queued++;
678 continue;
679 case BLK_MQ_RQ_QUEUE_BUSY:
680 /*
681 * FIXME: we should have a mechanism to stop the queue
682 * like blk_stop_queue, otherwise we will waste cpu
683 * time
684 */
685 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200686 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100687 break;
688 default:
689 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100690 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800691 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100692 blk_mq_end_io(rq, rq->errors);
693 break;
694 }
695
696 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
697 break;
698 }
699
700 if (!queued)
701 hctx->dispatched[0]++;
702 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
703 hctx->dispatched[ilog2(queued) + 1]++;
704
705 /*
706 * Any items that need requeuing? Stuff them into hctx->dispatch,
707 * that is where we will continue on next queue run.
708 */
709 if (!list_empty(&rq_list)) {
710 spin_lock(&hctx->lock);
711 list_splice(&rq_list, &hctx->dispatch);
712 spin_unlock(&hctx->lock);
713 }
714}
715
Jens Axboe506e9312014-05-07 10:26:44 -0600716/*
717 * It'd be great if the workqueue API had a way to pass
718 * in a mask and had some smarts for more clever placement.
719 * For now we just round-robin here, switching for every
720 * BLK_MQ_CPU_WORK_BATCH queued items.
721 */
722static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
723{
724 int cpu = hctx->next_cpu;
725
726 if (--hctx->next_cpu_batch <= 0) {
727 int next_cpu;
728
729 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
730 if (next_cpu >= nr_cpu_ids)
731 next_cpu = cpumask_first(hctx->cpumask);
732
733 hctx->next_cpu = next_cpu;
734 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
735 }
736
737 return cpu;
738}
739
Jens Axboe320ae512013-10-24 09:20:05 +0100740void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
741{
Jens Axboe5d12f902014-03-19 15:25:02 -0600742 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100743 return;
744
Jens Axboee4043dc2014-04-09 10:18:23 -0600745 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100746 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600747 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600748 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600749 else {
750 unsigned int cpu;
751
Jens Axboe506e9312014-05-07 10:26:44 -0600752 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600753 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600754 }
Jens Axboe320ae512013-10-24 09:20:05 +0100755}
756
757void blk_mq_run_queues(struct request_queue *q, bool async)
758{
759 struct blk_mq_hw_ctx *hctx;
760 int i;
761
762 queue_for_each_hw_ctx(q, hctx, i) {
763 if ((!blk_mq_hctx_has_pending(hctx) &&
764 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600765 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100766 continue;
767
Jens Axboee4043dc2014-04-09 10:18:23 -0600768 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100769 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600770 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100771 }
772}
773EXPORT_SYMBOL(blk_mq_run_queues);
774
775void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
776{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600777 cancel_delayed_work(&hctx->run_work);
778 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100779 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
780}
781EXPORT_SYMBOL(blk_mq_stop_hw_queue);
782
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100783void blk_mq_stop_hw_queues(struct request_queue *q)
784{
785 struct blk_mq_hw_ctx *hctx;
786 int i;
787
788 queue_for_each_hw_ctx(q, hctx, i)
789 blk_mq_stop_hw_queue(hctx);
790}
791EXPORT_SYMBOL(blk_mq_stop_hw_queues);
792
Jens Axboe320ae512013-10-24 09:20:05 +0100793void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
794{
795 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600796
797 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100798 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600799 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100800}
801EXPORT_SYMBOL(blk_mq_start_hw_queue);
802
Christoph Hellwig2f268552014-04-16 09:44:56 +0200803void blk_mq_start_hw_queues(struct request_queue *q)
804{
805 struct blk_mq_hw_ctx *hctx;
806 int i;
807
808 queue_for_each_hw_ctx(q, hctx, i)
809 blk_mq_start_hw_queue(hctx);
810}
811EXPORT_SYMBOL(blk_mq_start_hw_queues);
812
813
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200814void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100815{
816 struct blk_mq_hw_ctx *hctx;
817 int i;
818
819 queue_for_each_hw_ctx(q, hctx, i) {
820 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
821 continue;
822
823 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600824 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200825 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600826 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100827 }
828}
829EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
830
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600831static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100832{
833 struct blk_mq_hw_ctx *hctx;
834
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600835 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600836
Jens Axboe320ae512013-10-24 09:20:05 +0100837 __blk_mq_run_hw_queue(hctx);
838}
839
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600840static void blk_mq_delay_work_fn(struct work_struct *work)
841{
842 struct blk_mq_hw_ctx *hctx;
843
844 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
845
846 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
847 __blk_mq_run_hw_queue(hctx);
848}
849
850void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
851{
852 unsigned long tmo = msecs_to_jiffies(msecs);
853
854 if (hctx->queue->nr_hw_queues == 1)
855 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
856 else {
857 unsigned int cpu;
858
Jens Axboe506e9312014-05-07 10:26:44 -0600859 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600860 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
861 }
862}
863EXPORT_SYMBOL(blk_mq_delay_queue);
864
Jens Axboe320ae512013-10-24 09:20:05 +0100865static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800866 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100867{
868 struct blk_mq_ctx *ctx = rq->mq_ctx;
869
Jens Axboe01b983c2013-11-19 18:59:10 -0700870 trace_block_rq_insert(hctx->queue, rq);
871
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800872 if (at_head)
873 list_add(&rq->queuelist, &ctx->rq_list);
874 else
875 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100876 blk_mq_hctx_mark_pending(hctx, ctx);
877
878 /*
879 * We do this early, to ensure we are on the right CPU.
880 */
Jens Axboe87ee7b12014-04-24 08:51:47 -0600881 blk_add_timer(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100882}
883
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600884void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
885 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100886{
887 struct request_queue *q = rq->q;
888 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600889 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100890
891 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600892 if (!cpu_online(ctx->cpu))
893 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100894
Jens Axboe320ae512013-10-24 09:20:05 +0100895 hctx = q->mq_ops->map_queue(q, ctx->cpu);
896
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600897 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
898 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
899 blk_insert_flush(rq);
900 } else {
901 spin_lock(&ctx->lock);
902 __blk_mq_insert_request(hctx, rq, at_head);
903 spin_unlock(&ctx->lock);
904 }
Jens Axboe320ae512013-10-24 09:20:05 +0100905
Jens Axboe320ae512013-10-24 09:20:05 +0100906 if (run_queue)
907 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600908
909 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100910}
911
912static void blk_mq_insert_requests(struct request_queue *q,
913 struct blk_mq_ctx *ctx,
914 struct list_head *list,
915 int depth,
916 bool from_schedule)
917
918{
919 struct blk_mq_hw_ctx *hctx;
920 struct blk_mq_ctx *current_ctx;
921
922 trace_block_unplug(q, depth, !from_schedule);
923
924 current_ctx = blk_mq_get_ctx(q);
925
926 if (!cpu_online(ctx->cpu))
927 ctx = current_ctx;
928 hctx = q->mq_ops->map_queue(q, ctx->cpu);
929
930 /*
931 * preemption doesn't flush plug list, so it's possible ctx->cpu is
932 * offline now
933 */
934 spin_lock(&ctx->lock);
935 while (!list_empty(list)) {
936 struct request *rq;
937
938 rq = list_first_entry(list, struct request, queuelist);
939 list_del_init(&rq->queuelist);
940 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800941 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100942 }
943 spin_unlock(&ctx->lock);
944
Jens Axboe320ae512013-10-24 09:20:05 +0100945 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600946 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100947}
948
949static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
950{
951 struct request *rqa = container_of(a, struct request, queuelist);
952 struct request *rqb = container_of(b, struct request, queuelist);
953
954 return !(rqa->mq_ctx < rqb->mq_ctx ||
955 (rqa->mq_ctx == rqb->mq_ctx &&
956 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
957}
958
959void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
960{
961 struct blk_mq_ctx *this_ctx;
962 struct request_queue *this_q;
963 struct request *rq;
964 LIST_HEAD(list);
965 LIST_HEAD(ctx_list);
966 unsigned int depth;
967
968 list_splice_init(&plug->mq_list, &list);
969
970 list_sort(NULL, &list, plug_ctx_cmp);
971
972 this_q = NULL;
973 this_ctx = NULL;
974 depth = 0;
975
976 while (!list_empty(&list)) {
977 rq = list_entry_rq(list.next);
978 list_del_init(&rq->queuelist);
979 BUG_ON(!rq->q);
980 if (rq->mq_ctx != this_ctx) {
981 if (this_ctx) {
982 blk_mq_insert_requests(this_q, this_ctx,
983 &ctx_list, depth,
984 from_schedule);
985 }
986
987 this_ctx = rq->mq_ctx;
988 this_q = rq->q;
989 depth = 0;
990 }
991
992 depth++;
993 list_add_tail(&rq->queuelist, &ctx_list);
994 }
995
996 /*
997 * If 'this_ctx' is set, we know we have entries to complete
998 * on 'ctx_list'. Do those.
999 */
1000 if (this_ctx) {
1001 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1002 from_schedule);
1003 }
1004}
1005
1006static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1007{
1008 init_request_from_bio(rq, bio);
1009 blk_account_io_start(rq, 1);
1010}
1011
1012static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1013{
1014 struct blk_mq_hw_ctx *hctx;
1015 struct blk_mq_ctx *ctx;
1016 const int is_sync = rw_is_sync(bio->bi_rw);
1017 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1018 int rw = bio_data_dir(bio);
1019 struct request *rq;
1020 unsigned int use_plug, request_count = 0;
1021
1022 /*
1023 * If we have multiple hardware queues, just go directly to
1024 * one of those for sync IO.
1025 */
1026 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
1027
1028 blk_queue_bounce(q, &bio);
1029
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001030 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1031 bio_endio(bio, -EIO);
1032 return;
1033 }
1034
Jens Axboe320ae512013-10-24 09:20:05 +01001035 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
1036 return;
1037
1038 if (blk_mq_queue_enter(q)) {
1039 bio_endio(bio, -EIO);
1040 return;
1041 }
1042
1043 ctx = blk_mq_get_ctx(q);
1044 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1045
Shaohua Li27fbf4e82014-02-19 20:20:21 +08001046 if (is_sync)
1047 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +01001048 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -07001049 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001050 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -07001051 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +01001052 else {
1053 blk_mq_put_ctx(ctx);
1054 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -07001055 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
1056 false);
Jens Axboe320ae512013-10-24 09:20:05 +01001057 ctx = rq->mq_ctx;
1058 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1059 }
1060
1061 hctx->queued++;
1062
1063 if (unlikely(is_flush_fua)) {
1064 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001065 blk_insert_flush(rq);
1066 goto run_queue;
1067 }
1068
1069 /*
1070 * A task plug currently exists. Since this is completely lockless,
1071 * utilize that to temporarily store requests until the task is
1072 * either done or scheduled away.
1073 */
1074 if (use_plug) {
1075 struct blk_plug *plug = current->plug;
1076
1077 if (plug) {
1078 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001079 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001080 trace_block_plug(q);
1081 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1082 blk_flush_plug_list(plug, false);
1083 trace_block_plug(q);
1084 }
1085 list_add_tail(&rq->queuelist, &plug->mq_list);
1086 blk_mq_put_ctx(ctx);
1087 return;
1088 }
1089 }
1090
Jens Axboec6d600c2014-04-30 13:43:56 -06001091 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1092 init_request_from_bio(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001093
Jens Axboec6d600c2014-04-30 13:43:56 -06001094 spin_lock(&ctx->lock);
1095insert_rq:
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001096 __blk_mq_insert_request(hctx, rq, false);
Jens Axboec6d600c2014-04-30 13:43:56 -06001097 spin_unlock(&ctx->lock);
1098 blk_account_io_start(rq, 1);
1099 } else {
1100 spin_lock(&ctx->lock);
1101 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1102 init_request_from_bio(rq, bio);
1103 goto insert_rq;
1104 }
1105
1106 spin_unlock(&ctx->lock);
1107 __blk_mq_free_request(hctx, ctx, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001108 }
1109
Jens Axboe320ae512013-10-24 09:20:05 +01001110
1111 /*
1112 * For a SYNC request, send it to the hardware immediately. For an
1113 * ASYNC request, just ensure that we run it later on. The latter
1114 * allows for merging opportunities and more efficient dispatching.
1115 */
1116run_queue:
1117 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -06001118 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001119}
1120
1121/*
1122 * Default mapping to a software queue, since we use one per CPU.
1123 */
1124struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1125{
1126 return q->queue_hw_ctx[q->mq_map[cpu]];
1127}
1128EXPORT_SYMBOL(blk_mq_map_queue);
1129
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001130struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
Jens Axboe320ae512013-10-24 09:20:05 +01001131 unsigned int hctx_index)
1132{
1133 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001134 GFP_KERNEL | __GFP_ZERO, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001135}
1136EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1137
1138void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1139 unsigned int hctx_index)
1140{
1141 kfree(hctx);
1142}
1143EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1144
1145static void blk_mq_hctx_notify(void *data, unsigned long action,
1146 unsigned int cpu)
1147{
1148 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -06001149 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +01001150 struct blk_mq_ctx *ctx;
1151 LIST_HEAD(tmp);
1152
1153 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1154 return;
1155
1156 /*
1157 * Move ctx entries to new CPU, if this one is going away.
1158 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001159 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001160
1161 spin_lock(&ctx->lock);
1162 if (!list_empty(&ctx->rq_list)) {
1163 list_splice_init(&ctx->rq_list, &tmp);
1164 clear_bit(ctx->index_hw, hctx->ctx_map);
1165 }
1166 spin_unlock(&ctx->lock);
1167
1168 if (list_empty(&tmp))
1169 return;
1170
Jens Axboebccb5f72014-04-04 21:34:48 -06001171 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001172 spin_lock(&ctx->lock);
1173
1174 while (!list_empty(&tmp)) {
1175 struct request *rq;
1176
1177 rq = list_first_entry(&tmp, struct request, queuelist);
1178 rq->mq_ctx = ctx;
1179 list_move_tail(&rq->queuelist, &ctx->rq_list);
1180 }
1181
Jens Axboebccb5f72014-04-04 21:34:48 -06001182 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001183 blk_mq_hctx_mark_pending(hctx, ctx);
1184
1185 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001186
1187 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001188 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001189}
1190
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001191static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1192 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001193{
1194 struct page *page;
1195
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001196 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001197 int i;
1198
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001199 for (i = 0; i < tags->nr_tags; i++) {
1200 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001201 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001202 set->ops->exit_request(set->driver_data, tags->rqs[i],
1203 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001204 }
1205 }
1206
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001207 while (!list_empty(&tags->page_list)) {
1208 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001209 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001210 __free_pages(page, page->private);
1211 }
1212
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001213 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001214
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001215 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001216}
1217
1218static size_t order_to_size(unsigned int order)
1219{
Ming Lei4ca08502014-04-19 18:00:18 +08001220 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001221}
1222
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001223static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1224 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001225{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001226 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001227 unsigned int i, j, entries_per_page, max_order = 4;
1228 size_t rq_size, left;
1229
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001230 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1231 set->numa_node);
1232 if (!tags)
1233 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001234
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001235 INIT_LIST_HEAD(&tags->page_list);
1236
1237 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1238 GFP_KERNEL, set->numa_node);
1239 if (!tags->rqs) {
1240 blk_mq_free_tags(tags);
1241 return NULL;
1242 }
Jens Axboe320ae512013-10-24 09:20:05 +01001243
1244 /*
1245 * rq_size is the size of the request plus driver payload, rounded
1246 * to the cacheline size
1247 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001248 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001249 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001250 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001251
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001252 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001253 int this_order = max_order;
1254 struct page *page;
1255 int to_do;
1256 void *p;
1257
1258 while (left < order_to_size(this_order - 1) && this_order)
1259 this_order--;
1260
1261 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001262 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1263 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001264 if (page)
1265 break;
1266 if (!this_order--)
1267 break;
1268 if (order_to_size(this_order) < rq_size)
1269 break;
1270 } while (1);
1271
1272 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001273 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001274
1275 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001276 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001277
1278 p = page_address(page);
1279 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001280 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001281 left -= to_do * rq_size;
1282 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001283 tags->rqs[i] = p;
1284 if (set->ops->init_request) {
1285 if (set->ops->init_request(set->driver_data,
1286 tags->rqs[i], hctx_idx, i,
1287 set->numa_node))
1288 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001289 }
1290
Jens Axboe320ae512013-10-24 09:20:05 +01001291 p += rq_size;
1292 i++;
1293 }
1294 }
1295
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001296 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001297
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001298fail:
1299 pr_warn("%s: failed to allocate requests\n", __func__);
1300 blk_mq_free_rq_map(set, tags, hctx_idx);
1301 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001302}
1303
1304static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001305 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001306{
1307 struct blk_mq_hw_ctx *hctx;
1308 unsigned int i, j;
1309
1310 /*
1311 * Initialize hardware queues
1312 */
1313 queue_for_each_hw_ctx(q, hctx, i) {
1314 unsigned int num_maps;
1315 int node;
1316
1317 node = hctx->numa_node;
1318 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001319 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001320
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001321 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1322 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001323 spin_lock_init(&hctx->lock);
1324 INIT_LIST_HEAD(&hctx->dispatch);
1325 hctx->queue = q;
1326 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001327 hctx->flags = set->flags;
1328 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001329
1330 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1331 blk_mq_hctx_notify, hctx);
1332 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1333
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001334 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001335
1336 /*
1337 * Allocate space for all possible cpus to avoid allocation in
1338 * runtime
1339 */
1340 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1341 GFP_KERNEL, node);
1342 if (!hctx->ctxs)
1343 break;
1344
1345 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1346 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1347 GFP_KERNEL, node);
1348 if (!hctx->ctx_map)
1349 break;
1350
1351 hctx->nr_ctx_map = num_maps;
1352 hctx->nr_ctx = 0;
1353
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001354 if (set->ops->init_hctx &&
1355 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001356 break;
1357 }
1358
1359 if (i == q->nr_hw_queues)
1360 return 0;
1361
1362 /*
1363 * Init failed
1364 */
1365 queue_for_each_hw_ctx(q, hctx, j) {
1366 if (i == j)
1367 break;
1368
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001369 if (set->ops->exit_hctx)
1370 set->ops->exit_hctx(hctx, j);
Jens Axboe320ae512013-10-24 09:20:05 +01001371
1372 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Jens Axboe320ae512013-10-24 09:20:05 +01001373 kfree(hctx->ctxs);
Ming Lei11471e02014-04-19 18:00:16 +08001374 kfree(hctx->ctx_map);
Jens Axboe320ae512013-10-24 09:20:05 +01001375 }
1376
1377 return 1;
1378}
1379
1380static void blk_mq_init_cpu_queues(struct request_queue *q,
1381 unsigned int nr_hw_queues)
1382{
1383 unsigned int i;
1384
1385 for_each_possible_cpu(i) {
1386 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1387 struct blk_mq_hw_ctx *hctx;
1388
1389 memset(__ctx, 0, sizeof(*__ctx));
1390 __ctx->cpu = i;
1391 spin_lock_init(&__ctx->lock);
1392 INIT_LIST_HEAD(&__ctx->rq_list);
1393 __ctx->queue = q;
1394
1395 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001396 if (!cpu_online(i))
1397 continue;
1398
Jens Axboee4043dc2014-04-09 10:18:23 -06001399 hctx = q->mq_ops->map_queue(q, i);
1400 cpumask_set_cpu(i, hctx->cpumask);
1401 hctx->nr_ctx++;
1402
Jens Axboe320ae512013-10-24 09:20:05 +01001403 /*
1404 * Set local node, IFF we have more than one hw queue. If
1405 * not, we remain on the home node of the device
1406 */
1407 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1408 hctx->numa_node = cpu_to_node(i);
1409 }
1410}
1411
1412static void blk_mq_map_swqueue(struct request_queue *q)
1413{
1414 unsigned int i;
1415 struct blk_mq_hw_ctx *hctx;
1416 struct blk_mq_ctx *ctx;
1417
1418 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001419 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001420 hctx->nr_ctx = 0;
1421 }
1422
1423 /*
1424 * Map software to hardware queues
1425 */
1426 queue_for_each_ctx(q, ctx, i) {
1427 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001428 if (!cpu_online(i))
1429 continue;
1430
Jens Axboe320ae512013-10-24 09:20:05 +01001431 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001432 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001433 ctx->index_hw = hctx->nr_ctx;
1434 hctx->ctxs[hctx->nr_ctx++] = ctx;
1435 }
Jens Axboe506e9312014-05-07 10:26:44 -06001436
1437 queue_for_each_hw_ctx(q, hctx, i) {
1438 hctx->next_cpu = cpumask_first(hctx->cpumask);
1439 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1440 }
Jens Axboe320ae512013-10-24 09:20:05 +01001441}
1442
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001443struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001444{
1445 struct blk_mq_hw_ctx **hctxs;
1446 struct blk_mq_ctx *ctx;
1447 struct request_queue *q;
1448 int i;
1449
Jens Axboe320ae512013-10-24 09:20:05 +01001450 ctx = alloc_percpu(struct blk_mq_ctx);
1451 if (!ctx)
1452 return ERR_PTR(-ENOMEM);
1453
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001454 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1455 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001456
1457 if (!hctxs)
1458 goto err_percpu;
1459
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001460 for (i = 0; i < set->nr_hw_queues; i++) {
1461 hctxs[i] = set->ops->alloc_hctx(set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001462 if (!hctxs[i])
1463 goto err_hctxs;
1464
Jens Axboee4043dc2014-04-09 10:18:23 -06001465 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1466 goto err_hctxs;
1467
Jens Axboe320ae512013-10-24 09:20:05 +01001468 hctxs[i]->numa_node = NUMA_NO_NODE;
1469 hctxs[i]->queue_num = i;
1470 }
1471
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001472 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001473 if (!q)
1474 goto err_hctxs;
1475
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001476 q->mq_map = blk_mq_make_queue_map(set);
Jens Axboe320ae512013-10-24 09:20:05 +01001477 if (!q->mq_map)
1478 goto err_map;
1479
1480 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1481 blk_queue_rq_timeout(q, 30000);
1482
1483 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001484 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboe320ae512013-10-24 09:20:05 +01001485
1486 q->queue_ctx = ctx;
1487 q->queue_hw_ctx = hctxs;
1488
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001489 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001490 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001491
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001492 q->sg_reserved_size = INT_MAX;
1493
Jens Axboe320ae512013-10-24 09:20:05 +01001494 blk_queue_make_request(q, blk_mq_make_request);
Jens Axboe87ee7b12014-04-24 08:51:47 -06001495 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001496 if (set->timeout)
1497 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001498
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001499 if (set->ops->complete)
1500 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001501
Jens Axboe320ae512013-10-24 09:20:05 +01001502 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001503 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001504
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001505 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1506 set->cmd_size, cache_line_size()),
1507 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001508 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001509 goto err_hw;
1510
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001511 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001512 goto err_flush_rq;
1513
Jens Axboe320ae512013-10-24 09:20:05 +01001514 blk_mq_map_swqueue(q);
1515
1516 mutex_lock(&all_q_mutex);
1517 list_add_tail(&q->all_q_node, &all_q_list);
1518 mutex_unlock(&all_q_mutex);
1519
1520 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001521
1522err_flush_rq:
1523 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001524err_hw:
1525 kfree(q->mq_map);
1526err_map:
1527 blk_cleanup_queue(q);
1528err_hctxs:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001529 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001530 if (!hctxs[i])
1531 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001532 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001533 set->ops->free_hctx(hctxs[i], i);
Jens Axboe320ae512013-10-24 09:20:05 +01001534 }
1535 kfree(hctxs);
1536err_percpu:
1537 free_percpu(ctx);
1538 return ERR_PTR(-ENOMEM);
1539}
1540EXPORT_SYMBOL(blk_mq_init_queue);
1541
1542void blk_mq_free_queue(struct request_queue *q)
1543{
1544 struct blk_mq_hw_ctx *hctx;
1545 int i;
1546
1547 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001548 kfree(hctx->ctx_map);
1549 kfree(hctx->ctxs);
Jens Axboe320ae512013-10-24 09:20:05 +01001550 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1551 if (q->mq_ops->exit_hctx)
1552 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001553 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001554 q->mq_ops->free_hctx(hctx, i);
1555 }
1556
1557 free_percpu(q->queue_ctx);
1558 kfree(q->queue_hw_ctx);
1559 kfree(q->mq_map);
1560
1561 q->queue_ctx = NULL;
1562 q->queue_hw_ctx = NULL;
1563 q->mq_map = NULL;
1564
1565 mutex_lock(&all_q_mutex);
1566 list_del_init(&q->all_q_node);
1567 mutex_unlock(&all_q_mutex);
1568}
Jens Axboe320ae512013-10-24 09:20:05 +01001569
1570/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001571static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001572{
1573 blk_mq_freeze_queue(q);
1574
1575 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1576
1577 /*
1578 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1579 * we should change hctx numa_node according to new topology (this
1580 * involves free and re-allocate memory, worthy doing?)
1581 */
1582
1583 blk_mq_map_swqueue(q);
1584
1585 blk_mq_unfreeze_queue(q);
1586}
1587
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001588static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1589 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001590{
1591 struct request_queue *q;
1592
1593 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001594 * Before new mappings are established, hotadded cpu might already
1595 * start handling requests. This doesn't break anything as we map
1596 * offline CPUs to first hardware queue. We will re-init the queue
1597 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001598 */
1599 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1600 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1601 return NOTIFY_OK;
1602
1603 mutex_lock(&all_q_mutex);
1604 list_for_each_entry(q, &all_q_list, all_q_node)
1605 blk_mq_queue_reinit(q);
1606 mutex_unlock(&all_q_mutex);
1607 return NOTIFY_OK;
1608}
1609
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001610int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1611{
1612 int i;
1613
1614 if (!set->nr_hw_queues)
1615 return -EINVAL;
1616 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1617 return -EINVAL;
1618 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1619 return -EINVAL;
1620
1621 if (!set->nr_hw_queues ||
1622 !set->ops->queue_rq || !set->ops->map_queue ||
1623 !set->ops->alloc_hctx || !set->ops->free_hctx)
1624 return -EINVAL;
1625
1626
Ming Lei484790052014-04-19 18:00:17 +08001627 set->tags = kmalloc_node(set->nr_hw_queues *
1628 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001629 GFP_KERNEL, set->numa_node);
1630 if (!set->tags)
1631 goto out;
1632
1633 for (i = 0; i < set->nr_hw_queues; i++) {
1634 set->tags[i] = blk_mq_init_rq_map(set, i);
1635 if (!set->tags[i])
1636 goto out_unwind;
1637 }
1638
1639 return 0;
1640
1641out_unwind:
1642 while (--i >= 0)
1643 blk_mq_free_rq_map(set, set->tags[i], i);
1644out:
1645 return -ENOMEM;
1646}
1647EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1648
1649void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1650{
1651 int i;
1652
1653 for (i = 0; i < set->nr_hw_queues; i++)
1654 blk_mq_free_rq_map(set, set->tags[i], i);
Ming Lei981bd182014-04-24 00:07:34 +08001655 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001656}
1657EXPORT_SYMBOL(blk_mq_free_tag_set);
1658
Jens Axboe676141e2014-03-20 13:29:18 -06001659void blk_mq_disable_hotplug(void)
1660{
1661 mutex_lock(&all_q_mutex);
1662}
1663
1664void blk_mq_enable_hotplug(void)
1665{
1666 mutex_unlock(&all_q_mutex);
1667}
1668
Jens Axboe320ae512013-10-24 09:20:05 +01001669static int __init blk_mq_init(void)
1670{
Jens Axboe320ae512013-10-24 09:20:05 +01001671 blk_mq_cpu_init();
1672
1673 /* Must be called after percpu_counter_hotcpu_callback() */
1674 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1675
1676 return 0;
1677}
1678subsys_initcall(blk_mq_init);