1 // SPDX-License-Identifier: GPL-2.0
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>
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"
23 #include "blk-mq-debugfs.h"
24 #include "blk-mq-tag.h"
25 #include "blk-mq-sched.h"
26 #include "mq-deadline-cgroup.h"
27
28 /*
29 * See Documentation/block/deadline-iosched.rst
30 */
31 static const int read_expire = HZ / 2; /* max time before a read is submitted. */
32 static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
33 /*
34 * Time after which to dispatch lower priority requests even if higher
35 * priority requests are pending.
36 */
37 static const int aging_expire = 10 * HZ;
38 static const int writes_starved = 2; /* max times reads can starve a write */
39 static const int fifo_batch = 16; /* # of sequential requests treated as one
40 by the above parameters. For throughput. */
41
42 enum dd_data_dir {
43 DD_READ = READ,
44 DD_WRITE = WRITE,
45 };
46
47 enum { DD_DIR_COUNT = 2 };
48
49 enum dd_prio {
50 DD_RT_PRIO = 0,
51 DD_BE_PRIO = 1,
52 DD_IDLE_PRIO = 2,
53 DD_PRIO_MAX = 2,
54 };
55
56 enum { DD_PRIO_COUNT = 3 };
57
58 /* I/O statistics for all I/O priorities (enum dd_prio). */
59 struct io_stats {
60 struct io_stats_per_prio stats[DD_PRIO_COUNT];
61 };
62
63 /*
64 * Deadline scheduler data per I/O priority (enum dd_prio). Requests are
65 * present on both sort_list[] and fifo_list[].
66 */
67 struct 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
75 struct deadline_data {
76 /*
77 * run time data
78 */
79
80 /* Request queue that owns this data structure. */
81 struct request_queue *queue;
82
83 struct dd_per_prio per_prio[DD_PRIO_COUNT];
84
85 /* Data direction of latest dispatched request. */
86 enum dd_data_dir last_dir;
87 unsigned int batching; /* number of sequential requests made */
88 unsigned int starved; /* times reads have starved writes */
89
90 struct io_stats __percpu *stats;
91
92 /*
93 * settings that change how the i/o scheduler behaves
94 */
95 int fifo_expire[DD_DIR_COUNT];
96 int fifo_batch;
97 int writes_starved;
98 int front_merges;
99 u32 async_depth;
100 int aging_expire;
101
102 spinlock_t lock;
103 spinlock_t zone_lock;
104 };
105
106 /* 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
133 /* Maps an I/O priority class to a deadline scheduler priority. */
134 static 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,
139 };
140
141 static inline struct rb_root *
deadline_rb_root(struct dd_per_prio * per_prio,struct request * rq)142 deadline_rb_root(struct dd_per_prio *per_prio, struct request *rq)
143 {
144 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 */
dd_rq_ioclass(struct request * rq)151 static u8 dd_rq_ioclass(struct request *rq)
152 {
153 return IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
154 }
155
156 /*
157 * get the request after `rq' in sector-sorted order
158 */
159 static inline struct request *
deadline_latter_request(struct request * rq)160 deadline_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
170 static void
deadline_add_rq_rb(struct dd_per_prio * per_prio,struct request * rq)171 deadline_add_rq_rb(struct dd_per_prio *per_prio, struct request *rq)
172 {
173 struct rb_root *root = deadline_rb_root(per_prio, rq);
174
175 elv_rb_add(root, rq);
176 }
177
178 static inline void
deadline_del_rq_rb(struct dd_per_prio * per_prio,struct request * rq)179 deadline_del_rq_rb(struct dd_per_prio *per_prio, struct request *rq)
180 {
181 const enum dd_data_dir data_dir = rq_data_dir(rq);
182
183 if (per_prio->next_rq[data_dir] == rq)
184 per_prio->next_rq[data_dir] = deadline_latter_request(rq);
185
186 elv_rb_del(deadline_rb_root(per_prio, rq), rq);
187 }
188
189 /*
190 * remove rq from rbtree and fifo.
191 */
deadline_remove_request(struct request_queue * q,struct dd_per_prio * per_prio,struct request * rq)192 static void deadline_remove_request(struct request_queue *q,
193 struct dd_per_prio *per_prio,
194 struct request *rq)
195 {
196 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))
202 deadline_del_rq_rb(per_prio, rq);
203
204 elv_rqhash_del(q, rq);
205 if (q->last_merge == rq)
206 q->last_merge = NULL;
207 }
208
dd_request_merged(struct request_queue * q,struct request * req,enum elv_merge type)209 static void dd_request_merged(struct request_queue *q, struct request *req,
210 enum elv_merge type)
211 {
212 struct deadline_data *dd = q->elevator->elevator_data;
213 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];
216
217 /*
218 * if the merge was a front merge, we need to reposition request
219 */
220 if (type == ELEVATOR_FRONT_MERGE) {
221 elv_rb_del(deadline_rb_root(per_prio, req), req);
222 deadline_add_rq_rb(per_prio, req);
223 }
224 }
225
226 /*
227 * Callback function that is invoked after @next has been merged into @req.
228 */
dd_merged_requests(struct request_queue * q,struct request * req,struct request * next)229 static void dd_merged_requests(struct request_queue *q, struct request *req,
230 struct request *next)
231 {
232 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];
235 struct dd_blkcg *blkcg = next->elv.priv[0];
236
237 dd_count(dd, merged, prio);
238 ddcg_count(blkcg, merged, ioprio_class);
239
240 /*
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 */
255 deadline_remove_request(q, &dd->per_prio[prio], next);
256 }
257
258 /*
259 * move an entry to dispatch queue
260 */
261 static void
deadline_move_request(struct deadline_data * dd,struct dd_per_prio * per_prio,struct request * rq)262 deadline_move_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
263 struct request *rq)
264 {
265 const enum dd_data_dir data_dir = rq_data_dir(rq);
266
267 per_prio->next_rq[data_dir] = deadline_latter_request(rq);
268
269 /*
270 * take it off the sort and fifo list
271 */
272 deadline_remove_request(rq->q, per_prio, rq);
273 }
274
275 /* Number of requests queued for a given priority level. */
dd_queued(struct deadline_data * dd,enum dd_prio prio)276 static 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
281 /*
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 */
deadline_check_fifo(struct dd_per_prio * per_prio,enum dd_data_dir data_dir)285 static inline int deadline_check_fifo(struct dd_per_prio *per_prio,
286 enum dd_data_dir data_dir)
287 {
288 struct request *rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next);
289
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 /*
300 * For the specified data direction, return the next request to
301 * dispatch using arrival ordered lists.
302 */
303 static struct request *
deadline_fifo_request(struct deadline_data * dd,struct dd_per_prio * per_prio,enum dd_data_dir data_dir)304 deadline_fifo_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
305 enum dd_data_dir data_dir)
306 {
307 struct request *rq;
308 unsigned long flags;
309
310 if (list_empty(&per_prio->fifo_list[data_dir]))
311 return NULL;
312
313 rq = rq_entry_fifo(per_prio->fifo_list[data_dir].next);
314 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
315 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);
322 list_for_each_entry(rq, &per_prio->fifo_list[DD_WRITE], queuelist) {
323 if (blk_req_can_dispatch_to_zone(rq))
324 goto out;
325 }
326 rq = NULL;
327 out:
328 spin_unlock_irqrestore(&dd->zone_lock, flags);
329
330 return rq;
331 }
332
333 /*
334 * For the specified data direction, return the next request to
335 * dispatch using sector position sorted lists.
336 */
337 static struct request *
deadline_next_request(struct deadline_data * dd,struct dd_per_prio * per_prio,enum dd_data_dir data_dir)338 deadline_next_request(struct deadline_data *dd, struct dd_per_prio *per_prio,
339 enum dd_data_dir data_dir)
340 {
341 struct request *rq;
342 unsigned long flags;
343
344 rq = per_prio->next_rq[data_dir];
345 if (!rq)
346 return NULL;
347
348 if (data_dir == DD_READ || !blk_queue_is_zoned(rq->q))
349 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;
364 }
365
366 /*
367 * deadline_dispatch_requests selects the best request according to
368 * read/write expire, fifo_batch, etc and with a start time <= @latest.
369 */
__dd_dispatch_request(struct deadline_data * dd,struct dd_per_prio * per_prio,u64 latest_start_ns)370 static struct request *__dd_dispatch_request(struct deadline_data *dd,
371 struct dd_per_prio *per_prio,
372 u64 latest_start_ns)
373 {
374 struct request *rq, *next_rq;
375 enum dd_data_dir data_dir;
376 struct dd_blkcg *blkcg;
377 enum dd_prio prio;
378 u8 ioprio_class;
379
380 lockdep_assert_held(&dd->lock);
381
382 if (!list_empty(&per_prio->dispatch)) {
383 rq = list_first_entry(&per_prio->dispatch, struct request,
384 queuelist);
385 if (rq->start_time_ns > latest_start_ns)
386 return NULL;
387 list_del_init(&rq->queuelist);
388 goto done;
389 }
390
391 /*
392 * batches are currently reads XOR writes
393 */
394 rq = deadline_next_request(dd, per_prio, dd->last_dir);
395 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
404 if (!list_empty(&per_prio->fifo_list[DD_READ])) {
405 BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_READ]));
406
407 if (deadline_fifo_request(dd, per_prio, DD_WRITE) &&
408 (dd->starved++ >= dd->writes_starved))
409 goto dispatch_writes;
410
411 data_dir = DD_READ;
412
413 goto dispatch_find_request;
414 }
415
416 /*
417 * there are either no reads or writes have been starved
418 */
419
420 if (!list_empty(&per_prio->fifo_list[DD_WRITE])) {
421 dispatch_writes:
422 BUG_ON(RB_EMPTY_ROOT(&per_prio->sort_list[DD_WRITE]));
423
424 dd->starved = 0;
425
426 data_dir = DD_WRITE;
427
428 goto dispatch_find_request;
429 }
430
431 return NULL;
432
433 dispatch_find_request:
434 /*
435 * we are not running a batch, find best request for selected data_dir
436 */
437 next_rq = deadline_next_request(dd, per_prio, data_dir);
438 if (deadline_check_fifo(per_prio, data_dir) || !next_rq) {
439 /*
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 */
444 rq = deadline_fifo_request(dd, per_prio, data_dir);
445 } 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 */
450 rq = next_rq;
451 }
452
453 /*
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
460 dd->last_dir = data_dir;
461 dd->batching = 0;
462
463 dispatch_request:
464 if (rq->start_time_ns > latest_start_ns)
465 return NULL;
466 /*
467 * rq is the selected appropriate request.
468 */
469 dd->batching++;
470 deadline_move_request(dd, per_prio, rq);
471 done:
472 ioprio_class = dd_rq_ioclass(rq);
473 prio = ioprio_class_to_prio[ioprio_class];
474 dd_count(dd, dispatched, prio);
475 blkcg = rq->elv.priv[0];
476 ddcg_count(blkcg, dispatched, ioprio_class);
477 /*
478 * If the request needs its target zone locked, do it.
479 */
480 blk_req_zone_write_lock(rq);
481 rq->rq_flags |= RQF_STARTED;
482 return rq;
483 }
484
485 /*
486 * Called from blk_mq_run_hw_queue() -> __blk_mq_sched_dispatch_requests().
487 *
488 * One confusing aspect here is that we get called for a specific
489 * hardware queue, but we may return a request that is for a
490 * different hardware queue. This is because mq-deadline has shared
491 * state for all hardware queues, in terms of sorting, FIFOs, etc.
492 */
dd_dispatch_request(struct blk_mq_hw_ctx * hctx)493 static struct request *dd_dispatch_request(struct blk_mq_hw_ctx *hctx)
494 {
495 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
496 const u64 now_ns = ktime_get_ns();
497 struct request *rq = NULL;
498 enum dd_prio prio;
499
500 spin_lock(&dd->lock);
501 /*
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));
508 if (rq)
509 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))
518 break;
519 }
520
521 unlock:
522 spin_unlock(&dd->lock);
523
524 return rq;
525 }
526
527 /*
528 * Called by __blk_mq_alloc_request(). The shallow_depth value set by this
529 * function is used by __blk_mq_get_tag().
530 */
dd_limit_depth(unsigned int op,struct blk_mq_alloc_data * data)531 static 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(). */
dd_depth_updated(struct blk_mq_hw_ctx * hctx)547 static 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(). */
dd_init_hctx(struct blk_mq_hw_ctx * hctx,unsigned int hctx_idx)559 static 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
dd_exit_sched(struct elevator_queue * e)565 static void dd_exit_sched(struct elevator_queue *e)
566 {
567 struct deadline_data *dd = e->elevator_data;
568 enum dd_prio prio;
569
570 dd_deactivate_policy(dd->queue);
571
572 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 }
578
579 free_percpu(dd->stats);
580
581 kfree(dd);
582 }
583
584 /*
585 * Initialize elevator private data (deadline_data) and associate with blkcg.
586 */
dd_init_sched(struct request_queue * q,struct elevator_type * e)587 static int dd_init_sched(struct request_queue *q, struct elevator_type *e)
588 {
589 struct deadline_data *dd;
590 struct elevator_queue *eq;
591 enum dd_prio prio;
592 int ret = -ENOMEM;
593
594 /*
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
600 eq = elevator_alloc(q, e);
601 if (!eq)
602 return ret;
603
604 dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
605 if (!dd)
606 goto put_eq;
607
608 eq->elevator_data = dd;
609
610 dd->stats = alloc_percpu_gfp(typeof(*dd->stats),
611 GFP_KERNEL | __GFP_ZERO);
612 if (!dd->stats)
613 goto free_dd;
614
615 dd->queue = q;
616
617 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 }
626 dd->fifo_expire[DD_READ] = read_expire;
627 dd->fifo_expire[DD_WRITE] = write_expire;
628 dd->writes_starved = writes_starved;
629 dd->front_merges = 1;
630 dd->last_dir = DD_WRITE;
631 dd->fifo_batch = fifo_batch;
632 dd->aging_expire = aging_expire;
633 spin_lock_init(&dd->lock);
634 spin_lock_init(&dd->zone_lock);
635
636 ret = dd_activate_policy(q);
637 if (ret)
638 goto free_stats;
639
640 ret = 0;
641 q->elevator = eq;
642 return 0;
643
644 free_stats:
645 free_percpu(dd->stats);
646
647 free_dd:
648 kfree(dd);
649
650 put_eq:
651 kobject_put(&eq->kobj);
652 return ret;
653 }
654
655 /*
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 */
dd_request_merge(struct request_queue * q,struct request ** rq,struct bio * bio)659 static 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;
663 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];
666 sector_t sector = bio_end_sector(bio);
667 struct request *__rq;
668
669 if (!dd->front_merges)
670 return ELEVATOR_NO_MERGE;
671
672 __rq = elv_rb_find(&per_prio->sort_list[bio_data_dir(bio)], sector);
673 if (__rq) {
674 BUG_ON(sector != blk_rq_pos(__rq));
675
676 if (elv_bio_merge_ok(__rq, bio)) {
677 *rq = __rq;
678 if (blk_discard_mergable(__rq))
679 return ELEVATOR_DISCARD_MERGE;
680 return ELEVATOR_FRONT_MERGE;
681 }
682 }
683
684 return ELEVATOR_NO_MERGE;
685 }
686
687 /*
688 * Attempt to merge a bio into an existing request. This function is called
689 * before @bio is associated with a request.
690 */
dd_bio_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs)691 static bool dd_bio_merge(struct request_queue *q, struct bio *bio,
692 unsigned int nr_segs)
693 {
694 struct deadline_data *dd = q->elevator->elevator_data;
695 struct request *free = NULL;
696 bool ret;
697
698 spin_lock(&dd->lock);
699 ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
700 spin_unlock(&dd->lock);
701
702 if (free)
703 blk_mq_free_request(free);
704
705 return ret;
706 }
707
708 /*
709 * add rq to rbtree and fifo
710 */
dd_insert_request(struct blk_mq_hw_ctx * hctx,struct request * rq,bool at_head)711 static 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;
716 const enum dd_data_dir data_dir = rq_data_dir(rq);
717 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;
721 struct dd_blkcg *blkcg;
722
723 lockdep_assert_held(&dd->lock);
724
725 /*
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
731 /*
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 */
738 prio = ioprio_class_to_prio[ioprio_class];
739 dd_count(dd, inserted, prio);
740 blkcg = dd_blkcg_from_bio(rq->bio);
741 ddcg_count(blkcg, inserted, ioprio_class);
742 rq->elv.priv[0] = blkcg;
743
744 if (blk_mq_sched_try_insert_merge(q, rq))
745 return;
746
747 blk_mq_sched_request_inserted(rq);
748
749 per_prio = &dd->per_prio[prio];
750 if (at_head) {
751 list_add(&rq->queuelist, &per_prio->dispatch);
752 rq->fifo_time = jiffies;
753 } else {
754 deadline_add_rq_rb(per_prio, rq);
755
756 if (rq_mergeable(rq)) {
757 elv_rqhash_add(q, rq);
758 if (!q->last_merge)
759 q->last_merge = rq;
760 }
761
762 /*
763 * set expire time and add to fifo list
764 */
765 rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
766 list_add_tail(&rq->queuelist, &per_prio->fifo_list[data_dir]);
767 }
768 }
769
770 /*
771 * Called from blk_mq_sched_insert_request() or blk_mq_sched_insert_requests().
772 */
dd_insert_requests(struct blk_mq_hw_ctx * hctx,struct list_head * list,bool at_head)773 static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
774 struct list_head *list, bool at_head)
775 {
776 struct request_queue *q = hctx->queue;
777 struct deadline_data *dd = q->elevator->elevator_data;
778
779 spin_lock(&dd->lock);
780 while (!list_empty(list)) {
781 struct request *rq;
782
783 rq = list_first_entry(list, struct request, queuelist);
784 list_del_init(&rq->queuelist);
785 dd_insert_request(hctx, rq, at_head);
786 }
787 spin_unlock(&dd->lock);
788 }
789
790 /* Callback from inside blk_mq_rq_ctx_init(). */
dd_prepare_request(struct request * rq)791 static void dd_prepare_request(struct request *rq)
792 {
793 rq->elv.priv[0] = NULL;
794 }
795
796 /*
797 * Callback from inside blk_mq_free_request().
798 *
799 * For zoned block devices, write unlock the target zone of
800 * completed write requests. Do this while holding the zone lock
801 * spinlock so that the zone is never unlocked while deadline_fifo_request()
802 * or deadline_next_request() are executing. This function is called for
803 * all requests, whether or not these requests complete successfully.
804 *
805 * For a zoned block device, __dd_dispatch_request() may have stopped
806 * dispatching requests if all the queued requests are write requests directed
807 * at zones that are already locked due to on-going write requests. To ensure
808 * write request dispatch progress in this case, mark the queue as needing a
809 * restart to ensure that the queue is run again after completion of the
810 * request and zones being unlocked.
811 */
dd_finish_request(struct request * rq)812 static void dd_finish_request(struct request *rq)
813 {
814 struct request_queue *q = rq->q;
815 struct deadline_data *dd = q->elevator->elevator_data;
816 struct dd_blkcg *blkcg = rq->elv.priv[0];
817 const u8 ioprio_class = dd_rq_ioclass(rq);
818 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
819 struct dd_per_prio *per_prio = &dd->per_prio[prio];
820
821 dd_count(dd, completed, prio);
822 ddcg_count(blkcg, completed, ioprio_class);
823
824 if (blk_queue_is_zoned(q)) {
825 unsigned long flags;
826
827 spin_lock_irqsave(&dd->zone_lock, flags);
828 blk_req_zone_write_unlock(rq);
829 if (!list_empty(&per_prio->fifo_list[DD_WRITE]))
830 blk_mq_sched_mark_restart_hctx(rq->mq_hctx);
831 spin_unlock_irqrestore(&dd->zone_lock, flags);
832 }
833 }
834
dd_has_work_for_prio(struct dd_per_prio * per_prio)835 static bool dd_has_work_for_prio(struct dd_per_prio *per_prio)
836 {
837 return !list_empty_careful(&per_prio->dispatch) ||
838 !list_empty_careful(&per_prio->fifo_list[DD_READ]) ||
839 !list_empty_careful(&per_prio->fifo_list[DD_WRITE]);
840 }
841
dd_has_work(struct blk_mq_hw_ctx * hctx)842 static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
843 {
844 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
845 enum dd_prio prio;
846
847 for (prio = 0; prio <= DD_PRIO_MAX; prio++)
848 if (dd_has_work_for_prio(&dd->per_prio[prio]))
849 return true;
850
851 return false;
852 }
853
854 /*
855 * sysfs parts below
856 */
857 #define SHOW_INT(__FUNC, __VAR) \
858 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
859 { \
860 struct deadline_data *dd = e->elevator_data; \
861 \
862 return sysfs_emit(page, "%d\n", __VAR); \
863 }
864 #define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR))
865 SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]);
866 SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]);
867 SHOW_JIFFIES(deadline_aging_expire_show, dd->aging_expire);
868 SHOW_INT(deadline_writes_starved_show, dd->writes_starved);
869 SHOW_INT(deadline_front_merges_show, dd->front_merges);
870 SHOW_INT(deadline_async_depth_show, dd->async_depth);
871 SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch);
872 #undef SHOW_INT
873 #undef SHOW_JIFFIES
874
875 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
876 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
877 { \
878 struct deadline_data *dd = e->elevator_data; \
879 int __data, __ret; \
880 \
881 __ret = kstrtoint(page, 0, &__data); \
882 if (__ret < 0) \
883 return __ret; \
884 if (__data < (MIN)) \
885 __data = (MIN); \
886 else if (__data > (MAX)) \
887 __data = (MAX); \
888 *(__PTR) = __CONV(__data); \
889 return count; \
890 }
891 #define STORE_INT(__FUNC, __PTR, MIN, MAX) \
892 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, )
893 #define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \
894 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies)
895 STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX);
896 STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX);
897 STORE_JIFFIES(deadline_aging_expire_store, &dd->aging_expire, 0, INT_MAX);
898 STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX);
899 STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1);
900 STORE_INT(deadline_async_depth_store, &dd->async_depth, 1, INT_MAX);
901 STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX);
902 #undef STORE_FUNCTION
903 #undef STORE_INT
904 #undef STORE_JIFFIES
905
906 #define DD_ATTR(name) \
907 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
908
909 static struct elv_fs_entry deadline_attrs[] = {
910 DD_ATTR(read_expire),
911 DD_ATTR(write_expire),
912 DD_ATTR(writes_starved),
913 DD_ATTR(front_merges),
914 DD_ATTR(async_depth),
915 DD_ATTR(fifo_batch),
916 DD_ATTR(aging_expire),
917 __ATTR_NULL
918 };
919
920 #ifdef CONFIG_BLK_DEBUG_FS
921 #define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \
922 static void *deadline_##name##_fifo_start(struct seq_file *m, \
923 loff_t *pos) \
924 __acquires(&dd->lock) \
925 { \
926 struct request_queue *q = m->private; \
927 struct deadline_data *dd = q->elevator->elevator_data; \
928 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
929 \
930 spin_lock(&dd->lock); \
931 return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \
932 } \
933 \
934 static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
935 loff_t *pos) \
936 { \
937 struct request_queue *q = m->private; \
938 struct deadline_data *dd = q->elevator->elevator_data; \
939 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
940 \
941 return seq_list_next(v, &per_prio->fifo_list[data_dir], pos); \
942 } \
943 \
944 static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
945 __releases(&dd->lock) \
946 { \
947 struct request_queue *q = m->private; \
948 struct deadline_data *dd = q->elevator->elevator_data; \
949 \
950 spin_unlock(&dd->lock); \
951 } \
952 \
953 static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
954 .start = deadline_##name##_fifo_start, \
955 .next = deadline_##name##_fifo_next, \
956 .stop = deadline_##name##_fifo_stop, \
957 .show = blk_mq_debugfs_rq_show, \
958 }; \
959 \
960 static int deadline_##name##_next_rq_show(void *data, \
961 struct seq_file *m) \
962 { \
963 struct request_queue *q = data; \
964 struct deadline_data *dd = q->elevator->elevator_data; \
965 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
966 struct request *rq = per_prio->next_rq[data_dir]; \
967 \
968 if (rq) \
969 __blk_mq_debugfs_rq_show(m, rq); \
970 return 0; \
971 }
972
973 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_READ, read0);
974 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_WRITE, write0);
975 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_READ, read1);
976 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_WRITE, write1);
977 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_READ, read2);
978 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_WRITE, write2);
979 #undef DEADLINE_DEBUGFS_DDIR_ATTRS
980
deadline_batching_show(void * data,struct seq_file * m)981 static int deadline_batching_show(void *data, struct seq_file *m)
982 {
983 struct request_queue *q = data;
984 struct deadline_data *dd = q->elevator->elevator_data;
985
986 seq_printf(m, "%u\n", dd->batching);
987 return 0;
988 }
989
deadline_starved_show(void * data,struct seq_file * m)990 static int deadline_starved_show(void *data, struct seq_file *m)
991 {
992 struct request_queue *q = data;
993 struct deadline_data *dd = q->elevator->elevator_data;
994
995 seq_printf(m, "%u\n", dd->starved);
996 return 0;
997 }
998
dd_async_depth_show(void * data,struct seq_file * m)999 static int dd_async_depth_show(void *data, struct seq_file *m)
1000 {
1001 struct request_queue *q = data;
1002 struct deadline_data *dd = q->elevator->elevator_data;
1003
1004 seq_printf(m, "%u\n", dd->async_depth);
1005 return 0;
1006 }
1007
dd_queued_show(void * data,struct seq_file * m)1008 static int dd_queued_show(void *data, struct seq_file *m)
1009 {
1010 struct request_queue *q = data;
1011 struct deadline_data *dd = q->elevator->elevator_data;
1012
1013 seq_printf(m, "%u %u %u\n", dd_queued(dd, DD_RT_PRIO),
1014 dd_queued(dd, DD_BE_PRIO),
1015 dd_queued(dd, DD_IDLE_PRIO));
1016 return 0;
1017 }
1018
1019 /* Number of requests owned by the block driver for a given priority. */
dd_owned_by_driver(struct deadline_data * dd,enum dd_prio prio)1020 static u32 dd_owned_by_driver(struct deadline_data *dd, enum dd_prio prio)
1021 {
1022 return dd_sum(dd, dispatched, prio) + dd_sum(dd, merged, prio)
1023 - dd_sum(dd, completed, prio);
1024 }
1025
dd_owned_by_driver_show(void * data,struct seq_file * m)1026 static int dd_owned_by_driver_show(void *data, struct seq_file *m)
1027 {
1028 struct request_queue *q = data;
1029 struct deadline_data *dd = q->elevator->elevator_data;
1030
1031 seq_printf(m, "%u %u %u\n", dd_owned_by_driver(dd, DD_RT_PRIO),
1032 dd_owned_by_driver(dd, DD_BE_PRIO),
1033 dd_owned_by_driver(dd, DD_IDLE_PRIO));
1034 return 0;
1035 }
1036
1037 #define DEADLINE_DISPATCH_ATTR(prio) \
1038 static void *deadline_dispatch##prio##_start(struct seq_file *m, \
1039 loff_t *pos) \
1040 __acquires(&dd->lock) \
1041 { \
1042 struct request_queue *q = m->private; \
1043 struct deadline_data *dd = q->elevator->elevator_data; \
1044 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
1045 \
1046 spin_lock(&dd->lock); \
1047 return seq_list_start(&per_prio->dispatch, *pos); \
1048 } \
1049 \
1050 static void *deadline_dispatch##prio##_next(struct seq_file *m, \
1051 void *v, loff_t *pos) \
1052 { \
1053 struct request_queue *q = m->private; \
1054 struct deadline_data *dd = q->elevator->elevator_data; \
1055 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
1056 \
1057 return seq_list_next(v, &per_prio->dispatch, pos); \
1058 } \
1059 \
1060 static void deadline_dispatch##prio##_stop(struct seq_file *m, void *v) \
1061 __releases(&dd->lock) \
1062 { \
1063 struct request_queue *q = m->private; \
1064 struct deadline_data *dd = q->elevator->elevator_data; \
1065 \
1066 spin_unlock(&dd->lock); \
1067 } \
1068 \
1069 static const struct seq_operations deadline_dispatch##prio##_seq_ops = { \
1070 .start = deadline_dispatch##prio##_start, \
1071 .next = deadline_dispatch##prio##_next, \
1072 .stop = deadline_dispatch##prio##_stop, \
1073 .show = blk_mq_debugfs_rq_show, \
1074 }
1075
1076 DEADLINE_DISPATCH_ATTR(0);
1077 DEADLINE_DISPATCH_ATTR(1);
1078 DEADLINE_DISPATCH_ATTR(2);
1079 #undef DEADLINE_DISPATCH_ATTR
1080
1081 #define DEADLINE_QUEUE_DDIR_ATTRS(name) \
1082 {#name "_fifo_list", 0400, \
1083 .seq_ops = &deadline_##name##_fifo_seq_ops}
1084 #define DEADLINE_NEXT_RQ_ATTR(name) \
1085 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
1086 static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
1087 DEADLINE_QUEUE_DDIR_ATTRS(read0),
1088 DEADLINE_QUEUE_DDIR_ATTRS(write0),
1089 DEADLINE_QUEUE_DDIR_ATTRS(read1),
1090 DEADLINE_QUEUE_DDIR_ATTRS(write1),
1091 DEADLINE_QUEUE_DDIR_ATTRS(read2),
1092 DEADLINE_QUEUE_DDIR_ATTRS(write2),
1093 DEADLINE_NEXT_RQ_ATTR(read0),
1094 DEADLINE_NEXT_RQ_ATTR(write0),
1095 DEADLINE_NEXT_RQ_ATTR(read1),
1096 DEADLINE_NEXT_RQ_ATTR(write1),
1097 DEADLINE_NEXT_RQ_ATTR(read2),
1098 DEADLINE_NEXT_RQ_ATTR(write2),
1099 {"batching", 0400, deadline_batching_show},
1100 {"starved", 0400, deadline_starved_show},
1101 {"async_depth", 0400, dd_async_depth_show},
1102 {"dispatch0", 0400, .seq_ops = &deadline_dispatch0_seq_ops},
1103 {"dispatch1", 0400, .seq_ops = &deadline_dispatch1_seq_ops},
1104 {"dispatch2", 0400, .seq_ops = &deadline_dispatch2_seq_ops},
1105 {"owned_by_driver", 0400, dd_owned_by_driver_show},
1106 {"queued", 0400, dd_queued_show},
1107 {},
1108 };
1109 #undef DEADLINE_QUEUE_DDIR_ATTRS
1110 #endif
1111
1112 static struct elevator_type mq_deadline = {
1113 .ops = {
1114 .depth_updated = dd_depth_updated,
1115 .limit_depth = dd_limit_depth,
1116 .insert_requests = dd_insert_requests,
1117 .dispatch_request = dd_dispatch_request,
1118 .prepare_request = dd_prepare_request,
1119 .finish_request = dd_finish_request,
1120 .next_request = elv_rb_latter_request,
1121 .former_request = elv_rb_former_request,
1122 .bio_merge = dd_bio_merge,
1123 .request_merge = dd_request_merge,
1124 .requests_merged = dd_merged_requests,
1125 .request_merged = dd_request_merged,
1126 .has_work = dd_has_work,
1127 .init_sched = dd_init_sched,
1128 .exit_sched = dd_exit_sched,
1129 .init_hctx = dd_init_hctx,
1130 },
1131
1132 #ifdef CONFIG_BLK_DEBUG_FS
1133 .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
1134 #endif
1135 .elevator_attrs = deadline_attrs,
1136 .elevator_name = "mq-deadline",
1137 .elevator_alias = "deadline",
1138 .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE,
1139 .elevator_owner = THIS_MODULE,
1140 };
1141 MODULE_ALIAS("mq-deadline-iosched");
1142
deadline_init(void)1143 static int __init deadline_init(void)
1144 {
1145 int ret;
1146
1147 ret = elv_register(&mq_deadline);
1148 if (ret)
1149 goto out;
1150 ret = dd_blkcg_init();
1151 if (ret)
1152 goto unreg;
1153
1154 out:
1155 return ret;
1156
1157 unreg:
1158 elv_unregister(&mq_deadline);
1159 goto out;
1160 }
1161
deadline_exit(void)1162 static void __exit deadline_exit(void)
1163 {
1164 dd_blkcg_exit();
1165 elv_unregister(&mq_deadline);
1166 }
1167
1168 module_init(deadline_init);
1169 module_exit(deadline_exit);
1170
1171 MODULE_AUTHOR("Jens Axboe, Damien Le Moal and Bart Van Assche");
1172 MODULE_LICENSE("GPL");
1173 MODULE_DESCRIPTION("MQ deadline IO scheduler");
1174