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