Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 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> |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 12 | * |
| 13 | * For policy-specific per-blkcg data: |
| 14 | * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it> |
| 15 | * Arianna Avanzini <avanzini.arianna@gmail.com> |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 16 | */ |
| 17 | #include <linux/ioprio.h> |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 18 | #include <linux/kdev_t.h> |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 19 | #include <linux/module.h> |
Stephen Rothwell | accee78 | 2009-12-07 19:29:39 +1100 | [diff] [blame] | 20 | #include <linux/err.h> |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 22 | #include <linux/backing-dev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 24 | #include <linux/genhd.h> |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 25 | #include <linux/delay.h> |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 26 | #include <linux/atomic.h> |
Tejun Heo | eea8f41 | 2015-05-22 17:13:17 -0400 | [diff] [blame] | 27 | #include <linux/blk-cgroup.h> |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 28 | #include "blk.h" |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 29 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 30 | #define MAX_KEY_LEN 100 |
| 31 | |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 32 | /* |
| 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. |
| 38 | */ |
| 39 | static DEFINE_MUTEX(blkcg_pol_register_mutex); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 40 | static DEFINE_MUTEX(blkcg_pol_mutex); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 41 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 42 | struct blkcg blkcg_root; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(blkcg_root); |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 44 | |
Tejun Heo | 496d5e7 | 2015-05-22 17:13:21 -0400 | [diff] [blame] | 45 | struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css; |
| 46 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 47 | static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS]; |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 48 | |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 49 | static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */ |
| 50 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 51 | static bool blkcg_policy_enabled(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 52 | const struct blkcg_policy *pol) |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 53 | { |
| 54 | return pol && test_bit(pol->plid, q->blkcg_pols); |
| 55 | } |
| 56 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 57 | /** |
| 58 | * blkg_free - free a blkg |
| 59 | * @blkg: blkg to free |
| 60 | * |
| 61 | * Free @blkg which may be partially allocated. |
| 62 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 63 | static void blkg_free(struct blkcg_gq *blkg) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 64 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 65 | int i; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 66 | |
| 67 | if (!blkg) |
| 68 | return; |
| 69 | |
Tejun Heo | db61367 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 70 | for (i = 0; i < BLKCG_MAX_POLS; i++) |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 71 | if (blkg->pd[i]) |
| 72 | blkcg_policy[i]->pd_free_fn(blkg->pd[i]); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 73 | |
Tejun Heo | 994b783 | 2015-08-18 14:55:07 -0700 | [diff] [blame] | 74 | if (blkg->blkcg != &blkcg_root) |
| 75 | blk_exit_rl(&blkg->rl); |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 76 | kfree(blkg); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 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 |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 83 | * @gfp_mask: allocation mask to use |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 84 | * |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 85 | * Allocate a new blkg assocating @blkcg and @q. |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 86 | */ |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 87 | static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q, |
| 88 | gfp_t gfp_mask) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 89 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 90 | struct blkcg_gq *blkg; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 91 | int i; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 92 | |
| 93 | /* alloc and init base part */ |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 94 | blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 95 | if (!blkg) |
| 96 | return NULL; |
| 97 | |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 98 | blkg->q = q; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 99 | INIT_LIST_HEAD(&blkg->q_node); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 100 | blkg->blkcg = blkcg; |
Tejun Heo | a5049a8 | 2014-06-19 17:42:57 -0400 | [diff] [blame] | 101 | atomic_set(&blkg->refcnt, 1); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 102 | |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 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)) |
| 106 | goto err_free; |
| 107 | blkg->rl.blkg = blkg; |
| 108 | } |
| 109 | |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 110 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 111 | struct blkcg_policy *pol = blkcg_policy[i]; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 112 | struct blkg_policy_data *pd; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 113 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 114 | if (!blkcg_policy_enabled(q, pol)) |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 115 | continue; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 116 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 117 | /* alloc per-policy data and attach it to blkg */ |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 118 | pd = pol->pd_alloc_fn(gfp_mask, q->node); |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 119 | if (!pd) |
| 120 | goto err_free; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 121 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 122 | blkg->pd[i] = pd; |
| 123 | pd->blkg = blkg; |
Tejun Heo | b276a87 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 124 | pd->plid = i; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 127 | return blkg; |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 128 | |
| 129 | err_free: |
| 130 | blkg_free(blkg); |
| 131 | return NULL; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Tejun Heo | 24f2904 | 2015-08-18 14:55:17 -0700 | [diff] [blame] | 134 | struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, |
| 135 | struct request_queue *q, bool update_hint) |
Tejun Heo | 80fd997 | 2012-04-13 14:50:53 -0700 | [diff] [blame] | 136 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 137 | struct blkcg_gq *blkg; |
Tejun Heo | 80fd997 | 2012-04-13 14:50:53 -0700 | [diff] [blame] | 138 | |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 139 | /* |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 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. |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 144 | */ |
| 145 | blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id); |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 146 | if (blkg && blkg->q == q) { |
| 147 | if (update_hint) { |
| 148 | lockdep_assert_held(q->queue_lock); |
| 149 | rcu_assign_pointer(blkcg->blkg_hint, blkg); |
| 150 | } |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 151 | return blkg; |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 152 | } |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 153 | |
Tejun Heo | 80fd997 | 2012-04-13 14:50:53 -0700 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 156 | EXPORT_SYMBOL_GPL(blkg_lookup_slowpath); |
Tejun Heo | 80fd997 | 2012-04-13 14:50:53 -0700 | [diff] [blame] | 157 | |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 158 | /* |
| 159 | * If @new_blkg is %NULL, this function tries to allocate a new one as |
Tejun Heo | d93a11f | 2015-08-18 14:55:01 -0700 | [diff] [blame] | 160 | * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return. |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 161 | */ |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 162 | static struct blkcg_gq *blkg_create(struct blkcg *blkcg, |
| 163 | struct request_queue *q, |
| 164 | struct blkcg_gq *new_blkg) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 165 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 166 | struct blkcg_gq *blkg; |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 167 | struct bdi_writeback_congested *wb_congested; |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 168 | int i, ret; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 169 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 170 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 171 | lockdep_assert_held(q->queue_lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 172 | |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 173 | /* blkg holds a reference to blkcg */ |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 174 | if (!css_tryget_online(&blkcg->css)) { |
Tejun Heo | 93e6d5d | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 175 | ret = -EINVAL; |
| 176 | goto err_free_blkg; |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 177 | } |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 178 | |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 179 | wb_congested = wb_congested_get_create(&q->backing_dev_info, |
Tejun Heo | d93a11f | 2015-08-18 14:55:01 -0700 | [diff] [blame] | 180 | blkcg->css.id, GFP_NOWAIT); |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 181 | if (!wb_congested) { |
| 182 | ret = -ENOMEM; |
| 183 | goto err_put_css; |
| 184 | } |
| 185 | |
Tejun Heo | 496fb78 | 2012-04-19 16:29:23 -0700 | [diff] [blame] | 186 | /* allocate */ |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 187 | if (!new_blkg) { |
Tejun Heo | d93a11f | 2015-08-18 14:55:01 -0700 | [diff] [blame] | 188 | new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT); |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 189 | if (unlikely(!new_blkg)) { |
Tejun Heo | 93e6d5d | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 190 | ret = -ENOMEM; |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 191 | goto err_put_congested; |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | blkg = new_blkg; |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 195 | blkg->wb_congested = wb_congested; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 196 | |
Tejun Heo | db61367 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 197 | /* link parent */ |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 198 | if (blkcg_parent(blkcg)) { |
| 199 | blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false); |
| 200 | if (WARN_ON_ONCE(!blkg->parent)) { |
Tejun Heo | 2423c9c | 2013-05-14 13:52:30 -0700 | [diff] [blame] | 201 | ret = -EINVAL; |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 202 | goto err_put_congested; |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 203 | } |
| 204 | blkg_get(blkg->parent); |
| 205 | } |
| 206 | |
Tejun Heo | db61367 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 207 | /* invoke per-policy init */ |
| 208 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
| 209 | struct blkcg_policy *pol = blkcg_policy[i]; |
| 210 | |
| 211 | if (blkg->pd[i] && pol->pd_init_fn) |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 212 | pol->pd_init_fn(blkg->pd[i]); |
Tejun Heo | db61367 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* insert */ |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 216 | spin_lock(&blkcg->lock); |
| 217 | ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg); |
| 218 | if (likely(!ret)) { |
| 219 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
| 220 | list_add(&blkg->q_node, &q->blkg_list); |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 221 | |
| 222 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
| 223 | struct blkcg_policy *pol = blkcg_policy[i]; |
| 224 | |
| 225 | if (blkg->pd[i] && pol->pd_online_fn) |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 226 | pol->pd_online_fn(blkg->pd[i]); |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 227 | } |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 228 | } |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 229 | blkg->online = true; |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 230 | spin_unlock(&blkcg->lock); |
| 231 | |
Tejun Heo | ec13b1d | 2015-05-22 17:13:19 -0400 | [diff] [blame] | 232 | if (!ret) |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 233 | return blkg; |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 234 | |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 235 | /* @blkg failed fully initialized, use the usual release path */ |
| 236 | blkg_put(blkg); |
| 237 | return ERR_PTR(ret); |
| 238 | |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 239 | err_put_congested: |
| 240 | wb_congested_put(wb_congested); |
Tejun Heo | 93e6d5d | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 241 | err_put_css: |
Tejun Heo | 496fb78 | 2012-04-19 16:29:23 -0700 | [diff] [blame] | 242 | css_put(&blkcg->css); |
Tejun Heo | 93e6d5d | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 243 | err_free_blkg: |
Tejun Heo | 1597499 | 2012-06-04 20:40:52 -0700 | [diff] [blame] | 244 | blkg_free(new_blkg); |
Tejun Heo | 93e6d5d | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 245 | return ERR_PTR(ret); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 246 | } |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 247 | |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 248 | /** |
| 249 | * blkg_lookup_create - lookup blkg, try to create one if not there |
| 250 | * @blkcg: blkcg of interest |
| 251 | * @q: request_queue of interest |
| 252 | * |
| 253 | * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 254 | * create one. blkg creation is performed recursively from blkcg_root such |
| 255 | * that all non-root blkg's have access to the parent blkg. This function |
| 256 | * should be called under RCU read lock and @q->queue_lock. |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 257 | * |
| 258 | * Returns pointer to the looked up or created blkg on success, ERR_PTR() |
| 259 | * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not |
| 260 | * dead and bypassing, returns ERR_PTR(-EBUSY). |
| 261 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 262 | struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, |
| 263 | struct request_queue *q) |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 264 | { |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 265 | struct blkcg_gq *blkg; |
| 266 | |
| 267 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 268 | lockdep_assert_held(q->queue_lock); |
| 269 | |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 270 | /* |
| 271 | * This could be the first entry point of blkcg implementation and |
| 272 | * we shouldn't allow anything to go through for a bypassing queue. |
| 273 | */ |
| 274 | if (unlikely(blk_queue_bypass(q))) |
Bart Van Assche | 3f3299d | 2012-11-28 13:42:38 +0100 | [diff] [blame] | 275 | return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY); |
Tejun Heo | 86cde6b | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 276 | |
| 277 | blkg = __blkg_lookup(blkcg, q, true); |
| 278 | if (blkg) |
| 279 | return blkg; |
| 280 | |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 281 | /* |
| 282 | * Create blkgs walking down from blkcg_root to @blkcg, so that all |
| 283 | * non-root blkgs have access to their parents. |
| 284 | */ |
| 285 | while (true) { |
| 286 | struct blkcg *pos = blkcg; |
| 287 | struct blkcg *parent = blkcg_parent(blkcg); |
| 288 | |
| 289 | while (parent && !__blkg_lookup(parent, q, false)) { |
| 290 | pos = parent; |
| 291 | parent = blkcg_parent(parent); |
| 292 | } |
| 293 | |
| 294 | blkg = blkg_create(pos, q, NULL); |
| 295 | if (pos == blkcg || IS_ERR(blkg)) |
| 296 | return blkg; |
| 297 | } |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 298 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 299 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 300 | static void blkg_destroy(struct blkcg_gq *blkg) |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 301 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 302 | struct blkcg *blkcg = blkg->blkcg; |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 303 | int i; |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 304 | |
Tejun Heo | 27e1f9d | 2012-06-05 13:36:44 +0200 | [diff] [blame] | 305 | lockdep_assert_held(blkg->q->queue_lock); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 306 | lockdep_assert_held(&blkcg->lock); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 307 | |
| 308 | /* Something wrong if we are trying to remove same group twice */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 309 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 310 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 311 | |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 312 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
| 313 | struct blkcg_policy *pol = blkcg_policy[i]; |
| 314 | |
| 315 | if (blkg->pd[i] && pol->pd_offline_fn) |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 316 | pol->pd_offline_fn(blkg->pd[i]); |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 317 | } |
| 318 | blkg->online = false; |
| 319 | |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 320 | radix_tree_delete(&blkcg->blkg_tree, blkg->q->id); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 321 | list_del_init(&blkg->q_node); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 322 | hlist_del_init_rcu(&blkg->blkcg_node); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 323 | |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 324 | /* |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 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. |
| 328 | */ |
Paul E. McKenney | ec6c676a | 2014-02-17 13:35:57 -0800 | [diff] [blame] | 329 | if (rcu_access_pointer(blkcg->blkg_hint) == blkg) |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 330 | rcu_assign_pointer(blkcg->blkg_hint, NULL); |
| 331 | |
| 332 | /* |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 333 | * Put the reference taken at the time of creation so that when all |
| 334 | * queues are gone, group can be destroyed. |
| 335 | */ |
| 336 | blkg_put(blkg); |
| 337 | } |
| 338 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 339 | /** |
| 340 | * blkg_destroy_all - destroy all blkgs associated with a request_queue |
| 341 | * @q: request_queue of interest |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 342 | * |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 343 | * Destroy all blkgs associated with @q. |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 344 | */ |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 345 | static void blkg_destroy_all(struct request_queue *q) |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 346 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 347 | struct blkcg_gq *blkg, *n; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 348 | |
Tejun Heo | 6d18b00 | 2012-04-13 13:11:35 -0700 | [diff] [blame] | 349 | lockdep_assert_held(q->queue_lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 350 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 351 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 352 | struct blkcg *blkcg = blkg->blkcg; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 353 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 354 | spin_lock(&blkcg->lock); |
| 355 | blkg_destroy(blkg); |
| 356 | spin_unlock(&blkcg->lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Tejun Heo | 2a4fd07 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 360 | /* |
| 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. |
| 364 | * |
| 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. |
| 367 | */ |
| 368 | void __blkg_release_rcu(struct rcu_head *rcu_head) |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 369 | { |
Tejun Heo | 2a4fd07 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 370 | struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head); |
Tejun Heo | db61367 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 371 | |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 372 | /* release the blkcg and parent blkg refs this blkg has been holding */ |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 373 | css_put(&blkg->blkcg->css); |
Tejun Heo | a5049a8 | 2014-06-19 17:42:57 -0400 | [diff] [blame] | 374 | if (blkg->parent) |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 375 | blkg_put(blkg->parent); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 376 | |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 377 | wb_congested_put(blkg->wb_congested); |
| 378 | |
Tejun Heo | 2a4fd07 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 379 | blkg_free(blkg); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 380 | } |
Tejun Heo | 2a4fd07 | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 381 | EXPORT_SYMBOL_GPL(__blkg_release_rcu); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 382 | |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 383 | /* |
| 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. |
| 386 | */ |
| 387 | struct request_list *__blk_queue_next_rl(struct request_list *rl, |
| 388 | struct request_queue *q) |
| 389 | { |
| 390 | struct list_head *ent; |
| 391 | struct blkcg_gq *blkg; |
| 392 | |
| 393 | /* |
| 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. |
| 396 | */ |
| 397 | if (rl == &q->root_rl) { |
| 398 | ent = &q->blkg_list; |
Jun'ichi Nomura | 65c77fd | 2012-10-22 10:15:37 +0900 | [diff] [blame] | 399 | /* There are no more block groups, hence no request lists */ |
| 400 | if (list_empty(ent)) |
| 401 | return NULL; |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 402 | } else { |
| 403 | blkg = container_of(rl, struct blkcg_gq, rl); |
| 404 | ent = &blkg->q_node; |
| 405 | } |
| 406 | |
| 407 | /* walk to the next list_head, skip root blkcg */ |
| 408 | ent = ent->next; |
| 409 | if (ent == &q->root_blkg->q_node) |
| 410 | ent = ent->next; |
| 411 | if (ent == &q->blkg_list) |
| 412 | return NULL; |
| 413 | |
| 414 | blkg = container_of(ent, struct blkcg_gq, q_node); |
| 415 | return &blkg->rl; |
| 416 | } |
| 417 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 418 | static int blkcg_reset_stats(struct cgroup_subsys_state *css, |
| 419 | struct cftype *cftype, u64 val) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 420 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 421 | struct blkcg *blkcg = css_to_blkcg(css); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 422 | struct blkcg_gq *blkg; |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 423 | int i; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 424 | |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 425 | mutex_lock(&blkcg_pol_mutex); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 426 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 427 | |
| 428 | /* |
| 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. |
| 432 | */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 433 | hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 434 | for (i = 0; i < BLKCG_MAX_POLS; i++) { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 435 | struct blkcg_policy *pol = blkcg_policy[i]; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 436 | |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 437 | if (blkg->pd[i] && pol->pd_reset_stats_fn) |
| 438 | pol->pd_reset_stats_fn(blkg->pd[i]); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 439 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 440 | } |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 441 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 442 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 443 | mutex_unlock(&blkcg_pol_mutex); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 447 | static const char *blkg_dev_name(struct blkcg_gq *blkg) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 448 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 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); |
| 452 | return NULL; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 455 | /** |
| 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 |
| 463 | * |
| 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 |
Tejun Heo | 810ecfa | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 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. |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 469 | * |
| 470 | * This is to be used to construct print functions for |
| 471 | * cftype->read_seq_string method. |
| 472 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 473 | void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 474 | u64 (*prfill)(struct seq_file *, |
| 475 | struct blkg_policy_data *, int), |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 476 | const struct blkcg_policy *pol, int data, |
Tejun Heo | ec39934 | 2012-04-13 13:11:27 -0700 | [diff] [blame] | 477 | bool show_total) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 478 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 479 | struct blkcg_gq *blkg; |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 480 | u64 total = 0; |
| 481 | |
Tejun Heo | 810ecfa | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 482 | rcu_read_lock(); |
Linus Torvalds | ee89f81 | 2013-02-28 12:52:24 -0800 | [diff] [blame] | 483 | hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | 810ecfa | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 484 | spin_lock_irq(blkg->q->queue_lock); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 485 | if (blkcg_policy_enabled(blkg->q, pol)) |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 486 | total += prfill(sf, blkg->pd[pol->plid], data); |
Tejun Heo | 810ecfa | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 487 | spin_unlock_irq(blkg->q->queue_lock); |
| 488 | } |
| 489 | rcu_read_unlock(); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 490 | |
| 491 | if (show_total) |
| 492 | seq_printf(sf, "Total %llu\n", (unsigned long long)total); |
| 493 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(blkcg_print_blkgs); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 495 | |
| 496 | /** |
| 497 | * __blkg_prfill_u64 - prfill helper for a single u64 value |
| 498 | * @sf: seq_file to print to |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 499 | * @pd: policy private data of interest |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 500 | * @v: value to print |
| 501 | * |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 502 | * Print @v to @sf for the device assocaited with @pd. |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 503 | */ |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 504 | u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 505 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 506 | const char *dname = blkg_dev_name(pd->blkg); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 507 | |
| 508 | if (!dname) |
| 509 | return 0; |
| 510 | |
| 511 | seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v); |
| 512 | return v; |
| 513 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 514 | EXPORT_SYMBOL_GPL(__blkg_prfill_u64); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 515 | |
| 516 | /** |
| 517 | * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat |
| 518 | * @sf: seq_file to print to |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 519 | * @pd: policy private data of interest |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 520 | * @rwstat: rwstat to print |
| 521 | * |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 522 | * Print @rwstat to @sf for the device assocaited with @pd. |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 523 | */ |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 524 | u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 525 | const struct blkg_rwstat *rwstat) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 526 | { |
| 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", |
| 532 | }; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 533 | const char *dname = blkg_dev_name(pd->blkg); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 534 | u64 v; |
| 535 | int i; |
| 536 | |
| 537 | if (!dname) |
| 538 | return 0; |
| 539 | |
| 540 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
| 541 | seq_printf(sf, "%s %s %llu\n", dname, rwstr[i], |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 542 | (unsigned long long)atomic64_read(&rwstat->aux_cnt[i])); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 543 | |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 544 | v = atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_READ]) + |
| 545 | atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_WRITE]); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 546 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); |
| 547 | return v; |
| 548 | } |
Tejun Heo | b50da39 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 549 | EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 550 | |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 551 | /** |
| 552 | * blkg_prfill_stat - prfill callback for blkg_stat |
| 553 | * @sf: seq_file to print to |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 554 | * @pd: policy private data of interest |
| 555 | * @off: offset to the blkg_stat in @pd |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 556 | * |
| 557 | * prfill callback for printing a blkg_stat. |
| 558 | */ |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 559 | u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 560 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 561 | return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off)); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 562 | } |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 563 | EXPORT_SYMBOL_GPL(blkg_prfill_stat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 564 | |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 565 | /** |
| 566 | * blkg_prfill_rwstat - prfill callback for blkg_rwstat |
| 567 | * @sf: seq_file to print to |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 568 | * @pd: policy private data of interest |
| 569 | * @off: offset to the blkg_rwstat in @pd |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 570 | * |
| 571 | * prfill callback for printing a blkg_rwstat. |
| 572 | */ |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 573 | u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, |
| 574 | int off) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 575 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 576 | struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 577 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 578 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 579 | } |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 580 | EXPORT_SYMBOL_GPL(blkg_prfill_rwstat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 581 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 582 | /** |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 583 | * blkg_stat_recursive_sum - collect hierarchical blkg_stat |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 584 | * @blkg: blkg of interest |
| 585 | * @pol: blkcg_policy which contains the blkg_stat |
| 586 | * @off: offset to the blkg_stat in blkg_policy_data or @blkg |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 587 | * |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 588 | * Collect the blkg_stat specified by @blkg, @pol and @off and all its |
| 589 | * online descendants and their aux counts. The caller must be holding the |
| 590 | * queue lock for online tests. |
| 591 | * |
| 592 | * If @pol is NULL, blkg_stat is at @off bytes into @blkg; otherwise, it is |
| 593 | * at @off bytes into @blkg's blkg_policy_data of the policy. |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 594 | */ |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 595 | u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg, |
| 596 | struct blkcg_policy *pol, int off) |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 597 | { |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 598 | struct blkcg_gq *pos_blkg; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 599 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 600 | u64 sum = 0; |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 601 | |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 602 | lockdep_assert_held(blkg->q->queue_lock); |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 603 | |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 604 | rcu_read_lock(); |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 605 | blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) { |
| 606 | struct blkg_stat *stat; |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 607 | |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 608 | if (!pos_blkg->online) |
| 609 | continue; |
| 610 | |
| 611 | if (pol) |
| 612 | stat = (void *)blkg_to_pd(pos_blkg, pol) + off; |
| 613 | else |
| 614 | stat = (void *)blkg + off; |
| 615 | |
| 616 | sum += blkg_stat_read(stat) + atomic64_read(&stat->aux_cnt); |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 617 | } |
| 618 | rcu_read_unlock(); |
| 619 | |
| 620 | return sum; |
| 621 | } |
| 622 | EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum); |
| 623 | |
| 624 | /** |
| 625 | * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 626 | * @blkg: blkg of interest |
| 627 | * @pol: blkcg_policy which contains the blkg_rwstat |
| 628 | * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 629 | * |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 630 | * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its |
| 631 | * online descendants and their aux counts. The caller must be holding the |
| 632 | * queue lock for online tests. |
| 633 | * |
| 634 | * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it |
| 635 | * is at @off bytes into @blkg's blkg_policy_data of the policy. |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 636 | */ |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 637 | struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, |
| 638 | struct blkcg_policy *pol, int off) |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 639 | { |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 640 | struct blkcg_gq *pos_blkg; |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 641 | struct cgroup_subsys_state *pos_css; |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 642 | struct blkg_rwstat sum = { }; |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 643 | int i; |
| 644 | |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 645 | lockdep_assert_held(blkg->q->queue_lock); |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 646 | |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 647 | rcu_read_lock(); |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 648 | blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) { |
| 649 | struct blkg_rwstat *rwstat, tmp; |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 650 | |
| 651 | if (!pos_blkg->online) |
| 652 | continue; |
| 653 | |
Tejun Heo | f12c74c | 2015-08-18 14:55:23 -0700 | [diff] [blame^] | 654 | if (pol) |
| 655 | rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off; |
| 656 | else |
| 657 | rwstat = (void *)pos_blkg + off; |
| 658 | |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 659 | tmp = blkg_rwstat_read(rwstat); |
| 660 | |
| 661 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 662 | atomic64_add(atomic64_read(&tmp.aux_cnt[i]) + |
| 663 | atomic64_read(&rwstat->aux_cnt[i]), |
| 664 | &sum.aux_cnt[i]); |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 665 | } |
| 666 | rcu_read_unlock(); |
| 667 | |
| 668 | return sum; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum); |
| 671 | |
| 672 | /** |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 673 | * blkg_conf_prep - parse and prepare for per-blkg config update |
| 674 | * @blkcg: target block cgroup |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 675 | * @pol: target policy |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 676 | * @input: input string |
| 677 | * @ctx: blkg_conf_ctx to be filled |
| 678 | * |
| 679 | * Parse per-blkg config update from @input and initialize @ctx with the |
| 680 | * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 681 | * value. This function returns with RCU read lock and queue lock held and |
| 682 | * must be paired with blkg_conf_finish(). |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 683 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 684 | int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, |
| 685 | const char *input, struct blkg_conf_ctx *ctx) |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 686 | __acquires(rcu) __acquires(disk->queue->queue_lock) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 687 | { |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 688 | struct gendisk *disk; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 689 | struct blkcg_gq *blkg; |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 690 | unsigned int major, minor; |
| 691 | unsigned long long v; |
| 692 | int part, ret; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 693 | |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 694 | if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3) |
| 695 | return -EINVAL; |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 696 | |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 697 | disk = get_gendisk(MKDEV(major, minor), &part); |
Tejun Heo | 5f6c2d2 | 2015-07-22 18:05:53 -0400 | [diff] [blame] | 698 | if (!disk) |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 699 | return -EINVAL; |
Tejun Heo | 5f6c2d2 | 2015-07-22 18:05:53 -0400 | [diff] [blame] | 700 | if (part) { |
| 701 | put_disk(disk); |
| 702 | return -EINVAL; |
| 703 | } |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 704 | |
| 705 | rcu_read_lock(); |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 706 | spin_lock_irq(disk->queue->queue_lock); |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 707 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 708 | if (blkcg_policy_enabled(disk->queue, pol)) |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 709 | blkg = blkg_lookup_create(blkcg, disk->queue); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 710 | else |
| 711 | blkg = ERR_PTR(-EINVAL); |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 712 | |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 713 | if (IS_ERR(blkg)) { |
| 714 | ret = PTR_ERR(blkg); |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 715 | rcu_read_unlock(); |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 716 | spin_unlock_irq(disk->queue->queue_lock); |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 717 | put_disk(disk); |
| 718 | /* |
| 719 | * If queue was bypassing, we should retry. Do so after a |
| 720 | * short msleep(). It isn't strictly necessary but queue |
| 721 | * can be bypassing for some time and it's always nice to |
| 722 | * avoid busy looping. |
| 723 | */ |
| 724 | if (ret == -EBUSY) { |
| 725 | msleep(10); |
| 726 | ret = restart_syscall(); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 727 | } |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 728 | return ret; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 729 | } |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 730 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 731 | ctx->disk = disk; |
| 732 | ctx->blkg = blkg; |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 733 | ctx->v = v; |
| 734 | return 0; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 735 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 736 | EXPORT_SYMBOL_GPL(blkg_conf_prep); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 737 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 738 | /** |
| 739 | * blkg_conf_finish - finish up per-blkg config update |
| 740 | * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep() |
| 741 | * |
| 742 | * Finish up after per-blkg config update. This function must be paired |
| 743 | * with blkg_conf_prep(). |
| 744 | */ |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 745 | void blkg_conf_finish(struct blkg_conf_ctx *ctx) |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 746 | __releases(ctx->disk->queue->queue_lock) __releases(rcu) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 747 | { |
Tejun Heo | da8b066 | 2012-04-13 13:11:29 -0700 | [diff] [blame] | 748 | spin_unlock_irq(ctx->disk->queue->queue_lock); |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 749 | rcu_read_unlock(); |
| 750 | put_disk(ctx->disk); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 751 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 752 | EXPORT_SYMBOL_GPL(blkg_conf_finish); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 753 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 754 | struct cftype blkcg_files[] = { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 755 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 756 | .name = "reset_stats", |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 757 | .write_u64 = blkcg_reset_stats, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 758 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 759 | { } /* terminate */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 760 | }; |
| 761 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 762 | /** |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 763 | * blkcg_css_offline - cgroup css_offline callback |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 764 | * @css: css of interest |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 765 | * |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 766 | * This function is called when @css is about to go away and responsible |
| 767 | * for shooting down all blkgs associated with @css. blkgs should be |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 768 | * removed while holding both q and blkcg locks. As blkcg lock is nested |
| 769 | * inside q lock, this function performs reverse double lock dancing. |
| 770 | * |
| 771 | * This is the blkcg counterpart of ioc_release_fn(). |
| 772 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 773 | static void blkcg_css_offline(struct cgroup_subsys_state *css) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 774 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 775 | struct blkcg *blkcg = css_to_blkcg(css); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 776 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 777 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 778 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 779 | while (!hlist_empty(&blkcg->blkg_list)) { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 780 | struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first, |
| 781 | struct blkcg_gq, blkcg_node); |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 782 | struct request_queue *q = blkg->q; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 783 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 784 | if (spin_trylock(q->queue_lock)) { |
| 785 | blkg_destroy(blkg); |
| 786 | spin_unlock(q->queue_lock); |
| 787 | } else { |
| 788 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 789 | cpu_relax(); |
Dan Carpenter | a556793 | 2012-03-29 20:57:08 +0200 | [diff] [blame] | 790 | spin_lock_irq(&blkcg->lock); |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 791 | } |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 792 | } |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 793 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 794 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 795 | |
| 796 | wb_blkcg_offline(blkcg); |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 797 | } |
| 798 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 799 | static void blkcg_css_free(struct cgroup_subsys_state *css) |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 800 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 801 | struct blkcg *blkcg = css_to_blkcg(css); |
Tejun Heo | bc915e6 | 2015-08-18 14:55:08 -0700 | [diff] [blame] | 802 | int i; |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 803 | |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 804 | mutex_lock(&blkcg_pol_mutex); |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 805 | |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 806 | list_del(&blkcg->all_blkcgs_node); |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 807 | |
Tejun Heo | bc915e6 | 2015-08-18 14:55:08 -0700 | [diff] [blame] | 808 | for (i = 0; i < BLKCG_MAX_POLS; i++) |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 809 | if (blkcg->cpd[i]) |
| 810 | blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); |
| 811 | |
| 812 | mutex_unlock(&blkcg_pol_mutex); |
| 813 | |
Tejun Heo | bc915e6 | 2015-08-18 14:55:08 -0700 | [diff] [blame] | 814 | kfree(blkcg); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 817 | static struct cgroup_subsys_state * |
| 818 | blkcg_css_alloc(struct cgroup_subsys_state *parent_css) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 819 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 820 | struct blkcg *blkcg; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 821 | struct cgroup_subsys_state *ret; |
| 822 | int i; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 823 | |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 824 | mutex_lock(&blkcg_pol_mutex); |
| 825 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 826 | if (!parent_css) { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 827 | blkcg = &blkcg_root; |
Tejun Heo | bc915e6 | 2015-08-18 14:55:08 -0700 | [diff] [blame] | 828 | } else { |
| 829 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
| 830 | if (!blkcg) { |
| 831 | ret = ERR_PTR(-ENOMEM); |
| 832 | goto free_blkcg; |
| 833 | } |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 834 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 835 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 836 | for (i = 0; i < BLKCG_MAX_POLS ; i++) { |
| 837 | struct blkcg_policy *pol = blkcg_policy[i]; |
| 838 | struct blkcg_policy_data *cpd; |
| 839 | |
| 840 | /* |
| 841 | * If the policy hasn't been attached yet, wait for it |
| 842 | * to be attached before doing anything else. Otherwise, |
| 843 | * check if the policy requires any specific per-cgroup |
| 844 | * data: if it does, allocate and initialize it. |
| 845 | */ |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 846 | if (!pol || !pol->cpd_alloc_fn) |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 847 | continue; |
| 848 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 849 | cpd = pol->cpd_alloc_fn(GFP_KERNEL); |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 850 | if (!cpd) { |
| 851 | ret = ERR_PTR(-ENOMEM); |
| 852 | goto free_pd_blkcg; |
| 853 | } |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 854 | blkcg->cpd[i] = cpd; |
| 855 | cpd->blkcg = blkcg; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 856 | cpd->plid = i; |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 857 | if (pol->cpd_init_fn) |
| 858 | pol->cpd_init_fn(cpd); |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 861 | spin_lock_init(&blkcg->lock); |
Tejun Heo | d93a11f | 2015-08-18 14:55:01 -0700 | [diff] [blame] | 862 | INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 863 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 864 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 865 | INIT_LIST_HEAD(&blkcg->cgwb_list); |
| 866 | #endif |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 867 | list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs); |
| 868 | |
| 869 | mutex_unlock(&blkcg_pol_mutex); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 870 | return &blkcg->css; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 871 | |
| 872 | free_pd_blkcg: |
| 873 | for (i--; i >= 0; i--) |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 874 | if (blkcg->cpd[i]) |
| 875 | blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 876 | free_blkcg: |
| 877 | kfree(blkcg); |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 878 | mutex_unlock(&blkcg_pol_mutex); |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 879 | return ret; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 880 | } |
| 881 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 882 | /** |
| 883 | * blkcg_init_queue - initialize blkcg part of request queue |
| 884 | * @q: request_queue to initialize |
| 885 | * |
| 886 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg |
| 887 | * part of new request_queue @q. |
| 888 | * |
| 889 | * RETURNS: |
| 890 | * 0 on success, -errno on failure. |
| 891 | */ |
| 892 | int blkcg_init_queue(struct request_queue *q) |
| 893 | { |
Tejun Heo | ec13b1d | 2015-05-22 17:13:19 -0400 | [diff] [blame] | 894 | struct blkcg_gq *new_blkg, *blkg; |
| 895 | bool preloaded; |
| 896 | int ret; |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 897 | |
Tejun Heo | ec13b1d | 2015-05-22 17:13:19 -0400 | [diff] [blame] | 898 | new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL); |
| 899 | if (!new_blkg) |
| 900 | return -ENOMEM; |
| 901 | |
| 902 | preloaded = !radix_tree_preload(GFP_KERNEL); |
| 903 | |
| 904 | /* |
| 905 | * Make sure the root blkg exists and count the existing blkgs. As |
| 906 | * @q is bypassing at this point, blkg_lookup_create() can't be |
| 907 | * used. Open code insertion. |
| 908 | */ |
| 909 | rcu_read_lock(); |
| 910 | spin_lock_irq(q->queue_lock); |
| 911 | blkg = blkg_create(&blkcg_root, q, new_blkg); |
| 912 | spin_unlock_irq(q->queue_lock); |
| 913 | rcu_read_unlock(); |
| 914 | |
| 915 | if (preloaded) |
| 916 | radix_tree_preload_end(); |
| 917 | |
| 918 | if (IS_ERR(blkg)) { |
Tejun Heo | 994b783 | 2015-08-18 14:55:07 -0700 | [diff] [blame] | 919 | blkg_free(new_blkg); |
Tejun Heo | ec13b1d | 2015-05-22 17:13:19 -0400 | [diff] [blame] | 920 | return PTR_ERR(blkg); |
| 921 | } |
| 922 | |
| 923 | q->root_blkg = blkg; |
| 924 | q->root_rl.blkg = blkg; |
| 925 | |
| 926 | ret = blk_throtl_init(q); |
| 927 | if (ret) { |
| 928 | spin_lock_irq(q->queue_lock); |
| 929 | blkg_destroy_all(q); |
| 930 | spin_unlock_irq(q->queue_lock); |
| 931 | } |
| 932 | return ret; |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /** |
| 936 | * blkcg_drain_queue - drain blkcg part of request_queue |
| 937 | * @q: request_queue to drain |
| 938 | * |
| 939 | * Called from blk_drain_queue(). Responsible for draining blkcg part. |
| 940 | */ |
| 941 | void blkcg_drain_queue(struct request_queue *q) |
| 942 | { |
| 943 | lockdep_assert_held(q->queue_lock); |
| 944 | |
Tejun Heo | 0b462c8 | 2014-07-05 18:43:21 -0400 | [diff] [blame] | 945 | /* |
| 946 | * @q could be exiting and already have destroyed all blkgs as |
| 947 | * indicated by NULL root_blkg. If so, don't confuse policies. |
| 948 | */ |
| 949 | if (!q->root_blkg) |
| 950 | return; |
| 951 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 952 | blk_throtl_drain(q); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * blkcg_exit_queue - exit and release blkcg part of request_queue |
| 957 | * @q: request_queue being released |
| 958 | * |
| 959 | * Called from blk_release_queue(). Responsible for exiting blkcg part. |
| 960 | */ |
| 961 | void blkcg_exit_queue(struct request_queue *q) |
| 962 | { |
Tejun Heo | 6d18b00 | 2012-04-13 13:11:35 -0700 | [diff] [blame] | 963 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 964 | blkg_destroy_all(q); |
Tejun Heo | 6d18b00 | 2012-04-13 13:11:35 -0700 | [diff] [blame] | 965 | spin_unlock_irq(q->queue_lock); |
| 966 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 967 | blk_throtl_exit(q); |
| 968 | } |
| 969 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 970 | /* |
| 971 | * We cannot support shared io contexts, as we have no mean to support |
| 972 | * two tasks with the same ioc in two different groups without major rework |
| 973 | * of the main cic data structures. For now we allow a task to change |
| 974 | * its cgroup only if it's the only owner of its ioc. |
| 975 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 976 | static int blkcg_can_attach(struct cgroup_subsys_state *css, |
| 977 | struct cgroup_taskset *tset) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 978 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 979 | struct task_struct *task; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 980 | struct io_context *ioc; |
| 981 | int ret = 0; |
| 982 | |
| 983 | /* task_lock() is needed to avoid races with exit_io_context() */ |
Tejun Heo | 924f0d9a | 2014-02-13 06:58:41 -0500 | [diff] [blame] | 984 | cgroup_taskset_for_each(task, tset) { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 985 | task_lock(task); |
| 986 | ioc = task->io_context; |
| 987 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) |
| 988 | ret = -EINVAL; |
| 989 | task_unlock(task); |
| 990 | if (ret) |
| 991 | break; |
| 992 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 993 | return ret; |
| 994 | } |
| 995 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 996 | struct cgroup_subsys blkio_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 997 | .css_alloc = blkcg_css_alloc, |
| 998 | .css_offline = blkcg_css_offline, |
| 999 | .css_free = blkcg_css_free, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1000 | .can_attach = blkcg_can_attach, |
Tejun Heo | 5577964 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 1001 | .legacy_cftypes = blkcg_files, |
Tejun Heo | 1ced953 | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 1002 | #ifdef CONFIG_MEMCG |
| 1003 | /* |
| 1004 | * This ensures that, if available, memcg is automatically enabled |
| 1005 | * together on the default hierarchy so that the owner cgroup can |
| 1006 | * be retrieved from writeback pages. |
| 1007 | */ |
| 1008 | .depends_on = 1 << memory_cgrp_id, |
| 1009 | #endif |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1010 | }; |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1011 | EXPORT_SYMBOL_GPL(blkio_cgrp_subsys); |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 1012 | |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1013 | /** |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1014 | * blkcg_activate_policy - activate a blkcg policy on a request_queue |
| 1015 | * @q: request_queue of interest |
| 1016 | * @pol: blkcg policy to activate |
| 1017 | * |
| 1018 | * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through |
| 1019 | * bypass mode to populate its blkgs with policy_data for @pol. |
| 1020 | * |
| 1021 | * Activation happens with @q bypassed, so nobody would be accessing blkgs |
| 1022 | * from IO path. Update of each blkg is protected by both queue and blkcg |
| 1023 | * locks so that holding either lock and testing blkcg_policy_enabled() is |
| 1024 | * always enough for dereferencing policy data. |
| 1025 | * |
| 1026 | * The caller is responsible for synchronizing [de]activations and policy |
| 1027 | * [un]registerations. Returns 0 on success, -errno on failure. |
| 1028 | */ |
| 1029 | int blkcg_activate_policy(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1030 | const struct blkcg_policy *pol) |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1031 | { |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1032 | struct blkg_policy_data *pd_prealloc = NULL; |
Tejun Heo | ec13b1d | 2015-05-22 17:13:19 -0400 | [diff] [blame] | 1033 | struct blkcg_gq *blkg; |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1034 | int ret; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1035 | |
| 1036 | if (blkcg_policy_enabled(q, pol)) |
| 1037 | return 0; |
| 1038 | |
| 1039 | blk_queue_bypass_start(q); |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1040 | pd_prealloc: |
| 1041 | if (!pd_prealloc) { |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1042 | pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node); |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1043 | if (!pd_prealloc) { |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1044 | ret = -ENOMEM; |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1045 | goto out_bypass_end; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1046 | } |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1049 | spin_lock_irq(q->queue_lock); |
| 1050 | |
| 1051 | list_for_each_entry(blkg, &q->blkg_list, q_node) { |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1052 | struct blkg_policy_data *pd; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1053 | |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1054 | if (blkg->pd[pol->plid]) |
| 1055 | continue; |
| 1056 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1057 | pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node); |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1058 | if (!pd) |
| 1059 | swap(pd, pd_prealloc); |
| 1060 | if (!pd) { |
| 1061 | spin_unlock_irq(q->queue_lock); |
| 1062 | goto pd_prealloc; |
| 1063 | } |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1064 | |
| 1065 | blkg->pd[pol->plid] = pd; |
| 1066 | pd->blkg = blkg; |
Tejun Heo | b276a87 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 1067 | pd->plid = pol->plid; |
Tejun Heo | 3e41871 | 2015-08-18 14:55:10 -0700 | [diff] [blame] | 1068 | if (pol->pd_init_fn) |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1069 | pol->pd_init_fn(pd); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | __set_bit(pol->plid, q->blkcg_pols); |
| 1073 | ret = 0; |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1074 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1075 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | 4c55f4f | 2015-08-18 14:55:09 -0700 | [diff] [blame] | 1076 | out_bypass_end: |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1077 | blk_queue_bypass_end(q); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1078 | if (pd_prealloc) |
| 1079 | pol->pd_free_fn(pd_prealloc); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_GPL(blkcg_activate_policy); |
| 1083 | |
| 1084 | /** |
| 1085 | * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue |
| 1086 | * @q: request_queue of interest |
| 1087 | * @pol: blkcg policy to deactivate |
| 1088 | * |
| 1089 | * Deactivate @pol on @q. Follows the same synchronization rules as |
| 1090 | * blkcg_activate_policy(). |
| 1091 | */ |
| 1092 | void blkcg_deactivate_policy(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1093 | const struct blkcg_policy *pol) |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1094 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1095 | struct blkcg_gq *blkg; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (!blkcg_policy_enabled(q, pol)) |
| 1098 | return; |
| 1099 | |
| 1100 | blk_queue_bypass_start(q); |
| 1101 | spin_lock_irq(q->queue_lock); |
| 1102 | |
| 1103 | __clear_bit(pol->plid, q->blkcg_pols); |
| 1104 | |
| 1105 | list_for_each_entry(blkg, &q->blkg_list, q_node) { |
| 1106 | /* grab blkcg lock too while removing @pd from @blkg */ |
| 1107 | spin_lock(&blkg->blkcg->lock); |
| 1108 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1109 | if (blkg->pd[pol->plid]) { |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 1110 | if (pol->pd_offline_fn) |
| 1111 | pol->pd_offline_fn(blkg->pd[pol->plid]); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 1112 | pol->pd_free_fn(blkg->pd[pol->plid]); |
| 1113 | blkg->pd[pol->plid] = NULL; |
| 1114 | } |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1115 | |
| 1116 | spin_unlock(&blkg->blkcg->lock); |
| 1117 | } |
| 1118 | |
| 1119 | spin_unlock_irq(q->queue_lock); |
| 1120 | blk_queue_bypass_end(q); |
| 1121 | } |
| 1122 | EXPORT_SYMBOL_GPL(blkcg_deactivate_policy); |
| 1123 | |
| 1124 | /** |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1125 | * blkcg_policy_register - register a blkcg policy |
| 1126 | * @pol: blkcg policy to register |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1127 | * |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1128 | * Register @pol with blkcg core. Might sleep and @pol may be modified on |
| 1129 | * successful registration. Returns 0 on success and -errno on failure. |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1130 | */ |
Jens Axboe | d5bf029 | 2014-06-22 16:31:56 -0600 | [diff] [blame] | 1131 | int blkcg_policy_register(struct blkcg_policy *pol) |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1132 | { |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1133 | struct blkcg *blkcg; |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1134 | int i, ret; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 1135 | |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1136 | mutex_lock(&blkcg_pol_register_mutex); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 1137 | mutex_lock(&blkcg_pol_mutex); |
| 1138 | |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1139 | /* find an empty slot */ |
| 1140 | ret = -ENOSPC; |
| 1141 | for (i = 0; i < BLKCG_MAX_POLS; i++) |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1142 | if (!blkcg_policy[i]) |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1143 | break; |
| 1144 | if (i >= BLKCG_MAX_POLS) |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1145 | goto err_unlock; |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 1146 | |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1147 | /* register @pol */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1148 | pol->plid = i; |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1149 | blkcg_policy[pol->plid] = pol; |
| 1150 | |
| 1151 | /* allocate and install cpd's */ |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1152 | if (pol->cpd_alloc_fn) { |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1153 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
| 1154 | struct blkcg_policy_data *cpd; |
| 1155 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1156 | cpd = pol->cpd_alloc_fn(GFP_KERNEL); |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1157 | if (!cpd) { |
| 1158 | mutex_unlock(&blkcg_pol_mutex); |
| 1159 | goto err_free_cpds; |
| 1160 | } |
| 1161 | |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 1162 | blkcg->cpd[pol->plid] = cpd; |
| 1163 | cpd->blkcg = blkcg; |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1164 | cpd->plid = pol->plid; |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 1165 | pol->cpd_init_fn(cpd); |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1169 | mutex_unlock(&blkcg_pol_mutex); |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1170 | |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1171 | /* everything is in place, add intf files for the new policy */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1172 | if (pol->cftypes) |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 1173 | WARN_ON(cgroup_add_legacy_cftypes(&blkio_cgrp_subsys, |
| 1174 | pol->cftypes)); |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1175 | mutex_unlock(&blkcg_pol_register_mutex); |
| 1176 | return 0; |
| 1177 | |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1178 | err_free_cpds: |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1179 | if (pol->cpd_alloc_fn) { |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1180 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1181 | if (blkcg->cpd[pol->plid]) { |
| 1182 | pol->cpd_free_fn(blkcg->cpd[pol->plid]); |
| 1183 | blkcg->cpd[pol->plid] = NULL; |
| 1184 | } |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | blkcg_policy[pol->plid] = NULL; |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1188 | err_unlock: |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 1189 | mutex_unlock(&blkcg_pol_mutex); |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1190 | mutex_unlock(&blkcg_pol_register_mutex); |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1191 | return ret; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1192 | } |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1193 | EXPORT_SYMBOL_GPL(blkcg_policy_register); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1194 | |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1195 | /** |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1196 | * blkcg_policy_unregister - unregister a blkcg policy |
| 1197 | * @pol: blkcg policy to unregister |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1198 | * |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1199 | * Undo blkcg_policy_register(@pol). Might sleep. |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1200 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1201 | void blkcg_policy_unregister(struct blkcg_policy *pol) |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1202 | { |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1203 | struct blkcg *blkcg; |
| 1204 | |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1205 | mutex_lock(&blkcg_pol_register_mutex); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 1206 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1207 | if (WARN_ON(blkcg_policy[pol->plid] != pol)) |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 1208 | goto out_unlock; |
| 1209 | |
| 1210 | /* kill the intf files first */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1211 | if (pol->cftypes) |
Tejun Heo | 2bb566c | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 1212 | cgroup_rm_cftypes(pol->cftypes); |
Tejun Heo | 44ea53d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1213 | |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1214 | /* remove cpds and unregister */ |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1215 | mutex_lock(&blkcg_pol_mutex); |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1216 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1217 | if (pol->cpd_alloc_fn) { |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1218 | list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) { |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 1219 | if (blkcg->cpd[pol->plid]) { |
| 1220 | pol->cpd_free_fn(blkcg->cpd[pol->plid]); |
| 1221 | blkcg->cpd[pol->plid] = NULL; |
| 1222 | } |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1223 | } |
| 1224 | } |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1225 | blkcg_policy[pol->plid] = NULL; |
Tejun Heo | 06b285b | 2015-07-09 16:39:50 -0400 | [diff] [blame] | 1226 | |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame] | 1227 | mutex_unlock(&blkcg_pol_mutex); |
Tejun Heo | 838f13b | 2015-07-09 16:39:47 -0400 | [diff] [blame] | 1228 | out_unlock: |
| 1229 | mutex_unlock(&blkcg_pol_register_mutex); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 1230 | } |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1231 | EXPORT_SYMBOL_GPL(blkcg_policy_unregister); |