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 | { | |
e8989fae | 464 | int i; |
549d3aa8 TH |
465 | |
466 | if (!blkg) | |
467 | return; | |
468 | ||
e8989fae TH |
469 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
470 | struct blkg_policy_data *pd = blkg->pd[i]; | |
471 | ||
472 | if (pd) { | |
473 | free_percpu(pd->stats_cpu); | |
474 | kfree(pd); | |
475 | } | |
0381411e | 476 | } |
e8989fae | 477 | |
549d3aa8 | 478 | kfree(blkg); |
0381411e TH |
479 | } |
480 | ||
481 | /** | |
482 | * blkg_alloc - allocate a blkg | |
483 | * @blkcg: block cgroup the new blkg is associated with | |
484 | * @q: request_queue the new blkg is associated with | |
0381411e | 485 | * |
e8989fae | 486 | * Allocate a new blkg assocating @blkcg and @q. |
0381411e TH |
487 | * |
488 | * FIXME: Should be called with queue locked but currently isn't due to | |
489 | * percpu stat breakage. | |
490 | */ | |
491 | static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, | |
e8989fae | 492 | struct request_queue *q) |
0381411e TH |
493 | { |
494 | struct blkio_group *blkg; | |
e8989fae | 495 | int i; |
0381411e TH |
496 | |
497 | /* alloc and init base part */ | |
498 | blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node); | |
499 | if (!blkg) | |
500 | return NULL; | |
501 | ||
502 | spin_lock_init(&blkg->stats_lock); | |
503 | rcu_assign_pointer(blkg->q, q); | |
e8989fae | 504 | INIT_LIST_HEAD(&blkg->q_node); |
0381411e | 505 | blkg->blkcg = blkcg; |
1adaf3dd | 506 | blkg->refcnt = 1; |
0381411e TH |
507 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
508 | ||
e8989fae TH |
509 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
510 | struct blkio_policy_type *pol = blkio_policy[i]; | |
511 | struct blkg_policy_data *pd; | |
0381411e | 512 | |
e8989fae TH |
513 | if (!pol) |
514 | continue; | |
515 | ||
516 | /* alloc per-policy data and attach it to blkg */ | |
517 | pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC, | |
518 | q->node); | |
519 | if (!pd) { | |
520 | blkg_free(blkg); | |
521 | return NULL; | |
522 | } | |
549d3aa8 | 523 | |
e8989fae TH |
524 | blkg->pd[i] = pd; |
525 | pd->blkg = blkg; | |
549d3aa8 | 526 | |
e8989fae TH |
527 | /* broken, read comment in the callsite */ |
528 | pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); | |
529 | if (!pd->stats_cpu) { | |
530 | blkg_free(blkg); | |
531 | return NULL; | |
532 | } | |
0381411e TH |
533 | } |
534 | ||
549d3aa8 | 535 | /* invoke per-policy init */ |
e8989fae TH |
536 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
537 | struct blkio_policy_type *pol = blkio_policy[i]; | |
538 | ||
539 | if (pol) | |
540 | pol->ops.blkio_init_group_fn(blkg); | |
541 | } | |
542 | ||
0381411e TH |
543 | return blkg; |
544 | } | |
545 | ||
cd1604fa TH |
546 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
547 | struct request_queue *q, | |
548 | enum blkio_policy_id plid, | |
549 | bool for_root) | |
550 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
5624a4e4 | 551 | { |
cd1604fa | 552 | struct blkio_group *blkg, *new_blkg; |
5624a4e4 | 553 | |
cd1604fa TH |
554 | WARN_ON_ONCE(!rcu_read_lock_held()); |
555 | lockdep_assert_held(q->queue_lock); | |
556 | ||
557 | /* | |
558 | * This could be the first entry point of blkcg implementation and | |
559 | * we shouldn't allow anything to go through for a bypassing queue. | |
560 | * The following can be removed if blkg lookup is guaranteed to | |
561 | * fail on a bypassing queue. | |
562 | */ | |
563 | if (unlikely(blk_queue_bypass(q)) && !for_root) | |
564 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | |
565 | ||
e8989fae | 566 | blkg = blkg_lookup(blkcg, q); |
cd1604fa TH |
567 | if (blkg) |
568 | return blkg; | |
569 | ||
7ee9c562 | 570 | /* blkg holds a reference to blkcg */ |
cd1604fa TH |
571 | if (!css_tryget(&blkcg->css)) |
572 | return ERR_PTR(-EINVAL); | |
573 | ||
574 | /* | |
575 | * Allocate and initialize. | |
576 | * | |
577 | * FIXME: The following is broken. Percpu memory allocation | |
578 | * requires %GFP_KERNEL context and can't be performed from IO | |
579 | * path. Allocation here should inherently be atomic and the | |
580 | * following lock dancing can be removed once the broken percpu | |
581 | * allocation is fixed. | |
582 | */ | |
583 | spin_unlock_irq(q->queue_lock); | |
584 | rcu_read_unlock(); | |
585 | ||
e8989fae | 586 | new_blkg = blkg_alloc(blkcg, q); |
cd1604fa TH |
587 | |
588 | rcu_read_lock(); | |
589 | spin_lock_irq(q->queue_lock); | |
31e4c28d | 590 | |
cd1604fa TH |
591 | /* did bypass get turned on inbetween? */ |
592 | if (unlikely(blk_queue_bypass(q)) && !for_root) { | |
593 | blkg = ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | |
594 | goto out; | |
595 | } | |
596 | ||
597 | /* did someone beat us to it? */ | |
e8989fae | 598 | blkg = blkg_lookup(blkcg, q); |
cd1604fa TH |
599 | if (unlikely(blkg)) |
600 | goto out; | |
601 | ||
602 | /* did alloc fail? */ | |
0381411e | 603 | if (unlikely(!new_blkg)) { |
cd1604fa TH |
604 | blkg = ERR_PTR(-ENOMEM); |
605 | goto out; | |
606 | } | |
607 | ||
608 | /* insert */ | |
609 | spin_lock(&blkcg->lock); | |
610 | swap(blkg, new_blkg); | |
03aa264a | 611 | |
31e4c28d | 612 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
e8989fae TH |
613 | list_add(&blkg->q_node, &q->blkg_list); |
614 | q->nr_blkgs++; | |
03aa264a | 615 | |
cd1604fa TH |
616 | spin_unlock(&blkcg->lock); |
617 | out: | |
0381411e | 618 | blkg_free(new_blkg); |
cd1604fa | 619 | return blkg; |
31e4c28d | 620 | } |
cd1604fa | 621 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
31e4c28d | 622 | |
31e4c28d | 623 | /* called under rcu_read_lock(). */ |
cd1604fa | 624 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
e8989fae | 625 | struct request_queue *q) |
31e4c28d VG |
626 | { |
627 | struct blkio_group *blkg; | |
628 | struct hlist_node *n; | |
31e4c28d | 629 | |
ca32aefc | 630 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) |
e8989fae | 631 | if (blkg->q == q) |
31e4c28d | 632 | return blkg; |
31e4c28d VG |
633 | return NULL; |
634 | } | |
cd1604fa | 635 | EXPORT_SYMBOL_GPL(blkg_lookup); |
31e4c28d | 636 | |
e8989fae | 637 | static void blkg_destroy(struct blkio_group *blkg) |
03aa264a TH |
638 | { |
639 | struct request_queue *q = blkg->q; | |
9f13ef67 | 640 | struct blkio_cgroup *blkcg = blkg->blkcg; |
03aa264a TH |
641 | |
642 | lockdep_assert_held(q->queue_lock); | |
9f13ef67 | 643 | lockdep_assert_held(&blkcg->lock); |
03aa264a TH |
644 | |
645 | /* Something wrong if we are trying to remove same group twice */ | |
e8989fae | 646 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
9f13ef67 | 647 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
e8989fae | 648 | list_del_init(&blkg->q_node); |
9f13ef67 | 649 | hlist_del_init_rcu(&blkg->blkcg_node); |
03aa264a | 650 | |
e8989fae TH |
651 | WARN_ON_ONCE(q->nr_blkgs <= 0); |
652 | q->nr_blkgs--; | |
03aa264a TH |
653 | |
654 | /* | |
655 | * Put the reference taken at the time of creation so that when all | |
656 | * queues are gone, group can be destroyed. | |
657 | */ | |
658 | blkg_put(blkg); | |
659 | } | |
660 | ||
e8989fae TH |
661 | /* |
662 | * XXX: This updates blkg policy data in-place for root blkg, which is | |
663 | * necessary across elevator switch and policy registration as root blkgs | |
664 | * aren't shot down. This broken and racy implementation is temporary. | |
665 | * Eventually, blkg shoot down will be replaced by proper in-place update. | |
666 | */ | |
667 | void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid) | |
668 | { | |
669 | struct blkio_policy_type *pol = blkio_policy[plid]; | |
670 | struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q); | |
671 | struct blkg_policy_data *pd; | |
672 | ||
673 | if (!blkg) | |
674 | return; | |
675 | ||
676 | kfree(blkg->pd[plid]); | |
677 | blkg->pd[plid] = NULL; | |
678 | ||
679 | if (!pol) | |
680 | return; | |
681 | ||
682 | pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL); | |
683 | WARN_ON_ONCE(!pd); | |
684 | ||
685 | pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); | |
686 | WARN_ON_ONCE(!pd->stats_cpu); | |
687 | ||
688 | blkg->pd[plid] = pd; | |
689 | pd->blkg = blkg; | |
690 | pol->ops.blkio_init_group_fn(blkg); | |
691 | } | |
692 | EXPORT_SYMBOL_GPL(update_root_blkg_pd); | |
693 | ||
9f13ef67 TH |
694 | /** |
695 | * blkg_destroy_all - destroy all blkgs associated with a request_queue | |
696 | * @q: request_queue of interest | |
697 | * @destroy_root: whether to destroy root blkg or not | |
698 | * | |
699 | * Destroy blkgs associated with @q. If @destroy_root is %true, all are | |
700 | * destroyed; otherwise, root blkg is left alone. | |
701 | */ | |
e8989fae | 702 | void blkg_destroy_all(struct request_queue *q, bool destroy_root) |
72e06c25 | 703 | { |
03aa264a | 704 | struct blkio_group *blkg, *n; |
72e06c25 | 705 | |
9f13ef67 | 706 | spin_lock_irq(q->queue_lock); |
72e06c25 | 707 | |
9f13ef67 TH |
708 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
709 | struct blkio_cgroup *blkcg = blkg->blkcg; | |
72e06c25 | 710 | |
9f13ef67 TH |
711 | /* skip root? */ |
712 | if (!destroy_root && blkg->blkcg == &blkio_root_cgroup) | |
713 | continue; | |
72e06c25 | 714 | |
9f13ef67 TH |
715 | spin_lock(&blkcg->lock); |
716 | blkg_destroy(blkg); | |
717 | spin_unlock(&blkcg->lock); | |
72e06c25 | 718 | } |
9f13ef67 TH |
719 | |
720 | spin_unlock_irq(q->queue_lock); | |
72e06c25 | 721 | } |
03aa264a | 722 | EXPORT_SYMBOL_GPL(blkg_destroy_all); |
72e06c25 | 723 | |
1adaf3dd TH |
724 | static void blkg_rcu_free(struct rcu_head *rcu_head) |
725 | { | |
726 | blkg_free(container_of(rcu_head, struct blkio_group, rcu_head)); | |
727 | } | |
728 | ||
729 | void __blkg_release(struct blkio_group *blkg) | |
730 | { | |
731 | /* release the extra blkcg reference this blkg has been holding */ | |
732 | css_put(&blkg->blkcg->css); | |
733 | ||
734 | /* | |
735 | * A group is freed in rcu manner. But having an rcu lock does not | |
736 | * mean that one can access all the fields of blkg and assume these | |
737 | * are valid. For example, don't try to follow throtl_data and | |
738 | * request queue links. | |
739 | * | |
740 | * Having a reference to blkg under an rcu allows acess to only | |
741 | * values local to groups like group stats and group rate limits | |
742 | */ | |
743 | call_rcu(&blkg->rcu_head, blkg_rcu_free); | |
744 | } | |
745 | EXPORT_SYMBOL_GPL(__blkg_release); | |
746 | ||
c1768268 | 747 | static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid) |
f0bdc8cd | 748 | { |
c1768268 | 749 | struct blkg_policy_data *pd = blkg->pd[plid]; |
f0bdc8cd VG |
750 | struct blkio_group_stats_cpu *stats_cpu; |
751 | int i, j, k; | |
752 | /* | |
753 | * Note: On 64 bit arch this should not be an issue. This has the | |
754 | * possibility of returning some inconsistent value on 32bit arch | |
755 | * as 64bit update on 32bit is non atomic. Taking care of this | |
756 | * corner case makes code very complicated, like sending IPIs to | |
757 | * cpus, taking care of stats of offline cpus etc. | |
758 | * | |
759 | * reset stats is anyway more of a debug feature and this sounds a | |
760 | * corner case. So I am not complicating the code yet until and | |
761 | * unless this becomes a real issue. | |
762 | */ | |
763 | for_each_possible_cpu(i) { | |
549d3aa8 | 764 | stats_cpu = per_cpu_ptr(pd->stats_cpu, i); |
f0bdc8cd VG |
765 | stats_cpu->sectors = 0; |
766 | for(j = 0; j < BLKIO_STAT_CPU_NR; j++) | |
767 | for (k = 0; k < BLKIO_STAT_TOTAL; k++) | |
768 | stats_cpu->stat_arr_cpu[j][k] = 0; | |
769 | } | |
770 | } | |
771 | ||
303a3acb | 772 | static int |
84c124da | 773 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
303a3acb DS |
774 | { |
775 | struct blkio_cgroup *blkcg; | |
776 | struct blkio_group *blkg; | |
812df48d | 777 | struct blkio_group_stats *stats; |
303a3acb | 778 | struct hlist_node *n; |
cdc1184c DS |
779 | uint64_t queued[BLKIO_STAT_TOTAL]; |
780 | int i; | |
812df48d DS |
781 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
782 | bool idling, waiting, empty; | |
783 | unsigned long long now = sched_clock(); | |
784 | #endif | |
303a3acb DS |
785 | |
786 | blkcg = cgroup_to_blkio_cgroup(cgroup); | |
e8989fae | 787 | spin_lock(&blkio_list_lock); |
303a3acb DS |
788 | spin_lock_irq(&blkcg->lock); |
789 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { | |
e8989fae | 790 | struct blkio_policy_type *pol; |
549d3aa8 | 791 | |
e8989fae TH |
792 | list_for_each_entry(pol, &blkio_list, list) { |
793 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; | |
794 | ||
795 | spin_lock(&blkg->stats_lock); | |
796 | stats = &pd->stats; | |
812df48d | 797 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
e8989fae TH |
798 | idling = blkio_blkg_idling(stats); |
799 | waiting = blkio_blkg_waiting(stats); | |
800 | empty = blkio_blkg_empty(stats); | |
812df48d | 801 | #endif |
e8989fae TH |
802 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) |
803 | queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i]; | |
804 | memset(stats, 0, sizeof(struct blkio_group_stats)); | |
805 | for (i = 0; i < BLKIO_STAT_TOTAL; i++) | |
806 | stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i]; | |
812df48d | 807 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
e8989fae TH |
808 | if (idling) { |
809 | blkio_mark_blkg_idling(stats); | |
810 | stats->start_idle_time = now; | |
811 | } | |
812 | if (waiting) { | |
813 | blkio_mark_blkg_waiting(stats); | |
814 | stats->start_group_wait_time = now; | |
815 | } | |
816 | if (empty) { | |
817 | blkio_mark_blkg_empty(stats); | |
818 | stats->start_empty_time = now; | |
819 | } | |
812df48d | 820 | #endif |
e8989fae | 821 | spin_unlock(&blkg->stats_lock); |
f0bdc8cd | 822 | |
e8989fae TH |
823 | /* Reset Per cpu stats which don't take blkg->stats_lock */ |
824 | blkio_reset_stats_cpu(blkg, pol->plid); | |
825 | } | |
303a3acb | 826 | } |
f0bdc8cd | 827 | |
303a3acb | 828 | spin_unlock_irq(&blkcg->lock); |
e8989fae | 829 | spin_unlock(&blkio_list_lock); |
303a3acb DS |
830 | return 0; |
831 | } | |
832 | ||
7a4dd281 TH |
833 | static void blkio_get_key_name(enum stat_sub_type type, const char *dname, |
834 | char *str, int chars_left, bool diskname_only) | |
303a3acb | 835 | { |
7a4dd281 | 836 | snprintf(str, chars_left, "%s", dname); |
303a3acb DS |
837 | chars_left -= strlen(str); |
838 | if (chars_left <= 0) { | |
839 | printk(KERN_WARNING | |
840 | "Possibly incorrect cgroup stat display format"); | |
841 | return; | |
842 | } | |
84c124da DS |
843 | if (diskname_only) |
844 | return; | |
303a3acb | 845 | switch (type) { |
84c124da | 846 | case BLKIO_STAT_READ: |
303a3acb DS |
847 | strlcat(str, " Read", chars_left); |
848 | break; | |
84c124da | 849 | case BLKIO_STAT_WRITE: |
303a3acb DS |
850 | strlcat(str, " Write", chars_left); |
851 | break; | |
84c124da | 852 | case BLKIO_STAT_SYNC: |
303a3acb DS |
853 | strlcat(str, " Sync", chars_left); |
854 | break; | |
84c124da | 855 | case BLKIO_STAT_ASYNC: |
303a3acb DS |
856 | strlcat(str, " Async", chars_left); |
857 | break; | |
84c124da | 858 | case BLKIO_STAT_TOTAL: |
303a3acb DS |
859 | strlcat(str, " Total", chars_left); |
860 | break; | |
861 | default: | |
862 | strlcat(str, " Invalid", chars_left); | |
863 | } | |
864 | } | |
865 | ||
84c124da | 866 | static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val, |
7a4dd281 | 867 | struct cgroup_map_cb *cb, const char *dname) |
84c124da | 868 | { |
7a4dd281 | 869 | blkio_get_key_name(0, dname, str, chars_left, true); |
84c124da DS |
870 | cb->fill(cb, str, val); |
871 | return val; | |
872 | } | |
303a3acb | 873 | |
5624a4e4 | 874 | |
c1768268 | 875 | static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid, |
5624a4e4 VG |
876 | enum stat_type_cpu type, enum stat_sub_type sub_type) |
877 | { | |
c1768268 | 878 | struct blkg_policy_data *pd = blkg->pd[plid]; |
5624a4e4 VG |
879 | int cpu; |
880 | struct blkio_group_stats_cpu *stats_cpu; | |
575969a0 | 881 | u64 val = 0, tval; |
5624a4e4 VG |
882 | |
883 | for_each_possible_cpu(cpu) { | |
575969a0 | 884 | unsigned int start; |
549d3aa8 | 885 | stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu); |
5624a4e4 | 886 | |
575969a0 VG |
887 | do { |
888 | start = u64_stats_fetch_begin(&stats_cpu->syncp); | |
889 | if (type == BLKIO_STAT_CPU_SECTORS) | |
890 | tval = stats_cpu->sectors; | |
891 | else | |
892 | tval = stats_cpu->stat_arr_cpu[type][sub_type]; | |
893 | } while(u64_stats_fetch_retry(&stats_cpu->syncp, start)); | |
894 | ||
895 | val += tval; | |
5624a4e4 VG |
896 | } |
897 | ||
898 | return val; | |
899 | } | |
900 | ||
c1768268 | 901 | static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid, |
7a4dd281 TH |
902 | struct cgroup_map_cb *cb, const char *dname, |
903 | enum stat_type_cpu type) | |
5624a4e4 VG |
904 | { |
905 | uint64_t disk_total, val; | |
906 | char key_str[MAX_KEY_LEN]; | |
907 | enum stat_sub_type sub_type; | |
908 | ||
909 | if (type == BLKIO_STAT_CPU_SECTORS) { | |
c1768268 | 910 | val = blkio_read_stat_cpu(blkg, plid, type, 0); |
7a4dd281 TH |
911 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, |
912 | dname); | |
5624a4e4 VG |
913 | } |
914 | ||
915 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; | |
916 | sub_type++) { | |
7a4dd281 TH |
917 | blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN, |
918 | false); | |
c1768268 | 919 | val = blkio_read_stat_cpu(blkg, plid, type, sub_type); |
5624a4e4 VG |
920 | cb->fill(cb, key_str, val); |
921 | } | |
922 | ||
c1768268 TH |
923 | disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) + |
924 | blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE); | |
5624a4e4 | 925 | |
7a4dd281 TH |
926 | blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN, |
927 | false); | |
5624a4e4 VG |
928 | cb->fill(cb, key_str, disk_total); |
929 | return disk_total; | |
930 | } | |
931 | ||
84c124da | 932 | /* This should be called with blkg->stats_lock held */ |
c1768268 | 933 | static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid, |
7a4dd281 TH |
934 | struct cgroup_map_cb *cb, const char *dname, |
935 | enum stat_type type) | |
303a3acb | 936 | { |
c1768268 | 937 | struct blkg_policy_data *pd = blkg->pd[plid]; |
303a3acb DS |
938 | uint64_t disk_total; |
939 | char key_str[MAX_KEY_LEN]; | |
84c124da DS |
940 | enum stat_sub_type sub_type; |
941 | ||
942 | if (type == BLKIO_STAT_TIME) | |
943 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 944 | pd->stats.time, cb, dname); |
9026e521 | 945 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
167400d3 JT |
946 | if (type == BLKIO_STAT_UNACCOUNTED_TIME) |
947 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 948 | pd->stats.unaccounted_time, cb, dname); |
cdc1184c | 949 | if (type == BLKIO_STAT_AVG_QUEUE_SIZE) { |
549d3aa8 TH |
950 | uint64_t sum = pd->stats.avg_queue_size_sum; |
951 | uint64_t samples = pd->stats.avg_queue_size_samples; | |
cdc1184c DS |
952 | if (samples) |
953 | do_div(sum, samples); | |
954 | else | |
955 | sum = 0; | |
7a4dd281 TH |
956 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, |
957 | sum, cb, dname); | |
cdc1184c | 958 | } |
812df48d DS |
959 | if (type == BLKIO_STAT_GROUP_WAIT_TIME) |
960 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 961 | pd->stats.group_wait_time, cb, dname); |
812df48d DS |
962 | if (type == BLKIO_STAT_IDLE_TIME) |
963 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 964 | pd->stats.idle_time, cb, dname); |
812df48d DS |
965 | if (type == BLKIO_STAT_EMPTY_TIME) |
966 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 967 | pd->stats.empty_time, cb, dname); |
84c124da DS |
968 | if (type == BLKIO_STAT_DEQUEUE) |
969 | return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, | |
549d3aa8 | 970 | pd->stats.dequeue, cb, dname); |
84c124da | 971 | #endif |
303a3acb | 972 | |
84c124da DS |
973 | for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL; |
974 | sub_type++) { | |
7a4dd281 TH |
975 | blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN, |
976 | false); | |
549d3aa8 | 977 | cb->fill(cb, key_str, pd->stats.stat_arr[type][sub_type]); |
303a3acb | 978 | } |
549d3aa8 TH |
979 | disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ] + |
980 | pd->stats.stat_arr[type][BLKIO_STAT_WRITE]; | |
7a4dd281 TH |
981 | blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN, |
982 | false); | |
303a3acb DS |
983 | cb->fill(cb, key_str, disk_total); |
984 | return disk_total; | |
985 | } | |
986 | ||
4bfd482e TH |
987 | static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid, |
988 | int fileid, struct blkio_cgroup *blkcg) | |
34d0f179 | 989 | { |
ece84241 | 990 | struct gendisk *disk = NULL; |
e56da7e2 | 991 | struct blkio_group *blkg = NULL; |
549d3aa8 | 992 | struct blkg_policy_data *pd; |
34d0f179 | 993 | char *s[4], *p, *major_s = NULL, *minor_s = NULL; |
d11bb446 | 994 | unsigned long major, minor; |
ece84241 TH |
995 | int i = 0, ret = -EINVAL; |
996 | int part; | |
34d0f179 | 997 | dev_t dev; |
d11bb446 | 998 | u64 temp; |
34d0f179 GJ |
999 | |
1000 | memset(s, 0, sizeof(s)); | |
1001 | ||
1002 | while ((p = strsep(&buf, " ")) != NULL) { | |
1003 | if (!*p) | |
1004 | continue; | |
1005 | ||
1006 | s[i++] = p; | |
1007 | ||
1008 | /* Prevent from inputing too many things */ | |
1009 | if (i == 3) | |
1010 | break; | |
1011 | } | |
1012 | ||
1013 | if (i != 2) | |
ece84241 | 1014 | goto out; |
34d0f179 GJ |
1015 | |
1016 | p = strsep(&s[0], ":"); | |
1017 | if (p != NULL) | |
1018 | major_s = p; | |
1019 | else | |
ece84241 | 1020 | goto out; |
34d0f179 GJ |
1021 | |
1022 | minor_s = s[0]; | |
1023 | if (!minor_s) | |
ece84241 | 1024 | goto out; |
34d0f179 | 1025 | |
ece84241 TH |
1026 | if (strict_strtoul(major_s, 10, &major)) |
1027 | goto out; | |
34d0f179 | 1028 | |
ece84241 TH |
1029 | if (strict_strtoul(minor_s, 10, &minor)) |
1030 | goto out; | |
34d0f179 GJ |
1031 | |
1032 | dev = MKDEV(major, minor); | |
1033 | ||
ece84241 TH |
1034 | if (strict_strtoull(s[1], 10, &temp)) |
1035 | goto out; | |
34d0f179 | 1036 | |
e56da7e2 | 1037 | disk = get_gendisk(dev, &part); |
4bfd482e | 1038 | if (!disk || part) |
e56da7e2 | 1039 | goto out; |
e56da7e2 TH |
1040 | |
1041 | rcu_read_lock(); | |
1042 | ||
4bfd482e TH |
1043 | spin_lock_irq(disk->queue->queue_lock); |
1044 | blkg = blkg_lookup_create(blkcg, disk->queue, plid, false); | |
1045 | spin_unlock_irq(disk->queue->queue_lock); | |
e56da7e2 | 1046 | |
4bfd482e TH |
1047 | if (IS_ERR(blkg)) { |
1048 | ret = PTR_ERR(blkg); | |
1049 | goto out_unlock; | |
d11bb446 | 1050 | } |
34d0f179 | 1051 | |
549d3aa8 TH |
1052 | pd = blkg->pd[plid]; |
1053 | ||
062a644d VG |
1054 | switch (plid) { |
1055 | case BLKIO_POLICY_PROP: | |
d11bb446 WG |
1056 | if ((temp < BLKIO_WEIGHT_MIN && temp > 0) || |
1057 | temp > BLKIO_WEIGHT_MAX) | |
e56da7e2 | 1058 | goto out_unlock; |
34d0f179 | 1059 | |
549d3aa8 | 1060 | pd->conf.weight = temp; |
c1768268 | 1061 | blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight); |
4c9eefa1 VG |
1062 | break; |
1063 | case BLKIO_POLICY_THROTL: | |
7702e8f4 VG |
1064 | switch(fileid) { |
1065 | case BLKIO_THROTL_read_bps_device: | |
549d3aa8 | 1066 | pd->conf.bps[READ] = temp; |
c1768268 | 1067 | blkio_update_group_bps(blkg, plid, temp ?: -1, fileid); |
e56da7e2 | 1068 | break; |
7702e8f4 | 1069 | case BLKIO_THROTL_write_bps_device: |
549d3aa8 | 1070 | pd->conf.bps[WRITE] = temp; |
c1768268 | 1071 | blkio_update_group_bps(blkg, plid, temp ?: -1, fileid); |
7702e8f4 VG |
1072 | break; |
1073 | case BLKIO_THROTL_read_iops_device: | |
e56da7e2 TH |
1074 | if (temp > THROTL_IOPS_MAX) |
1075 | goto out_unlock; | |
549d3aa8 | 1076 | pd->conf.iops[READ] = temp; |
c1768268 | 1077 | blkio_update_group_iops(blkg, plid, temp ?: -1, fileid); |
e56da7e2 | 1078 | break; |
7702e8f4 | 1079 | case BLKIO_THROTL_write_iops_device: |
d11bb446 | 1080 | if (temp > THROTL_IOPS_MAX) |
e56da7e2 | 1081 | goto out_unlock; |
549d3aa8 | 1082 | pd->conf.iops[WRITE] = temp; |
c1768268 | 1083 | blkio_update_group_iops(blkg, plid, temp ?: -1, fileid); |
7702e8f4 VG |
1084 | break; |
1085 | } | |
062a644d VG |
1086 | break; |
1087 | default: | |
1088 | BUG(); | |
1089 | } | |
ece84241 | 1090 | ret = 0; |
e56da7e2 TH |
1091 | out_unlock: |
1092 | rcu_read_unlock(); | |
ece84241 TH |
1093 | out: |
1094 | put_disk(disk); | |
e56da7e2 TH |
1095 | |
1096 | /* | |
1097 | * If queue was bypassing, we should retry. Do so after a short | |
1098 | * msleep(). It isn't strictly necessary but queue can be | |
1099 | * bypassing for some time and it's always nice to avoid busy | |
1100 | * looping. | |
1101 | */ | |
1102 | if (ret == -EBUSY) { | |
1103 | msleep(10); | |
1104 | return restart_syscall(); | |
1105 | } | |
ece84241 | 1106 | return ret; |
34d0f179 GJ |
1107 | } |
1108 | ||
062a644d VG |
1109 | static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft, |
1110 | const char *buffer) | |
34d0f179 GJ |
1111 | { |
1112 | int ret = 0; | |
1113 | char *buf; | |
e56da7e2 | 1114 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
062a644d VG |
1115 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
1116 | int fileid = BLKIOFILE_ATTR(cft->private); | |
34d0f179 GJ |
1117 | |
1118 | buf = kstrdup(buffer, GFP_KERNEL); | |
1119 | if (!buf) | |
1120 | return -ENOMEM; | |
1121 | ||
4bfd482e | 1122 | ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg); |
34d0f179 GJ |
1123 | kfree(buf); |
1124 | return ret; | |
1125 | } | |
1126 | ||
92616b5b VG |
1127 | static const char *blkg_dev_name(struct blkio_group *blkg) |
1128 | { | |
1129 | /* some drivers (floppy) instantiate a queue w/o disk registered */ | |
1130 | if (blkg->q->backing_dev_info.dev) | |
1131 | return dev_name(blkg->q->backing_dev_info.dev); | |
1132 | return NULL; | |
1133 | } | |
1134 | ||
4bfd482e TH |
1135 | static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg, |
1136 | struct seq_file *m) | |
34d0f179 | 1137 | { |
c1768268 | 1138 | int plid = BLKIOFILE_POLICY(cft->private); |
4bfd482e | 1139 | int fileid = BLKIOFILE_ATTR(cft->private); |
c1768268 TH |
1140 | struct blkg_policy_data *pd = blkg->pd[plid]; |
1141 | const char *dname = blkg_dev_name(blkg); | |
4bfd482e TH |
1142 | int rw = WRITE; |
1143 | ||
92616b5b VG |
1144 | if (!dname) |
1145 | return; | |
1146 | ||
c1768268 | 1147 | switch (plid) { |
062a644d | 1148 | case BLKIO_POLICY_PROP: |
549d3aa8 | 1149 | if (pd->conf.weight) |
7a4dd281 | 1150 | seq_printf(m, "%s\t%u\n", |
549d3aa8 | 1151 | dname, pd->conf.weight); |
4c9eefa1 VG |
1152 | break; |
1153 | case BLKIO_POLICY_THROTL: | |
4bfd482e | 1154 | switch (fileid) { |
7702e8f4 | 1155 | case BLKIO_THROTL_read_bps_device: |
4bfd482e | 1156 | rw = READ; |
7702e8f4 | 1157 | case BLKIO_THROTL_write_bps_device: |
549d3aa8 | 1158 | if (pd->conf.bps[rw]) |
7a4dd281 | 1159 | seq_printf(m, "%s\t%llu\n", |
549d3aa8 | 1160 | dname, pd->conf.bps[rw]); |
7702e8f4 VG |
1161 | break; |
1162 | case BLKIO_THROTL_read_iops_device: | |
4bfd482e | 1163 | rw = READ; |
7702e8f4 | 1164 | case BLKIO_THROTL_write_iops_device: |
549d3aa8 | 1165 | if (pd->conf.iops[rw]) |
7a4dd281 | 1166 | seq_printf(m, "%s\t%u\n", |
549d3aa8 | 1167 | dname, pd->conf.iops[rw]); |
7702e8f4 VG |
1168 | break; |
1169 | } | |
062a644d VG |
1170 | break; |
1171 | default: | |
1172 | BUG(); | |
1173 | } | |
1174 | } | |
34d0f179 | 1175 | |
062a644d | 1176 | /* cgroup files which read their data from policy nodes end up here */ |
4bfd482e TH |
1177 | static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg, |
1178 | struct seq_file *m) | |
34d0f179 | 1179 | { |
4bfd482e TH |
1180 | struct blkio_group *blkg; |
1181 | struct hlist_node *n; | |
34d0f179 | 1182 | |
4bfd482e TH |
1183 | spin_lock_irq(&blkcg->lock); |
1184 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) | |
e8989fae | 1185 | blkio_print_group_conf(cft, blkg, m); |
4bfd482e | 1186 | spin_unlock_irq(&blkcg->lock); |
062a644d VG |
1187 | } |
1188 | ||
1189 | static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft, | |
1190 | struct seq_file *m) | |
1191 | { | |
1192 | struct blkio_cgroup *blkcg; | |
1193 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1194 | int name = BLKIOFILE_ATTR(cft->private); | |
1195 | ||
1196 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1197 | ||
1198 | switch(plid) { | |
1199 | case BLKIO_POLICY_PROP: | |
1200 | switch(name) { | |
1201 | case BLKIO_PROP_weight_device: | |
4bfd482e | 1202 | blkio_read_conf(cft, blkcg, m); |
062a644d VG |
1203 | return 0; |
1204 | default: | |
1205 | BUG(); | |
1206 | } | |
1207 | break; | |
4c9eefa1 VG |
1208 | case BLKIO_POLICY_THROTL: |
1209 | switch(name){ | |
1210 | case BLKIO_THROTL_read_bps_device: | |
1211 | case BLKIO_THROTL_write_bps_device: | |
7702e8f4 VG |
1212 | case BLKIO_THROTL_read_iops_device: |
1213 | case BLKIO_THROTL_write_iops_device: | |
4bfd482e | 1214 | blkio_read_conf(cft, blkcg, m); |
4c9eefa1 VG |
1215 | return 0; |
1216 | default: | |
1217 | BUG(); | |
1218 | } | |
1219 | break; | |
062a644d VG |
1220 | default: |
1221 | BUG(); | |
1222 | } | |
1223 | ||
1224 | return 0; | |
1225 | } | |
1226 | ||
1227 | static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg, | |
5624a4e4 VG |
1228 | struct cftype *cft, struct cgroup_map_cb *cb, |
1229 | enum stat_type type, bool show_total, bool pcpu) | |
062a644d VG |
1230 | { |
1231 | struct blkio_group *blkg; | |
1232 | struct hlist_node *n; | |
1233 | uint64_t cgroup_total = 0; | |
1234 | ||
1235 | rcu_read_lock(); | |
1236 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) { | |
92616b5b | 1237 | const char *dname = blkg_dev_name(blkg); |
c1768268 | 1238 | int plid = BLKIOFILE_POLICY(cft->private); |
7a4dd281 | 1239 | |
e8989fae | 1240 | if (!dname) |
7a4dd281 | 1241 | continue; |
c1768268 TH |
1242 | if (pcpu) { |
1243 | cgroup_total += blkio_get_stat_cpu(blkg, plid, | |
1244 | cb, dname, type); | |
1245 | } else { | |
7a4dd281 | 1246 | spin_lock_irq(&blkg->stats_lock); |
c1768268 TH |
1247 | cgroup_total += blkio_get_stat(blkg, plid, |
1248 | cb, dname, type); | |
7a4dd281 | 1249 | spin_unlock_irq(&blkg->stats_lock); |
062a644d VG |
1250 | } |
1251 | } | |
1252 | if (show_total) | |
1253 | cb->fill(cb, "Total", cgroup_total); | |
1254 | rcu_read_unlock(); | |
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | /* All map kind of cgroup file get serviced by this function */ | |
1259 | static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft, | |
1260 | struct cgroup_map_cb *cb) | |
1261 | { | |
1262 | struct blkio_cgroup *blkcg; | |
1263 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1264 | int name = BLKIOFILE_ATTR(cft->private); | |
1265 | ||
1266 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1267 | ||
1268 | switch(plid) { | |
1269 | case BLKIO_POLICY_PROP: | |
1270 | switch(name) { | |
1271 | case BLKIO_PROP_time: | |
1272 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1273 | BLKIO_STAT_TIME, 0, 0); |
062a644d VG |
1274 | case BLKIO_PROP_sectors: |
1275 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1276 | BLKIO_STAT_CPU_SECTORS, 0, 1); |
062a644d VG |
1277 | case BLKIO_PROP_io_service_bytes: |
1278 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1279 | BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1); |
062a644d VG |
1280 | case BLKIO_PROP_io_serviced: |
1281 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1282 | BLKIO_STAT_CPU_SERVICED, 1, 1); |
062a644d VG |
1283 | case BLKIO_PROP_io_service_time: |
1284 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1285 | BLKIO_STAT_SERVICE_TIME, 1, 0); |
062a644d VG |
1286 | case BLKIO_PROP_io_wait_time: |
1287 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1288 | BLKIO_STAT_WAIT_TIME, 1, 0); |
062a644d VG |
1289 | case BLKIO_PROP_io_merged: |
1290 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
317389a7 | 1291 | BLKIO_STAT_CPU_MERGED, 1, 1); |
062a644d VG |
1292 | case BLKIO_PROP_io_queued: |
1293 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1294 | BLKIO_STAT_QUEUED, 1, 0); |
062a644d | 1295 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
9026e521 JT |
1296 | case BLKIO_PROP_unaccounted_time: |
1297 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1298 | BLKIO_STAT_UNACCOUNTED_TIME, 0, 0); |
062a644d VG |
1299 | case BLKIO_PROP_dequeue: |
1300 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1301 | BLKIO_STAT_DEQUEUE, 0, 0); |
062a644d VG |
1302 | case BLKIO_PROP_avg_queue_size: |
1303 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1304 | BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0); |
062a644d VG |
1305 | case BLKIO_PROP_group_wait_time: |
1306 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1307 | BLKIO_STAT_GROUP_WAIT_TIME, 0, 0); |
062a644d VG |
1308 | case BLKIO_PROP_idle_time: |
1309 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1310 | BLKIO_STAT_IDLE_TIME, 0, 0); |
062a644d VG |
1311 | case BLKIO_PROP_empty_time: |
1312 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1313 | BLKIO_STAT_EMPTY_TIME, 0, 0); |
062a644d VG |
1314 | #endif |
1315 | default: | |
1316 | BUG(); | |
1317 | } | |
1318 | break; | |
4c9eefa1 VG |
1319 | case BLKIO_POLICY_THROTL: |
1320 | switch(name){ | |
1321 | case BLKIO_THROTL_io_service_bytes: | |
1322 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1323 | BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1); |
4c9eefa1 VG |
1324 | case BLKIO_THROTL_io_serviced: |
1325 | return blkio_read_blkg_stats(blkcg, cft, cb, | |
5624a4e4 | 1326 | BLKIO_STAT_CPU_SERVICED, 1, 1); |
4c9eefa1 VG |
1327 | default: |
1328 | BUG(); | |
1329 | } | |
1330 | break; | |
062a644d VG |
1331 | default: |
1332 | BUG(); | |
1333 | } | |
1334 | ||
1335 | return 0; | |
1336 | } | |
1337 | ||
4bfd482e | 1338 | static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val) |
062a644d VG |
1339 | { |
1340 | struct blkio_group *blkg; | |
1341 | struct hlist_node *n; | |
062a644d VG |
1342 | |
1343 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) | |
1344 | return -EINVAL; | |
1345 | ||
1346 | spin_lock(&blkio_list_lock); | |
1347 | spin_lock_irq(&blkcg->lock); | |
1348 | blkcg->weight = (unsigned int)val; | |
1349 | ||
549d3aa8 | 1350 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
e8989fae | 1351 | struct blkg_policy_data *pd = blkg->pd[plid]; |
549d3aa8 | 1352 | |
e8989fae | 1353 | if (!pd->conf.weight) |
c1768268 | 1354 | blkio_update_group_weight(blkg, plid, blkcg->weight); |
549d3aa8 | 1355 | } |
062a644d | 1356 | |
062a644d VG |
1357 | spin_unlock_irq(&blkcg->lock); |
1358 | spin_unlock(&blkio_list_lock); | |
1359 | return 0; | |
1360 | } | |
1361 | ||
1362 | static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) { | |
1363 | struct blkio_cgroup *blkcg; | |
1364 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1365 | int name = BLKIOFILE_ATTR(cft->private); | |
1366 | ||
1367 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1368 | ||
1369 | switch(plid) { | |
1370 | case BLKIO_POLICY_PROP: | |
1371 | switch(name) { | |
1372 | case BLKIO_PROP_weight: | |
1373 | return (u64)blkcg->weight; | |
1374 | } | |
1375 | break; | |
1376 | default: | |
1377 | BUG(); | |
1378 | } | |
1379 | return 0; | |
1380 | } | |
1381 | ||
1382 | static int | |
1383 | blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val) | |
1384 | { | |
1385 | struct blkio_cgroup *blkcg; | |
1386 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1387 | int name = BLKIOFILE_ATTR(cft->private); | |
1388 | ||
1389 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1390 | ||
1391 | switch(plid) { | |
1392 | case BLKIO_POLICY_PROP: | |
1393 | switch(name) { | |
1394 | case BLKIO_PROP_weight: | |
4bfd482e | 1395 | return blkio_weight_write(blkcg, plid, val); |
062a644d VG |
1396 | } |
1397 | break; | |
1398 | default: | |
1399 | BUG(); | |
1400 | } | |
34d0f179 | 1401 | |
34d0f179 GJ |
1402 | return 0; |
1403 | } | |
1404 | ||
31e4c28d | 1405 | struct cftype blkio_files[] = { |
34d0f179 GJ |
1406 | { |
1407 | .name = "weight_device", | |
062a644d VG |
1408 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1409 | BLKIO_PROP_weight_device), | |
1410 | .read_seq_string = blkiocg_file_read, | |
1411 | .write_string = blkiocg_file_write, | |
34d0f179 GJ |
1412 | .max_write_len = 256, |
1413 | }, | |
31e4c28d VG |
1414 | { |
1415 | .name = "weight", | |
062a644d VG |
1416 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1417 | BLKIO_PROP_weight), | |
1418 | .read_u64 = blkiocg_file_read_u64, | |
1419 | .write_u64 = blkiocg_file_write_u64, | |
31e4c28d | 1420 | }, |
22084190 VG |
1421 | { |
1422 | .name = "time", | |
062a644d VG |
1423 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1424 | BLKIO_PROP_time), | |
1425 | .read_map = blkiocg_file_read_map, | |
22084190 VG |
1426 | }, |
1427 | { | |
1428 | .name = "sectors", | |
062a644d VG |
1429 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1430 | BLKIO_PROP_sectors), | |
1431 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1432 | }, |
1433 | { | |
1434 | .name = "io_service_bytes", | |
062a644d VG |
1435 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1436 | BLKIO_PROP_io_service_bytes), | |
1437 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1438 | }, |
1439 | { | |
1440 | .name = "io_serviced", | |
062a644d VG |
1441 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1442 | BLKIO_PROP_io_serviced), | |
1443 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1444 | }, |
1445 | { | |
1446 | .name = "io_service_time", | |
062a644d VG |
1447 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1448 | BLKIO_PROP_io_service_time), | |
1449 | .read_map = blkiocg_file_read_map, | |
303a3acb DS |
1450 | }, |
1451 | { | |
1452 | .name = "io_wait_time", | |
062a644d VG |
1453 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1454 | BLKIO_PROP_io_wait_time), | |
1455 | .read_map = blkiocg_file_read_map, | |
84c124da | 1456 | }, |
812d4026 DS |
1457 | { |
1458 | .name = "io_merged", | |
062a644d VG |
1459 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1460 | BLKIO_PROP_io_merged), | |
1461 | .read_map = blkiocg_file_read_map, | |
812d4026 | 1462 | }, |
cdc1184c DS |
1463 | { |
1464 | .name = "io_queued", | |
062a644d VG |
1465 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1466 | BLKIO_PROP_io_queued), | |
1467 | .read_map = blkiocg_file_read_map, | |
cdc1184c | 1468 | }, |
84c124da DS |
1469 | { |
1470 | .name = "reset_stats", | |
1471 | .write_u64 = blkiocg_reset_stats, | |
22084190 | 1472 | }, |
13f98250 VG |
1473 | #ifdef CONFIG_BLK_DEV_THROTTLING |
1474 | { | |
1475 | .name = "throttle.read_bps_device", | |
1476 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1477 | BLKIO_THROTL_read_bps_device), | |
1478 | .read_seq_string = blkiocg_file_read, | |
1479 | .write_string = blkiocg_file_write, | |
1480 | .max_write_len = 256, | |
1481 | }, | |
1482 | ||
1483 | { | |
1484 | .name = "throttle.write_bps_device", | |
1485 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1486 | BLKIO_THROTL_write_bps_device), | |
1487 | .read_seq_string = blkiocg_file_read, | |
1488 | .write_string = blkiocg_file_write, | |
1489 | .max_write_len = 256, | |
1490 | }, | |
1491 | ||
1492 | { | |
1493 | .name = "throttle.read_iops_device", | |
1494 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1495 | BLKIO_THROTL_read_iops_device), | |
1496 | .read_seq_string = blkiocg_file_read, | |
1497 | .write_string = blkiocg_file_write, | |
1498 | .max_write_len = 256, | |
1499 | }, | |
1500 | ||
1501 | { | |
1502 | .name = "throttle.write_iops_device", | |
1503 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1504 | BLKIO_THROTL_write_iops_device), | |
1505 | .read_seq_string = blkiocg_file_read, | |
1506 | .write_string = blkiocg_file_write, | |
1507 | .max_write_len = 256, | |
1508 | }, | |
1509 | { | |
1510 | .name = "throttle.io_service_bytes", | |
1511 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1512 | BLKIO_THROTL_io_service_bytes), | |
1513 | .read_map = blkiocg_file_read_map, | |
1514 | }, | |
1515 | { | |
1516 | .name = "throttle.io_serviced", | |
1517 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1518 | BLKIO_THROTL_io_serviced), | |
1519 | .read_map = blkiocg_file_read_map, | |
1520 | }, | |
1521 | #endif /* CONFIG_BLK_DEV_THROTTLING */ | |
1522 | ||
22084190 | 1523 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
cdc1184c DS |
1524 | { |
1525 | .name = "avg_queue_size", | |
062a644d VG |
1526 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1527 | BLKIO_PROP_avg_queue_size), | |
1528 | .read_map = blkiocg_file_read_map, | |
cdc1184c | 1529 | }, |
812df48d DS |
1530 | { |
1531 | .name = "group_wait_time", | |
062a644d VG |
1532 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1533 | BLKIO_PROP_group_wait_time), | |
1534 | .read_map = blkiocg_file_read_map, | |
812df48d DS |
1535 | }, |
1536 | { | |
1537 | .name = "idle_time", | |
062a644d VG |
1538 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1539 | BLKIO_PROP_idle_time), | |
1540 | .read_map = blkiocg_file_read_map, | |
812df48d DS |
1541 | }, |
1542 | { | |
1543 | .name = "empty_time", | |
062a644d VG |
1544 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1545 | BLKIO_PROP_empty_time), | |
1546 | .read_map = blkiocg_file_read_map, | |
812df48d | 1547 | }, |
cdc1184c | 1548 | { |
22084190 | 1549 | .name = "dequeue", |
062a644d VG |
1550 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1551 | BLKIO_PROP_dequeue), | |
1552 | .read_map = blkiocg_file_read_map, | |
cdc1184c | 1553 | }, |
9026e521 JT |
1554 | { |
1555 | .name = "unaccounted_time", | |
1556 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, | |
1557 | BLKIO_PROP_unaccounted_time), | |
1558 | .read_map = blkiocg_file_read_map, | |
1559 | }, | |
22084190 | 1560 | #endif |
31e4c28d VG |
1561 | }; |
1562 | ||
1563 | static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup) | |
1564 | { | |
1565 | return cgroup_add_files(cgroup, subsys, blkio_files, | |
1566 | ARRAY_SIZE(blkio_files)); | |
1567 | } | |
1568 | ||
9f13ef67 TH |
1569 | /** |
1570 | * blkiocg_pre_destroy - cgroup pre_destroy callback | |
1571 | * @subsys: cgroup subsys | |
1572 | * @cgroup: cgroup of interest | |
1573 | * | |
1574 | * This function is called when @cgroup is about to go away and responsible | |
1575 | * for shooting down all blkgs associated with @cgroup. blkgs should be | |
1576 | * removed while holding both q and blkcg locks. As blkcg lock is nested | |
1577 | * inside q lock, this function performs reverse double lock dancing. | |
1578 | * | |
1579 | * This is the blkcg counterpart of ioc_release_fn(). | |
1580 | */ | |
7ee9c562 TH |
1581 | static int blkiocg_pre_destroy(struct cgroup_subsys *subsys, |
1582 | struct cgroup *cgroup) | |
31e4c28d VG |
1583 | { |
1584 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
b1c35769 VG |
1585 | |
1586 | rcu_read_lock(); | |
9f13ef67 | 1587 | spin_lock_irq(&blkcg->lock); |
7ee9c562 | 1588 | |
9f13ef67 TH |
1589 | while (!hlist_empty(&blkcg->blkg_list)) { |
1590 | struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first, | |
1591 | struct blkio_group, blkcg_node); | |
1592 | struct request_queue *q = rcu_dereference(blkg->q); | |
b1c35769 | 1593 | |
9f13ef67 TH |
1594 | if (spin_trylock(q->queue_lock)) { |
1595 | blkg_destroy(blkg); | |
1596 | spin_unlock(q->queue_lock); | |
1597 | } else { | |
1598 | spin_unlock_irq(&blkcg->lock); | |
1599 | rcu_read_unlock(); | |
1600 | cpu_relax(); | |
1601 | rcu_read_lock(); | |
1602 | spin_lock(&blkcg->lock); | |
0f3942a3 | 1603 | } |
9f13ef67 | 1604 | } |
b1c35769 | 1605 | |
9f13ef67 | 1606 | spin_unlock_irq(&blkcg->lock); |
b1c35769 | 1607 | rcu_read_unlock(); |
7ee9c562 TH |
1608 | return 0; |
1609 | } | |
1610 | ||
1611 | static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup) | |
1612 | { | |
1613 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
1614 | ||
67523c48 BB |
1615 | if (blkcg != &blkio_root_cgroup) |
1616 | kfree(blkcg); | |
31e4c28d VG |
1617 | } |
1618 | ||
1619 | static struct cgroup_subsys_state * | |
1620 | blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup) | |
1621 | { | |
0341509f LZ |
1622 | struct blkio_cgroup *blkcg; |
1623 | struct cgroup *parent = cgroup->parent; | |
31e4c28d | 1624 | |
0341509f | 1625 | if (!parent) { |
31e4c28d VG |
1626 | blkcg = &blkio_root_cgroup; |
1627 | goto done; | |
1628 | } | |
1629 | ||
31e4c28d VG |
1630 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
1631 | if (!blkcg) | |
1632 | return ERR_PTR(-ENOMEM); | |
1633 | ||
1634 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; | |
1635 | done: | |
1636 | spin_lock_init(&blkcg->lock); | |
1637 | INIT_HLIST_HEAD(&blkcg->blkg_list); | |
1638 | ||
1639 | return &blkcg->css; | |
1640 | } | |
1641 | ||
5efd6113 TH |
1642 | /** |
1643 | * blkcg_init_queue - initialize blkcg part of request queue | |
1644 | * @q: request_queue to initialize | |
1645 | * | |
1646 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
1647 | * part of new request_queue @q. | |
1648 | * | |
1649 | * RETURNS: | |
1650 | * 0 on success, -errno on failure. | |
1651 | */ | |
1652 | int blkcg_init_queue(struct request_queue *q) | |
1653 | { | |
923adde1 TH |
1654 | int ret; |
1655 | ||
5efd6113 TH |
1656 | might_sleep(); |
1657 | ||
923adde1 TH |
1658 | ret = blk_throtl_init(q); |
1659 | if (ret) | |
1660 | return ret; | |
1661 | ||
1662 | mutex_lock(&all_q_mutex); | |
1663 | INIT_LIST_HEAD(&q->all_q_node); | |
1664 | list_add_tail(&q->all_q_node, &all_q_list); | |
1665 | mutex_unlock(&all_q_mutex); | |
1666 | ||
1667 | return 0; | |
5efd6113 TH |
1668 | } |
1669 | ||
1670 | /** | |
1671 | * blkcg_drain_queue - drain blkcg part of request_queue | |
1672 | * @q: request_queue to drain | |
1673 | * | |
1674 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
1675 | */ | |
1676 | void blkcg_drain_queue(struct request_queue *q) | |
1677 | { | |
1678 | lockdep_assert_held(q->queue_lock); | |
1679 | ||
1680 | blk_throtl_drain(q); | |
1681 | } | |
1682 | ||
1683 | /** | |
1684 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
1685 | * @q: request_queue being released | |
1686 | * | |
1687 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
1688 | */ | |
1689 | void blkcg_exit_queue(struct request_queue *q) | |
1690 | { | |
923adde1 TH |
1691 | mutex_lock(&all_q_mutex); |
1692 | list_del_init(&q->all_q_node); | |
1693 | mutex_unlock(&all_q_mutex); | |
1694 | ||
e8989fae TH |
1695 | blkg_destroy_all(q, true); |
1696 | ||
5efd6113 TH |
1697 | blk_throtl_exit(q); |
1698 | } | |
1699 | ||
31e4c28d VG |
1700 | /* |
1701 | * We cannot support shared io contexts, as we have no mean to support | |
1702 | * two tasks with the same ioc in two different groups without major rework | |
1703 | * of the main cic data structures. For now we allow a task to change | |
1704 | * its cgroup only if it's the only owner of its ioc. | |
1705 | */ | |
bb9d97b6 TH |
1706 | static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
1707 | struct cgroup_taskset *tset) | |
31e4c28d | 1708 | { |
bb9d97b6 | 1709 | struct task_struct *task; |
31e4c28d VG |
1710 | struct io_context *ioc; |
1711 | int ret = 0; | |
1712 | ||
1713 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
bb9d97b6 TH |
1714 | cgroup_taskset_for_each(task, cgrp, tset) { |
1715 | task_lock(task); | |
1716 | ioc = task->io_context; | |
1717 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
1718 | ret = -EINVAL; | |
1719 | task_unlock(task); | |
1720 | if (ret) | |
1721 | break; | |
1722 | } | |
31e4c28d VG |
1723 | return ret; |
1724 | } | |
1725 | ||
bb9d97b6 TH |
1726 | static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
1727 | struct cgroup_taskset *tset) | |
31e4c28d | 1728 | { |
bb9d97b6 | 1729 | struct task_struct *task; |
31e4c28d VG |
1730 | struct io_context *ioc; |
1731 | ||
bb9d97b6 | 1732 | cgroup_taskset_for_each(task, cgrp, tset) { |
b3c9dd18 LT |
1733 | /* we don't lose anything even if ioc allocation fails */ |
1734 | ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); | |
1735 | if (ioc) { | |
1736 | ioc_cgroup_changed(ioc); | |
11a3122f | 1737 | put_io_context(ioc); |
b3c9dd18 | 1738 | } |
bb9d97b6 | 1739 | } |
31e4c28d VG |
1740 | } |
1741 | ||
923adde1 TH |
1742 | static void blkcg_bypass_start(void) |
1743 | __acquires(&all_q_mutex) | |
1744 | { | |
1745 | struct request_queue *q; | |
1746 | ||
1747 | mutex_lock(&all_q_mutex); | |
1748 | ||
1749 | list_for_each_entry(q, &all_q_list, all_q_node) { | |
1750 | blk_queue_bypass_start(q); | |
e8989fae | 1751 | blkg_destroy_all(q, false); |
923adde1 TH |
1752 | } |
1753 | } | |
1754 | ||
1755 | static void blkcg_bypass_end(void) | |
1756 | __releases(&all_q_mutex) | |
1757 | { | |
1758 | struct request_queue *q; | |
1759 | ||
1760 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1761 | blk_queue_bypass_end(q); | |
1762 | ||
1763 | mutex_unlock(&all_q_mutex); | |
1764 | } | |
1765 | ||
3e252066 VG |
1766 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
1767 | { | |
e8989fae TH |
1768 | struct request_queue *q; |
1769 | ||
923adde1 | 1770 | blkcg_bypass_start(); |
3e252066 | 1771 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1772 | |
1773 | BUG_ON(blkio_policy[blkiop->plid]); | |
1774 | blkio_policy[blkiop->plid] = blkiop; | |
3e252066 | 1775 | list_add_tail(&blkiop->list, &blkio_list); |
035d10b2 | 1776 | |
3e252066 | 1777 | spin_unlock(&blkio_list_lock); |
e8989fae TH |
1778 | list_for_each_entry(q, &all_q_list, all_q_node) |
1779 | update_root_blkg_pd(q, blkiop->plid); | |
923adde1 | 1780 | blkcg_bypass_end(); |
3e252066 VG |
1781 | } |
1782 | EXPORT_SYMBOL_GPL(blkio_policy_register); | |
1783 | ||
1784 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) | |
1785 | { | |
e8989fae TH |
1786 | struct request_queue *q; |
1787 | ||
923adde1 | 1788 | blkcg_bypass_start(); |
3e252066 | 1789 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1790 | |
1791 | BUG_ON(blkio_policy[blkiop->plid] != blkiop); | |
1792 | blkio_policy[blkiop->plid] = NULL; | |
3e252066 | 1793 | list_del_init(&blkiop->list); |
035d10b2 | 1794 | |
3e252066 | 1795 | spin_unlock(&blkio_list_lock); |
e8989fae TH |
1796 | list_for_each_entry(q, &all_q_list, all_q_node) |
1797 | update_root_blkg_pd(q, blkiop->plid); | |
923adde1 | 1798 | blkcg_bypass_end(); |
3e252066 VG |
1799 | } |
1800 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |