2 * Common Block IO controller cgroup interface
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
13 * For policy-specific per-blkcg data:
14 * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
15 * Arianna Avanzini <avanzini.arianna@gmail.com>
17 #include <linux/ioprio.h>
18 #include <linux/kdev_t.h>
19 #include <linux/module.h>
20 #include <linux/err.h>
21 #include <linux/blkdev.h>
22 #include <linux/backing-dev.h>
23 #include <linux/slab.h>
24 #include <linux/genhd.h>
25 #include <linux/delay.h>
26 #include <linux/atomic.h>
27 #include <linux/blk-cgroup.h>
30 #define MAX_KEY_LEN 100
33 * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
34 * blkcg_pol_register_mutex nests outside of it and synchronizes entire
35 * policy [un]register operations including cgroup file additions /
36 * removals. Putting cgroup file registration outside blkcg_pol_mutex
37 * allows grabbing it from cgroup callbacks.
39 static DEFINE_MUTEX(blkcg_pol_register_mutex
);
40 static DEFINE_MUTEX(blkcg_pol_mutex
);
42 struct blkcg blkcg_root
;
43 EXPORT_SYMBOL_GPL(blkcg_root
);
45 struct cgroup_subsys_state
* const blkcg_root_css
= &blkcg_root
.css
;
47 static struct blkcg_policy
*blkcg_policy
[BLKCG_MAX_POLS
];
49 static LIST_HEAD(all_blkcgs
); /* protected by blkcg_pol_mutex */
51 static bool blkcg_policy_enabled(struct request_queue
*q
,
52 const struct blkcg_policy
*pol
)
54 return pol
&& test_bit(pol
->plid
, q
->blkcg_pols
);
58 * blkg_free - free a blkg
61 * Free @blkg which may be partially allocated.
63 static void blkg_free(struct blkcg_gq
*blkg
)
70 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++)
72 blkcg_policy
[i
]->pd_free_fn(blkg
->pd
[i
]);
74 if (blkg
->blkcg
!= &blkcg_root
)
75 blk_exit_rl(&blkg
->rl
);
80 * blkg_alloc - allocate a blkg
81 * @blkcg: block cgroup the new blkg is associated with
82 * @q: request_queue the new blkg is associated with
83 * @gfp_mask: allocation mask to use
85 * Allocate a new blkg assocating @blkcg and @q.
87 static struct blkcg_gq
*blkg_alloc(struct blkcg
*blkcg
, struct request_queue
*q
,
90 struct blkcg_gq
*blkg
;
93 /* alloc and init base part */
94 blkg
= kzalloc_node(sizeof(*blkg
), gfp_mask
, q
->node
);
99 INIT_LIST_HEAD(&blkg
->q_node
);
101 atomic_set(&blkg
->refcnt
, 1);
103 /* root blkg uses @q->root_rl, init rl only for !root blkgs */
104 if (blkcg
!= &blkcg_root
) {
105 if (blk_init_rl(&blkg
->rl
, q
, gfp_mask
))
107 blkg
->rl
.blkg
= blkg
;
110 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++) {
111 struct blkcg_policy
*pol
= blkcg_policy
[i
];
112 struct blkg_policy_data
*pd
;
114 if (!blkcg_policy_enabled(q
, pol
))
117 /* alloc per-policy data and attach it to blkg */
118 pd
= pol
->pd_alloc_fn(gfp_mask
, q
->node
);
134 struct blkcg_gq
*blkg_lookup_slowpath(struct blkcg
*blkcg
,
135 struct request_queue
*q
, bool update_hint
)
137 struct blkcg_gq
*blkg
;
140 * Hint didn't match. Look up from the radix tree. Note that the
141 * hint can only be updated under queue_lock as otherwise @blkg
142 * could have already been removed from blkg_tree. The caller is
143 * responsible for grabbing queue_lock if @update_hint.
145 blkg
= radix_tree_lookup(&blkcg
->blkg_tree
, q
->id
);
146 if (blkg
&& blkg
->q
== q
) {
148 lockdep_assert_held(q
->queue_lock
);
149 rcu_assign_pointer(blkcg
->blkg_hint
, blkg
);
158 * If @new_blkg is %NULL, this function tries to allocate a new one as
159 * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
161 static struct blkcg_gq
*blkg_create(struct blkcg
*blkcg
,
162 struct request_queue
*q
,
163 struct blkcg_gq
*new_blkg
)
165 struct blkcg_gq
*blkg
;
166 struct bdi_writeback_congested
*wb_congested
;
169 WARN_ON_ONCE(!rcu_read_lock_held());
170 lockdep_assert_held(q
->queue_lock
);
172 /* blkg holds a reference to blkcg */
173 if (!css_tryget_online(&blkcg
->css
)) {
178 wb_congested
= wb_congested_get_create(&q
->backing_dev_info
,
179 blkcg
->css
.id
, GFP_NOWAIT
);
187 new_blkg
= blkg_alloc(blkcg
, q
, GFP_NOWAIT
);
188 if (unlikely(!new_blkg
)) {
190 goto err_put_congested
;
194 blkg
->wb_congested
= wb_congested
;
197 if (blkcg_parent(blkcg
)) {
198 blkg
->parent
= __blkg_lookup(blkcg_parent(blkcg
), q
, false);
199 if (WARN_ON_ONCE(!blkg
->parent
)) {
201 goto err_put_congested
;
203 blkg_get(blkg
->parent
);
206 /* invoke per-policy init */
207 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++) {
208 struct blkcg_policy
*pol
= blkcg_policy
[i
];
210 if (blkg
->pd
[i
] && pol
->pd_init_fn
)
211 pol
->pd_init_fn(blkg
->pd
[i
]);
215 spin_lock(&blkcg
->lock
);
216 ret
= radix_tree_insert(&blkcg
->blkg_tree
, q
->id
, blkg
);
218 hlist_add_head_rcu(&blkg
->blkcg_node
, &blkcg
->blkg_list
);
219 list_add(&blkg
->q_node
, &q
->blkg_list
);
221 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++) {
222 struct blkcg_policy
*pol
= blkcg_policy
[i
];
224 if (blkg
->pd
[i
] && pol
->pd_online_fn
)
225 pol
->pd_online_fn(blkg
->pd
[i
]);
229 spin_unlock(&blkcg
->lock
);
234 /* @blkg failed fully initialized, use the usual release path */
239 wb_congested_put(wb_congested
);
241 css_put(&blkcg
->css
);
248 * blkg_lookup_create - lookup blkg, try to create one if not there
249 * @blkcg: blkcg of interest
250 * @q: request_queue of interest
252 * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
253 * create one. blkg creation is performed recursively from blkcg_root such
254 * that all non-root blkg's have access to the parent blkg. This function
255 * should be called under RCU read lock and @q->queue_lock.
257 * Returns pointer to the looked up or created blkg on success, ERR_PTR()
258 * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
259 * dead and bypassing, returns ERR_PTR(-EBUSY).
261 struct blkcg_gq
*blkg_lookup_create(struct blkcg
*blkcg
,
262 struct request_queue
*q
)
264 struct blkcg_gq
*blkg
;
266 WARN_ON_ONCE(!rcu_read_lock_held());
267 lockdep_assert_held(q
->queue_lock
);
270 * This could be the first entry point of blkcg implementation and
271 * we shouldn't allow anything to go through for a bypassing queue.
273 if (unlikely(blk_queue_bypass(q
)))
274 return ERR_PTR(blk_queue_dying(q
) ? -EINVAL
: -EBUSY
);
276 blkg
= __blkg_lookup(blkcg
, q
, true);
281 * Create blkgs walking down from blkcg_root to @blkcg, so that all
282 * non-root blkgs have access to their parents.
285 struct blkcg
*pos
= blkcg
;
286 struct blkcg
*parent
= blkcg_parent(blkcg
);
288 while (parent
&& !__blkg_lookup(parent
, q
, false)) {
290 parent
= blkcg_parent(parent
);
293 blkg
= blkg_create(pos
, q
, NULL
);
294 if (pos
== blkcg
|| IS_ERR(blkg
))
298 EXPORT_SYMBOL_GPL(blkg_lookup_create
);
300 static void blkg_destroy(struct blkcg_gq
*blkg
)
302 struct blkcg
*blkcg
= blkg
->blkcg
;
305 lockdep_assert_held(blkg
->q
->queue_lock
);
306 lockdep_assert_held(&blkcg
->lock
);
308 /* Something wrong if we are trying to remove same group twice */
309 WARN_ON_ONCE(list_empty(&blkg
->q_node
));
310 WARN_ON_ONCE(hlist_unhashed(&blkg
->blkcg_node
));
312 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++) {
313 struct blkcg_policy
*pol
= blkcg_policy
[i
];
315 if (blkg
->pd
[i
] && pol
->pd_offline_fn
)
316 pol
->pd_offline_fn(blkg
->pd
[i
]);
318 blkg
->online
= false;
320 radix_tree_delete(&blkcg
->blkg_tree
, blkg
->q
->id
);
321 list_del_init(&blkg
->q_node
);
322 hlist_del_init_rcu(&blkg
->blkcg_node
);
325 * Both setting lookup hint to and clearing it from @blkg are done
326 * under queue_lock. If it's not pointing to @blkg now, it never
327 * will. Hint assignment itself can race safely.
329 if (rcu_access_pointer(blkcg
->blkg_hint
) == blkg
)
330 rcu_assign_pointer(blkcg
->blkg_hint
, NULL
);
333 * Put the reference taken at the time of creation so that when all
334 * queues are gone, group can be destroyed.
340 * blkg_destroy_all - destroy all blkgs associated with a request_queue
341 * @q: request_queue of interest
343 * Destroy all blkgs associated with @q.
345 static void blkg_destroy_all(struct request_queue
*q
)
347 struct blkcg_gq
*blkg
, *n
;
349 lockdep_assert_held(q
->queue_lock
);
351 list_for_each_entry_safe(blkg
, n
, &q
->blkg_list
, q_node
) {
352 struct blkcg
*blkcg
= blkg
->blkcg
;
354 spin_lock(&blkcg
->lock
);
356 spin_unlock(&blkcg
->lock
);
361 * A group is RCU protected, but having an rcu lock does not mean that one
362 * can access all the fields of blkg and assume these are valid. For
363 * example, don't try to follow throtl_data and request queue links.
365 * Having a reference to blkg under an rcu allows accesses to only values
366 * local to groups like group stats and group rate limits.
368 void __blkg_release_rcu(struct rcu_head
*rcu_head
)
370 struct blkcg_gq
*blkg
= container_of(rcu_head
, struct blkcg_gq
, rcu_head
);
372 /* release the blkcg and parent blkg refs this blkg has been holding */
373 css_put(&blkg
->blkcg
->css
);
375 blkg_put(blkg
->parent
);
377 wb_congested_put(blkg
->wb_congested
);
381 EXPORT_SYMBOL_GPL(__blkg_release_rcu
);
384 * The next function used by blk_queue_for_each_rl(). It's a bit tricky
385 * because the root blkg uses @q->root_rl instead of its own rl.
387 struct request_list
*__blk_queue_next_rl(struct request_list
*rl
,
388 struct request_queue
*q
)
390 struct list_head
*ent
;
391 struct blkcg_gq
*blkg
;
394 * Determine the current blkg list_head. The first entry is
395 * root_rl which is off @q->blkg_list and mapped to the head.
397 if (rl
== &q
->root_rl
) {
399 /* There are no more block groups, hence no request lists */
403 blkg
= container_of(rl
, struct blkcg_gq
, rl
);
407 /* walk to the next list_head, skip root blkcg */
409 if (ent
== &q
->root_blkg
->q_node
)
411 if (ent
== &q
->blkg_list
)
414 blkg
= container_of(ent
, struct blkcg_gq
, q_node
);
418 static int blkcg_reset_stats(struct cgroup_subsys_state
*css
,
419 struct cftype
*cftype
, u64 val
)
421 struct blkcg
*blkcg
= css_to_blkcg(css
);
422 struct blkcg_gq
*blkg
;
425 mutex_lock(&blkcg_pol_mutex
);
426 spin_lock_irq(&blkcg
->lock
);
429 * Note that stat reset is racy - it doesn't synchronize against
430 * stat updates. This is a debug feature which shouldn't exist
431 * anyway. If you get hit by a race, retry.
433 hlist_for_each_entry(blkg
, &blkcg
->blkg_list
, blkcg_node
) {
434 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++) {
435 struct blkcg_policy
*pol
= blkcg_policy
[i
];
437 if (blkg
->pd
[i
] && pol
->pd_reset_stats_fn
)
438 pol
->pd_reset_stats_fn(blkg
->pd
[i
]);
442 spin_unlock_irq(&blkcg
->lock
);
443 mutex_unlock(&blkcg_pol_mutex
);
447 static const char *blkg_dev_name(struct blkcg_gq
*blkg
)
449 /* some drivers (floppy) instantiate a queue w/o disk registered */
450 if (blkg
->q
->backing_dev_info
.dev
)
451 return dev_name(blkg
->q
->backing_dev_info
.dev
);
456 * blkcg_print_blkgs - helper for printing per-blkg data
457 * @sf: seq_file to print to
458 * @blkcg: blkcg of interest
459 * @prfill: fill function to print out a blkg
460 * @pol: policy in question
461 * @data: data to be passed to @prfill
462 * @show_total: to print out sum of prfill return values or not
464 * This function invokes @prfill on each blkg of @blkcg if pd for the
465 * policy specified by @pol exists. @prfill is invoked with @sf, the
466 * policy data and @data and the matching queue lock held. If @show_total
467 * is %true, the sum of the return values from @prfill is printed with
468 * "Total" label at the end.
470 * This is to be used to construct print functions for
471 * cftype->read_seq_string method.
473 void blkcg_print_blkgs(struct seq_file
*sf
, struct blkcg
*blkcg
,
474 u64 (*prfill
)(struct seq_file
*,
475 struct blkg_policy_data
*, int),
476 const struct blkcg_policy
*pol
, int data
,
479 struct blkcg_gq
*blkg
;
483 hlist_for_each_entry_rcu(blkg
, &blkcg
->blkg_list
, blkcg_node
) {
484 spin_lock_irq(blkg
->q
->queue_lock
);
485 if (blkcg_policy_enabled(blkg
->q
, pol
))
486 total
+= prfill(sf
, blkg
->pd
[pol
->plid
], data
);
487 spin_unlock_irq(blkg
->q
->queue_lock
);
492 seq_printf(sf
, "Total %llu\n", (unsigned long long)total
);
494 EXPORT_SYMBOL_GPL(blkcg_print_blkgs
);
497 * __blkg_prfill_u64 - prfill helper for a single u64 value
498 * @sf: seq_file to print to
499 * @pd: policy private data of interest
502 * Print @v to @sf for the device assocaited with @pd.
504 u64
__blkg_prfill_u64(struct seq_file
*sf
, struct blkg_policy_data
*pd
, u64 v
)
506 const char *dname
= blkg_dev_name(pd
->blkg
);
511 seq_printf(sf
, "%s %llu\n", dname
, (unsigned long long)v
);
514 EXPORT_SYMBOL_GPL(__blkg_prfill_u64
);
517 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
518 * @sf: seq_file to print to
519 * @pd: policy private data of interest
520 * @rwstat: rwstat to print
522 * Print @rwstat to @sf for the device assocaited with @pd.
524 u64
__blkg_prfill_rwstat(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
525 const struct blkg_rwstat
*rwstat
)
527 static const char *rwstr
[] = {
528 [BLKG_RWSTAT_READ
] = "Read",
529 [BLKG_RWSTAT_WRITE
] = "Write",
530 [BLKG_RWSTAT_SYNC
] = "Sync",
531 [BLKG_RWSTAT_ASYNC
] = "Async",
533 const char *dname
= blkg_dev_name(pd
->blkg
);
540 for (i
= 0; i
< BLKG_RWSTAT_NR
; i
++)
541 seq_printf(sf
, "%s %s %llu\n", dname
, rwstr
[i
],
542 (unsigned long long)rwstat
->cnt
[i
]);
544 v
= rwstat
->cnt
[BLKG_RWSTAT_READ
] + rwstat
->cnt
[BLKG_RWSTAT_WRITE
];
545 seq_printf(sf
, "%s Total %llu\n", dname
, (unsigned long long)v
);
548 EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat
);
551 * blkg_prfill_stat - prfill callback for blkg_stat
552 * @sf: seq_file to print to
553 * @pd: policy private data of interest
554 * @off: offset to the blkg_stat in @pd
556 * prfill callback for printing a blkg_stat.
558 u64
blkg_prfill_stat(struct seq_file
*sf
, struct blkg_policy_data
*pd
, int off
)
560 return __blkg_prfill_u64(sf
, pd
, blkg_stat_read((void *)pd
+ off
));
562 EXPORT_SYMBOL_GPL(blkg_prfill_stat
);
565 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
566 * @sf: seq_file to print to
567 * @pd: policy private data of interest
568 * @off: offset to the blkg_rwstat in @pd
570 * prfill callback for printing a blkg_rwstat.
572 u64
blkg_prfill_rwstat(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
575 struct blkg_rwstat rwstat
= blkg_rwstat_read((void *)pd
+ off
);
577 return __blkg_prfill_rwstat(sf
, pd
, &rwstat
);
579 EXPORT_SYMBOL_GPL(blkg_prfill_rwstat
);
582 * blkg_stat_recursive_sum - collect hierarchical blkg_stat
583 * @pd: policy private data of interest
584 * @off: offset to the blkg_stat in @pd
586 * Collect the blkg_stat specified by @off from @pd and all its online
587 * descendants and return the sum. The caller must be holding the queue
588 * lock for online tests.
590 u64
blkg_stat_recursive_sum(struct blkg_policy_data
*pd
, int off
)
592 struct blkcg_policy
*pol
= blkcg_policy
[pd
->plid
];
593 struct blkcg_gq
*pos_blkg
;
594 struct cgroup_subsys_state
*pos_css
;
597 lockdep_assert_held(pd
->blkg
->q
->queue_lock
);
600 blkg_for_each_descendant_pre(pos_blkg
, pos_css
, pd_to_blkg(pd
)) {
601 struct blkg_policy_data
*pos_pd
= blkg_to_pd(pos_blkg
, pol
);
602 struct blkg_stat
*stat
= (void *)pos_pd
+ off
;
604 if (pos_blkg
->online
)
605 sum
+= blkg_stat_read(stat
);
611 EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum
);
614 * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
615 * @pd: policy private data of interest
616 * @off: offset to the blkg_stat in @pd
618 * Collect the blkg_rwstat specified by @off from @pd and all its online
619 * descendants and return the sum. The caller must be holding the queue
620 * lock for online tests.
622 struct blkg_rwstat
blkg_rwstat_recursive_sum(struct blkg_policy_data
*pd
,
625 struct blkcg_policy
*pol
= blkcg_policy
[pd
->plid
];
626 struct blkcg_gq
*pos_blkg
;
627 struct cgroup_subsys_state
*pos_css
;
628 struct blkg_rwstat sum
= { };
631 lockdep_assert_held(pd
->blkg
->q
->queue_lock
);
634 blkg_for_each_descendant_pre(pos_blkg
, pos_css
, pd_to_blkg(pd
)) {
635 struct blkg_policy_data
*pos_pd
= blkg_to_pd(pos_blkg
, pol
);
636 struct blkg_rwstat
*rwstat
= (void *)pos_pd
+ off
;
637 struct blkg_rwstat tmp
;
639 if (!pos_blkg
->online
)
642 tmp
= blkg_rwstat_read(rwstat
);
644 for (i
= 0; i
< BLKG_RWSTAT_NR
; i
++)
645 sum
.cnt
[i
] += tmp
.cnt
[i
];
651 EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum
);
654 * blkg_conf_prep - parse and prepare for per-blkg config update
655 * @blkcg: target block cgroup
656 * @pol: target policy
657 * @input: input string
658 * @ctx: blkg_conf_ctx to be filled
660 * Parse per-blkg config update from @input and initialize @ctx with the
661 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
662 * value. This function returns with RCU read lock and queue lock held and
663 * must be paired with blkg_conf_finish().
665 int blkg_conf_prep(struct blkcg
*blkcg
, const struct blkcg_policy
*pol
,
666 const char *input
, struct blkg_conf_ctx
*ctx
)
667 __acquires(rcu
) __acquires(disk
->queue
->queue_lock
)
669 struct gendisk
*disk
;
670 struct blkcg_gq
*blkg
;
671 unsigned int major
, minor
;
672 unsigned long long v
;
675 if (sscanf(input
, "%u:%u %llu", &major
, &minor
, &v
) != 3)
678 disk
= get_gendisk(MKDEV(major
, minor
), &part
);
687 spin_lock_irq(disk
->queue
->queue_lock
);
689 if (blkcg_policy_enabled(disk
->queue
, pol
))
690 blkg
= blkg_lookup_create(blkcg
, disk
->queue
);
692 blkg
= ERR_PTR(-EINVAL
);
697 spin_unlock_irq(disk
->queue
->queue_lock
);
700 * If queue was bypassing, we should retry. Do so after a
701 * short msleep(). It isn't strictly necessary but queue
702 * can be bypassing for some time and it's always nice to
703 * avoid busy looping.
707 ret
= restart_syscall();
717 EXPORT_SYMBOL_GPL(blkg_conf_prep
);
720 * blkg_conf_finish - finish up per-blkg config update
721 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
723 * Finish up after per-blkg config update. This function must be paired
724 * with blkg_conf_prep().
726 void blkg_conf_finish(struct blkg_conf_ctx
*ctx
)
727 __releases(ctx
->disk
->queue
->queue_lock
) __releases(rcu
)
729 spin_unlock_irq(ctx
->disk
->queue
->queue_lock
);
733 EXPORT_SYMBOL_GPL(blkg_conf_finish
);
735 struct cftype blkcg_files
[] = {
737 .name
= "reset_stats",
738 .write_u64
= blkcg_reset_stats
,
744 * blkcg_css_offline - cgroup css_offline callback
745 * @css: css of interest
747 * This function is called when @css is about to go away and responsible
748 * for shooting down all blkgs associated with @css. blkgs should be
749 * removed while holding both q and blkcg locks. As blkcg lock is nested
750 * inside q lock, this function performs reverse double lock dancing.
752 * This is the blkcg counterpart of ioc_release_fn().
754 static void blkcg_css_offline(struct cgroup_subsys_state
*css
)
756 struct blkcg
*blkcg
= css_to_blkcg(css
);
758 spin_lock_irq(&blkcg
->lock
);
760 while (!hlist_empty(&blkcg
->blkg_list
)) {
761 struct blkcg_gq
*blkg
= hlist_entry(blkcg
->blkg_list
.first
,
762 struct blkcg_gq
, blkcg_node
);
763 struct request_queue
*q
= blkg
->q
;
765 if (spin_trylock(q
->queue_lock
)) {
767 spin_unlock(q
->queue_lock
);
769 spin_unlock_irq(&blkcg
->lock
);
771 spin_lock_irq(&blkcg
->lock
);
775 spin_unlock_irq(&blkcg
->lock
);
777 wb_blkcg_offline(blkcg
);
780 static void blkcg_css_free(struct cgroup_subsys_state
*css
)
782 struct blkcg
*blkcg
= css_to_blkcg(css
);
785 mutex_lock(&blkcg_pol_mutex
);
787 list_del(&blkcg
->all_blkcgs_node
);
789 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++)
791 blkcg_policy
[i
]->cpd_free_fn(blkcg
->cpd
[i
]);
793 mutex_unlock(&blkcg_pol_mutex
);
798 static struct cgroup_subsys_state
*
799 blkcg_css_alloc(struct cgroup_subsys_state
*parent_css
)
802 struct cgroup_subsys_state
*ret
;
805 mutex_lock(&blkcg_pol_mutex
);
810 blkcg
= kzalloc(sizeof(*blkcg
), GFP_KERNEL
);
812 ret
= ERR_PTR(-ENOMEM
);
817 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++) {
818 struct blkcg_policy
*pol
= blkcg_policy
[i
];
819 struct blkcg_policy_data
*cpd
;
822 * If the policy hasn't been attached yet, wait for it
823 * to be attached before doing anything else. Otherwise,
824 * check if the policy requires any specific per-cgroup
825 * data: if it does, allocate and initialize it.
827 if (!pol
|| !pol
->cpd_alloc_fn
)
830 cpd
= pol
->cpd_alloc_fn(GFP_KERNEL
);
832 ret
= ERR_PTR(-ENOMEM
);
838 if (pol
->cpd_init_fn
)
839 pol
->cpd_init_fn(cpd
);
842 spin_lock_init(&blkcg
->lock
);
843 INIT_RADIX_TREE(&blkcg
->blkg_tree
, GFP_NOWAIT
);
844 INIT_HLIST_HEAD(&blkcg
->blkg_list
);
845 #ifdef CONFIG_CGROUP_WRITEBACK
846 INIT_LIST_HEAD(&blkcg
->cgwb_list
);
848 list_add_tail(&blkcg
->all_blkcgs_node
, &all_blkcgs
);
850 mutex_unlock(&blkcg_pol_mutex
);
854 for (i
--; i
>= 0; i
--)
856 blkcg_policy
[i
]->cpd_free_fn(blkcg
->cpd
[i
]);
859 mutex_unlock(&blkcg_pol_mutex
);
864 * blkcg_init_queue - initialize blkcg part of request queue
865 * @q: request_queue to initialize
867 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
868 * part of new request_queue @q.
871 * 0 on success, -errno on failure.
873 int blkcg_init_queue(struct request_queue
*q
)
875 struct blkcg_gq
*new_blkg
, *blkg
;
879 new_blkg
= blkg_alloc(&blkcg_root
, q
, GFP_KERNEL
);
883 preloaded
= !radix_tree_preload(GFP_KERNEL
);
886 * Make sure the root blkg exists and count the existing blkgs. As
887 * @q is bypassing at this point, blkg_lookup_create() can't be
888 * used. Open code insertion.
891 spin_lock_irq(q
->queue_lock
);
892 blkg
= blkg_create(&blkcg_root
, q
, new_blkg
);
893 spin_unlock_irq(q
->queue_lock
);
897 radix_tree_preload_end();
901 return PTR_ERR(blkg
);
905 q
->root_rl
.blkg
= blkg
;
907 ret
= blk_throtl_init(q
);
909 spin_lock_irq(q
->queue_lock
);
911 spin_unlock_irq(q
->queue_lock
);
917 * blkcg_drain_queue - drain blkcg part of request_queue
918 * @q: request_queue to drain
920 * Called from blk_drain_queue(). Responsible for draining blkcg part.
922 void blkcg_drain_queue(struct request_queue
*q
)
924 lockdep_assert_held(q
->queue_lock
);
927 * @q could be exiting and already have destroyed all blkgs as
928 * indicated by NULL root_blkg. If so, don't confuse policies.
937 * blkcg_exit_queue - exit and release blkcg part of request_queue
938 * @q: request_queue being released
940 * Called from blk_release_queue(). Responsible for exiting blkcg part.
942 void blkcg_exit_queue(struct request_queue
*q
)
944 spin_lock_irq(q
->queue_lock
);
946 spin_unlock_irq(q
->queue_lock
);
952 * We cannot support shared io contexts, as we have no mean to support
953 * two tasks with the same ioc in two different groups without major rework
954 * of the main cic data structures. For now we allow a task to change
955 * its cgroup only if it's the only owner of its ioc.
957 static int blkcg_can_attach(struct cgroup_subsys_state
*css
,
958 struct cgroup_taskset
*tset
)
960 struct task_struct
*task
;
961 struct io_context
*ioc
;
964 /* task_lock() is needed to avoid races with exit_io_context() */
965 cgroup_taskset_for_each(task
, tset
) {
967 ioc
= task
->io_context
;
968 if (ioc
&& atomic_read(&ioc
->nr_tasks
) > 1)
977 struct cgroup_subsys blkio_cgrp_subsys
= {
978 .css_alloc
= blkcg_css_alloc
,
979 .css_offline
= blkcg_css_offline
,
980 .css_free
= blkcg_css_free
,
981 .can_attach
= blkcg_can_attach
,
982 .legacy_cftypes
= blkcg_files
,
985 * This ensures that, if available, memcg is automatically enabled
986 * together on the default hierarchy so that the owner cgroup can
987 * be retrieved from writeback pages.
989 .depends_on
= 1 << memory_cgrp_id
,
992 EXPORT_SYMBOL_GPL(blkio_cgrp_subsys
);
995 * blkcg_activate_policy - activate a blkcg policy on a request_queue
996 * @q: request_queue of interest
997 * @pol: blkcg policy to activate
999 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
1000 * bypass mode to populate its blkgs with policy_data for @pol.
1002 * Activation happens with @q bypassed, so nobody would be accessing blkgs
1003 * from IO path. Update of each blkg is protected by both queue and blkcg
1004 * locks so that holding either lock and testing blkcg_policy_enabled() is
1005 * always enough for dereferencing policy data.
1007 * The caller is responsible for synchronizing [de]activations and policy
1008 * [un]registerations. Returns 0 on success, -errno on failure.
1010 int blkcg_activate_policy(struct request_queue
*q
,
1011 const struct blkcg_policy
*pol
)
1013 struct blkg_policy_data
*pd_prealloc
= NULL
;
1014 struct blkcg_gq
*blkg
;
1017 if (blkcg_policy_enabled(q
, pol
))
1020 blk_queue_bypass_start(q
);
1023 pd_prealloc
= pol
->pd_alloc_fn(GFP_KERNEL
, q
->node
);
1026 goto out_bypass_end
;
1030 spin_lock_irq(q
->queue_lock
);
1032 list_for_each_entry(blkg
, &q
->blkg_list
, q_node
) {
1033 struct blkg_policy_data
*pd
;
1035 if (blkg
->pd
[pol
->plid
])
1038 pd
= pol
->pd_alloc_fn(GFP_NOWAIT
, q
->node
);
1040 swap(pd
, pd_prealloc
);
1042 spin_unlock_irq(q
->queue_lock
);
1046 blkg
->pd
[pol
->plid
] = pd
;
1048 pd
->plid
= pol
->plid
;
1049 if (pol
->pd_init_fn
)
1050 pol
->pd_init_fn(pd
);
1053 __set_bit(pol
->plid
, q
->blkcg_pols
);
1056 spin_unlock_irq(q
->queue_lock
);
1058 blk_queue_bypass_end(q
);
1060 pol
->pd_free_fn(pd_prealloc
);
1063 EXPORT_SYMBOL_GPL(blkcg_activate_policy
);
1066 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
1067 * @q: request_queue of interest
1068 * @pol: blkcg policy to deactivate
1070 * Deactivate @pol on @q. Follows the same synchronization rules as
1071 * blkcg_activate_policy().
1073 void blkcg_deactivate_policy(struct request_queue
*q
,
1074 const struct blkcg_policy
*pol
)
1076 struct blkcg_gq
*blkg
;
1078 if (!blkcg_policy_enabled(q
, pol
))
1081 blk_queue_bypass_start(q
);
1082 spin_lock_irq(q
->queue_lock
);
1084 __clear_bit(pol
->plid
, q
->blkcg_pols
);
1086 list_for_each_entry(blkg
, &q
->blkg_list
, q_node
) {
1087 /* grab blkcg lock too while removing @pd from @blkg */
1088 spin_lock(&blkg
->blkcg
->lock
);
1090 if (blkg
->pd
[pol
->plid
]) {
1091 if (pol
->pd_offline_fn
)
1092 pol
->pd_offline_fn(blkg
->pd
[pol
->plid
]);
1093 pol
->pd_free_fn(blkg
->pd
[pol
->plid
]);
1094 blkg
->pd
[pol
->plid
] = NULL
;
1097 spin_unlock(&blkg
->blkcg
->lock
);
1100 spin_unlock_irq(q
->queue_lock
);
1101 blk_queue_bypass_end(q
);
1103 EXPORT_SYMBOL_GPL(blkcg_deactivate_policy
);
1106 * blkcg_policy_register - register a blkcg policy
1107 * @pol: blkcg policy to register
1109 * Register @pol with blkcg core. Might sleep and @pol may be modified on
1110 * successful registration. Returns 0 on success and -errno on failure.
1112 int blkcg_policy_register(struct blkcg_policy
*pol
)
1114 struct blkcg
*blkcg
;
1117 mutex_lock(&blkcg_pol_register_mutex
);
1118 mutex_lock(&blkcg_pol_mutex
);
1120 /* find an empty slot */
1122 for (i
= 0; i
< BLKCG_MAX_POLS
; i
++)
1123 if (!blkcg_policy
[i
])
1125 if (i
>= BLKCG_MAX_POLS
)
1130 blkcg_policy
[pol
->plid
] = pol
;
1132 /* allocate and install cpd's */
1133 if (pol
->cpd_alloc_fn
) {
1134 list_for_each_entry(blkcg
, &all_blkcgs
, all_blkcgs_node
) {
1135 struct blkcg_policy_data
*cpd
;
1137 cpd
= pol
->cpd_alloc_fn(GFP_KERNEL
);
1139 mutex_unlock(&blkcg_pol_mutex
);
1143 blkcg
->cpd
[pol
->plid
] = cpd
;
1145 cpd
->plid
= pol
->plid
;
1146 pol
->cpd_init_fn(cpd
);
1150 mutex_unlock(&blkcg_pol_mutex
);
1152 /* everything is in place, add intf files for the new policy */
1154 WARN_ON(cgroup_add_legacy_cftypes(&blkio_cgrp_subsys
,
1156 mutex_unlock(&blkcg_pol_register_mutex
);
1160 if (pol
->cpd_alloc_fn
) {
1161 list_for_each_entry(blkcg
, &all_blkcgs
, all_blkcgs_node
) {
1162 if (blkcg
->cpd
[pol
->plid
]) {
1163 pol
->cpd_free_fn(blkcg
->cpd
[pol
->plid
]);
1164 blkcg
->cpd
[pol
->plid
] = NULL
;
1168 blkcg_policy
[pol
->plid
] = NULL
;
1170 mutex_unlock(&blkcg_pol_mutex
);
1171 mutex_unlock(&blkcg_pol_register_mutex
);
1174 EXPORT_SYMBOL_GPL(blkcg_policy_register
);
1177 * blkcg_policy_unregister - unregister a blkcg policy
1178 * @pol: blkcg policy to unregister
1180 * Undo blkcg_policy_register(@pol). Might sleep.
1182 void blkcg_policy_unregister(struct blkcg_policy
*pol
)
1184 struct blkcg
*blkcg
;
1186 mutex_lock(&blkcg_pol_register_mutex
);
1188 if (WARN_ON(blkcg_policy
[pol
->plid
] != pol
))
1191 /* kill the intf files first */
1193 cgroup_rm_cftypes(pol
->cftypes
);
1195 /* remove cpds and unregister */
1196 mutex_lock(&blkcg_pol_mutex
);
1198 if (pol
->cpd_alloc_fn
) {
1199 list_for_each_entry(blkcg
, &all_blkcgs
, all_blkcgs_node
) {
1200 if (blkcg
->cpd
[pol
->plid
]) {
1201 pol
->cpd_free_fn(blkcg
->cpd
[pol
->plid
]);
1202 blkcg
->cpd
[pol
->plid
] = NULL
;
1206 blkcg_policy
[pol
->plid
] = NULL
;
1208 mutex_unlock(&blkcg_pol_mutex
);
1210 mutex_unlock(&blkcg_pol_register_mutex
);
1212 EXPORT_SYMBOL_GPL(blkcg_policy_unregister
);