1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2eea8f41cSTejun Heo #ifndef _BLK_CGROUP_H 3eea8f41cSTejun Heo #define _BLK_CGROUP_H 4eea8f41cSTejun Heo /* 5eea8f41cSTejun Heo * Common Block IO controller cgroup interface 6eea8f41cSTejun Heo * 7eea8f41cSTejun Heo * Based on ideas and code from CFQ, CFS and BFQ: 8eea8f41cSTejun Heo * Copyright (C) 2003 Jens Axboe <[email protected]> 9eea8f41cSTejun Heo * 10eea8f41cSTejun Heo * Copyright (C) 2008 Fabio Checconi <[email protected]> 11eea8f41cSTejun Heo * Paolo Valente <[email protected]> 12eea8f41cSTejun Heo * 13eea8f41cSTejun Heo * Copyright (C) 2009 Vivek Goyal <[email protected]> 14eea8f41cSTejun Heo * Nauman Rafique <[email protected]> 15eea8f41cSTejun Heo */ 16eea8f41cSTejun Heo 17eea8f41cSTejun Heo #include <linux/cgroup.h> 18f7331648STejun Heo #include <linux/percpu.h> 1924bdb8efSTejun Heo #include <linux/percpu_counter.h> 20f7331648STejun Heo #include <linux/u64_stats_sync.h> 21eea8f41cSTejun Heo #include <linux/seq_file.h> 22eea8f41cSTejun Heo #include <linux/radix-tree.h> 23eea8f41cSTejun Heo #include <linux/blkdev.h> 24eea8f41cSTejun Heo #include <linux/atomic.h> 25902ec5b6SShaohua Li #include <linux/kthread.h> 265cdf2e3fSDennis Zhou #include <linux/fs.h> 27eea8f41cSTejun Heo 2824bdb8efSTejun Heo /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */ 2924bdb8efSTejun Heo #define BLKG_STAT_CPU_BATCH (INT_MAX / 2) 3024bdb8efSTejun Heo 31eea8f41cSTejun Heo /* Max limits for throttle policy */ 32eea8f41cSTejun Heo #define THROTL_IOPS_MAX UINT_MAX 33eea8f41cSTejun Heo 34eea8f41cSTejun Heo #ifdef CONFIG_BLK_CGROUP 35eea8f41cSTejun Heo 36f7331648STejun Heo enum blkg_iostat_type { 37f7331648STejun Heo BLKG_IOSTAT_READ, 38f7331648STejun Heo BLKG_IOSTAT_WRITE, 39f7331648STejun Heo BLKG_IOSTAT_DISCARD, 40f7331648STejun Heo 41f7331648STejun Heo BLKG_IOSTAT_NR, 42f7331648STejun Heo }; 43f7331648STejun Heo 44eea8f41cSTejun Heo struct blkcg_gq; 45eea8f41cSTejun Heo 46eea8f41cSTejun Heo struct blkcg { 47eea8f41cSTejun Heo struct cgroup_subsys_state css; 48eea8f41cSTejun Heo spinlock_t lock; 49d866dbf6STejun Heo refcount_t online_pin; 50eea8f41cSTejun Heo 51eea8f41cSTejun Heo struct radix_tree_root blkg_tree; 5255679c8dSBart Van Assche struct blkcg_gq __rcu *blkg_hint; 53eea8f41cSTejun Heo struct hlist_head blkg_list; 54eea8f41cSTejun Heo 5581437648STejun Heo struct blkcg_policy_data *cpd[BLKCG_MAX_POLS]; 5652ebea74STejun Heo 577876f930STejun Heo struct list_head all_blkcgs_node; 5852ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 5952ebea74STejun Heo struct list_head cgwb_list; 6052ebea74STejun Heo #endif 61eea8f41cSTejun Heo }; 62eea8f41cSTejun Heo 63f7331648STejun Heo struct blkg_iostat { 64f7331648STejun Heo u64 bytes[BLKG_IOSTAT_NR]; 65f7331648STejun Heo u64 ios[BLKG_IOSTAT_NR]; 66f7331648STejun Heo }; 67f7331648STejun Heo 68f7331648STejun Heo struct blkg_iostat_set { 69f7331648STejun Heo struct u64_stats_sync sync; 70f7331648STejun Heo struct blkg_iostat cur; 71f7331648STejun Heo struct blkg_iostat last; 72f7331648STejun Heo }; 73f7331648STejun Heo 74e6269c44STejun Heo /* 75eea8f41cSTejun Heo * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a 76eea8f41cSTejun Heo * request_queue (q). This is used by blkcg policies which need to track 77eea8f41cSTejun Heo * information per blkcg - q pair. 78eea8f41cSTejun Heo * 79001bea73STejun Heo * There can be multiple active blkcg policies and each blkg:policy pair is 80001bea73STejun Heo * represented by a blkg_policy_data which is allocated and freed by each 81001bea73STejun Heo * policy's pd_alloc/free_fn() methods. A policy can allocate private data 82001bea73STejun Heo * area by allocating larger data structure which embeds blkg_policy_data 83001bea73STejun Heo * at the beginning. 84eea8f41cSTejun Heo */ 85eea8f41cSTejun Heo struct blkg_policy_data { 86eea8f41cSTejun Heo /* the blkg and policy id this per-policy data belongs to */ 87eea8f41cSTejun Heo struct blkcg_gq *blkg; 88eea8f41cSTejun Heo int plid; 89eea8f41cSTejun Heo }; 90eea8f41cSTejun Heo 91e4bc13adSLinus Torvalds /* 92e4a9bde9STejun Heo * Policies that need to keep per-blkcg data which is independent from any 93e4a9bde9STejun Heo * request_queue associated to it should implement cpd_alloc/free_fn() 94e4a9bde9STejun Heo * methods. A policy can allocate private data area by allocating larger 95e4a9bde9STejun Heo * data structure which embeds blkcg_policy_data at the beginning. 96e4a9bde9STejun Heo * cpd_init() is invoked to let each policy handle per-blkcg data. 97e4bc13adSLinus Torvalds */ 98e4bc13adSLinus Torvalds struct blkcg_policy_data { 9981437648STejun Heo /* the blkcg and policy id this per-policy data belongs to */ 10081437648STejun Heo struct blkcg *blkcg; 101e4bc13adSLinus Torvalds int plid; 102e4bc13adSLinus Torvalds }; 103e4bc13adSLinus Torvalds 104eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */ 105eea8f41cSTejun Heo struct blkcg_gq { 106eea8f41cSTejun Heo /* Pointer to the associated request_queue */ 107eea8f41cSTejun Heo struct request_queue *q; 108eea8f41cSTejun Heo struct list_head q_node; 109eea8f41cSTejun Heo struct hlist_node blkcg_node; 110eea8f41cSTejun Heo struct blkcg *blkcg; 111eea8f41cSTejun Heo 112eea8f41cSTejun Heo /* all non-root blkcg_gq's are guaranteed to have access to parent */ 113eea8f41cSTejun Heo struct blkcg_gq *parent; 114eea8f41cSTejun Heo 115eea8f41cSTejun Heo /* reference count */ 1167fcf2b03SDennis Zhou struct percpu_ref refcnt; 117eea8f41cSTejun Heo 118eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 119eea8f41cSTejun Heo bool online; 120eea8f41cSTejun Heo 121f7331648STejun Heo struct blkg_iostat_set __percpu *iostat_cpu; 122f7331648STejun Heo struct blkg_iostat_set iostat; 12377ea7338STejun Heo 124eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 125eea8f41cSTejun Heo 126d3f77dfdSTejun Heo spinlock_t async_bio_lock; 127d3f77dfdSTejun Heo struct bio_list async_bios; 128d3f77dfdSTejun Heo struct work_struct async_bio_work; 129d09d8df3SJosef Bacik 130d09d8df3SJosef Bacik atomic_t use_delay; 131d09d8df3SJosef Bacik atomic64_t delay_nsec; 132d09d8df3SJosef Bacik atomic64_t delay_start; 133d09d8df3SJosef Bacik u64 last_delay; 134d09d8df3SJosef Bacik int last_use; 135d3f77dfdSTejun Heo 136d3f77dfdSTejun Heo struct rcu_head rcu_head; 137eea8f41cSTejun Heo }; 138eea8f41cSTejun Heo 139e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 14081437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 141e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 14269d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 143cf09a8eeSTejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, 144cf09a8eeSTejun Heo struct request_queue *q, struct blkcg *blkcg); 145a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 146a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 147a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 148001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 149a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 150903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 151903d23f0SJosef Bacik size_t size); 152eea8f41cSTejun Heo 153eea8f41cSTejun Heo struct blkcg_policy { 154eea8f41cSTejun Heo int plid; 155eea8f41cSTejun Heo /* cgroup files for the policy */ 1562ee867dcSTejun Heo struct cftype *dfl_cftypes; 157880f50e2STejun Heo struct cftype *legacy_cftypes; 158eea8f41cSTejun Heo 159eea8f41cSTejun Heo /* operations */ 160e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 161e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 162e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 16369d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 164e4a9bde9STejun Heo 165001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 166eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 167eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 168eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 169001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 170eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 171903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 172eea8f41cSTejun Heo }; 173eea8f41cSTejun Heo 174eea8f41cSTejun Heo extern struct blkcg blkcg_root; 175496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 17607b0fdecSTejun Heo extern bool blkcg_debug_stats; 177eea8f41cSTejun Heo 17824f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 17924f29046STejun Heo struct request_queue *q, bool update_hint); 180eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q); 181eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q); 182eea8f41cSTejun Heo 183eea8f41cSTejun Heo /* Blkio controller policy registration */ 184eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol); 185eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol); 186eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q, 187eea8f41cSTejun Heo const struct blkcg_policy *pol); 188eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q, 189eea8f41cSTejun Heo const struct blkcg_policy *pol); 190eea8f41cSTejun Heo 191dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg); 192eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, 193eea8f41cSTejun Heo u64 (*prfill)(struct seq_file *, 194eea8f41cSTejun Heo struct blkg_policy_data *, int), 195eea8f41cSTejun Heo const struct blkcg_policy *pol, int data, 196eea8f41cSTejun Heo bool show_total); 197eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); 198eea8f41cSTejun Heo 199eea8f41cSTejun Heo struct blkg_conf_ctx { 200*22ae8ce8SChristoph Hellwig struct block_device *bdev; 201eea8f41cSTejun Heo struct blkcg_gq *blkg; 20236aa9e5fSTejun Heo char *body; 203eea8f41cSTejun Heo }; 204eea8f41cSTejun Heo 205*22ae8ce8SChristoph Hellwig struct block_device *blkcg_conf_open_bdev(char **inputp); 206eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, 20736aa9e5fSTejun Heo char *input, struct blkg_conf_ctx *ctx); 208eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx); 209eea8f41cSTejun Heo 2100fe061b9SDennis Zhou /** 2110fe061b9SDennis Zhou * blkcg_css - find the current css 2120fe061b9SDennis Zhou * 2130fe061b9SDennis Zhou * Find the css associated with either the kthread or the current task. 2140fe061b9SDennis Zhou * This may return a dying css, so it is up to the caller to use tryget logic 2150fe061b9SDennis Zhou * to confirm it is alive and well. 2160fe061b9SDennis Zhou */ 2170fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_css(void) 2180fe061b9SDennis Zhou { 2190fe061b9SDennis Zhou struct cgroup_subsys_state *css; 2200fe061b9SDennis Zhou 2210fe061b9SDennis Zhou css = kthread_blkcg(); 2220fe061b9SDennis Zhou if (css) 2230fe061b9SDennis Zhou return css; 2240fe061b9SDennis Zhou return task_css(current, io_cgrp_id); 2250fe061b9SDennis Zhou } 2260fe061b9SDennis Zhou 227eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 228eea8f41cSTejun Heo { 229eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 230eea8f41cSTejun Heo } 231eea8f41cSTejun Heo 2320fe061b9SDennis Zhou /** 2330fe061b9SDennis Zhou * __bio_blkcg - internal, inconsistent version to get blkcg 2340fe061b9SDennis Zhou * 2350fe061b9SDennis Zhou * DO NOT USE. 2360fe061b9SDennis Zhou * This function is inconsistent and consequently is dangerous to use. The 2370fe061b9SDennis Zhou * first part of the function returns a blkcg where a reference is owned by the 2380fe061b9SDennis Zhou * bio. This means it does not need to be rcu protected as it cannot go away 2390fe061b9SDennis Zhou * with the bio owning a reference to it. However, the latter potentially gets 2400fe061b9SDennis Zhou * it from task_css(). This can race against task migration and the cgroup 2410fe061b9SDennis Zhou * dying. It is also semantically different as it must be called rcu protected 2420fe061b9SDennis Zhou * and is susceptible to failure when trying to get a reference to it. 2430fe061b9SDennis Zhou * Therefore, it is not ok to assume that *_get() will always succeed on the 2440fe061b9SDennis Zhou * blkcg returned here. 2450fe061b9SDennis Zhou */ 2460fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) 24727e6fa99SDennis Zhou (Facebook) { 248db6638d7SDennis Zhou if (bio && bio->bi_blkg) 249db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2500fe061b9SDennis Zhou return css_to_blkcg(blkcg_css()); 2510fe061b9SDennis Zhou } 2520fe061b9SDennis Zhou 2530fe061b9SDennis Zhou /** 2540fe061b9SDennis Zhou * bio_blkcg - grab the blkcg associated with a bio 2550fe061b9SDennis Zhou * @bio: target bio 2560fe061b9SDennis Zhou * 2570fe061b9SDennis Zhou * This returns the blkcg associated with a bio, %NULL if not associated. 2580fe061b9SDennis Zhou * Callers are expected to either handle %NULL or know association has been 2590fe061b9SDennis Zhou * done prior to calling this. 2600fe061b9SDennis Zhou */ 2610fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio) 2620fe061b9SDennis Zhou { 263db6638d7SDennis Zhou if (bio && bio->bi_blkg) 264db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2650fe061b9SDennis Zhou return NULL; 266fd383c2dSTejun Heo } 267fd383c2dSTejun Heo 268d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 269d09d8df3SJosef Bacik { 270d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 271d09d8df3SJosef Bacik bool ret = false; 272d09d8df3SJosef Bacik 273d09d8df3SJosef Bacik rcu_read_lock(); 274d09d8df3SJosef Bacik css = kthread_blkcg(); 275d09d8df3SJosef Bacik if (!css) 276d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 277d09d8df3SJosef Bacik while (css) { 278d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 279d09d8df3SJosef Bacik ret = true; 280d09d8df3SJosef Bacik break; 281d09d8df3SJosef Bacik } 282d09d8df3SJosef Bacik css = css->parent; 283d09d8df3SJosef Bacik } 284d09d8df3SJosef Bacik rcu_read_unlock(); 285d09d8df3SJosef Bacik return ret; 286d09d8df3SJosef Bacik } 287d09d8df3SJosef Bacik 288eea8f41cSTejun Heo /** 289c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 290c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 291c7c98fd3SJosef Bacik * 292c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 293c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 294c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 295c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 296c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 297c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 298c7c98fd3SJosef Bacik */ 299c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 300c7c98fd3SJosef Bacik { 3010d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 302c7c98fd3SJosef Bacik } 303c7c98fd3SJosef Bacik 304c7c98fd3SJosef Bacik /** 305eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 306eea8f41cSTejun Heo * @blkcg: blkcg of interest 307eea8f41cSTejun Heo * 308eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 309eea8f41cSTejun Heo */ 310eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 311eea8f41cSTejun Heo { 312eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 313eea8f41cSTejun Heo } 314eea8f41cSTejun Heo 315eea8f41cSTejun Heo /** 31624f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 31724f29046STejun Heo * @blkcg: blkcg of interest 31824f29046STejun Heo * @q: request_queue of interest 31924f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 32024f29046STejun Heo * 32124f29046STejun Heo * This is internal version and shouldn't be used by policy 32224f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 32324f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 32424f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 32524f29046STejun Heo */ 32624f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 32724f29046STejun Heo struct request_queue *q, 32824f29046STejun Heo bool update_hint) 32924f29046STejun Heo { 33024f29046STejun Heo struct blkcg_gq *blkg; 33124f29046STejun Heo 33285b6bc9dSTejun Heo if (blkcg == &blkcg_root) 33385b6bc9dSTejun Heo return q->root_blkg; 33485b6bc9dSTejun Heo 33524f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 33624f29046STejun Heo if (blkg && blkg->q == q) 33724f29046STejun Heo return blkg; 33824f29046STejun Heo 33924f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 34024f29046STejun Heo } 34124f29046STejun Heo 34224f29046STejun Heo /** 34324f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 34424f29046STejun Heo * @blkcg: blkcg of interest 34524f29046STejun Heo * @q: request_queue of interest 34624f29046STejun Heo * 34724f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 348012d4a65SBart Van Assche * under RCU read lock. 34924f29046STejun Heo */ 35024f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 35124f29046STejun Heo struct request_queue *q) 35224f29046STejun Heo { 35324f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 35424f29046STejun Heo return __blkg_lookup(blkcg, q, false); 35524f29046STejun Heo } 35624f29046STejun Heo 35724f29046STejun Heo /** 358b86d865cSBart Van Assche * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair 3596bad9b21SBart Van Assche * @q: request_queue of interest 3606bad9b21SBart Van Assche * 3616bad9b21SBart Van Assche * Lookup blkg for @q at the root level. See also blkg_lookup(). 3626bad9b21SBart Van Assche */ 363b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 3646bad9b21SBart Van Assche { 365b86d865cSBart Van Assche return q->root_blkg; 3666bad9b21SBart Van Assche } 3676bad9b21SBart Van Assche 3686bad9b21SBart Van Assche /** 369eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 370eea8f41cSTejun Heo * @blkg: blkg of interest 371eea8f41cSTejun Heo * @pol: policy of interest 372eea8f41cSTejun Heo * 373eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 374eea8f41cSTejun Heo */ 375eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 376eea8f41cSTejun Heo struct blkcg_policy *pol) 377eea8f41cSTejun Heo { 378eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 379eea8f41cSTejun Heo } 380eea8f41cSTejun Heo 381e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 382e4bc13adSLinus Torvalds struct blkcg_policy *pol) 383e4bc13adSLinus Torvalds { 38481437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 385e4bc13adSLinus Torvalds } 386e4bc13adSLinus Torvalds 387eea8f41cSTejun Heo /** 388eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 389eea8f41cSTejun Heo * @pd: policy private data of interest 390eea8f41cSTejun Heo * 391eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 392eea8f41cSTejun Heo */ 393eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 394eea8f41cSTejun Heo { 395eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 396eea8f41cSTejun Heo } 397eea8f41cSTejun Heo 39881437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 39981437648STejun Heo { 40081437648STejun Heo return cpd ? cpd->blkcg : NULL; 40181437648STejun Heo } 40281437648STejun Heo 40359b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg); 40459b57717SDennis Zhou (Facebook) 40559b57717SDennis Zhou (Facebook) /** 406d866dbf6STejun Heo * blkcg_pin_online - pin online state 40759b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 40859b57717SDennis Zhou (Facebook) * 409d866dbf6STejun Heo * While pinned, a blkcg is kept online. This is primarily used to 410d866dbf6STejun Heo * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline 411d866dbf6STejun Heo * while an associated cgwb is still active. 41259b57717SDennis Zhou (Facebook) */ 413d866dbf6STejun Heo static inline void blkcg_pin_online(struct blkcg *blkcg) 41459b57717SDennis Zhou (Facebook) { 415d866dbf6STejun Heo refcount_inc(&blkcg->online_pin); 41659b57717SDennis Zhou (Facebook) } 41759b57717SDennis Zhou (Facebook) 41859b57717SDennis Zhou (Facebook) /** 419d866dbf6STejun Heo * blkcg_unpin_online - unpin online state 42059b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 42159b57717SDennis Zhou (Facebook) * 422d866dbf6STejun Heo * This is primarily used to impedance-match blkg and cgwb lifetimes so 423d866dbf6STejun Heo * that blkg doesn't go offline while an associated cgwb is still active. 424d866dbf6STejun Heo * When this count goes to zero, all active cgwbs have finished so the 42559b57717SDennis Zhou (Facebook) * blkcg can continue destruction by calling blkcg_destroy_blkgs(). 42659b57717SDennis Zhou (Facebook) */ 427d866dbf6STejun Heo static inline void blkcg_unpin_online(struct blkcg *blkcg) 42859b57717SDennis Zhou (Facebook) { 4294308a434STejun Heo do { 4304308a434STejun Heo if (!refcount_dec_and_test(&blkcg->online_pin)) 4314308a434STejun Heo break; 43259b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 4334308a434STejun Heo blkcg = blkcg_parent(blkcg); 4344308a434STejun Heo } while (blkcg); 43559b57717SDennis Zhou (Facebook) } 43659b57717SDennis Zhou (Facebook) 437eea8f41cSTejun Heo /** 438eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 439eea8f41cSTejun Heo * @blkg: blkg of interest 440eea8f41cSTejun Heo * @buf: target buffer 441eea8f41cSTejun Heo * @buflen: target buffer length 442eea8f41cSTejun Heo * 443eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 444eea8f41cSTejun Heo */ 445eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 446eea8f41cSTejun Heo { 4474c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 448eea8f41cSTejun Heo } 449eea8f41cSTejun Heo 450eea8f41cSTejun Heo /** 451eea8f41cSTejun Heo * blkg_get - get a blkg reference 452eea8f41cSTejun Heo * @blkg: blkg to get 453eea8f41cSTejun Heo * 454eea8f41cSTejun Heo * The caller should be holding an existing reference. 455eea8f41cSTejun Heo */ 456eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 457eea8f41cSTejun Heo { 4587fcf2b03SDennis Zhou percpu_ref_get(&blkg->refcnt); 459eea8f41cSTejun Heo } 460eea8f41cSTejun Heo 461d09d8df3SJosef Bacik /** 4627754f669SDennis Zhou * blkg_tryget - try and get a blkg reference 463d09d8df3SJosef Bacik * @blkg: blkg to get 464d09d8df3SJosef Bacik * 465d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 466d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 467d09d8df3SJosef Bacik */ 4687754f669SDennis Zhou static inline bool blkg_tryget(struct blkcg_gq *blkg) 469d09d8df3SJosef Bacik { 4706ab21879SDennis Zhou return blkg && percpu_ref_tryget(&blkg->refcnt); 47107b05bccSDennis Zhou (Facebook) } 472d09d8df3SJosef Bacik 473beea9da0SDennis Zhou /** 474eea8f41cSTejun Heo * blkg_put - put a blkg reference 475eea8f41cSTejun Heo * @blkg: blkg to put 476eea8f41cSTejun Heo */ 477eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 478eea8f41cSTejun Heo { 4797fcf2b03SDennis Zhou percpu_ref_put(&blkg->refcnt); 480eea8f41cSTejun Heo } 481eea8f41cSTejun Heo 482eea8f41cSTejun Heo /** 483eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 484eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 485eea8f41cSTejun Heo * @pos_css: used for iteration 486eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 487eea8f41cSTejun Heo * 488eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 489eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 490eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 491eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 492eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 493eea8f41cSTejun Heo */ 494eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 495eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 496eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 497eea8f41cSTejun Heo (p_blkg)->q, false))) 498eea8f41cSTejun Heo 499eea8f41cSTejun Heo /** 500eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 501eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 502eea8f41cSTejun Heo * @pos_css: used for iteration 503eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 504eea8f41cSTejun Heo * 505eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 506eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 507eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 508eea8f41cSTejun Heo */ 509eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 510eea8f41cSTejun Heo css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ 511eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 512eea8f41cSTejun Heo (p_blkg)->q, false))) 513eea8f41cSTejun Heo 514d3f77dfdSTejun Heo bool __blkcg_punt_bio_submit(struct bio *bio); 515d3f77dfdSTejun Heo 516d3f77dfdSTejun Heo static inline bool blkcg_punt_bio_submit(struct bio *bio) 517d3f77dfdSTejun Heo { 518d3f77dfdSTejun Heo if (bio->bi_opf & REQ_CGROUP_PUNT) 519d3f77dfdSTejun Heo return __blkcg_punt_bio_submit(bio); 520d3f77dfdSTejun Heo else 521d3f77dfdSTejun Heo return false; 522d3f77dfdSTejun Heo } 523e439bedfSDennis Zhou 524e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) 525e439bedfSDennis Zhou { 526e439bedfSDennis Zhou bio_issue_init(&bio->bi_issue, bio_sectors(bio)); 527e439bedfSDennis Zhou } 528e439bedfSDennis Zhou 529d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 530d09d8df3SJosef Bacik { 53154c52e10STejun Heo if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0)) 53254c52e10STejun Heo return; 533d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 534d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 535d09d8df3SJosef Bacik } 536d09d8df3SJosef Bacik 537d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 538d09d8df3SJosef Bacik { 539d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 540d09d8df3SJosef Bacik 54154c52e10STejun Heo if (WARN_ON_ONCE(old < 0)) 54254c52e10STejun Heo return 0; 543d09d8df3SJosef Bacik if (old == 0) 544d09d8df3SJosef Bacik return 0; 545d09d8df3SJosef Bacik 546d09d8df3SJosef Bacik /* 547d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 548d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 549d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 550d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 551d09d8df3SJosef Bacik * congestion count on the cgroup. 552d09d8df3SJosef Bacik */ 553d09d8df3SJosef Bacik while (old) { 554d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 555d09d8df3SJosef Bacik if (cur == old) 556d09d8df3SJosef Bacik break; 557d09d8df3SJosef Bacik old = cur; 558d09d8df3SJosef Bacik } 559d09d8df3SJosef Bacik 560d09d8df3SJosef Bacik if (old == 0) 561d09d8df3SJosef Bacik return 0; 562d09d8df3SJosef Bacik if (old == 1) 563d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 564d09d8df3SJosef Bacik return 1; 565d09d8df3SJosef Bacik } 566d09d8df3SJosef Bacik 56754c52e10STejun Heo /** 56854c52e10STejun Heo * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount 56954c52e10STejun Heo * @blkg: target blkg 57054c52e10STejun Heo * @delay: delay duration in nsecs 57154c52e10STejun Heo * 57254c52e10STejun Heo * When enabled with this function, the delay is not decayed and must be 57354c52e10STejun Heo * explicitly cleared with blkcg_clear_delay(). Must not be mixed with 57454c52e10STejun Heo * blkcg_[un]use_delay() and blkcg_add_delay() usages. 57554c52e10STejun Heo */ 57654c52e10STejun Heo static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay) 57754c52e10STejun Heo { 57854c52e10STejun Heo int old = atomic_read(&blkg->use_delay); 57954c52e10STejun Heo 58054c52e10STejun Heo /* We only want 1 person setting the congestion count for this blkg. */ 58154c52e10STejun Heo if (!old && atomic_cmpxchg(&blkg->use_delay, old, -1) == old) 58254c52e10STejun Heo atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 58354c52e10STejun Heo 58454c52e10STejun Heo atomic64_set(&blkg->delay_nsec, delay); 58554c52e10STejun Heo } 58654c52e10STejun Heo 58754c52e10STejun Heo /** 58854c52e10STejun Heo * blkcg_clear_delay - Disable allocator delay mechanism 58954c52e10STejun Heo * @blkg: target blkg 59054c52e10STejun Heo * 59154c52e10STejun Heo * Disable use_delay mechanism. See blkcg_set_delay(). 59254c52e10STejun Heo */ 593d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 594d09d8df3SJosef Bacik { 595d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 59654c52e10STejun Heo 597d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 59854c52e10STejun Heo if (old && atomic_cmpxchg(&blkg->use_delay, old, 0) == old) 599d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 600d09d8df3SJosef Bacik } 601d09d8df3SJosef Bacik 602db18a53eSChristoph Hellwig void blk_cgroup_bio_start(struct bio *bio); 603d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 604d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 605d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 606eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 607eea8f41cSTejun Heo 608efa7d1c7STejun Heo struct blkcg { 609efa7d1c7STejun Heo }; 610eea8f41cSTejun Heo 611eea8f41cSTejun Heo struct blkg_policy_data { 612eea8f41cSTejun Heo }; 613eea8f41cSTejun Heo 614e4bc13adSLinus Torvalds struct blkcg_policy_data { 615e4bc13adSLinus Torvalds }; 616e4bc13adSLinus Torvalds 617eea8f41cSTejun Heo struct blkcg_gq { 618eea8f41cSTejun Heo }; 619eea8f41cSTejun Heo 620eea8f41cSTejun Heo struct blkcg_policy { 621eea8f41cSTejun Heo }; 622eea8f41cSTejun Heo 623496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 624496d5e75STejun Heo 625d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 626d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 627d09d8df3SJosef Bacik 628efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 629efa7d1c7STejun Heo 630d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 631d09d8df3SJosef Bacik 632eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 633b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 634b86d865cSBart Van Assche { return NULL; } 635eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 636eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 637eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 638eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 639eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 640eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 641eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 642eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 643eea8f41cSTejun Heo 6440fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; } 645eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 646eea8f41cSTejun Heo 647eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 648eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 649eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 650eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 651eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 652eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 653eea8f41cSTejun Heo 654d3f77dfdSTejun Heo static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; } 655e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) { } 656db18a53eSChristoph Hellwig static inline void blk_cgroup_bio_start(struct bio *bio) { } 657ae118896STejun Heo 658eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 659eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 660eea8f41cSTejun Heo 661efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 662eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 663eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 664