blob: 22601e5c6f1982a67d6c70890fac3303682a8f7f [file] [log] [blame]
Jens Axboebd166ef2017-01-17 06:03:22 -07001/*
2 * blk-mq scheduling framework
3 *
4 * Copyright (C) 2016 Jens Axboe
5 */
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/blk-mq.h>
9
10#include <trace/events/block.h>
11
12#include "blk.h"
13#include "blk-mq.h"
Omar Sandovald332ce02017-05-04 08:24:40 -060014#include "blk-mq-debugfs.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070015#include "blk-mq-sched.h"
16#include "blk-mq-tag.h"
17#include "blk-wbt.h"
18
19void blk_mq_sched_free_hctx_data(struct request_queue *q,
20 void (*exit)(struct blk_mq_hw_ctx *))
21{
22 struct blk_mq_hw_ctx *hctx;
23 int i;
24
25 queue_for_each_hw_ctx(q, hctx, i) {
26 if (exit && hctx->sched_data)
27 exit(hctx);
28 kfree(hctx->sched_data);
29 hctx->sched_data = NULL;
30 }
31}
32EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data);
33
Jens Axboebd166ef2017-01-17 06:03:22 -070034static void __blk_mq_sched_assign_ioc(struct request_queue *q,
Paolo Valentef1ba8262017-02-07 18:24:43 +010035 struct request *rq,
36 struct bio *bio,
37 struct io_context *ioc)
Jens Axboebd166ef2017-01-17 06:03:22 -070038{
Christoph Hellwigea511e32017-06-16 18:15:20 +020039 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -070040 struct io_cq *icq;
41
42 spin_lock_irq(q->queue_lock);
43 icq = ioc_lookup_icq(ioc, q);
44 spin_unlock_irq(q->queue_lock);
45
46 if (!icq) {
47 icq = ioc_create_icq(ioc, q, GFP_ATOMIC);
48 if (!icq)
49 return;
50 }
51
52 rq->elv.icq = icq;
Christoph Hellwigea511e32017-06-16 18:15:20 +020053 if (e && e->type->ops.mq.get_rq_priv &&
54 e->type->ops.mq.get_rq_priv(q, rq, bio)) {
55 rq->elv.icq = NULL;
Jens Axboebd166ef2017-01-17 06:03:22 -070056 return;
57 }
58
Christoph Hellwigea511e32017-06-16 18:15:20 +020059 rq->rq_flags |= RQF_ELVPRIV;
60 get_io_context(icq->ioc);
Jens Axboebd166ef2017-01-17 06:03:22 -070061}
62
Christoph Hellwigd2c0d382017-06-16 18:15:19 +020063void blk_mq_sched_assign_ioc(struct request_queue *q, struct request *rq,
64 struct bio *bio)
Jens Axboebd166ef2017-01-17 06:03:22 -070065{
66 struct io_context *ioc;
67
68 ioc = rq_ioc(bio);
69 if (ioc)
Paolo Valentef1ba8262017-02-07 18:24:43 +010070 __blk_mq_sched_assign_ioc(q, rq, bio, ioc);
Jens Axboebd166ef2017-01-17 06:03:22 -070071}
72
Jens Axboebd166ef2017-01-17 06:03:22 -070073void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
74{
Omar Sandoval81380ca2017-04-07 08:56:26 -060075 struct request_queue *q = hctx->queue;
76 struct elevator_queue *e = q->elevator;
Jens Axboe64765a72017-02-17 11:39:26 -070077 const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request;
78 bool did_work = false;
Jens Axboebd166ef2017-01-17 06:03:22 -070079 LIST_HEAD(rq_list);
80
81 if (unlikely(blk_mq_hctx_stopped(hctx)))
82 return;
83
84 hctx->run++;
85
86 /*
87 * If we have previous entries on our dispatch list, grab them first for
88 * more fair dispatch.
89 */
90 if (!list_empty_careful(&hctx->dispatch)) {
91 spin_lock(&hctx->lock);
92 if (!list_empty(&hctx->dispatch))
93 list_splice_init(&hctx->dispatch, &rq_list);
94 spin_unlock(&hctx->lock);
95 }
96
97 /*
98 * Only ask the scheduler for requests, if we didn't have residual
99 * requests from the dispatch list. This is to avoid the case where
100 * we only ever dispatch a fraction of the requests available because
101 * of low device queue depth. Once we pull requests out of the IO
102 * scheduler, we can no longer merge or sort them. So it's best to
103 * leave them there for as long as we can. Mark the hw queue as
104 * needing a restart in that case.
105 */
Jens Axboec13660a2017-01-26 12:40:07 -0700106 if (!list_empty(&rq_list)) {
Omar Sandovald38d3512017-02-22 10:58:30 -0800107 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandoval81380ca2017-04-07 08:56:26 -0600108 did_work = blk_mq_dispatch_rq_list(q, &rq_list);
Jens Axboe64765a72017-02-17 11:39:26 -0700109 } else if (!has_sched_dispatch) {
Jens Axboec13660a2017-01-26 12:40:07 -0700110 blk_mq_flush_busy_ctxs(hctx, &rq_list);
Omar Sandoval81380ca2017-04-07 08:56:26 -0600111 blk_mq_dispatch_rq_list(q, &rq_list);
Jens Axboe64765a72017-02-17 11:39:26 -0700112 }
113
114 /*
115 * We want to dispatch from the scheduler if we had no work left
116 * on the dispatch list, OR if we did have work but weren't able
117 * to make progress.
118 */
119 if (!did_work && has_sched_dispatch) {
Jens Axboec13660a2017-01-26 12:40:07 -0700120 do {
121 struct request *rq;
Jens Axboebd166ef2017-01-17 06:03:22 -0700122
Jens Axboec13660a2017-01-26 12:40:07 -0700123 rq = e->type->ops.mq.dispatch_request(hctx);
124 if (!rq)
125 break;
126 list_add(&rq->queuelist, &rq_list);
Omar Sandoval81380ca2017-04-07 08:56:26 -0600127 } while (blk_mq_dispatch_rq_list(q, &rq_list));
Jens Axboec13660a2017-01-26 12:40:07 -0700128 }
Jens Axboebd166ef2017-01-17 06:03:22 -0700129}
130
Jens Axboee4d750c2017-02-03 09:48:28 -0700131bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
132 struct request **merged_request)
Jens Axboebd166ef2017-01-17 06:03:22 -0700133{
134 struct request *rq;
Jens Axboebd166ef2017-01-17 06:03:22 -0700135
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100136 switch (elv_merge(q, &rq, bio)) {
137 case ELEVATOR_BACK_MERGE:
Jens Axboebd166ef2017-01-17 06:03:22 -0700138 if (!blk_mq_sched_allow_merge(q, rq, bio))
139 return false;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100140 if (!bio_attempt_back_merge(q, rq, bio))
141 return false;
142 *merged_request = attempt_back_merge(q, rq);
143 if (!*merged_request)
144 elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
145 return true;
146 case ELEVATOR_FRONT_MERGE:
Jens Axboebd166ef2017-01-17 06:03:22 -0700147 if (!blk_mq_sched_allow_merge(q, rq, bio))
148 return false;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100149 if (!bio_attempt_front_merge(q, rq, bio))
150 return false;
151 *merged_request = attempt_front_merge(q, rq);
152 if (!*merged_request)
153 elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
154 return true;
155 default:
156 return false;
Jens Axboebd166ef2017-01-17 06:03:22 -0700157 }
Jens Axboebd166ef2017-01-17 06:03:22 -0700158}
159EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
160
Ming Lei9bddeb22017-05-26 19:53:20 +0800161/*
162 * Reverse check our software queue for entries that we could potentially
163 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
164 * too much time checking for merges.
165 */
166static bool blk_mq_attempt_merge(struct request_queue *q,
167 struct blk_mq_ctx *ctx, struct bio *bio)
168{
169 struct request *rq;
170 int checked = 8;
171
172 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
173 bool merged = false;
174
175 if (!checked--)
176 break;
177
178 if (!blk_rq_merge_ok(rq, bio))
179 continue;
180
181 switch (blk_try_merge(rq, bio)) {
182 case ELEVATOR_BACK_MERGE:
183 if (blk_mq_sched_allow_merge(q, rq, bio))
184 merged = bio_attempt_back_merge(q, rq, bio);
185 break;
186 case ELEVATOR_FRONT_MERGE:
187 if (blk_mq_sched_allow_merge(q, rq, bio))
188 merged = bio_attempt_front_merge(q, rq, bio);
189 break;
190 case ELEVATOR_DISCARD_MERGE:
191 merged = bio_attempt_discard_merge(q, rq, bio);
192 break;
193 default:
194 continue;
195 }
196
197 if (merged)
198 ctx->rq_merged++;
199 return merged;
200 }
201
202 return false;
203}
204
Jens Axboebd166ef2017-01-17 06:03:22 -0700205bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio)
206{
207 struct elevator_queue *e = q->elevator;
Ming Lei9bddeb22017-05-26 19:53:20 +0800208 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
209 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
210 bool ret = false;
Jens Axboebd166ef2017-01-17 06:03:22 -0700211
Ming Lei9bddeb22017-05-26 19:53:20 +0800212 if (e && e->type->ops.mq.bio_merge) {
Jens Axboebd166ef2017-01-17 06:03:22 -0700213 blk_mq_put_ctx(ctx);
214 return e->type->ops.mq.bio_merge(hctx, bio);
215 }
216
Ming Lei9bddeb22017-05-26 19:53:20 +0800217 if (hctx->flags & BLK_MQ_F_SHOULD_MERGE) {
218 /* default per sw-queue merge */
219 spin_lock(&ctx->lock);
220 ret = blk_mq_attempt_merge(q, ctx, bio);
221 spin_unlock(&ctx->lock);
222 }
223
224 blk_mq_put_ctx(ctx);
225 return ret;
Jens Axboebd166ef2017-01-17 06:03:22 -0700226}
227
228bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
229{
230 return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
231}
232EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
233
234void blk_mq_sched_request_inserted(struct request *rq)
235{
236 trace_block_rq_insert(rq->q, rq);
237}
238EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
239
Omar Sandoval0cacba62017-02-02 15:42:39 -0800240static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
241 struct request *rq)
Jens Axboebd166ef2017-01-17 06:03:22 -0700242{
243 if (rq->tag == -1) {
244 rq->rq_flags |= RQF_SORTED;
245 return false;
246 }
247
248 /*
249 * If we already have a real request tag, send directly to
250 * the dispatch list.
251 */
252 spin_lock(&hctx->lock);
253 list_add(&rq->queuelist, &hctx->dispatch);
254 spin_unlock(&hctx->lock);
255 return true;
256}
Jens Axboebd166ef2017-01-17 06:03:22 -0700257
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600258static bool blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx *hctx)
Jens Axboe50e1dab2017-01-26 14:42:34 -0700259{
260 if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) {
261 clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600262 if (blk_mq_hctx_has_pending(hctx)) {
Jens Axboe50e1dab2017-01-26 14:42:34 -0700263 blk_mq_run_hw_queue(hctx, true);
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600264 return true;
265 }
Jens Axboe50e1dab2017-01-26 14:42:34 -0700266 }
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600267 return false;
Jens Axboe50e1dab2017-01-26 14:42:34 -0700268}
269
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600270/**
271 * list_for_each_entry_rcu_rr - iterate in a round-robin fashion over rcu list
272 * @pos: loop cursor.
273 * @skip: the list element that will not be examined. Iteration starts at
274 * @skip->next.
275 * @head: head of the list to examine. This list must have at least one
276 * element, namely @skip.
277 * @member: name of the list_head structure within typeof(*pos).
278 */
279#define list_for_each_entry_rcu_rr(pos, skip, head, member) \
280 for ((pos) = (skip); \
281 (pos = (pos)->member.next != (head) ? list_entry_rcu( \
282 (pos)->member.next, typeof(*pos), member) : \
283 list_entry_rcu((pos)->member.next->next, typeof(*pos), member)), \
284 (pos) != (skip); )
Jens Axboe50e1dab2017-01-26 14:42:34 -0700285
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600286/*
287 * Called after a driver tag has been freed to check whether a hctx needs to
288 * be restarted. Restarts @hctx if its tag set is not shared. Restarts hardware
289 * queues in a round-robin fashion if the tag set of @hctx is shared with other
290 * hardware queues.
291 */
292void blk_mq_sched_restart(struct blk_mq_hw_ctx *const hctx)
293{
294 struct blk_mq_tags *const tags = hctx->tags;
295 struct blk_mq_tag_set *const set = hctx->queue->tag_set;
296 struct request_queue *const queue = hctx->queue, *q;
297 struct blk_mq_hw_ctx *hctx2;
298 unsigned int i, j;
299
300 if (set->flags & BLK_MQ_F_TAG_SHARED) {
301 rcu_read_lock();
302 list_for_each_entry_rcu_rr(q, queue, &set->tag_list,
303 tag_set_list) {
304 queue_for_each_hw_ctx(q, hctx2, i)
305 if (hctx2->tags == tags &&
306 blk_mq_sched_restart_hctx(hctx2))
307 goto done;
Omar Sandovald38d3512017-02-22 10:58:30 -0800308 }
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600309 j = hctx->queue_num + 1;
310 for (i = 0; i < queue->nr_hw_queues; i++, j++) {
311 if (j == queue->nr_hw_queues)
312 j = 0;
313 hctx2 = queue->queue_hw_ctx[j];
314 if (hctx2->tags == tags &&
315 blk_mq_sched_restart_hctx(hctx2))
316 break;
317 }
318done:
319 rcu_read_unlock();
Omar Sandovald38d3512017-02-22 10:58:30 -0800320 } else {
Jens Axboe50e1dab2017-01-26 14:42:34 -0700321 blk_mq_sched_restart_hctx(hctx);
Jens Axboe50e1dab2017-01-26 14:42:34 -0700322 }
323}
324
Jens Axboebd6737f2017-01-27 01:00:47 -0700325/*
326 * Add flush/fua to the queue. If we fail getting a driver tag, then
327 * punt to the requeue list. Requeue will re-invoke us from a context
328 * that's safe to block from.
329 */
330static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx *hctx,
331 struct request *rq, bool can_block)
332{
333 if (blk_mq_get_driver_tag(rq, &hctx, can_block)) {
334 blk_insert_flush(rq);
335 blk_mq_run_hw_queue(hctx, true);
336 } else
Jens Axboec7a571b2017-02-17 11:37:14 -0700337 blk_mq_add_to_requeue_list(rq, false, true);
Jens Axboebd6737f2017-01-27 01:00:47 -0700338}
339
340void blk_mq_sched_insert_request(struct request *rq, bool at_head,
341 bool run_queue, bool async, bool can_block)
342{
343 struct request_queue *q = rq->q;
344 struct elevator_queue *e = q->elevator;
345 struct blk_mq_ctx *ctx = rq->mq_ctx;
346 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
347
Jens Axboef3a8ab72017-01-27 09:08:23 -0700348 if (rq->tag == -1 && op_is_flush(rq->cmd_flags)) {
Jens Axboebd6737f2017-01-27 01:00:47 -0700349 blk_mq_sched_insert_flush(hctx, rq, can_block);
350 return;
351 }
352
Omar Sandoval0cacba62017-02-02 15:42:39 -0800353 if (e && blk_mq_sched_bypass_insert(hctx, rq))
354 goto run;
355
Jens Axboebd6737f2017-01-27 01:00:47 -0700356 if (e && e->type->ops.mq.insert_requests) {
357 LIST_HEAD(list);
358
359 list_add(&rq->queuelist, &list);
360 e->type->ops.mq.insert_requests(hctx, &list, at_head);
361 } else {
362 spin_lock(&ctx->lock);
363 __blk_mq_insert_request(hctx, rq, at_head);
364 spin_unlock(&ctx->lock);
365 }
366
Omar Sandoval0cacba62017-02-02 15:42:39 -0800367run:
Jens Axboebd6737f2017-01-27 01:00:47 -0700368 if (run_queue)
369 blk_mq_run_hw_queue(hctx, async);
370}
371
372void blk_mq_sched_insert_requests(struct request_queue *q,
373 struct blk_mq_ctx *ctx,
374 struct list_head *list, bool run_queue_async)
375{
376 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
377 struct elevator_queue *e = hctx->queue->elevator;
378
Omar Sandoval0cacba62017-02-02 15:42:39 -0800379 if (e) {
380 struct request *rq, *next;
381
382 /*
383 * We bypass requests that already have a driver tag assigned,
384 * which should only be flushes. Flushes are only ever inserted
385 * as single requests, so we shouldn't ever hit the
386 * WARN_ON_ONCE() below (but let's handle it just in case).
387 */
388 list_for_each_entry_safe(rq, next, list, queuelist) {
389 if (WARN_ON_ONCE(rq->tag != -1)) {
390 list_del_init(&rq->queuelist);
391 blk_mq_sched_bypass_insert(hctx, rq);
392 }
393 }
394 }
395
Jens Axboebd6737f2017-01-27 01:00:47 -0700396 if (e && e->type->ops.mq.insert_requests)
397 e->type->ops.mq.insert_requests(hctx, list, false);
398 else
399 blk_mq_insert_requests(hctx, ctx, list);
400
401 blk_mq_run_hw_queue(hctx, run_queue_async);
402}
403
Jens Axboebd166ef2017-01-17 06:03:22 -0700404static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
405 struct blk_mq_hw_ctx *hctx,
406 unsigned int hctx_idx)
407{
408 if (hctx->sched_tags) {
409 blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
410 blk_mq_free_rq_map(hctx->sched_tags);
411 hctx->sched_tags = NULL;
412 }
413}
414
Omar Sandoval6917ff02017-04-05 12:01:30 -0700415static int blk_mq_sched_alloc_tags(struct request_queue *q,
416 struct blk_mq_hw_ctx *hctx,
417 unsigned int hctx_idx)
Jens Axboebd166ef2017-01-17 06:03:22 -0700418{
419 struct blk_mq_tag_set *set = q->tag_set;
Omar Sandoval6917ff02017-04-05 12:01:30 -0700420 int ret;
Jens Axboebd166ef2017-01-17 06:03:22 -0700421
Omar Sandoval6917ff02017-04-05 12:01:30 -0700422 hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
423 set->reserved_tags);
424 if (!hctx->sched_tags)
425 return -ENOMEM;
Jens Axboebd166ef2017-01-17 06:03:22 -0700426
Omar Sandoval6917ff02017-04-05 12:01:30 -0700427 ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
428 if (ret)
429 blk_mq_sched_free_tags(set, hctx, hctx_idx);
Jens Axboebd166ef2017-01-17 06:03:22 -0700430
Omar Sandoval6917ff02017-04-05 12:01:30 -0700431 return ret;
Jens Axboebd166ef2017-01-17 06:03:22 -0700432}
433
Omar Sandoval54d53292017-04-07 08:52:27 -0600434static void blk_mq_sched_tags_teardown(struct request_queue *q)
Jens Axboebd166ef2017-01-17 06:03:22 -0700435{
436 struct blk_mq_tag_set *set = q->tag_set;
437 struct blk_mq_hw_ctx *hctx;
438 int i;
439
440 queue_for_each_hw_ctx(q, hctx, i)
441 blk_mq_sched_free_tags(set, hctx, i);
442}
Jens Axboed3484992017-01-13 14:43:58 -0700443
Omar Sandoval93252632017-04-05 12:01:31 -0700444int blk_mq_sched_init_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
445 unsigned int hctx_idx)
446{
447 struct elevator_queue *e = q->elevator;
Omar Sandovalee056f92017-04-05 12:01:34 -0700448 int ret;
Omar Sandoval93252632017-04-05 12:01:31 -0700449
450 if (!e)
451 return 0;
452
Omar Sandovalee056f92017-04-05 12:01:34 -0700453 ret = blk_mq_sched_alloc_tags(q, hctx, hctx_idx);
454 if (ret)
455 return ret;
456
457 if (e->type->ops.mq.init_hctx) {
458 ret = e->type->ops.mq.init_hctx(hctx, hctx_idx);
459 if (ret) {
460 blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
461 return ret;
462 }
463 }
464
Omar Sandovald332ce02017-05-04 08:24:40 -0600465 blk_mq_debugfs_register_sched_hctx(q, hctx);
466
Omar Sandovalee056f92017-04-05 12:01:34 -0700467 return 0;
Omar Sandoval93252632017-04-05 12:01:31 -0700468}
469
470void blk_mq_sched_exit_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
471 unsigned int hctx_idx)
472{
473 struct elevator_queue *e = q->elevator;
474
475 if (!e)
476 return;
477
Omar Sandovald332ce02017-05-04 08:24:40 -0600478 blk_mq_debugfs_unregister_sched_hctx(hctx);
479
Omar Sandovalee056f92017-04-05 12:01:34 -0700480 if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
481 e->type->ops.mq.exit_hctx(hctx, hctx_idx);
482 hctx->sched_data = NULL;
483 }
484
Omar Sandoval93252632017-04-05 12:01:31 -0700485 blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
486}
487
Omar Sandoval6917ff02017-04-05 12:01:30 -0700488int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
489{
490 struct blk_mq_hw_ctx *hctx;
Omar Sandovalee056f92017-04-05 12:01:34 -0700491 struct elevator_queue *eq;
Omar Sandoval6917ff02017-04-05 12:01:30 -0700492 unsigned int i;
493 int ret;
494
495 if (!e) {
496 q->elevator = NULL;
497 return 0;
498 }
499
500 /*
501 * Default to 256, since we don't split into sync/async like the
502 * old code did. Additionally, this is a per-hw queue depth.
503 */
504 q->nr_requests = 2 * BLKDEV_MAX_RQ;
505
506 queue_for_each_hw_ctx(q, hctx, i) {
507 ret = blk_mq_sched_alloc_tags(q, hctx, i);
508 if (ret)
509 goto err;
510 }
511
512 ret = e->ops.mq.init_sched(q, e);
513 if (ret)
514 goto err;
515
Omar Sandovald332ce02017-05-04 08:24:40 -0600516 blk_mq_debugfs_register_sched(q);
517
518 queue_for_each_hw_ctx(q, hctx, i) {
519 if (e->ops.mq.init_hctx) {
Omar Sandovalee056f92017-04-05 12:01:34 -0700520 ret = e->ops.mq.init_hctx(hctx, i);
521 if (ret) {
522 eq = q->elevator;
523 blk_mq_exit_sched(q, eq);
524 kobject_put(&eq->kobj);
525 return ret;
526 }
527 }
Omar Sandovald332ce02017-05-04 08:24:40 -0600528 blk_mq_debugfs_register_sched_hctx(q, hctx);
Omar Sandovalee056f92017-04-05 12:01:34 -0700529 }
530
Omar Sandoval6917ff02017-04-05 12:01:30 -0700531 return 0;
532
533err:
Omar Sandoval54d53292017-04-07 08:52:27 -0600534 blk_mq_sched_tags_teardown(q);
535 q->elevator = NULL;
Omar Sandoval6917ff02017-04-05 12:01:30 -0700536 return ret;
537}
538
Omar Sandoval54d53292017-04-07 08:52:27 -0600539void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
540{
Omar Sandovalee056f92017-04-05 12:01:34 -0700541 struct blk_mq_hw_ctx *hctx;
542 unsigned int i;
543
Omar Sandovald332ce02017-05-04 08:24:40 -0600544 queue_for_each_hw_ctx(q, hctx, i) {
545 blk_mq_debugfs_unregister_sched_hctx(hctx);
546 if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
547 e->type->ops.mq.exit_hctx(hctx, i);
548 hctx->sched_data = NULL;
Omar Sandovalee056f92017-04-05 12:01:34 -0700549 }
550 }
Omar Sandovald332ce02017-05-04 08:24:40 -0600551 blk_mq_debugfs_unregister_sched(q);
Omar Sandoval54d53292017-04-07 08:52:27 -0600552 if (e->type->ops.mq.exit_sched)
553 e->type->ops.mq.exit_sched(e);
554 blk_mq_sched_tags_teardown(q);
555 q->elevator = NULL;
556}
557
Jens Axboed3484992017-01-13 14:43:58 -0700558int blk_mq_sched_init(struct request_queue *q)
559{
560 int ret;
561
Jens Axboed3484992017-01-13 14:43:58 -0700562 mutex_lock(&q->sysfs_lock);
563 ret = elevator_init(q, NULL);
564 mutex_unlock(&q->sysfs_lock);
565
566 return ret;
567}