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