1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Common Block IO controller cgroup interface
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Based on ideas and code from CFQ, CFS and BFQ:
6*4882a593Smuzhiyun * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
9*4882a593Smuzhiyun * Paolo Valente <paolo.valente@unimore.it>
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
12*4882a593Smuzhiyun * Nauman Rafique <nauman@google.com>
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * For policy-specific per-blkcg data:
15*4882a593Smuzhiyun * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
16*4882a593Smuzhiyun * Arianna Avanzini <avanzini.arianna@gmail.com>
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun #include <linux/ioprio.h>
19*4882a593Smuzhiyun #include <linux/kdev_t.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/sched/signal.h>
22*4882a593Smuzhiyun #include <linux/err.h>
23*4882a593Smuzhiyun #include <linux/blkdev.h>
24*4882a593Smuzhiyun #include <linux/backing-dev.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun #include <linux/genhd.h>
27*4882a593Smuzhiyun #include <linux/delay.h>
28*4882a593Smuzhiyun #include <linux/atomic.h>
29*4882a593Smuzhiyun #include <linux/ctype.h>
30*4882a593Smuzhiyun #include <linux/blk-cgroup.h>
31*4882a593Smuzhiyun #include <linux/tracehook.h>
32*4882a593Smuzhiyun #include <linux/psi.h>
33*4882a593Smuzhiyun #include "blk.h"
34*4882a593Smuzhiyun #include "blk-ioprio.h"
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define MAX_KEY_LEN 100
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
40*4882a593Smuzhiyun * blkcg_pol_register_mutex nests outside of it and synchronizes entire
41*4882a593Smuzhiyun * policy [un]register operations including cgroup file additions /
42*4882a593Smuzhiyun * removals. Putting cgroup file registration outside blkcg_pol_mutex
43*4882a593Smuzhiyun * allows grabbing it from cgroup callbacks.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun static DEFINE_MUTEX(blkcg_pol_register_mutex);
46*4882a593Smuzhiyun static DEFINE_MUTEX(blkcg_pol_mutex);
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun struct blkcg blkcg_root;
49*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_root);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
52*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_root_css);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun bool blkcg_debug_stats = false;
59*4882a593Smuzhiyun static struct workqueue_struct *blkcg_punt_bio_wq;
60*4882a593Smuzhiyun
blkcg_policy_enabled(struct request_queue * q,const struct blkcg_policy * pol)61*4882a593Smuzhiyun static bool blkcg_policy_enabled(struct request_queue *q,
62*4882a593Smuzhiyun const struct blkcg_policy *pol)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun return pol && test_bit(pol->plid, q->blkcg_pols);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun * blkg_free - free a blkg
69*4882a593Smuzhiyun * @blkg: blkg to free
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * Free @blkg which may be partially allocated.
72*4882a593Smuzhiyun */
blkg_free(struct blkcg_gq * blkg)73*4882a593Smuzhiyun static void blkg_free(struct blkcg_gq *blkg)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun int i;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (!blkg)
78*4882a593Smuzhiyun return;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++)
81*4882a593Smuzhiyun if (blkg->pd[i])
82*4882a593Smuzhiyun blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun free_percpu(blkg->iostat_cpu);
85*4882a593Smuzhiyun percpu_ref_exit(&blkg->refcnt);
86*4882a593Smuzhiyun kfree(blkg);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
__blkg_release(struct rcu_head * rcu)89*4882a593Smuzhiyun static void __blkg_release(struct rcu_head *rcu)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun WARN_ON(!bio_list_empty(&blkg->async_bios));
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* release the blkcg and parent blkg refs this blkg has been holding */
96*4882a593Smuzhiyun css_put(&blkg->blkcg->css);
97*4882a593Smuzhiyun if (blkg->parent)
98*4882a593Smuzhiyun blkg_put(blkg->parent);
99*4882a593Smuzhiyun blkg_free(blkg);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * A group is RCU protected, but having an rcu lock does not mean that one
104*4882a593Smuzhiyun * can access all the fields of blkg and assume these are valid. For
105*4882a593Smuzhiyun * example, don't try to follow throtl_data and request queue links.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Having a reference to blkg under an rcu allows accesses to only values
108*4882a593Smuzhiyun * local to groups like group stats and group rate limits.
109*4882a593Smuzhiyun */
blkg_release(struct percpu_ref * ref)110*4882a593Smuzhiyun static void blkg_release(struct percpu_ref *ref)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun struct blkcg_gq *blkg = container_of(ref, struct blkcg_gq, refcnt);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun call_rcu(&blkg->rcu_head, __blkg_release);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
blkg_async_bio_workfn(struct work_struct * work)117*4882a593Smuzhiyun static void blkg_async_bio_workfn(struct work_struct *work)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
120*4882a593Smuzhiyun async_bio_work);
121*4882a593Smuzhiyun struct bio_list bios = BIO_EMPTY_LIST;
122*4882a593Smuzhiyun struct bio *bio;
123*4882a593Smuzhiyun struct blk_plug plug;
124*4882a593Smuzhiyun bool need_plug = false;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* as long as there are pending bios, @blkg can't go away */
127*4882a593Smuzhiyun spin_lock_bh(&blkg->async_bio_lock);
128*4882a593Smuzhiyun bio_list_merge(&bios, &blkg->async_bios);
129*4882a593Smuzhiyun bio_list_init(&blkg->async_bios);
130*4882a593Smuzhiyun spin_unlock_bh(&blkg->async_bio_lock);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* start plug only when bio_list contains at least 2 bios */
133*4882a593Smuzhiyun if (bios.head && bios.head->bi_next) {
134*4882a593Smuzhiyun need_plug = true;
135*4882a593Smuzhiyun blk_start_plug(&plug);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun while ((bio = bio_list_pop(&bios)))
138*4882a593Smuzhiyun submit_bio(bio);
139*4882a593Smuzhiyun if (need_plug)
140*4882a593Smuzhiyun blk_finish_plug(&plug);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun * blkg_alloc - allocate a blkg
145*4882a593Smuzhiyun * @blkcg: block cgroup the new blkg is associated with
146*4882a593Smuzhiyun * @q: request_queue the new blkg is associated with
147*4882a593Smuzhiyun * @gfp_mask: allocation mask to use
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * Allocate a new blkg assocating @blkcg and @q.
150*4882a593Smuzhiyun */
blkg_alloc(struct blkcg * blkcg,struct request_queue * q,gfp_t gfp_mask)151*4882a593Smuzhiyun static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
152*4882a593Smuzhiyun gfp_t gfp_mask)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun struct blkcg_gq *blkg;
155*4882a593Smuzhiyun int i, cpu;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /* alloc and init base part */
158*4882a593Smuzhiyun blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
159*4882a593Smuzhiyun if (!blkg)
160*4882a593Smuzhiyun return NULL;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (percpu_ref_init(&blkg->refcnt, blkg_release, 0, gfp_mask))
163*4882a593Smuzhiyun goto err_free;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun blkg->iostat_cpu = alloc_percpu_gfp(struct blkg_iostat_set, gfp_mask);
166*4882a593Smuzhiyun if (!blkg->iostat_cpu)
167*4882a593Smuzhiyun goto err_free;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun blkg->q = q;
170*4882a593Smuzhiyun INIT_LIST_HEAD(&blkg->q_node);
171*4882a593Smuzhiyun spin_lock_init(&blkg->async_bio_lock);
172*4882a593Smuzhiyun bio_list_init(&blkg->async_bios);
173*4882a593Smuzhiyun INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn);
174*4882a593Smuzhiyun blkg->blkcg = blkcg;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun u64_stats_init(&blkg->iostat.sync);
177*4882a593Smuzhiyun for_each_possible_cpu(cpu)
178*4882a593Smuzhiyun u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
181*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
182*4882a593Smuzhiyun struct blkg_policy_data *pd;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun if (!blkcg_policy_enabled(q, pol))
185*4882a593Smuzhiyun continue;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* alloc per-policy data and attach it to blkg */
188*4882a593Smuzhiyun pd = pol->pd_alloc_fn(gfp_mask, q, blkcg);
189*4882a593Smuzhiyun if (!pd)
190*4882a593Smuzhiyun goto err_free;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun blkg->pd[i] = pd;
193*4882a593Smuzhiyun pd->blkg = blkg;
194*4882a593Smuzhiyun pd->plid = i;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun return blkg;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun err_free:
200*4882a593Smuzhiyun blkg_free(blkg);
201*4882a593Smuzhiyun return NULL;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
blkg_lookup_slowpath(struct blkcg * blkcg,struct request_queue * q,bool update_hint)204*4882a593Smuzhiyun struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
205*4882a593Smuzhiyun struct request_queue *q, bool update_hint)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct blkcg_gq *blkg;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun * Hint didn't match. Look up from the radix tree. Note that the
211*4882a593Smuzhiyun * hint can only be updated under queue_lock as otherwise @blkg
212*4882a593Smuzhiyun * could have already been removed from blkg_tree. The caller is
213*4882a593Smuzhiyun * responsible for grabbing queue_lock if @update_hint.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
216*4882a593Smuzhiyun if (blkg && blkg->q == q) {
217*4882a593Smuzhiyun if (update_hint) {
218*4882a593Smuzhiyun lockdep_assert_held(&q->queue_lock);
219*4882a593Smuzhiyun rcu_assign_pointer(blkcg->blkg_hint, blkg);
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun return blkg;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun return NULL;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_lookup_slowpath);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * If @new_blkg is %NULL, this function tries to allocate a new one as
230*4882a593Smuzhiyun * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
231*4882a593Smuzhiyun */
blkg_create(struct blkcg * blkcg,struct request_queue * q,struct blkcg_gq * new_blkg)232*4882a593Smuzhiyun static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
233*4882a593Smuzhiyun struct request_queue *q,
234*4882a593Smuzhiyun struct blkcg_gq *new_blkg)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun struct blkcg_gq *blkg;
237*4882a593Smuzhiyun int i, ret;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun WARN_ON_ONCE(!rcu_read_lock_held());
240*4882a593Smuzhiyun lockdep_assert_held(&q->queue_lock);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* request_queue is dying, do not create/recreate a blkg */
243*4882a593Smuzhiyun if (blk_queue_dying(q)) {
244*4882a593Smuzhiyun ret = -ENODEV;
245*4882a593Smuzhiyun goto err_free_blkg;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* blkg holds a reference to blkcg */
249*4882a593Smuzhiyun if (!css_tryget_online(&blkcg->css)) {
250*4882a593Smuzhiyun ret = -ENODEV;
251*4882a593Smuzhiyun goto err_free_blkg;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* allocate */
255*4882a593Smuzhiyun if (!new_blkg) {
256*4882a593Smuzhiyun new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT | __GFP_NOWARN);
257*4882a593Smuzhiyun if (unlikely(!new_blkg)) {
258*4882a593Smuzhiyun ret = -ENOMEM;
259*4882a593Smuzhiyun goto err_put_css;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun blkg = new_blkg;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /* link parent */
265*4882a593Smuzhiyun if (blkcg_parent(blkcg)) {
266*4882a593Smuzhiyun blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
267*4882a593Smuzhiyun if (WARN_ON_ONCE(!blkg->parent)) {
268*4882a593Smuzhiyun ret = -ENODEV;
269*4882a593Smuzhiyun goto err_put_css;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun blkg_get(blkg->parent);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun /* invoke per-policy init */
275*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
276*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (blkg->pd[i] && pol->pd_init_fn)
279*4882a593Smuzhiyun pol->pd_init_fn(blkg->pd[i]);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* insert */
283*4882a593Smuzhiyun spin_lock(&blkcg->lock);
284*4882a593Smuzhiyun ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
285*4882a593Smuzhiyun if (likely(!ret)) {
286*4882a593Smuzhiyun hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
287*4882a593Smuzhiyun list_add(&blkg->q_node, &q->blkg_list);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
290*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun if (blkg->pd[i] && pol->pd_online_fn)
293*4882a593Smuzhiyun pol->pd_online_fn(blkg->pd[i]);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun blkg->online = true;
297*4882a593Smuzhiyun spin_unlock(&blkcg->lock);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (!ret)
300*4882a593Smuzhiyun return blkg;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /* @blkg failed fully initialized, use the usual release path */
303*4882a593Smuzhiyun blkg_put(blkg);
304*4882a593Smuzhiyun return ERR_PTR(ret);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun err_put_css:
307*4882a593Smuzhiyun css_put(&blkcg->css);
308*4882a593Smuzhiyun err_free_blkg:
309*4882a593Smuzhiyun blkg_free(new_blkg);
310*4882a593Smuzhiyun return ERR_PTR(ret);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun /**
314*4882a593Smuzhiyun * blkg_lookup_create - lookup blkg, try to create one if not there
315*4882a593Smuzhiyun * @blkcg: blkcg of interest
316*4882a593Smuzhiyun * @q: request_queue of interest
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
319*4882a593Smuzhiyun * create one. blkg creation is performed recursively from blkcg_root such
320*4882a593Smuzhiyun * that all non-root blkg's have access to the parent blkg. This function
321*4882a593Smuzhiyun * should be called under RCU read lock and takes @q->queue_lock.
322*4882a593Smuzhiyun *
323*4882a593Smuzhiyun * Returns the blkg or the closest blkg if blkg_create() fails as it walks
324*4882a593Smuzhiyun * down from root.
325*4882a593Smuzhiyun */
blkg_lookup_create(struct blkcg * blkcg,struct request_queue * q)326*4882a593Smuzhiyun static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
327*4882a593Smuzhiyun struct request_queue *q)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun struct blkcg_gq *blkg;
330*4882a593Smuzhiyun unsigned long flags;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun WARN_ON_ONCE(!rcu_read_lock_held());
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun blkg = blkg_lookup(blkcg, q);
335*4882a593Smuzhiyun if (blkg)
336*4882a593Smuzhiyun return blkg;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun spin_lock_irqsave(&q->queue_lock, flags);
339*4882a593Smuzhiyun blkg = __blkg_lookup(blkcg, q, true);
340*4882a593Smuzhiyun if (blkg)
341*4882a593Smuzhiyun goto found;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun * Create blkgs walking down from blkcg_root to @blkcg, so that all
345*4882a593Smuzhiyun * non-root blkgs have access to their parents. Returns the closest
346*4882a593Smuzhiyun * blkg to the intended blkg should blkg_create() fail.
347*4882a593Smuzhiyun */
348*4882a593Smuzhiyun while (true) {
349*4882a593Smuzhiyun struct blkcg *pos = blkcg;
350*4882a593Smuzhiyun struct blkcg *parent = blkcg_parent(blkcg);
351*4882a593Smuzhiyun struct blkcg_gq *ret_blkg = q->root_blkg;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun while (parent) {
354*4882a593Smuzhiyun blkg = __blkg_lookup(parent, q, false);
355*4882a593Smuzhiyun if (blkg) {
356*4882a593Smuzhiyun /* remember closest blkg */
357*4882a593Smuzhiyun ret_blkg = blkg;
358*4882a593Smuzhiyun break;
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun pos = parent;
361*4882a593Smuzhiyun parent = blkcg_parent(parent);
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun blkg = blkg_create(pos, q, NULL);
365*4882a593Smuzhiyun if (IS_ERR(blkg)) {
366*4882a593Smuzhiyun blkg = ret_blkg;
367*4882a593Smuzhiyun break;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun if (pos == blkcg)
370*4882a593Smuzhiyun break;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun found:
374*4882a593Smuzhiyun spin_unlock_irqrestore(&q->queue_lock, flags);
375*4882a593Smuzhiyun return blkg;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
blkg_destroy(struct blkcg_gq * blkg)378*4882a593Smuzhiyun static void blkg_destroy(struct blkcg_gq *blkg)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun struct blkcg *blkcg = blkg->blkcg;
381*4882a593Smuzhiyun int i;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun lockdep_assert_held(&blkg->q->queue_lock);
384*4882a593Smuzhiyun lockdep_assert_held(&blkcg->lock);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* Something wrong if we are trying to remove same group twice */
387*4882a593Smuzhiyun WARN_ON_ONCE(list_empty(&blkg->q_node));
388*4882a593Smuzhiyun WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
391*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun if (blkg->pd[i] && pol->pd_offline_fn)
394*4882a593Smuzhiyun pol->pd_offline_fn(blkg->pd[i]);
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun blkg->online = false;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
400*4882a593Smuzhiyun list_del_init(&blkg->q_node);
401*4882a593Smuzhiyun hlist_del_init_rcu(&blkg->blkcg_node);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun /*
404*4882a593Smuzhiyun * Both setting lookup hint to and clearing it from @blkg are done
405*4882a593Smuzhiyun * under queue_lock. If it's not pointing to @blkg now, it never
406*4882a593Smuzhiyun * will. Hint assignment itself can race safely.
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
409*4882a593Smuzhiyun rcu_assign_pointer(blkcg->blkg_hint, NULL);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun /*
412*4882a593Smuzhiyun * Put the reference taken at the time of creation so that when all
413*4882a593Smuzhiyun * queues are gone, group can be destroyed.
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun percpu_ref_kill(&blkg->refcnt);
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /**
419*4882a593Smuzhiyun * blkg_destroy_all - destroy all blkgs associated with a request_queue
420*4882a593Smuzhiyun * @q: request_queue of interest
421*4882a593Smuzhiyun *
422*4882a593Smuzhiyun * Destroy all blkgs associated with @q.
423*4882a593Smuzhiyun */
blkg_destroy_all(struct request_queue * q)424*4882a593Smuzhiyun static void blkg_destroy_all(struct request_queue *q)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun struct blkcg_gq *blkg, *n;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
429*4882a593Smuzhiyun list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
430*4882a593Smuzhiyun struct blkcg *blkcg = blkg->blkcg;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun spin_lock(&blkcg->lock);
433*4882a593Smuzhiyun blkg_destroy(blkg);
434*4882a593Smuzhiyun spin_unlock(&blkcg->lock);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun q->root_blkg = NULL;
438*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
blkcg_reset_stats(struct cgroup_subsys_state * css,struct cftype * cftype,u64 val)441*4882a593Smuzhiyun static int blkcg_reset_stats(struct cgroup_subsys_state *css,
442*4882a593Smuzhiyun struct cftype *cftype, u64 val)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun struct blkcg *blkcg = css_to_blkcg(css);
445*4882a593Smuzhiyun struct blkcg_gq *blkg;
446*4882a593Smuzhiyun int i, cpu;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun mutex_lock(&blkcg_pol_mutex);
449*4882a593Smuzhiyun spin_lock_irq(&blkcg->lock);
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /*
452*4882a593Smuzhiyun * Note that stat reset is racy - it doesn't synchronize against
453*4882a593Smuzhiyun * stat updates. This is a debug feature which shouldn't exist
454*4882a593Smuzhiyun * anyway. If you get hit by a race, retry.
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
457*4882a593Smuzhiyun for_each_possible_cpu(cpu) {
458*4882a593Smuzhiyun struct blkg_iostat_set *bis =
459*4882a593Smuzhiyun per_cpu_ptr(blkg->iostat_cpu, cpu);
460*4882a593Smuzhiyun memset(bis, 0, sizeof(*bis));
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun memset(&blkg->iostat, 0, sizeof(blkg->iostat));
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
465*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (blkg->pd[i] && pol->pd_reset_stats_fn)
468*4882a593Smuzhiyun pol->pd_reset_stats_fn(blkg->pd[i]);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun spin_unlock_irq(&blkcg->lock);
473*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
474*4882a593Smuzhiyun return 0;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
blkg_dev_name(struct blkcg_gq * blkg)477*4882a593Smuzhiyun const char *blkg_dev_name(struct blkcg_gq *blkg)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun /* some drivers (floppy) instantiate a queue w/o disk registered */
480*4882a593Smuzhiyun if (blkg->q->backing_dev_info->dev)
481*4882a593Smuzhiyun return bdi_dev_name(blkg->q->backing_dev_info);
482*4882a593Smuzhiyun return NULL;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /**
486*4882a593Smuzhiyun * blkcg_print_blkgs - helper for printing per-blkg data
487*4882a593Smuzhiyun * @sf: seq_file to print to
488*4882a593Smuzhiyun * @blkcg: blkcg of interest
489*4882a593Smuzhiyun * @prfill: fill function to print out a blkg
490*4882a593Smuzhiyun * @pol: policy in question
491*4882a593Smuzhiyun * @data: data to be passed to @prfill
492*4882a593Smuzhiyun * @show_total: to print out sum of prfill return values or not
493*4882a593Smuzhiyun *
494*4882a593Smuzhiyun * This function invokes @prfill on each blkg of @blkcg if pd for the
495*4882a593Smuzhiyun * policy specified by @pol exists. @prfill is invoked with @sf, the
496*4882a593Smuzhiyun * policy data and @data and the matching queue lock held. If @show_total
497*4882a593Smuzhiyun * is %true, the sum of the return values from @prfill is printed with
498*4882a593Smuzhiyun * "Total" label at the end.
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * This is to be used to construct print functions for
501*4882a593Smuzhiyun * cftype->read_seq_string method.
502*4882a593Smuzhiyun */
blkcg_print_blkgs(struct seq_file * sf,struct blkcg * blkcg,u64 (* prfill)(struct seq_file *,struct blkg_policy_data *,int),const struct blkcg_policy * pol,int data,bool show_total)503*4882a593Smuzhiyun void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
504*4882a593Smuzhiyun u64 (*prfill)(struct seq_file *,
505*4882a593Smuzhiyun struct blkg_policy_data *, int),
506*4882a593Smuzhiyun const struct blkcg_policy *pol, int data,
507*4882a593Smuzhiyun bool show_total)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun struct blkcg_gq *blkg;
510*4882a593Smuzhiyun u64 total = 0;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun rcu_read_lock();
513*4882a593Smuzhiyun hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
514*4882a593Smuzhiyun spin_lock_irq(&blkg->q->queue_lock);
515*4882a593Smuzhiyun if (blkcg_policy_enabled(blkg->q, pol))
516*4882a593Smuzhiyun total += prfill(sf, blkg->pd[pol->plid], data);
517*4882a593Smuzhiyun spin_unlock_irq(&blkg->q->queue_lock);
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun rcu_read_unlock();
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun if (show_total)
522*4882a593Smuzhiyun seq_printf(sf, "Total %llu\n", (unsigned long long)total);
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /**
527*4882a593Smuzhiyun * __blkg_prfill_u64 - prfill helper for a single u64 value
528*4882a593Smuzhiyun * @sf: seq_file to print to
529*4882a593Smuzhiyun * @pd: policy private data of interest
530*4882a593Smuzhiyun * @v: value to print
531*4882a593Smuzhiyun *
532*4882a593Smuzhiyun * Print @v to @sf for the device assocaited with @pd.
533*4882a593Smuzhiyun */
__blkg_prfill_u64(struct seq_file * sf,struct blkg_policy_data * pd,u64 v)534*4882a593Smuzhiyun u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun const char *dname = blkg_dev_name(pd->blkg);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (!dname)
539*4882a593Smuzhiyun return 0;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
542*4882a593Smuzhiyun return v;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun /* Performs queue bypass and policy enabled checks then looks up blkg. */
blkg_lookup_check(struct blkcg * blkcg,const struct blkcg_policy * pol,struct request_queue * q)547*4882a593Smuzhiyun static struct blkcg_gq *blkg_lookup_check(struct blkcg *blkcg,
548*4882a593Smuzhiyun const struct blkcg_policy *pol,
549*4882a593Smuzhiyun struct request_queue *q)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun WARN_ON_ONCE(!rcu_read_lock_held());
552*4882a593Smuzhiyun lockdep_assert_held(&q->queue_lock);
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun if (!blkcg_policy_enabled(q, pol))
555*4882a593Smuzhiyun return ERR_PTR(-EOPNOTSUPP);
556*4882a593Smuzhiyun return __blkg_lookup(blkcg, q, true /* update_hint */);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /**
560*4882a593Smuzhiyun * blkg_conf_prep - parse and prepare for per-blkg config update
561*4882a593Smuzhiyun * @inputp: input string pointer
562*4882a593Smuzhiyun *
563*4882a593Smuzhiyun * Parse the device node prefix part, MAJ:MIN, of per-blkg config update
564*4882a593Smuzhiyun * from @input and get and return the matching gendisk. *@inputp is
565*4882a593Smuzhiyun * updated to point past the device node prefix. Returns an ERR_PTR()
566*4882a593Smuzhiyun * value on error.
567*4882a593Smuzhiyun *
568*4882a593Smuzhiyun * Use this function iff blkg_conf_prep() can't be used for some reason.
569*4882a593Smuzhiyun */
blkcg_conf_get_disk(char ** inputp)570*4882a593Smuzhiyun struct gendisk *blkcg_conf_get_disk(char **inputp)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun char *input = *inputp;
573*4882a593Smuzhiyun unsigned int major, minor;
574*4882a593Smuzhiyun struct gendisk *disk;
575*4882a593Smuzhiyun int key_len, part;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
578*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun input += key_len;
581*4882a593Smuzhiyun if (!isspace(*input))
582*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
583*4882a593Smuzhiyun input = skip_spaces(input);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun disk = get_gendisk(MKDEV(major, minor), &part);
586*4882a593Smuzhiyun if (!disk)
587*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
588*4882a593Smuzhiyun if (part) {
589*4882a593Smuzhiyun put_disk_and_module(disk);
590*4882a593Smuzhiyun return ERR_PTR(-ENODEV);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun *inputp = input;
594*4882a593Smuzhiyun return disk;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /**
598*4882a593Smuzhiyun * blkg_conf_prep - parse and prepare for per-blkg config update
599*4882a593Smuzhiyun * @blkcg: target block cgroup
600*4882a593Smuzhiyun * @pol: target policy
601*4882a593Smuzhiyun * @input: input string
602*4882a593Smuzhiyun * @ctx: blkg_conf_ctx to be filled
603*4882a593Smuzhiyun *
604*4882a593Smuzhiyun * Parse per-blkg config update from @input and initialize @ctx with the
605*4882a593Smuzhiyun * result. @ctx->blkg points to the blkg to be updated and @ctx->body the
606*4882a593Smuzhiyun * part of @input following MAJ:MIN. This function returns with RCU read
607*4882a593Smuzhiyun * lock and queue lock held and must be paired with blkg_conf_finish().
608*4882a593Smuzhiyun */
blkg_conf_prep(struct blkcg * blkcg,const struct blkcg_policy * pol,char * input,struct blkg_conf_ctx * ctx)609*4882a593Smuzhiyun int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
610*4882a593Smuzhiyun char *input, struct blkg_conf_ctx *ctx)
611*4882a593Smuzhiyun __acquires(rcu) __acquires(&disk->queue->queue_lock)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun struct gendisk *disk;
614*4882a593Smuzhiyun struct request_queue *q;
615*4882a593Smuzhiyun struct blkcg_gq *blkg;
616*4882a593Smuzhiyun int ret;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun disk = blkcg_conf_get_disk(&input);
619*4882a593Smuzhiyun if (IS_ERR(disk))
620*4882a593Smuzhiyun return PTR_ERR(disk);
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun q = disk->queue;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun rcu_read_lock();
625*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun blkg = blkg_lookup_check(blkcg, pol, q);
628*4882a593Smuzhiyun if (IS_ERR(blkg)) {
629*4882a593Smuzhiyun ret = PTR_ERR(blkg);
630*4882a593Smuzhiyun goto fail_unlock;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun if (blkg)
634*4882a593Smuzhiyun goto success;
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun /*
637*4882a593Smuzhiyun * Create blkgs walking down from blkcg_root to @blkcg, so that all
638*4882a593Smuzhiyun * non-root blkgs have access to their parents.
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun while (true) {
641*4882a593Smuzhiyun struct blkcg *pos = blkcg;
642*4882a593Smuzhiyun struct blkcg *parent;
643*4882a593Smuzhiyun struct blkcg_gq *new_blkg;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun parent = blkcg_parent(blkcg);
646*4882a593Smuzhiyun while (parent && !__blkg_lookup(parent, q, false)) {
647*4882a593Smuzhiyun pos = parent;
648*4882a593Smuzhiyun parent = blkcg_parent(parent);
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun /* Drop locks to do new blkg allocation with GFP_KERNEL. */
652*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
653*4882a593Smuzhiyun rcu_read_unlock();
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun new_blkg = blkg_alloc(pos, q, GFP_KERNEL);
656*4882a593Smuzhiyun if (unlikely(!new_blkg)) {
657*4882a593Smuzhiyun ret = -ENOMEM;
658*4882a593Smuzhiyun goto fail;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun if (radix_tree_preload(GFP_KERNEL)) {
662*4882a593Smuzhiyun blkg_free(new_blkg);
663*4882a593Smuzhiyun ret = -ENOMEM;
664*4882a593Smuzhiyun goto fail;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun rcu_read_lock();
668*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun blkg = blkg_lookup_check(pos, pol, q);
671*4882a593Smuzhiyun if (IS_ERR(blkg)) {
672*4882a593Smuzhiyun ret = PTR_ERR(blkg);
673*4882a593Smuzhiyun blkg_free(new_blkg);
674*4882a593Smuzhiyun goto fail_preloaded;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun if (blkg) {
678*4882a593Smuzhiyun blkg_free(new_blkg);
679*4882a593Smuzhiyun } else {
680*4882a593Smuzhiyun blkg = blkg_create(pos, q, new_blkg);
681*4882a593Smuzhiyun if (IS_ERR(blkg)) {
682*4882a593Smuzhiyun ret = PTR_ERR(blkg);
683*4882a593Smuzhiyun goto fail_preloaded;
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun radix_tree_preload_end();
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun if (pos == blkcg)
690*4882a593Smuzhiyun goto success;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun success:
693*4882a593Smuzhiyun ctx->disk = disk;
694*4882a593Smuzhiyun ctx->blkg = blkg;
695*4882a593Smuzhiyun ctx->body = input;
696*4882a593Smuzhiyun return 0;
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun fail_preloaded:
699*4882a593Smuzhiyun radix_tree_preload_end();
700*4882a593Smuzhiyun fail_unlock:
701*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
702*4882a593Smuzhiyun rcu_read_unlock();
703*4882a593Smuzhiyun fail:
704*4882a593Smuzhiyun put_disk_and_module(disk);
705*4882a593Smuzhiyun /*
706*4882a593Smuzhiyun * If queue was bypassing, we should retry. Do so after a
707*4882a593Smuzhiyun * short msleep(). It isn't strictly necessary but queue
708*4882a593Smuzhiyun * can be bypassing for some time and it's always nice to
709*4882a593Smuzhiyun * avoid busy looping.
710*4882a593Smuzhiyun */
711*4882a593Smuzhiyun if (ret == -EBUSY) {
712*4882a593Smuzhiyun msleep(10);
713*4882a593Smuzhiyun ret = restart_syscall();
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun return ret;
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_conf_prep);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun /**
720*4882a593Smuzhiyun * blkg_conf_finish - finish up per-blkg config update
721*4882a593Smuzhiyun * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
722*4882a593Smuzhiyun *
723*4882a593Smuzhiyun * Finish up after per-blkg config update. This function must be paired
724*4882a593Smuzhiyun * with blkg_conf_prep().
725*4882a593Smuzhiyun */
blkg_conf_finish(struct blkg_conf_ctx * ctx)726*4882a593Smuzhiyun void blkg_conf_finish(struct blkg_conf_ctx *ctx)
727*4882a593Smuzhiyun __releases(&ctx->disk->queue->queue_lock) __releases(rcu)
728*4882a593Smuzhiyun {
729*4882a593Smuzhiyun spin_unlock_irq(&ctx->disk->queue->queue_lock);
730*4882a593Smuzhiyun rcu_read_unlock();
731*4882a593Smuzhiyun put_disk_and_module(ctx->disk);
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_conf_finish);
734*4882a593Smuzhiyun
blkg_iostat_set(struct blkg_iostat * dst,struct blkg_iostat * src)735*4882a593Smuzhiyun static void blkg_iostat_set(struct blkg_iostat *dst, struct blkg_iostat *src)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun int i;
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun for (i = 0; i < BLKG_IOSTAT_NR; i++) {
740*4882a593Smuzhiyun dst->bytes[i] = src->bytes[i];
741*4882a593Smuzhiyun dst->ios[i] = src->ios[i];
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun
blkg_iostat_add(struct blkg_iostat * dst,struct blkg_iostat * src)745*4882a593Smuzhiyun static void blkg_iostat_add(struct blkg_iostat *dst, struct blkg_iostat *src)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun int i;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun for (i = 0; i < BLKG_IOSTAT_NR; i++) {
750*4882a593Smuzhiyun dst->bytes[i] += src->bytes[i];
751*4882a593Smuzhiyun dst->ios[i] += src->ios[i];
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun
blkg_iostat_sub(struct blkg_iostat * dst,struct blkg_iostat * src)755*4882a593Smuzhiyun static void blkg_iostat_sub(struct blkg_iostat *dst, struct blkg_iostat *src)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun int i;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun for (i = 0; i < BLKG_IOSTAT_NR; i++) {
760*4882a593Smuzhiyun dst->bytes[i] -= src->bytes[i];
761*4882a593Smuzhiyun dst->ios[i] -= src->ios[i];
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun
blkcg_rstat_flush(struct cgroup_subsys_state * css,int cpu)765*4882a593Smuzhiyun static void blkcg_rstat_flush(struct cgroup_subsys_state *css, int cpu)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun struct blkcg *blkcg = css_to_blkcg(css);
768*4882a593Smuzhiyun struct blkcg_gq *blkg;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun rcu_read_lock();
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
773*4882a593Smuzhiyun struct blkcg_gq *parent = blkg->parent;
774*4882a593Smuzhiyun struct blkg_iostat_set *bisc = per_cpu_ptr(blkg->iostat_cpu, cpu);
775*4882a593Smuzhiyun struct blkg_iostat cur, delta;
776*4882a593Smuzhiyun unsigned int seq;
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun /* fetch the current per-cpu values */
779*4882a593Smuzhiyun do {
780*4882a593Smuzhiyun seq = u64_stats_fetch_begin(&bisc->sync);
781*4882a593Smuzhiyun blkg_iostat_set(&cur, &bisc->cur);
782*4882a593Smuzhiyun } while (u64_stats_fetch_retry(&bisc->sync, seq));
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun /* propagate percpu delta to global */
785*4882a593Smuzhiyun u64_stats_update_begin(&blkg->iostat.sync);
786*4882a593Smuzhiyun blkg_iostat_set(&delta, &cur);
787*4882a593Smuzhiyun blkg_iostat_sub(&delta, &bisc->last);
788*4882a593Smuzhiyun blkg_iostat_add(&blkg->iostat.cur, &delta);
789*4882a593Smuzhiyun blkg_iostat_add(&bisc->last, &delta);
790*4882a593Smuzhiyun u64_stats_update_end(&blkg->iostat.sync);
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun /* propagate global delta to parent */
793*4882a593Smuzhiyun if (parent) {
794*4882a593Smuzhiyun u64_stats_update_begin(&parent->iostat.sync);
795*4882a593Smuzhiyun blkg_iostat_set(&delta, &blkg->iostat.cur);
796*4882a593Smuzhiyun blkg_iostat_sub(&delta, &blkg->iostat.last);
797*4882a593Smuzhiyun blkg_iostat_add(&parent->iostat.cur, &delta);
798*4882a593Smuzhiyun blkg_iostat_add(&blkg->iostat.last, &delta);
799*4882a593Smuzhiyun u64_stats_update_end(&parent->iostat.sync);
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun rcu_read_unlock();
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun /*
807*4882a593Smuzhiyun * The rstat algorithms intentionally don't handle the root cgroup to avoid
808*4882a593Smuzhiyun * incurring overhead when no cgroups are defined. For that reason,
809*4882a593Smuzhiyun * cgroup_rstat_flush in blkcg_print_stat does not actually fill out the
810*4882a593Smuzhiyun * iostat in the root cgroup's blkcg_gq.
811*4882a593Smuzhiyun *
812*4882a593Smuzhiyun * However, we would like to re-use the printing code between the root and
813*4882a593Smuzhiyun * non-root cgroups to the extent possible. For that reason, we simulate
814*4882a593Smuzhiyun * flushing the root cgroup's stats by explicitly filling in the iostat
815*4882a593Smuzhiyun * with disk level statistics.
816*4882a593Smuzhiyun */
blkcg_fill_root_iostats(void)817*4882a593Smuzhiyun static void blkcg_fill_root_iostats(void)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun struct class_dev_iter iter;
820*4882a593Smuzhiyun struct device *dev;
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
823*4882a593Smuzhiyun while ((dev = class_dev_iter_next(&iter))) {
824*4882a593Smuzhiyun struct gendisk *disk = dev_to_disk(dev);
825*4882a593Smuzhiyun struct hd_struct *part = disk_get_part(disk, 0);
826*4882a593Smuzhiyun struct blkcg_gq *blkg = blk_queue_root_blkg(disk->queue);
827*4882a593Smuzhiyun struct blkg_iostat tmp;
828*4882a593Smuzhiyun int cpu;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun memset(&tmp, 0, sizeof(tmp));
831*4882a593Smuzhiyun for_each_possible_cpu(cpu) {
832*4882a593Smuzhiyun struct disk_stats *cpu_dkstats;
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun cpu_dkstats = per_cpu_ptr(part->dkstats, cpu);
835*4882a593Smuzhiyun tmp.ios[BLKG_IOSTAT_READ] +=
836*4882a593Smuzhiyun cpu_dkstats->ios[STAT_READ];
837*4882a593Smuzhiyun tmp.ios[BLKG_IOSTAT_WRITE] +=
838*4882a593Smuzhiyun cpu_dkstats->ios[STAT_WRITE];
839*4882a593Smuzhiyun tmp.ios[BLKG_IOSTAT_DISCARD] +=
840*4882a593Smuzhiyun cpu_dkstats->ios[STAT_DISCARD];
841*4882a593Smuzhiyun // convert sectors to bytes
842*4882a593Smuzhiyun tmp.bytes[BLKG_IOSTAT_READ] +=
843*4882a593Smuzhiyun cpu_dkstats->sectors[STAT_READ] << 9;
844*4882a593Smuzhiyun tmp.bytes[BLKG_IOSTAT_WRITE] +=
845*4882a593Smuzhiyun cpu_dkstats->sectors[STAT_WRITE] << 9;
846*4882a593Smuzhiyun tmp.bytes[BLKG_IOSTAT_DISCARD] +=
847*4882a593Smuzhiyun cpu_dkstats->sectors[STAT_DISCARD] << 9;
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun u64_stats_update_begin(&blkg->iostat.sync);
850*4882a593Smuzhiyun blkg_iostat_set(&blkg->iostat.cur, &tmp);
851*4882a593Smuzhiyun u64_stats_update_end(&blkg->iostat.sync);
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun disk_put_part(part);
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
blkcg_print_stat(struct seq_file * sf,void * v)857*4882a593Smuzhiyun static int blkcg_print_stat(struct seq_file *sf, void *v)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
860*4882a593Smuzhiyun struct blkcg_gq *blkg;
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun if (!seq_css(sf)->parent)
863*4882a593Smuzhiyun blkcg_fill_root_iostats();
864*4882a593Smuzhiyun else
865*4882a593Smuzhiyun cgroup_rstat_flush(blkcg->css.cgroup);
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun rcu_read_lock();
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
870*4882a593Smuzhiyun struct blkg_iostat_set *bis = &blkg->iostat;
871*4882a593Smuzhiyun const char *dname;
872*4882a593Smuzhiyun char *buf;
873*4882a593Smuzhiyun u64 rbytes, wbytes, rios, wios, dbytes, dios;
874*4882a593Smuzhiyun size_t size = seq_get_buf(sf, &buf), off = 0;
875*4882a593Smuzhiyun int i;
876*4882a593Smuzhiyun bool has_stats = false;
877*4882a593Smuzhiyun unsigned seq;
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun spin_lock_irq(&blkg->q->queue_lock);
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun if (!blkg->online)
882*4882a593Smuzhiyun goto skip;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun dname = blkg_dev_name(blkg);
885*4882a593Smuzhiyun if (!dname)
886*4882a593Smuzhiyun goto skip;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun * Hooray string manipulation, count is the size written NOT
890*4882a593Smuzhiyun * INCLUDING THE \0, so size is now count+1 less than what we
891*4882a593Smuzhiyun * had before, but we want to start writing the next bit from
892*4882a593Smuzhiyun * the \0 so we only add count to buf.
893*4882a593Smuzhiyun */
894*4882a593Smuzhiyun off += scnprintf(buf+off, size-off, "%s ", dname);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun do {
897*4882a593Smuzhiyun seq = u64_stats_fetch_begin(&bis->sync);
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun rbytes = bis->cur.bytes[BLKG_IOSTAT_READ];
900*4882a593Smuzhiyun wbytes = bis->cur.bytes[BLKG_IOSTAT_WRITE];
901*4882a593Smuzhiyun dbytes = bis->cur.bytes[BLKG_IOSTAT_DISCARD];
902*4882a593Smuzhiyun rios = bis->cur.ios[BLKG_IOSTAT_READ];
903*4882a593Smuzhiyun wios = bis->cur.ios[BLKG_IOSTAT_WRITE];
904*4882a593Smuzhiyun dios = bis->cur.ios[BLKG_IOSTAT_DISCARD];
905*4882a593Smuzhiyun } while (u64_stats_fetch_retry(&bis->sync, seq));
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun if (rbytes || wbytes || rios || wios) {
908*4882a593Smuzhiyun has_stats = true;
909*4882a593Smuzhiyun off += scnprintf(buf+off, size-off,
910*4882a593Smuzhiyun "rbytes=%llu wbytes=%llu rios=%llu wios=%llu dbytes=%llu dios=%llu",
911*4882a593Smuzhiyun rbytes, wbytes, rios, wios,
912*4882a593Smuzhiyun dbytes, dios);
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun if (blkcg_debug_stats && atomic_read(&blkg->use_delay)) {
916*4882a593Smuzhiyun has_stats = true;
917*4882a593Smuzhiyun off += scnprintf(buf+off, size-off,
918*4882a593Smuzhiyun " use_delay=%d delay_nsec=%llu",
919*4882a593Smuzhiyun atomic_read(&blkg->use_delay),
920*4882a593Smuzhiyun (unsigned long long)atomic64_read(&blkg->delay_nsec));
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
924*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
925*4882a593Smuzhiyun size_t written;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun if (!blkg->pd[i] || !pol->pd_stat_fn)
928*4882a593Smuzhiyun continue;
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun written = pol->pd_stat_fn(blkg->pd[i], buf+off, size-off);
931*4882a593Smuzhiyun if (written)
932*4882a593Smuzhiyun has_stats = true;
933*4882a593Smuzhiyun off += written;
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun if (has_stats) {
937*4882a593Smuzhiyun if (off < size - 1) {
938*4882a593Smuzhiyun off += scnprintf(buf+off, size-off, "\n");
939*4882a593Smuzhiyun seq_commit(sf, off);
940*4882a593Smuzhiyun } else {
941*4882a593Smuzhiyun seq_commit(sf, -1);
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun skip:
945*4882a593Smuzhiyun spin_unlock_irq(&blkg->q->queue_lock);
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun rcu_read_unlock();
949*4882a593Smuzhiyun return 0;
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun static struct cftype blkcg_files[] = {
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun .name = "stat",
955*4882a593Smuzhiyun .seq_show = blkcg_print_stat,
956*4882a593Smuzhiyun },
957*4882a593Smuzhiyun { } /* terminate */
958*4882a593Smuzhiyun };
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun static struct cftype blkcg_legacy_files[] = {
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun .name = "reset_stats",
963*4882a593Smuzhiyun .write_u64 = blkcg_reset_stats,
964*4882a593Smuzhiyun },
965*4882a593Smuzhiyun { } /* terminate */
966*4882a593Smuzhiyun };
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun /*
969*4882a593Smuzhiyun * blkcg destruction is a three-stage process.
970*4882a593Smuzhiyun *
971*4882a593Smuzhiyun * 1. Destruction starts. The blkcg_css_offline() callback is invoked
972*4882a593Smuzhiyun * which offlines writeback. Here we tie the next stage of blkg destruction
973*4882a593Smuzhiyun * to the completion of writeback associated with the blkcg. This lets us
974*4882a593Smuzhiyun * avoid punting potentially large amounts of outstanding writeback to root
975*4882a593Smuzhiyun * while maintaining any ongoing policies. The next stage is triggered when
976*4882a593Smuzhiyun * the nr_cgwbs count goes to zero.
977*4882a593Smuzhiyun *
978*4882a593Smuzhiyun * 2. When the nr_cgwbs count goes to zero, blkcg_destroy_blkgs() is called
979*4882a593Smuzhiyun * and handles the destruction of blkgs. Here the css reference held by
980*4882a593Smuzhiyun * the blkg is put back eventually allowing blkcg_css_free() to be called.
981*4882a593Smuzhiyun * This work may occur in cgwb_release_workfn() on the cgwb_release
982*4882a593Smuzhiyun * workqueue. Any submitted ios that fail to get the blkg ref will be
983*4882a593Smuzhiyun * punted to the root_blkg.
984*4882a593Smuzhiyun *
985*4882a593Smuzhiyun * 3. Once the blkcg ref count goes to zero, blkcg_css_free() is called.
986*4882a593Smuzhiyun * This finally frees the blkcg.
987*4882a593Smuzhiyun */
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /**
990*4882a593Smuzhiyun * blkcg_css_offline - cgroup css_offline callback
991*4882a593Smuzhiyun * @css: css of interest
992*4882a593Smuzhiyun *
993*4882a593Smuzhiyun * This function is called when @css is about to go away. Here the cgwbs are
994*4882a593Smuzhiyun * offlined first and only once writeback associated with the blkcg has
995*4882a593Smuzhiyun * finished do we start step 2 (see above).
996*4882a593Smuzhiyun */
blkcg_css_offline(struct cgroup_subsys_state * css)997*4882a593Smuzhiyun static void blkcg_css_offline(struct cgroup_subsys_state *css)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun struct blkcg *blkcg = css_to_blkcg(css);
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /* this prevents anyone from attaching or migrating to this blkcg */
1002*4882a593Smuzhiyun wb_blkcg_offline(blkcg);
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun /* put the base online pin allowing step 2 to be triggered */
1005*4882a593Smuzhiyun blkcg_unpin_online(blkcg);
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun /**
1009*4882a593Smuzhiyun * blkcg_destroy_blkgs - responsible for shooting down blkgs
1010*4882a593Smuzhiyun * @blkcg: blkcg of interest
1011*4882a593Smuzhiyun *
1012*4882a593Smuzhiyun * blkgs should be removed while holding both q and blkcg locks. As blkcg lock
1013*4882a593Smuzhiyun * is nested inside q lock, this function performs reverse double lock dancing.
1014*4882a593Smuzhiyun * Destroying the blkgs releases the reference held on the blkcg's css allowing
1015*4882a593Smuzhiyun * blkcg_css_free to eventually be called.
1016*4882a593Smuzhiyun *
1017*4882a593Smuzhiyun * This is the blkcg counterpart of ioc_release_fn().
1018*4882a593Smuzhiyun */
blkcg_destroy_blkgs(struct blkcg * blkcg)1019*4882a593Smuzhiyun void blkcg_destroy_blkgs(struct blkcg *blkcg)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun might_sleep();
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun spin_lock_irq(&blkcg->lock);
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun while (!hlist_empty(&blkcg->blkg_list)) {
1026*4882a593Smuzhiyun struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
1027*4882a593Smuzhiyun struct blkcg_gq, blkcg_node);
1028*4882a593Smuzhiyun struct request_queue *q = blkg->q;
1029*4882a593Smuzhiyun
1030*4882a593Smuzhiyun if (need_resched() || !spin_trylock(&q->queue_lock)) {
1031*4882a593Smuzhiyun /*
1032*4882a593Smuzhiyun * Given that the system can accumulate a huge number
1033*4882a593Smuzhiyun * of blkgs in pathological cases, check to see if we
1034*4882a593Smuzhiyun * need to rescheduling to avoid softlockup.
1035*4882a593Smuzhiyun */
1036*4882a593Smuzhiyun spin_unlock_irq(&blkcg->lock);
1037*4882a593Smuzhiyun cond_resched();
1038*4882a593Smuzhiyun spin_lock_irq(&blkcg->lock);
1039*4882a593Smuzhiyun continue;
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun blkg_destroy(blkg);
1043*4882a593Smuzhiyun spin_unlock(&q->queue_lock);
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun spin_unlock_irq(&blkcg->lock);
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun
blkcg_css_free(struct cgroup_subsys_state * css)1049*4882a593Smuzhiyun static void blkcg_css_free(struct cgroup_subsys_state *css)
1050*4882a593Smuzhiyun {
1051*4882a593Smuzhiyun struct blkcg *blkcg = css_to_blkcg(css);
1052*4882a593Smuzhiyun int i;
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun mutex_lock(&blkcg_pol_mutex);
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun list_del(&blkcg->all_blkcgs_node);
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++)
1059*4882a593Smuzhiyun if (blkcg->cpd[i])
1060*4882a593Smuzhiyun blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun kfree(blkcg);
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun static struct cgroup_subsys_state *
blkcg_css_alloc(struct cgroup_subsys_state * parent_css)1068*4882a593Smuzhiyun blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun struct blkcg *blkcg;
1071*4882a593Smuzhiyun struct cgroup_subsys_state *ret;
1072*4882a593Smuzhiyun int i;
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun mutex_lock(&blkcg_pol_mutex);
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun if (!parent_css) {
1077*4882a593Smuzhiyun blkcg = &blkcg_root;
1078*4882a593Smuzhiyun } else {
1079*4882a593Smuzhiyun blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1080*4882a593Smuzhiyun if (!blkcg) {
1081*4882a593Smuzhiyun ret = ERR_PTR(-ENOMEM);
1082*4882a593Smuzhiyun goto unlock;
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun }
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS ; i++) {
1087*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
1088*4882a593Smuzhiyun struct blkcg_policy_data *cpd;
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /*
1091*4882a593Smuzhiyun * If the policy hasn't been attached yet, wait for it
1092*4882a593Smuzhiyun * to be attached before doing anything else. Otherwise,
1093*4882a593Smuzhiyun * check if the policy requires any specific per-cgroup
1094*4882a593Smuzhiyun * data: if it does, allocate and initialize it.
1095*4882a593Smuzhiyun */
1096*4882a593Smuzhiyun if (!pol || !pol->cpd_alloc_fn)
1097*4882a593Smuzhiyun continue;
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1100*4882a593Smuzhiyun if (!cpd) {
1101*4882a593Smuzhiyun ret = ERR_PTR(-ENOMEM);
1102*4882a593Smuzhiyun goto free_pd_blkcg;
1103*4882a593Smuzhiyun }
1104*4882a593Smuzhiyun blkcg->cpd[i] = cpd;
1105*4882a593Smuzhiyun cpd->blkcg = blkcg;
1106*4882a593Smuzhiyun cpd->plid = i;
1107*4882a593Smuzhiyun if (pol->cpd_init_fn)
1108*4882a593Smuzhiyun pol->cpd_init_fn(cpd);
1109*4882a593Smuzhiyun }
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun spin_lock_init(&blkcg->lock);
1112*4882a593Smuzhiyun refcount_set(&blkcg->online_pin, 1);
1113*4882a593Smuzhiyun INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT | __GFP_NOWARN);
1114*4882a593Smuzhiyun INIT_HLIST_HEAD(&blkcg->blkg_list);
1115*4882a593Smuzhiyun #ifdef CONFIG_CGROUP_WRITEBACK
1116*4882a593Smuzhiyun INIT_LIST_HEAD(&blkcg->cgwb_list);
1117*4882a593Smuzhiyun #endif
1118*4882a593Smuzhiyun list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1121*4882a593Smuzhiyun return &blkcg->css;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun free_pd_blkcg:
1124*4882a593Smuzhiyun for (i--; i >= 0; i--)
1125*4882a593Smuzhiyun if (blkcg->cpd[i])
1126*4882a593Smuzhiyun blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun if (blkcg != &blkcg_root)
1129*4882a593Smuzhiyun kfree(blkcg);
1130*4882a593Smuzhiyun unlock:
1131*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1132*4882a593Smuzhiyun return ret;
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun
blkcg_css_online(struct cgroup_subsys_state * css)1135*4882a593Smuzhiyun static int blkcg_css_online(struct cgroup_subsys_state *css)
1136*4882a593Smuzhiyun {
1137*4882a593Smuzhiyun struct blkcg *blkcg = css_to_blkcg(css);
1138*4882a593Smuzhiyun struct blkcg *parent = blkcg_parent(blkcg);
1139*4882a593Smuzhiyun
1140*4882a593Smuzhiyun /*
1141*4882a593Smuzhiyun * blkcg_pin_online() is used to delay blkcg offline so that blkgs
1142*4882a593Smuzhiyun * don't go offline while cgwbs are still active on them. Pin the
1143*4882a593Smuzhiyun * parent so that offline always happens towards the root.
1144*4882a593Smuzhiyun */
1145*4882a593Smuzhiyun if (parent)
1146*4882a593Smuzhiyun blkcg_pin_online(parent);
1147*4882a593Smuzhiyun return 0;
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /**
1151*4882a593Smuzhiyun * blkcg_init_queue - initialize blkcg part of request queue
1152*4882a593Smuzhiyun * @q: request_queue to initialize
1153*4882a593Smuzhiyun *
1154*4882a593Smuzhiyun * Called from blk_alloc_queue(). Responsible for initializing blkcg
1155*4882a593Smuzhiyun * part of new request_queue @q.
1156*4882a593Smuzhiyun *
1157*4882a593Smuzhiyun * RETURNS:
1158*4882a593Smuzhiyun * 0 on success, -errno on failure.
1159*4882a593Smuzhiyun */
blkcg_init_queue(struct request_queue * q)1160*4882a593Smuzhiyun int blkcg_init_queue(struct request_queue *q)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun struct blkcg_gq *new_blkg, *blkg;
1163*4882a593Smuzhiyun bool preloaded;
1164*4882a593Smuzhiyun int ret;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
1167*4882a593Smuzhiyun if (!new_blkg)
1168*4882a593Smuzhiyun return -ENOMEM;
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun preloaded = !radix_tree_preload(GFP_KERNEL);
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun /* Make sure the root blkg exists. */
1173*4882a593Smuzhiyun rcu_read_lock();
1174*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
1175*4882a593Smuzhiyun blkg = blkg_create(&blkcg_root, q, new_blkg);
1176*4882a593Smuzhiyun if (IS_ERR(blkg))
1177*4882a593Smuzhiyun goto err_unlock;
1178*4882a593Smuzhiyun q->root_blkg = blkg;
1179*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
1180*4882a593Smuzhiyun rcu_read_unlock();
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun if (preloaded)
1183*4882a593Smuzhiyun radix_tree_preload_end();
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun ret = blk_ioprio_init(q);
1186*4882a593Smuzhiyun if (ret)
1187*4882a593Smuzhiyun goto err_destroy_all;
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun ret = blk_throtl_init(q);
1190*4882a593Smuzhiyun if (ret)
1191*4882a593Smuzhiyun goto err_destroy_all;
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun ret = blk_iolatency_init(q);
1194*4882a593Smuzhiyun if (ret) {
1195*4882a593Smuzhiyun blk_throtl_exit(q);
1196*4882a593Smuzhiyun goto err_destroy_all;
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun return 0;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun err_destroy_all:
1202*4882a593Smuzhiyun blkg_destroy_all(q);
1203*4882a593Smuzhiyun return ret;
1204*4882a593Smuzhiyun err_unlock:
1205*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
1206*4882a593Smuzhiyun rcu_read_unlock();
1207*4882a593Smuzhiyun if (preloaded)
1208*4882a593Smuzhiyun radix_tree_preload_end();
1209*4882a593Smuzhiyun return PTR_ERR(blkg);
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun /**
1213*4882a593Smuzhiyun * blkcg_exit_queue - exit and release blkcg part of request_queue
1214*4882a593Smuzhiyun * @q: request_queue being released
1215*4882a593Smuzhiyun *
1216*4882a593Smuzhiyun * Called from blk_exit_queue(). Responsible for exiting blkcg part.
1217*4882a593Smuzhiyun */
blkcg_exit_queue(struct request_queue * q)1218*4882a593Smuzhiyun void blkcg_exit_queue(struct request_queue *q)
1219*4882a593Smuzhiyun {
1220*4882a593Smuzhiyun blkg_destroy_all(q);
1221*4882a593Smuzhiyun blk_throtl_exit(q);
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun /*
1225*4882a593Smuzhiyun * We cannot support shared io contexts, as we have no mean to support
1226*4882a593Smuzhiyun * two tasks with the same ioc in two different groups without major rework
1227*4882a593Smuzhiyun * of the main cic data structures. For now we allow a task to change
1228*4882a593Smuzhiyun * its cgroup only if it's the only owner of its ioc.
1229*4882a593Smuzhiyun */
blkcg_can_attach(struct cgroup_taskset * tset)1230*4882a593Smuzhiyun static int blkcg_can_attach(struct cgroup_taskset *tset)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun struct task_struct *task;
1233*4882a593Smuzhiyun struct cgroup_subsys_state *dst_css;
1234*4882a593Smuzhiyun struct io_context *ioc;
1235*4882a593Smuzhiyun int ret = 0;
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun /* task_lock() is needed to avoid races with exit_io_context() */
1238*4882a593Smuzhiyun cgroup_taskset_for_each(task, dst_css, tset) {
1239*4882a593Smuzhiyun task_lock(task);
1240*4882a593Smuzhiyun ioc = task->io_context;
1241*4882a593Smuzhiyun if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1242*4882a593Smuzhiyun ret = -EINVAL;
1243*4882a593Smuzhiyun task_unlock(task);
1244*4882a593Smuzhiyun if (ret)
1245*4882a593Smuzhiyun break;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun return ret;
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun
blkcg_bind(struct cgroup_subsys_state * root_css)1250*4882a593Smuzhiyun static void blkcg_bind(struct cgroup_subsys_state *root_css)
1251*4882a593Smuzhiyun {
1252*4882a593Smuzhiyun int i;
1253*4882a593Smuzhiyun
1254*4882a593Smuzhiyun mutex_lock(&blkcg_pol_mutex);
1255*4882a593Smuzhiyun
1256*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++) {
1257*4882a593Smuzhiyun struct blkcg_policy *pol = blkcg_policy[i];
1258*4882a593Smuzhiyun struct blkcg *blkcg;
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun if (!pol || !pol->cpd_bind_fn)
1261*4882a593Smuzhiyun continue;
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node)
1264*4882a593Smuzhiyun if (blkcg->cpd[pol->plid])
1265*4882a593Smuzhiyun pol->cpd_bind_fn(blkcg->cpd[pol->plid]);
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun
blkcg_exit(struct task_struct * tsk)1270*4882a593Smuzhiyun static void blkcg_exit(struct task_struct *tsk)
1271*4882a593Smuzhiyun {
1272*4882a593Smuzhiyun if (tsk->throttle_queue)
1273*4882a593Smuzhiyun blk_put_queue(tsk->throttle_queue);
1274*4882a593Smuzhiyun tsk->throttle_queue = NULL;
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun struct cgroup_subsys io_cgrp_subsys = {
1278*4882a593Smuzhiyun .css_alloc = blkcg_css_alloc,
1279*4882a593Smuzhiyun .css_online = blkcg_css_online,
1280*4882a593Smuzhiyun .css_offline = blkcg_css_offline,
1281*4882a593Smuzhiyun .css_free = blkcg_css_free,
1282*4882a593Smuzhiyun .can_attach = blkcg_can_attach,
1283*4882a593Smuzhiyun .css_rstat_flush = blkcg_rstat_flush,
1284*4882a593Smuzhiyun .bind = blkcg_bind,
1285*4882a593Smuzhiyun .dfl_cftypes = blkcg_files,
1286*4882a593Smuzhiyun .legacy_cftypes = blkcg_legacy_files,
1287*4882a593Smuzhiyun .legacy_name = "blkio",
1288*4882a593Smuzhiyun .exit = blkcg_exit,
1289*4882a593Smuzhiyun #ifdef CONFIG_MEMCG
1290*4882a593Smuzhiyun /*
1291*4882a593Smuzhiyun * This ensures that, if available, memcg is automatically enabled
1292*4882a593Smuzhiyun * together on the default hierarchy so that the owner cgroup can
1293*4882a593Smuzhiyun * be retrieved from writeback pages.
1294*4882a593Smuzhiyun */
1295*4882a593Smuzhiyun .depends_on = 1 << memory_cgrp_id,
1296*4882a593Smuzhiyun #endif
1297*4882a593Smuzhiyun };
1298*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(io_cgrp_subsys);
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun /**
1301*4882a593Smuzhiyun * blkcg_activate_policy - activate a blkcg policy on a request_queue
1302*4882a593Smuzhiyun * @q: request_queue of interest
1303*4882a593Smuzhiyun * @pol: blkcg policy to activate
1304*4882a593Smuzhiyun *
1305*4882a593Smuzhiyun * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
1306*4882a593Smuzhiyun * bypass mode to populate its blkgs with policy_data for @pol.
1307*4882a593Smuzhiyun *
1308*4882a593Smuzhiyun * Activation happens with @q bypassed, so nobody would be accessing blkgs
1309*4882a593Smuzhiyun * from IO path. Update of each blkg is protected by both queue and blkcg
1310*4882a593Smuzhiyun * locks so that holding either lock and testing blkcg_policy_enabled() is
1311*4882a593Smuzhiyun * always enough for dereferencing policy data.
1312*4882a593Smuzhiyun *
1313*4882a593Smuzhiyun * The caller is responsible for synchronizing [de]activations and policy
1314*4882a593Smuzhiyun * [un]registerations. Returns 0 on success, -errno on failure.
1315*4882a593Smuzhiyun */
blkcg_activate_policy(struct request_queue * q,const struct blkcg_policy * pol)1316*4882a593Smuzhiyun int blkcg_activate_policy(struct request_queue *q,
1317*4882a593Smuzhiyun const struct blkcg_policy *pol)
1318*4882a593Smuzhiyun {
1319*4882a593Smuzhiyun struct blkg_policy_data *pd_prealloc = NULL;
1320*4882a593Smuzhiyun struct blkcg_gq *blkg, *pinned_blkg = NULL;
1321*4882a593Smuzhiyun int ret;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun if (blkcg_policy_enabled(q, pol))
1324*4882a593Smuzhiyun return 0;
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun if (queue_is_mq(q))
1327*4882a593Smuzhiyun blk_mq_freeze_queue(q);
1328*4882a593Smuzhiyun retry:
1329*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /* blkg_list is pushed at the head, reverse walk to allocate parents first */
1332*4882a593Smuzhiyun list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) {
1333*4882a593Smuzhiyun struct blkg_policy_data *pd;
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun if (blkg->pd[pol->plid])
1336*4882a593Smuzhiyun continue;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun /* If prealloc matches, use it; otherwise try GFP_NOWAIT */
1339*4882a593Smuzhiyun if (blkg == pinned_blkg) {
1340*4882a593Smuzhiyun pd = pd_prealloc;
1341*4882a593Smuzhiyun pd_prealloc = NULL;
1342*4882a593Smuzhiyun } else {
1343*4882a593Smuzhiyun pd = pol->pd_alloc_fn(GFP_NOWAIT | __GFP_NOWARN, q,
1344*4882a593Smuzhiyun blkg->blkcg);
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun if (!pd) {
1348*4882a593Smuzhiyun /*
1349*4882a593Smuzhiyun * GFP_NOWAIT failed. Free the existing one and
1350*4882a593Smuzhiyun * prealloc for @blkg w/ GFP_KERNEL.
1351*4882a593Smuzhiyun */
1352*4882a593Smuzhiyun if (pinned_blkg)
1353*4882a593Smuzhiyun blkg_put(pinned_blkg);
1354*4882a593Smuzhiyun blkg_get(blkg);
1355*4882a593Smuzhiyun pinned_blkg = blkg;
1356*4882a593Smuzhiyun
1357*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
1358*4882a593Smuzhiyun
1359*4882a593Smuzhiyun if (pd_prealloc)
1360*4882a593Smuzhiyun pol->pd_free_fn(pd_prealloc);
1361*4882a593Smuzhiyun pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q,
1362*4882a593Smuzhiyun blkg->blkcg);
1363*4882a593Smuzhiyun if (pd_prealloc)
1364*4882a593Smuzhiyun goto retry;
1365*4882a593Smuzhiyun else
1366*4882a593Smuzhiyun goto enomem;
1367*4882a593Smuzhiyun }
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun blkg->pd[pol->plid] = pd;
1370*4882a593Smuzhiyun pd->blkg = blkg;
1371*4882a593Smuzhiyun pd->plid = pol->plid;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun /* all allocated, init in the same order */
1375*4882a593Smuzhiyun if (pol->pd_init_fn)
1376*4882a593Smuzhiyun list_for_each_entry_reverse(blkg, &q->blkg_list, q_node)
1377*4882a593Smuzhiyun pol->pd_init_fn(blkg->pd[pol->plid]);
1378*4882a593Smuzhiyun
1379*4882a593Smuzhiyun __set_bit(pol->plid, q->blkcg_pols);
1380*4882a593Smuzhiyun ret = 0;
1381*4882a593Smuzhiyun
1382*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
1383*4882a593Smuzhiyun out:
1384*4882a593Smuzhiyun if (queue_is_mq(q))
1385*4882a593Smuzhiyun blk_mq_unfreeze_queue(q);
1386*4882a593Smuzhiyun if (pinned_blkg)
1387*4882a593Smuzhiyun blkg_put(pinned_blkg);
1388*4882a593Smuzhiyun if (pd_prealloc)
1389*4882a593Smuzhiyun pol->pd_free_fn(pd_prealloc);
1390*4882a593Smuzhiyun return ret;
1391*4882a593Smuzhiyun
1392*4882a593Smuzhiyun enomem:
1393*4882a593Smuzhiyun /* alloc failed, nothing's initialized yet, free everything */
1394*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
1395*4882a593Smuzhiyun list_for_each_entry(blkg, &q->blkg_list, q_node) {
1396*4882a593Smuzhiyun struct blkcg *blkcg = blkg->blkcg;
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun spin_lock(&blkcg->lock);
1399*4882a593Smuzhiyun if (blkg->pd[pol->plid]) {
1400*4882a593Smuzhiyun pol->pd_free_fn(blkg->pd[pol->plid]);
1401*4882a593Smuzhiyun blkg->pd[pol->plid] = NULL;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun spin_unlock(&blkcg->lock);
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
1406*4882a593Smuzhiyun ret = -ENOMEM;
1407*4882a593Smuzhiyun goto out;
1408*4882a593Smuzhiyun }
1409*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_activate_policy);
1410*4882a593Smuzhiyun
1411*4882a593Smuzhiyun /**
1412*4882a593Smuzhiyun * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
1413*4882a593Smuzhiyun * @q: request_queue of interest
1414*4882a593Smuzhiyun * @pol: blkcg policy to deactivate
1415*4882a593Smuzhiyun *
1416*4882a593Smuzhiyun * Deactivate @pol on @q. Follows the same synchronization rules as
1417*4882a593Smuzhiyun * blkcg_activate_policy().
1418*4882a593Smuzhiyun */
blkcg_deactivate_policy(struct request_queue * q,const struct blkcg_policy * pol)1419*4882a593Smuzhiyun void blkcg_deactivate_policy(struct request_queue *q,
1420*4882a593Smuzhiyun const struct blkcg_policy *pol)
1421*4882a593Smuzhiyun {
1422*4882a593Smuzhiyun struct blkcg_gq *blkg;
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun if (!blkcg_policy_enabled(q, pol))
1425*4882a593Smuzhiyun return;
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun if (queue_is_mq(q))
1428*4882a593Smuzhiyun blk_mq_freeze_queue(q);
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun spin_lock_irq(&q->queue_lock);
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun __clear_bit(pol->plid, q->blkcg_pols);
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun list_for_each_entry(blkg, &q->blkg_list, q_node) {
1435*4882a593Smuzhiyun struct blkcg *blkcg = blkg->blkcg;
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun spin_lock(&blkcg->lock);
1438*4882a593Smuzhiyun if (blkg->pd[pol->plid]) {
1439*4882a593Smuzhiyun if (pol->pd_offline_fn)
1440*4882a593Smuzhiyun pol->pd_offline_fn(blkg->pd[pol->plid]);
1441*4882a593Smuzhiyun pol->pd_free_fn(blkg->pd[pol->plid]);
1442*4882a593Smuzhiyun blkg->pd[pol->plid] = NULL;
1443*4882a593Smuzhiyun }
1444*4882a593Smuzhiyun spin_unlock(&blkcg->lock);
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun spin_unlock_irq(&q->queue_lock);
1448*4882a593Smuzhiyun
1449*4882a593Smuzhiyun if (queue_is_mq(q))
1450*4882a593Smuzhiyun blk_mq_unfreeze_queue(q);
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
1453*4882a593Smuzhiyun
1454*4882a593Smuzhiyun /**
1455*4882a593Smuzhiyun * blkcg_policy_register - register a blkcg policy
1456*4882a593Smuzhiyun * @pol: blkcg policy to register
1457*4882a593Smuzhiyun *
1458*4882a593Smuzhiyun * Register @pol with blkcg core. Might sleep and @pol may be modified on
1459*4882a593Smuzhiyun * successful registration. Returns 0 on success and -errno on failure.
1460*4882a593Smuzhiyun */
blkcg_policy_register(struct blkcg_policy * pol)1461*4882a593Smuzhiyun int blkcg_policy_register(struct blkcg_policy *pol)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun struct blkcg *blkcg;
1464*4882a593Smuzhiyun int i, ret;
1465*4882a593Smuzhiyun
1466*4882a593Smuzhiyun mutex_lock(&blkcg_pol_register_mutex);
1467*4882a593Smuzhiyun mutex_lock(&blkcg_pol_mutex);
1468*4882a593Smuzhiyun
1469*4882a593Smuzhiyun /* find an empty slot */
1470*4882a593Smuzhiyun ret = -ENOSPC;
1471*4882a593Smuzhiyun for (i = 0; i < BLKCG_MAX_POLS; i++)
1472*4882a593Smuzhiyun if (!blkcg_policy[i])
1473*4882a593Smuzhiyun break;
1474*4882a593Smuzhiyun if (i >= BLKCG_MAX_POLS) {
1475*4882a593Smuzhiyun pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n");
1476*4882a593Smuzhiyun goto err_unlock;
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun /* Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs */
1480*4882a593Smuzhiyun if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) ||
1481*4882a593Smuzhiyun (!pol->pd_alloc_fn ^ !pol->pd_free_fn))
1482*4882a593Smuzhiyun goto err_unlock;
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun /* register @pol */
1485*4882a593Smuzhiyun pol->plid = i;
1486*4882a593Smuzhiyun blkcg_policy[pol->plid] = pol;
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun /* allocate and install cpd's */
1489*4882a593Smuzhiyun if (pol->cpd_alloc_fn) {
1490*4882a593Smuzhiyun list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1491*4882a593Smuzhiyun struct blkcg_policy_data *cpd;
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1494*4882a593Smuzhiyun if (!cpd)
1495*4882a593Smuzhiyun goto err_free_cpds;
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun blkcg->cpd[pol->plid] = cpd;
1498*4882a593Smuzhiyun cpd->blkcg = blkcg;
1499*4882a593Smuzhiyun cpd->plid = pol->plid;
1500*4882a593Smuzhiyun if (pol->cpd_init_fn)
1501*4882a593Smuzhiyun pol->cpd_init_fn(cpd);
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun }
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun /* everything is in place, add intf files for the new policy */
1508*4882a593Smuzhiyun if (pol->dfl_cftypes)
1509*4882a593Smuzhiyun WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
1510*4882a593Smuzhiyun pol->dfl_cftypes));
1511*4882a593Smuzhiyun if (pol->legacy_cftypes)
1512*4882a593Smuzhiyun WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
1513*4882a593Smuzhiyun pol->legacy_cftypes));
1514*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_register_mutex);
1515*4882a593Smuzhiyun return 0;
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun err_free_cpds:
1518*4882a593Smuzhiyun if (pol->cpd_free_fn) {
1519*4882a593Smuzhiyun list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1520*4882a593Smuzhiyun if (blkcg->cpd[pol->plid]) {
1521*4882a593Smuzhiyun pol->cpd_free_fn(blkcg->cpd[pol->plid]);
1522*4882a593Smuzhiyun blkcg->cpd[pol->plid] = NULL;
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun blkcg_policy[pol->plid] = NULL;
1527*4882a593Smuzhiyun err_unlock:
1528*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1529*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_register_mutex);
1530*4882a593Smuzhiyun return ret;
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_policy_register);
1533*4882a593Smuzhiyun
1534*4882a593Smuzhiyun /**
1535*4882a593Smuzhiyun * blkcg_policy_unregister - unregister a blkcg policy
1536*4882a593Smuzhiyun * @pol: blkcg policy to unregister
1537*4882a593Smuzhiyun *
1538*4882a593Smuzhiyun * Undo blkcg_policy_register(@pol). Might sleep.
1539*4882a593Smuzhiyun */
blkcg_policy_unregister(struct blkcg_policy * pol)1540*4882a593Smuzhiyun void blkcg_policy_unregister(struct blkcg_policy *pol)
1541*4882a593Smuzhiyun {
1542*4882a593Smuzhiyun struct blkcg *blkcg;
1543*4882a593Smuzhiyun
1544*4882a593Smuzhiyun mutex_lock(&blkcg_pol_register_mutex);
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun if (WARN_ON(blkcg_policy[pol->plid] != pol))
1547*4882a593Smuzhiyun goto out_unlock;
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun /* kill the intf files first */
1550*4882a593Smuzhiyun if (pol->dfl_cftypes)
1551*4882a593Smuzhiyun cgroup_rm_cftypes(pol->dfl_cftypes);
1552*4882a593Smuzhiyun if (pol->legacy_cftypes)
1553*4882a593Smuzhiyun cgroup_rm_cftypes(pol->legacy_cftypes);
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun /* remove cpds and unregister */
1556*4882a593Smuzhiyun mutex_lock(&blkcg_pol_mutex);
1557*4882a593Smuzhiyun
1558*4882a593Smuzhiyun if (pol->cpd_free_fn) {
1559*4882a593Smuzhiyun list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1560*4882a593Smuzhiyun if (blkcg->cpd[pol->plid]) {
1561*4882a593Smuzhiyun pol->cpd_free_fn(blkcg->cpd[pol->plid]);
1562*4882a593Smuzhiyun blkcg->cpd[pol->plid] = NULL;
1563*4882a593Smuzhiyun }
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun blkcg_policy[pol->plid] = NULL;
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_mutex);
1569*4882a593Smuzhiyun out_unlock:
1570*4882a593Smuzhiyun mutex_unlock(&blkcg_pol_register_mutex);
1571*4882a593Smuzhiyun }
1572*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkcg_policy_unregister);
1573*4882a593Smuzhiyun
__blkcg_punt_bio_submit(struct bio * bio)1574*4882a593Smuzhiyun bool __blkcg_punt_bio_submit(struct bio *bio)
1575*4882a593Smuzhiyun {
1576*4882a593Smuzhiyun struct blkcg_gq *blkg = bio->bi_blkg;
1577*4882a593Smuzhiyun
1578*4882a593Smuzhiyun /* consume the flag first */
1579*4882a593Smuzhiyun bio->bi_opf &= ~REQ_CGROUP_PUNT;
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun /* never bounce for the root cgroup */
1582*4882a593Smuzhiyun if (!blkg->parent)
1583*4882a593Smuzhiyun return false;
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun spin_lock_bh(&blkg->async_bio_lock);
1586*4882a593Smuzhiyun bio_list_add(&blkg->async_bios, bio);
1587*4882a593Smuzhiyun spin_unlock_bh(&blkg->async_bio_lock);
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work);
1590*4882a593Smuzhiyun return true;
1591*4882a593Smuzhiyun }
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun /*
1594*4882a593Smuzhiyun * Scale the accumulated delay based on how long it has been since we updated
1595*4882a593Smuzhiyun * the delay. We only call this when we are adding delay, in case it's been a
1596*4882a593Smuzhiyun * while since we added delay, and when we are checking to see if we need to
1597*4882a593Smuzhiyun * delay a task, to account for any delays that may have occurred.
1598*4882a593Smuzhiyun */
blkcg_scale_delay(struct blkcg_gq * blkg,u64 now)1599*4882a593Smuzhiyun static void blkcg_scale_delay(struct blkcg_gq *blkg, u64 now)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun u64 old = atomic64_read(&blkg->delay_start);
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun /* negative use_delay means no scaling, see blkcg_set_delay() */
1604*4882a593Smuzhiyun if (atomic_read(&blkg->use_delay) < 0)
1605*4882a593Smuzhiyun return;
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun /*
1608*4882a593Smuzhiyun * We only want to scale down every second. The idea here is that we
1609*4882a593Smuzhiyun * want to delay people for min(delay_nsec, NSEC_PER_SEC) in a certain
1610*4882a593Smuzhiyun * time window. We only want to throttle tasks for recent delay that
1611*4882a593Smuzhiyun * has occurred, in 1 second time windows since that's the maximum
1612*4882a593Smuzhiyun * things can be throttled. We save the current delay window in
1613*4882a593Smuzhiyun * blkg->last_delay so we know what amount is still left to be charged
1614*4882a593Smuzhiyun * to the blkg from this point onward. blkg->last_use keeps track of
1615*4882a593Smuzhiyun * the use_delay counter. The idea is if we're unthrottling the blkg we
1616*4882a593Smuzhiyun * are ok with whatever is happening now, and we can take away more of
1617*4882a593Smuzhiyun * the accumulated delay as we've already throttled enough that
1618*4882a593Smuzhiyun * everybody is happy with their IO latencies.
1619*4882a593Smuzhiyun */
1620*4882a593Smuzhiyun if (time_before64(old + NSEC_PER_SEC, now) &&
1621*4882a593Smuzhiyun atomic64_cmpxchg(&blkg->delay_start, old, now) == old) {
1622*4882a593Smuzhiyun u64 cur = atomic64_read(&blkg->delay_nsec);
1623*4882a593Smuzhiyun u64 sub = min_t(u64, blkg->last_delay, now - old);
1624*4882a593Smuzhiyun int cur_use = atomic_read(&blkg->use_delay);
1625*4882a593Smuzhiyun
1626*4882a593Smuzhiyun /*
1627*4882a593Smuzhiyun * We've been unthrottled, subtract a larger chunk of our
1628*4882a593Smuzhiyun * accumulated delay.
1629*4882a593Smuzhiyun */
1630*4882a593Smuzhiyun if (cur_use < blkg->last_use)
1631*4882a593Smuzhiyun sub = max_t(u64, sub, blkg->last_delay >> 1);
1632*4882a593Smuzhiyun
1633*4882a593Smuzhiyun /*
1634*4882a593Smuzhiyun * This shouldn't happen, but handle it anyway. Our delay_nsec
1635*4882a593Smuzhiyun * should only ever be growing except here where we subtract out
1636*4882a593Smuzhiyun * min(last_delay, 1 second), but lord knows bugs happen and I'd
1637*4882a593Smuzhiyun * rather not end up with negative numbers.
1638*4882a593Smuzhiyun */
1639*4882a593Smuzhiyun if (unlikely(cur < sub)) {
1640*4882a593Smuzhiyun atomic64_set(&blkg->delay_nsec, 0);
1641*4882a593Smuzhiyun blkg->last_delay = 0;
1642*4882a593Smuzhiyun } else {
1643*4882a593Smuzhiyun atomic64_sub(sub, &blkg->delay_nsec);
1644*4882a593Smuzhiyun blkg->last_delay = cur - sub;
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun blkg->last_use = cur_use;
1647*4882a593Smuzhiyun }
1648*4882a593Smuzhiyun }
1649*4882a593Smuzhiyun
1650*4882a593Smuzhiyun /*
1651*4882a593Smuzhiyun * This is called when we want to actually walk up the hierarchy and check to
1652*4882a593Smuzhiyun * see if we need to throttle, and then actually throttle if there is some
1653*4882a593Smuzhiyun * accumulated delay. This should only be called upon return to user space so
1654*4882a593Smuzhiyun * we're not holding some lock that would induce a priority inversion.
1655*4882a593Smuzhiyun */
blkcg_maybe_throttle_blkg(struct blkcg_gq * blkg,bool use_memdelay)1656*4882a593Smuzhiyun static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun unsigned long pflags;
1659*4882a593Smuzhiyun bool clamp;
1660*4882a593Smuzhiyun u64 now = ktime_to_ns(ktime_get());
1661*4882a593Smuzhiyun u64 exp;
1662*4882a593Smuzhiyun u64 delay_nsec = 0;
1663*4882a593Smuzhiyun int tok;
1664*4882a593Smuzhiyun
1665*4882a593Smuzhiyun while (blkg->parent) {
1666*4882a593Smuzhiyun int use_delay = atomic_read(&blkg->use_delay);
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun if (use_delay) {
1669*4882a593Smuzhiyun u64 this_delay;
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun blkcg_scale_delay(blkg, now);
1672*4882a593Smuzhiyun this_delay = atomic64_read(&blkg->delay_nsec);
1673*4882a593Smuzhiyun if (this_delay > delay_nsec) {
1674*4882a593Smuzhiyun delay_nsec = this_delay;
1675*4882a593Smuzhiyun clamp = use_delay > 0;
1676*4882a593Smuzhiyun }
1677*4882a593Smuzhiyun }
1678*4882a593Smuzhiyun blkg = blkg->parent;
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun if (!delay_nsec)
1682*4882a593Smuzhiyun return;
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun /*
1685*4882a593Smuzhiyun * Let's not sleep for all eternity if we've amassed a huge delay.
1686*4882a593Smuzhiyun * Swapping or metadata IO can accumulate 10's of seconds worth of
1687*4882a593Smuzhiyun * delay, and we want userspace to be able to do _something_ so cap the
1688*4882a593Smuzhiyun * delays at 0.25s. If there's 10's of seconds worth of delay then the
1689*4882a593Smuzhiyun * tasks will be delayed for 0.25 second for every syscall. If
1690*4882a593Smuzhiyun * blkcg_set_delay() was used as indicated by negative use_delay, the
1691*4882a593Smuzhiyun * caller is responsible for regulating the range.
1692*4882a593Smuzhiyun */
1693*4882a593Smuzhiyun if (clamp)
1694*4882a593Smuzhiyun delay_nsec = min_t(u64, delay_nsec, 250 * NSEC_PER_MSEC);
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun if (use_memdelay)
1697*4882a593Smuzhiyun psi_memstall_enter(&pflags);
1698*4882a593Smuzhiyun
1699*4882a593Smuzhiyun exp = ktime_add_ns(now, delay_nsec);
1700*4882a593Smuzhiyun tok = io_schedule_prepare();
1701*4882a593Smuzhiyun do {
1702*4882a593Smuzhiyun __set_current_state(TASK_KILLABLE);
1703*4882a593Smuzhiyun if (!schedule_hrtimeout(&exp, HRTIMER_MODE_ABS))
1704*4882a593Smuzhiyun break;
1705*4882a593Smuzhiyun } while (!fatal_signal_pending(current));
1706*4882a593Smuzhiyun io_schedule_finish(tok);
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun if (use_memdelay)
1709*4882a593Smuzhiyun psi_memstall_leave(&pflags);
1710*4882a593Smuzhiyun }
1711*4882a593Smuzhiyun
1712*4882a593Smuzhiyun /**
1713*4882a593Smuzhiyun * blkcg_maybe_throttle_current - throttle the current task if it has been marked
1714*4882a593Smuzhiyun *
1715*4882a593Smuzhiyun * This is only called if we've been marked with set_notify_resume(). Obviously
1716*4882a593Smuzhiyun * we can be set_notify_resume() for reasons other than blkcg throttling, so we
1717*4882a593Smuzhiyun * check to see if current->throttle_queue is set and if not this doesn't do
1718*4882a593Smuzhiyun * anything. This should only ever be called by the resume code, it's not meant
1719*4882a593Smuzhiyun * to be called by people willy-nilly as it will actually do the work to
1720*4882a593Smuzhiyun * throttle the task if it is setup for throttling.
1721*4882a593Smuzhiyun */
blkcg_maybe_throttle_current(void)1722*4882a593Smuzhiyun void blkcg_maybe_throttle_current(void)
1723*4882a593Smuzhiyun {
1724*4882a593Smuzhiyun struct request_queue *q = current->throttle_queue;
1725*4882a593Smuzhiyun struct cgroup_subsys_state *css;
1726*4882a593Smuzhiyun struct blkcg *blkcg;
1727*4882a593Smuzhiyun struct blkcg_gq *blkg;
1728*4882a593Smuzhiyun bool use_memdelay = current->use_memdelay;
1729*4882a593Smuzhiyun
1730*4882a593Smuzhiyun if (!q)
1731*4882a593Smuzhiyun return;
1732*4882a593Smuzhiyun
1733*4882a593Smuzhiyun current->throttle_queue = NULL;
1734*4882a593Smuzhiyun current->use_memdelay = false;
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun rcu_read_lock();
1737*4882a593Smuzhiyun css = kthread_blkcg();
1738*4882a593Smuzhiyun if (css)
1739*4882a593Smuzhiyun blkcg = css_to_blkcg(css);
1740*4882a593Smuzhiyun else
1741*4882a593Smuzhiyun blkcg = css_to_blkcg(task_css(current, io_cgrp_id));
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun if (!blkcg)
1744*4882a593Smuzhiyun goto out;
1745*4882a593Smuzhiyun blkg = blkg_lookup(blkcg, q);
1746*4882a593Smuzhiyun if (!blkg)
1747*4882a593Smuzhiyun goto out;
1748*4882a593Smuzhiyun if (!blkg_tryget(blkg))
1749*4882a593Smuzhiyun goto out;
1750*4882a593Smuzhiyun rcu_read_unlock();
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun blkcg_maybe_throttle_blkg(blkg, use_memdelay);
1753*4882a593Smuzhiyun blkg_put(blkg);
1754*4882a593Smuzhiyun blk_put_queue(q);
1755*4882a593Smuzhiyun return;
1756*4882a593Smuzhiyun out:
1757*4882a593Smuzhiyun rcu_read_unlock();
1758*4882a593Smuzhiyun blk_put_queue(q);
1759*4882a593Smuzhiyun }
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun /**
1762*4882a593Smuzhiyun * blkcg_schedule_throttle - this task needs to check for throttling
1763*4882a593Smuzhiyun * @q: the request queue IO was submitted on
1764*4882a593Smuzhiyun * @use_memdelay: do we charge this to memory delay for PSI
1765*4882a593Smuzhiyun *
1766*4882a593Smuzhiyun * This is called by the IO controller when we know there's delay accumulated
1767*4882a593Smuzhiyun * for the blkg for this task. We do not pass the blkg because there are places
1768*4882a593Smuzhiyun * we call this that may not have that information, the swapping code for
1769*4882a593Smuzhiyun * instance will only have a request_queue at that point. This set's the
1770*4882a593Smuzhiyun * notify_resume for the task to check and see if it requires throttling before
1771*4882a593Smuzhiyun * returning to user space.
1772*4882a593Smuzhiyun *
1773*4882a593Smuzhiyun * We will only schedule once per syscall. You can call this over and over
1774*4882a593Smuzhiyun * again and it will only do the check once upon return to user space, and only
1775*4882a593Smuzhiyun * throttle once. If the task needs to be throttled again it'll need to be
1776*4882a593Smuzhiyun * re-set at the next time we see the task.
1777*4882a593Smuzhiyun */
blkcg_schedule_throttle(struct request_queue * q,bool use_memdelay)1778*4882a593Smuzhiyun void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay)
1779*4882a593Smuzhiyun {
1780*4882a593Smuzhiyun if (unlikely(current->flags & PF_KTHREAD))
1781*4882a593Smuzhiyun return;
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun if (!blk_get_queue(q))
1784*4882a593Smuzhiyun return;
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun if (current->throttle_queue)
1787*4882a593Smuzhiyun blk_put_queue(current->throttle_queue);
1788*4882a593Smuzhiyun current->throttle_queue = q;
1789*4882a593Smuzhiyun if (use_memdelay)
1790*4882a593Smuzhiyun current->use_memdelay = use_memdelay;
1791*4882a593Smuzhiyun set_notify_resume(current);
1792*4882a593Smuzhiyun }
1793*4882a593Smuzhiyun
1794*4882a593Smuzhiyun /**
1795*4882a593Smuzhiyun * blkcg_add_delay - add delay to this blkg
1796*4882a593Smuzhiyun * @blkg: blkg of interest
1797*4882a593Smuzhiyun * @now: the current time in nanoseconds
1798*4882a593Smuzhiyun * @delta: how many nanoseconds of delay to add
1799*4882a593Smuzhiyun *
1800*4882a593Smuzhiyun * Charge @delta to the blkg's current delay accumulation. This is used to
1801*4882a593Smuzhiyun * throttle tasks if an IO controller thinks we need more throttling.
1802*4882a593Smuzhiyun */
blkcg_add_delay(struct blkcg_gq * blkg,u64 now,u64 delta)1803*4882a593Smuzhiyun void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
1804*4882a593Smuzhiyun {
1805*4882a593Smuzhiyun if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
1806*4882a593Smuzhiyun return;
1807*4882a593Smuzhiyun blkcg_scale_delay(blkg, now);
1808*4882a593Smuzhiyun atomic64_add(delta, &blkg->delay_nsec);
1809*4882a593Smuzhiyun }
1810*4882a593Smuzhiyun
1811*4882a593Smuzhiyun /**
1812*4882a593Smuzhiyun * blkg_tryget_closest - try and get a blkg ref on the closet blkg
1813*4882a593Smuzhiyun * @bio: target bio
1814*4882a593Smuzhiyun * @css: target css
1815*4882a593Smuzhiyun *
1816*4882a593Smuzhiyun * As the failure mode here is to walk up the blkg tree, this ensure that the
1817*4882a593Smuzhiyun * blkg->parent pointers are always valid. This returns the blkg that it ended
1818*4882a593Smuzhiyun * up taking a reference on or %NULL if no reference was taken.
1819*4882a593Smuzhiyun */
blkg_tryget_closest(struct bio * bio,struct cgroup_subsys_state * css)1820*4882a593Smuzhiyun static inline struct blkcg_gq *blkg_tryget_closest(struct bio *bio,
1821*4882a593Smuzhiyun struct cgroup_subsys_state *css)
1822*4882a593Smuzhiyun {
1823*4882a593Smuzhiyun struct blkcg_gq *blkg, *ret_blkg = NULL;
1824*4882a593Smuzhiyun
1825*4882a593Smuzhiyun rcu_read_lock();
1826*4882a593Smuzhiyun blkg = blkg_lookup_create(css_to_blkcg(css), bio->bi_disk->queue);
1827*4882a593Smuzhiyun while (blkg) {
1828*4882a593Smuzhiyun if (blkg_tryget(blkg)) {
1829*4882a593Smuzhiyun ret_blkg = blkg;
1830*4882a593Smuzhiyun break;
1831*4882a593Smuzhiyun }
1832*4882a593Smuzhiyun blkg = blkg->parent;
1833*4882a593Smuzhiyun }
1834*4882a593Smuzhiyun rcu_read_unlock();
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun return ret_blkg;
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun
1839*4882a593Smuzhiyun /**
1840*4882a593Smuzhiyun * bio_associate_blkg_from_css - associate a bio with a specified css
1841*4882a593Smuzhiyun * @bio: target bio
1842*4882a593Smuzhiyun * @css: target css
1843*4882a593Smuzhiyun *
1844*4882a593Smuzhiyun * Associate @bio with the blkg found by combining the css's blkg and the
1845*4882a593Smuzhiyun * request_queue of the @bio. An association failure is handled by walking up
1846*4882a593Smuzhiyun * the blkg tree. Therefore, the blkg associated can be anything between @blkg
1847*4882a593Smuzhiyun * and q->root_blkg. This situation only happens when a cgroup is dying and
1848*4882a593Smuzhiyun * then the remaining bios will spill to the closest alive blkg.
1849*4882a593Smuzhiyun *
1850*4882a593Smuzhiyun * A reference will be taken on the blkg and will be released when @bio is
1851*4882a593Smuzhiyun * freed.
1852*4882a593Smuzhiyun */
bio_associate_blkg_from_css(struct bio * bio,struct cgroup_subsys_state * css)1853*4882a593Smuzhiyun void bio_associate_blkg_from_css(struct bio *bio,
1854*4882a593Smuzhiyun struct cgroup_subsys_state *css)
1855*4882a593Smuzhiyun {
1856*4882a593Smuzhiyun if (bio->bi_blkg)
1857*4882a593Smuzhiyun blkg_put(bio->bi_blkg);
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun if (css && css->parent) {
1860*4882a593Smuzhiyun bio->bi_blkg = blkg_tryget_closest(bio, css);
1861*4882a593Smuzhiyun } else {
1862*4882a593Smuzhiyun blkg_get(bio->bi_disk->queue->root_blkg);
1863*4882a593Smuzhiyun bio->bi_blkg = bio->bi_disk->queue->root_blkg;
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun }
1866*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun /**
1869*4882a593Smuzhiyun * bio_associate_blkg - associate a bio with a blkg
1870*4882a593Smuzhiyun * @bio: target bio
1871*4882a593Smuzhiyun *
1872*4882a593Smuzhiyun * Associate @bio with the blkg found from the bio's css and request_queue.
1873*4882a593Smuzhiyun * If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is
1874*4882a593Smuzhiyun * already associated, the css is reused and association redone as the
1875*4882a593Smuzhiyun * request_queue may have changed.
1876*4882a593Smuzhiyun */
bio_associate_blkg(struct bio * bio)1877*4882a593Smuzhiyun void bio_associate_blkg(struct bio *bio)
1878*4882a593Smuzhiyun {
1879*4882a593Smuzhiyun struct cgroup_subsys_state *css;
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun rcu_read_lock();
1882*4882a593Smuzhiyun
1883*4882a593Smuzhiyun if (bio->bi_blkg)
1884*4882a593Smuzhiyun css = &bio_blkcg(bio)->css;
1885*4882a593Smuzhiyun else
1886*4882a593Smuzhiyun css = blkcg_css();
1887*4882a593Smuzhiyun
1888*4882a593Smuzhiyun bio_associate_blkg_from_css(bio, css);
1889*4882a593Smuzhiyun
1890*4882a593Smuzhiyun rcu_read_unlock();
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bio_associate_blkg);
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun /**
1895*4882a593Smuzhiyun * bio_clone_blkg_association - clone blkg association from src to dst bio
1896*4882a593Smuzhiyun * @dst: destination bio
1897*4882a593Smuzhiyun * @src: source bio
1898*4882a593Smuzhiyun */
bio_clone_blkg_association(struct bio * dst,struct bio * src)1899*4882a593Smuzhiyun void bio_clone_blkg_association(struct bio *dst, struct bio *src)
1900*4882a593Smuzhiyun {
1901*4882a593Smuzhiyun if (src->bi_blkg)
1902*4882a593Smuzhiyun bio_associate_blkg_from_css(dst, &bio_blkcg(src)->css);
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
1905*4882a593Smuzhiyun
blk_cgroup_io_type(struct bio * bio)1906*4882a593Smuzhiyun static int blk_cgroup_io_type(struct bio *bio)
1907*4882a593Smuzhiyun {
1908*4882a593Smuzhiyun if (op_is_discard(bio->bi_opf))
1909*4882a593Smuzhiyun return BLKG_IOSTAT_DISCARD;
1910*4882a593Smuzhiyun if (op_is_write(bio->bi_opf))
1911*4882a593Smuzhiyun return BLKG_IOSTAT_WRITE;
1912*4882a593Smuzhiyun return BLKG_IOSTAT_READ;
1913*4882a593Smuzhiyun }
1914*4882a593Smuzhiyun
blk_cgroup_bio_start(struct bio * bio)1915*4882a593Smuzhiyun void blk_cgroup_bio_start(struct bio *bio)
1916*4882a593Smuzhiyun {
1917*4882a593Smuzhiyun int rwd = blk_cgroup_io_type(bio), cpu;
1918*4882a593Smuzhiyun struct blkg_iostat_set *bis;
1919*4882a593Smuzhiyun
1920*4882a593Smuzhiyun cpu = get_cpu();
1921*4882a593Smuzhiyun bis = per_cpu_ptr(bio->bi_blkg->iostat_cpu, cpu);
1922*4882a593Smuzhiyun u64_stats_update_begin(&bis->sync);
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun /*
1925*4882a593Smuzhiyun * If the bio is flagged with BIO_CGROUP_ACCT it means this is a split
1926*4882a593Smuzhiyun * bio and we would have already accounted for the size of the bio.
1927*4882a593Smuzhiyun */
1928*4882a593Smuzhiyun if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
1929*4882a593Smuzhiyun bio_set_flag(bio, BIO_CGROUP_ACCT);
1930*4882a593Smuzhiyun bis->cur.bytes[rwd] += bio->bi_iter.bi_size;
1931*4882a593Smuzhiyun }
1932*4882a593Smuzhiyun bis->cur.ios[rwd]++;
1933*4882a593Smuzhiyun
1934*4882a593Smuzhiyun u64_stats_update_end(&bis->sync);
1935*4882a593Smuzhiyun if (cgroup_subsys_on_dfl(io_cgrp_subsys))
1936*4882a593Smuzhiyun cgroup_rstat_updated(bio->bi_blkg->blkcg->css.cgroup, cpu);
1937*4882a593Smuzhiyun put_cpu();
1938*4882a593Smuzhiyun }
1939*4882a593Smuzhiyun
blkcg_init(void)1940*4882a593Smuzhiyun static int __init blkcg_init(void)
1941*4882a593Smuzhiyun {
1942*4882a593Smuzhiyun blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",
1943*4882a593Smuzhiyun WQ_MEM_RECLAIM | WQ_FREEZABLE |
1944*4882a593Smuzhiyun WQ_UNBOUND | WQ_SYSFS, 0);
1945*4882a593Smuzhiyun if (!blkcg_punt_bio_wq)
1946*4882a593Smuzhiyun return -ENOMEM;
1947*4882a593Smuzhiyun return 0;
1948*4882a593Smuzhiyun }
1949*4882a593Smuzhiyun subsys_initcall(blkcg_init);
1950*4882a593Smuzhiyun
1951*4882a593Smuzhiyun module_param(blkcg_debug_stats, bool, 0644);
1952*4882a593Smuzhiyun MODULE_PARM_DESC(blkcg_debug_stats, "True if you want debug stats, false if not");
1953