Merge branch 'master' into for-2.6.35
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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
40struct cgroup_subsys blkio_subsys = {
41 .name = "blkio",
42 .create = blkiocg_create,
43 .can_attach = blkiocg_can_attach,
44 .attach = blkiocg_attach,
45 .destroy = blkiocg_destroy,
46 .populate = blkiocg_populate,
47#ifdef CONFIG_BLK_CGROUP
48 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
49 .subsys_id = blkio_subsys_id,
50#endif
51 .use_id = 1,
52 .module = THIS_MODULE,
53};
54EXPORT_SYMBOL_GPL(blkio_subsys);
55
34d0f179
GJ
56static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
57 struct blkio_policy_node *pn)
58{
59 list_add(&pn->node, &blkcg->policy_list);
60}
61
62/* Must be called with blkcg->lock held */
63static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
64{
65 list_del(&pn->node);
66}
67
68/* Must be called with blkcg->lock held */
69static struct blkio_policy_node *
70blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev)
71{
72 struct blkio_policy_node *pn;
73
74 list_for_each_entry(pn, &blkcg->policy_list, node) {
75 if (pn->dev == dev)
76 return pn;
77 }
78
79 return NULL;
80}
81
31e4c28d
VG
82struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
83{
84 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
85 struct blkio_cgroup, css);
86}
9d6a986c 87EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
31e4c28d 88
9195291e
DS
89/*
90 * Add to the appropriate stat variable depending on the request type.
91 * This should be called with the blkg->stats_lock held.
92 */
84c124da
DS
93static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
94 bool sync)
9195291e 95{
84c124da
DS
96 if (direction)
97 stat[BLKIO_STAT_WRITE] += add;
9195291e 98 else
84c124da
DS
99 stat[BLKIO_STAT_READ] += add;
100 if (sync)
101 stat[BLKIO_STAT_SYNC] += add;
9195291e 102 else
84c124da 103 stat[BLKIO_STAT_ASYNC] += add;
9195291e
DS
104}
105
cdc1184c
DS
106/*
107 * Decrements the appropriate stat variable if non-zero depending on the
108 * request type. Panics on value being zero.
109 * This should be called with the blkg->stats_lock held.
110 */
111static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
112{
113 if (direction) {
114 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
115 stat[BLKIO_STAT_WRITE]--;
116 } else {
117 BUG_ON(stat[BLKIO_STAT_READ] == 0);
118 stat[BLKIO_STAT_READ]--;
119 }
120 if (sync) {
121 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
122 stat[BLKIO_STAT_SYNC]--;
123 } else {
124 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
125 stat[BLKIO_STAT_ASYNC]--;
126 }
127}
128
129#ifdef CONFIG_DEBUG_BLK_CGROUP
812df48d
DS
130/* This should be called with the blkg->stats_lock held. */
131static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
132 struct blkio_group *curr_blkg)
133{
134 if (blkio_blkg_waiting(&blkg->stats))
135 return;
136 if (blkg == curr_blkg)
137 return;
138 blkg->stats.start_group_wait_time = sched_clock();
139 blkio_mark_blkg_waiting(&blkg->stats);
140}
141
142/* This should be called with the blkg->stats_lock held. */
143static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
144{
145 unsigned long long now;
146
147 if (!blkio_blkg_waiting(stats))
148 return;
149
150 now = sched_clock();
151 if (time_after64(now, stats->start_group_wait_time))
152 stats->group_wait_time += now - stats->start_group_wait_time;
153 blkio_clear_blkg_waiting(stats);
154}
155
156/* This should be called with the blkg->stats_lock held. */
157static void blkio_end_empty_time(struct blkio_group_stats *stats)
158{
159 unsigned long long now;
160
161 if (!blkio_blkg_empty(stats))
162 return;
163
164 now = sched_clock();
165 if (time_after64(now, stats->start_empty_time))
166 stats->empty_time += now - stats->start_empty_time;
167 blkio_clear_blkg_empty(stats);
168}
169
170void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
171{
172 unsigned long flags;
173
174 spin_lock_irqsave(&blkg->stats_lock, flags);
175 BUG_ON(blkio_blkg_idling(&blkg->stats));
176 blkg->stats.start_idle_time = sched_clock();
177 blkio_mark_blkg_idling(&blkg->stats);
178 spin_unlock_irqrestore(&blkg->stats_lock, flags);
179}
180EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
181
182void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
183{
184 unsigned long flags;
185 unsigned long long now;
186 struct blkio_group_stats *stats;
187
188 spin_lock_irqsave(&blkg->stats_lock, flags);
189 stats = &blkg->stats;
190 if (blkio_blkg_idling(stats)) {
191 now = sched_clock();
192 if (time_after64(now, stats->start_idle_time))
193 stats->idle_time += now - stats->start_idle_time;
194 blkio_clear_blkg_idling(stats);
195 }
196 spin_unlock_irqrestore(&blkg->stats_lock, flags);
197}
198EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
199
a11cdaa7 200void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
cdc1184c
DS
201{
202 unsigned long flags;
203 struct blkio_group_stats *stats;
204
205 spin_lock_irqsave(&blkg->stats_lock, flags);
206 stats = &blkg->stats;
207 stats->avg_queue_size_sum +=
208 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
209 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
210 stats->avg_queue_size_samples++;
812df48d 211 blkio_update_group_wait_time(stats);
cdc1184c
DS
212 spin_unlock_irqrestore(&blkg->stats_lock, flags);
213}
a11cdaa7
DS
214EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
215
e5ff082e 216void blkiocg_set_start_empty_time(struct blkio_group *blkg)
28baf442
DS
217{
218 unsigned long flags;
219 struct blkio_group_stats *stats;
220
221 spin_lock_irqsave(&blkg->stats_lock, flags);
222 stats = &blkg->stats;
223
224 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
225 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
226 spin_unlock_irqrestore(&blkg->stats_lock, flags);
227 return;
228 }
229
230 /*
e5ff082e
VG
231 * group is already marked empty. This can happen if cfqq got new
232 * request in parent group and moved to this group while being added
233 * to service tree. Just ignore the event and move on.
28baf442 234 */
e5ff082e
VG
235 if(blkio_blkg_empty(stats)) {
236 spin_unlock_irqrestore(&blkg->stats_lock, flags);
237 return;
238 }
239
28baf442
DS
240 stats->start_empty_time = sched_clock();
241 blkio_mark_blkg_empty(stats);
242 spin_unlock_irqrestore(&blkg->stats_lock, flags);
243}
244EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
245
a11cdaa7
DS
246void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
247 unsigned long dequeue)
248{
249 blkg->stats.dequeue += dequeue;
250}
251EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
812df48d
DS
252#else
253static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
254 struct blkio_group *curr_blkg) {}
255static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
cdc1184c
DS
256#endif
257
a11cdaa7 258void blkiocg_update_io_add_stats(struct blkio_group *blkg,
cdc1184c
DS
259 struct blkio_group *curr_blkg, bool direction,
260 bool sync)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(&blkg->stats_lock, flags);
265 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
266 sync);
812df48d
DS
267 blkio_end_empty_time(&blkg->stats);
268 blkio_set_start_group_wait_time(blkg, curr_blkg);
cdc1184c
DS
269 spin_unlock_irqrestore(&blkg->stats_lock, flags);
270}
a11cdaa7 271EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
cdc1184c 272
a11cdaa7 273void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
cdc1184c
DS
274 bool direction, bool sync)
275{
276 unsigned long flags;
277
278 spin_lock_irqsave(&blkg->stats_lock, flags);
279 blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
280 direction, sync);
281 spin_unlock_irqrestore(&blkg->stats_lock, flags);
282}
a11cdaa7 283EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
cdc1184c 284
303a3acb 285void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
22084190 286{
303a3acb
DS
287 unsigned long flags;
288
289 spin_lock_irqsave(&blkg->stats_lock, flags);
290 blkg->stats.time += time;
291 spin_unlock_irqrestore(&blkg->stats_lock, flags);
22084190 292}
303a3acb 293EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
22084190 294
84c124da
DS
295void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
296 uint64_t bytes, bool direction, bool sync)
9195291e
DS
297{
298 struct blkio_group_stats *stats;
299 unsigned long flags;
300
301 spin_lock_irqsave(&blkg->stats_lock, flags);
302 stats = &blkg->stats;
84c124da
DS
303 stats->sectors += bytes >> 9;
304 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
305 sync);
306 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
307 direction, sync);
9195291e
DS
308 spin_unlock_irqrestore(&blkg->stats_lock, flags);
309}
84c124da 310EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
9195291e 311
84c124da
DS
312void blkiocg_update_completion_stats(struct blkio_group *blkg,
313 uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
9195291e
DS
314{
315 struct blkio_group_stats *stats;
316 unsigned long flags;
317 unsigned long long now = sched_clock();
318
319 spin_lock_irqsave(&blkg->stats_lock, flags);
320 stats = &blkg->stats;
84c124da
DS
321 if (time_after64(now, io_start_time))
322 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
323 now - io_start_time, direction, sync);
324 if (time_after64(io_start_time, start_time))
325 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
326 io_start_time - start_time, direction, sync);
9195291e
DS
327 spin_unlock_irqrestore(&blkg->stats_lock, flags);
328}
84c124da 329EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
9195291e 330
812d4026
DS
331void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
332 bool sync)
333{
334 unsigned long flags;
335
336 spin_lock_irqsave(&blkg->stats_lock, flags);
337 blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
338 sync);
339 spin_unlock_irqrestore(&blkg->stats_lock, flags);
340}
341EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
342
31e4c28d 343void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
22084190 344 struct blkio_group *blkg, void *key, dev_t dev)
31e4c28d
VG
345{
346 unsigned long flags;
347
348 spin_lock_irqsave(&blkcg->lock, flags);
8d2a91f8 349 spin_lock_init(&blkg->stats_lock);
31e4c28d 350 rcu_assign_pointer(blkg->key, key);
b1c35769 351 blkg->blkcg_id = css_id(&blkcg->css);
31e4c28d
VG
352 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
353 spin_unlock_irqrestore(&blkcg->lock, flags);
2868ef7b
VG
354 /* Need to take css reference ? */
355 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
22084190 356 blkg->dev = dev;
31e4c28d 357}
9d6a986c 358EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
31e4c28d 359
b1c35769
VG
360static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
361{
362 hlist_del_init_rcu(&blkg->blkcg_node);
363 blkg->blkcg_id = 0;
364}
365
366/*
367 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
368 * indicating that blk_group was unhashed by the time we got to it.
369 */
31e4c28d
VG
370int blkiocg_del_blkio_group(struct blkio_group *blkg)
371{
b1c35769
VG
372 struct blkio_cgroup *blkcg;
373 unsigned long flags;
374 struct cgroup_subsys_state *css;
375 int ret = 1;
376
377 rcu_read_lock();
378 css = css_lookup(&blkio_subsys, blkg->blkcg_id);
379 if (!css)
380 goto out;
381
382 blkcg = container_of(css, struct blkio_cgroup, css);
383 spin_lock_irqsave(&blkcg->lock, flags);
384 if (!hlist_unhashed(&blkg->blkcg_node)) {
385 __blkiocg_del_blkio_group(blkg);
386 ret = 0;
387 }
388 spin_unlock_irqrestore(&blkcg->lock, flags);
389out:
390 rcu_read_unlock();
391 return ret;
31e4c28d 392}
9d6a986c 393EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
31e4c28d
VG
394
395/* called under rcu_read_lock(). */
396struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
397{
398 struct blkio_group *blkg;
399 struct hlist_node *n;
400 void *__key;
401
402 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
403 __key = blkg->key;
404 if (__key == key)
405 return blkg;
406 }
407
408 return NULL;
409}
9d6a986c 410EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
31e4c28d
VG
411
412#define SHOW_FUNCTION(__VAR) \
413static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
414 struct cftype *cftype) \
415{ \
416 struct blkio_cgroup *blkcg; \
417 \
418 blkcg = cgroup_to_blkio_cgroup(cgroup); \
419 return (u64)blkcg->__VAR; \
420}
421
422SHOW_FUNCTION(weight);
423#undef SHOW_FUNCTION
424
425static int
426blkiocg_weight_write(struct cgroup *cgroup, struct cftype *cftype, u64 val)
427{
428 struct blkio_cgroup *blkcg;
f8d461d6
VG
429 struct blkio_group *blkg;
430 struct hlist_node *n;
3e252066 431 struct blkio_policy_type *blkiop;
34d0f179 432 struct blkio_policy_node *pn;
31e4c28d
VG
433
434 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
435 return -EINVAL;
436
437 blkcg = cgroup_to_blkio_cgroup(cgroup);
bcf4dd43 438 spin_lock(&blkio_list_lock);
f8d461d6 439 spin_lock_irq(&blkcg->lock);
31e4c28d 440 blkcg->weight = (unsigned int)val;
34d0f179 441
3e252066 442 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
34d0f179
GJ
443 pn = blkio_policy_search_node(blkcg, blkg->dev);
444
445 if (pn)
446 continue;
447
3e252066
VG
448 list_for_each_entry(blkiop, &blkio_list, list)
449 blkiop->ops.blkio_update_group_weight_fn(blkg,
450 blkcg->weight);
3e252066 451 }
f8d461d6 452 spin_unlock_irq(&blkcg->lock);
bcf4dd43 453 spin_unlock(&blkio_list_lock);
31e4c28d
VG
454 return 0;
455}
456
303a3acb 457static int
84c124da 458blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
303a3acb
DS
459{
460 struct blkio_cgroup *blkcg;
461 struct blkio_group *blkg;
812df48d 462 struct blkio_group_stats *stats;
303a3acb 463 struct hlist_node *n;
cdc1184c
DS
464 uint64_t queued[BLKIO_STAT_TOTAL];
465 int i;
812df48d
DS
466#ifdef CONFIG_DEBUG_BLK_CGROUP
467 bool idling, waiting, empty;
468 unsigned long long now = sched_clock();
469#endif
303a3acb
DS
470
471 blkcg = cgroup_to_blkio_cgroup(cgroup);
472 spin_lock_irq(&blkcg->lock);
473 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
474 spin_lock(&blkg->stats_lock);
812df48d
DS
475 stats = &blkg->stats;
476#ifdef CONFIG_DEBUG_BLK_CGROUP
477 idling = blkio_blkg_idling(stats);
478 waiting = blkio_blkg_waiting(stats);
479 empty = blkio_blkg_empty(stats);
480#endif
cdc1184c 481 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
812df48d
DS
482 queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
483 memset(stats, 0, sizeof(struct blkio_group_stats));
cdc1184c 484 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
812df48d
DS
485 stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
486#ifdef CONFIG_DEBUG_BLK_CGROUP
487 if (idling) {
488 blkio_mark_blkg_idling(stats);
489 stats->start_idle_time = now;
490 }
491 if (waiting) {
492 blkio_mark_blkg_waiting(stats);
493 stats->start_group_wait_time = now;
494 }
495 if (empty) {
496 blkio_mark_blkg_empty(stats);
497 stats->start_empty_time = now;
498 }
499#endif
303a3acb
DS
500 spin_unlock(&blkg->stats_lock);
501 }
502 spin_unlock_irq(&blkcg->lock);
503 return 0;
504}
505
84c124da
DS
506static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
507 int chars_left, bool diskname_only)
303a3acb 508{
84c124da 509 snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
303a3acb
DS
510 chars_left -= strlen(str);
511 if (chars_left <= 0) {
512 printk(KERN_WARNING
513 "Possibly incorrect cgroup stat display format");
514 return;
515 }
84c124da
DS
516 if (diskname_only)
517 return;
303a3acb 518 switch (type) {
84c124da 519 case BLKIO_STAT_READ:
303a3acb
DS
520 strlcat(str, " Read", chars_left);
521 break;
84c124da 522 case BLKIO_STAT_WRITE:
303a3acb
DS
523 strlcat(str, " Write", chars_left);
524 break;
84c124da 525 case BLKIO_STAT_SYNC:
303a3acb
DS
526 strlcat(str, " Sync", chars_left);
527 break;
84c124da 528 case BLKIO_STAT_ASYNC:
303a3acb
DS
529 strlcat(str, " Async", chars_left);
530 break;
84c124da 531 case BLKIO_STAT_TOTAL:
303a3acb
DS
532 strlcat(str, " Total", chars_left);
533 break;
534 default:
535 strlcat(str, " Invalid", chars_left);
536 }
537}
538
84c124da
DS
539static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
540 struct cgroup_map_cb *cb, dev_t dev)
541{
542 blkio_get_key_name(0, dev, str, chars_left, true);
543 cb->fill(cb, str, val);
544 return val;
545}
303a3acb 546
84c124da
DS
547/* This should be called with blkg->stats_lock held */
548static uint64_t blkio_get_stat(struct blkio_group *blkg,
549 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
303a3acb
DS
550{
551 uint64_t disk_total;
552 char key_str[MAX_KEY_LEN];
84c124da
DS
553 enum stat_sub_type sub_type;
554
555 if (type == BLKIO_STAT_TIME)
556 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
557 blkg->stats.time, cb, dev);
558 if (type == BLKIO_STAT_SECTORS)
559 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
560 blkg->stats.sectors, cb, dev);
561#ifdef CONFIG_DEBUG_BLK_CGROUP
cdc1184c
DS
562 if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
563 uint64_t sum = blkg->stats.avg_queue_size_sum;
564 uint64_t samples = blkg->stats.avg_queue_size_samples;
565 if (samples)
566 do_div(sum, samples);
567 else
568 sum = 0;
569 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
570 }
812df48d
DS
571 if (type == BLKIO_STAT_GROUP_WAIT_TIME)
572 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
573 blkg->stats.group_wait_time, cb, dev);
574 if (type == BLKIO_STAT_IDLE_TIME)
575 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
576 blkg->stats.idle_time, cb, dev);
577 if (type == BLKIO_STAT_EMPTY_TIME)
578 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
579 blkg->stats.empty_time, cb, dev);
84c124da
DS
580 if (type == BLKIO_STAT_DEQUEUE)
581 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
582 blkg->stats.dequeue, cb, dev);
583#endif
303a3acb 584
84c124da
DS
585 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
586 sub_type++) {
587 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
588 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
303a3acb 589 }
84c124da
DS
590 disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
591 blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
592 blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
303a3acb
DS
593 cb->fill(cb, key_str, disk_total);
594 return disk_total;
595}
596
84c124da 597#define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \
22084190 598static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
303a3acb 599 struct cftype *cftype, struct cgroup_map_cb *cb) \
22084190
VG
600{ \
601 struct blkio_cgroup *blkcg; \
602 struct blkio_group *blkg; \
603 struct hlist_node *n; \
303a3acb 604 uint64_t cgroup_total = 0; \
22084190
VG
605 \
606 if (!cgroup_lock_live_group(cgroup)) \
607 return -ENODEV; \
608 \
609 blkcg = cgroup_to_blkio_cgroup(cgroup); \
610 rcu_read_lock(); \
611 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
303a3acb
DS
612 if (blkg->dev) { \
613 spin_lock_irq(&blkg->stats_lock); \
84c124da
DS
614 cgroup_total += blkio_get_stat(blkg, cb, \
615 blkg->dev, type); \
303a3acb
DS
616 spin_unlock_irq(&blkg->stats_lock); \
617 } \
22084190 618 } \
303a3acb
DS
619 if (show_total) \
620 cb->fill(cb, "Total", cgroup_total); \
22084190
VG
621 rcu_read_unlock(); \
622 cgroup_unlock(); \
623 return 0; \
624}
625
84c124da
DS
626SHOW_FUNCTION_PER_GROUP(time, BLKIO_STAT_TIME, 0);
627SHOW_FUNCTION_PER_GROUP(sectors, BLKIO_STAT_SECTORS, 0);
628SHOW_FUNCTION_PER_GROUP(io_service_bytes, BLKIO_STAT_SERVICE_BYTES, 1);
629SHOW_FUNCTION_PER_GROUP(io_serviced, BLKIO_STAT_SERVICED, 1);
630SHOW_FUNCTION_PER_GROUP(io_service_time, BLKIO_STAT_SERVICE_TIME, 1);
631SHOW_FUNCTION_PER_GROUP(io_wait_time, BLKIO_STAT_WAIT_TIME, 1);
812d4026 632SHOW_FUNCTION_PER_GROUP(io_merged, BLKIO_STAT_MERGED, 1);
cdc1184c 633SHOW_FUNCTION_PER_GROUP(io_queued, BLKIO_STAT_QUEUED, 1);
22084190 634#ifdef CONFIG_DEBUG_BLK_CGROUP
84c124da 635SHOW_FUNCTION_PER_GROUP(dequeue, BLKIO_STAT_DEQUEUE, 0);
cdc1184c 636SHOW_FUNCTION_PER_GROUP(avg_queue_size, BLKIO_STAT_AVG_QUEUE_SIZE, 0);
812df48d
DS
637SHOW_FUNCTION_PER_GROUP(group_wait_time, BLKIO_STAT_GROUP_WAIT_TIME, 0);
638SHOW_FUNCTION_PER_GROUP(idle_time, BLKIO_STAT_IDLE_TIME, 0);
639SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0);
22084190
VG
640#endif
641#undef SHOW_FUNCTION_PER_GROUP
642
34d0f179
GJ
643static int blkio_check_dev_num(dev_t dev)
644{
645 int part = 0;
646 struct gendisk *disk;
647
648 disk = get_gendisk(dev, &part);
649 if (!disk || part)
650 return -ENODEV;
651
652 return 0;
653}
654
655static int blkio_policy_parse_and_set(char *buf,
656 struct blkio_policy_node *newpn)
657{
658 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
659 int ret;
660 unsigned long major, minor, temp;
661 int i = 0;
662 dev_t dev;
663
664 memset(s, 0, sizeof(s));
665
666 while ((p = strsep(&buf, " ")) != NULL) {
667 if (!*p)
668 continue;
669
670 s[i++] = p;
671
672 /* Prevent from inputing too many things */
673 if (i == 3)
674 break;
675 }
676
677 if (i != 2)
678 return -EINVAL;
679
680 p = strsep(&s[0], ":");
681 if (p != NULL)
682 major_s = p;
683 else
684 return -EINVAL;
685
686 minor_s = s[0];
687 if (!minor_s)
688 return -EINVAL;
689
690 ret = strict_strtoul(major_s, 10, &major);
691 if (ret)
692 return -EINVAL;
693
694 ret = strict_strtoul(minor_s, 10, &minor);
695 if (ret)
696 return -EINVAL;
697
698 dev = MKDEV(major, minor);
699
700 ret = blkio_check_dev_num(dev);
701 if (ret)
702 return ret;
703
704 newpn->dev = dev;
705
706 if (s[1] == NULL)
707 return -EINVAL;
708
709 ret = strict_strtoul(s[1], 10, &temp);
710 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
711 temp > BLKIO_WEIGHT_MAX)
712 return -EINVAL;
713
714 newpn->weight = temp;
715
716 return 0;
717}
718
719unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
720 dev_t dev)
721{
722 struct blkio_policy_node *pn;
723
724 pn = blkio_policy_search_node(blkcg, dev);
725 if (pn)
726 return pn->weight;
727 else
728 return blkcg->weight;
729}
730EXPORT_SYMBOL_GPL(blkcg_get_weight);
731
732
733static int blkiocg_weight_device_write(struct cgroup *cgrp, struct cftype *cft,
734 const char *buffer)
735{
736 int ret = 0;
737 char *buf;
738 struct blkio_policy_node *newpn, *pn;
739 struct blkio_cgroup *blkcg;
740 struct blkio_group *blkg;
741 int keep_newpn = 0;
742 struct hlist_node *n;
743 struct blkio_policy_type *blkiop;
744
745 buf = kstrdup(buffer, GFP_KERNEL);
746 if (!buf)
747 return -ENOMEM;
748
749 newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
750 if (!newpn) {
751 ret = -ENOMEM;
752 goto free_buf;
753 }
754
755 ret = blkio_policy_parse_and_set(buf, newpn);
756 if (ret)
757 goto free_newpn;
758
759 blkcg = cgroup_to_blkio_cgroup(cgrp);
760
761 spin_lock_irq(&blkcg->lock);
762
763 pn = blkio_policy_search_node(blkcg, newpn->dev);
764 if (!pn) {
765 if (newpn->weight != 0) {
766 blkio_policy_insert_node(blkcg, newpn);
767 keep_newpn = 1;
768 }
769 spin_unlock_irq(&blkcg->lock);
770 goto update_io_group;
771 }
772
773 if (newpn->weight == 0) {
774 /* weight == 0 means deleteing a specific weight */
775 blkio_policy_delete_node(pn);
776 spin_unlock_irq(&blkcg->lock);
777 goto update_io_group;
778 }
779 spin_unlock_irq(&blkcg->lock);
780
781 pn->weight = newpn->weight;
782
783update_io_group:
784 /* update weight for each cfqg */
785 spin_lock(&blkio_list_lock);
786 spin_lock_irq(&blkcg->lock);
787
788 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
789 if (newpn->dev == blkg->dev) {
790 list_for_each_entry(blkiop, &blkio_list, list)
791 blkiop->ops.blkio_update_group_weight_fn(blkg,
792 newpn->weight ?
793 newpn->weight :
794 blkcg->weight);
795 }
796 }
797
798 spin_unlock_irq(&blkcg->lock);
799 spin_unlock(&blkio_list_lock);
800
801free_newpn:
802 if (!keep_newpn)
803 kfree(newpn);
804free_buf:
805 kfree(buf);
806 return ret;
807}
808
809static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft,
810 struct seq_file *m)
811{
812 struct blkio_cgroup *blkcg;
813 struct blkio_policy_node *pn;
814
815 seq_printf(m, "dev\tweight\n");
816
817 blkcg = cgroup_to_blkio_cgroup(cgrp);
818 if (list_empty(&blkcg->policy_list))
819 goto out;
820
821 spin_lock_irq(&blkcg->lock);
822 list_for_each_entry(pn, &blkcg->policy_list, node) {
823 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
824 MINOR(pn->dev), pn->weight);
825 }
826 spin_unlock_irq(&blkcg->lock);
827
828out:
829 return 0;
830}
831
31e4c28d 832struct cftype blkio_files[] = {
34d0f179
GJ
833 {
834 .name = "weight_device",
835 .read_seq_string = blkiocg_weight_device_read,
836 .write_string = blkiocg_weight_device_write,
837 .max_write_len = 256,
838 },
31e4c28d
VG
839 {
840 .name = "weight",
841 .read_u64 = blkiocg_weight_read,
842 .write_u64 = blkiocg_weight_write,
843 },
22084190
VG
844 {
845 .name = "time",
303a3acb 846 .read_map = blkiocg_time_read,
22084190
VG
847 },
848 {
849 .name = "sectors",
303a3acb 850 .read_map = blkiocg_sectors_read,
303a3acb
DS
851 },
852 {
853 .name = "io_service_bytes",
854 .read_map = blkiocg_io_service_bytes_read,
303a3acb
DS
855 },
856 {
857 .name = "io_serviced",
858 .read_map = blkiocg_io_serviced_read,
303a3acb
DS
859 },
860 {
861 .name = "io_service_time",
862 .read_map = blkiocg_io_service_time_read,
303a3acb
DS
863 },
864 {
865 .name = "io_wait_time",
866 .read_map = blkiocg_io_wait_time_read,
84c124da 867 },
812d4026
DS
868 {
869 .name = "io_merged",
870 .read_map = blkiocg_io_merged_read,
871 },
cdc1184c
DS
872 {
873 .name = "io_queued",
874 .read_map = blkiocg_io_queued_read,
875 },
84c124da
DS
876 {
877 .name = "reset_stats",
878 .write_u64 = blkiocg_reset_stats,
22084190
VG
879 },
880#ifdef CONFIG_DEBUG_BLK_CGROUP
cdc1184c
DS
881 {
882 .name = "avg_queue_size",
883 .read_map = blkiocg_avg_queue_size_read,
884 },
812df48d
DS
885 {
886 .name = "group_wait_time",
887 .read_map = blkiocg_group_wait_time_read,
888 },
889 {
890 .name = "idle_time",
891 .read_map = blkiocg_idle_time_read,
892 },
893 {
894 .name = "empty_time",
895 .read_map = blkiocg_empty_time_read,
896 },
cdc1184c 897 {
22084190 898 .name = "dequeue",
303a3acb 899 .read_map = blkiocg_dequeue_read,
cdc1184c 900 },
22084190 901#endif
31e4c28d
VG
902};
903
904static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
905{
906 return cgroup_add_files(cgroup, subsys, blkio_files,
907 ARRAY_SIZE(blkio_files));
908}
909
910static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
911{
912 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
b1c35769
VG
913 unsigned long flags;
914 struct blkio_group *blkg;
915 void *key;
3e252066 916 struct blkio_policy_type *blkiop;
34d0f179 917 struct blkio_policy_node *pn, *pntmp;
b1c35769
VG
918
919 rcu_read_lock();
920remove_entry:
921 spin_lock_irqsave(&blkcg->lock, flags);
922
923 if (hlist_empty(&blkcg->blkg_list)) {
924 spin_unlock_irqrestore(&blkcg->lock, flags);
925 goto done;
926 }
927
928 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
929 blkcg_node);
930 key = rcu_dereference(blkg->key);
931 __blkiocg_del_blkio_group(blkg);
31e4c28d 932
b1c35769
VG
933 spin_unlock_irqrestore(&blkcg->lock, flags);
934
935 /*
936 * This blkio_group is being unlinked as associated cgroup is going
937 * away. Let all the IO controlling policies know about this event.
938 *
939 * Currently this is static call to one io controlling policy. Once
940 * we have more policies in place, we need some dynamic registration
941 * of callback function.
942 */
3e252066
VG
943 spin_lock(&blkio_list_lock);
944 list_for_each_entry(blkiop, &blkio_list, list)
945 blkiop->ops.blkio_unlink_group_fn(key, blkg);
946 spin_unlock(&blkio_list_lock);
b1c35769 947 goto remove_entry;
34d0f179 948
b1c35769 949done:
34d0f179
GJ
950 list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
951 blkio_policy_delete_node(pn);
952 kfree(pn);
953 }
31e4c28d 954 free_css_id(&blkio_subsys, &blkcg->css);
b1c35769 955 rcu_read_unlock();
67523c48
BB
956 if (blkcg != &blkio_root_cgroup)
957 kfree(blkcg);
31e4c28d
VG
958}
959
960static struct cgroup_subsys_state *
961blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
962{
963 struct blkio_cgroup *blkcg, *parent_blkcg;
964
965 if (!cgroup->parent) {
966 blkcg = &blkio_root_cgroup;
967 goto done;
968 }
969
970 /* Currently we do not support hierarchy deeper than two level (0,1) */
971 parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
972 if (css_depth(&parent_blkcg->css) > 0)
973 return ERR_PTR(-EINVAL);
974
975 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
976 if (!blkcg)
977 return ERR_PTR(-ENOMEM);
978
979 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
980done:
981 spin_lock_init(&blkcg->lock);
982 INIT_HLIST_HEAD(&blkcg->blkg_list);
983
34d0f179 984 INIT_LIST_HEAD(&blkcg->policy_list);
31e4c28d
VG
985 return &blkcg->css;
986}
987
988/*
989 * We cannot support shared io contexts, as we have no mean to support
990 * two tasks with the same ioc in two different groups without major rework
991 * of the main cic data structures. For now we allow a task to change
992 * its cgroup only if it's the only owner of its ioc.
993 */
994static int blkiocg_can_attach(struct cgroup_subsys *subsys,
995 struct cgroup *cgroup, struct task_struct *tsk,
996 bool threadgroup)
997{
998 struct io_context *ioc;
999 int ret = 0;
1000
1001 /* task_lock() is needed to avoid races with exit_io_context() */
1002 task_lock(tsk);
1003 ioc = tsk->io_context;
1004 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1005 ret = -EINVAL;
1006 task_unlock(tsk);
1007
1008 return ret;
1009}
1010
1011static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1012 struct cgroup *prev, struct task_struct *tsk,
1013 bool threadgroup)
1014{
1015 struct io_context *ioc;
1016
1017 task_lock(tsk);
1018 ioc = tsk->io_context;
1019 if (ioc)
1020 ioc->cgroup_changed = 1;
1021 task_unlock(tsk);
1022}
1023
3e252066
VG
1024void blkio_policy_register(struct blkio_policy_type *blkiop)
1025{
1026 spin_lock(&blkio_list_lock);
1027 list_add_tail(&blkiop->list, &blkio_list);
1028 spin_unlock(&blkio_list_lock);
1029}
1030EXPORT_SYMBOL_GPL(blkio_policy_register);
1031
1032void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1033{
1034 spin_lock(&blkio_list_lock);
1035 list_del_init(&blkiop->list);
1036 spin_unlock(&blkio_list_lock);
1037}
1038EXPORT_SYMBOL_GPL(blkio_policy_unregister);
67523c48
BB
1039
1040static int __init init_cgroup_blkio(void)
1041{
1042 return cgroup_load_subsys(&blkio_subsys);
1043}
1044
1045static void __exit exit_cgroup_blkio(void)
1046{
1047 cgroup_unload_subsys(&blkio_subsys);
1048}
1049
1050module_init(init_cgroup_blkio);
1051module_exit(exit_cgroup_blkio);
1052MODULE_LICENSE("GPL");