blkcg: cfq doesn't need per-cpu dispatch stats
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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 14#include <linux/kdev_t.h>
9d6a986c 15#include <linux/module.h>
accee785 16#include <linux/err.h>
9195291e 17#include <linux/blkdev.h>
5a0e3ad6 18#include <linux/slab.h>
34d0f179 19#include <linux/genhd.h>
72e06c25 20#include <linux/delay.h>
9a9e8a26 21#include <linux/atomic.h>
72e06c25 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
31e4c28d
VG
45struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
46{
47 return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
48 struct blkio_cgroup, css);
49}
9d6a986c 50EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
31e4c28d 51
4f85cb96 52static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk)
70087dc3
VG
53{
54 return container_of(task_subsys_state(tsk, blkio_subsys_id),
55 struct blkio_cgroup, css);
56}
4f85cb96
TH
57
58struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio)
59{
60 if (bio && bio->bi_css)
61 return container_of(bio->bi_css, struct blkio_cgroup, css);
62 return task_blkio_cgroup(current);
63}
64EXPORT_SYMBOL_GPL(bio_blkio_cgroup);
70087dc3 65
1cd9e039
VG
66/*
67 * Worker for allocating per cpu stat for blk groups. This is scheduled on
68 * the system_nrt_wq once there are some groups on the alloc_list waiting
69 * for allocation.
70 */
71static void blkio_stat_alloc_fn(struct work_struct *work)
72{
73 static void *pcpu_stats[BLKIO_NR_POLICIES];
74 struct delayed_work *dwork = to_delayed_work(work);
75 struct blkio_group *blkg;
76 int i;
77 bool empty = false;
78
79alloc_stats:
80 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
81 if (pcpu_stats[i] != NULL)
82 continue;
83
84 pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu);
85
86 /* Allocation failed. Try again after some time. */
87 if (pcpu_stats[i] == NULL) {
88 queue_delayed_work(system_nrt_wq, dwork,
89 msecs_to_jiffies(10));
90 return;
91 }
92 }
93
94 spin_lock_irq(&blkio_list_lock);
95 spin_lock(&alloc_list_lock);
96
97 /* cgroup got deleted or queue exited. */
98 if (!list_empty(&alloc_list)) {
99 blkg = list_first_entry(&alloc_list, struct blkio_group,
100 alloc_node);
101 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
102 struct blkg_policy_data *pd = blkg->pd[i];
103
104 if (blkio_policy[i] && pd && !pd->stats_cpu)
105 swap(pd->stats_cpu, pcpu_stats[i]);
106 }
107
108 list_del_init(&blkg->alloc_node);
109 }
110
111 empty = list_empty(&alloc_list);
112
113 spin_unlock(&alloc_list_lock);
114 spin_unlock_irq(&blkio_list_lock);
115
116 if (!empty)
117 goto alloc_stats;
118}
119
0381411e
TH
120/**
121 * blkg_free - free a blkg
122 * @blkg: blkg to free
123 *
124 * Free @blkg which may be partially allocated.
125 */
126static void blkg_free(struct blkio_group *blkg)
127{
e8989fae 128 int i;
549d3aa8
TH
129
130 if (!blkg)
131 return;
132
e8989fae
TH
133 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
134 struct blkg_policy_data *pd = blkg->pd[i];
135
136 if (pd) {
137 free_percpu(pd->stats_cpu);
138 kfree(pd);
139 }
0381411e 140 }
e8989fae 141
549d3aa8 142 kfree(blkg);
0381411e
TH
143}
144
145/**
146 * blkg_alloc - allocate a blkg
147 * @blkcg: block cgroup the new blkg is associated with
148 * @q: request_queue the new blkg is associated with
0381411e 149 *
e8989fae 150 * Allocate a new blkg assocating @blkcg and @q.
0381411e
TH
151 */
152static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,
e8989fae 153 struct request_queue *q)
0381411e
TH
154{
155 struct blkio_group *blkg;
e8989fae 156 int i;
0381411e
TH
157
158 /* alloc and init base part */
159 blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node);
160 if (!blkg)
161 return NULL;
162
c875f4d0 163 blkg->q = q;
e8989fae 164 INIT_LIST_HEAD(&blkg->q_node);
1cd9e039 165 INIT_LIST_HEAD(&blkg->alloc_node);
0381411e 166 blkg->blkcg = blkcg;
1adaf3dd 167 blkg->refcnt = 1;
0381411e
TH
168 cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
169
e8989fae
TH
170 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
171 struct blkio_policy_type *pol = blkio_policy[i];
172 struct blkg_policy_data *pd;
0381411e 173
e8989fae
TH
174 if (!pol)
175 continue;
176
177 /* alloc per-policy data and attach it to blkg */
178 pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC,
179 q->node);
180 if (!pd) {
181 blkg_free(blkg);
182 return NULL;
183 }
549d3aa8 184
e8989fae
TH
185 blkg->pd[i] = pd;
186 pd->blkg = blkg;
0381411e
TH
187 }
188
549d3aa8 189 /* invoke per-policy init */
e8989fae
TH
190 for (i = 0; i < BLKIO_NR_POLICIES; i++) {
191 struct blkio_policy_type *pol = blkio_policy[i];
192
193 if (pol)
194 pol->ops.blkio_init_group_fn(blkg);
195 }
196
0381411e
TH
197 return blkg;
198}
199
cd1604fa
TH
200struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
201 struct request_queue *q,
cd1604fa
TH
202 bool for_root)
203 __releases(q->queue_lock) __acquires(q->queue_lock)
5624a4e4 204{
1cd9e039 205 struct blkio_group *blkg;
5624a4e4 206
cd1604fa
TH
207 WARN_ON_ONCE(!rcu_read_lock_held());
208 lockdep_assert_held(q->queue_lock);
209
210 /*
211 * This could be the first entry point of blkcg implementation and
212 * we shouldn't allow anything to go through for a bypassing queue.
213 * The following can be removed if blkg lookup is guaranteed to
214 * fail on a bypassing queue.
215 */
216 if (unlikely(blk_queue_bypass(q)) && !for_root)
217 return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY);
218
e8989fae 219 blkg = blkg_lookup(blkcg, q);
cd1604fa
TH
220 if (blkg)
221 return blkg;
222
7ee9c562 223 /* blkg holds a reference to blkcg */
cd1604fa
TH
224 if (!css_tryget(&blkcg->css))
225 return ERR_PTR(-EINVAL);
226
227 /*
228 * Allocate and initialize.
cd1604fa 229 */
1cd9e039 230 blkg = blkg_alloc(blkcg, q);
cd1604fa
TH
231
232 /* did alloc fail? */
1cd9e039 233 if (unlikely(!blkg)) {
cd1604fa
TH
234 blkg = ERR_PTR(-ENOMEM);
235 goto out;
236 }
237
238 /* insert */
239 spin_lock(&blkcg->lock);
31e4c28d 240 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
e8989fae 241 list_add(&blkg->q_node, &q->blkg_list);
cd1604fa 242 spin_unlock(&blkcg->lock);
1cd9e039
VG
243
244 spin_lock(&alloc_list_lock);
245 list_add(&blkg->alloc_node, &alloc_list);
246 /* Queue per cpu stat allocation from worker thread. */
247 queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0);
248 spin_unlock(&alloc_list_lock);
cd1604fa 249out:
cd1604fa 250 return blkg;
31e4c28d 251}
cd1604fa 252EXPORT_SYMBOL_GPL(blkg_lookup_create);
31e4c28d 253
31e4c28d 254/* called under rcu_read_lock(). */
cd1604fa 255struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
e8989fae 256 struct request_queue *q)
31e4c28d
VG
257{
258 struct blkio_group *blkg;
259 struct hlist_node *n;
31e4c28d 260
ca32aefc 261 hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node)
e8989fae 262 if (blkg->q == q)
31e4c28d 263 return blkg;
31e4c28d
VG
264 return NULL;
265}
cd1604fa 266EXPORT_SYMBOL_GPL(blkg_lookup);
31e4c28d 267
e8989fae 268static void blkg_destroy(struct blkio_group *blkg)
03aa264a
TH
269{
270 struct request_queue *q = blkg->q;
9f13ef67 271 struct blkio_cgroup *blkcg = blkg->blkcg;
03aa264a
TH
272
273 lockdep_assert_held(q->queue_lock);
9f13ef67 274 lockdep_assert_held(&blkcg->lock);
03aa264a
TH
275
276 /* Something wrong if we are trying to remove same group twice */
e8989fae 277 WARN_ON_ONCE(list_empty(&blkg->q_node));
9f13ef67 278 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
e8989fae 279 list_del_init(&blkg->q_node);
9f13ef67 280 hlist_del_init_rcu(&blkg->blkcg_node);
03aa264a 281
1cd9e039
VG
282 spin_lock(&alloc_list_lock);
283 list_del_init(&blkg->alloc_node);
284 spin_unlock(&alloc_list_lock);
285
03aa264a
TH
286 /*
287 * Put the reference taken at the time of creation so that when all
288 * queues are gone, group can be destroyed.
289 */
290 blkg_put(blkg);
291}
292
e8989fae
TH
293/*
294 * XXX: This updates blkg policy data in-place for root blkg, which is
295 * necessary across elevator switch and policy registration as root blkgs
296 * aren't shot down. This broken and racy implementation is temporary.
297 * Eventually, blkg shoot down will be replaced by proper in-place update.
298 */
299void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid)
300{
301 struct blkio_policy_type *pol = blkio_policy[plid];
302 struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q);
303 struct blkg_policy_data *pd;
304
305 if (!blkg)
306 return;
307
308 kfree(blkg->pd[plid]);
309 blkg->pd[plid] = NULL;
310
311 if (!pol)
312 return;
313
314 pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL);
315 WARN_ON_ONCE(!pd);
316
317 pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu);
318 WARN_ON_ONCE(!pd->stats_cpu);
319
320 blkg->pd[plid] = pd;
321 pd->blkg = blkg;
322 pol->ops.blkio_init_group_fn(blkg);
323}
324EXPORT_SYMBOL_GPL(update_root_blkg_pd);
325
9f13ef67
TH
326/**
327 * blkg_destroy_all - destroy all blkgs associated with a request_queue
328 * @q: request_queue of interest
329 * @destroy_root: whether to destroy root blkg or not
330 *
331 * Destroy blkgs associated with @q. If @destroy_root is %true, all are
332 * destroyed; otherwise, root blkg is left alone.
333 */
e8989fae 334void blkg_destroy_all(struct request_queue *q, bool destroy_root)
72e06c25 335{
03aa264a 336 struct blkio_group *blkg, *n;
72e06c25 337
9f13ef67 338 spin_lock_irq(q->queue_lock);
72e06c25 339
9f13ef67
TH
340 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
341 struct blkio_cgroup *blkcg = blkg->blkcg;
72e06c25 342
9f13ef67
TH
343 /* skip root? */
344 if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
345 continue;
72e06c25 346
9f13ef67
TH
347 spin_lock(&blkcg->lock);
348 blkg_destroy(blkg);
349 spin_unlock(&blkcg->lock);
72e06c25 350 }
9f13ef67
TH
351
352 spin_unlock_irq(q->queue_lock);
72e06c25 353}
03aa264a 354EXPORT_SYMBOL_GPL(blkg_destroy_all);
72e06c25 355
1adaf3dd
TH
356static void blkg_rcu_free(struct rcu_head *rcu_head)
357{
358 blkg_free(container_of(rcu_head, struct blkio_group, rcu_head));
359}
360
361void __blkg_release(struct blkio_group *blkg)
362{
363 /* release the extra blkcg reference this blkg has been holding */
364 css_put(&blkg->blkcg->css);
365
366 /*
367 * A group is freed in rcu manner. But having an rcu lock does not
368 * mean that one can access all the fields of blkg and assume these
369 * are valid. For example, don't try to follow throtl_data and
370 * request queue links.
371 *
372 * Having a reference to blkg under an rcu allows acess to only
373 * values local to groups like group stats and group rate limits
374 */
375 call_rcu(&blkg->rcu_head, blkg_rcu_free);
376}
377EXPORT_SYMBOL_GPL(__blkg_release);
378
c1768268 379static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)
f0bdc8cd 380{
c1768268 381 struct blkg_policy_data *pd = blkg->pd[plid];
997a026c 382 int cpu;
1cd9e039
VG
383
384 if (pd->stats_cpu == NULL)
385 return;
997a026c
TH
386
387 for_each_possible_cpu(cpu) {
388 struct blkio_group_stats_cpu *sc =
389 per_cpu_ptr(pd->stats_cpu, cpu);
390
edcb0722
TH
391 blkg_rwstat_reset(&sc->service_bytes);
392 blkg_rwstat_reset(&sc->serviced);
f0bdc8cd
VG
393 }
394}
395
303a3acb 396static int
84c124da 397blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
303a3acb 398{
997a026c 399 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
303a3acb
DS
400 struct blkio_group *blkg;
401 struct hlist_node *n;
303a3acb 402
e8989fae 403 spin_lock(&blkio_list_lock);
303a3acb 404 spin_lock_irq(&blkcg->lock);
997a026c
TH
405
406 /*
407 * Note that stat reset is racy - it doesn't synchronize against
408 * stat updates. This is a debug feature which shouldn't exist
409 * anyway. If you get hit by a race, retry.
410 */
303a3acb 411 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
e8989fae 412 struct blkio_policy_type *pol;
549d3aa8 413
e8989fae
TH
414 list_for_each_entry(pol, &blkio_list, list) {
415 struct blkg_policy_data *pd = blkg->pd[pol->plid];
997a026c
TH
416 struct blkio_group_stats *stats = &pd->stats;
417
418 /* queued stats shouldn't be cleared */
41b38b6d
TH
419 blkg_rwstat_reset(&stats->service_bytes);
420 blkg_rwstat_reset(&stats->serviced);
edcb0722
TH
421 blkg_rwstat_reset(&stats->merged);
422 blkg_rwstat_reset(&stats->service_time);
423 blkg_rwstat_reset(&stats->wait_time);
424 blkg_stat_reset(&stats->time);
812df48d 425#ifdef CONFIG_DEBUG_BLK_CGROUP
edcb0722
TH
426 blkg_stat_reset(&stats->unaccounted_time);
427 blkg_stat_reset(&stats->avg_queue_size_sum);
428 blkg_stat_reset(&stats->avg_queue_size_samples);
429 blkg_stat_reset(&stats->dequeue);
430 blkg_stat_reset(&stats->group_wait_time);
431 blkg_stat_reset(&stats->idle_time);
432 blkg_stat_reset(&stats->empty_time);
812df48d 433#endif
e8989fae
TH
434 blkio_reset_stats_cpu(blkg, pol->plid);
435 }
303a3acb 436 }
f0bdc8cd 437
303a3acb 438 spin_unlock_irq(&blkcg->lock);
e8989fae 439 spin_unlock(&blkio_list_lock);
303a3acb
DS
440 return 0;
441}
442
d3d32e69 443static const char *blkg_dev_name(struct blkio_group *blkg)
303a3acb 444{
d3d32e69
TH
445 /* some drivers (floppy) instantiate a queue w/o disk registered */
446 if (blkg->q->backing_dev_info.dev)
447 return dev_name(blkg->q->backing_dev_info.dev);
448 return NULL;
303a3acb
DS
449}
450
d3d32e69
TH
451/**
452 * blkcg_print_blkgs - helper for printing per-blkg data
453 * @sf: seq_file to print to
454 * @blkcg: blkcg of interest
455 * @prfill: fill function to print out a blkg
456 * @pol: policy in question
457 * @data: data to be passed to @prfill
458 * @show_total: to print out sum of prfill return values or not
459 *
460 * This function invokes @prfill on each blkg of @blkcg if pd for the
461 * policy specified by @pol exists. @prfill is invoked with @sf, the
462 * policy data and @data. If @show_total is %true, the sum of the return
463 * values from @prfill is printed with "Total" label at the end.
464 *
465 * This is to be used to construct print functions for
466 * cftype->read_seq_string method.
467 */
829fdb50
TH
468void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg,
469 u64 (*prfill)(struct seq_file *, struct blkg_policy_data *, int),
470 int pol, int data, bool show_total)
5624a4e4 471{
d3d32e69
TH
472 struct blkio_group *blkg;
473 struct hlist_node *n;
474 u64 total = 0;
5624a4e4 475
d3d32e69
TH
476 spin_lock_irq(&blkcg->lock);
477 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node)
478 if (blkg->pd[pol])
479 total += prfill(sf, blkg->pd[pol], data);
480 spin_unlock_irq(&blkcg->lock);
481
482 if (show_total)
483 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
484}
829fdb50 485EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
d3d32e69
TH
486
487/**
488 * __blkg_prfill_u64 - prfill helper for a single u64 value
489 * @sf: seq_file to print to
490 * @pd: policy data of interest
491 * @v: value to print
492 *
493 * Print @v to @sf for the device assocaited with @pd.
494 */
829fdb50 495u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
d3d32e69
TH
496{
497 const char *dname = blkg_dev_name(pd->blkg);
498
499 if (!dname)
500 return 0;
501
502 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
503 return v;
504}
829fdb50 505EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
d3d32e69
TH
506
507/**
508 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
509 * @sf: seq_file to print to
510 * @pd: policy data of interest
511 * @rwstat: rwstat to print
512 *
513 * Print @rwstat to @sf for the device assocaited with @pd.
514 */
829fdb50
TH
515u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
516 const struct blkg_rwstat *rwstat)
d3d32e69
TH
517{
518 static const char *rwstr[] = {
519 [BLKG_RWSTAT_READ] = "Read",
520 [BLKG_RWSTAT_WRITE] = "Write",
521 [BLKG_RWSTAT_SYNC] = "Sync",
522 [BLKG_RWSTAT_ASYNC] = "Async",
523 };
524 const char *dname = blkg_dev_name(pd->blkg);
525 u64 v;
526 int i;
527
528 if (!dname)
529 return 0;
530
531 for (i = 0; i < BLKG_RWSTAT_NR; i++)
532 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
533 (unsigned long long)rwstat->cnt[i]);
534
535 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
536 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
537 return v;
538}
539
540static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd,
541 int off)
542{
543 return __blkg_prfill_u64(sf, pd,
544 blkg_stat_read((void *)&pd->stats + off));
545}
546
547static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
548 int off)
549{
550 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off);
551
552 return __blkg_prfill_rwstat(sf, pd, &rwstat);
553}
554
555/* print blkg_stat specified by BLKCG_STAT_PRIV() */
829fdb50
TH
556int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft,
557 struct seq_file *sf)
d3d32e69
TH
558{
559 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
560
561 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat,
562 BLKCG_STAT_POL(cft->private),
563 BLKCG_STAT_OFF(cft->private), false);
564 return 0;
565}
829fdb50 566EXPORT_SYMBOL_GPL(blkcg_print_stat);
d3d32e69
TH
567
568/* print blkg_rwstat specified by BLKCG_STAT_PRIV() */
829fdb50
TH
569int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
570 struct seq_file *sf)
d3d32e69
TH
571{
572 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
573
574 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat,
575 BLKCG_STAT_POL(cft->private),
576 BLKCG_STAT_OFF(cft->private), true);
577 return 0;
578}
829fdb50 579EXPORT_SYMBOL_GPL(blkcg_print_rwstat);
d3d32e69 580
3a8b31d3
TH
581/**
582 * blkg_conf_prep - parse and prepare for per-blkg config update
583 * @blkcg: target block cgroup
584 * @input: input string
585 * @ctx: blkg_conf_ctx to be filled
586 *
587 * Parse per-blkg config update from @input and initialize @ctx with the
588 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
589 * value. This function returns with RCU read locked and must be paired
590 * with blkg_conf_finish().
591 */
829fdb50
TH
592int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input,
593 struct blkg_conf_ctx *ctx)
3a8b31d3 594 __acquires(rcu)
34d0f179 595{
3a8b31d3
TH
596 struct gendisk *disk;
597 struct blkio_group *blkg;
726fa694
TH
598 unsigned int major, minor;
599 unsigned long long v;
600 int part, ret;
34d0f179 601
726fa694
TH
602 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
603 return -EINVAL;
3a8b31d3 604
726fa694 605 disk = get_gendisk(MKDEV(major, minor), &part);
4bfd482e 606 if (!disk || part)
726fa694 607 return -EINVAL;
e56da7e2
TH
608
609 rcu_read_lock();
610
4bfd482e 611 spin_lock_irq(disk->queue->queue_lock);
aaec55a0 612 blkg = blkg_lookup_create(blkcg, disk->queue, false);
4bfd482e 613 spin_unlock_irq(disk->queue->queue_lock);
e56da7e2 614
4bfd482e
TH
615 if (IS_ERR(blkg)) {
616 ret = PTR_ERR(blkg);
3a8b31d3
TH
617 rcu_read_unlock();
618 put_disk(disk);
619 /*
620 * If queue was bypassing, we should retry. Do so after a
621 * short msleep(). It isn't strictly necessary but queue
622 * can be bypassing for some time and it's always nice to
623 * avoid busy looping.
624 */
625 if (ret == -EBUSY) {
626 msleep(10);
627 ret = restart_syscall();
7702e8f4 628 }
726fa694 629 return ret;
062a644d 630 }
3a8b31d3
TH
631
632 ctx->disk = disk;
633 ctx->blkg = blkg;
726fa694
TH
634 ctx->v = v;
635 return 0;
34d0f179 636}
829fdb50 637EXPORT_SYMBOL_GPL(blkg_conf_prep);
34d0f179 638
3a8b31d3
TH
639/**
640 * blkg_conf_finish - finish up per-blkg config update
641 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
642 *
643 * Finish up after per-blkg config update. This function must be paired
644 * with blkg_conf_prep().
645 */
829fdb50 646void blkg_conf_finish(struct blkg_conf_ctx *ctx)
3a8b31d3 647 __releases(rcu)
34d0f179 648{
3a8b31d3
TH
649 rcu_read_unlock();
650 put_disk(ctx->disk);
34d0f179 651}
829fdb50 652EXPORT_SYMBOL_GPL(blkg_conf_finish);
34d0f179 653
31e4c28d 654struct cftype blkio_files[] = {
84c124da
DS
655 {
656 .name = "reset_stats",
657 .write_u64 = blkiocg_reset_stats,
22084190 658 },
4baf6e33 659 { } /* terminate */
31e4c28d
VG
660};
661
9f13ef67
TH
662/**
663 * blkiocg_pre_destroy - cgroup pre_destroy callback
9f13ef67
TH
664 * @cgroup: cgroup of interest
665 *
666 * This function is called when @cgroup is about to go away and responsible
667 * for shooting down all blkgs associated with @cgroup. blkgs should be
668 * removed while holding both q and blkcg locks. As blkcg lock is nested
669 * inside q lock, this function performs reverse double lock dancing.
670 *
671 * This is the blkcg counterpart of ioc_release_fn().
672 */
959d851c 673static int blkiocg_pre_destroy(struct cgroup *cgroup)
31e4c28d
VG
674{
675 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
b1c35769 676
9f13ef67 677 spin_lock_irq(&blkcg->lock);
7ee9c562 678
9f13ef67
TH
679 while (!hlist_empty(&blkcg->blkg_list)) {
680 struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first,
681 struct blkio_group, blkcg_node);
c875f4d0 682 struct request_queue *q = blkg->q;
b1c35769 683
9f13ef67
TH
684 if (spin_trylock(q->queue_lock)) {
685 blkg_destroy(blkg);
686 spin_unlock(q->queue_lock);
687 } else {
688 spin_unlock_irq(&blkcg->lock);
9f13ef67 689 cpu_relax();
a5567932 690 spin_lock_irq(&blkcg->lock);
0f3942a3 691 }
9f13ef67 692 }
b1c35769 693
9f13ef67 694 spin_unlock_irq(&blkcg->lock);
7ee9c562
TH
695 return 0;
696}
697
959d851c 698static void blkiocg_destroy(struct cgroup *cgroup)
7ee9c562
TH
699{
700 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
701
67523c48
BB
702 if (blkcg != &blkio_root_cgroup)
703 kfree(blkcg);
31e4c28d
VG
704}
705
761b3ef5 706static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
31e4c28d 707{
9a9e8a26 708 static atomic64_t id_seq = ATOMIC64_INIT(0);
0341509f
LZ
709 struct blkio_cgroup *blkcg;
710 struct cgroup *parent = cgroup->parent;
31e4c28d 711
0341509f 712 if (!parent) {
31e4c28d
VG
713 blkcg = &blkio_root_cgroup;
714 goto done;
715 }
716
31e4c28d
VG
717 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
718 if (!blkcg)
719 return ERR_PTR(-ENOMEM);
720
721 blkcg->weight = BLKIO_WEIGHT_DEFAULT;
9a9e8a26 722 blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
31e4c28d
VG
723done:
724 spin_lock_init(&blkcg->lock);
725 INIT_HLIST_HEAD(&blkcg->blkg_list);
726
727 return &blkcg->css;
728}
729
5efd6113
TH
730/**
731 * blkcg_init_queue - initialize blkcg part of request queue
732 * @q: request_queue to initialize
733 *
734 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
735 * part of new request_queue @q.
736 *
737 * RETURNS:
738 * 0 on success, -errno on failure.
739 */
740int blkcg_init_queue(struct request_queue *q)
741{
923adde1
TH
742 int ret;
743
5efd6113
TH
744 might_sleep();
745
923adde1
TH
746 ret = blk_throtl_init(q);
747 if (ret)
748 return ret;
749
750 mutex_lock(&all_q_mutex);
751 INIT_LIST_HEAD(&q->all_q_node);
752 list_add_tail(&q->all_q_node, &all_q_list);
753 mutex_unlock(&all_q_mutex);
754
755 return 0;
5efd6113
TH
756}
757
758/**
759 * blkcg_drain_queue - drain blkcg part of request_queue
760 * @q: request_queue to drain
761 *
762 * Called from blk_drain_queue(). Responsible for draining blkcg part.
763 */
764void blkcg_drain_queue(struct request_queue *q)
765{
766 lockdep_assert_held(q->queue_lock);
767
768 blk_throtl_drain(q);
769}
770
771/**
772 * blkcg_exit_queue - exit and release blkcg part of request_queue
773 * @q: request_queue being released
774 *
775 * Called from blk_release_queue(). Responsible for exiting blkcg part.
776 */
777void blkcg_exit_queue(struct request_queue *q)
778{
923adde1
TH
779 mutex_lock(&all_q_mutex);
780 list_del_init(&q->all_q_node);
781 mutex_unlock(&all_q_mutex);
782
e8989fae
TH
783 blkg_destroy_all(q, true);
784
5efd6113
TH
785 blk_throtl_exit(q);
786}
787
31e4c28d
VG
788/*
789 * We cannot support shared io contexts, as we have no mean to support
790 * two tasks with the same ioc in two different groups without major rework
791 * of the main cic data structures. For now we allow a task to change
792 * its cgroup only if it's the only owner of its ioc.
793 */
761b3ef5 794static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
31e4c28d 795{
bb9d97b6 796 struct task_struct *task;
31e4c28d
VG
797 struct io_context *ioc;
798 int ret = 0;
799
800 /* task_lock() is needed to avoid races with exit_io_context() */
bb9d97b6
TH
801 cgroup_taskset_for_each(task, cgrp, tset) {
802 task_lock(task);
803 ioc = task->io_context;
804 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
805 ret = -EINVAL;
806 task_unlock(task);
807 if (ret)
808 break;
809 }
31e4c28d
VG
810 return ret;
811}
812
923adde1
TH
813static void blkcg_bypass_start(void)
814 __acquires(&all_q_mutex)
815{
816 struct request_queue *q;
817
818 mutex_lock(&all_q_mutex);
819
820 list_for_each_entry(q, &all_q_list, all_q_node) {
821 blk_queue_bypass_start(q);
e8989fae 822 blkg_destroy_all(q, false);
923adde1
TH
823 }
824}
825
826static void blkcg_bypass_end(void)
827 __releases(&all_q_mutex)
828{
829 struct request_queue *q;
830
831 list_for_each_entry(q, &all_q_list, all_q_node)
832 blk_queue_bypass_end(q);
833
834 mutex_unlock(&all_q_mutex);
835}
836
676f7c8f
TH
837struct cgroup_subsys blkio_subsys = {
838 .name = "blkio",
839 .create = blkiocg_create,
840 .can_attach = blkiocg_can_attach,
959d851c 841 .pre_destroy = blkiocg_pre_destroy,
676f7c8f 842 .destroy = blkiocg_destroy,
676f7c8f 843 .subsys_id = blkio_subsys_id,
4baf6e33 844 .base_cftypes = blkio_files,
676f7c8f
TH
845 .module = THIS_MODULE,
846};
847EXPORT_SYMBOL_GPL(blkio_subsys);
848
3e252066
VG
849void blkio_policy_register(struct blkio_policy_type *blkiop)
850{
e8989fae
TH
851 struct request_queue *q;
852
923adde1 853 blkcg_bypass_start();
3e252066 854 spin_lock(&blkio_list_lock);
035d10b2
TH
855
856 BUG_ON(blkio_policy[blkiop->plid]);
857 blkio_policy[blkiop->plid] = blkiop;
3e252066 858 list_add_tail(&blkiop->list, &blkio_list);
035d10b2 859
3e252066 860 spin_unlock(&blkio_list_lock);
e8989fae
TH
861 list_for_each_entry(q, &all_q_list, all_q_node)
862 update_root_blkg_pd(q, blkiop->plid);
923adde1 863 blkcg_bypass_end();
44ea53de
TH
864
865 if (blkiop->cftypes)
866 WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes));
3e252066
VG
867}
868EXPORT_SYMBOL_GPL(blkio_policy_register);
869
870void blkio_policy_unregister(struct blkio_policy_type *blkiop)
871{
e8989fae
TH
872 struct request_queue *q;
873
44ea53de
TH
874 if (blkiop->cftypes)
875 cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes);
876
923adde1 877 blkcg_bypass_start();
3e252066 878 spin_lock(&blkio_list_lock);
035d10b2
TH
879
880 BUG_ON(blkio_policy[blkiop->plid] != blkiop);
881 blkio_policy[blkiop->plid] = NULL;
3e252066 882 list_del_init(&blkiop->list);
035d10b2 883
3e252066 884 spin_unlock(&blkio_list_lock);
e8989fae
TH
885 list_for_each_entry(q, &all_q_list, all_q_node)
886 update_root_blkg_pd(q, blkiop->plid);
923adde1 887 blkcg_bypass_end();
3e252066
VG
888}
889EXPORT_SYMBOL_GPL(blkio_policy_unregister);