block/rq_qos: add rq_qos_merge()
[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>
10
cc56694f
ML
11#include "blk-mq-debugfs.h"
12
13struct blk_mq_debugfs_attr;
14
a7905043
JB
15enum rq_qos_id {
16 RQ_QOS_WBT,
17 RQ_QOS_CGROUP,
18};
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;
cc56694f
ML
30#ifdef CONFIG_BLK_DEBUG_FS
31 struct dentry *debugfs_dir;
32#endif
a7905043
JB
33};
34
35struct rq_qos_ops {
d5337560 36 void (*throttle)(struct rq_qos *, struct bio *);
c1c80384 37 void (*track)(struct rq_qos *, struct request *, struct bio *);
d3e65fff 38 void (*merge)(struct rq_qos *, struct request *, struct bio *);
a7905043
JB
39 void (*issue)(struct rq_qos *, struct request *);
40 void (*requeue)(struct rq_qos *, struct request *);
41 void (*done)(struct rq_qos *, struct request *);
67b42d0b 42 void (*done_bio)(struct rq_qos *, struct bio *);
c1c80384 43 void (*cleanup)(struct rq_qos *, struct bio *);
a7905043 44 void (*exit)(struct rq_qos *);
cc56694f 45 const struct blk_mq_debugfs_attr *debugfs_attrs;
a7905043
JB
46};
47
48struct rq_depth {
49 unsigned int max_depth;
50
51 int scale_step;
52 bool scaled_max;
53
54 unsigned int queue_depth;
55 unsigned int default_depth;
56};
57
58static inline struct rq_qos *rq_qos_id(struct request_queue *q,
59 enum rq_qos_id id)
60{
61 struct rq_qos *rqos;
62 for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
63 if (rqos->id == id)
64 break;
65 }
66 return rqos;
67}
68
69static inline struct rq_qos *wbt_rq_qos(struct request_queue *q)
70{
71 return rq_qos_id(q, RQ_QOS_WBT);
72}
73
74static inline struct rq_qos *blkcg_rq_qos(struct request_queue *q)
75{
76 return rq_qos_id(q, RQ_QOS_CGROUP);
77}
78
cc56694f
ML
79static inline const char *rq_qos_id_to_name(enum rq_qos_id id)
80{
81 switch (id) {
82 case RQ_QOS_WBT:
83 return "wbt";
84 case RQ_QOS_CGROUP:
85 return "cgroup";
86 }
87 return "unknown";
88}
89
a7905043
JB
90static inline void rq_wait_init(struct rq_wait *rq_wait)
91{
92 atomic_set(&rq_wait->inflight, 0);
93 init_waitqueue_head(&rq_wait->wait);
94}
95
96static inline void rq_qos_add(struct request_queue *q, struct rq_qos *rqos)
97{
98 rqos->next = q->rq_qos;
99 q->rq_qos = rqos;
cc56694f
ML
100
101 if (rqos->ops->debugfs_attrs)
102 blk_mq_debugfs_register_rqos(rqos);
a7905043
JB
103}
104
105static inline void rq_qos_del(struct request_queue *q, struct rq_qos *rqos)
106{
107 struct rq_qos *cur, *prev = NULL;
108 for (cur = q->rq_qos; cur; cur = cur->next) {
109 if (cur == rqos) {
110 if (prev)
111 prev->next = rqos->next;
112 else
113 q->rq_qos = cur;
114 break;
115 }
116 prev = cur;
117 }
cc56694f
ML
118
119 blk_mq_debugfs_unregister_rqos(rqos);
a7905043
JB
120}
121
84f60324
JB
122typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data);
123typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data);
124
125void rq_qos_wait(struct rq_wait *rqw, void *private_data,
126 acquire_inflight_cb_t *acquire_inflight_cb,
127 cleanup_cb_t *cleanup_cb);
22f17952 128bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit);
a7905043
JB
129void rq_depth_scale_up(struct rq_depth *rqd);
130void rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle);
131bool rq_depth_calc_max_depth(struct rq_depth *rqd);
132
e5045454
JA
133void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio);
134void __rq_qos_done(struct rq_qos *rqos, struct request *rq);
135void __rq_qos_issue(struct rq_qos *rqos, struct request *rq);
136void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq);
137void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio);
138void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio);
d3e65fff 139void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio);
e5045454
JA
140void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio);
141
142static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
143{
144 if (q->rq_qos)
145 __rq_qos_cleanup(q->rq_qos, bio);
146}
147
148static inline void rq_qos_done(struct request_queue *q, struct request *rq)
149{
150 if (q->rq_qos)
151 __rq_qos_done(q->rq_qos, rq);
152}
153
154static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
155{
156 if (q->rq_qos)
157 __rq_qos_issue(q->rq_qos, rq);
158}
159
160static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
161{
162 if (q->rq_qos)
163 __rq_qos_requeue(q->rq_qos, rq);
164}
165
166static inline void rq_qos_done_bio(struct request_queue *q, struct bio *bio)
167{
168 if (q->rq_qos)
169 __rq_qos_done_bio(q->rq_qos, bio);
170}
171
172static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
173{
13369816
DZ
174 /*
175 * BIO_TRACKED lets controllers know that a bio went through the
176 * normal rq_qos path.
177 */
178 bio_set_flag(bio, BIO_TRACKED);
e5045454
JA
179 if (q->rq_qos)
180 __rq_qos_throttle(q->rq_qos, bio);
181}
182
183static inline void rq_qos_track(struct request_queue *q, struct request *rq,
184 struct bio *bio)
185{
186 if (q->rq_qos)
187 __rq_qos_track(q->rq_qos, rq, bio);
188}
189
d3e65fff
TH
190static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
191 struct bio *bio)
192{
193 if (q->rq_qos)
194 __rq_qos_merge(q->rq_qos, rq, bio);
195}
196
a7905043 197void rq_qos_exit(struct request_queue *);
e5045454 198
a7905043 199#endif