blob: b7b02e04f64f655bb2511a9ba78dacec603b43f4 [file] [log] [blame]
Josef Bacika7905042018-07-03 09:32:35 -06001#include "blk-rq-qos.h"
2
Josef Bacika7905042018-07-03 09:32:35 -06003/*
4 * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
5 * false if 'v' + 1 would be bigger than 'below'.
6 */
7static bool atomic_inc_below(atomic_t *v, int below)
8{
9 int cur = atomic_read(v);
10
11 for (;;) {
12 int old;
13
14 if (cur >= below)
15 return false;
16 old = atomic_cmpxchg(v, cur, cur + 1);
17 if (old == cur)
18 break;
19 cur = old;
20 }
21
22 return true;
23}
24
25bool rq_wait_inc_below(struct rq_wait *rq_wait, int limit)
26{
27 return atomic_inc_below(&rq_wait->inflight, limit);
28}
29
Josef Bacikc1c80382018-07-03 11:14:59 -040030void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
Josef Bacika7905042018-07-03 09:32:35 -060031{
32 struct rq_qos *rqos;
33
34 for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
35 if (rqos->ops->cleanup)
Josef Bacikc1c80382018-07-03 11:14:59 -040036 rqos->ops->cleanup(rqos, bio);
Josef Bacika7905042018-07-03 09:32:35 -060037 }
38}
39
40void rq_qos_done(struct request_queue *q, struct request *rq)
41{
42 struct rq_qos *rqos;
43
44 for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
45 if (rqos->ops->done)
46 rqos->ops->done(rqos, rq);
47 }
48}
49
50void rq_qos_issue(struct request_queue *q, struct request *rq)
51{
52 struct rq_qos *rqos;
53
54 for(rqos = q->rq_qos; rqos; rqos = rqos->next) {
55 if (rqos->ops->issue)
56 rqos->ops->issue(rqos, rq);
57 }
58}
59
60void rq_qos_requeue(struct request_queue *q, struct request *rq)
61{
62 struct rq_qos *rqos;
63
64 for(rqos = q->rq_qos; rqos; rqos = rqos->next) {
65 if (rqos->ops->requeue)
66 rqos->ops->requeue(rqos, rq);
67 }
68}
69
Josef Bacikc1c80382018-07-03 11:14:59 -040070void rq_qos_throttle(struct request_queue *q, struct bio *bio,
71 spinlock_t *lock)
Josef Bacika7905042018-07-03 09:32:35 -060072{
73 struct rq_qos *rqos;
Josef Bacika7905042018-07-03 09:32:35 -060074
75 for(rqos = q->rq_qos; rqos; rqos = rqos->next) {
76 if (rqos->ops->throttle)
Josef Bacikc1c80382018-07-03 11:14:59 -040077 rqos->ops->throttle(rqos, bio, lock);
Josef Bacika7905042018-07-03 09:32:35 -060078 }
Josef Bacikc1c80382018-07-03 11:14:59 -040079}
80
81void rq_qos_track(struct request_queue *q, struct request *rq, struct bio *bio)
82{
83 struct rq_qos *rqos;
84
85 for(rqos = q->rq_qos; rqos; rqos = rqos->next) {
86 if (rqos->ops->track)
87 rqos->ops->track(rqos, rq, bio);
88 }
Josef Bacika7905042018-07-03 09:32:35 -060089}
90
91/*
92 * Return true, if we can't increase the depth further by scaling
93 */
94bool rq_depth_calc_max_depth(struct rq_depth *rqd)
95{
96 unsigned int depth;
97 bool ret = false;
98
99 /*
100 * For QD=1 devices, this is a special case. It's important for those
101 * to have one request ready when one completes, so force a depth of
102 * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
103 * since the device can't have more than that in flight. If we're
104 * scaling down, then keep a setting of 1/1/1.
105 */
106 if (rqd->queue_depth == 1) {
107 if (rqd->scale_step > 0)
108 rqd->max_depth = 1;
109 else {
110 rqd->max_depth = 2;
111 ret = true;
112 }
113 } else {
114 /*
115 * scale_step == 0 is our default state. If we have suffered
116 * latency spikes, step will be > 0, and we shrink the
117 * allowed write depths. If step is < 0, we're only doing
118 * writes, and we allow a temporarily higher depth to
119 * increase performance.
120 */
121 depth = min_t(unsigned int, rqd->default_depth,
122 rqd->queue_depth);
123 if (rqd->scale_step > 0)
124 depth = 1 + ((depth - 1) >> min(31, rqd->scale_step));
125 else if (rqd->scale_step < 0) {
126 unsigned int maxd = 3 * rqd->queue_depth / 4;
127
128 depth = 1 + ((depth - 1) << -rqd->scale_step);
129 if (depth > maxd) {
130 depth = maxd;
131 ret = true;
132 }
133 }
134
135 rqd->max_depth = depth;
136 }
137
138 return ret;
139}
140
141void rq_depth_scale_up(struct rq_depth *rqd)
142{
143 /*
144 * Hit max in previous round, stop here
145 */
146 if (rqd->scaled_max)
147 return;
148
149 rqd->scale_step--;
150
151 rqd->scaled_max = rq_depth_calc_max_depth(rqd);
152}
153
154/*
155 * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
156 * had a latency violation.
157 */
158void rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle)
159{
160 /*
161 * Stop scaling down when we've hit the limit. This also prevents
162 * ->scale_step from going to crazy values, if the device can't
163 * keep up.
164 */
165 if (rqd->max_depth == 1)
166 return;
167
168 if (rqd->scale_step < 0 && hard_throttle)
169 rqd->scale_step = 0;
170 else
171 rqd->scale_step++;
172
173 rqd->scaled_max = false;
174 rq_depth_calc_max_depth(rqd);
175}
176
177void rq_qos_exit(struct request_queue *q)
178{
179 while (q->rq_qos) {
180 struct rq_qos *rqos = q->rq_qos;
181 q->rq_qos = rqos->next;
182 rqos->ops->exit(rqos);
183 }
184}