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