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 unsigned int shift = tags->bitmap_tags->sb.shift;
553
554 dd->async_depth = max(1U, 3 * (1U << shift) / 4);
555
556 sbitmap_queue_min_shallow_depth(tags->bitmap_tags, dd->async_depth);
557 }
558
559 /* Called by blk_mq_init_hctx() and blk_mq_init_sched(). */
dd_init_hctx(struct blk_mq_hw_ctx * hctx,unsigned int hctx_idx)560 static int dd_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
561 {
562 dd_depth_updated(hctx);
563 return 0;
564 }
565
dd_exit_sched(struct elevator_queue * e)566 static void dd_exit_sched(struct elevator_queue *e)
567 {
568 struct deadline_data *dd = e->elevator_data;
569 enum dd_prio prio;
570
571 dd_deactivate_policy(dd->queue);
572
573 for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
574 struct dd_per_prio *per_prio = &dd->per_prio[prio];
575
576 WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_READ]));
577 WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_WRITE]));
578 }
579
580 free_percpu(dd->stats);
581
582 kfree(dd);
583 }
584
585 /*
586 * Initialize elevator private data (deadline_data) and associate with blkcg.
587 */
dd_init_sched(struct request_queue * q,struct elevator_type * e)588 static int dd_init_sched(struct request_queue *q, struct elevator_type *e)
589 {
590 struct deadline_data *dd;
591 struct elevator_queue *eq;
592 enum dd_prio prio;
593 int ret = -ENOMEM;
594
595 /*
596 * Initialization would be very tricky if the queue is not frozen,
597 * hence the warning statement below.
598 */
599 WARN_ON_ONCE(!percpu_ref_is_zero(&q->q_usage_counter));
600
601 eq = elevator_alloc(q, e);
602 if (!eq)
603 return ret;
604
605 dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
606 if (!dd)
607 goto put_eq;
608
609 eq->elevator_data = dd;
610
611 dd->stats = alloc_percpu_gfp(typeof(*dd->stats),
612 GFP_KERNEL | __GFP_ZERO);
613 if (!dd->stats)
614 goto free_dd;
615
616 dd->queue = q;
617
618 for (prio = 0; prio <= DD_PRIO_MAX; prio++) {
619 struct dd_per_prio *per_prio = &dd->per_prio[prio];
620
621 INIT_LIST_HEAD(&per_prio->dispatch);
622 INIT_LIST_HEAD(&per_prio->fifo_list[DD_READ]);
623 INIT_LIST_HEAD(&per_prio->fifo_list[DD_WRITE]);
624 per_prio->sort_list[DD_READ] = RB_ROOT;
625 per_prio->sort_list[DD_WRITE] = RB_ROOT;
626 }
627 dd->fifo_expire[DD_READ] = read_expire;
628 dd->fifo_expire[DD_WRITE] = write_expire;
629 dd->writes_starved = writes_starved;
630 dd->front_merges = 1;
631 dd->last_dir = DD_WRITE;
632 dd->fifo_batch = fifo_batch;
633 dd->aging_expire = aging_expire;
634 spin_lock_init(&dd->lock);
635 spin_lock_init(&dd->zone_lock);
636
637 ret = dd_activate_policy(q);
638 if (ret)
639 goto free_stats;
640
641 ret = 0;
642 q->elevator = eq;
643 return 0;
644
645 free_stats:
646 free_percpu(dd->stats);
647
648 free_dd:
649 kfree(dd);
650
651 put_eq:
652 kobject_put(&eq->kobj);
653 return ret;
654 }
655
656 /*
657 * Try to merge @bio into an existing request. If @bio has been merged into
658 * an existing request, store the pointer to that request into *@rq.
659 */
dd_request_merge(struct request_queue * q,struct request ** rq,struct bio * bio)660 static int dd_request_merge(struct request_queue *q, struct request **rq,
661 struct bio *bio)
662 {
663 struct deadline_data *dd = q->elevator->elevator_data;
664 const u8 ioprio_class = IOPRIO_PRIO_CLASS(bio->bi_ioprio);
665 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
666 struct dd_per_prio *per_prio = &dd->per_prio[prio];
667 sector_t sector = bio_end_sector(bio);
668 struct request *__rq;
669
670 if (!dd->front_merges)
671 return ELEVATOR_NO_MERGE;
672
673 __rq = elv_rb_find(&per_prio->sort_list[bio_data_dir(bio)], sector);
674 if (__rq) {
675 BUG_ON(sector != blk_rq_pos(__rq));
676
677 if (elv_bio_merge_ok(__rq, bio)) {
678 *rq = __rq;
679 if (blk_discard_mergable(__rq))
680 return ELEVATOR_DISCARD_MERGE;
681 return ELEVATOR_FRONT_MERGE;
682 }
683 }
684
685 return ELEVATOR_NO_MERGE;
686 }
687
688 /*
689 * Attempt to merge a bio into an existing request. This function is called
690 * before @bio is associated with a request.
691 */
dd_bio_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs)692 static bool dd_bio_merge(struct request_queue *q, struct bio *bio,
693 unsigned int nr_segs)
694 {
695 struct deadline_data *dd = q->elevator->elevator_data;
696 struct request *free = NULL;
697 bool ret;
698
699 spin_lock(&dd->lock);
700 ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
701 spin_unlock(&dd->lock);
702
703 if (free)
704 blk_mq_free_request(free);
705
706 return ret;
707 }
708
709 /*
710 * add rq to rbtree and fifo
711 */
dd_insert_request(struct blk_mq_hw_ctx * hctx,struct request * rq,bool at_head)712 static void dd_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
713 bool at_head)
714 {
715 struct request_queue *q = hctx->queue;
716 struct deadline_data *dd = q->elevator->elevator_data;
717 const enum dd_data_dir data_dir = rq_data_dir(rq);
718 u16 ioprio = req_get_ioprio(rq);
719 u8 ioprio_class = IOPRIO_PRIO_CLASS(ioprio);
720 struct dd_per_prio *per_prio;
721 enum dd_prio prio;
722 struct dd_blkcg *blkcg;
723
724 lockdep_assert_held(&dd->lock);
725
726 /*
727 * This may be a requeue of a write request that has locked its
728 * target zone. If it is the case, this releases the zone lock.
729 */
730 blk_req_zone_write_unlock(rq);
731
732 /*
733 * If a block cgroup has been associated with the submitter and if an
734 * I/O priority has been set in the associated block cgroup, use the
735 * lowest of the cgroup priority and the request priority for the
736 * request. If no priority has been set in the request, use the cgroup
737 * priority.
738 */
739 prio = ioprio_class_to_prio[ioprio_class];
740 dd_count(dd, inserted, prio);
741 blkcg = dd_blkcg_from_bio(rq->bio);
742 ddcg_count(blkcg, inserted, ioprio_class);
743 rq->elv.priv[0] = blkcg;
744
745 if (blk_mq_sched_try_insert_merge(q, rq))
746 return;
747
748 blk_mq_sched_request_inserted(rq);
749
750 per_prio = &dd->per_prio[prio];
751 if (at_head) {
752 list_add(&rq->queuelist, &per_prio->dispatch);
753 rq->fifo_time = jiffies;
754 } else {
755 deadline_add_rq_rb(per_prio, rq);
756
757 if (rq_mergeable(rq)) {
758 elv_rqhash_add(q, rq);
759 if (!q->last_merge)
760 q->last_merge = rq;
761 }
762
763 /*
764 * set expire time and add to fifo list
765 */
766 rq->fifo_time = jiffies + dd->fifo_expire[data_dir];
767 list_add_tail(&rq->queuelist, &per_prio->fifo_list[data_dir]);
768 }
769 }
770
771 /*
772 * Called from blk_mq_sched_insert_request() or blk_mq_sched_insert_requests().
773 */
dd_insert_requests(struct blk_mq_hw_ctx * hctx,struct list_head * list,bool at_head)774 static void dd_insert_requests(struct blk_mq_hw_ctx *hctx,
775 struct list_head *list, bool at_head)
776 {
777 struct request_queue *q = hctx->queue;
778 struct deadline_data *dd = q->elevator->elevator_data;
779
780 spin_lock(&dd->lock);
781 while (!list_empty(list)) {
782 struct request *rq;
783
784 rq = list_first_entry(list, struct request, queuelist);
785 list_del_init(&rq->queuelist);
786 dd_insert_request(hctx, rq, at_head);
787 }
788 spin_unlock(&dd->lock);
789 }
790
791 /* Callback from inside blk_mq_rq_ctx_init(). */
dd_prepare_request(struct request * rq)792 static void dd_prepare_request(struct request *rq)
793 {
794 rq->elv.priv[0] = NULL;
795 }
796
dd_has_write_work(struct blk_mq_hw_ctx * hctx)797 static bool dd_has_write_work(struct blk_mq_hw_ctx *hctx)
798 {
799 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
800 enum dd_prio p;
801
802 for (p = 0; p <= DD_PRIO_MAX; p++)
803 if (!list_empty_careful(&dd->per_prio[p].fifo_list[DD_WRITE]))
804 return true;
805
806 return false;
807 }
808
809 /*
810 * Callback from inside blk_mq_free_request().
811 *
812 * For zoned block devices, write unlock the target zone of
813 * completed write requests. Do this while holding the zone lock
814 * spinlock so that the zone is never unlocked while deadline_fifo_request()
815 * or deadline_next_request() are executing. This function is called for
816 * all requests, whether or not these requests complete successfully.
817 *
818 * For a zoned block device, __dd_dispatch_request() may have stopped
819 * dispatching requests if all the queued requests are write requests directed
820 * at zones that are already locked due to on-going write requests. To ensure
821 * write request dispatch progress in this case, mark the queue as needing a
822 * restart to ensure that the queue is run again after completion of the
823 * request and zones being unlocked.
824 */
dd_finish_request(struct request * rq)825 static void dd_finish_request(struct request *rq)
826 {
827 struct request_queue *q = rq->q;
828 struct deadline_data *dd = q->elevator->elevator_data;
829 struct dd_blkcg *blkcg = rq->elv.priv[0];
830 const u8 ioprio_class = dd_rq_ioclass(rq);
831 const enum dd_prio prio = ioprio_class_to_prio[ioprio_class];
832
833 dd_count(dd, completed, prio);
834 ddcg_count(blkcg, completed, ioprio_class);
835
836 if (blk_queue_is_zoned(q)) {
837 unsigned long flags;
838
839 spin_lock_irqsave(&dd->zone_lock, flags);
840 blk_req_zone_write_unlock(rq);
841 spin_unlock_irqrestore(&dd->zone_lock, flags);
842
843 if (dd_has_write_work(rq->mq_hctx))
844 blk_mq_sched_mark_restart_hctx(rq->mq_hctx);
845 }
846 }
847
dd_has_work_for_prio(struct dd_per_prio * per_prio)848 static bool dd_has_work_for_prio(struct dd_per_prio *per_prio)
849 {
850 return !list_empty_careful(&per_prio->dispatch) ||
851 !list_empty_careful(&per_prio->fifo_list[DD_READ]) ||
852 !list_empty_careful(&per_prio->fifo_list[DD_WRITE]);
853 }
854
dd_has_work(struct blk_mq_hw_ctx * hctx)855 static bool dd_has_work(struct blk_mq_hw_ctx *hctx)
856 {
857 struct deadline_data *dd = hctx->queue->elevator->elevator_data;
858 enum dd_prio prio;
859
860 for (prio = 0; prio <= DD_PRIO_MAX; prio++)
861 if (dd_has_work_for_prio(&dd->per_prio[prio]))
862 return true;
863
864 return false;
865 }
866
867 /*
868 * sysfs parts below
869 */
870 #define SHOW_INT(__FUNC, __VAR) \
871 static ssize_t __FUNC(struct elevator_queue *e, char *page) \
872 { \
873 struct deadline_data *dd = e->elevator_data; \
874 \
875 return sysfs_emit(page, "%d\n", __VAR); \
876 }
877 #define SHOW_JIFFIES(__FUNC, __VAR) SHOW_INT(__FUNC, jiffies_to_msecs(__VAR))
878 SHOW_JIFFIES(deadline_read_expire_show, dd->fifo_expire[DD_READ]);
879 SHOW_JIFFIES(deadline_write_expire_show, dd->fifo_expire[DD_WRITE]);
880 SHOW_JIFFIES(deadline_aging_expire_show, dd->aging_expire);
881 SHOW_INT(deadline_writes_starved_show, dd->writes_starved);
882 SHOW_INT(deadline_front_merges_show, dd->front_merges);
883 SHOW_INT(deadline_async_depth_show, dd->async_depth);
884 SHOW_INT(deadline_fifo_batch_show, dd->fifo_batch);
885 #undef SHOW_INT
886 #undef SHOW_JIFFIES
887
888 #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
889 static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
890 { \
891 struct deadline_data *dd = e->elevator_data; \
892 int __data, __ret; \
893 \
894 __ret = kstrtoint(page, 0, &__data); \
895 if (__ret < 0) \
896 return __ret; \
897 if (__data < (MIN)) \
898 __data = (MIN); \
899 else if (__data > (MAX)) \
900 __data = (MAX); \
901 *(__PTR) = __CONV(__data); \
902 return count; \
903 }
904 #define STORE_INT(__FUNC, __PTR, MIN, MAX) \
905 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, )
906 #define STORE_JIFFIES(__FUNC, __PTR, MIN, MAX) \
907 STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, msecs_to_jiffies)
908 STORE_JIFFIES(deadline_read_expire_store, &dd->fifo_expire[DD_READ], 0, INT_MAX);
909 STORE_JIFFIES(deadline_write_expire_store, &dd->fifo_expire[DD_WRITE], 0, INT_MAX);
910 STORE_JIFFIES(deadline_aging_expire_store, &dd->aging_expire, 0, INT_MAX);
911 STORE_INT(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX);
912 STORE_INT(deadline_front_merges_store, &dd->front_merges, 0, 1);
913 STORE_INT(deadline_async_depth_store, &dd->async_depth, 1, INT_MAX);
914 STORE_INT(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX);
915 #undef STORE_FUNCTION
916 #undef STORE_INT
917 #undef STORE_JIFFIES
918
919 #define DD_ATTR(name) \
920 __ATTR(name, 0644, deadline_##name##_show, deadline_##name##_store)
921
922 static struct elv_fs_entry deadline_attrs[] = {
923 DD_ATTR(read_expire),
924 DD_ATTR(write_expire),
925 DD_ATTR(writes_starved),
926 DD_ATTR(front_merges),
927 DD_ATTR(async_depth),
928 DD_ATTR(fifo_batch),
929 DD_ATTR(aging_expire),
930 __ATTR_NULL
931 };
932
933 #ifdef CONFIG_BLK_DEBUG_FS
934 #define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \
935 static void *deadline_##name##_fifo_start(struct seq_file *m, \
936 loff_t *pos) \
937 __acquires(&dd->lock) \
938 { \
939 struct request_queue *q = m->private; \
940 struct deadline_data *dd = q->elevator->elevator_data; \
941 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
942 \
943 spin_lock(&dd->lock); \
944 return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \
945 } \
946 \
947 static void *deadline_##name##_fifo_next(struct seq_file *m, void *v, \
948 loff_t *pos) \
949 { \
950 struct request_queue *q = m->private; \
951 struct deadline_data *dd = q->elevator->elevator_data; \
952 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
953 \
954 return seq_list_next(v, &per_prio->fifo_list[data_dir], pos); \
955 } \
956 \
957 static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \
958 __releases(&dd->lock) \
959 { \
960 struct request_queue *q = m->private; \
961 struct deadline_data *dd = q->elevator->elevator_data; \
962 \
963 spin_unlock(&dd->lock); \
964 } \
965 \
966 static const struct seq_operations deadline_##name##_fifo_seq_ops = { \
967 .start = deadline_##name##_fifo_start, \
968 .next = deadline_##name##_fifo_next, \
969 .stop = deadline_##name##_fifo_stop, \
970 .show = blk_mq_debugfs_rq_show, \
971 }; \
972 \
973 static int deadline_##name##_next_rq_show(void *data, \
974 struct seq_file *m) \
975 { \
976 struct request_queue *q = data; \
977 struct deadline_data *dd = q->elevator->elevator_data; \
978 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
979 struct request *rq = per_prio->next_rq[data_dir]; \
980 \
981 if (rq) \
982 __blk_mq_debugfs_rq_show(m, rq); \
983 return 0; \
984 }
985
986 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_READ, read0);
987 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_RT_PRIO, DD_WRITE, write0);
988 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_READ, read1);
989 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_BE_PRIO, DD_WRITE, write1);
990 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_READ, read2);
991 DEADLINE_DEBUGFS_DDIR_ATTRS(DD_IDLE_PRIO, DD_WRITE, write2);
992 #undef DEADLINE_DEBUGFS_DDIR_ATTRS
993
deadline_batching_show(void * data,struct seq_file * m)994 static int deadline_batching_show(void *data, struct seq_file *m)
995 {
996 struct request_queue *q = data;
997 struct deadline_data *dd = q->elevator->elevator_data;
998
999 seq_printf(m, "%u\n", dd->batching);
1000 return 0;
1001 }
1002
deadline_starved_show(void * data,struct seq_file * m)1003 static int deadline_starved_show(void *data, struct seq_file *m)
1004 {
1005 struct request_queue *q = data;
1006 struct deadline_data *dd = q->elevator->elevator_data;
1007
1008 seq_printf(m, "%u\n", dd->starved);
1009 return 0;
1010 }
1011
dd_async_depth_show(void * data,struct seq_file * m)1012 static int dd_async_depth_show(void *data, struct seq_file *m)
1013 {
1014 struct request_queue *q = data;
1015 struct deadline_data *dd = q->elevator->elevator_data;
1016
1017 seq_printf(m, "%u\n", dd->async_depth);
1018 return 0;
1019 }
1020
dd_queued_show(void * data,struct seq_file * m)1021 static int dd_queued_show(void *data, struct seq_file *m)
1022 {
1023 struct request_queue *q = data;
1024 struct deadline_data *dd = q->elevator->elevator_data;
1025
1026 seq_printf(m, "%u %u %u\n", dd_queued(dd, DD_RT_PRIO),
1027 dd_queued(dd, DD_BE_PRIO),
1028 dd_queued(dd, DD_IDLE_PRIO));
1029 return 0;
1030 }
1031
1032 /* Number of requests owned by the block driver for a given priority. */
dd_owned_by_driver(struct deadline_data * dd,enum dd_prio prio)1033 static u32 dd_owned_by_driver(struct deadline_data *dd, enum dd_prio prio)
1034 {
1035 return dd_sum(dd, dispatched, prio) + dd_sum(dd, merged, prio)
1036 - dd_sum(dd, completed, prio);
1037 }
1038
dd_owned_by_driver_show(void * data,struct seq_file * m)1039 static int dd_owned_by_driver_show(void *data, struct seq_file *m)
1040 {
1041 struct request_queue *q = data;
1042 struct deadline_data *dd = q->elevator->elevator_data;
1043
1044 seq_printf(m, "%u %u %u\n", dd_owned_by_driver(dd, DD_RT_PRIO),
1045 dd_owned_by_driver(dd, DD_BE_PRIO),
1046 dd_owned_by_driver(dd, DD_IDLE_PRIO));
1047 return 0;
1048 }
1049
1050 #define DEADLINE_DISPATCH_ATTR(prio) \
1051 static void *deadline_dispatch##prio##_start(struct seq_file *m, \
1052 loff_t *pos) \
1053 __acquires(&dd->lock) \
1054 { \
1055 struct request_queue *q = m->private; \
1056 struct deadline_data *dd = q->elevator->elevator_data; \
1057 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
1058 \
1059 spin_lock(&dd->lock); \
1060 return seq_list_start(&per_prio->dispatch, *pos); \
1061 } \
1062 \
1063 static void *deadline_dispatch##prio##_next(struct seq_file *m, \
1064 void *v, loff_t *pos) \
1065 { \
1066 struct request_queue *q = m->private; \
1067 struct deadline_data *dd = q->elevator->elevator_data; \
1068 struct dd_per_prio *per_prio = &dd->per_prio[prio]; \
1069 \
1070 return seq_list_next(v, &per_prio->dispatch, pos); \
1071 } \
1072 \
1073 static void deadline_dispatch##prio##_stop(struct seq_file *m, void *v) \
1074 __releases(&dd->lock) \
1075 { \
1076 struct request_queue *q = m->private; \
1077 struct deadline_data *dd = q->elevator->elevator_data; \
1078 \
1079 spin_unlock(&dd->lock); \
1080 } \
1081 \
1082 static const struct seq_operations deadline_dispatch##prio##_seq_ops = { \
1083 .start = deadline_dispatch##prio##_start, \
1084 .next = deadline_dispatch##prio##_next, \
1085 .stop = deadline_dispatch##prio##_stop, \
1086 .show = blk_mq_debugfs_rq_show, \
1087 }
1088
1089 DEADLINE_DISPATCH_ATTR(0);
1090 DEADLINE_DISPATCH_ATTR(1);
1091 DEADLINE_DISPATCH_ATTR(2);
1092 #undef DEADLINE_DISPATCH_ATTR
1093
1094 #define DEADLINE_QUEUE_DDIR_ATTRS(name) \
1095 {#name "_fifo_list", 0400, \
1096 .seq_ops = &deadline_##name##_fifo_seq_ops}
1097 #define DEADLINE_NEXT_RQ_ATTR(name) \
1098 {#name "_next_rq", 0400, deadline_##name##_next_rq_show}
1099 static const struct blk_mq_debugfs_attr deadline_queue_debugfs_attrs[] = {
1100 DEADLINE_QUEUE_DDIR_ATTRS(read0),
1101 DEADLINE_QUEUE_DDIR_ATTRS(write0),
1102 DEADLINE_QUEUE_DDIR_ATTRS(read1),
1103 DEADLINE_QUEUE_DDIR_ATTRS(write1),
1104 DEADLINE_QUEUE_DDIR_ATTRS(read2),
1105 DEADLINE_QUEUE_DDIR_ATTRS(write2),
1106 DEADLINE_NEXT_RQ_ATTR(read0),
1107 DEADLINE_NEXT_RQ_ATTR(write0),
1108 DEADLINE_NEXT_RQ_ATTR(read1),
1109 DEADLINE_NEXT_RQ_ATTR(write1),
1110 DEADLINE_NEXT_RQ_ATTR(read2),
1111 DEADLINE_NEXT_RQ_ATTR(write2),
1112 {"batching", 0400, deadline_batching_show},
1113 {"starved", 0400, deadline_starved_show},
1114 {"async_depth", 0400, dd_async_depth_show},
1115 {"dispatch0", 0400, .seq_ops = &deadline_dispatch0_seq_ops},
1116 {"dispatch1", 0400, .seq_ops = &deadline_dispatch1_seq_ops},
1117 {"dispatch2", 0400, .seq_ops = &deadline_dispatch2_seq_ops},
1118 {"owned_by_driver", 0400, dd_owned_by_driver_show},
1119 {"queued", 0400, dd_queued_show},
1120 {},
1121 };
1122 #undef DEADLINE_QUEUE_DDIR_ATTRS
1123 #endif
1124
1125 static struct elevator_type mq_deadline = {
1126 .ops = {
1127 .depth_updated = dd_depth_updated,
1128 .limit_depth = dd_limit_depth,
1129 .insert_requests = dd_insert_requests,
1130 .dispatch_request = dd_dispatch_request,
1131 .prepare_request = dd_prepare_request,
1132 .finish_request = dd_finish_request,
1133 .next_request = elv_rb_latter_request,
1134 .former_request = elv_rb_former_request,
1135 .bio_merge = dd_bio_merge,
1136 .request_merge = dd_request_merge,
1137 .requests_merged = dd_merged_requests,
1138 .request_merged = dd_request_merged,
1139 .has_work = dd_has_work,
1140 .init_sched = dd_init_sched,
1141 .exit_sched = dd_exit_sched,
1142 .init_hctx = dd_init_hctx,
1143 },
1144
1145 #ifdef CONFIG_BLK_DEBUG_FS
1146 .queue_debugfs_attrs = deadline_queue_debugfs_attrs,
1147 #endif
1148 .elevator_attrs = deadline_attrs,
1149 .elevator_name = "mq-deadline",
1150 .elevator_alias = "deadline",
1151 .elevator_features = ELEVATOR_F_ZBD_SEQ_WRITE,
1152 .elevator_owner = THIS_MODULE,
1153 };
1154 MODULE_ALIAS("mq-deadline-iosched");
1155
deadline_init(void)1156 static int __init deadline_init(void)
1157 {
1158 int ret;
1159
1160 ret = elv_register(&mq_deadline);
1161 if (ret)
1162 goto out;
1163 ret = dd_blkcg_init();
1164 if (ret)
1165 goto unreg;
1166
1167 out:
1168 return ret;
1169
1170 unreg:
1171 elv_unregister(&mq_deadline);
1172 goto out;
1173 }
1174
deadline_exit(void)1175 static void __exit deadline_exit(void)
1176 {
1177 dd_blkcg_exit();
1178 elv_unregister(&mq_deadline);
1179 }
1180
1181 module_init(deadline_init);
1182 module_exit(deadline_exit);
1183
1184 MODULE_AUTHOR("Jens Axboe, Damien Le Moal and Bart Van Assche");
1185 MODULE_LICENSE("GPL");
1186 MODULE_DESCRIPTION("MQ deadline IO scheduler");
1187