blob: 4603b115e234887860cbb28520c36ba9e5e7efd8 [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>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010023#include <linux/sched/topology.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Jens Axboe320ae512013-10-24 09:20:05 +010025#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060026#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060027#include <linux/prefetch.h>
Jens Axboe320ae512013-10-24 09:20:05 +010028
29#include <trace/events/block.h>
30
31#include <linux/blk-mq.h>
32#include "blk.h"
33#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060034#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010035#include "blk-mq-tag.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070036#include "blk-stat.h"
Jens Axboe87760e52016-11-09 12:38:14 -070037#include "blk-wbt.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070038#include "blk-mq-sched.h"
Jens Axboe320ae512013-10-24 09:20:05 +010039
Omar Sandoval34dbad52017-03-21 08:56:08 -070040static void blk_mq_poll_stats_start(struct request_queue *q);
41static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
42
Stephen Bates720b8cc2017-04-07 06:24:03 -060043static int blk_mq_poll_stats_bkt(const struct request *rq)
44{
45 int ddir, bytes, bucket;
46
Jens Axboe99c749a2017-04-21 07:55:42 -060047 ddir = rq_data_dir(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060048 bytes = blk_rq_bytes(rq);
49
50 bucket = ddir + 2*(ilog2(bytes) - 9);
51
52 if (bucket < 0)
53 return -1;
54 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
55 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
56
57 return bucket;
58}
59
Jens Axboe320ae512013-10-24 09:20:05 +010060/*
61 * Check if any of the ctx's have pending work in this hardware queue
62 */
Jens Axboe50e1dab2017-01-26 14:42:34 -070063bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010064{
Jens Axboebd166ef2017-01-17 06:03:22 -070065 return sbitmap_any_bit_set(&hctx->ctx_map) ||
66 !list_empty_careful(&hctx->dispatch) ||
67 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010068}
69
70/*
71 * Mark this ctx as having pending work in this hardware queue
72 */
73static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
74 struct blk_mq_ctx *ctx)
75{
Omar Sandoval88459642016-09-17 08:38:44 -060076 if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
77 sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
Jens Axboe1429d7c2014-05-19 09:23:55 -060078}
79
80static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
81 struct blk_mq_ctx *ctx)
82{
Omar Sandoval88459642016-09-17 08:38:44 -060083 sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
Jens Axboe320ae512013-10-24 09:20:05 +010084}
85
Ming Lei1671d522017-03-27 20:06:57 +080086void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +080087{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020088 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -040089
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +020090 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
91 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -040092 percpu_ref_kill(&q->q_usage_counter);
Mike Snitzerb94ec292015-03-11 23:56:38 -040093 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -040094 }
Tejun Heof3af0202014-11-04 13:52:27 -050095}
Ming Lei1671d522017-03-27 20:06:57 +080096EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -050097
Keith Busch6bae363e2017-03-01 14:22:10 -050098void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -050099{
Dan Williams3ef28e82015-10-21 13:20:12 -0400100 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800101}
Keith Busch6bae363e2017-03-01 14:22:10 -0500102EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800103
Keith Buschf91328c2017-03-01 14:22:11 -0500104int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
105 unsigned long timeout)
106{
107 return wait_event_timeout(q->mq_freeze_wq,
108 percpu_ref_is_zero(&q->q_usage_counter),
109 timeout);
110}
111EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100112
Tejun Heof3af0202014-11-04 13:52:27 -0500113/*
114 * Guarantee no request is in use, so we can change any data structure of
115 * the queue afterward.
116 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400117void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500118{
Dan Williams3ef28e82015-10-21 13:20:12 -0400119 /*
120 * In the !blk_mq case we are only calling this to kill the
121 * q_usage_counter, otherwise this increases the freeze depth
122 * and waits for it to return to zero. For this reason there is
123 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
124 * exported to drivers as the only user for unfreeze is blk_mq.
125 */
Ming Lei1671d522017-03-27 20:06:57 +0800126 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500127 blk_mq_freeze_queue_wait(q);
128}
Dan Williams3ef28e82015-10-21 13:20:12 -0400129
130void blk_mq_freeze_queue(struct request_queue *q)
131{
132 /*
133 * ...just an alias to keep freeze and unfreeze actions balanced
134 * in the blk_mq_* namespace
135 */
136 blk_freeze_queue(q);
137}
Jens Axboec761d962015-01-02 15:05:12 -0700138EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500139
Keith Buschb4c6a022014-12-19 17:54:14 -0700140void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100141{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200142 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100143
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200144 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
145 WARN_ON_ONCE(freeze_depth < 0);
146 if (!freeze_depth) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400147 percpu_ref_reinit(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100148 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600149 }
Jens Axboe320ae512013-10-24 09:20:05 +0100150}
Keith Buschb4c6a022014-12-19 17:54:14 -0700151EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100152
Bart Van Assche852ec802017-06-21 10:55:47 -0700153/*
154 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
155 * mpt3sas driver such that this function can be removed.
156 */
157void blk_mq_quiesce_queue_nowait(struct request_queue *q)
158{
159 unsigned long flags;
160
161 spin_lock_irqsave(q->queue_lock, flags);
162 queue_flag_set(QUEUE_FLAG_QUIESCED, q);
163 spin_unlock_irqrestore(q->queue_lock, flags);
164}
165EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
166
Bart Van Assche6a83e742016-11-02 10:09:51 -0600167/**
Ming Lei69e07c42017-06-06 23:22:07 +0800168 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600169 * @q: request queue.
170 *
171 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800172 * callback function is invoked. Once this function is returned, we make
173 * sure no dispatch can happen until the queue is unquiesced via
174 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600175 */
176void blk_mq_quiesce_queue(struct request_queue *q)
177{
178 struct blk_mq_hw_ctx *hctx;
179 unsigned int i;
180 bool rcu = false;
181
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800182 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600183
Bart Van Assche6a83e742016-11-02 10:09:51 -0600184 queue_for_each_hw_ctx(q, hctx, i) {
185 if (hctx->flags & BLK_MQ_F_BLOCKING)
Bart Van Assche07319672017-06-20 11:15:38 -0700186 synchronize_srcu(hctx->queue_rq_srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600187 else
188 rcu = true;
189 }
190 if (rcu)
191 synchronize_rcu();
192}
193EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
194
Ming Leie4e73912017-06-06 23:22:03 +0800195/*
196 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
197 * @q: request queue.
198 *
199 * This function recovers queue into the state before quiescing
200 * which is done by blk_mq_quiesce_queue.
201 */
202void blk_mq_unquiesce_queue(struct request_queue *q)
203{
Bart Van Assche852ec802017-06-21 10:55:47 -0700204 unsigned long flags;
205
206 spin_lock_irqsave(q->queue_lock, flags);
Ming Leif4560ff2017-06-18 14:24:27 -0600207 queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700208 spin_unlock_irqrestore(q->queue_lock, flags);
Ming Leif4560ff2017-06-18 14:24:27 -0600209
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800210 /* dispatch requests which are inserted during quiescing */
211 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800212}
213EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
214
Jens Axboeaed3ea92014-12-22 14:04:42 -0700215void blk_mq_wake_waiters(struct request_queue *q)
216{
217 struct blk_mq_hw_ctx *hctx;
218 unsigned int i;
219
220 queue_for_each_hw_ctx(q, hctx, i)
221 if (blk_mq_hw_queue_mapped(hctx))
222 blk_mq_tag_wakeup_all(hctx->tags, true);
Keith Busch3fd59402015-01-08 08:53:56 -0700223
224 /*
225 * If we are called because the queue has now been marked as
226 * dying, we need to ensure that processes currently waiting on
227 * the queue are notified as well.
228 */
229 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700230}
231
Jens Axboe320ae512013-10-24 09:20:05 +0100232bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
233{
234 return blk_mq_has_free_tags(hctx->tags);
235}
236EXPORT_SYMBOL(blk_mq_can_queue);
237
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200238static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
239 unsigned int tag, unsigned int op)
Jens Axboe320ae512013-10-24 09:20:05 +0100240{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200241 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
242 struct request *rq = tags->static_rqs[tag];
243
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700244 rq->rq_flags = 0;
245
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200246 if (data->flags & BLK_MQ_REQ_INTERNAL) {
247 rq->tag = -1;
248 rq->internal_tag = tag;
249 } else {
250 if (blk_mq_tag_busy(data->hctx)) {
251 rq->rq_flags = RQF_MQ_INFLIGHT;
252 atomic_inc(&data->hctx->nr_active);
253 }
254 rq->tag = tag;
255 rq->internal_tag = -1;
256 data->hctx->tags->rqs[rq->tag] = rq;
257 }
258
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200259 INIT_LIST_HEAD(&rq->queuelist);
260 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200261 rq->q = data->q;
262 rq->mq_ctx = data->ctx;
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600263 rq->cmd_flags = op;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200264 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200265 rq->rq_flags |= RQF_IO_STAT;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200266 /* do not touch atomic flags, it needs atomic ops against the timer */
267 rq->cpu = -1;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200268 INIT_HLIST_NODE(&rq->hash);
269 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200270 rq->rq_disk = NULL;
271 rq->part = NULL;
Jens Axboe3ee32372014-06-09 09:36:53 -0600272 rq->start_time = jiffies;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200273#ifdef CONFIG_BLK_CGROUP
274 rq->rl = NULL;
Ming Lei0fec08b2014-01-03 10:00:08 -0700275 set_start_time_ns(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200276 rq->io_start_time_ns = 0;
277#endif
278 rq->nr_phys_segments = 0;
279#if defined(CONFIG_BLK_DEV_INTEGRITY)
280 rq->nr_integrity_segments = 0;
281#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200282 rq->special = NULL;
283 /* tag was already set */
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200284 rq->extra_len = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200285
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200286 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600287 rq->timeout = 0;
288
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200289 rq->end_io = NULL;
290 rq->end_io_data = NULL;
291 rq->next_rq = NULL;
292
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200293 data->ctx->rq_dispatched[op_is_sync(op)]++;
294 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100295}
296
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200297static struct request *blk_mq_get_request(struct request_queue *q,
298 struct bio *bio, unsigned int op,
299 struct blk_mq_alloc_data *data)
300{
301 struct elevator_queue *e = q->elevator;
302 struct request *rq;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200303 unsigned int tag;
Ming Lei1ad43c02017-08-02 08:01:45 +0800304 struct blk_mq_ctx *local_ctx = NULL;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200305
306 blk_queue_enter_live(q);
307 data->q = q;
308 if (likely(!data->ctx))
Ming Lei1ad43c02017-08-02 08:01:45 +0800309 data->ctx = local_ctx = blk_mq_get_ctx(q);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200310 if (likely(!data->hctx))
311 data->hctx = blk_mq_map_queue(q, data->ctx->cpu);
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500312 if (op & REQ_NOWAIT)
313 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200314
315 if (e) {
316 data->flags |= BLK_MQ_REQ_INTERNAL;
317
318 /*
319 * Flush requests are special and go directly to the
320 * dispatch list.
321 */
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200322 if (!op_is_flush(op) && e->type->ops.mq.limit_depth)
323 e->type->ops.mq.limit_depth(op, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200324 }
325
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200326 tag = blk_mq_get_tag(data);
327 if (tag == BLK_MQ_TAG_FAIL) {
Ming Lei1ad43c02017-08-02 08:01:45 +0800328 if (local_ctx) {
329 blk_mq_put_ctx(local_ctx);
330 data->ctx = NULL;
331 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200332 blk_queue_exit(q);
333 return NULL;
334 }
335
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200336 rq = blk_mq_rq_ctx_init(data, tag, op);
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200337 if (!op_is_flush(op)) {
338 rq->elv.icq = NULL;
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200339 if (e && e->type->ops.mq.prepare_request) {
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200340 if (e->type->icq_cache && rq_ioc(bio))
341 blk_mq_sched_assign_ioc(rq, bio);
342
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200343 e->type->ops.mq.prepare_request(rq, bio);
344 rq->rq_flags |= RQF_ELVPRIV;
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200345 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200346 }
347 data->hctx->queued++;
348 return rq;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200349}
350
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700351struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100352 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100353{
Jens Axboe5a797e02017-01-26 12:22:11 -0700354 struct blk_mq_alloc_data alloc_data = { .flags = flags };
Jens Axboebd166ef2017-01-17 06:03:22 -0700355 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600356 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100357
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100358 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
Joe Lawrencea492f072014-08-28 08:15:21 -0600359 if (ret)
360 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100361
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700362 rq = blk_mq_get_request(q, NULL, op, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400363 blk_queue_exit(q);
Jens Axboe841bac22016-09-21 10:08:43 -0600364
Jens Axboebd166ef2017-01-17 06:03:22 -0700365 if (!rq)
Joe Lawrencea492f072014-08-28 08:15:21 -0600366 return ERR_PTR(-EWOULDBLOCK);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200367
Ming Lei1ad43c02017-08-02 08:01:45 +0800368 blk_mq_put_ctx(alloc_data.ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800369
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200370 rq->__data_len = 0;
371 rq->__sector = (sector_t) -1;
372 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100373 return rq;
374}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600375EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100376
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700377struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
378 unsigned int op, unsigned int flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200379{
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800380 struct blk_mq_alloc_data alloc_data = { .flags = flags };
Ming Lin1f5bd332016-06-13 16:45:21 +0200381 struct request *rq;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800382 unsigned int cpu;
Ming Lin1f5bd332016-06-13 16:45:21 +0200383 int ret;
384
385 /*
386 * If the tag allocator sleeps we could get an allocation for a
387 * different hardware context. No need to complicate the low level
388 * allocator for this for the rare use case of a command tied to
389 * a specific queue.
390 */
391 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
392 return ERR_PTR(-EINVAL);
393
394 if (hctx_idx >= q->nr_hw_queues)
395 return ERR_PTR(-EIO);
396
397 ret = blk_queue_enter(q, true);
398 if (ret)
399 return ERR_PTR(ret);
400
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600401 /*
402 * Check if the hardware context is actually mapped to anything.
403 * If not tell the caller that it should skip this queue.
404 */
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800405 alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
406 if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
407 blk_queue_exit(q);
408 return ERR_PTR(-EXDEV);
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600409 }
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800410 cpu = cpumask_first(alloc_data.hctx->cpumask);
411 alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200412
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700413 rq = blk_mq_get_request(q, NULL, op, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400414 blk_queue_exit(q);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800415
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800416 if (!rq)
417 return ERR_PTR(-EWOULDBLOCK);
Ming Lin1f5bd332016-06-13 16:45:21 +0200418
419 return rq;
420}
421EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
422
Christoph Hellwig6af54052017-06-16 18:15:22 +0200423void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100424{
Jens Axboe320ae512013-10-24 09:20:05 +0100425 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200426 struct elevator_queue *e = q->elevator;
427 struct blk_mq_ctx *ctx = rq->mq_ctx;
428 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
429 const int sched_tag = rq->internal_tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100430
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200431 if (rq->rq_flags & RQF_ELVPRIV) {
Christoph Hellwig6af54052017-06-16 18:15:22 +0200432 if (e && e->type->ops.mq.finish_request)
433 e->type->ops.mq.finish_request(rq);
434 if (rq->elv.icq) {
435 put_io_context(rq->elv.icq->ioc);
436 rq->elv.icq = NULL;
437 }
438 }
439
440 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200441 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600442 atomic_dec(&hctx->nr_active);
Jens Axboe87760e52016-11-09 12:38:14 -0700443
444 wbt_done(q->rq_wb, &rq->issue_stat);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600445
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200446 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
Jens Axboe06426ad2016-11-14 13:01:59 -0700447 clear_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
Jens Axboebd166ef2017-01-17 06:03:22 -0700448 if (rq->tag != -1)
449 blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
450 if (sched_tag != -1)
Omar Sandovalc05f8522017-04-14 01:00:01 -0700451 blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600452 blk_mq_sched_restart(hctx);
Dan Williams3ef28e82015-10-21 13:20:12 -0400453 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100454}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700455EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100456
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200457inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100458{
Ming Lei0d11e6a2013-12-05 10:50:39 -0700459 blk_account_io_done(rq);
460
Christoph Hellwig91b63632014-04-16 09:44:53 +0200461 if (rq->end_io) {
Jens Axboe87760e52016-11-09 12:38:14 -0700462 wbt_done(rq->q->rq_wb, &rq->issue_stat);
Jens Axboe320ae512013-10-24 09:20:05 +0100463 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200464 } else {
465 if (unlikely(blk_bidi_rq(rq)))
466 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100467 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200468 }
Jens Axboe320ae512013-10-24 09:20:05 +0100469}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700470EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200471
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200472void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200473{
474 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
475 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700476 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200477}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700478EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100479
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800480static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100481{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800482 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100483
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800484 rq->q->softirq_done_fn(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100485}
486
Christoph Hellwig453f8342017-04-20 16:03:10 +0200487static void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100488{
489 struct blk_mq_ctx *ctx = rq->mq_ctx;
Christoph Hellwig38535202014-04-25 02:32:53 -0700490 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100491 int cpu;
492
Christoph Hellwig453f8342017-04-20 16:03:10 +0200493 if (rq->internal_tag != -1)
494 blk_mq_sched_completed_request(rq);
495 if (rq->rq_flags & RQF_STATS) {
496 blk_mq_poll_stats_start(rq->q);
497 blk_stat_add(rq);
498 }
499
Christoph Hellwig38535202014-04-25 02:32:53 -0700500 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800501 rq->q->softirq_done_fn(rq);
502 return;
503 }
Jens Axboe320ae512013-10-24 09:20:05 +0100504
505 cpu = get_cpu();
Christoph Hellwig38535202014-04-25 02:32:53 -0700506 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
507 shared = cpus_share_cache(cpu, ctx->cpu);
508
509 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800510 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800511 rq->csd.info = rq;
512 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100513 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800514 } else {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800515 rq->q->softirq_done_fn(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800516 }
Jens Axboe320ae512013-10-24 09:20:05 +0100517 put_cpu();
518}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800519
520/**
521 * blk_mq_complete_request - end I/O on a request
522 * @rq: the request being processed
523 *
524 * Description:
525 * Ends all I/O on a request. It does not handle partial completions.
526 * The actual completion happens out-of-order, through a IPI handler.
527 **/
Christoph Hellwig08e00292017-04-20 16:03:09 +0200528void blk_mq_complete_request(struct request *rq)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800529{
Jens Axboe95f09682014-05-27 17:46:48 -0600530 struct request_queue *q = rq->q;
531
532 if (unlikely(blk_should_fake_timeout(q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800533 return;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200534 if (!blk_mark_rq_complete(rq))
Jens Axboeed851862014-05-30 21:20:50 -0600535 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800536}
537EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100538
Keith Busch973c0192015-01-07 18:55:43 -0700539int blk_mq_request_started(struct request *rq)
540{
541 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
542}
543EXPORT_SYMBOL_GPL(blk_mq_request_started);
544
Christoph Hellwige2490072014-09-13 16:40:09 -0700545void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100546{
547 struct request_queue *q = rq->q;
548
Jens Axboebd166ef2017-01-17 06:03:22 -0700549 blk_mq_sched_started_request(rq);
550
Jens Axboe320ae512013-10-24 09:20:05 +0100551 trace_block_rq_issue(q, rq);
552
Jens Axboecf43e6b2016-11-07 21:32:37 -0700553 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Shaohua Li88eeca42017-03-27 15:19:41 -0700554 blk_stat_set_issue(&rq->issue_stat, blk_rq_sectors(rq));
Jens Axboecf43e6b2016-11-07 21:32:37 -0700555 rq->rq_flags |= RQF_STATS;
Jens Axboe87760e52016-11-09 12:38:14 -0700556 wbt_issue(q->rq_wb, &rq->issue_stat);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700557 }
558
Ming Lei2b8393b2014-06-10 00:16:41 +0800559 blk_add_timer(rq);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600560
561 /*
Jens Axboe538b7532014-09-16 10:37:37 -0600562 * Ensure that ->deadline is visible before set the started
563 * flag and clear the completed flag.
564 */
565 smp_mb__before_atomic();
566
567 /*
Jens Axboe87ee7b12014-04-24 08:51:47 -0600568 * Mark us as started and clear complete. Complete might have been
569 * set if requeue raced with timeout, which then marked it as
570 * complete. So be sure to clear complete again when we start
571 * the request, otherwise we'll ignore the completion event.
572 */
Jens Axboe4b570522014-05-29 11:00:11 -0600573 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
574 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
575 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
576 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800577
578 if (q->dma_drain_size && blk_rq_bytes(rq)) {
579 /*
580 * Make sure space for the drain appears. We know we can do
581 * this because max_hw_segments has been adjusted to be one
582 * fewer than the device can handle.
583 */
584 rq->nr_phys_segments++;
585 }
Jens Axboe320ae512013-10-24 09:20:05 +0100586}
Christoph Hellwige2490072014-09-13 16:40:09 -0700587EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100588
Ming Leid9d149a2017-03-27 20:06:55 +0800589/*
590 * When we reach here because queue is busy, REQ_ATOM_COMPLETE
Jens Axboe48b99c92017-03-29 11:10:34 -0600591 * flag isn't set yet, so there may be race with timeout handler,
Ming Leid9d149a2017-03-27 20:06:55 +0800592 * but given rq->deadline is just set in .queue_rq() under
593 * this situation, the race won't be possible in reality because
594 * rq->timeout should be set as big enough to cover the window
595 * between blk_mq_start_request() called from .queue_rq() and
596 * clearing REQ_ATOM_STARTED here.
597 */
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200598static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100599{
600 struct request_queue *q = rq->q;
601
602 trace_block_rq_requeue(q, rq);
Jens Axboe87760e52016-11-09 12:38:14 -0700603 wbt_requeue(q->rq_wb, &rq->issue_stat);
Jens Axboebd166ef2017-01-17 06:03:22 -0700604 blk_mq_sched_requeue_request(rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800605
Christoph Hellwige2490072014-09-13 16:40:09 -0700606 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
607 if (q->dma_drain_size && blk_rq_bytes(rq))
608 rq->nr_phys_segments--;
609 }
Jens Axboe320ae512013-10-24 09:20:05 +0100610}
611
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700612void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200613{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200614 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200615
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200616 BUG_ON(blk_queued_rq(rq));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700617 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200618}
619EXPORT_SYMBOL(blk_mq_requeue_request);
620
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600621static void blk_mq_requeue_work(struct work_struct *work)
622{
623 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400624 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600625 LIST_HEAD(rq_list);
626 struct request *rq, *next;
627 unsigned long flags;
628
629 spin_lock_irqsave(&q->requeue_lock, flags);
630 list_splice_init(&q->requeue_list, &rq_list);
631 spin_unlock_irqrestore(&q->requeue_lock, flags);
632
633 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200634 if (!(rq->rq_flags & RQF_SOFTBARRIER))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600635 continue;
636
Christoph Hellwige8064022016-10-20 15:12:13 +0200637 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600638 list_del_init(&rq->queuelist);
Jens Axboebd6737f2017-01-27 01:00:47 -0700639 blk_mq_sched_insert_request(rq, true, false, false, true);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600640 }
641
642 while (!list_empty(&rq_list)) {
643 rq = list_entry(rq_list.next, struct request, queuelist);
644 list_del_init(&rq->queuelist);
Jens Axboebd6737f2017-01-27 01:00:47 -0700645 blk_mq_sched_insert_request(rq, false, false, false, true);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600646 }
647
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700648 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600649}
650
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700651void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
652 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600653{
654 struct request_queue *q = rq->q;
655 unsigned long flags;
656
657 /*
658 * We abuse this flag that is otherwise used by the I/O scheduler to
659 * request head insertation from the workqueue.
660 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200661 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600662
663 spin_lock_irqsave(&q->requeue_lock, flags);
664 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200665 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600666 list_add(&rq->queuelist, &q->requeue_list);
667 } else {
668 list_add_tail(&rq->queuelist, &q->requeue_list);
669 }
670 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700671
672 if (kick_requeue_list)
673 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600674}
675EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
676
677void blk_mq_kick_requeue_list(struct request_queue *q)
678{
Mike Snitzer28494502016-09-14 13:28:30 -0400679 kblockd_schedule_delayed_work(&q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600680}
681EXPORT_SYMBOL(blk_mq_kick_requeue_list);
682
Mike Snitzer28494502016-09-14 13:28:30 -0400683void blk_mq_delay_kick_requeue_list(struct request_queue *q,
684 unsigned long msecs)
685{
Bart Van Assched4acf362017-08-09 11:28:06 -0700686 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
687 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400688}
689EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
690
Jens Axboe0e62f512014-06-04 10:23:49 -0600691struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
692{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600693 if (tag < tags->nr_tags) {
694 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700695 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600696 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700697
698 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600699}
700EXPORT_SYMBOL(blk_mq_tag_to_rq);
701
Jens Axboe320ae512013-10-24 09:20:05 +0100702struct blk_mq_timeout_data {
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700703 unsigned long next;
704 unsigned int next_set;
Jens Axboe320ae512013-10-24 09:20:05 +0100705};
706
Christoph Hellwig90415832014-09-22 10:21:48 -0600707void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100708{
Jens Axboef8a5b122016-12-13 09:24:51 -0700709 const struct blk_mq_ops *ops = req->q->mq_ops;
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700710 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600711
712 /*
713 * We know that complete is set at this point. If STARTED isn't set
714 * anymore, then the request isn't active and the "timeout" should
715 * just be ignored. This can happen due to the bitflag ordering.
716 * Timeout first checks if STARTED is set, and if it is, assumes
717 * the request is active. But if we race with completion, then
Jens Axboe48b99c92017-03-29 11:10:34 -0600718 * both flags will get cleared. So check here again, and ignore
Jens Axboe87ee7b12014-04-24 08:51:47 -0600719 * a timeout event with a request that isn't active.
720 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700721 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
722 return;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600723
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700724 if (ops->timeout)
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700725 ret = ops->timeout(req, reserved);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600726
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700727 switch (ret) {
728 case BLK_EH_HANDLED:
729 __blk_mq_complete_request(req);
730 break;
731 case BLK_EH_RESET_TIMER:
732 blk_add_timer(req);
733 blk_clear_rq_complete(req);
734 break;
735 case BLK_EH_NOT_HANDLED:
736 break;
737 default:
738 printk(KERN_ERR "block: bad eh return: %d\n", ret);
739 break;
740 }
Jens Axboe87ee7b12014-04-24 08:51:47 -0600741}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700742
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700743static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
744 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100745{
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700746 struct blk_mq_timeout_data *data = priv;
747
Ming Lei95a49602017-03-22 10:14:43 +0800748 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700749 return;
Jens Axboe320ae512013-10-24 09:20:05 +0100750
Ming Leid9d149a2017-03-27 20:06:55 +0800751 /*
752 * The rq being checked may have been freed and reallocated
753 * out already here, we avoid this race by checking rq->deadline
754 * and REQ_ATOM_COMPLETE flag together:
755 *
756 * - if rq->deadline is observed as new value because of
757 * reusing, the rq won't be timed out because of timing.
758 * - if rq->deadline is observed as previous value,
759 * REQ_ATOM_COMPLETE flag won't be cleared in reuse path
760 * because we put a barrier between setting rq->deadline
761 * and clearing the flag in blk_mq_start_request(), so
762 * this rq won't be timed out too.
763 */
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700764 if (time_after_eq(jiffies, rq->deadline)) {
765 if (!blk_mark_rq_complete(rq))
Christoph Hellwig0152fb62014-09-13 16:40:13 -0700766 blk_mq_rq_timed_out(rq, reserved);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700767 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
768 data->next = rq->deadline;
769 data->next_set = 1;
770 }
Jens Axboe320ae512013-10-24 09:20:05 +0100771}
772
Christoph Hellwig287922e2015-10-30 20:57:30 +0800773static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100774{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800775 struct request_queue *q =
776 container_of(work, struct request_queue, timeout_work);
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700777 struct blk_mq_timeout_data data = {
778 .next = 0,
779 .next_set = 0,
780 };
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700781 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100782
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600783 /* A deadlock might occur if a request is stuck requiring a
784 * timeout at the same time a queue freeze is waiting
785 * completion, since the timeout code would not be able to
786 * acquire the queue reference here.
787 *
788 * That's why we don't use blk_queue_enter here; instead, we use
789 * percpu_ref_tryget directly, because we need to be able to
790 * obtain a reference even in the short window between the queue
791 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800792 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600793 * consumed, marked by the instant q_usage_counter reaches
794 * zero.
795 */
796 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800797 return;
798
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200799 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
Jens Axboe320ae512013-10-24 09:20:05 +0100800
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700801 if (data.next_set) {
802 data.next = blk_rq_timeout(round_jiffies_up(data.next));
803 mod_timer(&q->timeout, data.next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600804 } else {
Christoph Hellwig0bf6cd52015-09-27 21:01:51 +0200805 struct blk_mq_hw_ctx *hctx;
806
Ming Leif054b562015-04-21 10:00:19 +0800807 queue_for_each_hw_ctx(q, hctx, i) {
808 /* the hctx may be unmapped, so check it here */
809 if (blk_mq_hw_queue_mapped(hctx))
810 blk_mq_tag_idle(hctx);
811 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600812 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800813 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100814}
815
Omar Sandoval88459642016-09-17 08:38:44 -0600816struct flush_busy_ctx_data {
817 struct blk_mq_hw_ctx *hctx;
818 struct list_head *list;
819};
820
821static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
822{
823 struct flush_busy_ctx_data *flush_data = data;
824 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
825 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
826
827 sbitmap_clear_bit(sb, bitnr);
828 spin_lock(&ctx->lock);
829 list_splice_tail_init(&ctx->rq_list, flush_data->list);
830 spin_unlock(&ctx->lock);
831 return true;
832}
833
Jens Axboe320ae512013-10-24 09:20:05 +0100834/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600835 * Process software queues that have been marked busy, splicing them
836 * to the for-dispatch
837 */
Jens Axboe2c3ad662016-12-14 14:34:47 -0700838void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -0600839{
Omar Sandoval88459642016-09-17 08:38:44 -0600840 struct flush_busy_ctx_data data = {
841 .hctx = hctx,
842 .list = list,
843 };
Jens Axboe1429d7c2014-05-19 09:23:55 -0600844
Omar Sandoval88459642016-09-17 08:38:44 -0600845 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600846}
Jens Axboe2c3ad662016-12-14 14:34:47 -0700847EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600848
Jens Axboe703fd1c2016-09-16 13:59:14 -0600849static inline unsigned int queued_to_index(unsigned int queued)
850{
851 if (!queued)
852 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600853
Jens Axboe703fd1c2016-09-16 13:59:14 -0600854 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600855}
856
Jens Axboebd6737f2017-01-27 01:00:47 -0700857bool blk_mq_get_driver_tag(struct request *rq, struct blk_mq_hw_ctx **hctx,
858 bool wait)
Jens Axboebd166ef2017-01-17 06:03:22 -0700859{
860 struct blk_mq_alloc_data data = {
861 .q = rq->q,
Jens Axboebd166ef2017-01-17 06:03:22 -0700862 .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
863 .flags = wait ? 0 : BLK_MQ_REQ_NOWAIT,
864 };
865
Jens Axboe5feeacd2017-04-20 17:23:13 -0600866 might_sleep_if(wait);
867
Omar Sandoval81380ca2017-04-07 08:56:26 -0600868 if (rq->tag != -1)
869 goto done;
Jens Axboebd166ef2017-01-17 06:03:22 -0700870
Sagi Grimberg415b8062017-02-27 10:04:39 -0700871 if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
872 data.flags |= BLK_MQ_REQ_RESERVED;
873
Jens Axboebd166ef2017-01-17 06:03:22 -0700874 rq->tag = blk_mq_get_tag(&data);
875 if (rq->tag >= 0) {
Jens Axboe200e86b2017-01-25 08:11:38 -0700876 if (blk_mq_tag_busy(data.hctx)) {
877 rq->rq_flags |= RQF_MQ_INFLIGHT;
878 atomic_inc(&data.hctx->nr_active);
879 }
Jens Axboebd166ef2017-01-17 06:03:22 -0700880 data.hctx->tags->rqs[rq->tag] = rq;
Jens Axboebd166ef2017-01-17 06:03:22 -0700881 }
882
Omar Sandoval81380ca2017-04-07 08:56:26 -0600883done:
884 if (hctx)
885 *hctx = data.hctx;
886 return rq->tag != -1;
Jens Axboebd166ef2017-01-17 06:03:22 -0700887}
888
Jens Axboe113285b2017-03-02 13:26:04 -0700889static void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
890 struct request *rq)
Jens Axboe99cf1dc2017-01-26 12:32:32 -0700891{
Jens Axboe99cf1dc2017-01-26 12:32:32 -0700892 blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag);
893 rq->tag = -1;
894
895 if (rq->rq_flags & RQF_MQ_INFLIGHT) {
896 rq->rq_flags &= ~RQF_MQ_INFLIGHT;
897 atomic_dec(&hctx->nr_active);
898 }
899}
900
Jens Axboe113285b2017-03-02 13:26:04 -0700901static void blk_mq_put_driver_tag_hctx(struct blk_mq_hw_ctx *hctx,
902 struct request *rq)
903{
904 if (rq->tag == -1 || rq->internal_tag == -1)
905 return;
906
907 __blk_mq_put_driver_tag(hctx, rq);
908}
909
910static void blk_mq_put_driver_tag(struct request *rq)
911{
912 struct blk_mq_hw_ctx *hctx;
913
914 if (rq->tag == -1 || rq->internal_tag == -1)
915 return;
916
917 hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu);
918 __blk_mq_put_driver_tag(hctx, rq);
919}
920
Jens Axboebd166ef2017-01-17 06:03:22 -0700921/*
922 * If we fail getting a driver tag because all the driver tags are already
923 * assigned and on the dispatch list, BUT the first entry does not have a
924 * tag, then we could deadlock. For that case, move entries with assigned
925 * driver tags to the front, leaving the set of tagged requests in the
926 * same order, and the untagged set in the same order.
927 */
928static bool reorder_tags_to_front(struct list_head *list)
929{
930 struct request *rq, *tmp, *first = NULL;
931
932 list_for_each_entry_safe_reverse(rq, tmp, list, queuelist) {
933 if (rq == first)
934 break;
935 if (rq->tag != -1) {
936 list_move(&rq->queuelist, list);
937 if (!first)
938 first = rq;
939 }
940 }
941
942 return first != NULL;
943}
944
Ingo Molnarac6424b2017-06-20 12:06:13 +0200945static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode, int flags,
Omar Sandovalda55f2c2017-02-22 10:58:29 -0800946 void *key)
947{
948 struct blk_mq_hw_ctx *hctx;
949
950 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
951
Ingo Molnar2055da92017-06-20 12:06:46 +0200952 list_del(&wait->entry);
Omar Sandovalda55f2c2017-02-22 10:58:29 -0800953 clear_bit_unlock(BLK_MQ_S_TAG_WAITING, &hctx->state);
954 blk_mq_run_hw_queue(hctx, true);
955 return 1;
956}
957
958static bool blk_mq_dispatch_wait_add(struct blk_mq_hw_ctx *hctx)
959{
960 struct sbq_wait_state *ws;
961
962 /*
963 * The TAG_WAITING bit serves as a lock protecting hctx->dispatch_wait.
964 * The thread which wins the race to grab this bit adds the hardware
965 * queue to the wait queue.
966 */
967 if (test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state) ||
968 test_and_set_bit_lock(BLK_MQ_S_TAG_WAITING, &hctx->state))
969 return false;
970
971 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
972 ws = bt_wait_ptr(&hctx->tags->bitmap_tags, hctx);
973
974 /*
975 * As soon as this returns, it's no longer safe to fiddle with
976 * hctx->dispatch_wait, since a completion can wake up the wait queue
977 * and unlock the bit.
978 */
979 add_wait_queue(&ws->wait, &hctx->dispatch_wait);
980 return true;
981}
982
Omar Sandoval81380ca2017-04-07 08:56:26 -0600983bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list)
Jens Axboef04c3df2016-12-07 08:41:17 -0700984{
Omar Sandoval81380ca2017-04-07 08:56:26 -0600985 struct blk_mq_hw_ctx *hctx;
Jens Axboef04c3df2016-12-07 08:41:17 -0700986 struct request *rq;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200987 int errors, queued;
Jens Axboef04c3df2016-12-07 08:41:17 -0700988
Omar Sandoval81380ca2017-04-07 08:56:26 -0600989 if (list_empty(list))
990 return false;
991
Jens Axboef04c3df2016-12-07 08:41:17 -0700992 /*
Jens Axboef04c3df2016-12-07 08:41:17 -0700993 * Now process all the entries, sending them to the driver.
994 */
Jens Axboe93efe982017-03-24 12:04:19 -0600995 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -0600996 do {
Jens Axboef04c3df2016-12-07 08:41:17 -0700997 struct blk_mq_queue_data bd;
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200998 blk_status_t ret;
Jens Axboef04c3df2016-12-07 08:41:17 -0700999
1000 rq = list_first_entry(list, struct request, queuelist);
Jens Axboebd166ef2017-01-17 06:03:22 -07001001 if (!blk_mq_get_driver_tag(rq, &hctx, false)) {
1002 if (!queued && reorder_tags_to_front(list))
1003 continue;
Jens Axboe3c782d62017-01-26 12:50:36 -07001004
1005 /*
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001006 * The initial allocation attempt failed, so we need to
1007 * rerun the hardware queue when a tag is freed.
Jens Axboe3c782d62017-01-26 12:50:36 -07001008 */
Omar Sandoval807b1042017-04-05 12:01:35 -07001009 if (!blk_mq_dispatch_wait_add(hctx))
Jens Axboe3c782d62017-01-26 12:50:36 -07001010 break;
Omar Sandoval807b1042017-04-05 12:01:35 -07001011
1012 /*
1013 * It's possible that a tag was freed in the window
1014 * between the allocation failure and adding the
1015 * hardware queue to the wait queue.
1016 */
1017 if (!blk_mq_get_driver_tag(rq, &hctx, false))
1018 break;
Jens Axboebd166ef2017-01-17 06:03:22 -07001019 }
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001020
Jens Axboef04c3df2016-12-07 08:41:17 -07001021 list_del_init(&rq->queuelist);
1022
1023 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001024
1025 /*
1026 * Flag last if we have no more requests, or if we have more
1027 * but can't assign a driver tag to it.
1028 */
1029 if (list_empty(list))
1030 bd.last = true;
1031 else {
1032 struct request *nxt;
1033
1034 nxt = list_first_entry(list, struct request, queuelist);
1035 bd.last = !blk_mq_get_driver_tag(nxt, NULL, false);
1036 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001037
1038 ret = q->mq_ops->queue_rq(hctx, &bd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001039 if (ret == BLK_STS_RESOURCE) {
Jens Axboe113285b2017-03-02 13:26:04 -07001040 blk_mq_put_driver_tag_hctx(hctx, rq);
Jens Axboef04c3df2016-12-07 08:41:17 -07001041 list_add(&rq->queuelist, list);
1042 __blk_mq_requeue_request(rq);
1043 break;
Jens Axboef04c3df2016-12-07 08:41:17 -07001044 }
1045
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001046 if (unlikely(ret != BLK_STS_OK)) {
1047 errors++;
1048 blk_mq_end_request(rq, BLK_STS_IOERR);
1049 continue;
1050 }
1051
1052 queued++;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001053 } while (!list_empty(list));
Jens Axboef04c3df2016-12-07 08:41:17 -07001054
1055 hctx->dispatched[queued_to_index(queued)]++;
1056
1057 /*
1058 * Any items that need requeuing? Stuff them into hctx->dispatch,
1059 * that is where we will continue on next queue run.
1060 */
1061 if (!list_empty(list)) {
Jens Axboe113285b2017-03-02 13:26:04 -07001062 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001063 * If an I/O scheduler has been configured and we got a driver
1064 * tag for the next request already, free it again.
Jens Axboe113285b2017-03-02 13:26:04 -07001065 */
1066 rq = list_first_entry(list, struct request, queuelist);
1067 blk_mq_put_driver_tag(rq);
1068
Jens Axboef04c3df2016-12-07 08:41:17 -07001069 spin_lock(&hctx->lock);
Jens Axboec13660a2017-01-26 12:40:07 -07001070 list_splice_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001071 spin_unlock(&hctx->lock);
1072
1073 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001074 * If SCHED_RESTART was set by the caller of this function and
1075 * it is no longer set that means that it was cleared by another
1076 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001077 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001078 * If TAG_WAITING is set that means that an I/O scheduler has
1079 * been configured and another thread is waiting for a driver
1080 * tag. To guarantee fairness, do not rerun this hardware queue
1081 * but let the other thread grab the driver tag.
Jens Axboebd166ef2017-01-17 06:03:22 -07001082 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001083 * If no I/O scheduler has been configured it is possible that
1084 * the hardware queue got stopped and restarted before requests
1085 * were pushed back onto the dispatch list. Rerun the queue to
1086 * avoid starvation. Notes:
1087 * - blk_mq_run_hw_queue() checks whether or not a queue has
1088 * been stopped before rerunning a queue.
1089 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001090 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001091 * and dm-rq.
Jens Axboebd166ef2017-01-17 06:03:22 -07001092 */
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001093 if (!blk_mq_sched_needs_restart(hctx) &&
1094 !test_bit(BLK_MQ_S_TAG_WAITING, &hctx->state))
Jens Axboebd166ef2017-01-17 06:03:22 -07001095 blk_mq_run_hw_queue(hctx, true);
Jens Axboef04c3df2016-12-07 08:41:17 -07001096 }
1097
Jens Axboe93efe982017-03-24 12:04:19 -06001098 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001099}
1100
Bart Van Assche6a83e742016-11-02 10:09:51 -06001101static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1102{
1103 int srcu_idx;
1104
1105 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1106 cpu_online(hctx->next_cpu));
1107
1108 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1109 rcu_read_lock();
Jens Axboebd166ef2017-01-17 06:03:22 -07001110 blk_mq_sched_dispatch_requests(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001111 rcu_read_unlock();
1112 } else {
Jens Axboebf4907c2017-03-30 12:30:39 -06001113 might_sleep();
1114
Bart Van Assche07319672017-06-20 11:15:38 -07001115 srcu_idx = srcu_read_lock(hctx->queue_rq_srcu);
Jens Axboebd166ef2017-01-17 06:03:22 -07001116 blk_mq_sched_dispatch_requests(hctx);
Bart Van Assche07319672017-06-20 11:15:38 -07001117 srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001118 }
1119}
1120
Jens Axboe506e9312014-05-07 10:26:44 -06001121/*
1122 * It'd be great if the workqueue API had a way to pass
1123 * in a mask and had some smarts for more clever placement.
1124 * For now we just round-robin here, switching for every
1125 * BLK_MQ_CPU_WORK_BATCH queued items.
1126 */
1127static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1128{
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001129 if (hctx->queue->nr_hw_queues == 1)
1130 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001131
1132 if (--hctx->next_cpu_batch <= 0) {
Gabriel Krisman Bertazic02ebfd2016-09-28 00:24:24 -03001133 int next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001134
1135 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
1136 if (next_cpu >= nr_cpu_ids)
1137 next_cpu = cpumask_first(hctx->cpumask);
1138
1139 hctx->next_cpu = next_cpu;
1140 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1141 }
1142
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001143 return hctx->next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001144}
1145
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001146static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1147 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001148{
Bart Van Assche5435c022017-06-20 11:15:49 -07001149 if (WARN_ON_ONCE(!blk_mq_hw_queue_mapped(hctx)))
1150 return;
1151
1152 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001153 return;
1154
Jens Axboe1b792f22016-09-21 10:12:13 -06001155 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001156 int cpu = get_cpu();
1157 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001158 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001159 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001160 return;
1161 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001162
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001163 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001164 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001165
Jens Axboe9f993732017-04-10 09:54:54 -06001166 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1167 &hctx->run_work,
1168 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001169}
1170
1171void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1172{
1173 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1174}
1175EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1176
1177void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1178{
1179 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001180}
Omar Sandoval5b727272017-04-14 01:00:00 -07001181EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001182
Mike Snitzerb94ec292015-03-11 23:56:38 -04001183void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001184{
1185 struct blk_mq_hw_ctx *hctx;
1186 int i;
1187
1188 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboebd166ef2017-01-17 06:03:22 -07001189 if (!blk_mq_hctx_has_pending(hctx) ||
Bart Van Assche5d1b25c2016-10-28 17:19:15 -07001190 blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001191 continue;
1192
Mike Snitzerb94ec292015-03-11 23:56:38 -04001193 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001194 }
1195}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001196EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001197
Bart Van Asschefd001442016-10-28 17:19:37 -07001198/**
1199 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1200 * @q: request queue.
1201 *
1202 * The caller is responsible for serializing this function against
1203 * blk_mq_{start,stop}_hw_queue().
1204 */
1205bool blk_mq_queue_stopped(struct request_queue *q)
1206{
1207 struct blk_mq_hw_ctx *hctx;
1208 int i;
1209
1210 queue_for_each_hw_ctx(q, hctx, i)
1211 if (blk_mq_hctx_stopped(hctx))
1212 return true;
1213
1214 return false;
1215}
1216EXPORT_SYMBOL(blk_mq_queue_stopped);
1217
Ming Lei39a70c72017-06-06 23:22:09 +08001218/*
1219 * This function is often used for pausing .queue_rq() by driver when
1220 * there isn't enough resource or some conditions aren't satisfied, and
1221 * BLK_MQ_RQ_QUEUE_BUSY is usually returned.
1222 *
1223 * We do not guarantee that dispatch can be drained or blocked
1224 * after blk_mq_stop_hw_queue() returns. Please use
1225 * blk_mq_quiesce_queue() for that requirement.
1226 */
Jens Axboe320ae512013-10-24 09:20:05 +01001227void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1228{
Ming Lei641a9ed2017-06-06 23:22:10 +08001229 cancel_delayed_work(&hctx->run_work);
1230
1231 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001232}
1233EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1234
Ming Lei39a70c72017-06-06 23:22:09 +08001235/*
1236 * This function is often used for pausing .queue_rq() by driver when
1237 * there isn't enough resource or some conditions aren't satisfied, and
1238 * BLK_MQ_RQ_QUEUE_BUSY is usually returned.
1239 *
1240 * We do not guarantee that dispatch can be drained or blocked
1241 * after blk_mq_stop_hw_queues() returns. Please use
1242 * blk_mq_quiesce_queue() for that requirement.
1243 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001244void blk_mq_stop_hw_queues(struct request_queue *q)
1245{
Ming Lei641a9ed2017-06-06 23:22:10 +08001246 struct blk_mq_hw_ctx *hctx;
1247 int i;
1248
1249 queue_for_each_hw_ctx(q, hctx, i)
1250 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001251}
1252EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1253
Jens Axboe320ae512013-10-24 09:20:05 +01001254void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1255{
1256 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001257
Jens Axboe0ffbce82014-06-25 08:22:34 -06001258 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001259}
1260EXPORT_SYMBOL(blk_mq_start_hw_queue);
1261
Christoph Hellwig2f268552014-04-16 09:44:56 +02001262void blk_mq_start_hw_queues(struct request_queue *q)
1263{
1264 struct blk_mq_hw_ctx *hctx;
1265 int i;
1266
1267 queue_for_each_hw_ctx(q, hctx, i)
1268 blk_mq_start_hw_queue(hctx);
1269}
1270EXPORT_SYMBOL(blk_mq_start_hw_queues);
1271
Jens Axboeae911c52016-12-08 13:19:30 -07001272void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1273{
1274 if (!blk_mq_hctx_stopped(hctx))
1275 return;
1276
1277 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1278 blk_mq_run_hw_queue(hctx, async);
1279}
1280EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1281
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001282void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001283{
1284 struct blk_mq_hw_ctx *hctx;
1285 int i;
1286
Jens Axboeae911c52016-12-08 13:19:30 -07001287 queue_for_each_hw_ctx(q, hctx, i)
1288 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001289}
1290EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1291
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001292static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001293{
1294 struct blk_mq_hw_ctx *hctx;
1295
Jens Axboe9f993732017-04-10 09:54:54 -06001296 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001297
1298 /*
1299 * If we are stopped, don't run the queue. The exception is if
1300 * BLK_MQ_S_START_ON_RUN is set. For that case, we auto-clear
1301 * the STOPPED bit and run it.
1302 */
1303 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state)) {
1304 if (!test_bit(BLK_MQ_S_START_ON_RUN, &hctx->state))
1305 return;
1306
1307 clear_bit(BLK_MQ_S_START_ON_RUN, &hctx->state);
1308 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1309 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001310
Jens Axboe320ae512013-10-24 09:20:05 +01001311 __blk_mq_run_hw_queue(hctx);
1312}
1313
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001314
1315void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1316{
Bart Van Assche5435c022017-06-20 11:15:49 -07001317 if (WARN_ON_ONCE(!blk_mq_hw_queue_mapped(hctx)))
Ming Lei19c66e52014-12-03 19:38:04 +08001318 return;
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001319
Jens Axboe21c6e932017-04-10 09:54:56 -06001320 /*
1321 * Stop the hw queue, then modify currently delayed work.
1322 * This should prevent us from running the queue prematurely.
1323 * Mark the queue as auto-clearing STOPPED when it runs.
1324 */
Jens Axboe7e79dad2017-01-19 07:58:59 -07001325 blk_mq_stop_hw_queue(hctx);
Jens Axboe21c6e932017-04-10 09:54:56 -06001326 set_bit(BLK_MQ_S_START_ON_RUN, &hctx->state);
1327 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1328 &hctx->run_work,
1329 msecs_to_jiffies(msecs));
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001330}
1331EXPORT_SYMBOL(blk_mq_delay_queue);
1332
Ming Leicfd0c552015-10-20 23:13:57 +08001333static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001334 struct request *rq,
1335 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001336{
Jens Axboee57690f2016-08-24 15:34:35 -06001337 struct blk_mq_ctx *ctx = rq->mq_ctx;
1338
Bart Van Assche7b607812017-06-20 11:15:47 -07001339 lockdep_assert_held(&ctx->lock);
1340
Jens Axboe01b983c2013-11-19 18:59:10 -07001341 trace_block_rq_insert(hctx->queue, rq);
1342
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001343 if (at_head)
1344 list_add(&rq->queuelist, &ctx->rq_list);
1345 else
1346 list_add_tail(&rq->queuelist, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001347}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001348
Jens Axboe2c3ad662016-12-14 14:34:47 -07001349void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1350 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001351{
1352 struct blk_mq_ctx *ctx = rq->mq_ctx;
1353
Bart Van Assche7b607812017-06-20 11:15:47 -07001354 lockdep_assert_held(&ctx->lock);
1355
Jens Axboee57690f2016-08-24 15:34:35 -06001356 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001357 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001358}
1359
Jens Axboebd166ef2017-01-17 06:03:22 -07001360void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1361 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001362
1363{
Jens Axboe320ae512013-10-24 09:20:05 +01001364 /*
1365 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1366 * offline now
1367 */
1368 spin_lock(&ctx->lock);
1369 while (!list_empty(list)) {
1370 struct request *rq;
1371
1372 rq = list_first_entry(list, struct request, queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001373 BUG_ON(rq->mq_ctx != ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001374 list_del_init(&rq->queuelist);
Jens Axboee57690f2016-08-24 15:34:35 -06001375 __blk_mq_insert_req_list(hctx, rq, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001376 }
Ming Leicfd0c552015-10-20 23:13:57 +08001377 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001378 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001379}
1380
1381static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1382{
1383 struct request *rqa = container_of(a, struct request, queuelist);
1384 struct request *rqb = container_of(b, struct request, queuelist);
1385
1386 return !(rqa->mq_ctx < rqb->mq_ctx ||
1387 (rqa->mq_ctx == rqb->mq_ctx &&
1388 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1389}
1390
1391void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1392{
1393 struct blk_mq_ctx *this_ctx;
1394 struct request_queue *this_q;
1395 struct request *rq;
1396 LIST_HEAD(list);
1397 LIST_HEAD(ctx_list);
1398 unsigned int depth;
1399
1400 list_splice_init(&plug->mq_list, &list);
1401
1402 list_sort(NULL, &list, plug_ctx_cmp);
1403
1404 this_q = NULL;
1405 this_ctx = NULL;
1406 depth = 0;
1407
1408 while (!list_empty(&list)) {
1409 rq = list_entry_rq(list.next);
1410 list_del_init(&rq->queuelist);
1411 BUG_ON(!rq->q);
1412 if (rq->mq_ctx != this_ctx) {
1413 if (this_ctx) {
Jens Axboebd166ef2017-01-17 06:03:22 -07001414 trace_block_unplug(this_q, depth, from_schedule);
1415 blk_mq_sched_insert_requests(this_q, this_ctx,
1416 &ctx_list,
1417 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001418 }
1419
1420 this_ctx = rq->mq_ctx;
1421 this_q = rq->q;
1422 depth = 0;
1423 }
1424
1425 depth++;
1426 list_add_tail(&rq->queuelist, &ctx_list);
1427 }
1428
1429 /*
1430 * If 'this_ctx' is set, we know we have entries to complete
1431 * on 'ctx_list'. Do those.
1432 */
1433 if (this_ctx) {
Jens Axboebd166ef2017-01-17 06:03:22 -07001434 trace_block_unplug(this_q, depth, from_schedule);
1435 blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list,
1436 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001437 }
1438}
1439
1440static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1441{
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001442 blk_init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001443
Jens Axboe6e85eaf2016-12-02 20:00:14 -07001444 blk_account_io_start(rq, true);
Jens Axboe320ae512013-10-24 09:20:05 +01001445}
1446
Jens Axboe274a5842014-08-15 12:44:08 -06001447static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1448{
1449 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1450 !blk_queue_nomerges(hctx->queue);
1451}
1452
Ming Leiab42f352017-05-26 19:53:19 +08001453static inline void blk_mq_queue_io(struct blk_mq_hw_ctx *hctx,
1454 struct blk_mq_ctx *ctx,
1455 struct request *rq)
1456{
1457 spin_lock(&ctx->lock);
1458 __blk_mq_insert_request(hctx, rq, false);
1459 spin_unlock(&ctx->lock);
Jens Axboe07068d52014-05-22 10:40:51 -06001460}
1461
Jens Axboefd2d3322017-01-12 10:04:45 -07001462static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
1463{
Jens Axboebd166ef2017-01-17 06:03:22 -07001464 if (rq->tag != -1)
1465 return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
1466
1467 return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
Jens Axboefd2d3322017-01-12 10:04:45 -07001468}
1469
Ming Leid964f042017-06-06 23:22:00 +08001470static void __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1471 struct request *rq,
1472 blk_qc_t *cookie, bool may_sleep)
Shaohua Lif984df12015-05-08 10:51:32 -07001473{
Shaohua Lif984df12015-05-08 10:51:32 -07001474 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001475 struct blk_mq_queue_data bd = {
1476 .rq = rq,
Omar Sandovald945a362017-04-05 12:01:36 -07001477 .last = true,
Shaohua Lif984df12015-05-08 10:51:32 -07001478 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001479 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001480 blk_status_t ret;
Ming Leid964f042017-06-06 23:22:00 +08001481 bool run_queue = true;
1482
Ming Leif4560ff2017-06-18 14:24:27 -06001483 /* RCU or SRCU read lock is needed before checking quiesced flag */
1484 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08001485 run_queue = false;
1486 goto insert;
1487 }
Shaohua Lif984df12015-05-08 10:51:32 -07001488
Jens Axboebd166ef2017-01-17 06:03:22 -07001489 if (q->elevator)
Bart Van Assche2253efc2016-10-28 17:20:02 -07001490 goto insert;
1491
Ming Leid964f042017-06-06 23:22:00 +08001492 if (!blk_mq_get_driver_tag(rq, NULL, false))
Jens Axboebd166ef2017-01-17 06:03:22 -07001493 goto insert;
1494
1495 new_cookie = request_to_qc_t(hctx, rq);
1496
Shaohua Lif984df12015-05-08 10:51:32 -07001497 /*
1498 * For OK queue, we are done. For error, kill it. Any other
1499 * error (busy), just add it to our list as we previously
1500 * would have done
1501 */
1502 ret = q->mq_ops->queue_rq(hctx, &bd);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001503 switch (ret) {
1504 case BLK_STS_OK:
Jens Axboe7b371632015-11-05 10:41:40 -07001505 *cookie = new_cookie;
Bart Van Assche2253efc2016-10-28 17:20:02 -07001506 return;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001507 case BLK_STS_RESOURCE:
1508 __blk_mq_requeue_request(rq);
1509 goto insert;
1510 default:
Jens Axboe7b371632015-11-05 10:41:40 -07001511 *cookie = BLK_QC_T_NONE;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001512 blk_mq_end_request(rq, ret);
Bart Van Assche2253efc2016-10-28 17:20:02 -07001513 return;
Jens Axboe7b371632015-11-05 10:41:40 -07001514 }
1515
Bart Van Assche2253efc2016-10-28 17:20:02 -07001516insert:
Ming Leid964f042017-06-06 23:22:00 +08001517 blk_mq_sched_insert_request(rq, false, run_queue, false, may_sleep);
Shaohua Lif984df12015-05-08 10:51:32 -07001518}
1519
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001520static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1521 struct request *rq, blk_qc_t *cookie)
1522{
1523 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1524 rcu_read_lock();
Ming Leid964f042017-06-06 23:22:00 +08001525 __blk_mq_try_issue_directly(hctx, rq, cookie, false);
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001526 rcu_read_unlock();
1527 } else {
Jens Axboebf4907c2017-03-30 12:30:39 -06001528 unsigned int srcu_idx;
1529
1530 might_sleep();
1531
Bart Van Assche07319672017-06-20 11:15:38 -07001532 srcu_idx = srcu_read_lock(hctx->queue_rq_srcu);
Ming Leid964f042017-06-06 23:22:00 +08001533 __blk_mq_try_issue_directly(hctx, rq, cookie, true);
Bart Van Assche07319672017-06-20 11:15:38 -07001534 srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx);
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001535 }
1536}
1537
Jens Axboedece1632015-11-05 10:41:16 -07001538static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001539{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001540 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001541 const int is_flush_fua = op_is_flush(bio->bi_opf);
Jens Axboe5a797e02017-01-26 12:22:11 -07001542 struct blk_mq_alloc_data data = { .flags = 0 };
Jens Axboe07068d52014-05-22 10:40:51 -06001543 struct request *rq;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001544 unsigned int request_count = 0;
Shaohua Lif984df12015-05-08 10:51:32 -07001545 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001546 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001547 blk_qc_t cookie;
Jens Axboe87760e52016-11-09 12:38:14 -07001548 unsigned int wb_acct;
Jens Axboe07068d52014-05-22 10:40:51 -06001549
1550 blk_queue_bounce(q, &bio);
1551
NeilBrownaf67c312017-06-18 14:38:57 +10001552 blk_queue_split(q, &bio);
Wen Xiongf36ea502017-05-10 08:54:11 -05001553
Dmitry Monakhove23947b2017-06-29 11:31:11 -07001554 if (!bio_integrity_prep(bio))
Jens Axboedece1632015-11-05 10:41:16 -07001555 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001556
Omar Sandoval87c279e2016-06-01 22:18:48 -07001557 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1558 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1559 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001560
Jens Axboebd166ef2017-01-17 06:03:22 -07001561 if (blk_mq_sched_bio_merge(q, bio))
1562 return BLK_QC_T_NONE;
1563
Jens Axboe87760e52016-11-09 12:38:14 -07001564 wb_acct = wbt_wait(q->rq_wb, bio, NULL);
1565
Jens Axboebd166ef2017-01-17 06:03:22 -07001566 trace_block_getrq(q, bio, bio->bi_opf);
1567
Christoph Hellwigd2c0d382017-06-16 18:15:19 +02001568 rq = blk_mq_get_request(q, bio, bio->bi_opf, &data);
Jens Axboe87760e52016-11-09 12:38:14 -07001569 if (unlikely(!rq)) {
1570 __wbt_done(q->rq_wb, wb_acct);
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05001571 if (bio->bi_opf & REQ_NOWAIT)
1572 bio_wouldblock_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001573 return BLK_QC_T_NONE;
Jens Axboe87760e52016-11-09 12:38:14 -07001574 }
1575
1576 wbt_track(&rq->issue_stat, wb_acct);
Jens Axboe07068d52014-05-22 10:40:51 -06001577
Jens Axboefd2d3322017-01-12 10:04:45 -07001578 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06001579
Shaohua Lif984df12015-05-08 10:51:32 -07001580 plug = current->plug;
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001581 if (unlikely(is_flush_fua)) {
Shaohua Lif984df12015-05-08 10:51:32 -07001582 blk_mq_put_ctx(data.ctx);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001583 blk_mq_bio_to_request(rq, bio);
1584 if (q->elevator) {
1585 blk_mq_sched_insert_request(rq, false, true, true,
1586 true);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001587 } else {
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001588 blk_insert_flush(rq);
1589 blk_mq_run_hw_queue(data.hctx, true);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001590 }
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001591 } else if (plug && q->nr_hw_queues == 1) {
Shaohua Li600271d2016-11-03 17:03:54 -07001592 struct request *last = NULL;
1593
Jens Axboeb00c53e2017-04-20 16:40:36 -06001594 blk_mq_put_ctx(data.ctx);
Jeff Moyere6c44382015-05-08 10:51:30 -07001595 blk_mq_bio_to_request(rq, bio);
Ming Lei0a6219a2016-11-16 18:07:05 +08001596
1597 /*
1598 * @request_count may become stale because of schedule
1599 * out, so check the list again.
1600 */
1601 if (list_empty(&plug->mq_list))
1602 request_count = 0;
Christoph Hellwig254d2592017-03-22 15:01:50 -04001603 else if (blk_queue_nomerges(q))
1604 request_count = blk_plug_queued_count(q);
1605
Ming Lei676d0602015-10-20 23:13:56 +08001606 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001607 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07001608 else
1609 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07001610
Shaohua Li600271d2016-11-03 17:03:54 -07001611 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
1612 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001613 blk_flush_plug_list(plug, false);
1614 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001615 }
Jens Axboeb094f892015-11-20 20:29:45 -07001616
Jeff Moyere6c44382015-05-08 10:51:30 -07001617 list_add_tail(&rq->queuelist, &plug->mq_list);
Christoph Hellwig22997222017-03-22 15:01:52 -04001618 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01001619 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001620
Jens Axboe320ae512013-10-24 09:20:05 +01001621 /*
1622 * We do limited plugging. If the bio can be merged, do that.
1623 * Otherwise the existing request in the plug list will be
1624 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04001625 * The plug list might get flushed before this. If that happens,
1626 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01001627 */
Christoph Hellwig22997222017-03-22 15:01:52 -04001628 if (list_empty(&plug->mq_list))
1629 same_queue_rq = NULL;
1630 if (same_queue_rq)
1631 list_del_init(&same_queue_rq->queuelist);
1632 list_add_tail(&rq->queuelist, &plug->mq_list);
1633
Jens Axboebf4907c2017-03-30 12:30:39 -06001634 blk_mq_put_ctx(data.ctx);
1635
Ming Leidad7a3b2017-06-06 23:21:59 +08001636 if (same_queue_rq) {
1637 data.hctx = blk_mq_map_queue(q,
1638 same_queue_rq->mq_ctx->cpu);
Christoph Hellwig22997222017-03-22 15:01:52 -04001639 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
1640 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08001641 }
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001642 } else if (q->nr_hw_queues > 1 && is_sync) {
Jens Axboebd166ef2017-01-17 06:03:22 -07001643 blk_mq_put_ctx(data.ctx);
1644 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig22997222017-03-22 15:01:52 -04001645 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001646 } else if (q->elevator) {
Jens Axboeb00c53e2017-04-20 16:40:36 -06001647 blk_mq_put_ctx(data.ctx);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001648 blk_mq_bio_to_request(rq, bio);
1649 blk_mq_sched_insert_request(rq, false, true, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08001650 } else {
Jens Axboeb00c53e2017-04-20 16:40:36 -06001651 blk_mq_put_ctx(data.ctx);
Ming Leiab42f352017-05-26 19:53:19 +08001652 blk_mq_bio_to_request(rq, bio);
1653 blk_mq_queue_io(data.hctx, data.ctx, rq);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001654 blk_mq_run_hw_queue(data.hctx, true);
Ming Leiab42f352017-05-26 19:53:19 +08001655 }
Jens Axboe320ae512013-10-24 09:20:05 +01001656
Jens Axboe7b371632015-11-05 10:41:40 -07001657 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001658}
1659
Jens Axboecc71a6f2017-01-11 14:29:56 -07001660void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1661 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001662{
1663 struct page *page;
1664
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001665 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001666 int i;
1667
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001668 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001669 struct request *rq = tags->static_rqs[i];
1670
1671 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001672 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001673 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001674 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001675 }
1676 }
1677
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001678 while (!list_empty(&tags->page_list)) {
1679 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001680 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001681 /*
1682 * Remove kmemleak object previously allocated in
1683 * blk_mq_init_rq_map().
1684 */
1685 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01001686 __free_pages(page, page->private);
1687 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07001688}
Jens Axboe320ae512013-10-24 09:20:05 +01001689
Jens Axboecc71a6f2017-01-11 14:29:56 -07001690void blk_mq_free_rq_map(struct blk_mq_tags *tags)
1691{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001692 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07001693 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001694 kfree(tags->static_rqs);
1695 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001696
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001697 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001698}
1699
Jens Axboecc71a6f2017-01-11 14:29:56 -07001700struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
1701 unsigned int hctx_idx,
1702 unsigned int nr_tags,
1703 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01001704{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001705 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08001706 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01001707
Shaohua Li59f082e2017-02-01 09:53:14 -08001708 node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
1709 if (node == NUMA_NO_NODE)
1710 node = set->numa_node;
1711
1712 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
Shaohua Li24391c02015-01-23 14:18:00 -07001713 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001714 if (!tags)
1715 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001716
Jens Axboecc71a6f2017-01-11 14:29:56 -07001717 tags->rqs = kzalloc_node(nr_tags * sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02001718 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08001719 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001720 if (!tags->rqs) {
1721 blk_mq_free_tags(tags);
1722 return NULL;
1723 }
Jens Axboe320ae512013-10-24 09:20:05 +01001724
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001725 tags->static_rqs = kzalloc_node(nr_tags * sizeof(struct request *),
1726 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08001727 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001728 if (!tags->static_rqs) {
1729 kfree(tags->rqs);
1730 blk_mq_free_tags(tags);
1731 return NULL;
1732 }
1733
Jens Axboecc71a6f2017-01-11 14:29:56 -07001734 return tags;
1735}
1736
1737static size_t order_to_size(unsigned int order)
1738{
1739 return (size_t)PAGE_SIZE << order;
1740}
1741
1742int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1743 unsigned int hctx_idx, unsigned int depth)
1744{
1745 unsigned int i, j, entries_per_page, max_order = 4;
1746 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08001747 int node;
1748
1749 node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
1750 if (node == NUMA_NO_NODE)
1751 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07001752
1753 INIT_LIST_HEAD(&tags->page_list);
1754
Jens Axboe320ae512013-10-24 09:20:05 +01001755 /*
1756 * rq_size is the size of the request plus driver payload, rounded
1757 * to the cacheline size
1758 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001759 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01001760 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07001761 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01001762
Jens Axboecc71a6f2017-01-11 14:29:56 -07001763 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01001764 int this_order = max_order;
1765 struct page *page;
1766 int to_do;
1767 void *p;
1768
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06001769 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01001770 this_order--;
1771
1772 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08001773 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02001774 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06001775 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01001776 if (page)
1777 break;
1778 if (!this_order--)
1779 break;
1780 if (order_to_size(this_order) < rq_size)
1781 break;
1782 } while (1);
1783
1784 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001785 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01001786
1787 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001788 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001789
1790 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001791 /*
1792 * Allow kmemleak to scan these pages as they contain pointers
1793 * to additional allocations like via ops->init_request().
1794 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02001795 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01001796 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07001797 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01001798 left -= to_do * rq_size;
1799 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001800 struct request *rq = p;
1801
1802 tags->static_rqs[i] = rq;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001803 if (set->ops->init_request) {
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001804 if (set->ops->init_request(set, rq, hctx_idx,
Shaohua Li59f082e2017-02-01 09:53:14 -08001805 node)) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001806 tags->static_rqs[i] = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001807 goto fail;
Jens Axboea5164402014-09-10 09:02:03 -06001808 }
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001809 }
1810
Jens Axboe320ae512013-10-24 09:20:05 +01001811 p += rq_size;
1812 i++;
1813 }
1814 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07001815 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01001816
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001817fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07001818 blk_mq_free_rqs(set, tags, hctx_idx);
1819 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01001820}
1821
Jens Axboee57690f2016-08-24 15:34:35 -06001822/*
1823 * 'cpu' is going away. splice any existing rq_list entries from this
1824 * software queue to the hw queue dispatch list, and ensure that it
1825 * gets run.
1826 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06001827static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06001828{
Thomas Gleixner9467f852016-09-22 08:05:17 -06001829 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06001830 struct blk_mq_ctx *ctx;
1831 LIST_HEAD(tmp);
1832
Thomas Gleixner9467f852016-09-22 08:05:17 -06001833 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Jens Axboee57690f2016-08-24 15:34:35 -06001834 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Jens Axboe484b4062014-05-21 14:01:15 -06001835
1836 spin_lock(&ctx->lock);
1837 if (!list_empty(&ctx->rq_list)) {
1838 list_splice_init(&ctx->rq_list, &tmp);
1839 blk_mq_hctx_clear_pending(hctx, ctx);
1840 }
1841 spin_unlock(&ctx->lock);
1842
1843 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06001844 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06001845
Jens Axboee57690f2016-08-24 15:34:35 -06001846 spin_lock(&hctx->lock);
1847 list_splice_tail_init(&tmp, &hctx->dispatch);
1848 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06001849
1850 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06001851 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06001852}
1853
Thomas Gleixner9467f852016-09-22 08:05:17 -06001854static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06001855{
Thomas Gleixner9467f852016-09-22 08:05:17 -06001856 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
1857 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06001858}
1859
Ming Leic3b4afc2015-06-04 22:25:04 +08001860/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08001861static void blk_mq_exit_hctx(struct request_queue *q,
1862 struct blk_mq_tag_set *set,
1863 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1864{
Omar Sandoval9c1051a2017-05-04 08:17:21 -06001865 blk_mq_debugfs_unregister_hctx(hctx);
1866
Ming Lei08e98fc2014-09-25 23:23:38 +08001867 blk_mq_tag_idle(hctx);
1868
Ming Leif70ced02014-09-25 23:23:47 +08001869 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001870 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08001871
Omar Sandoval93252632017-04-05 12:01:31 -07001872 blk_mq_sched_exit_hctx(q, hctx, hctx_idx);
1873
Ming Lei08e98fc2014-09-25 23:23:38 +08001874 if (set->ops->exit_hctx)
1875 set->ops->exit_hctx(hctx, hctx_idx);
1876
Bart Van Assche6a83e742016-11-02 10:09:51 -06001877 if (hctx->flags & BLK_MQ_F_BLOCKING)
Bart Van Assche07319672017-06-20 11:15:38 -07001878 cleanup_srcu_struct(hctx->queue_rq_srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001879
Thomas Gleixner9467f852016-09-22 08:05:17 -06001880 blk_mq_remove_cpuhp(hctx);
Ming Leif70ced02014-09-25 23:23:47 +08001881 blk_free_flush_queue(hctx->fq);
Omar Sandoval88459642016-09-17 08:38:44 -06001882 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08001883}
1884
Ming Lei624dbe42014-05-27 23:35:13 +08001885static void blk_mq_exit_hw_queues(struct request_queue *q,
1886 struct blk_mq_tag_set *set, int nr_queue)
1887{
1888 struct blk_mq_hw_ctx *hctx;
1889 unsigned int i;
1890
1891 queue_for_each_hw_ctx(q, hctx, i) {
1892 if (i == nr_queue)
1893 break;
Ming Lei08e98fc2014-09-25 23:23:38 +08001894 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08001895 }
Ming Lei624dbe42014-05-27 23:35:13 +08001896}
1897
Ming Lei08e98fc2014-09-25 23:23:38 +08001898static int blk_mq_init_hctx(struct request_queue *q,
1899 struct blk_mq_tag_set *set,
1900 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1901{
1902 int node;
1903
1904 node = hctx->numa_node;
1905 if (node == NUMA_NO_NODE)
1906 node = hctx->numa_node = set->numa_node;
1907
Jens Axboe9f993732017-04-10 09:54:54 -06001908 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08001909 spin_lock_init(&hctx->lock);
1910 INIT_LIST_HEAD(&hctx->dispatch);
1911 hctx->queue = q;
Jeff Moyer2404e602015-11-03 10:40:06 -05001912 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08001913
Thomas Gleixner9467f852016-09-22 08:05:17 -06001914 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
Ming Lei08e98fc2014-09-25 23:23:38 +08001915
1916 hctx->tags = set->tags[hctx_idx];
1917
1918 /*
1919 * Allocate space for all possible cpus to avoid allocation at
1920 * runtime
1921 */
1922 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1923 GFP_KERNEL, node);
1924 if (!hctx->ctxs)
1925 goto unregister_cpu_notifier;
1926
Omar Sandoval88459642016-09-17 08:38:44 -06001927 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8), GFP_KERNEL,
1928 node))
Ming Lei08e98fc2014-09-25 23:23:38 +08001929 goto free_ctxs;
1930
1931 hctx->nr_ctx = 0;
1932
1933 if (set->ops->init_hctx &&
1934 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1935 goto free_bitmap;
1936
Omar Sandoval93252632017-04-05 12:01:31 -07001937 if (blk_mq_sched_init_hctx(q, hctx, hctx_idx))
1938 goto exit_hctx;
1939
Ming Leif70ced02014-09-25 23:23:47 +08001940 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1941 if (!hctx->fq)
Omar Sandoval93252632017-04-05 12:01:31 -07001942 goto sched_exit_hctx;
Ming Leif70ced02014-09-25 23:23:47 +08001943
1944 if (set->ops->init_request &&
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001945 set->ops->init_request(set, hctx->fq->flush_rq, hctx_idx,
1946 node))
Ming Leif70ced02014-09-25 23:23:47 +08001947 goto free_fq;
1948
Bart Van Assche6a83e742016-11-02 10:09:51 -06001949 if (hctx->flags & BLK_MQ_F_BLOCKING)
Bart Van Assche07319672017-06-20 11:15:38 -07001950 init_srcu_struct(hctx->queue_rq_srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001951
Omar Sandoval9c1051a2017-05-04 08:17:21 -06001952 blk_mq_debugfs_register_hctx(q, hctx);
1953
Ming Lei08e98fc2014-09-25 23:23:38 +08001954 return 0;
1955
Ming Leif70ced02014-09-25 23:23:47 +08001956 free_fq:
1957 kfree(hctx->fq);
Omar Sandoval93252632017-04-05 12:01:31 -07001958 sched_exit_hctx:
1959 blk_mq_sched_exit_hctx(q, hctx, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08001960 exit_hctx:
1961 if (set->ops->exit_hctx)
1962 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001963 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06001964 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08001965 free_ctxs:
1966 kfree(hctx->ctxs);
1967 unregister_cpu_notifier:
Thomas Gleixner9467f852016-09-22 08:05:17 -06001968 blk_mq_remove_cpuhp(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08001969 return -1;
1970}
1971
Jens Axboe320ae512013-10-24 09:20:05 +01001972static void blk_mq_init_cpu_queues(struct request_queue *q,
1973 unsigned int nr_hw_queues)
1974{
1975 unsigned int i;
1976
1977 for_each_possible_cpu(i) {
1978 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1979 struct blk_mq_hw_ctx *hctx;
1980
Jens Axboe320ae512013-10-24 09:20:05 +01001981 __ctx->cpu = i;
1982 spin_lock_init(&__ctx->lock);
1983 INIT_LIST_HEAD(&__ctx->rq_list);
1984 __ctx->queue = q;
1985
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02001986 /* If the cpu isn't present, the cpu is mapped to first hctx */
1987 if (!cpu_present(i))
Jens Axboe320ae512013-10-24 09:20:05 +01001988 continue;
1989
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02001990 hctx = blk_mq_map_queue(q, i);
Jens Axboee4043dc2014-04-09 10:18:23 -06001991
Jens Axboe320ae512013-10-24 09:20:05 +01001992 /*
1993 * Set local node, IFF we have more than one hw queue. If
1994 * not, we remain on the home node of the device
1995 */
1996 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Raghavendra K Tbffed452015-12-02 16:59:05 +05301997 hctx->numa_node = local_memory_node(cpu_to_node(i));
Jens Axboe320ae512013-10-24 09:20:05 +01001998 }
1999}
2000
Jens Axboecc71a6f2017-01-11 14:29:56 -07002001static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
2002{
2003 int ret = 0;
2004
2005 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2006 set->queue_depth, set->reserved_tags);
2007 if (!set->tags[hctx_idx])
2008 return false;
2009
2010 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2011 set->queue_depth);
2012 if (!ret)
2013 return true;
2014
2015 blk_mq_free_rq_map(set->tags[hctx_idx]);
2016 set->tags[hctx_idx] = NULL;
2017 return false;
2018}
2019
2020static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2021 unsigned int hctx_idx)
2022{
Jens Axboebd166ef2017-01-17 06:03:22 -07002023 if (set->tags[hctx_idx]) {
2024 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2025 blk_mq_free_rq_map(set->tags[hctx_idx]);
2026 set->tags[hctx_idx] = NULL;
2027 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002028}
2029
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002030static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002031{
Gabriel Krisman Bertazid1b1cea2016-12-14 18:48:36 -02002032 unsigned int i, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002033 struct blk_mq_hw_ctx *hctx;
2034 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002035 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002036
Akinobu Mita60de0742015-09-27 02:09:25 +09002037 /*
2038 * Avoid others reading imcomplete hctx->cpumask through sysfs
2039 */
2040 mutex_lock(&q->sysfs_lock);
2041
Jens Axboe320ae512013-10-24 09:20:05 +01002042 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002043 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002044 hctx->nr_ctx = 0;
2045 }
2046
2047 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002048 * Map software to hardware queues.
2049 *
2050 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002051 */
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002052 for_each_present_cpu(i) {
Gabriel Krisman Bertazid1b1cea2016-12-14 18:48:36 -02002053 hctx_idx = q->mq_map[i];
2054 /* unmapped hw queue can be remapped after CPU topo changed */
Jens Axboecc71a6f2017-01-11 14:29:56 -07002055 if (!set->tags[hctx_idx] &&
2056 !__blk_mq_alloc_rq_map(set, hctx_idx)) {
Gabriel Krisman Bertazid1b1cea2016-12-14 18:48:36 -02002057 /*
2058 * If tags initialization fail for some hctx,
2059 * that hctx won't be brought online. In this
2060 * case, remap the current ctx to hctx[0] which
2061 * is guaranteed to always have tags allocated
2062 */
Jens Axboecc71a6f2017-01-11 14:29:56 -07002063 q->mq_map[i] = 0;
Gabriel Krisman Bertazid1b1cea2016-12-14 18:48:36 -02002064 }
2065
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002066 ctx = per_cpu_ptr(q->queue_ctx, i);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002067 hctx = blk_mq_map_queue(q, i);
Keith Busch868f2f02015-12-17 17:08:14 -07002068
Jens Axboee4043dc2014-04-09 10:18:23 -06002069 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002070 ctx->index_hw = hctx->nr_ctx;
2071 hctx->ctxs[hctx->nr_ctx++] = ctx;
2072 }
Jens Axboe506e9312014-05-07 10:26:44 -06002073
Akinobu Mita60de0742015-09-27 02:09:25 +09002074 mutex_unlock(&q->sysfs_lock);
2075
Jens Axboe506e9312014-05-07 10:26:44 -06002076 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe484b4062014-05-21 14:01:15 -06002077 /*
Jens Axboea68aafa2014-08-15 13:19:15 -06002078 * If no software queues are mapped to this hardware queue,
2079 * disable it and free the request entries.
Jens Axboe484b4062014-05-21 14:01:15 -06002080 */
2081 if (!hctx->nr_ctx) {
Gabriel Krisman Bertazid1b1cea2016-12-14 18:48:36 -02002082 /* Never unmap queue 0. We need it as a
2083 * fallback in case of a new remap fails
2084 * allocation
2085 */
Jens Axboecc71a6f2017-01-11 14:29:56 -07002086 if (i && set->tags[i])
2087 blk_mq_free_map_and_requests(set, i);
2088
Ming Lei2a34c082015-04-21 10:00:20 +08002089 hctx->tags = NULL;
Jens Axboe484b4062014-05-21 14:01:15 -06002090 continue;
2091 }
2092
Ming Lei2a34c082015-04-21 10:00:20 +08002093 hctx->tags = set->tags[i];
2094 WARN_ON(!hctx->tags);
2095
Jens Axboe484b4062014-05-21 14:01:15 -06002096 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002097 * Set the map size to the number of mapped software queues.
2098 * This is more accurate and more efficient than looping
2099 * over all possibly mapped software queues.
2100 */
Omar Sandoval88459642016-09-17 08:38:44 -06002101 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002102
2103 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002104 * Initialize batch roundrobin counts
2105 */
Jens Axboe506e9312014-05-07 10:26:44 -06002106 hctx->next_cpu = cpumask_first(hctx->cpumask);
2107 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2108 }
Jens Axboe320ae512013-10-24 09:20:05 +01002109}
2110
Jens Axboe8e8320c2017-06-20 17:56:13 -06002111/*
2112 * Caller needs to ensure that we're either frozen/quiesced, or that
2113 * the queue isn't live yet.
2114 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002115static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002116{
2117 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002118 int i;
2119
Jeff Moyer2404e602015-11-03 10:40:06 -05002120 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe8e8320c2017-06-20 17:56:13 -06002121 if (shared) {
2122 if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
2123 atomic_inc(&q->shared_hctx_restart);
Jeff Moyer2404e602015-11-03 10:40:06 -05002124 hctx->flags |= BLK_MQ_F_TAG_SHARED;
Jens Axboe8e8320c2017-06-20 17:56:13 -06002125 } else {
2126 if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
2127 atomic_dec(&q->shared_hctx_restart);
Jeff Moyer2404e602015-11-03 10:40:06 -05002128 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
Jens Axboe8e8320c2017-06-20 17:56:13 -06002129 }
Jeff Moyer2404e602015-11-03 10:40:06 -05002130 }
2131}
2132
Jens Axboe8e8320c2017-06-20 17:56:13 -06002133static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2134 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002135{
2136 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002137
Bart Van Assche705cda92017-04-07 11:16:49 -07002138 lockdep_assert_held(&set->tag_list_lock);
2139
Jens Axboe0d2602c2014-05-13 15:10:52 -06002140 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2141 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002142 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002143 blk_mq_unfreeze_queue(q);
2144 }
2145}
2146
2147static void blk_mq_del_queue_tag_set(struct request_queue *q)
2148{
2149 struct blk_mq_tag_set *set = q->tag_set;
2150
Jens Axboe0d2602c2014-05-13 15:10:52 -06002151 mutex_lock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002152 list_del_rcu(&q->tag_set_list);
2153 INIT_LIST_HEAD(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002154 if (list_is_singular(&set->tag_list)) {
2155 /* just transitioned to unshared */
2156 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2157 /* update existing queue */
2158 blk_mq_update_tag_set_depth(set, false);
2159 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002160 mutex_unlock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002161
2162 synchronize_rcu();
Jens Axboe0d2602c2014-05-13 15:10:52 -06002163}
2164
2165static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2166 struct request_queue *q)
2167{
2168 q->tag_set = set;
2169
2170 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002171
2172 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
2173 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
2174 set->flags |= BLK_MQ_F_TAG_SHARED;
2175 /* update existing queue */
2176 blk_mq_update_tag_set_depth(set, true);
2177 }
2178 if (set->flags & BLK_MQ_F_TAG_SHARED)
2179 queue_set_hctx_shared(q, true);
Bart Van Assche705cda92017-04-07 11:16:49 -07002180 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002181
Jens Axboe0d2602c2014-05-13 15:10:52 -06002182 mutex_unlock(&set->tag_list_lock);
2183}
2184
Ming Leie09aae72015-01-29 20:17:27 +08002185/*
2186 * It is the actual release handler for mq, but we do it from
2187 * request queue's release handler for avoiding use-after-free
2188 * and headache because q->mq_kobj shouldn't have been introduced,
2189 * but we can't group ctx/kctx kobj without it.
2190 */
2191void blk_mq_release(struct request_queue *q)
2192{
2193 struct blk_mq_hw_ctx *hctx;
2194 unsigned int i;
2195
2196 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08002197 queue_for_each_hw_ctx(q, hctx, i) {
2198 if (!hctx)
2199 continue;
Ming Lei6c8b2322017-02-22 18:14:01 +08002200 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002201 }
Ming Leie09aae72015-01-29 20:17:27 +08002202
Akinobu Mitaa723bab2015-09-27 02:09:21 +09002203 q->mq_map = NULL;
2204
Ming Leie09aae72015-01-29 20:17:27 +08002205 kfree(q->queue_hw_ctx);
2206
Ming Lei7ea5fe32017-02-22 18:14:00 +08002207 /*
2208 * release .mq_kobj and sw queue's kobject now because
2209 * both share lifetime with request queue.
2210 */
2211 blk_mq_sysfs_deinit(q);
2212
Ming Leie09aae72015-01-29 20:17:27 +08002213 free_percpu(q->queue_ctx);
2214}
2215
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002216struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01002217{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002218 struct request_queue *uninit_q, *q;
2219
2220 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
2221 if (!uninit_q)
2222 return ERR_PTR(-ENOMEM);
2223
2224 q = blk_mq_init_allocated_queue(set, uninit_q);
2225 if (IS_ERR(q))
2226 blk_cleanup_queue(uninit_q);
2227
2228 return q;
2229}
2230EXPORT_SYMBOL(blk_mq_init_queue);
2231
Bart Van Assche07319672017-06-20 11:15:38 -07002232static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2233{
2234 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2235
2236 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, queue_rq_srcu),
2237 __alignof__(struct blk_mq_hw_ctx)) !=
2238 sizeof(struct blk_mq_hw_ctx));
2239
2240 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2241 hw_ctx_size += sizeof(struct srcu_struct);
2242
2243 return hw_ctx_size;
2244}
2245
Keith Busch868f2f02015-12-17 17:08:14 -07002246static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2247 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002248{
Keith Busch868f2f02015-12-17 17:08:14 -07002249 int i, j;
2250 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01002251
Keith Busch868f2f02015-12-17 17:08:14 -07002252 blk_mq_sysfs_unregister(q);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002253 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002254 int node;
Jens Axboef14bbe72014-05-27 12:06:53 -06002255
Keith Busch868f2f02015-12-17 17:08:14 -07002256 if (hctxs[i])
2257 continue;
2258
2259 node = blk_mq_hw_queue_to_node(q->mq_map, i);
Bart Van Assche07319672017-06-20 11:15:38 -07002260 hctxs[i] = kzalloc_node(blk_mq_hw_ctx_size(set),
Christoph Hellwigcdef54d2014-05-28 18:11:06 +02002261 GFP_KERNEL, node);
Jens Axboe320ae512013-10-24 09:20:05 +01002262 if (!hctxs[i])
Keith Busch868f2f02015-12-17 17:08:14 -07002263 break;
Jens Axboe320ae512013-10-24 09:20:05 +01002264
Jens Axboea86073e2014-10-13 15:41:54 -06002265 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
Keith Busch868f2f02015-12-17 17:08:14 -07002266 node)) {
2267 kfree(hctxs[i]);
2268 hctxs[i] = NULL;
2269 break;
2270 }
Jens Axboee4043dc2014-04-09 10:18:23 -06002271
Jens Axboe0d2602c2014-05-13 15:10:52 -06002272 atomic_set(&hctxs[i]->nr_active, 0);
Jens Axboef14bbe72014-05-27 12:06:53 -06002273 hctxs[i]->numa_node = node;
Jens Axboe320ae512013-10-24 09:20:05 +01002274 hctxs[i]->queue_num = i;
Keith Busch868f2f02015-12-17 17:08:14 -07002275
2276 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2277 free_cpumask_var(hctxs[i]->cpumask);
2278 kfree(hctxs[i]);
2279 hctxs[i] = NULL;
2280 break;
2281 }
2282 blk_mq_hctx_kobj_init(hctxs[i]);
Jens Axboe320ae512013-10-24 09:20:05 +01002283 }
Keith Busch868f2f02015-12-17 17:08:14 -07002284 for (j = i; j < q->nr_hw_queues; j++) {
2285 struct blk_mq_hw_ctx *hctx = hctxs[j];
2286
2287 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07002288 if (hctx->tags)
2289 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002290 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002291 kobject_put(&hctx->kobj);
Keith Busch868f2f02015-12-17 17:08:14 -07002292 hctxs[j] = NULL;
2293
2294 }
2295 }
2296 q->nr_hw_queues = i;
2297 blk_mq_sysfs_register(q);
2298}
2299
2300struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2301 struct request_queue *q)
2302{
Ming Lei66841672016-02-12 15:27:00 +08002303 /* mark the queue as mq asap */
2304 q->mq_ops = set->ops;
2305
Omar Sandoval34dbad52017-03-21 08:56:08 -07002306 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06002307 blk_mq_poll_stats_bkt,
2308 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07002309 if (!q->poll_cb)
2310 goto err_exit;
2311
Keith Busch868f2f02015-12-17 17:08:14 -07002312 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2313 if (!q->queue_ctx)
Ming Linc7de5722016-05-25 23:23:27 -07002314 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002315
Ming Lei737f98c2017-02-22 18:13:59 +08002316 /* init q->mq_kobj and sw queues' kobjects */
2317 blk_mq_sysfs_init(q);
2318
Keith Busch868f2f02015-12-17 17:08:14 -07002319 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2320 GFP_KERNEL, set->numa_node);
2321 if (!q->queue_hw_ctx)
2322 goto err_percpu;
2323
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002324 q->mq_map = set->mq_map;
Keith Busch868f2f02015-12-17 17:08:14 -07002325
2326 blk_mq_realloc_hw_ctxs(set, q);
2327 if (!q->nr_hw_queues)
2328 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002329
Christoph Hellwig287922e2015-10-30 20:57:30 +08002330 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002331 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002332
2333 q->nr_queues = nr_cpu_ids;
Jens Axboe320ae512013-10-24 09:20:05 +01002334
Jens Axboe94eddfb2013-11-19 09:25:07 -07002335 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002336
Jens Axboe05f1dd52014-05-29 09:53:32 -06002337 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2338 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2339
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002340 q->sg_reserved_size = INT_MAX;
2341
Mike Snitzer28494502016-09-14 13:28:30 -04002342 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002343 INIT_LIST_HEAD(&q->requeue_list);
2344 spin_lock_init(&q->requeue_lock);
2345
Christoph Hellwig254d2592017-03-22 15:01:50 -04002346 blk_queue_make_request(q, blk_mq_make_request);
Jens Axboe07068d52014-05-22 10:40:51 -06002347
Jens Axboeeba71762014-05-20 15:17:27 -06002348 /*
2349 * Do this after blk_queue_make_request() overrides it...
2350 */
2351 q->nr_requests = set->queue_depth;
2352
Jens Axboe64f1c212016-11-14 13:03:03 -07002353 /*
2354 * Default to classic polling
2355 */
2356 q->poll_nsec = -1;
2357
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002358 if (set->ops->complete)
2359 blk_queue_softirq_done(q, set->ops->complete);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08002360
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002361 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002362 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002363 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002364
Jens Axboed3484992017-01-13 14:43:58 -07002365 if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
2366 int ret;
2367
2368 ret = blk_mq_sched_init(q);
2369 if (ret)
2370 return ERR_PTR(ret);
2371 }
2372
Jens Axboe320ae512013-10-24 09:20:05 +01002373 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002374
Jens Axboe320ae512013-10-24 09:20:05 +01002375err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002376 kfree(q->queue_hw_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002377err_percpu:
Keith Busch868f2f02015-12-17 17:08:14 -07002378 free_percpu(q->queue_ctx);
Ming Linc7de5722016-05-25 23:23:27 -07002379err_exit:
2380 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002381 return ERR_PTR(-ENOMEM);
2382}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002383EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002384
2385void blk_mq_free_queue(struct request_queue *q)
2386{
Ming Lei624dbe42014-05-27 23:35:13 +08002387 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002388
Jens Axboe0d2602c2014-05-13 15:10:52 -06002389 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08002390 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002391}
Jens Axboe320ae512013-10-24 09:20:05 +01002392
2393/* Basically redo blk_mq_init_queue with queue frozen */
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002394static void blk_mq_queue_reinit(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002395{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +02002396 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
Jens Axboe320ae512013-10-24 09:20:05 +01002397
Omar Sandoval9c1051a2017-05-04 08:17:21 -06002398 blk_mq_debugfs_unregister_hctxs(q);
Jens Axboe67aec142014-05-30 08:25:36 -06002399 blk_mq_sysfs_unregister(q);
2400
Jens Axboe320ae512013-10-24 09:20:05 +01002401 /*
2402 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2403 * we should change hctx numa_node according to new topology (this
2404 * involves free and re-allocate memory, worthy doing?)
2405 */
2406
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002407 blk_mq_map_swqueue(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002408
Jens Axboe67aec142014-05-30 08:25:36 -06002409 blk_mq_sysfs_register(q);
Omar Sandoval9c1051a2017-05-04 08:17:21 -06002410 blk_mq_debugfs_register_hctxs(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002411}
2412
Jens Axboea5164402014-09-10 09:02:03 -06002413static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2414{
2415 int i;
2416
Jens Axboecc71a6f2017-01-11 14:29:56 -07002417 for (i = 0; i < set->nr_hw_queues; i++)
2418 if (!__blk_mq_alloc_rq_map(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06002419 goto out_unwind;
Jens Axboea5164402014-09-10 09:02:03 -06002420
2421 return 0;
2422
2423out_unwind:
2424 while (--i >= 0)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002425 blk_mq_free_rq_map(set->tags[i]);
Jens Axboea5164402014-09-10 09:02:03 -06002426
Jens Axboea5164402014-09-10 09:02:03 -06002427 return -ENOMEM;
2428}
2429
2430/*
2431 * Allocate the request maps associated with this tag_set. Note that this
2432 * may reduce the depth asked for, if memory is tight. set->queue_depth
2433 * will be updated to reflect the allocated depth.
2434 */
2435static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2436{
2437 unsigned int depth;
2438 int err;
2439
2440 depth = set->queue_depth;
2441 do {
2442 err = __blk_mq_alloc_rq_maps(set);
2443 if (!err)
2444 break;
2445
2446 set->queue_depth >>= 1;
2447 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2448 err = -ENOMEM;
2449 break;
2450 }
2451 } while (set->queue_depth);
2452
2453 if (!set->queue_depth || err) {
2454 pr_err("blk-mq: failed to allocate request map\n");
2455 return -ENOMEM;
2456 }
2457
2458 if (depth != set->queue_depth)
2459 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2460 depth, set->queue_depth);
2461
2462 return 0;
2463}
2464
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002465static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
2466{
2467 if (set->ops->map_queues)
2468 return set->ops->map_queues(set);
2469 else
2470 return blk_mq_map_queues(set);
2471}
2472
Jens Axboea4391c62014-06-05 15:21:56 -06002473/*
2474 * Alloc a tag set to be associated with one or more request queues.
2475 * May fail with EINVAL for various error conditions. May adjust the
2476 * requested depth down, if if it too large. In that case, the set
2477 * value will be stored in set->queue_depth.
2478 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002479int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2480{
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002481 int ret;
2482
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002483 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2484
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002485 if (!set->nr_hw_queues)
2486 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002487 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002488 return -EINVAL;
2489 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2490 return -EINVAL;
2491
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002492 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002493 return -EINVAL;
2494
Jens Axboea4391c62014-06-05 15:21:56 -06002495 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2496 pr_info("blk-mq: reduced tag depth to %u\n",
2497 BLK_MQ_MAX_DEPTH);
2498 set->queue_depth = BLK_MQ_MAX_DEPTH;
2499 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002500
Shaohua Li6637fad2014-11-30 16:00:58 -08002501 /*
2502 * If a crashdump is active, then we are potentially in a very
2503 * memory constrained environment. Limit us to 1 queue and
2504 * 64 tags to prevent using too much memory.
2505 */
2506 if (is_kdump_kernel()) {
2507 set->nr_hw_queues = 1;
2508 set->queue_depth = min(64U, set->queue_depth);
2509 }
Keith Busch868f2f02015-12-17 17:08:14 -07002510 /*
2511 * There is no use for more h/w queues than cpus.
2512 */
2513 if (set->nr_hw_queues > nr_cpu_ids)
2514 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08002515
Keith Busch868f2f02015-12-17 17:08:14 -07002516 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002517 GFP_KERNEL, set->numa_node);
2518 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002519 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002520
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002521 ret = -ENOMEM;
2522 set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
2523 GFP_KERNEL, set->numa_node);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002524 if (!set->mq_map)
2525 goto out_free_tags;
2526
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002527 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002528 if (ret)
2529 goto out_free_mq_map;
2530
2531 ret = blk_mq_alloc_rq_maps(set);
2532 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002533 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002534
Jens Axboe0d2602c2014-05-13 15:10:52 -06002535 mutex_init(&set->tag_list_lock);
2536 INIT_LIST_HEAD(&set->tag_list);
2537
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002538 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002539
2540out_free_mq_map:
2541 kfree(set->mq_map);
2542 set->mq_map = NULL;
2543out_free_tags:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002544 kfree(set->tags);
2545 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002546 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002547}
2548EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2549
2550void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2551{
2552 int i;
2553
Jens Axboecc71a6f2017-01-11 14:29:56 -07002554 for (i = 0; i < nr_cpu_ids; i++)
2555 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06002556
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002557 kfree(set->mq_map);
2558 set->mq_map = NULL;
2559
Ming Lei981bd182014-04-24 00:07:34 +08002560 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002561 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002562}
2563EXPORT_SYMBOL(blk_mq_free_tag_set);
2564
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002565int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2566{
2567 struct blk_mq_tag_set *set = q->tag_set;
2568 struct blk_mq_hw_ctx *hctx;
2569 int i, ret;
2570
Jens Axboebd166ef2017-01-17 06:03:22 -07002571 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002572 return -EINVAL;
2573
Jens Axboe70f36b62017-01-19 10:59:07 -07002574 blk_mq_freeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07002575
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002576 ret = 0;
2577 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07002578 if (!hctx->tags)
2579 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07002580 /*
2581 * If we're using an MQ scheduler, just update the scheduler
2582 * queue depth. This is similar to what the old code would do.
2583 */
Jens Axboe70f36b62017-01-19 10:59:07 -07002584 if (!hctx->sched_tags) {
2585 ret = blk_mq_tag_update_depth(hctx, &hctx->tags,
2586 min(nr, set->queue_depth),
2587 false);
2588 } else {
2589 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
2590 nr, true);
2591 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002592 if (ret)
2593 break;
2594 }
2595
2596 if (!ret)
2597 q->nr_requests = nr;
2598
Jens Axboe70f36b62017-01-19 10:59:07 -07002599 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07002600
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002601 return ret;
2602}
2603
Keith Busche4dc2b32017-05-30 14:39:11 -04002604static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
2605 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07002606{
2607 struct request_queue *q;
2608
Bart Van Assche705cda92017-04-07 11:16:49 -07002609 lockdep_assert_held(&set->tag_list_lock);
2610
Keith Busch868f2f02015-12-17 17:08:14 -07002611 if (nr_hw_queues > nr_cpu_ids)
2612 nr_hw_queues = nr_cpu_ids;
2613 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2614 return;
2615
2616 list_for_each_entry(q, &set->tag_list, tag_set_list)
2617 blk_mq_freeze_queue(q);
2618
2619 set->nr_hw_queues = nr_hw_queues;
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002620 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07002621 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2622 blk_mq_realloc_hw_ctxs(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002623 blk_mq_queue_reinit(q);
Keith Busch868f2f02015-12-17 17:08:14 -07002624 }
2625
2626 list_for_each_entry(q, &set->tag_list, tag_set_list)
2627 blk_mq_unfreeze_queue(q);
2628}
Keith Busche4dc2b32017-05-30 14:39:11 -04002629
2630void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2631{
2632 mutex_lock(&set->tag_list_lock);
2633 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
2634 mutex_unlock(&set->tag_list_lock);
2635}
Keith Busch868f2f02015-12-17 17:08:14 -07002636EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2637
Omar Sandoval34dbad52017-03-21 08:56:08 -07002638/* Enable polling stats and return whether they were already enabled. */
2639static bool blk_poll_stats_enable(struct request_queue *q)
2640{
2641 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
2642 test_and_set_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
2643 return true;
2644 blk_stat_add_callback(q, q->poll_cb);
2645 return false;
2646}
2647
2648static void blk_mq_poll_stats_start(struct request_queue *q)
2649{
2650 /*
2651 * We don't arm the callback if polling stats are not enabled or the
2652 * callback is already active.
2653 */
2654 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
2655 blk_stat_is_active(q->poll_cb))
2656 return;
2657
2658 blk_stat_activate_msecs(q->poll_cb, 100);
2659}
2660
2661static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
2662{
2663 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06002664 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07002665
Stephen Bates720b8cc2017-04-07 06:24:03 -06002666 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
2667 if (cb->stat[bucket].nr_samples)
2668 q->poll_stat[bucket] = cb->stat[bucket];
2669 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07002670}
2671
Jens Axboe64f1c212016-11-14 13:03:03 -07002672static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
2673 struct blk_mq_hw_ctx *hctx,
2674 struct request *rq)
2675{
Jens Axboe64f1c212016-11-14 13:03:03 -07002676 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06002677 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07002678
2679 /*
2680 * If stats collection isn't on, don't sleep but turn it on for
2681 * future users
2682 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07002683 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07002684 return 0;
2685
2686 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07002687 * As an optimistic guess, use half of the mean service time
2688 * for this type of request. We can (and should) make this smarter.
2689 * For instance, if the completion latencies are tight, we can
2690 * get closer than just half the mean. This is especially
2691 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06002692 * than ~10 usec. We do use the stats for the relevant IO size
2693 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07002694 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06002695 bucket = blk_mq_poll_stats_bkt(rq);
2696 if (bucket < 0)
2697 return ret;
2698
2699 if (q->poll_stat[bucket].nr_samples)
2700 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07002701
2702 return ret;
2703}
2704
Jens Axboe06426ad2016-11-14 13:01:59 -07002705static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07002706 struct blk_mq_hw_ctx *hctx,
Jens Axboe06426ad2016-11-14 13:01:59 -07002707 struct request *rq)
2708{
2709 struct hrtimer_sleeper hs;
2710 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07002711 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07002712 ktime_t kt;
2713
Jens Axboe64f1c212016-11-14 13:03:03 -07002714 if (test_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags))
2715 return false;
2716
2717 /*
2718 * poll_nsec can be:
2719 *
2720 * -1: don't ever hybrid sleep
2721 * 0: use half of prev avg
2722 * >0: use this specific value
2723 */
2724 if (q->poll_nsec == -1)
2725 return false;
2726 else if (q->poll_nsec > 0)
2727 nsecs = q->poll_nsec;
2728 else
2729 nsecs = blk_mq_poll_nsecs(q, hctx, rq);
2730
2731 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07002732 return false;
2733
2734 set_bit(REQ_ATOM_POLL_SLEPT, &rq->atomic_flags);
2735
2736 /*
2737 * This will be replaced with the stats tracking code, using
2738 * 'avg_completion_time / 2' as the pre-sleep target.
2739 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002740 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07002741
2742 mode = HRTIMER_MODE_REL;
2743 hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
2744 hrtimer_set_expires(&hs.timer, kt);
2745
2746 hrtimer_init_sleeper(&hs, current);
2747 do {
2748 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
2749 break;
2750 set_current_state(TASK_UNINTERRUPTIBLE);
2751 hrtimer_start_expires(&hs.timer, mode);
2752 if (hs.task)
2753 io_schedule();
2754 hrtimer_cancel(&hs.timer);
2755 mode = HRTIMER_MODE_ABS;
2756 } while (hs.task && !signal_pending(current));
2757
2758 __set_current_state(TASK_RUNNING);
2759 destroy_hrtimer_on_stack(&hs.timer);
2760 return true;
2761}
2762
Jens Axboebbd7bb72016-11-04 09:34:34 -06002763static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
2764{
2765 struct request_queue *q = hctx->queue;
2766 long state;
2767
Jens Axboe06426ad2016-11-14 13:01:59 -07002768 /*
2769 * If we sleep, have the caller restart the poll loop to reset
2770 * the state. Like for the other success return cases, the
2771 * caller is responsible for checking if the IO completed. If
2772 * the IO isn't complete, we'll get called again and will go
2773 * straight to the busy poll loop.
2774 */
Jens Axboe64f1c212016-11-14 13:03:03 -07002775 if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
Jens Axboe06426ad2016-11-14 13:01:59 -07002776 return true;
2777
Jens Axboebbd7bb72016-11-04 09:34:34 -06002778 hctx->poll_considered++;
2779
2780 state = current->state;
2781 while (!need_resched()) {
2782 int ret;
2783
2784 hctx->poll_invoked++;
2785
2786 ret = q->mq_ops->poll(hctx, rq->tag);
2787 if (ret > 0) {
2788 hctx->poll_success++;
2789 set_current_state(TASK_RUNNING);
2790 return true;
2791 }
2792
2793 if (signal_pending_state(state, current))
2794 set_current_state(TASK_RUNNING);
2795
2796 if (current->state == TASK_RUNNING)
2797 return true;
2798 if (ret < 0)
2799 break;
2800 cpu_relax();
2801 }
2802
2803 return false;
2804}
2805
2806bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
2807{
2808 struct blk_mq_hw_ctx *hctx;
2809 struct blk_plug *plug;
2810 struct request *rq;
2811
2812 if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
2813 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
2814 return false;
2815
2816 plug = current->plug;
2817 if (plug)
2818 blk_flush_plug_list(plug, false);
2819
2820 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
Jens Axboebd166ef2017-01-17 06:03:22 -07002821 if (!blk_qc_t_is_internal(cookie))
2822 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
Jens Axboe3a07bb12017-04-20 14:53:28 -06002823 else {
Jens Axboebd166ef2017-01-17 06:03:22 -07002824 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
Jens Axboe3a07bb12017-04-20 14:53:28 -06002825 /*
2826 * With scheduling, if the request has completed, we'll
2827 * get a NULL return here, as we clear the sched tag when
2828 * that happens. The request still remains valid, like always,
2829 * so we should be safe with just the NULL check.
2830 */
2831 if (!rq)
2832 return false;
2833 }
Jens Axboebbd7bb72016-11-04 09:34:34 -06002834
2835 return __blk_mq_poll(hctx, rq);
2836}
2837EXPORT_SYMBOL_GPL(blk_mq_poll);
2838
Jens Axboe320ae512013-10-24 09:20:05 +01002839static int __init blk_mq_init(void)
2840{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002841 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
2842 blk_mq_hctx_notify_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01002843 return 0;
2844}
2845subsys_initcall(blk_mq_init);