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