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