Blame view

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