Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
a7905043 JB |
2 | #ifndef RQ_QOS_H |
3 | #define RQ_QOS_H | |
4 | ||
5 | #include <linux/kernel.h> | |
6 | #include <linux/blkdev.h> | |
7 | #include <linux/blk_types.h> | |
8 | #include <linux/atomic.h> | |
9 | #include <linux/wait.h> | |
2cafe29a | 10 | #include <linux/blk-mq.h> |
a7905043 | 11 | |
cc56694f ML |
12 | #include "blk-mq-debugfs.h" |
13 | ||
14 | struct blk_mq_debugfs_attr; | |
15 | ||
a7905043 JB |
16 | enum rq_qos_id { |
17 | RQ_QOS_WBT, | |
beab17fc | 18 | RQ_QOS_LATENCY, |
7caa4715 | 19 | RQ_QOS_COST, |
a7905043 JB |
20 | }; |
21 | ||
22 | struct rq_wait { | |
23 | wait_queue_head_t wait; | |
24 | atomic_t inflight; | |
25 | }; | |
26 | ||
27 | struct rq_qos { | |
3963d84d | 28 | const struct rq_qos_ops *ops; |
ba91c849 | 29 | struct gendisk *disk; |
a7905043 JB |
30 | enum rq_qos_id id; |
31 | struct rq_qos *next; | |
cc56694f ML |
32 | #ifdef CONFIG_BLK_DEBUG_FS |
33 | struct dentry *debugfs_dir; | |
34 | #endif | |
a7905043 JB |
35 | }; |
36 | ||
37 | struct rq_qos_ops { | |
d5337560 | 38 | void (*throttle)(struct rq_qos *, struct bio *); |
c1c80384 | 39 | void (*track)(struct rq_qos *, struct request *, struct bio *); |
d3e65fff | 40 | void (*merge)(struct rq_qos *, struct request *, struct bio *); |
a7905043 JB |
41 | void (*issue)(struct rq_qos *, struct request *); |
42 | void (*requeue)(struct rq_qos *, struct request *); | |
43 | void (*done)(struct rq_qos *, struct request *); | |
67b42d0b | 44 | void (*done_bio)(struct rq_qos *, struct bio *); |
c1c80384 | 45 | void (*cleanup)(struct rq_qos *, struct bio *); |
9677a3e0 | 46 | void (*queue_depth_changed)(struct rq_qos *); |
a7905043 | 47 | void (*exit)(struct rq_qos *); |
cc56694f | 48 | const struct blk_mq_debugfs_attr *debugfs_attrs; |
a7905043 JB |
49 | }; |
50 | ||
51 | struct rq_depth { | |
52 | unsigned int max_depth; | |
53 | ||
54 | int scale_step; | |
55 | bool scaled_max; | |
56 | ||
57 | unsigned int queue_depth; | |
58 | unsigned int default_depth; | |
59 | }; | |
60 | ||
61 | static inline struct rq_qos *rq_qos_id(struct request_queue *q, | |
62 | enum rq_qos_id id) | |
63 | { | |
64 | struct rq_qos *rqos; | |
65 | for (rqos = q->rq_qos; rqos; rqos = rqos->next) { | |
66 | if (rqos->id == id) | |
67 | break; | |
68 | } | |
69 | return rqos; | |
70 | } | |
71 | ||
72 | static inline struct rq_qos *wbt_rq_qos(struct request_queue *q) | |
73 | { | |
74 | return rq_qos_id(q, RQ_QOS_WBT); | |
75 | } | |
76 | ||
33049187 | 77 | static inline struct rq_qos *iolat_rq_qos(struct request_queue *q) |
a7905043 | 78 | { |
beab17fc | 79 | return rq_qos_id(q, RQ_QOS_LATENCY); |
a7905043 JB |
80 | } |
81 | ||
82 | static inline void rq_wait_init(struct rq_wait *rq_wait) | |
83 | { | |
84 | atomic_set(&rq_wait->inflight, 0); | |
85 | init_waitqueue_head(&rq_wait->wait); | |
86 | } | |
87 | ||
ce57b558 | 88 | int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id, |
3963d84d | 89 | const struct rq_qos_ops *ops); |
ce57b558 | 90 | void rq_qos_del(struct rq_qos *rqos); |
a7905043 | 91 | |
84f60324 JB |
92 | typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data); |
93 | typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data); | |
94 | ||
95 | void rq_qos_wait(struct rq_wait *rqw, void *private_data, | |
96 | acquire_inflight_cb_t *acquire_inflight_cb, | |
97 | cleanup_cb_t *cleanup_cb); | |
22f17952 | 98 | bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit); |
b84477d3 HS |
99 | bool rq_depth_scale_up(struct rq_depth *rqd); |
100 | bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle); | |
a7905043 JB |
101 | bool rq_depth_calc_max_depth(struct rq_depth *rqd); |
102 | ||
e5045454 JA |
103 | void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio); |
104 | void __rq_qos_done(struct rq_qos *rqos, struct request *rq); | |
105 | void __rq_qos_issue(struct rq_qos *rqos, struct request *rq); | |
106 | void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq); | |
107 | void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio); | |
108 | void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio); | |
d3e65fff | 109 | void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio); |
e5045454 | 110 | void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio); |
9677a3e0 | 111 | void __rq_qos_queue_depth_changed(struct rq_qos *rqos); |
e5045454 JA |
112 | |
113 | static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio) | |
114 | { | |
115 | if (q->rq_qos) | |
116 | __rq_qos_cleanup(q->rq_qos, bio); | |
117 | } | |
118 | ||
119 | static inline void rq_qos_done(struct request_queue *q, struct request *rq) | |
120 | { | |
121 | if (q->rq_qos) | |
122 | __rq_qos_done(q->rq_qos, rq); | |
123 | } | |
124 | ||
125 | static inline void rq_qos_issue(struct request_queue *q, struct request *rq) | |
126 | { | |
127 | if (q->rq_qos) | |
128 | __rq_qos_issue(q->rq_qos, rq); | |
129 | } | |
130 | ||
131 | static inline void rq_qos_requeue(struct request_queue *q, struct request *rq) | |
132 | { | |
133 | if (q->rq_qos) | |
134 | __rq_qos_requeue(q->rq_qos, rq); | |
135 | } | |
136 | ||
aa1b46dc | 137 | static inline void rq_qos_done_bio(struct bio *bio) |
e5045454 | 138 | { |
aa1b46dc TH |
139 | if (bio->bi_bdev && (bio_flagged(bio, BIO_QOS_THROTTLED) || |
140 | bio_flagged(bio, BIO_QOS_MERGED))) { | |
141 | struct request_queue *q = bdev_get_queue(bio->bi_bdev); | |
142 | if (q->rq_qos) | |
143 | __rq_qos_done_bio(q->rq_qos, bio); | |
144 | } | |
e5045454 JA |
145 | } |
146 | ||
147 | static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio) | |
148 | { | |
90b8faa0 | 149 | if (q->rq_qos) { |
aa1b46dc | 150 | bio_set_flag(bio, BIO_QOS_THROTTLED); |
e5045454 | 151 | __rq_qos_throttle(q->rq_qos, bio); |
90b8faa0 | 152 | } |
e5045454 JA |
153 | } |
154 | ||
155 | static inline void rq_qos_track(struct request_queue *q, struct request *rq, | |
156 | struct bio *bio) | |
157 | { | |
158 | if (q->rq_qos) | |
159 | __rq_qos_track(q->rq_qos, rq, bio); | |
160 | } | |
161 | ||
d3e65fff TH |
162 | static inline void rq_qos_merge(struct request_queue *q, struct request *rq, |
163 | struct bio *bio) | |
164 | { | |
aa1b46dc TH |
165 | if (q->rq_qos) { |
166 | bio_set_flag(bio, BIO_QOS_MERGED); | |
d3e65fff | 167 | __rq_qos_merge(q->rq_qos, rq, bio); |
aa1b46dc | 168 | } |
d3e65fff TH |
169 | } |
170 | ||
9677a3e0 TH |
171 | static inline void rq_qos_queue_depth_changed(struct request_queue *q) |
172 | { | |
173 | if (q->rq_qos) | |
174 | __rq_qos_queue_depth_changed(q->rq_qos); | |
175 | } | |
176 | ||
a7905043 | 177 | void rq_qos_exit(struct request_queue *); |
e5045454 | 178 | |
a7905043 | 179 | #endif |