block: fix use-after-free of q->q_usage_counter
[linux-block.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,
a7905043
JB
20};
21
22struct rq_wait {
23 wait_queue_head_t wait;
24 atomic_t inflight;
25};
26
27struct rq_qos {
28 struct rq_qos_ops *ops;
29 struct request_queue *q;
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
37struct 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
51struct 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
61static 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
72static inline struct rq_qos *wbt_rq_qos(struct request_queue *q)
73{
74 return rq_qos_id(q, RQ_QOS_WBT);
75}
76
77static inline struct rq_qos *blkcg_rq_qos(struct request_queue *q)
78{
beab17fc 79 return rq_qos_id(q, RQ_QOS_LATENCY);
a7905043
JB
80}
81
82static 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
14a6e2eb 88static inline int rq_qos_add(struct request_queue *q, struct rq_qos *rqos)
a7905043 89{
2cafe29a
ML
90 /*
91 * No IO can be in-flight when adding rqos, so freeze queue, which
92 * is fine since we only support rq_qos for blk-mq queue.
93 *
94 * Reuse ->queue_lock for protecting against other concurrent
95 * rq_qos adding/deleting
96 */
97 blk_mq_freeze_queue(q);
98
99 spin_lock_irq(&q->queue_lock);
14a6e2eb
JH
100 if (rq_qos_id(q, rqos->id))
101 goto ebusy;
a7905043
JB
102 rqos->next = q->rq_qos;
103 q->rq_qos = rqos;
2cafe29a
ML
104 spin_unlock_irq(&q->queue_lock);
105
106 blk_mq_unfreeze_queue(q);
cc56694f 107
5cf9c91b
CH
108 if (rqos->ops->debugfs_attrs) {
109 mutex_lock(&q->debugfs_mutex);
cc56694f 110 blk_mq_debugfs_register_rqos(rqos);
5cf9c91b
CH
111 mutex_unlock(&q->debugfs_mutex);
112 }
14a6e2eb
JH
113
114 return 0;
115ebusy:
116 spin_unlock_irq(&q->queue_lock);
117 blk_mq_unfreeze_queue(q);
118 return -EBUSY;
119
a7905043
JB
120}
121
122static inline void rq_qos_del(struct request_queue *q, struct rq_qos *rqos)
123{
307f4065
TH
124 struct rq_qos **cur;
125
2cafe29a
ML
126 /*
127 * See comment in rq_qos_add() about freezing queue & using
128 * ->queue_lock.
129 */
130 blk_mq_freeze_queue(q);
131
132 spin_lock_irq(&q->queue_lock);
307f4065
TH
133 for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
134 if (*cur == rqos) {
135 *cur = rqos->next;
a7905043
JB
136 break;
137 }
a7905043 138 }
2cafe29a
ML
139 spin_unlock_irq(&q->queue_lock);
140
141 blk_mq_unfreeze_queue(q);
cc56694f 142
5cf9c91b 143 mutex_lock(&q->debugfs_mutex);
cc56694f 144 blk_mq_debugfs_unregister_rqos(rqos);
5cf9c91b 145 mutex_unlock(&q->debugfs_mutex);
a7905043
JB
146}
147
84f60324
JB
148typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data);
149typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data);
150
151void rq_qos_wait(struct rq_wait *rqw, void *private_data,
152 acquire_inflight_cb_t *acquire_inflight_cb,
153 cleanup_cb_t *cleanup_cb);
22f17952 154bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit);
b84477d3
HS
155bool rq_depth_scale_up(struct rq_depth *rqd);
156bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle);
a7905043
JB
157bool rq_depth_calc_max_depth(struct rq_depth *rqd);
158
e5045454
JA
159void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio);
160void __rq_qos_done(struct rq_qos *rqos, struct request *rq);
161void __rq_qos_issue(struct rq_qos *rqos, struct request *rq);
162void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq);
163void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio);
164void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio);
d3e65fff 165void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio);
e5045454 166void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio);
9677a3e0 167void __rq_qos_queue_depth_changed(struct rq_qos *rqos);
e5045454
JA
168
169static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
170{
171 if (q->rq_qos)
172 __rq_qos_cleanup(q->rq_qos, bio);
173}
174
175static inline void rq_qos_done(struct request_queue *q, struct request *rq)
176{
177 if (q->rq_qos)
178 __rq_qos_done(q->rq_qos, rq);
179}
180
181static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
182{
183 if (q->rq_qos)
184 __rq_qos_issue(q->rq_qos, rq);
185}
186
187static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
188{
189 if (q->rq_qos)
190 __rq_qos_requeue(q->rq_qos, rq);
191}
192
aa1b46dc 193static inline void rq_qos_done_bio(struct bio *bio)
e5045454 194{
aa1b46dc
TH
195 if (bio->bi_bdev && (bio_flagged(bio, BIO_QOS_THROTTLED) ||
196 bio_flagged(bio, BIO_QOS_MERGED))) {
197 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
198 if (q->rq_qos)
199 __rq_qos_done_bio(q->rq_qos, bio);
200 }
e5045454
JA
201}
202
203static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
204{
90b8faa0 205 if (q->rq_qos) {
aa1b46dc 206 bio_set_flag(bio, BIO_QOS_THROTTLED);
e5045454 207 __rq_qos_throttle(q->rq_qos, bio);
90b8faa0 208 }
e5045454
JA
209}
210
211static inline void rq_qos_track(struct request_queue *q, struct request *rq,
212 struct bio *bio)
213{
214 if (q->rq_qos)
215 __rq_qos_track(q->rq_qos, rq, bio);
216}
217
d3e65fff
TH
218static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
219 struct bio *bio)
220{
aa1b46dc
TH
221 if (q->rq_qos) {
222 bio_set_flag(bio, BIO_QOS_MERGED);
d3e65fff 223 __rq_qos_merge(q->rq_qos, rq, bio);
aa1b46dc 224 }
d3e65fff
TH
225}
226
9677a3e0
TH
227static inline void rq_qos_queue_depth_changed(struct request_queue *q)
228{
229 if (q->rq_qos)
230 __rq_qos_queue_depth_changed(q->rq_qos);
231}
232
a7905043 233void rq_qos_exit(struct request_queue *);
e5045454 234
a7905043 235#endif