]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-cgroup.c
blkio: Core implementation of throttle policy
[mirror_ubuntu-bionic-kernel.git] / block / blk-cgroup.c
CommitLineData
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>
31e4c28d 20#include "blk-cgroup.h"
34d0f179 21#include <linux/genhd.h>
3e252066 22
84c124da
DS
23#define MAX_KEY_LEN 100
24
3e252066
VG
25static DEFINE_SPINLOCK(blkio_list_lock);
26static LIST_HEAD(blkio_list);
b1c35769 27
31e4c28d 28struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
9d6a986c
VG
29EXPORT_SYMBOL_GPL(blkio_root_cgroup);
30
67523c48
BB
31static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
32 struct cgroup *);
33static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
34 struct task_struct *, bool);
35static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
36 struct cgroup *, struct task_struct *, bool);
37static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
38static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
39
062a644d
VG
40/* for encoding cft->private value on file */
41#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
42/* What policy owns the file, proportional or throttle */
43#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
44#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
45
67523c48
BB
46struct cgroup_subsys blkio_subsys = {
47 .name = "blkio",
48 .create = blkiocg_create,
49 .can_attach = blkiocg_can_attach,
50 .attach = blkiocg_attach,
51 .destroy = blkiocg_destroy,
52 .populate = blkiocg_populate,
53#ifdef CONFIG_BLK_CGROUP
54 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
55 .subsys_id = blkio_subsys_id,
56#endif
57 .use_id = 1,
58 .module = THIS_MODULE,
59};
60EXPORT_SYMBOL_GPL(blkio_subsys);
61
34d0f179
GJ
62static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
63 struct blkio_policy_node *pn)
64{
65 list_add(&pn->node, &blkcg->policy_list);
66}
67
062a644d
VG
68static inline bool cftype_blkg_same_policy(struct cftype *cft,
69 struct blkio_group *blkg)
70{
71 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
72
73 if (blkg->plid == plid)
74 return 1;
75
76 return 0;
77}
78
79/* Determines if policy node matches cgroup file being accessed */
80static inline bool pn_matches_cftype(struct cftype *cft,
81 struct blkio_policy_node *pn)
82{
83 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
84 int fileid = BLKIOFILE_ATTR(cft->private);
85
86 return (plid == pn->plid && fileid == pn->fileid);
87}
88
34d0f179
GJ
89/* Must be called with blkcg->lock held */
90static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
91{
92 list_del(&pn->node);
93}
94
95/* Must be called with blkcg->lock held */
96static struct blkio_policy_node *
062a644d
VG
97blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
98 enum blkio_policy_id plid, int fileid)
34d0f179
GJ
99{
100 struct blkio_policy_node *pn;
101
102 list_for_each_entry(pn, &blkcg->policy_list, node) {
062a644d 103 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
34d0f179
GJ
104 return pn;
105 }
106
107 return NULL;
108}
109
31e4c28d
VG
110struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
111{
112 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
113 struct blkio_cgroup, css);
114}
9d6a986c 115EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
31e4c28d 116
062a644d
VG
117static inline void
118blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
119{
120 struct blkio_policy_type *blkiop;
121
122 list_for_each_entry(blkiop, &blkio_list, list) {
123 /* If this policy does not own the blkg, do not send updates */
124 if (blkiop->plid != blkg->plid)
125 continue;
126 if (blkiop->ops.blkio_update_group_weight_fn)
127 blkiop->ops.blkio_update_group_weight_fn(blkg, weight);
128 }
129}
130
4c9eefa1
VG
131static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
132 int fileid)
133{
134 struct blkio_policy_type *blkiop;
135
136 list_for_each_entry(blkiop, &blkio_list, list) {
137
138 /* If this policy does not own the blkg, do not send updates */
139 if (blkiop->plid != blkg->plid)
140 continue;
141
142 if (fileid == BLKIO_THROTL_read_bps_device
143 && blkiop->ops.blkio_update_group_read_bps_fn)
144 blkiop->ops.blkio_update_group_read_bps_fn(blkg, bps);
145
146 if (fileid == BLKIO_THROTL_write_bps_device
147 && blkiop->ops.blkio_update_group_write_bps_fn)
148 blkiop->ops.blkio_update_group_write_bps_fn(blkg, bps);
149 }
150}
151
9195291e
DS
152/*
153 * Add to the appropriate stat variable depending on the request type.
154 * This should be called with the blkg->stats_lock held.
155 */
84c124da
DS
156static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
157 bool sync)
9195291e 158{
84c124da
DS
159 if (direction)
160 stat[BLKIO_STAT_WRITE] += add;
9195291e 161 else
84c124da
DS
162 stat[BLKIO_STAT_READ] += add;
163 if (sync)
164 stat[BLKIO_STAT_SYNC] += add;
9195291e 165 else
84c124da 166 stat[BLKIO_STAT_ASYNC] += add;
9195291e
DS
167}
168
cdc1184c
DS
169/*
170 * Decrements the appropriate stat variable if non-zero depending on the
171 * request type. Panics on value being zero.
172 * This should be called with the blkg->stats_lock held.
173 */
174static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
175{
176 if (direction) {
177 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
178 stat[BLKIO_STAT_WRITE]--;
179 } else {
180 BUG_ON(stat[BLKIO_STAT_READ] == 0);
181 stat[BLKIO_STAT_READ]--;
182 }
183 if (sync) {
184 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
185 stat[BLKIO_STAT_SYNC]--;
186 } else {
187 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
188 stat[BLKIO_STAT_ASYNC]--;
189 }
190}
191
192#ifdef CONFIG_DEBUG_BLK_CGROUP
812df48d
DS
193/* This should be called with the blkg->stats_lock held. */
194static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
195 struct blkio_group *curr_blkg)
196{
197 if (blkio_blkg_waiting(&blkg->stats))
198 return;
199 if (blkg == curr_blkg)
200 return;
201 blkg->stats.start_group_wait_time = sched_clock();
202 blkio_mark_blkg_waiting(&blkg->stats);
203}
204
205/* This should be called with the blkg->stats_lock held. */
206static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
207{
208 unsigned long long now;
209
210 if (!blkio_blkg_waiting(stats))
211 return;
212
213 now = sched_clock();
214 if (time_after64(now, stats->start_group_wait_time))
215 stats->group_wait_time += now - stats->start_group_wait_time;
216 blkio_clear_blkg_waiting(stats);
217}
218
219/* This should be called with the blkg->stats_lock held. */
220static void blkio_end_empty_time(struct blkio_group_stats *stats)
221{
222 unsigned long long now;
223
224 if (!blkio_blkg_empty(stats))
225 return;
226
227 now = sched_clock();
228 if (time_after64(now, stats->start_empty_time))
229 stats->empty_time += now - stats->start_empty_time;
230 blkio_clear_blkg_empty(stats);
231}
232
233void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&blkg->stats_lock, flags);
238 BUG_ON(blkio_blkg_idling(&blkg->stats));
239 blkg->stats.start_idle_time = sched_clock();
240 blkio_mark_blkg_idling(&blkg->stats);
241 spin_unlock_irqrestore(&blkg->stats_lock, flags);
242}
243EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
244
245void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
246{
247 unsigned long flags;
248 unsigned long long now;
249 struct blkio_group_stats *stats;
250
251 spin_lock_irqsave(&blkg->stats_lock, flags);
252 stats = &blkg->stats;
253 if (blkio_blkg_idling(stats)) {
254 now = sched_clock();
255 if (time_after64(now, stats->start_idle_time))
256 stats->idle_time += now - stats->start_idle_time;
257 blkio_clear_blkg_idling(stats);
258 }
259 spin_unlock_irqrestore(&blkg->stats_lock, flags);
260}
261EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
262
a11cdaa7 263void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
cdc1184c
DS
264{
265 unsigned long flags;
266 struct blkio_group_stats *stats;
267
268 spin_lock_irqsave(&blkg->stats_lock, flags);
269 stats = &blkg->stats;
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
277EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
278
e5ff082e 279void blkiocg_set_start_empty_time(struct blkio_group *blkg)
28baf442
DS
280{
281 unsigned long flags;
282 struct blkio_group_stats *stats;
283
284 spin_lock_irqsave(&blkg->stats_lock, flags);
285 stats = &blkg->stats;
286
287 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
288 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
289 spin_unlock_irqrestore(&blkg->stats_lock, flags);
290 return;
291 }
292
293 /*
e5ff082e
VG
294 * group is already marked empty. This can happen if cfqq got new
295 * request in parent group and moved to this group while being added
296 * to service tree. Just ignore the event and move on.
28baf442 297 */
e5ff082e
VG
298 if(blkio_blkg_empty(stats)) {
299 spin_unlock_irqrestore(&blkg->stats_lock, flags);
300 return;
301 }
302
28baf442
DS
303 stats->start_empty_time = sched_clock();
304 blkio_mark_blkg_empty(stats);
305 spin_unlock_irqrestore(&blkg->stats_lock, flags);
306}
307EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
308
a11cdaa7
DS
309void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
310 unsigned long dequeue)
311{
312 blkg->stats.dequeue += dequeue;
313}
314EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
812df48d
DS
315#else
316static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
317 struct blkio_group *curr_blkg) {}
318static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
cdc1184c
DS
319#endif
320
a11cdaa7 321void blkiocg_update_io_add_stats(struct blkio_group *blkg,
cdc1184c
DS
322 struct blkio_group *curr_blkg, bool direction,
323 bool sync)
324{
325 unsigned long flags;
326
327 spin_lock_irqsave(&blkg->stats_lock, flags);
328 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
329 sync);
812df48d
DS
330 blkio_end_empty_time(&blkg->stats);
331 blkio_set_start_group_wait_time(blkg, curr_blkg);
cdc1184c
DS
332 spin_unlock_irqrestore(&blkg->stats_lock, flags);
333}
a11cdaa7 334EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
cdc1184c 335
a11cdaa7 336void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
cdc1184c
DS
337 bool direction, bool sync)
338{
339 unsigned long flags;
340
341 spin_lock_irqsave(&blkg->stats_lock, flags);
342 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
343 direction, sync);
344 spin_unlock_irqrestore(&blkg->stats_lock, flags);
345}
a11cdaa7 346EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
cdc1184c 347
303a3acb 348void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
22084190 349{
303a3acb
DS
350 unsigned long flags;
351
352 spin_lock_irqsave(&blkg->stats_lock, flags);
353 blkg->stats.time += time;
354 spin_unlock_irqrestore(&blkg->stats_lock, flags);
22084190 355}
303a3acb 356EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
22084190 357
84c124da
DS
358void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
359 uint64_t bytes, bool direction, bool sync)
9195291e
DS
360{
361 struct blkio_group_stats *stats;
362 unsigned long flags;
363
364 spin_lock_irqsave(&blkg->stats_lock, flags);
365 stats = &blkg->stats;
84c124da
DS
366 stats->sectors += bytes >> 9;
367 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
368 sync);
369 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
370 direction, sync);
9195291e
DS
371 spin_unlock_irqrestore(&blkg->stats_lock, flags);
372}
84c124da 373EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
9195291e 374
84c124da
DS
375void blkiocg_update_completion_stats(struct blkio_group *blkg,
376 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
9195291e
DS
377{
378 struct blkio_group_stats *stats;
379 unsigned long flags;
380 unsigned long long now = sched_clock();
381
382 spin_lock_irqsave(&blkg->stats_lock, flags);
383 stats = &blkg->stats;
84c124da
DS
384 if (time_after64(now, io_start_time))
385 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
386 now - io_start_time, direction, sync);
387 if (time_after64(io_start_time, start_time))
388 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
389 io_start_time - start_time, direction, sync);
9195291e
DS
390 spin_unlock_irqrestore(&blkg->stats_lock, flags);
391}
84c124da 392EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
9195291e 393
812d4026
DS
394void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
395 bool sync)
396{
397 unsigned long flags;
398
399 spin_lock_irqsave(&blkg->stats_lock, flags);
400 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
401 sync);
402 spin_unlock_irqrestore(&blkg->stats_lock, flags);
403}
404EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
405
31e4c28d 406void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
062a644d
VG
407 struct blkio_group *blkg, void *key, dev_t dev,
408 enum blkio_policy_id plid)
31e4c28d
VG
409{
410 unsigned long flags;
411
412 spin_lock_irqsave(&blkcg->lock, flags);
8d2a91f8 413 spin_lock_init(&blkg->stats_lock);
31e4c28d 414 rcu_assign_pointer(blkg->key, key);
b1c35769 415 blkg->blkcg_id = css_id(&blkcg->css);
31e4c28d 416 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
062a644d 417 blkg->plid = plid;
31e4c28d 418 spin_unlock_irqrestore(&blkcg->lock, flags);
2868ef7b
VG
419 /* Need to take css reference ? */
420 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
22084190 421 blkg->dev = dev;
31e4c28d 422}
9d6a986c 423EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
31e4c28d 424
b1c35769
VG
425static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
426{
427 hlist_del_init_rcu(&blkg->blkcg_node);
428 blkg->blkcg_id = 0;
429}
430
431/*
432 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
433 * indicating that blk_group was unhashed by the time we got to it.
434 */
31e4c28d
VG
435int blkiocg_del_blkio_group(struct blkio_group *blkg)
436{
b1c35769
VG
437 struct blkio_cgroup *blkcg;
438 unsigned long flags;
439 struct cgroup_subsys_state *css;
440 int ret = 1;
441
442 rcu_read_lock();
443 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
0f3942a3
JA
444 if (css) {
445 blkcg = container_of(css, struct blkio_cgroup, css);
446 spin_lock_irqsave(&blkcg->lock, flags);
447 if (!hlist_unhashed(&blkg->blkcg_node)) {
448 __blkiocg_del_blkio_group(blkg);
449 ret = 0;
450 }
451 spin_unlock_irqrestore(&blkcg->lock, flags);
b1c35769 452 }
0f3942a3 453
b1c35769
VG
454 rcu_read_unlock();
455 return ret;
31e4c28d 456}
9d6a986c 457EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
31e4c28d
VG
458
459/* called under rcu_read_lock(). */
460struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
461{
462 struct blkio_group *blkg;
463 struct hlist_node *n;
464 void *__key;
465
466 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
467 __key = blkg->key;
468 if (__key == key)
469 return blkg;
470 }
471
472 return NULL;
473}
9d6a986c 474EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
31e4c28d 475
303a3acb 476static int
84c124da 477blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
303a3acb
DS
478{
479 struct blkio_cgroup *blkcg;
480 struct blkio_group *blkg;
812df48d 481 struct blkio_group_stats *stats;
303a3acb 482 struct hlist_node *n;
cdc1184c
DS
483 uint64_t queued[BLKIO_STAT_TOTAL];
484 int i;
812df48d
DS
485#ifdef CONFIG_DEBUG_BLK_CGROUP
486 bool idling, waiting, empty;
487 unsigned long long now = sched_clock();
488#endif
303a3acb
DS
489
490 blkcg = cgroup_to_blkio_cgroup(cgroup);
491 spin_lock_irq(&blkcg->lock);
492 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
493 spin_lock(&blkg->stats_lock);
812df48d
DS
494 stats = &blkg->stats;
495#ifdef CONFIG_DEBUG_BLK_CGROUP
496 idling = blkio_blkg_idling(stats);
497 waiting = blkio_blkg_waiting(stats);
498 empty = blkio_blkg_empty(stats);
499#endif
cdc1184c 500 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
812df48d
DS
501 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
502 memset(stats, 0, sizeof(struct blkio_group_stats));
cdc1184c 503 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
812df48d
DS
504 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
505#ifdef CONFIG_DEBUG_BLK_CGROUP
506 if (idling) {
507 blkio_mark_blkg_idling(stats);
508 stats->start_idle_time = now;
509 }
510 if (waiting) {
511 blkio_mark_blkg_waiting(stats);
512 stats->start_group_wait_time = now;
513 }
514 if (empty) {
515 blkio_mark_blkg_empty(stats);
516 stats->start_empty_time = now;
517 }
518#endif
303a3acb
DS
519 spin_unlock(&blkg->stats_lock);
520 }
521 spin_unlock_irq(&blkcg->lock);
522 return 0;
523}
524
84c124da
DS
525static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
526 int chars_left, bool diskname_only)
303a3acb 527{
84c124da 528 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
303a3acb
DS
529 chars_left -= strlen(str);
530 if (chars_left <= 0) {
531 printk(KERN_WARNING
532 "Possibly incorrect cgroup stat display format");
533 return;
534 }
84c124da
DS
535 if (diskname_only)
536 return;
303a3acb 537 switch (type) {
84c124da 538 case BLKIO_STAT_READ:
303a3acb
DS
539 strlcat(str, " Read", chars_left);
540 break;
84c124da 541 case BLKIO_STAT_WRITE:
303a3acb
DS
542 strlcat(str, " Write", chars_left);
543 break;
84c124da 544 case BLKIO_STAT_SYNC:
303a3acb
DS
545 strlcat(str, " Sync", chars_left);
546 break;
84c124da 547 case BLKIO_STAT_ASYNC:
303a3acb
DS
548 strlcat(str, " Async", chars_left);
549 break;
84c124da 550 case BLKIO_STAT_TOTAL:
303a3acb
DS
551 strlcat(str, " Total", chars_left);
552 break;
553 default:
554 strlcat(str, " Invalid", chars_left);
555 }
556}
557
84c124da
DS
558static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
559 struct cgroup_map_cb *cb, dev_t dev)
560{
561 blkio_get_key_name(0, dev, str, chars_left, true);
562 cb->fill(cb, str, val);
563 return val;
564}
303a3acb 565
84c124da
DS
566/* This should be called with blkg->stats_lock held */
567static uint64_t blkio_get_stat(struct blkio_group *blkg,
568 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
303a3acb
DS
569{
570 uint64_t disk_total;
571 char key_str[MAX_KEY_LEN];
84c124da
DS
572 enum stat_sub_type sub_type;
573
574 if (type == BLKIO_STAT_TIME)
575 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
576 blkg->stats.time, cb, dev);
577 if (type == BLKIO_STAT_SECTORS)
578 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
579 blkg->stats.sectors, cb, dev);
580#ifdef CONFIG_DEBUG_BLK_CGROUP
cdc1184c
DS
581 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
582 uint64_t sum = blkg->stats.avg_queue_size_sum;
583 uint64_t samples = blkg->stats.avg_queue_size_samples;
584 if (samples)
585 do_div(sum, samples);
586 else
587 sum = 0;
588 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
589 }
812df48d
DS
590 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
591 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
592 blkg->stats.group_wait_time, cb, dev);
593 if (type == BLKIO_STAT_IDLE_TIME)
594 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
595 blkg->stats.idle_time, cb, dev);
596 if (type == BLKIO_STAT_EMPTY_TIME)
597 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
598 blkg->stats.empty_time, cb, dev);
84c124da
DS
599 if (type == BLKIO_STAT_DEQUEUE)
600 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
601 blkg->stats.dequeue, cb, dev);
602#endif
303a3acb 603
84c124da
DS
604 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
605 sub_type++) {
606 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
607 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
303a3acb 608 }
84c124da
DS
609 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
610 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
611 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
303a3acb
DS
612 cb->fill(cb, key_str, disk_total);
613 return disk_total;
614}
615
34d0f179
GJ
616static int blkio_check_dev_num(dev_t dev)
617{
618 int part = 0;
619 struct gendisk *disk;
620
621 disk = get_gendisk(dev, &part);
622 if (!disk || part)
623 return -ENODEV;
624
625 return 0;
626}
627
628static int blkio_policy_parse_and_set(char *buf,
062a644d 629 struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
34d0f179
GJ
630{
631 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
632 int ret;
633 unsigned long major, minor, temp;
634 int i = 0;
635 dev_t dev;
4c9eefa1 636 u64 bps;
34d0f179
GJ
637
638 memset(s, 0, sizeof(s));
639
640 while ((p = strsep(&buf, " ")) != NULL) {
641 if (!*p)
642 continue;
643
644 s[i++] = p;
645
646 /* Prevent from inputing too many things */
647 if (i == 3)
648 break;
649 }
650
651 if (i != 2)
652 return -EINVAL;
653
654 p = strsep(&s[0], ":");
655 if (p != NULL)
656 major_s = p;
657 else
658 return -EINVAL;
659
660 minor_s = s[0];
661 if (!minor_s)
662 return -EINVAL;
663
664 ret = strict_strtoul(major_s, 10, &major);
665 if (ret)
666 return -EINVAL;
667
668 ret = strict_strtoul(minor_s, 10, &minor);
669 if (ret)
670 return -EINVAL;
671
672 dev = MKDEV(major, minor);
673
674 ret = blkio_check_dev_num(dev);
675 if (ret)
676 return ret;
677
678 newpn->dev = dev;
679
680 if (s[1] == NULL)
681 return -EINVAL;
682
062a644d
VG
683 switch (plid) {
684 case BLKIO_POLICY_PROP:
685 ret = strict_strtoul(s[1], 10, &temp);
686 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
687 temp > BLKIO_WEIGHT_MAX)
688 return -EINVAL;
34d0f179 689
062a644d
VG
690 newpn->plid = plid;
691 newpn->fileid = fileid;
4c9eefa1
VG
692 newpn->val.weight = temp;
693 break;
694 case BLKIO_POLICY_THROTL:
695 ret = strict_strtoull(s[1], 10, &bps);
696 if (ret)
697 return -EINVAL;
698
699 newpn->plid = plid;
700 newpn->fileid = fileid;
701 newpn->val.bps = bps;
062a644d
VG
702 break;
703 default:
704 BUG();
705 }
34d0f179
GJ
706
707 return 0;
708}
709
710unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
711 dev_t dev)
712{
713 struct blkio_policy_node *pn;
714
062a644d
VG
715 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
716 BLKIO_PROP_weight_device);
34d0f179 717 if (pn)
4c9eefa1 718 return pn->val.weight;
34d0f179
GJ
719 else
720 return blkcg->weight;
721}
722EXPORT_SYMBOL_GPL(blkcg_get_weight);
723
4c9eefa1
VG
724uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
725{
726 struct blkio_policy_node *pn;
727
728 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
729 BLKIO_THROTL_read_bps_device);
730 if (pn)
731 return pn->val.bps;
732 else
733 return -1;
734}
735
736uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
737{
738 struct blkio_policy_node *pn;
739 pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
740 BLKIO_THROTL_write_bps_device);
741 if (pn)
742 return pn->val.bps;
743 else
744 return -1;
745}
746
062a644d
VG
747/* Checks whether user asked for deleting a policy rule */
748static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
749{
750 switch(pn->plid) {
751 case BLKIO_POLICY_PROP:
4c9eefa1
VG
752 if (pn->val.weight == 0)
753 return 1;
754 break;
755 case BLKIO_POLICY_THROTL:
756 if (pn->val.bps == 0)
062a644d
VG
757 return 1;
758 break;
759 default:
760 BUG();
761 }
762
763 return 0;
764}
765
766static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
767 struct blkio_policy_node *newpn)
768{
769 switch(oldpn->plid) {
770 case BLKIO_POLICY_PROP:
4c9eefa1
VG
771 oldpn->val.weight = newpn->val.weight;
772 break;
773 case BLKIO_POLICY_THROTL:
774 oldpn->val.bps = newpn->val.bps;
062a644d
VG
775 break;
776 default:
777 BUG();
778 }
779}
780
781/*
782 * Some rules/values in blkg have changed. Propogate those to respective
783 * policies.
784 */
785static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
786 struct blkio_group *blkg, struct blkio_policy_node *pn)
787{
788 unsigned int weight;
4c9eefa1 789 u64 bps;
062a644d
VG
790
791 switch(pn->plid) {
792 case BLKIO_POLICY_PROP:
4c9eefa1 793 weight = pn->val.weight ? pn->val.weight :
062a644d
VG
794 blkcg->weight;
795 blkio_update_group_weight(blkg, weight);
796 break;
4c9eefa1
VG
797 case BLKIO_POLICY_THROTL:
798 switch(pn->fileid) {
799 case BLKIO_THROTL_read_bps_device:
800 case BLKIO_THROTL_write_bps_device:
801 bps = pn->val.bps ? pn->val.bps : (-1);
802 blkio_update_group_bps(blkg, bps, pn->fileid);
803 break;
804 }
805 break;
062a644d
VG
806 default:
807 BUG();
808 }
809}
810
811/*
812 * A policy node rule has been updated. Propogate this update to all the
813 * block groups which might be affected by this update.
814 */
815static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
816 struct blkio_policy_node *pn)
817{
818 struct blkio_group *blkg;
819 struct hlist_node *n;
820
821 spin_lock(&blkio_list_lock);
822 spin_lock_irq(&blkcg->lock);
34d0f179 823
062a644d
VG
824 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
825 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
826 continue;
827 blkio_update_blkg_policy(blkcg, blkg, pn);
828 }
829
830 spin_unlock_irq(&blkcg->lock);
831 spin_unlock(&blkio_list_lock);
832}
833
834static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
835 const char *buffer)
34d0f179
GJ
836{
837 int ret = 0;
838 char *buf;
839 struct blkio_policy_node *newpn, *pn;
840 struct blkio_cgroup *blkcg;
34d0f179 841 int keep_newpn = 0;
062a644d
VG
842 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
843 int fileid = BLKIOFILE_ATTR(cft->private);
34d0f179
GJ
844
845 buf = kstrdup(buffer, GFP_KERNEL);
846 if (!buf)
847 return -ENOMEM;
848
849 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
850 if (!newpn) {
851 ret = -ENOMEM;
852 goto free_buf;
853 }
854
062a644d 855 ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
34d0f179
GJ
856 if (ret)
857 goto free_newpn;
858
859 blkcg = cgroup_to_blkio_cgroup(cgrp);
860
861 spin_lock_irq(&blkcg->lock);
862
062a644d 863 pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
34d0f179 864 if (!pn) {
062a644d 865 if (!blkio_delete_rule_command(newpn)) {
34d0f179
GJ
866 blkio_policy_insert_node(blkcg, newpn);
867 keep_newpn = 1;
868 }
869 spin_unlock_irq(&blkcg->lock);
870 goto update_io_group;
871 }
872
062a644d 873 if (blkio_delete_rule_command(newpn)) {
34d0f179
GJ
874 blkio_policy_delete_node(pn);
875 spin_unlock_irq(&blkcg->lock);
876 goto update_io_group;
877 }
878 spin_unlock_irq(&blkcg->lock);
879
062a644d 880 blkio_update_policy_rule(pn, newpn);
34d0f179
GJ
881
882update_io_group:
062a644d 883 blkio_update_policy_node_blkg(blkcg, newpn);
34d0f179
GJ
884
885free_newpn:
886 if (!keep_newpn)
887 kfree(newpn);
888free_buf:
889 kfree(buf);
890 return ret;
891}
892
062a644d
VG
893static void
894blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
895{
896 switch(pn->plid) {
897 case BLKIO_POLICY_PROP:
898 if (pn->fileid == BLKIO_PROP_weight_device)
899 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
4c9eefa1
VG
900 MINOR(pn->dev), pn->val.weight);
901 break;
902 case BLKIO_POLICY_THROTL:
903 if (pn->fileid == BLKIO_THROTL_read_bps_device)
904 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
905 MINOR(pn->dev), pn->val.bps);
906 else if (pn->fileid == BLKIO_THROTL_write_bps_device)
907 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
908 MINOR(pn->dev), pn->val.bps);
909 else
910 BUG();
062a644d
VG
911 break;
912 default:
913 BUG();
914 }
915}
916
917/* cgroup files which read their data from policy nodes end up here */
918static void blkio_read_policy_node_files(struct cftype *cft,
919 struct blkio_cgroup *blkcg, struct seq_file *m)
34d0f179 920{
34d0f179
GJ
921 struct blkio_policy_node *pn;
922
0f3942a3
JA
923 if (!list_empty(&blkcg->policy_list)) {
924 spin_lock_irq(&blkcg->lock);
925 list_for_each_entry(pn, &blkcg->policy_list, node) {
062a644d
VG
926 if (!pn_matches_cftype(cft, pn))
927 continue;
928 blkio_print_policy_node(m, pn);
0f3942a3
JA
929 }
930 spin_unlock_irq(&blkcg->lock);
34d0f179 931 }
062a644d
VG
932}
933
934static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
935 struct seq_file *m)
936{
937 struct blkio_cgroup *blkcg;
938 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
939 int name = BLKIOFILE_ATTR(cft->private);
940
941 blkcg = cgroup_to_blkio_cgroup(cgrp);
942
943 switch(plid) {
944 case BLKIO_POLICY_PROP:
945 switch(name) {
946 case BLKIO_PROP_weight_device:
947 blkio_read_policy_node_files(cft, blkcg, m);
948 return 0;
949 default:
950 BUG();
951 }
952 break;
4c9eefa1
VG
953 case BLKIO_POLICY_THROTL:
954 switch(name){
955 case BLKIO_THROTL_read_bps_device:
956 case BLKIO_THROTL_write_bps_device:
957 blkio_read_policy_node_files(cft, blkcg, m);
958 return 0;
959 default:
960 BUG();
961 }
962 break;
062a644d
VG
963 default:
964 BUG();
965 }
966
967 return 0;
968}
969
970static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
971 struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
972 bool show_total)
973{
974 struct blkio_group *blkg;
975 struct hlist_node *n;
976 uint64_t cgroup_total = 0;
977
978 rcu_read_lock();
979 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
980 if (blkg->dev) {
981 if (!cftype_blkg_same_policy(cft, blkg))
982 continue;
983 spin_lock_irq(&blkg->stats_lock);
984 cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
985 type);
986 spin_unlock_irq(&blkg->stats_lock);
987 }
988 }
989 if (show_total)
990 cb->fill(cb, "Total", cgroup_total);
991 rcu_read_unlock();
992 return 0;
993}
994
995/* All map kind of cgroup file get serviced by this function */
996static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
997 struct cgroup_map_cb *cb)
998{
999 struct blkio_cgroup *blkcg;
1000 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1001 int name = BLKIOFILE_ATTR(cft->private);
1002
1003 blkcg = cgroup_to_blkio_cgroup(cgrp);
1004
1005 switch(plid) {
1006 case BLKIO_POLICY_PROP:
1007 switch(name) {
1008 case BLKIO_PROP_time:
1009 return blkio_read_blkg_stats(blkcg, cft, cb,
1010 BLKIO_STAT_TIME, 0);
1011 case BLKIO_PROP_sectors:
1012 return blkio_read_blkg_stats(blkcg, cft, cb,
1013 BLKIO_STAT_SECTORS, 0);
1014 case BLKIO_PROP_io_service_bytes:
1015 return blkio_read_blkg_stats(blkcg, cft, cb,
1016 BLKIO_STAT_SERVICE_BYTES, 1);
1017 case BLKIO_PROP_io_serviced:
1018 return blkio_read_blkg_stats(blkcg, cft, cb,
1019 BLKIO_STAT_SERVICED, 1);
1020 case BLKIO_PROP_io_service_time:
1021 return blkio_read_blkg_stats(blkcg, cft, cb,
1022 BLKIO_STAT_SERVICE_TIME, 1);
1023 case BLKIO_PROP_io_wait_time:
1024 return blkio_read_blkg_stats(blkcg, cft, cb,
1025 BLKIO_STAT_WAIT_TIME, 1);
1026 case BLKIO_PROP_io_merged:
1027 return blkio_read_blkg_stats(blkcg, cft, cb,
1028 BLKIO_STAT_MERGED, 1);
1029 case BLKIO_PROP_io_queued:
1030 return blkio_read_blkg_stats(blkcg, cft, cb,
1031 BLKIO_STAT_QUEUED, 1);
1032#ifdef CONFIG_DEBUG_BLK_CGROUP
1033 case BLKIO_PROP_dequeue:
1034 return blkio_read_blkg_stats(blkcg, cft, cb,
1035 BLKIO_STAT_DEQUEUE, 0);
1036 case BLKIO_PROP_avg_queue_size:
1037 return blkio_read_blkg_stats(blkcg, cft, cb,
1038 BLKIO_STAT_AVG_QUEUE_SIZE, 0);
1039 case BLKIO_PROP_group_wait_time:
1040 return blkio_read_blkg_stats(blkcg, cft, cb,
1041 BLKIO_STAT_GROUP_WAIT_TIME, 0);
1042 case BLKIO_PROP_idle_time:
1043 return blkio_read_blkg_stats(blkcg, cft, cb,
1044 BLKIO_STAT_IDLE_TIME, 0);
1045 case BLKIO_PROP_empty_time:
1046 return blkio_read_blkg_stats(blkcg, cft, cb,
1047 BLKIO_STAT_EMPTY_TIME, 0);
1048#endif
1049 default:
1050 BUG();
1051 }
1052 break;
4c9eefa1
VG
1053 case BLKIO_POLICY_THROTL:
1054 switch(name){
1055 case BLKIO_THROTL_io_service_bytes:
1056 return blkio_read_blkg_stats(blkcg, cft, cb,
1057 BLKIO_STAT_SERVICE_BYTES, 1);
1058 case BLKIO_THROTL_io_serviced:
1059 return blkio_read_blkg_stats(blkcg, cft, cb,
1060 BLKIO_STAT_SERVICED, 1);
1061 default:
1062 BUG();
1063 }
1064 break;
062a644d
VG
1065 default:
1066 BUG();
1067 }
1068
1069 return 0;
1070}
1071
1072static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1073{
1074 struct blkio_group *blkg;
1075 struct hlist_node *n;
1076 struct blkio_policy_node *pn;
1077
1078 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1079 return -EINVAL;
1080
1081 spin_lock(&blkio_list_lock);
1082 spin_lock_irq(&blkcg->lock);
1083 blkcg->weight = (unsigned int)val;
1084
1085 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1086 pn = blkio_policy_search_node(blkcg, blkg->dev,
1087 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1088 if (pn)
1089 continue;
1090
1091 blkio_update_group_weight(blkg, blkcg->weight);
1092 }
1093 spin_unlock_irq(&blkcg->lock);
1094 spin_unlock(&blkio_list_lock);
1095 return 0;
1096}
1097
1098static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1099 struct blkio_cgroup *blkcg;
1100 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1101 int name = BLKIOFILE_ATTR(cft->private);
1102
1103 blkcg = cgroup_to_blkio_cgroup(cgrp);
1104
1105 switch(plid) {
1106 case BLKIO_POLICY_PROP:
1107 switch(name) {
1108 case BLKIO_PROP_weight:
1109 return (u64)blkcg->weight;
1110 }
1111 break;
1112 default:
1113 BUG();
1114 }
1115 return 0;
1116}
1117
1118static int
1119blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1120{
1121 struct blkio_cgroup *blkcg;
1122 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1123 int name = BLKIOFILE_ATTR(cft->private);
1124
1125 blkcg = cgroup_to_blkio_cgroup(cgrp);
1126
1127 switch(plid) {
1128 case BLKIO_POLICY_PROP:
1129 switch(name) {
1130 case BLKIO_PROP_weight:
1131 return blkio_weight_write(blkcg, val);
1132 }
1133 break;
1134 default:
1135 BUG();
1136 }
34d0f179 1137
34d0f179
GJ
1138 return 0;
1139}
1140
31e4c28d 1141struct cftype blkio_files[] = {
34d0f179
GJ
1142 {
1143 .name = "weight_device",
062a644d
VG
1144 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1145 BLKIO_PROP_weight_device),
1146 .read_seq_string = blkiocg_file_read,
1147 .write_string = blkiocg_file_write,
34d0f179
GJ
1148 .max_write_len = 256,
1149 },
31e4c28d
VG
1150 {
1151 .name = "weight",
062a644d
VG
1152 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1153 BLKIO_PROP_weight),
1154 .read_u64 = blkiocg_file_read_u64,
1155 .write_u64 = blkiocg_file_write_u64,
31e4c28d 1156 },
4c9eefa1
VG
1157 {
1158 .name = "throttle.read_bps_device",
1159 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1160 BLKIO_THROTL_read_bps_device),
1161 .read_seq_string = blkiocg_file_read,
1162 .write_string = blkiocg_file_write,
1163 .max_write_len = 256,
1164 },
1165
1166 {
1167 .name = "throttle.write_bps_device",
1168 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1169 BLKIO_THROTL_write_bps_device),
1170 .read_seq_string = blkiocg_file_read,
1171 .write_string = blkiocg_file_write,
1172 .max_write_len = 256,
1173 },
22084190
VG
1174 {
1175 .name = "time",
062a644d
VG
1176 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1177 BLKIO_PROP_time),
1178 .read_map = blkiocg_file_read_map,
22084190
VG
1179 },
1180 {
1181 .name = "sectors",
062a644d
VG
1182 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1183 BLKIO_PROP_sectors),
1184 .read_map = blkiocg_file_read_map,
303a3acb
DS
1185 },
1186 {
1187 .name = "io_service_bytes",
062a644d
VG
1188 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1189 BLKIO_PROP_io_service_bytes),
1190 .read_map = blkiocg_file_read_map,
303a3acb 1191 },
4c9eefa1
VG
1192 {
1193 .name = "throttle.io_service_bytes",
1194 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1195 BLKIO_THROTL_io_service_bytes),
1196 .read_map = blkiocg_file_read_map,
1197 },
303a3acb
DS
1198 {
1199 .name = "io_serviced",
062a644d
VG
1200 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1201 BLKIO_PROP_io_serviced),
1202 .read_map = blkiocg_file_read_map,
303a3acb 1203 },
4c9eefa1
VG
1204 {
1205 .name = "throttle.io_serviced",
1206 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1207 BLKIO_THROTL_io_serviced),
1208 .read_map = blkiocg_file_read_map,
1209 },
303a3acb
DS
1210 {
1211 .name = "io_service_time",
062a644d
VG
1212 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1213 BLKIO_PROP_io_service_time),
1214 .read_map = blkiocg_file_read_map,
303a3acb
DS
1215 },
1216 {
1217 .name = "io_wait_time",
062a644d
VG
1218 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1219 BLKIO_PROP_io_wait_time),
1220 .read_map = blkiocg_file_read_map,
84c124da 1221 },
812d4026
DS
1222 {
1223 .name = "io_merged",
062a644d
VG
1224 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1225 BLKIO_PROP_io_merged),
1226 .read_map = blkiocg_file_read_map,
812d4026 1227 },
cdc1184c
DS
1228 {
1229 .name = "io_queued",
062a644d
VG
1230 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1231 BLKIO_PROP_io_queued),
1232 .read_map = blkiocg_file_read_map,
cdc1184c 1233 },
84c124da
DS
1234 {
1235 .name = "reset_stats",
1236 .write_u64 = blkiocg_reset_stats,
22084190
VG
1237 },
1238#ifdef CONFIG_DEBUG_BLK_CGROUP
cdc1184c
DS
1239 {
1240 .name = "avg_queue_size",
062a644d
VG
1241 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1242 BLKIO_PROP_avg_queue_size),
1243 .read_map = blkiocg_file_read_map,
cdc1184c 1244 },
812df48d
DS
1245 {
1246 .name = "group_wait_time",
062a644d
VG
1247 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1248 BLKIO_PROP_group_wait_time),
1249 .read_map = blkiocg_file_read_map,
812df48d
DS
1250 },
1251 {
1252 .name = "idle_time",
062a644d
VG
1253 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1254 BLKIO_PROP_idle_time),
1255 .read_map = blkiocg_file_read_map,
812df48d
DS
1256 },
1257 {
1258 .name = "empty_time",
062a644d
VG
1259 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1260 BLKIO_PROP_empty_time),
1261 .read_map = blkiocg_file_read_map,
812df48d 1262 },
cdc1184c 1263 {
22084190 1264 .name = "dequeue",
062a644d
VG
1265 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1266 BLKIO_PROP_dequeue),
1267 .read_map = blkiocg_file_read_map,
cdc1184c 1268 },
22084190 1269#endif
31e4c28d
VG
1270};
1271
1272static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1273{
1274 return cgroup_add_files(cgroup, subsys, blkio_files,
1275 ARRAY_SIZE(blkio_files));
1276}
1277
1278static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1279{
1280 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
b1c35769
VG
1281 unsigned long flags;
1282 struct blkio_group *blkg;
1283 void *key;
3e252066 1284 struct blkio_policy_type *blkiop;
34d0f179 1285 struct blkio_policy_node *pn, *pntmp;
b1c35769
VG
1286
1287 rcu_read_lock();
0f3942a3
JA
1288 do {
1289 spin_lock_irqsave(&blkcg->lock, flags);
b1c35769 1290
0f3942a3
JA
1291 if (hlist_empty(&blkcg->blkg_list)) {
1292 spin_unlock_irqrestore(&blkcg->lock, flags);
1293 break;
1294 }
b1c35769 1295
0f3942a3
JA
1296 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1297 blkcg_node);
1298 key = rcu_dereference(blkg->key);
1299 __blkiocg_del_blkio_group(blkg);
31e4c28d 1300
0f3942a3 1301 spin_unlock_irqrestore(&blkcg->lock, flags);
b1c35769 1302
0f3942a3
JA
1303 /*
1304 * This blkio_group is being unlinked as associated cgroup is
1305 * going away. Let all the IO controlling policies know about
1306 * this event. Currently this is static call to one io
1307 * controlling policy. Once we have more policies in place, we
1308 * need some dynamic registration of callback function.
1309 */
1310 spin_lock(&blkio_list_lock);
1311 list_for_each_entry(blkiop, &blkio_list, list)
1312 blkiop->ops.blkio_unlink_group_fn(key, blkg);
1313 spin_unlock(&blkio_list_lock);
1314 } while (1);
34d0f179 1315
34d0f179
GJ
1316 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1317 blkio_policy_delete_node(pn);
1318 kfree(pn);
1319 }
0f3942a3 1320
31e4c28d 1321 free_css_id(&blkio_subsys, &blkcg->css);
b1c35769 1322 rcu_read_unlock();
67523c48
BB
1323 if (blkcg != &blkio_root_cgroup)
1324 kfree(blkcg);
31e4c28d
VG
1325}
1326
1327static struct cgroup_subsys_state *
1328blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1329{
0341509f
LZ
1330 struct blkio_cgroup *blkcg;
1331 struct cgroup *parent = cgroup->parent;
31e4c28d 1332
0341509f 1333 if (!parent) {
31e4c28d
VG
1334 blkcg = &blkio_root_cgroup;
1335 goto done;
1336 }
1337
1338 /* Currently we do not support hierarchy deeper than two level (0,1) */
0341509f 1339 if (parent != cgroup->top_cgroup)
31e4c28d
VG
1340 return ERR_PTR(-EINVAL);
1341
1342 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1343 if (!blkcg)
1344 return ERR_PTR(-ENOMEM);
1345
1346 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1347done:
1348 spin_lock_init(&blkcg->lock);
1349 INIT_HLIST_HEAD(&blkcg->blkg_list);
1350
34d0f179 1351 INIT_LIST_HEAD(&blkcg->policy_list);
31e4c28d
VG
1352 return &blkcg->css;
1353}
1354
1355/*
1356 * We cannot support shared io contexts, as we have no mean to support
1357 * two tasks with the same ioc in two different groups without major rework
1358 * of the main cic data structures. For now we allow a task to change
1359 * its cgroup only if it's the only owner of its ioc.
1360 */
1361static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1362 struct cgroup *cgroup, struct task_struct *tsk,
1363 bool threadgroup)
1364{
1365 struct io_context *ioc;
1366 int ret = 0;
1367
1368 /* task_lock() is needed to avoid races with exit_io_context() */
1369 task_lock(tsk);
1370 ioc = tsk->io_context;
1371 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1372 ret = -EINVAL;
1373 task_unlock(tsk);
1374
1375 return ret;
1376}
1377
1378static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1379 struct cgroup *prev, struct task_struct *tsk,
1380 bool threadgroup)
1381{
1382 struct io_context *ioc;
1383
1384 task_lock(tsk);
1385 ioc = tsk->io_context;
1386 if (ioc)
1387 ioc->cgroup_changed = 1;
1388 task_unlock(tsk);
1389}
1390
3e252066
VG
1391void blkio_policy_register(struct blkio_policy_type *blkiop)
1392{
1393 spin_lock(&blkio_list_lock);
1394 list_add_tail(&blkiop->list, &blkio_list);
1395 spin_unlock(&blkio_list_lock);
1396}
1397EXPORT_SYMBOL_GPL(blkio_policy_register);
1398
1399void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1400{
1401 spin_lock(&blkio_list_lock);
1402 list_del_init(&blkiop->list);
1403 spin_unlock(&blkio_list_lock);
1404}
1405EXPORT_SYMBOL_GPL(blkio_policy_unregister);
67523c48
BB
1406
1407static int __init init_cgroup_blkio(void)
1408{
1409 return cgroup_load_subsys(&blkio_subsys);
1410}
1411
1412static void __exit exit_cgroup_blkio(void)
1413{
1414 cgroup_unload_subsys(&blkio_subsys);
1415}
1416
1417module_init(init_cgroup_blkio);
1418module_exit(exit_cgroup_blkio);
1419MODULE_LICENSE("GPL");