xref: /openbmc/linux/block/blk-mq-sched.c (revision cdfcef9e)
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>
9bd166ef1SJens Axboe #include <linux/blk-mq.h>
106e6fcbc2SMing Lei #include <linux/list_sort.h>
11bd166ef1SJens Axboe 
12bd166ef1SJens Axboe #include <trace/events/block.h>
13bd166ef1SJens Axboe 
14bd166ef1SJens Axboe #include "blk.h"
15bd166ef1SJens Axboe #include "blk-mq.h"
16d332ce09SOmar Sandoval #include "blk-mq-debugfs.h"
17bd166ef1SJens Axboe #include "blk-mq-sched.h"
18bd166ef1SJens Axboe #include "blk-mq-tag.h"
19bd166ef1SJens Axboe #include "blk-wbt.h"
20bd166ef1SJens Axboe 
21bd166ef1SJens Axboe void blk_mq_sched_free_hctx_data(struct request_queue *q,
22bd166ef1SJens Axboe 				 void (*exit)(struct blk_mq_hw_ctx *))
23bd166ef1SJens Axboe {
24bd166ef1SJens Axboe 	struct blk_mq_hw_ctx *hctx;
25bd166ef1SJens Axboe 	int i;
26bd166ef1SJens Axboe 
27bd166ef1SJens Axboe 	queue_for_each_hw_ctx(q, hctx, i) {
28bd166ef1SJens Axboe 		if (exit && hctx->sched_data)
29bd166ef1SJens Axboe 			exit(hctx);
30bd166ef1SJens Axboe 		kfree(hctx->sched_data);
31bd166ef1SJens Axboe 		hctx->sched_data = NULL;
32bd166ef1SJens Axboe 	}
33bd166ef1SJens Axboe }
34bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data);
35bd166ef1SJens Axboe 
36e2b3fa5aSDamien Le Moal void blk_mq_sched_assign_ioc(struct request *rq)
37bd166ef1SJens Axboe {
3844e8c2bfSChristoph Hellwig 	struct request_queue *q = rq->q;
390c62bff1SJens Axboe 	struct io_context *ioc;
40bd166ef1SJens Axboe 	struct io_cq *icq;
41bd166ef1SJens Axboe 
420c62bff1SJens Axboe 	/*
430c62bff1SJens Axboe 	 * May not have an IO context if it's a passthrough request
440c62bff1SJens Axboe 	 */
450c62bff1SJens Axboe 	ioc = current->io_context;
460c62bff1SJens Axboe 	if (!ioc)
470c62bff1SJens Axboe 		return;
480c62bff1SJens Axboe 
490d945c1fSChristoph Hellwig 	spin_lock_irq(&q->queue_lock);
50bd166ef1SJens Axboe 	icq = ioc_lookup_icq(ioc, q);
510d945c1fSChristoph Hellwig 	spin_unlock_irq(&q->queue_lock);
52bd166ef1SJens Axboe 
53bd166ef1SJens Axboe 	if (!icq) {
54bd166ef1SJens Axboe 		icq = ioc_create_icq(ioc, q, GFP_ATOMIC);
55bd166ef1SJens Axboe 		if (!icq)
56bd166ef1SJens Axboe 			return;
57bd166ef1SJens Axboe 	}
58ea511e3cSChristoph Hellwig 	get_io_context(icq->ioc);
5944e8c2bfSChristoph Hellwig 	rq->elv.icq = icq;
60bd166ef1SJens Axboe }
61bd166ef1SJens Axboe 
628e8320c9SJens Axboe /*
638e8320c9SJens Axboe  * Mark a hardware queue as needing a restart. For shared queues, maintain
648e8320c9SJens Axboe  * a count of how many hardware queues are marked for restart.
658e8320c9SJens Axboe  */
667211aef8SDamien Le Moal void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx)
678e8320c9SJens Axboe {
688e8320c9SJens Axboe 	if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
698e8320c9SJens Axboe 		return;
708e8320c9SJens Axboe 
718e8320c9SJens Axboe 	set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
728e8320c9SJens Axboe }
737211aef8SDamien Le Moal EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx);
748e8320c9SJens Axboe 
7597889f9aSMing Lei void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
768e8320c9SJens Axboe {
778e8320c9SJens Axboe 	if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
7897889f9aSMing Lei 		return;
798e8320c9SJens Axboe 	clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
808e8320c9SJens Axboe 
81d7d8535fSMing Lei 	/*
82d7d8535fSMing Lei 	 * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch)
83d7d8535fSMing Lei 	 * in blk_mq_run_hw_queue(). Its pair is the barrier in
84d7d8535fSMing Lei 	 * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART,
85d7d8535fSMing Lei 	 * meantime new request added to hctx->dispatch is missed to check in
86d7d8535fSMing Lei 	 * blk_mq_run_hw_queue().
87d7d8535fSMing Lei 	 */
88d7d8535fSMing Lei 	smp_mb();
89d7d8535fSMing Lei 
9097889f9aSMing Lei 	blk_mq_run_hw_queue(hctx, true);
918e8320c9SJens Axboe }
928e8320c9SJens Axboe 
936e6fcbc2SMing Lei static int sched_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
946e6fcbc2SMing Lei {
956e6fcbc2SMing Lei 	struct request *rqa = container_of(a, struct request, queuelist);
966e6fcbc2SMing Lei 	struct request *rqb = container_of(b, struct request, queuelist);
976e6fcbc2SMing Lei 
986e6fcbc2SMing Lei 	return rqa->mq_hctx > rqb->mq_hctx;
996e6fcbc2SMing Lei }
1006e6fcbc2SMing Lei 
1016e6fcbc2SMing Lei static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list)
1026e6fcbc2SMing Lei {
1036e6fcbc2SMing Lei 	struct blk_mq_hw_ctx *hctx =
1046e6fcbc2SMing Lei 		list_first_entry(rq_list, struct request, queuelist)->mq_hctx;
1056e6fcbc2SMing Lei 	struct request *rq;
1066e6fcbc2SMing Lei 	LIST_HEAD(hctx_list);
1076e6fcbc2SMing Lei 	unsigned int count = 0;
1086e6fcbc2SMing Lei 
1096e6fcbc2SMing Lei 	list_for_each_entry(rq, rq_list, queuelist) {
1106e6fcbc2SMing Lei 		if (rq->mq_hctx != hctx) {
1116e6fcbc2SMing Lei 			list_cut_before(&hctx_list, rq_list, &rq->queuelist);
1126e6fcbc2SMing Lei 			goto dispatch;
1136e6fcbc2SMing Lei 		}
1146e6fcbc2SMing Lei 		count++;
1156e6fcbc2SMing Lei 	}
1166e6fcbc2SMing Lei 	list_splice_tail_init(rq_list, &hctx_list);
1176e6fcbc2SMing Lei 
1186e6fcbc2SMing Lei dispatch:
119106e71c5SBaolin Wang 	return blk_mq_dispatch_rq_list(hctx, &hctx_list, count);
1206e6fcbc2SMing Lei }
1216e6fcbc2SMing Lei 
122a0823421SDouglas Anderson #define BLK_MQ_BUDGET_DELAY	3		/* ms units */
123a0823421SDouglas Anderson 
1241f460b63SMing Lei /*
1251f460b63SMing Lei  * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
1261f460b63SMing Lei  * its queue by itself in its completion handler, so we don't need to
1271f460b63SMing Lei  * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
12828d65729SSalman Qazi  *
12928d65729SSalman Qazi  * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to
13028d65729SSalman Qazi  * be run again.  This is necessary to avoid starving flushes.
1311f460b63SMing Lei  */
1326e6fcbc2SMing Lei static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
133caf8eb0dSMing Lei {
134caf8eb0dSMing Lei 	struct request_queue *q = hctx->queue;
135caf8eb0dSMing Lei 	struct elevator_queue *e = q->elevator;
1366e6fcbc2SMing Lei 	bool multi_hctxs = false, run_queue = false;
1376e6fcbc2SMing Lei 	bool dispatched = false, busy = false;
1386e6fcbc2SMing Lei 	unsigned int max_dispatch;
139caf8eb0dSMing Lei 	LIST_HEAD(rq_list);
1406e6fcbc2SMing Lei 	int count = 0;
1416e6fcbc2SMing Lei 
1426e6fcbc2SMing Lei 	if (hctx->dispatch_busy)
1436e6fcbc2SMing Lei 		max_dispatch = 1;
1446e6fcbc2SMing Lei 	else
1456e6fcbc2SMing Lei 		max_dispatch = hctx->queue->nr_requests;
146caf8eb0dSMing Lei 
147445874e8SMing Lei 	do {
1486e6fcbc2SMing Lei 		struct request *rq;
1496e6fcbc2SMing Lei 
150f9cd4bfeSJens Axboe 		if (e->type->ops.has_work && !e->type->ops.has_work(hctx))
151caf8eb0dSMing Lei 			break;
152de148297SMing Lei 
15328d65729SSalman Qazi 		if (!list_empty_careful(&hctx->dispatch)) {
1546e6fcbc2SMing Lei 			busy = true;
15528d65729SSalman Qazi 			break;
15628d65729SSalman Qazi 		}
15728d65729SSalman Qazi 
15865c76369SMing Lei 		if (!blk_mq_get_dispatch_budget(q))
1591f460b63SMing Lei 			break;
160de148297SMing Lei 
161f9cd4bfeSJens Axboe 		rq = e->type->ops.dispatch_request(hctx);
162de148297SMing Lei 		if (!rq) {
16365c76369SMing Lei 			blk_mq_put_dispatch_budget(q);
164a0823421SDouglas Anderson 			/*
165a0823421SDouglas Anderson 			 * We're releasing without dispatching. Holding the
166a0823421SDouglas Anderson 			 * budget could have blocked any "hctx"s with the
167a0823421SDouglas Anderson 			 * same queue and if we didn't dispatch then there's
168a0823421SDouglas Anderson 			 * no guarantee anyone will kick the queue.  Kick it
169a0823421SDouglas Anderson 			 * ourselves.
170a0823421SDouglas Anderson 			 */
1716e6fcbc2SMing Lei 			run_queue = true;
172de148297SMing Lei 			break;
173caf8eb0dSMing Lei 		}
174caf8eb0dSMing Lei 
175de148297SMing Lei 		/*
176de148297SMing Lei 		 * Now this rq owns the budget which has to be released
177de148297SMing Lei 		 * if this rq won't be queued to driver via .queue_rq()
178de148297SMing Lei 		 * in blk_mq_dispatch_rq_list().
179de148297SMing Lei 		 */
1806e6fcbc2SMing Lei 		list_add_tail(&rq->queuelist, &rq_list);
1816e6fcbc2SMing Lei 		if (rq->mq_hctx != hctx)
1826e6fcbc2SMing Lei 			multi_hctxs = true;
1836e6fcbc2SMing Lei 	} while (++count < max_dispatch);
1846e6fcbc2SMing Lei 
1856e6fcbc2SMing Lei 	if (!count) {
1866e6fcbc2SMing Lei 		if (run_queue)
1876e6fcbc2SMing Lei 			blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY);
1886e6fcbc2SMing Lei 	} else if (multi_hctxs) {
1896e6fcbc2SMing Lei 		/*
1906e6fcbc2SMing Lei 		 * Requests from different hctx may be dequeued from some
1916e6fcbc2SMing Lei 		 * schedulers, such as bfq and deadline.
1926e6fcbc2SMing Lei 		 *
1936e6fcbc2SMing Lei 		 * Sort the requests in the list according to their hctx,
1946e6fcbc2SMing Lei 		 * dispatch batching requests from same hctx at a time.
1956e6fcbc2SMing Lei 		 */
1966e6fcbc2SMing Lei 		list_sort(NULL, &rq_list, sched_rq_cmp);
1976e6fcbc2SMing Lei 		do {
1986e6fcbc2SMing Lei 			dispatched |= blk_mq_dispatch_hctx_list(&rq_list);
1996e6fcbc2SMing Lei 		} while (!list_empty(&rq_list));
2006e6fcbc2SMing Lei 	} else {
2016e6fcbc2SMing Lei 		dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, count);
2026e6fcbc2SMing Lei 	}
2036e6fcbc2SMing Lei 
2046e6fcbc2SMing Lei 	if (busy)
2056e6fcbc2SMing Lei 		return -EAGAIN;
2066e6fcbc2SMing Lei 	return !!dispatched;
2076e6fcbc2SMing Lei }
2086e6fcbc2SMing Lei 
2096e6fcbc2SMing Lei static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
2106e6fcbc2SMing Lei {
2116e6fcbc2SMing Lei 	int ret;
2126e6fcbc2SMing Lei 
2136e6fcbc2SMing Lei 	do {
2146e6fcbc2SMing Lei 		ret = __blk_mq_do_dispatch_sched(hctx);
2156e6fcbc2SMing Lei 	} while (ret == 1);
21628d65729SSalman Qazi 
21728d65729SSalman Qazi 	return ret;
218de148297SMing Lei }
219de148297SMing Lei 
220b347689fSMing Lei static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
221b347689fSMing Lei 					  struct blk_mq_ctx *ctx)
222b347689fSMing Lei {
223f31967f0SJens Axboe 	unsigned short idx = ctx->index_hw[hctx->type];
224b347689fSMing Lei 
225b347689fSMing Lei 	if (++idx == hctx->nr_ctx)
226b347689fSMing Lei 		idx = 0;
227b347689fSMing Lei 
228b347689fSMing Lei 	return hctx->ctxs[idx];
229b347689fSMing Lei }
230b347689fSMing Lei 
2311f460b63SMing Lei /*
2321f460b63SMing Lei  * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
2331f460b63SMing Lei  * its queue by itself in its completion handler, so we don't need to
2341f460b63SMing Lei  * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
23528d65729SSalman Qazi  *
23628d65729SSalman Qazi  * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to
237c4aecaa2SRandy Dunlap  * be run again.  This is necessary to avoid starving flushes.
2381f460b63SMing Lei  */
23928d65729SSalman Qazi static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
240b347689fSMing Lei {
241b347689fSMing Lei 	struct request_queue *q = hctx->queue;
242b347689fSMing Lei 	LIST_HEAD(rq_list);
243b347689fSMing Lei 	struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);
24428d65729SSalman Qazi 	int ret = 0;
245b347689fSMing Lei 	struct request *rq;
246b347689fSMing Lei 
247445874e8SMing Lei 	do {
24828d65729SSalman Qazi 		if (!list_empty_careful(&hctx->dispatch)) {
24928d65729SSalman Qazi 			ret = -EAGAIN;
25028d65729SSalman Qazi 			break;
25128d65729SSalman Qazi 		}
25228d65729SSalman Qazi 
253b347689fSMing Lei 		if (!sbitmap_any_bit_set(&hctx->ctx_map))
254b347689fSMing Lei 			break;
255b347689fSMing Lei 
25665c76369SMing Lei 		if (!blk_mq_get_dispatch_budget(q))
2571f460b63SMing Lei 			break;
258b347689fSMing Lei 
259b347689fSMing Lei 		rq = blk_mq_dequeue_from_ctx(hctx, ctx);
260b347689fSMing Lei 		if (!rq) {
26165c76369SMing Lei 			blk_mq_put_dispatch_budget(q);
262a0823421SDouglas Anderson 			/*
263a0823421SDouglas Anderson 			 * We're releasing without dispatching. Holding the
264a0823421SDouglas Anderson 			 * budget could have blocked any "hctx"s with the
265a0823421SDouglas Anderson 			 * same queue and if we didn't dispatch then there's
266a0823421SDouglas Anderson 			 * no guarantee anyone will kick the queue.  Kick it
267a0823421SDouglas Anderson 			 * ourselves.
268a0823421SDouglas Anderson 			 */
269a0823421SDouglas Anderson 			blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY);
270b347689fSMing Lei 			break;
271b347689fSMing Lei 		}
272b347689fSMing Lei 
273b347689fSMing Lei 		/*
274b347689fSMing Lei 		 * Now this rq owns the budget which has to be released
275b347689fSMing Lei 		 * if this rq won't be queued to driver via .queue_rq()
276b347689fSMing Lei 		 * in blk_mq_dispatch_rq_list().
277b347689fSMing Lei 		 */
278b347689fSMing Lei 		list_add(&rq->queuelist, &rq_list);
279b347689fSMing Lei 
280b347689fSMing Lei 		/* round robin for fair dispatch */
281b347689fSMing Lei 		ctx = blk_mq_next_ctx(hctx, rq->mq_ctx);
282b347689fSMing Lei 
2831fd40b5eSMing Lei 	} while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, 1));
284b347689fSMing Lei 
285b347689fSMing Lei 	WRITE_ONCE(hctx->dispatch_from, ctx);
28628d65729SSalman Qazi 	return ret;
287b347689fSMing Lei }
288b347689fSMing Lei 
289e1b586f2SZheng Bin static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
290bd166ef1SJens Axboe {
29181380ca1SOmar Sandoval 	struct request_queue *q = hctx->queue;
29281380ca1SOmar Sandoval 	struct elevator_queue *e = q->elevator;
293f9cd4bfeSJens Axboe 	const bool has_sched_dispatch = e && e->type->ops.dispatch_request;
29428d65729SSalman Qazi 	int ret = 0;
295bd166ef1SJens Axboe 	LIST_HEAD(rq_list);
296bd166ef1SJens Axboe 
297bd166ef1SJens Axboe 	/*
298bd166ef1SJens Axboe 	 * If we have previous entries on our dispatch list, grab them first for
299bd166ef1SJens Axboe 	 * more fair dispatch.
300bd166ef1SJens Axboe 	 */
301bd166ef1SJens Axboe 	if (!list_empty_careful(&hctx->dispatch)) {
302bd166ef1SJens Axboe 		spin_lock(&hctx->lock);
303bd166ef1SJens Axboe 		if (!list_empty(&hctx->dispatch))
304bd166ef1SJens Axboe 			list_splice_init(&hctx->dispatch, &rq_list);
305bd166ef1SJens Axboe 		spin_unlock(&hctx->lock);
306bd166ef1SJens Axboe 	}
307bd166ef1SJens Axboe 
308bd166ef1SJens Axboe 	/*
309bd166ef1SJens Axboe 	 * Only ask the scheduler for requests, if we didn't have residual
310bd166ef1SJens Axboe 	 * requests from the dispatch list. This is to avoid the case where
311bd166ef1SJens Axboe 	 * we only ever dispatch a fraction of the requests available because
312bd166ef1SJens Axboe 	 * of low device queue depth. Once we pull requests out of the IO
313bd166ef1SJens Axboe 	 * scheduler, we can no longer merge or sort them. So it's best to
314bd166ef1SJens Axboe 	 * leave them there for as long as we can. Mark the hw queue as
315bd166ef1SJens Axboe 	 * needing a restart in that case.
316caf8eb0dSMing Lei 	 *
3175e3d02bbSMing Lei 	 * We want to dispatch from the scheduler if there was nothing
3185e3d02bbSMing Lei 	 * on the dispatch list or we were able to dispatch from the
3195e3d02bbSMing Lei 	 * dispatch list.
32064765a75SJens Axboe 	 */
321caf8eb0dSMing Lei 	if (!list_empty(&rq_list)) {
322caf8eb0dSMing Lei 		blk_mq_sched_mark_restart_hctx(hctx);
3231fd40b5eSMing Lei 		if (blk_mq_dispatch_rq_list(hctx, &rq_list, 0)) {
324b347689fSMing Lei 			if (has_sched_dispatch)
32528d65729SSalman Qazi 				ret = blk_mq_do_dispatch_sched(hctx);
326b347689fSMing Lei 			else
32728d65729SSalman Qazi 				ret = blk_mq_do_dispatch_ctx(hctx);
328b347689fSMing Lei 		}
329caf8eb0dSMing Lei 	} else if (has_sched_dispatch) {
33028d65729SSalman Qazi 		ret = blk_mq_do_dispatch_sched(hctx);
3316e768717SMing Lei 	} else if (hctx->dispatch_busy) {
3326e768717SMing Lei 		/* dequeue request one by one from sw queue if queue is busy */
33328d65729SSalman Qazi 		ret = blk_mq_do_dispatch_ctx(hctx);
334caf8eb0dSMing Lei 	} else {
335caf8eb0dSMing Lei 		blk_mq_flush_busy_ctxs(hctx, &rq_list);
3361fd40b5eSMing Lei 		blk_mq_dispatch_rq_list(hctx, &rq_list, 0);
337c13660a0SJens Axboe 	}
33828d65729SSalman Qazi 
33928d65729SSalman Qazi 	return ret;
34028d65729SSalman Qazi }
34128d65729SSalman Qazi 
34228d65729SSalman Qazi void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
34328d65729SSalman Qazi {
34428d65729SSalman Qazi 	struct request_queue *q = hctx->queue;
34528d65729SSalman Qazi 
34628d65729SSalman Qazi 	/* RCU or SRCU read lock is needed before checking quiesced flag */
34728d65729SSalman Qazi 	if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
34828d65729SSalman Qazi 		return;
34928d65729SSalman Qazi 
35028d65729SSalman Qazi 	hctx->run++;
35128d65729SSalman Qazi 
35228d65729SSalman Qazi 	/*
35328d65729SSalman Qazi 	 * A return of -EAGAIN is an indication that hctx->dispatch is not
35428d65729SSalman Qazi 	 * empty and we must run again in order to avoid starving flushes.
35528d65729SSalman Qazi 	 */
35628d65729SSalman Qazi 	if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) {
35728d65729SSalman Qazi 		if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN)
35828d65729SSalman Qazi 			blk_mq_run_hw_queue(hctx, true);
35928d65729SSalman Qazi 	}
360bd166ef1SJens Axboe }
361bd166ef1SJens Axboe 
362e4d750c9SJens Axboe bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
36314ccb66bSChristoph Hellwig 		unsigned int nr_segs, struct request **merged_request)
364bd166ef1SJens Axboe {
365bd166ef1SJens Axboe 	struct request *rq;
366bd166ef1SJens Axboe 
36734fe7c05SChristoph Hellwig 	switch (elv_merge(q, &rq, bio)) {
36834fe7c05SChristoph Hellwig 	case ELEVATOR_BACK_MERGE:
369bd166ef1SJens Axboe 		if (!blk_mq_sched_allow_merge(q, rq, bio))
370bd166ef1SJens Axboe 			return false;
3717d7ca7c5SBaolin Wang 		if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
37234fe7c05SChristoph Hellwig 			return false;
373e4d750c9SJens Axboe 		*merged_request = attempt_back_merge(q, rq);
374e4d750c9SJens Axboe 		if (!*merged_request)
37534fe7c05SChristoph Hellwig 			elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
376bd166ef1SJens Axboe 		return true;
37734fe7c05SChristoph Hellwig 	case ELEVATOR_FRONT_MERGE:
378bd166ef1SJens Axboe 		if (!blk_mq_sched_allow_merge(q, rq, bio))
379bd166ef1SJens Axboe 			return false;
3807d7ca7c5SBaolin Wang 		if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
38134fe7c05SChristoph Hellwig 			return false;
382e4d750c9SJens Axboe 		*merged_request = attempt_front_merge(q, rq);
383e4d750c9SJens Axboe 		if (!*merged_request)
38434fe7c05SChristoph Hellwig 			elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
385bd166ef1SJens Axboe 		return true;
386bea99a50SKeith Busch 	case ELEVATOR_DISCARD_MERGE:
3877d7ca7c5SBaolin Wang 		return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK;
38834fe7c05SChristoph Hellwig 	default:
389bd166ef1SJens Axboe 		return false;
390bd166ef1SJens Axboe 	}
39134fe7c05SChristoph Hellwig }
392bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
393bd166ef1SJens Axboe 
39414ccb66bSChristoph Hellwig bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio,
39514ccb66bSChristoph Hellwig 		unsigned int nr_segs)
396bd166ef1SJens Axboe {
397bd166ef1SJens Axboe 	struct elevator_queue *e = q->elevator;
398bd166ef1SJens Axboe 	struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
3998ccdf4a3SJianchao Wang 	struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, bio->bi_opf, ctx);
4009bddeb2aSMing Lei 	bool ret = false;
401c16d6b5aSMing Lei 	enum hctx_type type;
402bd166ef1SJens Axboe 
403c05f4220SBart Van Assche 	if (e && e->type->ops.bio_merge)
40414ccb66bSChristoph Hellwig 		return e->type->ops.bio_merge(hctx, bio, nr_segs);
405bd166ef1SJens Axboe 
406c16d6b5aSMing Lei 	type = hctx->type;
407cdfcef9eSBaolin Wang 	if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) ||
408cdfcef9eSBaolin Wang 	    list_empty_careful(&ctx->rq_lists[type]))
409cdfcef9eSBaolin Wang 		return false;
410cdfcef9eSBaolin Wang 
4119bddeb2aSMing Lei 	/* default per sw-queue merge */
4129bddeb2aSMing Lei 	spin_lock(&ctx->lock);
413cdfcef9eSBaolin Wang 	/*
414cdfcef9eSBaolin Wang 	 * Reverse check our software queue for entries that we could
415cdfcef9eSBaolin Wang 	 * potentially merge with. Currently includes a hand-wavy stop
416cdfcef9eSBaolin Wang 	 * count of 8, to not spend too much time checking for merges.
417cdfcef9eSBaolin Wang 	 */
418cdfcef9eSBaolin Wang 	if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs)) {
419cdfcef9eSBaolin Wang 		ctx->rq_merged++;
420cdfcef9eSBaolin Wang 		ret = true;
4219bddeb2aSMing Lei 	}
4229bddeb2aSMing Lei 
423cdfcef9eSBaolin Wang 	spin_unlock(&ctx->lock);
424cdfcef9eSBaolin Wang 
4259bddeb2aSMing Lei 	return ret;
426bd166ef1SJens Axboe }
427bd166ef1SJens Axboe 
428bd166ef1SJens Axboe bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
429bd166ef1SJens Axboe {
430bd166ef1SJens Axboe 	return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
431bd166ef1SJens Axboe }
432bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
433bd166ef1SJens Axboe 
434bd166ef1SJens Axboe void blk_mq_sched_request_inserted(struct request *rq)
435bd166ef1SJens Axboe {
436bd166ef1SJens Axboe 	trace_block_rq_insert(rq->q, rq);
437bd166ef1SJens Axboe }
438bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
439bd166ef1SJens Axboe 
4400cacba6cSOmar Sandoval static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
441a6a252e6SMing Lei 				       bool has_sched,
4420cacba6cSOmar Sandoval 				       struct request *rq)
443bd166ef1SJens Axboe {
44401e99aecSMing Lei 	/*
44501e99aecSMing Lei 	 * dispatch flush and passthrough rq directly
44601e99aecSMing Lei 	 *
44701e99aecSMing Lei 	 * passthrough request has to be added to hctx->dispatch directly.
44801e99aecSMing Lei 	 * For some reason, device may be in one situation which can't
44901e99aecSMing Lei 	 * handle FS request, so STS_RESOURCE is always returned and the
45001e99aecSMing Lei 	 * FS request will be added to hctx->dispatch. However passthrough
45101e99aecSMing Lei 	 * request may be required at that time for fixing the problem. If
45201e99aecSMing Lei 	 * passthrough request is added to scheduler queue, there isn't any
45301e99aecSMing Lei 	 * chance to dispatch it given we prioritize requests in hctx->dispatch.
45401e99aecSMing Lei 	 */
45501e99aecSMing Lei 	if ((rq->rq_flags & RQF_FLUSH_SEQ) || blk_rq_is_passthrough(rq))
456bd166ef1SJens Axboe 		return true;
457bd166ef1SJens Axboe 
458923218f6SMing Lei 	if (has_sched)
459a6a252e6SMing Lei 		rq->rq_flags |= RQF_SORTED;
460a6a252e6SMing Lei 
461a6a252e6SMing Lei 	return false;
462a6a252e6SMing Lei }
463a6a252e6SMing Lei 
464bd6737f1SJens Axboe void blk_mq_sched_insert_request(struct request *rq, bool at_head,
4659e97d295SMike Snitzer 				 bool run_queue, bool async)
466bd6737f1SJens Axboe {
467bd6737f1SJens Axboe 	struct request_queue *q = rq->q;
468bd6737f1SJens Axboe 	struct elevator_queue *e = q->elevator;
469bd6737f1SJens Axboe 	struct blk_mq_ctx *ctx = rq->mq_ctx;
470ea4f995eSJens Axboe 	struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
471bd6737f1SJens Axboe 
472a6a252e6SMing Lei 	/* flush rq in flush machinery need to be dispatched directly */
473a6a252e6SMing Lei 	if (!(rq->rq_flags & RQF_FLUSH_SEQ) && op_is_flush(rq->cmd_flags)) {
474923218f6SMing Lei 		blk_insert_flush(rq);
475923218f6SMing Lei 		goto run;
476bd6737f1SJens Axboe 	}
477bd6737f1SJens Axboe 
478923218f6SMing Lei 	WARN_ON(e && (rq->tag != -1));
479923218f6SMing Lei 
48001e99aecSMing Lei 	if (blk_mq_sched_bypass_insert(hctx, !!e, rq)) {
481cc3200eaSMing Lei 		/*
482cc3200eaSMing Lei 		 * Firstly normal IO request is inserted to scheduler queue or
483cc3200eaSMing Lei 		 * sw queue, meantime we add flush request to dispatch queue(
484cc3200eaSMing Lei 		 * hctx->dispatch) directly and there is at most one in-flight
485cc3200eaSMing Lei 		 * flush request for each hw queue, so it doesn't matter to add
486cc3200eaSMing Lei 		 * flush request to tail or front of the dispatch queue.
487cc3200eaSMing Lei 		 *
488cc3200eaSMing Lei 		 * Secondly in case of NCQ, flush request belongs to non-NCQ
489cc3200eaSMing Lei 		 * command, and queueing it will fail when there is any
490cc3200eaSMing Lei 		 * in-flight normal IO request(NCQ command). When adding flush
491cc3200eaSMing Lei 		 * rq to the front of hctx->dispatch, it is easier to introduce
492cc3200eaSMing Lei 		 * extra time to flush rq's latency because of S_SCHED_RESTART
493cc3200eaSMing Lei 		 * compared with adding to the tail of dispatch queue, then
494cc3200eaSMing Lei 		 * chance of flush merge is increased, and less flush requests
495cc3200eaSMing Lei 		 * will be issued to controller. It is observed that ~10% time
496cc3200eaSMing Lei 		 * is saved in blktests block/004 on disk attached to AHCI/NCQ
497cc3200eaSMing Lei 		 * drive when adding flush rq to the front of hctx->dispatch.
498cc3200eaSMing Lei 		 *
499cc3200eaSMing Lei 		 * Simply queue flush rq to the front of hctx->dispatch so that
500cc3200eaSMing Lei 		 * intensive flush workloads can benefit in case of NCQ HW.
501cc3200eaSMing Lei 		 */
502cc3200eaSMing Lei 		at_head = (rq->rq_flags & RQF_FLUSH_SEQ) ? true : at_head;
50301e99aecSMing Lei 		blk_mq_request_bypass_insert(rq, at_head, false);
5040cacba6cSOmar Sandoval 		goto run;
50501e99aecSMing Lei 	}
5060cacba6cSOmar Sandoval 
507f9cd4bfeSJens Axboe 	if (e && e->type->ops.insert_requests) {
508bd6737f1SJens Axboe 		LIST_HEAD(list);
509bd6737f1SJens Axboe 
510bd6737f1SJens Axboe 		list_add(&rq->queuelist, &list);
511f9cd4bfeSJens Axboe 		e->type->ops.insert_requests(hctx, &list, at_head);
512bd6737f1SJens Axboe 	} else {
513bd6737f1SJens Axboe 		spin_lock(&ctx->lock);
514bd6737f1SJens Axboe 		__blk_mq_insert_request(hctx, rq, at_head);
515bd6737f1SJens Axboe 		spin_unlock(&ctx->lock);
516bd6737f1SJens Axboe 	}
517bd6737f1SJens Axboe 
5180cacba6cSOmar Sandoval run:
519bd6737f1SJens Axboe 	if (run_queue)
520bd6737f1SJens Axboe 		blk_mq_run_hw_queue(hctx, async);
521bd6737f1SJens Axboe }
522bd6737f1SJens Axboe 
52367cae4c9SJens Axboe void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx,
524bd6737f1SJens Axboe 				  struct blk_mq_ctx *ctx,
525bd6737f1SJens Axboe 				  struct list_head *list, bool run_queue_async)
526bd6737f1SJens Axboe {
527f9afca4dSJens Axboe 	struct elevator_queue *e;
528e87eb301SMing Lei 	struct request_queue *q = hctx->queue;
529e87eb301SMing Lei 
530e87eb301SMing Lei 	/*
531e87eb301SMing Lei 	 * blk_mq_sched_insert_requests() is called from flush plug
532e87eb301SMing Lei 	 * context only, and hold one usage counter to prevent queue
533e87eb301SMing Lei 	 * from being released.
534e87eb301SMing Lei 	 */
535e87eb301SMing Lei 	percpu_ref_get(&q->q_usage_counter);
536f9afca4dSJens Axboe 
537f9afca4dSJens Axboe 	e = hctx->queue->elevator;
538f9cd4bfeSJens Axboe 	if (e && e->type->ops.insert_requests)
539f9cd4bfeSJens Axboe 		e->type->ops.insert_requests(hctx, list, false);
5406ce3dd6eSMing Lei 	else {
5416ce3dd6eSMing Lei 		/*
5426ce3dd6eSMing Lei 		 * try to issue requests directly if the hw queue isn't
5436ce3dd6eSMing Lei 		 * busy in case of 'none' scheduler, and this way may save
5446ce3dd6eSMing Lei 		 * us one extra enqueue & dequeue to sw queue.
5456ce3dd6eSMing Lei 		 */
546fd9c40f6SBart Van Assche 		if (!hctx->dispatch_busy && !e && !run_queue_async) {
5476ce3dd6eSMing Lei 			blk_mq_try_issue_list_directly(hctx, list);
548fd9c40f6SBart Van Assche 			if (list_empty(list))
549e87eb301SMing Lei 				goto out;
550fd9c40f6SBart Van Assche 		}
551bd6737f1SJens Axboe 		blk_mq_insert_requests(hctx, ctx, list);
5526ce3dd6eSMing Lei 	}
553bd6737f1SJens Axboe 
554bd6737f1SJens Axboe 	blk_mq_run_hw_queue(hctx, run_queue_async);
555e87eb301SMing Lei  out:
556e87eb301SMing Lei 	percpu_ref_put(&q->q_usage_counter);
557bd6737f1SJens Axboe }
558bd6737f1SJens Axboe 
559bd166ef1SJens Axboe static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
560bd166ef1SJens Axboe 				   struct blk_mq_hw_ctx *hctx,
561bd166ef1SJens Axboe 				   unsigned int hctx_idx)
562bd166ef1SJens Axboe {
563bd166ef1SJens Axboe 	if (hctx->sched_tags) {
564bd166ef1SJens Axboe 		blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
565bd166ef1SJens Axboe 		blk_mq_free_rq_map(hctx->sched_tags);
566bd166ef1SJens Axboe 		hctx->sched_tags = NULL;
567bd166ef1SJens Axboe 	}
568bd166ef1SJens Axboe }
569bd166ef1SJens Axboe 
5706917ff0bSOmar Sandoval static int blk_mq_sched_alloc_tags(struct request_queue *q,
5716917ff0bSOmar Sandoval 				   struct blk_mq_hw_ctx *hctx,
5726917ff0bSOmar Sandoval 				   unsigned int hctx_idx)
573bd166ef1SJens Axboe {
574bd166ef1SJens Axboe 	struct blk_mq_tag_set *set = q->tag_set;
5756917ff0bSOmar Sandoval 	int ret;
576bd166ef1SJens Axboe 
5776917ff0bSOmar Sandoval 	hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
5786917ff0bSOmar Sandoval 					       set->reserved_tags);
579bd166ef1SJens Axboe 	if (!hctx->sched_tags)
5806917ff0bSOmar Sandoval 		return -ENOMEM;
5816917ff0bSOmar Sandoval 
5826917ff0bSOmar Sandoval 	ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
5836917ff0bSOmar Sandoval 	if (ret)
5846917ff0bSOmar Sandoval 		blk_mq_sched_free_tags(set, hctx, hctx_idx);
585bd166ef1SJens Axboe 
586bd166ef1SJens Axboe 	return ret;
587bd166ef1SJens Axboe }
588bd166ef1SJens Axboe 
589c3e22192SMing Lei /* called in queue's release handler, tagset has gone away */
59054d5329dSOmar Sandoval static void blk_mq_sched_tags_teardown(struct request_queue *q)
591bd166ef1SJens Axboe {
592bd166ef1SJens Axboe 	struct blk_mq_hw_ctx *hctx;
593bd166ef1SJens Axboe 	int i;
594bd166ef1SJens Axboe 
595c3e22192SMing Lei 	queue_for_each_hw_ctx(q, hctx, i) {
596c3e22192SMing Lei 		if (hctx->sched_tags) {
597c3e22192SMing Lei 			blk_mq_free_rq_map(hctx->sched_tags);
598c3e22192SMing Lei 			hctx->sched_tags = NULL;
599c3e22192SMing Lei 		}
600c3e22192SMing Lei 	}
601bd166ef1SJens Axboe }
602d3484991SJens Axboe 
6036917ff0bSOmar Sandoval int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
6046917ff0bSOmar Sandoval {
6056917ff0bSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
606ee056f98SOmar Sandoval 	struct elevator_queue *eq;
6076917ff0bSOmar Sandoval 	unsigned int i;
6086917ff0bSOmar Sandoval 	int ret;
6096917ff0bSOmar Sandoval 
6106917ff0bSOmar Sandoval 	if (!e) {
6116917ff0bSOmar Sandoval 		q->elevator = NULL;
61232a50fabSMing Lei 		q->nr_requests = q->tag_set->queue_depth;
6136917ff0bSOmar Sandoval 		return 0;
6146917ff0bSOmar Sandoval 	}
6156917ff0bSOmar Sandoval 
6166917ff0bSOmar Sandoval 	/*
61732825c45SMing Lei 	 * Default to double of smaller one between hw queue_depth and 128,
61832825c45SMing Lei 	 * since we don't split into sync/async like the old code did.
61932825c45SMing Lei 	 * Additionally, this is a per-hw queue depth.
6206917ff0bSOmar Sandoval 	 */
62132825c45SMing Lei 	q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth,
62232825c45SMing Lei 				   BLKDEV_MAX_RQ);
6236917ff0bSOmar Sandoval 
6246917ff0bSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
6256917ff0bSOmar Sandoval 		ret = blk_mq_sched_alloc_tags(q, hctx, i);
6266917ff0bSOmar Sandoval 		if (ret)
6276917ff0bSOmar Sandoval 			goto err;
6286917ff0bSOmar Sandoval 	}
6296917ff0bSOmar Sandoval 
630f9cd4bfeSJens Axboe 	ret = e->ops.init_sched(q, e);
6316917ff0bSOmar Sandoval 	if (ret)
6326917ff0bSOmar Sandoval 		goto err;
6336917ff0bSOmar Sandoval 
634d332ce09SOmar Sandoval 	blk_mq_debugfs_register_sched(q);
635d332ce09SOmar Sandoval 
636ee056f98SOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
637f9cd4bfeSJens Axboe 		if (e->ops.init_hctx) {
638f9cd4bfeSJens Axboe 			ret = e->ops.init_hctx(hctx, i);
639ee056f98SOmar Sandoval 			if (ret) {
640ee056f98SOmar Sandoval 				eq = q->elevator;
641c3e22192SMing Lei 				blk_mq_sched_free_requests(q);
642ee056f98SOmar Sandoval 				blk_mq_exit_sched(q, eq);
643ee056f98SOmar Sandoval 				kobject_put(&eq->kobj);
644ee056f98SOmar Sandoval 				return ret;
645ee056f98SOmar Sandoval 			}
646ee056f98SOmar Sandoval 		}
647d332ce09SOmar Sandoval 		blk_mq_debugfs_register_sched_hctx(q, hctx);
648ee056f98SOmar Sandoval 	}
649ee056f98SOmar Sandoval 
6506917ff0bSOmar Sandoval 	return 0;
6516917ff0bSOmar Sandoval 
6526917ff0bSOmar Sandoval err:
653c3e22192SMing Lei 	blk_mq_sched_free_requests(q);
65454d5329dSOmar Sandoval 	blk_mq_sched_tags_teardown(q);
65554d5329dSOmar Sandoval 	q->elevator = NULL;
6566917ff0bSOmar Sandoval 	return ret;
6576917ff0bSOmar Sandoval }
6586917ff0bSOmar Sandoval 
659c3e22192SMing Lei /*
660c3e22192SMing Lei  * called in either blk_queue_cleanup or elevator_switch, tagset
661c3e22192SMing Lei  * is required for freeing requests
662c3e22192SMing Lei  */
663c3e22192SMing Lei void blk_mq_sched_free_requests(struct request_queue *q)
664c3e22192SMing Lei {
665c3e22192SMing Lei 	struct blk_mq_hw_ctx *hctx;
666c3e22192SMing Lei 	int i;
667c3e22192SMing Lei 
668c3e22192SMing Lei 	queue_for_each_hw_ctx(q, hctx, i) {
669c3e22192SMing Lei 		if (hctx->sched_tags)
670c3e22192SMing Lei 			blk_mq_free_rqs(q->tag_set, hctx->sched_tags, i);
671c3e22192SMing Lei 	}
672c3e22192SMing Lei }
673c3e22192SMing Lei 
67454d5329dSOmar Sandoval void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
67554d5329dSOmar Sandoval {
676ee056f98SOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
677ee056f98SOmar Sandoval 	unsigned int i;
678ee056f98SOmar Sandoval 
679ee056f98SOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
680d332ce09SOmar Sandoval 		blk_mq_debugfs_unregister_sched_hctx(hctx);
681f9cd4bfeSJens Axboe 		if (e->type->ops.exit_hctx && hctx->sched_data) {
682f9cd4bfeSJens Axboe 			e->type->ops.exit_hctx(hctx, i);
683ee056f98SOmar Sandoval 			hctx->sched_data = NULL;
684ee056f98SOmar Sandoval 		}
685ee056f98SOmar Sandoval 	}
686d332ce09SOmar Sandoval 	blk_mq_debugfs_unregister_sched(q);
687f9cd4bfeSJens Axboe 	if (e->type->ops.exit_sched)
688f9cd4bfeSJens Axboe 		e->type->ops.exit_sched(e);
68954d5329dSOmar Sandoval 	blk_mq_sched_tags_teardown(q);
69054d5329dSOmar Sandoval 	q->elevator = NULL;
69154d5329dSOmar Sandoval }
692