]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - block/blk-cgroup.c
cfq: fix cfqg ref handling when BLK_CGROUP && !CFQ_GROUP_IOSCHED
[mirror_ubuntu-zesty-kernel.git] / block / blk-cgroup.c
CommitLineData
31e4c28d
VG
1/*
2 * Common Block IO controller cgroup interface
3 *
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
12 */
13#include <linux/ioprio.h>
22084190
VG
14#include <linux/seq_file.h>
15#include <linux/kdev_t.h>
9d6a986c 16#include <linux/module.h>
accee785 17#include <linux/err.h>
9195291e 18#include <linux/blkdev.h>
5a0e3ad6 19#include <linux/slab.h>
34d0f179 20#include <linux/genhd.h>
72e06c25 21#include <linux/delay.h>
9a9e8a26 22#include <linux/atomic.h>
72e06c25 23#include "blk-cgroup.h"
5efd6113 24#include "blk.h"
3e252066 25
84c124da
DS
26#define MAX_KEY_LEN 100
27
3e252066
VG
28static DEFINE_SPINLOCK(blkio_list_lock);
29static LIST_HEAD(blkio_list);
b1c35769 30
923adde1
TH
31static DEFINE_MUTEX(all_q_mutex);
32static LIST_HEAD(all_q_list);
33
1cd9e039
VG
34/* List of groups pending per cpu stats allocation */
35static DEFINE_SPINLOCK(alloc_list_lock);
36static LIST_HEAD(alloc_list);
37
38static void blkio_stat_alloc_fn(struct work_struct *);
39static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn);
40
31e4c28d 41struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
9d6a986c
VG
42EXPORT_SYMBOL_GPL(blkio_root_cgroup);
43
035d10b2
TH
44static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES];
45
67523c48
BB
46static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
47 struct cgroup *);
bb9d97b6
TH
48static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
49 struct cgroup_taskset *);
7ee9c562 50static int blkiocg_pre_destroy(struct cgroup_subsys *, struct cgroup *);
67523c48
BB
51static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
52static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
53
062a644d
VG
54/* for encoding cft->private value on file */
55#define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val))
56/* What policy owns the file, proportional or throttle */
57#define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff)
58#define BLKIOFILE_ATTR(val) ((val) & 0xffff)
59
67523c48
BB
60struct cgroup_subsys blkio_subsys = {
61 .name = "blkio",
62 .create = blkiocg_create,
bb9d97b6 63 .can_attach = blkiocg_can_attach,
7ee9c562 64 .pre_destroy = blkiocg_pre_destroy,
67523c48
BB
65 .destroy = blkiocg_destroy,
66 .populate = blkiocg_populate,
67523c48 67 .subsys_id = blkio_subsys_id,
67523c48
BB
68 .module = THIS_MODULE,
69};
70EXPORT_SYMBOL_GPL(blkio_subsys);
71
31e4c28d
VG
72struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
73{
74 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
75 struct blkio_cgroup, css);
76}
9d6a986c 77EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
31e4c28d 78
4f85cb96 79static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
70087dc3
VG
80{
81 return container_of(task_subsys_state(tsk, blkio_subsys_id),
82 struct blkio_cgroup, css);
83}
4f85cb96
TH
84
85struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
86{
87 if (bio && bio->bi_css)
88 return container_of(bio->bi_css, struct blkio_cgroup, css);
89 return task_blkio_cgroup(current);
90}
91EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
70087dc3 92
c1768268
TH
93static inline void blkio_update_group_weight(struct blkio_group *blkg,
94 int plid, unsigned int weight)
062a644d
VG
95{
96 struct blkio_policy_type *blkiop;
97
98 list_for_each_entry(blkiop, &blkio_list, list) {
99 /* If this policy does not own the blkg, do not send updates */
c1768268 100 if (blkiop->plid != plid)
062a644d
VG
101 continue;
102 if (blkiop->ops.blkio_update_group_weight_fn)
ca32aefc 103 blkiop->ops.blkio_update_group_weight_fn(blkg->q,
fe071437 104 blkg, weight);
062a644d
VG
105 }
106}
107
c1768268
TH
108static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid,
109 u64 bps, int fileid)
4c9eefa1
VG
110{
111 struct blkio_policy_type *blkiop;
112
113 list_for_each_entry(blkiop, &blkio_list, list) {
114
115 /* If this policy does not own the blkg, do not send updates */
c1768268 116 if (blkiop->plid != plid)
4c9eefa1
VG
117 continue;
118
119 if (fileid == BLKIO_THROTL_read_bps_device
120 && blkiop->ops.blkio_update_group_read_bps_fn)
ca32aefc 121 blkiop->ops.blkio_update_group_read_bps_fn(blkg->q,
fe071437 122 blkg, bps);
4c9eefa1
VG
123
124 if (fileid == BLKIO_THROTL_write_bps_device
125 && blkiop->ops.blkio_update_group_write_bps_fn)
ca32aefc 126 blkiop->ops.blkio_update_group_write_bps_fn(blkg->q,
fe071437 127 blkg, bps);
4c9eefa1
VG
128 }
129}
130
7702e8f4 131static inline void blkio_update_group_iops(struct blkio_group *blkg,
c1768268
TH
132 int plid, unsigned int iops,
133 int fileid)
7702e8f4
VG
134{
135 struct blkio_policy_type *blkiop;
136
137 list_for_each_entry(blkiop, &blkio_list, list) {
138
139 /* If this policy does not own the blkg, do not send updates */
c1768268 140 if (blkiop->plid != plid)
7702e8f4
VG
141 continue;
142
143 if (fileid == BLKIO_THROTL_read_iops_device
144 && blkiop->ops.blkio_update_group_read_iops_fn)
ca32aefc 145 blkiop->ops.blkio_update_group_read_iops_fn(blkg->q,
fe071437 146 blkg, iops);
7702e8f4
VG
147
148 if (fileid == BLKIO_THROTL_write_iops_device
149 && blkiop->ops.blkio_update_group_write_iops_fn)
ca32aefc 150 blkiop->ops.blkio_update_group_write_iops_fn(blkg->q,
fe071437 151 blkg,iops);
7702e8f4
VG
152 }
153}
154
9195291e
DS
155/*
156 * Add to the appropriate stat variable depending on the request type.
edf1b879 157 * This should be called with queue_lock held.
9195291e 158 */
84c124da
DS
159static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
160 bool sync)
9195291e 161{
84c124da
DS
162 if (direction)
163 stat[BLKIO_STAT_WRITE] += add;
9195291e 164 else
84c124da
DS
165 stat[BLKIO_STAT_READ] += add;
166 if (sync)
167 stat[BLKIO_STAT_SYNC] += add;
9195291e 168 else
84c124da 169 stat[BLKIO_STAT_ASYNC] += add;
9195291e
DS
170}
171
cdc1184c
DS
172/*
173 * Decrements the appropriate stat variable if non-zero depending on the
174 * request type. Panics on value being zero.
edf1b879 175 * This should be called with the queue_lock held.
cdc1184c
DS
176 */
177static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
178{
179 if (direction) {
180 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
181 stat[BLKIO_STAT_WRITE]--;
182 } else {
183 BUG_ON(stat[BLKIO_STAT_READ] == 0);
184 stat[BLKIO_STAT_READ]--;
185 }
186 if (sync) {
187 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
188 stat[BLKIO_STAT_SYNC]--;
189 } else {
190 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
191 stat[BLKIO_STAT_ASYNC]--;
192 }
193}
194
195#ifdef CONFIG_DEBUG_BLK_CGROUP
edf1b879 196/* This should be called with the queue_lock held. */
812df48d 197static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
c1768268
TH
198 struct blkio_policy_type *pol,
199 struct blkio_group *curr_blkg)
812df48d 200{
c1768268 201 struct blkg_policy_data *pd = blkg->pd[pol->plid];
549d3aa8
TH
202
203 if (blkio_blkg_waiting(&pd->stats))
812df48d
DS
204 return;
205 if (blkg == curr_blkg)
206 return;
549d3aa8
TH
207 pd->stats.start_group_wait_time = sched_clock();
208 blkio_mark_blkg_waiting(&pd->stats);
812df48d
DS
209}
210
edf1b879 211/* This should be called with the queue_lock held. */
812df48d
DS
212static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
213{
214 unsigned long long now;
215
216 if (!blkio_blkg_waiting(stats))
217 return;
218
219 now = sched_clock();
220 if (time_after64(now, stats->start_group_wait_time))
221 stats->group_wait_time += now - stats->start_group_wait_time;
222 blkio_clear_blkg_waiting(stats);
223}
224
edf1b879 225/* This should be called with the queue_lock held. */
812df48d
DS
226static void blkio_end_empty_time(struct blkio_group_stats *stats)
227{
228 unsigned long long now;
229
230 if (!blkio_blkg_empty(stats))
231 return;
232
233 now = sched_clock();
234 if (time_after64(now, stats->start_empty_time))
235 stats->empty_time += now - stats->start_empty_time;
236 blkio_clear_blkg_empty(stats);
237}
238
c1768268
TH
239void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
240 struct blkio_policy_type *pol)
812df48d 241{
edf1b879 242 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
812df48d 243
edf1b879
TH
244 lockdep_assert_held(blkg->q->queue_lock);
245 BUG_ON(blkio_blkg_idling(stats));
246
247 stats->start_idle_time = sched_clock();
248 blkio_mark_blkg_idling(stats);
812df48d
DS
249}
250EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
251
c1768268
TH
252void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
253 struct blkio_policy_type *pol)
812df48d 254{
edf1b879
TH
255 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
256
257 lockdep_assert_held(blkg->q->queue_lock);
812df48d 258
812df48d 259 if (blkio_blkg_idling(stats)) {
edf1b879
TH
260 unsigned long long now = sched_clock();
261
262 if (time_after64(now, stats->start_idle_time)) {
263 u64_stats_update_begin(&stats->syncp);
812df48d 264 stats->idle_time += now - stats->start_idle_time;
edf1b879
TH
265 u64_stats_update_end(&stats->syncp);
266 }
812df48d
DS
267 blkio_clear_blkg_idling(stats);
268 }
812df48d
DS
269}
270EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
271
c1768268
TH
272void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
273 struct blkio_policy_type *pol)
cdc1184c 274{
edf1b879 275 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
cdc1184c 276
edf1b879
TH
277 lockdep_assert_held(blkg->q->queue_lock);
278
279 u64_stats_update_begin(&stats->syncp);
cdc1184c
DS
280 stats->avg_queue_size_sum +=
281 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
282 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
283 stats->avg_queue_size_samples++;
812df48d 284 blkio_update_group_wait_time(stats);
edf1b879 285 u64_stats_update_end(&stats->syncp);
cdc1184c 286}
a11cdaa7
DS
287EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
288
c1768268
TH
289void blkiocg_set_start_empty_time(struct blkio_group *blkg,
290 struct blkio_policy_type *pol)
28baf442 291{
edf1b879 292 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
28baf442 293
edf1b879 294 lockdep_assert_held(blkg->q->queue_lock);
28baf442
DS
295
296 if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
edf1b879 297 stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE])
28baf442 298 return;
28baf442
DS
299
300 /*
e5ff082e
VG
301 * group is already marked empty. This can happen if cfqq got new
302 * request in parent group and moved to this group while being added
303 * to service tree. Just ignore the event and move on.
28baf442 304 */
edf1b879 305 if (blkio_blkg_empty(stats))
e5ff082e 306 return;
e5ff082e 307
28baf442
DS
308 stats->start_empty_time = sched_clock();
309 blkio_mark_blkg_empty(stats);
28baf442
DS
310}
311EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
312
a11cdaa7 313void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
c1768268
TH
314 struct blkio_policy_type *pol,
315 unsigned long dequeue)
a11cdaa7 316{
c1768268 317 struct blkg_policy_data *pd = blkg->pd[pol->plid];
549d3aa8 318
edf1b879
TH
319 lockdep_assert_held(blkg->q->queue_lock);
320
549d3aa8 321 pd->stats.dequeue += dequeue;
a11cdaa7
DS
322}
323EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
812df48d
DS
324#else
325static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
c1768268
TH
326 struct blkio_policy_type *pol,
327 struct blkio_group *curr_blkg) { }
328static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { }
cdc1184c
DS
329#endif
330
a11cdaa7 331void blkiocg_update_io_add_stats(struct blkio_group *blkg,
c1768268
TH
332 struct blkio_policy_type *pol,
333 struct blkio_group *curr_blkg, bool direction,
334 bool sync)
cdc1184c 335{
edf1b879
TH
336 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
337
338 lockdep_assert_held(blkg->q->queue_lock);
339
340 u64_stats_update_begin(&stats->syncp);
341 blkio_add_stat(stats->stat_arr[BLKIO_STAT_QUEUED], 1, direction, sync);
342 blkio_end_empty_time(stats);
343 u64_stats_update_end(&stats->syncp);
cdc1184c 344
c1768268 345 blkio_set_start_group_wait_time(blkg, pol, curr_blkg);
cdc1184c 346}
a11cdaa7 347EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
cdc1184c 348
a11cdaa7 349void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
c1768268
TH
350 struct blkio_policy_type *pol,
351 bool direction, bool sync)
cdc1184c 352{
edf1b879
TH
353 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
354
355 lockdep_assert_held(blkg->q->queue_lock);
cdc1184c 356
edf1b879
TH
357 u64_stats_update_begin(&stats->syncp);
358 blkio_check_and_dec_stat(stats->stat_arr[BLKIO_STAT_QUEUED], direction,
359 sync);
360 u64_stats_update_end(&stats->syncp);
cdc1184c 361}
a11cdaa7 362EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
cdc1184c 363
c1768268
TH
364void blkiocg_update_timeslice_used(struct blkio_group *blkg,
365 struct blkio_policy_type *pol,
366 unsigned long time,
367 unsigned long unaccounted_time)
22084190 368{
edf1b879
TH
369 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
370
371 lockdep_assert_held(blkg->q->queue_lock);
303a3acb 372
edf1b879
TH
373 u64_stats_update_begin(&stats->syncp);
374 stats->time += time;
a23e6869 375#ifdef CONFIG_DEBUG_BLK_CGROUP
edf1b879 376 stats->unaccounted_time += unaccounted_time;
a23e6869 377#endif
edf1b879 378 u64_stats_update_end(&stats->syncp);
22084190 379}
303a3acb 380EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
22084190 381
5624a4e4
VG
382/*
383 * should be called under rcu read lock or queue lock to make sure blkg pointer
384 * is valid.
385 */
84c124da 386void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
c1768268
TH
387 struct blkio_policy_type *pol,
388 uint64_t bytes, bool direction, bool sync)
9195291e 389{
c1768268 390 struct blkg_policy_data *pd = blkg->pd[pol->plid];
5624a4e4 391 struct blkio_group_stats_cpu *stats_cpu;
575969a0
VG
392 unsigned long flags;
393
1cd9e039
VG
394 /* If per cpu stats are not allocated yet, don't do any accounting. */
395 if (pd->stats_cpu == NULL)
396 return;
397
575969a0
VG
398 /*
399 * Disabling interrupts to provide mutual exclusion between two
400 * writes on same cpu. It probably is not needed for 64bit. Not
401 * optimizing that case yet.
402 */
403 local_irq_save(flags);
9195291e 404
549d3aa8 405 stats_cpu = this_cpu_ptr(pd->stats_cpu);
5624a4e4 406
575969a0 407 u64_stats_update_begin(&stats_cpu->syncp);
5624a4e4
VG
408 stats_cpu->sectors += bytes >> 9;
409 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICED],
410 1, direction, sync);
411 blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_SERVICE_BYTES],
412 bytes, direction, sync);
575969a0
VG
413 u64_stats_update_end(&stats_cpu->syncp);
414 local_irq_restore(flags);
9195291e 415}
84c124da 416EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
9195291e 417
84c124da 418void blkiocg_update_completion_stats(struct blkio_group *blkg,
c1768268
TH
419 struct blkio_policy_type *pol,
420 uint64_t start_time,
421 uint64_t io_start_time, bool direction,
422 bool sync)
9195291e 423{
edf1b879 424 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
9195291e
DS
425 unsigned long long now = sched_clock();
426
edf1b879
TH
427 lockdep_assert_held(blkg->q->queue_lock);
428
429 u64_stats_update_begin(&stats->syncp);
84c124da
DS
430 if (time_after64(now, io_start_time))
431 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
432 now - io_start_time, direction, sync);
433 if (time_after64(io_start_time, start_time))
434 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
435 io_start_time - start_time, direction, sync);
edf1b879 436 u64_stats_update_end(&stats->syncp);
9195291e 437}
84c124da 438EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
9195291e 439
317389a7 440/* Merged stats are per cpu. */
c1768268
TH
441void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
442 struct blkio_policy_type *pol,
443 bool direction, bool sync)
812d4026 444{
edf1b879
TH
445 struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;
446
447 lockdep_assert_held(blkg->q->queue_lock);
812d4026 448
edf1b879 449 u64_stats_update_begin(&stats->syncp);
5fe224d2 450 blkio_add_stat(stats->stat_arr[BLKIO_STAT_MERGED], 1, direction, sync);
edf1b879 451 u64_stats_update_end(&stats->syncp);
812d4026
DS
452}
453EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
454
1cd9e039
VG
455/*
456 * Worker for allocating per cpu stat for blk groups. This is scheduled on
457 * the system_nrt_wq once there are some groups on the alloc_list waiting
458 * for allocation.
459 */
460static void blkio_stat_alloc_fn(struct work_struct *work)
461{
462 static void *pcpu_stats[BLKIO_NR_POLICIES];
463 struct delayed_work *dwork = to_delayed_work(work);
464 struct blkio_group *blkg;
465 int i;
466 bool empty = false;
467
468alloc_stats:
469 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
470 if (pcpu_stats[i] != NULL)
471 continue;
472
473 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
474
475 /* Allocation failed. Try again after some time. */
476 if (pcpu_stats[i] == NULL) {
477 queue_delayed_work(system_nrt_wq, dwork,
478 msecs_to_jiffies(10));
479 return;
480 }
481 }
482
483 spin_lock_irq(&blkio_list_lock);
484 spin_lock(&alloc_list_lock);
485
486 /* cgroup got deleted or queue exited. */
487 if (!list_empty(&alloc_list)) {
488 blkg = list_first_entry(&alloc_list, struct blkio_group,
489 alloc_node);
490 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
491 struct blkg_policy_data *pd = blkg->pd[i];
492
493 if (blkio_policy[i] && pd && !pd->stats_cpu)
494 swap(pd->stats_cpu, pcpu_stats[i]);
495 }
496
497 list_del_init(&blkg->alloc_node);
498 }
499
500 empty = list_empty(&alloc_list);
501
502 spin_unlock(&alloc_list_lock);
503 spin_unlock_irq(&blkio_list_lock);
504
505 if (!empty)
506 goto alloc_stats;
507}
508
0381411e
TH
509/**
510 * blkg_free - free a blkg
511 * @blkg: blkg to free
512 *
513 * Free @blkg which may be partially allocated.
514 */
515static void blkg_free(struct blkio_group *blkg)
516{
e8989fae 517 int i;
549d3aa8
TH
518
519 if (!blkg)
520 return;
521
e8989fae
TH
522 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
523 struct blkg_policy_data *pd = blkg->pd[i];
524
525 if (pd) {
526 free_percpu(pd->stats_cpu);
527 kfree(pd);
528 }
0381411e 529 }
e8989fae 530
549d3aa8 531 kfree(blkg);
0381411e
TH
532}
533
534/**
535 * blkg_alloc - allocate a blkg
536 * @blkcg: block cgroup the new blkg is associated with
537 * @q: request_queue the new blkg is associated with
0381411e 538 *
e8989fae 539 * Allocate a new blkg assocating @blkcg and @q.
0381411e
TH
540 */
541static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
e8989fae 542 struct request_queue *q)
0381411e
TH
543{
544 struct blkio_group *blkg;
e8989fae 545 int i;
0381411e
TH
546
547 /* alloc and init base part */
548 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
549 if (!blkg)
550 return NULL;
551
c875f4d0 552 blkg->q = q;
e8989fae 553 INIT_LIST_HEAD(&blkg->q_node);
1cd9e039 554 INIT_LIST_HEAD(&blkg->alloc_node);
0381411e 555 blkg->blkcg = blkcg;
1adaf3dd 556 blkg->refcnt = 1;
0381411e
TH
557 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
558
e8989fae
TH
559 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
560 struct blkio_policy_type *pol = blkio_policy[i];
561 struct blkg_policy_data *pd;
0381411e 562
e8989fae
TH
563 if (!pol)
564 continue;
565
566 /* alloc per-policy data and attach it to blkg */
567 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
568 q->node);
569 if (!pd) {
570 blkg_free(blkg);
571 return NULL;
572 }
549d3aa8 573
e8989fae
TH
574 blkg->pd[i] = pd;
575 pd->blkg = blkg;
0381411e
TH
576 }
577
549d3aa8 578 /* invoke per-policy init */
e8989fae
TH
579 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
580 struct blkio_policy_type *pol = blkio_policy[i];
581
582 if (pol)
583 pol->ops.blkio_init_group_fn(blkg);
584 }
585
0381411e
TH
586 return blkg;
587}
588
cd1604fa
TH
589struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
590 struct request_queue *q,
591 enum blkio_policy_id plid,
592 bool for_root)
593 __releases(q->queue_lock) __acquires(q->queue_lock)
5624a4e4 594{
1cd9e039 595 struct blkio_group *blkg;
5624a4e4 596
cd1604fa
TH
597 WARN_ON_ONCE(!rcu_read_lock_held());
598 lockdep_assert_held(q->queue_lock);
599
600 /*
601 * This could be the first entry point of blkcg implementation and
602 * we shouldn't allow anything to go through for a bypassing queue.
603 * The following can be removed if blkg lookup is guaranteed to
604 * fail on a bypassing queue.
605 */
606 if (unlikely(blk_queue_bypass(q)) && !for_root)
607 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
608
e8989fae 609 blkg = blkg_lookup(blkcg, q);
cd1604fa
TH
610 if (blkg)
611 return blkg;
612
7ee9c562 613 /* blkg holds a reference to blkcg */
cd1604fa
TH
614 if (!css_tryget(&blkcg->css))
615 return ERR_PTR(-EINVAL);
616
617 /*
618 * Allocate and initialize.
cd1604fa 619 */
1cd9e039 620 blkg = blkg_alloc(blkcg, q);
cd1604fa
TH
621
622 /* did alloc fail? */
1cd9e039 623 if (unlikely(!blkg)) {
cd1604fa
TH
624 blkg = ERR_PTR(-ENOMEM);
625 goto out;
626 }
627
628 /* insert */
629 spin_lock(&blkcg->lock);
31e4c28d 630 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
e8989fae 631 list_add(&blkg->q_node, &q->blkg_list);
cd1604fa 632 spin_unlock(&blkcg->lock);
1cd9e039
VG
633
634 spin_lock(&alloc_list_lock);
635 list_add(&blkg->alloc_node, &alloc_list);
636 /* Queue per cpu stat allocation from worker thread. */
637 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
638 spin_unlock(&alloc_list_lock);
cd1604fa 639out:
cd1604fa 640 return blkg;
31e4c28d 641}
cd1604fa 642EXPORT_SYMBOL_GPL(blkg_lookup_create);
31e4c28d 643
31e4c28d 644/* called under rcu_read_lock(). */
cd1604fa 645struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
e8989fae 646 struct request_queue *q)
31e4c28d
VG
647{
648 struct blkio_group *blkg;
649 struct hlist_node *n;
31e4c28d 650
ca32aefc 651 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
e8989fae 652 if (blkg->q == q)
31e4c28d 653 return blkg;
31e4c28d
VG
654 return NULL;
655}
cd1604fa 656EXPORT_SYMBOL_GPL(blkg_lookup);
31e4c28d 657
e8989fae 658static void blkg_destroy(struct blkio_group *blkg)
03aa264a
TH
659{
660 struct request_queue *q = blkg->q;
9f13ef67 661 struct blkio_cgroup *blkcg = blkg->blkcg;
03aa264a
TH
662
663 lockdep_assert_held(q->queue_lock);
9f13ef67 664 lockdep_assert_held(&blkcg->lock);
03aa264a
TH
665
666 /* Something wrong if we are trying to remove same group twice */
e8989fae 667 WARN_ON_ONCE(list_empty(&blkg->q_node));
9f13ef67 668 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
e8989fae 669 list_del_init(&blkg->q_node);
9f13ef67 670 hlist_del_init_rcu(&blkg->blkcg_node);
03aa264a 671
1cd9e039
VG
672 spin_lock(&alloc_list_lock);
673 list_del_init(&blkg->alloc_node);
674 spin_unlock(&alloc_list_lock);
675
03aa264a
TH
676 /*
677 * Put the reference taken at the time of creation so that when all
678 * queues are gone, group can be destroyed.
679 */
680 blkg_put(blkg);
681}
682
e8989fae
TH
683/*
684 * XXX: This updates blkg policy data in-place for root blkg, which is
685 * necessary across elevator switch and policy registration as root blkgs
686 * aren't shot down. This broken and racy implementation is temporary.
687 * Eventually, blkg shoot down will be replaced by proper in-place update.
688 */
689void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
690{
691 struct blkio_policy_type *pol = blkio_policy[plid];
692 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
693 struct blkg_policy_data *pd;
694
695 if (!blkg)
696 return;
697
698 kfree(blkg->pd[plid]);
699 blkg->pd[plid] = NULL;
700
701 if (!pol)
702 return;
703
704 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
705 WARN_ON_ONCE(!pd);
706
707 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
708 WARN_ON_ONCE(!pd->stats_cpu);
709
710 blkg->pd[plid] = pd;
711 pd->blkg = blkg;
712 pol->ops.blkio_init_group_fn(blkg);
713}
714EXPORT_SYMBOL_GPL(update_root_blkg_pd);
715
9f13ef67
TH
716/**
717 * blkg_destroy_all - destroy all blkgs associated with a request_queue
718 * @q: request_queue of interest
719 * @destroy_root: whether to destroy root blkg or not
720 *
721 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
722 * destroyed; otherwise, root blkg is left alone.
723 */
e8989fae 724void blkg_destroy_all(struct request_queue *q, bool destroy_root)
72e06c25 725{
03aa264a 726 struct blkio_group *blkg, *n;
72e06c25 727
9f13ef67 728 spin_lock_irq(q->queue_lock);
72e06c25 729
9f13ef67
TH
730 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
731 struct blkio_cgroup *blkcg = blkg->blkcg;
72e06c25 732
9f13ef67
TH
733 /* skip root? */
734 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
735 continue;
72e06c25 736
9f13ef67
TH
737 spin_lock(&blkcg->lock);
738 blkg_destroy(blkg);
739 spin_unlock(&blkcg->lock);
72e06c25 740 }
9f13ef67
TH
741
742 spin_unlock_irq(q->queue_lock);
72e06c25 743}
03aa264a 744EXPORT_SYMBOL_GPL(blkg_destroy_all);
72e06c25 745
1adaf3dd
TH
746static void blkg_rcu_free(struct rcu_head *rcu_head)
747{
748 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
749}
750
751void __blkg_release(struct blkio_group *blkg)
752{
753 /* release the extra blkcg reference this blkg has been holding */
754 css_put(&blkg->blkcg->css);
755
756 /*
757 * A group is freed in rcu manner. But having an rcu lock does not
758 * mean that one can access all the fields of blkg and assume these
759 * are valid. For example, don't try to follow throtl_data and
760 * request queue links.
761 *
762 * Having a reference to blkg under an rcu allows acess to only
763 * values local to groups like group stats and group rate limits
764 */
765 call_rcu(&blkg->rcu_head, blkg_rcu_free);
766}
767EXPORT_SYMBOL_GPL(__blkg_release);
768
c1768268 769static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
f0bdc8cd 770{
c1768268 771 struct blkg_policy_data *pd = blkg->pd[plid];
997a026c 772 int cpu;
1cd9e039
VG
773
774 if (pd->stats_cpu == NULL)
775 return;
997a026c
TH
776
777 for_each_possible_cpu(cpu) {
778 struct blkio_group_stats_cpu *sc =
779 per_cpu_ptr(pd->stats_cpu, cpu);
780
781 sc->sectors = 0;
782 memset(sc->stat_arr_cpu, 0, sizeof(sc->stat_arr_cpu));
f0bdc8cd
VG
783 }
784}
785
303a3acb 786static int
84c124da 787blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
303a3acb 788{
997a026c 789 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
303a3acb
DS
790 struct blkio_group *blkg;
791 struct hlist_node *n;
cdc1184c 792 int i;
303a3acb 793
e8989fae 794 spin_lock(&blkio_list_lock);
303a3acb 795 spin_lock_irq(&blkcg->lock);
997a026c
TH
796
797 /*
798 * Note that stat reset is racy - it doesn't synchronize against
799 * stat updates. This is a debug feature which shouldn't exist
800 * anyway. If you get hit by a race, retry.
801 */
303a3acb 802 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
e8989fae 803 struct blkio_policy_type *pol;
549d3aa8 804
e8989fae
TH
805 list_for_each_entry(pol, &blkio_list, list) {
806 struct blkg_policy_data *pd = blkg->pd[pol->plid];
997a026c
TH
807 struct blkio_group_stats *stats = &pd->stats;
808
809 /* queued stats shouldn't be cleared */
810 for (i = 0; i < ARRAY_SIZE(stats->stat_arr); i++)
811 if (i != BLKIO_STAT_QUEUED)
812 memset(stats->stat_arr[i], 0,
813 sizeof(stats->stat_arr[i]));
814 stats->time = 0;
812df48d 815#ifdef CONFIG_DEBUG_BLK_CGROUP
997a026c
TH
816 memset((void *)stats + BLKG_STATS_DEBUG_CLEAR_START, 0,
817 BLKG_STATS_DEBUG_CLEAR_SIZE);
812df48d 818#endif
e8989fae
TH
819 blkio_reset_stats_cpu(blkg, pol->plid);
820 }
303a3acb 821 }
f0bdc8cd 822
303a3acb 823 spin_unlock_irq(&blkcg->lock);
e8989fae 824 spin_unlock(&blkio_list_lock);
303a3acb
DS
825 return 0;
826}
827
7a4dd281
TH
828static void blkio_get_key_name(enum stat_sub_type type, const char *dname,
829 char *str, int chars_left, bool diskname_only)
303a3acb 830{
7a4dd281 831 snprintf(str, chars_left, "%s", dname);
303a3acb
DS
832 chars_left -= strlen(str);
833 if (chars_left <= 0) {
834 printk(KERN_WARNING
835 "Possibly incorrect cgroup stat display format");
836 return;
837 }
84c124da
DS
838 if (diskname_only)
839 return;
303a3acb 840 switch (type) {
84c124da 841 case BLKIO_STAT_READ:
303a3acb
DS
842 strlcat(str, " Read", chars_left);
843 break;
84c124da 844 case BLKIO_STAT_WRITE:
303a3acb
DS
845 strlcat(str, " Write", chars_left);
846 break;
84c124da 847 case BLKIO_STAT_SYNC:
303a3acb
DS
848 strlcat(str, " Sync", chars_left);
849 break;
84c124da 850 case BLKIO_STAT_ASYNC:
303a3acb
DS
851 strlcat(str, " Async", chars_left);
852 break;
84c124da 853 case BLKIO_STAT_TOTAL:
303a3acb
DS
854 strlcat(str, " Total", chars_left);
855 break;
856 default:
857 strlcat(str, " Invalid", chars_left);
858 }
859}
860
c1768268 861static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,
5624a4e4
VG
862 enum stat_type_cpu type, enum stat_sub_type sub_type)
863{
c1768268 864 struct blkg_policy_data *pd = blkg->pd[plid];
5624a4e4
VG
865 int cpu;
866 struct blkio_group_stats_cpu *stats_cpu;
575969a0 867 u64 val = 0, tval;
5624a4e4 868
1cd9e039
VG
869 if (pd->stats_cpu == NULL)
870 return val;
871
5624a4e4 872 for_each_possible_cpu(cpu) {
575969a0 873 unsigned int start;
549d3aa8 874 stats_cpu = per_cpu_ptr(pd->stats_cpu, cpu);
5624a4e4 875
575969a0
VG
876 do {
877 start = u64_stats_fetch_begin(&stats_cpu->syncp);
878 if (type == BLKIO_STAT_CPU_SECTORS)
879 tval = stats_cpu->sectors;
880 else
881 tval = stats_cpu->stat_arr_cpu[type][sub_type];
882 } while(u64_stats_fetch_retry(&stats_cpu->syncp, start));
883
884 val += tval;
5624a4e4
VG
885 }
886
887 return val;
888}
889
c1768268 890static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,
7a4dd281
TH
891 struct cgroup_map_cb *cb, const char *dname,
892 enum stat_type_cpu type)
5624a4e4
VG
893{
894 uint64_t disk_total, val;
895 char key_str[MAX_KEY_LEN];
896 enum stat_sub_type sub_type;
897
898 if (type == BLKIO_STAT_CPU_SECTORS) {
c1768268 899 val = blkio_read_stat_cpu(blkg, plid, type, 0);
c4c76a05
TH
900 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
901 cb->fill(cb, key_str, val);
902 return val;
5624a4e4
VG
903 }
904
905 for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
906 sub_type++) {
7a4dd281
TH
907 blkio_get_key_name(sub_type, dname, key_str, MAX_KEY_LEN,
908 false);
c1768268 909 val = blkio_read_stat_cpu(blkg, plid, type, sub_type);
5624a4e4
VG
910 cb->fill(cb, key_str, val);
911 }
912
c1768268
TH
913 disk_total = blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_READ) +
914 blkio_read_stat_cpu(blkg, plid, type, BLKIO_STAT_WRITE);
5624a4e4 915
7a4dd281
TH
916 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
917 false);
5624a4e4
VG
918 cb->fill(cb, key_str, disk_total);
919 return disk_total;
920}
921
c1768268 922static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,
7a4dd281
TH
923 struct cgroup_map_cb *cb, const char *dname,
924 enum stat_type type)
303a3acb 925{
c4c76a05
TH
926 struct blkio_group_stats *stats = &blkg->pd[plid]->stats;
927 uint64_t v = 0, disk_total = 0;
303a3acb 928 char key_str[MAX_KEY_LEN];
edf1b879 929 unsigned int sync_start;
c4c76a05 930 int st;
84c124da 931
c4c76a05 932 if (type >= BLKIO_STAT_ARR_NR) {
edf1b879
TH
933 do {
934 sync_start = u64_stats_fetch_begin(&stats->syncp);
935 switch (type) {
936 case BLKIO_STAT_TIME:
937 v = stats->time;
938 break;
9026e521 939#ifdef CONFIG_DEBUG_BLK_CGROUP
edf1b879
TH
940 case BLKIO_STAT_UNACCOUNTED_TIME:
941 v = stats->unaccounted_time;
942 break;
943 case BLKIO_STAT_AVG_QUEUE_SIZE: {
944 uint64_t samples = stats->avg_queue_size_samples;
c4c76a05 945
edf1b879
TH
946 if (samples) {
947 v = stats->avg_queue_size_sum;
948 do_div(v, samples);
949 }
950 break;
c4c76a05 951 }
edf1b879
TH
952 case BLKIO_STAT_IDLE_TIME:
953 v = stats->idle_time;
954 break;
955 case BLKIO_STAT_EMPTY_TIME:
956 v = stats->empty_time;
957 break;
958 case BLKIO_STAT_DEQUEUE:
959 v = stats->dequeue;
960 break;
961 case BLKIO_STAT_GROUP_WAIT_TIME:
962 v = stats->group_wait_time;
963 break;
84c124da 964#endif
edf1b879
TH
965 default:
966 WARN_ON_ONCE(1);
967 }
968 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
303a3acb 969
c4c76a05
TH
970 blkio_get_key_name(0, dname, key_str, MAX_KEY_LEN, true);
971 cb->fill(cb, key_str, v);
972 return v;
303a3acb 973 }
c4c76a05
TH
974
975 for (st = BLKIO_STAT_READ; st < BLKIO_STAT_TOTAL; st++) {
edf1b879
TH
976 do {
977 sync_start = u64_stats_fetch_begin(&stats->syncp);
978 v = stats->stat_arr[type][st];
979 } while (u64_stats_fetch_retry(&stats->syncp, sync_start));
c4c76a05
TH
980
981 blkio_get_key_name(st, dname, key_str, MAX_KEY_LEN, false);
982 cb->fill(cb, key_str, v);
983 if (st == BLKIO_STAT_READ || st == BLKIO_STAT_WRITE)
984 disk_total += v;
985 }
986
7a4dd281
TH
987 blkio_get_key_name(BLKIO_STAT_TOTAL, dname, key_str, MAX_KEY_LEN,
988 false);
303a3acb
DS
989 cb->fill(cb, key_str, disk_total);
990 return disk_total;
991}
992
4bfd482e
TH
993static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid,
994 int fileid, struct blkio_cgroup *blkcg)
34d0f179 995{
ece84241 996 struct gendisk *disk = NULL;
e56da7e2 997 struct blkio_group *blkg = NULL;
549d3aa8 998 struct blkg_policy_data *pd;
34d0f179 999 char *s[4], *p, *major_s = NULL, *minor_s = NULL;
d11bb446 1000 unsigned long major, minor;
ece84241
TH
1001 int i = 0, ret = -EINVAL;
1002 int part;
34d0f179 1003 dev_t dev;
d11bb446 1004 u64 temp;
34d0f179
GJ
1005
1006 memset(s, 0, sizeof(s));
1007
1008 while ((p = strsep(&buf, " ")) != NULL) {
1009 if (!*p)
1010 continue;
1011
1012 s[i++] = p;
1013
1014 /* Prevent from inputing too many things */
1015 if (i == 3)
1016 break;
1017 }
1018
1019 if (i != 2)
ece84241 1020 goto out;
34d0f179
GJ
1021
1022 p = strsep(&s[0], ":");
1023 if (p != NULL)
1024 major_s = p;
1025 else
ece84241 1026 goto out;
34d0f179
GJ
1027
1028 minor_s = s[0];
1029 if (!minor_s)
ece84241 1030 goto out;
34d0f179 1031
ece84241
TH
1032 if (strict_strtoul(major_s, 10, &major))
1033 goto out;
34d0f179 1034
ece84241
TH
1035 if (strict_strtoul(minor_s, 10, &minor))
1036 goto out;
34d0f179
GJ
1037
1038 dev = MKDEV(major, minor);
1039
ece84241
TH
1040 if (strict_strtoull(s[1], 10, &temp))
1041 goto out;
34d0f179 1042
e56da7e2 1043 disk = get_gendisk(dev, &part);
4bfd482e 1044 if (!disk || part)
e56da7e2 1045 goto out;
e56da7e2
TH
1046
1047 rcu_read_lock();
1048
4bfd482e
TH
1049 spin_lock_irq(disk->queue->queue_lock);
1050 blkg = blkg_lookup_create(blkcg, disk->queue, plid, false);
1051 spin_unlock_irq(disk->queue->queue_lock);
e56da7e2 1052
4bfd482e
TH
1053 if (IS_ERR(blkg)) {
1054 ret = PTR_ERR(blkg);
1055 goto out_unlock;
d11bb446 1056 }
34d0f179 1057
549d3aa8
TH
1058 pd = blkg->pd[plid];
1059
062a644d
VG
1060 switch (plid) {
1061 case BLKIO_POLICY_PROP:
d11bb446
WG
1062 if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
1063 temp > BLKIO_WEIGHT_MAX)
e56da7e2 1064 goto out_unlock;
34d0f179 1065
549d3aa8 1066 pd->conf.weight = temp;
c1768268 1067 blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight);
4c9eefa1
VG
1068 break;
1069 case BLKIO_POLICY_THROTL:
7702e8f4
VG
1070 switch(fileid) {
1071 case BLKIO_THROTL_read_bps_device:
549d3aa8 1072 pd->conf.bps[READ] = temp;
c1768268 1073 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
e56da7e2 1074 break;
7702e8f4 1075 case BLKIO_THROTL_write_bps_device:
549d3aa8 1076 pd->conf.bps[WRITE] = temp;
c1768268 1077 blkio_update_group_bps(blkg, plid, temp ?: -1, fileid);
7702e8f4
VG
1078 break;
1079 case BLKIO_THROTL_read_iops_device:
e56da7e2
TH
1080 if (temp > THROTL_IOPS_MAX)
1081 goto out_unlock;
549d3aa8 1082 pd->conf.iops[READ] = temp;
c1768268 1083 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
e56da7e2 1084 break;
7702e8f4 1085 case BLKIO_THROTL_write_iops_device:
d11bb446 1086 if (temp > THROTL_IOPS_MAX)
e56da7e2 1087 goto out_unlock;
549d3aa8 1088 pd->conf.iops[WRITE] = temp;
c1768268 1089 blkio_update_group_iops(blkg, plid, temp ?: -1, fileid);
7702e8f4
VG
1090 break;
1091 }
062a644d
VG
1092 break;
1093 default:
1094 BUG();
1095 }
ece84241 1096 ret = 0;
e56da7e2
TH
1097out_unlock:
1098 rcu_read_unlock();
ece84241
TH
1099out:
1100 put_disk(disk);
e56da7e2
TH
1101
1102 /*
1103 * If queue was bypassing, we should retry. Do so after a short
1104 * msleep(). It isn't strictly necessary but queue can be
1105 * bypassing for some time and it's always nice to avoid busy
1106 * looping.
1107 */
1108 if (ret == -EBUSY) {
1109 msleep(10);
1110 return restart_syscall();
1111 }
ece84241 1112 return ret;
34d0f179
GJ
1113}
1114
062a644d
VG
1115static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
1116 const char *buffer)
34d0f179
GJ
1117{
1118 int ret = 0;
1119 char *buf;
e56da7e2 1120 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
062a644d
VG
1121 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1122 int fileid = BLKIOFILE_ATTR(cft->private);
34d0f179
GJ
1123
1124 buf = kstrdup(buffer, GFP_KERNEL);
1125 if (!buf)
1126 return -ENOMEM;
1127
4bfd482e 1128 ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg);
34d0f179
GJ
1129 kfree(buf);
1130 return ret;
1131}
1132
92616b5b
VG
1133static const char *blkg_dev_name(struct blkio_group *blkg)
1134{
1135 /* some drivers (floppy) instantiate a queue w/o disk registered */
1136 if (blkg->q->backing_dev_info.dev)
1137 return dev_name(blkg->q->backing_dev_info.dev);
1138 return NULL;
1139}
1140
4bfd482e
TH
1141static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg,
1142 struct seq_file *m)
34d0f179 1143{
c1768268 1144 int plid = BLKIOFILE_POLICY(cft->private);
4bfd482e 1145 int fileid = BLKIOFILE_ATTR(cft->private);
c1768268
TH
1146 struct blkg_policy_data *pd = blkg->pd[plid];
1147 const char *dname = blkg_dev_name(blkg);
4bfd482e
TH
1148 int rw = WRITE;
1149
92616b5b
VG
1150 if (!dname)
1151 return;
1152
c1768268 1153 switch (plid) {
062a644d 1154 case BLKIO_POLICY_PROP:
549d3aa8 1155 if (pd->conf.weight)
7a4dd281 1156 seq_printf(m, "%s\t%u\n",
549d3aa8 1157 dname, pd->conf.weight);
4c9eefa1
VG
1158 break;
1159 case BLKIO_POLICY_THROTL:
4bfd482e 1160 switch (fileid) {
7702e8f4 1161 case BLKIO_THROTL_read_bps_device:
4bfd482e 1162 rw = READ;
7702e8f4 1163 case BLKIO_THROTL_write_bps_device:
549d3aa8 1164 if (pd->conf.bps[rw])
7a4dd281 1165 seq_printf(m, "%s\t%llu\n",
549d3aa8 1166 dname, pd->conf.bps[rw]);
7702e8f4
VG
1167 break;
1168 case BLKIO_THROTL_read_iops_device:
4bfd482e 1169 rw = READ;
7702e8f4 1170 case BLKIO_THROTL_write_iops_device:
549d3aa8 1171 if (pd->conf.iops[rw])
7a4dd281 1172 seq_printf(m, "%s\t%u\n",
549d3aa8 1173 dname, pd->conf.iops[rw]);
7702e8f4
VG
1174 break;
1175 }
062a644d
VG
1176 break;
1177 default:
1178 BUG();
1179 }
1180}
34d0f179 1181
062a644d 1182/* cgroup files which read their data from policy nodes end up here */
4bfd482e
TH
1183static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg,
1184 struct seq_file *m)
34d0f179 1185{
4bfd482e
TH
1186 struct blkio_group *blkg;
1187 struct hlist_node *n;
34d0f179 1188
4bfd482e
TH
1189 spin_lock_irq(&blkcg->lock);
1190 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
e8989fae 1191 blkio_print_group_conf(cft, blkg, m);
4bfd482e 1192 spin_unlock_irq(&blkcg->lock);
062a644d
VG
1193}
1194
1195static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1196 struct seq_file *m)
1197{
1198 struct blkio_cgroup *blkcg;
1199 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1200 int name = BLKIOFILE_ATTR(cft->private);
1201
1202 blkcg = cgroup_to_blkio_cgroup(cgrp);
1203
1204 switch(plid) {
1205 case BLKIO_POLICY_PROP:
1206 switch(name) {
1207 case BLKIO_PROP_weight_device:
4bfd482e 1208 blkio_read_conf(cft, blkcg, m);
062a644d
VG
1209 return 0;
1210 default:
1211 BUG();
1212 }
1213 break;
4c9eefa1
VG
1214 case BLKIO_POLICY_THROTL:
1215 switch(name){
1216 case BLKIO_THROTL_read_bps_device:
1217 case BLKIO_THROTL_write_bps_device:
7702e8f4
VG
1218 case BLKIO_THROTL_read_iops_device:
1219 case BLKIO_THROTL_write_iops_device:
4bfd482e 1220 blkio_read_conf(cft, blkcg, m);
4c9eefa1
VG
1221 return 0;
1222 default:
1223 BUG();
1224 }
1225 break;
062a644d
VG
1226 default:
1227 BUG();
1228 }
1229
1230 return 0;
1231}
1232
1233static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
5624a4e4
VG
1234 struct cftype *cft, struct cgroup_map_cb *cb,
1235 enum stat_type type, bool show_total, bool pcpu)
062a644d
VG
1236{
1237 struct blkio_group *blkg;
1238 struct hlist_node *n;
1239 uint64_t cgroup_total = 0;
1240
c875f4d0
TH
1241 spin_lock_irq(&blkcg->lock);
1242
1243 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
92616b5b 1244 const char *dname = blkg_dev_name(blkg);
c1768268 1245 int plid = BLKIOFILE_POLICY(cft->private);
7a4dd281 1246
e8989fae 1247 if (!dname)
7a4dd281 1248 continue;
edf1b879 1249 if (pcpu)
c1768268
TH
1250 cgroup_total += blkio_get_stat_cpu(blkg, plid,
1251 cb, dname, type);
edf1b879 1252 else
c1768268
TH
1253 cgroup_total += blkio_get_stat(blkg, plid,
1254 cb, dname, type);
062a644d
VG
1255 }
1256 if (show_total)
1257 cb->fill(cb, "Total", cgroup_total);
c875f4d0
TH
1258
1259 spin_unlock_irq(&blkcg->lock);
062a644d
VG
1260 return 0;
1261}
1262
1263/* All map kind of cgroup file get serviced by this function */
1264static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1265 struct cgroup_map_cb *cb)
1266{
1267 struct blkio_cgroup *blkcg;
1268 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1269 int name = BLKIOFILE_ATTR(cft->private);
1270
1271 blkcg = cgroup_to_blkio_cgroup(cgrp);
1272
1273 switch(plid) {
1274 case BLKIO_POLICY_PROP:
1275 switch(name) {
1276 case BLKIO_PROP_time:
1277 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1278 BLKIO_STAT_TIME, 0, 0);
062a644d
VG
1279 case BLKIO_PROP_sectors:
1280 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1281 BLKIO_STAT_CPU_SECTORS, 0, 1);
062a644d
VG
1282 case BLKIO_PROP_io_service_bytes:
1283 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1284 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
062a644d
VG
1285 case BLKIO_PROP_io_serviced:
1286 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1287 BLKIO_STAT_CPU_SERVICED, 1, 1);
062a644d
VG
1288 case BLKIO_PROP_io_service_time:
1289 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1290 BLKIO_STAT_SERVICE_TIME, 1, 0);
062a644d
VG
1291 case BLKIO_PROP_io_wait_time:
1292 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1293 BLKIO_STAT_WAIT_TIME, 1, 0);
062a644d
VG
1294 case BLKIO_PROP_io_merged:
1295 return blkio_read_blkg_stats(blkcg, cft, cb,
5fe224d2 1296 BLKIO_STAT_MERGED, 1, 0);
062a644d
VG
1297 case BLKIO_PROP_io_queued:
1298 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1299 BLKIO_STAT_QUEUED, 1, 0);
062a644d 1300#ifdef CONFIG_DEBUG_BLK_CGROUP
9026e521
JT
1301 case BLKIO_PROP_unaccounted_time:
1302 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1303 BLKIO_STAT_UNACCOUNTED_TIME, 0, 0);
062a644d
VG
1304 case BLKIO_PROP_dequeue:
1305 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1306 BLKIO_STAT_DEQUEUE, 0, 0);
062a644d
VG
1307 case BLKIO_PROP_avg_queue_size:
1308 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1309 BLKIO_STAT_AVG_QUEUE_SIZE, 0, 0);
062a644d
VG
1310 case BLKIO_PROP_group_wait_time:
1311 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1312 BLKIO_STAT_GROUP_WAIT_TIME, 0, 0);
062a644d
VG
1313 case BLKIO_PROP_idle_time:
1314 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1315 BLKIO_STAT_IDLE_TIME, 0, 0);
062a644d
VG
1316 case BLKIO_PROP_empty_time:
1317 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1318 BLKIO_STAT_EMPTY_TIME, 0, 0);
062a644d
VG
1319#endif
1320 default:
1321 BUG();
1322 }
1323 break;
4c9eefa1
VG
1324 case BLKIO_POLICY_THROTL:
1325 switch(name){
1326 case BLKIO_THROTL_io_service_bytes:
1327 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1328 BLKIO_STAT_CPU_SERVICE_BYTES, 1, 1);
4c9eefa1
VG
1329 case BLKIO_THROTL_io_serviced:
1330 return blkio_read_blkg_stats(blkcg, cft, cb,
5624a4e4 1331 BLKIO_STAT_CPU_SERVICED, 1, 1);
4c9eefa1
VG
1332 default:
1333 BUG();
1334 }
1335 break;
062a644d
VG
1336 default:
1337 BUG();
1338 }
1339
1340 return 0;
1341}
1342
4bfd482e 1343static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val)
062a644d
VG
1344{
1345 struct blkio_group *blkg;
1346 struct hlist_node *n;
062a644d
VG
1347
1348 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1349 return -EINVAL;
1350
1351 spin_lock(&blkio_list_lock);
1352 spin_lock_irq(&blkcg->lock);
1353 blkcg->weight = (unsigned int)val;
1354
549d3aa8 1355 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
e8989fae 1356 struct blkg_policy_data *pd = blkg->pd[plid];
549d3aa8 1357
e8989fae 1358 if (!pd->conf.weight)
c1768268 1359 blkio_update_group_weight(blkg, plid, blkcg->weight);
549d3aa8 1360 }
062a644d 1361
062a644d
VG
1362 spin_unlock_irq(&blkcg->lock);
1363 spin_unlock(&blkio_list_lock);
1364 return 0;
1365}
1366
1367static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1368 struct blkio_cgroup *blkcg;
1369 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1370 int name = BLKIOFILE_ATTR(cft->private);
1371
1372 blkcg = cgroup_to_blkio_cgroup(cgrp);
1373
1374 switch(plid) {
1375 case BLKIO_POLICY_PROP:
1376 switch(name) {
1377 case BLKIO_PROP_weight:
1378 return (u64)blkcg->weight;
1379 }
1380 break;
1381 default:
1382 BUG();
1383 }
1384 return 0;
1385}
1386
1387static int
1388blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1389{
1390 struct blkio_cgroup *blkcg;
1391 enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1392 int name = BLKIOFILE_ATTR(cft->private);
1393
1394 blkcg = cgroup_to_blkio_cgroup(cgrp);
1395
1396 switch(plid) {
1397 case BLKIO_POLICY_PROP:
1398 switch(name) {
1399 case BLKIO_PROP_weight:
4bfd482e 1400 return blkio_weight_write(blkcg, plid, val);
062a644d
VG
1401 }
1402 break;
1403 default:
1404 BUG();
1405 }
34d0f179 1406
34d0f179
GJ
1407 return 0;
1408}
1409
31e4c28d 1410struct cftype blkio_files[] = {
34d0f179
GJ
1411 {
1412 .name = "weight_device",
062a644d
VG
1413 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1414 BLKIO_PROP_weight_device),
1415 .read_seq_string = blkiocg_file_read,
1416 .write_string = blkiocg_file_write,
34d0f179
GJ
1417 .max_write_len = 256,
1418 },
31e4c28d
VG
1419 {
1420 .name = "weight",
062a644d
VG
1421 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1422 BLKIO_PROP_weight),
1423 .read_u64 = blkiocg_file_read_u64,
1424 .write_u64 = blkiocg_file_write_u64,
31e4c28d 1425 },
22084190
VG
1426 {
1427 .name = "time",
062a644d
VG
1428 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1429 BLKIO_PROP_time),
1430 .read_map = blkiocg_file_read_map,
22084190
VG
1431 },
1432 {
1433 .name = "sectors",
062a644d
VG
1434 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1435 BLKIO_PROP_sectors),
1436 .read_map = blkiocg_file_read_map,
303a3acb
DS
1437 },
1438 {
1439 .name = "io_service_bytes",
062a644d
VG
1440 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1441 BLKIO_PROP_io_service_bytes),
1442 .read_map = blkiocg_file_read_map,
303a3acb
DS
1443 },
1444 {
1445 .name = "io_serviced",
062a644d
VG
1446 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1447 BLKIO_PROP_io_serviced),
1448 .read_map = blkiocg_file_read_map,
303a3acb
DS
1449 },
1450 {
1451 .name = "io_service_time",
062a644d
VG
1452 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1453 BLKIO_PROP_io_service_time),
1454 .read_map = blkiocg_file_read_map,
303a3acb
DS
1455 },
1456 {
1457 .name = "io_wait_time",
062a644d
VG
1458 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1459 BLKIO_PROP_io_wait_time),
1460 .read_map = blkiocg_file_read_map,
84c124da 1461 },
812d4026
DS
1462 {
1463 .name = "io_merged",
062a644d
VG
1464 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1465 BLKIO_PROP_io_merged),
1466 .read_map = blkiocg_file_read_map,
812d4026 1467 },
cdc1184c
DS
1468 {
1469 .name = "io_queued",
062a644d
VG
1470 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1471 BLKIO_PROP_io_queued),
1472 .read_map = blkiocg_file_read_map,
cdc1184c 1473 },
84c124da
DS
1474 {
1475 .name = "reset_stats",
1476 .write_u64 = blkiocg_reset_stats,
22084190 1477 },
13f98250
VG
1478#ifdef CONFIG_BLK_DEV_THROTTLING
1479 {
1480 .name = "throttle.read_bps_device",
1481 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1482 BLKIO_THROTL_read_bps_device),
1483 .read_seq_string = blkiocg_file_read,
1484 .write_string = blkiocg_file_write,
1485 .max_write_len = 256,
1486 },
1487
1488 {
1489 .name = "throttle.write_bps_device",
1490 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1491 BLKIO_THROTL_write_bps_device),
1492 .read_seq_string = blkiocg_file_read,
1493 .write_string = blkiocg_file_write,
1494 .max_write_len = 256,
1495 },
1496
1497 {
1498 .name = "throttle.read_iops_device",
1499 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1500 BLKIO_THROTL_read_iops_device),
1501 .read_seq_string = blkiocg_file_read,
1502 .write_string = blkiocg_file_write,
1503 .max_write_len = 256,
1504 },
1505
1506 {
1507 .name = "throttle.write_iops_device",
1508 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1509 BLKIO_THROTL_write_iops_device),
1510 .read_seq_string = blkiocg_file_read,
1511 .write_string = blkiocg_file_write,
1512 .max_write_len = 256,
1513 },
1514 {
1515 .name = "throttle.io_service_bytes",
1516 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1517 BLKIO_THROTL_io_service_bytes),
1518 .read_map = blkiocg_file_read_map,
1519 },
1520 {
1521 .name = "throttle.io_serviced",
1522 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1523 BLKIO_THROTL_io_serviced),
1524 .read_map = blkiocg_file_read_map,
1525 },
1526#endif /* CONFIG_BLK_DEV_THROTTLING */
1527
22084190 1528#ifdef CONFIG_DEBUG_BLK_CGROUP
cdc1184c
DS
1529 {
1530 .name = "avg_queue_size",
062a644d
VG
1531 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1532 BLKIO_PROP_avg_queue_size),
1533 .read_map = blkiocg_file_read_map,
cdc1184c 1534 },
812df48d
DS
1535 {
1536 .name = "group_wait_time",
062a644d
VG
1537 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1538 BLKIO_PROP_group_wait_time),
1539 .read_map = blkiocg_file_read_map,
812df48d
DS
1540 },
1541 {
1542 .name = "idle_time",
062a644d
VG
1543 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1544 BLKIO_PROP_idle_time),
1545 .read_map = blkiocg_file_read_map,
812df48d
DS
1546 },
1547 {
1548 .name = "empty_time",
062a644d
VG
1549 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1550 BLKIO_PROP_empty_time),
1551 .read_map = blkiocg_file_read_map,
812df48d 1552 },
cdc1184c 1553 {
22084190 1554 .name = "dequeue",
062a644d
VG
1555 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1556 BLKIO_PROP_dequeue),
1557 .read_map = blkiocg_file_read_map,
cdc1184c 1558 },
9026e521
JT
1559 {
1560 .name = "unaccounted_time",
1561 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1562 BLKIO_PROP_unaccounted_time),
1563 .read_map = blkiocg_file_read_map,
1564 },
22084190 1565#endif
31e4c28d
VG
1566};
1567
1568static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1569{
1570 return cgroup_add_files(cgroup, subsys, blkio_files,
1571 ARRAY_SIZE(blkio_files));
1572}
1573
9f13ef67
TH
1574/**
1575 * blkiocg_pre_destroy - cgroup pre_destroy callback
1576 * @subsys: cgroup subsys
1577 * @cgroup: cgroup of interest
1578 *
1579 * This function is called when @cgroup is about to go away and responsible
1580 * for shooting down all blkgs associated with @cgroup. blkgs should be
1581 * removed while holding both q and blkcg locks. As blkcg lock is nested
1582 * inside q lock, this function performs reverse double lock dancing.
1583 *
1584 * This is the blkcg counterpart of ioc_release_fn().
1585 */
7ee9c562
TH
1586static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
1587 struct cgroup *cgroup)
31e4c28d
VG
1588{
1589 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
b1c35769 1590
9f13ef67 1591 spin_lock_irq(&blkcg->lock);
7ee9c562 1592
9f13ef67
TH
1593 while (!hlist_empty(&blkcg->blkg_list)) {
1594 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
1595 struct blkio_group, blkcg_node);
c875f4d0 1596 struct request_queue *q = blkg->q;
b1c35769 1597
9f13ef67
TH
1598 if (spin_trylock(q->queue_lock)) {
1599 blkg_destroy(blkg);
1600 spin_unlock(q->queue_lock);
1601 } else {
1602 spin_unlock_irq(&blkcg->lock);
9f13ef67 1603 cpu_relax();
9f13ef67 1604 spin_lock(&blkcg->lock);
0f3942a3 1605 }
9f13ef67 1606 }
b1c35769 1607
9f13ef67 1608 spin_unlock_irq(&blkcg->lock);
7ee9c562
TH
1609 return 0;
1610}
1611
1612static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1613{
1614 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1615
67523c48
BB
1616 if (blkcg != &blkio_root_cgroup)
1617 kfree(blkcg);
31e4c28d
VG
1618}
1619
1620static struct cgroup_subsys_state *
1621blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1622{
9a9e8a26 1623 static atomic64_t id_seq = ATOMIC64_INIT(0);
0341509f
LZ
1624 struct blkio_cgroup *blkcg;
1625 struct cgroup *parent = cgroup->parent;
31e4c28d 1626
0341509f 1627 if (!parent) {
31e4c28d
VG
1628 blkcg = &blkio_root_cgroup;
1629 goto done;
1630 }
1631
31e4c28d
VG
1632 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1633 if (!blkcg)
1634 return ERR_PTR(-ENOMEM);
1635
1636 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
9a9e8a26 1637 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
31e4c28d
VG
1638done:
1639 spin_lock_init(&blkcg->lock);
1640 INIT_HLIST_HEAD(&blkcg->blkg_list);
1641
1642 return &blkcg->css;
1643}
1644
5efd6113
TH
1645/**
1646 * blkcg_init_queue - initialize blkcg part of request queue
1647 * @q: request_queue to initialize
1648 *
1649 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
1650 * part of new request_queue @q.
1651 *
1652 * RETURNS:
1653 * 0 on success, -errno on failure.
1654 */
1655int blkcg_init_queue(struct request_queue *q)
1656{
923adde1
TH
1657 int ret;
1658
5efd6113
TH
1659 might_sleep();
1660
923adde1
TH
1661 ret = blk_throtl_init(q);
1662 if (ret)
1663 return ret;
1664
1665 mutex_lock(&all_q_mutex);
1666 INIT_LIST_HEAD(&q->all_q_node);
1667 list_add_tail(&q->all_q_node, &all_q_list);
1668 mutex_unlock(&all_q_mutex);
1669
1670 return 0;
5efd6113
TH
1671}
1672
1673/**
1674 * blkcg_drain_queue - drain blkcg part of request_queue
1675 * @q: request_queue to drain
1676 *
1677 * Called from blk_drain_queue(). Responsible for draining blkcg part.
1678 */
1679void blkcg_drain_queue(struct request_queue *q)
1680{
1681 lockdep_assert_held(q->queue_lock);
1682
1683 blk_throtl_drain(q);
1684}
1685
1686/**
1687 * blkcg_exit_queue - exit and release blkcg part of request_queue
1688 * @q: request_queue being released
1689 *
1690 * Called from blk_release_queue(). Responsible for exiting blkcg part.
1691 */
1692void blkcg_exit_queue(struct request_queue *q)
1693{
923adde1
TH
1694 mutex_lock(&all_q_mutex);
1695 list_del_init(&q->all_q_node);
1696 mutex_unlock(&all_q_mutex);
1697
e8989fae
TH
1698 blkg_destroy_all(q, true);
1699
5efd6113
TH
1700 blk_throtl_exit(q);
1701}
1702
31e4c28d
VG
1703/*
1704 * We cannot support shared io contexts, as we have no mean to support
1705 * two tasks with the same ioc in two different groups without major rework
1706 * of the main cic data structures. For now we allow a task to change
1707 * its cgroup only if it's the only owner of its ioc.
1708 */
bb9d97b6
TH
1709static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
1710 struct cgroup_taskset *tset)
31e4c28d 1711{
bb9d97b6 1712 struct task_struct *task;
31e4c28d
VG
1713 struct io_context *ioc;
1714 int ret = 0;
1715
1716 /* task_lock() is needed to avoid races with exit_io_context() */
bb9d97b6
TH
1717 cgroup_taskset_for_each(task, cgrp, tset) {
1718 task_lock(task);
1719 ioc = task->io_context;
1720 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1721 ret = -EINVAL;
1722 task_unlock(task);
1723 if (ret)
1724 break;
1725 }
31e4c28d
VG
1726 return ret;
1727}
1728
923adde1
TH
1729static void blkcg_bypass_start(void)
1730 __acquires(&all_q_mutex)
1731{
1732 struct request_queue *q;
1733
1734 mutex_lock(&all_q_mutex);
1735
1736 list_for_each_entry(q, &all_q_list, all_q_node) {
1737 blk_queue_bypass_start(q);
e8989fae 1738 blkg_destroy_all(q, false);
923adde1
TH
1739 }
1740}
1741
1742static void blkcg_bypass_end(void)
1743 __releases(&all_q_mutex)
1744{
1745 struct request_queue *q;
1746
1747 list_for_each_entry(q, &all_q_list, all_q_node)
1748 blk_queue_bypass_end(q);
1749
1750 mutex_unlock(&all_q_mutex);
1751}
1752
3e252066
VG
1753void blkio_policy_register(struct blkio_policy_type *blkiop)
1754{
e8989fae
TH
1755 struct request_queue *q;
1756
923adde1 1757 blkcg_bypass_start();
3e252066 1758 spin_lock(&blkio_list_lock);
035d10b2
TH
1759
1760 BUG_ON(blkio_policy[blkiop->plid]);
1761 blkio_policy[blkiop->plid] = blkiop;
3e252066 1762 list_add_tail(&blkiop->list, &blkio_list);
035d10b2 1763
3e252066 1764 spin_unlock(&blkio_list_lock);
e8989fae
TH
1765 list_for_each_entry(q, &all_q_list, all_q_node)
1766 update_root_blkg_pd(q, blkiop->plid);
923adde1 1767 blkcg_bypass_end();
3e252066
VG
1768}
1769EXPORT_SYMBOL_GPL(blkio_policy_register);
1770
1771void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1772{
e8989fae
TH
1773 struct request_queue *q;
1774
923adde1 1775 blkcg_bypass_start();
3e252066 1776 spin_lock(&blkio_list_lock);
035d10b2
TH
1777
1778 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
1779 blkio_policy[blkiop->plid] = NULL;
3e252066 1780 list_del_init(&blkiop->list);
035d10b2 1781
3e252066 1782 spin_unlock(&blkio_list_lock);
e8989fae
TH
1783 list_for_each_entry(q, &all_q_list, all_q_node)
1784 update_root_blkg_pd(q, blkiop->plid);
923adde1 1785 blkcg_bypass_end();
3e252066
VG
1786}
1787EXPORT_SYMBOL_GPL(blkio_policy_unregister);