13dcf60bcSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
231e4c28dSVivek Goyal /*
331e4c28dSVivek Goyal * Common Block IO controller cgroup interface
431e4c28dSVivek Goyal *
531e4c28dSVivek Goyal * Based on ideas and code from CFQ, CFS and BFQ:
631e4c28dSVivek Goyal * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
731e4c28dSVivek Goyal *
831e4c28dSVivek Goyal * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
931e4c28dSVivek Goyal * Paolo Valente <paolo.valente@unimore.it>
1031e4c28dSVivek Goyal *
1131e4c28dSVivek Goyal * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
1231e4c28dSVivek Goyal * Nauman Rafique <nauman@google.com>
13e48453c3SArianna Avanzini *
14e48453c3SArianna Avanzini * For policy-specific per-blkcg data:
15e48453c3SArianna Avanzini * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
16e48453c3SArianna Avanzini * Arianna Avanzini <avanzini.arianna@gmail.com>
1731e4c28dSVivek Goyal */
1831e4c28dSVivek Goyal #include <linux/ioprio.h>
1922084190SVivek Goyal #include <linux/kdev_t.h>
209d6a986cSVivek Goyal #include <linux/module.h>
21174cd4b1SIngo Molnar #include <linux/sched/signal.h>
22accee785SStephen Rothwell #include <linux/err.h>
239195291eSDivyesh Shah #include <linux/blkdev.h>
2452ebea74STejun Heo #include <linux/backing-dev.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2672e06c25STejun Heo #include <linux/delay.h>
279a9e8a26STejun Heo #include <linux/atomic.h>
2836aa9e5fSTejun Heo #include <linux/ctype.h>
2903248addSEric W. Biederman #include <linux/resume_user_mode.h>
30fd112c74SJosef Bacik #include <linux/psi.h>
3182d981d4SChristoph Hellwig #include <linux/part_stat.h>
325efd6113STejun Heo #include "blk.h"
33672fdcf0SMing Lei #include "blk-cgroup.h"
34556910e3SBart Van Assche #include "blk-ioprio.h"
35a7b36ee6SJens Axboe #include "blk-throttle.h"
363e252066SVivek Goyal
3720cb1c2fSMing Lei static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu);
3820cb1c2fSMing Lei
39838f13bfSTejun Heo /*
40838f13bfSTejun Heo * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
41838f13bfSTejun Heo * blkcg_pol_register_mutex nests outside of it and synchronizes entire
42838f13bfSTejun Heo * policy [un]register operations including cgroup file additions /
43838f13bfSTejun Heo * removals. Putting cgroup file registration outside blkcg_pol_mutex
44838f13bfSTejun Heo * allows grabbing it from cgroup callbacks.
45838f13bfSTejun Heo */
46838f13bfSTejun Heo static DEFINE_MUTEX(blkcg_pol_register_mutex);
47bc0d6501STejun Heo static DEFINE_MUTEX(blkcg_pol_mutex);
48923adde1STejun Heo
49e48453c3SArianna Avanzini struct blkcg blkcg_root;
503c798398STejun Heo EXPORT_SYMBOL_GPL(blkcg_root);
519d6a986cSVivek Goyal
52496d5e75STejun Heo struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
539b0eb69bSTejun Heo EXPORT_SYMBOL_GPL(blkcg_root_css);
54496d5e75STejun Heo
553c798398STejun Heo static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
56035d10b2STejun Heo
577876f930STejun Heo static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */
587876f930STejun Heo
5907b0fdecSTejun Heo bool blkcg_debug_stats = false;
60903d23f0SJosef Bacik
6120cb1c2fSMing Lei static DEFINE_RAW_SPINLOCK(blkg_stat_lock);
6220cb1c2fSMing Lei
63a731763fSYu Kuai #define BLKG_DESTROY_BATCH_SIZE 64
64a731763fSYu Kuai
653b8cc629SWaiman Long /*
663b8cc629SWaiman Long * Lockless lists for tracking IO stats update
673b8cc629SWaiman Long *
683b8cc629SWaiman Long * New IO stats are stored in the percpu iostat_cpu within blkcg_gq (blkg).
693b8cc629SWaiman Long * There are multiple blkg's (one for each block device) attached to each
703b8cc629SWaiman Long * blkcg. The rstat code keeps track of which cpu has IO stats updated,
713b8cc629SWaiman Long * but it doesn't know which blkg has the updated stats. If there are many
723b8cc629SWaiman Long * block devices in a system, the cost of iterating all the blkg's to flush
733b8cc629SWaiman Long * out the IO stats can be high. To reduce such overhead, a set of percpu
743b8cc629SWaiman Long * lockless lists (lhead) per blkcg are used to track the set of recently
753b8cc629SWaiman Long * updated iostat_cpu's since the last flush. An iostat_cpu will be put
763b8cc629SWaiman Long * onto the lockless list on the update side [blk_cgroup_bio_start()] if
773b8cc629SWaiman Long * not there yet and then removed when being flushed [blkcg_rstat_flush()].
783b8cc629SWaiman Long * References to blkg are gotten and then put back in the process to
793b8cc629SWaiman Long * protect against blkg removal.
803b8cc629SWaiman Long *
813b8cc629SWaiman Long * Return: 0 if successful or -ENOMEM if allocation fails.
823b8cc629SWaiman Long */
init_blkcg_llists(struct blkcg * blkcg)833b8cc629SWaiman Long static int init_blkcg_llists(struct blkcg *blkcg)
843b8cc629SWaiman Long {
853b8cc629SWaiman Long int cpu;
863b8cc629SWaiman Long
873b8cc629SWaiman Long blkcg->lhead = alloc_percpu_gfp(struct llist_head, GFP_KERNEL);
883b8cc629SWaiman Long if (!blkcg->lhead)
893b8cc629SWaiman Long return -ENOMEM;
903b8cc629SWaiman Long
913b8cc629SWaiman Long for_each_possible_cpu(cpu)
923b8cc629SWaiman Long init_llist_head(per_cpu_ptr(blkcg->lhead, cpu));
933b8cc629SWaiman Long return 0;
943b8cc629SWaiman Long }
953b8cc629SWaiman Long
96bc5fee91SChristoph Hellwig /**
97bc5fee91SChristoph Hellwig * blkcg_css - find the current css
98bc5fee91SChristoph Hellwig *
99bc5fee91SChristoph Hellwig * Find the css associated with either the kthread or the current task.
100bc5fee91SChristoph Hellwig * This may return a dying css, so it is up to the caller to use tryget logic
101bc5fee91SChristoph Hellwig * to confirm it is alive and well.
102bc5fee91SChristoph Hellwig */
blkcg_css(void)103bc5fee91SChristoph Hellwig static struct cgroup_subsys_state *blkcg_css(void)
104bc5fee91SChristoph Hellwig {
105bc5fee91SChristoph Hellwig struct cgroup_subsys_state *css;
106bc5fee91SChristoph Hellwig
107bc5fee91SChristoph Hellwig css = kthread_blkcg();
108bc5fee91SChristoph Hellwig if (css)
109bc5fee91SChristoph Hellwig return css;
110bc5fee91SChristoph Hellwig return task_css(current, io_cgrp_id);
111bc5fee91SChristoph Hellwig }
112bc5fee91SChristoph Hellwig
blkcg_policy_enabled(struct request_queue * q,const struct blkcg_policy * pol)113a2b1693bSTejun Heo static bool blkcg_policy_enabled(struct request_queue *q,
1143c798398STejun Heo const struct blkcg_policy *pol)
115a2b1693bSTejun Heo {
116a2b1693bSTejun Heo return pol && test_bit(pol->plid, q->blkcg_pols);
117a2b1693bSTejun Heo }
118a2b1693bSTejun Heo
blkg_free_workfn(struct work_struct * work)119d578c770SMing Lei static void blkg_free_workfn(struct work_struct *work)
1200381411eSTejun Heo {
121d578c770SMing Lei struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
122d578c770SMing Lei free_work);
123a06377c5SChristoph Hellwig struct request_queue *q = blkg->q;
124e8989faeSTejun Heo int i;
125549d3aa8STejun Heo
126f1c006f1SYu Kuai /*
127f1c006f1SYu Kuai * pd_free_fn() can also be called from blkcg_deactivate_policy(),
128f1c006f1SYu Kuai * in order to make sure pd_free_fn() is called in order, the deletion
1291231039dSChristoph Hellwig * of the list blkg->q_node is delayed to here from blkg_destroy(), and
130f1c006f1SYu Kuai * blkcg_mutex is used to synchronize blkg_free_workfn() and
131f1c006f1SYu Kuai * blkcg_deactivate_policy().
132f1c006f1SYu Kuai */
133a06377c5SChristoph Hellwig mutex_lock(&q->blkcg_mutex);
134db613670STejun Heo for (i = 0; i < BLKCG_MAX_POLS; i++)
135001bea73STejun Heo if (blkg->pd[i])
136001bea73STejun Heo blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
137c7241babSYu Kuai if (blkg->parent)
138c7241babSYu Kuai blkg_put(blkg->parent);
139c164c7bcSMing Lei spin_lock_irq(&q->queue_lock);
1401231039dSChristoph Hellwig list_del_init(&blkg->q_node);
141c164c7bcSMing Lei spin_unlock_irq(&q->queue_lock);
142a06377c5SChristoph Hellwig mutex_unlock(&q->blkcg_mutex);
143e8989faeSTejun Heo
144a06377c5SChristoph Hellwig blk_put_queue(q);
145f7331648STejun Heo free_percpu(blkg->iostat_cpu);
146ef069b97STejun Heo percpu_ref_exit(&blkg->refcnt);
147549d3aa8STejun Heo kfree(blkg);
1480381411eSTejun Heo }
1490381411eSTejun Heo
150d578c770SMing Lei /**
151d578c770SMing Lei * blkg_free - free a blkg
152d578c770SMing Lei * @blkg: blkg to free
153d578c770SMing Lei *
154d578c770SMing Lei * Free @blkg which may be partially allocated.
155d578c770SMing Lei */
blkg_free(struct blkcg_gq * blkg)156d578c770SMing Lei static void blkg_free(struct blkcg_gq *blkg)
157d578c770SMing Lei {
158d578c770SMing Lei if (!blkg)
159d578c770SMing Lei return;
160d578c770SMing Lei
161d578c770SMing Lei /*
162d578c770SMing Lei * Both ->pd_free_fn() and request queue's release handler may
163d578c770SMing Lei * sleep, so free us by scheduling one work func
164d578c770SMing Lei */
165d578c770SMing Lei INIT_WORK(&blkg->free_work, blkg_free_workfn);
166d578c770SMing Lei schedule_work(&blkg->free_work);
167d578c770SMing Lei }
168d578c770SMing Lei
__blkg_release(struct rcu_head * rcu)1697fcf2b03SDennis Zhou static void __blkg_release(struct rcu_head *rcu)
1707fcf2b03SDennis Zhou {
1717fcf2b03SDennis Zhou struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
17220cb1c2fSMing Lei struct blkcg *blkcg = blkg->blkcg;
17320cb1c2fSMing Lei int cpu;
1747fcf2b03SDennis Zhou
1752c275afeSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
176d3f77dfdSTejun Heo WARN_ON(!bio_list_empty(&blkg->async_bios));
1772c275afeSChristoph Hellwig #endif
17820cb1c2fSMing Lei /*
17920cb1c2fSMing Lei * Flush all the non-empty percpu lockless lists before releasing
18020cb1c2fSMing Lei * us, given these stat belongs to us.
18120cb1c2fSMing Lei *
18220cb1c2fSMing Lei * blkg_stat_lock is for serializing blkg stat update
18320cb1c2fSMing Lei */
18420cb1c2fSMing Lei for_each_possible_cpu(cpu)
18520cb1c2fSMing Lei __blkcg_rstat_flush(blkcg, cpu);
186d3f77dfdSTejun Heo
1877fcf2b03SDennis Zhou /* release the blkcg and parent blkg refs this blkg has been holding */
1887fcf2b03SDennis Zhou css_put(&blkg->blkcg->css);
1897fcf2b03SDennis Zhou blkg_free(blkg);
1907fcf2b03SDennis Zhou }
1917fcf2b03SDennis Zhou
1927fcf2b03SDennis Zhou /*
1937fcf2b03SDennis Zhou * A group is RCU protected, but having an rcu lock does not mean that one
1947fcf2b03SDennis Zhou * can access all the fields of blkg and assume these are valid. For
1957fcf2b03SDennis Zhou * example, don't try to follow throtl_data and request queue links.
1967fcf2b03SDennis Zhou *
1977fcf2b03SDennis Zhou * Having a reference to blkg under an rcu allows accesses to only values
1987fcf2b03SDennis Zhou * local to groups like group stats and group rate limits.
1997fcf2b03SDennis Zhou */
blkg_release(struct percpu_ref * ref)2007fcf2b03SDennis Zhou static void blkg_release(struct percpu_ref *ref)
2017fcf2b03SDennis Zhou {
2027fcf2b03SDennis Zhou struct blkcg_gq *blkg = container_of(ref, struct blkcg_gq, refcnt);
2037fcf2b03SDennis Zhou
2047fcf2b03SDennis Zhou call_rcu(&blkg->rcu_head, __blkg_release);
2057fcf2b03SDennis Zhou }
2067fcf2b03SDennis Zhou
2072c275afeSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
2082c275afeSChristoph Hellwig static struct workqueue_struct *blkcg_punt_bio_wq;
2092c275afeSChristoph Hellwig
blkg_async_bio_workfn(struct work_struct * work)210d3f77dfdSTejun Heo static void blkg_async_bio_workfn(struct work_struct *work)
211d3f77dfdSTejun Heo {
212d3f77dfdSTejun Heo struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
213d3f77dfdSTejun Heo async_bio_work);
214d3f77dfdSTejun Heo struct bio_list bios = BIO_EMPTY_LIST;
215d3f77dfdSTejun Heo struct bio *bio;
216192f1c6bSXianting Tian struct blk_plug plug;
217192f1c6bSXianting Tian bool need_plug = false;
218d3f77dfdSTejun Heo
219d3f77dfdSTejun Heo /* as long as there are pending bios, @blkg can't go away */
22012be09feSChristoph Hellwig spin_lock(&blkg->async_bio_lock);
221d3f77dfdSTejun Heo bio_list_merge(&bios, &blkg->async_bios);
222d3f77dfdSTejun Heo bio_list_init(&blkg->async_bios);
22312be09feSChristoph Hellwig spin_unlock(&blkg->async_bio_lock);
224d3f77dfdSTejun Heo
225192f1c6bSXianting Tian /* start plug only when bio_list contains at least 2 bios */
226192f1c6bSXianting Tian if (bios.head && bios.head->bi_next) {
227192f1c6bSXianting Tian need_plug = true;
228192f1c6bSXianting Tian blk_start_plug(&plug);
229192f1c6bSXianting Tian }
230d3f77dfdSTejun Heo while ((bio = bio_list_pop(&bios)))
231d3f77dfdSTejun Heo submit_bio(bio);
232192f1c6bSXianting Tian if (need_plug)
233192f1c6bSXianting Tian blk_finish_plug(&plug);
234d3f77dfdSTejun Heo }
235d3f77dfdSTejun Heo
2362c275afeSChristoph Hellwig /*
2372c275afeSChristoph Hellwig * When a shared kthread issues a bio for a cgroup, doing so synchronously can
2382c275afeSChristoph Hellwig * lead to priority inversions as the kthread can be trapped waiting for that
2392c275afeSChristoph Hellwig * cgroup. Use this helper instead of submit_bio to punt the actual issuing to
2402c275afeSChristoph Hellwig * a dedicated per-blkcg work item to avoid such priority inversions.
2412c275afeSChristoph Hellwig */
blkcg_punt_bio_submit(struct bio * bio)2422c275afeSChristoph Hellwig void blkcg_punt_bio_submit(struct bio *bio)
2432c275afeSChristoph Hellwig {
2442c275afeSChristoph Hellwig struct blkcg_gq *blkg = bio->bi_blkg;
2452c275afeSChristoph Hellwig
2462c275afeSChristoph Hellwig if (blkg->parent) {
2472c275afeSChristoph Hellwig spin_lock(&blkg->async_bio_lock);
2482c275afeSChristoph Hellwig bio_list_add(&blkg->async_bios, bio);
2492c275afeSChristoph Hellwig spin_unlock(&blkg->async_bio_lock);
2502c275afeSChristoph Hellwig queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work);
2512c275afeSChristoph Hellwig } else {
2522c275afeSChristoph Hellwig /* never bounce for the root cgroup */
2532c275afeSChristoph Hellwig submit_bio(bio);
2542c275afeSChristoph Hellwig }
2552c275afeSChristoph Hellwig }
2562c275afeSChristoph Hellwig EXPORT_SYMBOL_GPL(blkcg_punt_bio_submit);
2572c275afeSChristoph Hellwig
blkcg_punt_bio_init(void)2582c275afeSChristoph Hellwig static int __init blkcg_punt_bio_init(void)
2592c275afeSChristoph Hellwig {
2602c275afeSChristoph Hellwig blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",
2612c275afeSChristoph Hellwig WQ_MEM_RECLAIM | WQ_FREEZABLE |
2622c275afeSChristoph Hellwig WQ_UNBOUND | WQ_SYSFS, 0);
2632c275afeSChristoph Hellwig if (!blkcg_punt_bio_wq)
2642c275afeSChristoph Hellwig return -ENOMEM;
2652c275afeSChristoph Hellwig return 0;
2662c275afeSChristoph Hellwig }
2672c275afeSChristoph Hellwig subsys_initcall(blkcg_punt_bio_init);
2682c275afeSChristoph Hellwig #endif /* CONFIG_BLK_CGROUP_PUNT_BIO */
2692c275afeSChristoph Hellwig
2700381411eSTejun Heo /**
271bbb1ebe7SChristoph Hellwig * bio_blkcg_css - return the blkcg CSS associated with a bio
272bbb1ebe7SChristoph Hellwig * @bio: target bio
273bbb1ebe7SChristoph Hellwig *
274bbb1ebe7SChristoph Hellwig * This returns the CSS for the blkcg associated with a bio, or %NULL if not
275bbb1ebe7SChristoph Hellwig * associated. Callers are expected to either handle %NULL or know association
276bbb1ebe7SChristoph Hellwig * has been done prior to calling this.
277bbb1ebe7SChristoph Hellwig */
bio_blkcg_css(struct bio * bio)278bbb1ebe7SChristoph Hellwig struct cgroup_subsys_state *bio_blkcg_css(struct bio *bio)
279bbb1ebe7SChristoph Hellwig {
280bbb1ebe7SChristoph Hellwig if (!bio || !bio->bi_blkg)
281bbb1ebe7SChristoph Hellwig return NULL;
282bbb1ebe7SChristoph Hellwig return &bio->bi_blkg->blkcg->css;
283bbb1ebe7SChristoph Hellwig }
284bbb1ebe7SChristoph Hellwig EXPORT_SYMBOL_GPL(bio_blkcg_css);
285bbb1ebe7SChristoph Hellwig
286bbb1ebe7SChristoph Hellwig /**
287397c9f46SChristoph Hellwig * blkcg_parent - get the parent of a blkcg
288397c9f46SChristoph Hellwig * @blkcg: blkcg of interest
289397c9f46SChristoph Hellwig *
290397c9f46SChristoph Hellwig * Return the parent blkcg of @blkcg. Can be called anytime.
291397c9f46SChristoph Hellwig */
blkcg_parent(struct blkcg * blkcg)292397c9f46SChristoph Hellwig static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
293397c9f46SChristoph Hellwig {
294397c9f46SChristoph Hellwig return css_to_blkcg(blkcg->css.parent);
295397c9f46SChristoph Hellwig }
296397c9f46SChristoph Hellwig
297397c9f46SChristoph Hellwig /**
2980381411eSTejun Heo * blkg_alloc - allocate a blkg
2990381411eSTejun Heo * @blkcg: block cgroup the new blkg is associated with
30099e60387SChristoph Hellwig * @disk: gendisk the new blkg is associated with
30115974993STejun Heo * @gfp_mask: allocation mask to use
3020381411eSTejun Heo *
303e8989faeSTejun Heo * Allocate a new blkg assocating @blkcg and @q.
3040381411eSTejun Heo */
blkg_alloc(struct blkcg * blkcg,struct gendisk * disk,gfp_t gfp_mask)30599e60387SChristoph Hellwig static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
30615974993STejun Heo gfp_t gfp_mask)
3070381411eSTejun Heo {
3083c798398STejun Heo struct blkcg_gq *blkg;
309f7331648STejun Heo int i, cpu;
3100381411eSTejun Heo
3110381411eSTejun Heo /* alloc and init base part */
31299e60387SChristoph Hellwig blkg = kzalloc_node(sizeof(*blkg), gfp_mask, disk->queue->node);
3130381411eSTejun Heo if (!blkg)
3140381411eSTejun Heo return NULL;
315ef069b97STejun Heo if (percpu_ref_init(&blkg->refcnt, blkg_release, 0, gfp_mask))
3160b6f93bdSChristoph Hellwig goto out_free_blkg;
317f7331648STejun Heo blkg->iostat_cpu = alloc_percpu_gfp(struct blkg_iostat_set, gfp_mask);
318f7331648STejun Heo if (!blkg->iostat_cpu)
3190b6f93bdSChristoph Hellwig goto out_exit_refcnt;
32099e60387SChristoph Hellwig if (!blk_get_queue(disk->queue))
32184d7d462SChristoph Hellwig goto out_free_iostat;
3220a9a25caSMing Lei
32399e60387SChristoph Hellwig blkg->q = disk->queue;
324e8989faeSTejun Heo INIT_LIST_HEAD(&blkg->q_node);
3252c275afeSChristoph Hellwig blkg->blkcg = blkcg;
326b1bee993SWaiman Long blkg->iostat.blkg = blkg;
3272c275afeSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
328d3f77dfdSTejun Heo spin_lock_init(&blkg->async_bio_lock);
329d3f77dfdSTejun Heo bio_list_init(&blkg->async_bios);
330d3f77dfdSTejun Heo INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn);
3312c275afeSChristoph Hellwig #endif
3320381411eSTejun Heo
333f7331648STejun Heo u64_stats_init(&blkg->iostat.sync);
3343b8cc629SWaiman Long for_each_possible_cpu(cpu) {
335f7331648STejun Heo u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync);
3363b8cc629SWaiman Long per_cpu_ptr(blkg->iostat_cpu, cpu)->blkg = blkg;
3373b8cc629SWaiman Long }
338f7331648STejun Heo
3398bd435b3STejun Heo for (i = 0; i < BLKCG_MAX_POLS; i++) {
3403c798398STejun Heo struct blkcg_policy *pol = blkcg_policy[i];
341e8989faeSTejun Heo struct blkg_policy_data *pd;
342e8989faeSTejun Heo
34399e60387SChristoph Hellwig if (!blkcg_policy_enabled(disk->queue, pol))
344e8989faeSTejun Heo continue;
345e8989faeSTejun Heo
346549d3aa8STejun Heo /* alloc per-policy data and attach it to blkg */
3470a0b4f79SChristoph Hellwig pd = pol->pd_alloc_fn(disk, blkcg, gfp_mask);
348a051661cSTejun Heo if (!pd)
3490b6f93bdSChristoph Hellwig goto out_free_pds;
350e8989faeSTejun Heo blkg->pd[i] = pd;
351549d3aa8STejun Heo pd->blkg = blkg;
352b276a876STejun Heo pd->plid = i;
353dfd6200aSYu Kuai pd->online = false;
354e8989faeSTejun Heo }
355e8989faeSTejun Heo
3560381411eSTejun Heo return blkg;
357a051661cSTejun Heo
3580b6f93bdSChristoph Hellwig out_free_pds:
3590b6f93bdSChristoph Hellwig while (--i >= 0)
3600b6f93bdSChristoph Hellwig if (blkg->pd[i])
3610b6f93bdSChristoph Hellwig blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
362a06377c5SChristoph Hellwig blk_put_queue(disk->queue);
3630b6f93bdSChristoph Hellwig out_free_iostat:
3640b6f93bdSChristoph Hellwig free_percpu(blkg->iostat_cpu);
3650b6f93bdSChristoph Hellwig out_exit_refcnt:
3660b6f93bdSChristoph Hellwig percpu_ref_exit(&blkg->refcnt);
3670b6f93bdSChristoph Hellwig out_free_blkg:
3680b6f93bdSChristoph Hellwig kfree(blkg);
369a051661cSTejun Heo return NULL;
3700381411eSTejun Heo }
3710381411eSTejun Heo
37215974993STejun Heo /*
373d708f0d5SJens Axboe * If @new_blkg is %NULL, this function tries to allocate a new one as
374d708f0d5SJens Axboe * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
37515974993STejun Heo */
blkg_create(struct blkcg * blkcg,struct gendisk * disk,struct blkcg_gq * new_blkg)37699e60387SChristoph Hellwig static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
377d708f0d5SJens Axboe struct blkcg_gq *new_blkg)
378cd1604faSTejun Heo {
379d708f0d5SJens Axboe struct blkcg_gq *blkg;
380f427d909STejun Heo int i, ret;
381cd1604faSTejun Heo
38299e60387SChristoph Hellwig lockdep_assert_held(&disk->queue->queue_lock);
383cd1604faSTejun Heo
3840273ac34SDennis Zhou /* request_queue is dying, do not create/recreate a blkg */
38599e60387SChristoph Hellwig if (blk_queue_dying(disk->queue)) {
3860273ac34SDennis Zhou ret = -ENODEV;
3870273ac34SDennis Zhou goto err_free_blkg;
3880273ac34SDennis Zhou }
3890273ac34SDennis Zhou
3907ee9c562STejun Heo /* blkg holds a reference to blkcg */
391ec903c0cSTejun Heo if (!css_tryget_online(&blkcg->css)) {
39220386ce0STejun Heo ret = -ENODEV;
39393e6d5d8STejun Heo goto err_free_blkg;
39415974993STejun Heo }
395cd1604faSTejun Heo
396d708f0d5SJens Axboe /* allocate */
397d708f0d5SJens Axboe if (!new_blkg) {
39899e60387SChristoph Hellwig new_blkg = blkg_alloc(blkcg, disk, GFP_NOWAIT | __GFP_NOWARN);
399d708f0d5SJens Axboe if (unlikely(!new_blkg)) {
400d708f0d5SJens Axboe ret = -ENOMEM;
4018c911f3dSChristoph Hellwig goto err_put_css;
402d708f0d5SJens Axboe }
403d708f0d5SJens Axboe }
404d708f0d5SJens Axboe blkg = new_blkg;
405cd1604faSTejun Heo
406db613670STejun Heo /* link parent */
4073c547865STejun Heo if (blkcg_parent(blkcg)) {
40899e60387SChristoph Hellwig blkg->parent = blkg_lookup(blkcg_parent(blkcg), disk->queue);
4093c547865STejun Heo if (WARN_ON_ONCE(!blkg->parent)) {
41020386ce0STejun Heo ret = -ENODEV;
4118c911f3dSChristoph Hellwig goto err_put_css;
4123c547865STejun Heo }
4133c547865STejun Heo blkg_get(blkg->parent);
4143c547865STejun Heo }
4153c547865STejun Heo
416db613670STejun Heo /* invoke per-policy init */
417db613670STejun Heo for (i = 0; i < BLKCG_MAX_POLS; i++) {
418db613670STejun Heo struct blkcg_policy *pol = blkcg_policy[i];
419db613670STejun Heo
420db613670STejun Heo if (blkg->pd[i] && pol->pd_init_fn)
421a9520cd6STejun Heo pol->pd_init_fn(blkg->pd[i]);
422db613670STejun Heo }
423db613670STejun Heo
424db613670STejun Heo /* insert */
425cd1604faSTejun Heo spin_lock(&blkcg->lock);
42699e60387SChristoph Hellwig ret = radix_tree_insert(&blkcg->blkg_tree, disk->queue->id, blkg);
427a637120eSTejun Heo if (likely(!ret)) {
42831e4c28dSVivek Goyal hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
42999e60387SChristoph Hellwig list_add(&blkg->q_node, &disk->queue->blkg_list);
430f427d909STejun Heo
431f427d909STejun Heo for (i = 0; i < BLKCG_MAX_POLS; i++) {
432f427d909STejun Heo struct blkcg_policy *pol = blkcg_policy[i];
433f427d909STejun Heo
434dfd6200aSYu Kuai if (blkg->pd[i]) {
435dfd6200aSYu Kuai if (pol->pd_online_fn)
436a9520cd6STejun Heo pol->pd_online_fn(blkg->pd[i]);
437dfd6200aSYu Kuai blkg->pd[i]->online = true;
438dfd6200aSYu Kuai }
439a637120eSTejun Heo }
440f427d909STejun Heo }
441f427d909STejun Heo blkg->online = true;
442cd1604faSTejun Heo spin_unlock(&blkcg->lock);
443496fb780STejun Heo
444ec13b1d6STejun Heo if (!ret)
445a637120eSTejun Heo return blkg;
44615974993STejun Heo
4473c547865STejun Heo /* @blkg failed fully initialized, use the usual release path */
4483c547865STejun Heo blkg_put(blkg);
4493c547865STejun Heo return ERR_PTR(ret);
4503c547865STejun Heo
451d708f0d5SJens Axboe err_put_css:
452496fb780STejun Heo css_put(&blkcg->css);
45393e6d5d8STejun Heo err_free_blkg:
45428e538a3SChristoph Hellwig if (new_blkg)
455d708f0d5SJens Axboe blkg_free(new_blkg);
45693e6d5d8STejun Heo return ERR_PTR(ret);
457cd1604faSTejun Heo }
4583c96cb32STejun Heo
45986cde6b6STejun Heo /**
4608c546287SChristoph Hellwig * blkg_lookup_create - lookup blkg, try to create one if not there
46186cde6b6STejun Heo * @blkcg: blkcg of interest
46299e60387SChristoph Hellwig * @disk: gendisk of interest
46386cde6b6STejun Heo *
46499e60387SChristoph Hellwig * Lookup blkg for the @blkcg - @disk pair. If it doesn't exist, try to
4653c547865STejun Heo * create one. blkg creation is performed recursively from blkcg_root such
4663c547865STejun Heo * that all non-root blkg's have access to the parent blkg. This function
46799e60387SChristoph Hellwig * should be called under RCU read lock and takes @disk->queue->queue_lock.
46886cde6b6STejun Heo *
469beea9da0SDennis Zhou * Returns the blkg or the closest blkg if blkg_create() fails as it walks
470beea9da0SDennis Zhou * down from root.
47186cde6b6STejun Heo */
blkg_lookup_create(struct blkcg * blkcg,struct gendisk * disk)4728c546287SChristoph Hellwig static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
47399e60387SChristoph Hellwig struct gendisk *disk)
4743c96cb32STejun Heo {
47599e60387SChristoph Hellwig struct request_queue *q = disk->queue;
47686cde6b6STejun Heo struct blkcg_gq *blkg;
4778c546287SChristoph Hellwig unsigned long flags;
47886cde6b6STejun Heo
47986cde6b6STejun Heo WARN_ON_ONCE(!rcu_read_lock_held());
48086cde6b6STejun Heo
4818c546287SChristoph Hellwig blkg = blkg_lookup(blkcg, q);
48286cde6b6STejun Heo if (blkg)
48386cde6b6STejun Heo return blkg;
48486cde6b6STejun Heo
4858c546287SChristoph Hellwig spin_lock_irqsave(&q->queue_lock, flags);
4864a69f325SChristoph Hellwig blkg = blkg_lookup(blkcg, q);
4874a69f325SChristoph Hellwig if (blkg) {
4885765033cSChristoph Hellwig if (blkcg != &blkcg_root &&
4895765033cSChristoph Hellwig blkg != rcu_dereference(blkcg->blkg_hint))
4905765033cSChristoph Hellwig rcu_assign_pointer(blkcg->blkg_hint, blkg);
4918c546287SChristoph Hellwig goto found;
4924a69f325SChristoph Hellwig }
4938c546287SChristoph Hellwig
4943c547865STejun Heo /*
4953c547865STejun Heo * Create blkgs walking down from blkcg_root to @blkcg, so that all
496beea9da0SDennis Zhou * non-root blkgs have access to their parents. Returns the closest
497beea9da0SDennis Zhou * blkg to the intended blkg should blkg_create() fail.
4983c547865STejun Heo */
4993c547865STejun Heo while (true) {
5003c547865STejun Heo struct blkcg *pos = blkcg;
5013c547865STejun Heo struct blkcg *parent = blkcg_parent(blkcg);
502beea9da0SDennis Zhou struct blkcg_gq *ret_blkg = q->root_blkg;
5033c547865STejun Heo
504beea9da0SDennis Zhou while (parent) {
50579fcc5beSChristoph Hellwig blkg = blkg_lookup(parent, q);
506beea9da0SDennis Zhou if (blkg) {
507beea9da0SDennis Zhou /* remember closest blkg */
508beea9da0SDennis Zhou ret_blkg = blkg;
509beea9da0SDennis Zhou break;
510beea9da0SDennis Zhou }
5113c547865STejun Heo pos = parent;
5123c547865STejun Heo parent = blkcg_parent(parent);
5133c547865STejun Heo }
5143c547865STejun Heo
51599e60387SChristoph Hellwig blkg = blkg_create(pos, disk, NULL);
5168c546287SChristoph Hellwig if (IS_ERR(blkg)) {
5178c546287SChristoph Hellwig blkg = ret_blkg;
5188c546287SChristoph Hellwig break;
5198c546287SChristoph Hellwig }
520beea9da0SDennis Zhou if (pos == blkcg)
5218c546287SChristoph Hellwig break;
5223c96cb32STejun Heo }
52331e4c28dSVivek Goyal
5248c546287SChristoph Hellwig found:
5253a762de5SMing Lei spin_unlock_irqrestore(&q->queue_lock, flags);
526b978962aSDennis Zhou return blkg;
527b978962aSDennis Zhou }
528b978962aSDennis Zhou
blkg_destroy(struct blkcg_gq * blkg)5293c798398STejun Heo static void blkg_destroy(struct blkcg_gq *blkg)
53072e06c25STejun Heo {
5313c798398STejun Heo struct blkcg *blkcg = blkg->blkcg;
5326b065462SDennis Zhou (Facebook) int i;
53303aa264aSTejun Heo
5340d945c1fSChristoph Hellwig lockdep_assert_held(&blkg->q->queue_lock);
5359f13ef67STejun Heo lockdep_assert_held(&blkcg->lock);
53603aa264aSTejun Heo
537f1c006f1SYu Kuai /*
538f1c006f1SYu Kuai * blkg stays on the queue list until blkg_free_workfn(), see details in
539f1c006f1SYu Kuai * blkg_free_workfn(), hence this function can be called from
540f1c006f1SYu Kuai * blkcg_destroy_blkgs() first and again from blkg_destroy_all() before
541f1c006f1SYu Kuai * blkg_free_workfn().
542f1c006f1SYu Kuai */
543f1c006f1SYu Kuai if (hlist_unhashed(&blkg->blkcg_node))
544f1c006f1SYu Kuai return;
545a637120eSTejun Heo
5466b065462SDennis Zhou (Facebook) for (i = 0; i < BLKCG_MAX_POLS; i++) {
5476b065462SDennis Zhou (Facebook) struct blkcg_policy *pol = blkcg_policy[i];
5486b065462SDennis Zhou (Facebook)
549dfd6200aSYu Kuai if (blkg->pd[i] && blkg->pd[i]->online) {
550f37bf75cSYu Kuai blkg->pd[i]->online = false;
551dfd6200aSYu Kuai if (pol->pd_offline_fn)
5526b065462SDennis Zhou (Facebook) pol->pd_offline_fn(blkg->pd[i]);
5536b065462SDennis Zhou (Facebook) }
5546b065462SDennis Zhou (Facebook) }
5556b065462SDennis Zhou (Facebook)
556f427d909STejun Heo blkg->online = false;
557f427d909STejun Heo
558a637120eSTejun Heo radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
5599f13ef67STejun Heo hlist_del_init_rcu(&blkg->blkcg_node);
56003aa264aSTejun Heo
56103aa264aSTejun Heo /*
562a637120eSTejun Heo * Both setting lookup hint to and clearing it from @blkg are done
563a637120eSTejun Heo * under queue_lock. If it's not pointing to @blkg now, it never
564a637120eSTejun Heo * will. Hint assignment itself can race safely.
565a637120eSTejun Heo */
566ec6c676aSPaul E. McKenney if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
567a637120eSTejun Heo rcu_assign_pointer(blkcg->blkg_hint, NULL);
568a637120eSTejun Heo
569a637120eSTejun Heo /*
57003aa264aSTejun Heo * Put the reference taken at the time of creation so that when all
57103aa264aSTejun Heo * queues are gone, group can be destroyed.
57203aa264aSTejun Heo */
5737fcf2b03SDennis Zhou percpu_ref_kill(&blkg->refcnt);
57403aa264aSTejun Heo }
57503aa264aSTejun Heo
blkg_destroy_all(struct gendisk * disk)57600ad6991SChristoph Hellwig static void blkg_destroy_all(struct gendisk *disk)
57703aa264aSTejun Heo {
57800ad6991SChristoph Hellwig struct request_queue *q = disk->queue;
5793c798398STejun Heo struct blkcg_gq *blkg, *n;
580a731763fSYu Kuai int count = BLKG_DESTROY_BATCH_SIZE;
5810730b1e3SMing Lei int i;
58272e06c25STejun Heo
583a731763fSYu Kuai restart:
5840d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
585e8989faeSTejun Heo list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
5863c798398STejun Heo struct blkcg *blkcg = blkg->blkcg;
5879f13ef67STejun Heo
5888176080dSTao Su if (hlist_unhashed(&blkg->blkcg_node))
5898176080dSTao Su continue;
5908176080dSTao Su
5919f13ef67STejun Heo spin_lock(&blkcg->lock);
592e8989faeSTejun Heo blkg_destroy(blkg);
5939f13ef67STejun Heo spin_unlock(&blkcg->lock);
594a731763fSYu Kuai
595a731763fSYu Kuai /*
596a731763fSYu Kuai * in order to avoid holding the spin lock for too long, release
597a731763fSYu Kuai * it when a batch of blkgs are destroyed.
598a731763fSYu Kuai */
599a731763fSYu Kuai if (!(--count)) {
600a731763fSYu Kuai count = BLKG_DESTROY_BATCH_SIZE;
601a731763fSYu Kuai spin_unlock_irq(&q->queue_lock);
602a731763fSYu Kuai cond_resched();
603a731763fSYu Kuai goto restart;
604a731763fSYu Kuai }
60503aa264aSTejun Heo }
6066fe810bdSTejun Heo
6070730b1e3SMing Lei /*
6080730b1e3SMing Lei * Mark policy deactivated since policy offline has been done, and
6090730b1e3SMing Lei * the free is scheduled, so future blkcg_deactivate_policy() can
6100730b1e3SMing Lei * be bypassed
6110730b1e3SMing Lei */
6120730b1e3SMing Lei for (i = 0; i < BLKCG_MAX_POLS; i++) {
6130730b1e3SMing Lei struct blkcg_policy *pol = blkcg_policy[i];
6140730b1e3SMing Lei
6150730b1e3SMing Lei if (pol)
6160730b1e3SMing Lei __clear_bit(pol->plid, q->blkcg_pols);
6170730b1e3SMing Lei }
6180730b1e3SMing Lei
6196fe810bdSTejun Heo q->root_blkg = NULL;
6200d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
62172e06c25STejun Heo }
62272e06c25STejun Heo
blkg_iostat_set(struct blkg_iostat * dst,struct blkg_iostat * src)623d4a60298SMing Lei static void blkg_iostat_set(struct blkg_iostat *dst, struct blkg_iostat *src)
624d4a60298SMing Lei {
625d4a60298SMing Lei int i;
626d4a60298SMing Lei
627d4a60298SMing Lei for (i = 0; i < BLKG_IOSTAT_NR; i++) {
628d4a60298SMing Lei dst->bytes[i] = src->bytes[i];
629d4a60298SMing Lei dst->ios[i] = src->ios[i];
630d4a60298SMing Lei }
631d4a60298SMing Lei }
632d4a60298SMing Lei
__blkg_clear_stat(struct blkg_iostat_set * bis)633d4a60298SMing Lei static void __blkg_clear_stat(struct blkg_iostat_set *bis)
634d4a60298SMing Lei {
635d4a60298SMing Lei struct blkg_iostat cur = {0};
636d4a60298SMing Lei unsigned long flags;
637d4a60298SMing Lei
638d4a60298SMing Lei flags = u64_stats_update_begin_irqsave(&bis->sync);
639d4a60298SMing Lei blkg_iostat_set(&bis->cur, &cur);
640d4a60298SMing Lei blkg_iostat_set(&bis->last, &cur);
641d4a60298SMing Lei u64_stats_update_end_irqrestore(&bis->sync, flags);
642d4a60298SMing Lei }
643d4a60298SMing Lei
blkg_clear_stat(struct blkcg_gq * blkg)644d4a60298SMing Lei static void blkg_clear_stat(struct blkcg_gq *blkg)
645d4a60298SMing Lei {
646d4a60298SMing Lei int cpu;
647d4a60298SMing Lei
648d4a60298SMing Lei for_each_possible_cpu(cpu) {
649d4a60298SMing Lei struct blkg_iostat_set *s = per_cpu_ptr(blkg->iostat_cpu, cpu);
650d4a60298SMing Lei
651d4a60298SMing Lei __blkg_clear_stat(s);
652d4a60298SMing Lei }
653d4a60298SMing Lei __blkg_clear_stat(&blkg->iostat);
654d4a60298SMing Lei }
655d4a60298SMing Lei
blkcg_reset_stats(struct cgroup_subsys_state * css,struct cftype * cftype,u64 val)656182446d0STejun Heo static int blkcg_reset_stats(struct cgroup_subsys_state *css,
657182446d0STejun Heo struct cftype *cftype, u64 val)
658303a3acbSDivyesh Shah {
659182446d0STejun Heo struct blkcg *blkcg = css_to_blkcg(css);
6603c798398STejun Heo struct blkcg_gq *blkg;
661d4a60298SMing Lei int i;
662303a3acbSDivyesh Shah
663838f13bfSTejun Heo mutex_lock(&blkcg_pol_mutex);
664303a3acbSDivyesh Shah spin_lock_irq(&blkcg->lock);
665997a026cSTejun Heo
666997a026cSTejun Heo /*
667997a026cSTejun Heo * Note that stat reset is racy - it doesn't synchronize against
668997a026cSTejun Heo * stat updates. This is a debug feature which shouldn't exist
669997a026cSTejun Heo * anyway. If you get hit by a race, retry.
670997a026cSTejun Heo */
671b67bfe0dSSasha Levin hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
672d4a60298SMing Lei blkg_clear_stat(blkg);
6738bd435b3STejun Heo for (i = 0; i < BLKCG_MAX_POLS; i++) {
6743c798398STejun Heo struct blkcg_policy *pol = blkcg_policy[i];
675e8989faeSTejun Heo
676a9520cd6STejun Heo if (blkg->pd[i] && pol->pd_reset_stats_fn)
677a9520cd6STejun Heo pol->pd_reset_stats_fn(blkg->pd[i]);
678e8989faeSTejun Heo }
679bc0d6501STejun Heo }
680f0bdc8cdSVivek Goyal
681303a3acbSDivyesh Shah spin_unlock_irq(&blkcg->lock);
682bc0d6501STejun Heo mutex_unlock(&blkcg_pol_mutex);
683303a3acbSDivyesh Shah return 0;
684303a3acbSDivyesh Shah }
685303a3acbSDivyesh Shah
blkg_dev_name(struct blkcg_gq * blkg)686dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg)
687303a3acbSDivyesh Shah {
688a06377c5SChristoph Hellwig if (!blkg->q->disk)
689d3d32e69STejun Heo return NULL;
690d152c682SChristoph Hellwig return bdi_dev_name(blkg->q->disk->bdi);
691303a3acbSDivyesh Shah }
692303a3acbSDivyesh Shah
693d3d32e69STejun Heo /**
694d3d32e69STejun Heo * blkcg_print_blkgs - helper for printing per-blkg data
695d3d32e69STejun Heo * @sf: seq_file to print to
696d3d32e69STejun Heo * @blkcg: blkcg of interest
697d3d32e69STejun Heo * @prfill: fill function to print out a blkg
698d3d32e69STejun Heo * @pol: policy in question
699d3d32e69STejun Heo * @data: data to be passed to @prfill
700d3d32e69STejun Heo * @show_total: to print out sum of prfill return values or not
701d3d32e69STejun Heo *
702d3d32e69STejun Heo * This function invokes @prfill on each blkg of @blkcg if pd for the
703d3d32e69STejun Heo * policy specified by @pol exists. @prfill is invoked with @sf, the
704810ecfa7STejun Heo * policy data and @data and the matching queue lock held. If @show_total
705810ecfa7STejun Heo * is %true, the sum of the return values from @prfill is printed with
706810ecfa7STejun Heo * "Total" label at the end.
707d3d32e69STejun Heo *
708d3d32e69STejun Heo * This is to be used to construct print functions for
709d3d32e69STejun Heo * cftype->read_seq_string method.
710d3d32e69STejun Heo */
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)7113c798398STejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
712f95a04afSTejun Heo u64 (*prfill)(struct seq_file *,
713f95a04afSTejun Heo struct blkg_policy_data *, int),
7143c798398STejun Heo const struct blkcg_policy *pol, int data,
715ec399347STejun Heo bool show_total)
7165624a4e4SVivek Goyal {
7173c798398STejun Heo struct blkcg_gq *blkg;
718d3d32e69STejun Heo u64 total = 0;
7195624a4e4SVivek Goyal
720810ecfa7STejun Heo rcu_read_lock();
721ee89f812SLinus Torvalds hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
7220d945c1fSChristoph Hellwig spin_lock_irq(&blkg->q->queue_lock);
723a2b1693bSTejun Heo if (blkcg_policy_enabled(blkg->q, pol))
724f95a04afSTejun Heo total += prfill(sf, blkg->pd[pol->plid], data);
7250d945c1fSChristoph Hellwig spin_unlock_irq(&blkg->q->queue_lock);
726810ecfa7STejun Heo }
727810ecfa7STejun Heo rcu_read_unlock();
7281cd9e039SVivek Goyal
729d3d32e69STejun Heo if (show_total)
730d3d32e69STejun Heo seq_printf(sf, "Total %llu\n", (unsigned long long)total);
7315624a4e4SVivek Goyal }
732829fdb50STejun Heo EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
7335624a4e4SVivek Goyal
734d3d32e69STejun Heo /**
735d3d32e69STejun Heo * __blkg_prfill_u64 - prfill helper for a single u64 value
736d3d32e69STejun Heo * @sf: seq_file to print to
737f95a04afSTejun Heo * @pd: policy private data of interest
738d3d32e69STejun Heo * @v: value to print
739d3d32e69STejun Heo *
74037754595SKemeng Shi * Print @v to @sf for the device associated with @pd.
741d3d32e69STejun Heo */
__blkg_prfill_u64(struct seq_file * sf,struct blkg_policy_data * pd,u64 v)742f95a04afSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
7435624a4e4SVivek Goyal {
744f95a04afSTejun Heo const char *dname = blkg_dev_name(pd->blkg);
7455624a4e4SVivek Goyal
746d3d32e69STejun Heo if (!dname)
747d3d32e69STejun Heo return 0;
7485624a4e4SVivek Goyal
749d3d32e69STejun Heo seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
750c4c76a05STejun Heo return v;
751c4c76a05STejun Heo }
752829fdb50STejun Heo EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
753c4c76a05STejun Heo
75416b3de66STejun Heo /**
755faffaab2STejun Heo * blkg_conf_init - initialize a blkg_conf_ctx
756faffaab2STejun Heo * @ctx: blkg_conf_ctx to initialize
757faffaab2STejun Heo * @input: input string
758015d254cSTejun Heo *
759faffaab2STejun Heo * Initialize @ctx which can be used to parse blkg config input string @input.
760faffaab2STejun Heo * Once initialized, @ctx can be used with blkg_conf_open_bdev() and
761faffaab2STejun Heo * blkg_conf_prep(), and must be cleaned up with blkg_conf_exit().
762015d254cSTejun Heo */
blkg_conf_init(struct blkg_conf_ctx * ctx,char * input)763faffaab2STejun Heo void blkg_conf_init(struct blkg_conf_ctx *ctx, char *input)
764015d254cSTejun Heo {
765faffaab2STejun Heo *ctx = (struct blkg_conf_ctx){ .input = input };
766faffaab2STejun Heo }
767faffaab2STejun Heo EXPORT_SYMBOL_GPL(blkg_conf_init);
768faffaab2STejun Heo
769faffaab2STejun Heo /**
770faffaab2STejun Heo * blkg_conf_open_bdev - parse and open bdev for per-blkg config update
771faffaab2STejun Heo * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
772faffaab2STejun Heo *
773faffaab2STejun Heo * Parse the device node prefix part, MAJ:MIN, of per-blkg config update from
774faffaab2STejun Heo * @ctx->input and get and store the matching bdev in @ctx->bdev. @ctx->body is
775faffaab2STejun Heo * set to point past the device node prefix.
776faffaab2STejun Heo *
777faffaab2STejun Heo * This function may be called multiple times on @ctx and the extra calls become
778faffaab2STejun Heo * NOOPs. blkg_conf_prep() implicitly calls this function. Use this function
779faffaab2STejun Heo * explicitly if bdev access is needed without resolving the blkcg / policy part
780faffaab2STejun Heo * of @ctx->input. Returns -errno on error.
781faffaab2STejun Heo */
blkg_conf_open_bdev(struct blkg_conf_ctx * ctx)782faffaab2STejun Heo int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)
783faffaab2STejun Heo {
784faffaab2STejun Heo char *input = ctx->input;
785015d254cSTejun Heo unsigned int major, minor;
78622ae8ce8SChristoph Hellwig struct block_device *bdev;
78722ae8ce8SChristoph Hellwig int key_len;
788015d254cSTejun Heo
789faffaab2STejun Heo if (ctx->bdev)
790faffaab2STejun Heo return 0;
791faffaab2STejun Heo
792015d254cSTejun Heo if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
793faffaab2STejun Heo return -EINVAL;
794015d254cSTejun Heo
795015d254cSTejun Heo input += key_len;
796015d254cSTejun Heo if (!isspace(*input))
797faffaab2STejun Heo return -EINVAL;
798015d254cSTejun Heo input = skip_spaces(input);
799015d254cSTejun Heo
80022ae8ce8SChristoph Hellwig bdev = blkdev_get_no_open(MKDEV(major, minor));
80122ae8ce8SChristoph Hellwig if (!bdev)
802faffaab2STejun Heo return -ENODEV;
80322ae8ce8SChristoph Hellwig if (bdev_is_partition(bdev)) {
80422ae8ce8SChristoph Hellwig blkdev_put_no_open(bdev);
805faffaab2STejun Heo return -ENODEV;
806015d254cSTejun Heo }
807015d254cSTejun Heo
808a13bd91bSYu Kuai mutex_lock(&bdev->bd_queue->rq_qos_mutex);
809a13bd91bSYu Kuai if (!disk_live(bdev->bd_disk)) {
810a13bd91bSYu Kuai blkdev_put_no_open(bdev);
811a13bd91bSYu Kuai mutex_unlock(&bdev->bd_queue->rq_qos_mutex);
812a13bd91bSYu Kuai return -ENODEV;
813a13bd91bSYu Kuai }
814a13bd91bSYu Kuai
815faffaab2STejun Heo ctx->body = input;
816faffaab2STejun Heo ctx->bdev = bdev;
817faffaab2STejun Heo return 0;
818015d254cSTejun Heo }
819015d254cSTejun Heo
820015d254cSTejun Heo /**
821015d254cSTejun Heo * blkg_conf_prep - parse and prepare for per-blkg config update
8223a8b31d3STejun Heo * @blkcg: target block cgroup
823da8b0662STejun Heo * @pol: target policy
824faffaab2STejun Heo * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
8253a8b31d3STejun Heo *
826faffaab2STejun Heo * Parse per-blkg config update from @ctx->input and initialize @ctx
827faffaab2STejun Heo * accordingly. On success, @ctx->body points to the part of @ctx->input
828faffaab2STejun Heo * following MAJ:MIN, @ctx->bdev points to the target block device and
829faffaab2STejun Heo * @ctx->blkg to the blkg being configured.
830faffaab2STejun Heo *
831faffaab2STejun Heo * blkg_conf_open_bdev() may be called on @ctx beforehand. On success, this
832faffaab2STejun Heo * function returns with queue lock held and must be followed by
833faffaab2STejun Heo * blkg_conf_exit().
8343a8b31d3STejun Heo */
blkg_conf_prep(struct blkcg * blkcg,const struct blkcg_policy * pol,struct blkg_conf_ctx * ctx)8353c798398STejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
836faffaab2STejun Heo struct blkg_conf_ctx *ctx)
83783462a6cSTejun Heo __acquires(&bdev->bd_queue->queue_lock)
83834d0f179SGui Jianfeng {
83999e60387SChristoph Hellwig struct gendisk *disk;
840457e490fSTahsin Erdogan struct request_queue *q;
8413c798398STejun Heo struct blkcg_gq *blkg;
842015d254cSTejun Heo int ret;
84334d0f179SGui Jianfeng
844faffaab2STejun Heo ret = blkg_conf_open_bdev(ctx);
845faffaab2STejun Heo if (ret)
846faffaab2STejun Heo return ret;
847faffaab2STejun Heo
848faffaab2STejun Heo disk = ctx->bdev->bd_disk;
84999e60387SChristoph Hellwig q = disk->queue;
850457e490fSTahsin Erdogan
8510c9d338cSYu Kuai /*
8520c9d338cSYu Kuai * blkcg_deactivate_policy() requires queue to be frozen, we can grab
8530c9d338cSYu Kuai * q_usage_counter to prevent concurrent with blkcg_deactivate_policy().
8540c9d338cSYu Kuai */
8550c9d338cSYu Kuai ret = blk_queue_enter(q, 0);
8560c9d338cSYu Kuai if (ret)
85715c30104SYu Kuai goto fail;
8580c9d338cSYu Kuai
8590d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
860457e490fSTahsin Erdogan
861f753526eSChristoph Hellwig if (!blkcg_policy_enabled(q, pol)) {
862f753526eSChristoph Hellwig ret = -EOPNOTSUPP;
863457e490fSTahsin Erdogan goto fail_unlock;
864457e490fSTahsin Erdogan }
865457e490fSTahsin Erdogan
866f753526eSChristoph Hellwig blkg = blkg_lookup(blkcg, q);
8675765033cSChristoph Hellwig if (blkg)
868457e490fSTahsin Erdogan goto success;
869457e490fSTahsin Erdogan
870457e490fSTahsin Erdogan /*
871457e490fSTahsin Erdogan * Create blkgs walking down from blkcg_root to @blkcg, so that all
872457e490fSTahsin Erdogan * non-root blkgs have access to their parents.
873457e490fSTahsin Erdogan */
874457e490fSTahsin Erdogan while (true) {
875457e490fSTahsin Erdogan struct blkcg *pos = blkcg;
876457e490fSTahsin Erdogan struct blkcg *parent;
877457e490fSTahsin Erdogan struct blkcg_gq *new_blkg;
878457e490fSTahsin Erdogan
879457e490fSTahsin Erdogan parent = blkcg_parent(blkcg);
88079fcc5beSChristoph Hellwig while (parent && !blkg_lookup(parent, q)) {
881457e490fSTahsin Erdogan pos = parent;
882457e490fSTahsin Erdogan parent = blkcg_parent(parent);
883457e490fSTahsin Erdogan }
884457e490fSTahsin Erdogan
885457e490fSTahsin Erdogan /* Drop locks to do new blkg allocation with GFP_KERNEL. */
8860d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
887457e490fSTahsin Erdogan
88899e60387SChristoph Hellwig new_blkg = blkg_alloc(pos, disk, GFP_KERNEL);
889457e490fSTahsin Erdogan if (unlikely(!new_blkg)) {
890457e490fSTahsin Erdogan ret = -ENOMEM;
89115c30104SYu Kuai goto fail_exit_queue;
8925f6c2d2bSTejun Heo }
893e56da7e2STejun Heo
894f255c19bSGabriel Krisman Bertazi if (radix_tree_preload(GFP_KERNEL)) {
895f255c19bSGabriel Krisman Bertazi blkg_free(new_blkg);
896f255c19bSGabriel Krisman Bertazi ret = -ENOMEM;
89715c30104SYu Kuai goto fail_exit_queue;
898f255c19bSGabriel Krisman Bertazi }
899f255c19bSGabriel Krisman Bertazi
9000d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
901da8b0662STejun Heo
902f753526eSChristoph Hellwig if (!blkcg_policy_enabled(q, pol)) {
90352abfcbdSGabriel Krisman Bertazi blkg_free(new_blkg);
904f753526eSChristoph Hellwig ret = -EOPNOTSUPP;
905f255c19bSGabriel Krisman Bertazi goto fail_preloaded;
906457e490fSTahsin Erdogan }
907457e490fSTahsin Erdogan
908f753526eSChristoph Hellwig blkg = blkg_lookup(pos, q);
909457e490fSTahsin Erdogan if (blkg) {
910457e490fSTahsin Erdogan blkg_free(new_blkg);
911457e490fSTahsin Erdogan } else {
91299e60387SChristoph Hellwig blkg = blkg_create(pos, disk, new_blkg);
91398d669b4SKefeng Wang if (IS_ERR(blkg)) {
914457e490fSTahsin Erdogan ret = PTR_ERR(blkg);
915f255c19bSGabriel Krisman Bertazi goto fail_preloaded;
916457e490fSTahsin Erdogan }
917457e490fSTahsin Erdogan }
918457e490fSTahsin Erdogan
919f255c19bSGabriel Krisman Bertazi radix_tree_preload_end();
920f255c19bSGabriel Krisman Bertazi
921457e490fSTahsin Erdogan if (pos == blkcg)
922457e490fSTahsin Erdogan goto success;
923457e490fSTahsin Erdogan }
924457e490fSTahsin Erdogan success:
9250c9d338cSYu Kuai blk_queue_exit(q);
926457e490fSTahsin Erdogan ctx->blkg = blkg;
927457e490fSTahsin Erdogan return 0;
928457e490fSTahsin Erdogan
929f255c19bSGabriel Krisman Bertazi fail_preloaded:
930f255c19bSGabriel Krisman Bertazi radix_tree_preload_end();
931457e490fSTahsin Erdogan fail_unlock:
9320d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
93315c30104SYu Kuai fail_exit_queue:
93415c30104SYu Kuai blk_queue_exit(q);
935457e490fSTahsin Erdogan fail:
936e56da7e2STejun Heo /*
9373a8b31d3STejun Heo * If queue was bypassing, we should retry. Do so after a
9383a8b31d3STejun Heo * short msleep(). It isn't strictly necessary but queue
9393a8b31d3STejun Heo * can be bypassing for some time and it's always nice to
9403a8b31d3STejun Heo * avoid busy looping.
941e56da7e2STejun Heo */
942e56da7e2STejun Heo if (ret == -EBUSY) {
943e56da7e2STejun Heo msleep(10);
9443a8b31d3STejun Heo ret = restart_syscall();
945e56da7e2STejun Heo }
946726fa694STejun Heo return ret;
94734d0f179SGui Jianfeng }
94889f3b6d6SPavel Begunkov EXPORT_SYMBOL_GPL(blkg_conf_prep);
94934d0f179SGui Jianfeng
9503a8b31d3STejun Heo /**
951faffaab2STejun Heo * blkg_conf_exit - clean up per-blkg config update
952faffaab2STejun Heo * @ctx: blkg_conf_ctx initialized with blkg_conf_init()
9533a8b31d3STejun Heo *
954faffaab2STejun Heo * Clean up after per-blkg config update. This function must be called on all
955faffaab2STejun Heo * blkg_conf_ctx's initialized with blkg_conf_init().
9563a8b31d3STejun Heo */
blkg_conf_exit(struct blkg_conf_ctx * ctx)957faffaab2STejun Heo void blkg_conf_exit(struct blkg_conf_ctx *ctx)
95883462a6cSTejun Heo __releases(&ctx->bdev->bd_queue->queue_lock)
959a13bd91bSYu Kuai __releases(&ctx->bdev->bd_queue->rq_qos_mutex)
9603a8b31d3STejun Heo {
961faffaab2STejun Heo if (ctx->blkg) {
962ed6cddefSPavel Begunkov spin_unlock_irq(&bdev_get_queue(ctx->bdev)->queue_lock);
963faffaab2STejun Heo ctx->blkg = NULL;
9643a8b31d3STejun Heo }
965faffaab2STejun Heo
966faffaab2STejun Heo if (ctx->bdev) {
967a13bd91bSYu Kuai mutex_unlock(&ctx->bdev->bd_queue->rq_qos_mutex);
968faffaab2STejun Heo blkdev_put_no_open(ctx->bdev);
969faffaab2STejun Heo ctx->body = NULL;
970faffaab2STejun Heo ctx->bdev = NULL;
971faffaab2STejun Heo }
972faffaab2STejun Heo }
973faffaab2STejun Heo EXPORT_SYMBOL_GPL(blkg_conf_exit);
9743a8b31d3STejun Heo
blkg_iostat_add(struct blkg_iostat * dst,struct blkg_iostat * src)975cd1fc4b9SBoris Burkov static void blkg_iostat_add(struct blkg_iostat *dst, struct blkg_iostat *src)
976cd1fc4b9SBoris Burkov {
977cd1fc4b9SBoris Burkov int i;
978cd1fc4b9SBoris Burkov
979cd1fc4b9SBoris Burkov for (i = 0; i < BLKG_IOSTAT_NR; i++) {
980cd1fc4b9SBoris Burkov dst->bytes[i] += src->bytes[i];
981cd1fc4b9SBoris Burkov dst->ios[i] += src->ios[i];
982cd1fc4b9SBoris Burkov }
983cd1fc4b9SBoris Burkov }
984cd1fc4b9SBoris Burkov
blkg_iostat_sub(struct blkg_iostat * dst,struct blkg_iostat * src)985cd1fc4b9SBoris Burkov static void blkg_iostat_sub(struct blkg_iostat *dst, struct blkg_iostat *src)
986cd1fc4b9SBoris Burkov {
987cd1fc4b9SBoris Burkov int i;
988cd1fc4b9SBoris Burkov
989cd1fc4b9SBoris Burkov for (i = 0; i < BLKG_IOSTAT_NR; i++) {
990cd1fc4b9SBoris Burkov dst->bytes[i] -= src->bytes[i];
991cd1fc4b9SBoris Burkov dst->ios[i] -= src->ios[i];
992cd1fc4b9SBoris Burkov }
993cd1fc4b9SBoris Burkov }
994cd1fc4b9SBoris Burkov
blkcg_iostat_update(struct blkcg_gq * blkg,struct blkg_iostat * cur,struct blkg_iostat * last)995362b8c16SJason Yan static void blkcg_iostat_update(struct blkcg_gq *blkg, struct blkg_iostat *cur,
996362b8c16SJason Yan struct blkg_iostat *last)
997362b8c16SJason Yan {
998362b8c16SJason Yan struct blkg_iostat delta;
999362b8c16SJason Yan unsigned long flags;
1000362b8c16SJason Yan
1001362b8c16SJason Yan /* propagate percpu delta to global */
1002362b8c16SJason Yan flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
1003362b8c16SJason Yan blkg_iostat_set(&delta, cur);
1004362b8c16SJason Yan blkg_iostat_sub(&delta, last);
1005362b8c16SJason Yan blkg_iostat_add(&blkg->iostat.cur, &delta);
1006362b8c16SJason Yan blkg_iostat_add(last, &delta);
1007362b8c16SJason Yan u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags);
1008362b8c16SJason Yan }
1009362b8c16SJason Yan
__blkcg_rstat_flush(struct blkcg * blkcg,int cpu)101020cb1c2fSMing Lei static void __blkcg_rstat_flush(struct blkcg *blkcg, int cpu)
1011cd1fc4b9SBoris Burkov {
10123b8cc629SWaiman Long struct llist_head *lhead = per_cpu_ptr(blkcg->lhead, cpu);
10133b8cc629SWaiman Long struct llist_node *lnode;
10143b8cc629SWaiman Long struct blkg_iostat_set *bisc, *next_bisc;
10159c39b7a9SMing Lei unsigned long flags;
1016cd1fc4b9SBoris Burkov
1017cd1fc4b9SBoris Burkov rcu_read_lock();
1018cd1fc4b9SBoris Burkov
10193b8cc629SWaiman Long lnode = llist_del_all(lhead);
10203b8cc629SWaiman Long if (!lnode)
10213b8cc629SWaiman Long goto out;
10223b8cc629SWaiman Long
10233b8cc629SWaiman Long /*
102420cb1c2fSMing Lei * For covering concurrent parent blkg update from blkg_release().
102520cb1c2fSMing Lei *
102620cb1c2fSMing Lei * When flushing from cgroup, cgroup_rstat_lock is always held, so
102720cb1c2fSMing Lei * this lock won't cause contention most of time.
102820cb1c2fSMing Lei */
10299c39b7a9SMing Lei raw_spin_lock_irqsave(&blkg_stat_lock, flags);
103020cb1c2fSMing Lei
103120cb1c2fSMing Lei /*
10323b8cc629SWaiman Long * Iterate only the iostat_cpu's queued in the lockless list.
10333b8cc629SWaiman Long */
10343b8cc629SWaiman Long llist_for_each_entry_safe(bisc, next_bisc, lnode, lnode) {
10353b8cc629SWaiman Long struct blkcg_gq *blkg = bisc->blkg;
1036cd1fc4b9SBoris Burkov struct blkcg_gq *parent = blkg->parent;
1037362b8c16SJason Yan struct blkg_iostat cur;
1038cd1fc4b9SBoris Burkov unsigned int seq;
1039cd1fc4b9SBoris Burkov
1040714e59b5SMing Lei /*
1041714e59b5SMing Lei * Order assignment of `next_bisc` from `bisc->lnode.next` in
1042714e59b5SMing Lei * llist_for_each_entry_safe and clearing `bisc->lqueued` for
1043714e59b5SMing Lei * avoiding to assign `next_bisc` with new next pointer added
1044714e59b5SMing Lei * in blk_cgroup_bio_start() in case of re-ordering.
1045714e59b5SMing Lei *
1046714e59b5SMing Lei * The pair barrier is implied in llist_add() in blk_cgroup_bio_start().
1047714e59b5SMing Lei */
1048714e59b5SMing Lei smp_mb();
1049714e59b5SMing Lei
10503b8cc629SWaiman Long WRITE_ONCE(bisc->lqueued, false);
1051b1bee993SWaiman Long if (bisc == &blkg->iostat)
1052b1bee993SWaiman Long goto propagate_up; /* propagate up to parent only */
10533b8cc629SWaiman Long
1054cd1fc4b9SBoris Burkov /* fetch the current per-cpu values */
1055cd1fc4b9SBoris Burkov do {
1056cd1fc4b9SBoris Burkov seq = u64_stats_fetch_begin(&bisc->sync);
1057cd1fc4b9SBoris Burkov blkg_iostat_set(&cur, &bisc->cur);
1058cd1fc4b9SBoris Burkov } while (u64_stats_fetch_retry(&bisc->sync, seq));
1059cd1fc4b9SBoris Burkov
1060362b8c16SJason Yan blkcg_iostat_update(blkg, &cur, &bisc->last);
1061cd1fc4b9SBoris Burkov
1062b1bee993SWaiman Long propagate_up:
1063dc26532aSJohannes Weiner /* propagate global delta to parent (unless that's root) */
1064b1bee993SWaiman Long if (parent && parent->parent) {
1065362b8c16SJason Yan blkcg_iostat_update(parent, &blkg->iostat.cur,
1066362b8c16SJason Yan &blkg->iostat.last);
1067b1bee993SWaiman Long /*
1068b1bee993SWaiman Long * Queue parent->iostat to its blkcg's lockless
1069b1bee993SWaiman Long * list to propagate up to the grandparent if the
1070b1bee993SWaiman Long * iostat hasn't been queued yet.
1071b1bee993SWaiman Long */
1072b1bee993SWaiman Long if (!parent->iostat.lqueued) {
1073b1bee993SWaiman Long struct llist_head *plhead;
1074b1bee993SWaiman Long
1075b1bee993SWaiman Long plhead = per_cpu_ptr(parent->blkcg->lhead, cpu);
1076b1bee993SWaiman Long llist_add(&parent->iostat.lnode, plhead);
1077b1bee993SWaiman Long parent->iostat.lqueued = true;
1078b1bee993SWaiman Long }
1079b1bee993SWaiman Long }
1080cd1fc4b9SBoris Burkov }
10819c39b7a9SMing Lei raw_spin_unlock_irqrestore(&blkg_stat_lock, flags);
10823b8cc629SWaiman Long out:
1083cd1fc4b9SBoris Burkov rcu_read_unlock();
1084cd1fc4b9SBoris Burkov }
1085cd1fc4b9SBoris Burkov
blkcg_rstat_flush(struct cgroup_subsys_state * css,int cpu)108620cb1c2fSMing Lei static void blkcg_rstat_flush(struct cgroup_subsys_state *css, int cpu)
108720cb1c2fSMing Lei {
108820cb1c2fSMing Lei /* Root-level stats are sourced from system-wide IO stats */
108920cb1c2fSMing Lei if (cgroup_parent(css->cgroup))
109020cb1c2fSMing Lei __blkcg_rstat_flush(css_to_blkcg(css), cpu);
109120cb1c2fSMing Lei }
109220cb1c2fSMing Lei
1093ef45fe47SBoris Burkov /*
1094dc26532aSJohannes Weiner * We source root cgroup stats from the system-wide stats to avoid
1095dc26532aSJohannes Weiner * tracking the same information twice and incurring overhead when no
1096dc26532aSJohannes Weiner * cgroups are defined. For that reason, cgroup_rstat_flush in
1097dc26532aSJohannes Weiner * blkcg_print_stat does not actually fill out the iostat in the root
1098dc26532aSJohannes Weiner * cgroup's blkcg_gq.
1099ef45fe47SBoris Burkov *
1100ef45fe47SBoris Burkov * However, we would like to re-use the printing code between the root and
1101ef45fe47SBoris Burkov * non-root cgroups to the extent possible. For that reason, we simulate
1102ef45fe47SBoris Burkov * flushing the root cgroup's stats by explicitly filling in the iostat
1103ef45fe47SBoris Burkov * with disk level statistics.
1104ef45fe47SBoris Burkov */
blkcg_fill_root_iostats(void)1105ef45fe47SBoris Burkov static void blkcg_fill_root_iostats(void)
1106ef45fe47SBoris Burkov {
1107ef45fe47SBoris Burkov struct class_dev_iter iter;
1108ef45fe47SBoris Burkov struct device *dev;
1109ef45fe47SBoris Burkov
1110ef45fe47SBoris Burkov class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
1111ef45fe47SBoris Burkov while ((dev = class_dev_iter_next(&iter))) {
11120d02129eSChristoph Hellwig struct block_device *bdev = dev_to_bdev(dev);
1113928f6f00SChristoph Hellwig struct blkcg_gq *blkg = bdev->bd_disk->queue->root_blkg;
1114ef45fe47SBoris Burkov struct blkg_iostat tmp;
1115ef45fe47SBoris Burkov int cpu;
1116f122d103SChengming Zhou unsigned long flags;
1117ef45fe47SBoris Burkov
1118ef45fe47SBoris Burkov memset(&tmp, 0, sizeof(tmp));
1119ef45fe47SBoris Burkov for_each_possible_cpu(cpu) {
1120ef45fe47SBoris Burkov struct disk_stats *cpu_dkstats;
1121ef45fe47SBoris Burkov
11220d02129eSChristoph Hellwig cpu_dkstats = per_cpu_ptr(bdev->bd_stats, cpu);
1123ef45fe47SBoris Burkov tmp.ios[BLKG_IOSTAT_READ] +=
1124ef45fe47SBoris Burkov cpu_dkstats->ios[STAT_READ];
1125ef45fe47SBoris Burkov tmp.ios[BLKG_IOSTAT_WRITE] +=
1126ef45fe47SBoris Burkov cpu_dkstats->ios[STAT_WRITE];
1127ef45fe47SBoris Burkov tmp.ios[BLKG_IOSTAT_DISCARD] +=
1128ef45fe47SBoris Burkov cpu_dkstats->ios[STAT_DISCARD];
1129ef45fe47SBoris Burkov // convert sectors to bytes
1130ef45fe47SBoris Burkov tmp.bytes[BLKG_IOSTAT_READ] +=
1131ef45fe47SBoris Burkov cpu_dkstats->sectors[STAT_READ] << 9;
1132ef45fe47SBoris Burkov tmp.bytes[BLKG_IOSTAT_WRITE] +=
1133ef45fe47SBoris Burkov cpu_dkstats->sectors[STAT_WRITE] << 9;
1134ef45fe47SBoris Burkov tmp.bytes[BLKG_IOSTAT_DISCARD] +=
1135ef45fe47SBoris Burkov cpu_dkstats->sectors[STAT_DISCARD] << 9;
1136f122d103SChengming Zhou }
1137ef45fe47SBoris Burkov
1138c3df5fb5STejun Heo flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
1139ef45fe47SBoris Burkov blkg_iostat_set(&blkg->iostat.cur, &tmp);
1140c3df5fb5STejun Heo u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags);
1141ef45fe47SBoris Burkov }
1142*99312148SZijun Hu class_dev_iter_exit(&iter);
1143ef45fe47SBoris Burkov }
1144ef45fe47SBoris Burkov
blkcg_print_one_stat(struct blkcg_gq * blkg,struct seq_file * s)114549cb5168SChristoph Hellwig static void blkcg_print_one_stat(struct blkcg_gq *blkg, struct seq_file *s)
11462ee867dcSTejun Heo {
1147f7331648STejun Heo struct blkg_iostat_set *bis = &blkg->iostat;
1148636620b6STejun Heo u64 rbytes, wbytes, rios, wios, dbytes, dios;
114949cb5168SChristoph Hellwig const char *dname;
1150f7331648STejun Heo unsigned seq;
115149cb5168SChristoph Hellwig int i;
1152b0814361STejun Heo
1153b0814361STejun Heo if (!blkg->online)
115449cb5168SChristoph Hellwig return;
1155b0814361STejun Heo
11562ee867dcSTejun Heo dname = blkg_dev_name(blkg);
11572ee867dcSTejun Heo if (!dname)
115849cb5168SChristoph Hellwig return;
11592ee867dcSTejun Heo
1160252c651aSChristoph Hellwig seq_printf(s, "%s ", dname);
1161903d23f0SJosef Bacik
1162f7331648STejun Heo do {
1163f7331648STejun Heo seq = u64_stats_fetch_begin(&bis->sync);
11642ee867dcSTejun Heo
1165f7331648STejun Heo rbytes = bis->cur.bytes[BLKG_IOSTAT_READ];
1166f7331648STejun Heo wbytes = bis->cur.bytes[BLKG_IOSTAT_WRITE];
1167f7331648STejun Heo dbytes = bis->cur.bytes[BLKG_IOSTAT_DISCARD];
1168f7331648STejun Heo rios = bis->cur.ios[BLKG_IOSTAT_READ];
1169f7331648STejun Heo wios = bis->cur.ios[BLKG_IOSTAT_WRITE];
1170f7331648STejun Heo dios = bis->cur.ios[BLKG_IOSTAT_DISCARD];
1171f7331648STejun Heo } while (u64_stats_fetch_retry(&bis->sync, seq));
11722ee867dcSTejun Heo
1173903d23f0SJosef Bacik if (rbytes || wbytes || rios || wios) {
1174252c651aSChristoph Hellwig seq_printf(s, "rbytes=%llu wbytes=%llu rios=%llu wios=%llu dbytes=%llu dios=%llu",
1175636620b6STejun Heo rbytes, wbytes, rios, wios,
1176636620b6STejun Heo dbytes, dios);
1177903d23f0SJosef Bacik }
1178903d23f0SJosef Bacik
117907b0fdecSTejun Heo if (blkcg_debug_stats && atomic_read(&blkg->use_delay)) {
1180252c651aSChristoph Hellwig seq_printf(s, " use_delay=%d delay_nsec=%llu",
1181d09d8df3SJosef Bacik atomic_read(&blkg->use_delay),
118249cb5168SChristoph Hellwig atomic64_read(&blkg->delay_nsec));
1183d09d8df3SJosef Bacik }
1184d09d8df3SJosef Bacik
1185903d23f0SJosef Bacik for (i = 0; i < BLKCG_MAX_POLS; i++) {
1186903d23f0SJosef Bacik struct blkcg_policy *pol = blkcg_policy[i];
1187903d23f0SJosef Bacik
1188903d23f0SJosef Bacik if (!blkg->pd[i] || !pol->pd_stat_fn)
1189903d23f0SJosef Bacik continue;
1190903d23f0SJosef Bacik
11913607849dSWolfgang Bumiller pol->pd_stat_fn(blkg->pd[i], s);
1192903d23f0SJosef Bacik }
119307b0fdecSTejun Heo
11943607849dSWolfgang Bumiller seq_puts(s, "\n");
1195f539da82STejun Heo }
11962ee867dcSTejun Heo
blkcg_print_stat(struct seq_file * sf,void * v)119749cb5168SChristoph Hellwig static int blkcg_print_stat(struct seq_file *sf, void *v)
119849cb5168SChristoph Hellwig {
119949cb5168SChristoph Hellwig struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
120049cb5168SChristoph Hellwig struct blkcg_gq *blkg;
120149cb5168SChristoph Hellwig
120249cb5168SChristoph Hellwig if (!seq_css(sf)->parent)
120349cb5168SChristoph Hellwig blkcg_fill_root_iostats();
120449cb5168SChristoph Hellwig else
120549cb5168SChristoph Hellwig cgroup_rstat_flush(blkcg->css.cgroup);
120649cb5168SChristoph Hellwig
120749cb5168SChristoph Hellwig rcu_read_lock();
120849cb5168SChristoph Hellwig hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
120949cb5168SChristoph Hellwig spin_lock_irq(&blkg->q->queue_lock);
121049cb5168SChristoph Hellwig blkcg_print_one_stat(blkg, sf);
12112ee867dcSTejun Heo spin_unlock_irq(&blkg->q->queue_lock);
12122ee867dcSTejun Heo }
12132ee867dcSTejun Heo rcu_read_unlock();
12142ee867dcSTejun Heo return 0;
12152ee867dcSTejun Heo }
12162ee867dcSTejun Heo
1217e1f3b941SBart Van Assche static struct cftype blkcg_files[] = {
12182ee867dcSTejun Heo {
12192ee867dcSTejun Heo .name = "stat",
12202ee867dcSTejun Heo .seq_show = blkcg_print_stat,
12212ee867dcSTejun Heo },
12222ee867dcSTejun Heo { } /* terminate */
12232ee867dcSTejun Heo };
12242ee867dcSTejun Heo
1225e1f3b941SBart Van Assche static struct cftype blkcg_legacy_files[] = {
122631e4c28dSVivek Goyal {
122784c124daSDivyesh Shah .name = "reset_stats",
12283c798398STejun Heo .write_u64 = blkcg_reset_stats,
122922084190SVivek Goyal },
12304baf6e33STejun Heo { } /* terminate */
123131e4c28dSVivek Goyal };
123231e4c28dSVivek Goyal
1233dec223c9SChristoph Hellwig #ifdef CONFIG_CGROUP_WRITEBACK
blkcg_get_cgwb_list(struct cgroup_subsys_state * css)1234dec223c9SChristoph Hellwig struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css)
1235dec223c9SChristoph Hellwig {
1236dec223c9SChristoph Hellwig return &css_to_blkcg(css)->cgwb_list;
1237dec223c9SChristoph Hellwig }
1238dec223c9SChristoph Hellwig #endif
1239dec223c9SChristoph Hellwig
124059b57717SDennis Zhou (Facebook) /*
124159b57717SDennis Zhou (Facebook) * blkcg destruction is a three-stage process.
124259b57717SDennis Zhou (Facebook) *
124359b57717SDennis Zhou (Facebook) * 1. Destruction starts. The blkcg_css_offline() callback is invoked
124459b57717SDennis Zhou (Facebook) * which offlines writeback. Here we tie the next stage of blkg destruction
124559b57717SDennis Zhou (Facebook) * to the completion of writeback associated with the blkcg. This lets us
124659b57717SDennis Zhou (Facebook) * avoid punting potentially large amounts of outstanding writeback to root
124759b57717SDennis Zhou (Facebook) * while maintaining any ongoing policies. The next stage is triggered when
124859b57717SDennis Zhou (Facebook) * the nr_cgwbs count goes to zero.
124959b57717SDennis Zhou (Facebook) *
125059b57717SDennis Zhou (Facebook) * 2. When the nr_cgwbs count goes to zero, blkcg_destroy_blkgs() is called
125159b57717SDennis Zhou (Facebook) * and handles the destruction of blkgs. Here the css reference held by
125259b57717SDennis Zhou (Facebook) * the blkg is put back eventually allowing blkcg_css_free() to be called.
125359b57717SDennis Zhou (Facebook) * This work may occur in cgwb_release_workfn() on the cgwb_release
125459b57717SDennis Zhou (Facebook) * workqueue. Any submitted ios that fail to get the blkg ref will be
125559b57717SDennis Zhou (Facebook) * punted to the root_blkg.
125659b57717SDennis Zhou (Facebook) *
125759b57717SDennis Zhou (Facebook) * 3. Once the blkcg ref count goes to zero, blkcg_css_free() is called.
125859b57717SDennis Zhou (Facebook) * This finally frees the blkcg.
125959b57717SDennis Zhou (Facebook) */
126059b57717SDennis Zhou (Facebook)
12619f13ef67STejun Heo /**
126259b57717SDennis Zhou (Facebook) * blkcg_destroy_blkgs - responsible for shooting down blkgs
126359b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest
126459b57717SDennis Zhou (Facebook) *
126559b57717SDennis Zhou (Facebook) * blkgs should be removed while holding both q and blkcg locks. As blkcg lock
126659b57717SDennis Zhou (Facebook) * is nested inside q lock, this function performs reverse double lock dancing.
126759b57717SDennis Zhou (Facebook) * Destroying the blkgs releases the reference held on the blkcg's css allowing
126859b57717SDennis Zhou (Facebook) * blkcg_css_free to eventually be called.
126959b57717SDennis Zhou (Facebook) *
127059b57717SDennis Zhou (Facebook) * This is the blkcg counterpart of ioc_release_fn().
127159b57717SDennis Zhou (Facebook) */
blkcg_destroy_blkgs(struct blkcg * blkcg)1272397c9f46SChristoph Hellwig static void blkcg_destroy_blkgs(struct blkcg *blkcg)
127359b57717SDennis Zhou (Facebook) {
12746c635caeSBaolin Wang might_sleep();
12756c635caeSBaolin Wang
12769f13ef67STejun Heo spin_lock_irq(&blkcg->lock);
12777ee9c562STejun Heo
12786b065462SDennis Zhou (Facebook) while (!hlist_empty(&blkcg->blkg_list)) {
12796b065462SDennis Zhou (Facebook) struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
12806b065462SDennis Zhou (Facebook) struct blkcg_gq, blkcg_node);
1281c875f4d0STejun Heo struct request_queue *q = blkg->q;
1282b1c35769SVivek Goyal
12836c635caeSBaolin Wang if (need_resched() || !spin_trylock(&q->queue_lock)) {
12846c635caeSBaolin Wang /*
12856c635caeSBaolin Wang * Given that the system can accumulate a huge number
12866c635caeSBaolin Wang * of blkgs in pathological cases, check to see if we
12876c635caeSBaolin Wang * need to rescheduling to avoid softlockup.
12886c635caeSBaolin Wang */
12896c635caeSBaolin Wang spin_unlock_irq(&blkcg->lock);
12906c635caeSBaolin Wang cond_resched();
12916c635caeSBaolin Wang spin_lock_irq(&blkcg->lock);
12926c635caeSBaolin Wang continue;
12936c635caeSBaolin Wang }
12946c635caeSBaolin Wang
12956b065462SDennis Zhou (Facebook) blkg_destroy(blkg);
12960d945c1fSChristoph Hellwig spin_unlock(&q->queue_lock);
1297b1c35769SVivek Goyal }
1298b1c35769SVivek Goyal
12999f13ef67STejun Heo spin_unlock_irq(&blkcg->lock);
13007ee9c562STejun Heo }
13017ee9c562STejun Heo
1302397c9f46SChristoph Hellwig /**
1303397c9f46SChristoph Hellwig * blkcg_pin_online - pin online state
1304397c9f46SChristoph Hellwig * @blkcg_css: blkcg of interest
1305397c9f46SChristoph Hellwig *
1306397c9f46SChristoph Hellwig * While pinned, a blkcg is kept online. This is primarily used to
1307397c9f46SChristoph Hellwig * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline
1308397c9f46SChristoph Hellwig * while an associated cgwb is still active.
1309397c9f46SChristoph Hellwig */
blkcg_pin_online(struct cgroup_subsys_state * blkcg_css)1310397c9f46SChristoph Hellwig void blkcg_pin_online(struct cgroup_subsys_state *blkcg_css)
1311397c9f46SChristoph Hellwig {
1312397c9f46SChristoph Hellwig refcount_inc(&css_to_blkcg(blkcg_css)->online_pin);
1313397c9f46SChristoph Hellwig }
1314397c9f46SChristoph Hellwig
1315397c9f46SChristoph Hellwig /**
1316397c9f46SChristoph Hellwig * blkcg_unpin_online - unpin online state
1317397c9f46SChristoph Hellwig * @blkcg_css: blkcg of interest
1318397c9f46SChristoph Hellwig *
1319397c9f46SChristoph Hellwig * This is primarily used to impedance-match blkg and cgwb lifetimes so
1320397c9f46SChristoph Hellwig * that blkg doesn't go offline while an associated cgwb is still active.
1321397c9f46SChristoph Hellwig * When this count goes to zero, all active cgwbs have finished so the
1322397c9f46SChristoph Hellwig * blkcg can continue destruction by calling blkcg_destroy_blkgs().
1323397c9f46SChristoph Hellwig */
blkcg_unpin_online(struct cgroup_subsys_state * blkcg_css)1324397c9f46SChristoph Hellwig void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css)
1325397c9f46SChristoph Hellwig {
1326397c9f46SChristoph Hellwig struct blkcg *blkcg = css_to_blkcg(blkcg_css);
1327397c9f46SChristoph Hellwig
1328397c9f46SChristoph Hellwig do {
13295baa2856STejun Heo struct blkcg *parent;
13305baa2856STejun Heo
1331397c9f46SChristoph Hellwig if (!refcount_dec_and_test(&blkcg->online_pin))
1332397c9f46SChristoph Hellwig break;
13335baa2856STejun Heo
13345baa2856STejun Heo parent = blkcg_parent(blkcg);
1335397c9f46SChristoph Hellwig blkcg_destroy_blkgs(blkcg);
13365baa2856STejun Heo blkcg = parent;
1337397c9f46SChristoph Hellwig } while (blkcg);
1338397c9f46SChristoph Hellwig }
1339397c9f46SChristoph Hellwig
1340397c9f46SChristoph Hellwig /**
1341397c9f46SChristoph Hellwig * blkcg_css_offline - cgroup css_offline callback
1342397c9f46SChristoph Hellwig * @css: css of interest
1343397c9f46SChristoph Hellwig *
1344397c9f46SChristoph Hellwig * This function is called when @css is about to go away. Here the cgwbs are
1345397c9f46SChristoph Hellwig * offlined first and only once writeback associated with the blkcg has
1346397c9f46SChristoph Hellwig * finished do we start step 2 (see above).
1347397c9f46SChristoph Hellwig */
blkcg_css_offline(struct cgroup_subsys_state * css)1348397c9f46SChristoph Hellwig static void blkcg_css_offline(struct cgroup_subsys_state *css)
1349397c9f46SChristoph Hellwig {
1350397c9f46SChristoph Hellwig /* this prevents anyone from attaching or migrating to this blkcg */
1351dec223c9SChristoph Hellwig wb_blkcg_offline(css);
1352397c9f46SChristoph Hellwig
1353397c9f46SChristoph Hellwig /* put the base online pin allowing step 2 to be triggered */
1354397c9f46SChristoph Hellwig blkcg_unpin_online(css);
1355397c9f46SChristoph Hellwig }
1356397c9f46SChristoph Hellwig
blkcg_css_free(struct cgroup_subsys_state * css)1357eb95419bSTejun Heo static void blkcg_css_free(struct cgroup_subsys_state *css)
13587ee9c562STejun Heo {
1359eb95419bSTejun Heo struct blkcg *blkcg = css_to_blkcg(css);
1360bc915e61STejun Heo int i;
13617ee9c562STejun Heo
13627876f930STejun Heo mutex_lock(&blkcg_pol_mutex);
1363e4a9bde9STejun Heo
13647876f930STejun Heo list_del(&blkcg->all_blkcgs_node);
13657876f930STejun Heo
1366a322baadSArianna Avanzini for (i = 0; i < BLKCG_MAX_POLS; i++)
1367e4a9bde9STejun Heo if (blkcg->cpd[i])
1368e4a9bde9STejun Heo blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
1369e4a9bde9STejun Heo
1370e4a9bde9STejun Heo mutex_unlock(&blkcg_pol_mutex);
1371e4a9bde9STejun Heo
13723b8cc629SWaiman Long free_percpu(blkcg->lhead);
137331e4c28dSVivek Goyal kfree(blkcg);
137431e4c28dSVivek Goyal }
137531e4c28dSVivek Goyal
1376eb95419bSTejun Heo static struct cgroup_subsys_state *
blkcg_css_alloc(struct cgroup_subsys_state * parent_css)1377eb95419bSTejun Heo blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
137831e4c28dSVivek Goyal {
13793c798398STejun Heo struct blkcg *blkcg;
1380e48453c3SArianna Avanzini int i;
138131e4c28dSVivek Goyal
13827876f930STejun Heo mutex_lock(&blkcg_pol_mutex);
13837876f930STejun Heo
1384eb95419bSTejun Heo if (!parent_css) {
13853c798398STejun Heo blkcg = &blkcg_root;
1386bc915e61STejun Heo } else {
138731e4c28dSVivek Goyal blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1388b5a9adcbSWaiman Long if (!blkcg)
13894c18c9e9Sweiping zhang goto unlock;
1390e48453c3SArianna Avanzini }
139131e4c28dSVivek Goyal
13923b8cc629SWaiman Long if (init_blkcg_llists(blkcg))
13933b8cc629SWaiman Long goto free_blkcg;
139431e4c28dSVivek Goyal
1395e48453c3SArianna Avanzini for (i = 0; i < BLKCG_MAX_POLS ; i++) {
1396e48453c3SArianna Avanzini struct blkcg_policy *pol = blkcg_policy[i];
1397e48453c3SArianna Avanzini struct blkcg_policy_data *cpd;
1398e48453c3SArianna Avanzini
1399e48453c3SArianna Avanzini /*
1400e48453c3SArianna Avanzini * If the policy hasn't been attached yet, wait for it
1401e48453c3SArianna Avanzini * to be attached before doing anything else. Otherwise,
1402e48453c3SArianna Avanzini * check if the policy requires any specific per-cgroup
1403e48453c3SArianna Avanzini * data: if it does, allocate and initialize it.
1404e48453c3SArianna Avanzini */
1405e4a9bde9STejun Heo if (!pol || !pol->cpd_alloc_fn)
1406e48453c3SArianna Avanzini continue;
1407e48453c3SArianna Avanzini
1408e4a9bde9STejun Heo cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1409b5a9adcbSWaiman Long if (!cpd)
1410e48453c3SArianna Avanzini goto free_pd_blkcg;
1411b5a9adcbSWaiman Long
141281437648STejun Heo blkcg->cpd[i] = cpd;
141381437648STejun Heo cpd->blkcg = blkcg;
1414e48453c3SArianna Avanzini cpd->plid = i;
1415e48453c3SArianna Avanzini }
1416e48453c3SArianna Avanzini
141731e4c28dSVivek Goyal spin_lock_init(&blkcg->lock);
1418d866dbf6STejun Heo refcount_set(&blkcg->online_pin, 1);
1419e00f4f4dSTejun Heo INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT | __GFP_NOWARN);
142031e4c28dSVivek Goyal INIT_HLIST_HEAD(&blkcg->blkg_list);
142152ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
142252ebea74STejun Heo INIT_LIST_HEAD(&blkcg->cgwb_list);
142352ebea74STejun Heo #endif
14247876f930STejun Heo list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
14257876f930STejun Heo
14267876f930STejun Heo mutex_unlock(&blkcg_pol_mutex);
142731e4c28dSVivek Goyal return &blkcg->css;
1428e48453c3SArianna Avanzini
1429e48453c3SArianna Avanzini free_pd_blkcg:
1430e48453c3SArianna Avanzini for (i--; i >= 0; i--)
1431e4a9bde9STejun Heo if (blkcg->cpd[i])
1432e4a9bde9STejun Heo blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
14333b8cc629SWaiman Long free_percpu(blkcg->lhead);
14343b8cc629SWaiman Long free_blkcg:
14354c18c9e9Sweiping zhang if (blkcg != &blkcg_root)
1436e48453c3SArianna Avanzini kfree(blkcg);
14374c18c9e9Sweiping zhang unlock:
14387876f930STejun Heo mutex_unlock(&blkcg_pol_mutex);
1439b5a9adcbSWaiman Long return ERR_PTR(-ENOMEM);
144031e4c28dSVivek Goyal }
144131e4c28dSVivek Goyal
blkcg_css_online(struct cgroup_subsys_state * css)14424308a434STejun Heo static int blkcg_css_online(struct cgroup_subsys_state *css)
14434308a434STejun Heo {
1444397c9f46SChristoph Hellwig struct blkcg *parent = blkcg_parent(css_to_blkcg(css));
14454308a434STejun Heo
14464308a434STejun Heo /*
14474308a434STejun Heo * blkcg_pin_online() is used to delay blkcg offline so that blkgs
14484308a434STejun Heo * don't go offline while cgwbs are still active on them. Pin the
14494308a434STejun Heo * parent so that offline always happens towards the root.
14504308a434STejun Heo */
14514308a434STejun Heo if (parent)
1452d7dbd43fSChris Mason blkcg_pin_online(&parent->css);
14534308a434STejun Heo return 0;
14544308a434STejun Heo }
14554308a434STejun Heo
blkg_init_queue(struct request_queue * q)1456740ffad9SMing Lei void blkg_init_queue(struct request_queue *q)
1457740ffad9SMing Lei {
1458740ffad9SMing Lei INIT_LIST_HEAD(&q->blkg_list);
1459740ffad9SMing Lei mutex_init(&q->blkcg_mutex);
1460740ffad9SMing Lei }
1461740ffad9SMing Lei
blkcg_init_disk(struct gendisk * disk)14629823538fSChristoph Hellwig int blkcg_init_disk(struct gendisk *disk)
14635efd6113STejun Heo {
14649823538fSChristoph Hellwig struct request_queue *q = disk->queue;
1465d708f0d5SJens Axboe struct blkcg_gq *new_blkg, *blkg;
1466d708f0d5SJens Axboe bool preloaded;
1467ec13b1d6STejun Heo int ret;
14685efd6113STejun Heo
146999e60387SChristoph Hellwig new_blkg = blkg_alloc(&blkcg_root, disk, GFP_KERNEL);
1470d708f0d5SJens Axboe if (!new_blkg)
1471d708f0d5SJens Axboe return -ENOMEM;
1472d708f0d5SJens Axboe
1473d708f0d5SJens Axboe preloaded = !radix_tree_preload(GFP_KERNEL);
1474d708f0d5SJens Axboe
1475bea54883SJiang Biao /* Make sure the root blkg exists. */
147677c570a1SFanjun Kong /* spin_lock_irq can serve as RCU read-side critical section. */
14770d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
147899e60387SChristoph Hellwig blkg = blkg_create(&blkcg_root, disk, new_blkg);
1479901932a3SJiang Biao if (IS_ERR(blkg))
1480901932a3SJiang Biao goto err_unlock;
1481901932a3SJiang Biao q->root_blkg = blkg;
14820d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
1483ec13b1d6STejun Heo
1484d708f0d5SJens Axboe if (preloaded)
1485d708f0d5SJens Axboe radix_tree_preload_end();
1486d708f0d5SJens Axboe
1487b0dde3f5SChristoph Hellwig ret = blk_ioprio_init(disk);
1488556910e3SBart Van Assche if (ret)
1489556910e3SBart Van Assche goto err_destroy_all;
1490556910e3SBart Van Assche
1491e13793baSChristoph Hellwig ret = blk_throtl_init(disk);
149204be60b5SChristoph Hellwig if (ret)
149333dc6279SChristoph Hellwig goto err_ioprio_exit;
149427029b4bSYufen Yu
149504be60b5SChristoph Hellwig return 0;
149604be60b5SChristoph Hellwig
149733dc6279SChristoph Hellwig err_ioprio_exit:
1498b0dde3f5SChristoph Hellwig blk_ioprio_exit(disk);
149904be60b5SChristoph Hellwig err_destroy_all:
150000ad6991SChristoph Hellwig blkg_destroy_all(disk);
1501ec13b1d6STejun Heo return ret;
1502901932a3SJiang Biao err_unlock:
15030d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
1504901932a3SJiang Biao if (preloaded)
1505901932a3SJiang Biao radix_tree_preload_end();
1506901932a3SJiang Biao return PTR_ERR(blkg);
15075efd6113STejun Heo }
15085efd6113STejun Heo
blkcg_exit_disk(struct gendisk * disk)15099823538fSChristoph Hellwig void blkcg_exit_disk(struct gendisk *disk)
15105efd6113STejun Heo {
151100ad6991SChristoph Hellwig blkg_destroy_all(disk);
1512e13793baSChristoph Hellwig blk_throtl_exit(disk);
15135efd6113STejun Heo }
15145efd6113STejun Heo
blkcg_exit(struct task_struct * tsk)1515d09d8df3SJosef Bacik static void blkcg_exit(struct task_struct *tsk)
1516d09d8df3SJosef Bacik {
1517f05837edSChristoph Hellwig if (tsk->throttle_disk)
1518f05837edSChristoph Hellwig put_disk(tsk->throttle_disk);
1519f05837edSChristoph Hellwig tsk->throttle_disk = NULL;
1520d09d8df3SJosef Bacik }
1521d09d8df3SJosef Bacik
1522c165b3e3STejun Heo struct cgroup_subsys io_cgrp_subsys = {
152392fb9748STejun Heo .css_alloc = blkcg_css_alloc,
15244308a434STejun Heo .css_online = blkcg_css_online,
152592fb9748STejun Heo .css_offline = blkcg_css_offline,
152692fb9748STejun Heo .css_free = blkcg_css_free,
1527f7331648STejun Heo .css_rstat_flush = blkcg_rstat_flush,
15282ee867dcSTejun Heo .dfl_cftypes = blkcg_files,
1529880f50e2STejun Heo .legacy_cftypes = blkcg_legacy_files,
1530c165b3e3STejun Heo .legacy_name = "blkio",
1531d09d8df3SJosef Bacik .exit = blkcg_exit,
15321ced953bSTejun Heo #ifdef CONFIG_MEMCG
15331ced953bSTejun Heo /*
15341ced953bSTejun Heo * This ensures that, if available, memcg is automatically enabled
15351ced953bSTejun Heo * together on the default hierarchy so that the owner cgroup can
15361ced953bSTejun Heo * be retrieved from writeback pages.
15371ced953bSTejun Heo */
15381ced953bSTejun Heo .depends_on = 1 << memory_cgrp_id,
15391ced953bSTejun Heo #endif
1540676f7c8fSTejun Heo };
1541c165b3e3STejun Heo EXPORT_SYMBOL_GPL(io_cgrp_subsys);
1542676f7c8fSTejun Heo
15438bd435b3STejun Heo /**
154440e4996eSChristoph Hellwig * blkcg_activate_policy - activate a blkcg policy on a gendisk
154540e4996eSChristoph Hellwig * @disk: gendisk of interest
1546a2b1693bSTejun Heo * @pol: blkcg policy to activate
1547a2b1693bSTejun Heo *
154840e4996eSChristoph Hellwig * Activate @pol on @disk. Requires %GFP_KERNEL context. @disk goes through
1549a2b1693bSTejun Heo * bypass mode to populate its blkgs with policy_data for @pol.
1550a2b1693bSTejun Heo *
155140e4996eSChristoph Hellwig * Activation happens with @disk bypassed, so nobody would be accessing blkgs
1552a2b1693bSTejun Heo * from IO path. Update of each blkg is protected by both queue and blkcg
1553a2b1693bSTejun Heo * locks so that holding either lock and testing blkcg_policy_enabled() is
1554a2b1693bSTejun Heo * always enough for dereferencing policy data.
1555a2b1693bSTejun Heo *
1556a2b1693bSTejun Heo * The caller is responsible for synchronizing [de]activations and policy
1557a2b1693bSTejun Heo * [un]registerations. Returns 0 on success, -errno on failure.
1558a2b1693bSTejun Heo */
blkcg_activate_policy(struct gendisk * disk,const struct blkcg_policy * pol)155940e4996eSChristoph Hellwig int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol)
1560a2b1693bSTejun Heo {
156140e4996eSChristoph Hellwig struct request_queue *q = disk->queue;
15624c55f4f9STejun Heo struct blkg_policy_data *pd_prealloc = NULL;
15639d179b86STejun Heo struct blkcg_gq *blkg, *pinned_blkg = NULL;
15644c55f4f9STejun Heo int ret;
1565a2b1693bSTejun Heo
1566a2b1693bSTejun Heo if (blkcg_policy_enabled(q, pol))
1567a2b1693bSTejun Heo return 0;
1568a2b1693bSTejun Heo
1569344e9ffcSJens Axboe if (queue_is_mq(q))
1570bd166ef1SJens Axboe blk_mq_freeze_queue(q);
15719d179b86STejun Heo retry:
15720d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
1573a2b1693bSTejun Heo
1574ec14a87eSTejun Heo /* blkg_list is pushed at the head, reverse walk to initialize parents first */
157571c81407STejun Heo list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) {
15764c55f4f9STejun Heo struct blkg_policy_data *pd;
1577a2b1693bSTejun Heo
15784c55f4f9STejun Heo if (blkg->pd[pol->plid])
15794c55f4f9STejun Heo continue;
15804c55f4f9STejun Heo
15819d179b86STejun Heo /* If prealloc matches, use it; otherwise try GFP_NOWAIT */
15829d179b86STejun Heo if (blkg == pinned_blkg) {
15839d179b86STejun Heo pd = pd_prealloc;
15849d179b86STejun Heo pd_prealloc = NULL;
15859d179b86STejun Heo } else {
15860a0b4f79SChristoph Hellwig pd = pol->pd_alloc_fn(disk, blkg->blkcg,
15870a0b4f79SChristoph Hellwig GFP_NOWAIT | __GFP_NOWARN);
15889d179b86STejun Heo }
15899d179b86STejun Heo
15904c55f4f9STejun Heo if (!pd) {
15919d179b86STejun Heo /*
15929d179b86STejun Heo * GFP_NOWAIT failed. Free the existing one and
15939d179b86STejun Heo * prealloc for @blkg w/ GFP_KERNEL.
15949d179b86STejun Heo */
15959d179b86STejun Heo if (pinned_blkg)
15969d179b86STejun Heo blkg_put(pinned_blkg);
15979d179b86STejun Heo blkg_get(blkg);
15989d179b86STejun Heo pinned_blkg = blkg;
15999d179b86STejun Heo
16000d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
16019d179b86STejun Heo
16029d179b86STejun Heo if (pd_prealloc)
16039d179b86STejun Heo pol->pd_free_fn(pd_prealloc);
16040a0b4f79SChristoph Hellwig pd_prealloc = pol->pd_alloc_fn(disk, blkg->blkcg,
16050a0b4f79SChristoph Hellwig GFP_KERNEL);
16069d179b86STejun Heo if (pd_prealloc)
16079d179b86STejun Heo goto retry;
16089d179b86STejun Heo else
16099d179b86STejun Heo goto enomem;
16104c55f4f9STejun Heo }
1611a2b1693bSTejun Heo
1612ec14a87eSTejun Heo spin_lock(&blkg->blkcg->lock);
1613ec14a87eSTejun Heo
1614a2b1693bSTejun Heo pd->blkg = blkg;
1615b276a876STejun Heo pd->plid = pol->plid;
1616ec14a87eSTejun Heo blkg->pd[pol->plid] = pd;
1617a2b1693bSTejun Heo
16189d179b86STejun Heo if (pol->pd_init_fn)
1619ec14a87eSTejun Heo pol->pd_init_fn(pd);
16209d179b86STejun Heo
1621e3ff8887SYu Kuai if (pol->pd_online_fn)
1622ec14a87eSTejun Heo pol->pd_online_fn(pd);
1623ec14a87eSTejun Heo pd->online = true;
1624ec14a87eSTejun Heo
1625ec14a87eSTejun Heo spin_unlock(&blkg->blkcg->lock);
1626dfd6200aSYu Kuai }
1627e3ff8887SYu Kuai
1628a2b1693bSTejun Heo __set_bit(pol->plid, q->blkcg_pols);
1629a2b1693bSTejun Heo ret = 0;
16304c55f4f9STejun Heo
16310d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
16329d179b86STejun Heo out:
1633344e9ffcSJens Axboe if (queue_is_mq(q))
1634bd166ef1SJens Axboe blk_mq_unfreeze_queue(q);
16359d179b86STejun Heo if (pinned_blkg)
16369d179b86STejun Heo blkg_put(pinned_blkg);
1637001bea73STejun Heo if (pd_prealloc)
1638001bea73STejun Heo pol->pd_free_fn(pd_prealloc);
1639a2b1693bSTejun Heo return ret;
16409d179b86STejun Heo
16419d179b86STejun Heo enomem:
1642ec14a87eSTejun Heo /* alloc failed, take down everything */
16439d179b86STejun Heo spin_lock_irq(&q->queue_lock);
16449d179b86STejun Heo list_for_each_entry(blkg, &q->blkg_list, q_node) {
1645858560b2SLi Jinlin struct blkcg *blkcg = blkg->blkcg;
1646ec14a87eSTejun Heo struct blkg_policy_data *pd;
1647858560b2SLi Jinlin
1648858560b2SLi Jinlin spin_lock(&blkcg->lock);
1649ec14a87eSTejun Heo pd = blkg->pd[pol->plid];
1650ec14a87eSTejun Heo if (pd) {
1651ec14a87eSTejun Heo if (pd->online && pol->pd_offline_fn)
1652ec14a87eSTejun Heo pol->pd_offline_fn(pd);
1653ec14a87eSTejun Heo pd->online = false;
1654ec14a87eSTejun Heo pol->pd_free_fn(pd);
16559d179b86STejun Heo blkg->pd[pol->plid] = NULL;
16569d179b86STejun Heo }
1657858560b2SLi Jinlin spin_unlock(&blkcg->lock);
16589d179b86STejun Heo }
16599d179b86STejun Heo spin_unlock_irq(&q->queue_lock);
16609d179b86STejun Heo ret = -ENOMEM;
16619d179b86STejun Heo goto out;
1662a2b1693bSTejun Heo }
1663a2b1693bSTejun Heo EXPORT_SYMBOL_GPL(blkcg_activate_policy);
1664a2b1693bSTejun Heo
1665a2b1693bSTejun Heo /**
166640e4996eSChristoph Hellwig * blkcg_deactivate_policy - deactivate a blkcg policy on a gendisk
166740e4996eSChristoph Hellwig * @disk: gendisk of interest
1668a2b1693bSTejun Heo * @pol: blkcg policy to deactivate
1669a2b1693bSTejun Heo *
167040e4996eSChristoph Hellwig * Deactivate @pol on @disk. Follows the same synchronization rules as
1671a2b1693bSTejun Heo * blkcg_activate_policy().
1672a2b1693bSTejun Heo */
blkcg_deactivate_policy(struct gendisk * disk,const struct blkcg_policy * pol)167340e4996eSChristoph Hellwig void blkcg_deactivate_policy(struct gendisk *disk,
16743c798398STejun Heo const struct blkcg_policy *pol)
1675a2b1693bSTejun Heo {
167640e4996eSChristoph Hellwig struct request_queue *q = disk->queue;
16773c798398STejun Heo struct blkcg_gq *blkg;
1678a2b1693bSTejun Heo
1679a2b1693bSTejun Heo if (!blkcg_policy_enabled(q, pol))
1680a2b1693bSTejun Heo return;
1681a2b1693bSTejun Heo
1682344e9ffcSJens Axboe if (queue_is_mq(q))
1683bd166ef1SJens Axboe blk_mq_freeze_queue(q);
1684bd166ef1SJens Axboe
16851231039dSChristoph Hellwig mutex_lock(&q->blkcg_mutex);
16860d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock);
1687a2b1693bSTejun Heo
1688a2b1693bSTejun Heo __clear_bit(pol->plid, q->blkcg_pols);
1689a2b1693bSTejun Heo
1690a2b1693bSTejun Heo list_for_each_entry(blkg, &q->blkg_list, q_node) {
1691858560b2SLi Jinlin struct blkcg *blkcg = blkg->blkcg;
1692858560b2SLi Jinlin
1693858560b2SLi Jinlin spin_lock(&blkcg->lock);
1694001bea73STejun Heo if (blkg->pd[pol->plid]) {
1695dfd6200aSYu Kuai if (blkg->pd[pol->plid]->online && pol->pd_offline_fn)
1696a9520cd6STejun Heo pol->pd_offline_fn(blkg->pd[pol->plid]);
1697001bea73STejun Heo pol->pd_free_fn(blkg->pd[pol->plid]);
1698a2b1693bSTejun Heo blkg->pd[pol->plid] = NULL;
1699001bea73STejun Heo }
1700858560b2SLi Jinlin spin_unlock(&blkcg->lock);
1701a2b1693bSTejun Heo }
1702a2b1693bSTejun Heo
17030d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock);
17041231039dSChristoph Hellwig mutex_unlock(&q->blkcg_mutex);
1705bd166ef1SJens Axboe
1706344e9ffcSJens Axboe if (queue_is_mq(q))
1707bd166ef1SJens Axboe blk_mq_unfreeze_queue(q);
1708a2b1693bSTejun Heo }
1709a2b1693bSTejun Heo EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
1710a2b1693bSTejun Heo
blkcg_free_all_cpd(struct blkcg_policy * pol)1711e55cf798SJason Yan static void blkcg_free_all_cpd(struct blkcg_policy *pol)
1712e55cf798SJason Yan {
1713e55cf798SJason Yan struct blkcg *blkcg;
1714e55cf798SJason Yan
1715e55cf798SJason Yan list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
1716e55cf798SJason Yan if (blkcg->cpd[pol->plid]) {
1717e55cf798SJason Yan pol->cpd_free_fn(blkcg->cpd[pol->plid]);
1718e55cf798SJason Yan blkcg->cpd[pol->plid] = NULL;
1719e55cf798SJason Yan }
1720e55cf798SJason Yan }
1721e55cf798SJason Yan }
1722e55cf798SJason Yan
1723a2b1693bSTejun Heo /**
17243c798398STejun Heo * blkcg_policy_register - register a blkcg policy
17253c798398STejun Heo * @pol: blkcg policy to register
17268bd435b3STejun Heo *
17273c798398STejun Heo * Register @pol with blkcg core. Might sleep and @pol may be modified on
17283c798398STejun Heo * successful registration. Returns 0 on success and -errno on failure.
17298bd435b3STejun Heo */
blkcg_policy_register(struct blkcg_policy * pol)1730d5bf0291SJens Axboe int blkcg_policy_register(struct blkcg_policy *pol)
17313e252066SVivek Goyal {
173206b285bdSTejun Heo struct blkcg *blkcg;
17338bd435b3STejun Heo int i, ret;
1734e8989faeSTejun Heo
1735838f13bfSTejun Heo mutex_lock(&blkcg_pol_register_mutex);
1736bc0d6501STejun Heo mutex_lock(&blkcg_pol_mutex);
1737bc0d6501STejun Heo
17388bd435b3STejun Heo /* find an empty slot */
17398bd435b3STejun Heo ret = -ENOSPC;
17408bd435b3STejun Heo for (i = 0; i < BLKCG_MAX_POLS; i++)
17413c798398STejun Heo if (!blkcg_policy[i])
17428bd435b3STejun Heo break;
174301c5f85aSJens Axboe if (i >= BLKCG_MAX_POLS) {
174401c5f85aSJens Axboe pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n");
1745838f13bfSTejun Heo goto err_unlock;
174601c5f85aSJens Axboe }
1747035d10b2STejun Heo
1748e8401073Sweiping zhang /* Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs */
1749e8401073Sweiping zhang if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) ||
1750e8401073Sweiping zhang (!pol->pd_alloc_fn ^ !pol->pd_free_fn))
1751e8401073Sweiping zhang goto err_unlock;
1752e8401073Sweiping zhang
175306b285bdSTejun Heo /* register @pol */
17543c798398STejun Heo pol->plid = i;
175506b285bdSTejun Heo blkcg_policy[pol->plid] = pol;
175606b285bdSTejun Heo
175706b285bdSTejun Heo /* allocate and install cpd's */
1758e4a9bde9STejun Heo if (pol->cpd_alloc_fn) {
175906b285bdSTejun Heo list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
176006b285bdSTejun Heo struct blkcg_policy_data *cpd;
176106b285bdSTejun Heo
1762e4a9bde9STejun Heo cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1763bbb427e3SBart Van Assche if (!cpd)
176406b285bdSTejun Heo goto err_free_cpds;
176506b285bdSTejun Heo
176681437648STejun Heo blkcg->cpd[pol->plid] = cpd;
176781437648STejun Heo cpd->blkcg = blkcg;
176806b285bdSTejun Heo cpd->plid = pol->plid;
176906b285bdSTejun Heo }
177006b285bdSTejun Heo }
177106b285bdSTejun Heo
1772838f13bfSTejun Heo mutex_unlock(&blkcg_pol_mutex);
17738bd435b3STejun Heo
17748bd435b3STejun Heo /* everything is in place, add intf files for the new policy */
17752ee867dcSTejun Heo if (pol->dfl_cftypes)
17762ee867dcSTejun Heo WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
17772ee867dcSTejun Heo pol->dfl_cftypes));
1778880f50e2STejun Heo if (pol->legacy_cftypes)
1779c165b3e3STejun Heo WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
1780880f50e2STejun Heo pol->legacy_cftypes));
1781838f13bfSTejun Heo mutex_unlock(&blkcg_pol_register_mutex);
1782838f13bfSTejun Heo return 0;
1783838f13bfSTejun Heo
178406b285bdSTejun Heo err_free_cpds:
1785e55cf798SJason Yan if (pol->cpd_free_fn)
1786e55cf798SJason Yan blkcg_free_all_cpd(pol);
1787e55cf798SJason Yan
178806b285bdSTejun Heo blkcg_policy[pol->plid] = NULL;
1789838f13bfSTejun Heo err_unlock:
1790bc0d6501STejun Heo mutex_unlock(&blkcg_pol_mutex);
1791838f13bfSTejun Heo mutex_unlock(&blkcg_pol_register_mutex);
17928bd435b3STejun Heo return ret;
17933e252066SVivek Goyal }
17943c798398STejun Heo EXPORT_SYMBOL_GPL(blkcg_policy_register);
17953e252066SVivek Goyal
17968bd435b3STejun Heo /**
17973c798398STejun Heo * blkcg_policy_unregister - unregister a blkcg policy
17983c798398STejun Heo * @pol: blkcg policy to unregister
17998bd435b3STejun Heo *
18003c798398STejun Heo * Undo blkcg_policy_register(@pol). Might sleep.
18018bd435b3STejun Heo */
blkcg_policy_unregister(struct blkcg_policy * pol)18023c798398STejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol)
18033e252066SVivek Goyal {
1804838f13bfSTejun Heo mutex_lock(&blkcg_pol_register_mutex);
1805bc0d6501STejun Heo
18063c798398STejun Heo if (WARN_ON(blkcg_policy[pol->plid] != pol))
18078bd435b3STejun Heo goto out_unlock;
18088bd435b3STejun Heo
18098bd435b3STejun Heo /* kill the intf files first */
18102ee867dcSTejun Heo if (pol->dfl_cftypes)
18112ee867dcSTejun Heo cgroup_rm_cftypes(pol->dfl_cftypes);
1812880f50e2STejun Heo if (pol->legacy_cftypes)
1813880f50e2STejun Heo cgroup_rm_cftypes(pol->legacy_cftypes);
181444ea53deSTejun Heo
181506b285bdSTejun Heo /* remove cpds and unregister */
1816838f13bfSTejun Heo mutex_lock(&blkcg_pol_mutex);
181706b285bdSTejun Heo
1818e55cf798SJason Yan if (pol->cpd_free_fn)
1819e55cf798SJason Yan blkcg_free_all_cpd(pol);
1820e55cf798SJason Yan
18213c798398STejun Heo blkcg_policy[pol->plid] = NULL;
182206b285bdSTejun Heo
1823bc0d6501STejun Heo mutex_unlock(&blkcg_pol_mutex);
1824838f13bfSTejun Heo out_unlock:
1825838f13bfSTejun Heo mutex_unlock(&blkcg_pol_register_mutex);
18263e252066SVivek Goyal }
18273c798398STejun Heo EXPORT_SYMBOL_GPL(blkcg_policy_unregister);
1828903d23f0SJosef Bacik
18293480373eSChristoph Hellwig /*
1830d09d8df3SJosef Bacik * Scale the accumulated delay based on how long it has been since we updated
1831d09d8df3SJosef Bacik * the delay. We only call this when we are adding delay, in case it's been a
1832d09d8df3SJosef Bacik * while since we added delay, and when we are checking to see if we need to
1833d09d8df3SJosef Bacik * delay a task, to account for any delays that may have occurred.
1834d09d8df3SJosef Bacik */
blkcg_scale_delay(struct blkcg_gq * blkg,u64 now)1835d09d8df3SJosef Bacik static void blkcg_scale_delay(struct blkcg_gq *blkg, u64 now)
1836d09d8df3SJosef Bacik {
1837d09d8df3SJosef Bacik u64 old = atomic64_read(&blkg->delay_start);
1838d09d8df3SJosef Bacik
183954c52e10STejun Heo /* negative use_delay means no scaling, see blkcg_set_delay() */
184054c52e10STejun Heo if (atomic_read(&blkg->use_delay) < 0)
184154c52e10STejun Heo return;
184254c52e10STejun Heo
1843d09d8df3SJosef Bacik /*
1844d09d8df3SJosef Bacik * We only want to scale down every second. The idea here is that we
1845d09d8df3SJosef Bacik * want to delay people for min(delay_nsec, NSEC_PER_SEC) in a certain
1846d09d8df3SJosef Bacik * time window. We only want to throttle tasks for recent delay that
1847d09d8df3SJosef Bacik * has occurred, in 1 second time windows since that's the maximum
1848d09d8df3SJosef Bacik * things can be throttled. We save the current delay window in
1849d09d8df3SJosef Bacik * blkg->last_delay so we know what amount is still left to be charged
1850d09d8df3SJosef Bacik * to the blkg from this point onward. blkg->last_use keeps track of
1851d09d8df3SJosef Bacik * the use_delay counter. The idea is if we're unthrottling the blkg we
1852d09d8df3SJosef Bacik * are ok with whatever is happening now, and we can take away more of
1853d09d8df3SJosef Bacik * the accumulated delay as we've already throttled enough that
1854d09d8df3SJosef Bacik * everybody is happy with their IO latencies.
1855d09d8df3SJosef Bacik */
1856d09d8df3SJosef Bacik if (time_before64(old + NSEC_PER_SEC, now) &&
185796388f57SUros Bizjak atomic64_try_cmpxchg(&blkg->delay_start, &old, now)) {
1858d09d8df3SJosef Bacik u64 cur = atomic64_read(&blkg->delay_nsec);
1859d09d8df3SJosef Bacik u64 sub = min_t(u64, blkg->last_delay, now - old);
1860d09d8df3SJosef Bacik int cur_use = atomic_read(&blkg->use_delay);
1861d09d8df3SJosef Bacik
1862d09d8df3SJosef Bacik /*
1863d09d8df3SJosef Bacik * We've been unthrottled, subtract a larger chunk of our
1864d09d8df3SJosef Bacik * accumulated delay.
1865d09d8df3SJosef Bacik */
1866d09d8df3SJosef Bacik if (cur_use < blkg->last_use)
1867d09d8df3SJosef Bacik sub = max_t(u64, sub, blkg->last_delay >> 1);
1868d09d8df3SJosef Bacik
1869d09d8df3SJosef Bacik /*
1870d09d8df3SJosef Bacik * This shouldn't happen, but handle it anyway. Our delay_nsec
1871d09d8df3SJosef Bacik * should only ever be growing except here where we subtract out
1872d09d8df3SJosef Bacik * min(last_delay, 1 second), but lord knows bugs happen and I'd
1873d09d8df3SJosef Bacik * rather not end up with negative numbers.
1874d09d8df3SJosef Bacik */
1875d09d8df3SJosef Bacik if (unlikely(cur < sub)) {
1876d09d8df3SJosef Bacik atomic64_set(&blkg->delay_nsec, 0);
1877d09d8df3SJosef Bacik blkg->last_delay = 0;
1878d09d8df3SJosef Bacik } else {
1879d09d8df3SJosef Bacik atomic64_sub(sub, &blkg->delay_nsec);
1880d09d8df3SJosef Bacik blkg->last_delay = cur - sub;
1881d09d8df3SJosef Bacik }
1882d09d8df3SJosef Bacik blkg->last_use = cur_use;
1883d09d8df3SJosef Bacik }
1884d09d8df3SJosef Bacik }
1885d09d8df3SJosef Bacik
1886d09d8df3SJosef Bacik /*
1887d09d8df3SJosef Bacik * This is called when we want to actually walk up the hierarchy and check to
1888d09d8df3SJosef Bacik * see if we need to throttle, and then actually throttle if there is some
1889d09d8df3SJosef Bacik * accumulated delay. This should only be called upon return to user space so
1890d09d8df3SJosef Bacik * we're not holding some lock that would induce a priority inversion.
1891d09d8df3SJosef Bacik */
blkcg_maybe_throttle_blkg(struct blkcg_gq * blkg,bool use_memdelay)1892d09d8df3SJosef Bacik static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
1893d09d8df3SJosef Bacik {
1894fd112c74SJosef Bacik unsigned long pflags;
18955160a5a5STejun Heo bool clamp;
1896d09d8df3SJosef Bacik u64 now = ktime_to_ns(ktime_get());
1897d09d8df3SJosef Bacik u64 exp;
1898d09d8df3SJosef Bacik u64 delay_nsec = 0;
1899d09d8df3SJosef Bacik int tok;
1900d09d8df3SJosef Bacik
1901d09d8df3SJosef Bacik while (blkg->parent) {
19025160a5a5STejun Heo int use_delay = atomic_read(&blkg->use_delay);
19035160a5a5STejun Heo
19045160a5a5STejun Heo if (use_delay) {
19055160a5a5STejun Heo u64 this_delay;
19065160a5a5STejun Heo
1907d09d8df3SJosef Bacik blkcg_scale_delay(blkg, now);
19085160a5a5STejun Heo this_delay = atomic64_read(&blkg->delay_nsec);
19095160a5a5STejun Heo if (this_delay > delay_nsec) {
19105160a5a5STejun Heo delay_nsec = this_delay;
19115160a5a5STejun Heo clamp = use_delay > 0;
19125160a5a5STejun Heo }
1913d09d8df3SJosef Bacik }
1914d09d8df3SJosef Bacik blkg = blkg->parent;
1915d09d8df3SJosef Bacik }
1916d09d8df3SJosef Bacik
1917d09d8df3SJosef Bacik if (!delay_nsec)
1918d09d8df3SJosef Bacik return;
1919d09d8df3SJosef Bacik
1920d09d8df3SJosef Bacik /*
1921d09d8df3SJosef Bacik * Let's not sleep for all eternity if we've amassed a huge delay.
1922d09d8df3SJosef Bacik * Swapping or metadata IO can accumulate 10's of seconds worth of
1923d09d8df3SJosef Bacik * delay, and we want userspace to be able to do _something_ so cap the
19245160a5a5STejun Heo * delays at 0.25s. If there's 10's of seconds worth of delay then the
19255160a5a5STejun Heo * tasks will be delayed for 0.25 second for every syscall. If
19265160a5a5STejun Heo * blkcg_set_delay() was used as indicated by negative use_delay, the
19275160a5a5STejun Heo * caller is responsible for regulating the range.
1928d09d8df3SJosef Bacik */
19295160a5a5STejun Heo if (clamp)
1930d09d8df3SJosef Bacik delay_nsec = min_t(u64, delay_nsec, 250 * NSEC_PER_MSEC);
1931d09d8df3SJosef Bacik
1932fd112c74SJosef Bacik if (use_memdelay)
1933fd112c74SJosef Bacik psi_memstall_enter(&pflags);
1934d09d8df3SJosef Bacik
1935d09d8df3SJosef Bacik exp = ktime_add_ns(now, delay_nsec);
1936d09d8df3SJosef Bacik tok = io_schedule_prepare();
1937d09d8df3SJosef Bacik do {
1938d09d8df3SJosef Bacik __set_current_state(TASK_KILLABLE);
1939d09d8df3SJosef Bacik if (!schedule_hrtimeout(&exp, HRTIMER_MODE_ABS))
1940d09d8df3SJosef Bacik break;
1941d09d8df3SJosef Bacik } while (!fatal_signal_pending(current));
1942d09d8df3SJosef Bacik io_schedule_finish(tok);
1943fd112c74SJosef Bacik
1944fd112c74SJosef Bacik if (use_memdelay)
1945fd112c74SJosef Bacik psi_memstall_leave(&pflags);
1946d09d8df3SJosef Bacik }
1947d09d8df3SJosef Bacik
1948d09d8df3SJosef Bacik /**
1949d09d8df3SJosef Bacik * blkcg_maybe_throttle_current - throttle the current task if it has been marked
1950d09d8df3SJosef Bacik *
1951d09d8df3SJosef Bacik * This is only called if we've been marked with set_notify_resume(). Obviously
1952d09d8df3SJosef Bacik * we can be set_notify_resume() for reasons other than blkcg throttling, so we
1953f05837edSChristoph Hellwig * check to see if current->throttle_disk is set and if not this doesn't do
1954d09d8df3SJosef Bacik * anything. This should only ever be called by the resume code, it's not meant
1955d09d8df3SJosef Bacik * to be called by people willy-nilly as it will actually do the work to
1956d09d8df3SJosef Bacik * throttle the task if it is setup for throttling.
1957d09d8df3SJosef Bacik */
blkcg_maybe_throttle_current(void)1958d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void)
1959d09d8df3SJosef Bacik {
1960f05837edSChristoph Hellwig struct gendisk *disk = current->throttle_disk;
1961d09d8df3SJosef Bacik struct blkcg *blkcg;
1962d09d8df3SJosef Bacik struct blkcg_gq *blkg;
1963d09d8df3SJosef Bacik bool use_memdelay = current->use_memdelay;
1964d09d8df3SJosef Bacik
1965f05837edSChristoph Hellwig if (!disk)
1966d09d8df3SJosef Bacik return;
1967d09d8df3SJosef Bacik
1968f05837edSChristoph Hellwig current->throttle_disk = NULL;
1969d09d8df3SJosef Bacik current->use_memdelay = false;
1970d09d8df3SJosef Bacik
1971d09d8df3SJosef Bacik rcu_read_lock();
197282778259SChristoph Hellwig blkcg = css_to_blkcg(blkcg_css());
1973d09d8df3SJosef Bacik if (!blkcg)
1974d09d8df3SJosef Bacik goto out;
19759a9c261eSChristoph Hellwig blkg = blkg_lookup(blkcg, disk->queue);
1976d09d8df3SJosef Bacik if (!blkg)
1977d09d8df3SJosef Bacik goto out;
19787754f669SDennis Zhou if (!blkg_tryget(blkg))
1979d09d8df3SJosef Bacik goto out;
1980d09d8df3SJosef Bacik rcu_read_unlock();
1981d09d8df3SJosef Bacik
1982d09d8df3SJosef Bacik blkcg_maybe_throttle_blkg(blkg, use_memdelay);
1983d09d8df3SJosef Bacik blkg_put(blkg);
1984f05837edSChristoph Hellwig put_disk(disk);
1985d09d8df3SJosef Bacik return;
1986d09d8df3SJosef Bacik out:
1987d09d8df3SJosef Bacik rcu_read_unlock();
1988d09d8df3SJosef Bacik }
1989d09d8df3SJosef Bacik
1990d09d8df3SJosef Bacik /**
1991d09d8df3SJosef Bacik * blkcg_schedule_throttle - this task needs to check for throttling
19921d6df9d3SYang Li * @disk: disk to throttle
1993537d71b3SBart Van Assche * @use_memdelay: do we charge this to memory delay for PSI
1994d09d8df3SJosef Bacik *
1995d09d8df3SJosef Bacik * This is called by the IO controller when we know there's delay accumulated
1996d09d8df3SJosef Bacik * for the blkg for this task. We do not pass the blkg because there are places
1997d09d8df3SJosef Bacik * we call this that may not have that information, the swapping code for
1998de185b56SChristoph Hellwig * instance will only have a block_device at that point. This set's the
1999d09d8df3SJosef Bacik * notify_resume for the task to check and see if it requires throttling before
2000d09d8df3SJosef Bacik * returning to user space.
2001d09d8df3SJosef Bacik *
2002d09d8df3SJosef Bacik * We will only schedule once per syscall. You can call this over and over
2003d09d8df3SJosef Bacik * again and it will only do the check once upon return to user space, and only
2004d09d8df3SJosef Bacik * throttle once. If the task needs to be throttled again it'll need to be
2005d09d8df3SJosef Bacik * re-set at the next time we see the task.
2006d09d8df3SJosef Bacik */
blkcg_schedule_throttle(struct gendisk * disk,bool use_memdelay)2007de185b56SChristoph Hellwig void blkcg_schedule_throttle(struct gendisk *disk, bool use_memdelay)
2008d09d8df3SJosef Bacik {
2009d09d8df3SJosef Bacik if (unlikely(current->flags & PF_KTHREAD))
2010d09d8df3SJosef Bacik return;
2011d09d8df3SJosef Bacik
2012f05837edSChristoph Hellwig if (current->throttle_disk != disk) {
2013f05837edSChristoph Hellwig if (test_bit(GD_DEAD, &disk->state))
2014d09d8df3SJosef Bacik return;
2015f05837edSChristoph Hellwig get_device(disk_to_dev(disk));
2016d09d8df3SJosef Bacik
2017f05837edSChristoph Hellwig if (current->throttle_disk)
2018f05837edSChristoph Hellwig put_disk(current->throttle_disk);
2019f05837edSChristoph Hellwig current->throttle_disk = disk;
202049d1822bSChunguang Xu }
202149d1822bSChunguang Xu
2022d09d8df3SJosef Bacik if (use_memdelay)
2023d09d8df3SJosef Bacik current->use_memdelay = use_memdelay;
2024d09d8df3SJosef Bacik set_notify_resume(current);
2025d09d8df3SJosef Bacik }
2026d09d8df3SJosef Bacik
2027d09d8df3SJosef Bacik /**
2028d09d8df3SJosef Bacik * blkcg_add_delay - add delay to this blkg
2029537d71b3SBart Van Assche * @blkg: blkg of interest
2030537d71b3SBart Van Assche * @now: the current time in nanoseconds
2031537d71b3SBart Van Assche * @delta: how many nanoseconds of delay to add
2032d09d8df3SJosef Bacik *
2033d09d8df3SJosef Bacik * Charge @delta to the blkg's current delay accumulation. This is used to
2034d09d8df3SJosef Bacik * throttle tasks if an IO controller thinks we need more throttling.
2035d09d8df3SJosef Bacik */
blkcg_add_delay(struct blkcg_gq * blkg,u64 now,u64 delta)2036d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
2037d09d8df3SJosef Bacik {
203854c52e10STejun Heo if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
203954c52e10STejun Heo return;
2040d09d8df3SJosef Bacik blkcg_scale_delay(blkg, now);
2041d09d8df3SJosef Bacik atomic64_add(delta, &blkg->delay_nsec);
2042d09d8df3SJosef Bacik }
2043d09d8df3SJosef Bacik
204428fc591fSChristoph Hellwig /**
204528fc591fSChristoph Hellwig * blkg_tryget_closest - try and get a blkg ref on the closet blkg
204613c7863dSChristoph Hellwig * @bio: target bio
204713c7863dSChristoph Hellwig * @css: target css
204828fc591fSChristoph Hellwig *
204913c7863dSChristoph Hellwig * As the failure mode here is to walk up the blkg tree, this ensure that the
205013c7863dSChristoph Hellwig * blkg->parent pointers are always valid. This returns the blkg that it ended
205113c7863dSChristoph Hellwig * up taking a reference on or %NULL if no reference was taken.
205228fc591fSChristoph Hellwig */
blkg_tryget_closest(struct bio * bio,struct cgroup_subsys_state * css)205313c7863dSChristoph Hellwig static inline struct blkcg_gq *blkg_tryget_closest(struct bio *bio,
205413c7863dSChristoph Hellwig struct cgroup_subsys_state *css)
205528fc591fSChristoph Hellwig {
205613c7863dSChristoph Hellwig struct blkcg_gq *blkg, *ret_blkg = NULL;
205728fc591fSChristoph Hellwig
205813c7863dSChristoph Hellwig rcu_read_lock();
205999e60387SChristoph Hellwig blkg = blkg_lookup_create(css_to_blkcg(css), bio->bi_bdev->bd_disk);
206028fc591fSChristoph Hellwig while (blkg) {
206128fc591fSChristoph Hellwig if (blkg_tryget(blkg)) {
206228fc591fSChristoph Hellwig ret_blkg = blkg;
206328fc591fSChristoph Hellwig break;
206428fc591fSChristoph Hellwig }
206528fc591fSChristoph Hellwig blkg = blkg->parent;
206628fc591fSChristoph Hellwig }
206713c7863dSChristoph Hellwig rcu_read_unlock();
206828fc591fSChristoph Hellwig
206928fc591fSChristoph Hellwig return ret_blkg;
207028fc591fSChristoph Hellwig }
207128fc591fSChristoph Hellwig
207228fc591fSChristoph Hellwig /**
207328fc591fSChristoph Hellwig * bio_associate_blkg_from_css - associate a bio with a specified css
207428fc591fSChristoph Hellwig * @bio: target bio
207528fc591fSChristoph Hellwig * @css: target css
207628fc591fSChristoph Hellwig *
207728fc591fSChristoph Hellwig * Associate @bio with the blkg found by combining the css's blkg and the
207828fc591fSChristoph Hellwig * request_queue of the @bio. An association failure is handled by walking up
207928fc591fSChristoph Hellwig * the blkg tree. Therefore, the blkg associated can be anything between @blkg
208028fc591fSChristoph Hellwig * and q->root_blkg. This situation only happens when a cgroup is dying and
208128fc591fSChristoph Hellwig * then the remaining bios will spill to the closest alive blkg.
208228fc591fSChristoph Hellwig *
208328fc591fSChristoph Hellwig * A reference will be taken on the blkg and will be released when @bio is
208428fc591fSChristoph Hellwig * freed.
208528fc591fSChristoph Hellwig */
bio_associate_blkg_from_css(struct bio * bio,struct cgroup_subsys_state * css)208628fc591fSChristoph Hellwig void bio_associate_blkg_from_css(struct bio *bio,
208728fc591fSChristoph Hellwig struct cgroup_subsys_state *css)
208828fc591fSChristoph Hellwig {
208928fc591fSChristoph Hellwig if (bio->bi_blkg)
209028fc591fSChristoph Hellwig blkg_put(bio->bi_blkg);
209128fc591fSChristoph Hellwig
2092a5b97526SChristoph Hellwig if (css && css->parent) {
209313c7863dSChristoph Hellwig bio->bi_blkg = blkg_tryget_closest(bio, css);
2094a5b97526SChristoph Hellwig } else {
2095ed6cddefSPavel Begunkov blkg_get(bdev_get_queue(bio->bi_bdev)->root_blkg);
2096ed6cddefSPavel Begunkov bio->bi_blkg = bdev_get_queue(bio->bi_bdev)->root_blkg;
2097a5b97526SChristoph Hellwig }
209828fc591fSChristoph Hellwig }
209928fc591fSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
210028fc591fSChristoph Hellwig
210128fc591fSChristoph Hellwig /**
210228fc591fSChristoph Hellwig * bio_associate_blkg - associate a bio with a blkg
210328fc591fSChristoph Hellwig * @bio: target bio
210428fc591fSChristoph Hellwig *
210528fc591fSChristoph Hellwig * Associate @bio with the blkg found from the bio's css and request_queue.
210628fc591fSChristoph Hellwig * If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is
210728fc591fSChristoph Hellwig * already associated, the css is reused and association redone as the
210828fc591fSChristoph Hellwig * request_queue may have changed.
210928fc591fSChristoph Hellwig */
bio_associate_blkg(struct bio * bio)211028fc591fSChristoph Hellwig void bio_associate_blkg(struct bio *bio)
211128fc591fSChristoph Hellwig {
211228fc591fSChristoph Hellwig struct cgroup_subsys_state *css;
211328fc591fSChristoph Hellwig
211428fc591fSChristoph Hellwig rcu_read_lock();
211528fc591fSChristoph Hellwig
211628fc591fSChristoph Hellwig if (bio->bi_blkg)
2117bbb1ebe7SChristoph Hellwig css = bio_blkcg_css(bio);
211828fc591fSChristoph Hellwig else
211928fc591fSChristoph Hellwig css = blkcg_css();
212028fc591fSChristoph Hellwig
212128fc591fSChristoph Hellwig bio_associate_blkg_from_css(bio, css);
212228fc591fSChristoph Hellwig
212328fc591fSChristoph Hellwig rcu_read_unlock();
212428fc591fSChristoph Hellwig }
212528fc591fSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_associate_blkg);
212628fc591fSChristoph Hellwig
212728fc591fSChristoph Hellwig /**
212828fc591fSChristoph Hellwig * bio_clone_blkg_association - clone blkg association from src to dst bio
212928fc591fSChristoph Hellwig * @dst: destination bio
213028fc591fSChristoph Hellwig * @src: source bio
213128fc591fSChristoph Hellwig */
bio_clone_blkg_association(struct bio * dst,struct bio * src)213228fc591fSChristoph Hellwig void bio_clone_blkg_association(struct bio *dst, struct bio *src)
213328fc591fSChristoph Hellwig {
213422b106e5SJan Kara if (src->bi_blkg)
213522b106e5SJan Kara bio_associate_blkg_from_css(dst, bio_blkcg_css(src));
213628fc591fSChristoph Hellwig }
213728fc591fSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
213828fc591fSChristoph Hellwig
blk_cgroup_io_type(struct bio * bio)2139db18a53eSChristoph Hellwig static int blk_cgroup_io_type(struct bio *bio)
2140db18a53eSChristoph Hellwig {
2141db18a53eSChristoph Hellwig if (op_is_discard(bio->bi_opf))
2142db18a53eSChristoph Hellwig return BLKG_IOSTAT_DISCARD;
2143db18a53eSChristoph Hellwig if (op_is_write(bio->bi_opf))
2144db18a53eSChristoph Hellwig return BLKG_IOSTAT_WRITE;
2145db18a53eSChristoph Hellwig return BLKG_IOSTAT_READ;
2146db18a53eSChristoph Hellwig }
2147db18a53eSChristoph Hellwig
blk_cgroup_bio_start(struct bio * bio)2148db18a53eSChristoph Hellwig void blk_cgroup_bio_start(struct bio *bio)
2149db18a53eSChristoph Hellwig {
21503b8cc629SWaiman Long struct blkcg *blkcg = bio->bi_blkg->blkcg;
2151db18a53eSChristoph Hellwig int rwd = blk_cgroup_io_type(bio), cpu;
2152db18a53eSChristoph Hellwig struct blkg_iostat_set *bis;
21533c08b093STejun Heo unsigned long flags;
2154db18a53eSChristoph Hellwig
2155ad7c3b41SJinke Han if (!cgroup_subsys_on_dfl(io_cgrp_subsys))
2156ad7c3b41SJinke Han return;
2157ad7c3b41SJinke Han
21580416f3beSMing Lei /* Root-level stats are sourced from system-wide IO stats */
21590416f3beSMing Lei if (!cgroup_parent(blkcg->css.cgroup))
21600416f3beSMing Lei return;
21610416f3beSMing Lei
2162db18a53eSChristoph Hellwig cpu = get_cpu();
2163db18a53eSChristoph Hellwig bis = per_cpu_ptr(bio->bi_blkg->iostat_cpu, cpu);
21643c08b093STejun Heo flags = u64_stats_update_begin_irqsave(&bis->sync);
2165db18a53eSChristoph Hellwig
2166db18a53eSChristoph Hellwig /*
2167db18a53eSChristoph Hellwig * If the bio is flagged with BIO_CGROUP_ACCT it means this is a split
2168db18a53eSChristoph Hellwig * bio and we would have already accounted for the size of the bio.
2169db18a53eSChristoph Hellwig */
2170db18a53eSChristoph Hellwig if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
2171db18a53eSChristoph Hellwig bio_set_flag(bio, BIO_CGROUP_ACCT);
2172db18a53eSChristoph Hellwig bis->cur.bytes[rwd] += bio->bi_iter.bi_size;
2173db18a53eSChristoph Hellwig }
2174db18a53eSChristoph Hellwig bis->cur.ios[rwd]++;
2175db18a53eSChristoph Hellwig
21763b8cc629SWaiman Long /*
21773b8cc629SWaiman Long * If the iostat_cpu isn't in a lockless list, put it into the
21783b8cc629SWaiman Long * list to indicate that a stat update is pending.
21793b8cc629SWaiman Long */
21803b8cc629SWaiman Long if (!READ_ONCE(bis->lqueued)) {
21813b8cc629SWaiman Long struct llist_head *lhead = this_cpu_ptr(blkcg->lhead);
21823b8cc629SWaiman Long
21833b8cc629SWaiman Long llist_add(&bis->lnode, lhead);
21843b8cc629SWaiman Long WRITE_ONCE(bis->lqueued, true);
21853b8cc629SWaiman Long }
21863b8cc629SWaiman Long
21873c08b093STejun Heo u64_stats_update_end_irqrestore(&bis->sync, flags);
21883b8cc629SWaiman Long cgroup_rstat_updated(blkcg->css.cgroup, cpu);
2189db18a53eSChristoph Hellwig put_cpu();
2190db18a53eSChristoph Hellwig }
2191db18a53eSChristoph Hellwig
blk_cgroup_congested(void)2192216889aaSChristoph Hellwig bool blk_cgroup_congested(void)
2193216889aaSChristoph Hellwig {
2194216889aaSChristoph Hellwig struct cgroup_subsys_state *css;
2195216889aaSChristoph Hellwig bool ret = false;
2196216889aaSChristoph Hellwig
2197216889aaSChristoph Hellwig rcu_read_lock();
2198d200ca14SChristoph Hellwig for (css = blkcg_css(); css; css = css->parent) {
2199216889aaSChristoph Hellwig if (atomic_read(&css->cgroup->congestion_count)) {
2200216889aaSChristoph Hellwig ret = true;
2201216889aaSChristoph Hellwig break;
2202216889aaSChristoph Hellwig }
2203216889aaSChristoph Hellwig }
2204216889aaSChristoph Hellwig rcu_read_unlock();
2205216889aaSChristoph Hellwig return ret;
2206216889aaSChristoph Hellwig }
2207216889aaSChristoph Hellwig
2208903d23f0SJosef Bacik module_param(blkcg_debug_stats, bool, 0644);
2209903d23f0SJosef Bacik MODULE_PARM_DESC(blkcg_debug_stats, "True if you want debug stats, false if not");
2210