blob: 71f564e8812e2258d80a747f327a06673d2f0908 [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
Jens Axboe320ae512013-10-24 09:20:05 +01007#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/backing-dev.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
12#include <linux/mm.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/workqueue.h>
16#include <linux/smp.h>
17#include <linux/llist.h>
18#include <linux/list_sort.h>
19#include <linux/cpu.h>
20#include <linux/cache.h>
21#include <linux/sched/sysctl.h>
22#include <linux/delay.h>
23
24#include <trace/events/block.h>
25
26#include <linux/blk-mq.h>
27#include "blk.h"
28#include "blk-mq.h"
29#include "blk-mq-tag.h"
30
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
34static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
35
Jens Axboe320ae512013-10-24 09:20:05 +010036static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
37 unsigned int cpu)
38{
39 return per_cpu_ptr(q->queue_ctx, cpu);
40}
41
42/*
43 * This assumes per-cpu software queueing queues. They could be per-node
44 * as well, for instance. For now this is hardcoded as-is. Note that we don't
45 * care about preemption, since we know the ctx's are persistent. This does
46 * mean that we can't rely on ctx always matching the currently running CPU.
47 */
48static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
49{
50 return __blk_mq_get_ctx(q, get_cpu());
51}
52
53static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
54{
55 put_cpu();
56}
57
58/*
59 * Check if any of the ctx's have pending work in this hardware queue
60 */
61static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
62{
63 unsigned int i;
64
Jens Axboe1429d7c2014-05-19 09:23:55 -060065 for (i = 0; i < hctx->ctx_map.map_size; i++)
66 if (hctx->ctx_map.map[i].word)
Jens Axboe320ae512013-10-24 09:20:05 +010067 return true;
68
69 return false;
70}
71
Jens Axboe1429d7c2014-05-19 09:23:55 -060072static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
73 struct blk_mq_ctx *ctx)
74{
75 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
76}
77
78#define CTX_TO_BIT(hctx, ctx) \
79 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
80
Jens Axboe320ae512013-10-24 09:20:05 +010081/*
82 * Mark this ctx as having pending work in this hardware queue
83 */
84static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
85 struct blk_mq_ctx *ctx)
86{
Jens Axboe1429d7c2014-05-19 09:23:55 -060087 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
88
89 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
90 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
91}
92
93static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
94 struct blk_mq_ctx *ctx)
95{
96 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
97
98 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
Jens Axboe320ae512013-10-24 09:20:05 +010099}
100
Jens Axboe320ae512013-10-24 09:20:05 +0100101static int blk_mq_queue_enter(struct request_queue *q)
102{
103 int ret;
104
105 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
106 smp_wmb();
107 /* we have problems to freeze the queue if it's initializing */
108 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
109 return 0;
110
111 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
112
113 spin_lock_irq(q->queue_lock);
114 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800115 !blk_queue_bypass(q) || blk_queue_dying(q),
116 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100117 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800118 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100119 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800120 else if (blk_queue_dying(q))
121 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100122 spin_unlock_irq(q->queue_lock);
123
124 return ret;
125}
126
127static void blk_mq_queue_exit(struct request_queue *q)
128{
129 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
130}
131
Ming Lei43a5e4e2013-12-26 21:31:35 +0800132static void __blk_mq_drain_queue(struct request_queue *q)
133{
134 while (true) {
135 s64 count;
136
137 spin_lock_irq(q->queue_lock);
138 count = percpu_counter_sum(&q->mq_usage_counter);
139 spin_unlock_irq(q->queue_lock);
140
141 if (count == 0)
142 break;
143 blk_mq_run_queues(q, false);
144 msleep(10);
145 }
146}
147
Jens Axboe320ae512013-10-24 09:20:05 +0100148/*
149 * Guarantee no request is in use, so we can change any data structure of
150 * the queue afterward.
151 */
152static void blk_mq_freeze_queue(struct request_queue *q)
153{
154 bool drain;
155
156 spin_lock_irq(q->queue_lock);
157 drain = !q->bypass_depth++;
158 queue_flag_set(QUEUE_FLAG_BYPASS, q);
159 spin_unlock_irq(q->queue_lock);
160
Ming Lei43a5e4e2013-12-26 21:31:35 +0800161 if (drain)
162 __blk_mq_drain_queue(q);
163}
Jens Axboe320ae512013-10-24 09:20:05 +0100164
Ming Lei43a5e4e2013-12-26 21:31:35 +0800165void blk_mq_drain_queue(struct request_queue *q)
166{
167 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100168}
169
170static void blk_mq_unfreeze_queue(struct request_queue *q)
171{
172 bool wake = false;
173
174 spin_lock_irq(q->queue_lock);
175 if (!--q->bypass_depth) {
176 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
177 wake = true;
178 }
179 WARN_ON_ONCE(q->bypass_depth < 0);
180 spin_unlock_irq(q->queue_lock);
181 if (wake)
182 wake_up_all(&q->mq_freeze_wq);
183}
184
185bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
186{
187 return blk_mq_has_free_tags(hctx->tags);
188}
189EXPORT_SYMBOL(blk_mq_can_queue);
190
Jens Axboe94eddfb2013-11-19 09:25:07 -0700191static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
192 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100193{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700194 if (blk_queue_io_stat(q))
195 rw_flags |= REQ_IO_STAT;
196
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200197 INIT_LIST_HEAD(&rq->queuelist);
198 /* csd/requeue_work/fifo_time is initialized before use */
199 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100200 rq->mq_ctx = ctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600201 rq->cmd_flags |= rw_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200202 /* do not touch atomic flags, it needs atomic ops against the timer */
203 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200204 INIT_HLIST_NODE(&rq->hash);
205 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200206 rq->rq_disk = NULL;
207 rq->part = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200208#ifdef CONFIG_BLK_CGROUP
209 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700210 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200211 rq->io_start_time_ns = 0;
212#endif
213 rq->nr_phys_segments = 0;
214#if defined(CONFIG_BLK_DEV_INTEGRITY)
215 rq->nr_integrity_segments = 0;
216#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200217 rq->special = NULL;
218 /* tag was already set */
219 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200220
221 rq->extra_len = 0;
222 rq->sense_len = 0;
223 rq->resid_len = 0;
224 rq->sense = NULL;
225
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200226 INIT_LIST_HEAD(&rq->timeout_list);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200227 rq->end_io = NULL;
228 rq->end_io_data = NULL;
229 rq->next_rq = NULL;
230
Jens Axboe320ae512013-10-24 09:20:05 +0100231 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
232}
233
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200234static struct request *
235__blk_mq_alloc_request(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
236 struct blk_mq_ctx *ctx, int rw, gfp_t gfp, bool reserved)
237{
238 struct request *rq;
239 unsigned int tag;
240
241 tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
242 if (tag != BLK_MQ_TAG_FAIL) {
243 rq = hctx->tags->rqs[tag];
244
245 rq->cmd_flags = 0;
246 if (blk_mq_tag_busy(hctx)) {
247 rq->cmd_flags = REQ_MQ_INFLIGHT;
248 atomic_inc(&hctx->nr_active);
249 }
250
251 rq->tag = tag;
252 blk_mq_rq_ctx_init(q, ctx, rq, rw);
253 return rq;
254 }
255
256 return NULL;
257}
258
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +0200259struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
260 bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100261{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200262 struct blk_mq_ctx *ctx;
263 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100264 struct request *rq;
265
266 if (blk_mq_queue_enter(q))
267 return NULL;
268
Christoph Hellwigd8525642014-05-27 20:59:50 +0200269 ctx = blk_mq_get_ctx(q);
270 hctx = q->mq_ops->map_queue(q, ctx->cpu);
271
272 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp & ~__GFP_WAIT,
273 reserved);
274 if (!rq && (gfp & __GFP_WAIT)) {
275 __blk_mq_run_hw_queue(hctx);
276 blk_mq_put_ctx(ctx);
277
278 ctx = blk_mq_get_ctx(q);
279 hctx = q->mq_ops->map_queue(q, ctx->cpu);
280 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp, reserved);
281 }
282 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100283 return rq;
284}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600285EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100286
Jens Axboe320ae512013-10-24 09:20:05 +0100287static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
288 struct blk_mq_ctx *ctx, struct request *rq)
289{
290 const int tag = rq->tag;
291 struct request_queue *q = rq->q;
292
Jens Axboe0d2602c2014-05-13 15:10:52 -0600293 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
294 atomic_dec(&hctx->nr_active);
295
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200296 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600297 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100298 blk_mq_queue_exit(q);
299}
300
301void blk_mq_free_request(struct request *rq)
302{
303 struct blk_mq_ctx *ctx = rq->mq_ctx;
304 struct blk_mq_hw_ctx *hctx;
305 struct request_queue *q = rq->q;
306
307 ctx->rq_completed[rq_is_sync(rq)]++;
308
309 hctx = q->mq_ops->map_queue(q, ctx->cpu);
310 __blk_mq_free_request(hctx, ctx, rq);
311}
312
Christoph Hellwig8727af42014-04-14 10:30:08 +0200313/*
314 * Clone all relevant state from a request that has been put on hold in
315 * the flush state machine into the preallocated flush request that hangs
316 * off the request queue.
317 *
318 * For a driver the flush request should be invisible, that's why we are
319 * impersonating the original request here.
320 */
321void blk_mq_clone_flush_request(struct request *flush_rq,
322 struct request *orig_rq)
323{
324 struct blk_mq_hw_ctx *hctx =
325 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
326
327 flush_rq->mq_ctx = orig_rq->mq_ctx;
328 flush_rq->tag = orig_rq->tag;
329 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
330 hctx->cmd_size);
331}
332
Christoph Hellwig63151a42014-04-16 09:44:52 +0200333inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100334{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700335 blk_account_io_done(rq);
336
Christoph Hellwig91b63632014-04-16 09:44:53 +0200337 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100338 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200339 } else {
340 if (unlikely(blk_bidi_rq(rq)))
341 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100342 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200343 }
Jens Axboe320ae512013-10-24 09:20:05 +0100344}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200345EXPORT_SYMBOL(__blk_mq_end_io);
346
347void blk_mq_end_io(struct request *rq, int error)
348{
349 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
350 BUG();
351 __blk_mq_end_io(rq, error);
352}
353EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100354
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800355static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100356{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800357 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100358
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800359 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100360}
361
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800362void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100363{
364 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700365 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100366 int cpu;
367
Christoph Hellwig38535202014-04-25 02:32:53 -0700368 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800369 rq->q->softirq_done_fn(rq);
370 return;
371 }
Jens Axboe320ae512013-10-24 09:20:05 +0100372
373 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700374 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
375 shared = cpus_share_cache(cpu, ctx->cpu);
376
377 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800378 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800379 rq->csd.info = rq;
380 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100381 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800382 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800383 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800384 }
Jens Axboe320ae512013-10-24 09:20:05 +0100385 put_cpu();
386}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800387
388/**
389 * blk_mq_complete_request - end I/O on a request
390 * @rq: the request being processed
391 *
392 * Description:
393 * Ends all I/O on a request. It does not handle partial completions.
394 * The actual completion happens out-of-order, through a IPI handler.
395 **/
396void blk_mq_complete_request(struct request *rq)
397{
Jens Axboe95f09682014-05-27 17:46:48 -0600398 struct request_queue *q = rq->q;
399
400 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800401 return;
Jens Axboe95f09682014-05-27 17:46:48 -0600402 if (!blk_mark_rq_complete(rq)) {
403 if (q->softirq_done_fn)
404 __blk_mq_complete_request(rq);
405 else
406 blk_mq_end_io(rq, rq->errors);
407 }
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800408}
409EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100410
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800411static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100412{
413 struct request_queue *q = rq->q;
414
415 trace_block_rq_issue(q, rq);
416
Christoph Hellwig742ee692014-04-14 10:30:06 +0200417 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200418 if (unlikely(blk_bidi_rq(rq)))
419 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200420
Jens Axboe320ae512013-10-24 09:20:05 +0100421 /*
422 * Just mark start time and set the started bit. Due to memory
423 * ordering, we know we'll see the correct deadline as long as
Jens Axboec22d9d82014-05-23 14:14:57 -0600424 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
425 * unless one has been set in the request.
Jens Axboe320ae512013-10-24 09:20:05 +0100426 */
Jens Axboec22d9d82014-05-23 14:14:57 -0600427 if (!rq->timeout)
428 rq->deadline = jiffies + q->rq_timeout;
429 else
430 rq->deadline = jiffies + rq->timeout;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600431
432 /*
433 * Mark us as started and clear complete. Complete might have been
434 * set if requeue raced with timeout, which then marked it as
435 * complete. So be sure to clear complete again when we start
436 * the request, otherwise we'll ignore the completion event.
437 */
Jens Axboe4b570522014-05-29 11:00:11 -0600438 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
439 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
440 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
441 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800442
443 if (q->dma_drain_size && blk_rq_bytes(rq)) {
444 /*
445 * Make sure space for the drain appears. We know we can do
446 * this because max_hw_segments has been adjusted to be one
447 * fewer than the device can handle.
448 */
449 rq->nr_phys_segments++;
450 }
451
452 /*
453 * Flag the last request in the series so that drivers know when IO
454 * should be kicked off, if they don't do it on a per-request basis.
455 *
456 * Note: the flag isn't the only condition drivers should do kick off.
457 * If drive is busy, the last request might not have the bit set.
458 */
459 if (last)
460 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100461}
462
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200463static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100464{
465 struct request_queue *q = rq->q;
466
467 trace_block_rq_requeue(q, rq);
468 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800469
470 rq->cmd_flags &= ~REQ_END;
471
472 if (q->dma_drain_size && blk_rq_bytes(rq))
473 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100474}
475
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200476void blk_mq_requeue_request(struct request *rq)
477{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200478 __blk_mq_requeue_request(rq);
479 blk_clear_rq_complete(rq);
480
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200481 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600482 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200483}
484EXPORT_SYMBOL(blk_mq_requeue_request);
485
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600486static void blk_mq_requeue_work(struct work_struct *work)
487{
488 struct request_queue *q =
489 container_of(work, struct request_queue, requeue_work);
490 LIST_HEAD(rq_list);
491 struct request *rq, *next;
492 unsigned long flags;
493
494 spin_lock_irqsave(&q->requeue_lock, flags);
495 list_splice_init(&q->requeue_list, &rq_list);
496 spin_unlock_irqrestore(&q->requeue_lock, flags);
497
498 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
499 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
500 continue;
501
502 rq->cmd_flags &= ~REQ_SOFTBARRIER;
503 list_del_init(&rq->queuelist);
504 blk_mq_insert_request(rq, true, false, false);
505 }
506
507 while (!list_empty(&rq_list)) {
508 rq = list_entry(rq_list.next, struct request, queuelist);
509 list_del_init(&rq->queuelist);
510 blk_mq_insert_request(rq, false, false, false);
511 }
512
513 blk_mq_run_queues(q, false);
514}
515
516void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
517{
518 struct request_queue *q = rq->q;
519 unsigned long flags;
520
521 /*
522 * We abuse this flag that is otherwise used by the I/O scheduler to
523 * request head insertation from the workqueue.
524 */
525 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
526
527 spin_lock_irqsave(&q->requeue_lock, flags);
528 if (at_head) {
529 rq->cmd_flags |= REQ_SOFTBARRIER;
530 list_add(&rq->queuelist, &q->requeue_list);
531 } else {
532 list_add_tail(&rq->queuelist, &q->requeue_list);
533 }
534 spin_unlock_irqrestore(&q->requeue_lock, flags);
535}
536EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
537
538void blk_mq_kick_requeue_list(struct request_queue *q)
539{
540 kblockd_schedule_work(&q->requeue_work);
541}
542EXPORT_SYMBOL(blk_mq_kick_requeue_list);
543
Shaohua Li22302372014-05-30 08:06:42 -0600544struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx, unsigned int tag)
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600545{
Shaohua Li22302372014-05-30 08:06:42 -0600546 struct request_queue *q = hctx->queue;
547
548 if ((q->flush_rq->cmd_flags & REQ_FLUSH_SEQ) &&
549 q->flush_rq->tag == tag)
550 return q->flush_rq;
551
552 return hctx->tags->rqs[tag];
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600553}
554EXPORT_SYMBOL(blk_mq_tag_to_rq);
555
Jens Axboe320ae512013-10-24 09:20:05 +0100556struct blk_mq_timeout_data {
557 struct blk_mq_hw_ctx *hctx;
558 unsigned long *next;
559 unsigned int *next_set;
560};
561
562static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
563{
564 struct blk_mq_timeout_data *data = __data;
565 struct blk_mq_hw_ctx *hctx = data->hctx;
566 unsigned int tag;
567
568 /* It may not be in flight yet (this is where
569 * the REQ_ATOMIC_STARTED flag comes in). The requests are
570 * statically allocated, so we know it's always safe to access the
571 * memory associated with a bit offset into ->rqs[].
572 */
573 tag = 0;
574 do {
575 struct request *rq;
576
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600577 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
578 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100579 break;
580
Shaohua Li22302372014-05-30 08:06:42 -0600581 rq = blk_mq_tag_to_rq(hctx, tag++);
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600582 if (rq->q != hctx->queue)
583 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100584 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
585 continue;
586
587 blk_rq_check_expired(rq, data->next, data->next_set);
588 } while (1);
589}
590
591static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
592 unsigned long *next,
593 unsigned int *next_set)
594{
595 struct blk_mq_timeout_data data = {
596 .hctx = hctx,
597 .next = next,
598 .next_set = next_set,
599 };
600
601 /*
602 * Ask the tagging code to iterate busy requests, so we can
603 * check them for timeout.
604 */
605 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
606}
607
Jens Axboe87ee7b12014-04-24 08:51:47 -0600608static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
609{
610 struct request_queue *q = rq->q;
611
612 /*
613 * We know that complete is set at this point. If STARTED isn't set
614 * anymore, then the request isn't active and the "timeout" should
615 * just be ignored. This can happen due to the bitflag ordering.
616 * Timeout first checks if STARTED is set, and if it is, assumes
617 * the request is active. But if we race with completion, then
618 * we both flags will get cleared. So check here again, and ignore
619 * a timeout event with a request that isn't active.
620 */
621 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
622 return BLK_EH_NOT_HANDLED;
623
624 if (!q->mq_ops->timeout)
625 return BLK_EH_RESET_TIMER;
626
627 return q->mq_ops->timeout(rq);
628}
629
Jens Axboe320ae512013-10-24 09:20:05 +0100630static void blk_mq_rq_timer(unsigned long data)
631{
632 struct request_queue *q = (struct request_queue *) data;
633 struct blk_mq_hw_ctx *hctx;
634 unsigned long next = 0;
635 int i, next_set = 0;
636
Jens Axboe484b4062014-05-21 14:01:15 -0600637 queue_for_each_hw_ctx(q, hctx, i) {
638 /*
639 * If not software queues are currently mapped to this
640 * hardware queue, there's nothing to check
641 */
642 if (!hctx->nr_ctx || !hctx->tags)
643 continue;
644
Jens Axboe320ae512013-10-24 09:20:05 +0100645 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
Jens Axboe484b4062014-05-21 14:01:15 -0600646 }
Jens Axboe320ae512013-10-24 09:20:05 +0100647
Jens Axboe0d2602c2014-05-13 15:10:52 -0600648 if (next_set) {
649 next = blk_rq_timeout(round_jiffies_up(next));
650 mod_timer(&q->timeout, next);
651 } else {
652 queue_for_each_hw_ctx(q, hctx, i)
653 blk_mq_tag_idle(hctx);
654 }
Jens Axboe320ae512013-10-24 09:20:05 +0100655}
656
657/*
658 * Reverse check our software queue for entries that we could potentially
659 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
660 * too much time checking for merges.
661 */
662static bool blk_mq_attempt_merge(struct request_queue *q,
663 struct blk_mq_ctx *ctx, struct bio *bio)
664{
665 struct request *rq;
666 int checked = 8;
667
668 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
669 int el_ret;
670
671 if (!checked--)
672 break;
673
674 if (!blk_rq_merge_ok(rq, bio))
675 continue;
676
677 el_ret = blk_try_merge(rq, bio);
678 if (el_ret == ELEVATOR_BACK_MERGE) {
679 if (bio_attempt_back_merge(q, rq, bio)) {
680 ctx->rq_merged++;
681 return true;
682 }
683 break;
684 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
685 if (bio_attempt_front_merge(q, rq, bio)) {
686 ctx->rq_merged++;
687 return true;
688 }
689 break;
690 }
691 }
692
693 return false;
694}
695
Jens Axboe320ae512013-10-24 09:20:05 +0100696/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600697 * Process software queues that have been marked busy, splicing them
698 * to the for-dispatch
699 */
700static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
701{
702 struct blk_mq_ctx *ctx;
703 int i;
704
705 for (i = 0; i < hctx->ctx_map.map_size; i++) {
706 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
707 unsigned int off, bit;
708
709 if (!bm->word)
710 continue;
711
712 bit = 0;
713 off = i * hctx->ctx_map.bits_per_word;
714 do {
715 bit = find_next_bit(&bm->word, bm->depth, bit);
716 if (bit >= bm->depth)
717 break;
718
719 ctx = hctx->ctxs[bit + off];
720 clear_bit(bit, &bm->word);
721 spin_lock(&ctx->lock);
722 list_splice_tail_init(&ctx->rq_list, list);
723 spin_unlock(&ctx->lock);
724
725 bit++;
726 } while (1);
727 }
728}
729
730/*
Jens Axboe320ae512013-10-24 09:20:05 +0100731 * Run this hardware queue, pulling any software queues mapped to it in.
732 * Note that this function currently has various problems around ordering
733 * of IO. In particular, we'd like FIFO behaviour on handling existing
734 * items on the hctx->dispatch list. Ignore that for now.
735 */
736static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
737{
738 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100739 struct request *rq;
740 LIST_HEAD(rq_list);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600741 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100742
Jens Axboefd1270d2014-04-16 09:23:48 -0600743 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600744
Jens Axboe5d12f902014-03-19 15:25:02 -0600745 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100746 return;
747
748 hctx->run++;
749
750 /*
751 * Touch any software queue that has pending entries.
752 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600753 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100754
755 /*
756 * If we have previous entries on our dispatch list, grab them
757 * and stuff them at the front for more fair dispatch.
758 */
759 if (!list_empty_careful(&hctx->dispatch)) {
760 spin_lock(&hctx->lock);
761 if (!list_empty(&hctx->dispatch))
762 list_splice_init(&hctx->dispatch, &rq_list);
763 spin_unlock(&hctx->lock);
764 }
765
766 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100767 * Now process all the entries, sending them to the driver.
768 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600769 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100770 while (!list_empty(&rq_list)) {
771 int ret;
772
773 rq = list_first_entry(&rq_list, struct request, queuelist);
774 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100775
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800776 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100777
778 ret = q->mq_ops->queue_rq(hctx, rq);
779 switch (ret) {
780 case BLK_MQ_RQ_QUEUE_OK:
781 queued++;
782 continue;
783 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100784 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200785 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100786 break;
787 default:
788 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100789 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800790 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100791 blk_mq_end_io(rq, rq->errors);
792 break;
793 }
794
795 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
796 break;
797 }
798
799 if (!queued)
800 hctx->dispatched[0]++;
801 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
802 hctx->dispatched[ilog2(queued) + 1]++;
803
804 /*
805 * Any items that need requeuing? Stuff them into hctx->dispatch,
806 * that is where we will continue on next queue run.
807 */
808 if (!list_empty(&rq_list)) {
809 spin_lock(&hctx->lock);
810 list_splice(&rq_list, &hctx->dispatch);
811 spin_unlock(&hctx->lock);
812 }
813}
814
Jens Axboe506e9312014-05-07 10:26:44 -0600815/*
816 * It'd be great if the workqueue API had a way to pass
817 * in a mask and had some smarts for more clever placement.
818 * For now we just round-robin here, switching for every
819 * BLK_MQ_CPU_WORK_BATCH queued items.
820 */
821static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
822{
823 int cpu = hctx->next_cpu;
824
825 if (--hctx->next_cpu_batch <= 0) {
826 int next_cpu;
827
828 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
829 if (next_cpu >= nr_cpu_ids)
830 next_cpu = cpumask_first(hctx->cpumask);
831
832 hctx->next_cpu = next_cpu;
833 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
834 }
835
836 return cpu;
837}
838
Jens Axboe320ae512013-10-24 09:20:05 +0100839void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
840{
Jens Axboe5d12f902014-03-19 15:25:02 -0600841 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100842 return;
843
Jens Axboee4043dc2014-04-09 10:18:23 -0600844 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100845 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600846 else if (hctx->queue->nr_hw_queues == 1)
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600847 kblockd_schedule_delayed_work(&hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600848 else {
849 unsigned int cpu;
850
Jens Axboe506e9312014-05-07 10:26:44 -0600851 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600852 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600853 }
Jens Axboe320ae512013-10-24 09:20:05 +0100854}
855
856void blk_mq_run_queues(struct request_queue *q, bool async)
857{
858 struct blk_mq_hw_ctx *hctx;
859 int i;
860
861 queue_for_each_hw_ctx(q, hctx, i) {
862 if ((!blk_mq_hctx_has_pending(hctx) &&
863 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600864 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100865 continue;
866
Jens Axboee4043dc2014-04-09 10:18:23 -0600867 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100868 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600869 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100870 }
871}
872EXPORT_SYMBOL(blk_mq_run_queues);
873
874void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
875{
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600876 cancel_delayed_work(&hctx->run_work);
877 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100878 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
879}
880EXPORT_SYMBOL(blk_mq_stop_hw_queue);
881
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100882void blk_mq_stop_hw_queues(struct request_queue *q)
883{
884 struct blk_mq_hw_ctx *hctx;
885 int i;
886
887 queue_for_each_hw_ctx(q, hctx, i)
888 blk_mq_stop_hw_queue(hctx);
889}
890EXPORT_SYMBOL(blk_mq_stop_hw_queues);
891
Jens Axboe320ae512013-10-24 09:20:05 +0100892void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
893{
894 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600895
896 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100897 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600898 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100899}
900EXPORT_SYMBOL(blk_mq_start_hw_queue);
901
Christoph Hellwig2f268552014-04-16 09:44:56 +0200902void blk_mq_start_hw_queues(struct request_queue *q)
903{
904 struct blk_mq_hw_ctx *hctx;
905 int i;
906
907 queue_for_each_hw_ctx(q, hctx, i)
908 blk_mq_start_hw_queue(hctx);
909}
910EXPORT_SYMBOL(blk_mq_start_hw_queues);
911
912
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200913void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100914{
915 struct blk_mq_hw_ctx *hctx;
916 int i;
917
918 queue_for_each_hw_ctx(q, hctx, i) {
919 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
920 continue;
921
922 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600923 preempt_disable();
Christoph Hellwig1b4a3252014-04-16 09:44:54 +0200924 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600925 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100926 }
927}
928EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
929
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600930static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100931{
932 struct blk_mq_hw_ctx *hctx;
933
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600934 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600935
Jens Axboe320ae512013-10-24 09:20:05 +0100936 __blk_mq_run_hw_queue(hctx);
937}
938
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600939static void blk_mq_delay_work_fn(struct work_struct *work)
940{
941 struct blk_mq_hw_ctx *hctx;
942
943 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
944
945 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
946 __blk_mq_run_hw_queue(hctx);
947}
948
949void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
950{
951 unsigned long tmo = msecs_to_jiffies(msecs);
952
953 if (hctx->queue->nr_hw_queues == 1)
954 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
955 else {
956 unsigned int cpu;
957
Jens Axboe506e9312014-05-07 10:26:44 -0600958 cpu = blk_mq_hctx_next_cpu(hctx);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600959 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
960 }
961}
962EXPORT_SYMBOL(blk_mq_delay_queue);
963
Jens Axboe320ae512013-10-24 09:20:05 +0100964static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800965 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100966{
967 struct blk_mq_ctx *ctx = rq->mq_ctx;
968
Jens Axboe01b983c2013-11-19 18:59:10 -0700969 trace_block_rq_insert(hctx->queue, rq);
970
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800971 if (at_head)
972 list_add(&rq->queuelist, &ctx->rq_list);
973 else
974 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe4bb659b2014-05-09 09:36:49 -0600975
Jens Axboe320ae512013-10-24 09:20:05 +0100976 blk_mq_hctx_mark_pending(hctx, ctx);
977
978 /*
979 * We do this early, to ensure we are on the right CPU.
980 */
Jens Axboe87ee7b12014-04-24 08:51:47 -0600981 blk_add_timer(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100982}
983
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600984void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
985 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100986{
987 struct request_queue *q = rq->q;
988 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600989 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100990
991 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600992 if (!cpu_online(ctx->cpu))
993 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100994
Jens Axboe320ae512013-10-24 09:20:05 +0100995 hctx = q->mq_ops->map_queue(q, ctx->cpu);
996
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600997 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
998 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
999 blk_insert_flush(rq);
1000 } else {
1001 spin_lock(&ctx->lock);
1002 __blk_mq_insert_request(hctx, rq, at_head);
1003 spin_unlock(&ctx->lock);
1004 }
Jens Axboe320ae512013-10-24 09:20:05 +01001005
Jens Axboe320ae512013-10-24 09:20:05 +01001006 if (run_queue)
1007 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -06001008
1009 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001010}
1011
1012static void blk_mq_insert_requests(struct request_queue *q,
1013 struct blk_mq_ctx *ctx,
1014 struct list_head *list,
1015 int depth,
1016 bool from_schedule)
1017
1018{
1019 struct blk_mq_hw_ctx *hctx;
1020 struct blk_mq_ctx *current_ctx;
1021
1022 trace_block_unplug(q, depth, !from_schedule);
1023
1024 current_ctx = blk_mq_get_ctx(q);
1025
1026 if (!cpu_online(ctx->cpu))
1027 ctx = current_ctx;
1028 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1029
1030 /*
1031 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1032 * offline now
1033 */
1034 spin_lock(&ctx->lock);
1035 while (!list_empty(list)) {
1036 struct request *rq;
1037
1038 rq = list_first_entry(list, struct request, queuelist);
1039 list_del_init(&rq->queuelist);
1040 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001041 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001042 }
1043 spin_unlock(&ctx->lock);
1044
Jens Axboe320ae512013-10-24 09:20:05 +01001045 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -06001046 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001047}
1048
1049static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1050{
1051 struct request *rqa = container_of(a, struct request, queuelist);
1052 struct request *rqb = container_of(b, struct request, queuelist);
1053
1054 return !(rqa->mq_ctx < rqb->mq_ctx ||
1055 (rqa->mq_ctx == rqb->mq_ctx &&
1056 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1057}
1058
1059void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1060{
1061 struct blk_mq_ctx *this_ctx;
1062 struct request_queue *this_q;
1063 struct request *rq;
1064 LIST_HEAD(list);
1065 LIST_HEAD(ctx_list);
1066 unsigned int depth;
1067
1068 list_splice_init(&plug->mq_list, &list);
1069
1070 list_sort(NULL, &list, plug_ctx_cmp);
1071
1072 this_q = NULL;
1073 this_ctx = NULL;
1074 depth = 0;
1075
1076 while (!list_empty(&list)) {
1077 rq = list_entry_rq(list.next);
1078 list_del_init(&rq->queuelist);
1079 BUG_ON(!rq->q);
1080 if (rq->mq_ctx != this_ctx) {
1081 if (this_ctx) {
1082 blk_mq_insert_requests(this_q, this_ctx,
1083 &ctx_list, depth,
1084 from_schedule);
1085 }
1086
1087 this_ctx = rq->mq_ctx;
1088 this_q = rq->q;
1089 depth = 0;
1090 }
1091
1092 depth++;
1093 list_add_tail(&rq->queuelist, &ctx_list);
1094 }
1095
1096 /*
1097 * If 'this_ctx' is set, we know we have entries to complete
1098 * on 'ctx_list'. Do those.
1099 */
1100 if (this_ctx) {
1101 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1102 from_schedule);
1103 }
1104}
1105
1106static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1107{
1108 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001109
1110 if (blk_do_io_stat(rq)) {
1111 rq->start_time = jiffies;
1112 blk_account_io_start(rq, 1);
1113 }
Jens Axboe320ae512013-10-24 09:20:05 +01001114}
1115
Jens Axboe07068d52014-05-22 10:40:51 -06001116static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1117 struct blk_mq_ctx *ctx,
1118 struct request *rq, struct bio *bio)
1119{
1120 struct request_queue *q = hctx->queue;
1121
1122 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1123 blk_mq_bio_to_request(rq, bio);
1124 spin_lock(&ctx->lock);
1125insert_rq:
1126 __blk_mq_insert_request(hctx, rq, false);
1127 spin_unlock(&ctx->lock);
1128 return false;
1129 } else {
1130 spin_lock(&ctx->lock);
1131 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1132 blk_mq_bio_to_request(rq, bio);
1133 goto insert_rq;
1134 }
1135
1136 spin_unlock(&ctx->lock);
1137 __blk_mq_free_request(hctx, ctx, rq);
1138 return true;
1139 }
1140}
1141
1142struct blk_map_ctx {
1143 struct blk_mq_hw_ctx *hctx;
1144 struct blk_mq_ctx *ctx;
1145};
1146
1147static struct request *blk_mq_map_request(struct request_queue *q,
1148 struct bio *bio,
1149 struct blk_map_ctx *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001150{
1151 struct blk_mq_hw_ctx *hctx;
1152 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001153 struct request *rq;
Jens Axboe07068d52014-05-22 10:40:51 -06001154 int rw = bio_data_dir(bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001155
Jens Axboe07068d52014-05-22 10:40:51 -06001156 if (unlikely(blk_mq_queue_enter(q))) {
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -07001157 bio_endio(bio, -EIO);
Jens Axboe07068d52014-05-22 10:40:51 -06001158 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001159 }
1160
1161 ctx = blk_mq_get_ctx(q);
1162 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1163
Jens Axboe07068d52014-05-22 10:40:51 -06001164 if (rw_is_sync(bio->bi_rw))
Shaohua Li27fbf4e82014-02-19 20:20:21 +08001165 rw |= REQ_SYNC;
Jens Axboe07068d52014-05-22 10:40:51 -06001166
Jens Axboe320ae512013-10-24 09:20:05 +01001167 trace_block_getrq(q, bio, rw);
Christoph Hellwig5dee8572014-05-27 20:59:47 +02001168 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, GFP_ATOMIC, false);
1169 if (unlikely(!rq)) {
Christoph Hellwig793597a2014-05-27 20:59:49 +02001170 __blk_mq_run_hw_queue(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001171 blk_mq_put_ctx(ctx);
1172 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001173
1174 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001175 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Christoph Hellwig793597a2014-05-27 20:59:49 +02001176 rq = __blk_mq_alloc_request(q, hctx, ctx, rw,
1177 __GFP_WAIT|GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001178 }
1179
1180 hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001181 data->hctx = hctx;
1182 data->ctx = ctx;
1183 return rq;
1184}
1185
1186/*
1187 * Multiple hardware queue variant. This will not use per-process plugs,
1188 * but will attempt to bypass the hctx queueing if we can go straight to
1189 * hardware for SYNC IO.
1190 */
1191static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1192{
1193 const int is_sync = rw_is_sync(bio->bi_rw);
1194 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1195 struct blk_map_ctx data;
1196 struct request *rq;
1197
1198 blk_queue_bounce(q, &bio);
1199
1200 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1201 bio_endio(bio, -EIO);
1202 return;
1203 }
1204
1205 rq = blk_mq_map_request(q, bio, &data);
1206 if (unlikely(!rq))
1207 return;
1208
1209 if (unlikely(is_flush_fua)) {
1210 blk_mq_bio_to_request(rq, bio);
1211 blk_insert_flush(rq);
1212 goto run_queue;
1213 }
1214
1215 if (is_sync) {
1216 int ret;
1217
1218 blk_mq_bio_to_request(rq, bio);
1219 blk_mq_start_request(rq, true);
1220
1221 /*
1222 * For OK queue, we are done. For error, kill it. Any other
1223 * error (busy), just add it to our list as we previously
1224 * would have done
1225 */
1226 ret = q->mq_ops->queue_rq(data.hctx, rq);
1227 if (ret == BLK_MQ_RQ_QUEUE_OK)
1228 goto done;
1229 else {
1230 __blk_mq_requeue_request(rq);
1231
1232 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1233 rq->errors = -EIO;
1234 blk_mq_end_io(rq, rq->errors);
1235 goto done;
1236 }
1237 }
1238 }
1239
1240 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1241 /*
1242 * For a SYNC request, send it to the hardware immediately. For
1243 * an ASYNC request, just ensure that we run it later on. The
1244 * latter allows for merging opportunities and more efficient
1245 * dispatching.
1246 */
1247run_queue:
1248 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1249 }
1250done:
1251 blk_mq_put_ctx(data.ctx);
1252}
1253
1254/*
1255 * Single hardware queue variant. This will attempt to use any per-process
1256 * plug for merging and IO deferral.
1257 */
1258static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1259{
1260 const int is_sync = rw_is_sync(bio->bi_rw);
1261 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1262 unsigned int use_plug, request_count = 0;
1263 struct blk_map_ctx data;
1264 struct request *rq;
1265
1266 /*
1267 * If we have multiple hardware queues, just go directly to
1268 * one of those for sync IO.
1269 */
1270 use_plug = !is_flush_fua && !is_sync;
1271
1272 blk_queue_bounce(q, &bio);
1273
1274 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1275 bio_endio(bio, -EIO);
1276 return;
1277 }
1278
1279 if (use_plug && !blk_queue_nomerges(q) &&
1280 blk_attempt_plug_merge(q, bio, &request_count))
1281 return;
1282
1283 rq = blk_mq_map_request(q, bio, &data);
Jens Axboe320ae512013-10-24 09:20:05 +01001284
1285 if (unlikely(is_flush_fua)) {
1286 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001287 blk_insert_flush(rq);
1288 goto run_queue;
1289 }
1290
1291 /*
1292 * A task plug currently exists. Since this is completely lockless,
1293 * utilize that to temporarily store requests until the task is
1294 * either done or scheduled away.
1295 */
1296 if (use_plug) {
1297 struct blk_plug *plug = current->plug;
1298
1299 if (plug) {
1300 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -06001301 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001302 trace_block_plug(q);
1303 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1304 blk_flush_plug_list(plug, false);
1305 trace_block_plug(q);
1306 }
1307 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe07068d52014-05-22 10:40:51 -06001308 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001309 return;
1310 }
1311 }
1312
Jens Axboe07068d52014-05-22 10:40:51 -06001313 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1314 /*
1315 * For a SYNC request, send it to the hardware immediately. For
1316 * an ASYNC request, just ensure that we run it later on. The
1317 * latter allows for merging opportunities and more efficient
1318 * dispatching.
1319 */
1320run_queue:
1321 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001322 }
1323
Jens Axboe07068d52014-05-22 10:40:51 -06001324 blk_mq_put_ctx(data.ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001325}
1326
1327/*
1328 * Default mapping to a software queue, since we use one per CPU.
1329 */
1330struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1331{
1332 return q->queue_hw_ctx[q->mq_map[cpu]];
1333}
1334EXPORT_SYMBOL(blk_mq_map_queue);
1335
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001336static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1337 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001338{
1339 struct page *page;
1340
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001341 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001342 int i;
1343
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001344 for (i = 0; i < tags->nr_tags; i++) {
1345 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001346 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001347 set->ops->exit_request(set->driver_data, tags->rqs[i],
1348 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001349 }
1350 }
1351
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001352 while (!list_empty(&tags->page_list)) {
1353 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001354 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001355 __free_pages(page, page->private);
1356 }
1357
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001358 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001359
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001360 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001361}
1362
1363static size_t order_to_size(unsigned int order)
1364{
Ming Lei4ca08502014-04-19 18:00:18 +08001365 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001366}
1367
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001368static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1369 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001370{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001371 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001372 unsigned int i, j, entries_per_page, max_order = 4;
1373 size_t rq_size, left;
1374
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001375 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1376 set->numa_node);
1377 if (!tags)
1378 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001379
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001380 INIT_LIST_HEAD(&tags->page_list);
1381
1382 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1383 GFP_KERNEL, set->numa_node);
1384 if (!tags->rqs) {
1385 blk_mq_free_tags(tags);
1386 return NULL;
1387 }
Jens Axboe320ae512013-10-24 09:20:05 +01001388
1389 /*
1390 * rq_size is the size of the request plus driver payload, rounded
1391 * to the cacheline size
1392 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001393 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001394 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001395 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001396
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001397 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001398 int this_order = max_order;
1399 struct page *page;
1400 int to_do;
1401 void *p;
1402
1403 while (left < order_to_size(this_order - 1) && this_order)
1404 this_order--;
1405
1406 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001407 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1408 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001409 if (page)
1410 break;
1411 if (!this_order--)
1412 break;
1413 if (order_to_size(this_order) < rq_size)
1414 break;
1415 } while (1);
1416
1417 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001418 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001419
1420 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001421 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001422
1423 p = page_address(page);
1424 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001425 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001426 left -= to_do * rq_size;
1427 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001428 tags->rqs[i] = p;
1429 if (set->ops->init_request) {
1430 if (set->ops->init_request(set->driver_data,
1431 tags->rqs[i], hctx_idx, i,
1432 set->numa_node))
1433 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001434 }
1435
Jens Axboe320ae512013-10-24 09:20:05 +01001436 p += rq_size;
1437 i++;
1438 }
1439 }
1440
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001441 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001442
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001443fail:
1444 pr_warn("%s: failed to allocate requests\n", __func__);
1445 blk_mq_free_rq_map(set, tags, hctx_idx);
1446 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001447}
1448
Jens Axboe1429d7c2014-05-19 09:23:55 -06001449static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1450{
1451 kfree(bitmap->map);
1452}
1453
1454static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1455{
1456 unsigned int bpw = 8, total, num_maps, i;
1457
1458 bitmap->bits_per_word = bpw;
1459
1460 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1461 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1462 GFP_KERNEL, node);
1463 if (!bitmap->map)
1464 return -ENOMEM;
1465
1466 bitmap->map_size = num_maps;
1467
1468 total = nr_cpu_ids;
1469 for (i = 0; i < num_maps; i++) {
1470 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1471 total -= bitmap->map[i].depth;
1472 }
1473
1474 return 0;
1475}
1476
Jens Axboe484b4062014-05-21 14:01:15 -06001477static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1478{
1479 struct request_queue *q = hctx->queue;
1480 struct blk_mq_ctx *ctx;
1481 LIST_HEAD(tmp);
1482
1483 /*
1484 * Move ctx entries to new CPU, if this one is going away.
1485 */
1486 ctx = __blk_mq_get_ctx(q, cpu);
1487
1488 spin_lock(&ctx->lock);
1489 if (!list_empty(&ctx->rq_list)) {
1490 list_splice_init(&ctx->rq_list, &tmp);
1491 blk_mq_hctx_clear_pending(hctx, ctx);
1492 }
1493 spin_unlock(&ctx->lock);
1494
1495 if (list_empty(&tmp))
1496 return NOTIFY_OK;
1497
1498 ctx = blk_mq_get_ctx(q);
1499 spin_lock(&ctx->lock);
1500
1501 while (!list_empty(&tmp)) {
1502 struct request *rq;
1503
1504 rq = list_first_entry(&tmp, struct request, queuelist);
1505 rq->mq_ctx = ctx;
1506 list_move_tail(&rq->queuelist, &ctx->rq_list);
1507 }
1508
1509 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1510 blk_mq_hctx_mark_pending(hctx, ctx);
1511
1512 spin_unlock(&ctx->lock);
1513
1514 blk_mq_run_hw_queue(hctx, true);
1515 blk_mq_put_ctx(ctx);
1516 return NOTIFY_OK;
1517}
1518
1519static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1520{
1521 struct request_queue *q = hctx->queue;
1522 struct blk_mq_tag_set *set = q->tag_set;
1523
1524 if (set->tags[hctx->queue_num])
1525 return NOTIFY_OK;
1526
1527 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1528 if (!set->tags[hctx->queue_num])
1529 return NOTIFY_STOP;
1530
1531 hctx->tags = set->tags[hctx->queue_num];
1532 return NOTIFY_OK;
1533}
1534
1535static int blk_mq_hctx_notify(void *data, unsigned long action,
1536 unsigned int cpu)
1537{
1538 struct blk_mq_hw_ctx *hctx = data;
1539
1540 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1541 return blk_mq_hctx_cpu_offline(hctx, cpu);
1542 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1543 return blk_mq_hctx_cpu_online(hctx, cpu);
1544
1545 return NOTIFY_OK;
1546}
1547
Ming Lei624dbe42014-05-27 23:35:13 +08001548static void blk_mq_exit_hw_queues(struct request_queue *q,
1549 struct blk_mq_tag_set *set, int nr_queue)
1550{
1551 struct blk_mq_hw_ctx *hctx;
1552 unsigned int i;
1553
1554 queue_for_each_hw_ctx(q, hctx, i) {
1555 if (i == nr_queue)
1556 break;
1557
1558 if (set->ops->exit_hctx)
1559 set->ops->exit_hctx(hctx, i);
1560
1561 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1562 kfree(hctx->ctxs);
1563 blk_mq_free_bitmap(&hctx->ctx_map);
1564 }
1565
1566}
1567
1568static void blk_mq_free_hw_queues(struct request_queue *q,
1569 struct blk_mq_tag_set *set)
1570{
1571 struct blk_mq_hw_ctx *hctx;
1572 unsigned int i;
1573
1574 queue_for_each_hw_ctx(q, hctx, i) {
1575 free_cpumask_var(hctx->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001576 kfree(hctx);
Ming Lei624dbe42014-05-27 23:35:13 +08001577 }
1578}
1579
Jens Axboe320ae512013-10-24 09:20:05 +01001580static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001581 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001582{
1583 struct blk_mq_hw_ctx *hctx;
Ming Lei624dbe42014-05-27 23:35:13 +08001584 unsigned int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001585
1586 /*
1587 * Initialize hardware queues
1588 */
1589 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001590 int node;
1591
1592 node = hctx->numa_node;
1593 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001594 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001595
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001596 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1597 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
Jens Axboe320ae512013-10-24 09:20:05 +01001598 spin_lock_init(&hctx->lock);
1599 INIT_LIST_HEAD(&hctx->dispatch);
1600 hctx->queue = q;
1601 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001602 hctx->flags = set->flags;
1603 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001604
1605 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1606 blk_mq_hctx_notify, hctx);
1607 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1608
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001609 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001610
1611 /*
1612 * Allocate space for all possible cpus to avoid allocation in
1613 * runtime
1614 */
1615 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1616 GFP_KERNEL, node);
1617 if (!hctx->ctxs)
1618 break;
1619
Jens Axboe1429d7c2014-05-19 09:23:55 -06001620 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
Jens Axboe320ae512013-10-24 09:20:05 +01001621 break;
1622
Jens Axboe320ae512013-10-24 09:20:05 +01001623 hctx->nr_ctx = 0;
1624
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001625 if (set->ops->init_hctx &&
1626 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001627 break;
1628 }
1629
1630 if (i == q->nr_hw_queues)
1631 return 0;
1632
1633 /*
1634 * Init failed
1635 */
Ming Lei624dbe42014-05-27 23:35:13 +08001636 blk_mq_exit_hw_queues(q, set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001637
1638 return 1;
1639}
1640
1641static void blk_mq_init_cpu_queues(struct request_queue *q,
1642 unsigned int nr_hw_queues)
1643{
1644 unsigned int i;
1645
1646 for_each_possible_cpu(i) {
1647 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1648 struct blk_mq_hw_ctx *hctx;
1649
1650 memset(__ctx, 0, sizeof(*__ctx));
1651 __ctx->cpu = i;
1652 spin_lock_init(&__ctx->lock);
1653 INIT_LIST_HEAD(&__ctx->rq_list);
1654 __ctx->queue = q;
1655
1656 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001657 if (!cpu_online(i))
1658 continue;
1659
Jens Axboee4043dc2014-04-09 10:18:23 -06001660 hctx = q->mq_ops->map_queue(q, i);
1661 cpumask_set_cpu(i, hctx->cpumask);
1662 hctx->nr_ctx++;
1663
Jens Axboe320ae512013-10-24 09:20:05 +01001664 /*
1665 * Set local node, IFF we have more than one hw queue. If
1666 * not, we remain on the home node of the device
1667 */
1668 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1669 hctx->numa_node = cpu_to_node(i);
1670 }
1671}
1672
1673static void blk_mq_map_swqueue(struct request_queue *q)
1674{
1675 unsigned int i;
1676 struct blk_mq_hw_ctx *hctx;
1677 struct blk_mq_ctx *ctx;
1678
1679 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001680 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001681 hctx->nr_ctx = 0;
1682 }
1683
1684 /*
1685 * Map software to hardware queues
1686 */
1687 queue_for_each_ctx(q, ctx, i) {
1688 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001689 if (!cpu_online(i))
1690 continue;
1691
Jens Axboe320ae512013-10-24 09:20:05 +01001692 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001693 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001694 ctx->index_hw = hctx->nr_ctx;
1695 hctx->ctxs[hctx->nr_ctx++] = ctx;
1696 }
Jens Axboe506e9312014-05-07 10:26:44 -06001697
1698 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001699 /*
1700 * If not software queues are mapped to this hardware queue,
1701 * disable it and free the request entries
1702 */
1703 if (!hctx->nr_ctx) {
1704 struct blk_mq_tag_set *set = q->tag_set;
1705
1706 if (set->tags[i]) {
1707 blk_mq_free_rq_map(set, set->tags[i], i);
1708 set->tags[i] = NULL;
1709 hctx->tags = NULL;
1710 }
1711 continue;
1712 }
1713
1714 /*
1715 * Initialize batch roundrobin counts
1716 */
Jens Axboe506e9312014-05-07 10:26:44 -06001717 hctx->next_cpu = cpumask_first(hctx->cpumask);
1718 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1719 }
Jens Axboe320ae512013-10-24 09:20:05 +01001720}
1721
Jens Axboe0d2602c2014-05-13 15:10:52 -06001722static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1723{
1724 struct blk_mq_hw_ctx *hctx;
1725 struct request_queue *q;
1726 bool shared;
1727 int i;
1728
1729 if (set->tag_list.next == set->tag_list.prev)
1730 shared = false;
1731 else
1732 shared = true;
1733
1734 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1735 blk_mq_freeze_queue(q);
1736
1737 queue_for_each_hw_ctx(q, hctx, i) {
1738 if (shared)
1739 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1740 else
1741 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1742 }
1743 blk_mq_unfreeze_queue(q);
1744 }
1745}
1746
1747static void blk_mq_del_queue_tag_set(struct request_queue *q)
1748{
1749 struct blk_mq_tag_set *set = q->tag_set;
1750
1751 blk_mq_freeze_queue(q);
1752
1753 mutex_lock(&set->tag_list_lock);
1754 list_del_init(&q->tag_set_list);
1755 blk_mq_update_tag_set_depth(set);
1756 mutex_unlock(&set->tag_list_lock);
1757
1758 blk_mq_unfreeze_queue(q);
1759}
1760
1761static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1762 struct request_queue *q)
1763{
1764 q->tag_set = set;
1765
1766 mutex_lock(&set->tag_list_lock);
1767 list_add_tail(&q->tag_set_list, &set->tag_list);
1768 blk_mq_update_tag_set_depth(set);
1769 mutex_unlock(&set->tag_list_lock);
1770}
1771
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001772struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001773{
1774 struct blk_mq_hw_ctx **hctxs;
1775 struct blk_mq_ctx *ctx;
1776 struct request_queue *q;
Jens Axboef14bbe72014-05-27 12:06:53 -06001777 unsigned int *map;
Jens Axboe320ae512013-10-24 09:20:05 +01001778 int i;
1779
Jens Axboe320ae512013-10-24 09:20:05 +01001780 ctx = alloc_percpu(struct blk_mq_ctx);
1781 if (!ctx)
1782 return ERR_PTR(-ENOMEM);
1783
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001784 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1785 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001786
1787 if (!hctxs)
1788 goto err_percpu;
1789
Jens Axboef14bbe72014-05-27 12:06:53 -06001790 map = blk_mq_make_queue_map(set);
1791 if (!map)
1792 goto err_map;
1793
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001794 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboef14bbe72014-05-27 12:06:53 -06001795 int node = blk_mq_hw_queue_to_node(map, i);
1796
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001797 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1798 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001799 if (!hctxs[i])
1800 goto err_hctxs;
1801
Jens Axboee4043dc2014-04-09 10:18:23 -06001802 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1803 goto err_hctxs;
1804
Jens Axboe0d2602c2014-05-13 15:10:52 -06001805 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001806 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001807 hctxs[i]->queue_num = i;
1808 }
1809
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001810 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001811 if (!q)
1812 goto err_hctxs;
1813
Ming Lei3d2936f2014-05-27 23:35:14 +08001814 if (percpu_counter_init(&q->mq_usage_counter, 0))
1815 goto err_map;
1816
Jens Axboe320ae512013-10-24 09:20:05 +01001817 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1818 blk_queue_rq_timeout(q, 30000);
1819
1820 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001821 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboef14bbe72014-05-27 12:06:53 -06001822 q->mq_map = map;
Jens Axboe320ae512013-10-24 09:20:05 +01001823
1824 q->queue_ctx = ctx;
1825 q->queue_hw_ctx = hctxs;
1826
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001827 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001828 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001829
Jens Axboe05f1dd52014-05-29 09:53:32 -06001830 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1831 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1832
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001833 q->sg_reserved_size = INT_MAX;
1834
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001835 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1836 INIT_LIST_HEAD(&q->requeue_list);
1837 spin_lock_init(&q->requeue_lock);
1838
Jens Axboe07068d52014-05-22 10:40:51 -06001839 if (q->nr_hw_queues > 1)
1840 blk_queue_make_request(q, blk_mq_make_request);
1841 else
1842 blk_queue_make_request(q, blk_sq_make_request);
1843
Jens Axboe87ee7b12014-04-24 08:51:47 -06001844 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001845 if (set->timeout)
1846 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001847
Jens Axboeeba71762014-05-20 15:17:27 -06001848 /*
1849 * Do this after blk_queue_make_request() overrides it...
1850 */
1851 q->nr_requests = set->queue_depth;
1852
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001853 if (set->ops->complete)
1854 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001855
Jens Axboe320ae512013-10-24 09:20:05 +01001856 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001857 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001858
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001859 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1860 set->cmd_size, cache_line_size()),
1861 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001862 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001863 goto err_hw;
1864
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001865 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001866 goto err_flush_rq;
1867
Jens Axboe320ae512013-10-24 09:20:05 +01001868 mutex_lock(&all_q_mutex);
1869 list_add_tail(&q->all_q_node, &all_q_list);
1870 mutex_unlock(&all_q_mutex);
1871
Jens Axboe0d2602c2014-05-13 15:10:52 -06001872 blk_mq_add_queue_tag_set(set, q);
1873
Jens Axboe484b4062014-05-21 14:01:15 -06001874 blk_mq_map_swqueue(q);
1875
Jens Axboe320ae512013-10-24 09:20:05 +01001876 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001877
1878err_flush_rq:
1879 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001880err_hw:
Jens Axboe320ae512013-10-24 09:20:05 +01001881 blk_cleanup_queue(q);
1882err_hctxs:
Jens Axboef14bbe72014-05-27 12:06:53 -06001883 kfree(map);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001884 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001885 if (!hctxs[i])
1886 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001887 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001888 kfree(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01001889 }
Jens Axboef14bbe72014-05-27 12:06:53 -06001890err_map:
Jens Axboe320ae512013-10-24 09:20:05 +01001891 kfree(hctxs);
1892err_percpu:
1893 free_percpu(ctx);
1894 return ERR_PTR(-ENOMEM);
1895}
1896EXPORT_SYMBOL(blk_mq_init_queue);
1897
1898void blk_mq_free_queue(struct request_queue *q)
1899{
Ming Lei624dbe42014-05-27 23:35:13 +08001900 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001901
Jens Axboe0d2602c2014-05-13 15:10:52 -06001902 blk_mq_del_queue_tag_set(q);
1903
Ming Lei624dbe42014-05-27 23:35:13 +08001904 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1905 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01001906
Ming Lei3d2936f2014-05-27 23:35:14 +08001907 percpu_counter_destroy(&q->mq_usage_counter);
1908
Jens Axboe320ae512013-10-24 09:20:05 +01001909 free_percpu(q->queue_ctx);
1910 kfree(q->queue_hw_ctx);
1911 kfree(q->mq_map);
1912
1913 q->queue_ctx = NULL;
1914 q->queue_hw_ctx = NULL;
1915 q->mq_map = NULL;
1916
1917 mutex_lock(&all_q_mutex);
1918 list_del_init(&q->all_q_node);
1919 mutex_unlock(&all_q_mutex);
1920}
Jens Axboe320ae512013-10-24 09:20:05 +01001921
1922/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001923static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001924{
1925 blk_mq_freeze_queue(q);
1926
Jens Axboe67aec142014-05-30 08:25:36 -06001927 blk_mq_sysfs_unregister(q);
1928
Jens Axboe320ae512013-10-24 09:20:05 +01001929 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1930
1931 /*
1932 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1933 * we should change hctx numa_node according to new topology (this
1934 * involves free and re-allocate memory, worthy doing?)
1935 */
1936
1937 blk_mq_map_swqueue(q);
1938
Jens Axboe67aec142014-05-30 08:25:36 -06001939 blk_mq_sysfs_register(q);
1940
Jens Axboe320ae512013-10-24 09:20:05 +01001941 blk_mq_unfreeze_queue(q);
1942}
1943
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001944static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1945 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001946{
1947 struct request_queue *q;
1948
1949 /*
Jens Axboe9fccfed2014-05-08 14:50:19 -06001950 * Before new mappings are established, hotadded cpu might already
1951 * start handling requests. This doesn't break anything as we map
1952 * offline CPUs to first hardware queue. We will re-init the queue
1953 * below to get optimal settings.
Jens Axboe320ae512013-10-24 09:20:05 +01001954 */
1955 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1956 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1957 return NOTIFY_OK;
1958
1959 mutex_lock(&all_q_mutex);
1960 list_for_each_entry(q, &all_q_list, all_q_node)
1961 blk_mq_queue_reinit(q);
1962 mutex_unlock(&all_q_mutex);
1963 return NOTIFY_OK;
1964}
1965
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001966int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1967{
1968 int i;
1969
1970 if (!set->nr_hw_queues)
1971 return -EINVAL;
1972 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1973 return -EINVAL;
1974 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1975 return -EINVAL;
1976
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001977 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001978 return -EINVAL;
1979
1980
Ming Lei484790052014-04-19 18:00:17 +08001981 set->tags = kmalloc_node(set->nr_hw_queues *
1982 sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001983 GFP_KERNEL, set->numa_node);
1984 if (!set->tags)
1985 goto out;
1986
1987 for (i = 0; i < set->nr_hw_queues; i++) {
1988 set->tags[i] = blk_mq_init_rq_map(set, i);
1989 if (!set->tags[i])
1990 goto out_unwind;
1991 }
1992
Jens Axboe0d2602c2014-05-13 15:10:52 -06001993 mutex_init(&set->tag_list_lock);
1994 INIT_LIST_HEAD(&set->tag_list);
1995
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001996 return 0;
1997
1998out_unwind:
1999 while (--i >= 0)
2000 blk_mq_free_rq_map(set, set->tags[i], i);
2001out:
2002 return -ENOMEM;
2003}
2004EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2005
2006void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2007{
2008 int i;
2009
Jens Axboe484b4062014-05-21 14:01:15 -06002010 for (i = 0; i < set->nr_hw_queues; i++) {
2011 if (set->tags[i])
2012 blk_mq_free_rq_map(set, set->tags[i], i);
2013 }
2014
Ming Lei981bd182014-04-24 00:07:34 +08002015 kfree(set->tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002016}
2017EXPORT_SYMBOL(blk_mq_free_tag_set);
2018
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002019int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2020{
2021 struct blk_mq_tag_set *set = q->tag_set;
2022 struct blk_mq_hw_ctx *hctx;
2023 int i, ret;
2024
2025 if (!set || nr > set->queue_depth)
2026 return -EINVAL;
2027
2028 ret = 0;
2029 queue_for_each_hw_ctx(q, hctx, i) {
2030 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2031 if (ret)
2032 break;
2033 }
2034
2035 if (!ret)
2036 q->nr_requests = nr;
2037
2038 return ret;
2039}
2040
Jens Axboe676141e2014-03-20 13:29:18 -06002041void blk_mq_disable_hotplug(void)
2042{
2043 mutex_lock(&all_q_mutex);
2044}
2045
2046void blk_mq_enable_hotplug(void)
2047{
2048 mutex_unlock(&all_q_mutex);
2049}
2050
Jens Axboe320ae512013-10-24 09:20:05 +01002051static int __init blk_mq_init(void)
2052{
Jens Axboe320ae512013-10-24 09:20:05 +01002053 blk_mq_cpu_init();
2054
2055 /* Must be called after percpu_counter_hotcpu_callback() */
2056 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2057
2058 return 0;
2059}
2060subsys_initcall(blk_mq_init);