blob: 9e3c0f92851b2bcfcb6a1740aaefca3a4c00a0a7 [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"
14#include "blk-mq-sched.h"
15#include "blk-mq-tag.h"
16#include "blk-wbt.h"
17
18void blk_mq_sched_free_hctx_data(struct request_queue *q,
19 void (*exit)(struct blk_mq_hw_ctx *))
20{
21 struct blk_mq_hw_ctx *hctx;
22 int i;
23
24 queue_for_each_hw_ctx(q, hctx, i) {
25 if (exit && hctx->sched_data)
26 exit(hctx);
27 kfree(hctx->sched_data);
28 hctx->sched_data = NULL;
29 }
30}
31EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data);
32
Jens Axboebd166ef2017-01-17 06:03:22 -070033static void __blk_mq_sched_assign_ioc(struct request_queue *q,
Paolo Valentef1ba8262017-02-07 18:24:43 +010034 struct request *rq,
35 struct bio *bio,
36 struct io_context *ioc)
Jens Axboebd166ef2017-01-17 06:03:22 -070037{
38 struct io_cq *icq;
39
40 spin_lock_irq(q->queue_lock);
41 icq = ioc_lookup_icq(ioc, q);
42 spin_unlock_irq(q->queue_lock);
43
44 if (!icq) {
45 icq = ioc_create_icq(ioc, q, GFP_ATOMIC);
46 if (!icq)
47 return;
48 }
49
50 rq->elv.icq = icq;
Paolo Valentef1ba8262017-02-07 18:24:43 +010051 if (!blk_mq_sched_get_rq_priv(q, rq, bio)) {
Jens Axboebd166ef2017-01-17 06:03:22 -070052 rq->rq_flags |= RQF_ELVPRIV;
53 get_io_context(icq->ioc);
54 return;
55 }
56
57 rq->elv.icq = NULL;
58}
59
60static void blk_mq_sched_assign_ioc(struct request_queue *q,
61 struct request *rq, struct bio *bio)
62{
63 struct io_context *ioc;
64
65 ioc = rq_ioc(bio);
66 if (ioc)
Paolo Valentef1ba8262017-02-07 18:24:43 +010067 __blk_mq_sched_assign_ioc(q, rq, bio, ioc);
Jens Axboebd166ef2017-01-17 06:03:22 -070068}
69
70struct request *blk_mq_sched_get_request(struct request_queue *q,
71 struct bio *bio,
72 unsigned int op,
73 struct blk_mq_alloc_data *data)
74{
75 struct elevator_queue *e = q->elevator;
Jens Axboebd166ef2017-01-17 06:03:22 -070076 struct request *rq;
Jens Axboebd166ef2017-01-17 06:03:22 -070077
78 blk_queue_enter_live(q);
Omar Sandoval6d2809d2017-02-27 10:28:27 -080079 data->q = q;
80 if (likely(!data->ctx))
81 data->ctx = blk_mq_get_ctx(q);
82 if (likely(!data->hctx))
83 data->hctx = blk_mq_map_queue(q, data->ctx->cpu);
Jens Axboebd166ef2017-01-17 06:03:22 -070084
85 if (e) {
86 data->flags |= BLK_MQ_REQ_INTERNAL;
87
88 /*
89 * Flush requests are special and go directly to the
90 * dispatch list.
91 */
Christoph Hellwigf73f44e2017-01-27 08:30:47 -070092 if (!op_is_flush(op) && e->type->ops.mq.get_request) {
Jens Axboebd166ef2017-01-17 06:03:22 -070093 rq = e->type->ops.mq.get_request(q, op, data);
94 if (rq)
95 rq->rq_flags |= RQF_QUEUED;
96 } else
97 rq = __blk_mq_alloc_request(data, op);
98 } else {
99 rq = __blk_mq_alloc_request(data, op);
Jens Axboebd166ef2017-01-17 06:03:22 -0700100 }
101
102 if (rq) {
Christoph Hellwigf73f44e2017-01-27 08:30:47 -0700103 if (!op_is_flush(op)) {
Jens Axboebd166ef2017-01-17 06:03:22 -0700104 rq->elv.icq = NULL;
105 if (e && e->type->icq_cache)
106 blk_mq_sched_assign_ioc(q, rq, bio);
107 }
108 data->hctx->queued++;
109 return rq;
110 }
111
112 blk_queue_exit(q);
113 return NULL;
114}
115
116void blk_mq_sched_put_request(struct request *rq)
117{
118 struct request_queue *q = rq->q;
119 struct elevator_queue *e = q->elevator;
120
121 if (rq->rq_flags & RQF_ELVPRIV) {
122 blk_mq_sched_put_rq_priv(rq->q, rq);
123 if (rq->elv.icq) {
124 put_io_context(rq->elv.icq->ioc);
125 rq->elv.icq = NULL;
126 }
127 }
128
129 if ((rq->rq_flags & RQF_QUEUED) && e && e->type->ops.mq.put_request)
130 e->type->ops.mq.put_request(rq);
131 else
132 blk_mq_finish_request(rq);
133}
134
135void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
136{
Omar Sandoval81380ca2017-04-07 08:56:26 -0600137 struct request_queue *q = hctx->queue;
138 struct elevator_queue *e = q->elevator;
Jens Axboe64765a72017-02-17 11:39:26 -0700139 const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request;
140 bool did_work = false;
Jens Axboebd166ef2017-01-17 06:03:22 -0700141 LIST_HEAD(rq_list);
142
143 if (unlikely(blk_mq_hctx_stopped(hctx)))
144 return;
145
146 hctx->run++;
147
148 /*
149 * If we have previous entries on our dispatch list, grab them first for
150 * more fair dispatch.
151 */
152 if (!list_empty_careful(&hctx->dispatch)) {
153 spin_lock(&hctx->lock);
154 if (!list_empty(&hctx->dispatch))
155 list_splice_init(&hctx->dispatch, &rq_list);
156 spin_unlock(&hctx->lock);
157 }
158
159 /*
160 * Only ask the scheduler for requests, if we didn't have residual
161 * requests from the dispatch list. This is to avoid the case where
162 * we only ever dispatch a fraction of the requests available because
163 * of low device queue depth. Once we pull requests out of the IO
164 * scheduler, we can no longer merge or sort them. So it's best to
165 * leave them there for as long as we can. Mark the hw queue as
166 * needing a restart in that case.
167 */
Jens Axboec13660a2017-01-26 12:40:07 -0700168 if (!list_empty(&rq_list)) {
Omar Sandovald38d3512017-02-22 10:58:30 -0800169 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandoval81380ca2017-04-07 08:56:26 -0600170 did_work = blk_mq_dispatch_rq_list(q, &rq_list);
Jens Axboe64765a72017-02-17 11:39:26 -0700171 } else if (!has_sched_dispatch) {
Jens Axboec13660a2017-01-26 12:40:07 -0700172 blk_mq_flush_busy_ctxs(hctx, &rq_list);
Omar Sandoval81380ca2017-04-07 08:56:26 -0600173 blk_mq_dispatch_rq_list(q, &rq_list);
Jens Axboe64765a72017-02-17 11:39:26 -0700174 }
175
176 /*
177 * We want to dispatch from the scheduler if we had no work left
178 * on the dispatch list, OR if we did have work but weren't able
179 * to make progress.
180 */
181 if (!did_work && has_sched_dispatch) {
Jens Axboec13660a2017-01-26 12:40:07 -0700182 do {
183 struct request *rq;
Jens Axboebd166ef2017-01-17 06:03:22 -0700184
Jens Axboec13660a2017-01-26 12:40:07 -0700185 rq = e->type->ops.mq.dispatch_request(hctx);
186 if (!rq)
187 break;
188 list_add(&rq->queuelist, &rq_list);
Omar Sandoval81380ca2017-04-07 08:56:26 -0600189 } while (blk_mq_dispatch_rq_list(q, &rq_list));
Jens Axboec13660a2017-01-26 12:40:07 -0700190 }
Jens Axboebd166ef2017-01-17 06:03:22 -0700191}
192
193void blk_mq_sched_move_to_dispatch(struct blk_mq_hw_ctx *hctx,
194 struct list_head *rq_list,
195 struct request *(*get_rq)(struct blk_mq_hw_ctx *))
196{
197 do {
198 struct request *rq;
199
200 rq = get_rq(hctx);
201 if (!rq)
202 break;
203
204 list_add_tail(&rq->queuelist, rq_list);
205 } while (1);
206}
207EXPORT_SYMBOL_GPL(blk_mq_sched_move_to_dispatch);
208
Jens Axboee4d750c2017-02-03 09:48:28 -0700209bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
210 struct request **merged_request)
Jens Axboebd166ef2017-01-17 06:03:22 -0700211{
212 struct request *rq;
Jens Axboebd166ef2017-01-17 06:03:22 -0700213
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100214 switch (elv_merge(q, &rq, bio)) {
215 case ELEVATOR_BACK_MERGE:
Jens Axboebd166ef2017-01-17 06:03:22 -0700216 if (!blk_mq_sched_allow_merge(q, rq, bio))
217 return false;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100218 if (!bio_attempt_back_merge(q, rq, bio))
219 return false;
220 *merged_request = attempt_back_merge(q, rq);
221 if (!*merged_request)
222 elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
223 return true;
224 case ELEVATOR_FRONT_MERGE:
Jens Axboebd166ef2017-01-17 06:03:22 -0700225 if (!blk_mq_sched_allow_merge(q, rq, bio))
226 return false;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100227 if (!bio_attempt_front_merge(q, rq, bio))
228 return false;
229 *merged_request = attempt_front_merge(q, rq);
230 if (!*merged_request)
231 elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
232 return true;
233 default:
234 return false;
Jens Axboebd166ef2017-01-17 06:03:22 -0700235 }
Jens Axboebd166ef2017-01-17 06:03:22 -0700236}
237EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
238
239bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio)
240{
241 struct elevator_queue *e = q->elevator;
242
243 if (e->type->ops.mq.bio_merge) {
244 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
245 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
246
247 blk_mq_put_ctx(ctx);
248 return e->type->ops.mq.bio_merge(hctx, bio);
249 }
250
251 return false;
252}
253
254bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
255{
256 return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
257}
258EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
259
260void blk_mq_sched_request_inserted(struct request *rq)
261{
262 trace_block_rq_insert(rq->q, rq);
263}
264EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
265
Omar Sandoval0cacba62017-02-02 15:42:39 -0800266static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
267 struct request *rq)
Jens Axboebd166ef2017-01-17 06:03:22 -0700268{
269 if (rq->tag == -1) {
270 rq->rq_flags |= RQF_SORTED;
271 return false;
272 }
273
274 /*
275 * If we already have a real request tag, send directly to
276 * the dispatch list.
277 */
278 spin_lock(&hctx->lock);
279 list_add(&rq->queuelist, &hctx->dispatch);
280 spin_unlock(&hctx->lock);
281 return true;
282}
Jens Axboebd166ef2017-01-17 06:03:22 -0700283
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600284static bool blk_mq_sched_restart_hctx(struct blk_mq_hw_ctx *hctx)
Jens Axboe50e1dab2017-01-26 14:42:34 -0700285{
286 if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) {
287 clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600288 if (blk_mq_hctx_has_pending(hctx)) {
Jens Axboe50e1dab2017-01-26 14:42:34 -0700289 blk_mq_run_hw_queue(hctx, true);
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600290 return true;
291 }
Jens Axboe50e1dab2017-01-26 14:42:34 -0700292 }
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600293 return false;
Jens Axboe50e1dab2017-01-26 14:42:34 -0700294}
295
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600296/**
297 * list_for_each_entry_rcu_rr - iterate in a round-robin fashion over rcu list
298 * @pos: loop cursor.
299 * @skip: the list element that will not be examined. Iteration starts at
300 * @skip->next.
301 * @head: head of the list to examine. This list must have at least one
302 * element, namely @skip.
303 * @member: name of the list_head structure within typeof(*pos).
304 */
305#define list_for_each_entry_rcu_rr(pos, skip, head, member) \
306 for ((pos) = (skip); \
307 (pos = (pos)->member.next != (head) ? list_entry_rcu( \
308 (pos)->member.next, typeof(*pos), member) : \
309 list_entry_rcu((pos)->member.next->next, typeof(*pos), member)), \
310 (pos) != (skip); )
Jens Axboe50e1dab2017-01-26 14:42:34 -0700311
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600312/*
313 * Called after a driver tag has been freed to check whether a hctx needs to
314 * be restarted. Restarts @hctx if its tag set is not shared. Restarts hardware
315 * queues in a round-robin fashion if the tag set of @hctx is shared with other
316 * hardware queues.
317 */
318void blk_mq_sched_restart(struct blk_mq_hw_ctx *const hctx)
319{
320 struct blk_mq_tags *const tags = hctx->tags;
321 struct blk_mq_tag_set *const set = hctx->queue->tag_set;
322 struct request_queue *const queue = hctx->queue, *q;
323 struct blk_mq_hw_ctx *hctx2;
324 unsigned int i, j;
325
326 if (set->flags & BLK_MQ_F_TAG_SHARED) {
327 rcu_read_lock();
328 list_for_each_entry_rcu_rr(q, queue, &set->tag_list,
329 tag_set_list) {
330 queue_for_each_hw_ctx(q, hctx2, i)
331 if (hctx2->tags == tags &&
332 blk_mq_sched_restart_hctx(hctx2))
333 goto done;
Omar Sandovald38d3512017-02-22 10:58:30 -0800334 }
Bart Van Assche6d8c6c02017-04-07 12:40:09 -0600335 j = hctx->queue_num + 1;
336 for (i = 0; i < queue->nr_hw_queues; i++, j++) {
337 if (j == queue->nr_hw_queues)
338 j = 0;
339 hctx2 = queue->queue_hw_ctx[j];
340 if (hctx2->tags == tags &&
341 blk_mq_sched_restart_hctx(hctx2))
342 break;
343 }
344done:
345 rcu_read_unlock();
Omar Sandovald38d3512017-02-22 10:58:30 -0800346 } else {
Jens Axboe50e1dab2017-01-26 14:42:34 -0700347 blk_mq_sched_restart_hctx(hctx);
Jens Axboe50e1dab2017-01-26 14:42:34 -0700348 }
349}
350
Jens Axboebd6737f2017-01-27 01:00:47 -0700351/*
352 * Add flush/fua to the queue. If we fail getting a driver tag, then
353 * punt to the requeue list. Requeue will re-invoke us from a context
354 * that's safe to block from.
355 */
356static void blk_mq_sched_insert_flush(struct blk_mq_hw_ctx *hctx,
357 struct request *rq, bool can_block)
358{
359 if (blk_mq_get_driver_tag(rq, &hctx, can_block)) {
360 blk_insert_flush(rq);
361 blk_mq_run_hw_queue(hctx, true);
362 } else
Jens Axboec7a571b2017-02-17 11:37:14 -0700363 blk_mq_add_to_requeue_list(rq, false, true);
Jens Axboebd6737f2017-01-27 01:00:47 -0700364}
365
366void blk_mq_sched_insert_request(struct request *rq, bool at_head,
367 bool run_queue, bool async, bool can_block)
368{
369 struct request_queue *q = rq->q;
370 struct elevator_queue *e = q->elevator;
371 struct blk_mq_ctx *ctx = rq->mq_ctx;
372 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
373
Jens Axboef3a8ab72017-01-27 09:08:23 -0700374 if (rq->tag == -1 && op_is_flush(rq->cmd_flags)) {
Jens Axboebd6737f2017-01-27 01:00:47 -0700375 blk_mq_sched_insert_flush(hctx, rq, can_block);
376 return;
377 }
378
Omar Sandoval0cacba62017-02-02 15:42:39 -0800379 if (e && blk_mq_sched_bypass_insert(hctx, rq))
380 goto run;
381
Jens Axboebd6737f2017-01-27 01:00:47 -0700382 if (e && e->type->ops.mq.insert_requests) {
383 LIST_HEAD(list);
384
385 list_add(&rq->queuelist, &list);
386 e->type->ops.mq.insert_requests(hctx, &list, at_head);
387 } else {
388 spin_lock(&ctx->lock);
389 __blk_mq_insert_request(hctx, rq, at_head);
390 spin_unlock(&ctx->lock);
391 }
392
Omar Sandoval0cacba62017-02-02 15:42:39 -0800393run:
Jens Axboebd6737f2017-01-27 01:00:47 -0700394 if (run_queue)
395 blk_mq_run_hw_queue(hctx, async);
396}
397
398void blk_mq_sched_insert_requests(struct request_queue *q,
399 struct blk_mq_ctx *ctx,
400 struct list_head *list, bool run_queue_async)
401{
402 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
403 struct elevator_queue *e = hctx->queue->elevator;
404
Omar Sandoval0cacba62017-02-02 15:42:39 -0800405 if (e) {
406 struct request *rq, *next;
407
408 /*
409 * We bypass requests that already have a driver tag assigned,
410 * which should only be flushes. Flushes are only ever inserted
411 * as single requests, so we shouldn't ever hit the
412 * WARN_ON_ONCE() below (but let's handle it just in case).
413 */
414 list_for_each_entry_safe(rq, next, list, queuelist) {
415 if (WARN_ON_ONCE(rq->tag != -1)) {
416 list_del_init(&rq->queuelist);
417 blk_mq_sched_bypass_insert(hctx, rq);
418 }
419 }
420 }
421
Jens Axboebd6737f2017-01-27 01:00:47 -0700422 if (e && e->type->ops.mq.insert_requests)
423 e->type->ops.mq.insert_requests(hctx, list, false);
424 else
425 blk_mq_insert_requests(hctx, ctx, list);
426
427 blk_mq_run_hw_queue(hctx, run_queue_async);
428}
429
Jens Axboebd166ef2017-01-17 06:03:22 -0700430static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
431 struct blk_mq_hw_ctx *hctx,
432 unsigned int hctx_idx)
433{
434 if (hctx->sched_tags) {
435 blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
436 blk_mq_free_rq_map(hctx->sched_tags);
437 hctx->sched_tags = NULL;
438 }
439}
440
Omar Sandoval6917ff02017-04-05 12:01:30 -0700441static int blk_mq_sched_alloc_tags(struct request_queue *q,
442 struct blk_mq_hw_ctx *hctx,
443 unsigned int hctx_idx)
Jens Axboebd166ef2017-01-17 06:03:22 -0700444{
445 struct blk_mq_tag_set *set = q->tag_set;
Omar Sandoval6917ff02017-04-05 12:01:30 -0700446 int ret;
Jens Axboebd166ef2017-01-17 06:03:22 -0700447
Omar Sandoval6917ff02017-04-05 12:01:30 -0700448 hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
449 set->reserved_tags);
450 if (!hctx->sched_tags)
451 return -ENOMEM;
Jens Axboebd166ef2017-01-17 06:03:22 -0700452
Omar Sandoval6917ff02017-04-05 12:01:30 -0700453 ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
454 if (ret)
455 blk_mq_sched_free_tags(set, hctx, hctx_idx);
Jens Axboebd166ef2017-01-17 06:03:22 -0700456
Omar Sandoval6917ff02017-04-05 12:01:30 -0700457 return ret;
Jens Axboebd166ef2017-01-17 06:03:22 -0700458}
459
Omar Sandoval54d53292017-04-07 08:52:27 -0600460static void blk_mq_sched_tags_teardown(struct request_queue *q)
Jens Axboebd166ef2017-01-17 06:03:22 -0700461{
462 struct blk_mq_tag_set *set = q->tag_set;
463 struct blk_mq_hw_ctx *hctx;
464 int i;
465
466 queue_for_each_hw_ctx(q, hctx, i)
467 blk_mq_sched_free_tags(set, hctx, i);
468}
Jens Axboed3484992017-01-13 14:43:58 -0700469
Omar Sandoval93252632017-04-05 12:01:31 -0700470int blk_mq_sched_init_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
471 unsigned int hctx_idx)
472{
473 struct elevator_queue *e = q->elevator;
Omar Sandovalee056f92017-04-05 12:01:34 -0700474 int ret;
Omar Sandoval93252632017-04-05 12:01:31 -0700475
476 if (!e)
477 return 0;
478
Omar Sandovalee056f92017-04-05 12:01:34 -0700479 ret = blk_mq_sched_alloc_tags(q, hctx, hctx_idx);
480 if (ret)
481 return ret;
482
483 if (e->type->ops.mq.init_hctx) {
484 ret = e->type->ops.mq.init_hctx(hctx, hctx_idx);
485 if (ret) {
486 blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
487 return ret;
488 }
489 }
490
491 return 0;
Omar Sandoval93252632017-04-05 12:01:31 -0700492}
493
494void blk_mq_sched_exit_hctx(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
495 unsigned int hctx_idx)
496{
497 struct elevator_queue *e = q->elevator;
498
499 if (!e)
500 return;
501
Omar Sandovalee056f92017-04-05 12:01:34 -0700502 if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
503 e->type->ops.mq.exit_hctx(hctx, hctx_idx);
504 hctx->sched_data = NULL;
505 }
506
Omar Sandoval93252632017-04-05 12:01:31 -0700507 blk_mq_sched_free_tags(q->tag_set, hctx, hctx_idx);
508}
509
Omar Sandoval6917ff02017-04-05 12:01:30 -0700510int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
511{
512 struct blk_mq_hw_ctx *hctx;
Omar Sandovalee056f92017-04-05 12:01:34 -0700513 struct elevator_queue *eq;
Omar Sandoval6917ff02017-04-05 12:01:30 -0700514 unsigned int i;
515 int ret;
516
517 if (!e) {
518 q->elevator = NULL;
519 return 0;
520 }
521
522 /*
523 * Default to 256, since we don't split into sync/async like the
524 * old code did. Additionally, this is a per-hw queue depth.
525 */
526 q->nr_requests = 2 * BLKDEV_MAX_RQ;
527
528 queue_for_each_hw_ctx(q, hctx, i) {
529 ret = blk_mq_sched_alloc_tags(q, hctx, i);
530 if (ret)
531 goto err;
532 }
533
534 ret = e->ops.mq.init_sched(q, e);
535 if (ret)
536 goto err;
537
Omar Sandovalee056f92017-04-05 12:01:34 -0700538 if (e->ops.mq.init_hctx) {
539 queue_for_each_hw_ctx(q, hctx, i) {
540 ret = e->ops.mq.init_hctx(hctx, i);
541 if (ret) {
542 eq = q->elevator;
543 blk_mq_exit_sched(q, eq);
544 kobject_put(&eq->kobj);
545 return ret;
546 }
547 }
548 }
549
Omar Sandoval6917ff02017-04-05 12:01:30 -0700550 return 0;
551
552err:
Omar Sandoval54d53292017-04-07 08:52:27 -0600553 blk_mq_sched_tags_teardown(q);
554 q->elevator = NULL;
Omar Sandoval6917ff02017-04-05 12:01:30 -0700555 return ret;
556}
557
Omar Sandoval54d53292017-04-07 08:52:27 -0600558void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
559{
Omar Sandovalee056f92017-04-05 12:01:34 -0700560 struct blk_mq_hw_ctx *hctx;
561 unsigned int i;
562
563 if (e->type->ops.mq.exit_hctx) {
564 queue_for_each_hw_ctx(q, hctx, i) {
565 if (hctx->sched_data) {
566 e->type->ops.mq.exit_hctx(hctx, i);
567 hctx->sched_data = NULL;
568 }
569 }
570 }
Omar Sandoval54d53292017-04-07 08:52:27 -0600571 if (e->type->ops.mq.exit_sched)
572 e->type->ops.mq.exit_sched(e);
573 blk_mq_sched_tags_teardown(q);
574 q->elevator = NULL;
575}
576
Jens Axboed3484992017-01-13 14:43:58 -0700577int blk_mq_sched_init(struct request_queue *q)
578{
579 int ret;
580
Jens Axboed3484992017-01-13 14:43:58 -0700581 mutex_lock(&q->sysfs_lock);
582 ret = elevator_init(q, NULL);
583 mutex_unlock(&q->sysfs_lock);
584
585 return ret;
586}