blob: 43bcd4e7a7f9ae5da1fa005b5bca0110e7be9aa9 [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 */
Josef Bacik22f17952018-07-19 21:42:13 -04007static bool atomic_inc_below(atomic_t *v, unsigned int below)
Josef Bacika7905042018-07-03 09:32:35 -06008{
Josef Bacik22f17952018-07-19 21:42:13 -04009 unsigned int cur = atomic_read(v);
Josef Bacika7905042018-07-03 09:32:35 -060010
11 for (;;) {
Josef Bacik22f17952018-07-19 21:42:13 -040012 unsigned int old;
Josef Bacika7905042018-07-03 09:32:35 -060013
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
Josef Bacik22f17952018-07-19 21:42:13 -040025bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit)
Josef Bacika7905042018-07-03 09:32:35 -060026{
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
Josef Bacik67b42d02018-07-03 11:15:00 -040091void rq_qos_done_bio(struct request_queue *q, struct bio *bio)
92{
93 struct rq_qos *rqos;
94
95 for(rqos = q->rq_qos; rqos; rqos = rqos->next) {
96 if (rqos->ops->done_bio)
97 rqos->ops->done_bio(rqos, bio);
98 }
99}
100
Josef Bacika7905042018-07-03 09:32:35 -0600101/*
102 * Return true, if we can't increase the depth further by scaling
103 */
104bool rq_depth_calc_max_depth(struct rq_depth *rqd)
105{
106 unsigned int depth;
107 bool ret = false;
108
109 /*
110 * For QD=1 devices, this is a special case. It's important for those
111 * to have one request ready when one completes, so force a depth of
112 * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
113 * since the device can't have more than that in flight. If we're
114 * scaling down, then keep a setting of 1/1/1.
115 */
116 if (rqd->queue_depth == 1) {
117 if (rqd->scale_step > 0)
118 rqd->max_depth = 1;
119 else {
120 rqd->max_depth = 2;
121 ret = true;
122 }
123 } else {
124 /*
125 * scale_step == 0 is our default state. If we have suffered
126 * latency spikes, step will be > 0, and we shrink the
127 * allowed write depths. If step is < 0, we're only doing
128 * writes, and we allow a temporarily higher depth to
129 * increase performance.
130 */
131 depth = min_t(unsigned int, rqd->default_depth,
132 rqd->queue_depth);
133 if (rqd->scale_step > 0)
134 depth = 1 + ((depth - 1) >> min(31, rqd->scale_step));
135 else if (rqd->scale_step < 0) {
136 unsigned int maxd = 3 * rqd->queue_depth / 4;
137
138 depth = 1 + ((depth - 1) << -rqd->scale_step);
139 if (depth > maxd) {
140 depth = maxd;
141 ret = true;
142 }
143 }
144
145 rqd->max_depth = depth;
146 }
147
148 return ret;
149}
150
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700151/* Returns true on success and false if scaling up wasn't possible */
152bool rq_depth_scale_up(struct rq_depth *rqd)
Josef Bacika7905042018-07-03 09:32:35 -0600153{
154 /*
155 * Hit max in previous round, stop here
156 */
157 if (rqd->scaled_max)
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700158 return false;
Josef Bacika7905042018-07-03 09:32:35 -0600159
160 rqd->scale_step--;
161
162 rqd->scaled_max = rq_depth_calc_max_depth(rqd);
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700163 return true;
Josef Bacika7905042018-07-03 09:32:35 -0600164}
165
166/*
167 * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700168 * had a latency violation. Returns true on success and returns false if
169 * scaling down wasn't possible.
Josef Bacika7905042018-07-03 09:32:35 -0600170 */
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700171bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle)
Josef Bacika7905042018-07-03 09:32:35 -0600172{
173 /*
174 * Stop scaling down when we've hit the limit. This also prevents
175 * ->scale_step from going to crazy values, if the device can't
176 * keep up.
177 */
178 if (rqd->max_depth == 1)
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700179 return false;
Josef Bacika7905042018-07-03 09:32:35 -0600180
181 if (rqd->scale_step < 0 && hard_throttle)
182 rqd->scale_step = 0;
183 else
184 rqd->scale_step++;
185
186 rqd->scaled_max = false;
187 rq_depth_calc_max_depth(rqd);
Harshad Shirwadkar345c03a2019-10-05 11:59:27 -0700188 return true;
Josef Bacika7905042018-07-03 09:32:35 -0600189}
190
191void rq_qos_exit(struct request_queue *q)
192{
193 while (q->rq_qos) {
194 struct rq_qos *rqos = q->rq_qos;
195 q->rq_qos = rqos->next;
196 rqos->ops->exit(rqos);
197 }
198}