blob: 963a8210938624d3db05ca3d609791b25c7a356b [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];
Christoph Hellwiged448322014-04-14 10:30:10 +020085 blk_rq_init(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +010086 rq->tag = tag;
87
88 return rq;
89 }
90
91 return NULL;
92}
93
94static int blk_mq_queue_enter(struct request_queue *q)
95{
96 int ret;
97
98 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
99 smp_wmb();
100 /* we have problems to freeze the queue if it's initializing */
101 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
102 return 0;
103
104 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
105
106 spin_lock_irq(q->queue_lock);
107 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
Ming Lei43a5e4e2013-12-26 21:31:35 +0800108 !blk_queue_bypass(q) || blk_queue_dying(q),
109 *q->queue_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100110 /* inc usage with lock hold to avoid freeze_queue runs here */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800111 if (!ret && !blk_queue_dying(q))
Jens Axboe320ae512013-10-24 09:20:05 +0100112 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800113 else if (blk_queue_dying(q))
114 ret = -ENODEV;
Jens Axboe320ae512013-10-24 09:20:05 +0100115 spin_unlock_irq(q->queue_lock);
116
117 return ret;
118}
119
120static void blk_mq_queue_exit(struct request_queue *q)
121{
122 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
123}
124
Ming Lei43a5e4e2013-12-26 21:31:35 +0800125static void __blk_mq_drain_queue(struct request_queue *q)
126{
127 while (true) {
128 s64 count;
129
130 spin_lock_irq(q->queue_lock);
131 count = percpu_counter_sum(&q->mq_usage_counter);
132 spin_unlock_irq(q->queue_lock);
133
134 if (count == 0)
135 break;
136 blk_mq_run_queues(q, false);
137 msleep(10);
138 }
139}
140
Jens Axboe320ae512013-10-24 09:20:05 +0100141/*
142 * Guarantee no request is in use, so we can change any data structure of
143 * the queue afterward.
144 */
145static void blk_mq_freeze_queue(struct request_queue *q)
146{
147 bool drain;
148
149 spin_lock_irq(q->queue_lock);
150 drain = !q->bypass_depth++;
151 queue_flag_set(QUEUE_FLAG_BYPASS, q);
152 spin_unlock_irq(q->queue_lock);
153
Ming Lei43a5e4e2013-12-26 21:31:35 +0800154 if (drain)
155 __blk_mq_drain_queue(q);
156}
Jens Axboe320ae512013-10-24 09:20:05 +0100157
Ming Lei43a5e4e2013-12-26 21:31:35 +0800158void blk_mq_drain_queue(struct request_queue *q)
159{
160 __blk_mq_drain_queue(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100161}
162
163static void blk_mq_unfreeze_queue(struct request_queue *q)
164{
165 bool wake = false;
166
167 spin_lock_irq(q->queue_lock);
168 if (!--q->bypass_depth) {
169 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
170 wake = true;
171 }
172 WARN_ON_ONCE(q->bypass_depth < 0);
173 spin_unlock_irq(q->queue_lock);
174 if (wake)
175 wake_up_all(&q->mq_freeze_wq);
176}
177
178bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
179{
180 return blk_mq_has_free_tags(hctx->tags);
181}
182EXPORT_SYMBOL(blk_mq_can_queue);
183
Jens Axboe94eddfb2013-11-19 09:25:07 -0700184static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
185 struct request *rq, unsigned int rw_flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100186{
Jens Axboe94eddfb2013-11-19 09:25:07 -0700187 if (blk_queue_io_stat(q))
188 rw_flags |= REQ_IO_STAT;
189
Jens Axboe320ae512013-10-24 09:20:05 +0100190 rq->mq_ctx = ctx;
191 rq->cmd_flags = rw_flags;
Ming Lei0fec08b2014-01-03 10:00:08 -0700192 rq->start_time = jiffies;
193 set_start_time_ns(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100194 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
195}
196
Jens Axboe320ae512013-10-24 09:20:05 +0100197static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
198 int rw, gfp_t gfp,
199 bool reserved)
200{
201 struct request *rq;
202
203 do {
204 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
205 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
206
Christoph Hellwig18741982014-02-10 09:29:00 -0700207 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
Jens Axboe320ae512013-10-24 09:20:05 +0100208 if (rq) {
Jens Axboe94eddfb2013-11-19 09:25:07 -0700209 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100210 break;
Jeff Moyer959a35f2013-12-03 14:23:00 -0700211 }
Jens Axboe320ae512013-10-24 09:20:05 +0100212
Jens Axboee4043dc2014-04-09 10:18:23 -0600213 if (gfp & __GFP_WAIT) {
214 __blk_mq_run_hw_queue(hctx);
215 blk_mq_put_ctx(ctx);
216 } else {
217 blk_mq_put_ctx(ctx);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700218 break;
Jens Axboee4043dc2014-04-09 10:18:23 -0600219 }
Jeff Moyer959a35f2013-12-03 14:23:00 -0700220
Jens Axboe320ae512013-10-24 09:20:05 +0100221 blk_mq_wait_for_tags(hctx->tags);
222 } while (1);
223
224 return rq;
225}
226
Christoph Hellwig18741982014-02-10 09:29:00 -0700227struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
Jens Axboe320ae512013-10-24 09:20:05 +0100228{
229 struct request *rq;
230
231 if (blk_mq_queue_enter(q))
232 return NULL;
233
Christoph Hellwig18741982014-02-10 09:29:00 -0700234 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700235 if (rq)
236 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100237 return rq;
238}
239
240struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
241 gfp_t gfp)
242{
243 struct request *rq;
244
245 if (blk_mq_queue_enter(q))
246 return NULL;
247
248 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
Jeff Moyer959a35f2013-12-03 14:23:00 -0700249 if (rq)
250 blk_mq_put_ctx(rq->mq_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100251 return rq;
252}
253EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
254
Jens Axboe320ae512013-10-24 09:20:05 +0100255static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
256 struct blk_mq_ctx *ctx, struct request *rq)
257{
258 const int tag = rq->tag;
259 struct request_queue *q = rq->q;
260
Jens Axboe320ae512013-10-24 09:20:05 +0100261 blk_mq_put_tag(hctx->tags, tag);
Jens Axboe320ae512013-10-24 09:20:05 +0100262 blk_mq_queue_exit(q);
263}
264
265void blk_mq_free_request(struct request *rq)
266{
267 struct blk_mq_ctx *ctx = rq->mq_ctx;
268 struct blk_mq_hw_ctx *hctx;
269 struct request_queue *q = rq->q;
270
271 ctx->rq_completed[rq_is_sync(rq)]++;
272
273 hctx = q->mq_ops->map_queue(q, ctx->cpu);
274 __blk_mq_free_request(hctx, ctx, rq);
275}
276
Christoph Hellwig8727af42014-04-14 10:30:08 +0200277/*
278 * Clone all relevant state from a request that has been put on hold in
279 * the flush state machine into the preallocated flush request that hangs
280 * off the request queue.
281 *
282 * For a driver the flush request should be invisible, that's why we are
283 * impersonating the original request here.
284 */
285void blk_mq_clone_flush_request(struct request *flush_rq,
286 struct request *orig_rq)
287{
288 struct blk_mq_hw_ctx *hctx =
289 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
290
291 flush_rq->mq_ctx = orig_rq->mq_ctx;
292 flush_rq->tag = orig_rq->tag;
293 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
294 hctx->cmd_size);
295}
296
Christoph Hellwig63151a42014-04-16 09:44:52 +0200297inline void __blk_mq_end_io(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100298{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700299 blk_account_io_done(rq);
300
Christoph Hellwig91b63632014-04-16 09:44:53 +0200301 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100302 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200303 } else {
304 if (unlikely(blk_bidi_rq(rq)))
305 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100306 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200307 }
Jens Axboe320ae512013-10-24 09:20:05 +0100308}
Christoph Hellwig63151a42014-04-16 09:44:52 +0200309EXPORT_SYMBOL(__blk_mq_end_io);
310
311void blk_mq_end_io(struct request *rq, int error)
312{
313 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
314 BUG();
315 __blk_mq_end_io(rq, error);
316}
317EXPORT_SYMBOL(blk_mq_end_io);
Jens Axboe320ae512013-10-24 09:20:05 +0100318
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800319static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100320{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800321 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100322
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800323 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100324}
325
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800326void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100327{
328 struct blk_mq_ctx *ctx = rq->mq_ctx;
329 int cpu;
330
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800331 if (!ctx->ipi_redirect) {
332 rq->q->softirq_done_fn(rq);
333 return;
334 }
Jens Axboe320ae512013-10-24 09:20:05 +0100335
336 cpu = get_cpu();
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800337 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800338 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800339 rq->csd.info = rq;
340 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100341 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800342 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800343 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800344 }
Jens Axboe320ae512013-10-24 09:20:05 +0100345 put_cpu();
346}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800347
348/**
349 * blk_mq_complete_request - end I/O on a request
350 * @rq: the request being processed
351 *
352 * Description:
353 * Ends all I/O on a request. It does not handle partial completions.
354 * The actual completion happens out-of-order, through a IPI handler.
355 **/
356void blk_mq_complete_request(struct request *rq)
357{
358 if (unlikely(blk_should_fake_timeout(rq->q)))
359 return;
360 if (!blk_mark_rq_complete(rq))
361 __blk_mq_complete_request(rq);
362}
363EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100364
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800365static void blk_mq_start_request(struct request *rq, bool last)
Jens Axboe320ae512013-10-24 09:20:05 +0100366{
367 struct request_queue *q = rq->q;
368
369 trace_block_rq_issue(q, rq);
370
Christoph Hellwig742ee692014-04-14 10:30:06 +0200371 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200372 if (unlikely(blk_bidi_rq(rq)))
373 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200374
Jens Axboe320ae512013-10-24 09:20:05 +0100375 /*
376 * Just mark start time and set the started bit. Due to memory
377 * ordering, we know we'll see the correct deadline as long as
378 * REQ_ATOMIC_STARTED is seen.
379 */
380 rq->deadline = jiffies + q->rq_timeout;
381 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800382
383 if (q->dma_drain_size && blk_rq_bytes(rq)) {
384 /*
385 * Make sure space for the drain appears. We know we can do
386 * this because max_hw_segments has been adjusted to be one
387 * fewer than the device can handle.
388 */
389 rq->nr_phys_segments++;
390 }
391
392 /*
393 * Flag the last request in the series so that drivers know when IO
394 * should be kicked off, if they don't do it on a per-request basis.
395 *
396 * Note: the flag isn't the only condition drivers should do kick off.
397 * If drive is busy, the last request might not have the bit set.
398 */
399 if (last)
400 rq->cmd_flags |= REQ_END;
Jens Axboe320ae512013-10-24 09:20:05 +0100401}
402
403static void blk_mq_requeue_request(struct request *rq)
404{
405 struct request_queue *q = rq->q;
406
407 trace_block_rq_requeue(q, rq);
408 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800409
410 rq->cmd_flags &= ~REQ_END;
411
412 if (q->dma_drain_size && blk_rq_bytes(rq))
413 rq->nr_phys_segments--;
Jens Axboe320ae512013-10-24 09:20:05 +0100414}
415
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600416struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
417{
418 return tags->rqs[tag];
419}
420EXPORT_SYMBOL(blk_mq_tag_to_rq);
421
Jens Axboe320ae512013-10-24 09:20:05 +0100422struct blk_mq_timeout_data {
423 struct blk_mq_hw_ctx *hctx;
424 unsigned long *next;
425 unsigned int *next_set;
426};
427
428static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
429{
430 struct blk_mq_timeout_data *data = __data;
431 struct blk_mq_hw_ctx *hctx = data->hctx;
432 unsigned int tag;
433
434 /* It may not be in flight yet (this is where
435 * the REQ_ATOMIC_STARTED flag comes in). The requests are
436 * statically allocated, so we know it's always safe to access the
437 * memory associated with a bit offset into ->rqs[].
438 */
439 tag = 0;
440 do {
441 struct request *rq;
442
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600443 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
444 if (tag >= hctx->tags->nr_tags)
Jens Axboe320ae512013-10-24 09:20:05 +0100445 break;
446
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600447 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
448 if (rq->q != hctx->queue)
449 continue;
Jens Axboe320ae512013-10-24 09:20:05 +0100450 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
451 continue;
452
453 blk_rq_check_expired(rq, data->next, data->next_set);
454 } while (1);
455}
456
457static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
458 unsigned long *next,
459 unsigned int *next_set)
460{
461 struct blk_mq_timeout_data data = {
462 .hctx = hctx,
463 .next = next,
464 .next_set = next_set,
465 };
466
467 /*
468 * Ask the tagging code to iterate busy requests, so we can
469 * check them for timeout.
470 */
471 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
472}
473
474static void blk_mq_rq_timer(unsigned long data)
475{
476 struct request_queue *q = (struct request_queue *) data;
477 struct blk_mq_hw_ctx *hctx;
478 unsigned long next = 0;
479 int i, next_set = 0;
480
481 queue_for_each_hw_ctx(q, hctx, i)
482 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
483
484 if (next_set)
485 mod_timer(&q->timeout, round_jiffies_up(next));
486}
487
488/*
489 * Reverse check our software queue for entries that we could potentially
490 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
491 * too much time checking for merges.
492 */
493static bool blk_mq_attempt_merge(struct request_queue *q,
494 struct blk_mq_ctx *ctx, struct bio *bio)
495{
496 struct request *rq;
497 int checked = 8;
498
499 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
500 int el_ret;
501
502 if (!checked--)
503 break;
504
505 if (!blk_rq_merge_ok(rq, bio))
506 continue;
507
508 el_ret = blk_try_merge(rq, bio);
509 if (el_ret == ELEVATOR_BACK_MERGE) {
510 if (bio_attempt_back_merge(q, rq, bio)) {
511 ctx->rq_merged++;
512 return true;
513 }
514 break;
515 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
516 if (bio_attempt_front_merge(q, rq, bio)) {
517 ctx->rq_merged++;
518 return true;
519 }
520 break;
521 }
522 }
523
524 return false;
525}
526
527void blk_mq_add_timer(struct request *rq)
528{
529 __blk_add_timer(rq, NULL);
530}
531
532/*
533 * Run this hardware queue, pulling any software queues mapped to it in.
534 * Note that this function currently has various problems around ordering
535 * of IO. In particular, we'd like FIFO behaviour on handling existing
536 * items on the hctx->dispatch list. Ignore that for now.
537 */
538static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
539{
540 struct request_queue *q = hctx->queue;
541 struct blk_mq_ctx *ctx;
542 struct request *rq;
543 LIST_HEAD(rq_list);
544 int bit, queued;
545
Jens Axboefd1270d2014-04-16 09:23:48 -0600546 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
Jens Axboee4043dc2014-04-09 10:18:23 -0600547
Jens Axboe5d12f902014-03-19 15:25:02 -0600548 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100549 return;
550
551 hctx->run++;
552
553 /*
554 * Touch any software queue that has pending entries.
555 */
556 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
557 clear_bit(bit, hctx->ctx_map);
558 ctx = hctx->ctxs[bit];
559 BUG_ON(bit != ctx->index_hw);
560
561 spin_lock(&ctx->lock);
562 list_splice_tail_init(&ctx->rq_list, &rq_list);
563 spin_unlock(&ctx->lock);
564 }
565
566 /*
567 * If we have previous entries on our dispatch list, grab them
568 * and stuff them at the front for more fair dispatch.
569 */
570 if (!list_empty_careful(&hctx->dispatch)) {
571 spin_lock(&hctx->lock);
572 if (!list_empty(&hctx->dispatch))
573 list_splice_init(&hctx->dispatch, &rq_list);
574 spin_unlock(&hctx->lock);
575 }
576
577 /*
578 * Delete and return all entries from our dispatch list
579 */
580 queued = 0;
581
582 /*
583 * Now process all the entries, sending them to the driver.
584 */
585 while (!list_empty(&rq_list)) {
586 int ret;
587
588 rq = list_first_entry(&rq_list, struct request, queuelist);
589 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100590
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800591 blk_mq_start_request(rq, list_empty(&rq_list));
Jens Axboe320ae512013-10-24 09:20:05 +0100592
593 ret = q->mq_ops->queue_rq(hctx, rq);
594 switch (ret) {
595 case BLK_MQ_RQ_QUEUE_OK:
596 queued++;
597 continue;
598 case BLK_MQ_RQ_QUEUE_BUSY:
599 /*
600 * FIXME: we should have a mechanism to stop the queue
601 * like blk_stop_queue, otherwise we will waste cpu
602 * time
603 */
604 list_add(&rq->queuelist, &rq_list);
605 blk_mq_requeue_request(rq);
606 break;
607 default:
608 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100609 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800610 rq->errors = -EIO;
Jens Axboe320ae512013-10-24 09:20:05 +0100611 blk_mq_end_io(rq, rq->errors);
612 break;
613 }
614
615 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
616 break;
617 }
618
619 if (!queued)
620 hctx->dispatched[0]++;
621 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
622 hctx->dispatched[ilog2(queued) + 1]++;
623
624 /*
625 * Any items that need requeuing? Stuff them into hctx->dispatch,
626 * that is where we will continue on next queue run.
627 */
628 if (!list_empty(&rq_list)) {
629 spin_lock(&hctx->lock);
630 list_splice(&rq_list, &hctx->dispatch);
631 spin_unlock(&hctx->lock);
632 }
633}
634
635void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
636{
Jens Axboe5d12f902014-03-19 15:25:02 -0600637 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
Jens Axboe320ae512013-10-24 09:20:05 +0100638 return;
639
Jens Axboee4043dc2014-04-09 10:18:23 -0600640 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
Jens Axboe320ae512013-10-24 09:20:05 +0100641 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600642 else if (hctx->queue->nr_hw_queues == 1)
Jens Axboe59c3d452014-04-08 09:15:35 -0600643 kblockd_schedule_delayed_work(&hctx->delayed_work, 0);
Jens Axboee4043dc2014-04-09 10:18:23 -0600644 else {
645 unsigned int cpu;
646
647 /*
648 * It'd be great if the workqueue API had a way to pass
649 * in a mask and had some smarts for more clever placement
650 * than the first CPU. Or we could round-robin here. For now,
651 * just queue on the first CPU.
652 */
653 cpu = cpumask_first(hctx->cpumask);
654 kblockd_schedule_delayed_work_on(cpu, &hctx->delayed_work, 0);
655 }
Jens Axboe320ae512013-10-24 09:20:05 +0100656}
657
658void blk_mq_run_queues(struct request_queue *q, bool async)
659{
660 struct blk_mq_hw_ctx *hctx;
661 int i;
662
663 queue_for_each_hw_ctx(q, hctx, i) {
664 if ((!blk_mq_hctx_has_pending(hctx) &&
665 list_empty_careful(&hctx->dispatch)) ||
Jens Axboe5d12f902014-03-19 15:25:02 -0600666 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jens Axboe320ae512013-10-24 09:20:05 +0100667 continue;
668
Jens Axboee4043dc2014-04-09 10:18:23 -0600669 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100670 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600671 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100672 }
673}
674EXPORT_SYMBOL(blk_mq_run_queues);
675
676void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
677{
678 cancel_delayed_work(&hctx->delayed_work);
679 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
680}
681EXPORT_SYMBOL(blk_mq_stop_hw_queue);
682
Christoph Hellwig280d45f2013-10-25 14:45:58 +0100683void blk_mq_stop_hw_queues(struct request_queue *q)
684{
685 struct blk_mq_hw_ctx *hctx;
686 int i;
687
688 queue_for_each_hw_ctx(q, hctx, i)
689 blk_mq_stop_hw_queue(hctx);
690}
691EXPORT_SYMBOL(blk_mq_stop_hw_queues);
692
Jens Axboe320ae512013-10-24 09:20:05 +0100693void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
694{
695 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600696
697 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100698 __blk_mq_run_hw_queue(hctx);
Jens Axboee4043dc2014-04-09 10:18:23 -0600699 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100700}
701EXPORT_SYMBOL(blk_mq_start_hw_queue);
702
703void blk_mq_start_stopped_hw_queues(struct request_queue *q)
704{
705 struct blk_mq_hw_ctx *hctx;
706 int i;
707
708 queue_for_each_hw_ctx(q, hctx, i) {
709 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
710 continue;
711
712 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -0600713 preempt_disable();
Jens Axboe320ae512013-10-24 09:20:05 +0100714 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -0600715 preempt_enable();
Jens Axboe320ae512013-10-24 09:20:05 +0100716 }
717}
718EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
719
720static void blk_mq_work_fn(struct work_struct *work)
721{
722 struct blk_mq_hw_ctx *hctx;
723
724 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
Jens Axboee4043dc2014-04-09 10:18:23 -0600725
Jens Axboe320ae512013-10-24 09:20:05 +0100726 __blk_mq_run_hw_queue(hctx);
727}
728
729static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800730 struct request *rq, bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +0100731{
732 struct blk_mq_ctx *ctx = rq->mq_ctx;
733
Jens Axboe01b983c2013-11-19 18:59:10 -0700734 trace_block_rq_insert(hctx->queue, rq);
735
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800736 if (at_head)
737 list_add(&rq->queuelist, &ctx->rq_list);
738 else
739 list_add_tail(&rq->queuelist, &ctx->rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100740 blk_mq_hctx_mark_pending(hctx, ctx);
741
742 /*
743 * We do this early, to ensure we are on the right CPU.
744 */
745 blk_mq_add_timer(rq);
746}
747
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600748void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
749 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100750{
751 struct request_queue *q = rq->q;
752 struct blk_mq_hw_ctx *hctx;
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600753 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100754
755 current_ctx = blk_mq_get_ctx(q);
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600756 if (!cpu_online(ctx->cpu))
757 rq->mq_ctx = ctx = current_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100758
Jens Axboe320ae512013-10-24 09:20:05 +0100759 hctx = q->mq_ops->map_queue(q, ctx->cpu);
760
Christoph Hellwigeeabc852014-03-21 08:57:37 -0600761 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
762 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
763 blk_insert_flush(rq);
764 } else {
765 spin_lock(&ctx->lock);
766 __blk_mq_insert_request(hctx, rq, at_head);
767 spin_unlock(&ctx->lock);
768 }
Jens Axboe320ae512013-10-24 09:20:05 +0100769
Jens Axboe320ae512013-10-24 09:20:05 +0100770 if (run_queue)
771 blk_mq_run_hw_queue(hctx, async);
Jens Axboee4043dc2014-04-09 10:18:23 -0600772
773 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100774}
775
776static void blk_mq_insert_requests(struct request_queue *q,
777 struct blk_mq_ctx *ctx,
778 struct list_head *list,
779 int depth,
780 bool from_schedule)
781
782{
783 struct blk_mq_hw_ctx *hctx;
784 struct blk_mq_ctx *current_ctx;
785
786 trace_block_unplug(q, depth, !from_schedule);
787
788 current_ctx = blk_mq_get_ctx(q);
789
790 if (!cpu_online(ctx->cpu))
791 ctx = current_ctx;
792 hctx = q->mq_ops->map_queue(q, ctx->cpu);
793
794 /*
795 * preemption doesn't flush plug list, so it's possible ctx->cpu is
796 * offline now
797 */
798 spin_lock(&ctx->lock);
799 while (!list_empty(list)) {
800 struct request *rq;
801
802 rq = list_first_entry(list, struct request, queuelist);
803 list_del_init(&rq->queuelist);
804 rq->mq_ctx = ctx;
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800805 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100806 }
807 spin_unlock(&ctx->lock);
808
Jens Axboe320ae512013-10-24 09:20:05 +0100809 blk_mq_run_hw_queue(hctx, from_schedule);
Jens Axboee4043dc2014-04-09 10:18:23 -0600810 blk_mq_put_ctx(current_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100811}
812
813static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
814{
815 struct request *rqa = container_of(a, struct request, queuelist);
816 struct request *rqb = container_of(b, struct request, queuelist);
817
818 return !(rqa->mq_ctx < rqb->mq_ctx ||
819 (rqa->mq_ctx == rqb->mq_ctx &&
820 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
821}
822
823void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
824{
825 struct blk_mq_ctx *this_ctx;
826 struct request_queue *this_q;
827 struct request *rq;
828 LIST_HEAD(list);
829 LIST_HEAD(ctx_list);
830 unsigned int depth;
831
832 list_splice_init(&plug->mq_list, &list);
833
834 list_sort(NULL, &list, plug_ctx_cmp);
835
836 this_q = NULL;
837 this_ctx = NULL;
838 depth = 0;
839
840 while (!list_empty(&list)) {
841 rq = list_entry_rq(list.next);
842 list_del_init(&rq->queuelist);
843 BUG_ON(!rq->q);
844 if (rq->mq_ctx != this_ctx) {
845 if (this_ctx) {
846 blk_mq_insert_requests(this_q, this_ctx,
847 &ctx_list, depth,
848 from_schedule);
849 }
850
851 this_ctx = rq->mq_ctx;
852 this_q = rq->q;
853 depth = 0;
854 }
855
856 depth++;
857 list_add_tail(&rq->queuelist, &ctx_list);
858 }
859
860 /*
861 * If 'this_ctx' is set, we know we have entries to complete
862 * on 'ctx_list'. Do those.
863 */
864 if (this_ctx) {
865 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
866 from_schedule);
867 }
868}
869
870static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
871{
872 init_request_from_bio(rq, bio);
873 blk_account_io_start(rq, 1);
874}
875
876static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
877{
878 struct blk_mq_hw_ctx *hctx;
879 struct blk_mq_ctx *ctx;
880 const int is_sync = rw_is_sync(bio->bi_rw);
881 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
882 int rw = bio_data_dir(bio);
883 struct request *rq;
884 unsigned int use_plug, request_count = 0;
885
886 /*
887 * If we have multiple hardware queues, just go directly to
888 * one of those for sync IO.
889 */
890 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
891
892 blk_queue_bounce(q, &bio);
893
Nicholas Bellinger14ec77f2014-02-07 13:45:39 -0700894 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
895 bio_endio(bio, -EIO);
896 return;
897 }
898
Jens Axboe320ae512013-10-24 09:20:05 +0100899 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
900 return;
901
902 if (blk_mq_queue_enter(q)) {
903 bio_endio(bio, -EIO);
904 return;
905 }
906
907 ctx = blk_mq_get_ctx(q);
908 hctx = q->mq_ops->map_queue(q, ctx->cpu);
909
Shaohua Li27fbf4e82014-02-19 20:20:21 +0800910 if (is_sync)
911 rw |= REQ_SYNC;
Jens Axboe320ae512013-10-24 09:20:05 +0100912 trace_block_getrq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700913 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100914 if (likely(rq))
Christoph Hellwig18741982014-02-10 09:29:00 -0700915 blk_mq_rq_ctx_init(q, ctx, rq, rw);
Jens Axboe320ae512013-10-24 09:20:05 +0100916 else {
917 blk_mq_put_ctx(ctx);
918 trace_block_sleeprq(q, bio, rw);
Christoph Hellwig18741982014-02-10 09:29:00 -0700919 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
920 false);
Jens Axboe320ae512013-10-24 09:20:05 +0100921 ctx = rq->mq_ctx;
922 hctx = q->mq_ops->map_queue(q, ctx->cpu);
923 }
924
925 hctx->queued++;
926
927 if (unlikely(is_flush_fua)) {
928 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +0100929 blk_insert_flush(rq);
930 goto run_queue;
931 }
932
933 /*
934 * A task plug currently exists. Since this is completely lockless,
935 * utilize that to temporarily store requests until the task is
936 * either done or scheduled away.
937 */
938 if (use_plug) {
939 struct blk_plug *plug = current->plug;
940
941 if (plug) {
942 blk_mq_bio_to_request(rq, bio);
Shaohua Li92f399c2013-10-29 12:01:03 -0600943 if (list_empty(&plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +0100944 trace_block_plug(q);
945 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
946 blk_flush_plug_list(plug, false);
947 trace_block_plug(q);
948 }
949 list_add_tail(&rq->queuelist, &plug->mq_list);
950 blk_mq_put_ctx(ctx);
951 return;
952 }
953 }
954
955 spin_lock(&ctx->lock);
956
957 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
958 blk_mq_attempt_merge(q, ctx, bio))
959 __blk_mq_free_request(hctx, ctx, rq);
960 else {
961 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig72a0a362014-02-07 10:22:36 -0800962 __blk_mq_insert_request(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +0100963 }
964
965 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100966
967 /*
968 * For a SYNC request, send it to the hardware immediately. For an
969 * ASYNC request, just ensure that we run it later on. The latter
970 * allows for merging opportunities and more efficient dispatching.
971 */
972run_queue:
973 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
Jens Axboee4043dc2014-04-09 10:18:23 -0600974 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100975}
976
977/*
978 * Default mapping to a software queue, since we use one per CPU.
979 */
980struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
981{
982 return q->queue_hw_ctx[q->mq_map[cpu]];
983}
984EXPORT_SYMBOL(blk_mq_map_queue);
985
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600986struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
Jens Axboe320ae512013-10-24 09:20:05 +0100987 unsigned int hctx_index)
988{
989 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600990 GFP_KERNEL | __GFP_ZERO, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +0100991}
992EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
993
994void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
995 unsigned int hctx_index)
996{
997 kfree(hctx);
998}
999EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1000
1001static void blk_mq_hctx_notify(void *data, unsigned long action,
1002 unsigned int cpu)
1003{
1004 struct blk_mq_hw_ctx *hctx = data;
Jens Axboebccb5f72014-04-04 21:34:48 -06001005 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +01001006 struct blk_mq_ctx *ctx;
1007 LIST_HEAD(tmp);
1008
1009 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1010 return;
1011
1012 /*
1013 * Move ctx entries to new CPU, if this one is going away.
1014 */
Jens Axboebccb5f72014-04-04 21:34:48 -06001015 ctx = __blk_mq_get_ctx(q, cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001016
1017 spin_lock(&ctx->lock);
1018 if (!list_empty(&ctx->rq_list)) {
1019 list_splice_init(&ctx->rq_list, &tmp);
1020 clear_bit(ctx->index_hw, hctx->ctx_map);
1021 }
1022 spin_unlock(&ctx->lock);
1023
1024 if (list_empty(&tmp))
1025 return;
1026
Jens Axboebccb5f72014-04-04 21:34:48 -06001027 ctx = blk_mq_get_ctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001028 spin_lock(&ctx->lock);
1029
1030 while (!list_empty(&tmp)) {
1031 struct request *rq;
1032
1033 rq = list_first_entry(&tmp, struct request, queuelist);
1034 rq->mq_ctx = ctx;
1035 list_move_tail(&rq->queuelist, &ctx->rq_list);
1036 }
1037
Jens Axboebccb5f72014-04-04 21:34:48 -06001038 hctx = q->mq_ops->map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001039 blk_mq_hctx_mark_pending(hctx, ctx);
1040
1041 spin_unlock(&ctx->lock);
Jens Axboebccb5f72014-04-04 21:34:48 -06001042
1043 blk_mq_run_hw_queue(hctx, true);
Jens Axboee4043dc2014-04-09 10:18:23 -06001044 blk_mq_put_ctx(ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001045}
1046
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001047static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1048 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001049{
1050 struct page *page;
1051
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001052 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001053 int i;
1054
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001055 for (i = 0; i < tags->nr_tags; i++) {
1056 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001057 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001058 set->ops->exit_request(set->driver_data, tags->rqs[i],
1059 hctx_idx, i);
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001060 }
1061 }
1062
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001063 while (!list_empty(&tags->page_list)) {
1064 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001065 list_del_init(&page->lru);
Jens Axboe320ae512013-10-24 09:20:05 +01001066 __free_pages(page, page->private);
1067 }
1068
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001069 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001070
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001071 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001072}
1073
1074static size_t order_to_size(unsigned int order)
1075{
1076 size_t ret = PAGE_SIZE;
1077
1078 while (order--)
1079 ret *= 2;
1080
1081 return ret;
1082}
1083
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001084static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1085 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001086{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001087 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001088 unsigned int i, j, entries_per_page, max_order = 4;
1089 size_t rq_size, left;
1090
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001091 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1092 set->numa_node);
1093 if (!tags)
1094 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001095
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001096 INIT_LIST_HEAD(&tags->page_list);
1097
1098 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1099 GFP_KERNEL, set->numa_node);
1100 if (!tags->rqs) {
1101 blk_mq_free_tags(tags);
1102 return NULL;
1103 }
Jens Axboe320ae512013-10-24 09:20:05 +01001104
1105 /*
1106 * rq_size is the size of the request plus driver payload, rounded
1107 * to the cacheline size
1108 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001109 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001110 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001111 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001112
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001113 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001114 int this_order = max_order;
1115 struct page *page;
1116 int to_do;
1117 void *p;
1118
1119 while (left < order_to_size(this_order - 1) && this_order)
1120 this_order--;
1121
1122 do {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001123 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1124 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001125 if (page)
1126 break;
1127 if (!this_order--)
1128 break;
1129 if (order_to_size(this_order) < rq_size)
1130 break;
1131 } while (1);
1132
1133 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001134 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001135
1136 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001137 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001138
1139 p = page_address(page);
1140 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001141 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001142 left -= to_do * rq_size;
1143 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001144 tags->rqs[i] = p;
1145 if (set->ops->init_request) {
1146 if (set->ops->init_request(set->driver_data,
1147 tags->rqs[i], hctx_idx, i,
1148 set->numa_node))
1149 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001150 }
1151
Jens Axboe320ae512013-10-24 09:20:05 +01001152 p += rq_size;
1153 i++;
1154 }
1155 }
1156
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001157 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001158
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001159fail:
1160 pr_warn("%s: failed to allocate requests\n", __func__);
1161 blk_mq_free_rq_map(set, tags, hctx_idx);
1162 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001163}
1164
1165static int blk_mq_init_hw_queues(struct request_queue *q,
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001166 struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001167{
1168 struct blk_mq_hw_ctx *hctx;
1169 unsigned int i, j;
1170
1171 /*
1172 * Initialize hardware queues
1173 */
1174 queue_for_each_hw_ctx(q, hctx, i) {
1175 unsigned int num_maps;
1176 int node;
1177
1178 node = hctx->numa_node;
1179 if (node == NUMA_NO_NODE)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001180 node = hctx->numa_node = set->numa_node;
Jens Axboe320ae512013-10-24 09:20:05 +01001181
1182 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1183 spin_lock_init(&hctx->lock);
1184 INIT_LIST_HEAD(&hctx->dispatch);
1185 hctx->queue = q;
1186 hctx->queue_num = i;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001187 hctx->flags = set->flags;
1188 hctx->cmd_size = set->cmd_size;
Jens Axboe320ae512013-10-24 09:20:05 +01001189
1190 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1191 blk_mq_hctx_notify, hctx);
1192 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1193
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001194 hctx->tags = set->tags[i];
Jens Axboe320ae512013-10-24 09:20:05 +01001195
1196 /*
1197 * Allocate space for all possible cpus to avoid allocation in
1198 * runtime
1199 */
1200 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1201 GFP_KERNEL, node);
1202 if (!hctx->ctxs)
1203 break;
1204
1205 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1206 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1207 GFP_KERNEL, node);
1208 if (!hctx->ctx_map)
1209 break;
1210
1211 hctx->nr_ctx_map = num_maps;
1212 hctx->nr_ctx = 0;
1213
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001214 if (set->ops->init_hctx &&
1215 set->ops->init_hctx(hctx, set->driver_data, i))
Jens Axboe320ae512013-10-24 09:20:05 +01001216 break;
1217 }
1218
1219 if (i == q->nr_hw_queues)
1220 return 0;
1221
1222 /*
1223 * Init failed
1224 */
1225 queue_for_each_hw_ctx(q, hctx, j) {
1226 if (i == j)
1227 break;
1228
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001229 if (set->ops->exit_hctx)
1230 set->ops->exit_hctx(hctx, j);
Jens Axboe320ae512013-10-24 09:20:05 +01001231
1232 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
Jens Axboe320ae512013-10-24 09:20:05 +01001233 kfree(hctx->ctxs);
1234 }
1235
1236 return 1;
1237}
1238
1239static void blk_mq_init_cpu_queues(struct request_queue *q,
1240 unsigned int nr_hw_queues)
1241{
1242 unsigned int i;
1243
1244 for_each_possible_cpu(i) {
1245 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1246 struct blk_mq_hw_ctx *hctx;
1247
1248 memset(__ctx, 0, sizeof(*__ctx));
1249 __ctx->cpu = i;
1250 spin_lock_init(&__ctx->lock);
1251 INIT_LIST_HEAD(&__ctx->rq_list);
1252 __ctx->queue = q;
1253
1254 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001255 if (!cpu_online(i))
1256 continue;
1257
Jens Axboee4043dc2014-04-09 10:18:23 -06001258 hctx = q->mq_ops->map_queue(q, i);
1259 cpumask_set_cpu(i, hctx->cpumask);
1260 hctx->nr_ctx++;
1261
Jens Axboe320ae512013-10-24 09:20:05 +01001262 /*
1263 * Set local node, IFF we have more than one hw queue. If
1264 * not, we remain on the home node of the device
1265 */
1266 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1267 hctx->numa_node = cpu_to_node(i);
1268 }
1269}
1270
1271static void blk_mq_map_swqueue(struct request_queue *q)
1272{
1273 unsigned int i;
1274 struct blk_mq_hw_ctx *hctx;
1275 struct blk_mq_ctx *ctx;
1276
1277 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001278 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001279 hctx->nr_ctx = 0;
1280 }
1281
1282 /*
1283 * Map software to hardware queues
1284 */
1285 queue_for_each_ctx(q, ctx, i) {
1286 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboee4043dc2014-04-09 10:18:23 -06001287 if (!cpu_online(i))
1288 continue;
1289
Jens Axboe320ae512013-10-24 09:20:05 +01001290 hctx = q->mq_ops->map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001291 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001292 ctx->index_hw = hctx->nr_ctx;
1293 hctx->ctxs[hctx->nr_ctx++] = ctx;
1294 }
1295}
1296
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001297struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001298{
1299 struct blk_mq_hw_ctx **hctxs;
1300 struct blk_mq_ctx *ctx;
1301 struct request_queue *q;
1302 int i;
1303
Jens Axboe320ae512013-10-24 09:20:05 +01001304 ctx = alloc_percpu(struct blk_mq_ctx);
1305 if (!ctx)
1306 return ERR_PTR(-ENOMEM);
1307
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001308 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1309 set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001310
1311 if (!hctxs)
1312 goto err_percpu;
1313
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001314 for (i = 0; i < set->nr_hw_queues; i++) {
1315 hctxs[i] = set->ops->alloc_hctx(set, i);
Jens Axboe320ae512013-10-24 09:20:05 +01001316 if (!hctxs[i])
1317 goto err_hctxs;
1318
Jens Axboee4043dc2014-04-09 10:18:23 -06001319 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1320 goto err_hctxs;
1321
Jens Axboe320ae512013-10-24 09:20:05 +01001322 hctxs[i]->numa_node = NUMA_NO_NODE;
1323 hctxs[i]->queue_num = i;
1324 }
1325
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001326 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Jens Axboe320ae512013-10-24 09:20:05 +01001327 if (!q)
1328 goto err_hctxs;
1329
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001330 q->mq_map = blk_mq_make_queue_map(set);
Jens Axboe320ae512013-10-24 09:20:05 +01001331 if (!q->mq_map)
1332 goto err_map;
1333
1334 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1335 blk_queue_rq_timeout(q, 30000);
1336
1337 q->nr_queues = nr_cpu_ids;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001338 q->nr_hw_queues = set->nr_hw_queues;
Jens Axboe320ae512013-10-24 09:20:05 +01001339
1340 q->queue_ctx = ctx;
1341 q->queue_hw_ctx = hctxs;
1342
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001343 q->mq_ops = set->ops;
Jens Axboe94eddfb2013-11-19 09:25:07 -07001344 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01001345
Christoph Hellwig1be036e2014-02-07 10:22:39 -08001346 q->sg_reserved_size = INT_MAX;
1347
Jens Axboe320ae512013-10-24 09:20:05 +01001348 blk_queue_make_request(q, blk_mq_make_request);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001349 blk_queue_rq_timed_out(q, set->ops->timeout);
1350 if (set->timeout)
1351 blk_queue_rq_timeout(q, set->timeout);
Jens Axboe320ae512013-10-24 09:20:05 +01001352
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001353 if (set->ops->complete)
1354 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001355
Jens Axboe320ae512013-10-24 09:20:05 +01001356 blk_mq_init_flush(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001357 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001358
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001359 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1360 set->cmd_size, cache_line_size()),
1361 GFP_KERNEL);
Christoph Hellwig18741982014-02-10 09:29:00 -07001362 if (!q->flush_rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001363 goto err_hw;
1364
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001365 if (blk_mq_init_hw_queues(q, set))
Christoph Hellwig18741982014-02-10 09:29:00 -07001366 goto err_flush_rq;
1367
Jens Axboe320ae512013-10-24 09:20:05 +01001368 blk_mq_map_swqueue(q);
1369
1370 mutex_lock(&all_q_mutex);
1371 list_add_tail(&q->all_q_node, &all_q_list);
1372 mutex_unlock(&all_q_mutex);
1373
1374 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07001375
1376err_flush_rq:
1377 kfree(q->flush_rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001378err_hw:
1379 kfree(q->mq_map);
1380err_map:
1381 blk_cleanup_queue(q);
1382err_hctxs:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001383 for (i = 0; i < set->nr_hw_queues; i++) {
Jens Axboe320ae512013-10-24 09:20:05 +01001384 if (!hctxs[i])
1385 break;
Jens Axboee4043dc2014-04-09 10:18:23 -06001386 free_cpumask_var(hctxs[i]->cpumask);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001387 set->ops->free_hctx(hctxs[i], i);
Jens Axboe320ae512013-10-24 09:20:05 +01001388 }
1389 kfree(hctxs);
1390err_percpu:
1391 free_percpu(ctx);
1392 return ERR_PTR(-ENOMEM);
1393}
1394EXPORT_SYMBOL(blk_mq_init_queue);
1395
1396void blk_mq_free_queue(struct request_queue *q)
1397{
1398 struct blk_mq_hw_ctx *hctx;
1399 int i;
1400
1401 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001402 kfree(hctx->ctx_map);
1403 kfree(hctx->ctxs);
Jens Axboe320ae512013-10-24 09:20:05 +01001404 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1405 if (q->mq_ops->exit_hctx)
1406 q->mq_ops->exit_hctx(hctx, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001407 free_cpumask_var(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001408 q->mq_ops->free_hctx(hctx, i);
1409 }
1410
1411 free_percpu(q->queue_ctx);
1412 kfree(q->queue_hw_ctx);
1413 kfree(q->mq_map);
1414
1415 q->queue_ctx = NULL;
1416 q->queue_hw_ctx = NULL;
1417 q->mq_map = NULL;
1418
1419 mutex_lock(&all_q_mutex);
1420 list_del_init(&q->all_q_node);
1421 mutex_unlock(&all_q_mutex);
1422}
Jens Axboe320ae512013-10-24 09:20:05 +01001423
1424/* Basically redo blk_mq_init_queue with queue frozen */
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001425static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01001426{
1427 blk_mq_freeze_queue(q);
1428
1429 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1430
1431 /*
1432 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1433 * we should change hctx numa_node according to new topology (this
1434 * involves free and re-allocate memory, worthy doing?)
1435 */
1436
1437 blk_mq_map_swqueue(q);
1438
1439 blk_mq_unfreeze_queue(q);
1440}
1441
Paul Gortmakerf618ef72013-11-14 08:26:02 -07001442static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1443 unsigned long action, void *hcpu)
Jens Axboe320ae512013-10-24 09:20:05 +01001444{
1445 struct request_queue *q;
1446
1447 /*
1448 * Before new mapping is established, hotadded cpu might already start
1449 * handling requests. This doesn't break anything as we map offline
1450 * CPUs to first hardware queue. We will re-init queue below to get
1451 * optimal settings.
1452 */
1453 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1454 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1455 return NOTIFY_OK;
1456
1457 mutex_lock(&all_q_mutex);
1458 list_for_each_entry(q, &all_q_list, all_q_node)
1459 blk_mq_queue_reinit(q);
1460 mutex_unlock(&all_q_mutex);
1461 return NOTIFY_OK;
1462}
1463
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001464int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1465{
1466 int i;
1467
1468 if (!set->nr_hw_queues)
1469 return -EINVAL;
1470 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1471 return -EINVAL;
1472 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1473 return -EINVAL;
1474
1475 if (!set->nr_hw_queues ||
1476 !set->ops->queue_rq || !set->ops->map_queue ||
1477 !set->ops->alloc_hctx || !set->ops->free_hctx)
1478 return -EINVAL;
1479
1480
1481 set->tags = kmalloc_node(set->nr_hw_queues * sizeof(struct blk_mq_tags),
1482 GFP_KERNEL, set->numa_node);
1483 if (!set->tags)
1484 goto out;
1485
1486 for (i = 0; i < set->nr_hw_queues; i++) {
1487 set->tags[i] = blk_mq_init_rq_map(set, i);
1488 if (!set->tags[i])
1489 goto out_unwind;
1490 }
1491
1492 return 0;
1493
1494out_unwind:
1495 while (--i >= 0)
1496 blk_mq_free_rq_map(set, set->tags[i], i);
1497out:
1498 return -ENOMEM;
1499}
1500EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1501
1502void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1503{
1504 int i;
1505
1506 for (i = 0; i < set->nr_hw_queues; i++)
1507 blk_mq_free_rq_map(set, set->tags[i], i);
1508}
1509EXPORT_SYMBOL(blk_mq_free_tag_set);
1510
Jens Axboe676141e2014-03-20 13:29:18 -06001511void blk_mq_disable_hotplug(void)
1512{
1513 mutex_lock(&all_q_mutex);
1514}
1515
1516void blk_mq_enable_hotplug(void)
1517{
1518 mutex_unlock(&all_q_mutex);
1519}
1520
Jens Axboe320ae512013-10-24 09:20:05 +01001521static int __init blk_mq_init(void)
1522{
Jens Axboe320ae512013-10-24 09:20:05 +01001523 blk_mq_cpu_init();
1524
1525 /* Must be called after percpu_counter_hotcpu_callback() */
1526 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1527
1528 return 0;
1529}
1530subsys_initcall(blk_mq_init);