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 #include <linux/ioprio.h>
14 #include <linux/kdev_t.h>
15 #include <linux/module.h>
16 #include <linux/err.h>
17 #include <linux/blkdev.h>
18 #include <linux/backing-dev.h>
19 #include <linux/slab.h>
20 #include <linux/genhd.h>
21 #include <linux/delay.h>
22 #include <linux/atomic.h>
23 #include <linux/blk-cgroup.h>
26 #define MAX_KEY_LEN 100
28 static DEFINE_MUTEX(blkcg_pol_mutex);
30 struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
31 .cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
32 EXPORT_SYMBOL_GPL(blkcg_root);
34 struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
36 static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
38 static bool blkcg_policy_enabled(struct request_queue *q,
39 const struct blkcg_policy *pol)
41 return pol && test_bit(pol->plid, q->blkcg_pols);
45 * blkg_free - free a blkg
48 * Free @blkg which may be partially allocated.
50 static void blkg_free(struct blkcg_gq *blkg)
57 for (i = 0; i < BLKCG_MAX_POLS; i++)
60 blk_exit_rl(&blkg->rl);
65 * blkg_alloc - allocate a blkg
66 * @blkcg: block cgroup the new blkg is associated with
67 * @q: request_queue the new blkg is associated with
68 * @gfp_mask: allocation mask to use
70 * Allocate a new blkg assocating @blkcg and @q.
72 static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
75 struct blkcg_gq *blkg;
78 /* alloc and init base part */
79 blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
84 INIT_LIST_HEAD(&blkg->q_node);
86 atomic_set(&blkg->refcnt, 1);
88 /* root blkg uses @q->root_rl, init rl only for !root blkgs */
89 if (blkcg != &blkcg_root) {
90 if (blk_init_rl(&blkg->rl, q, gfp_mask))
95 for (i = 0; i < BLKCG_MAX_POLS; i++) {
96 struct blkcg_policy *pol = blkcg_policy[i];
97 struct blkg_policy_data *pd;
99 if (!blkcg_policy_enabled(q, pol))
102 /* alloc per-policy data and attach it to blkg */
103 pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
120 * __blkg_lookup - internal version of blkg_lookup()
121 * @blkcg: blkcg of interest
122 * @q: request_queue of interest
123 * @update_hint: whether to update lookup hint with the result or not
125 * This is internal version and shouldn't be used by policy
126 * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
127 * @q's bypass state. If @update_hint is %true, the caller should be
128 * holding @q->queue_lock and lookup hint is updated on success.
130 struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, struct request_queue *q,
133 struct blkcg_gq *blkg;
135 blkg = rcu_dereference(blkcg->blkg_hint);
136 if (blkg && blkg->q == q)
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 * blkg_lookup - lookup blkg for the specified blkcg - q pair
159 * @blkcg: blkcg of interest
160 * @q: request_queue of interest
162 * Lookup blkg for the @blkcg - @q pair. This function should be called
163 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
164 * - see blk_queue_bypass_start() for details.
166 struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
168 WARN_ON_ONCE(!rcu_read_lock_held());
170 if (unlikely(blk_queue_bypass(q)))
172 return __blkg_lookup(blkcg, q, false);
174 EXPORT_SYMBOL_GPL(blkg_lookup);
177 * If @new_blkg is %NULL, this function tries to allocate a new one as
178 * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
180 static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
181 struct request_queue *q,
182 struct blkcg_gq *new_blkg)
184 struct blkcg_gq *blkg;
187 WARN_ON_ONCE(!rcu_read_lock_held());
188 lockdep_assert_held(q->queue_lock);
190 /* blkg holds a reference to blkcg */
191 if (!css_tryget_online(&blkcg->css)) {
198 new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
199 if (unlikely(!new_blkg)) {
207 if (blkcg_parent(blkcg)) {
208 blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
209 if (WARN_ON_ONCE(!blkg->parent)) {
213 blkg_get(blkg->parent);
216 /* invoke per-policy init */
217 for (i = 0; i < BLKCG_MAX_POLS; i++) {
218 struct blkcg_policy *pol = blkcg_policy[i];
220 if (blkg->pd[i] && pol->pd_init_fn)
221 pol->pd_init_fn(blkg);
225 spin_lock(&blkcg->lock);
226 ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
228 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
229 list_add(&blkg->q_node, &q->blkg_list);
231 for (i = 0; i < BLKCG_MAX_POLS; i++) {
232 struct blkcg_policy *pol = blkcg_policy[i];
234 if (blkg->pd[i] && pol->pd_online_fn)
235 pol->pd_online_fn(blkg);
239 spin_unlock(&blkcg->lock);
244 /* @blkg failed fully initialized, use the usual release path */
249 css_put(&blkcg->css);
256 * blkg_lookup_create - lookup blkg, try to create one if not there
257 * @blkcg: blkcg of interest
258 * @q: request_queue of interest
260 * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
261 * create one. blkg creation is performed recursively from blkcg_root such
262 * that all non-root blkg's have access to the parent blkg. This function
263 * should be called under RCU read lock and @q->queue_lock.
265 * Returns pointer to the looked up or created blkg on success, ERR_PTR()
266 * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
267 * dead and bypassing, returns ERR_PTR(-EBUSY).
269 struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
270 struct request_queue *q)
272 struct blkcg_gq *blkg;
274 WARN_ON_ONCE(!rcu_read_lock_held());
275 lockdep_assert_held(q->queue_lock);
278 * This could be the first entry point of blkcg implementation and
279 * we shouldn't allow anything to go through for a bypassing queue.
281 if (unlikely(blk_queue_bypass(q)))
282 return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
284 blkg = __blkg_lookup(blkcg, q, true);
289 * Create blkgs walking down from blkcg_root to @blkcg, so that all
290 * non-root blkgs have access to their parents.
293 struct blkcg *pos = blkcg;
294 struct blkcg *parent = blkcg_parent(blkcg);
296 while (parent && !__blkg_lookup(parent, q, false)) {
298 parent = blkcg_parent(parent);
301 blkg = blkg_create(pos, q, NULL);
302 if (pos == blkcg || IS_ERR(blkg))
306 EXPORT_SYMBOL_GPL(blkg_lookup_create);
308 static void blkg_destroy(struct blkcg_gq *blkg)
310 struct blkcg *blkcg = blkg->blkcg;
313 lockdep_assert_held(blkg->q->queue_lock);
314 lockdep_assert_held(&blkcg->lock);
316 /* Something wrong if we are trying to remove same group twice */
317 WARN_ON_ONCE(list_empty(&blkg->q_node));
318 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
320 for (i = 0; i < BLKCG_MAX_POLS; i++) {
321 struct blkcg_policy *pol = blkcg_policy[i];
323 if (blkg->pd[i] && pol->pd_offline_fn)
324 pol->pd_offline_fn(blkg);
326 blkg->online = false;
328 radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
329 list_del_init(&blkg->q_node);
330 hlist_del_init_rcu(&blkg->blkcg_node);
333 * Both setting lookup hint to and clearing it from @blkg are done
334 * under queue_lock. If it's not pointing to @blkg now, it never
335 * will. Hint assignment itself can race safely.
337 if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
338 rcu_assign_pointer(blkcg->blkg_hint, NULL);
341 * Put the reference taken at the time of creation so that when all
342 * queues are gone, group can be destroyed.
348 * blkg_destroy_all - destroy all blkgs associated with a request_queue
349 * @q: request_queue of interest
351 * Destroy all blkgs associated with @q.
353 static void blkg_destroy_all(struct request_queue *q)
355 struct blkcg_gq *blkg, *n;
357 lockdep_assert_held(q->queue_lock);
359 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
360 struct blkcg *blkcg = blkg->blkcg;
362 spin_lock(&blkcg->lock);
364 spin_unlock(&blkcg->lock);
369 * A group is RCU protected, but having an rcu lock does not mean that one
370 * can access all the fields of blkg and assume these are valid. For
371 * example, don't try to follow throtl_data and request queue links.
373 * Having a reference to blkg under an rcu allows accesses to only values
374 * local to groups like group stats and group rate limits.
376 void __blkg_release_rcu(struct rcu_head *rcu_head)
378 struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head);
381 /* tell policies that this one is being freed */
382 for (i = 0; i < BLKCG_MAX_POLS; i++) {
383 struct blkcg_policy *pol = blkcg_policy[i];
385 if (blkg->pd[i] && pol->pd_exit_fn)
386 pol->pd_exit_fn(blkg);
389 /* release the blkcg and parent blkg refs this blkg has been holding */
390 css_put(&blkg->blkcg->css);
392 blkg_put(blkg->parent);
396 EXPORT_SYMBOL_GPL(__blkg_release_rcu);
399 * The next function used by blk_queue_for_each_rl(). It's a bit tricky
400 * because the root blkg uses @q->root_rl instead of its own rl.
402 struct request_list *__blk_queue_next_rl(struct request_list *rl,
403 struct request_queue *q)
405 struct list_head *ent;
406 struct blkcg_gq *blkg;
409 * Determine the current blkg list_head. The first entry is
410 * root_rl which is off @q->blkg_list and mapped to the head.
412 if (rl == &q->root_rl) {
414 /* There are no more block groups, hence no request lists */
418 blkg = container_of(rl, struct blkcg_gq, rl);
422 /* walk to the next list_head, skip root blkcg */
424 if (ent == &q->root_blkg->q_node)
426 if (ent == &q->blkg_list)
429 blkg = container_of(ent, struct blkcg_gq, q_node);
433 static int blkcg_reset_stats(struct cgroup_subsys_state *css,
434 struct cftype *cftype, u64 val)
436 struct blkcg *blkcg = css_to_blkcg(css);
437 struct blkcg_gq *blkg;
441 * XXX: We invoke cgroup_add/rm_cftypes() under blkcg_pol_mutex
442 * which ends up putting cgroup's internal cgroup_tree_mutex under
443 * it; however, cgroup_tree_mutex is nested above cgroup file
444 * active protection and grabbing blkcg_pol_mutex from a cgroup
445 * file operation creates a possible circular dependency. cgroup
446 * internal locking is planned to go through further simplification
447 * and this issue should go away soon. For now, let's trylock
448 * blkcg_pol_mutex and restart the write on failure.
450 * http://lkml.kernel.org/g/5363C04B.4010400@oracle.com
452 if (!mutex_trylock(&blkcg_pol_mutex))
453 return restart_syscall();
454 spin_lock_irq(&blkcg->lock);
457 * Note that stat reset is racy - it doesn't synchronize against
458 * stat updates. This is a debug feature which shouldn't exist
459 * anyway. If you get hit by a race, retry.
461 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
462 for (i = 0; i < BLKCG_MAX_POLS; i++) {
463 struct blkcg_policy *pol = blkcg_policy[i];
465 if (blkcg_policy_enabled(blkg->q, pol) &&
466 pol->pd_reset_stats_fn)
467 pol->pd_reset_stats_fn(blkg);
471 spin_unlock_irq(&blkcg->lock);
472 mutex_unlock(&blkcg_pol_mutex);
476 static const char *blkg_dev_name(struct blkcg_gq *blkg)
478 /* some drivers (floppy) instantiate a queue w/o disk registered */
479 if (blkg->q->backing_dev_info.dev)
480 return dev_name(blkg->q->backing_dev_info.dev);
485 * blkcg_print_blkgs - helper for printing per-blkg data
486 * @sf: seq_file to print to
487 * @blkcg: blkcg of interest
488 * @prfill: fill function to print out a blkg
489 * @pol: policy in question
490 * @data: data to be passed to @prfill
491 * @show_total: to print out sum of prfill return values or not
493 * This function invokes @prfill on each blkg of @blkcg if pd for the
494 * policy specified by @pol exists. @prfill is invoked with @sf, the
495 * policy data and @data and the matching queue lock held. If @show_total
496 * is %true, the sum of the return values from @prfill is printed with
497 * "Total" label at the end.
499 * This is to be used to construct print functions for
500 * cftype->read_seq_string method.
502 void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
503 u64 (*prfill)(struct seq_file *,
504 struct blkg_policy_data *, int),
505 const struct blkcg_policy *pol, int data,
508 struct blkcg_gq *blkg;
512 hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
513 spin_lock_irq(blkg->q->queue_lock);
514 if (blkcg_policy_enabled(blkg->q, pol))
515 total += prfill(sf, blkg->pd[pol->plid], data);
516 spin_unlock_irq(blkg->q->queue_lock);
521 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
523 EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
526 * __blkg_prfill_u64 - prfill helper for a single u64 value
527 * @sf: seq_file to print to
528 * @pd: policy private data of interest
531 * Print @v to @sf for the device assocaited with @pd.
533 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
535 const char *dname = blkg_dev_name(pd->blkg);
540 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
543 EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
546 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
547 * @sf: seq_file to print to
548 * @pd: policy private data of interest
549 * @rwstat: rwstat to print
551 * Print @rwstat to @sf for the device assocaited with @pd.
553 u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
554 const struct blkg_rwstat *rwstat)
556 static const char *rwstr[] = {
557 [BLKG_RWSTAT_READ] = "Read",
558 [BLKG_RWSTAT_WRITE] = "Write",
559 [BLKG_RWSTAT_SYNC] = "Sync",
560 [BLKG_RWSTAT_ASYNC] = "Async",
562 const char *dname = blkg_dev_name(pd->blkg);
569 for (i = 0; i < BLKG_RWSTAT_NR; i++)
570 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
571 (unsigned long long)rwstat->cnt[i]);
573 v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
574 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
577 EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
580 * blkg_prfill_stat - prfill callback for blkg_stat
581 * @sf: seq_file to print to
582 * @pd: policy private data of interest
583 * @off: offset to the blkg_stat in @pd
585 * prfill callback for printing a blkg_stat.
587 u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
589 return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
591 EXPORT_SYMBOL_GPL(blkg_prfill_stat);
594 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
595 * @sf: seq_file to print to
596 * @pd: policy private data of interest
597 * @off: offset to the blkg_rwstat in @pd
599 * prfill callback for printing a blkg_rwstat.
601 u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
604 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
606 return __blkg_prfill_rwstat(sf, pd, &rwstat);
608 EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
611 * blkg_stat_recursive_sum - collect hierarchical blkg_stat
612 * @pd: policy private data of interest
613 * @off: offset to the blkg_stat in @pd
615 * Collect the blkg_stat specified by @off from @pd and all its online
616 * descendants and return the sum. The caller must be holding the queue
617 * lock for online tests.
619 u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off)
621 struct blkcg_policy *pol = blkcg_policy[pd->plid];
622 struct blkcg_gq *pos_blkg;
623 struct cgroup_subsys_state *pos_css;
626 lockdep_assert_held(pd->blkg->q->queue_lock);
629 blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) {
630 struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
631 struct blkg_stat *stat = (void *)pos_pd + off;
633 if (pos_blkg->online)
634 sum += blkg_stat_read(stat);
640 EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
643 * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
644 * @pd: policy private data of interest
645 * @off: offset to the blkg_stat in @pd
647 * Collect the blkg_rwstat specified by @off from @pd and all its online
648 * descendants and return the sum. The caller must be holding the queue
649 * lock for online tests.
651 struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
654 struct blkcg_policy *pol = blkcg_policy[pd->plid];
655 struct blkcg_gq *pos_blkg;
656 struct cgroup_subsys_state *pos_css;
657 struct blkg_rwstat sum = { };
660 lockdep_assert_held(pd->blkg->q->queue_lock);
663 blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) {
664 struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
665 struct blkg_rwstat *rwstat = (void *)pos_pd + off;
666 struct blkg_rwstat tmp;
668 if (!pos_blkg->online)
671 tmp = blkg_rwstat_read(rwstat);
673 for (i = 0; i < BLKG_RWSTAT_NR; i++)
674 sum.cnt[i] += tmp.cnt[i];
680 EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
683 * blkg_conf_prep - parse and prepare for per-blkg config update
684 * @blkcg: target block cgroup
685 * @pol: target policy
686 * @input: input string
687 * @ctx: blkg_conf_ctx to be filled
689 * Parse per-blkg config update from @input and initialize @ctx with the
690 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
691 * value. This function returns with RCU read lock and queue lock held and
692 * must be paired with blkg_conf_finish().
694 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
695 const char *input, struct blkg_conf_ctx *ctx)
696 __acquires(rcu) __acquires(disk->queue->queue_lock)
698 struct gendisk *disk;
699 struct blkcg_gq *blkg;
700 unsigned int major, minor;
701 unsigned long long v;
704 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
707 disk = get_gendisk(MKDEV(major, minor), &part);
712 spin_lock_irq(disk->queue->queue_lock);
714 if (blkcg_policy_enabled(disk->queue, pol))
715 blkg = blkg_lookup_create(blkcg, disk->queue);
717 blkg = ERR_PTR(-EINVAL);
722 spin_unlock_irq(disk->queue->queue_lock);
725 * If queue was bypassing, we should retry. Do so after a
726 * short msleep(). It isn't strictly necessary but queue
727 * can be bypassing for some time and it's always nice to
728 * avoid busy looping.
732 ret = restart_syscall();
742 EXPORT_SYMBOL_GPL(blkg_conf_prep);
745 * blkg_conf_finish - finish up per-blkg config update
746 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
748 * Finish up after per-blkg config update. This function must be paired
749 * with blkg_conf_prep().
751 void blkg_conf_finish(struct blkg_conf_ctx *ctx)
752 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
754 spin_unlock_irq(ctx->disk->queue->queue_lock);
758 EXPORT_SYMBOL_GPL(blkg_conf_finish);
760 struct cftype blkcg_files[] = {
762 .name = "reset_stats",
763 .write_u64 = blkcg_reset_stats,
769 * blkcg_css_offline - cgroup css_offline callback
770 * @css: css of interest
772 * This function is called when @css is about to go away and responsible
773 * for shooting down all blkgs associated with @css. blkgs should be
774 * removed while holding both q and blkcg locks. As blkcg lock is nested
775 * inside q lock, this function performs reverse double lock dancing.
777 * This is the blkcg counterpart of ioc_release_fn().
779 static void blkcg_css_offline(struct cgroup_subsys_state *css)
781 struct blkcg *blkcg = css_to_blkcg(css);
783 spin_lock_irq(&blkcg->lock);
785 while (!hlist_empty(&blkcg->blkg_list)) {
786 struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
787 struct blkcg_gq, blkcg_node);
788 struct request_queue *q = blkg->q;
790 if (spin_trylock(q->queue_lock)) {
792 spin_unlock(q->queue_lock);
794 spin_unlock_irq(&blkcg->lock);
796 spin_lock_irq(&blkcg->lock);
800 spin_unlock_irq(&blkcg->lock);
802 wb_blkcg_offline(blkcg);
805 static void blkcg_css_free(struct cgroup_subsys_state *css)
807 struct blkcg *blkcg = css_to_blkcg(css);
809 if (blkcg != &blkcg_root)
813 static struct cgroup_subsys_state *
814 blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
823 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
825 return ERR_PTR(-ENOMEM);
827 blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
828 blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
830 spin_lock_init(&blkcg->lock);
831 INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
832 INIT_HLIST_HEAD(&blkcg->blkg_list);
833 #ifdef CONFIG_CGROUP_WRITEBACK
834 INIT_LIST_HEAD(&blkcg->cgwb_list);
840 * blkcg_init_queue - initialize blkcg part of request queue
841 * @q: request_queue to initialize
843 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
844 * part of new request_queue @q.
847 * 0 on success, -errno on failure.
849 int blkcg_init_queue(struct request_queue *q)
851 struct blkcg_gq *new_blkg, *blkg;
855 new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
859 preloaded = !radix_tree_preload(GFP_KERNEL);
862 * Make sure the root blkg exists and count the existing blkgs. As
863 * @q is bypassing at this point, blkg_lookup_create() can't be
864 * used. Open code insertion.
867 spin_lock_irq(q->queue_lock);
868 blkg = blkg_create(&blkcg_root, q, new_blkg);
869 spin_unlock_irq(q->queue_lock);
873 radix_tree_preload_end();
877 return PTR_ERR(blkg);
881 q->root_rl.blkg = blkg;
883 ret = blk_throtl_init(q);
885 spin_lock_irq(q->queue_lock);
887 spin_unlock_irq(q->queue_lock);
893 * blkcg_drain_queue - drain blkcg part of request_queue
894 * @q: request_queue to drain
896 * Called from blk_drain_queue(). Responsible for draining blkcg part.
898 void blkcg_drain_queue(struct request_queue *q)
900 lockdep_assert_held(q->queue_lock);
903 * @q could be exiting and already have destroyed all blkgs as
904 * indicated by NULL root_blkg. If so, don't confuse policies.
913 * blkcg_exit_queue - exit and release blkcg part of request_queue
914 * @q: request_queue being released
916 * Called from blk_release_queue(). Responsible for exiting blkcg part.
918 void blkcg_exit_queue(struct request_queue *q)
920 spin_lock_irq(q->queue_lock);
922 spin_unlock_irq(q->queue_lock);
928 * We cannot support shared io contexts, as we have no mean to support
929 * two tasks with the same ioc in two different groups without major rework
930 * of the main cic data structures. For now we allow a task to change
931 * its cgroup only if it's the only owner of its ioc.
933 static int blkcg_can_attach(struct cgroup_subsys_state *css,
934 struct cgroup_taskset *tset)
936 struct task_struct *task;
937 struct io_context *ioc;
940 /* task_lock() is needed to avoid races with exit_io_context() */
941 cgroup_taskset_for_each(task, tset) {
943 ioc = task->io_context;
944 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
953 struct cgroup_subsys blkio_cgrp_subsys = {
954 .css_alloc = blkcg_css_alloc,
955 .css_offline = blkcg_css_offline,
956 .css_free = blkcg_css_free,
957 .can_attach = blkcg_can_attach,
958 .legacy_cftypes = blkcg_files,
961 * This ensures that, if available, memcg is automatically enabled
962 * together on the default hierarchy so that the owner cgroup can
963 * be retrieved from writeback pages.
965 .depends_on = 1 << memory_cgrp_id,
968 EXPORT_SYMBOL_GPL(blkio_cgrp_subsys);
971 * blkcg_activate_policy - activate a blkcg policy on a request_queue
972 * @q: request_queue of interest
973 * @pol: blkcg policy to activate
975 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
976 * bypass mode to populate its blkgs with policy_data for @pol.
978 * Activation happens with @q bypassed, so nobody would be accessing blkgs
979 * from IO path. Update of each blkg is protected by both queue and blkcg
980 * locks so that holding either lock and testing blkcg_policy_enabled() is
981 * always enough for dereferencing policy data.
983 * The caller is responsible for synchronizing [de]activations and policy
984 * [un]registerations. Returns 0 on success, -errno on failure.
986 int blkcg_activate_policy(struct request_queue *q,
987 const struct blkcg_policy *pol)
990 struct blkcg_gq *blkg;
991 struct blkg_policy_data *pd, *n;
994 if (blkcg_policy_enabled(q, pol))
997 /* count and allocate policy_data for all existing blkgs */
998 blk_queue_bypass_start(q);
999 spin_lock_irq(q->queue_lock);
1000 list_for_each_entry(blkg, &q->blkg_list, q_node)
1002 spin_unlock_irq(q->queue_lock);
1005 pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
1010 list_add_tail(&pd->alloc_node, &pds);
1014 * Install the allocated pds. With @q bypassing, no new blkg
1015 * should have been created while the queue lock was dropped.
1017 spin_lock_irq(q->queue_lock);
1019 list_for_each_entry(blkg, &q->blkg_list, q_node) {
1020 if (WARN_ON(list_empty(&pds))) {
1021 /* umm... this shouldn't happen, just abort */
1025 pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
1026 list_del_init(&pd->alloc_node);
1028 /* grab blkcg lock too while installing @pd on @blkg */
1029 spin_lock(&blkg->blkcg->lock);
1031 blkg->pd[pol->plid] = pd;
1033 pd->plid = pol->plid;
1034 pol->pd_init_fn(blkg);
1036 spin_unlock(&blkg->blkcg->lock);
1039 __set_bit(pol->plid, q->blkcg_pols);
1042 spin_unlock_irq(q->queue_lock);
1044 blk_queue_bypass_end(q);
1045 list_for_each_entry_safe(pd, n, &pds, alloc_node)
1049 EXPORT_SYMBOL_GPL(blkcg_activate_policy);
1052 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
1053 * @q: request_queue of interest
1054 * @pol: blkcg policy to deactivate
1056 * Deactivate @pol on @q. Follows the same synchronization rules as
1057 * blkcg_activate_policy().
1059 void blkcg_deactivate_policy(struct request_queue *q,
1060 const struct blkcg_policy *pol)
1062 struct blkcg_gq *blkg;
1064 if (!blkcg_policy_enabled(q, pol))
1067 blk_queue_bypass_start(q);
1068 spin_lock_irq(q->queue_lock);
1070 __clear_bit(pol->plid, q->blkcg_pols);
1072 list_for_each_entry(blkg, &q->blkg_list, q_node) {
1073 /* grab blkcg lock too while removing @pd from @blkg */
1074 spin_lock(&blkg->blkcg->lock);
1076 if (pol->pd_offline_fn)
1077 pol->pd_offline_fn(blkg);
1078 if (pol->pd_exit_fn)
1079 pol->pd_exit_fn(blkg);
1081 kfree(blkg->pd[pol->plid]);
1082 blkg->pd[pol->plid] = NULL;
1084 spin_unlock(&blkg->blkcg->lock);
1087 spin_unlock_irq(q->queue_lock);
1088 blk_queue_bypass_end(q);
1090 EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
1093 * blkcg_policy_register - register a blkcg policy
1094 * @pol: blkcg policy to register
1096 * Register @pol with blkcg core. Might sleep and @pol may be modified on
1097 * successful registration. Returns 0 on success and -errno on failure.
1099 int blkcg_policy_register(struct blkcg_policy *pol)
1103 if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
1106 mutex_lock(&blkcg_pol_mutex);
1108 /* find an empty slot */
1110 for (i = 0; i < BLKCG_MAX_POLS; i++)
1111 if (!blkcg_policy[i])
1113 if (i >= BLKCG_MAX_POLS)
1116 /* register and update blkgs */
1118 blkcg_policy[i] = pol;
1120 /* everything is in place, add intf files for the new policy */
1122 WARN_ON(cgroup_add_legacy_cftypes(&blkio_cgrp_subsys,
1126 mutex_unlock(&blkcg_pol_mutex);
1129 EXPORT_SYMBOL_GPL(blkcg_policy_register);
1132 * blkcg_policy_unregister - unregister a blkcg policy
1133 * @pol: blkcg policy to unregister
1135 * Undo blkcg_policy_register(@pol). Might sleep.
1137 void blkcg_policy_unregister(struct blkcg_policy *pol)
1139 mutex_lock(&blkcg_pol_mutex);
1141 if (WARN_ON(blkcg_policy[pol->plid] != pol))
1144 /* kill the intf files first */
1146 cgroup_rm_cftypes(pol->cftypes);
1148 /* unregister and update blkgs */
1149 blkcg_policy[pol->plid] = NULL;
1151 mutex_unlock(&blkcg_pol_mutex);
1153 EXPORT_SYMBOL_GPL(blkcg_policy_unregister);