blob: 3dc32354329389b3487517a86298ec30a984fe2c [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>
Catalin Marinasf75782e2015-09-14 18:16:02 +010012#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010013#include <linux/mm.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/workqueue.h>
17#include <linux/smp.h>
18#include <linux/llist.h>
19#include <linux/list_sort.h>
20#include <linux/cpu.h>
21#include <linux/cache.h>
22#include <linux/sched/sysctl.h>
23#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060024#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060025#include <linux/prefetch.h>
Jens Axboe320ae512013-10-24 09:20:05 +010026
27#include <trace/events/block.h>
28
29#include <linux/blk-mq.h>
30#include "blk.h"
31#include "blk-mq.h"
32#include "blk-mq-tag.h"
33
34static DEFINE_MUTEX(all_q_mutex);
35static LIST_HEAD(all_q_list);
36
Jens Axboe320ae512013-10-24 09:20:05 +010037/*
38 * Check if any of the ctx's have pending work in this hardware queue
39 */
40static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
41{
Omar Sandoval88459642016-09-17 08:38:44 -060042 return sbitmap_any_bit_set(&hctx->ctx_map);
Jens Axboe320ae512013-10-24 09:20:05 +010043}
44
45/*
46 * Mark this ctx as having pending work in this hardware queue
47 */
48static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
49 struct blk_mq_ctx *ctx)
50{
Omar Sandoval88459642016-09-17 08:38:44 -060051 if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
52 sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
Jens Axboe1429d7c2014-05-19 09:23:55 -060053}
54
55static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
56 struct blk_mq_ctx *ctx)
57{
Omar Sandoval88459642016-09-17 08:38:44 -060058 sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
Jens Axboe320ae512013-10-24 09:20:05 +010059}
60
Keith Buschb4c6a022014-12-19 17:54:14 -070061void blk_mq_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +080062{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020063 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -040064
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020065 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
66 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -040067 percpu_ref_kill(&q->q_usage_counter);
Mike Snitzerb94ec292015-03-11 23:56:38 -040068 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -040069 }
Tejun Heof3af0202014-11-04 13:52:27 -050070}
Keith Buschb4c6a022014-12-19 17:54:14 -070071EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -050072
73static void blk_mq_freeze_queue_wait(struct request_queue *q)
74{
Dan Williams3ef28e82015-10-21 13:20:12 -040075 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +080076}
77
Tejun Heof3af0202014-11-04 13:52:27 -050078/*
79 * Guarantee no request is in use, so we can change any data structure of
80 * the queue afterward.
81 */
Dan Williams3ef28e82015-10-21 13:20:12 -040082void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -050083{
Dan Williams3ef28e82015-10-21 13:20:12 -040084 /*
85 * In the !blk_mq case we are only calling this to kill the
86 * q_usage_counter, otherwise this increases the freeze depth
87 * and waits for it to return to zero. For this reason there is
88 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
89 * exported to drivers as the only user for unfreeze is blk_mq.
90 */
Tejun Heof3af0202014-11-04 13:52:27 -050091 blk_mq_freeze_queue_start(q);
92 blk_mq_freeze_queue_wait(q);
93}
Dan Williams3ef28e82015-10-21 13:20:12 -040094
95void blk_mq_freeze_queue(struct request_queue *q)
96{
97 /*
98 * ...just an alias to keep freeze and unfreeze actions balanced
99 * in the blk_mq_* namespace
100 */
101 blk_freeze_queue(q);
102}
Jens Axboec761d962015-01-02 15:05:12 -0700103EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500104
Keith Buschb4c6a022014-12-19 17:54:14 -0700105void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100106{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200107 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100108
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200109 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
110 WARN_ON_ONCE(freeze_depth < 0);
111 if (!freeze_depth) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400112 percpu_ref_reinit(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100113 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600114 }
Jens Axboe320ae512013-10-24 09:20:05 +0100115}
Keith Buschb4c6a022014-12-19 17:54:14 -0700116EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100117
Bart Van Assche6a83e742016-11-02 10:09:51 -0600118/**
119 * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished
120 * @q: request queue.
121 *
122 * Note: this function does not prevent that the struct request end_io()
123 * callback function is invoked. Additionally, it is not prevented that
124 * new queue_rq() calls occur unless the queue has been stopped first.
125 */
126void blk_mq_quiesce_queue(struct request_queue *q)
127{
128 struct blk_mq_hw_ctx *hctx;
129 unsigned int i;
130 bool rcu = false;
131
132 blk_mq_stop_hw_queues(q);
133
134 queue_for_each_hw_ctx(q, hctx, i) {
135 if (hctx->flags & BLK_MQ_F_BLOCKING)
136 synchronize_srcu(&hctx->queue_rq_srcu);
137 else
138 rcu = true;
139 }
140 if (rcu)
141 synchronize_rcu();
142}
143EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
144
Jens Axboeaed3ea92014-12-22 14:04:42 -0700145void blk_mq_wake_waiters(struct request_queue *q)
146{
147 struct blk_mq_hw_ctx *hctx;
148 unsigned int i;
149
150 queue_for_each_hw_ctx(q, hctx, i)
151 if (blk_mq_hw_queue_mapped(hctx))
152 blk_mq_tag_wakeup_all(hctx->tags, true);
Keith Busch3fd59402015-01-08 08:53:56 -0700153
154 /*
155 * If we are called because the queue has now been marked as
156 * dying, we need to ensure that processes currently waiting on
157 * the queue are notified as well.
158 */
159 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700160}
161
Jens Axboe320ae512013-10-24 09:20:05 +0100162bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
163{
164 return blk_mq_has_free_tags(hctx->tags);
165}
166EXPORT_SYMBOL(blk_mq_can_queue);
167
Jens Axboe94eddfb2013-11-19 09:25:07 -0700168static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600169 struct request *rq, unsigned int op)
Jens Axboe320ae512013-10-24 09:20:05 +0100170{
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200171 INIT_LIST_HEAD(&rq->queuelist);
172 /* csd/requeue_work/fifo_time is initialized before use */
173 rq->q = q;
Jens Axboe320ae512013-10-24 09:20:05 +0100174 rq->mq_ctx = ctx;
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600175 rq->cmd_flags = op;
Christoph Hellwige8064022016-10-20 15:12:13 +0200176 if (blk_queue_io_stat(q))
177 rq->rq_flags |= RQF_IO_STAT;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200178 /* do not touch atomic flags, it needs atomic ops against the timer */
179 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200180 INIT_HLIST_NODE(&rq->hash);
181 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200182 rq->rq_disk = NULL;
183 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600184 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200185#ifdef CONFIG_BLK_CGROUP
186 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700187 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200188 rq->io_start_time_ns = 0;
189#endif
190 rq->nr_phys_segments = 0;
191#if defined(CONFIG_BLK_DEV_INTEGRITY)
192 rq->nr_integrity_segments = 0;
193#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200194 rq->special = NULL;
195 /* tag was already set */
196 rq->errors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200197
Tony Battersby6f4a16262014-08-22 15:53:39 -0400198 rq->cmd = rq->__cmd;
199
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200200 rq->extra_len = 0;
201 rq->sense_len = 0;
202 rq->resid_len = 0;
203 rq->sense = NULL;
204
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200205 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600206 rq->timeout = 0;
207
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200208 rq->end_io = NULL;
209 rq->end_io_data = NULL;
210 rq->next_rq = NULL;
211
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600212 ctx->rq_dispatched[op_is_sync(op)]++;
Jens Axboe320ae512013-10-24 09:20:05 +0100213}
214
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200215static struct request *
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600216__blk_mq_alloc_request(struct blk_mq_alloc_data *data, unsigned int op)
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200217{
218 struct request *rq;
219 unsigned int tag;
220
Ming Leicb96a422014-06-01 00:43:37 +0800221 tag = blk_mq_get_tag(data);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200222 if (tag != BLK_MQ_TAG_FAIL) {
Ming Leicb96a422014-06-01 00:43:37 +0800223 rq = data->hctx->tags->rqs[tag];
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200224
Ming Leicb96a422014-06-01 00:43:37 +0800225 if (blk_mq_tag_busy(data->hctx)) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200226 rq->rq_flags = RQF_MQ_INFLIGHT;
Ming Leicb96a422014-06-01 00:43:37 +0800227 atomic_inc(&data->hctx->nr_active);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200228 }
229
230 rq->tag = tag;
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600231 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op);
Christoph Hellwig5dee8572014-05-27 20:59:47 +0200232 return rq;
233 }
234
235 return NULL;
236}
237
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100238struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
239 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100240{
Christoph Hellwigd8525642014-05-27 20:59:50 +0200241 struct blk_mq_ctx *ctx;
242 struct blk_mq_hw_ctx *hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100243 struct request *rq;
Ming Leicb96a422014-06-01 00:43:37 +0800244 struct blk_mq_alloc_data alloc_data;
Joe Lawrencea492f072014-08-28 08:15:21 -0600245 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100246
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100247 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
Joe Lawrencea492f072014-08-28 08:15:21 -0600248 if (ret)
249 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100250
Christoph Hellwigd8525642014-05-27 20:59:50 +0200251 ctx = blk_mq_get_ctx(q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200252 hctx = blk_mq_map_queue(q, ctx->cpu);
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100253 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600254 rq = __blk_mq_alloc_request(&alloc_data, rw);
Christoph Hellwigd8525642014-05-27 20:59:50 +0200255 blk_mq_put_ctx(ctx);
Jens Axboe841bac22016-09-21 10:08:43 -0600256
Keith Buschc76541a2014-12-19 17:54:13 -0700257 if (!rq) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400258 blk_queue_exit(q);
Joe Lawrencea492f072014-08-28 08:15:21 -0600259 return ERR_PTR(-EWOULDBLOCK);
Keith Buschc76541a2014-12-19 17:54:13 -0700260 }
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200261
262 rq->__data_len = 0;
263 rq->__sector = (sector_t) -1;
264 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100265 return rq;
266}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600267EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100268
Ming Lin1f5bd332016-06-13 16:45:21 +0200269struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
270 unsigned int flags, unsigned int hctx_idx)
271{
272 struct blk_mq_hw_ctx *hctx;
273 struct blk_mq_ctx *ctx;
274 struct request *rq;
275 struct blk_mq_alloc_data alloc_data;
276 int ret;
277
278 /*
279 * If the tag allocator sleeps we could get an allocation for a
280 * different hardware context. No need to complicate the low level
281 * allocator for this for the rare use case of a command tied to
282 * a specific queue.
283 */
284 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
285 return ERR_PTR(-EINVAL);
286
287 if (hctx_idx >= q->nr_hw_queues)
288 return ERR_PTR(-EIO);
289
290 ret = blk_queue_enter(q, true);
291 if (ret)
292 return ERR_PTR(ret);
293
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600294 /*
295 * Check if the hardware context is actually mapped to anything.
296 * If not tell the caller that it should skip this queue.
297 */
Ming Lin1f5bd332016-06-13 16:45:21 +0200298 hctx = q->queue_hw_ctx[hctx_idx];
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600299 if (!blk_mq_hw_queue_mapped(hctx)) {
300 ret = -EXDEV;
301 goto out_queue_exit;
302 }
Ming Lin1f5bd332016-06-13 16:45:21 +0200303 ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask));
304
305 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600306 rq = __blk_mq_alloc_request(&alloc_data, rw);
Ming Lin1f5bd332016-06-13 16:45:21 +0200307 if (!rq) {
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600308 ret = -EWOULDBLOCK;
309 goto out_queue_exit;
Ming Lin1f5bd332016-06-13 16:45:21 +0200310 }
311
312 return rq;
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600313
314out_queue_exit:
315 blk_queue_exit(q);
316 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200317}
318EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
319
Jens Axboe320ae512013-10-24 09:20:05 +0100320static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
321 struct blk_mq_ctx *ctx, struct request *rq)
322{
323 const int tag = rq->tag;
324 struct request_queue *q = rq->q;
325
Christoph Hellwige8064022016-10-20 15:12:13 +0200326 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600327 atomic_dec(&hctx->nr_active);
Christoph Hellwige8064022016-10-20 15:12:13 +0200328 rq->rq_flags = 0;
Jens Axboe0d2602c2014-05-13 15:10:52 -0600329
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200330 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Omar Sandoval40aabb62016-09-17 01:28:23 -0700331 blk_mq_put_tag(hctx, ctx, tag);
Dan Williams3ef28e82015-10-21 13:20:12 -0400332 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100333}
334
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700335void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100336{
337 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe7c7f2f22014-11-17 10:41:57 -0700338
339 ctx->rq_completed[rq_is_sync(rq)]++;
340 __blk_mq_free_request(hctx, ctx, rq);
341
342}
343EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
344
345void blk_mq_free_request(struct request *rq)
346{
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +0200347 blk_mq_free_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100348}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700349EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100350
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700351inline void __blk_mq_end_request(struct request *rq, int error)
Jens Axboe320ae512013-10-24 09:20:05 +0100352{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700353 blk_account_io_done(rq);
354
Christoph Hellwig91b63632014-04-16 09:44:53 +0200355 if (rq->end_io) {
Jens Axboe320ae512013-10-24 09:20:05 +0100356 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200357 } else {
358 if (unlikely(blk_bidi_rq(rq)))
359 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100360 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200361 }
Jens Axboe320ae512013-10-24 09:20:05 +0100362}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700363EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200364
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700365void blk_mq_end_request(struct request *rq, int error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200366{
367 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
368 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700369 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200370}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700371EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100372
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800373static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100374{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800375 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100376
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800377 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100378}
379
Jens Axboeed851862014-05-30 21:20:50 -0600380static void blk_mq_ipi_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100381{
382 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700383 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100384 int cpu;
385
Christoph Hellwig38535202014-04-25 02:32:53 -0700386 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800387 rq->q->softirq_done_fn(rq);
388 return;
389 }
Jens Axboe320ae512013-10-24 09:20:05 +0100390
391 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700392 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
393 shared = cpus_share_cache(cpu, ctx->cpu);
394
395 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800396 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800397 rq->csd.info = rq;
398 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100399 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800400 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800401 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800402 }
Jens Axboe320ae512013-10-24 09:20:05 +0100403 put_cpu();
404}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800405
Jens Axboe1fa8cc52015-11-05 14:32:55 -0700406static void __blk_mq_complete_request(struct request *rq)
Jens Axboeed851862014-05-30 21:20:50 -0600407{
408 struct request_queue *q = rq->q;
409
410 if (!q->softirq_done_fn)
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700411 blk_mq_end_request(rq, rq->errors);
Jens Axboeed851862014-05-30 21:20:50 -0600412 else
413 blk_mq_ipi_complete_request(rq);
414}
415
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800416/**
417 * blk_mq_complete_request - end I/O on a request
418 * @rq: the request being processed
419 *
420 * Description:
421 * Ends all I/O on a request. It does not handle partial completions.
422 * The actual completion happens out-of-order, through a IPI handler.
423 **/
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200424void blk_mq_complete_request(struct request *rq, int error)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800425{
Jens Axboe95f09682014-05-27 17:46:48 -0600426 struct request_queue *q = rq->q;
427
428 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800429 return;
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200430 if (!blk_mark_rq_complete(rq)) {
431 rq->errors = error;
Jens Axboeed851862014-05-30 21:20:50 -0600432 __blk_mq_complete_request(rq);
Christoph Hellwigf4829a92015-09-27 21:01:50 +0200433 }
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800434}
435EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100436
Keith Busch973c0192015-01-07 18:55:43 -0700437int blk_mq_request_started(struct request *rq)
438{
439 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
440}
441EXPORT_SYMBOL_GPL(blk_mq_request_started);
442
Christoph Hellwige2490072014-09-13 16:40:09 -0700443void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100444{
445 struct request_queue *q = rq->q;
446
447 trace_block_rq_issue(q, rq);
448
Christoph Hellwig742ee692014-04-14 10:30:06 +0200449 rq->resid_len = blk_rq_bytes(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200450 if (unlikely(blk_bidi_rq(rq)))
451 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
Christoph Hellwig742ee692014-04-14 10:30:06 +0200452
Ming Lei2b8393b2014-06-10 00:16:41 +0800453 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600454
455 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600456 * Ensure that ->deadline is visible before set the started
457 * flag and clear the completed flag.
458 */
459 smp_mb__before_atomic();
460
461 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600462 * Mark us as started and clear complete. Complete might have been
463 * set if requeue raced with timeout, which then marked it as
464 * complete. So be sure to clear complete again when we start
465 * the request, otherwise we'll ignore the completion event.
466 */
Jens Axboe4b570522014-05-29 11:00:11 -0600467 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
468 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
469 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
470 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800471
472 if (q->dma_drain_size && blk_rq_bytes(rq)) {
473 /*
474 * Make sure space for the drain appears. We know we can do
475 * this because max_hw_segments has been adjusted to be one
476 * fewer than the device can handle.
477 */
478 rq->nr_phys_segments++;
479 }
Jens Axboe320ae512013-10-24 09:20:05 +0100480}
Christoph Hellwige2490072014-09-13 16:40:09 -0700481EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100482
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200483static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100484{
485 struct request_queue *q = rq->q;
486
487 trace_block_rq_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800488
Christoph Hellwige2490072014-09-13 16:40:09 -0700489 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
490 if (q->dma_drain_size && blk_rq_bytes(rq))
491 rq->nr_phys_segments--;
492 }
Jens Axboe320ae512013-10-24 09:20:05 +0100493}
494
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200495void blk_mq_requeue_request(struct request *rq)
496{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200497 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200498
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200499 BUG_ON(blk_queued_rq(rq));
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600500 blk_mq_add_to_requeue_list(rq, true);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200501}
502EXPORT_SYMBOL(blk_mq_requeue_request);
503
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600504static void blk_mq_requeue_work(struct work_struct *work)
505{
506 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400507 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600508 LIST_HEAD(rq_list);
509 struct request *rq, *next;
510 unsigned long flags;
511
512 spin_lock_irqsave(&q->requeue_lock, flags);
513 list_splice_init(&q->requeue_list, &rq_list);
514 spin_unlock_irqrestore(&q->requeue_lock, flags);
515
516 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200517 if (!(rq->rq_flags & RQF_SOFTBARRIER))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600518 continue;
519
Christoph Hellwige8064022016-10-20 15:12:13 +0200520 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600521 list_del_init(&rq->queuelist);
522 blk_mq_insert_request(rq, true, false, false);
523 }
524
525 while (!list_empty(&rq_list)) {
526 rq = list_entry(rq_list.next, struct request, queuelist);
527 list_del_init(&rq->queuelist);
528 blk_mq_insert_request(rq, false, false, false);
529 }
530
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700531 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600532}
533
534void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
535{
536 struct request_queue *q = rq->q;
537 unsigned long flags;
538
539 /*
540 * We abuse this flag that is otherwise used by the I/O scheduler to
541 * request head insertation from the workqueue.
542 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200543 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600544
545 spin_lock_irqsave(&q->requeue_lock, flags);
546 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200547 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600548 list_add(&rq->queuelist, &q->requeue_list);
549 } else {
550 list_add_tail(&rq->queuelist, &q->requeue_list);
551 }
552 spin_unlock_irqrestore(&q->requeue_lock, flags);
553}
554EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
555
556void blk_mq_kick_requeue_list(struct request_queue *q)
557{
Mike Snitzer28494502016-09-14 13:28:30 -0400558 kblockd_schedule_delayed_work(&q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600559}
560EXPORT_SYMBOL(blk_mq_kick_requeue_list);
561
Mike Snitzer28494502016-09-14 13:28:30 -0400562void blk_mq_delay_kick_requeue_list(struct request_queue *q,
563 unsigned long msecs)
564{
565 kblockd_schedule_delayed_work(&q->requeue_work,
566 msecs_to_jiffies(msecs));
567}
568EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
569
Jens Axboe1885b242015-01-07 18:55:45 -0700570void blk_mq_abort_requeue_list(struct request_queue *q)
571{
572 unsigned long flags;
573 LIST_HEAD(rq_list);
574
575 spin_lock_irqsave(&q->requeue_lock, flags);
576 list_splice_init(&q->requeue_list, &rq_list);
577 spin_unlock_irqrestore(&q->requeue_lock, flags);
578
579 while (!list_empty(&rq_list)) {
580 struct request *rq;
581
582 rq = list_first_entry(&rq_list, struct request, queuelist);
583 list_del_init(&rq->queuelist);
584 rq->errors = -EIO;
585 blk_mq_end_request(rq, rq->errors);
586 }
587}
588EXPORT_SYMBOL(blk_mq_abort_requeue_list);
589
Jens Axboe0e62f512014-06-04 10:23:49 -0600590struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
591{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600592 if (tag < tags->nr_tags) {
593 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700594 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600595 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700596
597 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600598}
599EXPORT_SYMBOL(blk_mq_tag_to_rq);
600
Jens Axboe320ae512013-10-24 09:20:05 +0100601struct blk_mq_timeout_data {
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700602 unsigned long next;
603 unsigned int next_set;
Jens Axboe320ae512013-10-24 09:20:05 +0100604};
605
Christoph Hellwig90415832014-09-22 10:21:48 -0600606void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100607{
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700608 struct blk_mq_ops *ops = req->q->mq_ops;
609 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600610
611 /*
612 * We know that complete is set at this point. If STARTED isn't set
613 * anymore, then the request isn't active and the "timeout" should
614 * just be ignored. This can happen due to the bitflag ordering.
615 * Timeout first checks if STARTED is set, and if it is, assumes
616 * the request is active. But if we race with completion, then
617 * we both flags will get cleared. So check here again, and ignore
618 * a timeout event with a request that isn't active.
619 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700620 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
621 return;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600622
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700623 if (ops->timeout)
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700624 ret = ops->timeout(req, reserved);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600625
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700626 switch (ret) {
627 case BLK_EH_HANDLED:
628 __blk_mq_complete_request(req);
629 break;
630 case BLK_EH_RESET_TIMER:
631 blk_add_timer(req);
632 blk_clear_rq_complete(req);
633 break;
634 case BLK_EH_NOT_HANDLED:
635 break;
636 default:
637 printk(KERN_ERR "block: bad eh return: %d\n", ret);
638 break;
639 }
Jens Axboe87ee7b12014-04-24 08:51:47 -0600640}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700641
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700642static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
643 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100644{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700645 struct blk_mq_timeout_data *data = priv;
646
Keith Buscheb130db2015-01-08 08:59:53 -0700647 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
648 /*
649 * If a request wasn't started before the queue was
650 * marked dying, kill it here or it'll go unnoticed.
651 */
Keith Buscha59e0f52016-02-11 13:05:38 -0700652 if (unlikely(blk_queue_dying(rq->q))) {
653 rq->errors = -EIO;
654 blk_mq_end_request(rq, rq->errors);
655 }
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700656 return;
Keith Buscheb130db2015-01-08 08:59:53 -0700657 }
Jens Axboe320ae512013-10-24 09:20:05 +0100658
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700659 if (time_after_eq(jiffies, rq->deadline)) {
660 if (!blk_mark_rq_complete(rq))
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700661 blk_mq_rq_timed_out(rq, reserved);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700662 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
663 data->next = rq->deadline;
664 data->next_set = 1;
665 }
Jens Axboe320ae512013-10-24 09:20:05 +0100666}
667
Christoph Hellwig287922e2015-10-30 20:57:30 +0800668static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100669{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800670 struct request_queue *q =
671 container_of(work, struct request_queue, timeout_work);
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700672 struct blk_mq_timeout_data data = {
673 .next = 0,
674 .next_set = 0,
675 };
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700676 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100677
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600678 /* A deadlock might occur if a request is stuck requiring a
679 * timeout at the same time a queue freeze is waiting
680 * completion, since the timeout code would not be able to
681 * acquire the queue reference here.
682 *
683 * That's why we don't use blk_queue_enter here; instead, we use
684 * percpu_ref_tryget directly, because we need to be able to
685 * obtain a reference even in the short window between the queue
686 * starting to freeze, by dropping the first reference in
687 * blk_mq_freeze_queue_start, and the moment the last request is
688 * consumed, marked by the instant q_usage_counter reaches
689 * zero.
690 */
691 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800692 return;
693
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200694 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
Jens Axboe320ae512013-10-24 09:20:05 +0100695
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700696 if (data.next_set) {
697 data.next = blk_rq_timeout(round_jiffies_up(data.next));
698 mod_timer(&q->timeout, data.next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600699 } else {
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200700 struct blk_mq_hw_ctx *hctx;
701
Ming Leif054b562015-04-21 10:00:19 +0800702 queue_for_each_hw_ctx(q, hctx, i) {
703 /* the hctx may be unmapped, so check it here */
704 if (blk_mq_hw_queue_mapped(hctx))
705 blk_mq_tag_idle(hctx);
706 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600707 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800708 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100709}
710
711/*
712 * Reverse check our software queue for entries that we could potentially
713 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
714 * too much time checking for merges.
715 */
716static bool blk_mq_attempt_merge(struct request_queue *q,
717 struct blk_mq_ctx *ctx, struct bio *bio)
718{
719 struct request *rq;
720 int checked = 8;
721
722 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
723 int el_ret;
724
725 if (!checked--)
726 break;
727
728 if (!blk_rq_merge_ok(rq, bio))
729 continue;
730
731 el_ret = blk_try_merge(rq, bio);
732 if (el_ret == ELEVATOR_BACK_MERGE) {
733 if (bio_attempt_back_merge(q, rq, bio)) {
734 ctx->rq_merged++;
735 return true;
736 }
737 break;
738 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
739 if (bio_attempt_front_merge(q, rq, bio)) {
740 ctx->rq_merged++;
741 return true;
742 }
743 break;
744 }
745 }
746
747 return false;
748}
749
Omar Sandoval88459642016-09-17 08:38:44 -0600750struct flush_busy_ctx_data {
751 struct blk_mq_hw_ctx *hctx;
752 struct list_head *list;
753};
754
755static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
756{
757 struct flush_busy_ctx_data *flush_data = data;
758 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
759 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
760
761 sbitmap_clear_bit(sb, bitnr);
762 spin_lock(&ctx->lock);
763 list_splice_tail_init(&ctx->rq_list, flush_data->list);
764 spin_unlock(&ctx->lock);
765 return true;
766}
767
Jens Axboe320ae512013-10-24 09:20:05 +0100768/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600769 * Process software queues that have been marked busy, splicing them
770 * to the for-dispatch
771 */
772static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
773{
Omar Sandoval88459642016-09-17 08:38:44 -0600774 struct flush_busy_ctx_data data = {
775 .hctx = hctx,
776 .list = list,
777 };
Jens Axboe1429d7c2014-05-19 09:23:55 -0600778
Omar Sandoval88459642016-09-17 08:38:44 -0600779 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600780}
Jens Axboe1429d7c2014-05-19 09:23:55 -0600781
Jens Axboe703fd1c2016-09-16 13:59:14 -0600782static inline unsigned int queued_to_index(unsigned int queued)
783{
784 if (!queued)
785 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600786
Jens Axboe703fd1c2016-09-16 13:59:14 -0600787 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600788}
789
790/*
Jens Axboe320ae512013-10-24 09:20:05 +0100791 * Run this hardware queue, pulling any software queues mapped to it in.
792 * Note that this function currently has various problems around ordering
793 * of IO. In particular, we'd like FIFO behaviour on handling existing
794 * items on the hctx->dispatch list. Ignore that for now.
795 */
Bart Van Assche6a83e742016-11-02 10:09:51 -0600796static void blk_mq_process_rq_list(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +0100797{
798 struct request_queue *q = hctx->queue;
Jens Axboe320ae512013-10-24 09:20:05 +0100799 struct request *rq;
800 LIST_HEAD(rq_list);
Jens Axboe74c45052014-10-29 11:14:52 -0600801 LIST_HEAD(driver_list);
802 struct list_head *dptr;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600803 int queued;
Jens Axboe320ae512013-10-24 09:20:05 +0100804
Bart Van Assche5d1b25c2016-10-28 17:19:15 -0700805 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +0100806 return;
807
808 hctx->run++;
809
810 /*
811 * Touch any software queue that has pending entries.
812 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600813 flush_busy_ctxs(hctx, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +0100814
815 /*
816 * If we have previous entries on our dispatch list, grab them
817 * and stuff them at the front for more fair dispatch.
818 */
819 if (!list_empty_careful(&hctx->dispatch)) {
820 spin_lock(&hctx->lock);
821 if (!list_empty(&hctx->dispatch))
822 list_splice_init(&hctx->dispatch, &rq_list);
823 spin_unlock(&hctx->lock);
824 }
825
826 /*
Jens Axboe74c45052014-10-29 11:14:52 -0600827 * Start off with dptr being NULL, so we start the first request
828 * immediately, even if we have more pending.
829 */
830 dptr = NULL;
831
832 /*
Jens Axboe320ae512013-10-24 09:20:05 +0100833 * Now process all the entries, sending them to the driver.
834 */
Jens Axboe1429d7c2014-05-19 09:23:55 -0600835 queued = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100836 while (!list_empty(&rq_list)) {
Jens Axboe74c45052014-10-29 11:14:52 -0600837 struct blk_mq_queue_data bd;
Jens Axboe320ae512013-10-24 09:20:05 +0100838 int ret;
839
840 rq = list_first_entry(&rq_list, struct request, queuelist);
841 list_del_init(&rq->queuelist);
Jens Axboe320ae512013-10-24 09:20:05 +0100842
Jens Axboe74c45052014-10-29 11:14:52 -0600843 bd.rq = rq;
844 bd.list = dptr;
845 bd.last = list_empty(&rq_list);
846
847 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe320ae512013-10-24 09:20:05 +0100848 switch (ret) {
849 case BLK_MQ_RQ_QUEUE_OK:
850 queued++;
Omar Sandoval52b9c332016-06-08 18:22:20 -0700851 break;
Jens Axboe320ae512013-10-24 09:20:05 +0100852 case BLK_MQ_RQ_QUEUE_BUSY:
Jens Axboe320ae512013-10-24 09:20:05 +0100853 list_add(&rq->queuelist, &rq_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200854 __blk_mq_requeue_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100855 break;
856 default:
857 pr_err("blk-mq: bad return on queue: %d\n", ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100858 case BLK_MQ_RQ_QUEUE_ERROR:
Christoph Hellwig1e93b8c2014-02-11 08:27:13 -0800859 rq->errors = -EIO;
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700860 blk_mq_end_request(rq, rq->errors);
Jens Axboe320ae512013-10-24 09:20:05 +0100861 break;
862 }
863
864 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
865 break;
Jens Axboe74c45052014-10-29 11:14:52 -0600866
867 /*
868 * We've done the first request. If we have more than 1
869 * left in the list, set dptr to defer issue.
870 */
871 if (!dptr && rq_list.next != rq_list.prev)
872 dptr = &driver_list;
Jens Axboe320ae512013-10-24 09:20:05 +0100873 }
874
Jens Axboe703fd1c2016-09-16 13:59:14 -0600875 hctx->dispatched[queued_to_index(queued)]++;
Jens Axboe320ae512013-10-24 09:20:05 +0100876
877 /*
878 * Any items that need requeuing? Stuff them into hctx->dispatch,
879 * that is where we will continue on next queue run.
880 */
881 if (!list_empty(&rq_list)) {
882 spin_lock(&hctx->lock);
883 list_splice(&rq_list, &hctx->dispatch);
884 spin_unlock(&hctx->lock);
Shaohua Li9ba52e52015-05-04 14:32:48 -0600885 /*
886 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
887 * it's possible the queue is stopped and restarted again
888 * before this. Queue restart will dispatch requests. And since
889 * requests in rq_list aren't added into hctx->dispatch yet,
890 * the requests in rq_list might get lost.
891 *
892 * blk_mq_run_hw_queue() already checks the STOPPED bit
893 **/
894 blk_mq_run_hw_queue(hctx, true);
Jens Axboe320ae512013-10-24 09:20:05 +0100895 }
896}
897
Bart Van Assche6a83e742016-11-02 10:09:51 -0600898static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
899{
900 int srcu_idx;
901
902 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
903 cpu_online(hctx->next_cpu));
904
905 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
906 rcu_read_lock();
907 blk_mq_process_rq_list(hctx);
908 rcu_read_unlock();
909 } else {
910 srcu_idx = srcu_read_lock(&hctx->queue_rq_srcu);
911 blk_mq_process_rq_list(hctx);
912 srcu_read_unlock(&hctx->queue_rq_srcu, srcu_idx);
913 }
914}
915
Jens Axboe506e9312014-05-07 10:26:44 -0600916/*
917 * It'd be great if the workqueue API had a way to pass
918 * in a mask and had some smarts for more clever placement.
919 * For now we just round-robin here, switching for every
920 * BLK_MQ_CPU_WORK_BATCH queued items.
921 */
922static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
923{
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100924 if (hctx->queue->nr_hw_queues == 1)
925 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -0600926
927 if (--hctx->next_cpu_batch <= 0) {
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100928 int cpu = hctx->next_cpu, next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600929
930 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
931 if (next_cpu >= nr_cpu_ids)
932 next_cpu = cpumask_first(hctx->cpumask);
933
934 hctx->next_cpu = next_cpu;
935 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100936
937 return cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600938 }
939
Christoph Hellwigb657d7e2014-11-24 09:27:23 +0100940 return hctx->next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -0600941}
942
Jens Axboe320ae512013-10-24 09:20:05 +0100943void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
944{
Bart Van Assche5d1b25c2016-10-28 17:19:15 -0700945 if (unlikely(blk_mq_hctx_stopped(hctx) ||
946 !blk_mq_hw_queue_mapped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +0100947 return;
948
Jens Axboe1b792f22016-09-21 10:12:13 -0600949 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100950 int cpu = get_cpu();
951 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +0100952 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100953 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +0100954 return;
955 }
Jens Axboee4043dc2014-04-09 10:18:23 -0600956
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +0100957 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -0600958 }
Paolo Bonzini398205b2014-11-07 23:03:59 +0100959
Jens Axboe27489a32016-08-24 15:54:25 -0600960 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work);
Jens Axboe320ae512013-10-24 09:20:05 +0100961}
962
Mike Snitzerb94ec292015-03-11 23:56:38 -0400963void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +0100964{
965 struct blk_mq_hw_ctx *hctx;
966 int i;
967
968 queue_for_each_hw_ctx(q, hctx, i) {
969 if ((!blk_mq_hctx_has_pending(hctx) &&
970 list_empty_careful(&hctx->dispatch)) ||
Bart Van Assche5d1b25c2016-10-28 17:19:15 -0700971 blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +0100972 continue;
973
Mike Snitzerb94ec292015-03-11 23:56:38 -0400974 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +0100975 }
976}
Mike Snitzerb94ec292015-03-11 23:56:38 -0400977EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +0100978
Bart Van Asschefd001442016-10-28 17:19:37 -0700979/**
980 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
981 * @q: request queue.
982 *
983 * The caller is responsible for serializing this function against
984 * blk_mq_{start,stop}_hw_queue().
985 */
986bool blk_mq_queue_stopped(struct request_queue *q)
987{
988 struct blk_mq_hw_ctx *hctx;
989 int i;
990
991 queue_for_each_hw_ctx(q, hctx, i)
992 if (blk_mq_hctx_stopped(hctx))
993 return true;
994
995 return false;
996}
997EXPORT_SYMBOL(blk_mq_queue_stopped);
998
Jens Axboe320ae512013-10-24 09:20:05 +0100999void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1000{
Jens Axboe27489a32016-08-24 15:54:25 -06001001 cancel_work(&hctx->run_work);
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001002 cancel_delayed_work(&hctx->delay_work);
Jens Axboe320ae512013-10-24 09:20:05 +01001003 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
1004}
1005EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1006
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001007void blk_mq_stop_hw_queues(struct request_queue *q)
1008{
1009 struct blk_mq_hw_ctx *hctx;
1010 int i;
1011
1012 queue_for_each_hw_ctx(q, hctx, i)
1013 blk_mq_stop_hw_queue(hctx);
1014}
1015EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1016
Jens Axboe320ae512013-10-24 09:20:05 +01001017void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1018{
1019 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001020
Jens Axboe0ffbce82014-06-25 08:22:34 -06001021 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001022}
1023EXPORT_SYMBOL(blk_mq_start_hw_queue);
1024
Christoph Hellwig2f268552014-04-16 09:44:56 +02001025void blk_mq_start_hw_queues(struct request_queue *q)
1026{
1027 struct blk_mq_hw_ctx *hctx;
1028 int i;
1029
1030 queue_for_each_hw_ctx(q, hctx, i)
1031 blk_mq_start_hw_queue(hctx);
1032}
1033EXPORT_SYMBOL(blk_mq_start_hw_queues);
1034
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001035void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001036{
1037 struct blk_mq_hw_ctx *hctx;
1038 int i;
1039
1040 queue_for_each_hw_ctx(q, hctx, i) {
Bart Van Assche5d1b25c2016-10-28 17:19:15 -07001041 if (!blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001042 continue;
1043
1044 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001045 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001046 }
1047}
1048EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1049
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001050static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001051{
1052 struct blk_mq_hw_ctx *hctx;
1053
Jens Axboe27489a32016-08-24 15:54:25 -06001054 hctx = container_of(work, struct blk_mq_hw_ctx, run_work);
Jens Axboee4043dc2014-04-09 10:18:23 -06001055
Jens Axboe320ae512013-10-24 09:20:05 +01001056 __blk_mq_run_hw_queue(hctx);
1057}
1058
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001059static void blk_mq_delay_work_fn(struct work_struct *work)
1060{
1061 struct blk_mq_hw_ctx *hctx;
1062
1063 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1064
1065 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1066 __blk_mq_run_hw_queue(hctx);
1067}
1068
1069void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1070{
Ming Lei19c66e52014-12-03 19:38:04 +08001071 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1072 return;
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001073
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001074 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1075 &hctx->delay_work, msecs_to_jiffies(msecs));
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001076}
1077EXPORT_SYMBOL(blk_mq_delay_queue);
1078
Ming Leicfd0c552015-10-20 23:13:57 +08001079static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001080 struct request *rq,
1081 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001082{
Jens Axboee57690f2016-08-24 15:34:35 -06001083 struct blk_mq_ctx *ctx = rq->mq_ctx;
1084
Jens Axboe01b983c2013-11-19 18:59:10 -07001085 trace_block_rq_insert(hctx->queue, rq);
1086
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001087 if (at_head)
1088 list_add(&rq->queuelist, &ctx->rq_list);
1089 else
1090 list_add_tail(&rq->queuelist, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001091}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001092
Ming Leicfd0c552015-10-20 23:13:57 +08001093static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
1094 struct request *rq, bool at_head)
1095{
1096 struct blk_mq_ctx *ctx = rq->mq_ctx;
1097
Jens Axboee57690f2016-08-24 15:34:35 -06001098 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001099 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001100}
1101
Christoph Hellwigeeabc852014-03-21 08:57:37 -06001102void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
Jens Axboee57690f2016-08-24 15:34:35 -06001103 bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001104{
Jens Axboee57690f2016-08-24 15:34:35 -06001105 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001106 struct request_queue *q = rq->q;
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001107 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001108
Christoph Hellwiga57a1782014-09-16 14:44:07 -07001109 spin_lock(&ctx->lock);
1110 __blk_mq_insert_request(hctx, rq, at_head);
1111 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001112
Jens Axboe320ae512013-10-24 09:20:05 +01001113 if (run_queue)
1114 blk_mq_run_hw_queue(hctx, async);
1115}
1116
1117static void blk_mq_insert_requests(struct request_queue *q,
1118 struct blk_mq_ctx *ctx,
1119 struct list_head *list,
1120 int depth,
1121 bool from_schedule)
1122
1123{
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001124 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001125
1126 trace_block_unplug(q, depth, !from_schedule);
1127
Jens Axboe320ae512013-10-24 09:20:05 +01001128 /*
1129 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1130 * offline now
1131 */
1132 spin_lock(&ctx->lock);
1133 while (!list_empty(list)) {
1134 struct request *rq;
1135
1136 rq = list_first_entry(list, struct request, queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001137 BUG_ON(rq->mq_ctx != ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001138 list_del_init(&rq->queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001139 __blk_mq_insert_req_list(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001140 }
Ming Leicfd0c552015-10-20 23:13:57 +08001141 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001142 spin_unlock(&ctx->lock);
1143
Jens Axboe320ae512013-10-24 09:20:05 +01001144 blk_mq_run_hw_queue(hctx, from_schedule);
1145}
1146
1147static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1148{
1149 struct request *rqa = container_of(a, struct request, queuelist);
1150 struct request *rqb = container_of(b, struct request, queuelist);
1151
1152 return !(rqa->mq_ctx < rqb->mq_ctx ||
1153 (rqa->mq_ctx == rqb->mq_ctx &&
1154 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1155}
1156
1157void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1158{
1159 struct blk_mq_ctx *this_ctx;
1160 struct request_queue *this_q;
1161 struct request *rq;
1162 LIST_HEAD(list);
1163 LIST_HEAD(ctx_list);
1164 unsigned int depth;
1165
1166 list_splice_init(&plug->mq_list, &list);
1167
1168 list_sort(NULL, &list, plug_ctx_cmp);
1169
1170 this_q = NULL;
1171 this_ctx = NULL;
1172 depth = 0;
1173
1174 while (!list_empty(&list)) {
1175 rq = list_entry_rq(list.next);
1176 list_del_init(&rq->queuelist);
1177 BUG_ON(!rq->q);
1178 if (rq->mq_ctx != this_ctx) {
1179 if (this_ctx) {
1180 blk_mq_insert_requests(this_q, this_ctx,
1181 &ctx_list, depth,
1182 from_schedule);
1183 }
1184
1185 this_ctx = rq->mq_ctx;
1186 this_q = rq->q;
1187 depth = 0;
1188 }
1189
1190 depth++;
1191 list_add_tail(&rq->queuelist, &ctx_list);
1192 }
1193
1194 /*
1195 * If 'this_ctx' is set, we know we have entries to complete
1196 * on 'ctx_list'. Do those.
1197 */
1198 if (this_ctx) {
1199 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1200 from_schedule);
1201 }
1202}
1203
1204static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1205{
1206 init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001207
Michael Callahana21f2a32016-05-03 11:12:49 -04001208 blk_account_io_start(rq, 1);
Jens Axboe320ae512013-10-24 09:20:05 +01001209}
1210
Jens Axboe274a5842014-08-15 12:44:08 -06001211static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1212{
1213 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1214 !blk_queue_nomerges(hctx->queue);
1215}
1216
Jens Axboe07068d52014-05-22 10:40:51 -06001217static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1218 struct blk_mq_ctx *ctx,
1219 struct request *rq, struct bio *bio)
1220{
Ming Leie18378a2015-10-20 23:13:54 +08001221 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
Jens Axboe07068d52014-05-22 10:40:51 -06001222 blk_mq_bio_to_request(rq, bio);
1223 spin_lock(&ctx->lock);
1224insert_rq:
1225 __blk_mq_insert_request(hctx, rq, false);
1226 spin_unlock(&ctx->lock);
1227 return false;
1228 } else {
Jens Axboe274a5842014-08-15 12:44:08 -06001229 struct request_queue *q = hctx->queue;
1230
Jens Axboe07068d52014-05-22 10:40:51 -06001231 spin_lock(&ctx->lock);
1232 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1233 blk_mq_bio_to_request(rq, bio);
1234 goto insert_rq;
1235 }
1236
1237 spin_unlock(&ctx->lock);
1238 __blk_mq_free_request(hctx, ctx, rq);
1239 return true;
1240 }
1241}
1242
Jens Axboe07068d52014-05-22 10:40:51 -06001243static struct request *blk_mq_map_request(struct request_queue *q,
1244 struct bio *bio,
Jens Axboe2552e3f2016-10-27 19:03:55 -06001245 struct blk_mq_alloc_data *data)
Jens Axboe320ae512013-10-24 09:20:05 +01001246{
1247 struct blk_mq_hw_ctx *hctx;
1248 struct blk_mq_ctx *ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001249 struct request *rq;
Jens Axboe320ae512013-10-24 09:20:05 +01001250
Dan Williams3ef28e82015-10-21 13:20:12 -04001251 blk_queue_enter_live(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001252 ctx = blk_mq_get_ctx(q);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001253 hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +01001254
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001255 trace_block_getrq(q, bio, bio->bi_opf);
Jens Axboe2552e3f2016-10-27 19:03:55 -06001256 blk_mq_set_alloc_data(data, q, 0, ctx, hctx);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001257 rq = __blk_mq_alloc_request(data, bio->bi_opf);
Jens Axboe320ae512013-10-24 09:20:05 +01001258
Jens Axboe7dd2fb62016-10-27 09:49:19 -06001259 data->hctx->queued++;
Jens Axboe07068d52014-05-22 10:40:51 -06001260 return rq;
1261}
1262
Bart Van Assche2253efc2016-10-28 17:20:02 -07001263static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1264 struct request *rq, blk_qc_t *cookie)
Shaohua Lif984df12015-05-08 10:51:32 -07001265{
1266 int ret;
1267 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001268 struct blk_mq_queue_data bd = {
1269 .rq = rq,
1270 .list = NULL,
1271 .last = 1
1272 };
Jens Axboe7b371632015-11-05 10:41:40 -07001273 blk_qc_t new_cookie = blk_tag_to_qc_t(rq->tag, hctx->queue_num);
Shaohua Lif984df12015-05-08 10:51:32 -07001274
Bart Van Assche2253efc2016-10-28 17:20:02 -07001275 if (blk_mq_hctx_stopped(hctx))
1276 goto insert;
1277
Shaohua Lif984df12015-05-08 10:51:32 -07001278 /*
1279 * For OK queue, we are done. For error, kill it. Any other
1280 * error (busy), just add it to our list as we previously
1281 * would have done
1282 */
1283 ret = q->mq_ops->queue_rq(hctx, &bd);
Jens Axboe7b371632015-11-05 10:41:40 -07001284 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1285 *cookie = new_cookie;
Bart Van Assche2253efc2016-10-28 17:20:02 -07001286 return;
Shaohua Lif984df12015-05-08 10:51:32 -07001287 }
Jens Axboe7b371632015-11-05 10:41:40 -07001288
1289 __blk_mq_requeue_request(rq);
1290
1291 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1292 *cookie = BLK_QC_T_NONE;
1293 rq->errors = -EIO;
1294 blk_mq_end_request(rq, rq->errors);
Bart Van Assche2253efc2016-10-28 17:20:02 -07001295 return;
Jens Axboe7b371632015-11-05 10:41:40 -07001296 }
1297
Bart Van Assche2253efc2016-10-28 17:20:02 -07001298insert:
1299 blk_mq_insert_request(rq, false, true, true);
Shaohua Lif984df12015-05-08 10:51:32 -07001300}
1301
Jens Axboe07068d52014-05-22 10:40:51 -06001302/*
1303 * Multiple hardware queue variant. This will not use per-process plugs,
1304 * but will attempt to bypass the hctx queueing if we can go straight to
1305 * hardware for SYNC IO.
1306 */
Jens Axboedece1632015-11-05 10:41:16 -07001307static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001308{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001309 const int is_sync = op_is_sync(bio->bi_opf);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001310 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Jens Axboe2552e3f2016-10-27 19:03:55 -06001311 struct blk_mq_alloc_data data;
Jens Axboe07068d52014-05-22 10:40:51 -06001312 struct request *rq;
Bart Van Assche6a83e742016-11-02 10:09:51 -06001313 unsigned int request_count = 0, srcu_idx;
Shaohua Lif984df12015-05-08 10:51:32 -07001314 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001315 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001316 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001317
1318 blk_queue_bounce(q, &bio);
1319
1320 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001321 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001322 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001323 }
1324
Kent Overstreet54efd502015-04-23 22:37:18 -07001325 blk_queue_split(q, &bio, q->bio_split);
1326
Omar Sandoval87c279e2016-06-01 22:18:48 -07001327 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1328 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1329 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001330
Jens Axboe07068d52014-05-22 10:40:51 -06001331 rq = blk_mq_map_request(q, bio, &data);
1332 if (unlikely(!rq))
Jens Axboedece1632015-11-05 10:41:16 -07001333 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001334
Jens Axboe7b371632015-11-05 10:41:40 -07001335 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
Jens Axboe07068d52014-05-22 10:40:51 -06001336
1337 if (unlikely(is_flush_fua)) {
1338 blk_mq_bio_to_request(rq, bio);
1339 blk_insert_flush(rq);
1340 goto run_queue;
1341 }
1342
Shaohua Lif984df12015-05-08 10:51:32 -07001343 plug = current->plug;
Jens Axboee167dfb2014-10-29 11:18:26 -06001344 /*
1345 * If the driver supports defer issued based on 'last', then
1346 * queue it up like normal since we can potentially save some
1347 * CPU this way.
1348 */
Shaohua Lif984df12015-05-08 10:51:32 -07001349 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1350 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1351 struct request *old_rq = NULL;
Jens Axboe07068d52014-05-22 10:40:51 -06001352
1353 blk_mq_bio_to_request(rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001354
1355 /*
Bart Van Assche6a83e742016-11-02 10:09:51 -06001356 * We do limited plugging. If the bio can be merged, do that.
Shaohua Lif984df12015-05-08 10:51:32 -07001357 * Otherwise the existing request in the plug list will be
1358 * issued. So the plug list will have one request at most
Jens Axboe07068d52014-05-22 10:40:51 -06001359 */
Shaohua Lif984df12015-05-08 10:51:32 -07001360 if (plug) {
Shaohua Li5b3f3412015-05-08 10:51:33 -07001361 /*
1362 * The plug list might get flushed before this. If that
Jens Axboeb094f892015-11-20 20:29:45 -07001363 * happens, same_queue_rq is invalid and plug list is
1364 * empty
1365 */
Shaohua Li5b3f3412015-05-08 10:51:33 -07001366 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1367 old_rq = same_queue_rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001368 list_del_init(&old_rq->queuelist);
Jens Axboe07068d52014-05-22 10:40:51 -06001369 }
Shaohua Lif984df12015-05-08 10:51:32 -07001370 list_add_tail(&rq->queuelist, &plug->mq_list);
1371 } else /* is_sync */
1372 old_rq = rq;
1373 blk_mq_put_ctx(data.ctx);
1374 if (!old_rq)
Jens Axboe7b371632015-11-05 10:41:40 -07001375 goto done;
Bart Van Assche6a83e742016-11-02 10:09:51 -06001376
1377 if (!(data.hctx->flags & BLK_MQ_F_BLOCKING)) {
1378 rcu_read_lock();
1379 blk_mq_try_issue_directly(data.hctx, old_rq, &cookie);
1380 rcu_read_unlock();
1381 } else {
1382 srcu_idx = srcu_read_lock(&data.hctx->queue_rq_srcu);
1383 blk_mq_try_issue_directly(data.hctx, old_rq, &cookie);
1384 srcu_read_unlock(&data.hctx->queue_rq_srcu, srcu_idx);
1385 }
Jens Axboe7b371632015-11-05 10:41:40 -07001386 goto done;
Jens Axboe07068d52014-05-22 10:40:51 -06001387 }
1388
1389 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1390 /*
1391 * For a SYNC request, send it to the hardware immediately. For
1392 * an ASYNC request, just ensure that we run it later on. The
1393 * latter allows for merging opportunities and more efficient
1394 * dispatching.
1395 */
1396run_queue:
1397 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1398 }
Jens Axboe07068d52014-05-22 10:40:51 -06001399 blk_mq_put_ctx(data.ctx);
Jens Axboe7b371632015-11-05 10:41:40 -07001400done:
1401 return cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001402}
1403
1404/*
1405 * Single hardware queue variant. This will attempt to use any per-process
1406 * plug for merging and IO deferral.
1407 */
Jens Axboedece1632015-11-05 10:41:16 -07001408static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001409{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001410 const int is_sync = op_is_sync(bio->bi_opf);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001411 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
Jeff Moyere6c44382015-05-08 10:51:30 -07001412 struct blk_plug *plug;
1413 unsigned int request_count = 0;
Jens Axboe2552e3f2016-10-27 19:03:55 -06001414 struct blk_mq_alloc_data data;
Jens Axboe07068d52014-05-22 10:40:51 -06001415 struct request *rq;
Jens Axboe7b371632015-11-05 10:41:40 -07001416 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001417
Jens Axboe07068d52014-05-22 10:40:51 -06001418 blk_queue_bounce(q, &bio);
1419
1420 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001421 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001422 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001423 }
1424
Kent Overstreet54efd502015-04-23 22:37:18 -07001425 blk_queue_split(q, &bio, q->bio_split);
1426
Omar Sandoval87c279e2016-06-01 22:18:48 -07001427 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1428 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1429 return BLK_QC_T_NONE;
1430 } else
1431 request_count = blk_plug_queued_count(q);
Jens Axboe07068d52014-05-22 10:40:51 -06001432
1433 rq = blk_mq_map_request(q, bio, &data);
Jens Axboeff87bce2014-06-03 11:59:49 -06001434 if (unlikely(!rq))
Jens Axboedece1632015-11-05 10:41:16 -07001435 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01001436
Jens Axboe7b371632015-11-05 10:41:40 -07001437 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
Jens Axboe320ae512013-10-24 09:20:05 +01001438
1439 if (unlikely(is_flush_fua)) {
1440 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001441 blk_insert_flush(rq);
1442 goto run_queue;
1443 }
1444
1445 /*
1446 * A task plug currently exists. Since this is completely lockless,
1447 * utilize that to temporarily store requests until the task is
1448 * either done or scheduled away.
1449 */
Jeff Moyere6c44382015-05-08 10:51:30 -07001450 plug = current->plug;
1451 if (plug) {
1452 blk_mq_bio_to_request(rq, bio);
Ming Lei676d0602015-10-20 23:13:56 +08001453 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001454 trace_block_plug(q);
Jens Axboeb094f892015-11-20 20:29:45 -07001455
1456 blk_mq_put_ctx(data.ctx);
1457
1458 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001459 blk_flush_plug_list(plug, false);
1460 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001461 }
Jens Axboeb094f892015-11-20 20:29:45 -07001462
Jeff Moyere6c44382015-05-08 10:51:30 -07001463 list_add_tail(&rq->queuelist, &plug->mq_list);
Jens Axboe7b371632015-11-05 10:41:40 -07001464 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001465 }
1466
Jens Axboe07068d52014-05-22 10:40:51 -06001467 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1468 /*
1469 * For a SYNC request, send it to the hardware immediately. For
1470 * an ASYNC request, just ensure that we run it later on. The
1471 * latter allows for merging opportunities and more efficient
1472 * dispatching.
1473 */
1474run_queue:
1475 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
Jens Axboe320ae512013-10-24 09:20:05 +01001476 }
1477
Jens Axboe07068d52014-05-22 10:40:51 -06001478 blk_mq_put_ctx(data.ctx);
Jens Axboe7b371632015-11-05 10:41:40 -07001479 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001480}
1481
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001482static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1483 struct blk_mq_tags *tags, unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001484{
1485 struct page *page;
1486
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001487 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001488 int i;
1489
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001490 for (i = 0; i < tags->nr_tags; i++) {
1491 if (!tags->rqs[i])
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001492 continue;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001493 set->ops->exit_request(set->driver_data, tags->rqs[i],
1494 hctx_idx, i);
Jens Axboea5164402014-09-10 09:02:03 -06001495 tags->rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001496 }
1497 }
1498
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001499 while (!list_empty(&tags->page_list)) {
1500 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001501 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001502 /*
1503 * Remove kmemleak object previously allocated in
1504 * blk_mq_init_rq_map().
1505 */
1506 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01001507 __free_pages(page, page->private);
1508 }
1509
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001510 kfree(tags->rqs);
Jens Axboe320ae512013-10-24 09:20:05 +01001511
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001512 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001513}
1514
1515static size_t order_to_size(unsigned int order)
1516{
Ming Lei4ca08502014-04-19 18:00:18 +08001517 return (size_t)PAGE_SIZE << order;
Jens Axboe320ae512013-10-24 09:20:05 +01001518}
1519
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001520static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1521 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001522{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001523 struct blk_mq_tags *tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001524 unsigned int i, j, entries_per_page, max_order = 4;
1525 size_t rq_size, left;
1526
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001527 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
Shaohua Li24391c02015-01-23 14:18:00 -07001528 set->numa_node,
1529 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001530 if (!tags)
1531 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001532
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001533 INIT_LIST_HEAD(&tags->page_list);
1534
Jens Axboea5164402014-09-10 09:02:03 -06001535 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1536 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1537 set->numa_node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001538 if (!tags->rqs) {
1539 blk_mq_free_tags(tags);
1540 return NULL;
1541 }
Jens Axboe320ae512013-10-24 09:20:05 +01001542
1543 /*
1544 * rq_size is the size of the request plus driver payload, rounded
1545 * to the cacheline size
1546 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001547 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001548 cache_line_size());
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001549 left = rq_size * set->queue_depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001550
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001551 for (i = 0; i < set->queue_depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001552 int this_order = max_order;
1553 struct page *page;
1554 int to_do;
1555 void *p;
1556
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06001557 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01001558 this_order--;
1559
1560 do {
Jens Axboea5164402014-09-10 09:02:03 -06001561 page = alloc_pages_node(set->numa_node,
Linus Torvaldsac211172015-04-09 14:12:22 -07001562 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06001563 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001564 if (page)
1565 break;
1566 if (!this_order--)
1567 break;
1568 if (order_to_size(this_order) < rq_size)
1569 break;
1570 } while (1);
1571
1572 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001573 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001574
1575 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001576 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001577
1578 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001579 /*
1580 * Allow kmemleak to scan these pages as they contain pointers
1581 * to additional allocations like via ops->init_request().
1582 */
1583 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL);
Jens Axboe320ae512013-10-24 09:20:05 +01001584 entries_per_page = order_to_size(this_order) / rq_size;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001585 to_do = min(entries_per_page, set->queue_depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001586 left -= to_do * rq_size;
1587 for (j = 0; j < to_do; j++) {
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001588 tags->rqs[i] = p;
1589 if (set->ops->init_request) {
1590 if (set->ops->init_request(set->driver_data,
1591 tags->rqs[i], hctx_idx, i,
Jens Axboea5164402014-09-10 09:02:03 -06001592 set->numa_node)) {
1593 tags->rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001594 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001595 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001596 }
1597
Jens Axboe320ae512013-10-24 09:20:05 +01001598 p += rq_size;
1599 i++;
1600 }
1601 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001602 return tags;
Jens Axboe320ae512013-10-24 09:20:05 +01001603
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001604fail:
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001605 blk_mq_free_rq_map(set, tags, hctx_idx);
1606 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001607}
1608
Jens Axboee57690f2016-08-24 15:34:35 -06001609/*
1610 * 'cpu' is going away. splice any existing rq_list entries from this
1611 * software queue to the hw queue dispatch list, and ensure that it
1612 * gets run.
1613 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06001614static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06001615{
Thomas Gleixner9467f852016-09-22 08:05:17 -06001616 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06001617 struct blk_mq_ctx *ctx;
1618 LIST_HEAD(tmp);
1619
Thomas Gleixner9467f852016-09-22 08:05:17 -06001620 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Jens Axboee57690f2016-08-24 15:34:35 -06001621 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Jens Axboe484b4062014-05-21 14:01:15 -06001622
1623 spin_lock(&ctx->lock);
1624 if (!list_empty(&ctx->rq_list)) {
1625 list_splice_init(&ctx->rq_list, &tmp);
1626 blk_mq_hctx_clear_pending(hctx, ctx);
1627 }
1628 spin_unlock(&ctx->lock);
1629
1630 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06001631 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06001632
Jens Axboee57690f2016-08-24 15:34:35 -06001633 spin_lock(&hctx->lock);
1634 list_splice_tail_init(&tmp, &hctx->dispatch);
1635 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06001636
1637 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06001638 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06001639}
1640
Thomas Gleixner9467f852016-09-22 08:05:17 -06001641static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06001642{
Thomas Gleixner9467f852016-09-22 08:05:17 -06001643 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
1644 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06001645}
1646
Ming Leic3b4afc2015-06-04 22:25:04 +08001647/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08001648static void blk_mq_exit_hctx(struct request_queue *q,
1649 struct blk_mq_tag_set *set,
1650 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1651{
Ming Leif70ced02014-09-25 23:23:47 +08001652 unsigned flush_start_tag = set->queue_depth;
1653
Ming Lei08e98fc2014-09-25 23:23:38 +08001654 blk_mq_tag_idle(hctx);
1655
Ming Leif70ced02014-09-25 23:23:47 +08001656 if (set->ops->exit_request)
1657 set->ops->exit_request(set->driver_data,
1658 hctx->fq->flush_rq, hctx_idx,
1659 flush_start_tag + hctx_idx);
1660
Ming Lei08e98fc2014-09-25 23:23:38 +08001661 if (set->ops->exit_hctx)
1662 set->ops->exit_hctx(hctx, hctx_idx);
1663
Bart Van Assche6a83e742016-11-02 10:09:51 -06001664 if (hctx->flags & BLK_MQ_F_BLOCKING)
1665 cleanup_srcu_struct(&hctx->queue_rq_srcu);
1666
Thomas Gleixner9467f852016-09-22 08:05:17 -06001667 blk_mq_remove_cpuhp(hctx);
Ming Leif70ced02014-09-25 23:23:47 +08001668 blk_free_flush_queue(hctx->fq);
Omar Sandoval88459642016-09-17 08:38:44 -06001669 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08001670}
1671
Ming Lei624dbe42014-05-27 23:35:13 +08001672static void blk_mq_exit_hw_queues(struct request_queue *q,
1673 struct blk_mq_tag_set *set, int nr_queue)
1674{
1675 struct blk_mq_hw_ctx *hctx;
1676 unsigned int i;
1677
1678 queue_for_each_hw_ctx(q, hctx, i) {
1679 if (i == nr_queue)
1680 break;
Ming Lei08e98fc2014-09-25 23:23:38 +08001681 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08001682 }
Ming Lei624dbe42014-05-27 23:35:13 +08001683}
1684
1685static void blk_mq_free_hw_queues(struct request_queue *q,
1686 struct blk_mq_tag_set *set)
1687{
1688 struct blk_mq_hw_ctx *hctx;
1689 unsigned int i;
1690
Ming Leie09aae7e2015-01-29 20:17:27 +08001691 queue_for_each_hw_ctx(q, hctx, i)
Ming Lei624dbe42014-05-27 23:35:13 +08001692 free_cpumask_var(hctx->cpumask);
Ming Lei624dbe42014-05-27 23:35:13 +08001693}
1694
Ming Lei08e98fc2014-09-25 23:23:38 +08001695static int blk_mq_init_hctx(struct request_queue *q,
1696 struct blk_mq_tag_set *set,
1697 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1698{
1699 int node;
Ming Leif70ced02014-09-25 23:23:47 +08001700 unsigned flush_start_tag = set->queue_depth;
Ming Lei08e98fc2014-09-25 23:23:38 +08001701
1702 node = hctx->numa_node;
1703 if (node == NUMA_NO_NODE)
1704 node = hctx->numa_node = set->numa_node;
1705
Jens Axboe27489a32016-08-24 15:54:25 -06001706 INIT_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08001707 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1708 spin_lock_init(&hctx->lock);
1709 INIT_LIST_HEAD(&hctx->dispatch);
1710 hctx->queue = q;
1711 hctx->queue_num = hctx_idx;
Jeff Moyer2404e602015-11-03 10:40:06 -05001712 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08001713
Thomas Gleixner9467f852016-09-22 08:05:17 -06001714 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
Ming Lei08e98fc2014-09-25 23:23:38 +08001715
1716 hctx->tags = set->tags[hctx_idx];
1717
1718 /*
1719 * Allocate space for all possible cpus to avoid allocation at
1720 * runtime
1721 */
1722 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1723 GFP_KERNEL, node);
1724 if (!hctx->ctxs)
1725 goto unregister_cpu_notifier;
1726
Omar Sandoval88459642016-09-17 08:38:44 -06001727 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL,
1728 node))
Ming Lei08e98fc2014-09-25 23:23:38 +08001729 goto free_ctxs;
1730
1731 hctx->nr_ctx = 0;
1732
1733 if (set->ops->init_hctx &&
1734 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1735 goto free_bitmap;
1736
Ming Leif70ced02014-09-25 23:23:47 +08001737 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1738 if (!hctx->fq)
1739 goto exit_hctx;
1740
1741 if (set->ops->init_request &&
1742 set->ops->init_request(set->driver_data,
1743 hctx->fq->flush_rq, hctx_idx,
1744 flush_start_tag + hctx_idx, node))
1745 goto free_fq;
1746
Bart Van Assche6a83e742016-11-02 10:09:51 -06001747 if (hctx->flags & BLK_MQ_F_BLOCKING)
1748 init_srcu_struct(&hctx->queue_rq_srcu);
1749
Ming Lei08e98fc2014-09-25 23:23:38 +08001750 return 0;
1751
Ming Leif70ced02014-09-25 23:23:47 +08001752 free_fq:
1753 kfree(hctx->fq);
1754 exit_hctx:
1755 if (set->ops->exit_hctx)
1756 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001757 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06001758 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08001759 free_ctxs:
1760 kfree(hctx->ctxs);
1761 unregister_cpu_notifier:
Thomas Gleixner9467f852016-09-22 08:05:17 -06001762 blk_mq_remove_cpuhp(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001763 return -1;
1764}
1765
Jens Axboe320ae512013-10-24 09:20:05 +01001766static void blk_mq_init_cpu_queues(struct request_queue *q,
1767 unsigned int nr_hw_queues)
1768{
1769 unsigned int i;
1770
1771 for_each_possible_cpu(i) {
1772 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1773 struct blk_mq_hw_ctx *hctx;
1774
1775 memset(__ctx, 0, sizeof(*__ctx));
1776 __ctx->cpu = i;
1777 spin_lock_init(&__ctx->lock);
1778 INIT_LIST_HEAD(&__ctx->rq_list);
1779 __ctx->queue = q;
1780
1781 /* If the cpu isn't online, the cpu is mapped to first hctx */
Jens Axboe320ae512013-10-24 09:20:05 +01001782 if (!cpu_online(i))
1783 continue;
1784
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001785 hctx = blk_mq_map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001786
Jens Axboe320ae512013-10-24 09:20:05 +01001787 /*
1788 * Set local node, IFF we have more than one hw queue. If
1789 * not, we remain on the home node of the device
1790 */
1791 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Raghavendra K Tbffed452015-12-02 16:59:05 +05301792 hctx->numa_node = local_memory_node(cpu_to_node(i));
Jens Axboe320ae512013-10-24 09:20:05 +01001793 }
1794}
1795
Akinobu Mita57783222015-09-27 02:09:23 +09001796static void blk_mq_map_swqueue(struct request_queue *q,
1797 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01001798{
1799 unsigned int i;
1800 struct blk_mq_hw_ctx *hctx;
1801 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08001802 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01001803
Akinobu Mita60de0742015-09-27 02:09:25 +09001804 /*
1805 * Avoid others reading imcomplete hctx->cpumask through sysfs
1806 */
1807 mutex_lock(&q->sysfs_lock);
1808
Jens Axboe320ae512013-10-24 09:20:05 +01001809 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06001810 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001811 hctx->nr_ctx = 0;
1812 }
1813
1814 /*
1815 * Map software to hardware queues
1816 */
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01001817 for_each_possible_cpu(i) {
Jens Axboe320ae512013-10-24 09:20:05 +01001818 /* If the cpu isn't online, the cpu is mapped to first hctx */
Akinobu Mita57783222015-09-27 02:09:23 +09001819 if (!cpumask_test_cpu(i, online_mask))
Jens Axboee4043dc2014-04-09 10:18:23 -06001820 continue;
1821
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01001822 ctx = per_cpu_ptr(q->queue_ctx, i);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001823 hctx = blk_mq_map_queue(q, i);
Keith Busch868f2f02015-12-17 17:08:14 -07001824
Jens Axboee4043dc2014-04-09 10:18:23 -06001825 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01001826 ctx->index_hw = hctx->nr_ctx;
1827 hctx->ctxs[hctx->nr_ctx++] = ctx;
1828 }
Jens Axboe506e9312014-05-07 10:26:44 -06001829
Akinobu Mita60de0742015-09-27 02:09:25 +09001830 mutex_unlock(&q->sysfs_lock);
1831
Jens Axboe506e9312014-05-07 10:26:44 -06001832 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06001833 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06001834 * If no software queues are mapped to this hardware queue,
1835 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06001836 */
1837 if (!hctx->nr_ctx) {
Jens Axboe484b4062014-05-21 14:01:15 -06001838 if (set->tags[i]) {
1839 blk_mq_free_rq_map(set, set->tags[i], i);
1840 set->tags[i] = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001841 }
Ming Lei2a34c082015-04-21 10:00:20 +08001842 hctx->tags = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06001843 continue;
1844 }
1845
Ming Lei2a34c082015-04-21 10:00:20 +08001846 /* unmapped hw queue can be remapped after CPU topo changed */
1847 if (!set->tags[i])
1848 set->tags[i] = blk_mq_init_rq_map(set, i);
1849 hctx->tags = set->tags[i];
1850 WARN_ON(!hctx->tags);
1851
Jens Axboe484b4062014-05-21 14:01:15 -06001852 /*
Chong Yuan889fa312015-04-15 11:39:29 -06001853 * Set the map size to the number of mapped software queues.
1854 * This is more accurate and more efficient than looping
1855 * over all possibly mapped software queues.
1856 */
Omar Sandoval88459642016-09-17 08:38:44 -06001857 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06001858
1859 /*
Jens Axboe484b4062014-05-21 14:01:15 -06001860 * Initialize batch roundrobin counts
1861 */
Jens Axboe506e9312014-05-07 10:26:44 -06001862 hctx->next_cpu = cpumask_first(hctx->cpumask);
1863 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1864 }
Jens Axboe320ae512013-10-24 09:20:05 +01001865}
1866
Jeff Moyer2404e602015-11-03 10:40:06 -05001867static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06001868{
1869 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06001870 int i;
1871
Jeff Moyer2404e602015-11-03 10:40:06 -05001872 queue_for_each_hw_ctx(q, hctx, i) {
1873 if (shared)
1874 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1875 else
1876 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1877 }
1878}
1879
1880static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
1881{
1882 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06001883
1884 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1885 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05001886 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001887 blk_mq_unfreeze_queue(q);
1888 }
1889}
1890
1891static void blk_mq_del_queue_tag_set(struct request_queue *q)
1892{
1893 struct blk_mq_tag_set *set = q->tag_set;
1894
Jens Axboe0d2602c2014-05-13 15:10:52 -06001895 mutex_lock(&set->tag_list_lock);
1896 list_del_init(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05001897 if (list_is_singular(&set->tag_list)) {
1898 /* just transitioned to unshared */
1899 set->flags &= ~BLK_MQ_F_TAG_SHARED;
1900 /* update existing queue */
1901 blk_mq_update_tag_set_depth(set, false);
1902 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001903 mutex_unlock(&set->tag_list_lock);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001904}
1905
1906static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1907 struct request_queue *q)
1908{
1909 q->tag_set = set;
1910
1911 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05001912
1913 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1914 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
1915 set->flags |= BLK_MQ_F_TAG_SHARED;
1916 /* update existing queue */
1917 blk_mq_update_tag_set_depth(set, true);
1918 }
1919 if (set->flags & BLK_MQ_F_TAG_SHARED)
1920 queue_set_hctx_shared(q, true);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001921 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05001922
Jens Axboe0d2602c2014-05-13 15:10:52 -06001923 mutex_unlock(&set->tag_list_lock);
1924}
1925
Ming Leie09aae7e2015-01-29 20:17:27 +08001926/*
1927 * It is the actual release handler for mq, but we do it from
1928 * request queue's release handler for avoiding use-after-free
1929 * and headache because q->mq_kobj shouldn't have been introduced,
1930 * but we can't group ctx/kctx kobj without it.
1931 */
1932void blk_mq_release(struct request_queue *q)
1933{
1934 struct blk_mq_hw_ctx *hctx;
1935 unsigned int i;
1936
1937 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08001938 queue_for_each_hw_ctx(q, hctx, i) {
1939 if (!hctx)
1940 continue;
1941 kfree(hctx->ctxs);
Ming Leie09aae7e2015-01-29 20:17:27 +08001942 kfree(hctx);
Ming Leic3b4afc2015-06-04 22:25:04 +08001943 }
Ming Leie09aae7e2015-01-29 20:17:27 +08001944
Akinobu Mitaa723bab2015-09-27 02:09:21 +09001945 q->mq_map = NULL;
1946
Ming Leie09aae7e2015-01-29 20:17:27 +08001947 kfree(q->queue_hw_ctx);
1948
1949 /* ctx kobj stays in queue_ctx */
1950 free_percpu(q->queue_ctx);
1951}
1952
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001953struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01001954{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04001955 struct request_queue *uninit_q, *q;
1956
1957 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1958 if (!uninit_q)
1959 return ERR_PTR(-ENOMEM);
1960
1961 q = blk_mq_init_allocated_queue(set, uninit_q);
1962 if (IS_ERR(q))
1963 blk_cleanup_queue(uninit_q);
1964
1965 return q;
1966}
1967EXPORT_SYMBOL(blk_mq_init_queue);
1968
Keith Busch868f2f02015-12-17 17:08:14 -07001969static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
1970 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04001971{
Keith Busch868f2f02015-12-17 17:08:14 -07001972 int i, j;
1973 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001974
Keith Busch868f2f02015-12-17 17:08:14 -07001975 blk_mq_sysfs_unregister(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001976 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07001977 int node;
Jens Axboef14bbe72014-05-27 12:06:53 -06001978
Keith Busch868f2f02015-12-17 17:08:14 -07001979 if (hctxs[i])
1980 continue;
1981
1982 node = blk_mq_hw_queue_to_node(q->mq_map, i);
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02001983 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1984 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01001985 if (!hctxs[i])
Keith Busch868f2f02015-12-17 17:08:14 -07001986 break;
Jens Axboe320ae512013-10-24 09:20:05 +01001987
Jens Axboea86073e2014-10-13 15:41:54 -06001988 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
Keith Busch868f2f02015-12-17 17:08:14 -07001989 node)) {
1990 kfree(hctxs[i]);
1991 hctxs[i] = NULL;
1992 break;
1993 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001994
Jens Axboe0d2602c2014-05-13 15:10:52 -06001995 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06001996 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01001997 hctxs[i]->queue_num = i;
Keith Busch868f2f02015-12-17 17:08:14 -07001998
1999 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2000 free_cpumask_var(hctxs[i]->cpumask);
2001 kfree(hctxs[i]);
2002 hctxs[i] = NULL;
2003 break;
2004 }
2005 blk_mq_hctx_kobj_init(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01002006 }
Keith Busch868f2f02015-12-17 17:08:14 -07002007 for (j = i; j < q->nr_hw_queues; j++) {
2008 struct blk_mq_hw_ctx *hctx = hctxs[j];
2009
2010 if (hctx) {
2011 if (hctx->tags) {
2012 blk_mq_free_rq_map(set, hctx->tags, j);
2013 set->tags[j] = NULL;
2014 }
2015 blk_mq_exit_hctx(q, set, hctx, j);
2016 free_cpumask_var(hctx->cpumask);
2017 kobject_put(&hctx->kobj);
2018 kfree(hctx->ctxs);
2019 kfree(hctx);
2020 hctxs[j] = NULL;
2021
2022 }
2023 }
2024 q->nr_hw_queues = i;
2025 blk_mq_sysfs_register(q);
2026}
2027
2028struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2029 struct request_queue *q)
2030{
Ming Lei66841672016-02-12 15:27:00 +08002031 /* mark the queue as mq asap */
2032 q->mq_ops = set->ops;
2033
Keith Busch868f2f02015-12-17 17:08:14 -07002034 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2035 if (!q->queue_ctx)
Ming Linc7de5722016-05-25 23:23:27 -07002036 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002037
2038 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2039 GFP_KERNEL, set->numa_node);
2040 if (!q->queue_hw_ctx)
2041 goto err_percpu;
2042
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002043 q->mq_map = set->mq_map;
Keith Busch868f2f02015-12-17 17:08:14 -07002044
2045 blk_mq_realloc_hw_ctxs(set, q);
2046 if (!q->nr_hw_queues)
2047 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002048
Christoph Hellwig287922e2015-10-30 20:57:30 +08002049 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002050 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002051
2052 q->nr_queues = nr_cpu_ids;
Jens Axboe320ae512013-10-24 09:20:05 +01002053
Jens Axboe94eddfb2013-11-19 09:25:07 -07002054 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002055
Jens Axboe05f1dd52014-05-29 09:53:32 -06002056 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2057 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2058
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002059 q->sg_reserved_size = INT_MAX;
2060
Mike Snitzer28494502016-09-14 13:28:30 -04002061 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002062 INIT_LIST_HEAD(&q->requeue_list);
2063 spin_lock_init(&q->requeue_lock);
2064
Jens Axboe07068d52014-05-22 10:40:51 -06002065 if (q->nr_hw_queues > 1)
2066 blk_queue_make_request(q, blk_mq_make_request);
2067 else
2068 blk_queue_make_request(q, blk_sq_make_request);
2069
Jens Axboeeba71762014-05-20 15:17:27 -06002070 /*
2071 * Do this after blk_queue_make_request() overrides it...
2072 */
2073 q->nr_requests = set->queue_depth;
2074
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002075 if (set->ops->complete)
2076 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08002077
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002078 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002079
Akinobu Mita57783222015-09-27 02:09:23 +09002080 get_online_cpus();
Jens Axboe320ae512013-10-24 09:20:05 +01002081 mutex_lock(&all_q_mutex);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002082
Jens Axboe320ae512013-10-24 09:20:05 +01002083 list_add_tail(&q->all_q_node, &all_q_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002084 blk_mq_add_queue_tag_set(set, q);
Akinobu Mita57783222015-09-27 02:09:23 +09002085 blk_mq_map_swqueue(q, cpu_online_mask);
Jens Axboe484b4062014-05-21 14:01:15 -06002086
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002087 mutex_unlock(&all_q_mutex);
Akinobu Mita57783222015-09-27 02:09:23 +09002088 put_online_cpus();
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002089
Jens Axboe320ae512013-10-24 09:20:05 +01002090 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002091
Jens Axboe320ae512013-10-24 09:20:05 +01002092err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002093 kfree(q->queue_hw_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002094err_percpu:
Keith Busch868f2f02015-12-17 17:08:14 -07002095 free_percpu(q->queue_ctx);
Ming Linc7de5722016-05-25 23:23:27 -07002096err_exit:
2097 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002098 return ERR_PTR(-ENOMEM);
2099}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002100EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002101
2102void blk_mq_free_queue(struct request_queue *q)
2103{
Ming Lei624dbe42014-05-27 23:35:13 +08002104 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002105
Akinobu Mita0e626362015-09-27 02:09:22 +09002106 mutex_lock(&all_q_mutex);
2107 list_del_init(&q->all_q_node);
2108 mutex_unlock(&all_q_mutex);
2109
Jens Axboe0d2602c2014-05-13 15:10:52 -06002110 blk_mq_del_queue_tag_set(q);
2111
Ming Lei624dbe42014-05-27 23:35:13 +08002112 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2113 blk_mq_free_hw_queues(q, set);
Jens Axboe320ae512013-10-24 09:20:05 +01002114}
Jens Axboe320ae512013-10-24 09:20:05 +01002115
2116/* Basically redo blk_mq_init_queue with queue frozen */
Akinobu Mita57783222015-09-27 02:09:23 +09002117static void blk_mq_queue_reinit(struct request_queue *q,
2118 const struct cpumask *online_mask)
Jens Axboe320ae512013-10-24 09:20:05 +01002119{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +02002120 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
Jens Axboe320ae512013-10-24 09:20:05 +01002121
Jens Axboe67aec142014-05-30 08:25:36 -06002122 blk_mq_sysfs_unregister(q);
2123
Jens Axboe320ae512013-10-24 09:20:05 +01002124 /*
2125 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2126 * we should change hctx numa_node according to new topology (this
2127 * involves free and re-allocate memory, worthy doing?)
2128 */
2129
Akinobu Mita57783222015-09-27 02:09:23 +09002130 blk_mq_map_swqueue(q, online_mask);
Jens Axboe320ae512013-10-24 09:20:05 +01002131
Jens Axboe67aec142014-05-30 08:25:36 -06002132 blk_mq_sysfs_register(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002133}
2134
Sebastian Andrzej Siewior65d52912016-09-22 08:05:19 -06002135/*
2136 * New online cpumask which is going to be set in this hotplug event.
2137 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2138 * one-by-one and dynamically allocating this could result in a failure.
2139 */
2140static struct cpumask cpuhp_online_new;
2141
2142static void blk_mq_queue_reinit_work(void)
Jens Axboe320ae512013-10-24 09:20:05 +01002143{
2144 struct request_queue *q;
Jens Axboe320ae512013-10-24 09:20:05 +01002145
2146 mutex_lock(&all_q_mutex);
Tejun Heof3af0202014-11-04 13:52:27 -05002147 /*
2148 * We need to freeze and reinit all existing queues. Freezing
2149 * involves synchronous wait for an RCU grace period and doing it
2150 * one by one may take a long time. Start freezing all queues in
2151 * one swoop and then wait for the completions so that freezing can
2152 * take place in parallel.
2153 */
2154 list_for_each_entry(q, &all_q_list, all_q_node)
2155 blk_mq_freeze_queue_start(q);
Ming Leif054b562015-04-21 10:00:19 +08002156 list_for_each_entry(q, &all_q_list, all_q_node) {
Tejun Heof3af0202014-11-04 13:52:27 -05002157 blk_mq_freeze_queue_wait(q);
2158
Ming Leif054b562015-04-21 10:00:19 +08002159 /*
2160 * timeout handler can't touch hw queue during the
2161 * reinitialization
2162 */
2163 del_timer_sync(&q->timeout);
2164 }
2165
Jens Axboe320ae512013-10-24 09:20:05 +01002166 list_for_each_entry(q, &all_q_list, all_q_node)
Sebastian Andrzej Siewior65d52912016-09-22 08:05:19 -06002167 blk_mq_queue_reinit(q, &cpuhp_online_new);
Tejun Heof3af0202014-11-04 13:52:27 -05002168
2169 list_for_each_entry(q, &all_q_list, all_q_node)
2170 blk_mq_unfreeze_queue(q);
2171
Jens Axboe320ae512013-10-24 09:20:05 +01002172 mutex_unlock(&all_q_mutex);
Sebastian Andrzej Siewior65d52912016-09-22 08:05:19 -06002173}
2174
2175static int blk_mq_queue_reinit_dead(unsigned int cpu)
2176{
Sebastian Andrzej Siewior97a32862016-09-23 15:02:38 +02002177 cpumask_copy(&cpuhp_online_new, cpu_online_mask);
Sebastian Andrzej Siewior65d52912016-09-22 08:05:19 -06002178 blk_mq_queue_reinit_work();
2179 return 0;
2180}
2181
2182/*
2183 * Before hotadded cpu starts handling requests, new mappings must be
2184 * established. Otherwise, these requests in hw queue might never be
2185 * dispatched.
2186 *
2187 * For example, there is a single hw queue (hctx) and two CPU queues (ctx0
2188 * for CPU0, and ctx1 for CPU1).
2189 *
2190 * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list
2191 * and set bit0 in pending bitmap as ctx1->index_hw is still zero.
2192 *
2193 * And then while running hw queue, flush_busy_ctxs() finds bit0 is set in
2194 * pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list.
2195 * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list
2196 * is ignored.
2197 */
2198static int blk_mq_queue_reinit_prepare(unsigned int cpu)
2199{
2200 cpumask_copy(&cpuhp_online_new, cpu_online_mask);
2201 cpumask_set_cpu(cpu, &cpuhp_online_new);
2202 blk_mq_queue_reinit_work();
2203 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002204}
2205
Jens Axboea5164402014-09-10 09:02:03 -06002206static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2207{
2208 int i;
2209
2210 for (i = 0; i < set->nr_hw_queues; i++) {
2211 set->tags[i] = blk_mq_init_rq_map(set, i);
2212 if (!set->tags[i])
2213 goto out_unwind;
2214 }
2215
2216 return 0;
2217
2218out_unwind:
2219 while (--i >= 0)
2220 blk_mq_free_rq_map(set, set->tags[i], i);
2221
Jens Axboea5164402014-09-10 09:02:03 -06002222 return -ENOMEM;
2223}
2224
2225/*
2226 * Allocate the request maps associated with this tag_set. Note that this
2227 * may reduce the depth asked for, if memory is tight. set->queue_depth
2228 * will be updated to reflect the allocated depth.
2229 */
2230static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2231{
2232 unsigned int depth;
2233 int err;
2234
2235 depth = set->queue_depth;
2236 do {
2237 err = __blk_mq_alloc_rq_maps(set);
2238 if (!err)
2239 break;
2240
2241 set->queue_depth >>= 1;
2242 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2243 err = -ENOMEM;
2244 break;
2245 }
2246 } while (set->queue_depth);
2247
2248 if (!set->queue_depth || err) {
2249 pr_err("blk-mq: failed to allocate request map\n");
2250 return -ENOMEM;
2251 }
2252
2253 if (depth != set->queue_depth)
2254 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2255 depth, set->queue_depth);
2256
2257 return 0;
2258}
2259
Jens Axboea4391c62014-06-05 15:21:56 -06002260/*
2261 * Alloc a tag set to be associated with one or more request queues.
2262 * May fail with EINVAL for various error conditions. May adjust the
2263 * requested depth down, if if it too large. In that case, the set
2264 * value will be stored in set->queue_depth.
2265 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002266int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2267{
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002268 int ret;
2269
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002270 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2271
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002272 if (!set->nr_hw_queues)
2273 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002274 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002275 return -EINVAL;
2276 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2277 return -EINVAL;
2278
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002279 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002280 return -EINVAL;
2281
Jens Axboea4391c62014-06-05 15:21:56 -06002282 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2283 pr_info("blk-mq: reduced tag depth to %u\n",
2284 BLK_MQ_MAX_DEPTH);
2285 set->queue_depth = BLK_MQ_MAX_DEPTH;
2286 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002287
Shaohua Li6637fad2014-11-30 16:00:58 -08002288 /*
2289 * If a crashdump is active, then we are potentially in a very
2290 * memory constrained environment. Limit us to 1 queue and
2291 * 64 tags to prevent using too much memory.
2292 */
2293 if (is_kdump_kernel()) {
2294 set->nr_hw_queues = 1;
2295 set->queue_depth = min(64U, set->queue_depth);
2296 }
Keith Busch868f2f02015-12-17 17:08:14 -07002297 /*
2298 * There is no use for more h/w queues than cpus.
2299 */
2300 if (set->nr_hw_queues > nr_cpu_ids)
2301 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08002302
Keith Busch868f2f02015-12-17 17:08:14 -07002303 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002304 GFP_KERNEL, set->numa_node);
2305 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002306 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002307
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002308 ret = -ENOMEM;
2309 set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
2310 GFP_KERNEL, set->numa_node);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002311 if (!set->mq_map)
2312 goto out_free_tags;
2313
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002314 if (set->ops->map_queues)
2315 ret = set->ops->map_queues(set);
2316 else
2317 ret = blk_mq_map_queues(set);
2318 if (ret)
2319 goto out_free_mq_map;
2320
2321 ret = blk_mq_alloc_rq_maps(set);
2322 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002323 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002324
Jens Axboe0d2602c2014-05-13 15:10:52 -06002325 mutex_init(&set->tag_list_lock);
2326 INIT_LIST_HEAD(&set->tag_list);
2327
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002328 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002329
2330out_free_mq_map:
2331 kfree(set->mq_map);
2332 set->mq_map = NULL;
2333out_free_tags:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002334 kfree(set->tags);
2335 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002336 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002337}
2338EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2339
2340void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2341{
2342 int i;
2343
Keith Busch868f2f02015-12-17 17:08:14 -07002344 for (i = 0; i < nr_cpu_ids; i++) {
Junichi Nomuraf42d79a2015-10-14 05:02:15 +00002345 if (set->tags[i])
Jens Axboe484b4062014-05-21 14:01:15 -06002346 blk_mq_free_rq_map(set, set->tags[i], i);
2347 }
2348
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002349 kfree(set->mq_map);
2350 set->mq_map = NULL;
2351
Ming Lei981bd182014-04-24 00:07:34 +08002352 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002353 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002354}
2355EXPORT_SYMBOL(blk_mq_free_tag_set);
2356
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002357int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2358{
2359 struct blk_mq_tag_set *set = q->tag_set;
2360 struct blk_mq_hw_ctx *hctx;
2361 int i, ret;
2362
2363 if (!set || nr > set->queue_depth)
2364 return -EINVAL;
2365
2366 ret = 0;
2367 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07002368 if (!hctx->tags)
2369 continue;
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002370 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2371 if (ret)
2372 break;
2373 }
2374
2375 if (!ret)
2376 q->nr_requests = nr;
2377
2378 return ret;
2379}
2380
Keith Busch868f2f02015-12-17 17:08:14 -07002381void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2382{
2383 struct request_queue *q;
2384
2385 if (nr_hw_queues > nr_cpu_ids)
2386 nr_hw_queues = nr_cpu_ids;
2387 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2388 return;
2389
2390 list_for_each_entry(q, &set->tag_list, tag_set_list)
2391 blk_mq_freeze_queue(q);
2392
2393 set->nr_hw_queues = nr_hw_queues;
2394 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2395 blk_mq_realloc_hw_ctxs(set, q);
2396
2397 if (q->nr_hw_queues > 1)
2398 blk_queue_make_request(q, blk_mq_make_request);
2399 else
2400 blk_queue_make_request(q, blk_sq_make_request);
2401
2402 blk_mq_queue_reinit(q, cpu_online_mask);
2403 }
2404
2405 list_for_each_entry(q, &set->tag_list, tag_set_list)
2406 blk_mq_unfreeze_queue(q);
2407}
2408EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2409
Jens Axboe676141e2014-03-20 13:29:18 -06002410void blk_mq_disable_hotplug(void)
2411{
2412 mutex_lock(&all_q_mutex);
2413}
2414
2415void blk_mq_enable_hotplug(void)
2416{
2417 mutex_unlock(&all_q_mutex);
2418}
2419
Jens Axboe320ae512013-10-24 09:20:05 +01002420static int __init blk_mq_init(void)
2421{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002422 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
2423 blk_mq_hctx_notify_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01002424
Sebastian Andrzej Siewior65d52912016-09-22 08:05:19 -06002425 cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE, "block/mq:prepare",
2426 blk_mq_queue_reinit_prepare,
2427 blk_mq_queue_reinit_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01002428 return 0;
2429}
2430subsys_initcall(blk_mq_init);