blob: 69126beff77dbc66c9d5890da8d2067652cc102f [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;
70
71 spinlock_t lock;
Damien Le Moal5700f692017-12-21 15:43:40 +090072 spinlock_t zone_lock;
Jens Axboe945ffb62017-01-14 17:11:11 -070073 struct list_head dispatch;
74};
75
76static inline struct rb_root *
77deadline_rb_root(struct deadline_data *dd, struct request *rq)
78{
79 return &dd->sort_list[rq_data_dir(rq)];
80}
81
82/*
83 * get the request after `rq' in sector-sorted order
84 */
85static inline struct request *
86deadline_latter_request(struct request *rq)
87{
88 struct rb_node *node = rb_next(&rq->rb_node);
89
90 if (node)
91 return rb_entry_rq(node);
92
93 return NULL;
94}
95
96static void
97deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
98{
99 struct rb_root *root = deadline_rb_root(dd, rq);
100
101 elv_rb_add(root, rq);
102}
103
104static inline void
105deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)
106{
Bart Van Assche004a26b2021-06-17 17:44:49 -0700107 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700108
109 if (dd->next_rq[data_dir] == rq)
110 dd->next_rq[data_dir] = deadline_latter_request(rq);
111
112 elv_rb_del(deadline_rb_root(dd, rq), rq);
113}
114
115/*
116 * remove rq from rbtree and fifo.
117 */
118static void deadline_remove_request(struct request_queue *q, struct request *rq)
119{
120 struct deadline_data *dd = q->elevator->elevator_data;
121
122 list_del_init(&rq->queuelist);
123
124 /*
125 * We might not be on the rbtree, if we are doing an insert merge
126 */
127 if (!RB_EMPTY_NODE(&rq->rb_node))
128 deadline_del_rq_rb(dd, rq);
129
130 elv_rqhash_del(q, rq);
131 if (q->last_merge == rq)
132 q->last_merge = NULL;
133}
134
135static void dd_request_merged(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100136 enum elv_merge type)
Jens Axboe945ffb62017-01-14 17:11:11 -0700137{
138 struct deadline_data *dd = q->elevator->elevator_data;
139
140 /*
141 * if the merge was a front merge, we need to reposition request
142 */
143 if (type == ELEVATOR_FRONT_MERGE) {
144 elv_rb_del(deadline_rb_root(dd, req), req);
145 deadline_add_rq_rb(dd, req);
146 }
147}
148
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700149/*
150 * Callback function that is invoked after @next has been merged into @req.
151 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700152static void dd_merged_requests(struct request_queue *q, struct request *req,
153 struct request *next)
154{
155 /*
156 * if next expires before rq, assign its expire time to rq
157 * and move into next position (next will be deleted) in fifo
158 */
159 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
160 if (time_before((unsigned long)next->fifo_time,
161 (unsigned long)req->fifo_time)) {
162 list_move(&req->queuelist, &next->queuelist);
163 req->fifo_time = next->fifo_time;
164 }
165 }
166
167 /*
168 * kill knowledge of next, this one is a goner
169 */
170 deadline_remove_request(q, next);
171}
172
173/*
174 * move an entry to dispatch queue
175 */
176static void
177deadline_move_request(struct deadline_data *dd, struct request *rq)
178{
Bart Van Assche004a26b2021-06-17 17:44:49 -0700179 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700180
Bart Van Assche004a26b2021-06-17 17:44:49 -0700181 dd->next_rq[DD_READ] = NULL;
182 dd->next_rq[DD_WRITE] = NULL;
Jens Axboe945ffb62017-01-14 17:11:11 -0700183 dd->next_rq[data_dir] = deadline_latter_request(rq);
184
185 /*
186 * take it off the sort and fifo list
187 */
188 deadline_remove_request(rq->q, rq);
189}
190
191/*
192 * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
193 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
194 */
Bart Van Assche004a26b2021-06-17 17:44:49 -0700195static inline int deadline_check_fifo(struct deadline_data *dd,
196 enum dd_data_dir data_dir)
Jens Axboe945ffb62017-01-14 17:11:11 -0700197{
Bart Van Assche004a26b2021-06-17 17:44:49 -0700198 struct request *rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
Jens Axboe945ffb62017-01-14 17:11:11 -0700199
200 /*
201 * rq is expired!
202 */
203 if (time_after_eq(jiffies, (unsigned long)rq->fifo_time))
204 return 1;
205
206 return 0;
207}
208
209/*
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900210 * For the specified data direction, return the next request to
211 * dispatch using arrival ordered lists.
212 */
213static struct request *
Bart Van Assche004a26b2021-06-17 17:44:49 -0700214deadline_fifo_request(struct deadline_data *dd, enum dd_data_dir data_dir)
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900215{
Damien Le Moal5700f692017-12-21 15:43:40 +0900216 struct request *rq;
217 unsigned long flags;
218
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900219 if (list_empty(&dd->fifo_list[data_dir]))
220 return NULL;
221
Damien Le Moal5700f692017-12-21 15:43:40 +0900222 rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
Bart Van Assche004a26b2021-06-17 17:44:49 -0700223 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
Damien Le Moal5700f692017-12-21 15:43:40 +0900224 return rq;
225
226 /*
227 * Look for a write request that can be dispatched, that is one with
228 * an unlocked target zone.
229 */
230 spin_lock_irqsave(&dd->zone_lock, flags);
Bart Van Assche004a26b2021-06-17 17:44:49 -0700231 list_for_each_entry(rq, &dd->fifo_list[DD_WRITE], queuelist) {
Damien Le Moal5700f692017-12-21 15:43:40 +0900232 if (blk_req_can_dispatch_to_zone(rq))
233 goto out;
234 }
235 rq = NULL;
236out:
237 spin_unlock_irqrestore(&dd->zone_lock, flags);
238
239 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900240}
241
242/*
243 * For the specified data direction, return the next request to
244 * dispatch using sector position sorted lists.
245 */
246static struct request *
Bart Van Assche004a26b2021-06-17 17:44:49 -0700247deadline_next_request(struct deadline_data *dd, enum dd_data_dir data_dir)
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900248{
Damien Le Moal5700f692017-12-21 15:43:40 +0900249 struct request *rq;
250 unsigned long flags;
251
Damien Le Moal5700f692017-12-21 15:43:40 +0900252 rq = dd->next_rq[data_dir];
253 if (!rq)
254 return NULL;
255
Bart Van Assche004a26b2021-06-17 17:44:49 -0700256 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
Damien Le Moal5700f692017-12-21 15:43:40 +0900257 return rq;
258
259 /*
260 * Look for a write request that can be dispatched, that is one with
261 * an unlocked target zone.
262 */
263 spin_lock_irqsave(&dd->zone_lock, flags);
264 while (rq) {
265 if (blk_req_can_dispatch_to_zone(rq))
266 break;
267 rq = deadline_latter_request(rq);
268 }
269 spin_unlock_irqrestore(&dd->zone_lock, flags);
270
271 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900272}
273
274/*
Jens Axboe945ffb62017-01-14 17:11:11 -0700275 * deadline_dispatch_requests selects the best request according to
276 * read/write expire, fifo_batch, etc
277 */
Jens Axboeca11f202018-01-06 09:23:11 -0700278static struct request *__dd_dispatch_request(struct deadline_data *dd)
Jens Axboe945ffb62017-01-14 17:11:11 -0700279{
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900280 struct request *rq, *next_rq;
Bart Van Assche004a26b2021-06-17 17:44:49 -0700281 enum dd_data_dir data_dir;
Jens Axboe945ffb62017-01-14 17:11:11 -0700282
Bart Van Assche3bd473f2021-06-17 17:44:46 -0700283 lockdep_assert_held(&dd->lock);
284
Jens Axboe945ffb62017-01-14 17:11:11 -0700285 if (!list_empty(&dd->dispatch)) {
286 rq = list_first_entry(&dd->dispatch, struct request, queuelist);
287 list_del_init(&rq->queuelist);
288 goto done;
289 }
290
Jens Axboe945ffb62017-01-14 17:11:11 -0700291 /*
292 * batches are currently reads XOR writes
293 */
Bart Van Assche004a26b2021-06-17 17:44:49 -0700294 rq = deadline_next_request(dd, DD_WRITE);
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900295 if (!rq)
Bart Van Assche004a26b2021-06-17 17:44:49 -0700296 rq = deadline_next_request(dd, DD_READ);
Jens Axboe945ffb62017-01-14 17:11:11 -0700297
298 if (rq && dd->batching < dd->fifo_batch)
299 /* we have a next request are still entitled to batch */
300 goto dispatch_request;
301
302 /*
303 * at this point we are not running a batch. select the appropriate
304 * data direction (read / write)
305 */
306
Bart Van Assche004a26b2021-06-17 17:44:49 -0700307 if (!list_empty(&dd->fifo_list[DD_READ])) {
308 BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[DD_READ]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700309
Bart Van Assche004a26b2021-06-17 17:44:49 -0700310 if (deadline_fifo_request(dd, DD_WRITE) &&
Damien Le Moal5700f692017-12-21 15:43:40 +0900311 (dd->starved++ >= dd->writes_starved))
Jens Axboe945ffb62017-01-14 17:11:11 -0700312 goto dispatch_writes;
313
Bart Van Assche004a26b2021-06-17 17:44:49 -0700314 data_dir = DD_READ;
Jens Axboe945ffb62017-01-14 17:11:11 -0700315
316 goto dispatch_find_request;
317 }
318
319 /*
320 * there are either no reads or writes have been starved
321 */
322
Bart Van Assche004a26b2021-06-17 17:44:49 -0700323 if (!list_empty(&dd->fifo_list[DD_WRITE])) {
Jens Axboe945ffb62017-01-14 17:11:11 -0700324dispatch_writes:
Bart Van Assche004a26b2021-06-17 17:44:49 -0700325 BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[DD_WRITE]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700326
327 dd->starved = 0;
328
Bart Van Assche004a26b2021-06-17 17:44:49 -0700329 data_dir = DD_WRITE;
Jens Axboe945ffb62017-01-14 17:11:11 -0700330
331 goto dispatch_find_request;
332 }
333
334 return NULL;
335
336dispatch_find_request:
337 /*
338 * we are not running a batch, find best request for selected data_dir
339 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900340 next_rq = deadline_next_request(dd, data_dir);
341 if (deadline_check_fifo(dd, data_dir) || !next_rq) {
Jens Axboe945ffb62017-01-14 17:11:11 -0700342 /*
343 * A deadline has expired, the last request was in the other
344 * direction, or we have run out of higher-sectored requests.
345 * Start again from the request with the earliest expiry time.
346 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900347 rq = deadline_fifo_request(dd, data_dir);
Jens Axboe945ffb62017-01-14 17:11:11 -0700348 } else {
349 /*
350 * The last req was the same dir and we have a next request in
351 * sort order. No expired requests so continue on from here.
352 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900353 rq = next_rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700354 }
355
Damien Le Moal5700f692017-12-21 15:43:40 +0900356 /*
357 * For a zoned block device, if we only have writes queued and none of
358 * them can be dispatched, rq will be NULL.
359 */
360 if (!rq)
361 return NULL;
362
Jens Axboe945ffb62017-01-14 17:11:11 -0700363 dd->batching = 0;
364
365dispatch_request:
366 /*
367 * rq is the selected appropriate request.
368 */
369 dd->batching++;
370 deadline_move_request(dd, rq);
371done:
Damien Le Moal5700f692017-12-21 15:43:40 +0900372 /*
373 * If the request needs its target zone locked, do it.
374 */
375 blk_req_zone_write_lock(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700376 rq->rq_flags |= RQF_STARTED;
377 return rq;
378}
379
Jens Axboeca11f202018-01-06 09:23:11 -0700380/*
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700381 * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests().
382 *
Jens Axboeca11f202018-01-06 09:23:11 -0700383 * One confusing aspect here is that we get called for a specific
Damien Le Moal7211aef82018-12-17 15:14:05 +0900384 * hardware queue, but we may return a request that is for a
Jens Axboeca11f202018-01-06 09:23:11 -0700385 * different hardware queue. This is because mq-deadline has shared
386 * state for all hardware queues, in terms of sorting, FIFOs, etc.
387 */
Jens Axboec13660a2017-01-26 12:40:07 -0700388static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
Jens Axboe945ffb62017-01-14 17:11:11 -0700389{
390 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
Jens Axboec13660a2017-01-26 12:40:07 -0700391 struct request *rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700392
393 spin_lock(&dd->lock);
Jens Axboeca11f202018-01-06 09:23:11 -0700394 rq = __dd_dispatch_request(dd);
Jens Axboe945ffb62017-01-14 17:11:11 -0700395 spin_unlock(&dd->lock);
Jens Axboec13660a2017-01-26 12:40:07 -0700396
397 return rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700398}
399
Bart Van Assche3e9a99e2021-06-17 17:44:48 -0700400static void dd_exit_sched(struct elevator_queue *e)
Jens Axboe945ffb62017-01-14 17:11:11 -0700401{
402 struct deadline_data *dd = e->elevator_data;
403
Bart Van Assche004a26b2021-06-17 17:44:49 -0700404 BUG_ON(!list_empty(&dd->fifo_list[DD_READ]));
405 BUG_ON(!list_empty(&dd->fifo_list[DD_WRITE]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700406
407 kfree(dd);
408}
409
410/*
411 * initialize elevator private data (deadline_data).
412 */
Bart Van Assche3e9a99e2021-06-17 17:44:48 -0700413static int dd_init_sched(struct request_queue *q, struct elevator_type *e)
Jens Axboe945ffb62017-01-14 17:11:11 -0700414{
415 struct deadline_data *dd;
416 struct elevator_queue *eq;
417
418 eq = elevator_alloc(q, e);
419 if (!eq)
420 return -ENOMEM;
421
422 dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
423 if (!dd) {
424 kobject_put(&eq->kobj);
425 return -ENOMEM;
426 }
427 eq->elevator_data = dd;
428
Bart Van Assche004a26b2021-06-17 17:44:49 -0700429 INIT_LIST_HEAD(&dd->fifo_list[DD_READ]);
430 INIT_LIST_HEAD(&dd->fifo_list[DD_WRITE]);
431 dd->sort_list[DD_READ] = RB_ROOT;
432 dd->sort_list[DD_WRITE] = RB_ROOT;
433 dd->fifo_expire[DD_READ] = read_expire;
434 dd->fifo_expire[DD_WRITE] = write_expire;
Jens Axboe945ffb62017-01-14 17:11:11 -0700435 dd->writes_starved = writes_starved;
436 dd->front_merges = 1;
437 dd->fifo_batch = fifo_batch;
438 spin_lock_init(&dd->lock);
Damien Le Moal5700f692017-12-21 15:43:40 +0900439 spin_lock_init(&dd->zone_lock);
Jens Axboe945ffb62017-01-14 17:11:11 -0700440 INIT_LIST_HEAD(&dd->dispatch);
441
442 q->elevator = eq;
443 return 0;
444}
445
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700446/*
447 * Try to merge @bio into an existing request. If @bio has been merged into
448 * an existing request, store the pointer to that request into *@rq.
449 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700450static int dd_request_merge(struct request_queue *q, struct request **rq,
451 struct bio *bio)
452{
453 struct deadline_data *dd = q->elevator->elevator_data;
454 sector_t sector = bio_end_sector(bio);
455 struct request *__rq;
456
457 if (!dd->front_merges)
458 return ELEVATOR_NO_MERGE;
459
460 __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);
461 if (__rq) {
462 BUG_ON(sector != blk_rq_pos(__rq));
463
464 if (elv_bio_merge_ok(__rq, bio)) {
465 *rq = __rq;
466 return ELEVATOR_FRONT_MERGE;
467 }
468 }
469
470 return ELEVATOR_NO_MERGE;
471}
472
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700473/*
474 * Attempt to merge a bio into an existing request. This function is called
475 * before @bio is associated with a request.
476 */
Omar Sandovalefed9a32021-05-10 17:05:35 -0700477static bool dd_bio_merge(struct request_queue *q, struct bio *bio,
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200478 unsigned int nr_segs)
Jens Axboe945ffb62017-01-14 17:11:11 -0700479{
Jens Axboe945ffb62017-01-14 17:11:11 -0700480 struct deadline_data *dd = q->elevator->elevator_data;
Jens Axboee4d750c2017-02-03 09:48:28 -0700481 struct request *free = NULL;
482 bool ret;
Jens Axboe945ffb62017-01-14 17:11:11 -0700483
484 spin_lock(&dd->lock);
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200485 ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
Jens Axboe945ffb62017-01-14 17:11:11 -0700486 spin_unlock(&dd->lock);
487
Jens Axboee4d750c2017-02-03 09:48:28 -0700488 if (free)
489 blk_mq_free_request(free);
490
Jens Axboe945ffb62017-01-14 17:11:11 -0700491 return ret;
492}
493
494/*
495 * add rq to rbtree and fifo
496 */
497static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
498 bool at_head)
499{
500 struct request_queue *q = hctx->queue;
501 struct deadline_data *dd = q->elevator->elevator_data;
Bart Van Assche004a26b2021-06-17 17:44:49 -0700502 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700503
Bart Van Assche3bd473f2021-06-17 17:44:46 -0700504 lockdep_assert_held(&dd->lock);
505
Damien Le Moal5700f692017-12-21 15:43:40 +0900506 /*
507 * This may be a requeue of a write request that has locked its
508 * target zone. If it is the case, this releases the zone lock.
509 */
510 blk_req_zone_write_unlock(rq);
511
Jens Axboe945ffb62017-01-14 17:11:11 -0700512 if (blk_mq_sched_try_insert_merge(q, rq))
513 return;
514
Chaitanya Kulkarnib357e4a2021-02-21 21:29:59 -0800515 trace_block_rq_insert(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700516
Lin Feng7687b382021-04-15 11:43:26 +0800517 if (at_head) {
518 list_add(&rq->queuelist, &dd->dispatch);
Jens Axboe945ffb62017-01-14 17:11:11 -0700519 } else {
520 deadline_add_rq_rb(dd, rq);
521
522 if (rq_mergeable(rq)) {
523 elv_rqhash_add(q, rq);
524 if (!q->last_merge)
525 q->last_merge = rq;
526 }
527
528 /*
529 * set expire time and add to fifo list
530 */
531 rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
532 list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);
533 }
534}
535
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700536/*
537 * Called from blk_mq_sched_insert_request() or blk_mq_sched_insert_requests().
538 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700539static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
540 struct list_head *list, bool at_head)
541{
542 struct request_queue *q = hctx->queue;
543 struct deadline_data *dd = q->elevator->elevator_data;
544
545 spin_lock(&dd->lock);
546 while (!list_empty(list)) {
547 struct request *rq;
548
549 rq = list_first_entry(list, struct request, queuelist);
550 list_del_init(&rq->queuelist);
551 dd_insert_request(hctx, rq, at_head);
552 }
553 spin_unlock(&dd->lock);
554}
555
Damien Le Moal5700f692017-12-21 15:43:40 +0900556/*
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800557 * Nothing to do here. This is defined only to ensure that .finish_request
558 * method is called upon request completion.
559 */
Christoph Hellwig5d9c3052020-05-29 15:53:08 +0200560static void dd_prepare_request(struct request *rq)
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800561{
562}
563
564/*
Bart Van Assche46eae2e2021-06-17 17:44:45 -0700565 * Callback from inside blk_mq_free_request().
566 *
Damien Le Moal5700f692017-12-21 15:43:40 +0900567 * For zoned block devices, write unlock the target zone of
568 * completed write requests. Do this while holding the zone lock
569 * spinlock so that the zone is never unlocked while deadline_fifo_request()
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800570 * or deadline_next_request() are executing. This function is called for
571 * all requests, whether or not these requests complete successfully.
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900572 *
573 * For a zoned block device, __dd_dispatch_request() may have stopped
574 * dispatching requests if all the queued requests are write requests directed
575 * at zones that are already locked due to on-going write requests. To ensure
576 * write request dispatch progress in this case, mark the queue as needing a
577 * restart to ensure that the queue is run again after completion of the
578 * request and zones being unlocked.
Damien Le Moal5700f692017-12-21 15:43:40 +0900579 */
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800580static void dd_finish_request(struct request *rq)
Damien Le Moal5700f692017-12-21 15:43:40 +0900581{
582 struct request_queue *q = rq->q;
583
584 if (blk_queue_is_zoned(q)) {
585 struct deadline_data *dd = q->elevator->elevator_data;
586 unsigned long flags;
587
588 spin_lock_irqsave(&dd->zone_lock, flags);
589 blk_req_zone_write_unlock(rq);
Bart Van Assche004a26b2021-06-17 17:44:49 -0700590 if (!list_empty(&dd->fifo_list[DD_WRITE]))
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900591 blk_mq_sched_mark_restart_hctx(rq->mq_hctx);
Damien Le Moal5700f692017-12-21 15:43:40 +0900592 spin_unlock_irqrestore(&dd->zone_lock, flags);
593 }
594}
595
Jens Axboe945ffb62017-01-14 17:11:11 -0700596static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
597{
598 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
599
600 return !list_empty_careful(&dd->dispatch) ||
601 !list_empty_careful(&dd->fifo_list[0]) ||
602 !list_empty_careful(&dd->fifo_list[1]);
603}
604
605/*
606 * sysfs parts below
607 */
608static ssize_t
609deadline_var_show(int var, char *page)
610{
611 return sprintf(page, "%d\n", var);
612}
613
weiping zhang235f8da2017-08-25 01:11:33 +0800614static void
615deadline_var_store(int *var, const char *page)
Jens Axboe945ffb62017-01-14 17:11:11 -0700616{
617 char *p = (char *) page;
618
619 *var = simple_strtol(p, &p, 10);
Jens Axboe945ffb62017-01-14 17:11:11 -0700620}
621
622#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
623static ssize_t __FUNC(struct elevator_queue *e, char *page) \
624{ \
625 struct deadline_data *dd = e->elevator_data; \
626 int __data = __VAR; \
627 if (__CONV) \
628 __data = jiffies_to_msecs(__data); \
629 return deadline_var_show(__data, (page)); \
630}
Bart Van Assche004a26b2021-06-17 17:44:49 -0700631SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[DD_READ], 1);
632SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[DD_WRITE], 1);
Jens Axboe945ffb62017-01-14 17:11:11 -0700633SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0);
634SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0);
635SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0);
636#undef SHOW_FUNCTION
637
638#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
639static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
640{ \
641 struct deadline_data *dd = e->elevator_data; \
642 int __data; \
weiping zhang235f8da2017-08-25 01:11:33 +0800643 deadline_var_store(&__data, (page)); \
Jens Axboe945ffb62017-01-14 17:11:11 -0700644 if (__data < (MIN)) \
645 __data = (MIN); \
646 else if (__data > (MAX)) \
647 __data = (MAX); \
648 if (__CONV) \
649 *(__PTR) = msecs_to_jiffies(__data); \
650 else \
651 *(__PTR) = __data; \
weiping zhang235f8da2017-08-25 01:11:33 +0800652 return count; \
Jens Axboe945ffb62017-01-14 17:11:11 -0700653}
Bart Van Assche004a26b2021-06-17 17:44:49 -0700654STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX, 1);
655STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX, 1);
Jens Axboe945ffb62017-01-14 17:11:11 -0700656STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
657STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0);
658STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0);
659#undef STORE_FUNCTION
660
661#define DD_ATTR(name) \
Joe Perches5657a812018-05-24 13:38:59 -0600662 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
Jens Axboe945ffb62017-01-14 17:11:11 -0700663
664static struct elv_fs_entry deadline_attrs[] = {
665 DD_ATTR(read_expire),
666 DD_ATTR(write_expire),
667 DD_ATTR(writes_starved),
668 DD_ATTR(front_merges),
669 DD_ATTR(fifo_batch),
670 __ATTR_NULL
671};
672
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700673#ifdef CONFIG_BLK_DEBUG_FS
674#define DEADLINE_DEBUGFS_DDIR_ATTRS(ddir, name) \
675static void *deadline_##name##_fifo_start(struct seq_file *m, \
676 loff_t *pos) \
677 __acquires(&dd->lock) \
678{ \
679 struct request_queue *q = m->private; \
680 struct deadline_data *dd = q->elevator->elevator_data; \
681 \
682 spin_lock(&dd->lock); \
683 return seq_list_start(&dd->fifo_list[ddir], *pos); \
684} \
685 \
686static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
687 loff_t *pos) \
688{ \
689 struct request_queue *q = m->private; \
690 struct deadline_data *dd = q->elevator->elevator_data; \
691 \
692 return seq_list_next(v, &dd->fifo_list[ddir], pos); \
693} \
694 \
695static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
696 __releases(&dd->lock) \
697{ \
698 struct request_queue *q = m->private; \
699 struct deadline_data *dd = q->elevator->elevator_data; \
700 \
701 spin_unlock(&dd->lock); \
702} \
703 \
704static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
705 .start = deadline_##name##_fifo_start, \
706 .next = deadline_##name##_fifo_next, \
707 .stop = deadline_##name##_fifo_stop, \
708 .show = blk_mq_debugfs_rq_show, \
709}; \
710 \
711static int deadline_##name##_next_rq_show(void *data, \
712 struct seq_file *m) \
713{ \
714 struct request_queue *q = data; \
715 struct deadline_data *dd = q->elevator->elevator_data; \
716 struct request *rq = dd->next_rq[ddir]; \
717 \
718 if (rq) \
719 __blk_mq_debugfs_rq_show(m, rq); \
720 return 0; \
721}
Bart Van Assche004a26b2021-06-17 17:44:49 -0700722DEADLINE_DEBUGFS_DDIR_ATTRS(DD_READ, read)
723DEADLINE_DEBUGFS_DDIR_ATTRS(DD_WRITE, write)
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700724#undef DEADLINE_DEBUGFS_DDIR_ATTRS
725
726static int deadline_batching_show(void *data, struct seq_file *m)
727{
728 struct request_queue *q = data;
729 struct deadline_data *dd = q->elevator->elevator_data;
730
731 seq_printf(m, "%u\n", dd->batching);
732 return 0;
733}
734
735static int deadline_starved_show(void *data, struct seq_file *m)
736{
737 struct request_queue *q = data;
738 struct deadline_data *dd = q->elevator->elevator_data;
739
740 seq_printf(m, "%u\n", dd->starved);
741 return 0;
742}
743
744static void *deadline_dispatch_start(struct seq_file *m, loff_t *pos)
745 __acquires(&dd->lock)
746{
747 struct request_queue *q = m->private;
748 struct deadline_data *dd = q->elevator->elevator_data;
749
750 spin_lock(&dd->lock);
751 return seq_list_start(&dd->dispatch, *pos);
752}
753
754static void *deadline_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
755{
756 struct request_queue *q = m->private;
757 struct deadline_data *dd = q->elevator->elevator_data;
758
759 return seq_list_next(v, &dd->dispatch, pos);
760}
761
762static void deadline_dispatch_stop(struct seq_file *m, void *v)
763 __releases(&dd->lock)
764{
765 struct request_queue *q = m->private;
766 struct deadline_data *dd = q->elevator->elevator_data;
767
768 spin_unlock(&dd->lock);
769}
770
771static const struct seq_operations deadline_dispatch_seq_ops = {
772 .start = deadline_dispatch_start,
773 .next = deadline_dispatch_next,
774 .stop = deadline_dispatch_stop,
775 .show = blk_mq_debugfs_rq_show,
776};
777
778#define DEADLINE_QUEUE_DDIR_ATTRS(name) \
779 {#name "_fifo_list", 0400, .seq_ops = &deadline_##name##_fifo_seq_ops}, \
780 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
781static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
782 DEADLINE_QUEUE_DDIR_ATTRS(read),
783 DEADLINE_QUEUE_DDIR_ATTRS(write),
784 {"batching", 0400, deadline_batching_show},
785 {"starved", 0400, deadline_starved_show},
786 {"dispatch", 0400, .seq_ops = &deadline_dispatch_seq_ops},
787 {},
788};
789#undef DEADLINE_QUEUE_DDIR_ATTRS
790#endif
791
Jens Axboe945ffb62017-01-14 17:11:11 -0700792static struct elevator_type mq_deadline = {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600793 .ops = {
Jens Axboe945ffb62017-01-14 17:11:11 -0700794 .insert_requests = dd_insert_requests,
Jens Axboec13660a2017-01-26 12:40:07 -0700795 .dispatch_request = dd_dispatch_request,
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800796 .prepare_request = dd_prepare_request,
797 .finish_request = dd_finish_request,
Jens Axboe945ffb62017-01-14 17:11:11 -0700798 .next_request = elv_rb_latter_request,
799 .former_request = elv_rb_former_request,
800 .bio_merge = dd_bio_merge,
801 .request_merge = dd_request_merge,
802 .requests_merged = dd_merged_requests,
803 .request_merged = dd_request_merged,
804 .has_work = dd_has_work,
Bart Van Assche3e9a99e2021-06-17 17:44:48 -0700805 .init_sched = dd_init_sched,
806 .exit_sched = dd_exit_sched,
Jens Axboe945ffb62017-01-14 17:11:11 -0700807 },
808
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700809#ifdef CONFIG_BLK_DEBUG_FS
810 .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
811#endif
Jens Axboe945ffb62017-01-14 17:11:11 -0700812 .elevator_attrs = deadline_attrs,
813 .elevator_name = "mq-deadline",
Jens Axboe4d740bc2017-10-25 09:47:20 -0600814 .elevator_alias = "deadline",
Damien Le Moal68c43f12019-09-05 18:51:31 +0900815 .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE,
Jens Axboe945ffb62017-01-14 17:11:11 -0700816 .elevator_owner = THIS_MODULE,
817};
Ben Hutchings7de967e2017-08-13 18:03:15 +0100818MODULE_ALIAS("mq-deadline-iosched");
Jens Axboe945ffb62017-01-14 17:11:11 -0700819
820static int __init deadline_init(void)
821{
822 return elv_register(&mq_deadline);
823}
824
825static void __exit deadline_exit(void)
826{
827 elv_unregister(&mq_deadline);
828}
829
830module_init(deadline_init);
831module_exit(deadline_exit);
832
833MODULE_AUTHOR("Jens Axboe");
834MODULE_LICENSE("GPL");
835MODULE_DESCRIPTION("MQ deadline IO scheduler");