2 * Common Block IO controller cgroup interface
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
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 "blk-cgroup.h"
20 #include <linux/genhd.h>
22 #define MAX_KEY_LEN 100
24 static DEFINE_SPINLOCK(blkio_list_lock
);
25 static LIST_HEAD(blkio_list
);
27 struct blkio_cgroup blkio_root_cgroup
= { .weight
= 2*BLKIO_WEIGHT_DEFAULT
};
28 EXPORT_SYMBOL_GPL(blkio_root_cgroup
);
30 static struct cgroup_subsys_state
*blkiocg_create(struct cgroup_subsys
*,
32 static int blkiocg_can_attach(struct cgroup_subsys
*, struct cgroup
*,
33 struct task_struct
*, bool);
34 static void blkiocg_attach(struct cgroup_subsys
*, struct cgroup
*,
35 struct cgroup
*, struct task_struct
*, bool);
36 static void blkiocg_destroy(struct cgroup_subsys
*, struct cgroup
*);
37 static int blkiocg_populate(struct cgroup_subsys
*, struct cgroup
*);
39 struct cgroup_subsys blkio_subsys
= {
41 .create
= blkiocg_create
,
42 .can_attach
= blkiocg_can_attach
,
43 .attach
= blkiocg_attach
,
44 .destroy
= blkiocg_destroy
,
45 .populate
= blkiocg_populate
,
46 #ifdef CONFIG_BLK_CGROUP
47 /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
48 .subsys_id
= blkio_subsys_id
,
51 .module
= THIS_MODULE
,
53 EXPORT_SYMBOL_GPL(blkio_subsys
);
55 static inline void blkio_policy_insert_node(struct blkio_cgroup
*blkcg
,
56 struct blkio_policy_node
*pn
)
58 list_add(&pn
->node
, &blkcg
->policy_list
);
61 /* Must be called with blkcg->lock held */
62 static inline void blkio_policy_delete_node(struct blkio_policy_node
*pn
)
67 /* Must be called with blkcg->lock held */
68 static struct blkio_policy_node
*
69 blkio_policy_search_node(const struct blkio_cgroup
*blkcg
, dev_t dev
)
71 struct blkio_policy_node
*pn
;
73 list_for_each_entry(pn
, &blkcg
->policy_list
, node
) {
81 struct blkio_cgroup
*cgroup_to_blkio_cgroup(struct cgroup
*cgroup
)
83 return container_of(cgroup_subsys_state(cgroup
, blkio_subsys_id
),
84 struct blkio_cgroup
, css
);
86 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup
);
88 void blkio_group_init(struct blkio_group
*blkg
)
90 spin_lock_init(&blkg
->stats_lock
);
92 EXPORT_SYMBOL_GPL(blkio_group_init
);
95 * Add to the appropriate stat variable depending on the request type.
96 * This should be called with the blkg->stats_lock held.
98 static void blkio_add_stat(uint64_t *stat
, uint64_t add
, bool direction
,
102 stat
[BLKIO_STAT_WRITE
] += add
;
104 stat
[BLKIO_STAT_READ
] += add
;
106 stat
[BLKIO_STAT_SYNC
] += add
;
108 stat
[BLKIO_STAT_ASYNC
] += add
;
112 * Decrements the appropriate stat variable if non-zero depending on the
113 * request type. Panics on value being zero.
114 * This should be called with the blkg->stats_lock held.
116 static void blkio_check_and_dec_stat(uint64_t *stat
, bool direction
, bool sync
)
119 BUG_ON(stat
[BLKIO_STAT_WRITE
] == 0);
120 stat
[BLKIO_STAT_WRITE
]--;
122 BUG_ON(stat
[BLKIO_STAT_READ
] == 0);
123 stat
[BLKIO_STAT_READ
]--;
126 BUG_ON(stat
[BLKIO_STAT_SYNC
] == 0);
127 stat
[BLKIO_STAT_SYNC
]--;
129 BUG_ON(stat
[BLKIO_STAT_ASYNC
] == 0);
130 stat
[BLKIO_STAT_ASYNC
]--;
134 #ifdef CONFIG_DEBUG_BLK_CGROUP
135 /* This should be called with the blkg->stats_lock held. */
136 static void blkio_set_start_group_wait_time(struct blkio_group
*blkg
,
137 struct blkio_group
*curr_blkg
)
139 if (blkio_blkg_waiting(&blkg
->stats
))
141 if (blkg
== curr_blkg
)
143 blkg
->stats
.start_group_wait_time
= sched_clock();
144 blkio_mark_blkg_waiting(&blkg
->stats
);
147 /* This should be called with the blkg->stats_lock held. */
148 static void blkio_update_group_wait_time(struct blkio_group_stats
*stats
)
150 unsigned long long now
;
152 if (!blkio_blkg_waiting(stats
))
156 if (time_after64(now
, stats
->start_group_wait_time
))
157 stats
->group_wait_time
+= now
- stats
->start_group_wait_time
;
158 blkio_clear_blkg_waiting(stats
);
161 /* This should be called with the blkg->stats_lock held. */
162 static void blkio_end_empty_time(struct blkio_group_stats
*stats
)
164 unsigned long long now
;
166 if (!blkio_blkg_empty(stats
))
170 if (time_after64(now
, stats
->start_empty_time
))
171 stats
->empty_time
+= now
- stats
->start_empty_time
;
172 blkio_clear_blkg_empty(stats
);
175 void blkiocg_update_set_idle_time_stats(struct blkio_group
*blkg
)
179 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
180 BUG_ON(blkio_blkg_idling(&blkg
->stats
));
181 blkg
->stats
.start_idle_time
= sched_clock();
182 blkio_mark_blkg_idling(&blkg
->stats
);
183 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
185 EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats
);
187 void blkiocg_update_idle_time_stats(struct blkio_group
*blkg
)
190 unsigned long long now
;
191 struct blkio_group_stats
*stats
;
193 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
194 stats
= &blkg
->stats
;
195 if (blkio_blkg_idling(stats
)) {
197 if (time_after64(now
, stats
->start_idle_time
))
198 stats
->idle_time
+= now
- stats
->start_idle_time
;
199 blkio_clear_blkg_idling(stats
);
201 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
203 EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats
);
205 void blkiocg_update_set_active_queue_stats(struct blkio_group
*blkg
)
208 struct blkio_group_stats
*stats
;
210 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
211 stats
= &blkg
->stats
;
212 stats
->avg_queue_size_sum
+=
213 stats
->stat_arr
[BLKIO_STAT_QUEUED
][BLKIO_STAT_READ
] +
214 stats
->stat_arr
[BLKIO_STAT_QUEUED
][BLKIO_STAT_WRITE
];
215 stats
->avg_queue_size_samples
++;
216 blkio_update_group_wait_time(stats
);
217 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
219 EXPORT_SYMBOL_GPL(blkiocg_update_set_active_queue_stats
);
221 static inline void blkio_set_start_group_wait_time(struct blkio_group
*blkg
,
222 struct blkio_group
*curr_blkg
) {}
223 static inline void blkio_end_empty_time(struct blkio_group_stats
*stats
) {}
226 void blkiocg_update_request_add_stats(struct blkio_group
*blkg
,
227 struct blkio_group
*curr_blkg
, bool direction
,
232 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
233 blkio_add_stat(blkg
->stats
.stat_arr
[BLKIO_STAT_QUEUED
], 1, direction
,
235 blkio_end_empty_time(&blkg
->stats
);
236 blkio_set_start_group_wait_time(blkg
, curr_blkg
);
237 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
239 EXPORT_SYMBOL_GPL(blkiocg_update_request_add_stats
);
241 void blkiocg_update_request_remove_stats(struct blkio_group
*blkg
,
242 bool direction
, bool sync
)
246 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
247 blkio_check_and_dec_stat(blkg
->stats
.stat_arr
[BLKIO_STAT_QUEUED
],
249 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
251 EXPORT_SYMBOL_GPL(blkiocg_update_request_remove_stats
);
253 void blkiocg_update_timeslice_used(struct blkio_group
*blkg
, unsigned long time
)
257 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
258 blkg
->stats
.time
+= time
;
259 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
261 EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used
);
263 void blkiocg_set_start_empty_time(struct blkio_group
*blkg
, bool ignore
)
266 struct blkio_group_stats
*stats
;
268 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
269 stats
= &blkg
->stats
;
271 if (stats
->stat_arr
[BLKIO_STAT_QUEUED
][BLKIO_STAT_READ
] ||
272 stats
->stat_arr
[BLKIO_STAT_QUEUED
][BLKIO_STAT_WRITE
]) {
273 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
278 * If ignore is set, we do not panic on the empty flag being set
279 * already. This is to avoid cases where there are superfluous timeslice
280 * complete events (for eg., forced_dispatch in CFQ) when no IOs are
281 * served which could result in triggering the empty check incorrectly.
283 BUG_ON(!ignore
&& blkio_blkg_empty(stats
));
284 stats
->start_empty_time
= sched_clock();
285 blkio_mark_blkg_empty(stats
);
286 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
288 EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time
);
290 void blkiocg_update_dispatch_stats(struct blkio_group
*blkg
,
291 uint64_t bytes
, bool direction
, bool sync
)
293 struct blkio_group_stats
*stats
;
296 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
297 stats
= &blkg
->stats
;
298 stats
->sectors
+= bytes
>> 9;
299 blkio_add_stat(stats
->stat_arr
[BLKIO_STAT_SERVICED
], 1, direction
,
301 blkio_add_stat(stats
->stat_arr
[BLKIO_STAT_SERVICE_BYTES
], bytes
,
303 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
305 EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats
);
307 void blkiocg_update_completion_stats(struct blkio_group
*blkg
,
308 uint64_t start_time
, uint64_t io_start_time
, bool direction
, bool sync
)
310 struct blkio_group_stats
*stats
;
312 unsigned long long now
= sched_clock();
314 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
315 stats
= &blkg
->stats
;
316 if (time_after64(now
, io_start_time
))
317 blkio_add_stat(stats
->stat_arr
[BLKIO_STAT_SERVICE_TIME
],
318 now
- io_start_time
, direction
, sync
);
319 if (time_after64(io_start_time
, start_time
))
320 blkio_add_stat(stats
->stat_arr
[BLKIO_STAT_WAIT_TIME
],
321 io_start_time
- start_time
, direction
, sync
);
322 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
324 EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats
);
326 void blkiocg_update_io_merged_stats(struct blkio_group
*blkg
, bool direction
,
331 spin_lock_irqsave(&blkg
->stats_lock
, flags
);
332 blkio_add_stat(blkg
->stats
.stat_arr
[BLKIO_STAT_MERGED
], 1, direction
,
334 spin_unlock_irqrestore(&blkg
->stats_lock
, flags
);
336 EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats
);
338 void blkiocg_add_blkio_group(struct blkio_cgroup
*blkcg
,
339 struct blkio_group
*blkg
, void *key
, dev_t dev
)
343 spin_lock_irqsave(&blkcg
->lock
, flags
);
344 rcu_assign_pointer(blkg
->key
, key
);
345 blkg
->blkcg_id
= css_id(&blkcg
->css
);
346 hlist_add_head_rcu(&blkg
->blkcg_node
, &blkcg
->blkg_list
);
347 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
348 #ifdef CONFIG_DEBUG_BLK_CGROUP
349 /* Need to take css reference ? */
350 cgroup_path(blkcg
->css
.cgroup
, blkg
->path
, sizeof(blkg
->path
));
354 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group
);
356 static void __blkiocg_del_blkio_group(struct blkio_group
*blkg
)
358 hlist_del_init_rcu(&blkg
->blkcg_node
);
363 * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
364 * indicating that blk_group was unhashed by the time we got to it.
366 int blkiocg_del_blkio_group(struct blkio_group
*blkg
)
368 struct blkio_cgroup
*blkcg
;
370 struct cgroup_subsys_state
*css
;
374 css
= css_lookup(&blkio_subsys
, blkg
->blkcg_id
);
378 blkcg
= container_of(css
, struct blkio_cgroup
, css
);
379 spin_lock_irqsave(&blkcg
->lock
, flags
);
380 if (!hlist_unhashed(&blkg
->blkcg_node
)) {
381 __blkiocg_del_blkio_group(blkg
);
384 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
389 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group
);
391 /* called under rcu_read_lock(). */
392 struct blkio_group
*blkiocg_lookup_group(struct blkio_cgroup
*blkcg
, void *key
)
394 struct blkio_group
*blkg
;
395 struct hlist_node
*n
;
398 hlist_for_each_entry_rcu(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
406 EXPORT_SYMBOL_GPL(blkiocg_lookup_group
);
408 #define SHOW_FUNCTION(__VAR) \
409 static u64 blkiocg_##__VAR##_read(struct cgroup *cgroup, \
410 struct cftype *cftype) \
412 struct blkio_cgroup *blkcg; \
414 blkcg = cgroup_to_blkio_cgroup(cgroup); \
415 return (u64)blkcg->__VAR; \
418 SHOW_FUNCTION(weight
);
422 blkiocg_weight_write(struct cgroup
*cgroup
, struct cftype
*cftype
, u64 val
)
424 struct blkio_cgroup
*blkcg
;
425 struct blkio_group
*blkg
;
426 struct hlist_node
*n
;
427 struct blkio_policy_type
*blkiop
;
428 struct blkio_policy_node
*pn
;
430 if (val
< BLKIO_WEIGHT_MIN
|| val
> BLKIO_WEIGHT_MAX
)
433 blkcg
= cgroup_to_blkio_cgroup(cgroup
);
434 spin_lock(&blkio_list_lock
);
435 spin_lock_irq(&blkcg
->lock
);
436 blkcg
->weight
= (unsigned int)val
;
438 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
439 pn
= blkio_policy_search_node(blkcg
, blkg
->dev
);
444 list_for_each_entry(blkiop
, &blkio_list
, list
)
445 blkiop
->ops
.blkio_update_group_weight_fn(blkg
,
448 spin_unlock_irq(&blkcg
->lock
);
449 spin_unlock(&blkio_list_lock
);
454 blkiocg_reset_stats(struct cgroup
*cgroup
, struct cftype
*cftype
, u64 val
)
456 struct blkio_cgroup
*blkcg
;
457 struct blkio_group
*blkg
;
458 struct blkio_group_stats
*stats
;
459 struct hlist_node
*n
;
460 uint64_t queued
[BLKIO_STAT_TOTAL
];
462 #ifdef CONFIG_DEBUG_BLK_CGROUP
463 bool idling
, waiting
, empty
;
464 unsigned long long now
= sched_clock();
467 blkcg
= cgroup_to_blkio_cgroup(cgroup
);
468 spin_lock_irq(&blkcg
->lock
);
469 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
470 spin_lock(&blkg
->stats_lock
);
471 stats
= &blkg
->stats
;
472 #ifdef CONFIG_DEBUG_BLK_CGROUP
473 idling
= blkio_blkg_idling(stats
);
474 waiting
= blkio_blkg_waiting(stats
);
475 empty
= blkio_blkg_empty(stats
);
477 for (i
= 0; i
< BLKIO_STAT_TOTAL
; i
++)
478 queued
[i
] = stats
->stat_arr
[BLKIO_STAT_QUEUED
][i
];
479 memset(stats
, 0, sizeof(struct blkio_group_stats
));
480 for (i
= 0; i
< BLKIO_STAT_TOTAL
; i
++)
481 stats
->stat_arr
[BLKIO_STAT_QUEUED
][i
] = queued
[i
];
482 #ifdef CONFIG_DEBUG_BLK_CGROUP
484 blkio_mark_blkg_idling(stats
);
485 stats
->start_idle_time
= now
;
488 blkio_mark_blkg_waiting(stats
);
489 stats
->start_group_wait_time
= now
;
492 blkio_mark_blkg_empty(stats
);
493 stats
->start_empty_time
= now
;
496 spin_unlock(&blkg
->stats_lock
);
498 spin_unlock_irq(&blkcg
->lock
);
502 static void blkio_get_key_name(enum stat_sub_type type
, dev_t dev
, char *str
,
503 int chars_left
, bool diskname_only
)
505 snprintf(str
, chars_left
, "%d:%d", MAJOR(dev
), MINOR(dev
));
506 chars_left
-= strlen(str
);
507 if (chars_left
<= 0) {
509 "Possibly incorrect cgroup stat display format");
515 case BLKIO_STAT_READ
:
516 strlcat(str
, " Read", chars_left
);
518 case BLKIO_STAT_WRITE
:
519 strlcat(str
, " Write", chars_left
);
521 case BLKIO_STAT_SYNC
:
522 strlcat(str
, " Sync", chars_left
);
524 case BLKIO_STAT_ASYNC
:
525 strlcat(str
, " Async", chars_left
);
527 case BLKIO_STAT_TOTAL
:
528 strlcat(str
, " Total", chars_left
);
531 strlcat(str
, " Invalid", chars_left
);
535 static uint64_t blkio_fill_stat(char *str
, int chars_left
, uint64_t val
,
536 struct cgroup_map_cb
*cb
, dev_t dev
)
538 blkio_get_key_name(0, dev
, str
, chars_left
, true);
539 cb
->fill(cb
, str
, val
);
543 /* This should be called with blkg->stats_lock held */
544 static uint64_t blkio_get_stat(struct blkio_group
*blkg
,
545 struct cgroup_map_cb
*cb
, dev_t dev
, enum stat_type type
)
548 char key_str
[MAX_KEY_LEN
];
549 enum stat_sub_type sub_type
;
551 if (type
== BLKIO_STAT_TIME
)
552 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1,
553 blkg
->stats
.time
, cb
, dev
);
554 if (type
== BLKIO_STAT_SECTORS
)
555 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1,
556 blkg
->stats
.sectors
, cb
, dev
);
557 #ifdef CONFIG_DEBUG_BLK_CGROUP
558 if (type
== BLKIO_STAT_AVG_QUEUE_SIZE
) {
559 uint64_t sum
= blkg
->stats
.avg_queue_size_sum
;
560 uint64_t samples
= blkg
->stats
.avg_queue_size_samples
;
562 do_div(sum
, samples
);
565 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1, sum
, cb
, dev
);
567 if (type
== BLKIO_STAT_GROUP_WAIT_TIME
)
568 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1,
569 blkg
->stats
.group_wait_time
, cb
, dev
);
570 if (type
== BLKIO_STAT_IDLE_TIME
)
571 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1,
572 blkg
->stats
.idle_time
, cb
, dev
);
573 if (type
== BLKIO_STAT_EMPTY_TIME
)
574 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1,
575 blkg
->stats
.empty_time
, cb
, dev
);
576 if (type
== BLKIO_STAT_DEQUEUE
)
577 return blkio_fill_stat(key_str
, MAX_KEY_LEN
- 1,
578 blkg
->stats
.dequeue
, cb
, dev
);
581 for (sub_type
= BLKIO_STAT_READ
; sub_type
< BLKIO_STAT_TOTAL
;
583 blkio_get_key_name(sub_type
, dev
, key_str
, MAX_KEY_LEN
, false);
584 cb
->fill(cb
, key_str
, blkg
->stats
.stat_arr
[type
][sub_type
]);
586 disk_total
= blkg
->stats
.stat_arr
[type
][BLKIO_STAT_READ
] +
587 blkg
->stats
.stat_arr
[type
][BLKIO_STAT_WRITE
];
588 blkio_get_key_name(BLKIO_STAT_TOTAL
, dev
, key_str
, MAX_KEY_LEN
, false);
589 cb
->fill(cb
, key_str
, disk_total
);
593 #define SHOW_FUNCTION_PER_GROUP(__VAR, type, show_total) \
594 static int blkiocg_##__VAR##_read(struct cgroup *cgroup, \
595 struct cftype *cftype, struct cgroup_map_cb *cb) \
597 struct blkio_cgroup *blkcg; \
598 struct blkio_group *blkg; \
599 struct hlist_node *n; \
600 uint64_t cgroup_total = 0; \
602 if (!cgroup_lock_live_group(cgroup)) \
605 blkcg = cgroup_to_blkio_cgroup(cgroup); \
607 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {\
609 spin_lock_irq(&blkg->stats_lock); \
610 cgroup_total += blkio_get_stat(blkg, cb, \
612 spin_unlock_irq(&blkg->stats_lock); \
616 cb->fill(cb, "Total", cgroup_total); \
622 SHOW_FUNCTION_PER_GROUP(time
, BLKIO_STAT_TIME
, 0);
623 SHOW_FUNCTION_PER_GROUP(sectors
, BLKIO_STAT_SECTORS
, 0);
624 SHOW_FUNCTION_PER_GROUP(io_service_bytes
, BLKIO_STAT_SERVICE_BYTES
, 1);
625 SHOW_FUNCTION_PER_GROUP(io_serviced
, BLKIO_STAT_SERVICED
, 1);
626 SHOW_FUNCTION_PER_GROUP(io_service_time
, BLKIO_STAT_SERVICE_TIME
, 1);
627 SHOW_FUNCTION_PER_GROUP(io_wait_time
, BLKIO_STAT_WAIT_TIME
, 1);
628 SHOW_FUNCTION_PER_GROUP(io_merged
, BLKIO_STAT_MERGED
, 1);
629 SHOW_FUNCTION_PER_GROUP(io_queued
, BLKIO_STAT_QUEUED
, 1);
630 #ifdef CONFIG_DEBUG_BLK_CGROUP
631 SHOW_FUNCTION_PER_GROUP(dequeue
, BLKIO_STAT_DEQUEUE
, 0);
632 SHOW_FUNCTION_PER_GROUP(avg_queue_size
, BLKIO_STAT_AVG_QUEUE_SIZE
, 0);
633 SHOW_FUNCTION_PER_GROUP(group_wait_time
, BLKIO_STAT_GROUP_WAIT_TIME
, 0);
634 SHOW_FUNCTION_PER_GROUP(idle_time
, BLKIO_STAT_IDLE_TIME
, 0);
635 SHOW_FUNCTION_PER_GROUP(empty_time
, BLKIO_STAT_EMPTY_TIME
, 0);
637 #undef SHOW_FUNCTION_PER_GROUP
639 #ifdef CONFIG_DEBUG_BLK_CGROUP
640 void blkiocg_update_dequeue_stats(struct blkio_group
*blkg
,
641 unsigned long dequeue
)
643 blkg
->stats
.dequeue
+= dequeue
;
645 EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats
);
648 static int blkio_check_dev_num(dev_t dev
)
651 struct gendisk
*disk
;
653 disk
= get_gendisk(dev
, &part
);
660 static int blkio_policy_parse_and_set(char *buf
,
661 struct blkio_policy_node
*newpn
)
663 char *s
[4], *p
, *major_s
= NULL
, *minor_s
= NULL
;
665 unsigned long major
, minor
, temp
;
669 memset(s
, 0, sizeof(s
));
671 while ((p
= strsep(&buf
, " ")) != NULL
) {
677 /* Prevent from inputing too many things */
685 p
= strsep(&s
[0], ":");
695 ret
= strict_strtoul(major_s
, 10, &major
);
699 ret
= strict_strtoul(minor_s
, 10, &minor
);
703 dev
= MKDEV(major
, minor
);
705 ret
= blkio_check_dev_num(dev
);
714 ret
= strict_strtoul(s
[1], 10, &temp
);
715 if (ret
|| (temp
< BLKIO_WEIGHT_MIN
&& temp
> 0) ||
716 temp
> BLKIO_WEIGHT_MAX
)
719 newpn
->weight
= temp
;
724 unsigned int blkcg_get_weight(struct blkio_cgroup
*blkcg
,
727 struct blkio_policy_node
*pn
;
729 pn
= blkio_policy_search_node(blkcg
, dev
);
733 return blkcg
->weight
;
735 EXPORT_SYMBOL_GPL(blkcg_get_weight
);
738 static int blkiocg_weight_device_write(struct cgroup
*cgrp
, struct cftype
*cft
,
743 struct blkio_policy_node
*newpn
, *pn
;
744 struct blkio_cgroup
*blkcg
;
745 struct blkio_group
*blkg
;
747 struct hlist_node
*n
;
748 struct blkio_policy_type
*blkiop
;
750 buf
= kstrdup(buffer
, GFP_KERNEL
);
754 newpn
= kzalloc(sizeof(*newpn
), GFP_KERNEL
);
760 ret
= blkio_policy_parse_and_set(buf
, newpn
);
764 blkcg
= cgroup_to_blkio_cgroup(cgrp
);
766 spin_lock_irq(&blkcg
->lock
);
768 pn
= blkio_policy_search_node(blkcg
, newpn
->dev
);
770 if (newpn
->weight
!= 0) {
771 blkio_policy_insert_node(blkcg
, newpn
);
774 spin_unlock_irq(&blkcg
->lock
);
775 goto update_io_group
;
778 if (newpn
->weight
== 0) {
779 /* weight == 0 means deleteing a specific weight */
780 blkio_policy_delete_node(pn
);
781 spin_unlock_irq(&blkcg
->lock
);
782 goto update_io_group
;
784 spin_unlock_irq(&blkcg
->lock
);
786 pn
->weight
= newpn
->weight
;
789 /* update weight for each cfqg */
790 spin_lock(&blkio_list_lock
);
791 spin_lock_irq(&blkcg
->lock
);
793 hlist_for_each_entry(blkg
, n
, &blkcg
->blkg_list
, blkcg_node
) {
794 if (newpn
->dev
== blkg
->dev
) {
795 list_for_each_entry(blkiop
, &blkio_list
, list
)
796 blkiop
->ops
.blkio_update_group_weight_fn(blkg
,
803 spin_unlock_irq(&blkcg
->lock
);
804 spin_unlock(&blkio_list_lock
);
814 static int blkiocg_weight_device_read(struct cgroup
*cgrp
, struct cftype
*cft
,
817 struct blkio_cgroup
*blkcg
;
818 struct blkio_policy_node
*pn
;
820 seq_printf(m
, "dev\tweight\n");
822 blkcg
= cgroup_to_blkio_cgroup(cgrp
);
823 if (list_empty(&blkcg
->policy_list
))
826 spin_lock_irq(&blkcg
->lock
);
827 list_for_each_entry(pn
, &blkcg
->policy_list
, node
) {
828 seq_printf(m
, "%u:%u\t%u\n", MAJOR(pn
->dev
),
829 MINOR(pn
->dev
), pn
->weight
);
831 spin_unlock_irq(&blkcg
->lock
);
837 struct cftype blkio_files
[] = {
839 .name
= "weight_device",
840 .read_seq_string
= blkiocg_weight_device_read
,
841 .write_string
= blkiocg_weight_device_write
,
842 .max_write_len
= 256,
846 .read_u64
= blkiocg_weight_read
,
847 .write_u64
= blkiocg_weight_write
,
851 .read_map
= blkiocg_time_read
,
855 .read_map
= blkiocg_sectors_read
,
858 .name
= "io_service_bytes",
859 .read_map
= blkiocg_io_service_bytes_read
,
862 .name
= "io_serviced",
863 .read_map
= blkiocg_io_serviced_read
,
866 .name
= "io_service_time",
867 .read_map
= blkiocg_io_service_time_read
,
870 .name
= "io_wait_time",
871 .read_map
= blkiocg_io_wait_time_read
,
875 .read_map
= blkiocg_io_merged_read
,
879 .read_map
= blkiocg_io_queued_read
,
882 .name
= "reset_stats",
883 .write_u64
= blkiocg_reset_stats
,
885 #ifdef CONFIG_DEBUG_BLK_CGROUP
887 .name
= "avg_queue_size",
888 .read_map
= blkiocg_avg_queue_size_read
,
891 .name
= "group_wait_time",
892 .read_map
= blkiocg_group_wait_time_read
,
896 .read_map
= blkiocg_idle_time_read
,
899 .name
= "empty_time",
900 .read_map
= blkiocg_empty_time_read
,
904 .read_map
= blkiocg_dequeue_read
,
909 static int blkiocg_populate(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
911 return cgroup_add_files(cgroup
, subsys
, blkio_files
,
912 ARRAY_SIZE(blkio_files
));
915 static void blkiocg_destroy(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
917 struct blkio_cgroup
*blkcg
= cgroup_to_blkio_cgroup(cgroup
);
919 struct blkio_group
*blkg
;
921 struct blkio_policy_type
*blkiop
;
922 struct blkio_policy_node
*pn
, *pntmp
;
926 spin_lock_irqsave(&blkcg
->lock
, flags
);
928 if (hlist_empty(&blkcg
->blkg_list
)) {
929 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
933 blkg
= hlist_entry(blkcg
->blkg_list
.first
, struct blkio_group
,
935 key
= rcu_dereference(blkg
->key
);
936 __blkiocg_del_blkio_group(blkg
);
938 spin_unlock_irqrestore(&blkcg
->lock
, flags
);
941 * This blkio_group is being unlinked as associated cgroup is going
942 * away. Let all the IO controlling policies know about this event.
944 * Currently this is static call to one io controlling policy. Once
945 * we have more policies in place, we need some dynamic registration
946 * of callback function.
948 spin_lock(&blkio_list_lock
);
949 list_for_each_entry(blkiop
, &blkio_list
, list
)
950 blkiop
->ops
.blkio_unlink_group_fn(key
, blkg
);
951 spin_unlock(&blkio_list_lock
);
955 list_for_each_entry_safe(pn
, pntmp
, &blkcg
->policy_list
, node
) {
956 blkio_policy_delete_node(pn
);
959 free_css_id(&blkio_subsys
, &blkcg
->css
);
961 if (blkcg
!= &blkio_root_cgroup
)
965 static struct cgroup_subsys_state
*
966 blkiocg_create(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
)
968 struct blkio_cgroup
*blkcg
, *parent_blkcg
;
970 if (!cgroup
->parent
) {
971 blkcg
= &blkio_root_cgroup
;
975 /* Currently we do not support hierarchy deeper than two level (0,1) */
976 parent_blkcg
= cgroup_to_blkio_cgroup(cgroup
->parent
);
977 if (css_depth(&parent_blkcg
->css
) > 0)
978 return ERR_PTR(-EINVAL
);
980 blkcg
= kzalloc(sizeof(*blkcg
), GFP_KERNEL
);
982 return ERR_PTR(-ENOMEM
);
984 blkcg
->weight
= BLKIO_WEIGHT_DEFAULT
;
986 spin_lock_init(&blkcg
->lock
);
987 INIT_HLIST_HEAD(&blkcg
->blkg_list
);
989 INIT_LIST_HEAD(&blkcg
->policy_list
);
994 * We cannot support shared io contexts, as we have no mean to support
995 * two tasks with the same ioc in two different groups without major rework
996 * of the main cic data structures. For now we allow a task to change
997 * its cgroup only if it's the only owner of its ioc.
999 static int blkiocg_can_attach(struct cgroup_subsys
*subsys
,
1000 struct cgroup
*cgroup
, struct task_struct
*tsk
,
1003 struct io_context
*ioc
;
1006 /* task_lock() is needed to avoid races with exit_io_context() */
1008 ioc
= tsk
->io_context
;
1009 if (ioc
&& atomic_read(&ioc
->nr_tasks
) > 1)
1016 static void blkiocg_attach(struct cgroup_subsys
*subsys
, struct cgroup
*cgroup
,
1017 struct cgroup
*prev
, struct task_struct
*tsk
,
1020 struct io_context
*ioc
;
1023 ioc
= tsk
->io_context
;
1025 ioc
->cgroup_changed
= 1;
1029 void blkio_policy_register(struct blkio_policy_type
*blkiop
)
1031 spin_lock(&blkio_list_lock
);
1032 list_add_tail(&blkiop
->list
, &blkio_list
);
1033 spin_unlock(&blkio_list_lock
);
1035 EXPORT_SYMBOL_GPL(blkio_policy_register
);
1037 void blkio_policy_unregister(struct blkio_policy_type
*blkiop
)
1039 spin_lock(&blkio_list_lock
);
1040 list_del_init(&blkiop
->list
);
1041 spin_unlock(&blkio_list_lock
);
1043 EXPORT_SYMBOL_GPL(blkio_policy_unregister
);
1045 static int __init
init_cgroup_blkio(void)
1047 return cgroup_load_subsys(&blkio_subsys
);
1050 static void __exit
exit_cgroup_blkio(void)
1052 cgroup_unload_subsys(&blkio_subsys
);
1055 module_init(init_cgroup_blkio
);
1056 module_exit(exit_cgroup_blkio
);
1057 MODULE_LICENSE("GPL");