blob: 44da481c3fea919dd2ce2bf0e3e5899941bcc8d8 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe945ffb62017-01-14 17:11:11 -07002/*
3 * MQ Deadline i/o scheduler - adaptation of the legacy deadline scheduler,
4 * for the blk-mq scheduling framework
5 *
6 * Copyright (C) 2016 Jens Axboe <axboe@kernel.dk>
7 */
8#include <linux/kernel.h>
9#include <linux/fs.h>
10#include <linux/blkdev.h>
11#include <linux/blk-mq.h>
12#include <linux/elevator.h>
13#include <linux/bio.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/init.h>
17#include <linux/compiler.h>
18#include <linux/rbtree.h>
19#include <linux/sbitmap.h>
20
Chaitanya Kulkarnib357e4a2021-02-21 21:29:59 -080021#include <trace/events/block.h>
22
Jens Axboe945ffb62017-01-14 17:11:11 -070023#include "blk.h"
24#include "blk-mq.h"
Omar Sandovaldaaadb32017-05-04 00:31:34 -070025#include "blk-mq-debugfs.h"
Jens Axboe945ffb62017-01-14 17:11:11 -070026#include "blk-mq-tag.h"
27#include "blk-mq-sched.h"
28
29/*
Mauro Carvalho Chehab898bd372019-04-18 19:45:00 -030030 * See Documentation/block/deadline-iosched.rst
Jens Axboe945ffb62017-01-14 17:11:11 -070031 */
32static const int read_expire = HZ / 2; /* max time before a read is submitted. */
33static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
34static const int writes_starved = 2; /* max times reads can starve a write */
35static const int fifo_batch = 16; /* # of sequential requests treated as one
36 by the above parameters. For throughput. */
37
Bart Van Assche004a26b2021-06-17 17:44:49 -070038enum dd_data_dir {
39 DD_READ = READ,
40 DD_WRITE = WRITE,
41};
42
43enum { DD_DIR_COUNT = 2 };
44
Jens Axboe945ffb62017-01-14 17:11:11 -070045struct deadline_data {
46 /*
47 * run time data
48 */
49
50 /*
51 * requests (deadline_rq s) are present on both sort_list and fifo_list
52 */
Bart Van Assche004a26b2021-06-17 17:44:49 -070053 struct rb_root sort_list[DD_DIR_COUNT];
54 struct list_head fifo_list[DD_DIR_COUNT];
Jens Axboe945ffb62017-01-14 17:11:11 -070055
56 /*
57 * next in sort order. read, write or both are NULL
58 */
Bart Van Assche004a26b2021-06-17 17:44:49 -070059 struct request *next_rq[DD_DIR_COUNT];
Jens Axboe945ffb62017-01-14 17:11:11 -070060 unsigned int batching; /* number of sequential requests made */
61 unsigned int starved; /* times reads have starved writes */
62
63 /*
64 * settings that change how the i/o scheduler behaves
65 */
Bart Van Assche004a26b2021-06-17 17:44:49 -070066 int fifo_expire[DD_DIR_COUNT];
Jens Axboe945ffb62017-01-14 17:11:11 -070067 int fifo_batch;
68 int writes_starved;
69 int front_merges;
Bart Van Assche07757582021-06-17 17:44:51 -070070 u32 async_depth;
Jens Axboe945ffb62017-01-14 17:11:11 -070071
72 spinlock_t lock;
Damien Le Moal5700f692017-12-21 15:43:40 +090073 spinlock_t zone_lock;
Jens Axboe945ffb62017-01-14 17:11:11 -070074 struct list_head dispatch;
75};
76
77static inline struct rb_root *
78deadline_rb_root(struct deadline_data *dd, struct request *rq)
79{
80 return &dd->sort_list[rq_data_dir(rq)];
81}
82
83/*
84 * get the request after `rq' in sector-sorted order
85 */
86static inline struct request *
87deadline_latter_request(struct request *rq)
88{
89 struct rb_node *node = rb_next(&rq->rb_node);
90
91 if (node)
92 return rb_entry_rq(node);
93
94 return NULL;
95}
96
97static void
98deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
99{
100 struct rb_root *root = deadline_rb_root(dd, rq);
101
102 elv_rb_add(root, rq);
103}
104
105static inline void
106deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)
107{
Bart Van Assche004a26b2021-06-17 17:44:49 -0700108 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700109
110 if (dd->next_rq[data_dir] == rq)
111 dd->next_rq[data_dir] = deadline_latter_request(rq);
112
113 elv_rb_del(deadline_rb_root(dd, rq), rq);
114}
115
116/*
117 * remove rq from rbtree and fifo.
118 */
119static void deadline_remove_request(struct request_queue *q, struct request *rq)
120{
121 struct deadline_data *dd = q->elevator->elevator_data;
122
123 list_del_init(&rq->queuelist);
124
125 /*
126 * We might not be on the rbtree, if we are doing an insert merge
127 */
128 if (!RB_EMPTY_NODE(&rq->rb_node))
129 deadline_del_rq_rb(dd, rq);
130
131 elv_rqhash_del(q, rq);
132 if (q->last_merge == rq)
133 q->last_merge = NULL;
134}
135
136static void dd_request_merged(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100137 enum elv_merge type)
Jens Axboe945ffb62017-01-14 17:11:11 -0700138{
139 struct deadline_data *dd = q->elevator->elevator_data;
140
141 /*
142 * if the merge was a front merge, we need to reposition request
143 */
144 if (type == ELEVATOR_FRONT_MERGE) {
145 elv_rb_del(deadline_rb_root(dd, req), req);
146 deadline_add_rq_rb(dd, req);
147 }
148}
149
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700150/*
151 * Callback function that is invoked after @next has been merged into @req.
152 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700153static void dd_merged_requests(struct request_queue *q, struct request *req,
154 struct request *next)
155{
156 /*
157 * if next expires before rq, assign its expire time to rq
158 * and move into next position (next will be deleted) in fifo
159 */
160 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
161 if (time_before((unsigned long)next->fifo_time,
162 (unsigned long)req->fifo_time)) {
163 list_move(&req->queuelist, &next->queuelist);
164 req->fifo_time = next->fifo_time;
165 }
166 }
167
168 /*
169 * kill knowledge of next, this one is a goner
170 */
171 deadline_remove_request(q, next);
172}
173
174/*
175 * move an entry to dispatch queue
176 */
177static void
178deadline_move_request(struct deadline_data *dd, struct request *rq)
179{
Bart Van Assche004a26b2021-06-17 17:44:49 -0700180 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700181
Bart Van Assche004a26b2021-06-17 17:44:49 -0700182 dd->next_rq[DD_READ] = NULL;
183 dd->next_rq[DD_WRITE] = NULL;
Jens Axboe945ffb62017-01-14 17:11:11 -0700184 dd->next_rq[data_dir] = deadline_latter_request(rq);
185
186 /*
187 * take it off the sort and fifo list
188 */
189 deadline_remove_request(rq->q, rq);
190}
191
192/*
193 * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
194 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
195 */
Bart Van Assche004a26b2021-06-17 17:44:49 -0700196static inline int deadline_check_fifo(struct deadline_data *dd,
197 enum dd_data_dir data_dir)
Jens Axboe945ffb62017-01-14 17:11:11 -0700198{
Bart Van Assche004a26b2021-06-17 17:44:49 -0700199 struct request *rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
Jens Axboe945ffb62017-01-14 17:11:11 -0700200
201 /*
202 * rq is expired!
203 */
204 if (time_after_eq(jiffies, (unsigned long)rq->fifo_time))
205 return 1;
206
207 return 0;
208}
209
210/*
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900211 * For the specified data direction, return the next request to
212 * dispatch using arrival ordered lists.
213 */
214static struct request *
Bart Van Assche004a26b2021-06-17 17:44:49 -0700215deadline_fifo_request(struct deadline_data *dd, enum dd_data_dir data_dir)
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900216{
Damien Le Moal5700f692017-12-21 15:43:40 +0900217 struct request *rq;
218 unsigned long flags;
219
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900220 if (list_empty(&dd->fifo_list[data_dir]))
221 return NULL;
222
Damien Le Moal5700f692017-12-21 15:43:40 +0900223 rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
Bart Van Assche004a26b2021-06-17 17:44:49 -0700224 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
Damien Le Moal5700f692017-12-21 15:43:40 +0900225 return rq;
226
227 /*
228 * Look for a write request that can be dispatched, that is one with
229 * an unlocked target zone.
230 */
231 spin_lock_irqsave(&dd->zone_lock, flags);
Bart Van Assche004a26b2021-06-17 17:44:49 -0700232 list_for_each_entry(rq, &dd->fifo_list[DD_WRITE], queuelist) {
Damien Le Moal5700f692017-12-21 15:43:40 +0900233 if (blk_req_can_dispatch_to_zone(rq))
234 goto out;
235 }
236 rq = NULL;
237out:
238 spin_unlock_irqrestore(&dd->zone_lock, flags);
239
240 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900241}
242
243/*
244 * For the specified data direction, return the next request to
245 * dispatch using sector position sorted lists.
246 */
247static struct request *
Bart Van Assche004a26b2021-06-17 17:44:49 -0700248deadline_next_request(struct deadline_data *dd, enum dd_data_dir data_dir)
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900249{
Damien Le Moal5700f692017-12-21 15:43:40 +0900250 struct request *rq;
251 unsigned long flags;
252
Damien Le Moal5700f692017-12-21 15:43:40 +0900253 rq = dd->next_rq[data_dir];
254 if (!rq)
255 return NULL;
256
Bart Van Assche004a26b2021-06-17 17:44:49 -0700257 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
Damien Le Moal5700f692017-12-21 15:43:40 +0900258 return rq;
259
260 /*
261 * Look for a write request that can be dispatched, that is one with
262 * an unlocked target zone.
263 */
264 spin_lock_irqsave(&dd->zone_lock, flags);
265 while (rq) {
266 if (blk_req_can_dispatch_to_zone(rq))
267 break;
268 rq = deadline_latter_request(rq);
269 }
270 spin_unlock_irqrestore(&dd->zone_lock, flags);
271
272 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900273}
274
275/*
Jens Axboe945ffb62017-01-14 17:11:11 -0700276 * deadline_dispatch_requests selects the best request according to
277 * read/write expire, fifo_batch, etc
278 */
Jens Axboeca11f202018-01-06 09:23:11 -0700279static struct request *__dd_dispatch_request(struct deadline_data *dd)
Jens Axboe945ffb62017-01-14 17:11:11 -0700280{
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900281 struct request *rq, *next_rq;
Bart Van Assche004a26b2021-06-17 17:44:49 -0700282 enum dd_data_dir data_dir;
Jens Axboe945ffb62017-01-14 17:11:11 -0700283
Bart Van Assche3bd473f2021-06-17 17:44:46 -0700284 lockdep_assert_held(&dd->lock);
285
Jens Axboe945ffb62017-01-14 17:11:11 -0700286 if (!list_empty(&dd->dispatch)) {
287 rq = list_first_entry(&dd->dispatch, struct request, queuelist);
288 list_del_init(&rq->queuelist);
289 goto done;
290 }
291
Jens Axboe945ffb62017-01-14 17:11:11 -0700292 /*
293 * batches are currently reads XOR writes
294 */
Bart Van Assche004a26b2021-06-17 17:44:49 -0700295 rq = deadline_next_request(dd, DD_WRITE);
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900296 if (!rq)
Bart Van Assche004a26b2021-06-17 17:44:49 -0700297 rq = deadline_next_request(dd, DD_READ);
Jens Axboe945ffb62017-01-14 17:11:11 -0700298
299 if (rq && dd->batching < dd->fifo_batch)
300 /* we have a next request are still entitled to batch */
301 goto dispatch_request;
302
303 /*
304 * at this point we are not running a batch. select the appropriate
305 * data direction (read / write)
306 */
307
Bart Van Assche004a26b2021-06-17 17:44:49 -0700308 if (!list_empty(&dd->fifo_list[DD_READ])) {
309 BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[DD_READ]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700310
Bart Van Assche004a26b2021-06-17 17:44:49 -0700311 if (deadline_fifo_request(dd, DD_WRITE) &&
Damien Le Moal5700f692017-12-21 15:43:40 +0900312 (dd->starved++ >= dd->writes_starved))
Jens Axboe945ffb62017-01-14 17:11:11 -0700313 goto dispatch_writes;
314
Bart Van Assche004a26b2021-06-17 17:44:49 -0700315 data_dir = DD_READ;
Jens Axboe945ffb62017-01-14 17:11:11 -0700316
317 goto dispatch_find_request;
318 }
319
320 /*
321 * there are either no reads or writes have been starved
322 */
323
Bart Van Assche004a26b2021-06-17 17:44:49 -0700324 if (!list_empty(&dd->fifo_list[DD_WRITE])) {
Jens Axboe945ffb62017-01-14 17:11:11 -0700325dispatch_writes:
Bart Van Assche004a26b2021-06-17 17:44:49 -0700326 BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[DD_WRITE]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700327
328 dd->starved = 0;
329
Bart Van Assche004a26b2021-06-17 17:44:49 -0700330 data_dir = DD_WRITE;
Jens Axboe945ffb62017-01-14 17:11:11 -0700331
332 goto dispatch_find_request;
333 }
334
335 return NULL;
336
337dispatch_find_request:
338 /*
339 * we are not running a batch, find best request for selected data_dir
340 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900341 next_rq = deadline_next_request(dd, data_dir);
342 if (deadline_check_fifo(dd, data_dir) || !next_rq) {
Jens Axboe945ffb62017-01-14 17:11:11 -0700343 /*
344 * A deadline has expired, the last request was in the other
345 * direction, or we have run out of higher-sectored requests.
346 * Start again from the request with the earliest expiry time.
347 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900348 rq = deadline_fifo_request(dd, data_dir);
Jens Axboe945ffb62017-01-14 17:11:11 -0700349 } else {
350 /*
351 * The last req was the same dir and we have a next request in
352 * sort order. No expired requests so continue on from here.
353 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900354 rq = next_rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700355 }
356
Damien Le Moal5700f692017-12-21 15:43:40 +0900357 /*
358 * For a zoned block device, if we only have writes queued and none of
359 * them can be dispatched, rq will be NULL.
360 */
361 if (!rq)
362 return NULL;
363
Jens Axboe945ffb62017-01-14 17:11:11 -0700364 dd->batching = 0;
365
366dispatch_request:
367 /*
368 * rq is the selected appropriate request.
369 */
370 dd->batching++;
371 deadline_move_request(dd, rq);
372done:
Damien Le Moal5700f692017-12-21 15:43:40 +0900373 /*
374 * If the request needs its target zone locked, do it.
375 */
376 blk_req_zone_write_lock(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700377 rq->rq_flags |= RQF_STARTED;
378 return rq;
379}
380
Jens Axboeca11f202018-01-06 09:23:11 -0700381/*
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700382 * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests().
383 *
Jens Axboeca11f202018-01-06 09:23:11 -0700384 * One confusing aspect here is that we get called for a specific
Damien Le Moal7211aef82018-12-17 15:14:05 +0900385 * hardware queue, but we may return a request that is for a
Jens Axboeca11f202018-01-06 09:23:11 -0700386 * different hardware queue. This is because mq-deadline has shared
387 * state for all hardware queues, in terms of sorting, FIFOs, etc.
388 */
Jens Axboec13660a2017-01-26 12:40:07 -0700389static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
Jens Axboe945ffb62017-01-14 17:11:11 -0700390{
391 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
Jens Axboec13660a2017-01-26 12:40:07 -0700392 struct request *rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700393
394 spin_lock(&dd->lock);
Jens Axboeca11f202018-01-06 09:23:11 -0700395 rq = __dd_dispatch_request(dd);
Jens Axboe945ffb62017-01-14 17:11:11 -0700396 spin_unlock(&dd->lock);
Jens Axboec13660a2017-01-26 12:40:07 -0700397
398 return rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700399}
400
Bart Van Assche07757582021-06-17 17:44:51 -0700401/*
402 * Called by __blk_mq_alloc_request(). The shallow_depth value set by this
403 * function is used by __blk_mq_get_tag().
404 */
405static void dd_limit_depth(unsigned int op, struct blk_mq_alloc_data *data)
406{
407 struct deadline_data *dd = data->q->elevator->elevator_data;
408
409 /* Do not throttle synchronous reads. */
410 if (op_is_sync(op) && !op_is_write(op))
411 return;
412
413 /*
414 * Throttle asynchronous requests and writes such that these requests
415 * do not block the allocation of synchronous requests.
416 */
417 data->shallow_depth = dd->async_depth;
418}
419
420/* Called by blk_mq_update_nr_requests(). */
421static void dd_depth_updated(struct blk_mq_hw_ctx *hctx)
422{
423 struct request_queue *q = hctx->queue;
424 struct deadline_data *dd = q->elevator->elevator_data;
425 struct blk_mq_tags *tags = hctx->sched_tags;
426
427 dd->async_depth = max(1UL, 3 * q->nr_requests / 4);
428
429 sbitmap_queue_min_shallow_depth(tags->bitmap_tags, dd->async_depth);
430}
431
432/* Called by blk_mq_init_hctx() and blk_mq_init_sched(). */
433static int dd_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
434{
435 dd_depth_updated(hctx);
436 return 0;
437}
438
Bart Van Assche3e9a99e2021-06-17 17:44:48 -0700439static void dd_exit_sched(struct elevator_queue *e)
Jens Axboe945ffb62017-01-14 17:11:11 -0700440{
441 struct deadline_data *dd = e->elevator_data;
442
Bart Van Assche004a26b2021-06-17 17:44:49 -0700443 BUG_ON(!list_empty(&dd->fifo_list[DD_READ]));
444 BUG_ON(!list_empty(&dd->fifo_list[DD_WRITE]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700445
446 kfree(dd);
447}
448
449/*
450 * initialize elevator private data (deadline_data).
451 */
Bart Van Assche3e9a99e2021-06-17 17:44:48 -0700452static int dd_init_sched(struct request_queue *q, struct elevator_type *e)
Jens Axboe945ffb62017-01-14 17:11:11 -0700453{
454 struct deadline_data *dd;
455 struct elevator_queue *eq;
456
457 eq = elevator_alloc(q, e);
458 if (!eq)
459 return -ENOMEM;
460
461 dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
462 if (!dd) {
463 kobject_put(&eq->kobj);
464 return -ENOMEM;
465 }
466 eq->elevator_data = dd;
467
Bart Van Assche004a26b2021-06-17 17:44:49 -0700468 INIT_LIST_HEAD(&dd->fifo_list[DD_READ]);
469 INIT_LIST_HEAD(&dd->fifo_list[DD_WRITE]);
470 dd->sort_list[DD_READ] = RB_ROOT;
471 dd->sort_list[DD_WRITE] = RB_ROOT;
472 dd->fifo_expire[DD_READ] = read_expire;
473 dd->fifo_expire[DD_WRITE] = write_expire;
Jens Axboe945ffb62017-01-14 17:11:11 -0700474 dd->writes_starved = writes_starved;
475 dd->front_merges = 1;
476 dd->fifo_batch = fifo_batch;
477 spin_lock_init(&dd->lock);
Damien Le Moal5700f692017-12-21 15:43:40 +0900478 spin_lock_init(&dd->zone_lock);
Jens Axboe945ffb62017-01-14 17:11:11 -0700479 INIT_LIST_HEAD(&dd->dispatch);
480
481 q->elevator = eq;
482 return 0;
483}
484
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700485/*
486 * Try to merge @bio into an existing request. If @bio has been merged into
487 * an existing request, store the pointer to that request into *@rq.
488 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700489static int dd_request_merge(struct request_queue *q, struct request **rq,
490 struct bio *bio)
491{
492 struct deadline_data *dd = q->elevator->elevator_data;
493 sector_t sector = bio_end_sector(bio);
494 struct request *__rq;
495
496 if (!dd->front_merges)
497 return ELEVATOR_NO_MERGE;
498
499 __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);
500 if (__rq) {
501 BUG_ON(sector != blk_rq_pos(__rq));
502
503 if (elv_bio_merge_ok(__rq, bio)) {
504 *rq = __rq;
505 return ELEVATOR_FRONT_MERGE;
506 }
507 }
508
509 return ELEVATOR_NO_MERGE;
510}
511
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700512/*
513 * Attempt to merge a bio into an existing request. This function is called
514 * before @bio is associated with a request.
515 */
Omar Sandovalefed9a32021-05-10 17:05:35 -0700516static bool dd_bio_merge(struct request_queue *q, struct bio *bio,
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200517 unsigned int nr_segs)
Jens Axboe945ffb62017-01-14 17:11:11 -0700518{
Jens Axboe945ffb62017-01-14 17:11:11 -0700519 struct deadline_data *dd = q->elevator->elevator_data;
Jens Axboee4d750c2017-02-03 09:48:28 -0700520 struct request *free = NULL;
521 bool ret;
Jens Axboe945ffb62017-01-14 17:11:11 -0700522
523 spin_lock(&dd->lock);
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200524 ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
Jens Axboe945ffb62017-01-14 17:11:11 -0700525 spin_unlock(&dd->lock);
526
Jens Axboee4d750c2017-02-03 09:48:28 -0700527 if (free)
528 blk_mq_free_request(free);
529
Jens Axboe945ffb62017-01-14 17:11:11 -0700530 return ret;
531}
532
533/*
534 * add rq to rbtree and fifo
535 */
536static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
537 bool at_head)
538{
539 struct request_queue *q = hctx->queue;
540 struct deadline_data *dd = q->elevator->elevator_data;
Bart Van Assche004a26b2021-06-17 17:44:49 -0700541 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700542
Bart Van Assche3bd473f2021-06-17 17:44:46 -0700543 lockdep_assert_held(&dd->lock);
544
Damien Le Moal5700f692017-12-21 15:43:40 +0900545 /*
546 * This may be a requeue of a write request that has locked its
547 * target zone. If it is the case, this releases the zone lock.
548 */
549 blk_req_zone_write_unlock(rq);
550
Jens Axboe945ffb62017-01-14 17:11:11 -0700551 if (blk_mq_sched_try_insert_merge(q, rq))
552 return;
553
Chaitanya Kulkarnib357e4a2021-02-21 21:29:59 -0800554 trace_block_rq_insert(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700555
Lin Feng7687b382021-04-15 11:43:26 +0800556 if (at_head) {
557 list_add(&rq->queuelist, &dd->dispatch);
Jens Axboe945ffb62017-01-14 17:11:11 -0700558 } else {
559 deadline_add_rq_rb(dd, rq);
560
561 if (rq_mergeable(rq)) {
562 elv_rqhash_add(q, rq);
563 if (!q->last_merge)
564 q->last_merge = rq;
565 }
566
567 /*
568 * set expire time and add to fifo list
569 */
570 rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
571 list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);
572 }
573}
574
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700575/*
576 * Called from blk_mq_sched_insert_request() or blk_mq_sched_insert_requests().
577 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700578static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
579 struct list_head *list, bool at_head)
580{
581 struct request_queue *q = hctx->queue;
582 struct deadline_data *dd = q->elevator->elevator_data;
583
584 spin_lock(&dd->lock);
585 while (!list_empty(list)) {
586 struct request *rq;
587
588 rq = list_first_entry(list, struct request, queuelist);
589 list_del_init(&rq->queuelist);
590 dd_insert_request(hctx, rq, at_head);
591 }
592 spin_unlock(&dd->lock);
593}
594
Damien Le Moal5700f692017-12-21 15:43:40 +0900595/*
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800596 * Nothing to do here. This is defined only to ensure that .finish_request
597 * method is called upon request completion.
598 */
Christoph Hellwig5d9c3052020-05-29 15:53:08 +0200599static void dd_prepare_request(struct request *rq)
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800600{
601}
602
603/*
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700604 * Callback from inside blk_mq_free_request().
605 *
Damien Le Moal5700f692017-12-21 15:43:40 +0900606 * For zoned block devices, write unlock the target zone of
607 * completed write requests. Do this while holding the zone lock
608 * spinlock so that the zone is never unlocked while deadline_fifo_request()
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800609 * or deadline_next_request() are executing. This function is called for
610 * all requests, whether or not these requests complete successfully.
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900611 *
612 * For a zoned block device, __dd_dispatch_request() may have stopped
613 * dispatching requests if all the queued requests are write requests directed
614 * at zones that are already locked due to on-going write requests. To ensure
615 * write request dispatch progress in this case, mark the queue as needing a
616 * restart to ensure that the queue is run again after completion of the
617 * request and zones being unlocked.
Damien Le Moal5700f692017-12-21 15:43:40 +0900618 */
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800619static void dd_finish_request(struct request *rq)
Damien Le Moal5700f692017-12-21 15:43:40 +0900620{
621 struct request_queue *q = rq->q;
622
623 if (blk_queue_is_zoned(q)) {
624 struct deadline_data *dd = q->elevator->elevator_data;
625 unsigned long flags;
626
627 spin_lock_irqsave(&dd->zone_lock, flags);
628 blk_req_zone_write_unlock(rq);
Bart Van Assche004a26b2021-06-17 17:44:49 -0700629 if (!list_empty(&dd->fifo_list[DD_WRITE]))
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900630 blk_mq_sched_mark_restart_hctx(rq->mq_hctx);
Damien Le Moal5700f692017-12-21 15:43:40 +0900631 spin_unlock_irqrestore(&dd->zone_lock, flags);
632 }
633}
634
Jens Axboe945ffb62017-01-14 17:11:11 -0700635static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
636{
637 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
638
639 return !list_empty_careful(&dd->dispatch) ||
640 !list_empty_careful(&dd->fifo_list[0]) ||
641 !list_empty_careful(&dd->fifo_list[1]);
642}
643
644/*
645 * sysfs parts below
646 */
Bart Van Assched6d7f012021-06-17 17:44:50 -0700647#define SHOW_INT(__FUNC, __VAR) \
Jens Axboe945ffb62017-01-14 17:11:11 -0700648static ssize_t __FUNC(struct elevator_queue *e, char *page) \
649{ \
650 struct deadline_data *dd = e->elevator_data; \
Bart Van Assched6d7f012021-06-17 17:44:50 -0700651 \
652 return sysfs_emit(page, "%d\n", __VAR); \
Jens Axboe945ffb62017-01-14 17:11:11 -0700653}
Bart Van Assched6d7f012021-06-17 17:44:50 -0700654#define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR))
655SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]);
656SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]);
657SHOW_INT(deadline_writes_starved_show, dd->writes_starved);
658SHOW_INT(deadline_front_merges_show, dd->front_merges);
Bart Van Assche07757582021-06-17 17:44:51 -0700659SHOW_INT(deadline_async_depth_show, dd->front_merges);
Bart Van Assched6d7f012021-06-17 17:44:50 -0700660SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch);
661#undef SHOW_INT
662#undef SHOW_JIFFIES
Jens Axboe945ffb62017-01-14 17:11:11 -0700663
664#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
665static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
666{ \
667 struct deadline_data *dd = e->elevator_data; \
Bart Van Assched6d7f012021-06-17 17:44:50 -0700668 int __data, __ret; \
669 \
670 __ret = kstrtoint(page, 0, &__data); \
671 if (__ret < 0) \
672 return __ret; \
Jens Axboe945ffb62017-01-14 17:11:11 -0700673 if (__data < (MIN)) \
674 __data = (MIN); \
675 else if (__data > (MAX)) \
676 __data = (MAX); \
Bart Van Assched6d7f012021-06-17 17:44:50 -0700677 *(__PTR) = __CONV(__data); \
weiping zhang235f8da2017-08-25 01:11:33 +0800678 return count; \
Jens Axboe945ffb62017-01-14 17:11:11 -0700679}
Bart Van Assched6d7f012021-06-17 17:44:50 -0700680#define STORE_INT(__FUNC, __PTR, MIN, MAX) \
681 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, )
682#define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \
683 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies)
684STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX);
685STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX);
686STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX);
687STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1);
Bart Van Assche07757582021-06-17 17:44:51 -0700688STORE_INT(deadline_async_depth_store, &dd->front_merges, 1, INT_MAX);
Bart Van Assched6d7f012021-06-17 17:44:50 -0700689STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX);
Jens Axboe945ffb62017-01-14 17:11:11 -0700690#undef STORE_FUNCTION
Bart Van Assched6d7f012021-06-17 17:44:50 -0700691#undef STORE_INT
692#undef STORE_JIFFIES
Jens Axboe945ffb62017-01-14 17:11:11 -0700693
694#define DD_ATTR(name) \
Joe Perches5657a812018-05-24 13:38:59 -0600695 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
Jens Axboe945ffb62017-01-14 17:11:11 -0700696
697static struct elv_fs_entry deadline_attrs[] = {
698 DD_ATTR(read_expire),
699 DD_ATTR(write_expire),
700 DD_ATTR(writes_starved),
701 DD_ATTR(front_merges),
Bart Van Assche07757582021-06-17 17:44:51 -0700702 DD_ATTR(async_depth),
Jens Axboe945ffb62017-01-14 17:11:11 -0700703 DD_ATTR(fifo_batch),
704 __ATTR_NULL
705};
706
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700707#ifdef CONFIG_BLK_DEBUG_FS
708#define DEADLINE_DEBUGFS_DDIR_ATTRS(ddir, name) \
709static void *deadline_##name##_fifo_start(struct seq_file *m, \
710 loff_t *pos) \
711 __acquires(&dd->lock) \
712{ \
713 struct request_queue *q = m->private; \
714 struct deadline_data *dd = q->elevator->elevator_data; \
715 \
716 spin_lock(&dd->lock); \
717 return seq_list_start(&dd->fifo_list[ddir], *pos); \
718} \
719 \
720static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
721 loff_t *pos) \
722{ \
723 struct request_queue *q = m->private; \
724 struct deadline_data *dd = q->elevator->elevator_data; \
725 \
726 return seq_list_next(v, &dd->fifo_list[ddir], pos); \
727} \
728 \
729static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
730 __releases(&dd->lock) \
731{ \
732 struct request_queue *q = m->private; \
733 struct deadline_data *dd = q->elevator->elevator_data; \
734 \
735 spin_unlock(&dd->lock); \
736} \
737 \
738static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
739 .start = deadline_##name##_fifo_start, \
740 .next = deadline_##name##_fifo_next, \
741 .stop = deadline_##name##_fifo_stop, \
742 .show = blk_mq_debugfs_rq_show, \
743}; \
744 \
745static int deadline_##name##_next_rq_show(void *data, \
746 struct seq_file *m) \
747{ \
748 struct request_queue *q = data; \
749 struct deadline_data *dd = q->elevator->elevator_data; \
750 struct request *rq = dd->next_rq[ddir]; \
751 \
752 if (rq) \
753 __blk_mq_debugfs_rq_show(m, rq); \
754 return 0; \
755}
Bart Van Assche004a26b2021-06-17 17:44:49 -0700756DEADLINE_DEBUGFS_DDIR_ATTRS(DD_READ, read)
757DEADLINE_DEBUGFS_DDIR_ATTRS(DD_WRITE, write)
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700758#undef DEADLINE_DEBUGFS_DDIR_ATTRS
759
760static int deadline_batching_show(void *data, struct seq_file *m)
761{
762 struct request_queue *q = data;
763 struct deadline_data *dd = q->elevator->elevator_data;
764
765 seq_printf(m, "%u\n", dd->batching);
766 return 0;
767}
768
769static int deadline_starved_show(void *data, struct seq_file *m)
770{
771 struct request_queue *q = data;
772 struct deadline_data *dd = q->elevator->elevator_data;
773
774 seq_printf(m, "%u\n", dd->starved);
775 return 0;
776}
777
Bart Van Assche07757582021-06-17 17:44:51 -0700778static int dd_async_depth_show(void *data, struct seq_file *m)
779{
780 struct request_queue *q = data;
781 struct deadline_data *dd = q->elevator->elevator_data;
782
783 seq_printf(m, "%u\n", dd->async_depth);
784 return 0;
785}
786
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700787static void *deadline_dispatch_start(struct seq_file *m, loff_t *pos)
788 __acquires(&dd->lock)
789{
790 struct request_queue *q = m->private;
791 struct deadline_data *dd = q->elevator->elevator_data;
792
793 spin_lock(&dd->lock);
794 return seq_list_start(&dd->dispatch, *pos);
795}
796
797static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
798{
799 struct request_queue *q = m->private;
800 struct deadline_data *dd = q->elevator->elevator_data;
801
802 return seq_list_next(v, &dd->dispatch, pos);
803}
804
805static void deadline_dispatch_stop(struct seq_file *m, void *v)
806 __releases(&dd->lock)
807{
808 struct request_queue *q = m->private;
809 struct deadline_data *dd = q->elevator->elevator_data;
810
811 spin_unlock(&dd->lock);
812}
813
814static const struct seq_operations deadline_dispatch_seq_ops = {
815 .start = deadline_dispatch_start,
816 .next = deadline_dispatch_next,
817 .stop = deadline_dispatch_stop,
818 .show = blk_mq_debugfs_rq_show,
819};
820
821#define DEADLINE_QUEUE_DDIR_ATTRS(name) \
822 {#name "_fifo_list", 0400, .seq_ops = &deadline_##name##_fifo_seq_ops}, \
823 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
824static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
825 DEADLINE_QUEUE_DDIR_ATTRS(read),
826 DEADLINE_QUEUE_DDIR_ATTRS(write),
827 {"batching", 0400, deadline_batching_show},
828 {"starved", 0400, deadline_starved_show},
Bart Van Assche07757582021-06-17 17:44:51 -0700829 {"async_depth", 0400, dd_async_depth_show},
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700830 {"dispatch", 0400, .seq_ops = &deadline_dispatch_seq_ops},
831 {},
832};
833#undef DEADLINE_QUEUE_DDIR_ATTRS
834#endif
835
Jens Axboe945ffb62017-01-14 17:11:11 -0700836static struct elevator_type mq_deadline = {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600837 .ops = {
Bart Van Assche07757582021-06-17 17:44:51 -0700838 .depth_updated = dd_depth_updated,
839 .limit_depth = dd_limit_depth,
Jens Axboe945ffb62017-01-14 17:11:11 -0700840 .insert_requests = dd_insert_requests,
Jens Axboec13660a2017-01-26 12:40:07 -0700841 .dispatch_request = dd_dispatch_request,
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800842 .prepare_request = dd_prepare_request,
843 .finish_request = dd_finish_request,
Jens Axboe945ffb62017-01-14 17:11:11 -0700844 .next_request = elv_rb_latter_request,
845 .former_request = elv_rb_former_request,
846 .bio_merge = dd_bio_merge,
847 .request_merge = dd_request_merge,
848 .requests_merged = dd_merged_requests,
849 .request_merged = dd_request_merged,
850 .has_work = dd_has_work,
Bart Van Assche3e9a99e2021-06-17 17:44:48 -0700851 .init_sched = dd_init_sched,
852 .exit_sched = dd_exit_sched,
Bart Van Assche07757582021-06-17 17:44:51 -0700853 .init_hctx = dd_init_hctx,
Jens Axboe945ffb62017-01-14 17:11:11 -0700854 },
855
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700856#ifdef CONFIG_BLK_DEBUG_FS
857 .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
858#endif
Jens Axboe945ffb62017-01-14 17:11:11 -0700859 .elevator_attrs = deadline_attrs,
860 .elevator_name = "mq-deadline",
Jens Axboe4d740bc2017-10-25 09:47:20 -0600861 .elevator_alias = "deadline",
Damien Le Moal68c43f12019-09-05 18:51:31 +0900862 .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE,
Jens Axboe945ffb62017-01-14 17:11:11 -0700863 .elevator_owner = THIS_MODULE,
864};
Ben Hutchings7de967e2017-08-13 18:03:15 +0100865MODULE_ALIAS("mq-deadline-iosched");
Jens Axboe945ffb62017-01-14 17:11:11 -0700866
867static int __init deadline_init(void)
868{
869 return elv_register(&mq_deadline);
870}
871
872static void __exit deadline_exit(void)
873{
874 elv_unregister(&mq_deadline);
875}
876
877module_init(deadline_init);
878module_exit(deadline_exit);
879
880MODULE_AUTHOR("Jens Axboe");
881MODULE_LICENSE("GPL");
882MODULE_DESCRIPTION("MQ deadline IO scheduler");