blob: a63b8303c0bf35905588bc5fec1d9f0db63ae13e [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
21#include "blk.h"
22#include "blk-mq.h"
Omar Sandovaldaaadb32017-05-04 00:31:34 -070023#include "blk-mq-debugfs.h"
Jens Axboe945ffb62017-01-14 17:11:11 -070024#include "blk-mq-tag.h"
25#include "blk-mq-sched.h"
26
27/*
Mauro Carvalho Chehab898bd372019-04-18 19:45:00 -030028 * See Documentation/block/deadline-iosched.rst
Jens Axboe945ffb62017-01-14 17:11:11 -070029 */
30static const int read_expire = HZ / 2; /* max time before a read is submitted. */
31static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
32static const int writes_starved = 2; /* max times reads can starve a write */
33static const int fifo_batch = 16; /* # of sequential requests treated as one
34 by the above parameters. For throughput. */
35
36struct deadline_data {
37 /*
38 * run time data
39 */
40
41 /*
42 * requests (deadline_rq s) are present on both sort_list and fifo_list
43 */
44 struct rb_root sort_list[2];
45 struct list_head fifo_list[2];
46
47 /*
48 * next in sort order. read, write or both are NULL
49 */
50 struct request *next_rq[2];
51 unsigned int batching; /* number of sequential requests made */
52 unsigned int starved; /* times reads have starved writes */
53
54 /*
55 * settings that change how the i/o scheduler behaves
56 */
57 int fifo_expire[2];
58 int fifo_batch;
59 int writes_starved;
60 int front_merges;
61
62 spinlock_t lock;
Damien Le Moal5700f692017-12-21 15:43:40 +090063 spinlock_t zone_lock;
Jens Axboe945ffb62017-01-14 17:11:11 -070064 struct list_head dispatch;
65};
66
67static inline struct rb_root *
68deadline_rb_root(struct deadline_data *dd, struct request *rq)
69{
70 return &dd->sort_list[rq_data_dir(rq)];
71}
72
73/*
74 * get the request after `rq' in sector-sorted order
75 */
76static inline struct request *
77deadline_latter_request(struct request *rq)
78{
79 struct rb_node *node = rb_next(&rq->rb_node);
80
81 if (node)
82 return rb_entry_rq(node);
83
84 return NULL;
85}
86
87static void
88deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
89{
90 struct rb_root *root = deadline_rb_root(dd, rq);
91
92 elv_rb_add(root, rq);
93}
94
95static inline void
96deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)
97{
98 const int data_dir = rq_data_dir(rq);
99
100 if (dd->next_rq[data_dir] == rq)
101 dd->next_rq[data_dir] = deadline_latter_request(rq);
102
103 elv_rb_del(deadline_rb_root(dd, rq), rq);
104}
105
106/*
107 * remove rq from rbtree and fifo.
108 */
109static void deadline_remove_request(struct request_queue *q, struct request *rq)
110{
111 struct deadline_data *dd = q->elevator->elevator_data;
112
113 list_del_init(&rq->queuelist);
114
115 /*
116 * We might not be on the rbtree, if we are doing an insert merge
117 */
118 if (!RB_EMPTY_NODE(&rq->rb_node))
119 deadline_del_rq_rb(dd, rq);
120
121 elv_rqhash_del(q, rq);
122 if (q->last_merge == rq)
123 q->last_merge = NULL;
124}
125
126static void dd_request_merged(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100127 enum elv_merge type)
Jens Axboe945ffb62017-01-14 17:11:11 -0700128{
129 struct deadline_data *dd = q->elevator->elevator_data;
130
131 /*
132 * if the merge was a front merge, we need to reposition request
133 */
134 if (type == ELEVATOR_FRONT_MERGE) {
135 elv_rb_del(deadline_rb_root(dd, req), req);
136 deadline_add_rq_rb(dd, req);
137 }
138}
139
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700140/*
141 * Callback function that is invoked after @next has been merged into @req.
142 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700143static void dd_merged_requests(struct request_queue *q, struct request *req,
144 struct request *next)
145{
146 /*
147 * if next expires before rq, assign its expire time to rq
148 * and move into next position (next will be deleted) in fifo
149 */
150 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
151 if (time_before((unsigned long)next->fifo_time,
152 (unsigned long)req->fifo_time)) {
153 list_move(&req->queuelist, &next->queuelist);
154 req->fifo_time = next->fifo_time;
155 }
156 }
157
158 /*
159 * kill knowledge of next, this one is a goner
160 */
161 deadline_remove_request(q, next);
162}
163
164/*
165 * move an entry to dispatch queue
166 */
167static void
168deadline_move_request(struct deadline_data *dd, struct request *rq)
169{
170 const int data_dir = rq_data_dir(rq);
171
172 dd->next_rq[READ] = NULL;
173 dd->next_rq[WRITE] = NULL;
174 dd->next_rq[data_dir] = deadline_latter_request(rq);
175
176 /*
177 * take it off the sort and fifo list
178 */
179 deadline_remove_request(rq->q, rq);
180}
181
182/*
183 * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
184 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
185 */
186static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
187{
188 struct request *rq = rq_entry_fifo(dd->fifo_list[ddir].next);
189
190 /*
191 * rq is expired!
192 */
193 if (time_after_eq(jiffies, (unsigned long)rq->fifo_time))
194 return 1;
195
196 return 0;
197}
198
199/*
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900200 * For the specified data direction, return the next request to
201 * dispatch using arrival ordered lists.
202 */
203static struct request *
204deadline_fifo_request(struct deadline_data *dd, int data_dir)
205{
Damien Le Moal5700f692017-12-21 15:43:40 +0900206 struct request *rq;
207 unsigned long flags;
208
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900209 if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
210 return NULL;
211
212 if (list_empty(&dd->fifo_list[data_dir]))
213 return NULL;
214
Damien Le Moal5700f692017-12-21 15:43:40 +0900215 rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
216 if (data_dir == READ || !blk_queue_is_zoned(rq->q))
217 return rq;
218
219 /*
220 * Look for a write request that can be dispatched, that is one with
221 * an unlocked target zone.
222 */
223 spin_lock_irqsave(&dd->zone_lock, flags);
224 list_for_each_entry(rq, &dd->fifo_list[WRITE], queuelist) {
225 if (blk_req_can_dispatch_to_zone(rq))
226 goto out;
227 }
228 rq = NULL;
229out:
230 spin_unlock_irqrestore(&dd->zone_lock, flags);
231
232 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900233}
234
235/*
236 * For the specified data direction, return the next request to
237 * dispatch using sector position sorted lists.
238 */
239static struct request *
240deadline_next_request(struct deadline_data *dd, int data_dir)
241{
Damien Le Moal5700f692017-12-21 15:43:40 +0900242 struct request *rq;
243 unsigned long flags;
244
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900245 if (WARN_ON_ONCE(data_dir != READ && data_dir != WRITE))
246 return NULL;
247
Damien Le Moal5700f692017-12-21 15:43:40 +0900248 rq = dd->next_rq[data_dir];
249 if (!rq)
250 return NULL;
251
252 if (data_dir == READ || !blk_queue_is_zoned(rq->q))
253 return rq;
254
255 /*
256 * Look for a write request that can be dispatched, that is one with
257 * an unlocked target zone.
258 */
259 spin_lock_irqsave(&dd->zone_lock, flags);
260 while (rq) {
261 if (blk_req_can_dispatch_to_zone(rq))
262 break;
263 rq = deadline_latter_request(rq);
264 }
265 spin_unlock_irqrestore(&dd->zone_lock, flags);
266
267 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900268}
269
270/*
Jens Axboe945ffb62017-01-14 17:11:11 -0700271 * deadline_dispatch_requests selects the best request according to
272 * read/write expire, fifo_batch, etc
273 */
Jens Axboeca11f202018-01-06 09:23:11 -0700274static struct request *__dd_dispatch_request(struct deadline_data *dd)
Jens Axboe945ffb62017-01-14 17:11:11 -0700275{
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900276 struct request *rq, *next_rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700277 bool reads, writes;
278 int data_dir;
279
Bart Van Assched0d50fe2021-05-19 16:16:06 -0700280 lockdep_assert_held(&dd->lock);
281
Jens Axboe945ffb62017-01-14 17:11:11 -0700282 if (!list_empty(&dd->dispatch)) {
283 rq = list_first_entry(&dd->dispatch, struct request, queuelist);
284 list_del_init(&rq->queuelist);
285 goto done;
286 }
287
288 reads = !list_empty(&dd->fifo_list[READ]);
289 writes = !list_empty(&dd->fifo_list[WRITE]);
290
291 /*
292 * batches are currently reads XOR writes
293 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900294 rq = deadline_next_request(dd, WRITE);
295 if (!rq)
296 rq = deadline_next_request(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
307 if (reads) {
308 BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
309
Damien Le Moal5700f692017-12-21 15:43:40 +0900310 if (deadline_fifo_request(dd, WRITE) &&
311 (dd->starved++ >= dd->writes_starved))
Jens Axboe945ffb62017-01-14 17:11:11 -0700312 goto dispatch_writes;
313
314 data_dir = READ;
315
316 goto dispatch_find_request;
317 }
318
319 /*
320 * there are either no reads or writes have been starved
321 */
322
323 if (writes) {
324dispatch_writes:
325 BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));
326
327 dd->starved = 0;
328
329 data_dir = WRITE;
330
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 Asschedfc5e142021-05-21 11:00:46 -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
400static void dd_exit_queue(struct elevator_queue *e)
401{
402 struct deadline_data *dd = e->elevator_data;
403
404 BUG_ON(!list_empty(&dd->fifo_list[READ]));
405 BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
406
407 kfree(dd);
408}
409
410/*
411 * initialize elevator private data (deadline_data).
412 */
413static int dd_init_queue(struct request_queue *q, struct elevator_type *e)
414{
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
429 INIT_LIST_HEAD(&dd->fifo_list[READ]);
430 INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
431 dd->sort_list[READ] = RB_ROOT;
432 dd->sort_list[WRITE] = RB_ROOT;
433 dd->fifo_expire[READ] = read_expire;
434 dd->fifo_expire[WRITE] = write_expire;
435 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 Asschedfc5e142021-05-21 11:00:46 -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 Asschedfc5e142021-05-21 11:00:46 -0700473/*
474 * Attempt to merge a bio into an existing request. This function is called
475 * before @bio is associated with a request.
476 */
Greg Kroah-Hartmandf50ab92021-05-20 15:50:44 +0200477static 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;
502 const int data_dir = rq_data_dir(rq);
503
Bart Van Assched0d50fe2021-05-19 16:16:06 -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
515 blk_mq_sched_request_inserted(rq);
516
Lin Fenga749efa2021-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 Asschedfc5e142021-05-21 11:00:46 -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 Asschedfc5e142021-05-21 11:00:46 -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);
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900590 if (!list_empty(&dd->fifo_list[WRITE]))
591 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}
631SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[READ], 1);
632SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[WRITE], 1);
633SHOW_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}
654STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
655STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
656STORE_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}
722DEADLINE_DEBUGFS_DDIR_ATTRS(READ, read)
723DEADLINE_DEBUGFS_DDIR_ATTRS(WRITE, write)
724#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,
805 .init_sched = dd_init_queue,
806 .exit_sched = dd_exit_queue,
807 },
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");