1 /*
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-debugfs.h"
15 #include "blk-mq-sched.h"
16 #include "blk-mq-tag.h"
17 #include "blk-wbt.h"
18 
blk_mq_sched_free_hctx_data(struct request_queue * q,void (* exit)(struct blk_mq_hw_ctx *))19 void 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 }
32 EXPORT_SYMBOL_GPL(blk_mq_sched_free_hctx_data);
33 
blk_mq_sched_assign_ioc(struct request * rq,struct bio * bio)34 void blk_mq_sched_assign_ioc(struct request *rq, struct bio *bio)
35 {
36 	struct request_queue *q = rq->q;
37 	struct io_context *ioc = rq_ioc(bio);
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 	get_io_context(icq->ioc);
50 	rq->elv.icq = icq;
51 }
52 
53 /*
54  * Mark a hardware queue as needing a restart.
55  */
blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx * hctx)56 void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx)
57 {
58 	if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
59 		return;
60 
61 	set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
62 }
63 EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx);
64 
blk_mq_sched_restart(struct blk_mq_hw_ctx * hctx)65 void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
66 {
67 	if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
68 		return;
69 	clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
70 
71 	/*
72 	 * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch)
73 	 * in blk_mq_run_hw_queue(). Its pair is the barrier in
74 	 * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART,
75 	 * meantime new request added to hctx->dispatch is missed to check in
76 	 * blk_mq_run_hw_queue().
77 	 */
78 	smp_mb();
79 
80 	blk_mq_run_hw_queue(hctx, true);
81 }
82 
83 /*
84  * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
85  * its queue by itself in its completion handler, so we don't need to
86  * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
87  */
blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx * hctx)88 static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
89 {
90 	struct request_queue *q = hctx->queue;
91 	struct elevator_queue *e = q->elevator;
92 	LIST_HEAD(rq_list);
93 
94 	do {
95 		struct request *rq;
96 
97 		if (e->type->ops.mq.has_work &&
98 				!e->type->ops.mq.has_work(hctx))
99 			break;
100 
101 		if (!blk_mq_get_dispatch_budget(hctx))
102 			break;
103 
104 		rq = e->type->ops.mq.dispatch_request(hctx);
105 		if (!rq) {
106 			blk_mq_put_dispatch_budget(hctx);
107 			break;
108 		}
109 
110 		/*
111 		 * Now this rq owns the budget which has to be released
112 		 * if this rq won't be queued to driver via .queue_rq()
113 		 * in blk_mq_dispatch_rq_list().
114 		 */
115 		list_add(&rq->queuelist, &rq_list);
116 	} while (blk_mq_dispatch_rq_list(q, &rq_list, true));
117 }
118 
blk_mq_next_ctx(struct blk_mq_hw_ctx * hctx,struct blk_mq_ctx * ctx)119 static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
120 					  struct blk_mq_ctx *ctx)
121 {
122 	unsigned idx = ctx->index_hw;
123 
124 	if (++idx == hctx->nr_ctx)
125 		idx = 0;
126 
127 	return hctx->ctxs[idx];
128 }
129 
130 /*
131  * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
132  * its queue by itself in its completion handler, so we don't need to
133  * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE.
134  */
blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx * hctx)135 static void blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
136 {
137 	struct request_queue *q = hctx->queue;
138 	LIST_HEAD(rq_list);
139 	struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);
140 
141 	do {
142 		struct request *rq;
143 
144 		if (!sbitmap_any_bit_set(&hctx->ctx_map))
145 			break;
146 
147 		if (!blk_mq_get_dispatch_budget(hctx))
148 			break;
149 
150 		rq = blk_mq_dequeue_from_ctx(hctx, ctx);
151 		if (!rq) {
152 			blk_mq_put_dispatch_budget(hctx);
153 			break;
154 		}
155 
156 		/*
157 		 * Now this rq owns the budget which has to be released
158 		 * if this rq won't be queued to driver via .queue_rq()
159 		 * in blk_mq_dispatch_rq_list().
160 		 */
161 		list_add(&rq->queuelist, &rq_list);
162 
163 		/* round robin for fair dispatch */
164 		ctx = blk_mq_next_ctx(hctx, rq->mq_ctx);
165 
166 	} while (blk_mq_dispatch_rq_list(q, &rq_list, true));
167 
168 	WRITE_ONCE(hctx->dispatch_from, ctx);
169 }
170 
blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx * hctx)171 void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
172 {
173 	struct request_queue *q = hctx->queue;
174 	struct elevator_queue *e = q->elevator;
175 	const bool has_sched_dispatch = e && e->type->ops.mq.dispatch_request;
176 	LIST_HEAD(rq_list);
177 
178 	/* RCU or SRCU read lock is needed before checking quiesced flag */
179 	if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
180 		return;
181 
182 	hctx->run++;
183 
184 	/*
185 	 * If we have previous entries on our dispatch list, grab them first for
186 	 * more fair dispatch.
187 	 */
188 	if (!list_empty_careful(&hctx->dispatch)) {
189 		spin_lock(&hctx->lock);
190 		if (!list_empty(&hctx->dispatch))
191 			list_splice_init(&hctx->dispatch, &rq_list);
192 		spin_unlock(&hctx->lock);
193 	}
194 
195 	/*
196 	 * Only ask the scheduler for requests, if we didn't have residual
197 	 * requests from the dispatch list. This is to avoid the case where
198 	 * we only ever dispatch a fraction of the requests available because
199 	 * of low device queue depth. Once we pull requests out of the IO
200 	 * scheduler, we can no longer merge or sort them. So it's best to
201 	 * leave them there for as long as we can. Mark the hw queue as
202 	 * needing a restart in that case.
203 	 *
204 	 * We want to dispatch from the scheduler if there was nothing
205 	 * on the dispatch list or we were able to dispatch from the
206 	 * dispatch list.
207 	 */
208 	if (!list_empty(&rq_list)) {
209 		blk_mq_sched_mark_restart_hctx(hctx);
210 		if (blk_mq_dispatch_rq_list(q, &rq_list, false)) {
211 			if (has_sched_dispatch)
212 				blk_mq_do_dispatch_sched(hctx);
213 			else
214 				blk_mq_do_dispatch_ctx(hctx);
215 		}
216 	} else if (has_sched_dispatch) {
217 		blk_mq_do_dispatch_sched(hctx);
218 	} else if (hctx->dispatch_busy) {
219 		/* dequeue request one by one from sw queue if queue is busy */
220 		blk_mq_do_dispatch_ctx(hctx);
221 	} else {
222 		blk_mq_flush_busy_ctxs(hctx, &rq_list);
223 		blk_mq_dispatch_rq_list(q, &rq_list, false);
224 	}
225 }
226 
blk_mq_sched_try_merge(struct request_queue * q,struct bio * bio,struct request ** merged_request)227 bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
228 			    struct request **merged_request)
229 {
230 	struct request *rq;
231 
232 	switch (elv_merge(q, &rq, bio)) {
233 	case ELEVATOR_BACK_MERGE:
234 		if (!blk_mq_sched_allow_merge(q, rq, bio))
235 			return false;
236 		if (!bio_attempt_back_merge(q, rq, bio))
237 			return false;
238 		*merged_request = attempt_back_merge(q, rq);
239 		if (!*merged_request)
240 			elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
241 		return true;
242 	case ELEVATOR_FRONT_MERGE:
243 		if (!blk_mq_sched_allow_merge(q, rq, bio))
244 			return false;
245 		if (!bio_attempt_front_merge(q, rq, bio))
246 			return false;
247 		*merged_request = attempt_front_merge(q, rq);
248 		if (!*merged_request)
249 			elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
250 		return true;
251 	case ELEVATOR_DISCARD_MERGE:
252 		return bio_attempt_discard_merge(q, rq, bio);
253 	default:
254 		return false;
255 	}
256 }
257 EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
258 
259 /*
260  * Iterate list of requests and see if we can merge this bio with any
261  * of them.
262  */
blk_mq_bio_list_merge(struct request_queue * q,struct list_head * list,struct bio * bio)263 bool blk_mq_bio_list_merge(struct request_queue *q, struct list_head *list,
264 			   struct bio *bio)
265 {
266 	struct request *rq;
267 	int checked = 8;
268 
269 	list_for_each_entry_reverse(rq, list, queuelist) {
270 		bool merged = false;
271 
272 		if (!checked--)
273 			break;
274 
275 		if (!blk_rq_merge_ok(rq, bio))
276 			continue;
277 
278 		switch (blk_try_merge(rq, bio)) {
279 		case ELEVATOR_BACK_MERGE:
280 			if (blk_mq_sched_allow_merge(q, rq, bio))
281 				merged = bio_attempt_back_merge(q, rq, bio);
282 			break;
283 		case ELEVATOR_FRONT_MERGE:
284 			if (blk_mq_sched_allow_merge(q, rq, bio))
285 				merged = bio_attempt_front_merge(q, rq, bio);
286 			break;
287 		case ELEVATOR_DISCARD_MERGE:
288 			merged = bio_attempt_discard_merge(q, rq, bio);
289 			break;
290 		default:
291 			continue;
292 		}
293 
294 		return merged;
295 	}
296 
297 	return false;
298 }
299 EXPORT_SYMBOL_GPL(blk_mq_bio_list_merge);
300 
301 /*
302  * Reverse check our software queue for entries that we could potentially
303  * merge with. Currently includes a hand-wavy stop count of 8, to not spend
304  * too much time checking for merges.
305  */
blk_mq_attempt_merge(struct request_queue * q,struct blk_mq_ctx * ctx,struct bio * bio)306 static bool blk_mq_attempt_merge(struct request_queue *q,
307 				 struct blk_mq_ctx *ctx, struct bio *bio)
308 {
309 	lockdep_assert_held(&ctx->lock);
310 
311 	if (blk_mq_bio_list_merge(q, &ctx->rq_list, bio)) {
312 		ctx->rq_merged++;
313 		return true;
314 	}
315 
316 	return false;
317 }
318 
__blk_mq_sched_bio_merge(struct request_queue * q,struct bio * bio)319 bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio)
320 {
321 	struct elevator_queue *e = q->elevator;
322 	struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
323 	struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
324 	bool ret = false;
325 
326 	if (e && e->type->ops.mq.bio_merge) {
327 		blk_mq_put_ctx(ctx);
328 		return e->type->ops.mq.bio_merge(hctx, bio);
329 	}
330 
331 	if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
332 			!list_empty_careful(&ctx->rq_list)) {
333 		/* default per sw-queue merge */
334 		spin_lock(&ctx->lock);
335 		ret = blk_mq_attempt_merge(q, ctx, bio);
336 		spin_unlock(&ctx->lock);
337 	}
338 
339 	blk_mq_put_ctx(ctx);
340 	return ret;
341 }
342 
blk_mq_sched_try_insert_merge(struct request_queue * q,struct request * rq)343 bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq)
344 {
345 	return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq);
346 }
347 EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
348 
blk_mq_sched_request_inserted(struct request * rq)349 void blk_mq_sched_request_inserted(struct request *rq)
350 {
351 	trace_block_rq_insert(rq->q, rq);
352 }
353 EXPORT_SYMBOL_GPL(blk_mq_sched_request_inserted);
354 
blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx * hctx,bool has_sched,struct request * rq)355 static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
356 				       bool has_sched,
357 				       struct request *rq)
358 {
359 	/* dispatch flush rq directly */
360 	if (rq->rq_flags & RQF_FLUSH_SEQ) {
361 		spin_lock(&hctx->lock);
362 		list_add(&rq->queuelist, &hctx->dispatch);
363 		spin_unlock(&hctx->lock);
364 		return true;
365 	}
366 
367 	if (has_sched)
368 		rq->rq_flags |= RQF_SORTED;
369 
370 	return false;
371 }
372 
blk_mq_sched_insert_request(struct request * rq,bool at_head,bool run_queue,bool async)373 void blk_mq_sched_insert_request(struct request *rq, bool at_head,
374 				 bool run_queue, bool async)
375 {
376 	struct request_queue *q = rq->q;
377 	struct elevator_queue *e = q->elevator;
378 	struct blk_mq_ctx *ctx = rq->mq_ctx;
379 	struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
380 
381 	/* flush rq in flush machinery need to be dispatched directly */
382 	if (!(rq->rq_flags & RQF_FLUSH_SEQ) && op_is_flush(rq->cmd_flags)) {
383 		blk_insert_flush(rq);
384 		goto run;
385 	}
386 
387 	WARN_ON(e && (rq->tag != -1));
388 
389 	if (blk_mq_sched_bypass_insert(hctx, !!e, rq))
390 		goto run;
391 
392 	if (e && e->type->ops.mq.insert_requests) {
393 		LIST_HEAD(list);
394 
395 		list_add(&rq->queuelist, &list);
396 		e->type->ops.mq.insert_requests(hctx, &list, at_head);
397 	} else {
398 		spin_lock(&ctx->lock);
399 		__blk_mq_insert_request(hctx, rq, at_head);
400 		spin_unlock(&ctx->lock);
401 	}
402 
403 run:
404 	if (run_queue)
405 		blk_mq_run_hw_queue(hctx, async);
406 }
407 
blk_mq_sched_insert_requests(struct request_queue * q,struct blk_mq_ctx * ctx,struct list_head * list,bool run_queue_async)408 void blk_mq_sched_insert_requests(struct request_queue *q,
409 				  struct blk_mq_ctx *ctx,
410 				  struct list_head *list, bool run_queue_async)
411 {
412 	struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
413 	struct elevator_queue *e = hctx->queue->elevator;
414 
415 	if (e && e->type->ops.mq.insert_requests)
416 		e->type->ops.mq.insert_requests(hctx, list, false);
417 	else {
418 		/*
419 		 * try to issue requests directly if the hw queue isn't
420 		 * busy in case of 'none' scheduler, and this way may save
421 		 * us one extra enqueue & dequeue to sw queue.
422 		 */
423 		if (!hctx->dispatch_busy && !e && !run_queue_async) {
424 			blk_mq_try_issue_list_directly(hctx, list);
425 			if (list_empty(list))
426 				return;
427 		}
428 		blk_mq_insert_requests(hctx, ctx, list);
429 	}
430 
431 	blk_mq_run_hw_queue(hctx, run_queue_async);
432 }
433 
blk_mq_sched_free_tags(struct blk_mq_tag_set * set,struct blk_mq_hw_ctx * hctx,unsigned int hctx_idx)434 static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set,
435 				   struct blk_mq_hw_ctx *hctx,
436 				   unsigned int hctx_idx)
437 {
438 	if (hctx->sched_tags) {
439 		blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx);
440 		blk_mq_free_rq_map(hctx->sched_tags);
441 		hctx->sched_tags = NULL;
442 	}
443 }
444 
blk_mq_sched_alloc_tags(struct request_queue * q,struct blk_mq_hw_ctx * hctx,unsigned int hctx_idx)445 static int blk_mq_sched_alloc_tags(struct request_queue *q,
446 				   struct blk_mq_hw_ctx *hctx,
447 				   unsigned int hctx_idx)
448 {
449 	struct blk_mq_tag_set *set = q->tag_set;
450 	int ret;
451 
452 	hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests,
453 					       set->reserved_tags);
454 	if (!hctx->sched_tags)
455 		return -ENOMEM;
456 
457 	ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests);
458 	if (ret)
459 		blk_mq_sched_free_tags(set, hctx, hctx_idx);
460 
461 	return ret;
462 }
463 
blk_mq_sched_tags_teardown(struct request_queue * q)464 static void blk_mq_sched_tags_teardown(struct request_queue *q)
465 {
466 	struct blk_mq_tag_set *set = q->tag_set;
467 	struct blk_mq_hw_ctx *hctx;
468 	int i;
469 
470 	queue_for_each_hw_ctx(q, hctx, i)
471 		blk_mq_sched_free_tags(set, hctx, i);
472 }
473 
blk_mq_init_sched(struct request_queue * q,struct elevator_type * e)474 int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
475 {
476 	struct blk_mq_hw_ctx *hctx;
477 	struct elevator_queue *eq;
478 	unsigned int i;
479 	int ret;
480 
481 	if (!e) {
482 		q->elevator = NULL;
483 		q->nr_requests = q->tag_set->queue_depth;
484 		return 0;
485 	}
486 
487 	/*
488 	 * Default to double of smaller one between hw queue_depth and 128,
489 	 * since we don't split into sync/async like the old code did.
490 	 * Additionally, this is a per-hw queue depth.
491 	 */
492 	q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth,
493 				   BLKDEV_MAX_RQ);
494 
495 	queue_for_each_hw_ctx(q, hctx, i) {
496 		ret = blk_mq_sched_alloc_tags(q, hctx, i);
497 		if (ret)
498 			goto err;
499 	}
500 
501 	ret = e->ops.mq.init_sched(q, e);
502 	if (ret)
503 		goto err;
504 
505 	blk_mq_debugfs_register_sched(q);
506 
507 	queue_for_each_hw_ctx(q, hctx, i) {
508 		if (e->ops.mq.init_hctx) {
509 			ret = e->ops.mq.init_hctx(hctx, i);
510 			if (ret) {
511 				eq = q->elevator;
512 				blk_mq_exit_sched(q, eq);
513 				kobject_put(&eq->kobj);
514 				return ret;
515 			}
516 		}
517 		blk_mq_debugfs_register_sched_hctx(q, hctx);
518 	}
519 
520 	return 0;
521 
522 err:
523 	blk_mq_sched_tags_teardown(q);
524 	q->elevator = NULL;
525 	return ret;
526 }
527 
blk_mq_exit_sched(struct request_queue * q,struct elevator_queue * e)528 void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
529 {
530 	struct blk_mq_hw_ctx *hctx;
531 	unsigned int i;
532 
533 	queue_for_each_hw_ctx(q, hctx, i) {
534 		blk_mq_debugfs_unregister_sched_hctx(hctx);
535 		if (e->type->ops.mq.exit_hctx && hctx->sched_data) {
536 			e->type->ops.mq.exit_hctx(hctx, i);
537 			hctx->sched_data = NULL;
538 		}
539 	}
540 	blk_mq_debugfs_unregister_sched(q);
541 	if (e->type->ops.mq.exit_sched)
542 		e->type->ops.mq.exit_sched(e);
543 	blk_mq_sched_tags_teardown(q);
544 	q->elevator = NULL;
545 }
546