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> |
| 12 | */ |
| 13 | #include <linux/ioprio.h> |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 14 | #include <linux/kdev_t.h> |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 15 | #include <linux/module.h> |
Stephen Rothwell | accee78 | 2009-12-07 19:29:39 +1100 | [diff] [blame] | 16 | #include <linux/err.h> |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 17 | #include <linux/blkdev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 19 | #include <linux/genhd.h> |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 20 | #include <linux/delay.h> |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 21 | #include <linux/atomic.h> |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 22 | #include "blk-cgroup.h" |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 23 | #include "blk.h" |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 24 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 25 | #define MAX_KEY_LEN 100 |
| 26 | |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 27 | static DEFINE_MUTEX(blkcg_pol_mutex); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 28 | static DEFINE_MUTEX(all_q_mutex); |
| 29 | static LIST_HEAD(all_q_list); |
| 30 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 31 | struct blkio_cgroup blkio_root_cgroup = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT }; |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 32 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
| 33 | |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 34 | static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES]; |
| 35 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 36 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
| 37 | { |
| 38 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), |
| 39 | struct blkio_cgroup, css); |
| 40 | } |
Vivek Goyal | 9d6a986 | 2009-12-04 10:36:41 -0500 | [diff] [blame] | 41 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 42 | |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 43 | static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk) |
Vivek Goyal | 70087dc | 2011-05-16 15:24:08 +0200 | [diff] [blame] | 44 | { |
| 45 | return container_of(task_subsys_state(tsk, blkio_subsys_id), |
| 46 | struct blkio_cgroup, css); |
| 47 | } |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 48 | |
| 49 | struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio) |
| 50 | { |
| 51 | if (bio && bio->bi_css) |
| 52 | return container_of(bio->bi_css, struct blkio_cgroup, css); |
| 53 | return task_blkio_cgroup(current); |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(bio_blkio_cgroup); |
Vivek Goyal | 70087dc | 2011-05-16 15:24:08 +0200 | [diff] [blame] | 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 | */ |
| 63 | static void blkg_free(struct blkio_group *blkg) |
| 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 | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 70 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
Tejun Heo | 9ade5ea | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 71 | struct blkio_policy_type *pol = blkio_policy[i]; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 72 | struct blkg_policy_data *pd = blkg->pd[i]; |
| 73 | |
Tejun Heo | 9ade5ea | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 74 | if (!pd) |
| 75 | continue; |
| 76 | |
| 77 | if (pol && pol->ops.blkio_exit_group_fn) |
| 78 | pol->ops.blkio_exit_group_fn(blkg); |
| 79 | |
Tejun Heo | 9ade5ea | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 80 | kfree(pd); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 81 | } |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 82 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 83 | kfree(blkg); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * blkg_alloc - allocate a blkg |
| 88 | * @blkcg: block cgroup the new blkg is associated with |
| 89 | * @q: request_queue the new blkg is associated with |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 90 | * |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 91 | * Allocate a new blkg assocating @blkcg and @q. |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 92 | */ |
| 93 | static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 94 | struct request_queue *q) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 95 | { |
| 96 | struct blkio_group *blkg; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 97 | int i; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 98 | |
| 99 | /* alloc and init base part */ |
| 100 | blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node); |
| 101 | if (!blkg) |
| 102 | return NULL; |
| 103 | |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 104 | blkg->q = q; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 105 | INIT_LIST_HEAD(&blkg->q_node); |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 106 | blkg->blkcg = blkcg; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 107 | blkg->refcnt = 1; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 108 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
| 109 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 110 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 111 | struct blkio_policy_type *pol = blkio_policy[i]; |
| 112 | struct blkg_policy_data *pd; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 113 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 114 | if (!pol) |
| 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 */ |
| 118 | pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC, |
| 119 | q->node); |
| 120 | if (!pd) { |
| 121 | blkg_free(blkg); |
| 122 | return NULL; |
| 123 | } |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 124 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 125 | blkg->pd[i] = pd; |
| 126 | pd->blkg = blkg; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 129 | /* invoke per-policy init */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 130 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 131 | struct blkio_policy_type *pol = blkio_policy[i]; |
| 132 | |
| 133 | if (pol) |
| 134 | pol->ops.blkio_init_group_fn(blkg); |
| 135 | } |
| 136 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 137 | return blkg; |
| 138 | } |
| 139 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 140 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
| 141 | struct request_queue *q, |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 142 | bool for_root) |
| 143 | __releases(q->queue_lock) __acquires(q->queue_lock) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 144 | { |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 145 | struct blkio_group *blkg; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 146 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 147 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 148 | lockdep_assert_held(q->queue_lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 149 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 150 | /* |
| 151 | * This could be the first entry point of blkcg implementation and |
| 152 | * we shouldn't allow anything to go through for a bypassing queue. |
| 153 | * The following can be removed if blkg lookup is guaranteed to |
| 154 | * fail on a bypassing queue. |
| 155 | */ |
| 156 | if (unlikely(blk_queue_bypass(q)) && !for_root) |
| 157 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); |
| 158 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 159 | blkg = blkg_lookup(blkcg, q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 160 | if (blkg) |
| 161 | return blkg; |
| 162 | |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 163 | /* blkg holds a reference to blkcg */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 164 | if (!css_tryget(&blkcg->css)) |
| 165 | return ERR_PTR(-EINVAL); |
| 166 | |
| 167 | /* |
| 168 | * Allocate and initialize. |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 169 | */ |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 170 | blkg = blkg_alloc(blkcg, q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 171 | |
| 172 | /* did alloc fail? */ |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 173 | if (unlikely(!blkg)) { |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 174 | blkg = ERR_PTR(-ENOMEM); |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | /* insert */ |
| 179 | spin_lock(&blkcg->lock); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 180 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 181 | list_add(&blkg->q_node, &q->blkg_list); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 182 | spin_unlock(&blkcg->lock); |
| 183 | out: |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 184 | return blkg; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 185 | } |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 187 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 188 | /* called under rcu_read_lock(). */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 189 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 190 | struct request_queue *q) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 191 | { |
| 192 | struct blkio_group *blkg; |
| 193 | struct hlist_node *n; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 194 | |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 195 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 196 | if (blkg->q == q) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 197 | return blkg; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 198 | return NULL; |
| 199 | } |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(blkg_lookup); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 201 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 202 | static void blkg_destroy(struct blkio_group *blkg) |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 203 | { |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 204 | struct request_queue *q = blkg->q; |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 205 | struct blkio_cgroup *blkcg = blkg->blkcg; |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 206 | |
| 207 | lockdep_assert_held(q->queue_lock); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 208 | lockdep_assert_held(&blkcg->lock); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 209 | |
| 210 | /* Something wrong if we are trying to remove same group twice */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 211 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 212 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 213 | list_del_init(&blkg->q_node); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 214 | hlist_del_init_rcu(&blkg->blkcg_node); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 215 | |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 216 | /* |
| 217 | * Put the reference taken at the time of creation so that when all |
| 218 | * queues are gone, group can be destroyed. |
| 219 | */ |
| 220 | blkg_put(blkg); |
| 221 | } |
| 222 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 223 | /* |
| 224 | * XXX: This updates blkg policy data in-place for root blkg, which is |
| 225 | * necessary across elevator switch and policy registration as root blkgs |
| 226 | * aren't shot down. This broken and racy implementation is temporary. |
| 227 | * Eventually, blkg shoot down will be replaced by proper in-place update. |
| 228 | */ |
| 229 | void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid) |
| 230 | { |
| 231 | struct blkio_policy_type *pol = blkio_policy[plid]; |
| 232 | struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q); |
| 233 | struct blkg_policy_data *pd; |
| 234 | |
| 235 | if (!blkg) |
| 236 | return; |
| 237 | |
| 238 | kfree(blkg->pd[plid]); |
| 239 | blkg->pd[plid] = NULL; |
| 240 | |
| 241 | if (!pol) |
| 242 | return; |
| 243 | |
| 244 | pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL); |
| 245 | WARN_ON_ONCE(!pd); |
| 246 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 247 | blkg->pd[plid] = pd; |
| 248 | pd->blkg = blkg; |
| 249 | pol->ops.blkio_init_group_fn(blkg); |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(update_root_blkg_pd); |
| 252 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 253 | /** |
| 254 | * blkg_destroy_all - destroy all blkgs associated with a request_queue |
| 255 | * @q: request_queue of interest |
| 256 | * @destroy_root: whether to destroy root blkg or not |
| 257 | * |
| 258 | * Destroy blkgs associated with @q. If @destroy_root is %true, all are |
| 259 | * destroyed; otherwise, root blkg is left alone. |
| 260 | */ |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 261 | void blkg_destroy_all(struct request_queue *q, bool destroy_root) |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 262 | { |
| 263 | struct blkio_group *blkg, *n; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 264 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 265 | spin_lock_irq(q->queue_lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 266 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 267 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
| 268 | struct blkio_cgroup *blkcg = blkg->blkcg; |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 269 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 270 | /* skip root? */ |
| 271 | if (!destroy_root && blkg->blkcg == &blkio_root_cgroup) |
| 272 | continue; |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 273 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 274 | spin_lock(&blkcg->lock); |
| 275 | blkg_destroy(blkg); |
| 276 | spin_unlock(&blkcg->lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 277 | } |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 278 | |
| 279 | spin_unlock_irq(q->queue_lock); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 280 | } |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 281 | EXPORT_SYMBOL_GPL(blkg_destroy_all); |
Tejun Heo | 72e06c2 | 2012-03-05 13:15:00 -0800 | [diff] [blame] | 282 | |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 283 | static void blkg_rcu_free(struct rcu_head *rcu_head) |
| 284 | { |
| 285 | blkg_free(container_of(rcu_head, struct blkio_group, rcu_head)); |
| 286 | } |
| 287 | |
| 288 | void __blkg_release(struct blkio_group *blkg) |
| 289 | { |
| 290 | /* release the extra blkcg reference this blkg has been holding */ |
| 291 | css_put(&blkg->blkcg->css); |
| 292 | |
| 293 | /* |
| 294 | * A group is freed in rcu manner. But having an rcu lock does not |
| 295 | * mean that one can access all the fields of blkg and assume these |
| 296 | * are valid. For example, don't try to follow throtl_data and |
| 297 | * request queue links. |
| 298 | * |
| 299 | * Having a reference to blkg under an rcu allows acess to only |
| 300 | * values local to groups like group stats and group rate limits |
| 301 | */ |
| 302 | call_rcu(&blkg->rcu_head, blkg_rcu_free); |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(__blkg_release); |
| 305 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 306 | static int |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 307 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 308 | { |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 309 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 310 | struct blkio_group *blkg; |
| 311 | struct hlist_node *n; |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 312 | int i; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 313 | |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 314 | mutex_lock(&blkcg_pol_mutex); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 315 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * Note that stat reset is racy - it doesn't synchronize against |
| 319 | * stat updates. This is a debug feature which shouldn't exist |
| 320 | * anyway. If you get hit by a race, retry. |
| 321 | */ |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 322 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 323 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
| 324 | struct blkio_policy_type *pol = blkio_policy[i]; |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 325 | |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 326 | if (pol && pol->ops.blkio_reset_group_stats_fn) |
Tejun Heo | 9ade5ea | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 327 | pol->ops.blkio_reset_group_stats_fn(blkg); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 328 | } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 329 | } |
Vivek Goyal | f0bdc8c | 2011-05-19 15:38:30 -0400 | [diff] [blame] | 330 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 331 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 332 | mutex_unlock(&blkcg_pol_mutex); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 336 | static const char *blkg_dev_name(struct blkio_group *blkg) |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 337 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 338 | /* some drivers (floppy) instantiate a queue w/o disk registered */ |
| 339 | if (blkg->q->backing_dev_info.dev) |
| 340 | return dev_name(blkg->q->backing_dev_info.dev); |
| 341 | return NULL; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 344 | /** |
| 345 | * blkcg_print_blkgs - helper for printing per-blkg data |
| 346 | * @sf: seq_file to print to |
| 347 | * @blkcg: blkcg of interest |
| 348 | * @prfill: fill function to print out a blkg |
| 349 | * @pol: policy in question |
| 350 | * @data: data to be passed to @prfill |
| 351 | * @show_total: to print out sum of prfill return values or not |
| 352 | * |
| 353 | * This function invokes @prfill on each blkg of @blkcg if pd for the |
| 354 | * policy specified by @pol exists. @prfill is invoked with @sf, the |
| 355 | * policy data and @data. If @show_total is %true, the sum of the return |
| 356 | * values from @prfill is printed with "Total" label at the end. |
| 357 | * |
| 358 | * This is to be used to construct print functions for |
| 359 | * cftype->read_seq_string method. |
| 360 | */ |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 361 | void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg, |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 362 | u64 (*prfill)(struct seq_file *, void *, int), |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 363 | int pol, int data, bool show_total) |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 364 | { |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 365 | struct blkio_group *blkg; |
| 366 | struct hlist_node *n; |
| 367 | u64 total = 0; |
| 368 | |
| 369 | spin_lock_irq(&blkcg->lock); |
| 370 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) |
| 371 | if (blkg->pd[pol]) |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 372 | total += prfill(sf, blkg->pd[pol]->pdata, data); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 373 | spin_unlock_irq(&blkcg->lock); |
| 374 | |
| 375 | if (show_total) |
| 376 | seq_printf(sf, "Total %llu\n", (unsigned long long)total); |
| 377 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(blkcg_print_blkgs); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 379 | |
| 380 | /** |
| 381 | * __blkg_prfill_u64 - prfill helper for a single u64 value |
| 382 | * @sf: seq_file to print to |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 383 | * @pdata: policy private data of interest |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 384 | * @v: value to print |
| 385 | * |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 386 | * Print @v to @sf for the device assocaited with @pdata. |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 387 | */ |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 388 | u64 __blkg_prfill_u64(struct seq_file *sf, void *pdata, u64 v) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 389 | { |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 390 | const char *dname = blkg_dev_name(pdata_to_blkg(pdata)); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 391 | |
| 392 | if (!dname) |
| 393 | return 0; |
| 394 | |
| 395 | seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v); |
| 396 | return v; |
| 397 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 398 | EXPORT_SYMBOL_GPL(__blkg_prfill_u64); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 399 | |
| 400 | /** |
| 401 | * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat |
| 402 | * @sf: seq_file to print to |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 403 | * @pdata: policy private data of interest |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 404 | * @rwstat: rwstat to print |
| 405 | * |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 406 | * Print @rwstat to @sf for the device assocaited with @pdata. |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 407 | */ |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 408 | u64 __blkg_prfill_rwstat(struct seq_file *sf, void *pdata, |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 409 | const struct blkg_rwstat *rwstat) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 410 | { |
| 411 | static const char *rwstr[] = { |
| 412 | [BLKG_RWSTAT_READ] = "Read", |
| 413 | [BLKG_RWSTAT_WRITE] = "Write", |
| 414 | [BLKG_RWSTAT_SYNC] = "Sync", |
| 415 | [BLKG_RWSTAT_ASYNC] = "Async", |
| 416 | }; |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 417 | const char *dname = blkg_dev_name(pdata_to_blkg(pdata)); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 418 | u64 v; |
| 419 | int i; |
| 420 | |
| 421 | if (!dname) |
| 422 | return 0; |
| 423 | |
| 424 | for (i = 0; i < BLKG_RWSTAT_NR; i++) |
| 425 | seq_printf(sf, "%s %s %llu\n", dname, rwstr[i], |
| 426 | (unsigned long long)rwstat->cnt[i]); |
| 427 | |
| 428 | v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE]; |
| 429 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); |
| 430 | return v; |
| 431 | } |
| 432 | |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 433 | /** |
| 434 | * blkg_prfill_stat - prfill callback for blkg_stat |
| 435 | * @sf: seq_file to print to |
| 436 | * @pdata: policy private data of interest |
| 437 | * @off: offset to the blkg_stat in @pdata |
| 438 | * |
| 439 | * prfill callback for printing a blkg_stat. |
| 440 | */ |
| 441 | u64 blkg_prfill_stat(struct seq_file *sf, void *pdata, int off) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 442 | { |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 443 | return __blkg_prfill_u64(sf, pdata, blkg_stat_read(pdata + off)); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 444 | } |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 445 | EXPORT_SYMBOL_GPL(blkg_prfill_stat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 446 | |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 447 | /** |
| 448 | * blkg_prfill_rwstat - prfill callback for blkg_rwstat |
| 449 | * @sf: seq_file to print to |
| 450 | * @pdata: policy private data of interest |
| 451 | * @off: offset to the blkg_rwstat in @pdata |
| 452 | * |
| 453 | * prfill callback for printing a blkg_rwstat. |
| 454 | */ |
| 455 | u64 blkg_prfill_rwstat(struct seq_file *sf, void *pdata, int off) |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 456 | { |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 457 | struct blkg_rwstat rwstat = blkg_rwstat_read(pdata + off); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 458 | |
Tejun Heo | d366e7e | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 459 | return __blkg_prfill_rwstat(sf, pdata, &rwstat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 460 | } |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 461 | EXPORT_SYMBOL_GPL(blkg_prfill_rwstat); |
Tejun Heo | d3d32e6 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 462 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 463 | /** |
| 464 | * blkg_conf_prep - parse and prepare for per-blkg config update |
| 465 | * @blkcg: target block cgroup |
| 466 | * @input: input string |
| 467 | * @ctx: blkg_conf_ctx to be filled |
| 468 | * |
| 469 | * Parse per-blkg config update from @input and initialize @ctx with the |
| 470 | * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new |
| 471 | * value. This function returns with RCU read locked and must be paired |
| 472 | * with blkg_conf_finish(). |
| 473 | */ |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 474 | int blkg_conf_prep(struct blkio_cgroup *blkcg, const char *input, |
| 475 | struct blkg_conf_ctx *ctx) |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 476 | __acquires(rcu) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 477 | { |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 478 | struct gendisk *disk; |
| 479 | struct blkio_group *blkg; |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 480 | unsigned int major, minor; |
| 481 | unsigned long long v; |
| 482 | int part, ret; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 483 | |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 484 | if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3) |
| 485 | return -EINVAL; |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 486 | |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 487 | disk = get_gendisk(MKDEV(major, minor), &part); |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 488 | if (!disk || part) |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 489 | return -EINVAL; |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 490 | |
| 491 | rcu_read_lock(); |
| 492 | |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 493 | spin_lock_irq(disk->queue->queue_lock); |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 494 | blkg = blkg_lookup_create(blkcg, disk->queue, false); |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 495 | spin_unlock_irq(disk->queue->queue_lock); |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 496 | |
Tejun Heo | 4bfd482 | 2012-03-05 13:15:08 -0800 | [diff] [blame] | 497 | if (IS_ERR(blkg)) { |
| 498 | ret = PTR_ERR(blkg); |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 499 | rcu_read_unlock(); |
| 500 | put_disk(disk); |
| 501 | /* |
| 502 | * If queue was bypassing, we should retry. Do so after a |
| 503 | * short msleep(). It isn't strictly necessary but queue |
| 504 | * can be bypassing for some time and it's always nice to |
| 505 | * avoid busy looping. |
| 506 | */ |
| 507 | if (ret == -EBUSY) { |
| 508 | msleep(10); |
| 509 | ret = restart_syscall(); |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 510 | } |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 511 | return ret; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 512 | } |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 513 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 514 | ctx->disk = disk; |
| 515 | ctx->blkg = blkg; |
Tejun Heo | 726fa69 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 516 | ctx->v = v; |
| 517 | return 0; |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 518 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 519 | EXPORT_SYMBOL_GPL(blkg_conf_prep); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 520 | |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 521 | /** |
| 522 | * blkg_conf_finish - finish up per-blkg config update |
| 523 | * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep() |
| 524 | * |
| 525 | * Finish up after per-blkg config update. This function must be paired |
| 526 | * with blkg_conf_prep(). |
| 527 | */ |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 528 | void blkg_conf_finish(struct blkg_conf_ctx *ctx) |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 529 | __releases(rcu) |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 530 | { |
Tejun Heo | 3a8b31d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 531 | rcu_read_unlock(); |
| 532 | put_disk(ctx->disk); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 533 | } |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 534 | EXPORT_SYMBOL_GPL(blkg_conf_finish); |
Gui Jianfeng | 34d0f17 | 2010-04-13 16:05:49 +0800 | [diff] [blame] | 535 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 536 | struct cftype blkio_files[] = { |
| 537 | { |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 538 | .name = "reset_stats", |
| 539 | .write_u64 = blkiocg_reset_stats, |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 540 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 541 | { } /* terminate */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 542 | }; |
| 543 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 544 | /** |
| 545 | * blkiocg_pre_destroy - cgroup pre_destroy callback |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 546 | * @cgroup: cgroup of interest |
| 547 | * |
| 548 | * This function is called when @cgroup is about to go away and responsible |
| 549 | * for shooting down all blkgs associated with @cgroup. blkgs should be |
| 550 | * removed while holding both q and blkcg locks. As blkcg lock is nested |
| 551 | * inside q lock, this function performs reverse double lock dancing. |
| 552 | * |
| 553 | * This is the blkcg counterpart of ioc_release_fn(). |
| 554 | */ |
Tejun Heo | 959d851 | 2012-04-01 12:30:01 -0700 | [diff] [blame] | 555 | static int blkiocg_pre_destroy(struct cgroup *cgroup) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 556 | { |
| 557 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 558 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 559 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 560 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 561 | while (!hlist_empty(&blkcg->blkg_list)) { |
| 562 | struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first, |
| 563 | struct blkio_group, blkcg_node); |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 564 | struct request_queue *q = blkg->q; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 565 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 566 | if (spin_trylock(q->queue_lock)) { |
| 567 | blkg_destroy(blkg); |
| 568 | spin_unlock(q->queue_lock); |
| 569 | } else { |
| 570 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 571 | cpu_relax(); |
Dan Carpenter | a556793 | 2012-03-29 20:57:08 +0200 | [diff] [blame] | 572 | spin_lock_irq(&blkcg->lock); |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 573 | } |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 574 | } |
Jens Axboe | 0f3942a | 2010-05-03 14:28:55 +0200 | [diff] [blame] | 575 | |
Tejun Heo | 9f13ef6 | 2012-03-05 13:15:21 -0800 | [diff] [blame] | 576 | spin_unlock_irq(&blkcg->lock); |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 580 | static void blkiocg_destroy(struct cgroup *cgroup) |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 581 | { |
| 582 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
| 583 | |
Ben Blum | 67523c4 | 2010-03-10 15:22:11 -0800 | [diff] [blame] | 584 | if (blkcg != &blkio_root_cgroup) |
| 585 | kfree(blkcg); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 588 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 589 | { |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 590 | static atomic64_t id_seq = ATOMIC64_INIT(0); |
Li Zefan | 0341509 | 2010-05-07 08:57:00 +0200 | [diff] [blame] | 591 | struct blkio_cgroup *blkcg; |
| 592 | struct cgroup *parent = cgroup->parent; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 593 | |
Li Zefan | 0341509 | 2010-05-07 08:57:00 +0200 | [diff] [blame] | 594 | if (!parent) { |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 595 | blkcg = &blkio_root_cgroup; |
| 596 | goto done; |
| 597 | } |
| 598 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 599 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
| 600 | if (!blkcg) |
| 601 | return ERR_PTR(-ENOMEM); |
| 602 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 603 | blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT; |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 604 | blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 605 | done: |
| 606 | spin_lock_init(&blkcg->lock); |
| 607 | INIT_HLIST_HEAD(&blkcg->blkg_list); |
| 608 | |
| 609 | return &blkcg->css; |
| 610 | } |
| 611 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 612 | /** |
| 613 | * blkcg_init_queue - initialize blkcg part of request queue |
| 614 | * @q: request_queue to initialize |
| 615 | * |
| 616 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg |
| 617 | * part of new request_queue @q. |
| 618 | * |
| 619 | * RETURNS: |
| 620 | * 0 on success, -errno on failure. |
| 621 | */ |
| 622 | int blkcg_init_queue(struct request_queue *q) |
| 623 | { |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 624 | int ret; |
| 625 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 626 | might_sleep(); |
| 627 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 628 | ret = blk_throtl_init(q); |
| 629 | if (ret) |
| 630 | return ret; |
| 631 | |
| 632 | mutex_lock(&all_q_mutex); |
| 633 | INIT_LIST_HEAD(&q->all_q_node); |
| 634 | list_add_tail(&q->all_q_node, &all_q_list); |
| 635 | mutex_unlock(&all_q_mutex); |
| 636 | |
| 637 | return 0; |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * blkcg_drain_queue - drain blkcg part of request_queue |
| 642 | * @q: request_queue to drain |
| 643 | * |
| 644 | * Called from blk_drain_queue(). Responsible for draining blkcg part. |
| 645 | */ |
| 646 | void blkcg_drain_queue(struct request_queue *q) |
| 647 | { |
| 648 | lockdep_assert_held(q->queue_lock); |
| 649 | |
| 650 | blk_throtl_drain(q); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * blkcg_exit_queue - exit and release blkcg part of request_queue |
| 655 | * @q: request_queue being released |
| 656 | * |
| 657 | * Called from blk_release_queue(). Responsible for exiting blkcg part. |
| 658 | */ |
| 659 | void blkcg_exit_queue(struct request_queue *q) |
| 660 | { |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 661 | mutex_lock(&all_q_mutex); |
| 662 | list_del_init(&q->all_q_node); |
| 663 | mutex_unlock(&all_q_mutex); |
| 664 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 665 | blkg_destroy_all(q, true); |
| 666 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 667 | blk_throtl_exit(q); |
| 668 | } |
| 669 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 670 | /* |
| 671 | * We cannot support shared io contexts, as we have no mean to support |
| 672 | * two tasks with the same ioc in two different groups without major rework |
| 673 | * of the main cic data structures. For now we allow a task to change |
| 674 | * its cgroup only if it's the only owner of its ioc. |
| 675 | */ |
Li Zefan | 761b3ef5 | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 676 | static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 677 | { |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 678 | struct task_struct *task; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 679 | struct io_context *ioc; |
| 680 | int ret = 0; |
| 681 | |
| 682 | /* task_lock() is needed to avoid races with exit_io_context() */ |
Tejun Heo | bb9d97b | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 683 | cgroup_taskset_for_each(task, cgrp, tset) { |
| 684 | task_lock(task); |
| 685 | ioc = task->io_context; |
| 686 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) |
| 687 | ret = -EINVAL; |
| 688 | task_unlock(task); |
| 689 | if (ret) |
| 690 | break; |
| 691 | } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 692 | return ret; |
| 693 | } |
| 694 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 695 | static void blkcg_bypass_start(void) |
| 696 | __acquires(&all_q_mutex) |
| 697 | { |
| 698 | struct request_queue *q; |
| 699 | |
| 700 | mutex_lock(&all_q_mutex); |
| 701 | |
| 702 | list_for_each_entry(q, &all_q_list, all_q_node) { |
| 703 | blk_queue_bypass_start(q); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 704 | blkg_destroy_all(q, false); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | static void blkcg_bypass_end(void) |
| 709 | __releases(&all_q_mutex) |
| 710 | { |
| 711 | struct request_queue *q; |
| 712 | |
| 713 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 714 | blk_queue_bypass_end(q); |
| 715 | |
| 716 | mutex_unlock(&all_q_mutex); |
| 717 | } |
| 718 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 719 | struct cgroup_subsys blkio_subsys = { |
| 720 | .name = "blkio", |
| 721 | .create = blkiocg_create, |
| 722 | .can_attach = blkiocg_can_attach, |
Tejun Heo | 959d851 | 2012-04-01 12:30:01 -0700 | [diff] [blame] | 723 | .pre_destroy = blkiocg_pre_destroy, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 724 | .destroy = blkiocg_destroy, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 725 | .subsys_id = blkio_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 726 | .base_cftypes = blkio_files, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 727 | .module = THIS_MODULE, |
| 728 | }; |
| 729 | EXPORT_SYMBOL_GPL(blkio_subsys); |
| 730 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 731 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
| 732 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 733 | struct request_queue *q; |
| 734 | |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 735 | mutex_lock(&blkcg_pol_mutex); |
| 736 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 737 | blkcg_bypass_start(); |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 738 | |
| 739 | BUG_ON(blkio_policy[blkiop->plid]); |
| 740 | blkio_policy[blkiop->plid] = blkiop; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 741 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 742 | update_root_blkg_pd(q, blkiop->plid); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 743 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 744 | blkcg_bypass_end(); |
Tejun Heo | 44ea53d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 745 | |
| 746 | if (blkiop->cftypes) |
| 747 | WARN_ON(cgroup_add_cftypes(&blkio_subsys, blkiop->cftypes)); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 748 | |
| 749 | mutex_unlock(&blkcg_pol_mutex); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 750 | } |
| 751 | EXPORT_SYMBOL_GPL(blkio_policy_register); |
| 752 | |
| 753 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) |
| 754 | { |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 755 | struct request_queue *q; |
| 756 | |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 757 | mutex_lock(&blkcg_pol_mutex); |
| 758 | |
Tejun Heo | 44ea53d | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 759 | if (blkiop->cftypes) |
| 760 | cgroup_rm_cftypes(&blkio_subsys, blkiop->cftypes); |
| 761 | |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 762 | blkcg_bypass_start(); |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 763 | |
| 764 | BUG_ON(blkio_policy[blkiop->plid] != blkiop); |
| 765 | blkio_policy[blkiop->plid] = NULL; |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 766 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 767 | list_for_each_entry(q, &all_q_list, all_q_node) |
| 768 | update_root_blkg_pd(q, blkiop->plid); |
Tejun Heo | 923adde | 2012-03-05 13:15:13 -0800 | [diff] [blame] | 769 | blkcg_bypass_end(); |
Tejun Heo | bc0d650 | 2012-04-13 13:11:26 -0700 | [diff] [blame^] | 770 | |
| 771 | mutex_unlock(&blkcg_pol_mutex); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 772 | } |
| 773 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |