Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | // SPDX-License-Identifier: GPL-2.0 |
945ffb60 JA |
2 | /* |
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> | |
945ffb60 JA |
11 | #include <linux/bio.h> |
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/compiler.h> | |
16 | #include <linux/rbtree.h> | |
17 | #include <linux/sbitmap.h> | |
18 | ||
b357e4a6 CK |
19 | #include <trace/events/block.h> |
20 | ||
2e9bc346 | 21 | #include "elevator.h" |
945ffb60 JA |
22 | #include "blk.h" |
23 | #include "blk-mq.h" | |
daaadb3e | 24 | #include "blk-mq-debugfs.h" |
945ffb60 JA |
25 | #include "blk-mq-sched.h" |
26 | ||
27 | /* | |
898bd37a | 28 | * See Documentation/block/deadline-iosched.rst |
945ffb60 JA |
29 | */ |
30 | static const int read_expire = HZ / 2; /* max time before a read is submitted. */ | |
31 | static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */ | |
322cff70 BVA |
32 | /* |
33 | * Time after which to dispatch lower priority requests even if higher | |
34 | * priority requests are pending. | |
35 | */ | |
36 | static const int prio_aging_expire = 10 * HZ; | |
945ffb60 JA |
37 | static const int writes_starved = 2; /* max times reads can starve a write */ |
38 | static const int fifo_batch = 16; /* # of sequential requests treated as one | |
39 | by the above parameters. For throughput. */ | |
40 | ||
004a26b3 BVA |
41 | enum dd_data_dir { |
42 | DD_READ = READ, | |
43 | DD_WRITE = WRITE, | |
44 | }; | |
45 | ||
46 | enum { DD_DIR_COUNT = 2 }; | |
47 | ||
c807ab52 BVA |
48 | enum dd_prio { |
49 | DD_RT_PRIO = 0, | |
50 | DD_BE_PRIO = 1, | |
51 | DD_IDLE_PRIO = 2, | |
52 | DD_PRIO_MAX = 2, | |
53 | }; | |
54 | ||
55 | enum { DD_PRIO_COUNT = 3 }; | |
56 | ||
bce0363e BVA |
57 | /* |
58 | * I/O statistics per I/O priority. It is fine if these counters overflow. | |
59 | * What matters is that these counters are at least as wide as | |
60 | * log2(max_outstanding_requests). | |
61 | */ | |
0f783995 | 62 | struct io_stats_per_prio { |
bce0363e BVA |
63 | uint32_t inserted; |
64 | uint32_t merged; | |
65 | uint32_t dispatched; | |
66 | atomic_t completed; | |
38ba64d1 BVA |
67 | }; |
68 | ||
c807ab52 BVA |
69 | /* |
70 | * Deadline scheduler data per I/O priority (enum dd_prio). Requests are | |
71 | * present on both sort_list[] and fifo_list[]. | |
72 | */ | |
73 | struct dd_per_prio { | |
74 | struct list_head dispatch; | |
75 | struct rb_root sort_list[DD_DIR_COUNT]; | |
76 | struct list_head fifo_list[DD_DIR_COUNT]; | |
83c46ed6 BVA |
77 | /* Position of the most recently dispatched request. */ |
78 | sector_t latest_pos[DD_DIR_COUNT]; | |
bce0363e | 79 | struct io_stats_per_prio stats; |
c807ab52 BVA |
80 | }; |
81 | ||
945ffb60 JA |
82 | struct deadline_data { |
83 | /* | |
84 | * run time data | |
85 | */ | |
86 | ||
c807ab52 | 87 | struct dd_per_prio per_prio[DD_PRIO_COUNT]; |
945ffb60 | 88 | |
d672d325 BVA |
89 | /* Data direction of latest dispatched request. */ |
90 | enum dd_data_dir last_dir; | |
945ffb60 JA |
91 | unsigned int batching; /* number of sequential requests made */ |
92 | unsigned int starved; /* times reads have starved writes */ | |
93 | ||
94 | /* | |
95 | * settings that change how the i/o scheduler behaves | |
96 | */ | |
004a26b3 | 97 | int fifo_expire[DD_DIR_COUNT]; |
945ffb60 JA |
98 | int fifo_batch; |
99 | int writes_starved; | |
100 | int front_merges; | |
07757588 | 101 | u32 async_depth; |
322cff70 | 102 | int prio_aging_expire; |
945ffb60 JA |
103 | |
104 | spinlock_t lock; | |
5700f691 | 105 | spinlock_t zone_lock; |
c807ab52 BVA |
106 | }; |
107 | ||
108 | /* Maps an I/O priority class to a deadline scheduler priority. */ | |
109 | static const enum dd_prio ioprio_class_to_prio[] = { | |
110 | [IOPRIO_CLASS_NONE] = DD_BE_PRIO, | |
111 | [IOPRIO_CLASS_RT] = DD_RT_PRIO, | |
112 | [IOPRIO_CLASS_BE] = DD_BE_PRIO, | |
113 | [IOPRIO_CLASS_IDLE] = DD_IDLE_PRIO, | |
945ffb60 JA |
114 | }; |
115 | ||
116 | static inline struct rb_root * | |
c807ab52 | 117 | deadline_rb_root(struct dd_per_prio *per_prio, struct request *rq) |
945ffb60 | 118 | { |
c807ab52 BVA |
119 | return &per_prio->sort_list[rq_data_dir(rq)]; |
120 | } | |
121 | ||
122 | /* | |
123 | * Returns the I/O priority class (IOPRIO_CLASS_*) that has been assigned to a | |
124 | * request. | |
125 | */ | |
126 | static u8 dd_rq_ioclass(struct request *rq) | |
127 | { | |
128 | return IOPRIO_PRIO_CLASS(req_get_ioprio(rq)); | |
945ffb60 JA |
129 | } |
130 | ||
015d02f4 DLM |
131 | /* |
132 | * get the request before `rq' in sector-sorted order | |
133 | */ | |
134 | static inline struct request * | |
135 | deadline_earlier_request(struct request *rq) | |
136 | { | |
137 | struct rb_node *node = rb_prev(&rq->rb_node); | |
138 | ||
139 | if (node) | |
140 | return rb_entry_rq(node); | |
141 | ||
142 | return NULL; | |
143 | } | |
144 | ||
945ffb60 JA |
145 | /* |
146 | * get the request after `rq' in sector-sorted order | |
147 | */ | |
148 | static inline struct request * | |
149 | deadline_latter_request(struct request *rq) | |
150 | { | |
151 | struct rb_node *node = rb_next(&rq->rb_node); | |
152 | ||
153 | if (node) | |
154 | return rb_entry_rq(node); | |
155 | ||
156 | return NULL; | |
157 | } | |
158 | ||
83c46ed6 BVA |
159 | /* Return the first request for which blk_rq_pos() >= pos. */ |
160 | static inline struct request *deadline_from_pos(struct dd_per_prio *per_prio, | |
161 | enum dd_data_dir data_dir, sector_t pos) | |
162 | { | |
163 | struct rb_node *node = per_prio->sort_list[data_dir].rb_node; | |
164 | struct request *rq, *res = NULL; | |
165 | ||
166 | while (node) { | |
167 | rq = rb_entry_rq(node); | |
168 | if (blk_rq_pos(rq) >= pos) { | |
169 | res = rq; | |
170 | node = node->rb_left; | |
171 | } else { | |
172 | node = node->rb_right; | |
173 | } | |
174 | } | |
175 | return res; | |
176 | } | |
177 | ||
945ffb60 | 178 | static void |
c807ab52 | 179 | deadline_add_rq_rb(struct dd_per_prio *per_prio, struct request *rq) |
945ffb60 | 180 | { |
c807ab52 | 181 | struct rb_root *root = deadline_rb_root(per_prio, rq); |
945ffb60 JA |
182 | |
183 | elv_rb_add(root, rq); | |
184 | } | |
185 | ||
186 | static inline void | |
c807ab52 | 187 | deadline_del_rq_rb(struct dd_per_prio *per_prio, struct request *rq) |
945ffb60 | 188 | { |
c807ab52 | 189 | elv_rb_del(deadline_rb_root(per_prio, rq), rq); |
945ffb60 JA |
190 | } |
191 | ||
192 | /* | |
193 | * remove rq from rbtree and fifo. | |
194 | */ | |
c807ab52 BVA |
195 | static void deadline_remove_request(struct request_queue *q, |
196 | struct dd_per_prio *per_prio, | |
197 | struct request *rq) | |
945ffb60 | 198 | { |
945ffb60 JA |
199 | list_del_init(&rq->queuelist); |
200 | ||
201 | /* | |
202 | * We might not be on the rbtree, if we are doing an insert merge | |
203 | */ | |
204 | if (!RB_EMPTY_NODE(&rq->rb_node)) | |
c807ab52 | 205 | deadline_del_rq_rb(per_prio, rq); |
945ffb60 JA |
206 | |
207 | elv_rqhash_del(q, rq); | |
208 | if (q->last_merge == rq) | |
209 | q->last_merge = NULL; | |
210 | } | |
211 | ||
212 | static void dd_request_merged(struct request_queue *q, struct request *req, | |
34fe7c05 | 213 | enum elv_merge type) |
945ffb60 JA |
214 | { |
215 | struct deadline_data *dd = q->elevator->elevator_data; | |
c807ab52 BVA |
216 | const u8 ioprio_class = dd_rq_ioclass(req); |
217 | const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; | |
218 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; | |
945ffb60 JA |
219 | |
220 | /* | |
221 | * if the merge was a front merge, we need to reposition request | |
222 | */ | |
223 | if (type == ELEVATOR_FRONT_MERGE) { | |
c807ab52 BVA |
224 | elv_rb_del(deadline_rb_root(per_prio, req), req); |
225 | deadline_add_rq_rb(per_prio, req); | |
945ffb60 JA |
226 | } |
227 | } | |
228 | ||
46eae2e3 BVA |
229 | /* |
230 | * Callback function that is invoked after @next has been merged into @req. | |
231 | */ | |
945ffb60 JA |
232 | static void dd_merged_requests(struct request_queue *q, struct request *req, |
233 | struct request *next) | |
234 | { | |
38ba64d1 | 235 | struct deadline_data *dd = q->elevator->elevator_data; |
c807ab52 BVA |
236 | const u8 ioprio_class = dd_rq_ioclass(next); |
237 | const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; | |
238 | ||
bce0363e BVA |
239 | lockdep_assert_held(&dd->lock); |
240 | ||
241 | dd->per_prio[prio].stats.merged++; | |
38ba64d1 | 242 | |
945ffb60 JA |
243 | /* |
244 | * if next expires before rq, assign its expire time to rq | |
245 | * and move into next position (next will be deleted) in fifo | |
246 | */ | |
247 | if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) { | |
248 | if (time_before((unsigned long)next->fifo_time, | |
249 | (unsigned long)req->fifo_time)) { | |
250 | list_move(&req->queuelist, &next->queuelist); | |
251 | req->fifo_time = next->fifo_time; | |
252 | } | |
253 | } | |
254 | ||
255 | /* | |
256 | * kill knowledge of next, this one is a goner | |
257 | */ | |
c807ab52 | 258 | deadline_remove_request(q, &dd->per_prio[prio], next); |
945ffb60 JA |
259 | } |
260 | ||
261 | /* | |
262 | * move an entry to dispatch queue | |
263 | */ | |
264 | static void | |
c807ab52 BVA |
265 | deadline_move_request(struct deadline_data *dd, struct dd_per_prio *per_prio, |
266 | struct request *rq) | |
945ffb60 | 267 | { |
945ffb60 JA |
268 | /* |
269 | * take it off the sort and fifo list | |
270 | */ | |
c807ab52 | 271 | deadline_remove_request(rq->q, per_prio, rq); |
945ffb60 JA |
272 | } |
273 | ||
32f64cad BVA |
274 | /* Number of requests queued for a given priority level. */ |
275 | static u32 dd_queued(struct deadline_data *dd, enum dd_prio prio) | |
276 | { | |
bce0363e BVA |
277 | const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats; |
278 | ||
279 | lockdep_assert_held(&dd->lock); | |
280 | ||
281 | return stats->inserted - atomic_read(&stats->completed); | |
32f64cad BVA |
282 | } |
283 | ||
945ffb60 | 284 | /* |
e0d85cde BVA |
285 | * deadline_check_fifo returns true if and only if there are expired requests |
286 | * in the FIFO list. Requires !list_empty(&dd->fifo_list[data_dir]). | |
945ffb60 | 287 | */ |
e0d85cde BVA |
288 | static inline bool deadline_check_fifo(struct dd_per_prio *per_prio, |
289 | enum dd_data_dir data_dir) | |
945ffb60 | 290 | { |
c807ab52 | 291 | struct request *rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next); |
945ffb60 | 292 | |
e0d85cde | 293 | return time_is_before_eq_jiffies((unsigned long)rq->fifo_time); |
945ffb60 JA |
294 | } |
295 | ||
015d02f4 DLM |
296 | /* |
297 | * Check if rq has a sequential request preceding it. | |
298 | */ | |
3692fec8 | 299 | static bool deadline_is_seq_write(struct deadline_data *dd, struct request *rq) |
015d02f4 DLM |
300 | { |
301 | struct request *prev = deadline_earlier_request(rq); | |
302 | ||
303 | if (!prev) | |
304 | return false; | |
305 | ||
306 | return blk_rq_pos(prev) + blk_rq_sectors(prev) == blk_rq_pos(rq); | |
307 | } | |
308 | ||
309 | /* | |
310 | * Skip all write requests that are sequential from @rq, even if we cross | |
311 | * a zone boundary. | |
312 | */ | |
313 | static struct request *deadline_skip_seq_writes(struct deadline_data *dd, | |
314 | struct request *rq) | |
315 | { | |
316 | sector_t pos = blk_rq_pos(rq); | |
015d02f4 | 317 | |
3b463cbe BVA |
318 | do { |
319 | pos += blk_rq_sectors(rq); | |
015d02f4 | 320 | rq = deadline_latter_request(rq); |
3b463cbe | 321 | } while (rq && blk_rq_pos(rq) == pos); |
015d02f4 DLM |
322 | |
323 | return rq; | |
324 | } | |
325 | ||
bf09ce56 DLM |
326 | /* |
327 | * For the specified data direction, return the next request to | |
328 | * dispatch using arrival ordered lists. | |
329 | */ | |
330 | static struct request * | |
c807ab52 BVA |
331 | deadline_fifo_request(struct deadline_data *dd, struct dd_per_prio *per_prio, |
332 | enum dd_data_dir data_dir) | |
bf09ce56 | 333 | { |
5700f691 DLM |
334 | struct request *rq; |
335 | unsigned long flags; | |
336 | ||
c807ab52 | 337 | if (list_empty(&per_prio->fifo_list[data_dir])) |
bf09ce56 DLM |
338 | return NULL; |
339 | ||
c807ab52 | 340 | rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next); |
004a26b3 | 341 | if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q)) |
5700f691 DLM |
342 | return rq; |
343 | ||
344 | /* | |
345 | * Look for a write request that can be dispatched, that is one with | |
015d02f4 DLM |
346 | * an unlocked target zone. For some HDDs, breaking a sequential |
347 | * write stream can lead to lower throughput, so make sure to preserve | |
348 | * sequential write streams, even if that stream crosses into the next | |
349 | * zones and these zones are unlocked. | |
5700f691 DLM |
350 | */ |
351 | spin_lock_irqsave(&dd->zone_lock, flags); | |
c807ab52 | 352 | list_for_each_entry(rq, &per_prio->fifo_list[DD_WRITE], queuelist) { |
015d02f4 DLM |
353 | if (blk_req_can_dispatch_to_zone(rq) && |
354 | (blk_queue_nonrot(rq->q) || | |
3692fec8 | 355 | !deadline_is_seq_write(dd, rq))) |
5700f691 DLM |
356 | goto out; |
357 | } | |
358 | rq = NULL; | |
359 | out: | |
360 | spin_unlock_irqrestore(&dd->zone_lock, flags); | |
361 | ||
362 | return rq; | |
bf09ce56 DLM |
363 | } |
364 | ||
365 | /* | |
366 | * For the specified data direction, return the next request to | |
367 | * dispatch using sector position sorted lists. | |
368 | */ | |
369 | static struct request * | |
c807ab52 BVA |
370 | deadline_next_request(struct deadline_data *dd, struct dd_per_prio *per_prio, |
371 | enum dd_data_dir data_dir) | |
bf09ce56 | 372 | { |
5700f691 DLM |
373 | struct request *rq; |
374 | unsigned long flags; | |
375 | ||
83c46ed6 BVA |
376 | rq = deadline_from_pos(per_prio, data_dir, |
377 | per_prio->latest_pos[data_dir]); | |
5700f691 DLM |
378 | if (!rq) |
379 | return NULL; | |
380 | ||
004a26b3 | 381 | if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q)) |
5700f691 DLM |
382 | return rq; |
383 | ||
384 | /* | |
385 | * Look for a write request that can be dispatched, that is one with | |
015d02f4 DLM |
386 | * an unlocked target zone. For some HDDs, breaking a sequential |
387 | * write stream can lead to lower throughput, so make sure to preserve | |
388 | * sequential write streams, even if that stream crosses into the next | |
389 | * zones and these zones are unlocked. | |
5700f691 DLM |
390 | */ |
391 | spin_lock_irqsave(&dd->zone_lock, flags); | |
392 | while (rq) { | |
393 | if (blk_req_can_dispatch_to_zone(rq)) | |
394 | break; | |
015d02f4 DLM |
395 | if (blk_queue_nonrot(rq->q)) |
396 | rq = deadline_latter_request(rq); | |
397 | else | |
398 | rq = deadline_skip_seq_writes(dd, rq); | |
5700f691 DLM |
399 | } |
400 | spin_unlock_irqrestore(&dd->zone_lock, flags); | |
401 | ||
402 | return rq; | |
bf09ce56 DLM |
403 | } |
404 | ||
322cff70 BVA |
405 | /* |
406 | * Returns true if and only if @rq started after @latest_start where | |
407 | * @latest_start is in jiffies. | |
408 | */ | |
409 | static bool started_after(struct deadline_data *dd, struct request *rq, | |
410 | unsigned long latest_start) | |
411 | { | |
412 | unsigned long start_time = (unsigned long)rq->fifo_time; | |
413 | ||
414 | start_time -= dd->fifo_expire[rq_data_dir(rq)]; | |
415 | ||
416 | return time_after(start_time, latest_start); | |
417 | } | |
418 | ||
945ffb60 JA |
419 | /* |
420 | * deadline_dispatch_requests selects the best request according to | |
322cff70 | 421 | * read/write expire, fifo_batch, etc and with a start time <= @latest_start. |
945ffb60 | 422 | */ |
c807ab52 | 423 | static struct request *__dd_dispatch_request(struct deadline_data *dd, |
322cff70 BVA |
424 | struct dd_per_prio *per_prio, |
425 | unsigned long latest_start) | |
945ffb60 | 426 | { |
bf09ce56 | 427 | struct request *rq, *next_rq; |
004a26b3 | 428 | enum dd_data_dir data_dir; |
38ba64d1 BVA |
429 | enum dd_prio prio; |
430 | u8 ioprio_class; | |
945ffb60 | 431 | |
3bd473f4 BVA |
432 | lockdep_assert_held(&dd->lock); |
433 | ||
c807ab52 BVA |
434 | if (!list_empty(&per_prio->dispatch)) { |
435 | rq = list_first_entry(&per_prio->dispatch, struct request, | |
436 | queuelist); | |
322cff70 BVA |
437 | if (started_after(dd, rq, latest_start)) |
438 | return NULL; | |
945ffb60 | 439 | list_del_init(&rq->queuelist); |
83c46ed6 | 440 | data_dir = rq_data_dir(rq); |
945ffb60 JA |
441 | goto done; |
442 | } | |
443 | ||
945ffb60 JA |
444 | /* |
445 | * batches are currently reads XOR writes | |
446 | */ | |
c807ab52 | 447 | rq = deadline_next_request(dd, per_prio, dd->last_dir); |
83c46ed6 | 448 | if (rq && dd->batching < dd->fifo_batch) { |
45b46b6f | 449 | /* we have a next request and are still entitled to batch */ |
83c46ed6 | 450 | data_dir = rq_data_dir(rq); |
945ffb60 | 451 | goto dispatch_request; |
83c46ed6 | 452 | } |
945ffb60 JA |
453 | |
454 | /* | |
455 | * at this point we are not running a batch. select the appropriate | |
456 | * data direction (read / write) | |
457 | */ | |
458 | ||
c807ab52 BVA |
459 | if (!list_empty(&per_prio->fifo_list[DD_READ])) { |
460 | BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_READ])); | |
945ffb60 | 461 | |
c807ab52 | 462 | if (deadline_fifo_request(dd, per_prio, DD_WRITE) && |
5700f691 | 463 | (dd->starved++ >= dd->writes_starved)) |
945ffb60 JA |
464 | goto dispatch_writes; |
465 | ||
004a26b3 | 466 | data_dir = DD_READ; |
945ffb60 JA |
467 | |
468 | goto dispatch_find_request; | |
469 | } | |
470 | ||
471 | /* | |
472 | * there are either no reads or writes have been starved | |
473 | */ | |
474 | ||
c807ab52 | 475 | if (!list_empty(&per_prio->fifo_list[DD_WRITE])) { |
945ffb60 | 476 | dispatch_writes: |
c807ab52 | 477 | BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_WRITE])); |
945ffb60 JA |
478 | |
479 | dd->starved = 0; | |
480 | ||
004a26b3 | 481 | data_dir = DD_WRITE; |
945ffb60 JA |
482 | |
483 | goto dispatch_find_request; | |
484 | } | |
485 | ||
486 | return NULL; | |
487 | ||
488 | dispatch_find_request: | |
489 | /* | |
490 | * we are not running a batch, find best request for selected data_dir | |
491 | */ | |
c807ab52 BVA |
492 | next_rq = deadline_next_request(dd, per_prio, data_dir); |
493 | if (deadline_check_fifo(per_prio, data_dir) || !next_rq) { | |
945ffb60 JA |
494 | /* |
495 | * A deadline has expired, the last request was in the other | |
496 | * direction, or we have run out of higher-sectored requests. | |
497 | * Start again from the request with the earliest expiry time. | |
498 | */ | |
c807ab52 | 499 | rq = deadline_fifo_request(dd, per_prio, data_dir); |
945ffb60 JA |
500 | } else { |
501 | /* | |
502 | * The last req was the same dir and we have a next request in | |
503 | * sort order. No expired requests so continue on from here. | |
504 | */ | |
bf09ce56 | 505 | rq = next_rq; |
945ffb60 JA |
506 | } |
507 | ||
5700f691 DLM |
508 | /* |
509 | * For a zoned block device, if we only have writes queued and none of | |
510 | * them can be dispatched, rq will be NULL. | |
511 | */ | |
512 | if (!rq) | |
513 | return NULL; | |
514 | ||
d672d325 | 515 | dd->last_dir = data_dir; |
945ffb60 JA |
516 | dd->batching = 0; |
517 | ||
518 | dispatch_request: | |
322cff70 BVA |
519 | if (started_after(dd, rq, latest_start)) |
520 | return NULL; | |
521 | ||
945ffb60 JA |
522 | /* |
523 | * rq is the selected appropriate request. | |
524 | */ | |
525 | dd->batching++; | |
c807ab52 | 526 | deadline_move_request(dd, per_prio, rq); |
945ffb60 | 527 | done: |
38ba64d1 BVA |
528 | ioprio_class = dd_rq_ioclass(rq); |
529 | prio = ioprio_class_to_prio[ioprio_class]; | |
83c46ed6 | 530 | dd->per_prio[prio].latest_pos[data_dir] = blk_rq_pos(rq); |
bce0363e | 531 | dd->per_prio[prio].stats.dispatched++; |
5700f691 DLM |
532 | /* |
533 | * If the request needs its target zone locked, do it. | |
534 | */ | |
535 | blk_req_zone_write_lock(rq); | |
945ffb60 JA |
536 | rq->rq_flags |= RQF_STARTED; |
537 | return rq; | |
538 | } | |
539 | ||
322cff70 BVA |
540 | /* |
541 | * Check whether there are any requests with priority other than DD_RT_PRIO | |
542 | * that were inserted more than prio_aging_expire jiffies ago. | |
543 | */ | |
544 | static struct request *dd_dispatch_prio_aged_requests(struct deadline_data *dd, | |
545 | unsigned long now) | |
546 | { | |
547 | struct request *rq; | |
548 | enum dd_prio prio; | |
549 | int prio_cnt; | |
550 | ||
551 | lockdep_assert_held(&dd->lock); | |
552 | ||
553 | prio_cnt = !!dd_queued(dd, DD_RT_PRIO) + !!dd_queued(dd, DD_BE_PRIO) + | |
554 | !!dd_queued(dd, DD_IDLE_PRIO); | |
555 | if (prio_cnt < 2) | |
556 | return NULL; | |
557 | ||
558 | for (prio = DD_BE_PRIO; prio <= DD_PRIO_MAX; prio++) { | |
559 | rq = __dd_dispatch_request(dd, &dd->per_prio[prio], | |
560 | now - dd->prio_aging_expire); | |
561 | if (rq) | |
562 | return rq; | |
563 | } | |
564 | ||
565 | return NULL; | |
566 | } | |
567 | ||
ca11f209 | 568 | /* |
46eae2e3 BVA |
569 | * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests(). |
570 | * | |
ca11f209 | 571 | * One confusing aspect here is that we get called for a specific |
7211aef8 | 572 | * hardware queue, but we may return a request that is for a |
ca11f209 JA |
573 | * different hardware queue. This is because mq-deadline has shared |
574 | * state for all hardware queues, in terms of sorting, FIFOs, etc. | |
575 | */ | |
c13660a0 | 576 | static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx) |
945ffb60 JA |
577 | { |
578 | struct deadline_data *dd = hctx->queue->elevator->elevator_data; | |
322cff70 | 579 | const unsigned long now = jiffies; |
7b05bf77 | 580 | struct request *rq; |
c807ab52 | 581 | enum dd_prio prio; |
945ffb60 JA |
582 | |
583 | spin_lock(&dd->lock); | |
322cff70 BVA |
584 | rq = dd_dispatch_prio_aged_requests(dd, now); |
585 | if (rq) | |
586 | goto unlock; | |
587 | ||
588 | /* | |
589 | * Next, dispatch requests in priority order. Ignore lower priority | |
590 | * requests if any higher priority requests are pending. | |
591 | */ | |
fb926032 | 592 | for (prio = 0; prio <= DD_PRIO_MAX; prio++) { |
322cff70 BVA |
593 | rq = __dd_dispatch_request(dd, &dd->per_prio[prio], now); |
594 | if (rq || dd_queued(dd, prio)) | |
c807ab52 BVA |
595 | break; |
596 | } | |
322cff70 BVA |
597 | |
598 | unlock: | |
945ffb60 | 599 | spin_unlock(&dd->lock); |
c13660a0 JA |
600 | |
601 | return rq; | |
945ffb60 JA |
602 | } |
603 | ||
07757588 BVA |
604 | /* |
605 | * Called by __blk_mq_alloc_request(). The shallow_depth value set by this | |
606 | * function is used by __blk_mq_get_tag(). | |
607 | */ | |
f8359efe | 608 | static void dd_limit_depth(blk_opf_t opf, struct blk_mq_alloc_data *data) |
07757588 BVA |
609 | { |
610 | struct deadline_data *dd = data->q->elevator->elevator_data; | |
611 | ||
612 | /* Do not throttle synchronous reads. */ | |
f8359efe | 613 | if (op_is_sync(opf) && !op_is_write(opf)) |
07757588 BVA |
614 | return; |
615 | ||
616 | /* | |
617 | * Throttle asynchronous requests and writes such that these requests | |
618 | * do not block the allocation of synchronous requests. | |
619 | */ | |
620 | data->shallow_depth = dd->async_depth; | |
621 | } | |
622 | ||
623 | /* Called by blk_mq_update_nr_requests(). */ | |
624 | static void dd_depth_updated(struct blk_mq_hw_ctx *hctx) | |
625 | { | |
626 | struct request_queue *q = hctx->queue; | |
627 | struct deadline_data *dd = q->elevator->elevator_data; | |
628 | struct blk_mq_tags *tags = hctx->sched_tags; | |
629 | ||
630 | dd->async_depth = max(1UL, 3 * q->nr_requests / 4); | |
631 | ||
ae0f1a73 | 632 | sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, dd->async_depth); |
07757588 BVA |
633 | } |
634 | ||
635 | /* Called by blk_mq_init_hctx() and blk_mq_init_sched(). */ | |
636 | static int dd_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx) | |
637 | { | |
638 | dd_depth_updated(hctx); | |
639 | return 0; | |
640 | } | |
641 | ||
3e9a99eb | 642 | static void dd_exit_sched(struct elevator_queue *e) |
945ffb60 JA |
643 | { |
644 | struct deadline_data *dd = e->elevator_data; | |
c807ab52 | 645 | enum dd_prio prio; |
945ffb60 | 646 | |
c807ab52 BVA |
647 | for (prio = 0; prio <= DD_PRIO_MAX; prio++) { |
648 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; | |
bce0363e BVA |
649 | const struct io_stats_per_prio *stats = &per_prio->stats; |
650 | uint32_t queued; | |
c807ab52 BVA |
651 | |
652 | WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_READ])); | |
653 | WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_WRITE])); | |
bce0363e BVA |
654 | |
655 | spin_lock(&dd->lock); | |
656 | queued = dd_queued(dd, prio); | |
657 | spin_unlock(&dd->lock); | |
658 | ||
659 | WARN_ONCE(queued != 0, | |
32f64cad | 660 | "statistics for priority %d: i %u m %u d %u c %u\n", |
bce0363e BVA |
661 | prio, stats->inserted, stats->merged, |
662 | stats->dispatched, atomic_read(&stats->completed)); | |
c807ab52 | 663 | } |
945ffb60 JA |
664 | |
665 | kfree(dd); | |
666 | } | |
667 | ||
668 | /* | |
0f783995 | 669 | * initialize elevator private data (deadline_data). |
945ffb60 | 670 | */ |
3e9a99eb | 671 | static int dd_init_sched(struct request_queue *q, struct elevator_type *e) |
945ffb60 JA |
672 | { |
673 | struct deadline_data *dd; | |
674 | struct elevator_queue *eq; | |
c807ab52 BVA |
675 | enum dd_prio prio; |
676 | int ret = -ENOMEM; | |
945ffb60 JA |
677 | |
678 | eq = elevator_alloc(q, e); | |
679 | if (!eq) | |
c807ab52 | 680 | return ret; |
945ffb60 JA |
681 | |
682 | dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node); | |
c807ab52 BVA |
683 | if (!dd) |
684 | goto put_eq; | |
685 | ||
945ffb60 JA |
686 | eq->elevator_data = dd; |
687 | ||
c807ab52 BVA |
688 | for (prio = 0; prio <= DD_PRIO_MAX; prio++) { |
689 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; | |
690 | ||
691 | INIT_LIST_HEAD(&per_prio->dispatch); | |
692 | INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]); | |
693 | INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]); | |
694 | per_prio->sort_list[DD_READ] = RB_ROOT; | |
695 | per_prio->sort_list[DD_WRITE] = RB_ROOT; | |
696 | } | |
004a26b3 BVA |
697 | dd->fifo_expire[DD_READ] = read_expire; |
698 | dd->fifo_expire[DD_WRITE] = write_expire; | |
945ffb60 JA |
699 | dd->writes_starved = writes_starved; |
700 | dd->front_merges = 1; | |
d672d325 | 701 | dd->last_dir = DD_WRITE; |
945ffb60 | 702 | dd->fifo_batch = fifo_batch; |
322cff70 | 703 | dd->prio_aging_expire = prio_aging_expire; |
945ffb60 | 704 | spin_lock_init(&dd->lock); |
5700f691 | 705 | spin_lock_init(&dd->zone_lock); |
945ffb60 | 706 | |
4d337ceb ML |
707 | /* We dispatch from request queue wide instead of hw queue */ |
708 | blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q); | |
709 | ||
945ffb60 JA |
710 | q->elevator = eq; |
711 | return 0; | |
c807ab52 BVA |
712 | |
713 | put_eq: | |
714 | kobject_put(&eq->kobj); | |
715 | return ret; | |
945ffb60 JA |
716 | } |
717 | ||
46eae2e3 BVA |
718 | /* |
719 | * Try to merge @bio into an existing request. If @bio has been merged into | |
720 | * an existing request, store the pointer to that request into *@rq. | |
721 | */ | |
945ffb60 JA |
722 | static int dd_request_merge(struct request_queue *q, struct request **rq, |
723 | struct bio *bio) | |
724 | { | |
725 | struct deadline_data *dd = q->elevator->elevator_data; | |
c807ab52 BVA |
726 | const u8 ioprio_class = IOPRIO_PRIO_CLASS(bio->bi_ioprio); |
727 | const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; | |
728 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; | |
945ffb60 JA |
729 | sector_t sector = bio_end_sector(bio); |
730 | struct request *__rq; | |
731 | ||
732 | if (!dd->front_merges) | |
733 | return ELEVATOR_NO_MERGE; | |
734 | ||
c807ab52 | 735 | __rq = elv_rb_find(&per_prio->sort_list[bio_data_dir(bio)], sector); |
945ffb60 JA |
736 | if (__rq) { |
737 | BUG_ON(sector != blk_rq_pos(__rq)); | |
738 | ||
739 | if (elv_bio_merge_ok(__rq, bio)) { | |
740 | *rq = __rq; | |
866663b7 ML |
741 | if (blk_discard_mergable(__rq)) |
742 | return ELEVATOR_DISCARD_MERGE; | |
945ffb60 JA |
743 | return ELEVATOR_FRONT_MERGE; |
744 | } | |
745 | } | |
746 | ||
747 | return ELEVATOR_NO_MERGE; | |
748 | } | |
749 | ||
46eae2e3 BVA |
750 | /* |
751 | * Attempt to merge a bio into an existing request. This function is called | |
752 | * before @bio is associated with a request. | |
753 | */ | |
efed9a33 | 754 | static bool dd_bio_merge(struct request_queue *q, struct bio *bio, |
14ccb66b | 755 | unsigned int nr_segs) |
945ffb60 | 756 | { |
945ffb60 | 757 | struct deadline_data *dd = q->elevator->elevator_data; |
e4d750c9 JA |
758 | struct request *free = NULL; |
759 | bool ret; | |
945ffb60 JA |
760 | |
761 | spin_lock(&dd->lock); | |
14ccb66b | 762 | ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free); |
945ffb60 JA |
763 | spin_unlock(&dd->lock); |
764 | ||
e4d750c9 JA |
765 | if (free) |
766 | blk_mq_free_request(free); | |
767 | ||
945ffb60 JA |
768 | return ret; |
769 | } | |
770 | ||
771 | /* | |
772 | * add rq to rbtree and fifo | |
773 | */ | |
774 | static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, | |
b2097bd2 | 775 | blk_insert_t flags, struct list_head *free) |
945ffb60 JA |
776 | { |
777 | struct request_queue *q = hctx->queue; | |
778 | struct deadline_data *dd = q->elevator->elevator_data; | |
004a26b3 | 779 | const enum dd_data_dir data_dir = rq_data_dir(rq); |
c807ab52 BVA |
780 | u16 ioprio = req_get_ioprio(rq); |
781 | u8 ioprio_class = IOPRIO_PRIO_CLASS(ioprio); | |
782 | struct dd_per_prio *per_prio; | |
783 | enum dd_prio prio; | |
945ffb60 | 784 | |
3bd473f4 BVA |
785 | lockdep_assert_held(&dd->lock); |
786 | ||
5700f691 DLM |
787 | /* |
788 | * This may be a requeue of a write request that has locked its | |
789 | * target zone. If it is the case, this releases the zone lock. | |
790 | */ | |
791 | blk_req_zone_write_unlock(rq); | |
792 | ||
c807ab52 | 793 | prio = ioprio_class_to_prio[ioprio_class]; |
bce0363e | 794 | per_prio = &dd->per_prio[prio]; |
e2c7275d | 795 | if (!rq->elv.priv[0]) { |
bce0363e | 796 | per_prio->stats.inserted++; |
e2c7275d BVA |
797 | rq->elv.priv[0] = (void *)(uintptr_t)1; |
798 | } | |
c807ab52 | 799 | |
b2097bd2 | 800 | if (blk_mq_sched_try_insert_merge(q, rq, free)) |
945ffb60 JA |
801 | return; |
802 | ||
b357e4a6 | 803 | trace_block_rq_insert(rq); |
945ffb60 | 804 | |
93fffe16 | 805 | if (flags & BLK_MQ_INSERT_AT_HEAD) { |
c807ab52 | 806 | list_add(&rq->queuelist, &per_prio->dispatch); |
725f22a1 | 807 | rq->fifo_time = jiffies; |
945ffb60 | 808 | } else { |
c807ab52 | 809 | deadline_add_rq_rb(per_prio, rq); |
945ffb60 JA |
810 | |
811 | if (rq_mergeable(rq)) { | |
812 | elv_rqhash_add(q, rq); | |
813 | if (!q->last_merge) | |
814 | q->last_merge = rq; | |
815 | } | |
816 | ||
817 | /* | |
818 | * set expire time and add to fifo list | |
819 | */ | |
820 | rq->fifo_time = jiffies + dd->fifo_expire[data_dir]; | |
c807ab52 | 821 | list_add_tail(&rq->queuelist, &per_prio->fifo_list[data_dir]); |
945ffb60 JA |
822 | } |
823 | } | |
824 | ||
46eae2e3 | 825 | /* |
2bd215df | 826 | * Called from blk_mq_insert_request() or blk_mq_dispatch_plug_list(). |
46eae2e3 | 827 | */ |
945ffb60 | 828 | static void dd_insert_requests(struct blk_mq_hw_ctx *hctx, |
93fffe16 CH |
829 | struct list_head *list, |
830 | blk_insert_t flags) | |
945ffb60 JA |
831 | { |
832 | struct request_queue *q = hctx->queue; | |
833 | struct deadline_data *dd = q->elevator->elevator_data; | |
b2097bd2 | 834 | LIST_HEAD(free); |
945ffb60 JA |
835 | |
836 | spin_lock(&dd->lock); | |
837 | while (!list_empty(list)) { | |
838 | struct request *rq; | |
839 | ||
840 | rq = list_first_entry(list, struct request, queuelist); | |
841 | list_del_init(&rq->queuelist); | |
b2097bd2 | 842 | dd_insert_request(hctx, rq, flags, &free); |
945ffb60 JA |
843 | } |
844 | spin_unlock(&dd->lock); | |
b2097bd2 BVA |
845 | |
846 | blk_mq_free_requests(&free); | |
945ffb60 JA |
847 | } |
848 | ||
b6d2b054 | 849 | /* Callback from inside blk_mq_rq_ctx_init(). */ |
5d9c305b | 850 | static void dd_prepare_request(struct request *rq) |
f3bc78d2 | 851 | { |
b6d2b054 | 852 | rq->elv.priv[0] = NULL; |
f3bc78d2 DLM |
853 | } |
854 | ||
2820e5d0 DLM |
855 | static bool dd_has_write_work(struct blk_mq_hw_ctx *hctx) |
856 | { | |
857 | struct deadline_data *dd = hctx->queue->elevator->elevator_data; | |
858 | enum dd_prio p; | |
859 | ||
860 | for (p = 0; p <= DD_PRIO_MAX; p++) | |
861 | if (!list_empty_careful(&dd->per_prio[p].fifo_list[DD_WRITE])) | |
862 | return true; | |
863 | ||
864 | return false; | |
865 | } | |
866 | ||
5700f691 | 867 | /* |
46eae2e3 BVA |
868 | * Callback from inside blk_mq_free_request(). |
869 | * | |
5700f691 DLM |
870 | * For zoned block devices, write unlock the target zone of |
871 | * completed write requests. Do this while holding the zone lock | |
872 | * spinlock so that the zone is never unlocked while deadline_fifo_request() | |
f3bc78d2 DLM |
873 | * or deadline_next_request() are executing. This function is called for |
874 | * all requests, whether or not these requests complete successfully. | |
cb8acabb DLM |
875 | * |
876 | * For a zoned block device, __dd_dispatch_request() may have stopped | |
877 | * dispatching requests if all the queued requests are write requests directed | |
878 | * at zones that are already locked due to on-going write requests. To ensure | |
879 | * write request dispatch progress in this case, mark the queue as needing a | |
880 | * restart to ensure that the queue is run again after completion of the | |
881 | * request and zones being unlocked. | |
5700f691 | 882 | */ |
f3bc78d2 | 883 | static void dd_finish_request(struct request *rq) |
5700f691 DLM |
884 | { |
885 | struct request_queue *q = rq->q; | |
c807ab52 BVA |
886 | struct deadline_data *dd = q->elevator->elevator_data; |
887 | const u8 ioprio_class = dd_rq_ioclass(rq); | |
888 | const enum dd_prio prio = ioprio_class_to_prio[ioprio_class]; | |
889 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; | |
5700f691 | 890 | |
b6d2b054 BVA |
891 | /* |
892 | * The block layer core may call dd_finish_request() without having | |
e2c7275d BVA |
893 | * called dd_insert_requests(). Skip requests that bypassed I/O |
894 | * scheduling. See also blk_mq_request_bypass_insert(). | |
b6d2b054 | 895 | */ |
e2c7275d BVA |
896 | if (!rq->elv.priv[0]) |
897 | return; | |
898 | ||
bce0363e | 899 | atomic_inc(&per_prio->stats.completed); |
38ba64d1 | 900 | |
5700f691 | 901 | if (blk_queue_is_zoned(q)) { |
5700f691 DLM |
902 | unsigned long flags; |
903 | ||
904 | spin_lock_irqsave(&dd->zone_lock, flags); | |
905 | blk_req_zone_write_unlock(rq); | |
906 | spin_unlock_irqrestore(&dd->zone_lock, flags); | |
2820e5d0 DLM |
907 | |
908 | if (dd_has_write_work(rq->mq_hctx)) | |
909 | blk_mq_sched_mark_restart_hctx(rq->mq_hctx); | |
5700f691 DLM |
910 | } |
911 | } | |
912 | ||
c807ab52 BVA |
913 | static bool dd_has_work_for_prio(struct dd_per_prio *per_prio) |
914 | { | |
915 | return !list_empty_careful(&per_prio->dispatch) || | |
916 | !list_empty_careful(&per_prio->fifo_list[DD_READ]) || | |
917 | !list_empty_careful(&per_prio->fifo_list[DD_WRITE]); | |
918 | } | |
919 | ||
945ffb60 JA |
920 | static bool dd_has_work(struct blk_mq_hw_ctx *hctx) |
921 | { | |
922 | struct deadline_data *dd = hctx->queue->elevator->elevator_data; | |
c807ab52 BVA |
923 | enum dd_prio prio; |
924 | ||
925 | for (prio = 0; prio <= DD_PRIO_MAX; prio++) | |
926 | if (dd_has_work_for_prio(&dd->per_prio[prio])) | |
927 | return true; | |
945ffb60 | 928 | |
c807ab52 | 929 | return false; |
945ffb60 JA |
930 | } |
931 | ||
932 | /* | |
933 | * sysfs parts below | |
934 | */ | |
d6d7f013 | 935 | #define SHOW_INT(__FUNC, __VAR) \ |
945ffb60 JA |
936 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
937 | { \ | |
938 | struct deadline_data *dd = e->elevator_data; \ | |
d6d7f013 BVA |
939 | \ |
940 | return sysfs_emit(page, "%d\n", __VAR); \ | |
945ffb60 | 941 | } |
d6d7f013 BVA |
942 | #define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR)) |
943 | SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]); | |
944 | SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]); | |
322cff70 | 945 | SHOW_JIFFIES(deadline_prio_aging_expire_show, dd->prio_aging_expire); |
d6d7f013 BVA |
946 | SHOW_INT(deadline_writes_starved_show, dd->writes_starved); |
947 | SHOW_INT(deadline_front_merges_show, dd->front_merges); | |
46cdc45a | 948 | SHOW_INT(deadline_async_depth_show, dd->async_depth); |
d6d7f013 BVA |
949 | SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch); |
950 | #undef SHOW_INT | |
951 | #undef SHOW_JIFFIES | |
945ffb60 JA |
952 | |
953 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ | |
954 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ | |
955 | { \ | |
956 | struct deadline_data *dd = e->elevator_data; \ | |
d6d7f013 BVA |
957 | int __data, __ret; \ |
958 | \ | |
959 | __ret = kstrtoint(page, 0, &__data); \ | |
960 | if (__ret < 0) \ | |
961 | return __ret; \ | |
945ffb60 JA |
962 | if (__data < (MIN)) \ |
963 | __data = (MIN); \ | |
964 | else if (__data > (MAX)) \ | |
965 | __data = (MAX); \ | |
d6d7f013 | 966 | *(__PTR) = __CONV(__data); \ |
235f8da1 | 967 | return count; \ |
945ffb60 | 968 | } |
d6d7f013 BVA |
969 | #define STORE_INT(__FUNC, __PTR, MIN, MAX) \ |
970 | STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, ) | |
971 | #define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \ | |
972 | STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies) | |
973 | STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX); | |
974 | STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX); | |
322cff70 | 975 | STORE_JIFFIES(deadline_prio_aging_expire_store, &dd->prio_aging_expire, 0, INT_MAX); |
d6d7f013 BVA |
976 | STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX); |
977 | STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1); | |
46cdc45a | 978 | STORE_INT(deadline_async_depth_store, &dd->async_depth, 1, INT_MAX); |
d6d7f013 | 979 | STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX); |
945ffb60 | 980 | #undef STORE_FUNCTION |
d6d7f013 BVA |
981 | #undef STORE_INT |
982 | #undef STORE_JIFFIES | |
945ffb60 JA |
983 | |
984 | #define DD_ATTR(name) \ | |
5657a819 | 985 | __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store) |
945ffb60 JA |
986 | |
987 | static struct elv_fs_entry deadline_attrs[] = { | |
988 | DD_ATTR(read_expire), | |
989 | DD_ATTR(write_expire), | |
990 | DD_ATTR(writes_starved), | |
991 | DD_ATTR(front_merges), | |
07757588 | 992 | DD_ATTR(async_depth), |
945ffb60 | 993 | DD_ATTR(fifo_batch), |
322cff70 | 994 | DD_ATTR(prio_aging_expire), |
945ffb60 JA |
995 | __ATTR_NULL |
996 | }; | |
997 | ||
daaadb3e | 998 | #ifdef CONFIG_BLK_DEBUG_FS |
c807ab52 | 999 | #define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \ |
daaadb3e OS |
1000 | static void *deadline_##name##_fifo_start(struct seq_file *m, \ |
1001 | loff_t *pos) \ | |
1002 | __acquires(&dd->lock) \ | |
1003 | { \ | |
1004 | struct request_queue *q = m->private; \ | |
1005 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
c807ab52 | 1006 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ |
daaadb3e OS |
1007 | \ |
1008 | spin_lock(&dd->lock); \ | |
c807ab52 | 1009 | return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \ |
daaadb3e OS |
1010 | } \ |
1011 | \ | |
1012 | static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \ | |
1013 | loff_t *pos) \ | |
1014 | { \ | |
1015 | struct request_queue *q = m->private; \ | |
1016 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
c807ab52 | 1017 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ |
daaadb3e | 1018 | \ |
c807ab52 | 1019 | return seq_list_next(v, &per_prio->fifo_list[data_dir], pos); \ |
daaadb3e OS |
1020 | } \ |
1021 | \ | |
1022 | static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \ | |
1023 | __releases(&dd->lock) \ | |
1024 | { \ | |
1025 | struct request_queue *q = m->private; \ | |
1026 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
1027 | \ | |
1028 | spin_unlock(&dd->lock); \ | |
1029 | } \ | |
1030 | \ | |
1031 | static const struct seq_operations deadline_##name##_fifo_seq_ops = { \ | |
1032 | .start = deadline_##name##_fifo_start, \ | |
1033 | .next = deadline_##name##_fifo_next, \ | |
1034 | .stop = deadline_##name##_fifo_stop, \ | |
1035 | .show = blk_mq_debugfs_rq_show, \ | |
1036 | }; \ | |
1037 | \ | |
1038 | static int deadline_##name##_next_rq_show(void *data, \ | |
1039 | struct seq_file *m) \ | |
1040 | { \ | |
1041 | struct request_queue *q = data; \ | |
1042 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
c807ab52 | 1043 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ |
83c46ed6 | 1044 | struct request *rq; \ |
daaadb3e | 1045 | \ |
83c46ed6 BVA |
1046 | rq = deadline_from_pos(per_prio, data_dir, \ |
1047 | per_prio->latest_pos[data_dir]); \ | |
daaadb3e OS |
1048 | if (rq) \ |
1049 | __blk_mq_debugfs_rq_show(m, rq); \ | |
1050 | return 0; \ | |
1051 | } | |
c807ab52 BVA |
1052 | |
1053 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_READ, read0); | |
1054 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_WRITE, write0); | |
1055 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_READ, read1); | |
1056 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_WRITE, write1); | |
1057 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_READ, read2); | |
1058 | DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_WRITE, write2); | |
daaadb3e OS |
1059 | #undef DEADLINE_DEBUGFS_DDIR_ATTRS |
1060 | ||
1061 | static int deadline_batching_show(void *data, struct seq_file *m) | |
1062 | { | |
1063 | struct request_queue *q = data; | |
1064 | struct deadline_data *dd = q->elevator->elevator_data; | |
1065 | ||
1066 | seq_printf(m, "%u\n", dd->batching); | |
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | static int deadline_starved_show(void *data, struct seq_file *m) | |
1071 | { | |
1072 | struct request_queue *q = data; | |
1073 | struct deadline_data *dd = q->elevator->elevator_data; | |
1074 | ||
1075 | seq_printf(m, "%u\n", dd->starved); | |
1076 | return 0; | |
1077 | } | |
1078 | ||
07757588 BVA |
1079 | static int dd_async_depth_show(void *data, struct seq_file *m) |
1080 | { | |
1081 | struct request_queue *q = data; | |
1082 | struct deadline_data *dd = q->elevator->elevator_data; | |
1083 | ||
1084 | seq_printf(m, "%u\n", dd->async_depth); | |
1085 | return 0; | |
1086 | } | |
1087 | ||
38ba64d1 BVA |
1088 | static int dd_queued_show(void *data, struct seq_file *m) |
1089 | { | |
1090 | struct request_queue *q = data; | |
1091 | struct deadline_data *dd = q->elevator->elevator_data; | |
bce0363e BVA |
1092 | u32 rt, be, idle; |
1093 | ||
1094 | spin_lock(&dd->lock); | |
1095 | rt = dd_queued(dd, DD_RT_PRIO); | |
1096 | be = dd_queued(dd, DD_BE_PRIO); | |
1097 | idle = dd_queued(dd, DD_IDLE_PRIO); | |
1098 | spin_unlock(&dd->lock); | |
1099 | ||
1100 | seq_printf(m, "%u %u %u\n", rt, be, idle); | |
38ba64d1 | 1101 | |
38ba64d1 BVA |
1102 | return 0; |
1103 | } | |
1104 | ||
1105 | /* Number of requests owned by the block driver for a given priority. */ | |
1106 | static u32 dd_owned_by_driver(struct deadline_data *dd, enum dd_prio prio) | |
1107 | { | |
bce0363e BVA |
1108 | const struct io_stats_per_prio *stats = &dd->per_prio[prio].stats; |
1109 | ||
1110 | lockdep_assert_held(&dd->lock); | |
1111 | ||
1112 | return stats->dispatched + stats->merged - | |
1113 | atomic_read(&stats->completed); | |
38ba64d1 BVA |
1114 | } |
1115 | ||
1116 | static int dd_owned_by_driver_show(void *data, struct seq_file *m) | |
1117 | { | |
1118 | struct request_queue *q = data; | |
1119 | struct deadline_data *dd = q->elevator->elevator_data; | |
bce0363e BVA |
1120 | u32 rt, be, idle; |
1121 | ||
1122 | spin_lock(&dd->lock); | |
1123 | rt = dd_owned_by_driver(dd, DD_RT_PRIO); | |
1124 | be = dd_owned_by_driver(dd, DD_BE_PRIO); | |
1125 | idle = dd_owned_by_driver(dd, DD_IDLE_PRIO); | |
1126 | spin_unlock(&dd->lock); | |
1127 | ||
1128 | seq_printf(m, "%u %u %u\n", rt, be, idle); | |
38ba64d1 | 1129 | |
38ba64d1 BVA |
1130 | return 0; |
1131 | } | |
1132 | ||
c807ab52 BVA |
1133 | #define DEADLINE_DISPATCH_ATTR(prio) \ |
1134 | static void *deadline_dispatch##prio##_start(struct seq_file *m, \ | |
1135 | loff_t *pos) \ | |
1136 | __acquires(&dd->lock) \ | |
1137 | { \ | |
1138 | struct request_queue *q = m->private; \ | |
1139 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
1140 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ | |
1141 | \ | |
1142 | spin_lock(&dd->lock); \ | |
1143 | return seq_list_start(&per_prio->dispatch, *pos); \ | |
1144 | } \ | |
1145 | \ | |
1146 | static void *deadline_dispatch##prio##_next(struct seq_file *m, \ | |
1147 | void *v, loff_t *pos) \ | |
1148 | { \ | |
1149 | struct request_queue *q = m->private; \ | |
1150 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
1151 | struct dd_per_prio *per_prio = &dd->per_prio[prio]; \ | |
1152 | \ | |
1153 | return seq_list_next(v, &per_prio->dispatch, pos); \ | |
1154 | } \ | |
1155 | \ | |
1156 | static void deadline_dispatch##prio##_stop(struct seq_file *m, void *v) \ | |
1157 | __releases(&dd->lock) \ | |
1158 | { \ | |
1159 | struct request_queue *q = m->private; \ | |
1160 | struct deadline_data *dd = q->elevator->elevator_data; \ | |
1161 | \ | |
1162 | spin_unlock(&dd->lock); \ | |
1163 | } \ | |
1164 | \ | |
1165 | static const struct seq_operations deadline_dispatch##prio##_seq_ops = { \ | |
1166 | .start = deadline_dispatch##prio##_start, \ | |
1167 | .next = deadline_dispatch##prio##_next, \ | |
1168 | .stop = deadline_dispatch##prio##_stop, \ | |
1169 | .show = blk_mq_debugfs_rq_show, \ | |
daaadb3e OS |
1170 | } |
1171 | ||
c807ab52 BVA |
1172 | DEADLINE_DISPATCH_ATTR(0); |
1173 | DEADLINE_DISPATCH_ATTR(1); | |
1174 | DEADLINE_DISPATCH_ATTR(2); | |
1175 | #undef DEADLINE_DISPATCH_ATTR | |
daaadb3e | 1176 | |
c807ab52 BVA |
1177 | #define DEADLINE_QUEUE_DDIR_ATTRS(name) \ |
1178 | {#name "_fifo_list", 0400, \ | |
1179 | .seq_ops = &deadline_##name##_fifo_seq_ops} | |
1180 | #define DEADLINE_NEXT_RQ_ATTR(name) \ | |
daaadb3e OS |
1181 | {#name "_next_rq", 0400, deadline_##name##_next_rq_show} |
1182 | static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = { | |
c807ab52 BVA |
1183 | DEADLINE_QUEUE_DDIR_ATTRS(read0), |
1184 | DEADLINE_QUEUE_DDIR_ATTRS(write0), | |
1185 | DEADLINE_QUEUE_DDIR_ATTRS(read1), | |
1186 | DEADLINE_QUEUE_DDIR_ATTRS(write1), | |
1187 | DEADLINE_QUEUE_DDIR_ATTRS(read2), | |
1188 | DEADLINE_QUEUE_DDIR_ATTRS(write2), | |
1189 | DEADLINE_NEXT_RQ_ATTR(read0), | |
1190 | DEADLINE_NEXT_RQ_ATTR(write0), | |
1191 | DEADLINE_NEXT_RQ_ATTR(read1), | |
1192 | DEADLINE_NEXT_RQ_ATTR(write1), | |
1193 | DEADLINE_NEXT_RQ_ATTR(read2), | |
1194 | DEADLINE_NEXT_RQ_ATTR(write2), | |
daaadb3e OS |
1195 | {"batching", 0400, deadline_batching_show}, |
1196 | {"starved", 0400, deadline_starved_show}, | |
07757588 | 1197 | {"async_depth", 0400, dd_async_depth_show}, |
c807ab52 BVA |
1198 | {"dispatch0", 0400, .seq_ops = &deadline_dispatch0_seq_ops}, |
1199 | {"dispatch1", 0400, .seq_ops = &deadline_dispatch1_seq_ops}, | |
1200 | {"dispatch2", 0400, .seq_ops = &deadline_dispatch2_seq_ops}, | |
38ba64d1 BVA |
1201 | {"owned_by_driver", 0400, dd_owned_by_driver_show}, |
1202 | {"queued", 0400, dd_queued_show}, | |
daaadb3e OS |
1203 | {}, |
1204 | }; | |
1205 | #undef DEADLINE_QUEUE_DDIR_ATTRS | |
1206 | #endif | |
1207 | ||
945ffb60 | 1208 | static struct elevator_type mq_deadline = { |
f9cd4bfe | 1209 | .ops = { |
07757588 BVA |
1210 | .depth_updated = dd_depth_updated, |
1211 | .limit_depth = dd_limit_depth, | |
945ffb60 | 1212 | .insert_requests = dd_insert_requests, |
c13660a0 | 1213 | .dispatch_request = dd_dispatch_request, |
f3bc78d2 DLM |
1214 | .prepare_request = dd_prepare_request, |
1215 | .finish_request = dd_finish_request, | |
945ffb60 JA |
1216 | .next_request = elv_rb_latter_request, |
1217 | .former_request = elv_rb_former_request, | |
1218 | .bio_merge = dd_bio_merge, | |
1219 | .request_merge = dd_request_merge, | |
1220 | .requests_merged = dd_merged_requests, | |
1221 | .request_merged = dd_request_merged, | |
1222 | .has_work = dd_has_work, | |
3e9a99eb BVA |
1223 | .init_sched = dd_init_sched, |
1224 | .exit_sched = dd_exit_sched, | |
07757588 | 1225 | .init_hctx = dd_init_hctx, |
945ffb60 JA |
1226 | }, |
1227 | ||
daaadb3e OS |
1228 | #ifdef CONFIG_BLK_DEBUG_FS |
1229 | .queue_debugfs_attrs = deadline_queue_debugfs_attrs, | |
1230 | #endif | |
945ffb60 JA |
1231 | .elevator_attrs = deadline_attrs, |
1232 | .elevator_name = "mq-deadline", | |
4d740bc9 | 1233 | .elevator_alias = "deadline", |
68c43f13 | 1234 | .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE, |
945ffb60 JA |
1235 | .elevator_owner = THIS_MODULE, |
1236 | }; | |
7de967e7 | 1237 | MODULE_ALIAS("mq-deadline-iosched"); |
945ffb60 JA |
1238 | |
1239 | static int __init deadline_init(void) | |
1240 | { | |
0f783995 | 1241 | return elv_register(&mq_deadline); |
945ffb60 JA |
1242 | } |
1243 | ||
1244 | static void __exit deadline_exit(void) | |
1245 | { | |
1246 | elv_unregister(&mq_deadline); | |
1247 | } | |
1248 | ||
1249 | module_init(deadline_init); | |
1250 | module_exit(deadline_exit); | |
1251 | ||
c807ab52 | 1252 | MODULE_AUTHOR("Jens Axboe, Damien Le Moal and Bart Van Assche"); |
945ffb60 JA |
1253 | MODULE_LICENSE("GPL"); |
1254 | MODULE_DESCRIPTION("MQ deadline IO scheduler"); |