2 * blk-mq scheduling framework
4 * Copyright (C) 2016 Jens Axboe
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/blk-mq.h>
10 #include <trace/events/block.h>
14 #include "blk-mq-sched.h"
15 #include "blk-mq-tag.h"
18 void blk_mq_sched_free_hctx_data(struct request_queue
*q
,
19 void (*exit
)(struct blk_mq_hw_ctx
*))
21 struct blk_mq_hw_ctx
*hctx
;
24 queue_for_each_hw_ctx(q
, hctx
, i
) {
25 if (exit
&& hctx
->sched_data
)
27 kfree(hctx
->sched_data
);
28 hctx
->sched_data
= NULL
;
31 EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data
);
33 int blk_mq_sched_init_hctx_data(struct request_queue
*q
, size_t size
,
34 int (*init
)(struct blk_mq_hw_ctx
*),
35 void (*exit
)(struct blk_mq_hw_ctx
*))
37 struct blk_mq_hw_ctx
*hctx
;
41 queue_for_each_hw_ctx(q
, hctx
, i
) {
42 hctx
->sched_data
= kmalloc_node(size
, GFP_KERNEL
, hctx
->numa_node
);
43 if (!hctx
->sched_data
) {
52 * We don't want to give exit() a partially
53 * initialized sched_data. init() must clean up
56 kfree(hctx
->sched_data
);
57 hctx
->sched_data
= NULL
;
65 blk_mq_sched_free_hctx_data(q
, exit
);
68 EXPORT_SYMBOL_GPL(blk_mq_sched_init_hctx_data
);
70 static void __blk_mq_sched_assign_ioc(struct request_queue
*q
,
71 struct request
*rq
, struct io_context
*ioc
)
75 spin_lock_irq(q
->queue_lock
);
76 icq
= ioc_lookup_icq(ioc
, q
);
77 spin_unlock_irq(q
->queue_lock
);
80 icq
= ioc_create_icq(ioc
, q
, GFP_ATOMIC
);
86 if (!blk_mq_sched_get_rq_priv(q
, rq
)) {
87 rq
->rq_flags
|= RQF_ELVPRIV
;
88 get_io_context(icq
->ioc
);
95 static void blk_mq_sched_assign_ioc(struct request_queue
*q
,
96 struct request
*rq
, struct bio
*bio
)
98 struct io_context
*ioc
;
102 __blk_mq_sched_assign_ioc(q
, rq
, ioc
);
105 struct request
*blk_mq_sched_get_request(struct request_queue
*q
,
108 struct blk_mq_alloc_data
*data
)
110 struct elevator_queue
*e
= q
->elevator
;
111 struct blk_mq_hw_ctx
*hctx
;
112 struct blk_mq_ctx
*ctx
;
115 blk_queue_enter_live(q
);
116 ctx
= blk_mq_get_ctx(q
);
117 hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
119 blk_mq_set_alloc_data(data
, q
, data
->flags
, ctx
, hctx
);
122 data
->flags
|= BLK_MQ_REQ_INTERNAL
;
125 * Flush requests are special and go directly to the
128 if (!op_is_flush(op
) && e
->type
->ops
.mq
.get_request
) {
129 rq
= e
->type
->ops
.mq
.get_request(q
, op
, data
);
131 rq
->rq_flags
|= RQF_QUEUED
;
133 rq
= __blk_mq_alloc_request(data
, op
);
135 rq
= __blk_mq_alloc_request(data
, op
);
137 data
->hctx
->tags
->rqs
[rq
->tag
] = rq
;
141 if (!op_is_flush(op
)) {
143 if (e
&& e
->type
->icq_cache
)
144 blk_mq_sched_assign_ioc(q
, rq
, bio
);
146 data
->hctx
->queued
++;
154 void blk_mq_sched_put_request(struct request
*rq
)
156 struct request_queue
*q
= rq
->q
;
157 struct elevator_queue
*e
= q
->elevator
;
159 if (rq
->rq_flags
& RQF_ELVPRIV
) {
160 blk_mq_sched_put_rq_priv(rq
->q
, rq
);
162 put_io_context(rq
->elv
.icq
->ioc
);
167 if ((rq
->rq_flags
& RQF_QUEUED
) && e
&& e
->type
->ops
.mq
.put_request
)
168 e
->type
->ops
.mq
.put_request(rq
);
170 blk_mq_finish_request(rq
);
173 void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx
*hctx
)
175 struct elevator_queue
*e
= hctx
->queue
->elevator
;
178 if (unlikely(blk_mq_hctx_stopped(hctx
)))
184 * If we have previous entries on our dispatch list, grab them first for
185 * more fair dispatch.
187 if (!list_empty_careful(&hctx
->dispatch
)) {
188 spin_lock(&hctx
->lock
);
189 if (!list_empty(&hctx
->dispatch
))
190 list_splice_init(&hctx
->dispatch
, &rq_list
);
191 spin_unlock(&hctx
->lock
);
195 * Only ask the scheduler for requests, if we didn't have residual
196 * requests from the dispatch list. This is to avoid the case where
197 * we only ever dispatch a fraction of the requests available because
198 * of low device queue depth. Once we pull requests out of the IO
199 * scheduler, we can no longer merge or sort them. So it's best to
200 * leave them there for as long as we can. Mark the hw queue as
201 * needing a restart in that case.
203 if (!list_empty(&rq_list
)) {
204 blk_mq_sched_mark_restart(hctx
);
205 blk_mq_dispatch_rq_list(hctx
, &rq_list
);
206 } else if (!e
|| !e
->type
->ops
.mq
.dispatch_request
) {
207 blk_mq_flush_busy_ctxs(hctx
, &rq_list
);
208 blk_mq_dispatch_rq_list(hctx
, &rq_list
);
213 rq
= e
->type
->ops
.mq
.dispatch_request(hctx
);
216 list_add(&rq
->queuelist
, &rq_list
);
217 } while (blk_mq_dispatch_rq_list(hctx
, &rq_list
));
221 void blk_mq_sched_move_to_dispatch(struct blk_mq_hw_ctx
*hctx
,
222 struct list_head
*rq_list
,
223 struct request
*(*get_rq
)(struct blk_mq_hw_ctx
*))
232 list_add_tail(&rq
->queuelist
, rq_list
);
235 EXPORT_SYMBOL_GPL(blk_mq_sched_move_to_dispatch
);
237 bool blk_mq_sched_try_merge(struct request_queue
*q
, struct bio
*bio
,
238 struct request
**merged_request
)
243 ret
= elv_merge(q
, &rq
, bio
);
244 if (ret
== ELEVATOR_BACK_MERGE
) {
245 if (!blk_mq_sched_allow_merge(q
, rq
, bio
))
247 if (bio_attempt_back_merge(q
, rq
, bio
)) {
248 *merged_request
= attempt_back_merge(q
, rq
);
249 if (!*merged_request
)
250 elv_merged_request(q
, rq
, ret
);
253 } else if (ret
== ELEVATOR_FRONT_MERGE
) {
254 if (!blk_mq_sched_allow_merge(q
, rq
, bio
))
256 if (bio_attempt_front_merge(q
, rq
, bio
)) {
257 *merged_request
= attempt_front_merge(q
, rq
);
258 if (!*merged_request
)
259 elv_merged_request(q
, rq
, ret
);
266 EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge
);
268 bool __blk_mq_sched_bio_merge(struct request_queue
*q
, struct bio
*bio
)
270 struct elevator_queue
*e
= q
->elevator
;
272 if (e
->type
->ops
.mq
.bio_merge
) {
273 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
274 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
277 return e
->type
->ops
.mq
.bio_merge(hctx
, bio
);
283 bool blk_mq_sched_try_insert_merge(struct request_queue
*q
, struct request
*rq
)
285 return rq_mergeable(rq
) && elv_attempt_insert_merge(q
, rq
);
287 EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge
);
289 void blk_mq_sched_request_inserted(struct request
*rq
)
291 trace_block_rq_insert(rq
->q
, rq
);
293 EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted
);
295 static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx
*hctx
,
299 rq
->rq_flags
|= RQF_SORTED
;
304 * If we already have a real request tag, send directly to
307 spin_lock(&hctx
->lock
);
308 list_add(&rq
->queuelist
, &hctx
->dispatch
);
309 spin_unlock(&hctx
->lock
);
313 static void blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx
*hctx
)
315 if (test_bit(BLK_MQ_S_SCHED_RESTART
, &hctx
->state
)) {
316 clear_bit(BLK_MQ_S_SCHED_RESTART
, &hctx
->state
);
317 if (blk_mq_hctx_has_pending(hctx
))
318 blk_mq_run_hw_queue(hctx
, true);
322 void blk_mq_sched_restart_queues(struct blk_mq_hw_ctx
*hctx
)
326 if (!(hctx
->flags
& BLK_MQ_F_TAG_SHARED
))
327 blk_mq_sched_restart_hctx(hctx
);
329 struct request_queue
*q
= hctx
->queue
;
331 if (!test_bit(QUEUE_FLAG_RESTART
, &q
->queue_flags
))
334 clear_bit(QUEUE_FLAG_RESTART
, &q
->queue_flags
);
336 queue_for_each_hw_ctx(q
, hctx
, i
)
337 blk_mq_sched_restart_hctx(hctx
);
342 * Add flush/fua to the queue. If we fail getting a driver tag, then
343 * punt to the requeue list. Requeue will re-invoke us from a context
344 * that's safe to block from.
346 static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx
*hctx
,
347 struct request
*rq
, bool can_block
)
349 if (blk_mq_get_driver_tag(rq
, &hctx
, can_block
)) {
350 blk_insert_flush(rq
);
351 blk_mq_run_hw_queue(hctx
, true);
353 blk_mq_add_to_requeue_list(rq
, true, true);
356 void blk_mq_sched_insert_request(struct request
*rq
, bool at_head
,
357 bool run_queue
, bool async
, bool can_block
)
359 struct request_queue
*q
= rq
->q
;
360 struct elevator_queue
*e
= q
->elevator
;
361 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
362 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
364 if (rq
->tag
== -1 && op_is_flush(rq
->cmd_flags
)) {
365 blk_mq_sched_insert_flush(hctx
, rq
, can_block
);
369 if (e
&& blk_mq_sched_bypass_insert(hctx
, rq
))
372 if (e
&& e
->type
->ops
.mq
.insert_requests
) {
375 list_add(&rq
->queuelist
, &list
);
376 e
->type
->ops
.mq
.insert_requests(hctx
, &list
, at_head
);
378 spin_lock(&ctx
->lock
);
379 __blk_mq_insert_request(hctx
, rq
, at_head
);
380 spin_unlock(&ctx
->lock
);
385 blk_mq_run_hw_queue(hctx
, async
);
388 void blk_mq_sched_insert_requests(struct request_queue
*q
,
389 struct blk_mq_ctx
*ctx
,
390 struct list_head
*list
, bool run_queue_async
)
392 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
393 struct elevator_queue
*e
= hctx
->queue
->elevator
;
396 struct request
*rq
, *next
;
399 * We bypass requests that already have a driver tag assigned,
400 * which should only be flushes. Flushes are only ever inserted
401 * as single requests, so we shouldn't ever hit the
402 * WARN_ON_ONCE() below (but let's handle it just in case).
404 list_for_each_entry_safe(rq
, next
, list
, queuelist
) {
405 if (WARN_ON_ONCE(rq
->tag
!= -1)) {
406 list_del_init(&rq
->queuelist
);
407 blk_mq_sched_bypass_insert(hctx
, rq
);
412 if (e
&& e
->type
->ops
.mq
.insert_requests
)
413 e
->type
->ops
.mq
.insert_requests(hctx
, list
, false);
415 blk_mq_insert_requests(hctx
, ctx
, list
);
417 blk_mq_run_hw_queue(hctx
, run_queue_async
);
420 static void blk_mq_sched_free_tags(struct blk_mq_tag_set
*set
,
421 struct blk_mq_hw_ctx
*hctx
,
422 unsigned int hctx_idx
)
424 if (hctx
->sched_tags
) {
425 blk_mq_free_rqs(set
, hctx
->sched_tags
, hctx_idx
);
426 blk_mq_free_rq_map(hctx
->sched_tags
);
427 hctx
->sched_tags
= NULL
;
431 int blk_mq_sched_setup(struct request_queue
*q
)
433 struct blk_mq_tag_set
*set
= q
->tag_set
;
434 struct blk_mq_hw_ctx
*hctx
;
438 * Default to 256, since we don't split into sync/async like the
439 * old code did. Additionally, this is a per-hw queue depth.
441 q
->nr_requests
= 2 * BLKDEV_MAX_RQ
;
444 * We're switching to using an IO scheduler, so setup the hctx
445 * scheduler tags and switch the request map from the regular
446 * tags to scheduler tags. First allocate what we need, so we
447 * can safely fail and fallback, if needed.
450 queue_for_each_hw_ctx(q
, hctx
, i
) {
451 hctx
->sched_tags
= blk_mq_alloc_rq_map(set
, i
, q
->nr_requests
, 0);
452 if (!hctx
->sched_tags
) {
456 ret
= blk_mq_alloc_rqs(set
, hctx
->sched_tags
, i
, q
->nr_requests
);
462 * If we failed, free what we did allocate
465 queue_for_each_hw_ctx(q
, hctx
, i
) {
466 if (!hctx
->sched_tags
)
468 blk_mq_sched_free_tags(set
, hctx
, i
);
477 void blk_mq_sched_teardown(struct request_queue
*q
)
479 struct blk_mq_tag_set
*set
= q
->tag_set
;
480 struct blk_mq_hw_ctx
*hctx
;
483 queue_for_each_hw_ctx(q
, hctx
, i
)
484 blk_mq_sched_free_tags(set
, hctx
, i
);
487 int blk_mq_sched_init(struct request_queue
*q
)
491 #if defined(CONFIG_DEFAULT_SQ_NONE)
492 if (q
->nr_hw_queues
== 1)
495 #if defined(CONFIG_DEFAULT_MQ_NONE)
496 if (q
->nr_hw_queues
> 1)
500 mutex_lock(&q
->sysfs_lock
);
501 ret
= elevator_init(q
, NULL
);
502 mutex_unlock(&q
->sysfs_lock
);