blob: f441bf5929be58f2f9a565a29585b44c3260b403 [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"
Bart Van Assche855265c2021-06-11 16:48:32 -070026#include "mq-deadline-cgroup.h"
Jens Axboe945ffb62017-01-14 17:11:11 -070027
28/*
Mauro Carvalho Chehab898bd372019-04-18 19:45:00 -030029 * See Documentation/block/deadline-iosched.rst
Jens Axboe945ffb62017-01-14 17:11:11 -070030 */
31static const int read_expire = HZ / 2; /* max time before a read is submitted. */
32static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
Bart Van Asschee35b90b2021-05-27 16:53:27 -070033/*
34 * Time after which to dispatch lower priority requests even if higher
35 * priority requests are pending.
36 */
37static const int aging_expire = 10 * HZ;
Jens Axboe945ffb62017-01-14 17:11:11 -070038static const int writes_starved = 2; /* max times reads can starve a write */
39static const int fifo_batch = 16; /* # of sequential requests treated as one
40 by the above parameters. For throughput. */
41
Bart Van Asscheaf700302021-05-19 14:52:22 -070042enum dd_data_dir {
43 DD_READ = READ,
44 DD_WRITE = WRITE,
45};
46
47enum { DD_DIR_COUNT = 2 };
48
Bart Van Asschee3880a62021-05-19 14:47:00 -070049enum dd_prio {
50 DD_RT_PRIO = 0,
51 DD_BE_PRIO = 1,
52 DD_IDLE_PRIO = 2,
53 DD_PRIO_MAX = 2,
54};
55
56enum { DD_PRIO_COUNT = 3 };
57
Bart Van Assche6981c532021-06-11 16:58:51 -070058/* I/O statistics for all I/O priorities (enum dd_prio). */
59struct io_stats {
60 struct io_stats_per_prio stats[DD_PRIO_COUNT];
61};
62
Bart Van Asschee3880a62021-05-19 14:47:00 -070063/*
64 * Deadline scheduler data per I/O priority (enum dd_prio). Requests are
65 * present on both sort_list[] and fifo_list[].
66 */
67struct dd_per_prio {
68 struct list_head dispatch;
69 struct rb_root sort_list[DD_DIR_COUNT];
70 struct list_head fifo_list[DD_DIR_COUNT];
71 /* Next request in FIFO order. Read, write or both are NULL. */
72 struct request *next_rq[DD_DIR_COUNT];
73};
74
Jens Axboe945ffb62017-01-14 17:11:11 -070075struct deadline_data {
76 /*
77 * run time data
78 */
79
Bart Van Assche855265c2021-06-11 16:48:32 -070080 /* Request queue that owns this data structure. */
81 struct request_queue *queue;
82
Bart Van Asschee3880a62021-05-19 14:47:00 -070083 struct dd_per_prio per_prio[DD_PRIO_COUNT];
Jens Axboe945ffb62017-01-14 17:11:11 -070084
Bart Van Assche63544e142021-06-10 16:51:14 -070085 /* Data direction of latest dispatched request. */
86 enum dd_data_dir last_dir;
Jens Axboe945ffb62017-01-14 17:11:11 -070087 unsigned int batching; /* number of sequential requests made */
88 unsigned int starved; /* times reads have starved writes */
89
Bart Van Assche6981c532021-06-11 16:58:51 -070090 struct io_stats __percpu *stats;
91
Jens Axboe945ffb62017-01-14 17:11:11 -070092 /*
93 * settings that change how the i/o scheduler behaves
94 */
Bart Van Asscheaf700302021-05-19 14:52:22 -070095 int fifo_expire[DD_DIR_COUNT];
Jens Axboe945ffb62017-01-14 17:11:11 -070096 int fifo_batch;
97 int writes_starved;
98 int front_merges;
Bart Van Assche179aecb2021-05-18 15:48:36 -070099 u32 async_depth;
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700100 int aging_expire;
Jens Axboe945ffb62017-01-14 17:11:11 -0700101
102 spinlock_t lock;
Damien Le Moal5700f692017-12-21 15:43:40 +0900103 spinlock_t zone_lock;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700104};
105
Bart Van Assche6981c532021-06-11 16:58:51 -0700106/* Count one event of type 'event_type' and with I/O priority 'prio' */
107#define dd_count(dd, event_type, prio) do { \
108 struct io_stats *io_stats = get_cpu_ptr((dd)->stats); \
109 \
110 BUILD_BUG_ON(!__same_type((dd), struct deadline_data *)); \
111 BUILD_BUG_ON(!__same_type((prio), enum dd_prio)); \
112 local_inc(&io_stats->stats[(prio)].event_type); \
113 put_cpu_ptr(io_stats); \
114} while (0)
115
116/*
117 * Returns the total number of dd_count(dd, event_type, prio) calls across all
118 * CPUs. No locking or barriers since it is fine if the returned sum is slightly
119 * outdated.
120 */
121#define dd_sum(dd, event_type, prio) ({ \
122 unsigned int cpu; \
123 u32 sum = 0; \
124 \
125 BUILD_BUG_ON(!__same_type((dd), struct deadline_data *)); \
126 BUILD_BUG_ON(!__same_type((prio), enum dd_prio)); \
127 for_each_present_cpu(cpu) \
128 sum += local_read(&per_cpu_ptr((dd)->stats, cpu)-> \
129 stats[(prio)].event_type); \
130 sum; \
131})
132
Bart Van Asschee3880a62021-05-19 14:47:00 -0700133/* Maps an I/O priority class to a deadline scheduler priority. */
134static const enum dd_prio ioprio_class_to_prio[] = {
135 [IOPRIO_CLASS_NONE] = DD_BE_PRIO,
136 [IOPRIO_CLASS_RT] = DD_RT_PRIO,
137 [IOPRIO_CLASS_BE] = DD_BE_PRIO,
138 [IOPRIO_CLASS_IDLE] = DD_IDLE_PRIO,
Jens Axboe945ffb62017-01-14 17:11:11 -0700139};
140
141static inline struct rb_root *
Bart Van Asschee3880a62021-05-19 14:47:00 -0700142deadline_rb_root(struct dd_per_prio *per_prio, struct request *rq)
Jens Axboe945ffb62017-01-14 17:11:11 -0700143{
Bart Van Asschee3880a62021-05-19 14:47:00 -0700144 return &per_prio->sort_list[rq_data_dir(rq)];
145}
146
147/*
148 * Returns the I/O priority class (IOPRIO_CLASS_*) that has been assigned to a
149 * request.
150 */
151static u8 dd_rq_ioclass(struct request *rq)
152{
153 return IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
Jens Axboe945ffb62017-01-14 17:11:11 -0700154}
155
156/*
157 * get the request after `rq' in sector-sorted order
158 */
159static inline struct request *
160deadline_latter_request(struct request *rq)
161{
162 struct rb_node *node = rb_next(&rq->rb_node);
163
164 if (node)
165 return rb_entry_rq(node);
166
167 return NULL;
168}
169
170static void
Bart Van Asschee3880a62021-05-19 14:47:00 -0700171deadline_add_rq_rb(struct dd_per_prio *per_prio, struct request *rq)
Jens Axboe945ffb62017-01-14 17:11:11 -0700172{
Bart Van Asschee3880a62021-05-19 14:47:00 -0700173 struct rb_root *root = deadline_rb_root(per_prio, rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700174
175 elv_rb_add(root, rq);
176}
177
178static inline void
Bart Van Asschee3880a62021-05-19 14:47:00 -0700179deadline_del_rq_rb(struct dd_per_prio *per_prio, struct request *rq)
Jens Axboe945ffb62017-01-14 17:11:11 -0700180{
Bart Van Asscheaf700302021-05-19 14:52:22 -0700181 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700182
Bart Van Asschee3880a62021-05-19 14:47:00 -0700183 if (per_prio->next_rq[data_dir] == rq)
184 per_prio->next_rq[data_dir] = deadline_latter_request(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700185
Bart Van Asschee3880a62021-05-19 14:47:00 -0700186 elv_rb_del(deadline_rb_root(per_prio, rq), rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700187}
188
189/*
190 * remove rq from rbtree and fifo.
191 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700192static void deadline_remove_request(struct request_queue *q,
193 struct dd_per_prio *per_prio,
194 struct request *rq)
Jens Axboe945ffb62017-01-14 17:11:11 -0700195{
Jens Axboe945ffb62017-01-14 17:11:11 -0700196 list_del_init(&rq->queuelist);
197
198 /*
199 * We might not be on the rbtree, if we are doing an insert merge
200 */
201 if (!RB_EMPTY_NODE(&rq->rb_node))
Bart Van Asschee3880a62021-05-19 14:47:00 -0700202 deadline_del_rq_rb(per_prio, rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700203
204 elv_rqhash_del(q, rq);
205 if (q->last_merge == rq)
206 q->last_merge = NULL;
207}
208
209static void dd_request_merged(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100210 enum elv_merge type)
Jens Axboe945ffb62017-01-14 17:11:11 -0700211{
212 struct deadline_data *dd = q->elevator->elevator_data;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700213 const u8 ioprio_class = dd_rq_ioclass(req);
214 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
215 struct dd_per_prio *per_prio = &dd->per_prio[prio];
Jens Axboe945ffb62017-01-14 17:11:11 -0700216
217 /*
218 * if the merge was a front merge, we need to reposition request
219 */
220 if (type == ELEVATOR_FRONT_MERGE) {
Bart Van Asschee3880a62021-05-19 14:47:00 -0700221 elv_rb_del(deadline_rb_root(per_prio, req), req);
222 deadline_add_rq_rb(per_prio, req);
Jens Axboe945ffb62017-01-14 17:11:11 -0700223 }
224}
225
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700226/*
227 * Callback function that is invoked after @next has been merged into @req.
228 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700229static void dd_merged_requests(struct request_queue *q, struct request *req,
230 struct request *next)
231{
Bart Van Asschee3880a62021-05-19 14:47:00 -0700232 struct deadline_data *dd = q->elevator->elevator_data;
233 const u8 ioprio_class = dd_rq_ioclass(next);
234 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
Bart Van Assche855265c2021-06-11 16:48:32 -0700235 struct dd_blkcg *blkcg = next->elv.priv[0];
Bart Van Asschee3880a62021-05-19 14:47:00 -0700236
Bart Van Assche6981c532021-06-11 16:58:51 -0700237 dd_count(dd, merged, prio);
Bart Van Assche855265c2021-06-11 16:48:32 -0700238 ddcg_count(blkcg, merged, ioprio_class);
Bart Van Assche6981c532021-06-11 16:58:51 -0700239
Jens Axboe945ffb62017-01-14 17:11:11 -0700240 /*
241 * if next expires before rq, assign its expire time to rq
242 * and move into next position (next will be deleted) in fifo
243 */
244 if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
245 if (time_before((unsigned long)next->fifo_time,
246 (unsigned long)req->fifo_time)) {
247 list_move(&req->queuelist, &next->queuelist);
248 req->fifo_time = next->fifo_time;
249 }
250 }
251
252 /*
253 * kill knowledge of next, this one is a goner
254 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700255 deadline_remove_request(q, &dd->per_prio[prio], next);
Jens Axboe945ffb62017-01-14 17:11:11 -0700256}
257
258/*
259 * move an entry to dispatch queue
260 */
261static void
Bart Van Asschee3880a62021-05-19 14:47:00 -0700262deadline_move_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
263 struct request *rq)
Jens Axboe945ffb62017-01-14 17:11:11 -0700264{
Bart Van Asscheaf700302021-05-19 14:52:22 -0700265 const enum dd_data_dir data_dir = rq_data_dir(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700266
Bart Van Asschee3880a62021-05-19 14:47:00 -0700267 per_prio->next_rq[data_dir] = deadline_latter_request(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700268
269 /*
270 * take it off the sort and fifo list
271 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700272 deadline_remove_request(rq->q, per_prio, rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700273}
274
Bart Van Assche6981c532021-06-11 16:58:51 -0700275/* Number of requests queued for a given priority level. */
276static u32 dd_queued(struct deadline_data *dd, enum dd_prio prio)
277{
278 return dd_sum(dd, inserted, prio) - dd_sum(dd, completed, prio);
279}
280
Jens Axboe945ffb62017-01-14 17:11:11 -0700281/*
282 * deadline_check_fifo returns 0 if there are no expired requests on the fifo,
283 * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir])
284 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700285static inline int deadline_check_fifo(struct dd_per_prio *per_prio,
Bart Van Asscheaf700302021-05-19 14:52:22 -0700286 enum dd_data_dir data_dir)
Jens Axboe945ffb62017-01-14 17:11:11 -0700287{
Bart Van Asschee3880a62021-05-19 14:47:00 -0700288 struct request *rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next);
Jens Axboe945ffb62017-01-14 17:11:11 -0700289
290 /*
291 * rq is expired!
292 */
293 if (time_after_eq(jiffies, (unsigned long)rq->fifo_time))
294 return 1;
295
296 return 0;
297}
298
299/*
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900300 * For the specified data direction, return the next request to
301 * dispatch using arrival ordered lists.
302 */
303static struct request *
Bart Van Asschee3880a62021-05-19 14:47:00 -0700304deadline_fifo_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
305 enum dd_data_dir data_dir)
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900306{
Damien Le Moal5700f692017-12-21 15:43:40 +0900307 struct request *rq;
308 unsigned long flags;
309
Bart Van Asschee3880a62021-05-19 14:47:00 -0700310 if (list_empty(&per_prio->fifo_list[data_dir]))
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900311 return NULL;
312
Bart Van Asschee3880a62021-05-19 14:47:00 -0700313 rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next);
Bart Van Asscheaf700302021-05-19 14:52:22 -0700314 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
Damien Le Moal5700f692017-12-21 15:43:40 +0900315 return rq;
316
317 /*
318 * Look for a write request that can be dispatched, that is one with
319 * an unlocked target zone.
320 */
321 spin_lock_irqsave(&dd->zone_lock, flags);
Bart Van Asschee3880a62021-05-19 14:47:00 -0700322 list_for_each_entry(rq, &per_prio->fifo_list[DD_WRITE], queuelist) {
Damien Le Moal5700f692017-12-21 15:43:40 +0900323 if (blk_req_can_dispatch_to_zone(rq))
324 goto out;
325 }
326 rq = NULL;
327out:
328 spin_unlock_irqrestore(&dd->zone_lock, flags);
329
330 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900331}
332
333/*
334 * For the specified data direction, return the next request to
335 * dispatch using sector position sorted lists.
336 */
337static struct request *
Bart Van Asschee3880a62021-05-19 14:47:00 -0700338deadline_next_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
339 enum dd_data_dir data_dir)
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900340{
Damien Le Moal5700f692017-12-21 15:43:40 +0900341 struct request *rq;
342 unsigned long flags;
343
Bart Van Asschee3880a62021-05-19 14:47:00 -0700344 rq = per_prio->next_rq[data_dir];
Damien Le Moal5700f692017-12-21 15:43:40 +0900345 if (!rq)
346 return NULL;
347
Bart Van Asscheaf700302021-05-19 14:52:22 -0700348 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
Damien Le Moal5700f692017-12-21 15:43:40 +0900349 return rq;
350
351 /*
352 * Look for a write request that can be dispatched, that is one with
353 * an unlocked target zone.
354 */
355 spin_lock_irqsave(&dd->zone_lock, flags);
356 while (rq) {
357 if (blk_req_can_dispatch_to_zone(rq))
358 break;
359 rq = deadline_latter_request(rq);
360 }
361 spin_unlock_irqrestore(&dd->zone_lock, flags);
362
363 return rq;
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900364}
365
366/*
Jens Axboe945ffb62017-01-14 17:11:11 -0700367 * deadline_dispatch_requests selects the best request according to
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700368 * read/write expire, fifo_batch, etc and with a start time <= @latest.
Jens Axboe945ffb62017-01-14 17:11:11 -0700369 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700370static struct request *__dd_dispatch_request(struct deadline_data *dd,
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700371 struct dd_per_prio *per_prio,
372 u64 latest_start_ns)
Jens Axboe945ffb62017-01-14 17:11:11 -0700373{
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900374 struct request *rq, *next_rq;
Bart Van Asscheaf700302021-05-19 14:52:22 -0700375 enum dd_data_dir data_dir;
Bart Van Assche855265c2021-06-11 16:48:32 -0700376 struct dd_blkcg *blkcg;
Bart Van Assche6981c532021-06-11 16:58:51 -0700377 enum dd_prio prio;
378 u8 ioprio_class;
Jens Axboe945ffb62017-01-14 17:11:11 -0700379
Bart Van Assched0d50fe2021-05-19 16:16:06 -0700380 lockdep_assert_held(&dd->lock);
381
Bart Van Asschee3880a62021-05-19 14:47:00 -0700382 if (!list_empty(&per_prio->dispatch)) {
383 rq = list_first_entry(&per_prio->dispatch, struct request,
384 queuelist);
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700385 if (rq->start_time_ns > latest_start_ns)
386 return NULL;
Jens Axboe945ffb62017-01-14 17:11:11 -0700387 list_del_init(&rq->queuelist);
388 goto done;
389 }
390
Jens Axboe945ffb62017-01-14 17:11:11 -0700391 /*
392 * batches are currently reads XOR writes
393 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700394 rq = deadline_next_request(dd, per_prio, dd->last_dir);
Jens Axboe945ffb62017-01-14 17:11:11 -0700395 if (rq && dd->batching < dd->fifo_batch)
396 /* we have a next request are still entitled to batch */
397 goto dispatch_request;
398
399 /*
400 * at this point we are not running a batch. select the appropriate
401 * data direction (read / write)
402 */
403
Bart Van Asschee3880a62021-05-19 14:47:00 -0700404 if (!list_empty(&per_prio->fifo_list[DD_READ])) {
405 BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_READ]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700406
Bart Van Asschee3880a62021-05-19 14:47:00 -0700407 if (deadline_fifo_request(dd, per_prio, DD_WRITE) &&
Damien Le Moal5700f692017-12-21 15:43:40 +0900408 (dd->starved++ >= dd->writes_starved))
Jens Axboe945ffb62017-01-14 17:11:11 -0700409 goto dispatch_writes;
410
Bart Van Asscheaf700302021-05-19 14:52:22 -0700411 data_dir = DD_READ;
Jens Axboe945ffb62017-01-14 17:11:11 -0700412
413 goto dispatch_find_request;
414 }
415
416 /*
417 * there are either no reads or writes have been starved
418 */
419
Bart Van Asschee3880a62021-05-19 14:47:00 -0700420 if (!list_empty(&per_prio->fifo_list[DD_WRITE])) {
Jens Axboe945ffb62017-01-14 17:11:11 -0700421dispatch_writes:
Bart Van Asschee3880a62021-05-19 14:47:00 -0700422 BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_WRITE]));
Jens Axboe945ffb62017-01-14 17:11:11 -0700423
424 dd->starved = 0;
425
Bart Van Asscheaf700302021-05-19 14:52:22 -0700426 data_dir = DD_WRITE;
Jens Axboe945ffb62017-01-14 17:11:11 -0700427
428 goto dispatch_find_request;
429 }
430
431 return NULL;
432
433dispatch_find_request:
434 /*
435 * we are not running a batch, find best request for selected data_dir
436 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700437 next_rq = deadline_next_request(dd, per_prio, data_dir);
438 if (deadline_check_fifo(per_prio, data_dir) || !next_rq) {
Jens Axboe945ffb62017-01-14 17:11:11 -0700439 /*
440 * A deadline has expired, the last request was in the other
441 * direction, or we have run out of higher-sectored requests.
442 * Start again from the request with the earliest expiry time.
443 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700444 rq = deadline_fifo_request(dd, per_prio, data_dir);
Jens Axboe945ffb62017-01-14 17:11:11 -0700445 } else {
446 /*
447 * The last req was the same dir and we have a next request in
448 * sort order. No expired requests so continue on from here.
449 */
Damien Le Moalbf09ce52017-12-21 15:43:39 +0900450 rq = next_rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700451 }
452
Damien Le Moal5700f692017-12-21 15:43:40 +0900453 /*
454 * For a zoned block device, if we only have writes queued and none of
455 * them can be dispatched, rq will be NULL.
456 */
457 if (!rq)
458 return NULL;
459
Bart Van Assche63544e142021-06-10 16:51:14 -0700460 dd->last_dir = data_dir;
Jens Axboe945ffb62017-01-14 17:11:11 -0700461 dd->batching = 0;
462
463dispatch_request:
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700464 if (rq->start_time_ns > latest_start_ns)
465 return NULL;
Jens Axboe945ffb62017-01-14 17:11:11 -0700466 /*
467 * rq is the selected appropriate request.
468 */
469 dd->batching++;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700470 deadline_move_request(dd, per_prio, rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700471done:
Bart Van Assche6981c532021-06-11 16:58:51 -0700472 ioprio_class = dd_rq_ioclass(rq);
473 prio = ioprio_class_to_prio[ioprio_class];
474 dd_count(dd, dispatched, prio);
Bart Van Assche855265c2021-06-11 16:48:32 -0700475 blkcg = rq->elv.priv[0];
476 ddcg_count(blkcg, dispatched, ioprio_class);
Damien Le Moal5700f692017-12-21 15:43:40 +0900477 /*
478 * If the request needs its target zone locked, do it.
479 */
480 blk_req_zone_write_lock(rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700481 rq->rq_flags |= RQF_STARTED;
482 return rq;
483}
484
Jens Axboeca11f202018-01-06 09:23:11 -0700485/*
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700486 * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests().
487 *
Jens Axboeca11f202018-01-06 09:23:11 -0700488 * One confusing aspect here is that we get called for a specific
Damien Le Moal7211aef82018-12-17 15:14:05 +0900489 * hardware queue, but we may return a request that is for a
Jens Axboeca11f202018-01-06 09:23:11 -0700490 * different hardware queue. This is because mq-deadline has shared
491 * state for all hardware queues, in terms of sorting, FIFOs, etc.
492 */
Jens Axboec13660a2017-01-26 12:40:07 -0700493static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
Jens Axboe945ffb62017-01-14 17:11:11 -0700494{
495 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700496 const u64 now_ns = ktime_get_ns();
497 struct request *rq = NULL;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700498 enum dd_prio prio;
Jens Axboe945ffb62017-01-14 17:11:11 -0700499
500 spin_lock(&dd->lock);
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700501 /*
502 * Start with dispatching requests whose deadline expired more than
503 * aging_expire jiffies ago.
504 */
505 for (prio = DD_BE_PRIO; prio <= DD_PRIO_MAX; prio++) {
506 rq = __dd_dispatch_request(dd, &dd->per_prio[prio], now_ns -
507 jiffies_to_nsecs(dd->aging_expire));
Bart Van Asschee3880a62021-05-19 14:47:00 -0700508 if (rq)
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700509 goto unlock;
510 }
511 /*
512 * Next, dispatch requests in priority order. Ignore lower priority
513 * requests if any higher priority requests are pending.
514 */
515 for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
516 rq = __dd_dispatch_request(dd, &dd->per_prio[prio], now_ns);
517 if (rq || dd_queued(dd, prio))
Bart Van Asschee3880a62021-05-19 14:47:00 -0700518 break;
519 }
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700520
521unlock:
Jens Axboe945ffb62017-01-14 17:11:11 -0700522 spin_unlock(&dd->lock);
Jens Axboec13660a2017-01-26 12:40:07 -0700523
524 return rq;
Jens Axboe945ffb62017-01-14 17:11:11 -0700525}
526
Bart Van Assche179aecb2021-05-18 15:48:36 -0700527/*
528 * Called by __blk_mq_alloc_request(). The shallow_depth value set by this
529 * function is used by __blk_mq_get_tag().
530 */
531static void dd_limit_depth(unsigned int op, struct blk_mq_alloc_data *data)
532{
533 struct deadline_data *dd = data->q->elevator->elevator_data;
534
535 /* Do not throttle synchronous reads. */
536 if (op_is_sync(op) && !op_is_write(op))
537 return;
538
539 /*
540 * Throttle asynchronous requests and writes such that these requests
541 * do not block the allocation of synchronous requests.
542 */
543 data->shallow_depth = dd->async_depth;
544}
545
546/* Called by blk_mq_update_nr_requests(). */
547static void dd_depth_updated(struct blk_mq_hw_ctx *hctx)
548{
549 struct request_queue *q = hctx->queue;
550 struct deadline_data *dd = q->elevator->elevator_data;
551 struct blk_mq_tags *tags = hctx->sched_tags;
552
553 dd->async_depth = max(1UL, 3 * q->nr_requests / 4);
554
555 sbitmap_queue_min_shallow_depth(tags->bitmap_tags, dd->async_depth);
556}
557
558/* Called by blk_mq_init_hctx() and blk_mq_init_sched(). */
559static int dd_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
560{
561 dd_depth_updated(hctx);
562 return 0;
563}
564
Bart Van Assched3cebf12021-05-20 14:06:56 -0700565static void dd_exit_sched(struct elevator_queue *e)
Jens Axboe945ffb62017-01-14 17:11:11 -0700566{
567 struct deadline_data *dd = e->elevator_data;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700568 enum dd_prio prio;
Jens Axboe945ffb62017-01-14 17:11:11 -0700569
Bart Van Assche855265c2021-06-11 16:48:32 -0700570 dd_deactivate_policy(dd->queue);
571
Bart Van Asschee3880a62021-05-19 14:47:00 -0700572 for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
573 struct dd_per_prio *per_prio = &dd->per_prio[prio];
574
575 WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_READ]));
576 WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_WRITE]));
577 }
Jens Axboe945ffb62017-01-14 17:11:11 -0700578
Bart Van Assche6981c532021-06-11 16:58:51 -0700579 free_percpu(dd->stats);
580
Jens Axboe945ffb62017-01-14 17:11:11 -0700581 kfree(dd);
582}
583
584/*
Bart Van Assche855265c2021-06-11 16:48:32 -0700585 * Initialize elevator private data (deadline_data) and associate with blkcg.
Jens Axboe945ffb62017-01-14 17:11:11 -0700586 */
Bart Van Assched3cebf12021-05-20 14:06:56 -0700587static int dd_init_sched(struct request_queue *q, struct elevator_type *e)
Jens Axboe945ffb62017-01-14 17:11:11 -0700588{
589 struct deadline_data *dd;
590 struct elevator_queue *eq;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700591 enum dd_prio prio;
592 int ret = -ENOMEM;
Jens Axboe945ffb62017-01-14 17:11:11 -0700593
Bart Van Assche855265c2021-06-11 16:48:32 -0700594 /*
595 * Initialization would be very tricky if the queue is not frozen,
596 * hence the warning statement below.
597 */
598 WARN_ON_ONCE(!percpu_ref_is_zero(&q->q_usage_counter));
599
Jens Axboe945ffb62017-01-14 17:11:11 -0700600 eq = elevator_alloc(q, e);
601 if (!eq)
Bart Van Asschee3880a62021-05-19 14:47:00 -0700602 return ret;
Jens Axboe945ffb62017-01-14 17:11:11 -0700603
604 dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
Bart Van Asschee3880a62021-05-19 14:47:00 -0700605 if (!dd)
606 goto put_eq;
607
Jens Axboe945ffb62017-01-14 17:11:11 -0700608 eq->elevator_data = dd;
609
Bart Van Assche6981c532021-06-11 16:58:51 -0700610 dd->stats = alloc_percpu_gfp(typeof(*dd->stats),
611 GFP_KERNEL | __GFP_ZERO);
612 if (!dd->stats)
613 goto free_dd;
614
Bart Van Assche855265c2021-06-11 16:48:32 -0700615 dd->queue = q;
616
Bart Van Asschee3880a62021-05-19 14:47:00 -0700617 for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
618 struct dd_per_prio *per_prio = &dd->per_prio[prio];
619
620 INIT_LIST_HEAD(&per_prio->dispatch);
621 INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]);
622 INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]);
623 per_prio->sort_list[DD_READ] = RB_ROOT;
624 per_prio->sort_list[DD_WRITE] = RB_ROOT;
625 }
Bart Van Asscheaf700302021-05-19 14:52:22 -0700626 dd->fifo_expire[DD_READ] = read_expire;
627 dd->fifo_expire[DD_WRITE] = write_expire;
Jens Axboe945ffb62017-01-14 17:11:11 -0700628 dd->writes_starved = writes_starved;
629 dd->front_merges = 1;
Bart Van Assche63544e142021-06-10 16:51:14 -0700630 dd->last_dir = DD_WRITE;
Jens Axboe945ffb62017-01-14 17:11:11 -0700631 dd->fifo_batch = fifo_batch;
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700632 dd->aging_expire = aging_expire;
Jens Axboe945ffb62017-01-14 17:11:11 -0700633 spin_lock_init(&dd->lock);
Damien Le Moal5700f692017-12-21 15:43:40 +0900634 spin_lock_init(&dd->zone_lock);
Jens Axboe945ffb62017-01-14 17:11:11 -0700635
Bart Van Assche855265c2021-06-11 16:48:32 -0700636 ret = dd_activate_policy(q);
637 if (ret)
638 goto free_stats;
639
640 ret = 0;
Jens Axboe945ffb62017-01-14 17:11:11 -0700641 q->elevator = eq;
642 return 0;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700643
Bart Van Assche855265c2021-06-11 16:48:32 -0700644free_stats:
645 free_percpu(dd->stats);
646
Bart Van Assche6981c532021-06-11 16:58:51 -0700647free_dd:
648 kfree(dd);
649
Bart Van Asschee3880a62021-05-19 14:47:00 -0700650put_eq:
651 kobject_put(&eq->kobj);
652 return ret;
Jens Axboe945ffb62017-01-14 17:11:11 -0700653}
654
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700655/*
656 * Try to merge @bio into an existing request. If @bio has been merged into
657 * an existing request, store the pointer to that request into *@rq.
658 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700659static int dd_request_merge(struct request_queue *q, struct request **rq,
660 struct bio *bio)
661{
662 struct deadline_data *dd = q->elevator->elevator_data;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700663 const u8 ioprio_class = IOPRIO_PRIO_CLASS(bio->bi_ioprio);
664 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
665 struct dd_per_prio *per_prio = &dd->per_prio[prio];
Jens Axboe945ffb62017-01-14 17:11:11 -0700666 sector_t sector = bio_end_sector(bio);
667 struct request *__rq;
668
669 if (!dd->front_merges)
670 return ELEVATOR_NO_MERGE;
671
Bart Van Asschee3880a62021-05-19 14:47:00 -0700672 __rq = elv_rb_find(&per_prio->sort_list[bio_data_dir(bio)], sector);
Jens Axboe945ffb62017-01-14 17:11:11 -0700673 if (__rq) {
674 BUG_ON(sector != blk_rq_pos(__rq));
675
676 if (elv_bio_merge_ok(__rq, bio)) {
677 *rq = __rq;
Greg Kroah-Hartman23004182021-09-15 14:16:47 +0200678 if (blk_discard_mergable(__rq))
679 return ELEVATOR_DISCARD_MERGE;
Jens Axboe945ffb62017-01-14 17:11:11 -0700680 return ELEVATOR_FRONT_MERGE;
681 }
682 }
683
684 return ELEVATOR_NO_MERGE;
685}
686
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700687/*
688 * Attempt to merge a bio into an existing request. This function is called
689 * before @bio is associated with a request.
690 */
Greg Kroah-Hartmandf50ab92021-05-20 15:50:44 +0200691static bool dd_bio_merge(struct request_queue *q, struct bio *bio,
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200692 unsigned int nr_segs)
Jens Axboe945ffb62017-01-14 17:11:11 -0700693{
Jens Axboe945ffb62017-01-14 17:11:11 -0700694 struct deadline_data *dd = q->elevator->elevator_data;
Jens Axboee4d750c2017-02-03 09:48:28 -0700695 struct request *free = NULL;
696 bool ret;
Jens Axboe945ffb62017-01-14 17:11:11 -0700697
698 spin_lock(&dd->lock);
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200699 ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
Jens Axboe945ffb62017-01-14 17:11:11 -0700700 spin_unlock(&dd->lock);
701
Jens Axboee4d750c2017-02-03 09:48:28 -0700702 if (free)
703 blk_mq_free_request(free);
704
Jens Axboe945ffb62017-01-14 17:11:11 -0700705 return ret;
706}
707
708/*
709 * add rq to rbtree and fifo
710 */
711static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
712 bool at_head)
713{
714 struct request_queue *q = hctx->queue;
715 struct deadline_data *dd = q->elevator->elevator_data;
Bart Van Asscheaf700302021-05-19 14:52:22 -0700716 const enum dd_data_dir data_dir = rq_data_dir(rq);
Bart Van Asschee3880a62021-05-19 14:47:00 -0700717 u16 ioprio = req_get_ioprio(rq);
718 u8 ioprio_class = IOPRIO_PRIO_CLASS(ioprio);
719 struct dd_per_prio *per_prio;
720 enum dd_prio prio;
Bart Van Assche855265c2021-06-11 16:48:32 -0700721 struct dd_blkcg *blkcg;
Jens Axboe945ffb62017-01-14 17:11:11 -0700722
Bart Van Assched0d50fe2021-05-19 16:16:06 -0700723 lockdep_assert_held(&dd->lock);
724
Damien Le Moal5700f692017-12-21 15:43:40 +0900725 /*
726 * This may be a requeue of a write request that has locked its
727 * target zone. If it is the case, this releases the zone lock.
728 */
729 blk_req_zone_write_unlock(rq);
730
Bart Van Assche855265c2021-06-11 16:48:32 -0700731 /*
732 * If a block cgroup has been associated with the submitter and if an
733 * I/O priority has been set in the associated block cgroup, use the
734 * lowest of the cgroup priority and the request priority for the
735 * request. If no priority has been set in the request, use the cgroup
736 * priority.
737 */
Bart Van Asschee3880a62021-05-19 14:47:00 -0700738 prio = ioprio_class_to_prio[ioprio_class];
Bart Van Assche6981c532021-06-11 16:58:51 -0700739 dd_count(dd, inserted, prio);
Bart Van Assche855265c2021-06-11 16:48:32 -0700740 blkcg = dd_blkcg_from_bio(rq->bio);
741 ddcg_count(blkcg, inserted, ioprio_class);
742 rq->elv.priv[0] = blkcg;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700743
Jens Axboe945ffb62017-01-14 17:11:11 -0700744 if (blk_mq_sched_try_insert_merge(q, rq))
745 return;
746
747 blk_mq_sched_request_inserted(rq);
748
Bart Van Asschee3880a62021-05-19 14:47:00 -0700749 per_prio = &dd->per_prio[prio];
Lin Fenga749efa2021-04-15 11:43:26 +0800750 if (at_head) {
Bart Van Asschee3880a62021-05-19 14:47:00 -0700751 list_add(&rq->queuelist, &per_prio->dispatch);
Jens Axboe945ffb62017-01-14 17:11:11 -0700752 } else {
Bart Van Asschee3880a62021-05-19 14:47:00 -0700753 deadline_add_rq_rb(per_prio, rq);
Jens Axboe945ffb62017-01-14 17:11:11 -0700754
755 if (rq_mergeable(rq)) {
756 elv_rqhash_add(q, rq);
757 if (!q->last_merge)
758 q->last_merge = rq;
759 }
760
761 /*
762 * set expire time and add to fifo list
763 */
764 rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
Bart Van Asschee3880a62021-05-19 14:47:00 -0700765 list_add_tail(&rq->queuelist, &per_prio->fifo_list[data_dir]);
Jens Axboe945ffb62017-01-14 17:11:11 -0700766 }
767}
768
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700769/*
770 * Called from blk_mq_sched_insert_request() or blk_mq_sched_insert_requests().
771 */
Jens Axboe945ffb62017-01-14 17:11:11 -0700772static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
773 struct list_head *list, bool at_head)
774{
775 struct request_queue *q = hctx->queue;
776 struct deadline_data *dd = q->elevator->elevator_data;
777
778 spin_lock(&dd->lock);
779 while (!list_empty(list)) {
780 struct request *rq;
781
782 rq = list_first_entry(list, struct request, queuelist);
783 list_del_init(&rq->queuelist);
784 dd_insert_request(hctx, rq, at_head);
785 }
786 spin_unlock(&dd->lock);
787}
788
Bart Van Assche855265c2021-06-11 16:48:32 -0700789/* Callback from inside blk_mq_rq_ctx_init(). */
Christoph Hellwig5d9c3052020-05-29 15:53:08 +0200790static void dd_prepare_request(struct request *rq)
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800791{
Bart Van Assche855265c2021-06-11 16:48:32 -0700792 rq->elv.priv[0] = NULL;
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800793}
794
795/*
Bart Van Asschedfc5e142021-05-21 11:00:46 -0700796 * Callback from inside blk_mq_free_request().
797 *
Damien Le Moal5700f692017-12-21 15:43:40 +0900798 * For zoned block devices, write unlock the target zone of
799 * completed write requests. Do this while holding the zone lock
800 * spinlock so that the zone is never unlocked while deadline_fifo_request()
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800801 * or deadline_next_request() are executing. This function is called for
802 * all requests, whether or not these requests complete successfully.
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900803 *
804 * For a zoned block device, __dd_dispatch_request() may have stopped
805 * dispatching requests if all the queued requests are write requests directed
806 * at zones that are already locked due to on-going write requests. To ensure
807 * write request dispatch progress in this case, mark the queue as needing a
808 * restart to ensure that the queue is run again after completion of the
809 * request and zones being unlocked.
Damien Le Moal5700f692017-12-21 15:43:40 +0900810 */
Damien Le Moalf3bc78d2018-02-28 09:35:29 -0800811static void dd_finish_request(struct request *rq)
Damien Le Moal5700f692017-12-21 15:43:40 +0900812{
813 struct request_queue *q = rq->q;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700814 struct deadline_data *dd = q->elevator->elevator_data;
Bart Van Assche855265c2021-06-11 16:48:32 -0700815 struct dd_blkcg *blkcg = rq->elv.priv[0];
Bart Van Asschee3880a62021-05-19 14:47:00 -0700816 const u8 ioprio_class = dd_rq_ioclass(rq);
817 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
818 struct dd_per_prio *per_prio = &dd->per_prio[prio];
Damien Le Moal5700f692017-12-21 15:43:40 +0900819
Bart Van Assche6981c532021-06-11 16:58:51 -0700820 dd_count(dd, completed, prio);
Bart Van Assche855265c2021-06-11 16:48:32 -0700821 ddcg_count(blkcg, completed, ioprio_class);
Bart Van Assche6981c532021-06-11 16:58:51 -0700822
Damien Le Moal5700f692017-12-21 15:43:40 +0900823 if (blk_queue_is_zoned(q)) {
Damien Le Moal5700f692017-12-21 15:43:40 +0900824 unsigned long flags;
825
826 spin_lock_irqsave(&dd->zone_lock, flags);
827 blk_req_zone_write_unlock(rq);
Bart Van Asschee3880a62021-05-19 14:47:00 -0700828 if (!list_empty(&per_prio->fifo_list[DD_WRITE]))
Damien Le Moalcb8acab2019-08-28 13:40:20 +0900829 blk_mq_sched_mark_restart_hctx(rq->mq_hctx);
Damien Le Moal5700f692017-12-21 15:43:40 +0900830 spin_unlock_irqrestore(&dd->zone_lock, flags);
831 }
832}
833
Bart Van Asschee3880a62021-05-19 14:47:00 -0700834static bool dd_has_work_for_prio(struct dd_per_prio *per_prio)
835{
836 return !list_empty_careful(&per_prio->dispatch) ||
837 !list_empty_careful(&per_prio->fifo_list[DD_READ]) ||
838 !list_empty_careful(&per_prio->fifo_list[DD_WRITE]);
839}
840
Jens Axboe945ffb62017-01-14 17:11:11 -0700841static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
842{
843 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
Bart Van Asschee3880a62021-05-19 14:47:00 -0700844 enum dd_prio prio;
Jens Axboe945ffb62017-01-14 17:11:11 -0700845
Bart Van Asschee3880a62021-05-19 14:47:00 -0700846 for (prio = 0; prio <= DD_PRIO_MAX; prio++)
847 if (dd_has_work_for_prio(&dd->per_prio[prio]))
848 return true;
849
850 return false;
Jens Axboe945ffb62017-01-14 17:11:11 -0700851}
852
853/*
854 * sysfs parts below
855 */
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700856#define SHOW_INT(__FUNC, __VAR) \
Jens Axboe945ffb62017-01-14 17:11:11 -0700857static ssize_t __FUNC(struct elevator_queue *e, char *page) \
858{ \
859 struct deadline_data *dd = e->elevator_data; \
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700860 \
861 return sysfs_emit(page, "%d\n", __VAR); \
Jens Axboe945ffb62017-01-14 17:11:11 -0700862}
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700863#define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR))
864SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]);
865SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]);
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700866SHOW_JIFFIES(deadline_aging_expire_show, dd->aging_expire);
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700867SHOW_INT(deadline_writes_starved_show, dd->writes_starved);
868SHOW_INT(deadline_front_merges_show, dd->front_merges);
Bart Van Assche179aecb2021-05-18 15:48:36 -0700869SHOW_INT(deadline_async_depth_show, dd->front_merges);
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700870SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch);
871#undef SHOW_INT
872#undef SHOW_JIFFIES
Jens Axboe945ffb62017-01-14 17:11:11 -0700873
874#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
875static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
876{ \
877 struct deadline_data *dd = e->elevator_data; \
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700878 int __data, __ret; \
879 \
880 __ret = kstrtoint(page, 0, &__data); \
881 if (__ret < 0) \
882 return __ret; \
Jens Axboe945ffb62017-01-14 17:11:11 -0700883 if (__data < (MIN)) \
884 __data = (MIN); \
885 else if (__data > (MAX)) \
886 __data = (MAX); \
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700887 *(__PTR) = __CONV(__data); \
weiping zhang235f8da2017-08-25 01:11:33 +0800888 return count; \
Jens Axboe945ffb62017-01-14 17:11:11 -0700889}
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700890#define STORE_INT(__FUNC, __PTR, MIN, MAX) \
891 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, )
892#define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \
893 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies)
894STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX);
895STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX);
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700896STORE_JIFFIES(deadline_aging_expire_store, &dd->aging_expire, 0, INT_MAX);
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700897STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX);
898STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1);
Bart Van Assche179aecb2021-05-18 15:48:36 -0700899STORE_INT(deadline_async_depth_store, &dd->front_merges, 1, INT_MAX);
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700900STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX);
Jens Axboe945ffb62017-01-14 17:11:11 -0700901#undef STORE_FUNCTION
Bart Van Asschef3daa8d2021-06-07 16:52:20 -0700902#undef STORE_INT
903#undef STORE_JIFFIES
Jens Axboe945ffb62017-01-14 17:11:11 -0700904
905#define DD_ATTR(name) \
Joe Perches5657a812018-05-24 13:38:59 -0600906 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
Jens Axboe945ffb62017-01-14 17:11:11 -0700907
908static struct elv_fs_entry deadline_attrs[] = {
909 DD_ATTR(read_expire),
910 DD_ATTR(write_expire),
911 DD_ATTR(writes_starved),
912 DD_ATTR(front_merges),
Bart Van Assche179aecb2021-05-18 15:48:36 -0700913 DD_ATTR(async_depth),
Jens Axboe945ffb62017-01-14 17:11:11 -0700914 DD_ATTR(fifo_batch),
Bart Van Asschee35b90b2021-05-27 16:53:27 -0700915 DD_ATTR(aging_expire),
Jens Axboe945ffb62017-01-14 17:11:11 -0700916 __ATTR_NULL
917};
918
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700919#ifdef CONFIG_BLK_DEBUG_FS
Bart Van Asschee3880a62021-05-19 14:47:00 -0700920#define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700921static void *deadline_##name##_fifo_start(struct seq_file *m, \
922 loff_t *pos) \
923 __acquires(&dd->lock) \
924{ \
925 struct request_queue *q = m->private; \
926 struct deadline_data *dd = q->elevator->elevator_data; \
Bart Van Asschee3880a62021-05-19 14:47:00 -0700927 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700928 \
929 spin_lock(&dd->lock); \
Bart Van Asschee3880a62021-05-19 14:47:00 -0700930 return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700931} \
932 \
933static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
934 loff_t *pos) \
935{ \
936 struct request_queue *q = m->private; \
937 struct deadline_data *dd = q->elevator->elevator_data; \
Bart Van Asschee3880a62021-05-19 14:47:00 -0700938 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700939 \
Bart Van Asschee3880a62021-05-19 14:47:00 -0700940 return seq_list_next(v, &per_prio->fifo_list[data_dir], pos); \
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700941} \
942 \
943static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
944 __releases(&dd->lock) \
945{ \
946 struct request_queue *q = m->private; \
947 struct deadline_data *dd = q->elevator->elevator_data; \
948 \
949 spin_unlock(&dd->lock); \
950} \
951 \
952static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
953 .start = deadline_##name##_fifo_start, \
954 .next = deadline_##name##_fifo_next, \
955 .stop = deadline_##name##_fifo_stop, \
956 .show = blk_mq_debugfs_rq_show, \
957}; \
958 \
959static int deadline_##name##_next_rq_show(void *data, \
960 struct seq_file *m) \
961{ \
962 struct request_queue *q = data; \
963 struct deadline_data *dd = q->elevator->elevator_data; \
Bart Van Asschee3880a62021-05-19 14:47:00 -0700964 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
965 struct request *rq = per_prio->next_rq[data_dir]; \
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700966 \
967 if (rq) \
968 __blk_mq_debugfs_rq_show(m, rq); \
969 return 0; \
970}
Bart Van Asschee3880a62021-05-19 14:47:00 -0700971
972DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_READ, read0);
973DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_WRITE, write0);
974DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_READ, read1);
975DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_WRITE, write1);
976DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_READ, read2);
977DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_WRITE, write2);
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700978#undef DEADLINE_DEBUGFS_DDIR_ATTRS
979
980static int deadline_batching_show(void *data, struct seq_file *m)
981{
982 struct request_queue *q = data;
983 struct deadline_data *dd = q->elevator->elevator_data;
984
985 seq_printf(m, "%u\n", dd->batching);
986 return 0;
987}
988
989static int deadline_starved_show(void *data, struct seq_file *m)
990{
991 struct request_queue *q = data;
992 struct deadline_data *dd = q->elevator->elevator_data;
993
994 seq_printf(m, "%u\n", dd->starved);
995 return 0;
996}
997
Bart Van Assche179aecb2021-05-18 15:48:36 -0700998static int dd_async_depth_show(void *data, struct seq_file *m)
999{
1000 struct request_queue *q = data;
1001 struct deadline_data *dd = q->elevator->elevator_data;
1002
1003 seq_printf(m, "%u\n", dd->async_depth);
1004 return 0;
1005}
1006
Bart Van Assche6981c532021-06-11 16:58:51 -07001007static int dd_queued_show(void *data, struct seq_file *m)
1008{
1009 struct request_queue *q = data;
1010 struct deadline_data *dd = q->elevator->elevator_data;
1011
1012 seq_printf(m, "%u %u %u\n", dd_queued(dd, DD_RT_PRIO),
1013 dd_queued(dd, DD_BE_PRIO),
1014 dd_queued(dd, DD_IDLE_PRIO));
1015 return 0;
1016}
1017
1018/* Number of requests owned by the block driver for a given priority. */
1019static u32 dd_owned_by_driver(struct deadline_data *dd, enum dd_prio prio)
1020{
1021 return dd_sum(dd, dispatched, prio) + dd_sum(dd, merged, prio)
1022 - dd_sum(dd, completed, prio);
1023}
1024
1025static int dd_owned_by_driver_show(void *data, struct seq_file *m)
1026{
1027 struct request_queue *q = data;
1028 struct deadline_data *dd = q->elevator->elevator_data;
1029
1030 seq_printf(m, "%u %u %u\n", dd_owned_by_driver(dd, DD_RT_PRIO),
1031 dd_owned_by_driver(dd, DD_BE_PRIO),
1032 dd_owned_by_driver(dd, DD_IDLE_PRIO));
1033 return 0;
1034}
1035
Bart Van Asschee3880a62021-05-19 14:47:00 -07001036#define DEADLINE_DISPATCH_ATTR(prio) \
1037static void *deadline_dispatch##prio##_start(struct seq_file *m, \
1038 loff_t *pos) \
1039 __acquires(&dd->lock) \
1040{ \
1041 struct request_queue *q = m->private; \
1042 struct deadline_data *dd = q->elevator->elevator_data; \
1043 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
1044 \
1045 spin_lock(&dd->lock); \
1046 return seq_list_start(&per_prio->dispatch, *pos); \
1047} \
1048 \
1049static void *deadline_dispatch##prio##_next(struct seq_file *m, \
1050 void *v, loff_t *pos) \
1051{ \
1052 struct request_queue *q = m->private; \
1053 struct deadline_data *dd = q->elevator->elevator_data; \
1054 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
1055 \
1056 return seq_list_next(v, &per_prio->dispatch, pos); \
1057} \
1058 \
1059static void deadline_dispatch##prio##_stop(struct seq_file *m, void *v) \
1060 __releases(&dd->lock) \
1061{ \
1062 struct request_queue *q = m->private; \
1063 struct deadline_data *dd = q->elevator->elevator_data; \
1064 \
1065 spin_unlock(&dd->lock); \
1066} \
1067 \
1068static const struct seq_operations deadline_dispatch##prio##_seq_ops = { \
1069 .start = deadline_dispatch##prio##_start, \
1070 .next = deadline_dispatch##prio##_next, \
1071 .stop = deadline_dispatch##prio##_stop, \
1072 .show = blk_mq_debugfs_rq_show, \
Omar Sandovaldaaadb32017-05-04 00:31:34 -07001073}
1074
Bart Van Asschee3880a62021-05-19 14:47:00 -07001075DEADLINE_DISPATCH_ATTR(0);
1076DEADLINE_DISPATCH_ATTR(1);
1077DEADLINE_DISPATCH_ATTR(2);
1078#undef DEADLINE_DISPATCH_ATTR
Omar Sandovaldaaadb32017-05-04 00:31:34 -07001079
Bart Van Asschee3880a62021-05-19 14:47:00 -07001080#define DEADLINE_QUEUE_DDIR_ATTRS(name) \
1081 {#name "_fifo_list", 0400, \
1082 .seq_ops = &deadline_##name##_fifo_seq_ops}
1083#define DEADLINE_NEXT_RQ_ATTR(name) \
Omar Sandovaldaaadb32017-05-04 00:31:34 -07001084 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
1085static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
Bart Van Asschee3880a62021-05-19 14:47:00 -07001086 DEADLINE_QUEUE_DDIR_ATTRS(read0),
1087 DEADLINE_QUEUE_DDIR_ATTRS(write0),
1088 DEADLINE_QUEUE_DDIR_ATTRS(read1),
1089 DEADLINE_QUEUE_DDIR_ATTRS(write1),
1090 DEADLINE_QUEUE_DDIR_ATTRS(read2),
1091 DEADLINE_QUEUE_DDIR_ATTRS(write2),
1092 DEADLINE_NEXT_RQ_ATTR(read0),
1093 DEADLINE_NEXT_RQ_ATTR(write0),
1094 DEADLINE_NEXT_RQ_ATTR(read1),
1095 DEADLINE_NEXT_RQ_ATTR(write1),
1096 DEADLINE_NEXT_RQ_ATTR(read2),
1097 DEADLINE_NEXT_RQ_ATTR(write2),
Omar Sandovaldaaadb32017-05-04 00:31:34 -07001098 {"batching", 0400, deadline_batching_show},
1099 {"starved", 0400, deadline_starved_show},
Bart Van Assche179aecb2021-05-18 15:48:36 -07001100 {"async_depth", 0400, dd_async_depth_show},
Bart Van Asschee3880a62021-05-19 14:47:00 -07001101 {"dispatch0", 0400, .seq_ops = &deadline_dispatch0_seq_ops},
1102 {"dispatch1", 0400, .seq_ops = &deadline_dispatch1_seq_ops},
1103 {"dispatch2", 0400, .seq_ops = &deadline_dispatch2_seq_ops},
Bart Van Assche6981c532021-06-11 16:58:51 -07001104 {"owned_by_driver", 0400, dd_owned_by_driver_show},
1105 {"queued", 0400, dd_queued_show},
Omar Sandovaldaaadb32017-05-04 00:31:34 -07001106 {},
1107};
1108#undef DEADLINE_QUEUE_DDIR_ATTRS
1109#endif
1110
Jens Axboe945ffb62017-01-14 17:11:11 -07001111static struct elevator_type mq_deadline = {
Jens Axboef9cd4bf2018-11-01 16:41:41 -06001112 .ops = {
Bart Van Assche179aecb2021-05-18 15:48:36 -07001113 .depth_updated = dd_depth_updated,
1114 .limit_depth = dd_limit_depth,
Jens Axboe945ffb62017-01-14 17:11:11 -07001115 .insert_requests = dd_insert_requests,
Jens Axboec13660a2017-01-26 12:40:07 -07001116 .dispatch_request = dd_dispatch_request,
Damien Le Moalf3bc78d2018-02-28 09:35:29 -08001117 .prepare_request = dd_prepare_request,
1118 .finish_request = dd_finish_request,
Jens Axboe945ffb62017-01-14 17:11:11 -07001119 .next_request = elv_rb_latter_request,
1120 .former_request = elv_rb_former_request,
1121 .bio_merge = dd_bio_merge,
1122 .request_merge = dd_request_merge,
1123 .requests_merged = dd_merged_requests,
1124 .request_merged = dd_request_merged,
1125 .has_work = dd_has_work,
Bart Van Assched3cebf12021-05-20 14:06:56 -07001126 .init_sched = dd_init_sched,
1127 .exit_sched = dd_exit_sched,
Bart Van Assche179aecb2021-05-18 15:48:36 -07001128 .init_hctx = dd_init_hctx,
Jens Axboe945ffb62017-01-14 17:11:11 -07001129 },
1130
Omar Sandovaldaaadb32017-05-04 00:31:34 -07001131#ifdef CONFIG_BLK_DEBUG_FS
1132 .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
1133#endif
Jens Axboe945ffb62017-01-14 17:11:11 -07001134 .elevator_attrs = deadline_attrs,
1135 .elevator_name = "mq-deadline",
Jens Axboe4d740bc2017-10-25 09:47:20 -06001136 .elevator_alias = "deadline",
Damien Le Moal68c43f12019-09-05 18:51:31 +09001137 .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE,
Jens Axboe945ffb62017-01-14 17:11:11 -07001138 .elevator_owner = THIS_MODULE,
1139};
Ben Hutchings7de967e2017-08-13 18:03:15 +01001140MODULE_ALIAS("mq-deadline-iosched");
Jens Axboe945ffb62017-01-14 17:11:11 -07001141
1142static int __init deadline_init(void)
1143{
Bart Van Assche855265c2021-06-11 16:48:32 -07001144 int ret;
1145
1146 ret = elv_register(&mq_deadline);
1147 if (ret)
1148 goto out;
1149 ret = dd_blkcg_init();
1150 if (ret)
1151 goto unreg;
1152
1153out:
1154 return ret;
1155
1156unreg:
1157 elv_unregister(&mq_deadline);
1158 goto out;
Jens Axboe945ffb62017-01-14 17:11:11 -07001159}
1160
1161static void __exit deadline_exit(void)
1162{
Bart Van Assche855265c2021-06-11 16:48:32 -07001163 dd_blkcg_exit();
Jens Axboe945ffb62017-01-14 17:11:11 -07001164 elv_unregister(&mq_deadline);
1165}
1166
1167module_init(deadline_init);
1168module_exit(deadline_exit);
1169
Bart Van Asschee3880a62021-05-19 14:47:00 -07001170MODULE_AUTHOR("Jens Axboe, Damien Le Moal and Bart Van Assche");
Jens Axboe945ffb62017-01-14 17:11:11 -07001171MODULE_LICENSE("GPL");
1172MODULE_DESCRIPTION("MQ deadline IO scheduler");