]> git.karo-electronics.de Git - karo-tx-linux.git/blob - block/blk-cgroup.c
cgroups: add previous cgroup in can_attach_task/attach_task callbacks
[karo-tx-linux.git] / block / blk-cgroup.c
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>
14 #include <linux/seq_file.h>
15 #include <linux/kdev_t.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include <linux/blkdev.h>
19 #include <linux/slab.h>
20 #include "blk-cgroup.h"
21 #include <linux/genhd.h>
22
23 #define MAX_KEY_LEN 100
24
25 static DEFINE_SPINLOCK(blkio_list_lock);
26 static LIST_HEAD(blkio_list);
27
28 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
29 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
30
31 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
32                                                   struct cgroup *);
33 static int blkiocg_can_attach_task(struct cgroup *, struct cgroup *,
34                                    struct task_struct *);
35 static void blkiocg_attach_task(struct cgroup *, struct cgroup *,
36                                 struct task_struct *);
37 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
38 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
39
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
46 struct cgroup_subsys blkio_subsys = {
47         .name = "blkio",
48         .create = blkiocg_create,
49         .can_attach_task = blkiocg_can_attach_task,
50         .attach_task = blkiocg_attach_task,
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 };
60 EXPORT_SYMBOL_GPL(blkio_subsys);
61
62 static 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
68 static 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 */
80 static 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
89 /* Must be called with blkcg->lock held */
90 static 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 */
96 static struct blkio_policy_node *
97 blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
98                 enum blkio_policy_id plid, int fileid)
99 {
100         struct blkio_policy_node *pn;
101
102         list_for_each_entry(pn, &blkcg->policy_list, node) {
103                 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
104                         return pn;
105         }
106
107         return NULL;
108 }
109
110 struct 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 }
115 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
116
117 struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
118 {
119         return container_of(task_subsys_state(tsk, blkio_subsys_id),
120                             struct blkio_cgroup, css);
121 }
122 EXPORT_SYMBOL_GPL(task_blkio_cgroup);
123
124 static inline void
125 blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
126 {
127         struct blkio_policy_type *blkiop;
128
129         list_for_each_entry(blkiop, &blkio_list, list) {
130                 /* If this policy does not own the blkg, do not send updates */
131                 if (blkiop->plid != blkg->plid)
132                         continue;
133                 if (blkiop->ops.blkio_update_group_weight_fn)
134                         blkiop->ops.blkio_update_group_weight_fn(blkg->key,
135                                                         blkg, weight);
136         }
137 }
138
139 static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
140                                 int fileid)
141 {
142         struct blkio_policy_type *blkiop;
143
144         list_for_each_entry(blkiop, &blkio_list, list) {
145
146                 /* If this policy does not own the blkg, do not send updates */
147                 if (blkiop->plid != blkg->plid)
148                         continue;
149
150                 if (fileid == BLKIO_THROTL_read_bps_device
151                     && blkiop->ops.blkio_update_group_read_bps_fn)
152                         blkiop->ops.blkio_update_group_read_bps_fn(blkg->key,
153                                                                 blkg, bps);
154
155                 if (fileid == BLKIO_THROTL_write_bps_device
156                     && blkiop->ops.blkio_update_group_write_bps_fn)
157                         blkiop->ops.blkio_update_group_write_bps_fn(blkg->key,
158                                                                 blkg, bps);
159         }
160 }
161
162 static inline void blkio_update_group_iops(struct blkio_group *blkg,
163                         unsigned int iops, int fileid)
164 {
165         struct blkio_policy_type *blkiop;
166
167         list_for_each_entry(blkiop, &blkio_list, list) {
168
169                 /* If this policy does not own the blkg, do not send updates */
170                 if (blkiop->plid != blkg->plid)
171                         continue;
172
173                 if (fileid == BLKIO_THROTL_read_iops_device
174                     && blkiop->ops.blkio_update_group_read_iops_fn)
175                         blkiop->ops.blkio_update_group_read_iops_fn(blkg->key,
176                                                                 blkg, iops);
177
178                 if (fileid == BLKIO_THROTL_write_iops_device
179                     && blkiop->ops.blkio_update_group_write_iops_fn)
180                         blkiop->ops.blkio_update_group_write_iops_fn(blkg->key,
181                                                                 blkg,iops);
182         }
183 }
184
185 /*
186  * Add to the appropriate stat variable depending on the request type.
187  * This should be called with the blkg->stats_lock held.
188  */
189 static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
190                                 bool sync)
191 {
192         if (direction)
193                 stat[BLKIO_STAT_WRITE] += add;
194         else
195                 stat[BLKIO_STAT_READ] += add;
196         if (sync)
197                 stat[BLKIO_STAT_SYNC] += add;
198         else
199                 stat[BLKIO_STAT_ASYNC] += add;
200 }
201
202 /*
203  * Decrements the appropriate stat variable if non-zero depending on the
204  * request type. Panics on value being zero.
205  * This should be called with the blkg->stats_lock held.
206  */
207 static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
208 {
209         if (direction) {
210                 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
211                 stat[BLKIO_STAT_WRITE]--;
212         } else {
213                 BUG_ON(stat[BLKIO_STAT_READ] == 0);
214                 stat[BLKIO_STAT_READ]--;
215         }
216         if (sync) {
217                 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
218                 stat[BLKIO_STAT_SYNC]--;
219         } else {
220                 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
221                 stat[BLKIO_STAT_ASYNC]--;
222         }
223 }
224
225 #ifdef CONFIG_DEBUG_BLK_CGROUP
226 /* This should be called with the blkg->stats_lock held. */
227 static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
228                                                 struct blkio_group *curr_blkg)
229 {
230         if (blkio_blkg_waiting(&blkg->stats))
231                 return;
232         if (blkg == curr_blkg)
233                 return;
234         blkg->stats.start_group_wait_time = sched_clock();
235         blkio_mark_blkg_waiting(&blkg->stats);
236 }
237
238 /* This should be called with the blkg->stats_lock held. */
239 static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
240 {
241         unsigned long long now;
242
243         if (!blkio_blkg_waiting(stats))
244                 return;
245
246         now = sched_clock();
247         if (time_after64(now, stats->start_group_wait_time))
248                 stats->group_wait_time += now - stats->start_group_wait_time;
249         blkio_clear_blkg_waiting(stats);
250 }
251
252 /* This should be called with the blkg->stats_lock held. */
253 static void blkio_end_empty_time(struct blkio_group_stats *stats)
254 {
255         unsigned long long now;
256
257         if (!blkio_blkg_empty(stats))
258                 return;
259
260         now = sched_clock();
261         if (time_after64(now, stats->start_empty_time))
262                 stats->empty_time += now - stats->start_empty_time;
263         blkio_clear_blkg_empty(stats);
264 }
265
266 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
267 {
268         unsigned long flags;
269
270         spin_lock_irqsave(&blkg->stats_lock, flags);
271         BUG_ON(blkio_blkg_idling(&blkg->stats));
272         blkg->stats.start_idle_time = sched_clock();
273         blkio_mark_blkg_idling(&blkg->stats);
274         spin_unlock_irqrestore(&blkg->stats_lock, flags);
275 }
276 EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
277
278 void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
279 {
280         unsigned long flags;
281         unsigned long long now;
282         struct blkio_group_stats *stats;
283
284         spin_lock_irqsave(&blkg->stats_lock, flags);
285         stats = &blkg->stats;
286         if (blkio_blkg_idling(stats)) {
287                 now = sched_clock();
288                 if (time_after64(now, stats->start_idle_time))
289                         stats->idle_time += now - stats->start_idle_time;
290                 blkio_clear_blkg_idling(stats);
291         }
292         spin_unlock_irqrestore(&blkg->stats_lock, flags);
293 }
294 EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
295
296 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
297 {
298         unsigned long flags;
299         struct blkio_group_stats *stats;
300
301         spin_lock_irqsave(&blkg->stats_lock, flags);
302         stats = &blkg->stats;
303         stats->avg_queue_size_sum +=
304                         stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
305                         stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
306         stats->avg_queue_size_samples++;
307         blkio_update_group_wait_time(stats);
308         spin_unlock_irqrestore(&blkg->stats_lock, flags);
309 }
310 EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
311
312 void blkiocg_set_start_empty_time(struct blkio_group *blkg)
313 {
314         unsigned long flags;
315         struct blkio_group_stats *stats;
316
317         spin_lock_irqsave(&blkg->stats_lock, flags);
318         stats = &blkg->stats;
319
320         if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
321                         stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
322                 spin_unlock_irqrestore(&blkg->stats_lock, flags);
323                 return;
324         }
325
326         /*
327          * group is already marked empty. This can happen if cfqq got new
328          * request in parent group and moved to this group while being added
329          * to service tree. Just ignore the event and move on.
330          */
331         if(blkio_blkg_empty(stats)) {
332                 spin_unlock_irqrestore(&blkg->stats_lock, flags);
333                 return;
334         }
335
336         stats->start_empty_time = sched_clock();
337         blkio_mark_blkg_empty(stats);
338         spin_unlock_irqrestore(&blkg->stats_lock, flags);
339 }
340 EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
341
342 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
343                         unsigned long dequeue)
344 {
345         blkg->stats.dequeue += dequeue;
346 }
347 EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
348 #else
349 static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
350                                         struct blkio_group *curr_blkg) {}
351 static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
352 #endif
353
354 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
355                         struct blkio_group *curr_blkg, bool direction,
356                         bool sync)
357 {
358         unsigned long flags;
359
360         spin_lock_irqsave(&blkg->stats_lock, flags);
361         blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
362                         sync);
363         blkio_end_empty_time(&blkg->stats);
364         blkio_set_start_group_wait_time(blkg, curr_blkg);
365         spin_unlock_irqrestore(&blkg->stats_lock, flags);
366 }
367 EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
368
369 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
370                                                 bool direction, bool sync)
371 {
372         unsigned long flags;
373
374         spin_lock_irqsave(&blkg->stats_lock, flags);
375         blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
376                                         direction, sync);
377         spin_unlock_irqrestore(&blkg->stats_lock, flags);
378 }
379 EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
380
381 void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time,
382                                 unsigned long unaccounted_time)
383 {
384         unsigned long flags;
385
386         spin_lock_irqsave(&blkg->stats_lock, flags);
387         blkg->stats.time += time;
388 #ifdef CONFIG_DEBUG_BLK_CGROUP
389         blkg->stats.unaccounted_time += unaccounted_time;
390 #endif
391         spin_unlock_irqrestore(&blkg->stats_lock, flags);
392 }
393 EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
394
395 /*
396  * should be called under rcu read lock or queue lock to make sure blkg pointer
397  * is valid.
398  */
399 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
400                                 uint64_t bytes, bool direction, bool sync)
401 {
402         struct blkio_group_stats_cpu *stats_cpu;
403         unsigned long flags;
404
405         /*
406          * Disabling interrupts to provide mutual exclusion between two
407          * writes on same cpu. It probably is not needed for 64bit. Not
408          * optimizing that case yet.
409          */
410         local_irq_save(flags);
411
412         stats_cpu = this_cpu_ptr(blkg->stats_cpu);
413
414         u64_stats_update_begin(&stats_cpu->syncp);
415         stats_cpu->sectors += bytes >> 9;
416         blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
417                         1, direction, sync);
418         blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
419                         bytes, direction, sync);
420         u64_stats_update_end(&stats_cpu->syncp);
421         local_irq_restore(flags);
422 }
423 EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
424
425 void blkiocg_update_completion_stats(struct blkio_group *blkg,
426         uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
427 {
428         struct blkio_group_stats *stats;
429         unsigned long flags;
430         unsigned long long now = sched_clock();
431
432         spin_lock_irqsave(&blkg->stats_lock, flags);
433         stats = &blkg->stats;
434         if (time_after64(now, io_start_time))
435                 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
436                                 now - io_start_time, direction, sync);
437         if (time_after64(io_start_time, start_time))
438                 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
439                                 io_start_time - start_time, direction, sync);
440         spin_unlock_irqrestore(&blkg->stats_lock, flags);
441 }
442 EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
443
444 /*  Merged stats are per cpu.  */
445 void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
446                                         bool sync)
447 {
448         struct blkio_group_stats_cpu *stats_cpu;
449         unsigned long flags;
450
451         /*
452          * Disabling interrupts to provide mutual exclusion between two
453          * writes on same cpu. It probably is not needed for 64bit. Not
454          * optimizing that case yet.
455          */
456         local_irq_save(flags);
457
458         stats_cpu = this_cpu_ptr(blkg->stats_cpu);
459
460         u64_stats_update_begin(&stats_cpu->syncp);
461         blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
462                                 direction, sync);
463         u64_stats_update_end(&stats_cpu->syncp);
464         local_irq_restore(flags);
465 }
466 EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
467
468 /*
469  * This function allocates the per cpu stats for blkio_group. Should be called
470  * from sleepable context as alloc_per_cpu() requires that.
471  */
472 int blkio_alloc_blkg_stats(struct blkio_group *blkg)
473 {
474         /* Allocate memory for per cpu stats */
475         blkg->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
476         if (!blkg->stats_cpu)
477                 return -ENOMEM;
478         return 0;
479 }
480 EXPORT_SYMBOL_GPL(blkio_alloc_blkg_stats);
481
482 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
483                 struct blkio_group *blkg, void *key, dev_t dev,
484                 enum blkio_policy_id plid)
485 {
486         unsigned long flags;
487
488         spin_lock_irqsave(&blkcg->lock, flags);
489         spin_lock_init(&blkg->stats_lock);
490         rcu_assign_pointer(blkg->key, key);
491         blkg->blkcg_id = css_id(&blkcg->css);
492         hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
493         blkg->plid = plid;
494         spin_unlock_irqrestore(&blkcg->lock, flags);
495         /* Need to take css reference ? */
496         cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
497         blkg->dev = dev;
498 }
499 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
500
501 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
502 {
503         hlist_del_init_rcu(&blkg->blkcg_node);
504         blkg->blkcg_id = 0;
505 }
506
507 /*
508  * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
509  * indicating that blk_group was unhashed by the time we got to it.
510  */
511 int blkiocg_del_blkio_group(struct blkio_group *blkg)
512 {
513         struct blkio_cgroup *blkcg;
514         unsigned long flags;
515         struct cgroup_subsys_state *css;
516         int ret = 1;
517
518         rcu_read_lock();
519         css = css_lookup(&blkio_subsys, blkg->blkcg_id);
520         if (css) {
521                 blkcg = container_of(css, struct blkio_cgroup, css);
522                 spin_lock_irqsave(&blkcg->lock, flags);
523                 if (!hlist_unhashed(&blkg->blkcg_node)) {
524                         __blkiocg_del_blkio_group(blkg);
525                         ret = 0;
526                 }
527                 spin_unlock_irqrestore(&blkcg->lock, flags);
528         }
529
530         rcu_read_unlock();
531         return ret;
532 }
533 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
534
535 /* called under rcu_read_lock(). */
536 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
537 {
538         struct blkio_group *blkg;
539         struct hlist_node *n;
540         void *__key;
541
542         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
543                 __key = blkg->key;
544                 if (__key == key)
545                         return blkg;
546         }
547
548         return NULL;
549 }
550 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
551
552 static void blkio_reset_stats_cpu(struct blkio_group *blkg)
553 {
554         struct blkio_group_stats_cpu *stats_cpu;
555         int i, j, k;
556         /*
557          * Note: On 64 bit arch this should not be an issue. This has the
558          * possibility of returning some inconsistent value on 32bit arch
559          * as 64bit update on 32bit is non atomic. Taking care of this
560          * corner case makes code very complicated, like sending IPIs to
561          * cpus, taking care of stats of offline cpus etc.
562          *
563          * reset stats is anyway more of a debug feature and this sounds a
564          * corner case. So I am not complicating the code yet until and
565          * unless this becomes a real issue.
566          */
567         for_each_possible_cpu(i) {
568                 stats_cpu = per_cpu_ptr(blkg->stats_cpu, i);
569                 stats_cpu->sectors = 0;
570                 for(j = 0; j < BLKIO_STAT_CPU_NR; j++)
571                         for (k = 0; k < BLKIO_STAT_TOTAL; k++)
572                                 stats_cpu->stat_arr_cpu[j][k] = 0;
573         }
574 }
575
576 static int
577 blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
578 {
579         struct blkio_cgroup *blkcg;
580         struct blkio_group *blkg;
581         struct blkio_group_stats *stats;
582         struct hlist_node *n;
583         uint64_t queued[BLKIO_STAT_TOTAL];
584         int i;
585 #ifdef CONFIG_DEBUG_BLK_CGROUP
586         bool idling, waiting, empty;
587         unsigned long long now = sched_clock();
588 #endif
589
590         blkcg = cgroup_to_blkio_cgroup(cgroup);
591         spin_lock_irq(&blkcg->lock);
592         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
593                 spin_lock(&blkg->stats_lock);
594                 stats = &blkg->stats;
595 #ifdef CONFIG_DEBUG_BLK_CGROUP
596                 idling = blkio_blkg_idling(stats);
597                 waiting = blkio_blkg_waiting(stats);
598                 empty = blkio_blkg_empty(stats);
599 #endif
600                 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
601                         queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
602                 memset(stats, 0, sizeof(struct blkio_group_stats));
603                 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
604                         stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
605 #ifdef CONFIG_DEBUG_BLK_CGROUP
606                 if (idling) {
607                         blkio_mark_blkg_idling(stats);
608                         stats->start_idle_time = now;
609                 }
610                 if (waiting) {
611                         blkio_mark_blkg_waiting(stats);
612                         stats->start_group_wait_time = now;
613                 }
614                 if (empty) {
615                         blkio_mark_blkg_empty(stats);
616                         stats->start_empty_time = now;
617                 }
618 #endif
619                 spin_unlock(&blkg->stats_lock);
620
621                 /* Reset Per cpu stats which don't take blkg->stats_lock */
622                 blkio_reset_stats_cpu(blkg);
623         }
624
625         spin_unlock_irq(&blkcg->lock);
626         return 0;
627 }
628
629 static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
630                                 int chars_left, bool diskname_only)
631 {
632         snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
633         chars_left -= strlen(str);
634         if (chars_left <= 0) {
635                 printk(KERN_WARNING
636                         "Possibly incorrect cgroup stat display format");
637                 return;
638         }
639         if (diskname_only)
640                 return;
641         switch (type) {
642         case BLKIO_STAT_READ:
643                 strlcat(str, " Read", chars_left);
644                 break;
645         case BLKIO_STAT_WRITE:
646                 strlcat(str, " Write", chars_left);
647                 break;
648         case BLKIO_STAT_SYNC:
649                 strlcat(str, " Sync", chars_left);
650                 break;
651         case BLKIO_STAT_ASYNC:
652                 strlcat(str, " Async", chars_left);
653                 break;
654         case BLKIO_STAT_TOTAL:
655                 strlcat(str, " Total", chars_left);
656                 break;
657         default:
658                 strlcat(str, " Invalid", chars_left);
659         }
660 }
661
662 static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
663                                 struct cgroup_map_cb *cb, dev_t dev)
664 {
665         blkio_get_key_name(0, dev, str, chars_left, true);
666         cb->fill(cb, str, val);
667         return val;
668 }
669
670
671 static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg,
672                         enum stat_type_cpu type, enum stat_sub_type sub_type)
673 {
674         int cpu;
675         struct blkio_group_stats_cpu *stats_cpu;
676         u64 val = 0, tval;
677
678         for_each_possible_cpu(cpu) {
679                 unsigned int start;
680                 stats_cpu  = per_cpu_ptr(blkg->stats_cpu, cpu);
681
682                 do {
683                         start = u64_stats_fetch_begin(&stats_cpu->syncp);
684                         if (type == BLKIO_STAT_CPU_SECTORS)
685                                 tval = stats_cpu->sectors;
686                         else
687                                 tval = stats_cpu->stat_arr_cpu[type][sub_type];
688                 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
689
690                 val += tval;
691         }
692
693         return val;
694 }
695
696 static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg,
697                 struct cgroup_map_cb *cb, dev_t dev, enum stat_type_cpu type)
698 {
699         uint64_t disk_total, val;
700         char key_str[MAX_KEY_LEN];
701         enum stat_sub_type sub_type;
702
703         if (type == BLKIO_STAT_CPU_SECTORS) {
704                 val = blkio_read_stat_cpu(blkg, type, 0);
705                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, val, cb, dev);
706         }
707
708         for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
709                         sub_type++) {
710                 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
711                 val = blkio_read_stat_cpu(blkg, type, sub_type);
712                 cb->fill(cb, key_str, val);
713         }
714
715         disk_total = blkio_read_stat_cpu(blkg, type, BLKIO_STAT_READ) +
716                         blkio_read_stat_cpu(blkg, type, BLKIO_STAT_WRITE);
717
718         blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
719         cb->fill(cb, key_str, disk_total);
720         return disk_total;
721 }
722
723 /* This should be called with blkg->stats_lock held */
724 static uint64_t blkio_get_stat(struct blkio_group *blkg,
725                 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
726 {
727         uint64_t disk_total;
728         char key_str[MAX_KEY_LEN];
729         enum stat_sub_type sub_type;
730
731         if (type == BLKIO_STAT_TIME)
732                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
733                                         blkg->stats.time, cb, dev);
734 #ifdef CONFIG_DEBUG_BLK_CGROUP
735         if (type == BLKIO_STAT_UNACCOUNTED_TIME)
736                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
737                                         blkg->stats.unaccounted_time, cb, dev);
738         if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
739                 uint64_t sum = blkg->stats.avg_queue_size_sum;
740                 uint64_t samples = blkg->stats.avg_queue_size_samples;
741                 if (samples)
742                         do_div(sum, samples);
743                 else
744                         sum = 0;
745                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
746         }
747         if (type == BLKIO_STAT_GROUP_WAIT_TIME)
748                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
749                                         blkg->stats.group_wait_time, cb, dev);
750         if (type == BLKIO_STAT_IDLE_TIME)
751                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
752                                         blkg->stats.idle_time, cb, dev);
753         if (type == BLKIO_STAT_EMPTY_TIME)
754                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
755                                         blkg->stats.empty_time, cb, dev);
756         if (type == BLKIO_STAT_DEQUEUE)
757                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
758                                         blkg->stats.dequeue, cb, dev);
759 #endif
760
761         for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
762                         sub_type++) {
763                 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
764                 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
765         }
766         disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
767                         blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
768         blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
769         cb->fill(cb, key_str, disk_total);
770         return disk_total;
771 }
772
773 static int blkio_check_dev_num(dev_t dev)
774 {
775         int part = 0;
776         struct gendisk *disk;
777
778         disk = get_gendisk(dev, &part);
779         if (!disk || part)
780                 return -ENODEV;
781
782         return 0;
783 }
784
785 static int blkio_policy_parse_and_set(char *buf,
786         struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
787 {
788         char *s[4], *p, *major_s = NULL, *minor_s = NULL;
789         int ret;
790         unsigned long major, minor;
791         int i = 0;
792         dev_t dev;
793         u64 temp;
794
795         memset(s, 0, sizeof(s));
796
797         while ((p = strsep(&buf, " ")) != NULL) {
798                 if (!*p)
799                         continue;
800
801                 s[i++] = p;
802
803                 /* Prevent from inputing too many things */
804                 if (i == 3)
805                         break;
806         }
807
808         if (i != 2)
809                 return -EINVAL;
810
811         p = strsep(&s[0], ":");
812         if (p != NULL)
813                 major_s = p;
814         else
815                 return -EINVAL;
816
817         minor_s = s[0];
818         if (!minor_s)
819                 return -EINVAL;
820
821         ret = strict_strtoul(major_s, 10, &major);
822         if (ret)
823                 return -EINVAL;
824
825         ret = strict_strtoul(minor_s, 10, &minor);
826         if (ret)
827                 return -EINVAL;
828
829         dev = MKDEV(major, minor);
830
831         ret = strict_strtoull(s[1], 10, &temp);
832         if (ret)
833                 return -EINVAL;
834
835         /* For rule removal, do not check for device presence. */
836         if (temp) {
837                 ret = blkio_check_dev_num(dev);
838                 if (ret)
839                         return ret;
840         }
841
842         newpn->dev = dev;
843
844         switch (plid) {
845         case BLKIO_POLICY_PROP:
846                 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
847                      temp > BLKIO_WEIGHT_MAX)
848                         return -EINVAL;
849
850                 newpn->plid = plid;
851                 newpn->fileid = fileid;
852                 newpn->val.weight = temp;
853                 break;
854         case BLKIO_POLICY_THROTL:
855                 switch(fileid) {
856                 case BLKIO_THROTL_read_bps_device:
857                 case BLKIO_THROTL_write_bps_device:
858                         newpn->plid = plid;
859                         newpn->fileid = fileid;
860                         newpn->val.bps = temp;
861                         break;
862                 case BLKIO_THROTL_read_iops_device:
863                 case BLKIO_THROTL_write_iops_device:
864                         if (temp > THROTL_IOPS_MAX)
865                                 return -EINVAL;
866
867                         newpn->plid = plid;
868                         newpn->fileid = fileid;
869                         newpn->val.iops = (unsigned int)temp;
870                         break;
871                 }
872                 break;
873         default:
874                 BUG();
875         }
876
877         return 0;
878 }
879
880 unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
881                               dev_t dev)
882 {
883         struct blkio_policy_node *pn;
884
885         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
886                                 BLKIO_PROP_weight_device);
887         if (pn)
888                 return pn->val.weight;
889         else
890                 return blkcg->weight;
891 }
892 EXPORT_SYMBOL_GPL(blkcg_get_weight);
893
894 uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
895 {
896         struct blkio_policy_node *pn;
897
898         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
899                                 BLKIO_THROTL_read_bps_device);
900         if (pn)
901                 return pn->val.bps;
902         else
903                 return -1;
904 }
905
906 uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
907 {
908         struct blkio_policy_node *pn;
909         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
910                                 BLKIO_THROTL_write_bps_device);
911         if (pn)
912                 return pn->val.bps;
913         else
914                 return -1;
915 }
916
917 unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
918 {
919         struct blkio_policy_node *pn;
920
921         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
922                                 BLKIO_THROTL_read_iops_device);
923         if (pn)
924                 return pn->val.iops;
925         else
926                 return -1;
927 }
928
929 unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
930 {
931         struct blkio_policy_node *pn;
932         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
933                                 BLKIO_THROTL_write_iops_device);
934         if (pn)
935                 return pn->val.iops;
936         else
937                 return -1;
938 }
939
940 /* Checks whether user asked for deleting a policy rule */
941 static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
942 {
943         switch(pn->plid) {
944         case BLKIO_POLICY_PROP:
945                 if (pn->val.weight == 0)
946                         return 1;
947                 break;
948         case BLKIO_POLICY_THROTL:
949                 switch(pn->fileid) {
950                 case BLKIO_THROTL_read_bps_device:
951                 case BLKIO_THROTL_write_bps_device:
952                         if (pn->val.bps == 0)
953                                 return 1;
954                         break;
955                 case BLKIO_THROTL_read_iops_device:
956                 case BLKIO_THROTL_write_iops_device:
957                         if (pn->val.iops == 0)
958                                 return 1;
959                 }
960                 break;
961         default:
962                 BUG();
963         }
964
965         return 0;
966 }
967
968 static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
969                                         struct blkio_policy_node *newpn)
970 {
971         switch(oldpn->plid) {
972         case BLKIO_POLICY_PROP:
973                 oldpn->val.weight = newpn->val.weight;
974                 break;
975         case BLKIO_POLICY_THROTL:
976                 switch(newpn->fileid) {
977                 case BLKIO_THROTL_read_bps_device:
978                 case BLKIO_THROTL_write_bps_device:
979                         oldpn->val.bps = newpn->val.bps;
980                         break;
981                 case BLKIO_THROTL_read_iops_device:
982                 case BLKIO_THROTL_write_iops_device:
983                         oldpn->val.iops = newpn->val.iops;
984                 }
985                 break;
986         default:
987                 BUG();
988         }
989 }
990
991 /*
992  * Some rules/values in blkg have changed. Propagate those to respective
993  * policies.
994  */
995 static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
996                 struct blkio_group *blkg, struct blkio_policy_node *pn)
997 {
998         unsigned int weight, iops;
999         u64 bps;
1000
1001         switch(pn->plid) {
1002         case BLKIO_POLICY_PROP:
1003                 weight = pn->val.weight ? pn->val.weight :
1004                                 blkcg->weight;
1005                 blkio_update_group_weight(blkg, weight);
1006                 break;
1007         case BLKIO_POLICY_THROTL:
1008                 switch(pn->fileid) {
1009                 case BLKIO_THROTL_read_bps_device:
1010                 case BLKIO_THROTL_write_bps_device:
1011                         bps = pn->val.bps ? pn->val.bps : (-1);
1012                         blkio_update_group_bps(blkg, bps, pn->fileid);
1013                         break;
1014                 case BLKIO_THROTL_read_iops_device:
1015                 case BLKIO_THROTL_write_iops_device:
1016                         iops = pn->val.iops ? pn->val.iops : (-1);
1017                         blkio_update_group_iops(blkg, iops, pn->fileid);
1018                         break;
1019                 }
1020                 break;
1021         default:
1022                 BUG();
1023         }
1024 }
1025
1026 /*
1027  * A policy node rule has been updated. Propagate this update to all the
1028  * block groups which might be affected by this update.
1029  */
1030 static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
1031                                 struct blkio_policy_node *pn)
1032 {
1033         struct blkio_group *blkg;
1034         struct hlist_node *n;
1035
1036         spin_lock(&blkio_list_lock);
1037         spin_lock_irq(&blkcg->lock);
1038
1039         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1040                 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
1041                         continue;
1042                 blkio_update_blkg_policy(blkcg, blkg, pn);
1043         }
1044
1045         spin_unlock_irq(&blkcg->lock);
1046         spin_unlock(&blkio_list_lock);
1047 }
1048
1049 static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1050                                        const char *buffer)
1051 {
1052         int ret = 0;
1053         char *buf;
1054         struct blkio_policy_node *newpn, *pn;
1055         struct blkio_cgroup *blkcg;
1056         int keep_newpn = 0;
1057         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1058         int fileid = BLKIOFILE_ATTR(cft->private);
1059
1060         buf = kstrdup(buffer, GFP_KERNEL);
1061         if (!buf)
1062                 return -ENOMEM;
1063
1064         newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
1065         if (!newpn) {
1066                 ret = -ENOMEM;
1067                 goto free_buf;
1068         }
1069
1070         ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
1071         if (ret)
1072                 goto free_newpn;
1073
1074         blkcg = cgroup_to_blkio_cgroup(cgrp);
1075
1076         spin_lock_irq(&blkcg->lock);
1077
1078         pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
1079         if (!pn) {
1080                 if (!blkio_delete_rule_command(newpn)) {
1081                         blkio_policy_insert_node(blkcg, newpn);
1082                         keep_newpn = 1;
1083                 }
1084                 spin_unlock_irq(&blkcg->lock);
1085                 goto update_io_group;
1086         }
1087
1088         if (blkio_delete_rule_command(newpn)) {
1089                 blkio_policy_delete_node(pn);
1090                 spin_unlock_irq(&blkcg->lock);
1091                 goto update_io_group;
1092         }
1093         spin_unlock_irq(&blkcg->lock);
1094
1095         blkio_update_policy_rule(pn, newpn);
1096
1097 update_io_group:
1098         blkio_update_policy_node_blkg(blkcg, newpn);
1099
1100 free_newpn:
1101         if (!keep_newpn)
1102                 kfree(newpn);
1103 free_buf:
1104         kfree(buf);
1105         return ret;
1106 }
1107
1108 static void
1109 blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
1110 {
1111         switch(pn->plid) {
1112                 case BLKIO_POLICY_PROP:
1113                         if (pn->fileid == BLKIO_PROP_weight_device)
1114                                 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1115                                         MINOR(pn->dev), pn->val.weight);
1116                         break;
1117                 case BLKIO_POLICY_THROTL:
1118                         switch(pn->fileid) {
1119                         case BLKIO_THROTL_read_bps_device:
1120                         case BLKIO_THROTL_write_bps_device:
1121                                 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
1122                                         MINOR(pn->dev), pn->val.bps);
1123                                 break;
1124                         case BLKIO_THROTL_read_iops_device:
1125                         case BLKIO_THROTL_write_iops_device:
1126                                 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
1127                                         MINOR(pn->dev), pn->val.iops);
1128                                 break;
1129                         }
1130                         break;
1131                 default:
1132                         BUG();
1133         }
1134 }
1135
1136 /* cgroup files which read their data from policy nodes end up here */
1137 static void blkio_read_policy_node_files(struct cftype *cft,
1138                         struct blkio_cgroup *blkcg, struct seq_file *m)
1139 {
1140         struct blkio_policy_node *pn;
1141
1142         if (!list_empty(&blkcg->policy_list)) {
1143                 spin_lock_irq(&blkcg->lock);
1144                 list_for_each_entry(pn, &blkcg->policy_list, node) {
1145                         if (!pn_matches_cftype(cft, pn))
1146                                 continue;
1147                         blkio_print_policy_node(m, pn);
1148                 }
1149                 spin_unlock_irq(&blkcg->lock);
1150         }
1151 }
1152
1153 static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1154                                 struct seq_file *m)
1155 {
1156         struct blkio_cgroup *blkcg;
1157         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1158         int name = BLKIOFILE_ATTR(cft->private);
1159
1160         blkcg = cgroup_to_blkio_cgroup(cgrp);
1161
1162         switch(plid) {
1163         case BLKIO_POLICY_PROP:
1164                 switch(name) {
1165                 case BLKIO_PROP_weight_device:
1166                         blkio_read_policy_node_files(cft, blkcg, m);
1167                         return 0;
1168                 default:
1169                         BUG();
1170                 }
1171                 break;
1172         case BLKIO_POLICY_THROTL:
1173                 switch(name){
1174                 case BLKIO_THROTL_read_bps_device:
1175                 case BLKIO_THROTL_write_bps_device:
1176                 case BLKIO_THROTL_read_iops_device:
1177                 case BLKIO_THROTL_write_iops_device:
1178                         blkio_read_policy_node_files(cft, blkcg, m);
1179                         return 0;
1180                 default:
1181                         BUG();
1182                 }
1183                 break;
1184         default:
1185                 BUG();
1186         }
1187
1188         return 0;
1189 }
1190
1191 static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1192                 struct cftype *cft, struct cgroup_map_cb *cb,
1193                 enum stat_type type, bool show_total, bool pcpu)
1194 {
1195         struct blkio_group *blkg;
1196         struct hlist_node *n;
1197         uint64_t cgroup_total = 0;
1198
1199         rcu_read_lock();
1200         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1201                 if (blkg->dev) {
1202                         if (!cftype_blkg_same_policy(cft, blkg))
1203                                 continue;
1204                         if (pcpu)
1205                                 cgroup_total += blkio_get_stat_cpu(blkg, cb,
1206                                                 blkg->dev, type);
1207                         else {
1208                                 spin_lock_irq(&blkg->stats_lock);
1209                                 cgroup_total += blkio_get_stat(blkg, cb,
1210                                                 blkg->dev, type);
1211                                 spin_unlock_irq(&blkg->stats_lock);
1212                         }
1213                 }
1214         }
1215         if (show_total)
1216                 cb->fill(cb, "Total", cgroup_total);
1217         rcu_read_unlock();
1218         return 0;
1219 }
1220
1221 /* All map kind of cgroup file get serviced by this function */
1222 static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1223                                 struct cgroup_map_cb *cb)
1224 {
1225         struct blkio_cgroup *blkcg;
1226         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1227         int name = BLKIOFILE_ATTR(cft->private);
1228
1229         blkcg = cgroup_to_blkio_cgroup(cgrp);
1230
1231         switch(plid) {
1232         case BLKIO_POLICY_PROP:
1233                 switch(name) {
1234                 case BLKIO_PROP_time:
1235                         return blkio_read_blkg_stats(blkcg, cft, cb,
1236                                                 BLKIO_STAT_TIME, 0, 0);
1237                 case BLKIO_PROP_sectors:
1238                         return blkio_read_blkg_stats(blkcg, cft, cb,
1239                                                 BLKIO_STAT_CPU_SECTORS, 0, 1);
1240                 case BLKIO_PROP_io_service_bytes:
1241                         return blkio_read_blkg_stats(blkcg, cft, cb,
1242                                         BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1243                 case BLKIO_PROP_io_serviced:
1244                         return blkio_read_blkg_stats(blkcg, cft, cb,
1245                                                 BLKIO_STAT_CPU_SERVICED, 1, 1);
1246                 case BLKIO_PROP_io_service_time:
1247                         return blkio_read_blkg_stats(blkcg, cft, cb,
1248                                                 BLKIO_STAT_SERVICE_TIME, 1, 0);
1249                 case BLKIO_PROP_io_wait_time:
1250                         return blkio_read_blkg_stats(blkcg, cft, cb,
1251                                                 BLKIO_STAT_WAIT_TIME, 1, 0);
1252                 case BLKIO_PROP_io_merged:
1253                         return blkio_read_blkg_stats(blkcg, cft, cb,
1254                                                 BLKIO_STAT_CPU_MERGED, 1, 1);
1255                 case BLKIO_PROP_io_queued:
1256                         return blkio_read_blkg_stats(blkcg, cft, cb,
1257                                                 BLKIO_STAT_QUEUED, 1, 0);
1258 #ifdef CONFIG_DEBUG_BLK_CGROUP
1259                 case BLKIO_PROP_unaccounted_time:
1260                         return blkio_read_blkg_stats(blkcg, cft, cb,
1261                                         BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
1262                 case BLKIO_PROP_dequeue:
1263                         return blkio_read_blkg_stats(blkcg, cft, cb,
1264                                                 BLKIO_STAT_DEQUEUE, 0, 0);
1265                 case BLKIO_PROP_avg_queue_size:
1266                         return blkio_read_blkg_stats(blkcg, cft, cb,
1267                                         BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
1268                 case BLKIO_PROP_group_wait_time:
1269                         return blkio_read_blkg_stats(blkcg, cft, cb,
1270                                         BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
1271                 case BLKIO_PROP_idle_time:
1272                         return blkio_read_blkg_stats(blkcg, cft, cb,
1273                                                 BLKIO_STAT_IDLE_TIME, 0, 0);
1274                 case BLKIO_PROP_empty_time:
1275                         return blkio_read_blkg_stats(blkcg, cft, cb,
1276                                                 BLKIO_STAT_EMPTY_TIME, 0, 0);
1277 #endif
1278                 default:
1279                         BUG();
1280                 }
1281                 break;
1282         case BLKIO_POLICY_THROTL:
1283                 switch(name){
1284                 case BLKIO_THROTL_io_service_bytes:
1285                         return blkio_read_blkg_stats(blkcg, cft, cb,
1286                                                 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
1287                 case BLKIO_THROTL_io_serviced:
1288                         return blkio_read_blkg_stats(blkcg, cft, cb,
1289                                                 BLKIO_STAT_CPU_SERVICED, 1, 1);
1290                 default:
1291                         BUG();
1292                 }
1293                 break;
1294         default:
1295                 BUG();
1296         }
1297
1298         return 0;
1299 }
1300
1301 static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1302 {
1303         struct blkio_group *blkg;
1304         struct hlist_node *n;
1305         struct blkio_policy_node *pn;
1306
1307         if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1308                 return -EINVAL;
1309
1310         spin_lock(&blkio_list_lock);
1311         spin_lock_irq(&blkcg->lock);
1312         blkcg->weight = (unsigned int)val;
1313
1314         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1315                 pn = blkio_policy_search_node(blkcg, blkg->dev,
1316                                 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1317                 if (pn)
1318                         continue;
1319
1320                 blkio_update_group_weight(blkg, blkcg->weight);
1321         }
1322         spin_unlock_irq(&blkcg->lock);
1323         spin_unlock(&blkio_list_lock);
1324         return 0;
1325 }
1326
1327 static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1328         struct blkio_cgroup *blkcg;
1329         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1330         int name = BLKIOFILE_ATTR(cft->private);
1331
1332         blkcg = cgroup_to_blkio_cgroup(cgrp);
1333
1334         switch(plid) {
1335         case BLKIO_POLICY_PROP:
1336                 switch(name) {
1337                 case BLKIO_PROP_weight:
1338                         return (u64)blkcg->weight;
1339                 }
1340                 break;
1341         default:
1342                 BUG();
1343         }
1344         return 0;
1345 }
1346
1347 static int
1348 blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1349 {
1350         struct blkio_cgroup *blkcg;
1351         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1352         int name = BLKIOFILE_ATTR(cft->private);
1353
1354         blkcg = cgroup_to_blkio_cgroup(cgrp);
1355
1356         switch(plid) {
1357         case BLKIO_POLICY_PROP:
1358                 switch(name) {
1359                 case BLKIO_PROP_weight:
1360                         return blkio_weight_write(blkcg, val);
1361                 }
1362                 break;
1363         default:
1364                 BUG();
1365         }
1366
1367         return 0;
1368 }
1369
1370 struct cftype blkio_files[] = {
1371         {
1372                 .name = "weight_device",
1373                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1374                                 BLKIO_PROP_weight_device),
1375                 .read_seq_string = blkiocg_file_read,
1376                 .write_string = blkiocg_file_write,
1377                 .max_write_len = 256,
1378         },
1379         {
1380                 .name = "weight",
1381                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1382                                 BLKIO_PROP_weight),
1383                 .read_u64 = blkiocg_file_read_u64,
1384                 .write_u64 = blkiocg_file_write_u64,
1385         },
1386         {
1387                 .name = "time",
1388                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1389                                 BLKIO_PROP_time),
1390                 .read_map = blkiocg_file_read_map,
1391         },
1392         {
1393                 .name = "sectors",
1394                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1395                                 BLKIO_PROP_sectors),
1396                 .read_map = blkiocg_file_read_map,
1397         },
1398         {
1399                 .name = "io_service_bytes",
1400                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1401                                 BLKIO_PROP_io_service_bytes),
1402                 .read_map = blkiocg_file_read_map,
1403         },
1404         {
1405                 .name = "io_serviced",
1406                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1407                                 BLKIO_PROP_io_serviced),
1408                 .read_map = blkiocg_file_read_map,
1409         },
1410         {
1411                 .name = "io_service_time",
1412                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1413                                 BLKIO_PROP_io_service_time),
1414                 .read_map = blkiocg_file_read_map,
1415         },
1416         {
1417                 .name = "io_wait_time",
1418                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1419                                 BLKIO_PROP_io_wait_time),
1420                 .read_map = blkiocg_file_read_map,
1421         },
1422         {
1423                 .name = "io_merged",
1424                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1425                                 BLKIO_PROP_io_merged),
1426                 .read_map = blkiocg_file_read_map,
1427         },
1428         {
1429                 .name = "io_queued",
1430                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1431                                 BLKIO_PROP_io_queued),
1432                 .read_map = blkiocg_file_read_map,
1433         },
1434         {
1435                 .name = "reset_stats",
1436                 .write_u64 = blkiocg_reset_stats,
1437         },
1438 #ifdef CONFIG_BLK_DEV_THROTTLING
1439         {
1440                 .name = "throttle.read_bps_device",
1441                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1442                                 BLKIO_THROTL_read_bps_device),
1443                 .read_seq_string = blkiocg_file_read,
1444                 .write_string = blkiocg_file_write,
1445                 .max_write_len = 256,
1446         },
1447
1448         {
1449                 .name = "throttle.write_bps_device",
1450                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1451                                 BLKIO_THROTL_write_bps_device),
1452                 .read_seq_string = blkiocg_file_read,
1453                 .write_string = blkiocg_file_write,
1454                 .max_write_len = 256,
1455         },
1456
1457         {
1458                 .name = "throttle.read_iops_device",
1459                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1460                                 BLKIO_THROTL_read_iops_device),
1461                 .read_seq_string = blkiocg_file_read,
1462                 .write_string = blkiocg_file_write,
1463                 .max_write_len = 256,
1464         },
1465
1466         {
1467                 .name = "throttle.write_iops_device",
1468                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1469                                 BLKIO_THROTL_write_iops_device),
1470                 .read_seq_string = blkiocg_file_read,
1471                 .write_string = blkiocg_file_write,
1472                 .max_write_len = 256,
1473         },
1474         {
1475                 .name = "throttle.io_service_bytes",
1476                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1477                                 BLKIO_THROTL_io_service_bytes),
1478                 .read_map = blkiocg_file_read_map,
1479         },
1480         {
1481                 .name = "throttle.io_serviced",
1482                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1483                                 BLKIO_THROTL_io_serviced),
1484                 .read_map = blkiocg_file_read_map,
1485         },
1486 #endif /* CONFIG_BLK_DEV_THROTTLING */
1487
1488 #ifdef CONFIG_DEBUG_BLK_CGROUP
1489         {
1490                 .name = "avg_queue_size",
1491                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1492                                 BLKIO_PROP_avg_queue_size),
1493                 .read_map = blkiocg_file_read_map,
1494         },
1495         {
1496                 .name = "group_wait_time",
1497                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1498                                 BLKIO_PROP_group_wait_time),
1499                 .read_map = blkiocg_file_read_map,
1500         },
1501         {
1502                 .name = "idle_time",
1503                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1504                                 BLKIO_PROP_idle_time),
1505                 .read_map = blkiocg_file_read_map,
1506         },
1507         {
1508                 .name = "empty_time",
1509                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1510                                 BLKIO_PROP_empty_time),
1511                 .read_map = blkiocg_file_read_map,
1512         },
1513         {
1514                 .name = "dequeue",
1515                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1516                                 BLKIO_PROP_dequeue),
1517                 .read_map = blkiocg_file_read_map,
1518         },
1519         {
1520                 .name = "unaccounted_time",
1521                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1522                                 BLKIO_PROP_unaccounted_time),
1523                 .read_map = blkiocg_file_read_map,
1524         },
1525 #endif
1526 };
1527
1528 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1529 {
1530         return cgroup_add_files(cgroup, subsys, blkio_files,
1531                                 ARRAY_SIZE(blkio_files));
1532 }
1533
1534 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1535 {
1536         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1537         unsigned long flags;
1538         struct blkio_group *blkg;
1539         void *key;
1540         struct blkio_policy_type *blkiop;
1541         struct blkio_policy_node *pn, *pntmp;
1542
1543         rcu_read_lock();
1544         do {
1545                 spin_lock_irqsave(&blkcg->lock, flags);
1546
1547                 if (hlist_empty(&blkcg->blkg_list)) {
1548                         spin_unlock_irqrestore(&blkcg->lock, flags);
1549                         break;
1550                 }
1551
1552                 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1553                                         blkcg_node);
1554                 key = rcu_dereference(blkg->key);
1555                 __blkiocg_del_blkio_group(blkg);
1556
1557                 spin_unlock_irqrestore(&blkcg->lock, flags);
1558
1559                 /*
1560                  * This blkio_group is being unlinked as associated cgroup is
1561                  * going away. Let all the IO controlling policies know about
1562                  * this event.
1563                  */
1564                 spin_lock(&blkio_list_lock);
1565                 list_for_each_entry(blkiop, &blkio_list, list) {
1566                         if (blkiop->plid != blkg->plid)
1567                                 continue;
1568                         blkiop->ops.blkio_unlink_group_fn(key, blkg);
1569                 }
1570                 spin_unlock(&blkio_list_lock);
1571         } while (1);
1572
1573         list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1574                 blkio_policy_delete_node(pn);
1575                 kfree(pn);
1576         }
1577
1578         free_css_id(&blkio_subsys, &blkcg->css);
1579         rcu_read_unlock();
1580         if (blkcg != &blkio_root_cgroup)
1581                 kfree(blkcg);
1582 }
1583
1584 static struct cgroup_subsys_state *
1585 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1586 {
1587         struct blkio_cgroup *blkcg;
1588         struct cgroup *parent = cgroup->parent;
1589
1590         if (!parent) {
1591                 blkcg = &blkio_root_cgroup;
1592                 goto done;
1593         }
1594
1595         blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1596         if (!blkcg)
1597                 return ERR_PTR(-ENOMEM);
1598
1599         blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1600 done:
1601         spin_lock_init(&blkcg->lock);
1602         INIT_HLIST_HEAD(&blkcg->blkg_list);
1603
1604         INIT_LIST_HEAD(&blkcg->policy_list);
1605         return &blkcg->css;
1606 }
1607
1608 /*
1609  * We cannot support shared io contexts, as we have no mean to support
1610  * two tasks with the same ioc in two different groups without major rework
1611  * of the main cic data structures.  For now we allow a task to change
1612  * its cgroup only if it's the only owner of its ioc.
1613  */
1614 static int blkiocg_can_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
1615                                    struct task_struct *tsk)
1616 {
1617         struct io_context *ioc;
1618         int ret = 0;
1619
1620         /* task_lock() is needed to avoid races with exit_io_context() */
1621         task_lock(tsk);
1622         ioc = tsk->io_context;
1623         if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1624                 ret = -EINVAL;
1625         task_unlock(tsk);
1626
1627         return ret;
1628 }
1629
1630 static void blkiocg_attach_task(struct cgroup *cgrp, struct cgroup *old_cgrp,
1631                                 struct task_struct *tsk)
1632 {
1633         struct io_context *ioc;
1634
1635         task_lock(tsk);
1636         ioc = tsk->io_context;
1637         if (ioc)
1638                 ioc->cgroup_changed = 1;
1639         task_unlock(tsk);
1640 }
1641
1642 void blkio_policy_register(struct blkio_policy_type *blkiop)
1643 {
1644         spin_lock(&blkio_list_lock);
1645         list_add_tail(&blkiop->list, &blkio_list);
1646         spin_unlock(&blkio_list_lock);
1647 }
1648 EXPORT_SYMBOL_GPL(blkio_policy_register);
1649
1650 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1651 {
1652         spin_lock(&blkio_list_lock);
1653         list_del_init(&blkiop->list);
1654         spin_unlock(&blkio_list_lock);
1655 }
1656 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
1657
1658 static int __init init_cgroup_blkio(void)
1659 {
1660         return cgroup_load_subsys(&blkio_subsys);
1661 }
1662
1663 static void __exit exit_cgroup_blkio(void)
1664 {
1665         cgroup_unload_subsys(&blkio_subsys);
1666 }
1667
1668 module_init(init_cgroup_blkio);
1669 module_exit(exit_cgroup_blkio);
1670 MODULE_LICENSE("GPL");