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