]> git.ipfire.org Git - people/ms/linux.git/blame - block/blk-rq-qos.h
Merge tag 'pm-6.0-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[people/ms/linux.git] / block / blk-rq-qos.h
CommitLineData
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
14struct blk_mq_debugfs_attr;
15
a7905043
JB
16enum rq_qos_id {
17 RQ_QOS_WBT,
beab17fc 18 RQ_QOS_LATENCY,
7caa4715 19 RQ_QOS_COST,
556910e3 20 RQ_QOS_IOPRIO,
a7905043
JB
21};
22
23struct rq_wait {
24 wait_queue_head_t wait;
25 atomic_t inflight;
26};
27
28struct rq_qos {
29 struct rq_qos_ops *ops;
30 struct request_queue *q;
31 enum rq_qos_id id;
32 struct rq_qos *next;
cc56694f
ML
33#ifdef CONFIG_BLK_DEBUG_FS
34 struct dentry *debugfs_dir;
35#endif
a7905043
JB
36};
37
38struct rq_qos_ops {
d5337560 39 void (*throttle)(struct rq_qos *, struct bio *);
c1c80384 40 void (*track)(struct rq_qos *, struct request *, struct bio *);
d3e65fff 41 void (*merge)(struct rq_qos *, struct request *, struct bio *);
a7905043
JB
42 void (*issue)(struct rq_qos *, struct request *);
43 void (*requeue)(struct rq_qos *, struct request *);
44 void (*done)(struct rq_qos *, struct request *);
67b42d0b 45 void (*done_bio)(struct rq_qos *, struct bio *);
c1c80384 46 void (*cleanup)(struct rq_qos *, struct bio *);
9677a3e0 47 void (*queue_depth_changed)(struct rq_qos *);
a7905043 48 void (*exit)(struct rq_qos *);
cc56694f 49 const struct blk_mq_debugfs_attr *debugfs_attrs;
a7905043
JB
50};
51
52struct rq_depth {
53 unsigned int max_depth;
54
55 int scale_step;
56 bool scaled_max;
57
58 unsigned int queue_depth;
59 unsigned int default_depth;
60};
61
62static inline struct rq_qos *rq_qos_id(struct request_queue *q,
63 enum rq_qos_id id)
64{
65 struct rq_qos *rqos;
66 for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
67 if (rqos->id == id)
68 break;
69 }
70 return rqos;
71}
72
73static inline struct rq_qos *wbt_rq_qos(struct request_queue *q)
74{
75 return rq_qos_id(q, RQ_QOS_WBT);
76}
77
78static inline struct rq_qos *blkcg_rq_qos(struct request_queue *q)
79{
beab17fc 80 return rq_qos_id(q, RQ_QOS_LATENCY);
a7905043
JB
81}
82
83static inline void rq_wait_init(struct rq_wait *rq_wait)
84{
85 atomic_set(&rq_wait->inflight, 0);
86 init_waitqueue_head(&rq_wait->wait);
87}
88
14a6e2eb 89static inline int rq_qos_add(struct request_queue *q, struct rq_qos *rqos)
a7905043 90{
2cafe29a
ML
91 /*
92 * No IO can be in-flight when adding rqos, so freeze queue, which
93 * is fine since we only support rq_qos for blk-mq queue.
94 *
95 * Reuse ->queue_lock for protecting against other concurrent
96 * rq_qos adding/deleting
97 */
98 blk_mq_freeze_queue(q);
99
100 spin_lock_irq(&q->queue_lock);
14a6e2eb
JH
101 if (rq_qos_id(q, rqos->id))
102 goto ebusy;
a7905043
JB
103 rqos->next = q->rq_qos;
104 q->rq_qos = rqos;
2cafe29a
ML
105 spin_unlock_irq(&q->queue_lock);
106
107 blk_mq_unfreeze_queue(q);
cc56694f 108
5cf9c91b
CH
109 if (rqos->ops->debugfs_attrs) {
110 mutex_lock(&q->debugfs_mutex);
cc56694f 111 blk_mq_debugfs_register_rqos(rqos);
5cf9c91b
CH
112 mutex_unlock(&q->debugfs_mutex);
113 }
14a6e2eb
JH
114
115 return 0;
116ebusy:
117 spin_unlock_irq(&q->queue_lock);
118 blk_mq_unfreeze_queue(q);
119 return -EBUSY;
120
a7905043
JB
121}
122
123static inline void rq_qos_del(struct request_queue *q, struct rq_qos *rqos)
124{
307f4065
TH
125 struct rq_qos **cur;
126
2cafe29a
ML
127 /*
128 * See comment in rq_qos_add() about freezing queue & using
129 * ->queue_lock.
130 */
131 blk_mq_freeze_queue(q);
132
133 spin_lock_irq(&q->queue_lock);
307f4065
TH
134 for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
135 if (*cur == rqos) {
136 *cur = rqos->next;
a7905043
JB
137 break;
138 }
a7905043 139 }
2cafe29a
ML
140 spin_unlock_irq(&q->queue_lock);
141
142 blk_mq_unfreeze_queue(q);
cc56694f 143
5cf9c91b 144 mutex_lock(&q->debugfs_mutex);
cc56694f 145 blk_mq_debugfs_unregister_rqos(rqos);
5cf9c91b 146 mutex_unlock(&q->debugfs_mutex);
a7905043
JB
147}
148
84f60324
JB
149typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data);
150typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data);
151
152void rq_qos_wait(struct rq_wait *rqw, void *private_data,
153 acquire_inflight_cb_t *acquire_inflight_cb,
154 cleanup_cb_t *cleanup_cb);
22f17952 155bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit);
b84477d3
HS
156bool rq_depth_scale_up(struct rq_depth *rqd);
157bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle);
a7905043
JB
158bool rq_depth_calc_max_depth(struct rq_depth *rqd);
159
e5045454
JA
160void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio);
161void __rq_qos_done(struct rq_qos *rqos, struct request *rq);
162void __rq_qos_issue(struct rq_qos *rqos, struct request *rq);
163void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq);
164void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio);
165void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio);
d3e65fff 166void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio);
e5045454 167void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio);
9677a3e0 168void __rq_qos_queue_depth_changed(struct rq_qos *rqos);
e5045454
JA
169
170static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
171{
172 if (q->rq_qos)
173 __rq_qos_cleanup(q->rq_qos, bio);
174}
175
176static inline void rq_qos_done(struct request_queue *q, struct request *rq)
177{
178 if (q->rq_qos)
179 __rq_qos_done(q->rq_qos, rq);
180}
181
182static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
183{
184 if (q->rq_qos)
185 __rq_qos_issue(q->rq_qos, rq);
186}
187
188static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
189{
190 if (q->rq_qos)
191 __rq_qos_requeue(q->rq_qos, rq);
192}
193
aa1b46dc 194static inline void rq_qos_done_bio(struct bio *bio)
e5045454 195{
aa1b46dc
TH
196 if (bio->bi_bdev && (bio_flagged(bio, BIO_QOS_THROTTLED) ||
197 bio_flagged(bio, BIO_QOS_MERGED))) {
198 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
199 if (q->rq_qos)
200 __rq_qos_done_bio(q->rq_qos, bio);
201 }
e5045454
JA
202}
203
204static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
205{
90b8faa0 206 if (q->rq_qos) {
aa1b46dc 207 bio_set_flag(bio, BIO_QOS_THROTTLED);
e5045454 208 __rq_qos_throttle(q->rq_qos, bio);
90b8faa0 209 }
e5045454
JA
210}
211
212static inline void rq_qos_track(struct request_queue *q, struct request *rq,
213 struct bio *bio)
214{
215 if (q->rq_qos)
216 __rq_qos_track(q->rq_qos, rq, bio);
217}
218
d3e65fff
TH
219static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
220 struct bio *bio)
221{
aa1b46dc
TH
222 if (q->rq_qos) {
223 bio_set_flag(bio, BIO_QOS_MERGED);
d3e65fff 224 __rq_qos_merge(q->rq_qos, rq, bio);
aa1b46dc 225 }
d3e65fff
TH
226}
227
9677a3e0
TH
228static inline void rq_qos_queue_depth_changed(struct request_queue *q)
229{
230 if (q->rq_qos)
231 __rq_qos_queue_depth_changed(q->rq_qos);
232}
233
a7905043 234void rq_qos_exit(struct request_queue *);
e5045454 235
a7905043 236#endif