xref: /linux/block/blk-mq-sched.c (revision 3e9e952bb3139ad1e08f3e1960239c2988ab90c9)
13dcf60bcSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2bd166ef1SJens Axboe /*
3bd166ef1SJens Axboe  * blk-mq scheduling framework
4bd166ef1SJens Axboe  *
5bd166ef1SJens Axboe  * Copyright (C) 2016 Jens Axboe
6bd166ef1SJens Axboe  */
7bd166ef1SJens Axboe #include <linux/kernel.h>
8bd166ef1SJens Axboe #include <linux/module.h>
96e6fcbc2SMing Lei #include <linux/list_sort.h>
10bd166ef1SJens Axboe 
11bd166ef1SJens Axboe #include <trace/events/block.h>
12bd166ef1SJens Axboe 
13bd166ef1SJens Axboe #include "blk.h"
14bd166ef1SJens Axboe #include "blk-mq.h"
15d332ce09SOmar Sandoval #include "blk-mq-debugfs.h"
16bd166ef1SJens Axboe #include "blk-mq-sched.h"
17bd166ef1SJens Axboe #include "blk-wbt.h"
18bd166ef1SJens Axboe 
198e8320c9SJens Axboe /*
20c31e76bcSKemeng Shi  * Mark a hardware queue as needing a restart.
218e8320c9SJens Axboe  */
blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx * hctx)227211aef8SDamien Le Moal void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx)
238e8320c9SJens Axboe {
248e8320c9SJens Axboe 	if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
258e8320c9SJens Axboe 		return;
268e8320c9SJens Axboe 
278e8320c9SJens Axboe 	set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
288e8320c9SJens Axboe }
297211aef8SDamien Le Moal EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx);
308e8320c9SJens Axboe 
__blk_mq_sched_restart(struct blk_mq_hw_ctx * hctx)31e9ea1596SPavel Begunkov void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
328e8320c9SJens Axboe {
338e8320c9SJens Axboe 	clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
348e8320c9SJens Axboe 
35d7d8535fSMing Lei 	/*
36d7d8535fSMing Lei 	 * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch)
37d7d8535fSMing Lei 	 * in blk_mq_run_hw_queue(). Its pair is the barrier in
38d7d8535fSMing Lei 	 * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART,
39d7d8535fSMing Lei 	 * meantime new request added to hctx->dispatch is missed to check in
40d7d8535fSMing Lei 	 * blk_mq_run_hw_queue().
41d7d8535fSMing Lei 	 */
42d7d8535fSMing Lei 	smp_mb();
43d7d8535fSMing Lei 
4497889f9aSMing Lei 	blk_mq_run_hw_queue(hctx, true);
458e8320c9SJens Axboe }
468e8320c9SJens Axboe 
sched_rq_cmp(void * priv,const struct list_head * a,const struct list_head * b)474f0f586bSSami Tolvanen static int sched_rq_cmp(void *priv, const struct list_head *a,
484f0f586bSSami Tolvanen 			const struct list_head *b)
496e6fcbc2SMing Lei {
506e6fcbc2SMing Lei 	struct request *rqa = container_of(a, struct request, queuelist);
516e6fcbc2SMing Lei 	struct request *rqb = container_of(b, struct request, queuelist);
526e6fcbc2SMing Lei 
536e6fcbc2SMing Lei 	return rqa->mq_hctx > rqb->mq_hctx;
546e6fcbc2SMing Lei }
556e6fcbc2SMing Lei 
blk_mq_dispatch_hctx_list(struct list_head * rq_list)566e6fcbc2SMing Lei static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list)
576e6fcbc2SMing Lei {
586e6fcbc2SMing Lei 	struct blk_mq_hw_ctx *hctx =
596e6fcbc2SMing Lei 		list_first_entry(rq_list, struct request, queuelist)->mq_hctx;
606e6fcbc2SMing Lei 	struct request *rq;
616e6fcbc2SMing Lei 	LIST_HEAD(hctx_list);
626e6fcbc2SMing Lei 
636e6fcbc2SMing Lei 	list_for_each_entry(rq, rq_list, queuelist) {
646e6fcbc2SMing Lei 		if (rq->mq_hctx != hctx) {
656e6fcbc2SMing Lei 			list_cut_before(&hctx_list, rq_list, &rq->queuelist);
666e6fcbc2SMing Lei 			goto dispatch;
676e6fcbc2SMing Lei 		}
686e6fcbc2SMing Lei 	}
696e6fcbc2SMing Lei 	list_splice_tail_init(rq_list, &hctx_list);
706e6fcbc2SMing Lei 
716e6fcbc2SMing Lei dispatch:
72e093b784SBart Van Assche 	return blk_mq_dispatch_rq_list(hctx, &hctx_list, false);
736e6fcbc2SMing Lei }
746e6fcbc2SMing Lei 
75a0823421SDouglas Anderson #define BLK_MQ_BUDGET_DELAY	3		/* ms units */
76a0823421SDouglas Anderson 
771f460b63SMing Lei /*
781f460b63SMing Lei  * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
791f460b63SMing Lei  * its queue by itself in its completion handler, so we don't need to
8001542f65SKemeng Shi  * restart queue if .get_budget() fails to get the budget.
8128d65729SSalman Qazi  *
8228d65729SSalman Qazi  * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to
8328d65729SSalman Qazi  * be run again.  This is necessary to avoid starving flushes.
841f460b63SMing Lei  */
__blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx * hctx)856e6fcbc2SMing Lei static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
86caf8eb0dSMing Lei {
87caf8eb0dSMing Lei 	struct request_queue *q = hctx->queue;
88caf8eb0dSMing Lei 	struct elevator_queue *e = q->elevator;
896e6fcbc2SMing Lei 	bool multi_hctxs = false, run_queue = false;
906e6fcbc2SMing Lei 	bool dispatched = false, busy = false;
916e6fcbc2SMing Lei 	unsigned int max_dispatch;
92caf8eb0dSMing Lei 	LIST_HEAD(rq_list);
936e6fcbc2SMing Lei 	int count = 0;
946e6fcbc2SMing Lei 
956e6fcbc2SMing Lei 	if (hctx->dispatch_busy)
966e6fcbc2SMing Lei 		max_dispatch = 1;
976e6fcbc2SMing Lei 	else
986e6fcbc2SMing Lei 		max_dispatch = hctx->queue->nr_requests;
99caf8eb0dSMing Lei 
100445874e8SMing Lei 	do {
1016e6fcbc2SMing Lei 		struct request *rq;
1022a5a24aaSMing Lei 		int budget_token;
1036e6fcbc2SMing Lei 
104f9cd4bfeSJens Axboe 		if (e->type->ops.has_work && !e->type->ops.has_work(hctx))
105caf8eb0dSMing Lei 			break;
106de148297SMing Lei 
10728d65729SSalman Qazi 		if (!list_empty_careful(&hctx->dispatch)) {
1086e6fcbc2SMing Lei 			busy = true;
10928d65729SSalman Qazi 			break;
11028d65729SSalman Qazi 		}
11128d65729SSalman Qazi 
1122a5a24aaSMing Lei 		budget_token = blk_mq_get_dispatch_budget(q);
1132a5a24aaSMing Lei 		if (budget_token < 0)
1141f460b63SMing Lei 			break;
115de148297SMing Lei 
116f9cd4bfeSJens Axboe 		rq = e->type->ops.dispatch_request(hctx);
117de148297SMing Lei 		if (!rq) {
1182a5a24aaSMing Lei 			blk_mq_put_dispatch_budget(q, budget_token);
119a0823421SDouglas Anderson 			/*
120a0823421SDouglas Anderson 			 * We're releasing without dispatching. Holding the
121a0823421SDouglas Anderson 			 * budget could have blocked any "hctx"s with the
122a0823421SDouglas Anderson 			 * same queue and if we didn't dispatch then there's
123a0823421SDouglas Anderson 			 * no guarantee anyone will kick the queue.  Kick it
124a0823421SDouglas Anderson 			 * ourselves.
125a0823421SDouglas Anderson 			 */
1266e6fcbc2SMing Lei 			run_queue = true;
127de148297SMing Lei 			break;
128caf8eb0dSMing Lei 		}
129caf8eb0dSMing Lei 
1302a5a24aaSMing Lei 		blk_mq_set_rq_budget_token(rq, budget_token);
1312a5a24aaSMing Lei 
132de148297SMing Lei 		/*
133de148297SMing Lei 		 * Now this rq owns the budget which has to be released
134de148297SMing Lei 		 * if this rq won't be queued to driver via .queue_rq()
135de148297SMing Lei 		 * in blk_mq_dispatch_rq_list().
136de148297SMing Lei 		 */
1376e6fcbc2SMing Lei 		list_add_tail(&rq->queuelist, &rq_list);
13861347154SJan Kara 		count++;
1396e6fcbc2SMing Lei 		if (rq->mq_hctx != hctx)
1406e6fcbc2SMing Lei 			multi_hctxs = true;
14161347154SJan Kara 
14261347154SJan Kara 		/*
14361347154SJan Kara 		 * If we cannot get tag for the request, stop dequeueing
14461347154SJan Kara 		 * requests from the IO scheduler. We are unlikely to be able
14561347154SJan Kara 		 * to submit them anyway and it creates false impression for
14661347154SJan Kara 		 * scheduling heuristics that the device can take more IO.
14761347154SJan Kara 		 */
14861347154SJan Kara 		if (!blk_mq_get_driver_tag(rq))
14961347154SJan Kara 			break;
15061347154SJan Kara 	} while (count < max_dispatch);
1516e6fcbc2SMing Lei 
1526e6fcbc2SMing Lei 	if (!count) {
1536e6fcbc2SMing Lei 		if (run_queue)
1546e6fcbc2SMing Lei 			blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY);
1556e6fcbc2SMing Lei 	} else if (multi_hctxs) {
1566e6fcbc2SMing Lei 		/*
1576e6fcbc2SMing Lei 		 * Requests from different hctx may be dequeued from some
1586e6fcbc2SMing Lei 		 * schedulers, such as bfq and deadline.
1596e6fcbc2SMing Lei 		 *
1606e6fcbc2SMing Lei 		 * Sort the requests in the list according to their hctx,
1616e6fcbc2SMing Lei 		 * dispatch batching requests from same hctx at a time.
1626e6fcbc2SMing Lei 		 */
1636e6fcbc2SMing Lei 		list_sort(NULL, &rq_list, sched_rq_cmp);
1646e6fcbc2SMing Lei 		do {
1656e6fcbc2SMing Lei 			dispatched |= blk_mq_dispatch_hctx_list(&rq_list);
1666e6fcbc2SMing Lei 		} while (!list_empty(&rq_list));
1676e6fcbc2SMing Lei 	} else {
168e093b784SBart Van Assche 		dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, false);
1696e6fcbc2SMing Lei 	}
1706e6fcbc2SMing Lei 
1716e6fcbc2SMing Lei 	if (busy)
1726e6fcbc2SMing Lei 		return -EAGAIN;
1736e6fcbc2SMing Lei 	return !!dispatched;
1746e6fcbc2SMing Lei }
1756e6fcbc2SMing Lei 
blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx * hctx)1766e6fcbc2SMing Lei static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
1776e6fcbc2SMing Lei {
178572299f0SShin'ichiro Kawasaki 	unsigned long end = jiffies + HZ;
1796e6fcbc2SMing Lei 	int ret;
1806e6fcbc2SMing Lei 
1816e6fcbc2SMing Lei 	do {
1826e6fcbc2SMing Lei 		ret = __blk_mq_do_dispatch_sched(hctx);
183572299f0SShin'ichiro Kawasaki 		if (ret != 1)
184572299f0SShin'ichiro Kawasaki 			break;
185572299f0SShin'ichiro Kawasaki 		if (need_resched() || time_is_before_jiffies(end)) {
186572299f0SShin'ichiro Kawasaki 			blk_mq_delay_run_hw_queue(hctx, 0);
187572299f0SShin'ichiro Kawasaki 			break;
188572299f0SShin'ichiro Kawasaki 		}
189572299f0SShin'ichiro Kawasaki 	} while (1);
19028d65729SSalman Qazi 
19128d65729SSalman Qazi 	return ret;
192de148297SMing Lei }
193de148297SMing Lei 
blk_mq_next_ctx(struct blk_mq_hw_ctx * hctx,struct blk_mq_ctx * ctx)194b347689fSMing Lei static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
195b347689fSMing Lei 					  struct blk_mq_ctx *ctx)
196b347689fSMing Lei {
197f31967f0SJens Axboe 	unsigned short idx = ctx->index_hw[hctx->type];
198b347689fSMing Lei 
199b347689fSMing Lei 	if (++idx == hctx->nr_ctx)
200b347689fSMing Lei 		idx = 0;
201b347689fSMing Lei 
202b347689fSMing Lei 	return hctx->ctxs[idx];
203b347689fSMing Lei }
204b347689fSMing Lei 
2051f460b63SMing Lei /*
2061f460b63SMing Lei  * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
2071f460b63SMing Lei  * its queue by itself in its completion handler, so we don't need to
20801542f65SKemeng Shi  * restart queue if .get_budget() fails to get the budget.
20928d65729SSalman Qazi  *
21028d65729SSalman Qazi  * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to
211c4aecaa2SRandy Dunlap  * be run again.  This is necessary to avoid starving flushes.
2121f460b63SMing Lei  */
blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx * hctx)21328d65729SSalman Qazi static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
214b347689fSMing Lei {
215b347689fSMing Lei 	struct request_queue *q = hctx->queue;
216b347689fSMing Lei 	LIST_HEAD(rq_list);
217b347689fSMing Lei 	struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);
21828d65729SSalman Qazi 	int ret = 0;
219b347689fSMing Lei 	struct request *rq;
220b347689fSMing Lei 
221445874e8SMing Lei 	do {
2222a5a24aaSMing Lei 		int budget_token;
2232a5a24aaSMing Lei 
22428d65729SSalman Qazi 		if (!list_empty_careful(&hctx->dispatch)) {
22528d65729SSalman Qazi 			ret = -EAGAIN;
22628d65729SSalman Qazi 			break;
22728d65729SSalman Qazi 		}
22828d65729SSalman Qazi 
229b347689fSMing Lei 		if (!sbitmap_any_bit_set(&hctx->ctx_map))
230b347689fSMing Lei 			break;
231b347689fSMing Lei 
2322a5a24aaSMing Lei 		budget_token = blk_mq_get_dispatch_budget(q);
2332a5a24aaSMing Lei 		if (budget_token < 0)
2341f460b63SMing Lei 			break;
235b347689fSMing Lei 
236b347689fSMing Lei 		rq = blk_mq_dequeue_from_ctx(hctx, ctx);
237b347689fSMing Lei 		if (!rq) {
2382a5a24aaSMing Lei 			blk_mq_put_dispatch_budget(q, budget_token);
239a0823421SDouglas Anderson 			/*
240a0823421SDouglas Anderson 			 * We're releasing without dispatching. Holding the
241a0823421SDouglas Anderson 			 * budget could have blocked any "hctx"s with the
242a0823421SDouglas Anderson 			 * same queue and if we didn't dispatch then there's
243a0823421SDouglas Anderson 			 * no guarantee anyone will kick the queue.  Kick it
244a0823421SDouglas Anderson 			 * ourselves.
245a0823421SDouglas Anderson 			 */
246a0823421SDouglas Anderson 			blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY);
247b347689fSMing Lei 			break;
248b347689fSMing Lei 		}
249b347689fSMing Lei 
2502a5a24aaSMing Lei 		blk_mq_set_rq_budget_token(rq, budget_token);
2512a5a24aaSMing Lei 
252b347689fSMing Lei 		/*
253b347689fSMing Lei 		 * Now this rq owns the budget which has to be released
254b347689fSMing Lei 		 * if this rq won't be queued to driver via .queue_rq()
255b347689fSMing Lei 		 * in blk_mq_dispatch_rq_list().
256b347689fSMing Lei 		 */
257b347689fSMing Lei 		list_add(&rq->queuelist, &rq_list);
258b347689fSMing Lei 
259b347689fSMing Lei 		/* round robin for fair dispatch */
260b347689fSMing Lei 		ctx = blk_mq_next_ctx(hctx, rq->mq_ctx);
261b347689fSMing Lei 
262e093b784SBart Van Assche 	} while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, false));
263b347689fSMing Lei 
264b347689fSMing Lei 	WRITE_ONCE(hctx->dispatch_from, ctx);
26528d65729SSalman Qazi 	return ret;
266b347689fSMing Lei }
267b347689fSMing Lei 
__blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx * hctx)268e1b586f2SZheng Bin static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
269bd166ef1SJens Axboe {
27089ea5cebSChristoph Hellwig 	bool need_dispatch = false;
271bd166ef1SJens Axboe 	LIST_HEAD(rq_list);
272bd166ef1SJens Axboe 
273bd166ef1SJens Axboe 	/*
274bd166ef1SJens Axboe 	 * If we have previous entries on our dispatch list, grab them first for
275bd166ef1SJens Axboe 	 * more fair dispatch.
276bd166ef1SJens Axboe 	 */
277bd166ef1SJens Axboe 	if (!list_empty_careful(&hctx->dispatch)) {
278bd166ef1SJens Axboe 		spin_lock(&hctx->lock);
279bd166ef1SJens Axboe 		if (!list_empty(&hctx->dispatch))
280bd166ef1SJens Axboe 			list_splice_init(&hctx->dispatch, &rq_list);
281bd166ef1SJens Axboe 		spin_unlock(&hctx->lock);
282bd166ef1SJens Axboe 	}
283bd166ef1SJens Axboe 
284bd166ef1SJens Axboe 	/*
285bd166ef1SJens Axboe 	 * Only ask the scheduler for requests, if we didn't have residual
286bd166ef1SJens Axboe 	 * requests from the dispatch list. This is to avoid the case where
287bd166ef1SJens Axboe 	 * we only ever dispatch a fraction of the requests available because
288bd166ef1SJens Axboe 	 * of low device queue depth. Once we pull requests out of the IO
289bd166ef1SJens Axboe 	 * scheduler, we can no longer merge or sort them. So it's best to
290bd166ef1SJens Axboe 	 * leave them there for as long as we can. Mark the hw queue as
291bd166ef1SJens Axboe 	 * needing a restart in that case.
292caf8eb0dSMing Lei 	 *
2935e3d02bbSMing Lei 	 * We want to dispatch from the scheduler if there was nothing
2945e3d02bbSMing Lei 	 * on the dispatch list or we were able to dispatch from the
2955e3d02bbSMing Lei 	 * dispatch list.
29664765a75SJens Axboe 	 */
297caf8eb0dSMing Lei 	if (!list_empty(&rq_list)) {
298caf8eb0dSMing Lei 		blk_mq_sched_mark_restart_hctx(hctx);
299e093b784SBart Van Assche 		if (!blk_mq_dispatch_rq_list(hctx, &rq_list, true))
30089ea5cebSChristoph Hellwig 			return 0;
30189ea5cebSChristoph Hellwig 		need_dispatch = true;
302caf8eb0dSMing Lei 	} else {
30389ea5cebSChristoph Hellwig 		need_dispatch = hctx->dispatch_busy;
304c13660a0SJens Axboe 	}
30528d65729SSalman Qazi 
30689ea5cebSChristoph Hellwig 	if (hctx->queue->elevator)
30789ea5cebSChristoph Hellwig 		return blk_mq_do_dispatch_sched(hctx);
30889ea5cebSChristoph Hellwig 
30989ea5cebSChristoph Hellwig 	/* dequeue request one by one from sw queue if queue is busy */
31089ea5cebSChristoph Hellwig 	if (need_dispatch)
31189ea5cebSChristoph Hellwig 		return blk_mq_do_dispatch_ctx(hctx);
31289ea5cebSChristoph Hellwig 	blk_mq_flush_busy_ctxs(hctx, &rq_list);
313e093b784SBart Van Assche 	blk_mq_dispatch_rq_list(hctx, &rq_list, true);
31489ea5cebSChristoph Hellwig 	return 0;
31528d65729SSalman Qazi }
31628d65729SSalman Qazi 
blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx * hctx)31728d65729SSalman Qazi void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
31828d65729SSalman Qazi {
31928d65729SSalman Qazi 	struct request_queue *q = hctx->queue;
32028d65729SSalman Qazi 
32128d65729SSalman Qazi 	/* RCU or SRCU read lock is needed before checking quiesced flag */
32228d65729SSalman Qazi 	if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
32328d65729SSalman Qazi 		return;
32428d65729SSalman Qazi 
32528d65729SSalman Qazi 	/*
32628d65729SSalman Qazi 	 * A return of -EAGAIN is an indication that hctx->dispatch is not
32728d65729SSalman Qazi 	 * empty and we must run again in order to avoid starving flushes.
32828d65729SSalman Qazi 	 */
32928d65729SSalman Qazi 	if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) {
33028d65729SSalman Qazi 		if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN)
33128d65729SSalman Qazi 			blk_mq_run_hw_queue(hctx, true);
33228d65729SSalman Qazi 	}
333bd166ef1SJens Axboe }
334bd166ef1SJens Axboe 
blk_mq_sched_bio_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs)335179ae84fSPavel Begunkov bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio,
33614ccb66bSChristoph Hellwig 		unsigned int nr_segs)
337bd166ef1SJens Axboe {
338bd166ef1SJens Axboe 	struct elevator_queue *e = q->elevator;
339efed9a33SOmar Sandoval 	struct blk_mq_ctx *ctx;
340efed9a33SOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
3419bddeb2aSMing Lei 	bool ret = false;
342c16d6b5aSMing Lei 	enum hctx_type type;
343bd166ef1SJens Axboe 
344900e0807SJens Axboe 	if (e && e->type->ops.bio_merge) {
345900e0807SJens Axboe 		ret = e->type->ops.bio_merge(q, bio, nr_segs);
346900e0807SJens Axboe 		goto out_put;
347900e0807SJens Axboe 	}
348bd166ef1SJens Axboe 
349efed9a33SOmar Sandoval 	ctx = blk_mq_get_ctx(q);
35061667cb6SGuixin Liu 	hctx = blk_mq_map_queue(bio->bi_opf, ctx);
351c16d6b5aSMing Lei 	type = hctx->type;
352cc76ace4SChristoph Hellwig 	if (list_empty_careful(&ctx->rq_lists[type]))
353900e0807SJens Axboe 		goto out_put;
354cdfcef9eSBaolin Wang 
3559bddeb2aSMing Lei 	/* default per sw-queue merge */
3569bddeb2aSMing Lei 	spin_lock(&ctx->lock);
357cdfcef9eSBaolin Wang 	/*
358cdfcef9eSBaolin Wang 	 * Reverse check our software queue for entries that we could
359cdfcef9eSBaolin Wang 	 * potentially merge with. Currently includes a hand-wavy stop
360cdfcef9eSBaolin Wang 	 * count of 8, to not spend too much time checking for merges.
361cdfcef9eSBaolin Wang 	 */
3629a14d6ceSJens Axboe 	if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs))
363cdfcef9eSBaolin Wang 		ret = true;
3649bddeb2aSMing Lei 
365cdfcef9eSBaolin Wang 	spin_unlock(&ctx->lock);
366900e0807SJens Axboe out_put:
3679bddeb2aSMing Lei 	return ret;
368bd166ef1SJens Axboe }
369bd166ef1SJens Axboe 
blk_mq_sched_try_insert_merge(struct request_queue * q,struct request * rq,struct list_head * free)370fd2ef39cSJan Kara bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq,
371fd2ef39cSJan Kara 				   struct list_head *free)
372bd166ef1SJens Axboe {
373fd2ef39cSJan Kara 	return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq, free);
374bd166ef1SJens Axboe }
375bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
376bd166ef1SJens Axboe 
377c3e22192SMing Lei /* called in queue's release handler, tagset has gone away */
blk_mq_sched_tags_teardown(struct request_queue * q,unsigned int flags)378e155b0c2SJohn Garry static void blk_mq_sched_tags_teardown(struct request_queue *q, unsigned int flags)
379bd166ef1SJens Axboe {
380bd166ef1SJens Axboe 	struct blk_mq_hw_ctx *hctx;
3814f481208SMing Lei 	unsigned long i;
382bd166ef1SJens Axboe 
383f5a6604fSNilay Shroff 	queue_for_each_hw_ctx(q, hctx, i)
384c3e22192SMing Lei 		hctx->sched_tags = NULL;
385e155b0c2SJohn Garry 
386079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(flags))
387f5a6604fSNilay Shroff 		q->sched_shared_tags = NULL;
388d97e594cSJohn Garry }
389d97e594cSJohn Garry 
blk_mq_sched_reg_debugfs(struct request_queue * q)39092c22d7eSMing Lei void blk_mq_sched_reg_debugfs(struct request_queue *q)
391ed3896acSMing Lei {
392ed3896acSMing Lei 	struct blk_mq_hw_ctx *hctx;
393dfe48ea1SYu Kuai 	unsigned int memflags;
394ed3896acSMing Lei 	unsigned long i;
395ed3896acSMing Lei 
396dfe48ea1SYu Kuai 	memflags = blk_debugfs_lock(q);
397ed3896acSMing Lei 	blk_mq_debugfs_register_sched(q);
398ed3896acSMing Lei 	queue_for_each_hw_ctx(q, hctx, i)
399ed3896acSMing Lei 		blk_mq_debugfs_register_sched_hctx(q, hctx);
400dfe48ea1SYu Kuai 	blk_debugfs_unlock(q, memflags);
401ed3896acSMing Lei }
402ed3896acSMing Lei 
blk_mq_sched_unreg_debugfs(struct request_queue * q)40392c22d7eSMing Lei void blk_mq_sched_unreg_debugfs(struct request_queue *q)
404ed3896acSMing Lei {
405ed3896acSMing Lei 	struct blk_mq_hw_ctx *hctx;
406ed3896acSMing Lei 	unsigned long i;
407ed3896acSMing Lei 
408dfe48ea1SYu Kuai 	blk_debugfs_lock_nomemsave(q);
409ed3896acSMing Lei 	queue_for_each_hw_ctx(q, hctx, i)
410ed3896acSMing Lei 		blk_mq_debugfs_unregister_sched_hctx(hctx);
411ed3896acSMing Lei 	blk_mq_debugfs_unregister_sched(q);
412dfe48ea1SYu Kuai 	blk_debugfs_unlock_nomemrestore(q);
413ed3896acSMing Lei }
414ed3896acSMing Lei 
blk_mq_free_sched_tags(struct elevator_tags * et,struct blk_mq_tag_set * set)415f5a6604fSNilay Shroff void blk_mq_free_sched_tags(struct elevator_tags *et,
416f5a6604fSNilay Shroff 		struct blk_mq_tag_set *set)
417f5a6604fSNilay Shroff {
418f5a6604fSNilay Shroff 	unsigned long i;
419f5a6604fSNilay Shroff 
420f5a6604fSNilay Shroff 	/* Shared tags are stored at index 0 in @tags. */
421f5a6604fSNilay Shroff 	if (blk_mq_is_shared_tags(set->flags))
422f5a6604fSNilay Shroff 		blk_mq_free_map_and_rqs(set, et->tags[0], BLK_MQ_NO_HCTX_IDX);
423f5a6604fSNilay Shroff 	else {
424f5a6604fSNilay Shroff 		for (i = 0; i < et->nr_hw_queues; i++)
425f5a6604fSNilay Shroff 			blk_mq_free_map_and_rqs(set, et->tags[i], i);
426f5a6604fSNilay Shroff 	}
427f5a6604fSNilay Shroff 
428f5a6604fSNilay Shroff 	kfree(et);
429f5a6604fSNilay Shroff }
430f5a6604fSNilay Shroff 
blk_mq_free_sched_res(struct elevator_resources * res,struct elevator_type * type,struct blk_mq_tag_set * set)43104728ce9SNilay Shroff void blk_mq_free_sched_res(struct elevator_resources *res,
4320315476eSNilay Shroff 		struct elevator_type *type,
43304728ce9SNilay Shroff 		struct blk_mq_tag_set *set)
43404728ce9SNilay Shroff {
43504728ce9SNilay Shroff 	if (res->et) {
43604728ce9SNilay Shroff 		blk_mq_free_sched_tags(res->et, set);
43704728ce9SNilay Shroff 		res->et = NULL;
43804728ce9SNilay Shroff 	}
4390315476eSNilay Shroff 	if (res->data) {
4400315476eSNilay Shroff 		blk_mq_free_sched_data(type, res->data);
4410315476eSNilay Shroff 		res->data = NULL;
4420315476eSNilay Shroff 	}
44304728ce9SNilay Shroff }
44404728ce9SNilay Shroff 
blk_mq_free_sched_res_batch(struct xarray * elv_tbl,struct blk_mq_tag_set * set)44504728ce9SNilay Shroff void blk_mq_free_sched_res_batch(struct xarray *elv_tbl,
44604225d13SNilay Shroff 		struct blk_mq_tag_set *set)
44704225d13SNilay Shroff {
44804225d13SNilay Shroff 	struct request_queue *q;
449232143b6SNilay Shroff 	struct elv_change_ctx *ctx;
45004225d13SNilay Shroff 
45104225d13SNilay Shroff 	lockdep_assert_held_write(&set->update_nr_hwq_lock);
45204225d13SNilay Shroff 
45304225d13SNilay Shroff 	list_for_each_entry(q, &set->tag_list, tag_set_list) {
45404225d13SNilay Shroff 		/*
45504225d13SNilay Shroff 		 * Accessing q->elevator without holding q->elevator_lock is
45604225d13SNilay Shroff 		 * safe because we're holding here set->update_nr_hwq_lock in
45704225d13SNilay Shroff 		 * the writer context. So, scheduler update/switch code (which
45804225d13SNilay Shroff 		 * acquires the same lock but in the reader context) can't run
45904225d13SNilay Shroff 		 * concurrently.
46004225d13SNilay Shroff 		 */
46104225d13SNilay Shroff 		if (q->elevator) {
462232143b6SNilay Shroff 			ctx = xa_load(elv_tbl, q->id);
46304728ce9SNilay Shroff 			if (!ctx) {
46404225d13SNilay Shroff 				WARN_ON_ONCE(1);
465232143b6SNilay Shroff 				continue;
466232143b6SNilay Shroff 			}
4670315476eSNilay Shroff 			blk_mq_free_sched_res(&ctx->res, ctx->type, set);
46804225d13SNilay Shroff 		}
46904225d13SNilay Shroff 	}
47004225d13SNilay Shroff }
47104225d13SNilay Shroff 
blk_mq_free_sched_ctx_batch(struct xarray * elv_tbl)472232143b6SNilay Shroff void blk_mq_free_sched_ctx_batch(struct xarray *elv_tbl)
473232143b6SNilay Shroff {
474232143b6SNilay Shroff 	unsigned long i;
475232143b6SNilay Shroff 	struct elv_change_ctx *ctx;
476232143b6SNilay Shroff 
477232143b6SNilay Shroff 	xa_for_each(elv_tbl, i, ctx) {
478232143b6SNilay Shroff 		xa_erase(elv_tbl, i);
479232143b6SNilay Shroff 		kfree(ctx);
480232143b6SNilay Shroff 	}
481232143b6SNilay Shroff }
482232143b6SNilay Shroff 
blk_mq_alloc_sched_ctx_batch(struct xarray * elv_tbl,struct blk_mq_tag_set * set)483232143b6SNilay Shroff int blk_mq_alloc_sched_ctx_batch(struct xarray *elv_tbl,
484232143b6SNilay Shroff 		struct blk_mq_tag_set *set)
485232143b6SNilay Shroff {
486232143b6SNilay Shroff 	struct request_queue *q;
487232143b6SNilay Shroff 	struct elv_change_ctx *ctx;
488232143b6SNilay Shroff 
489232143b6SNilay Shroff 	lockdep_assert_held_write(&set->update_nr_hwq_lock);
490232143b6SNilay Shroff 
491232143b6SNilay Shroff 	list_for_each_entry(q, &set->tag_list, tag_set_list) {
492*bf4afc53SLinus Torvalds 		ctx = kzalloc_obj(struct elv_change_ctx);
493232143b6SNilay Shroff 		if (!ctx)
494232143b6SNilay Shroff 			return -ENOMEM;
495232143b6SNilay Shroff 
496232143b6SNilay Shroff 		if (xa_insert(elv_tbl, q->id, ctx, GFP_KERNEL)) {
497232143b6SNilay Shroff 			kfree(ctx);
498232143b6SNilay Shroff 			return -ENOMEM;
499232143b6SNilay Shroff 		}
500232143b6SNilay Shroff 	}
501232143b6SNilay Shroff 	return 0;
502232143b6SNilay Shroff }
503232143b6SNilay Shroff 
blk_mq_alloc_sched_tags(struct blk_mq_tag_set * set,unsigned int nr_hw_queues,unsigned int nr_requests)504f5a6604fSNilay Shroff struct elevator_tags *blk_mq_alloc_sched_tags(struct blk_mq_tag_set *set,
5056293e336SYu Kuai 		unsigned int nr_hw_queues, unsigned int nr_requests)
506f5a6604fSNilay Shroff {
507f5a6604fSNilay Shroff 	unsigned int nr_tags;
508f5a6604fSNilay Shroff 	int i;
509f5a6604fSNilay Shroff 	struct elevator_tags *et;
510f5a6604fSNilay Shroff 	gfp_t gfp = GFP_NOIO | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
511f5a6604fSNilay Shroff 
512f5a6604fSNilay Shroff 	if (blk_mq_is_shared_tags(set->flags))
513f5a6604fSNilay Shroff 		nr_tags = 1;
514f5a6604fSNilay Shroff 	else
515f5a6604fSNilay Shroff 		nr_tags = nr_hw_queues;
516f5a6604fSNilay Shroff 
51769050f8dSKees Cook 	et = kmalloc_flex(*et, tags, nr_tags, gfp);
518f5a6604fSNilay Shroff 	if (!et)
519f5a6604fSNilay Shroff 		return NULL;
5206293e336SYu Kuai 
5216293e336SYu Kuai 	et->nr_requests = nr_requests;
522f5a6604fSNilay Shroff 	et->nr_hw_queues = nr_hw_queues;
523f5a6604fSNilay Shroff 
524f5a6604fSNilay Shroff 	if (blk_mq_is_shared_tags(set->flags)) {
525f5a6604fSNilay Shroff 		/* Shared tags are stored at index 0 in @tags. */
526f5a6604fSNilay Shroff 		et->tags[0] = blk_mq_alloc_map_and_rqs(set, BLK_MQ_NO_HCTX_IDX,
527f5a6604fSNilay Shroff 					MAX_SCHED_RQ);
528f5a6604fSNilay Shroff 		if (!et->tags[0])
529f5a6604fSNilay Shroff 			goto out;
530f5a6604fSNilay Shroff 	} else {
531f5a6604fSNilay Shroff 		for (i = 0; i < et->nr_hw_queues; i++) {
532f5a6604fSNilay Shroff 			et->tags[i] = blk_mq_alloc_map_and_rqs(set, i,
533f5a6604fSNilay Shroff 					et->nr_requests);
534f5a6604fSNilay Shroff 			if (!et->tags[i])
535f5a6604fSNilay Shroff 				goto out_unwind;
536f5a6604fSNilay Shroff 		}
537f5a6604fSNilay Shroff 	}
538f5a6604fSNilay Shroff 
539f5a6604fSNilay Shroff 	return et;
540f5a6604fSNilay Shroff out_unwind:
541f5a6604fSNilay Shroff 	while (--i >= 0)
542f5a6604fSNilay Shroff 		blk_mq_free_map_and_rqs(set, et->tags[i], i);
543f5a6604fSNilay Shroff out:
544f5a6604fSNilay Shroff 	kfree(et);
545f5a6604fSNilay Shroff 	return NULL;
546f5a6604fSNilay Shroff }
547f5a6604fSNilay Shroff 
blk_mq_alloc_sched_res(struct request_queue * q,struct elevator_type * type,struct elevator_resources * res,unsigned int nr_hw_queues)54804728ce9SNilay Shroff int blk_mq_alloc_sched_res(struct request_queue *q,
5490315476eSNilay Shroff 		struct elevator_type *type,
5500315476eSNilay Shroff 		struct elevator_resources *res,
5510315476eSNilay Shroff 		unsigned int nr_hw_queues)
55204728ce9SNilay Shroff {
55304728ce9SNilay Shroff 	struct blk_mq_tag_set *set = q->tag_set;
55404728ce9SNilay Shroff 
55504728ce9SNilay Shroff 	res->et = blk_mq_alloc_sched_tags(set, nr_hw_queues,
55604728ce9SNilay Shroff 			blk_mq_default_nr_requests(set));
55704728ce9SNilay Shroff 	if (!res->et)
55804728ce9SNilay Shroff 		return -ENOMEM;
55904728ce9SNilay Shroff 
5600315476eSNilay Shroff 	res->data = blk_mq_alloc_sched_data(q, type);
5610315476eSNilay Shroff 	if (IS_ERR(res->data)) {
5620315476eSNilay Shroff 		blk_mq_free_sched_tags(res->et, set);
5630315476eSNilay Shroff 		return -ENOMEM;
5640315476eSNilay Shroff 	}
5650315476eSNilay Shroff 
56604728ce9SNilay Shroff 	return 0;
56704728ce9SNilay Shroff }
56804728ce9SNilay Shroff 
blk_mq_alloc_sched_res_batch(struct xarray * elv_tbl,struct blk_mq_tag_set * set,unsigned int nr_hw_queues)56904728ce9SNilay Shroff int blk_mq_alloc_sched_res_batch(struct xarray *elv_tbl,
57004225d13SNilay Shroff 		struct blk_mq_tag_set *set, unsigned int nr_hw_queues)
57104225d13SNilay Shroff {
572232143b6SNilay Shroff 	struct elv_change_ctx *ctx;
57304225d13SNilay Shroff 	struct request_queue *q;
574232143b6SNilay Shroff 	int ret = -ENOMEM;
57504225d13SNilay Shroff 
57604225d13SNilay Shroff 	lockdep_assert_held_write(&set->update_nr_hwq_lock);
57704225d13SNilay Shroff 
57804225d13SNilay Shroff 	list_for_each_entry(q, &set->tag_list, tag_set_list) {
57904225d13SNilay Shroff 		/*
58004225d13SNilay Shroff 		 * Accessing q->elevator without holding q->elevator_lock is
58104225d13SNilay Shroff 		 * safe because we're holding here set->update_nr_hwq_lock in
58204225d13SNilay Shroff 		 * the writer context. So, scheduler update/switch code (which
58304225d13SNilay Shroff 		 * acquires the same lock but in the reader context) can't run
58404225d13SNilay Shroff 		 * concurrently.
58504225d13SNilay Shroff 		 */
58604225d13SNilay Shroff 		if (q->elevator) {
587232143b6SNilay Shroff 			ctx = xa_load(elv_tbl, q->id);
588232143b6SNilay Shroff 			if (WARN_ON_ONCE(!ctx)) {
589232143b6SNilay Shroff 				ret = -ENOENT;
59004225d13SNilay Shroff 				goto out_unwind;
591232143b6SNilay Shroff 			}
592232143b6SNilay Shroff 
5930315476eSNilay Shroff 			ret = blk_mq_alloc_sched_res(q, q->elevator->type,
5940315476eSNilay Shroff 					&ctx->res, nr_hw_queues);
59504728ce9SNilay Shroff 			if (ret)
596232143b6SNilay Shroff 				goto out_unwind;
59704225d13SNilay Shroff 		}
59804225d13SNilay Shroff 	}
59904225d13SNilay Shroff 	return 0;
6000315476eSNilay Shroff 
60104225d13SNilay Shroff out_unwind:
60204225d13SNilay Shroff 	list_for_each_entry_continue_reverse(q, &set->tag_list, tag_set_list) {
60304225d13SNilay Shroff 		if (q->elevator) {
604232143b6SNilay Shroff 			ctx = xa_load(elv_tbl, q->id);
60504728ce9SNilay Shroff 			if (ctx)
6060315476eSNilay Shroff 				blk_mq_free_sched_res(&ctx->res,
6070315476eSNilay Shroff 						ctx->type, set);
60804225d13SNilay Shroff 		}
609232143b6SNilay Shroff 	}
610232143b6SNilay Shroff 	return ret;
61104225d13SNilay Shroff }
61204225d13SNilay Shroff 
6138ed40ee3SJinlong Chen /* caller must have a reference to @e, will grab another one if successful */
blk_mq_init_sched(struct request_queue * q,struct elevator_type * e,struct elevator_resources * res)614f5a6604fSNilay Shroff int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e,
61504728ce9SNilay Shroff 		struct elevator_resources *res)
6166917ff0bSOmar Sandoval {
6174f481208SMing Lei 	unsigned int flags = q->tag_set->flags;
61804728ce9SNilay Shroff 	struct elevator_tags *et = res->et;
6196917ff0bSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
620ee056f98SOmar Sandoval 	struct elevator_queue *eq;
6214f481208SMing Lei 	unsigned long i;
6226917ff0bSOmar Sandoval 	int ret;
6236917ff0bSOmar Sandoval 
6240315476eSNilay Shroff 	eq = elevator_alloc(q, e, res);
62549811586SNilay Shroff 	if (!eq)
62649811586SNilay Shroff 		return -ENOMEM;
62749811586SNilay Shroff 
628f5a6604fSNilay Shroff 	q->nr_requests = et->nr_requests;
629f5a6604fSNilay Shroff 
630079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(flags)) {
631f5a6604fSNilay Shroff 		/* Shared tags are stored at index 0 in @et->tags. */
632f5a6604fSNilay Shroff 		q->sched_shared_tags = et->tags[0];
633dc96cefeSYu Kuai 		blk_mq_tag_update_sched_shared_tags(q, et->nr_requests);
634e155b0c2SJohn Garry 	}
635e155b0c2SJohn Garry 
6366917ff0bSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
637f5a6604fSNilay Shroff 		if (blk_mq_is_shared_tags(flags))
638f5a6604fSNilay Shroff 			hctx->sched_tags = q->sched_shared_tags;
639f5a6604fSNilay Shroff 		else
640f5a6604fSNilay Shroff 			hctx->sched_tags = et->tags[i];
641d97e594cSJohn Garry 	}
642d97e594cSJohn Garry 
64349811586SNilay Shroff 	ret = e->ops.init_sched(q, eq);
6446917ff0bSOmar Sandoval 	if (ret)
645f5a6604fSNilay Shroff 		goto out;
6466917ff0bSOmar Sandoval 
647ee056f98SOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
648f9cd4bfeSJens Axboe 		if (e->ops.init_hctx) {
649f9cd4bfeSJens Axboe 			ret = e->ops.init_hctx(hctx, i);
650ee056f98SOmar Sandoval 			if (ret) {
651ee056f98SOmar Sandoval 				blk_mq_exit_sched(q, eq);
652ee056f98SOmar Sandoval 				kobject_put(&eq->kobj);
653ee056f98SOmar Sandoval 				return ret;
654ee056f98SOmar Sandoval 			}
655ee056f98SOmar Sandoval 		}
656ee056f98SOmar Sandoval 	}
6576917ff0bSOmar Sandoval 	return 0;
6586917ff0bSOmar Sandoval 
659f5a6604fSNilay Shroff out:
660e155b0c2SJohn Garry 	blk_mq_sched_tags_teardown(q, flags);
66149811586SNilay Shroff 	kobject_put(&eq->kobj);
66254d5329dSOmar Sandoval 	q->elevator = NULL;
6636917ff0bSOmar Sandoval 	return ret;
6646917ff0bSOmar Sandoval }
6656917ff0bSOmar Sandoval 
666c3e22192SMing Lei /*
667c3e22192SMing Lei  * called in either blk_queue_cleanup or elevator_switch, tagset
668c3e22192SMing Lei  * is required for freeing requests
669c3e22192SMing Lei  */
blk_mq_sched_free_rqs(struct request_queue * q)6701820f4f0SJohn Garry void blk_mq_sched_free_rqs(struct request_queue *q)
671c3e22192SMing Lei {
672c3e22192SMing Lei 	struct blk_mq_hw_ctx *hctx;
6734f481208SMing Lei 	unsigned long i;
674c3e22192SMing Lei 
675079a2e3eSJohn Garry 	if (blk_mq_is_shared_tags(q->tag_set->flags)) {
676079a2e3eSJohn Garry 		blk_mq_free_rqs(q->tag_set, q->sched_shared_tags,
677e155b0c2SJohn Garry 				BLK_MQ_NO_HCTX_IDX);
678e155b0c2SJohn Garry 	} else {
679c3e22192SMing Lei 		queue_for_each_hw_ctx(q, hctx, i) {
680c3e22192SMing Lei 			if (hctx->sched_tags)
681e155b0c2SJohn Garry 				blk_mq_free_rqs(q->tag_set,
682e155b0c2SJohn Garry 						hctx->sched_tags, i);
683e155b0c2SJohn Garry 		}
684c3e22192SMing Lei 	}
685c3e22192SMing Lei }
686c3e22192SMing Lei 
blk_mq_exit_sched(struct request_queue * q,struct elevator_queue * e)68754d5329dSOmar Sandoval void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
68854d5329dSOmar Sandoval {
689ee056f98SOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
6904f481208SMing Lei 	unsigned long i;
691f0c1c4d2SMing Lei 	unsigned int flags = 0;
692ee056f98SOmar Sandoval 
693ed3896acSMing Lei 	queue_for_each_hw_ctx(q, hctx, i) {
694f9cd4bfeSJens Axboe 		if (e->type->ops.exit_hctx && hctx->sched_data) {
695f9cd4bfeSJens Axboe 			e->type->ops.exit_hctx(hctx, i);
696ee056f98SOmar Sandoval 			hctx->sched_data = NULL;
697ee056f98SOmar Sandoval 		}
698f0c1c4d2SMing Lei 		flags = hctx->flags;
699ee056f98SOmar Sandoval 	}
7005cf9c91bSChristoph Hellwig 
701f9cd4bfeSJens Axboe 	if (e->type->ops.exit_sched)
702f9cd4bfeSJens Axboe 		e->type->ops.exit_sched(e);
703e155b0c2SJohn Garry 	blk_mq_sched_tags_teardown(q, flags);
7045c3d858cSMing Lei 	set_bit(ELEVATOR_FLAG_DYING, &q->elevator->flags);
70554d5329dSOmar Sandoval 	q->elevator = NULL;
70654d5329dSOmar Sandoval }
707