blob: 5f8b75826a988a5d30a0ee67b3505229ee1ef742 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001/* SPDX-License-Identifier: GPL-2.0 */
Josef Bacika7905042018-07-03 09:32:35 -06002#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>
10
Ming Leicc566942018-12-17 09:46:00 +080011#include "blk-mq-debugfs.h"
12
13struct blk_mq_debugfs_attr;
14
Josef Bacika7905042018-07-03 09:32:35 -060015enum rq_qos_id {
16 RQ_QOS_WBT,
Tejun Heobeab17f2019-08-28 15:05:56 -070017 RQ_QOS_LATENCY,
Josef Bacika7905042018-07-03 09:32:35 -060018};
19
20struct rq_wait {
21 wait_queue_head_t wait;
22 atomic_t inflight;
23};
24
25struct rq_qos {
26 struct rq_qos_ops *ops;
27 struct request_queue *q;
28 enum rq_qos_id id;
29 struct rq_qos *next;
Ming Leicc566942018-12-17 09:46:00 +080030#ifdef CONFIG_BLK_DEBUG_FS
31 struct dentry *debugfs_dir;
32#endif
Josef Bacika7905042018-07-03 09:32:35 -060033};
34
35struct rq_qos_ops {
Christoph Hellwigd5337562018-11-14 17:02:09 +010036 void (*throttle)(struct rq_qos *, struct bio *);
Josef Bacikc1c80382018-07-03 11:14:59 -040037 void (*track)(struct rq_qos *, struct request *, struct bio *);
Tejun Heod3e65ff2019-08-28 15:05:54 -070038 void (*merge)(struct rq_qos *, struct request *, struct bio *);
Josef Bacika7905042018-07-03 09:32:35 -060039 void (*issue)(struct rq_qos *, struct request *);
40 void (*requeue)(struct rq_qos *, struct request *);
41 void (*done)(struct rq_qos *, struct request *);
Josef Bacik67b42d02018-07-03 11:15:00 -040042 void (*done_bio)(struct rq_qos *, struct bio *);
Josef Bacikc1c80382018-07-03 11:14:59 -040043 void (*cleanup)(struct rq_qos *, struct bio *);
Tejun Heo9677a3e2019-08-28 15:05:55 -070044 void (*queue_depth_changed)(struct rq_qos *);
Josef Bacika7905042018-07-03 09:32:35 -060045 void (*exit)(struct rq_qos *);
Ming Leicc566942018-12-17 09:46:00 +080046 const struct blk_mq_debugfs_attr *debugfs_attrs;
Josef Bacika7905042018-07-03 09:32:35 -060047};
48
49struct rq_depth {
50 unsigned int max_depth;
51
52 int scale_step;
53 bool scaled_max;
54
55 unsigned int queue_depth;
56 unsigned int default_depth;
57};
58
59static inline struct rq_qos *rq_qos_id(struct request_queue *q,
60 enum rq_qos_id id)
61{
62 struct rq_qos *rqos;
63 for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
64 if (rqos->id == id)
65 break;
66 }
67 return rqos;
68}
69
70static inline struct rq_qos *wbt_rq_qos(struct request_queue *q)
71{
72 return rq_qos_id(q, RQ_QOS_WBT);
73}
74
75static inline struct rq_qos *blkcg_rq_qos(struct request_queue *q)
76{
Tejun Heobeab17f2019-08-28 15:05:56 -070077 return rq_qos_id(q, RQ_QOS_LATENCY);
Josef Bacika7905042018-07-03 09:32:35 -060078}
79
Ming Leicc566942018-12-17 09:46:00 +080080static inline const char *rq_qos_id_to_name(enum rq_qos_id id)
81{
82 switch (id) {
83 case RQ_QOS_WBT:
84 return "wbt";
Tejun Heobeab17f2019-08-28 15:05:56 -070085 case RQ_QOS_LATENCY:
86 return "latency";
Ming Leicc566942018-12-17 09:46:00 +080087 }
88 return "unknown";
89}
90
Josef Bacika7905042018-07-03 09:32:35 -060091static inline void rq_wait_init(struct rq_wait *rq_wait)
92{
93 atomic_set(&rq_wait->inflight, 0);
94 init_waitqueue_head(&rq_wait->wait);
95}
96
97static inline void rq_qos_add(struct request_queue *q, struct rq_qos *rqos)
98{
99 rqos->next = q->rq_qos;
100 q->rq_qos = rqos;
Ming Leicc566942018-12-17 09:46:00 +0800101
102 if (rqos->ops->debugfs_attrs)
103 blk_mq_debugfs_register_rqos(rqos);
Josef Bacika7905042018-07-03 09:32:35 -0600104}
105
106static inline void rq_qos_del(struct request_queue *q, struct rq_qos *rqos)
107{
108 struct rq_qos *cur, *prev = NULL;
109 for (cur = q->rq_qos; cur; cur = cur->next) {
110 if (cur == rqos) {
111 if (prev)
112 prev->next = rqos->next;
113 else
114 q->rq_qos = cur;
115 break;
116 }
117 prev = cur;
118 }
Ming Leicc566942018-12-17 09:46:00 +0800119
120 blk_mq_debugfs_unregister_rqos(rqos);
Josef Bacika7905042018-07-03 09:32:35 -0600121}
122
Josef Bacik84f60322018-12-04 12:59:02 -0500123typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data);
124typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data);
125
126void rq_qos_wait(struct rq_wait *rqw, void *private_data,
127 acquire_inflight_cb_t *acquire_inflight_cb,
128 cleanup_cb_t *cleanup_cb);
Josef Bacik22f17952018-07-19 21:42:13 -0400129bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit);
Josef Bacika7905042018-07-03 09:32:35 -0600130void rq_depth_scale_up(struct rq_depth *rqd);
131void rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle);
132bool rq_depth_calc_max_depth(struct rq_depth *rqd);
133
Jens Axboee5045452018-11-15 12:25:10 -0700134void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio);
135void __rq_qos_done(struct rq_qos *rqos, struct request *rq);
136void __rq_qos_issue(struct rq_qos *rqos, struct request *rq);
137void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq);
138void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio);
139void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio);
Tejun Heod3e65ff2019-08-28 15:05:54 -0700140void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio);
Jens Axboee5045452018-11-15 12:25:10 -0700141void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio);
Tejun Heo9677a3e2019-08-28 15:05:55 -0700142void __rq_qos_queue_depth_changed(struct rq_qos *rqos);
Jens Axboee5045452018-11-15 12:25:10 -0700143
144static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
145{
146 if (q->rq_qos)
147 __rq_qos_cleanup(q->rq_qos, bio);
148}
149
150static inline void rq_qos_done(struct request_queue *q, struct request *rq)
151{
152 if (q->rq_qos)
153 __rq_qos_done(q->rq_qos, rq);
154}
155
156static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
157{
158 if (q->rq_qos)
159 __rq_qos_issue(q->rq_qos, rq);
160}
161
162static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
163{
164 if (q->rq_qos)
165 __rq_qos_requeue(q->rq_qos, rq);
166}
167
168static inline void rq_qos_done_bio(struct request_queue *q, struct bio *bio)
169{
170 if (q->rq_qos)
171 __rq_qos_done_bio(q->rq_qos, bio);
172}
173
174static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
175{
Dennis Zhou13369812018-12-17 11:03:51 -0500176 /*
177 * BIO_TRACKED lets controllers know that a bio went through the
178 * normal rq_qos path.
179 */
180 bio_set_flag(bio, BIO_TRACKED);
Jens Axboee5045452018-11-15 12:25:10 -0700181 if (q->rq_qos)
182 __rq_qos_throttle(q->rq_qos, bio);
183}
184
185static inline void rq_qos_track(struct request_queue *q, struct request *rq,
186 struct bio *bio)
187{
188 if (q->rq_qos)
189 __rq_qos_track(q->rq_qos, rq, bio);
190}
191
Tejun Heod3e65ff2019-08-28 15:05:54 -0700192static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
193 struct bio *bio)
194{
195 if (q->rq_qos)
196 __rq_qos_merge(q->rq_qos, rq, bio);
197}
198
Tejun Heo9677a3e2019-08-28 15:05:55 -0700199static inline void rq_qos_queue_depth_changed(struct request_queue *q)
200{
201 if (q->rq_qos)
202 __rq_qos_queue_depth_changed(q->rq_qos);
203}
204
Josef Bacika7905042018-07-03 09:32:35 -0600205void rq_qos_exit(struct request_queue *);
Jens Axboee5045452018-11-15 12:25:10 -0700206
Josef Bacika7905042018-07-03 09:32:35 -0600207#endif