]>
Commit | Line | Data |
---|---|---|
31e4c28d VG |
1 | /* |
2 | * Common Block IO controller cgroup interface | |
3 | * | |
4 | * Based on ideas and code from CFQ, CFS and BFQ: | |
5 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> | |
6 | * | |
7 | * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it> | |
8 | * Paolo Valente <paolo.valente@unimore.it> | |
9 | * | |
10 | * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com> | |
11 | * Nauman Rafique <nauman@google.com> | |
12 | */ | |
13 | #include <linux/ioprio.h> | |
22084190 VG |
14 | #include <linux/seq_file.h> |
15 | #include <linux/kdev_t.h> | |
9d6a986c | 16 | #include <linux/module.h> |
accee785 | 17 | #include <linux/err.h> |
9195291e | 18 | #include <linux/blkdev.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
34d0f179 | 20 | #include <linux/genhd.h> |
72e06c25 TH |
21 | #include <linux/delay.h> |
22 | #include "blk-cgroup.h" | |
5efd6113 | 23 | #include "blk.h" |
3e252066 | 24 | |
84c124da DS |
25 | #define MAX_KEY_LEN 100 |
26 | ||
3e252066 VG |
27 | static DEFINE_SPINLOCK(blkio_list_lock); |
28 | static LIST_HEAD(blkio_list); | |
b1c35769 | 29 | |
923adde1 TH |
30 | static DEFINE_MUTEX(all_q_mutex); |
31 | static LIST_HEAD(all_q_list); | |
32 | ||
31e4c28d | 33 | struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT }; |
9d6a986c VG |
34 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
35 | ||
035d10b2 TH |
36 | static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES]; |
37 | ||
67523c48 BB |
38 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *, |
39 | struct cgroup *); | |
bb9d97b6 TH |
40 | static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *, |
41 | struct cgroup_taskset *); | |
42 | static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *, | |
43 | struct cgroup_taskset *); | |
7ee9c562 | 44 | static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *); |
67523c48 BB |
45 | static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *); |
46 | static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *); | |
47 | ||
062a644d VG |
48 | /* for encoding cft->private value on file */ |
49 | #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val)) | |
50 | /* What policy owns the file, proportional or throttle */ | |
51 | #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff) | |
52 | #define BLKIOFILE_ATTR(val) ((val) & 0xffff) | |
53 | ||
67523c48 BB |
54 | struct cgroup_subsys blkio_subsys = { |
55 | .name = "blkio", | |
56 | .create = blkiocg_create, | |
bb9d97b6 TH |
57 | .can_attach = blkiocg_can_attach, |
58 | .attach = blkiocg_attach, | |
7ee9c562 | 59 | .pre_destroy = blkiocg_pre_destroy, |
67523c48 BB |
60 | .destroy = blkiocg_destroy, |
61 | .populate = blkiocg_populate, | |
67523c48 | 62 | .subsys_id = blkio_subsys_id, |
67523c48 BB |
63 | .module = THIS_MODULE, |
64 | }; | |
65 | EXPORT_SYMBOL_GPL(blkio_subsys); | |
66 | ||
31e4c28d VG |
67 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
68 | { | |
69 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), | |
70 | struct blkio_cgroup, css); | |
71 | } | |
9d6a986c | 72 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
31e4c28d | 73 | |
70087dc3 VG |
74 | struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk) |
75 | { | |
76 | return container_of(task_subsys_state(tsk, blkio_subsys_id), | |
77 | struct blkio_cgroup, css); | |
78 | } | |
79 | EXPORT_SYMBOL_GPL(task_blkio_cgroup); | |
80 | ||
c1768268 TH |
81 | static inline void blkio_update_group_weight(struct blkio_group *blkg, |
82 | int plid, unsigned int weight) | |
062a644d VG |
83 | { |
84 | struct blkio_policy_type *blkiop; | |
85 | ||
86 | list_for_each_entry(blkiop, &blkio_list, list) { | |
87 | /* If this policy does not own the blkg, do not send updates */ | |
c1768268 | 88 | if (blkiop->plid != plid) |
062a644d VG |
89 | continue; |
90 | if (blkiop->ops.blkio_update_group_weight_fn) | |
ca32aefc | 91 | blkiop->ops.blkio_update_group_weight_fn(blkg->q, |
fe071437 | 92 | blkg, weight); |
062a644d VG |
93 | } |
94 | } | |
95 | ||
c1768268 TH |
96 | static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid, |
97 | u64 bps, int fileid) | |
4c9eefa1 VG |
98 | { |
99 | struct blkio_policy_type *blkiop; | |
100 | ||
101 | list_for_each_entry(blkiop, &blkio_list, list) { | |
102 | ||
103 | /* If this policy does not own the blkg, do not send updates */ | |
c1768268 | 104 | if (blkiop->plid != plid) |
4c9eefa1 VG |
105 | continue; |
106 | ||
107 | if (fileid == BLKIO_THROTL_read_bps_device | |
108 | && blkiop->ops.blkio_update_group_read_bps_fn) | |
ca32aefc | 109 | blkiop->ops.blkio_update_group_read_bps_fn(blkg->q, |
fe071437 | 110 | blkg, bps); |
4c9eefa1 VG |
111 | |
112 | if (fileid == BLKIO_THROTL_write_bps_device | |
113 | && blkiop->ops.blkio_update_group_write_bps_fn) | |
ca32aefc | 114 | blkiop->ops.blkio_update_group_write_bps_fn(blkg->q, |
fe071437 | 115 | blkg, bps); |
4c9eefa1 VG |
116 | } |
117 | } | |
118 | ||
7702e8f4 | 119 | static inline void blkio_update_group_iops(struct blkio_group *blkg, |
c1768268 TH |
120 | int plid, unsigned int iops, |
121 | int fileid) | |
7702e8f4 VG |
122 | { |
123 | struct blkio_policy_type *blkiop; | |
124 | ||
125 | list_for_each_entry(blkiop, &blkio_list, list) { | |
126 | ||
127 | /* If this policy does not own the blkg, do not send updates */ | |
c1768268 | 128 | if (blkiop->plid != plid) |
7702e8f4 VG |
129 | continue; |
130 | ||
131 | if (fileid == BLKIO_THROTL_read_iops_device | |
132 | && blkiop->ops.blkio_update_group_read_iops_fn) | |
ca32aefc | 133 | blkiop->ops.blkio_update_group_read_iops_fn(blkg->q, |
fe071437 | 134 | blkg, iops); |
7702e8f4 VG |
135 | |
136 | if (fileid == BLKIO_THROTL_write_iops_device | |
137 | && blkiop->ops.blkio_update_group_write_iops_fn) | |
ca32aefc | 138 | blkiop->ops.blkio_update_group_write_iops_fn(blkg->q, |
fe071437 | 139 | blkg,iops); |
7702e8f4 VG |
140 | } |
141 | } | |
142 | ||
9195291e DS |
143 | /* |
144 | * Add to the appropriate stat variable depending on the request type. | |
145 | * This should be called with the blkg->stats_lock held. | |
146 | */ | |
84c124da DS |
147 | static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction, |
148 | bool sync) | |
9195291e | 149 | { |
84c124da DS |
150 | if (direction) |
151 | stat[BLKIO_STAT_WRITE] += add; | |
9195291e | 152 | else |
84c124da DS |
153 | stat[BLKIO_STAT_READ] += add; |
154 | if (sync) | |
155 | stat[BLKIO_STAT_SYNC] += add; | |
9195291e | 156 | else |
84c124da | 157 | stat[BLKIO_STAT_ASYNC] += add; |
9195291e DS |
158 | } |
159 | ||
cdc1184c DS |
160 | /* |
161 | * Decrements the appropriate stat variable if non-zero depending on the | |
162 | * request type. Panics on value being zero. | |
163 | * This should be called with the blkg->stats_lock held. | |
164 | */ | |
165 | static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync) | |
166 | { | |
167 | if (direction) { | |
168 | BUG_ON(stat[BLKIO_STAT_WRITE] == 0); | |
169 | stat[BLKIO_STAT_WRITE]--; | |
170 | } else { | |
171 | BUG_ON(stat[BLKIO_STAT_READ] == 0); | |
172 | stat[BLKIO_STAT_READ]--; | |
173 | } | |
174 | if (sync) { | |
175 | BUG_ON(stat[BLKIO_STAT_SYNC] == 0); | |
176 | stat[BLKIO_STAT_SYNC]--; | |
177 | } else { | |
178 | BUG_ON(stat[BLKIO_STAT_ASYNC] == 0); | |
179 | stat[BLKIO_STAT_ASYNC]--; | |
180 | } | |
181 | } | |
182 | ||
183 | #ifdef CONFIG_DEBUG_BLK_CGROUP | |
812df48d DS |
184 | /* This should be called with the blkg->stats_lock held. */ |
185 | static void blkio_set_start_group_wait_time(struct blkio_group *blkg, | |
c1768268 TH |
186 | struct blkio_policy_type *pol, |
187 | struct blkio_group *curr_blkg) | |
812df48d | 188 | { |
c1768268 | 189 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
549d3aa8 TH |
190 | |
191 | if (blkio_blkg_waiting(&pd->stats)) | |
812df48d DS |
192 | return; |
193 | if (blkg == curr_blkg) | |
194 | return; | |
549d3aa8 TH |
195 | pd->stats.start_group_wait_time = sched_clock(); |
196 | blkio_mark_blkg_waiting(&pd->stats); | |
812df48d DS |
197 | } |
198 | ||
199 | /* This should be called with the blkg->stats_lock held. */ | |
200 | static void blkio_update_group_wait_time(struct blkio_group_stats *stats) | |
201 | { | |
202 | unsigned long long now; | |
203 | ||
204 | if (!blkio_blkg_waiting(stats)) | |
205 | return; | |
206 | ||
207 | now = sched_clock(); | |
208 | if (time_after64(now, stats->start_group_wait_time)) | |
209 | stats->group_wait_time += now - stats->start_group_wait_time; | |
210 | blkio_clear_blkg_waiting(stats); | |
211 | } | |
212 | ||
213 | /* This should be called with the blkg->stats_lock held. */ | |
214 | static void blkio_end_empty_time(struct blkio_group_stats *stats) | |
215 | { | |
216 | unsigned long long now; | |
217 | ||
218 | if (!blkio_blkg_empty(stats)) | |
219 | return; | |
220 | ||
221 | now = sched_clock(); | |
222 | if (time_after64(now, stats->start_empty_time)) | |
223 | stats->empty_time += now - stats->start_empty_time; | |
224 | blkio_clear_blkg_empty(stats); | |
225 | } | |
226 | ||
c1768268 TH |
227 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg, |
228 | struct blkio_policy_type *pol) | |
812df48d | 229 | { |
c1768268 | 230 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
812df48d DS |
231 | unsigned long flags; |
232 | ||
233 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 TH |
234 | BUG_ON(blkio_blkg_idling(&pd->stats)); |
235 | pd->stats.start_idle_time = sched_clock(); | |
236 | blkio_mark_blkg_idling(&pd->stats); | |
812df48d DS |
237 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
238 | } | |
239 | EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats); | |
240 | ||
c1768268 TH |
241 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg, |
242 | struct blkio_policy_type *pol) | |
812df48d | 243 | { |
c1768268 | 244 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
812df48d DS |
245 | unsigned long flags; |
246 | unsigned long long now; | |
247 | struct blkio_group_stats *stats; | |
248 | ||
249 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 250 | stats = &pd->stats; |
812df48d DS |
251 | if (blkio_blkg_idling(stats)) { |
252 | now = sched_clock(); | |
253 | if (time_after64(now, stats->start_idle_time)) | |
254 | stats->idle_time += now - stats->start_idle_time; | |
255 | blkio_clear_blkg_idling(stats); | |
256 | } | |
257 | spin_unlock_irqrestore(&blkg->stats_lock, flags); | |
258 | } | |
259 | EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats); | |
260 | ||
c1768268 TH |
261 | void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg, |
262 | struct blkio_policy_type *pol) | |
cdc1184c | 263 | { |
c1768268 | 264 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
cdc1184c DS |
265 | unsigned long flags; |
266 | struct blkio_group_stats *stats; | |
267 | ||
268 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 269 | stats = &pd->stats; |
cdc1184c DS |
270 | stats->avg_queue_size_sum += |
271 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] + | |
272 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]; | |
273 | stats->avg_queue_size_samples++; | |
812df48d | 274 | blkio_update_group_wait_time(stats); |
cdc1184c DS |
275 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
276 | } | |
a11cdaa7 DS |
277 | EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats); |
278 | ||
c1768268 TH |
279 | void blkiocg_set_start_empty_time(struct blkio_group *blkg, |
280 | struct blkio_policy_type *pol) | |
28baf442 | 281 | { |
c1768268 | 282 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
28baf442 DS |
283 | unsigned long flags; |
284 | struct blkio_group_stats *stats; | |
285 | ||
286 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 287 | stats = &pd->stats; |
28baf442 DS |
288 | |
289 | if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] || | |
290 | stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) { | |
291 | spin_unlock_irqrestore(&blkg->stats_lock, flags); | |
292 | return; | |
293 | } | |
294 | ||
295 | /* | |
e5ff082e VG |
296 | * group is already marked empty. This can happen if cfqq got new |
297 | * request in parent group and moved to this group while being added | |
298 | * to service tree. Just ignore the event and move on. | |
28baf442 | 299 | */ |
e5ff082e VG |
300 | if(blkio_blkg_empty(stats)) { |
301 | spin_unlock_irqrestore(&blkg->stats_lock, flags); | |
302 | return; | |
303 | } | |
304 | ||
28baf442 DS |
305 | stats->start_empty_time = sched_clock(); |
306 | blkio_mark_blkg_empty(stats); | |
307 | spin_unlock_irqrestore(&blkg->stats_lock, flags); | |
308 | } | |
309 | EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time); | |
310 | ||
a11cdaa7 | 311 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
c1768268 TH |
312 | struct blkio_policy_type *pol, |
313 | unsigned long dequeue) | |
a11cdaa7 | 314 | { |
c1768268 | 315 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
549d3aa8 TH |
316 | |
317 | pd->stats.dequeue += dequeue; | |
a11cdaa7 DS |
318 | } |
319 | EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats); | |
812df48d DS |
320 | #else |
321 | static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg, | |
c1768268 TH |
322 | struct blkio_policy_type *pol, |
323 | struct blkio_group *curr_blkg) { } | |
324 | static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { } | |
cdc1184c DS |
325 | #endif |
326 | ||
a11cdaa7 | 327 | void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
c1768268 TH |
328 | struct blkio_policy_type *pol, |
329 | struct blkio_group *curr_blkg, bool direction, | |
330 | bool sync) | |
cdc1184c | 331 | { |
c1768268 | 332 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
cdc1184c DS |
333 | unsigned long flags; |
334 | ||
335 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 336 | blkio_add_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction, |
cdc1184c | 337 | sync); |
549d3aa8 | 338 | blkio_end_empty_time(&pd->stats); |
c1768268 | 339 | blkio_set_start_group_wait_time(blkg, pol, curr_blkg); |
cdc1184c DS |
340 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
341 | } | |
a11cdaa7 | 342 | EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats); |
cdc1184c | 343 | |
a11cdaa7 | 344 | void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
c1768268 TH |
345 | struct blkio_policy_type *pol, |
346 | bool direction, bool sync) | |
cdc1184c | 347 | { |
c1768268 | 348 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
cdc1184c DS |
349 | unsigned long flags; |
350 | ||
351 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 352 | blkio_check_and_dec_stat(pd->stats.stat_arr[BLKIO_STAT_QUEUED], |
cdc1184c DS |
353 | direction, sync); |
354 | spin_unlock_irqrestore(&blkg->stats_lock, flags); | |
355 | } | |
a11cdaa7 | 356 | EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats); |
cdc1184c | 357 | |
c1768268 TH |
358 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
359 | struct blkio_policy_type *pol, | |
360 | unsigned long time, | |
361 | unsigned long unaccounted_time) | |
22084190 | 362 | { |
c1768268 | 363 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
303a3acb DS |
364 | unsigned long flags; |
365 | ||
366 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 367 | pd->stats.time += time; |
a23e6869 | 368 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
549d3aa8 | 369 | pd->stats.unaccounted_time += unaccounted_time; |
a23e6869 | 370 | #endif |
303a3acb | 371 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
22084190 | 372 | } |
303a3acb | 373 | EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used); |
22084190 | 374 | |
5624a4e4 VG |
375 | /* |
376 | * should be called under rcu read lock or queue lock to make sure blkg pointer | |
377 | * is valid. | |
378 | */ | |
84c124da | 379 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
c1768268 TH |
380 | struct blkio_policy_type *pol, |
381 | uint64_t bytes, bool direction, bool sync) | |
9195291e | 382 | { |
c1768268 | 383 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
5624a4e4 | 384 | struct blkio_group_stats_cpu *stats_cpu; |
575969a0 VG |
385 | unsigned long flags; |
386 | ||
387 | /* | |
388 | * Disabling interrupts to provide mutual exclusion between two | |
389 | * writes on same cpu. It probably is not needed for 64bit. Not | |
390 | * optimizing that case yet. | |
391 | */ | |
392 | local_irq_save(flags); | |
9195291e | 393 | |
549d3aa8 | 394 | stats_cpu = this_cpu_ptr(pd->stats_cpu); |
5624a4e4 | 395 | |
575969a0 | 396 | u64_stats_update_begin(&stats_cpu->syncp); |
5624a4e4 VG |
397 | stats_cpu->sectors += bytes >> 9; |
398 | blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED], | |
399 | 1, direction, sync); | |
400 | blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES], | |
401 | bytes, direction, sync); | |
575969a0 VG |
402 | u64_stats_update_end(&stats_cpu->syncp); |
403 | local_irq_restore(flags); | |
9195291e | 404 | } |
84c124da | 405 | EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats); |
9195291e | 406 | |
84c124da | 407 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
c1768268 TH |
408 | struct blkio_policy_type *pol, |
409 | uint64_t start_time, | |
410 | uint64_t io_start_time, bool direction, | |
411 | bool sync) | |
9195291e | 412 | { |
c1768268 | 413 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
9195291e DS |
414 | struct blkio_group_stats *stats; |
415 | unsigned long flags; | |
416 | unsigned long long now = sched_clock(); | |
417 | ||
418 | spin_lock_irqsave(&blkg->stats_lock, flags); | |
549d3aa8 | 419 | stats = &pd->stats; |
84c124da DS |
420 | if (time_after64(now, io_start_time)) |
421 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME], | |
422 | now - io_start_time, direction, sync); | |
423 | if (time_after64(io_start_time, start_time)) | |
424 | blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME], | |
425 | io_start_time - start_time, direction, sync); | |
9195291e DS |
426 | spin_unlock_irqrestore(&blkg->stats_lock, flags); |
427 | } | |
84c124da | 428 | EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats); |
9195291e | 429 | |
317389a7 | 430 | /* Merged stats are per cpu. */ |
c1768268 TH |
431 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, |
432 | struct blkio_policy_type *pol, | |
433 | bool direction, bool sync) | |
812d4026 | 434 | { |
c1768268 | 435 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
317389a7 | 436 | struct blkio_group_stats_cpu *stats_cpu; |
812d4026 DS |
437 | unsigned long flags; |
438 | ||
317389a7 VG |
439 | /* |
440 | * Disabling interrupts to provide mutual exclusion between two | |
441 | * writes on same cpu. It probably is not needed for 64bit. Not | |
442 | * optimizing that case yet. | |
443 | */ | |
444 | local_irq_save(flags); | |
445 | ||
549d3aa8 | 446 | stats_cpu = this_cpu_ptr(pd->stats_cpu); |
317389a7 VG |
447 | |
448 | u64_stats_update_begin(&stats_cpu->syncp); | |
449 | blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1, | |
450 | direction, sync); | |
451 | u64_stats_update_end(&stats_cpu->syncp); | |
452 | local_irq_restore(flags); | |
812d4026 DS |
453 | } |
454 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); | |
455 | ||
0381411e TH |
456 | /** |
457 | * blkg_free - free a blkg | |
458 | * @blkg: blkg to free | |
459 | * | |
460 | * Free @blkg which may be partially allocated. | |
461 | */ | |
462 | static void blkg_free(struct blkio_group *blkg) | |
463 | { | |
549d3aa8 TH |
464 | struct blkg_policy_data *pd; |
465 | ||
466 | if (!blkg) | |
467 | return; | |
468 | ||
469 | pd = blkg->pd[blkg->plid]; | |
470 | if (pd) { | |
471 | free_percpu(pd->stats_cpu); | |
472 | kfree(pd); | |
0381411e | 473 | } |
549d3aa8 | 474 | kfree(blkg); |
0381411e TH |
475 | } |
476 | ||
477 | /** | |
478 | * blkg_alloc - allocate a blkg | |
479 | * @blkcg: block cgroup the new blkg is associated with | |
480 | * @q: request_queue the new blkg is associated with | |
481 | * @pol: policy the new blkg is associated with | |
482 | * | |
483 | * Allocate a new blkg assocating @blkcg and @q for @pol. | |
484 | * | |
485 | * FIXME: Should be called with queue locked but currently isn't due to | |
486 | * percpu stat breakage. | |
487 | */ | |
488 | static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, | |
489 | struct request_queue *q, | |
490 | struct blkio_policy_type *pol) | |
491 | { | |
492 | struct blkio_group *blkg; | |
549d3aa8 | 493 | struct blkg_policy_data *pd; |
0381411e TH |
494 | |
495 | /* alloc and init base part */ | |
496 | blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node); | |
497 | if (!blkg) | |
498 | return NULL; | |
499 | ||
500 | spin_lock_init(&blkg->stats_lock); | |
501 | rcu_assign_pointer(blkg->q, q); | |
4eef3049 TH |
502 | INIT_LIST_HEAD(&blkg->q_node[0]); |
503 | INIT_LIST_HEAD(&blkg->q_node[1]); | |
0381411e TH |
504 | blkg->blkcg = blkcg; |
505 | blkg->plid = pol->plid; | |
1adaf3dd | 506 | blkg->refcnt = 1; |
0381411e TH |
507 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
508 | ||
549d3aa8 TH |
509 | /* alloc per-policy data and attach it to blkg */ |
510 | pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC, | |
511 | q->node); | |
512 | if (!pd) { | |
0381411e TH |
513 | blkg_free(blkg); |
514 | return NULL; | |
515 | } | |
516 | ||
549d3aa8 TH |
517 | blkg->pd[pol->plid] = pd; |
518 | pd->blkg = blkg; | |
519 | ||
0381411e | 520 | /* broken, read comment in the callsite */ |
549d3aa8 TH |
521 | |
522 | pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); | |
523 | if (!pd->stats_cpu) { | |
0381411e TH |
524 | blkg_free(blkg); |
525 | return NULL; | |
526 | } | |
527 | ||
549d3aa8 | 528 | /* invoke per-policy init */ |
0381411e TH |
529 | pol->ops.blkio_init_group_fn(blkg); |
530 | return blkg; | |
531 | } | |
532 | ||
cd1604fa TH |
533 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
534 | struct request_queue *q, | |
535 | enum blkio_policy_id plid, | |
536 | bool for_root) | |
537 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
5624a4e4 | 538 | { |
cd1604fa TH |
539 | struct blkio_policy_type *pol = blkio_policy[plid]; |
540 | struct blkio_group *blkg, *new_blkg; | |
5624a4e4 | 541 | |
cd1604fa TH |
542 | WARN_ON_ONCE(!rcu_read_lock_held()); |
543 | lockdep_assert_held(q->queue_lock); | |
544 | ||
545 | /* | |
546 | * This could be the first entry point of blkcg implementation and | |
547 | * we shouldn't allow anything to go through for a bypassing queue. | |
548 | * The following can be removed if blkg lookup is guaranteed to | |
549 | * fail on a bypassing queue. | |
550 | */ | |
551 | if (unlikely(blk_queue_bypass(q)) && !for_root) | |
552 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | |
553 | ||
554 | blkg = blkg_lookup(blkcg, q, plid); | |
555 | if (blkg) | |
556 | return blkg; | |
557 | ||
7ee9c562 | 558 | /* blkg holds a reference to blkcg */ |
cd1604fa TH |
559 | if (!css_tryget(&blkcg->css)) |
560 | return ERR_PTR(-EINVAL); | |
561 | ||
562 | /* | |
563 | * Allocate and initialize. | |
564 | * | |
565 | * FIXME: The following is broken. Percpu memory allocation | |
566 | * requires %GFP_KERNEL context and can't be performed from IO | |
567 | * path. Allocation here should inherently be atomic and the | |
568 | * following lock dancing can be removed once the broken percpu | |
569 | * allocation is fixed. | |
570 | */ | |
571 | spin_unlock_irq(q->queue_lock); | |
572 | rcu_read_unlock(); | |
573 | ||
0381411e | 574 | new_blkg = blkg_alloc(blkcg, q, pol); |
cd1604fa TH |
575 | |
576 | rcu_read_lock(); | |
577 | spin_lock_irq(q->queue_lock); | |
31e4c28d | 578 | |
cd1604fa TH |
579 | /* did bypass get turned on inbetween? */ |
580 | if (unlikely(blk_queue_bypass(q)) && !for_root) { | |
581 | blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | |
582 | goto out; | |
583 | } | |
584 | ||
585 | /* did someone beat us to it? */ | |
586 | blkg = blkg_lookup(blkcg, q, plid); | |
587 | if (unlikely(blkg)) | |
588 | goto out; | |
589 | ||
590 | /* did alloc fail? */ | |
0381411e | 591 | if (unlikely(!new_blkg)) { |
cd1604fa TH |
592 | blkg = ERR_PTR(-ENOMEM); |
593 | goto out; | |
594 | } | |
595 | ||
596 | /* insert */ | |
597 | spin_lock(&blkcg->lock); | |
598 | swap(blkg, new_blkg); | |
31e4c28d | 599 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
cd1604fa TH |
600 | pol->ops.blkio_link_group_fn(q, blkg); |
601 | spin_unlock(&blkcg->lock); | |
602 | out: | |
0381411e | 603 | blkg_free(new_blkg); |
cd1604fa | 604 | return blkg; |
31e4c28d | 605 | } |
cd1604fa | 606 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
31e4c28d | 607 | |
b1c35769 VG |
608 | static void __blkiocg_del_blkio_group(struct blkio_group *blkg) |
609 | { | |
610 | hlist_del_init_rcu(&blkg->blkcg_node); | |
b1c35769 VG |
611 | } |
612 | ||
613 | /* | |
614 | * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1 | |
615 | * indicating that blk_group was unhashed by the time we got to it. | |
616 | */ | |
31e4c28d VG |
617 | int blkiocg_del_blkio_group(struct blkio_group *blkg) |
618 | { | |
7ee9c562 | 619 | struct blkio_cgroup *blkcg = blkg->blkcg; |
b1c35769 | 620 | unsigned long flags; |
b1c35769 VG |
621 | int ret = 1; |
622 | ||
7ee9c562 TH |
623 | spin_lock_irqsave(&blkcg->lock, flags); |
624 | if (!hlist_unhashed(&blkg->blkcg_node)) { | |
625 | __blkiocg_del_blkio_group(blkg); | |
626 | ret = 0; | |
b1c35769 | 627 | } |
7ee9c562 | 628 | spin_unlock_irqrestore(&blkcg->lock, flags); |
0f3942a3 | 629 | |
b1c35769 | 630 | return ret; |
31e4c28d | 631 | } |
9d6a986c | 632 | EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group); |
31e4c28d VG |
633 | |
634 | /* called under rcu_read_lock(). */ | |
cd1604fa TH |
635 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
636 | struct request_queue *q, | |
637 | enum blkio_policy_id plid) | |
31e4c28d VG |
638 | { |
639 | struct blkio_group *blkg; | |
640 | struct hlist_node *n; | |
31e4c28d | 641 | |
ca32aefc TH |
642 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) |
643 | if (blkg->q == q && blkg->plid == plid) | |
31e4c28d | 644 | return blkg; |
31e4c28d VG |
645 | return NULL; |
646 | } | |
cd1604fa | 647 | EXPORT_SYMBOL_GPL(blkg_lookup); |
31e4c28d | 648 | |
72e06c25 TH |
649 | void blkg_destroy_all(struct request_queue *q) |
650 | { | |
651 | struct blkio_policy_type *pol; | |
652 | ||
653 | while (true) { | |
654 | bool done = true; | |
655 | ||
656 | spin_lock(&blkio_list_lock); | |
657 | spin_lock_irq(q->queue_lock); | |
658 | ||
659 | /* | |
660 | * clear_queue_fn() might return with non-empty group list | |
661 | * if it raced cgroup removal and lost. cgroup removal is | |
662 | * guaranteed to make forward progress and retrying after a | |
663 | * while is enough. This ugliness is scheduled to be | |
664 | * removed after locking update. | |
665 | */ | |
666 | list_for_each_entry(pol, &blkio_list, list) | |
667 | if (!pol->ops.blkio_clear_queue_fn(q)) | |
668 | done = false; | |
669 | ||
670 | spin_unlock_irq(q->queue_lock); | |
671 | spin_unlock(&blkio_list_lock); | |
672 | ||
673 | if (done) | |
674 | break; | |
675 | ||
676 | msleep(10); /* just some random duration I like */ | |
677 | } | |
678 | } | |
679 | ||
1adaf3dd TH |
680 | static void blkg_rcu_free(struct rcu_head *rcu_head) |
681 | { | |
682 | blkg_free(container_of(rcu_head, struct blkio_group, rcu_head)); | |
683 | } | |
684 | ||
685 | void __blkg_release(struct blkio_group *blkg) | |
686 | { | |
687 | /* release the extra blkcg reference this blkg has been holding */ | |
688 | css_put(&blkg->blkcg->css); | |
689 | ||
690 | /* | |
691 | * A group is freed in rcu manner. But having an rcu lock does not | |
692 | * mean that one can access all the fields of blkg and assume these | |
693 | * are valid. For example, don't try to follow throtl_data and | |
694 | * request queue links. | |
695 | * | |
696 | * Having a reference to blkg under an rcu allows acess to only | |
697 | * values local to groups like group stats and group rate limits | |
698 | */ | |
699 | call_rcu(&blkg->rcu_head, blkg_rcu_free); | |
700 | } | |
701 | EXPORT_SYMBOL_GPL(__blkg_release); | |
702 | ||
c1768268 | 703 | static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid) |
f0bdc8cd | 704 | { |
c1768268 | 705 | struct blkg_policy_data *pd = blkg->pd[plid]; |
f0bdc8cd VG |
706 | struct blkio_group_stats_cpu *stats_cpu; |
707 | int i, j, k; | |
708 | /* | |
709 | * Note: On 64 bit arch this should not be an issue. This has the | |
710 | * possibility of returning some inconsistent value on 32bit arch | |
711 | * as 64bit update on 32bit is non atomic. Taking care of this | |
712 | * corner case makes code very complicated, like sending IPIs to | |
713 | * cpus, taking care of stats of offline cpus etc. | |
714 | * | |
715 | * reset stats is anyway more of a debug feature and this sounds a | |
716 | * corner case. So I am not complicating the code yet until and | |
717 | * unless this becomes a real issue. | |
718 | */ | |
719 | for_each_possible_cpu(i) { | |
549d3aa8 | 720 | stats_cpu = per_cpu_ptr(pd->stats_cpu, i); |
f0bdc8cd VG |
721 | stats_cpu->sectors = 0; |
722 | for(j = 0; j < BLKIO_STAT_CPU_NR; j++) | |
723 | for (k = 0; k < BLKIO_STAT_TOTAL; k++) | |
724 | stats_cpu->stat_arr_cpu[j][k] = 0; | |
725 | } | |
726 | } | |
727 | ||
303a3acb | 728 | static int |
84c124da | 729 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
303a3acb DS |
730 | { |
731 | struct blkio_cgroup *blkcg; | |
732 | struct blkio_group *blkg; | |
812df48d | 733 | struct blkio_group_stats *stats; |
303a3acb | 734 | struct hlist_node *n; |
cdc1184c DS |
735 | uint64_t queued[BLKIO_STAT_TOTAL]; |
736 | int i; | |
812df48d DS |
737 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
738 | bool idling, waiting, empty; | |
739 | unsigned long long now = sched_clock(); | |
740 | #endif | |
303a3acb DS |
741 | |
742 | blkcg = cgroup_to_blkio_cgroup(cgroup); | |
743 | spin_lock_irq(&blkcg->lock); | |
744 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { | |
549d3aa8 TH |
745 | struct blkg_policy_data *pd = blkg->pd[blkg->plid]; |
746 | ||
303a3acb | 747 | spin_lock(&blkg->stats_lock); |
549d3aa8 | 748 | stats = &pd->stats; |
812df48d DS |
749 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
750 | idling = blkio_blkg_idling(stats); | |
751 | waiting = blkio_blkg_waiting(stats); | |
752 | empty = blkio_blkg_empty(stats); | |
753 | #endif | |
cdc1184c | 754 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
812df48d DS |
755 | queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i]; |
756 | memset(stats, 0, sizeof(struct blkio_group_stats)); | |
cdc1184c | 757 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
812df48d DS |
758 | stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i]; |
759 | #ifdef CONFIG_DEBUG_BLK_CGROUP | |
760 | if (idling) { | |
761 | blkio_mark_blkg_idling(stats); | |
762 | stats->start_idle_time = now; | |
763 | } | |
764 | if (waiting) { | |
765 | blkio_mark_blkg_waiting(stats); | |
766 | stats->start_group_wait_time = now; | |
767 | } | |
768 | if (empty) { | |
769 | blkio_mark_blkg_empty(stats); | |
770 | stats->start_empty_time = now; | |
771 | } | |
772 | #endif | |
303a3acb | 773 | spin_unlock(&blkg->stats_lock); |
f0bdc8cd VG |
774 | |
775 | /* Reset Per cpu stats which don't take blkg->stats_lock */ | |
c1768268 | 776 | blkio_reset_stats_cpu(blkg, blkg->plid); |
303a3acb | 777 | } |
f0bdc8cd | 778 | |
303a3acb DS |
779 | spin_unlock_irq(&blkcg->lock); |
780 | return 0; | |
781 | } | |
782 | ||
7a4dd281 TH |
783 | static void blkio_get_key_name(enum stat_sub_type type, const char *dname, |
784 | char *str, int chars_left, bool diskname_only) | |
303a3acb | 785 | { |
7a4dd281 | 786 | snprintf(str, chars_left, "%s", dname); |
303a3acb DS |
787 | chars_left -= strlen(str); |
788 | if (chars_left <= 0) { | |
789 | printk(KERN_WARNING | |
790 | "Possibly incorrect cgroup stat display format"); | |
791 | return; | |
792 | } | |
84c124da DS |
793 | if (diskname_only) |
794 | return; | |
303a3acb | 795 | switch (type) { |
84c124da | 796 | case BLKIO_STAT_READ: |
303a3acb DS |
797 | strlcat(str, " Read", chars_left); |
798 | break; | |
84c124da | 799 | case BLKIO_STAT_WRITE: |
303a3acb DS |
800 | strlcat(str, " Write", chars_left); |
801 | break; | |
84c124da | 802 | case BLKIO_STAT_SYNC: |
303a3acb DS |
803 | strlcat(str, " Sync", chars_left); |
804 | break; | |
84c124da | 805 | case BLKIO_STAT_ASYNC: |
303a3acb DS |
806 | strlcat(str, " Async", chars_left); |
807 | break; | |
84c124da | 808 | case BLKIO_STAT_TOTAL: |
303a3acb DS |
809 | strlcat(str, " Total", chars_left); |
810 | break; | |
811 | default: | |
812 | strlcat(str, " Invalid", chars_left); | |
813 | } | |
814 | } | |
815 | ||
84c124da | 816 | static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val, |
7a4dd281 | 817 | struct cgroup_map_cb *cb, const char *dname) |
84c124da | 818 | { |
7a4dd281 | 819 | blkio_get_key_name(0, dname, str, chars_left, true); |
84c124da DS |
820 | cb->fill(cb, str, val); |
821 | return val; | |
822 | } | |
303a3acb | 823 | |
5624a4e4 | 824 | |
c1768268 | 825 | static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid, |
5624a4e4 VG |
826 | enum stat_type_cpu type, enum stat_sub_type sub_type) |
827 | { | |
c1768268 | 828 | struct blkg_policy_data *pd = blkg->pd[plid]; |
5624a4e4 VG |
829 | int cpu; |
830 | struct blkio_group_stats_cpu *stats_cpu; | |
575969a0 | 831 | u64 val = 0, tval; |
5624a4e4 VG |
832 | |
833 | for_each_possible_cpu(cpu) { | |
575969a0 | 834 | unsigned int start; |
549d3aa8 | 835 | stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu); |
5624a4e4 | 836 | |
575969a0 VG |
837 | do { |
838 | start = u64_stats_fetch_begin(&stats_cpu->syncp); | |
839 | if (type == BLKIO_STAT_CPU_SECTORS) | |
840 | tval = stats_cpu->sectors; | |
841 | else | |
842 | tval = stats_cpu->stat_arr_cpu[type][sub_type]; | |
843 | } while(u64_stats_fetch_retry(&stats_cpu->syncp, start)); | |
844 | ||
845 | val += tval; | |
5624a4e4 VG |
846 | } |
847 | ||
848 | return val; | |
849 | } | |
850 | ||
c1768268 | 851 | static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid, |
7a4dd281 TH |
852 | struct cgroup_map_cb *cb, const char *dname, |
853 | enum stat_type_cpu type) | |
5624a4e4 VG |
854 | { |
855 | uint64_t disk_total, val; | |
856 | char key_str[MAX_KEY_LEN]; | |
857 | enum stat_sub_type sub_type; | |
858 | ||
859 | if (type == BLKIO_STAT_CPU_SECTORS) { | |
c1768268 | 860 | val = blkio_read_stat_cpu(blkg, plid, type, 0); |
7a4dd281 TH |
861 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, |
862 | dname); | |
5624a4e4 VG |
863 | } |
864 | ||
865 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; | |
866 | sub_type++) { | |
7a4dd281 TH |
867 | blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN, |
868 | false); | |
c1768268 | 869 | val = blkio_read_stat_cpu(blkg, plid, type, sub_type); |
5624a4e4 VG |
870 | cb->fill(cb, key_str, val); |
871 | } | |
872 | ||
c1768268 TH |
873 | disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) + |
874 | blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE); | |
5624a4e4 | 875 | |
7a4dd281 TH |
876 | blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN, |
877 | false); | |
5624a4e4 VG |
878 | cb->fill(cb, key_str, disk_total); |
879 | return disk_total; | |
880 | } | |
881 | ||
84c124da | 882 | /* This should be called with blkg->stats_lock held */ |
c1768268 | 883 | static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid, |
7a4dd281 TH |
884 | struct cgroup_map_cb *cb, const char *dname, |
885 | enum stat_type type) | |
303a3acb | 886 | { |
c1768268 | 887 | struct blkg_policy_data *pd = blkg->pd[plid]; |
303a3acb DS |
888 | uint64_t disk_total; |
889 | char key_str[MAX_KEY_LEN]; | |
84c124da DS |
890 | enum stat_sub_type sub_type; |
891 | ||
892 | if (type == BLKIO_STAT_TIME) | |
893 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 894 | pd->stats.time, cb, dname); |
9026e521 | 895 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
167400d3 JT |
896 | if (type == BLKIO_STAT_UNACCOUNTED_TIME) |
897 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 898 | pd->stats.unaccounted_time, cb, dname); |
cdc1184c | 899 | if (type == BLKIO_STAT_AVG_QUEUE_SIZE) { |
549d3aa8 TH |
900 | uint64_t sum = pd->stats.avg_queue_size_sum; |
901 | uint64_t samples = pd->stats.avg_queue_size_samples; | |
cdc1184c DS |
902 | if (samples) |
903 | do_div(sum, samples); | |
904 | else | |
905 | sum = 0; | |
7a4dd281 TH |
906 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
907 | sum, cb, dname); | |
cdc1184c | 908 | } |
812df48d DS |
909 | if (type == BLKIO_STAT_GROUP_WAIT_TIME) |
910 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 911 | pd->stats.group_wait_time, cb, dname); |
812df48d DS |
912 | if (type == BLKIO_STAT_IDLE_TIME) |
913 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 914 | pd->stats.idle_time, cb, dname); |
812df48d DS |
915 | if (type == BLKIO_STAT_EMPTY_TIME) |
916 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 917 | pd->stats.empty_time, cb, dname); |
84c124da DS |
918 | if (type == BLKIO_STAT_DEQUEUE) |
919 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 920 | pd->stats.dequeue, cb, dname); |
84c124da | 921 | #endif |
303a3acb | 922 | |
84c124da DS |
923 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; |
924 | sub_type++) { | |
7a4dd281 TH |
925 | blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN, |
926 | false); | |
549d3aa8 | 927 | cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]); |
303a3acb | 928 | } |
549d3aa8 TH |
929 | disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] + |
930 | pd->stats.stat_arr[type][BLKIO_STAT_WRITE]; | |
7a4dd281 TH |
931 | blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN, |
932 | false); | |
303a3acb DS |
933 | cb->fill(cb, key_str, disk_total); |
934 | return disk_total; | |
935 | } | |
936 | ||
4bfd482e TH |
937 | static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid, |
938 | int fileid, struct blkio_cgroup *blkcg) | |
34d0f179 | 939 | { |
ece84241 | 940 | struct gendisk *disk = NULL; |
e56da7e2 | 941 | struct blkio_group *blkg = NULL; |
549d3aa8 | 942 | struct blkg_policy_data *pd; |
34d0f179 | 943 | char *s[4], *p, *major_s = NULL, *minor_s = NULL; |
d11bb446 | 944 | unsigned long major, minor; |
ece84241 TH |
945 | int i = 0, ret = -EINVAL; |
946 | int part; | |
34d0f179 | 947 | dev_t dev; |
d11bb446 | 948 | u64 temp; |
34d0f179 GJ |
949 | |
950 | memset(s, 0, sizeof(s)); | |
951 | ||
952 | while ((p = strsep(&buf, " ")) != NULL) { | |
953 | if (!*p) | |
954 | continue; | |
955 | ||
956 | s[i++] = p; | |
957 | ||
958 | /* Prevent from inputing too many things */ | |
959 | if (i == 3) | |
960 | break; | |
961 | } | |
962 | ||
963 | if (i != 2) | |
ece84241 | 964 | goto out; |
34d0f179 GJ |
965 | |
966 | p = strsep(&s[0], ":"); | |
967 | if (p != NULL) | |
968 | major_s = p; | |
969 | else | |
ece84241 | 970 | goto out; |
34d0f179 GJ |
971 | |
972 | minor_s = s[0]; | |
973 | if (!minor_s) | |
ece84241 | 974 | goto out; |
34d0f179 | 975 | |
ece84241 TH |
976 | if (strict_strtoul(major_s, 10, &major)) |
977 | goto out; | |
34d0f179 | 978 | |
ece84241 TH |
979 | if (strict_strtoul(minor_s, 10, &minor)) |
980 | goto out; | |
34d0f179 GJ |
981 | |
982 | dev = MKDEV(major, minor); | |
983 | ||
ece84241 TH |
984 | if (strict_strtoull(s[1], 10, &temp)) |
985 | goto out; | |
34d0f179 | 986 | |
e56da7e2 | 987 | disk = get_gendisk(dev, &part); |
4bfd482e | 988 | if (!disk || part) |
e56da7e2 | 989 | goto out; |
e56da7e2 TH |
990 | |
991 | rcu_read_lock(); | |
992 | ||
4bfd482e TH |
993 | spin_lock_irq(disk->queue->queue_lock); |
994 | blkg = blkg_lookup_create(blkcg, disk->queue, plid, false); | |
995 | spin_unlock_irq(disk->queue->queue_lock); | |
e56da7e2 | 996 | |
4bfd482e TH |
997 | if (IS_ERR(blkg)) { |
998 | ret = PTR_ERR(blkg); | |
999 | goto out_unlock; | |
d11bb446 | 1000 | } |
34d0f179 | 1001 | |
549d3aa8 TH |
1002 | pd = blkg->pd[plid]; |
1003 | ||
062a644d VG |
1004 | switch (plid) { |
1005 | case BLKIO_POLICY_PROP: | |
d11bb446 WG |
1006 | if ((temp < BLKIO_WEIGHT_MIN && temp > 0) || |
1007 | temp > BLKIO_WEIGHT_MAX) | |
e56da7e2 | 1008 | goto out_unlock; |
34d0f179 | 1009 | |
549d3aa8 | 1010 | pd->conf.weight = temp; |
c1768268 | 1011 | blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight); |
4c9eefa1 VG |
1012 | break; |
1013 | case BLKIO_POLICY_THROTL: | |
7702e8f4 VG |
1014 | switch(fileid) { |
1015 | case BLKIO_THROTL_read_bps_device: | |
549d3aa8 | 1016 | pd->conf.bps[READ] = temp; |
c1768268 | 1017 | blkio_update_group_bps(blkg, plid, temp ?: -1, fileid); |
e56da7e2 | 1018 | break; |
7702e8f4 | 1019 | case BLKIO_THROTL_write_bps_device: |
549d3aa8 | 1020 | pd->conf.bps[WRITE] = temp; |
c1768268 | 1021 | blkio_update_group_bps(blkg, plid, temp ?: -1, fileid); |
7702e8f4 VG |
1022 | break; |
1023 | case BLKIO_THROTL_read_iops_device: | |
e56da7e2 TH |
1024 | if (temp > THROTL_IOPS_MAX) |
1025 | goto out_unlock; | |
549d3aa8 | 1026 | pd->conf.iops[READ] = temp; |
c1768268 | 1027 | blkio_update_group_iops(blkg, plid, temp ?: -1, fileid); |
e56da7e2 | 1028 | break; |
7702e8f4 | 1029 | case BLKIO_THROTL_write_iops_device: |
d11bb446 | 1030 | if (temp > THROTL_IOPS_MAX) |
e56da7e2 | 1031 | goto out_unlock; |
549d3aa8 | 1032 | pd->conf.iops[WRITE] = temp; |
c1768268 | 1033 | blkio_update_group_iops(blkg, plid, temp ?: -1, fileid); |
7702e8f4 VG |
1034 | break; |
1035 | } | |
062a644d VG |
1036 | break; |
1037 | default: | |
1038 | BUG(); | |
1039 | } | |
ece84241 | 1040 | ret = 0; |
e56da7e2 TH |
1041 | out_unlock: |
1042 | rcu_read_unlock(); | |
ece84241 TH |
1043 | out: |
1044 | put_disk(disk); | |
e56da7e2 TH |
1045 | |
1046 | /* | |
1047 | * If queue was bypassing, we should retry. Do so after a short | |
1048 | * msleep(). It isn't strictly necessary but queue can be | |
1049 | * bypassing for some time and it's always nice to avoid busy | |
1050 | * looping. | |
1051 | */ | |
1052 | if (ret == -EBUSY) { | |
1053 | msleep(10); | |
1054 | return restart_syscall(); | |
1055 | } | |
ece84241 | 1056 | return ret; |
34d0f179 GJ |
1057 | } |
1058 | ||
062a644d VG |
1059 | static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft, |
1060 | const char *buffer) | |
34d0f179 GJ |
1061 | { |
1062 | int ret = 0; | |
1063 | char *buf; | |
e56da7e2 | 1064 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
062a644d VG |
1065 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
1066 | int fileid = BLKIOFILE_ATTR(cft->private); | |
34d0f179 GJ |
1067 | |
1068 | buf = kstrdup(buffer, GFP_KERNEL); | |
1069 | if (!buf) | |
1070 | return -ENOMEM; | |
1071 | ||
4bfd482e | 1072 | ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg); |
34d0f179 GJ |
1073 | kfree(buf); |
1074 | return ret; | |
1075 | } | |
1076 | ||
92616b5b VG |
1077 | static const char *blkg_dev_name(struct blkio_group *blkg) |
1078 | { | |
1079 | /* some drivers (floppy) instantiate a queue w/o disk registered */ | |
1080 | if (blkg->q->backing_dev_info.dev) | |
1081 | return dev_name(blkg->q->backing_dev_info.dev); | |
1082 | return NULL; | |
1083 | } | |
1084 | ||
4bfd482e TH |
1085 | static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg, |
1086 | struct seq_file *m) | |
34d0f179 | 1087 | { |
c1768268 | 1088 | int plid = BLKIOFILE_POLICY(cft->private); |
4bfd482e | 1089 | int fileid = BLKIOFILE_ATTR(cft->private); |
c1768268 TH |
1090 | struct blkg_policy_data *pd = blkg->pd[plid]; |
1091 | const char *dname = blkg_dev_name(blkg); | |
4bfd482e TH |
1092 | int rw = WRITE; |
1093 | ||
92616b5b VG |
1094 | if (!dname) |
1095 | return; | |
1096 | ||
c1768268 | 1097 | switch (plid) { |
062a644d | 1098 | case BLKIO_POLICY_PROP: |
549d3aa8 | 1099 | if (pd->conf.weight) |
7a4dd281 | 1100 | seq_printf(m, "%s\t%u\n", |
549d3aa8 | 1101 | dname, pd->conf.weight); |
4c9eefa1 VG |
1102 | break; |
1103 | case BLKIO_POLICY_THROTL: | |
4bfd482e | 1104 | switch (fileid) { |
7702e8f4 | 1105 | case BLKIO_THROTL_read_bps_device: |
4bfd482e | 1106 | rw = READ; |
7702e8f4 | 1107 | case BLKIO_THROTL_write_bps_device: |
549d3aa8 | 1108 | if (pd->conf.bps[rw]) |
7a4dd281 | 1109 | seq_printf(m, "%s\t%llu\n", |
549d3aa8 | 1110 | dname, pd->conf.bps[rw]); |
7702e8f4 VG |
1111 | break; |
1112 | case BLKIO_THROTL_read_iops_device: | |
4bfd482e | 1113 | rw = READ; |
7702e8f4 | 1114 | case BLKIO_THROTL_write_iops_device: |
549d3aa8 | 1115 | if (pd->conf.iops[rw]) |
7a4dd281 | 1116 | seq_printf(m, "%s\t%u\n", |
549d3aa8 | 1117 | dname, pd->conf.iops[rw]); |
7702e8f4 VG |
1118 | break; |
1119 | } | |
062a644d VG |
1120 | break; |
1121 | default: | |
1122 | BUG(); | |
1123 | } | |
1124 | } | |
34d0f179 | 1125 | |
062a644d | 1126 | /* cgroup files which read their data from policy nodes end up here */ |
4bfd482e TH |
1127 | static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg, |
1128 | struct seq_file *m) | |
34d0f179 | 1129 | { |
4bfd482e TH |
1130 | struct blkio_group *blkg; |
1131 | struct hlist_node *n; | |
34d0f179 | 1132 | |
4bfd482e TH |
1133 | spin_lock_irq(&blkcg->lock); |
1134 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) | |
1135 | if (BLKIOFILE_POLICY(cft->private) == blkg->plid) | |
1136 | blkio_print_group_conf(cft, blkg, m); | |
1137 | spin_unlock_irq(&blkcg->lock); | |
062a644d VG |
1138 | } |
1139 | ||
1140 | static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft, | |
1141 | struct seq_file *m) | |
1142 | { | |
1143 | struct blkio_cgroup *blkcg; | |
1144 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1145 | int name = BLKIOFILE_ATTR(cft->private); | |
1146 | ||
1147 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1148 | ||
1149 | switch(plid) { | |
1150 | case BLKIO_POLICY_PROP: | |
1151 | switch(name) { | |
1152 | case BLKIO_PROP_weight_device: | |
4bfd482e | 1153 | blkio_read_conf(cft, blkcg, m); |
062a644d VG |
1154 | return 0; |
1155 | default: | |
1156 | BUG(); | |
1157 | } | |
1158 | break; | |
4c9eefa1 VG |
1159 | case BLKIO_POLICY_THROTL: |
1160 | switch(name){ | |
1161 | case BLKIO_THROTL_read_bps_device: | |
1162 | case BLKIO_THROTL_write_bps_device: | |
7702e8f4 VG |
1163 | case BLKIO_THROTL_read_iops_device: |
1164 | case BLKIO_THROTL_write_iops_device: | |
4bfd482e | 1165 | blkio_read_conf(cft, blkcg, m); |
4c9eefa1 VG |
1166 | return 0; |
1167 | default: | |
1168 | BUG(); | |
1169 | } | |
1170 | break; | |
062a644d VG |
1171 | default: |
1172 | BUG(); | |
1173 | } | |
1174 | ||
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg, | |
5624a4e4 VG |
1179 | struct cftype *cft, struct cgroup_map_cb *cb, |
1180 | enum stat_type type, bool show_total, bool pcpu) | |
062a644d VG |
1181 | { |
1182 | struct blkio_group *blkg; | |
1183 | struct hlist_node *n; | |
1184 | uint64_t cgroup_total = 0; | |
1185 | ||
1186 | rcu_read_lock(); | |
1187 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) { | |
92616b5b | 1188 | const char *dname = blkg_dev_name(blkg); |
c1768268 | 1189 | int plid = BLKIOFILE_POLICY(cft->private); |
7a4dd281 | 1190 | |
c1768268 | 1191 | if (!dname || plid != blkg->plid) |
7a4dd281 | 1192 | continue; |
c1768268 TH |
1193 | if (pcpu) { |
1194 | cgroup_total += blkio_get_stat_cpu(blkg, plid, | |
1195 | cb, dname, type); | |
1196 | } else { | |
7a4dd281 | 1197 | spin_lock_irq(&blkg->stats_lock); |
c1768268 TH |
1198 | cgroup_total += blkio_get_stat(blkg, plid, |
1199 | cb, dname, type); | |
7a4dd281 | 1200 | spin_unlock_irq(&blkg->stats_lock); |
062a644d VG |
1201 | } |
1202 | } | |
1203 | if (show_total) | |
1204 | cb->fill(cb, "Total", cgroup_total); | |
1205 | rcu_read_unlock(); | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | /* All map kind of cgroup file get serviced by this function */ | |
1210 | static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft, | |
1211 | struct cgroup_map_cb *cb) | |
1212 | { | |
1213 | struct blkio_cgroup *blkcg; | |
1214 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1215 | int name = BLKIOFILE_ATTR(cft->private); | |
1216 | ||
1217 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1218 | ||
1219 | switch(plid) { | |
1220 | case BLKIO_POLICY_PROP: | |
1221 | switch(name) { | |
1222 | case BLKIO_PROP_time: | |
1223 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1224 | BLKIO_STAT_TIME, 0, 0); |
062a644d VG |
1225 | case BLKIO_PROP_sectors: |
1226 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1227 | BLKIO_STAT_CPU_SECTORS, 0, 1); |
062a644d VG |
1228 | case BLKIO_PROP_io_service_bytes: |
1229 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1230 | BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1); |
062a644d VG |
1231 | case BLKIO_PROP_io_serviced: |
1232 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1233 | BLKIO_STAT_CPU_SERVICED, 1, 1); |
062a644d VG |
1234 | case BLKIO_PROP_io_service_time: |
1235 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1236 | BLKIO_STAT_SERVICE_TIME, 1, 0); |
062a644d VG |
1237 | case BLKIO_PROP_io_wait_time: |
1238 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1239 | BLKIO_STAT_WAIT_TIME, 1, 0); |
062a644d VG |
1240 | case BLKIO_PROP_io_merged: |
1241 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
317389a7 | 1242 | BLKIO_STAT_CPU_MERGED, 1, 1); |
062a644d VG |
1243 | case BLKIO_PROP_io_queued: |
1244 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1245 | BLKIO_STAT_QUEUED, 1, 0); |
062a644d | 1246 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
9026e521 JT |
1247 | case BLKIO_PROP_unaccounted_time: |
1248 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1249 | BLKIO_STAT_UNACCOUNTED_TIME, 0, 0); |
062a644d VG |
1250 | case BLKIO_PROP_dequeue: |
1251 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1252 | BLKIO_STAT_DEQUEUE, 0, 0); |
062a644d VG |
1253 | case BLKIO_PROP_avg_queue_size: |
1254 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1255 | BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0); |
062a644d VG |
1256 | case BLKIO_PROP_group_wait_time: |
1257 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1258 | BLKIO_STAT_GROUP_WAIT_TIME, 0, 0); |
062a644d VG |
1259 | case BLKIO_PROP_idle_time: |
1260 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1261 | BLKIO_STAT_IDLE_TIME, 0, 0); |
062a644d VG |
1262 | case BLKIO_PROP_empty_time: |
1263 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1264 | BLKIO_STAT_EMPTY_TIME, 0, 0); |
062a644d VG |
1265 | #endif |
1266 | default: | |
1267 | BUG(); | |
1268 | } | |
1269 | break; | |
4c9eefa1 VG |
1270 | case BLKIO_POLICY_THROTL: |
1271 | switch(name){ | |
1272 | case BLKIO_THROTL_io_service_bytes: | |
1273 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1274 | BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1); |
4c9eefa1 VG |
1275 | case BLKIO_THROTL_io_serviced: |
1276 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1277 | BLKIO_STAT_CPU_SERVICED, 1, 1); |
4c9eefa1 VG |
1278 | default: |
1279 | BUG(); | |
1280 | } | |
1281 | break; | |
062a644d VG |
1282 | default: |
1283 | BUG(); | |
1284 | } | |
1285 | ||
1286 | return 0; | |
1287 | } | |
1288 | ||
4bfd482e | 1289 | static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val) |
062a644d VG |
1290 | { |
1291 | struct blkio_group *blkg; | |
1292 | struct hlist_node *n; | |
062a644d VG |
1293 | |
1294 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) | |
1295 | return -EINVAL; | |
1296 | ||
1297 | spin_lock(&blkio_list_lock); | |
1298 | spin_lock_irq(&blkcg->lock); | |
1299 | blkcg->weight = (unsigned int)val; | |
1300 | ||
549d3aa8 TH |
1301 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
1302 | struct blkg_policy_data *pd = blkg->pd[blkg->plid]; | |
1303 | ||
1304 | if (blkg->plid == plid && !pd->conf.weight) | |
c1768268 | 1305 | blkio_update_group_weight(blkg, plid, blkcg->weight); |
549d3aa8 | 1306 | } |
062a644d | 1307 | |
062a644d VG |
1308 | spin_unlock_irq(&blkcg->lock); |
1309 | spin_unlock(&blkio_list_lock); | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) { | |
1314 | struct blkio_cgroup *blkcg; | |
1315 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1316 | int name = BLKIOFILE_ATTR(cft->private); | |
1317 | ||
1318 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1319 | ||
1320 | switch(plid) { | |
1321 | case BLKIO_POLICY_PROP: | |
1322 | switch(name) { | |
1323 | case BLKIO_PROP_weight: | |
1324 | return (u64)blkcg->weight; | |
1325 | } | |
1326 | break; | |
1327 | default: | |
1328 | BUG(); | |
1329 | } | |
1330 | return 0; | |
1331 | } | |
1332 | ||
1333 | static int | |
1334 | blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val) | |
1335 | { | |
1336 | struct blkio_cgroup *blkcg; | |
1337 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1338 | int name = BLKIOFILE_ATTR(cft->private); | |
1339 | ||
1340 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1341 | ||
1342 | switch(plid) { | |
1343 | case BLKIO_POLICY_PROP: | |
1344 | switch(name) { | |
1345 | case BLKIO_PROP_weight: | |
4bfd482e | 1346 | return blkio_weight_write(blkcg, plid, val); |
062a644d VG |
1347 | } |
1348 | break; | |
1349 | default: | |
1350 | BUG(); | |
1351 | } | |
34d0f179 | 1352 | |
34d0f179 GJ |
1353 | return 0; |
1354 | } | |
1355 | ||
31e4c28d | 1356 | struct cftype blkio_files[] = { |
34d0f179 GJ |
1357 | { |
1358 | .name = "weight_device", | |
062a644d VG |
1359 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1360 | BLKIO_PROP_weight_device), | |
1361 | .read_seq_string = blkiocg_file_read, | |
1362 | .write_string = blkiocg_file_write, | |
34d0f179 GJ |
1363 | .max_write_len = 256, |
1364 | }, | |
31e4c28d VG |
1365 | { |
1366 | .name = "weight", | |
062a644d VG |
1367 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1368 | BLKIO_PROP_weight), | |
1369 | .read_u64 = blkiocg_file_read_u64, | |
1370 | .write_u64 = blkiocg_file_write_u64, | |
31e4c28d | 1371 | }, |
22084190 VG |
1372 | { |
1373 | .name = "time", | |
062a644d VG |
1374 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1375 | BLKIO_PROP_time), | |
1376 | .read_map = blkiocg_file_read_map, | |
22084190 VG |
1377 | }, |
1378 | { | |
1379 | .name = "sectors", | |
062a644d VG |
1380 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1381 | BLKIO_PROP_sectors), | |
1382 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1383 | }, |
1384 | { | |
1385 | .name = "io_service_bytes", | |
062a644d VG |
1386 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1387 | BLKIO_PROP_io_service_bytes), | |
1388 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1389 | }, |
1390 | { | |
1391 | .name = "io_serviced", | |
062a644d VG |
1392 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1393 | BLKIO_PROP_io_serviced), | |
1394 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1395 | }, |
1396 | { | |
1397 | .name = "io_service_time", | |
062a644d VG |
1398 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1399 | BLKIO_PROP_io_service_time), | |
1400 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1401 | }, |
1402 | { | |
1403 | .name = "io_wait_time", | |
062a644d VG |
1404 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1405 | BLKIO_PROP_io_wait_time), | |
1406 | .read_map = blkiocg_file_read_map, | |
84c124da | 1407 | }, |
812d4026 DS |
1408 | { |
1409 | .name = "io_merged", | |
062a644d VG |
1410 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1411 | BLKIO_PROP_io_merged), | |
1412 | .read_map = blkiocg_file_read_map, | |
812d4026 | 1413 | }, |
cdc1184c DS |
1414 | { |
1415 | .name = "io_queued", | |
062a644d VG |
1416 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1417 | BLKIO_PROP_io_queued), | |
1418 | .read_map = blkiocg_file_read_map, | |
cdc1184c | 1419 | }, |
84c124da DS |
1420 | { |
1421 | .name = "reset_stats", | |
1422 | .write_u64 = blkiocg_reset_stats, | |
22084190 | 1423 | }, |
13f98250 VG |
1424 | #ifdef CONFIG_BLK_DEV_THROTTLING |
1425 | { | |
1426 | .name = "throttle.read_bps_device", | |
1427 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1428 | BLKIO_THROTL_read_bps_device), | |
1429 | .read_seq_string = blkiocg_file_read, | |
1430 | .write_string = blkiocg_file_write, | |
1431 | .max_write_len = 256, | |
1432 | }, | |
1433 | ||
1434 | { | |
1435 | .name = "throttle.write_bps_device", | |
1436 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1437 | BLKIO_THROTL_write_bps_device), | |
1438 | .read_seq_string = blkiocg_file_read, | |
1439 | .write_string = blkiocg_file_write, | |
1440 | .max_write_len = 256, | |
1441 | }, | |
1442 | ||
1443 | { | |
1444 | .name = "throttle.read_iops_device", | |
1445 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1446 | BLKIO_THROTL_read_iops_device), | |
1447 | .read_seq_string = blkiocg_file_read, | |
1448 | .write_string = blkiocg_file_write, | |
1449 | .max_write_len = 256, | |
1450 | }, | |
1451 | ||
1452 | { | |
1453 | .name = "throttle.write_iops_device", | |
1454 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1455 | BLKIO_THROTL_write_iops_device), | |
1456 | .read_seq_string = blkiocg_file_read, | |
1457 | .write_string = blkiocg_file_write, | |
1458 | .max_write_len = 256, | |
1459 | }, | |
1460 | { | |
1461 | .name = "throttle.io_service_bytes", | |
1462 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1463 | BLKIO_THROTL_io_service_bytes), | |
1464 | .read_map = blkiocg_file_read_map, | |
1465 | }, | |
1466 | { | |
1467 | .name = "throttle.io_serviced", | |
1468 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1469 | BLKIO_THROTL_io_serviced), | |
1470 | .read_map = blkiocg_file_read_map, | |
1471 | }, | |
1472 | #endif /* CONFIG_BLK_DEV_THROTTLING */ | |
1473 | ||
22084190 | 1474 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
cdc1184c DS |
1475 | { |
1476 | .name = "avg_queue_size", | |
062a644d VG |
1477 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1478 | BLKIO_PROP_avg_queue_size), | |
1479 | .read_map = blkiocg_file_read_map, | |
cdc1184c | 1480 | }, |
812df48d DS |
1481 | { |
1482 | .name = "group_wait_time", | |
062a644d VG |
1483 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1484 | BLKIO_PROP_group_wait_time), | |
1485 | .read_map = blkiocg_file_read_map, | |
812df48d DS |
1486 | }, |
1487 | { | |
1488 | .name = "idle_time", | |
062a644d VG |
1489 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1490 | BLKIO_PROP_idle_time), | |
1491 | .read_map = blkiocg_file_read_map, | |
812df48d DS |
1492 | }, |
1493 | { | |
1494 | .name = "empty_time", | |
062a644d VG |
1495 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1496 | BLKIO_PROP_empty_time), | |
1497 | .read_map = blkiocg_file_read_map, | |
812df48d | 1498 | }, |
cdc1184c | 1499 | { |
22084190 | 1500 | .name = "dequeue", |
062a644d VG |
1501 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1502 | BLKIO_PROP_dequeue), | |
1503 | .read_map = blkiocg_file_read_map, | |
cdc1184c | 1504 | }, |
9026e521 JT |
1505 | { |
1506 | .name = "unaccounted_time", | |
1507 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, | |
1508 | BLKIO_PROP_unaccounted_time), | |
1509 | .read_map = blkiocg_file_read_map, | |
1510 | }, | |
22084190 | 1511 | #endif |
31e4c28d VG |
1512 | }; |
1513 | ||
1514 | static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup) | |
1515 | { | |
1516 | return cgroup_add_files(cgroup, subsys, blkio_files, | |
1517 | ARRAY_SIZE(blkio_files)); | |
1518 | } | |
1519 | ||
7ee9c562 TH |
1520 | static int blkiocg_pre_destroy(struct cgroup_subsys *subsys, |
1521 | struct cgroup *cgroup) | |
31e4c28d VG |
1522 | { |
1523 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
b1c35769 VG |
1524 | unsigned long flags; |
1525 | struct blkio_group *blkg; | |
ca32aefc | 1526 | struct request_queue *q; |
3e252066 | 1527 | struct blkio_policy_type *blkiop; |
b1c35769 VG |
1528 | |
1529 | rcu_read_lock(); | |
7ee9c562 | 1530 | |
0f3942a3 JA |
1531 | do { |
1532 | spin_lock_irqsave(&blkcg->lock, flags); | |
b1c35769 | 1533 | |
0f3942a3 JA |
1534 | if (hlist_empty(&blkcg->blkg_list)) { |
1535 | spin_unlock_irqrestore(&blkcg->lock, flags); | |
1536 | break; | |
1537 | } | |
b1c35769 | 1538 | |
0f3942a3 JA |
1539 | blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group, |
1540 | blkcg_node); | |
ca32aefc | 1541 | q = rcu_dereference(blkg->q); |
0f3942a3 | 1542 | __blkiocg_del_blkio_group(blkg); |
31e4c28d | 1543 | |
0f3942a3 | 1544 | spin_unlock_irqrestore(&blkcg->lock, flags); |
b1c35769 | 1545 | |
0f3942a3 JA |
1546 | /* |
1547 | * This blkio_group is being unlinked as associated cgroup is | |
1548 | * going away. Let all the IO controlling policies know about | |
61014e96 | 1549 | * this event. |
0f3942a3 JA |
1550 | */ |
1551 | spin_lock(&blkio_list_lock); | |
61014e96 VG |
1552 | list_for_each_entry(blkiop, &blkio_list, list) { |
1553 | if (blkiop->plid != blkg->plid) | |
1554 | continue; | |
ca32aefc | 1555 | blkiop->ops.blkio_unlink_group_fn(q, blkg); |
61014e96 | 1556 | } |
0f3942a3 JA |
1557 | spin_unlock(&blkio_list_lock); |
1558 | } while (1); | |
34d0f179 | 1559 | |
b1c35769 | 1560 | rcu_read_unlock(); |
7ee9c562 TH |
1561 | |
1562 | return 0; | |
1563 | } | |
1564 | ||
1565 | static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup) | |
1566 | { | |
1567 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
1568 | ||
67523c48 BB |
1569 | if (blkcg != &blkio_root_cgroup) |
1570 | kfree(blkcg); | |
31e4c28d VG |
1571 | } |
1572 | ||
1573 | static struct cgroup_subsys_state * | |
1574 | blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup) | |
1575 | { | |
0341509f LZ |
1576 | struct blkio_cgroup *blkcg; |
1577 | struct cgroup *parent = cgroup->parent; | |
31e4c28d | 1578 | |
0341509f | 1579 | if (!parent) { |
31e4c28d VG |
1580 | blkcg = &blkio_root_cgroup; |
1581 | goto done; | |
1582 | } | |
1583 | ||
31e4c28d VG |
1584 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
1585 | if (!blkcg) | |
1586 | return ERR_PTR(-ENOMEM); | |
1587 | ||
1588 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; | |
1589 | done: | |
1590 | spin_lock_init(&blkcg->lock); | |
1591 | INIT_HLIST_HEAD(&blkcg->blkg_list); | |
1592 | ||
1593 | return &blkcg->css; | |
1594 | } | |
1595 | ||
5efd6113 TH |
1596 | /** |
1597 | * blkcg_init_queue - initialize blkcg part of request queue | |
1598 | * @q: request_queue to initialize | |
1599 | * | |
1600 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
1601 | * part of new request_queue @q. | |
1602 | * | |
1603 | * RETURNS: | |
1604 | * 0 on success, -errno on failure. | |
1605 | */ | |
1606 | int blkcg_init_queue(struct request_queue *q) | |
1607 | { | |
923adde1 TH |
1608 | int ret; |
1609 | ||
5efd6113 TH |
1610 | might_sleep(); |
1611 | ||
923adde1 TH |
1612 | ret = blk_throtl_init(q); |
1613 | if (ret) | |
1614 | return ret; | |
1615 | ||
1616 | mutex_lock(&all_q_mutex); | |
1617 | INIT_LIST_HEAD(&q->all_q_node); | |
1618 | list_add_tail(&q->all_q_node, &all_q_list); | |
1619 | mutex_unlock(&all_q_mutex); | |
1620 | ||
1621 | return 0; | |
5efd6113 TH |
1622 | } |
1623 | ||
1624 | /** | |
1625 | * blkcg_drain_queue - drain blkcg part of request_queue | |
1626 | * @q: request_queue to drain | |
1627 | * | |
1628 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
1629 | */ | |
1630 | void blkcg_drain_queue(struct request_queue *q) | |
1631 | { | |
1632 | lockdep_assert_held(q->queue_lock); | |
1633 | ||
1634 | blk_throtl_drain(q); | |
1635 | } | |
1636 | ||
1637 | /** | |
1638 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
1639 | * @q: request_queue being released | |
1640 | * | |
1641 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
1642 | */ | |
1643 | void blkcg_exit_queue(struct request_queue *q) | |
1644 | { | |
923adde1 TH |
1645 | mutex_lock(&all_q_mutex); |
1646 | list_del_init(&q->all_q_node); | |
1647 | mutex_unlock(&all_q_mutex); | |
1648 | ||
5efd6113 TH |
1649 | blk_throtl_exit(q); |
1650 | } | |
1651 | ||
31e4c28d VG |
1652 | /* |
1653 | * We cannot support shared io contexts, as we have no mean to support | |
1654 | * two tasks with the same ioc in two different groups without major rework | |
1655 | * of the main cic data structures. For now we allow a task to change | |
1656 | * its cgroup only if it's the only owner of its ioc. | |
1657 | */ | |
bb9d97b6 TH |
1658 | static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
1659 | struct cgroup_taskset *tset) | |
31e4c28d | 1660 | { |
bb9d97b6 | 1661 | struct task_struct *task; |
31e4c28d VG |
1662 | struct io_context *ioc; |
1663 | int ret = 0; | |
1664 | ||
1665 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
bb9d97b6 TH |
1666 | cgroup_taskset_for_each(task, cgrp, tset) { |
1667 | task_lock(task); | |
1668 | ioc = task->io_context; | |
1669 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
1670 | ret = -EINVAL; | |
1671 | task_unlock(task); | |
1672 | if (ret) | |
1673 | break; | |
1674 | } | |
31e4c28d VG |
1675 | return ret; |
1676 | } | |
1677 | ||
bb9d97b6 TH |
1678 | static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
1679 | struct cgroup_taskset *tset) | |
31e4c28d | 1680 | { |
bb9d97b6 | 1681 | struct task_struct *task; |
31e4c28d VG |
1682 | struct io_context *ioc; |
1683 | ||
bb9d97b6 | 1684 | cgroup_taskset_for_each(task, cgrp, tset) { |
b3c9dd18 LT |
1685 | /* we don't lose anything even if ioc allocation fails */ |
1686 | ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); | |
1687 | if (ioc) { | |
1688 | ioc_cgroup_changed(ioc); | |
11a3122f | 1689 | put_io_context(ioc); |
b3c9dd18 | 1690 | } |
bb9d97b6 | 1691 | } |
31e4c28d VG |
1692 | } |
1693 | ||
923adde1 TH |
1694 | static void blkcg_bypass_start(void) |
1695 | __acquires(&all_q_mutex) | |
1696 | { | |
1697 | struct request_queue *q; | |
1698 | ||
1699 | mutex_lock(&all_q_mutex); | |
1700 | ||
1701 | list_for_each_entry(q, &all_q_list, all_q_node) { | |
1702 | blk_queue_bypass_start(q); | |
1703 | blkg_destroy_all(q); | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | static void blkcg_bypass_end(void) | |
1708 | __releases(&all_q_mutex) | |
1709 | { | |
1710 | struct request_queue *q; | |
1711 | ||
1712 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1713 | blk_queue_bypass_end(q); | |
1714 | ||
1715 | mutex_unlock(&all_q_mutex); | |
1716 | } | |
1717 | ||
3e252066 VG |
1718 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
1719 | { | |
923adde1 | 1720 | blkcg_bypass_start(); |
3e252066 | 1721 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1722 | |
1723 | BUG_ON(blkio_policy[blkiop->plid]); | |
1724 | blkio_policy[blkiop->plid] = blkiop; | |
3e252066 | 1725 | list_add_tail(&blkiop->list, &blkio_list); |
035d10b2 | 1726 | |
3e252066 | 1727 | spin_unlock(&blkio_list_lock); |
923adde1 | 1728 | blkcg_bypass_end(); |
3e252066 VG |
1729 | } |
1730 | EXPORT_SYMBOL_GPL(blkio_policy_register); | |
1731 | ||
1732 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) | |
1733 | { | |
923adde1 | 1734 | blkcg_bypass_start(); |
3e252066 | 1735 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1736 | |
1737 | BUG_ON(blkio_policy[blkiop->plid] != blkiop); | |
1738 | blkio_policy[blkiop->plid] = NULL; | |
3e252066 | 1739 | list_del_init(&blkiop->list); |
035d10b2 | 1740 | |
3e252066 | 1741 | spin_unlock(&blkio_list_lock); |
923adde1 | 1742 | blkcg_bypass_end(); |
3e252066 VG |
1743 | } |
1744 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |