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