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
;
114 const bool is_flush
= op
& (REQ_PREFLUSH
| REQ_FUA
);
116 blk_queue_enter_live(q
);
117 ctx
= blk_mq_get_ctx(q
);
118 hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
120 blk_mq_set_alloc_data(data
, q
, data
->flags
, ctx
, hctx
);
123 data
->flags
|= BLK_MQ_REQ_INTERNAL
;
126 * Flush requests are special and go directly to the
129 if (!is_flush
&& e
->type
->ops
.mq
.get_request
) {
130 rq
= e
->type
->ops
.mq
.get_request(q
, op
, data
);
132 rq
->rq_flags
|= RQF_QUEUED
;
134 rq
= __blk_mq_alloc_request(data
, op
);
136 rq
= __blk_mq_alloc_request(data
, op
);
138 data
->hctx
->tags
->rqs
[rq
->tag
] = rq
;
144 if (e
&& e
->type
->icq_cache
)
145 blk_mq_sched_assign_ioc(q
, rq
, bio
);
147 data
->hctx
->queued
++;
155 void blk_mq_sched_put_request(struct request
*rq
)
157 struct request_queue
*q
= rq
->q
;
158 struct elevator_queue
*e
= q
->elevator
;
160 if (rq
->rq_flags
& RQF_ELVPRIV
) {
161 blk_mq_sched_put_rq_priv(rq
->q
, rq
);
163 put_io_context(rq
->elv
.icq
->ioc
);
168 if ((rq
->rq_flags
& RQF_QUEUED
) && e
&& e
->type
->ops
.mq
.put_request
)
169 e
->type
->ops
.mq
.put_request(rq
);
171 blk_mq_finish_request(rq
);
174 void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx
*hctx
)
176 struct elevator_queue
*e
= hctx
->queue
->elevator
;
179 if (unlikely(blk_mq_hctx_stopped(hctx
)))
185 * If we have previous entries on our dispatch list, grab them first for
186 * more fair dispatch.
188 if (!list_empty_careful(&hctx
->dispatch
)) {
189 spin_lock(&hctx
->lock
);
190 if (!list_empty(&hctx
->dispatch
))
191 list_splice_init(&hctx
->dispatch
, &rq_list
);
192 spin_unlock(&hctx
->lock
);
196 * Only ask the scheduler for requests, if we didn't have residual
197 * requests from the dispatch list. This is to avoid the case where
198 * we only ever dispatch a fraction of the requests available because
199 * of low device queue depth. Once we pull requests out of the IO
200 * scheduler, we can no longer merge or sort them. So it's best to
201 * leave them there for as long as we can. Mark the hw queue as
202 * needing a restart in that case.
204 if (!list_empty(&rq_list
)) {
205 blk_mq_sched_mark_restart(hctx
);
206 blk_mq_dispatch_rq_list(hctx
, &rq_list
);
207 } else if (!e
|| !e
->type
->ops
.mq
.dispatch_request
) {
208 blk_mq_flush_busy_ctxs(hctx
, &rq_list
);
209 blk_mq_dispatch_rq_list(hctx
, &rq_list
);
214 rq
= e
->type
->ops
.mq
.dispatch_request(hctx
);
217 list_add(&rq
->queuelist
, &rq_list
);
218 } while (blk_mq_dispatch_rq_list(hctx
, &rq_list
));
222 void blk_mq_sched_move_to_dispatch(struct blk_mq_hw_ctx
*hctx
,
223 struct list_head
*rq_list
,
224 struct request
*(*get_rq
)(struct blk_mq_hw_ctx
*))
233 list_add_tail(&rq
->queuelist
, rq_list
);
236 EXPORT_SYMBOL_GPL(blk_mq_sched_move_to_dispatch
);
238 bool blk_mq_sched_try_merge(struct request_queue
*q
, struct bio
*bio
)
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 if (!attempt_back_merge(q
, rq
))
249 elv_merged_request(q
, rq
, ret
);
252 } else if (ret
== ELEVATOR_FRONT_MERGE
) {
253 if (!blk_mq_sched_allow_merge(q
, rq
, bio
))
255 if (bio_attempt_front_merge(q
, rq
, bio
)) {
256 if (!attempt_front_merge(q
, rq
))
257 elv_merged_request(q
, rq
, ret
);
264 EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge
);
266 bool __blk_mq_sched_bio_merge(struct request_queue
*q
, struct bio
*bio
)
268 struct elevator_queue
*e
= q
->elevator
;
270 if (e
->type
->ops
.mq
.bio_merge
) {
271 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
272 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
275 return e
->type
->ops
.mq
.bio_merge(hctx
, bio
);
281 bool blk_mq_sched_try_insert_merge(struct request_queue
*q
, struct request
*rq
)
283 return rq_mergeable(rq
) && elv_attempt_insert_merge(q
, rq
);
285 EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge
);
287 void blk_mq_sched_request_inserted(struct request
*rq
)
289 trace_block_rq_insert(rq
->q
, rq
);
291 EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted
);
293 bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
296 rq
->rq_flags
|= RQF_SORTED
;
301 * If we already have a real request tag, send directly to
304 spin_lock(&hctx
->lock
);
305 list_add(&rq
->queuelist
, &hctx
->dispatch
);
306 spin_unlock(&hctx
->lock
);
309 EXPORT_SYMBOL_GPL(blk_mq_sched_bypass_insert
);
311 static void blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx
*hctx
)
313 if (test_bit(BLK_MQ_S_SCHED_RESTART
, &hctx
->state
)) {
314 clear_bit(BLK_MQ_S_SCHED_RESTART
, &hctx
->state
);
315 if (blk_mq_hctx_has_pending(hctx
))
316 blk_mq_run_hw_queue(hctx
, true);
320 void blk_mq_sched_restart_queues(struct blk_mq_hw_ctx
*hctx
)
324 if (!(hctx
->flags
& BLK_MQ_F_TAG_SHARED
))
325 blk_mq_sched_restart_hctx(hctx
);
327 struct request_queue
*q
= hctx
->queue
;
329 if (!test_bit(QUEUE_FLAG_RESTART
, &q
->queue_flags
))
332 clear_bit(QUEUE_FLAG_RESTART
, &q
->queue_flags
);
334 queue_for_each_hw_ctx(q
, hctx
, i
)
335 blk_mq_sched_restart_hctx(hctx
);
339 static void blk_mq_sched_free_tags(struct blk_mq_tag_set
*set
,
340 struct blk_mq_hw_ctx
*hctx
,
341 unsigned int hctx_idx
)
343 if (hctx
->sched_tags
) {
344 blk_mq_free_rqs(set
, hctx
->sched_tags
, hctx_idx
);
345 blk_mq_free_rq_map(hctx
->sched_tags
);
346 hctx
->sched_tags
= NULL
;
350 int blk_mq_sched_setup(struct request_queue
*q
)
352 struct blk_mq_tag_set
*set
= q
->tag_set
;
353 struct blk_mq_hw_ctx
*hctx
;
357 * Default to 256, since we don't split into sync/async like the
358 * old code did. Additionally, this is a per-hw queue depth.
360 q
->nr_requests
= 2 * BLKDEV_MAX_RQ
;
363 * We're switching to using an IO scheduler, so setup the hctx
364 * scheduler tags and switch the request map from the regular
365 * tags to scheduler tags. First allocate what we need, so we
366 * can safely fail and fallback, if needed.
369 queue_for_each_hw_ctx(q
, hctx
, i
) {
370 hctx
->sched_tags
= blk_mq_alloc_rq_map(set
, i
, q
->nr_requests
, 0);
371 if (!hctx
->sched_tags
) {
375 ret
= blk_mq_alloc_rqs(set
, hctx
->sched_tags
, i
, q
->nr_requests
);
381 * If we failed, free what we did allocate
384 queue_for_each_hw_ctx(q
, hctx
, i
) {
385 if (!hctx
->sched_tags
)
387 blk_mq_sched_free_tags(set
, hctx
, i
);
396 void blk_mq_sched_teardown(struct request_queue
*q
)
398 struct blk_mq_tag_set
*set
= q
->tag_set
;
399 struct blk_mq_hw_ctx
*hctx
;
402 queue_for_each_hw_ctx(q
, hctx
, i
)
403 blk_mq_sched_free_tags(set
, hctx
, i
);
406 int blk_mq_sched_init(struct request_queue
*q
)
410 #if defined(CONFIG_DEFAULT_SQ_NONE)
411 if (q
->nr_hw_queues
== 1)
414 #if defined(CONFIG_DEFAULT_MQ_NONE)
415 if (q
->nr_hw_queues
> 1)
419 mutex_lock(&q
->sysfs_lock
);
420 ret
= elevator_init(q
, NULL
);
421 mutex_unlock(&q
->sysfs_lock
);