blob: b26320720a3cd087008193a39af45304440a22a3 [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001/*
2 * Common Block IO controller cgroup interface
3 *
4 * Based on ideas and code from CFQ, CFS and BFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11 * Nauman Rafique <nauman@google.com>
Arianna Avanzinie48453c2015-06-05 23:38:42 +020012 *
13 * For policy-specific per-blkcg data:
14 * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
15 * Arianna Avanzini <avanzini.arianna@gmail.com>
Vivek Goyal31e4c282009-12-03 12:59:42 -050016 */
17#include <linux/ioprio.h>
Vivek Goyal22084192009-12-03 12:59:49 -050018#include <linux/kdev_t.h>
Vivek Goyal9d6a9862009-12-04 10:36:41 -050019#include <linux/module.h>
Stephen Rothwellaccee782009-12-07 19:29:39 +110020#include <linux/err.h>
Divyesh Shah91952912010-04-01 15:01:41 -070021#include <linux/blkdev.h>
Tejun Heo52ebea72015-05-22 17:13:37 -040022#include <linux/backing-dev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Gui Jianfeng34d0f172010-04-13 16:05:49 +080024#include <linux/genhd.h>
Tejun Heo72e06c22012-03-05 13:15:00 -080025#include <linux/delay.h>
Tejun Heo9a9e8a22012-03-19 15:10:56 -070026#include <linux/atomic.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040027#include <linux/blk-cgroup.h>
Tejun Heo5efd6112012-03-05 13:15:12 -080028#include "blk.h"
Vivek Goyal3e252062009-12-04 10:36:42 -050029
Divyesh Shah84c124d2010-04-09 08:31:19 +020030#define MAX_KEY_LEN 100
31
Tejun Heo838f13b2015-07-09 16:39:47 -040032/*
33 * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
34 * blkcg_pol_register_mutex nests outside of it and synchronizes entire
35 * policy [un]register operations including cgroup file additions /
36 * removals. Putting cgroup file registration outside blkcg_pol_mutex
37 * allows grabbing it from cgroup callbacks.
38 */
39static DEFINE_MUTEX(blkcg_pol_register_mutex);
Tejun Heobc0d6502012-04-13 13:11:26 -070040static DEFINE_MUTEX(blkcg_pol_mutex);
Tejun Heo923adde2012-03-05 13:15:13 -080041
Arianna Avanzinie48453c2015-06-05 23:38:42 +020042struct blkcg blkcg_root;
Tejun Heo3c798392012-04-16 13:57:25 -070043EXPORT_SYMBOL_GPL(blkcg_root);
Vivek Goyal9d6a9862009-12-04 10:36:41 -050044
Tejun Heo496d5e72015-05-22 17:13:21 -040045struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
46
Tejun Heo3c798392012-04-16 13:57:25 -070047static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
Tejun Heo035d10b2012-03-05 13:15:04 -080048
Tejun Heo7876f932015-07-09 16:39:49 -040049static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */
50
Tejun Heoa2b16932012-04-13 13:11:33 -070051static bool blkcg_policy_enabled(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -070052 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -070053{
54 return pol && test_bit(pol->plid, q->blkcg_pols);
55}
56
Tejun Heo03814112012-03-05 13:15:14 -080057/**
58 * blkg_free - free a blkg
59 * @blkg: blkg to free
60 *
61 * Free @blkg which may be partially allocated.
62 */
Tejun Heo3c798392012-04-16 13:57:25 -070063static void blkg_free(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -080064{
Tejun Heoe8989fa2012-03-05 13:15:20 -080065 int i;
Tejun Heo549d3aa2012-03-05 13:15:16 -080066
67 if (!blkg)
68 return;
69
Tejun Heodb613672013-05-14 13:52:31 -070070 for (i = 0; i < BLKCG_MAX_POLS; i++)
Tejun Heo001bea72015-08-18 14:55:11 -070071 if (blkg->pd[i])
72 blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
Tejun Heoe8989fa2012-03-05 13:15:20 -080073
Tejun Heo994b7832015-08-18 14:55:07 -070074 if (blkg->blkcg != &blkcg_root)
75 blk_exit_rl(&blkg->rl);
Tejun Heo549d3aa2012-03-05 13:15:16 -080076 kfree(blkg);
Tejun Heo03814112012-03-05 13:15:14 -080077}
78
79/**
80 * blkg_alloc - allocate a blkg
81 * @blkcg: block cgroup the new blkg is associated with
82 * @q: request_queue the new blkg is associated with
Tejun Heo15974992012-06-04 20:40:52 -070083 * @gfp_mask: allocation mask to use
Tejun Heo03814112012-03-05 13:15:14 -080084 *
Tejun Heoe8989fa2012-03-05 13:15:20 -080085 * Allocate a new blkg assocating @blkcg and @q.
Tejun Heo03814112012-03-05 13:15:14 -080086 */
Tejun Heo15974992012-06-04 20:40:52 -070087static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
88 gfp_t gfp_mask)
Tejun Heo03814112012-03-05 13:15:14 -080089{
Tejun Heo3c798392012-04-16 13:57:25 -070090 struct blkcg_gq *blkg;
Tejun Heoe8989fa2012-03-05 13:15:20 -080091 int i;
Tejun Heo03814112012-03-05 13:15:14 -080092
93 /* alloc and init base part */
Tejun Heo15974992012-06-04 20:40:52 -070094 blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
Tejun Heo03814112012-03-05 13:15:14 -080095 if (!blkg)
96 return NULL;
97
Tejun Heoc875f4d2012-03-05 13:15:22 -080098 blkg->q = q;
Tejun Heoe8989fa2012-03-05 13:15:20 -080099 INIT_LIST_HEAD(&blkg->q_node);
Tejun Heo03814112012-03-05 13:15:14 -0800100 blkg->blkcg = blkcg;
Tejun Heoa5049a82014-06-19 17:42:57 -0400101 atomic_set(&blkg->refcnt, 1);
Tejun Heo03814112012-03-05 13:15:14 -0800102
Tejun Heoa0516612012-06-26 15:05:44 -0700103 /* root blkg uses @q->root_rl, init rl only for !root blkgs */
104 if (blkcg != &blkcg_root) {
105 if (blk_init_rl(&blkg->rl, q, gfp_mask))
106 goto err_free;
107 blkg->rl.blkg = blkg;
108 }
109
Tejun Heo8bd435b2012-04-13 13:11:28 -0700110 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700111 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heoe8989fa2012-03-05 13:15:20 -0800112 struct blkg_policy_data *pd;
Tejun Heo03814112012-03-05 13:15:14 -0800113
Tejun Heoa2b16932012-04-13 13:11:33 -0700114 if (!blkcg_policy_enabled(q, pol))
Tejun Heoe8989fa2012-03-05 13:15:20 -0800115 continue;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800116
Tejun Heoe8989fa2012-03-05 13:15:20 -0800117 /* alloc per-policy data and attach it to blkg */
Tejun Heo001bea72015-08-18 14:55:11 -0700118 pd = pol->pd_alloc_fn(gfp_mask, q->node);
Tejun Heoa0516612012-06-26 15:05:44 -0700119 if (!pd)
120 goto err_free;
Tejun Heo549d3aa2012-03-05 13:15:16 -0800121
Tejun Heoe8989fa2012-03-05 13:15:20 -0800122 blkg->pd[i] = pd;
123 pd->blkg = blkg;
Tejun Heob276a872013-01-09 08:05:12 -0800124 pd->plid = i;
Tejun Heoe8989fa2012-03-05 13:15:20 -0800125 }
126
Tejun Heo03814112012-03-05 13:15:14 -0800127 return blkg;
Tejun Heoa0516612012-06-26 15:05:44 -0700128
129err_free:
130 blkg_free(blkg);
131 return NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800132}
133
Tejun Heo24f29042015-08-18 14:55:17 -0700134struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
135 struct request_queue *q, bool update_hint)
Tejun Heo80fd9972012-04-13 14:50:53 -0700136{
Tejun Heo3c798392012-04-16 13:57:25 -0700137 struct blkcg_gq *blkg;
Tejun Heo80fd9972012-04-13 14:50:53 -0700138
Tejun Heoa6371202012-04-19 16:29:24 -0700139 /*
Tejun Heo86cde6b2013-01-09 08:05:10 -0800140 * Hint didn't match. Look up from the radix tree. Note that the
141 * hint can only be updated under queue_lock as otherwise @blkg
142 * could have already been removed from blkg_tree. The caller is
143 * responsible for grabbing queue_lock if @update_hint.
Tejun Heoa6371202012-04-19 16:29:24 -0700144 */
145 blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
Tejun Heo86cde6b2013-01-09 08:05:10 -0800146 if (blkg && blkg->q == q) {
147 if (update_hint) {
148 lockdep_assert_held(q->queue_lock);
149 rcu_assign_pointer(blkcg->blkg_hint, blkg);
150 }
Tejun Heoa6371202012-04-19 16:29:24 -0700151 return blkg;
Tejun Heo86cde6b2013-01-09 08:05:10 -0800152 }
Tejun Heoa6371202012-04-19 16:29:24 -0700153
Tejun Heo80fd9972012-04-13 14:50:53 -0700154 return NULL;
155}
Tejun Heoae118892015-08-18 14:55:20 -0700156EXPORT_SYMBOL_GPL(blkg_lookup_slowpath);
Tejun Heo80fd9972012-04-13 14:50:53 -0700157
Tejun Heo15974992012-06-04 20:40:52 -0700158/*
159 * If @new_blkg is %NULL, this function tries to allocate a new one as
Tejun Heod93a11f2015-08-18 14:55:01 -0700160 * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
Tejun Heo15974992012-06-04 20:40:52 -0700161 */
Tejun Heo86cde6b2013-01-09 08:05:10 -0800162static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
163 struct request_queue *q,
164 struct blkcg_gq *new_blkg)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400165{
Tejun Heo3c798392012-04-16 13:57:25 -0700166 struct blkcg_gq *blkg;
Tejun Heoce7acfe2015-05-22 17:13:38 -0400167 struct bdi_writeback_congested *wb_congested;
Tejun Heof427d902013-01-09 08:05:12 -0800168 int i, ret;
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400169
Tejun Heocd1604f2012-03-05 13:15:06 -0800170 WARN_ON_ONCE(!rcu_read_lock_held());
171 lockdep_assert_held(q->queue_lock);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500172
Tejun Heo7ee9c562012-03-05 13:15:11 -0800173 /* blkg holds a reference to blkcg */
Tejun Heoec903c02014-05-13 12:11:01 -0400174 if (!css_tryget_online(&blkcg->css)) {
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800175 ret = -EINVAL;
176 goto err_free_blkg;
Tejun Heo15974992012-06-04 20:40:52 -0700177 }
Tejun Heocd1604f2012-03-05 13:15:06 -0800178
Tejun Heoce7acfe2015-05-22 17:13:38 -0400179 wb_congested = wb_congested_get_create(&q->backing_dev_info,
Tejun Heod93a11f2015-08-18 14:55:01 -0700180 blkcg->css.id, GFP_NOWAIT);
Tejun Heoce7acfe2015-05-22 17:13:38 -0400181 if (!wb_congested) {
182 ret = -ENOMEM;
183 goto err_put_css;
184 }
185
Tejun Heo496fb782012-04-19 16:29:23 -0700186 /* allocate */
Tejun Heo15974992012-06-04 20:40:52 -0700187 if (!new_blkg) {
Tejun Heod93a11f2015-08-18 14:55:01 -0700188 new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT);
Tejun Heo15974992012-06-04 20:40:52 -0700189 if (unlikely(!new_blkg)) {
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800190 ret = -ENOMEM;
Tejun Heoce7acfe2015-05-22 17:13:38 -0400191 goto err_put_congested;
Tejun Heo15974992012-06-04 20:40:52 -0700192 }
193 }
194 blkg = new_blkg;
Tejun Heoce7acfe2015-05-22 17:13:38 -0400195 blkg->wb_congested = wb_congested;
Tejun Heocd1604f2012-03-05 13:15:06 -0800196
Tejun Heodb613672013-05-14 13:52:31 -0700197 /* link parent */
Tejun Heo3c547862013-01-09 08:05:10 -0800198 if (blkcg_parent(blkcg)) {
199 blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
200 if (WARN_ON_ONCE(!blkg->parent)) {
Tejun Heo2423c9c2013-05-14 13:52:30 -0700201 ret = -EINVAL;
Tejun Heoce7acfe2015-05-22 17:13:38 -0400202 goto err_put_congested;
Tejun Heo3c547862013-01-09 08:05:10 -0800203 }
204 blkg_get(blkg->parent);
205 }
206
Tejun Heodb613672013-05-14 13:52:31 -0700207 /* invoke per-policy init */
208 for (i = 0; i < BLKCG_MAX_POLS; i++) {
209 struct blkcg_policy *pol = blkcg_policy[i];
210
211 if (blkg->pd[i] && pol->pd_init_fn)
Tejun Heoa9520cd2015-08-18 14:55:14 -0700212 pol->pd_init_fn(blkg->pd[i]);
Tejun Heodb613672013-05-14 13:52:31 -0700213 }
214
215 /* insert */
Tejun Heoa6371202012-04-19 16:29:24 -0700216 spin_lock(&blkcg->lock);
217 ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
218 if (likely(!ret)) {
219 hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
220 list_add(&blkg->q_node, &q->blkg_list);
Tejun Heof427d902013-01-09 08:05:12 -0800221
222 for (i = 0; i < BLKCG_MAX_POLS; i++) {
223 struct blkcg_policy *pol = blkcg_policy[i];
224
225 if (blkg->pd[i] && pol->pd_online_fn)
Tejun Heoa9520cd2015-08-18 14:55:14 -0700226 pol->pd_online_fn(blkg->pd[i]);
Tejun Heof427d902013-01-09 08:05:12 -0800227 }
Tejun Heoa6371202012-04-19 16:29:24 -0700228 }
Tejun Heof427d902013-01-09 08:05:12 -0800229 blkg->online = true;
Tejun Heoa6371202012-04-19 16:29:24 -0700230 spin_unlock(&blkcg->lock);
231
Tejun Heoec13b1d2015-05-22 17:13:19 -0400232 if (!ret)
Tejun Heoa6371202012-04-19 16:29:24 -0700233 return blkg;
Tejun Heo15974992012-06-04 20:40:52 -0700234
Tejun Heo3c547862013-01-09 08:05:10 -0800235 /* @blkg failed fully initialized, use the usual release path */
236 blkg_put(blkg);
237 return ERR_PTR(ret);
238
Tejun Heoce7acfe2015-05-22 17:13:38 -0400239err_put_congested:
240 wb_congested_put(wb_congested);
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800241err_put_css:
Tejun Heo496fb782012-04-19 16:29:23 -0700242 css_put(&blkcg->css);
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800243err_free_blkg:
Tejun Heo15974992012-06-04 20:40:52 -0700244 blkg_free(new_blkg);
Tejun Heo93e6d5d2013-01-09 08:05:10 -0800245 return ERR_PTR(ret);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500246}
Tejun Heo3c96cb32012-04-13 13:11:34 -0700247
Tejun Heo86cde6b2013-01-09 08:05:10 -0800248/**
249 * blkg_lookup_create - lookup blkg, try to create one if not there
250 * @blkcg: blkcg of interest
251 * @q: request_queue of interest
252 *
253 * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
Tejun Heo3c547862013-01-09 08:05:10 -0800254 * create one. blkg creation is performed recursively from blkcg_root such
255 * that all non-root blkg's have access to the parent blkg. This function
256 * should be called under RCU read lock and @q->queue_lock.
Tejun Heo86cde6b2013-01-09 08:05:10 -0800257 *
258 * Returns pointer to the looked up or created blkg on success, ERR_PTR()
259 * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
260 * dead and bypassing, returns ERR_PTR(-EBUSY).
261 */
Tejun Heo3c798392012-04-16 13:57:25 -0700262struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
263 struct request_queue *q)
Tejun Heo3c96cb32012-04-13 13:11:34 -0700264{
Tejun Heo86cde6b2013-01-09 08:05:10 -0800265 struct blkcg_gq *blkg;
266
267 WARN_ON_ONCE(!rcu_read_lock_held());
268 lockdep_assert_held(q->queue_lock);
269
Tejun Heo3c96cb32012-04-13 13:11:34 -0700270 /*
271 * This could be the first entry point of blkcg implementation and
272 * we shouldn't allow anything to go through for a bypassing queue.
273 */
274 if (unlikely(blk_queue_bypass(q)))
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100275 return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
Tejun Heo86cde6b2013-01-09 08:05:10 -0800276
277 blkg = __blkg_lookup(blkcg, q, true);
278 if (blkg)
279 return blkg;
280
Tejun Heo3c547862013-01-09 08:05:10 -0800281 /*
282 * Create blkgs walking down from blkcg_root to @blkcg, so that all
283 * non-root blkgs have access to their parents.
284 */
285 while (true) {
286 struct blkcg *pos = blkcg;
287 struct blkcg *parent = blkcg_parent(blkcg);
288
289 while (parent && !__blkg_lookup(parent, q, false)) {
290 pos = parent;
291 parent = blkcg_parent(parent);
292 }
293
294 blkg = blkg_create(pos, q, NULL);
295 if (pos == blkcg || IS_ERR(blkg))
296 return blkg;
297 }
Tejun Heo3c96cb32012-04-13 13:11:34 -0700298}
Vivek Goyal31e4c282009-12-03 12:59:42 -0500299
Tejun Heo3c798392012-04-16 13:57:25 -0700300static void blkg_destroy(struct blkcg_gq *blkg)
Tejun Heo72e06c22012-03-05 13:15:00 -0800301{
Tejun Heo3c798392012-04-16 13:57:25 -0700302 struct blkcg *blkcg = blkg->blkcg;
Tejun Heof427d902013-01-09 08:05:12 -0800303 int i;
Tejun Heo03aa2642012-03-05 13:15:19 -0800304
Tejun Heo27e1f9d2012-06-05 13:36:44 +0200305 lockdep_assert_held(blkg->q->queue_lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800306 lockdep_assert_held(&blkcg->lock);
Tejun Heo03aa2642012-03-05 13:15:19 -0800307
308 /* Something wrong if we are trying to remove same group twice */
Tejun Heoe8989fa2012-03-05 13:15:20 -0800309 WARN_ON_ONCE(list_empty(&blkg->q_node));
Tejun Heo9f13ef62012-03-05 13:15:21 -0800310 WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
Tejun Heoa6371202012-04-19 16:29:24 -0700311
Tejun Heof427d902013-01-09 08:05:12 -0800312 for (i = 0; i < BLKCG_MAX_POLS; i++) {
313 struct blkcg_policy *pol = blkcg_policy[i];
314
315 if (blkg->pd[i] && pol->pd_offline_fn)
Tejun Heoa9520cd2015-08-18 14:55:14 -0700316 pol->pd_offline_fn(blkg->pd[i]);
Tejun Heof427d902013-01-09 08:05:12 -0800317 }
318 blkg->online = false;
319
Tejun Heoa6371202012-04-19 16:29:24 -0700320 radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
Tejun Heoe8989fa2012-03-05 13:15:20 -0800321 list_del_init(&blkg->q_node);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800322 hlist_del_init_rcu(&blkg->blkcg_node);
Tejun Heo03aa2642012-03-05 13:15:19 -0800323
Tejun Heo03aa2642012-03-05 13:15:19 -0800324 /*
Tejun Heoa6371202012-04-19 16:29:24 -0700325 * Both setting lookup hint to and clearing it from @blkg are done
326 * under queue_lock. If it's not pointing to @blkg now, it never
327 * will. Hint assignment itself can race safely.
328 */
Paul E. McKenneyec6c6762014-02-17 13:35:57 -0800329 if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
Tejun Heoa6371202012-04-19 16:29:24 -0700330 rcu_assign_pointer(blkcg->blkg_hint, NULL);
331
332 /*
Tejun Heo03aa2642012-03-05 13:15:19 -0800333 * Put the reference taken at the time of creation so that when all
334 * queues are gone, group can be destroyed.
335 */
336 blkg_put(blkg);
337}
338
Tejun Heo9f13ef62012-03-05 13:15:21 -0800339/**
340 * blkg_destroy_all - destroy all blkgs associated with a request_queue
341 * @q: request_queue of interest
Tejun Heo9f13ef62012-03-05 13:15:21 -0800342 *
Tejun Heo3c96cb32012-04-13 13:11:34 -0700343 * Destroy all blkgs associated with @q.
Tejun Heo9f13ef62012-03-05 13:15:21 -0800344 */
Tejun Heo3c96cb32012-04-13 13:11:34 -0700345static void blkg_destroy_all(struct request_queue *q)
Tejun Heo03aa2642012-03-05 13:15:19 -0800346{
Tejun Heo3c798392012-04-16 13:57:25 -0700347 struct blkcg_gq *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800348
Tejun Heo6d18b002012-04-13 13:11:35 -0700349 lockdep_assert_held(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800350
Tejun Heo9f13ef62012-03-05 13:15:21 -0800351 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo3c798392012-04-16 13:57:25 -0700352 struct blkcg *blkcg = blkg->blkcg;
Tejun Heo72e06c22012-03-05 13:15:00 -0800353
Tejun Heo9f13ef62012-03-05 13:15:21 -0800354 spin_lock(&blkcg->lock);
355 blkg_destroy(blkg);
356 spin_unlock(&blkcg->lock);
Tejun Heo72e06c22012-03-05 13:15:00 -0800357 }
358}
359
Tejun Heo2a4fd072013-05-14 13:52:31 -0700360/*
361 * A group is RCU protected, but having an rcu lock does not mean that one
362 * can access all the fields of blkg and assume these are valid. For
363 * example, don't try to follow throtl_data and request queue links.
364 *
365 * Having a reference to blkg under an rcu allows accesses to only values
366 * local to groups like group stats and group rate limits.
367 */
368void __blkg_release_rcu(struct rcu_head *rcu_head)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800369{
Tejun Heo2a4fd072013-05-14 13:52:31 -0700370 struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head);
Tejun Heodb613672013-05-14 13:52:31 -0700371
Tejun Heo3c547862013-01-09 08:05:10 -0800372 /* release the blkcg and parent blkg refs this blkg has been holding */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800373 css_put(&blkg->blkcg->css);
Tejun Heoa5049a82014-06-19 17:42:57 -0400374 if (blkg->parent)
Tejun Heo3c547862013-01-09 08:05:10 -0800375 blkg_put(blkg->parent);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800376
Tejun Heoce7acfe2015-05-22 17:13:38 -0400377 wb_congested_put(blkg->wb_congested);
378
Tejun Heo2a4fd072013-05-14 13:52:31 -0700379 blkg_free(blkg);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800380}
Tejun Heo2a4fd072013-05-14 13:52:31 -0700381EXPORT_SYMBOL_GPL(__blkg_release_rcu);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800382
Tejun Heoa0516612012-06-26 15:05:44 -0700383/*
384 * The next function used by blk_queue_for_each_rl(). It's a bit tricky
385 * because the root blkg uses @q->root_rl instead of its own rl.
386 */
387struct request_list *__blk_queue_next_rl(struct request_list *rl,
388 struct request_queue *q)
389{
390 struct list_head *ent;
391 struct blkcg_gq *blkg;
392
393 /*
394 * Determine the current blkg list_head. The first entry is
395 * root_rl which is off @q->blkg_list and mapped to the head.
396 */
397 if (rl == &q->root_rl) {
398 ent = &q->blkg_list;
Jun'ichi Nomura65c77fd2012-10-22 10:15:37 +0900399 /* There are no more block groups, hence no request lists */
400 if (list_empty(ent))
401 return NULL;
Tejun Heoa0516612012-06-26 15:05:44 -0700402 } else {
403 blkg = container_of(rl, struct blkcg_gq, rl);
404 ent = &blkg->q_node;
405 }
406
407 /* walk to the next list_head, skip root blkcg */
408 ent = ent->next;
409 if (ent == &q->root_blkg->q_node)
410 ent = ent->next;
411 if (ent == &q->blkg_list)
412 return NULL;
413
414 blkg = container_of(ent, struct blkcg_gq, q_node);
415 return &blkg->rl;
416}
417
Tejun Heo182446d2013-08-08 20:11:24 -0400418static int blkcg_reset_stats(struct cgroup_subsys_state *css,
419 struct cftype *cftype, u64 val)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700420{
Tejun Heo182446d2013-08-08 20:11:24 -0400421 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -0700422 struct blkcg_gq *blkg;
Tejun Heobc0d6502012-04-13 13:11:26 -0700423 int i;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700424
Tejun Heo838f13b2015-07-09 16:39:47 -0400425 mutex_lock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700426 spin_lock_irq(&blkcg->lock);
Tejun Heo997a0262012-03-08 10:53:58 -0800427
428 /*
429 * Note that stat reset is racy - it doesn't synchronize against
430 * stat updates. This is a debug feature which shouldn't exist
431 * anyway. If you get hit by a race, retry.
432 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800433 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo8bd435b2012-04-13 13:11:28 -0700434 for (i = 0; i < BLKCG_MAX_POLS; i++) {
Tejun Heo3c798392012-04-16 13:57:25 -0700435 struct blkcg_policy *pol = blkcg_policy[i];
Tejun Heo549d3aa2012-03-05 13:15:16 -0800436
Tejun Heoa9520cd2015-08-18 14:55:14 -0700437 if (blkg->pd[i] && pol->pd_reset_stats_fn)
438 pol->pd_reset_stats_fn(blkg->pd[i]);
Tejun Heobc0d6502012-04-13 13:11:26 -0700439 }
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700440 }
Vivek Goyalf0bdc8c2011-05-19 15:38:30 -0400441
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700442 spin_unlock_irq(&blkcg->lock);
Tejun Heobc0d6502012-04-13 13:11:26 -0700443 mutex_unlock(&blkcg_pol_mutex);
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700444 return 0;
445}
446
Tejun Heo3c798392012-04-16 13:57:25 -0700447static const char *blkg_dev_name(struct blkcg_gq *blkg)
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700448{
Tejun Heod3d32e62012-04-01 14:38:42 -0700449 /* some drivers (floppy) instantiate a queue w/o disk registered */
450 if (blkg->q->backing_dev_info.dev)
451 return dev_name(blkg->q->backing_dev_info.dev);
452 return NULL;
Divyesh Shah303a3ac2010-04-01 15:01:24 -0700453}
454
Tejun Heod3d32e62012-04-01 14:38:42 -0700455/**
456 * blkcg_print_blkgs - helper for printing per-blkg data
457 * @sf: seq_file to print to
458 * @blkcg: blkcg of interest
459 * @prfill: fill function to print out a blkg
460 * @pol: policy in question
461 * @data: data to be passed to @prfill
462 * @show_total: to print out sum of prfill return values or not
463 *
464 * This function invokes @prfill on each blkg of @blkcg if pd for the
465 * policy specified by @pol exists. @prfill is invoked with @sf, the
Tejun Heo810ecfa2013-01-09 08:05:13 -0800466 * policy data and @data and the matching queue lock held. If @show_total
467 * is %true, the sum of the return values from @prfill is printed with
468 * "Total" label at the end.
Tejun Heod3d32e62012-04-01 14:38:42 -0700469 *
470 * This is to be used to construct print functions for
471 * cftype->read_seq_string method.
472 */
Tejun Heo3c798392012-04-16 13:57:25 -0700473void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700474 u64 (*prfill)(struct seq_file *,
475 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700476 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700477 bool show_total)
Vivek Goyal5624a4e2011-05-19 15:38:28 -0400478{
Tejun Heo3c798392012-04-16 13:57:25 -0700479 struct blkcg_gq *blkg;
Tejun Heod3d32e62012-04-01 14:38:42 -0700480 u64 total = 0;
481
Tejun Heo810ecfa2013-01-09 08:05:13 -0800482 rcu_read_lock();
Linus Torvaldsee89f812013-02-28 12:52:24 -0800483 hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo810ecfa2013-01-09 08:05:13 -0800484 spin_lock_irq(blkg->q->queue_lock);
Tejun Heoa2b16932012-04-13 13:11:33 -0700485 if (blkcg_policy_enabled(blkg->q, pol))
Tejun Heof95a04a2012-04-16 13:57:26 -0700486 total += prfill(sf, blkg->pd[pol->plid], data);
Tejun Heo810ecfa2013-01-09 08:05:13 -0800487 spin_unlock_irq(blkg->q->queue_lock);
488 }
489 rcu_read_unlock();
Tejun Heod3d32e62012-04-01 14:38:42 -0700490
491 if (show_total)
492 seq_printf(sf, "Total %llu\n", (unsigned long long)total);
493}
Tejun Heo829fdb52012-04-01 14:38:43 -0700494EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
Tejun Heod3d32e62012-04-01 14:38:42 -0700495
496/**
497 * __blkg_prfill_u64 - prfill helper for a single u64 value
498 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700499 * @pd: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700500 * @v: value to print
501 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700502 * Print @v to @sf for the device assocaited with @pd.
Tejun Heod3d32e62012-04-01 14:38:42 -0700503 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700504u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
Tejun Heod3d32e62012-04-01 14:38:42 -0700505{
Tejun Heof95a04a2012-04-16 13:57:26 -0700506 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e62012-04-01 14:38:42 -0700507
508 if (!dname)
509 return 0;
510
511 seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
512 return v;
513}
Tejun Heo829fdb52012-04-01 14:38:43 -0700514EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
Tejun Heod3d32e62012-04-01 14:38:42 -0700515
516/**
517 * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
518 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700519 * @pd: policy private data of interest
Tejun Heod3d32e62012-04-01 14:38:42 -0700520 * @rwstat: rwstat to print
521 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700522 * Print @rwstat to @sf for the device assocaited with @pd.
Tejun Heod3d32e62012-04-01 14:38:42 -0700523 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700524u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700525 const struct blkg_rwstat *rwstat)
Tejun Heod3d32e62012-04-01 14:38:42 -0700526{
527 static const char *rwstr[] = {
528 [BLKG_RWSTAT_READ] = "Read",
529 [BLKG_RWSTAT_WRITE] = "Write",
530 [BLKG_RWSTAT_SYNC] = "Sync",
531 [BLKG_RWSTAT_ASYNC] = "Async",
532 };
Tejun Heof95a04a2012-04-16 13:57:26 -0700533 const char *dname = blkg_dev_name(pd->blkg);
Tejun Heod3d32e62012-04-01 14:38:42 -0700534 u64 v;
535 int i;
536
537 if (!dname)
538 return 0;
539
540 for (i = 0; i < BLKG_RWSTAT_NR; i++)
541 seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700542 (unsigned long long)atomic64_read(&rwstat->aux_cnt[i]));
Tejun Heod3d32e62012-04-01 14:38:42 -0700543
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700544 v = atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_READ]) +
545 atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_WRITE]);
Tejun Heod3d32e62012-04-01 14:38:42 -0700546 seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
547 return v;
548}
Tejun Heob50da392013-01-09 08:05:12 -0800549EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700550
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700551/**
552 * blkg_prfill_stat - prfill callback for blkg_stat
553 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700554 * @pd: policy private data of interest
555 * @off: offset to the blkg_stat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700556 *
557 * prfill callback for printing a blkg_stat.
558 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700559u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700560{
Tejun Heof95a04a2012-04-16 13:57:26 -0700561 return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
Tejun Heod3d32e62012-04-01 14:38:42 -0700562}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700563EXPORT_SYMBOL_GPL(blkg_prfill_stat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700564
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700565/**
566 * blkg_prfill_rwstat - prfill callback for blkg_rwstat
567 * @sf: seq_file to print to
Tejun Heof95a04a2012-04-16 13:57:26 -0700568 * @pd: policy private data of interest
569 * @off: offset to the blkg_rwstat in @pd
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700570 *
571 * prfill callback for printing a blkg_rwstat.
572 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700573u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
574 int off)
Tejun Heod3d32e62012-04-01 14:38:42 -0700575{
Tejun Heof95a04a2012-04-16 13:57:26 -0700576 struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
Tejun Heod3d32e62012-04-01 14:38:42 -0700577
Tejun Heof95a04a2012-04-16 13:57:26 -0700578 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700579}
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700580EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
Tejun Heod3d32e62012-04-01 14:38:42 -0700581
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700582/**
Tejun Heo16b3de62013-01-09 08:05:12 -0800583 * blkg_stat_recursive_sum - collect hierarchical blkg_stat
Tejun Heof12c74c2015-08-18 14:55:23 -0700584 * @blkg: blkg of interest
585 * @pol: blkcg_policy which contains the blkg_stat
586 * @off: offset to the blkg_stat in blkg_policy_data or @blkg
Tejun Heo16b3de62013-01-09 08:05:12 -0800587 *
Tejun Heof12c74c2015-08-18 14:55:23 -0700588 * Collect the blkg_stat specified by @blkg, @pol and @off and all its
589 * online descendants and their aux counts. The caller must be holding the
590 * queue lock for online tests.
591 *
592 * If @pol is NULL, blkg_stat is at @off bytes into @blkg; otherwise, it is
593 * at @off bytes into @blkg's blkg_policy_data of the policy.
Tejun Heo16b3de62013-01-09 08:05:12 -0800594 */
Tejun Heof12c74c2015-08-18 14:55:23 -0700595u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
596 struct blkcg_policy *pol, int off)
Tejun Heo16b3de62013-01-09 08:05:12 -0800597{
Tejun Heo16b3de62013-01-09 08:05:12 -0800598 struct blkcg_gq *pos_blkg;
Tejun Heo492eb212013-08-08 20:11:25 -0400599 struct cgroup_subsys_state *pos_css;
Tejun Heobd8815a2013-08-08 20:11:27 -0400600 u64 sum = 0;
Tejun Heo16b3de62013-01-09 08:05:12 -0800601
Tejun Heof12c74c2015-08-18 14:55:23 -0700602 lockdep_assert_held(blkg->q->queue_lock);
Tejun Heo16b3de62013-01-09 08:05:12 -0800603
Tejun Heo16b3de62013-01-09 08:05:12 -0800604 rcu_read_lock();
Tejun Heof12c74c2015-08-18 14:55:23 -0700605 blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
606 struct blkg_stat *stat;
Tejun Heo16b3de62013-01-09 08:05:12 -0800607
Tejun Heof12c74c2015-08-18 14:55:23 -0700608 if (!pos_blkg->online)
609 continue;
610
611 if (pol)
612 stat = (void *)blkg_to_pd(pos_blkg, pol) + off;
613 else
614 stat = (void *)blkg + off;
615
616 sum += blkg_stat_read(stat) + atomic64_read(&stat->aux_cnt);
Tejun Heo16b3de62013-01-09 08:05:12 -0800617 }
618 rcu_read_unlock();
619
620 return sum;
621}
622EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
623
624/**
625 * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
Tejun Heof12c74c2015-08-18 14:55:23 -0700626 * @blkg: blkg of interest
627 * @pol: blkcg_policy which contains the blkg_rwstat
628 * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg
Tejun Heo16b3de62013-01-09 08:05:12 -0800629 *
Tejun Heof12c74c2015-08-18 14:55:23 -0700630 * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its
631 * online descendants and their aux counts. The caller must be holding the
632 * queue lock for online tests.
633 *
634 * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it
635 * is at @off bytes into @blkg's blkg_policy_data of the policy.
Tejun Heo16b3de62013-01-09 08:05:12 -0800636 */
Tejun Heof12c74c2015-08-18 14:55:23 -0700637struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
638 struct blkcg_policy *pol, int off)
Tejun Heo16b3de62013-01-09 08:05:12 -0800639{
Tejun Heo16b3de62013-01-09 08:05:12 -0800640 struct blkcg_gq *pos_blkg;
Tejun Heo492eb212013-08-08 20:11:25 -0400641 struct cgroup_subsys_state *pos_css;
Tejun Heobd8815a2013-08-08 20:11:27 -0400642 struct blkg_rwstat sum = { };
Tejun Heo16b3de62013-01-09 08:05:12 -0800643 int i;
644
Tejun Heof12c74c2015-08-18 14:55:23 -0700645 lockdep_assert_held(blkg->q->queue_lock);
Tejun Heo16b3de62013-01-09 08:05:12 -0800646
Tejun Heo16b3de62013-01-09 08:05:12 -0800647 rcu_read_lock();
Tejun Heof12c74c2015-08-18 14:55:23 -0700648 blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
649 struct blkg_rwstat *rwstat, tmp;
Tejun Heo16b3de62013-01-09 08:05:12 -0800650
651 if (!pos_blkg->online)
652 continue;
653
Tejun Heof12c74c2015-08-18 14:55:23 -0700654 if (pol)
655 rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off;
656 else
657 rwstat = (void *)pos_blkg + off;
658
Tejun Heo16b3de62013-01-09 08:05:12 -0800659 tmp = blkg_rwstat_read(rwstat);
660
661 for (i = 0; i < BLKG_RWSTAT_NR; i++)
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700662 atomic64_add(atomic64_read(&tmp.aux_cnt[i]) +
663 atomic64_read(&rwstat->aux_cnt[i]),
664 &sum.aux_cnt[i]);
Tejun Heo16b3de62013-01-09 08:05:12 -0800665 }
666 rcu_read_unlock();
667
668 return sum;
669}
670EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
671
672/**
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700673 * blkg_conf_prep - parse and prepare for per-blkg config update
674 * @blkcg: target block cgroup
Tejun Heoda8b0662012-04-13 13:11:29 -0700675 * @pol: target policy
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700676 * @input: input string
677 * @ctx: blkg_conf_ctx to be filled
678 *
679 * Parse per-blkg config update from @input and initialize @ctx with the
680 * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
Tejun Heoda8b0662012-04-13 13:11:29 -0700681 * value. This function returns with RCU read lock and queue lock held and
682 * must be paired with blkg_conf_finish().
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700683 */
Tejun Heo3c798392012-04-16 13:57:25 -0700684int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
685 const char *input, struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700686 __acquires(rcu) __acquires(disk->queue->queue_lock)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800687{
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700688 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700689 struct blkcg_gq *blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700690 unsigned int major, minor;
691 unsigned long long v;
692 int part, ret;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800693
Tejun Heo726fa692012-04-01 14:38:43 -0700694 if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
695 return -EINVAL;
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700696
Tejun Heo726fa692012-04-01 14:38:43 -0700697 disk = get_gendisk(MKDEV(major, minor), &part);
Tejun Heo5f6c2d22015-07-22 18:05:53 -0400698 if (!disk)
Tejun Heo726fa692012-04-01 14:38:43 -0700699 return -EINVAL;
Tejun Heo5f6c2d22015-07-22 18:05:53 -0400700 if (part) {
701 put_disk(disk);
702 return -EINVAL;
703 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800704
705 rcu_read_lock();
Tejun Heo4bfd4822012-03-05 13:15:08 -0800706 spin_lock_irq(disk->queue->queue_lock);
Tejun Heoda8b0662012-04-13 13:11:29 -0700707
Tejun Heoa2b16932012-04-13 13:11:33 -0700708 if (blkcg_policy_enabled(disk->queue, pol))
Tejun Heo3c96cb32012-04-13 13:11:34 -0700709 blkg = blkg_lookup_create(blkcg, disk->queue);
Tejun Heoa2b16932012-04-13 13:11:33 -0700710 else
711 blkg = ERR_PTR(-EINVAL);
Tejun Heoe56da7e2012-03-05 13:15:07 -0800712
Tejun Heo4bfd4822012-03-05 13:15:08 -0800713 if (IS_ERR(blkg)) {
714 ret = PTR_ERR(blkg);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700715 rcu_read_unlock();
Tejun Heoda8b0662012-04-13 13:11:29 -0700716 spin_unlock_irq(disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700717 put_disk(disk);
718 /*
719 * If queue was bypassing, we should retry. Do so after a
720 * short msleep(). It isn't strictly necessary but queue
721 * can be bypassing for some time and it's always nice to
722 * avoid busy looping.
723 */
724 if (ret == -EBUSY) {
725 msleep(10);
726 ret = restart_syscall();
Vivek Goyal7702e8f2010-09-15 17:06:36 -0400727 }
Tejun Heo726fa692012-04-01 14:38:43 -0700728 return ret;
Vivek Goyal062a6442010-09-15 17:06:33 -0400729 }
Tejun Heoe56da7e2012-03-05 13:15:07 -0800730
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700731 ctx->disk = disk;
732 ctx->blkg = blkg;
Tejun Heo726fa692012-04-01 14:38:43 -0700733 ctx->v = v;
734 return 0;
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800735}
Tejun Heo829fdb52012-04-01 14:38:43 -0700736EXPORT_SYMBOL_GPL(blkg_conf_prep);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800737
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700738/**
739 * blkg_conf_finish - finish up per-blkg config update
740 * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
741 *
742 * Finish up after per-blkg config update. This function must be paired
743 * with blkg_conf_prep().
744 */
Tejun Heo829fdb52012-04-01 14:38:43 -0700745void blkg_conf_finish(struct blkg_conf_ctx *ctx)
Tejun Heoda8b0662012-04-13 13:11:29 -0700746 __releases(ctx->disk->queue->queue_lock) __releases(rcu)
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800747{
Tejun Heoda8b0662012-04-13 13:11:29 -0700748 spin_unlock_irq(ctx->disk->queue->queue_lock);
Tejun Heo3a8b31d2012-04-01 14:38:43 -0700749 rcu_read_unlock();
750 put_disk(ctx->disk);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800751}
Tejun Heo829fdb52012-04-01 14:38:43 -0700752EXPORT_SYMBOL_GPL(blkg_conf_finish);
Gui Jianfeng34d0f172010-04-13 16:05:49 +0800753
Tejun Heo3c798392012-04-16 13:57:25 -0700754struct cftype blkcg_files[] = {
Vivek Goyal31e4c282009-12-03 12:59:42 -0500755 {
Divyesh Shah84c124d2010-04-09 08:31:19 +0200756 .name = "reset_stats",
Tejun Heo3c798392012-04-16 13:57:25 -0700757 .write_u64 = blkcg_reset_stats,
Vivek Goyal22084192009-12-03 12:59:49 -0500758 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700759 { } /* terminate */
Vivek Goyal31e4c282009-12-03 12:59:42 -0500760};
761
Tejun Heo9f13ef62012-03-05 13:15:21 -0800762/**
Tejun Heo92fb9742012-11-19 08:13:38 -0800763 * blkcg_css_offline - cgroup css_offline callback
Tejun Heoeb954192013-08-08 20:11:23 -0400764 * @css: css of interest
Tejun Heo9f13ef62012-03-05 13:15:21 -0800765 *
Tejun Heoeb954192013-08-08 20:11:23 -0400766 * This function is called when @css is about to go away and responsible
767 * for shooting down all blkgs associated with @css. blkgs should be
Tejun Heo9f13ef62012-03-05 13:15:21 -0800768 * removed while holding both q and blkcg locks. As blkcg lock is nested
769 * inside q lock, this function performs reverse double lock dancing.
770 *
771 * This is the blkcg counterpart of ioc_release_fn().
772 */
Tejun Heoeb954192013-08-08 20:11:23 -0400773static void blkcg_css_offline(struct cgroup_subsys_state *css)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500774{
Tejun Heoeb954192013-08-08 20:11:23 -0400775 struct blkcg *blkcg = css_to_blkcg(css);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500776
Tejun Heo9f13ef62012-03-05 13:15:21 -0800777 spin_lock_irq(&blkcg->lock);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800778
Tejun Heo9f13ef62012-03-05 13:15:21 -0800779 while (!hlist_empty(&blkcg->blkg_list)) {
Tejun Heo3c798392012-04-16 13:57:25 -0700780 struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
781 struct blkcg_gq, blkcg_node);
Tejun Heoc875f4d2012-03-05 13:15:22 -0800782 struct request_queue *q = blkg->q;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500783
Tejun Heo9f13ef62012-03-05 13:15:21 -0800784 if (spin_trylock(q->queue_lock)) {
785 blkg_destroy(blkg);
786 spin_unlock(q->queue_lock);
787 } else {
788 spin_unlock_irq(&blkcg->lock);
Tejun Heo9f13ef62012-03-05 13:15:21 -0800789 cpu_relax();
Dan Carpentera5567932012-03-29 20:57:08 +0200790 spin_lock_irq(&blkcg->lock);
Jens Axboe0f3942a2010-05-03 14:28:55 +0200791 }
Tejun Heo9f13ef62012-03-05 13:15:21 -0800792 }
Jens Axboe0f3942a2010-05-03 14:28:55 +0200793
Tejun Heo9f13ef62012-03-05 13:15:21 -0800794 spin_unlock_irq(&blkcg->lock);
Tejun Heo52ebea72015-05-22 17:13:37 -0400795
796 wb_blkcg_offline(blkcg);
Tejun Heo7ee9c562012-03-05 13:15:11 -0800797}
798
Tejun Heoeb954192013-08-08 20:11:23 -0400799static void blkcg_css_free(struct cgroup_subsys_state *css)
Tejun Heo7ee9c562012-03-05 13:15:11 -0800800{
Tejun Heoeb954192013-08-08 20:11:23 -0400801 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heobc915e62015-08-18 14:55:08 -0700802 int i;
Tejun Heo7ee9c562012-03-05 13:15:11 -0800803
Tejun Heo7876f932015-07-09 16:39:49 -0400804 mutex_lock(&blkcg_pol_mutex);
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700805
Tejun Heo7876f932015-07-09 16:39:49 -0400806 list_del(&blkcg->all_blkcgs_node);
Tejun Heo7876f932015-07-09 16:39:49 -0400807
Tejun Heobc915e62015-08-18 14:55:08 -0700808 for (i = 0; i < BLKCG_MAX_POLS; i++)
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700809 if (blkcg->cpd[i])
810 blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
811
812 mutex_unlock(&blkcg_pol_mutex);
813
Tejun Heobc915e62015-08-18 14:55:08 -0700814 kfree(blkcg);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500815}
816
Tejun Heoeb954192013-08-08 20:11:23 -0400817static struct cgroup_subsys_state *
818blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500819{
Tejun Heo3c798392012-04-16 13:57:25 -0700820 struct blkcg *blkcg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200821 struct cgroup_subsys_state *ret;
822 int i;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500823
Tejun Heo7876f932015-07-09 16:39:49 -0400824 mutex_lock(&blkcg_pol_mutex);
825
Tejun Heoeb954192013-08-08 20:11:23 -0400826 if (!parent_css) {
Tejun Heo3c798392012-04-16 13:57:25 -0700827 blkcg = &blkcg_root;
Tejun Heobc915e62015-08-18 14:55:08 -0700828 } else {
829 blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
830 if (!blkcg) {
831 ret = ERR_PTR(-ENOMEM);
832 goto free_blkcg;
833 }
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200834 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500835
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200836 for (i = 0; i < BLKCG_MAX_POLS ; i++) {
837 struct blkcg_policy *pol = blkcg_policy[i];
838 struct blkcg_policy_data *cpd;
839
840 /*
841 * If the policy hasn't been attached yet, wait for it
842 * to be attached before doing anything else. Otherwise,
843 * check if the policy requires any specific per-cgroup
844 * data: if it does, allocate and initialize it.
845 */
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700846 if (!pol || !pol->cpd_alloc_fn)
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200847 continue;
848
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700849 cpd = pol->cpd_alloc_fn(GFP_KERNEL);
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200850 if (!cpd) {
851 ret = ERR_PTR(-ENOMEM);
852 goto free_pd_blkcg;
853 }
Tejun Heo81437642015-08-18 14:55:15 -0700854 blkcg->cpd[i] = cpd;
855 cpd->blkcg = blkcg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200856 cpd->plid = i;
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700857 if (pol->cpd_init_fn)
858 pol->cpd_init_fn(cpd);
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200859 }
860
Vivek Goyal31e4c282009-12-03 12:59:42 -0500861 spin_lock_init(&blkcg->lock);
Tejun Heod93a11f2015-08-18 14:55:01 -0700862 INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500863 INIT_HLIST_HEAD(&blkcg->blkg_list);
Tejun Heo52ebea72015-05-22 17:13:37 -0400864#ifdef CONFIG_CGROUP_WRITEBACK
865 INIT_LIST_HEAD(&blkcg->cgwb_list);
866#endif
Tejun Heo7876f932015-07-09 16:39:49 -0400867 list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
868
869 mutex_unlock(&blkcg_pol_mutex);
Vivek Goyal31e4c282009-12-03 12:59:42 -0500870 return &blkcg->css;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200871
872free_pd_blkcg:
873 for (i--; i >= 0; i--)
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700874 if (blkcg->cpd[i])
875 blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200876free_blkcg:
877 kfree(blkcg);
Tejun Heo7876f932015-07-09 16:39:49 -0400878 mutex_unlock(&blkcg_pol_mutex);
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200879 return ret;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500880}
881
Tejun Heo5efd6112012-03-05 13:15:12 -0800882/**
883 * blkcg_init_queue - initialize blkcg part of request queue
884 * @q: request_queue to initialize
885 *
886 * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
887 * part of new request_queue @q.
888 *
889 * RETURNS:
890 * 0 on success, -errno on failure.
891 */
892int blkcg_init_queue(struct request_queue *q)
893{
Tejun Heoec13b1d2015-05-22 17:13:19 -0400894 struct blkcg_gq *new_blkg, *blkg;
895 bool preloaded;
896 int ret;
Tejun Heo5efd6112012-03-05 13:15:12 -0800897
Tejun Heoec13b1d2015-05-22 17:13:19 -0400898 new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
899 if (!new_blkg)
900 return -ENOMEM;
901
902 preloaded = !radix_tree_preload(GFP_KERNEL);
903
904 /*
905 * Make sure the root blkg exists and count the existing blkgs. As
906 * @q is bypassing at this point, blkg_lookup_create() can't be
907 * used. Open code insertion.
908 */
909 rcu_read_lock();
910 spin_lock_irq(q->queue_lock);
911 blkg = blkg_create(&blkcg_root, q, new_blkg);
912 spin_unlock_irq(q->queue_lock);
913 rcu_read_unlock();
914
915 if (preloaded)
916 radix_tree_preload_end();
917
918 if (IS_ERR(blkg)) {
Tejun Heo994b7832015-08-18 14:55:07 -0700919 blkg_free(new_blkg);
Tejun Heoec13b1d2015-05-22 17:13:19 -0400920 return PTR_ERR(blkg);
921 }
922
923 q->root_blkg = blkg;
924 q->root_rl.blkg = blkg;
925
926 ret = blk_throtl_init(q);
927 if (ret) {
928 spin_lock_irq(q->queue_lock);
929 blkg_destroy_all(q);
930 spin_unlock_irq(q->queue_lock);
931 }
932 return ret;
Tejun Heo5efd6112012-03-05 13:15:12 -0800933}
934
935/**
936 * blkcg_drain_queue - drain blkcg part of request_queue
937 * @q: request_queue to drain
938 *
939 * Called from blk_drain_queue(). Responsible for draining blkcg part.
940 */
941void blkcg_drain_queue(struct request_queue *q)
942{
943 lockdep_assert_held(q->queue_lock);
944
Tejun Heo0b462c82014-07-05 18:43:21 -0400945 /*
946 * @q could be exiting and already have destroyed all blkgs as
947 * indicated by NULL root_blkg. If so, don't confuse policies.
948 */
949 if (!q->root_blkg)
950 return;
951
Tejun Heo5efd6112012-03-05 13:15:12 -0800952 blk_throtl_drain(q);
953}
954
955/**
956 * blkcg_exit_queue - exit and release blkcg part of request_queue
957 * @q: request_queue being released
958 *
959 * Called from blk_release_queue(). Responsible for exiting blkcg part.
960 */
961void blkcg_exit_queue(struct request_queue *q)
962{
Tejun Heo6d18b002012-04-13 13:11:35 -0700963 spin_lock_irq(q->queue_lock);
Tejun Heo3c96cb32012-04-13 13:11:34 -0700964 blkg_destroy_all(q);
Tejun Heo6d18b002012-04-13 13:11:35 -0700965 spin_unlock_irq(q->queue_lock);
966
Tejun Heo5efd6112012-03-05 13:15:12 -0800967 blk_throtl_exit(q);
968}
969
Vivek Goyal31e4c282009-12-03 12:59:42 -0500970/*
971 * We cannot support shared io contexts, as we have no mean to support
972 * two tasks with the same ioc in two different groups without major rework
973 * of the main cic data structures. For now we allow a task to change
974 * its cgroup only if it's the only owner of its ioc.
975 */
Tejun Heoeb954192013-08-08 20:11:23 -0400976static int blkcg_can_attach(struct cgroup_subsys_state *css,
977 struct cgroup_taskset *tset)
Vivek Goyal31e4c282009-12-03 12:59:42 -0500978{
Tejun Heobb9d97b2011-12-12 18:12:21 -0800979 struct task_struct *task;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500980 struct io_context *ioc;
981 int ret = 0;
982
983 /* task_lock() is needed to avoid races with exit_io_context() */
Tejun Heo924f0d92014-02-13 06:58:41 -0500984 cgroup_taskset_for_each(task, tset) {
Tejun Heobb9d97b2011-12-12 18:12:21 -0800985 task_lock(task);
986 ioc = task->io_context;
987 if (ioc && atomic_read(&ioc->nr_tasks) > 1)
988 ret = -EINVAL;
989 task_unlock(task);
990 if (ret)
991 break;
992 }
Vivek Goyal31e4c282009-12-03 12:59:42 -0500993 return ret;
994}
995
Tejun Heo073219e2014-02-08 10:36:58 -0500996struct cgroup_subsys blkio_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800997 .css_alloc = blkcg_css_alloc,
998 .css_offline = blkcg_css_offline,
999 .css_free = blkcg_css_free,
Tejun Heo3c798392012-04-16 13:57:25 -07001000 .can_attach = blkcg_can_attach,
Tejun Heo55779642014-07-15 11:05:09 -04001001 .legacy_cftypes = blkcg_files,
Tejun Heo1ced9532014-07-08 18:02:57 -04001002#ifdef CONFIG_MEMCG
1003 /*
1004 * This ensures that, if available, memcg is automatically enabled
1005 * together on the default hierarchy so that the owner cgroup can
1006 * be retrieved from writeback pages.
1007 */
1008 .depends_on = 1 << memory_cgrp_id,
1009#endif
Tejun Heo676f7c82012-04-01 12:09:55 -07001010};
Tejun Heo073219e2014-02-08 10:36:58 -05001011EXPORT_SYMBOL_GPL(blkio_cgrp_subsys);
Tejun Heo676f7c82012-04-01 12:09:55 -07001012
Tejun Heo8bd435b2012-04-13 13:11:28 -07001013/**
Tejun Heoa2b16932012-04-13 13:11:33 -07001014 * blkcg_activate_policy - activate a blkcg policy on a request_queue
1015 * @q: request_queue of interest
1016 * @pol: blkcg policy to activate
1017 *
1018 * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
1019 * bypass mode to populate its blkgs with policy_data for @pol.
1020 *
1021 * Activation happens with @q bypassed, so nobody would be accessing blkgs
1022 * from IO path. Update of each blkg is protected by both queue and blkcg
1023 * locks so that holding either lock and testing blkcg_policy_enabled() is
1024 * always enough for dereferencing policy data.
1025 *
1026 * The caller is responsible for synchronizing [de]activations and policy
1027 * [un]registerations. Returns 0 on success, -errno on failure.
1028 */
1029int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -07001030 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -07001031{
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001032 struct blkg_policy_data *pd_prealloc = NULL;
Tejun Heoec13b1d2015-05-22 17:13:19 -04001033 struct blkcg_gq *blkg;
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001034 int ret;
Tejun Heoa2b16932012-04-13 13:11:33 -07001035
1036 if (blkcg_policy_enabled(q, pol))
1037 return 0;
1038
1039 blk_queue_bypass_start(q);
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001040pd_prealloc:
1041 if (!pd_prealloc) {
Tejun Heo001bea72015-08-18 14:55:11 -07001042 pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node);
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001043 if (!pd_prealloc) {
Tejun Heoa2b16932012-04-13 13:11:33 -07001044 ret = -ENOMEM;
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001045 goto out_bypass_end;
Tejun Heoa2b16932012-04-13 13:11:33 -07001046 }
Tejun Heoa2b16932012-04-13 13:11:33 -07001047 }
1048
Tejun Heoa2b16932012-04-13 13:11:33 -07001049 spin_lock_irq(q->queue_lock);
1050
1051 list_for_each_entry(blkg, &q->blkg_list, q_node) {
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001052 struct blkg_policy_data *pd;
Tejun Heoa2b16932012-04-13 13:11:33 -07001053
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001054 if (blkg->pd[pol->plid])
1055 continue;
1056
Tejun Heo001bea72015-08-18 14:55:11 -07001057 pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node);
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001058 if (!pd)
1059 swap(pd, pd_prealloc);
1060 if (!pd) {
1061 spin_unlock_irq(q->queue_lock);
1062 goto pd_prealloc;
1063 }
Tejun Heoa2b16932012-04-13 13:11:33 -07001064
1065 blkg->pd[pol->plid] = pd;
1066 pd->blkg = blkg;
Tejun Heob276a872013-01-09 08:05:12 -08001067 pd->plid = pol->plid;
Tejun Heo3e418712015-08-18 14:55:10 -07001068 if (pol->pd_init_fn)
Tejun Heoa9520cd2015-08-18 14:55:14 -07001069 pol->pd_init_fn(pd);
Tejun Heoa2b16932012-04-13 13:11:33 -07001070 }
1071
1072 __set_bit(pol->plid, q->blkcg_pols);
1073 ret = 0;
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001074
Tejun Heoa2b16932012-04-13 13:11:33 -07001075 spin_unlock_irq(q->queue_lock);
Tejun Heo4c55f4f2015-08-18 14:55:09 -07001076out_bypass_end:
Tejun Heoa2b16932012-04-13 13:11:33 -07001077 blk_queue_bypass_end(q);
Tejun Heo001bea72015-08-18 14:55:11 -07001078 if (pd_prealloc)
1079 pol->pd_free_fn(pd_prealloc);
Tejun Heoa2b16932012-04-13 13:11:33 -07001080 return ret;
1081}
1082EXPORT_SYMBOL_GPL(blkcg_activate_policy);
1083
1084/**
1085 * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
1086 * @q: request_queue of interest
1087 * @pol: blkcg policy to deactivate
1088 *
1089 * Deactivate @pol on @q. Follows the same synchronization rules as
1090 * blkcg_activate_policy().
1091 */
1092void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -07001093 const struct blkcg_policy *pol)
Tejun Heoa2b16932012-04-13 13:11:33 -07001094{
Tejun Heo3c798392012-04-16 13:57:25 -07001095 struct blkcg_gq *blkg;
Tejun Heoa2b16932012-04-13 13:11:33 -07001096
1097 if (!blkcg_policy_enabled(q, pol))
1098 return;
1099
1100 blk_queue_bypass_start(q);
1101 spin_lock_irq(q->queue_lock);
1102
1103 __clear_bit(pol->plid, q->blkcg_pols);
1104
1105 list_for_each_entry(blkg, &q->blkg_list, q_node) {
1106 /* grab blkcg lock too while removing @pd from @blkg */
1107 spin_lock(&blkg->blkcg->lock);
1108
Tejun Heo001bea72015-08-18 14:55:11 -07001109 if (blkg->pd[pol->plid]) {
Tejun Heoa9520cd2015-08-18 14:55:14 -07001110 if (pol->pd_offline_fn)
1111 pol->pd_offline_fn(blkg->pd[pol->plid]);
Tejun Heo001bea72015-08-18 14:55:11 -07001112 pol->pd_free_fn(blkg->pd[pol->plid]);
1113 blkg->pd[pol->plid] = NULL;
1114 }
Tejun Heoa2b16932012-04-13 13:11:33 -07001115
1116 spin_unlock(&blkg->blkcg->lock);
1117 }
1118
1119 spin_unlock_irq(q->queue_lock);
1120 blk_queue_bypass_end(q);
1121}
1122EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
1123
1124/**
Tejun Heo3c798392012-04-16 13:57:25 -07001125 * blkcg_policy_register - register a blkcg policy
1126 * @pol: blkcg policy to register
Tejun Heo8bd435b2012-04-13 13:11:28 -07001127 *
Tejun Heo3c798392012-04-16 13:57:25 -07001128 * Register @pol with blkcg core. Might sleep and @pol may be modified on
1129 * successful registration. Returns 0 on success and -errno on failure.
Tejun Heo8bd435b2012-04-13 13:11:28 -07001130 */
Jens Axboed5bf0292014-06-22 16:31:56 -06001131int blkcg_policy_register(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -05001132{
Tejun Heo06b285b2015-07-09 16:39:50 -04001133 struct blkcg *blkcg;
Tejun Heo8bd435b2012-04-13 13:11:28 -07001134 int i, ret;
Tejun Heoe8989fa2012-03-05 13:15:20 -08001135
Tejun Heo838f13b2015-07-09 16:39:47 -04001136 mutex_lock(&blkcg_pol_register_mutex);
Tejun Heobc0d6502012-04-13 13:11:26 -07001137 mutex_lock(&blkcg_pol_mutex);
1138
Tejun Heo8bd435b2012-04-13 13:11:28 -07001139 /* find an empty slot */
1140 ret = -ENOSPC;
1141 for (i = 0; i < BLKCG_MAX_POLS; i++)
Tejun Heo3c798392012-04-16 13:57:25 -07001142 if (!blkcg_policy[i])
Tejun Heo8bd435b2012-04-13 13:11:28 -07001143 break;
1144 if (i >= BLKCG_MAX_POLS)
Tejun Heo838f13b2015-07-09 16:39:47 -04001145 goto err_unlock;
Tejun Heo035d10b2012-03-05 13:15:04 -08001146
Tejun Heo06b285b2015-07-09 16:39:50 -04001147 /* register @pol */
Tejun Heo3c798392012-04-16 13:57:25 -07001148 pol->plid = i;
Tejun Heo06b285b2015-07-09 16:39:50 -04001149 blkcg_policy[pol->plid] = pol;
1150
1151 /* allocate and install cpd's */
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001152 if (pol->cpd_alloc_fn) {
Tejun Heo06b285b2015-07-09 16:39:50 -04001153 list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1154 struct blkcg_policy_data *cpd;
1155
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001156 cpd = pol->cpd_alloc_fn(GFP_KERNEL);
Tejun Heo06b285b2015-07-09 16:39:50 -04001157 if (!cpd) {
1158 mutex_unlock(&blkcg_pol_mutex);
1159 goto err_free_cpds;
1160 }
1161
Tejun Heo81437642015-08-18 14:55:15 -07001162 blkcg->cpd[pol->plid] = cpd;
1163 cpd->blkcg = blkcg;
Tejun Heo06b285b2015-07-09 16:39:50 -04001164 cpd->plid = pol->plid;
Tejun Heo81437642015-08-18 14:55:15 -07001165 pol->cpd_init_fn(cpd);
Tejun Heo06b285b2015-07-09 16:39:50 -04001166 }
1167 }
1168
Tejun Heo838f13b2015-07-09 16:39:47 -04001169 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -07001170
Tejun Heo8bd435b2012-04-13 13:11:28 -07001171 /* everything is in place, add intf files for the new policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001172 if (pol->cftypes)
Tejun Heo2cf669a2014-07-15 11:05:09 -04001173 WARN_ON(cgroup_add_legacy_cftypes(&blkio_cgrp_subsys,
1174 pol->cftypes));
Tejun Heo838f13b2015-07-09 16:39:47 -04001175 mutex_unlock(&blkcg_pol_register_mutex);
1176 return 0;
1177
Tejun Heo06b285b2015-07-09 16:39:50 -04001178err_free_cpds:
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001179 if (pol->cpd_alloc_fn) {
Tejun Heo06b285b2015-07-09 16:39:50 -04001180 list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001181 if (blkcg->cpd[pol->plid]) {
1182 pol->cpd_free_fn(blkcg->cpd[pol->plid]);
1183 blkcg->cpd[pol->plid] = NULL;
1184 }
Tejun Heo06b285b2015-07-09 16:39:50 -04001185 }
1186 }
1187 blkcg_policy[pol->plid] = NULL;
Tejun Heo838f13b2015-07-09 16:39:47 -04001188err_unlock:
Tejun Heobc0d6502012-04-13 13:11:26 -07001189 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo838f13b2015-07-09 16:39:47 -04001190 mutex_unlock(&blkcg_pol_register_mutex);
Tejun Heo8bd435b2012-04-13 13:11:28 -07001191 return ret;
Vivek Goyal3e252062009-12-04 10:36:42 -05001192}
Tejun Heo3c798392012-04-16 13:57:25 -07001193EXPORT_SYMBOL_GPL(blkcg_policy_register);
Vivek Goyal3e252062009-12-04 10:36:42 -05001194
Tejun Heo8bd435b2012-04-13 13:11:28 -07001195/**
Tejun Heo3c798392012-04-16 13:57:25 -07001196 * blkcg_policy_unregister - unregister a blkcg policy
1197 * @pol: blkcg policy to unregister
Tejun Heo8bd435b2012-04-13 13:11:28 -07001198 *
Tejun Heo3c798392012-04-16 13:57:25 -07001199 * Undo blkcg_policy_register(@pol). Might sleep.
Tejun Heo8bd435b2012-04-13 13:11:28 -07001200 */
Tejun Heo3c798392012-04-16 13:57:25 -07001201void blkcg_policy_unregister(struct blkcg_policy *pol)
Vivek Goyal3e252062009-12-04 10:36:42 -05001202{
Tejun Heo06b285b2015-07-09 16:39:50 -04001203 struct blkcg *blkcg;
1204
Tejun Heo838f13b2015-07-09 16:39:47 -04001205 mutex_lock(&blkcg_pol_register_mutex);
Tejun Heobc0d6502012-04-13 13:11:26 -07001206
Tejun Heo3c798392012-04-16 13:57:25 -07001207 if (WARN_ON(blkcg_policy[pol->plid] != pol))
Tejun Heo8bd435b2012-04-13 13:11:28 -07001208 goto out_unlock;
1209
1210 /* kill the intf files first */
Tejun Heo3c798392012-04-16 13:57:25 -07001211 if (pol->cftypes)
Tejun Heo2bb566c2013-08-08 20:11:23 -04001212 cgroup_rm_cftypes(pol->cftypes);
Tejun Heo44ea53d2012-04-01 14:38:43 -07001213
Tejun Heo06b285b2015-07-09 16:39:50 -04001214 /* remove cpds and unregister */
Tejun Heo838f13b2015-07-09 16:39:47 -04001215 mutex_lock(&blkcg_pol_mutex);
Tejun Heo06b285b2015-07-09 16:39:50 -04001216
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001217 if (pol->cpd_alloc_fn) {
Tejun Heo06b285b2015-07-09 16:39:50 -04001218 list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001219 if (blkcg->cpd[pol->plid]) {
1220 pol->cpd_free_fn(blkcg->cpd[pol->plid]);
1221 blkcg->cpd[pol->plid] = NULL;
1222 }
Tejun Heo06b285b2015-07-09 16:39:50 -04001223 }
1224 }
Tejun Heo3c798392012-04-16 13:57:25 -07001225 blkcg_policy[pol->plid] = NULL;
Tejun Heo06b285b2015-07-09 16:39:50 -04001226
Tejun Heobc0d6502012-04-13 13:11:26 -07001227 mutex_unlock(&blkcg_pol_mutex);
Tejun Heo838f13b2015-07-09 16:39:47 -04001228out_unlock:
1229 mutex_unlock(&blkcg_pol_register_mutex);
Vivek Goyal3e252062009-12-04 10:36:42 -05001230}
Tejun Heo3c798392012-04-16 13:57:25 -07001231EXPORT_SYMBOL_GPL(blkcg_policy_unregister);