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> 18*f7331648STejun Heo #include <linux/percpu.h> 1924bdb8efSTejun Heo #include <linux/percpu_counter.h> 20*f7331648STejun 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 36*f7331648STejun Heo enum blkg_iostat_type { 37*f7331648STejun Heo BLKG_IOSTAT_READ, 38*f7331648STejun Heo BLKG_IOSTAT_WRITE, 39*f7331648STejun Heo BLKG_IOSTAT_DISCARD, 40*f7331648STejun Heo 41*f7331648STejun Heo BLKG_IOSTAT_NR, 42*f7331648STejun Heo }; 43*f7331648STejun Heo 44eea8f41cSTejun Heo enum blkg_rwstat_type { 45eea8f41cSTejun Heo BLKG_RWSTAT_READ, 46eea8f41cSTejun Heo BLKG_RWSTAT_WRITE, 47eea8f41cSTejun Heo BLKG_RWSTAT_SYNC, 48eea8f41cSTejun Heo BLKG_RWSTAT_ASYNC, 49636620b6STejun Heo BLKG_RWSTAT_DISCARD, 50eea8f41cSTejun Heo 51eea8f41cSTejun Heo BLKG_RWSTAT_NR, 52eea8f41cSTejun Heo BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR, 53eea8f41cSTejun Heo }; 54eea8f41cSTejun Heo 55eea8f41cSTejun Heo struct blkcg_gq; 56eea8f41cSTejun Heo 57eea8f41cSTejun Heo struct blkcg { 58eea8f41cSTejun Heo struct cgroup_subsys_state css; 59eea8f41cSTejun Heo spinlock_t lock; 60eea8f41cSTejun Heo 61eea8f41cSTejun Heo struct radix_tree_root blkg_tree; 6255679c8dSBart Van Assche struct blkcg_gq __rcu *blkg_hint; 63eea8f41cSTejun Heo struct hlist_head blkg_list; 64eea8f41cSTejun Heo 6581437648STejun Heo struct blkcg_policy_data *cpd[BLKCG_MAX_POLS]; 6652ebea74STejun Heo 677876f930STejun Heo struct list_head all_blkcgs_node; 6852ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 6952ebea74STejun Heo struct list_head cgwb_list; 7059b57717SDennis Zhou (Facebook) refcount_t cgwb_refcnt; 7152ebea74STejun Heo #endif 72eea8f41cSTejun Heo }; 73eea8f41cSTejun Heo 74*f7331648STejun Heo struct blkg_iostat { 75*f7331648STejun Heo u64 bytes[BLKG_IOSTAT_NR]; 76*f7331648STejun Heo u64 ios[BLKG_IOSTAT_NR]; 77*f7331648STejun Heo }; 78*f7331648STejun Heo 79*f7331648STejun Heo struct blkg_iostat_set { 80*f7331648STejun Heo struct u64_stats_sync sync; 81*f7331648STejun Heo struct blkg_iostat cur; 82*f7331648STejun Heo struct blkg_iostat last; 83*f7331648STejun Heo }; 84*f7331648STejun Heo 85e6269c44STejun Heo /* 86e6269c44STejun Heo * blkg_[rw]stat->aux_cnt is excluded for local stats but included for 877af6fd91SChristoph Hellwig * recursive. Used to carry stats of dead children. 88e6269c44STejun Heo */ 89eea8f41cSTejun Heo struct blkg_rwstat { 9024bdb8efSTejun Heo struct percpu_counter cpu_cnt[BLKG_RWSTAT_NR]; 91e6269c44STejun Heo atomic64_t aux_cnt[BLKG_RWSTAT_NR]; 92eea8f41cSTejun Heo }; 93eea8f41cSTejun Heo 947af6fd91SChristoph Hellwig struct blkg_rwstat_sample { 957af6fd91SChristoph Hellwig u64 cnt[BLKG_RWSTAT_NR]; 967af6fd91SChristoph Hellwig }; 977af6fd91SChristoph Hellwig 98eea8f41cSTejun Heo /* 99eea8f41cSTejun Heo * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a 100eea8f41cSTejun Heo * request_queue (q). This is used by blkcg policies which need to track 101eea8f41cSTejun Heo * information per blkcg - q pair. 102eea8f41cSTejun Heo * 103001bea73STejun Heo * There can be multiple active blkcg policies and each blkg:policy pair is 104001bea73STejun Heo * represented by a blkg_policy_data which is allocated and freed by each 105001bea73STejun Heo * policy's pd_alloc/free_fn() methods. A policy can allocate private data 106001bea73STejun Heo * area by allocating larger data structure which embeds blkg_policy_data 107001bea73STejun Heo * at the beginning. 108eea8f41cSTejun Heo */ 109eea8f41cSTejun Heo struct blkg_policy_data { 110eea8f41cSTejun Heo /* the blkg and policy id this per-policy data belongs to */ 111eea8f41cSTejun Heo struct blkcg_gq *blkg; 112eea8f41cSTejun Heo int plid; 113eea8f41cSTejun Heo }; 114eea8f41cSTejun Heo 115e4bc13adSLinus Torvalds /* 116e4a9bde9STejun Heo * Policies that need to keep per-blkcg data which is independent from any 117e4a9bde9STejun Heo * request_queue associated to it should implement cpd_alloc/free_fn() 118e4a9bde9STejun Heo * methods. A policy can allocate private data area by allocating larger 119e4a9bde9STejun Heo * data structure which embeds blkcg_policy_data at the beginning. 120e4a9bde9STejun Heo * cpd_init() is invoked to let each policy handle per-blkcg data. 121e4bc13adSLinus Torvalds */ 122e4bc13adSLinus Torvalds struct blkcg_policy_data { 12381437648STejun Heo /* the blkcg and policy id this per-policy data belongs to */ 12481437648STejun Heo struct blkcg *blkcg; 125e4bc13adSLinus Torvalds int plid; 126e4bc13adSLinus Torvalds }; 127e4bc13adSLinus Torvalds 128eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */ 129eea8f41cSTejun Heo struct blkcg_gq { 130eea8f41cSTejun Heo /* Pointer to the associated request_queue */ 131eea8f41cSTejun Heo struct request_queue *q; 132eea8f41cSTejun Heo struct list_head q_node; 133eea8f41cSTejun Heo struct hlist_node blkcg_node; 134eea8f41cSTejun Heo struct blkcg *blkcg; 135eea8f41cSTejun Heo 136ce7acfeaSTejun Heo /* 137ce7acfeaSTejun Heo * Each blkg gets congested separately and the congestion state is 138ce7acfeaSTejun Heo * propagated to the matching bdi_writeback_congested. 139ce7acfeaSTejun Heo */ 140ce7acfeaSTejun Heo struct bdi_writeback_congested *wb_congested; 141ce7acfeaSTejun Heo 142eea8f41cSTejun Heo /* all non-root blkcg_gq's are guaranteed to have access to parent */ 143eea8f41cSTejun Heo struct blkcg_gq *parent; 144eea8f41cSTejun Heo 145eea8f41cSTejun Heo /* reference count */ 1467fcf2b03SDennis Zhou struct percpu_ref refcnt; 147eea8f41cSTejun Heo 148eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 149eea8f41cSTejun Heo bool online; 150eea8f41cSTejun Heo 151*f7331648STejun Heo struct blkg_iostat_set __percpu *iostat_cpu; 152*f7331648STejun Heo struct blkg_iostat_set iostat; 15377ea7338STejun Heo 154eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 155eea8f41cSTejun Heo 156d3f77dfdSTejun Heo spinlock_t async_bio_lock; 157d3f77dfdSTejun Heo struct bio_list async_bios; 158d3f77dfdSTejun Heo struct work_struct async_bio_work; 159d09d8df3SJosef Bacik 160d09d8df3SJosef Bacik atomic_t use_delay; 161d09d8df3SJosef Bacik atomic64_t delay_nsec; 162d09d8df3SJosef Bacik atomic64_t delay_start; 163d09d8df3SJosef Bacik u64 last_delay; 164d09d8df3SJosef Bacik int last_use; 165d3f77dfdSTejun Heo 166d3f77dfdSTejun Heo struct rcu_head rcu_head; 167eea8f41cSTejun Heo }; 168eea8f41cSTejun Heo 169e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 17081437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 171e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 17269d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 173cf09a8eeSTejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, 174cf09a8eeSTejun Heo struct request_queue *q, struct blkcg *blkcg); 175a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 176a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 177a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 178001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 179a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 180903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 181903d23f0SJosef Bacik size_t size); 182eea8f41cSTejun Heo 183eea8f41cSTejun Heo struct blkcg_policy { 184eea8f41cSTejun Heo int plid; 185eea8f41cSTejun Heo /* cgroup files for the policy */ 1862ee867dcSTejun Heo struct cftype *dfl_cftypes; 187880f50e2STejun Heo struct cftype *legacy_cftypes; 188eea8f41cSTejun Heo 189eea8f41cSTejun Heo /* operations */ 190e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 191e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 192e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 19369d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 194e4a9bde9STejun Heo 195001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 196eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 197eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 198eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 199001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 200eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 201903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 202eea8f41cSTejun Heo }; 203eea8f41cSTejun Heo 204eea8f41cSTejun Heo extern struct blkcg blkcg_root; 205496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 20607b0fdecSTejun Heo extern bool blkcg_debug_stats; 207eea8f41cSTejun Heo 20824f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 20924f29046STejun Heo struct request_queue *q, bool update_hint); 210b978962aSDennis Zhou struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg, 211b978962aSDennis Zhou struct request_queue *q); 212eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, 213eea8f41cSTejun Heo struct request_queue *q); 214eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q); 215eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q); 216eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q); 217eea8f41cSTejun Heo 218eea8f41cSTejun Heo /* Blkio controller policy registration */ 219eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol); 220eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol); 221eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q, 222eea8f41cSTejun Heo const struct blkcg_policy *pol); 223eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q, 224eea8f41cSTejun Heo const struct blkcg_policy *pol); 225eea8f41cSTejun Heo 226239eeb08SChristoph Hellwig static inline u64 blkg_rwstat_read_counter(struct blkg_rwstat *rwstat, 227239eeb08SChristoph Hellwig unsigned int idx) 228239eeb08SChristoph Hellwig { 229239eeb08SChristoph Hellwig return atomic64_read(&rwstat->aux_cnt[idx]) + 230239eeb08SChristoph Hellwig percpu_counter_sum_positive(&rwstat->cpu_cnt[idx]); 231239eeb08SChristoph Hellwig } 232239eeb08SChristoph Hellwig 233dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg); 234eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, 235eea8f41cSTejun Heo u64 (*prfill)(struct seq_file *, 236eea8f41cSTejun Heo struct blkg_policy_data *, int), 237eea8f41cSTejun Heo const struct blkcg_policy *pol, int data, 238eea8f41cSTejun Heo bool show_total); 239eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); 240eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 2417af6fd91SChristoph Hellwig const struct blkg_rwstat_sample *rwstat); 242eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 243eea8f41cSTejun Heo int off); 2445d0b6e48SChristoph Hellwig void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol, 2457af6fd91SChristoph Hellwig int off, struct blkg_rwstat_sample *sum); 246eea8f41cSTejun Heo 247eea8f41cSTejun Heo struct blkg_conf_ctx { 248eea8f41cSTejun Heo struct gendisk *disk; 249eea8f41cSTejun Heo struct blkcg_gq *blkg; 25036aa9e5fSTejun Heo char *body; 251eea8f41cSTejun Heo }; 252eea8f41cSTejun Heo 253015d254cSTejun Heo struct gendisk *blkcg_conf_get_disk(char **inputp); 254eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, 25536aa9e5fSTejun Heo char *input, struct blkg_conf_ctx *ctx); 256eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx); 257eea8f41cSTejun Heo 2580fe061b9SDennis Zhou /** 2590fe061b9SDennis Zhou * blkcg_css - find the current css 2600fe061b9SDennis Zhou * 2610fe061b9SDennis Zhou * Find the css associated with either the kthread or the current task. 2620fe061b9SDennis Zhou * This may return a dying css, so it is up to the caller to use tryget logic 2630fe061b9SDennis Zhou * to confirm it is alive and well. 2640fe061b9SDennis Zhou */ 2650fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_css(void) 2660fe061b9SDennis Zhou { 2670fe061b9SDennis Zhou struct cgroup_subsys_state *css; 2680fe061b9SDennis Zhou 2690fe061b9SDennis Zhou css = kthread_blkcg(); 2700fe061b9SDennis Zhou if (css) 2710fe061b9SDennis Zhou return css; 2720fe061b9SDennis Zhou return task_css(current, io_cgrp_id); 2730fe061b9SDennis Zhou } 2740fe061b9SDennis Zhou 275eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 276eea8f41cSTejun Heo { 277eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 278eea8f41cSTejun Heo } 279eea8f41cSTejun Heo 2800fe061b9SDennis Zhou /** 2810fe061b9SDennis Zhou * __bio_blkcg - internal, inconsistent version to get blkcg 2820fe061b9SDennis Zhou * 2830fe061b9SDennis Zhou * DO NOT USE. 2840fe061b9SDennis Zhou * This function is inconsistent and consequently is dangerous to use. The 2850fe061b9SDennis Zhou * first part of the function returns a blkcg where a reference is owned by the 2860fe061b9SDennis Zhou * bio. This means it does not need to be rcu protected as it cannot go away 2870fe061b9SDennis Zhou * with the bio owning a reference to it. However, the latter potentially gets 2880fe061b9SDennis Zhou * it from task_css(). This can race against task migration and the cgroup 2890fe061b9SDennis Zhou * dying. It is also semantically different as it must be called rcu protected 2900fe061b9SDennis Zhou * and is susceptible to failure when trying to get a reference to it. 2910fe061b9SDennis Zhou * Therefore, it is not ok to assume that *_get() will always succeed on the 2920fe061b9SDennis Zhou * blkcg returned here. 2930fe061b9SDennis Zhou */ 2940fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) 29527e6fa99SDennis Zhou (Facebook) { 296db6638d7SDennis Zhou if (bio && bio->bi_blkg) 297db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2980fe061b9SDennis Zhou return css_to_blkcg(blkcg_css()); 2990fe061b9SDennis Zhou } 3000fe061b9SDennis Zhou 3010fe061b9SDennis Zhou /** 3020fe061b9SDennis Zhou * bio_blkcg - grab the blkcg associated with a bio 3030fe061b9SDennis Zhou * @bio: target bio 3040fe061b9SDennis Zhou * 3050fe061b9SDennis Zhou * This returns the blkcg associated with a bio, %NULL if not associated. 3060fe061b9SDennis Zhou * Callers are expected to either handle %NULL or know association has been 3070fe061b9SDennis Zhou * done prior to calling this. 3080fe061b9SDennis Zhou */ 3090fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio) 3100fe061b9SDennis Zhou { 311db6638d7SDennis Zhou if (bio && bio->bi_blkg) 312db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 3130fe061b9SDennis Zhou return NULL; 314fd383c2dSTejun Heo } 315fd383c2dSTejun Heo 316d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 317d09d8df3SJosef Bacik { 318d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 319d09d8df3SJosef Bacik bool ret = false; 320d09d8df3SJosef Bacik 321d09d8df3SJosef Bacik rcu_read_lock(); 322d09d8df3SJosef Bacik css = kthread_blkcg(); 323d09d8df3SJosef Bacik if (!css) 324d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 325d09d8df3SJosef Bacik while (css) { 326d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 327d09d8df3SJosef Bacik ret = true; 328d09d8df3SJosef Bacik break; 329d09d8df3SJosef Bacik } 330d09d8df3SJosef Bacik css = css->parent; 331d09d8df3SJosef Bacik } 332d09d8df3SJosef Bacik rcu_read_unlock(); 333d09d8df3SJosef Bacik return ret; 334d09d8df3SJosef Bacik } 335d09d8df3SJosef Bacik 336eea8f41cSTejun Heo /** 337c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 338c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 339c7c98fd3SJosef Bacik * 340c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 341c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 342c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 343c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 344c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 345c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 346c7c98fd3SJosef Bacik */ 347c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 348c7c98fd3SJosef Bacik { 3490d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 350c7c98fd3SJosef Bacik } 351c7c98fd3SJosef Bacik 352c7c98fd3SJosef Bacik /** 353eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 354eea8f41cSTejun Heo * @blkcg: blkcg of interest 355eea8f41cSTejun Heo * 356eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 357eea8f41cSTejun Heo */ 358eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 359eea8f41cSTejun Heo { 360eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 361eea8f41cSTejun Heo } 362eea8f41cSTejun Heo 363eea8f41cSTejun Heo /** 36424f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 36524f29046STejun Heo * @blkcg: blkcg of interest 36624f29046STejun Heo * @q: request_queue of interest 36724f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 36824f29046STejun Heo * 36924f29046STejun Heo * This is internal version and shouldn't be used by policy 37024f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 37124f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 37224f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 37324f29046STejun Heo */ 37424f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 37524f29046STejun Heo struct request_queue *q, 37624f29046STejun Heo bool update_hint) 37724f29046STejun Heo { 37824f29046STejun Heo struct blkcg_gq *blkg; 37924f29046STejun Heo 38085b6bc9dSTejun Heo if (blkcg == &blkcg_root) 38185b6bc9dSTejun Heo return q->root_blkg; 38285b6bc9dSTejun Heo 38324f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 38424f29046STejun Heo if (blkg && blkg->q == q) 38524f29046STejun Heo return blkg; 38624f29046STejun Heo 38724f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 38824f29046STejun Heo } 38924f29046STejun Heo 39024f29046STejun Heo /** 39124f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 39224f29046STejun Heo * @blkcg: blkcg of interest 39324f29046STejun Heo * @q: request_queue of interest 39424f29046STejun Heo * 39524f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 396012d4a65SBart Van Assche * under RCU read lock. 39724f29046STejun Heo */ 39824f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 39924f29046STejun Heo struct request_queue *q) 40024f29046STejun Heo { 40124f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 40224f29046STejun Heo return __blkg_lookup(blkcg, q, false); 40324f29046STejun Heo } 40424f29046STejun Heo 40524f29046STejun Heo /** 406b86d865cSBart Van Assche * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair 4076bad9b21SBart Van Assche * @q: request_queue of interest 4086bad9b21SBart Van Assche * 4096bad9b21SBart Van Assche * Lookup blkg for @q at the root level. See also blkg_lookup(). 4106bad9b21SBart Van Assche */ 411b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 4126bad9b21SBart Van Assche { 413b86d865cSBart Van Assche return q->root_blkg; 4146bad9b21SBart Van Assche } 4156bad9b21SBart Van Assche 4166bad9b21SBart Van Assche /** 417eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 418eea8f41cSTejun Heo * @blkg: blkg of interest 419eea8f41cSTejun Heo * @pol: policy of interest 420eea8f41cSTejun Heo * 421eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 422eea8f41cSTejun Heo */ 423eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 424eea8f41cSTejun Heo struct blkcg_policy *pol) 425eea8f41cSTejun Heo { 426eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 427eea8f41cSTejun Heo } 428eea8f41cSTejun Heo 429e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 430e4bc13adSLinus Torvalds struct blkcg_policy *pol) 431e4bc13adSLinus Torvalds { 43281437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 433e4bc13adSLinus Torvalds } 434e4bc13adSLinus Torvalds 435eea8f41cSTejun Heo /** 436eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 437eea8f41cSTejun Heo * @pd: policy private data of interest 438eea8f41cSTejun Heo * 439eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 440eea8f41cSTejun Heo */ 441eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 442eea8f41cSTejun Heo { 443eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 444eea8f41cSTejun Heo } 445eea8f41cSTejun Heo 44681437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 44781437648STejun Heo { 44881437648STejun Heo return cpd ? cpd->blkcg : NULL; 44981437648STejun Heo } 45081437648STejun Heo 45159b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg); 45259b57717SDennis Zhou (Facebook) 45359b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK 45459b57717SDennis Zhou (Facebook) 45559b57717SDennis Zhou (Facebook) /** 45659b57717SDennis Zhou (Facebook) * blkcg_cgwb_get - get a reference for blkcg->cgwb_list 45759b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 45859b57717SDennis Zhou (Facebook) * 45959b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 46059b57717SDennis Zhou (Facebook) */ 46159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) 46259b57717SDennis Zhou (Facebook) { 46359b57717SDennis Zhou (Facebook) refcount_inc(&blkcg->cgwb_refcnt); 46459b57717SDennis Zhou (Facebook) } 46559b57717SDennis Zhou (Facebook) 46659b57717SDennis Zhou (Facebook) /** 46759b57717SDennis Zhou (Facebook) * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list 46859b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 46959b57717SDennis Zhou (Facebook) * 47059b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 47159b57717SDennis Zhou (Facebook) * When this count goes to zero, all active wb has finished so the 47259b57717SDennis Zhou (Facebook) * blkcg can continue destruction by calling blkcg_destroy_blkgs(). 47359b57717SDennis Zhou (Facebook) * This work may occur in cgwb_release_workfn() on the cgwb_release 47459b57717SDennis Zhou (Facebook) * workqueue. 47559b57717SDennis Zhou (Facebook) */ 47659b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 47759b57717SDennis Zhou (Facebook) { 47859b57717SDennis Zhou (Facebook) if (refcount_dec_and_test(&blkcg->cgwb_refcnt)) 47959b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 48059b57717SDennis Zhou (Facebook) } 48159b57717SDennis Zhou (Facebook) 48259b57717SDennis Zhou (Facebook) #else 48359b57717SDennis Zhou (Facebook) 48459b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { } 48559b57717SDennis Zhou (Facebook) 48659b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 48759b57717SDennis Zhou (Facebook) { 48859b57717SDennis Zhou (Facebook) /* wb isn't being accounted, so trigger destruction right away */ 48959b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 49059b57717SDennis Zhou (Facebook) } 49159b57717SDennis Zhou (Facebook) 49259b57717SDennis Zhou (Facebook) #endif 49359b57717SDennis Zhou (Facebook) 494eea8f41cSTejun Heo /** 495eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 496eea8f41cSTejun Heo * @blkg: blkg of interest 497eea8f41cSTejun Heo * @buf: target buffer 498eea8f41cSTejun Heo * @buflen: target buffer length 499eea8f41cSTejun Heo * 500eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 501eea8f41cSTejun Heo */ 502eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 503eea8f41cSTejun Heo { 5044c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 505eea8f41cSTejun Heo } 506eea8f41cSTejun Heo 507eea8f41cSTejun Heo /** 508eea8f41cSTejun Heo * blkg_get - get a blkg reference 509eea8f41cSTejun Heo * @blkg: blkg to get 510eea8f41cSTejun Heo * 511eea8f41cSTejun Heo * The caller should be holding an existing reference. 512eea8f41cSTejun Heo */ 513eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 514eea8f41cSTejun Heo { 5157fcf2b03SDennis Zhou percpu_ref_get(&blkg->refcnt); 516eea8f41cSTejun Heo } 517eea8f41cSTejun Heo 518d09d8df3SJosef Bacik /** 5197754f669SDennis Zhou * blkg_tryget - try and get a blkg reference 520d09d8df3SJosef Bacik * @blkg: blkg to get 521d09d8df3SJosef Bacik * 522d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 523d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 524d09d8df3SJosef Bacik */ 5257754f669SDennis Zhou static inline bool blkg_tryget(struct blkcg_gq *blkg) 526d09d8df3SJosef Bacik { 5276ab21879SDennis Zhou return blkg && percpu_ref_tryget(&blkg->refcnt); 52807b05bccSDennis Zhou (Facebook) } 529d09d8df3SJosef Bacik 530beea9da0SDennis Zhou /** 5317754f669SDennis Zhou * blkg_tryget_closest - try and get a blkg ref on the closet blkg 532beea9da0SDennis Zhou * @blkg: blkg to get 533beea9da0SDennis Zhou * 5346ab21879SDennis Zhou * This needs to be called rcu protected. As the failure mode here is to walk 5356ab21879SDennis Zhou * up the blkg tree, this ensure that the blkg->parent pointers are always 5366ab21879SDennis Zhou * valid. This returns the blkg that it ended up taking a reference on or %NULL 5376ab21879SDennis Zhou * if no reference was taken. 538beea9da0SDennis Zhou */ 5397754f669SDennis Zhou static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg) 540beea9da0SDennis Zhou { 5416ab21879SDennis Zhou struct blkcg_gq *ret_blkg = NULL; 542beea9da0SDennis Zhou 5436ab21879SDennis Zhou WARN_ON_ONCE(!rcu_read_lock_held()); 5446ab21879SDennis Zhou 5456ab21879SDennis Zhou while (blkg) { 5466ab21879SDennis Zhou if (blkg_tryget(blkg)) { 5476ab21879SDennis Zhou ret_blkg = blkg; 5486ab21879SDennis Zhou break; 5496ab21879SDennis Zhou } 5506ab21879SDennis Zhou blkg = blkg->parent; 5516ab21879SDennis Zhou } 5526ab21879SDennis Zhou 5536ab21879SDennis Zhou return ret_blkg; 554beea9da0SDennis Zhou } 555b5f2954dSDennis Zhou 556eea8f41cSTejun Heo /** 557eea8f41cSTejun Heo * blkg_put - put a blkg reference 558eea8f41cSTejun Heo * @blkg: blkg to put 559eea8f41cSTejun Heo */ 560eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 561eea8f41cSTejun Heo { 5627fcf2b03SDennis Zhou percpu_ref_put(&blkg->refcnt); 563eea8f41cSTejun Heo } 564eea8f41cSTejun Heo 565eea8f41cSTejun Heo /** 566eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 567eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 568eea8f41cSTejun Heo * @pos_css: used for iteration 569eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 570eea8f41cSTejun Heo * 571eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 572eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 573eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 574eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 575eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 576eea8f41cSTejun Heo */ 577eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 578eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 579eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 580eea8f41cSTejun Heo (p_blkg)->q, false))) 581eea8f41cSTejun Heo 582eea8f41cSTejun Heo /** 583eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 584eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 585eea8f41cSTejun Heo * @pos_css: used for iteration 586eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 587eea8f41cSTejun Heo * 588eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 589eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 590eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 591eea8f41cSTejun Heo */ 592eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 593eea8f41cSTejun Heo css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ 594eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 595eea8f41cSTejun Heo (p_blkg)->q, false))) 596eea8f41cSTejun Heo 59724bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp) 59824bdb8efSTejun Heo { 59924bdb8efSTejun Heo int i, ret; 60024bdb8efSTejun Heo 60124bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 60224bdb8efSTejun Heo ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp); 60324bdb8efSTejun Heo if (ret) { 60424bdb8efSTejun Heo while (--i >= 0) 60524bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 60624bdb8efSTejun Heo return ret; 60724bdb8efSTejun Heo } 60824bdb8efSTejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 60924bdb8efSTejun Heo } 61024bdb8efSTejun Heo return 0; 61124bdb8efSTejun Heo } 61224bdb8efSTejun Heo 61324bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat) 614eea8f41cSTejun Heo { 615e6269c44STejun Heo int i; 616e6269c44STejun Heo 617e6269c44STejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 61824bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 619eea8f41cSTejun Heo } 620eea8f41cSTejun Heo 621eea8f41cSTejun Heo /** 622eea8f41cSTejun Heo * blkg_rwstat_add - add a value to a blkg_rwstat 623eea8f41cSTejun Heo * @rwstat: target blkg_rwstat 624ef295ecfSChristoph Hellwig * @op: REQ_OP and flags 625eea8f41cSTejun Heo * @val: value to add 626eea8f41cSTejun Heo * 627eea8f41cSTejun Heo * Add @val to @rwstat. The counters are chosen according to @rw. The 628eea8f41cSTejun Heo * caller is responsible for synchronizing calls to this function. 629eea8f41cSTejun Heo */ 630eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, 631ef295ecfSChristoph Hellwig unsigned int op, uint64_t val) 632eea8f41cSTejun Heo { 63324bdb8efSTejun Heo struct percpu_counter *cnt; 634eea8f41cSTejun Heo 635636620b6STejun Heo if (op_is_discard(op)) 636636620b6STejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD]; 637636620b6STejun Heo else if (op_is_write(op)) 63824bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE]; 639eea8f41cSTejun Heo else 64024bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ]; 641eea8f41cSTejun Heo 642104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 64324bdb8efSTejun Heo 644d71d9ae1SChristoph Hellwig if (op_is_sync(op)) 64524bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC]; 64624bdb8efSTejun Heo else 64724bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC]; 64824bdb8efSTejun Heo 649104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 650eea8f41cSTejun Heo } 651eea8f41cSTejun Heo 652eea8f41cSTejun Heo /** 653eea8f41cSTejun Heo * blkg_rwstat_read - read the current values of a blkg_rwstat 654eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 655eea8f41cSTejun Heo * 65624bdb8efSTejun Heo * Read the current snapshot of @rwstat and return it in the aux counts. 657eea8f41cSTejun Heo */ 6585d0b6e48SChristoph Hellwig static inline void blkg_rwstat_read(struct blkg_rwstat *rwstat, 6597af6fd91SChristoph Hellwig struct blkg_rwstat_sample *result) 660eea8f41cSTejun Heo { 66124bdb8efSTejun Heo int i; 662eea8f41cSTejun Heo 66324bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 6647af6fd91SChristoph Hellwig result->cnt[i] = 6657af6fd91SChristoph Hellwig percpu_counter_sum_positive(&rwstat->cpu_cnt[i]); 666eea8f41cSTejun Heo } 667eea8f41cSTejun Heo 668eea8f41cSTejun Heo /** 669eea8f41cSTejun Heo * blkg_rwstat_total - read the total count of a blkg_rwstat 670eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 671eea8f41cSTejun Heo * 672eea8f41cSTejun Heo * Return the total count of @rwstat regardless of the IO direction. This 673eea8f41cSTejun Heo * function can be called without synchronization and takes care of u64 674eea8f41cSTejun Heo * atomicity. 675eea8f41cSTejun Heo */ 676eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat) 677eea8f41cSTejun Heo { 6787af6fd91SChristoph Hellwig struct blkg_rwstat_sample tmp = { }; 679eea8f41cSTejun Heo 6805d0b6e48SChristoph Hellwig blkg_rwstat_read(rwstat, &tmp); 6817af6fd91SChristoph Hellwig return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]; 682eea8f41cSTejun Heo } 683eea8f41cSTejun Heo 684eea8f41cSTejun Heo /** 685eea8f41cSTejun Heo * blkg_rwstat_reset - reset a blkg_rwstat 686eea8f41cSTejun Heo * @rwstat: blkg_rwstat to reset 687eea8f41cSTejun Heo */ 688eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) 689eea8f41cSTejun Heo { 690e6269c44STejun Heo int i; 691e6269c44STejun Heo 69224bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 69324bdb8efSTejun Heo percpu_counter_set(&rwstat->cpu_cnt[i], 0); 694e6269c44STejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 695eea8f41cSTejun Heo } 69624bdb8efSTejun Heo } 697eea8f41cSTejun Heo 698eea8f41cSTejun Heo /** 699e6269c44STejun Heo * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count 700eea8f41cSTejun Heo * @to: the destination blkg_rwstat 701eea8f41cSTejun Heo * @from: the source 702eea8f41cSTejun Heo * 703e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 704eea8f41cSTejun Heo */ 705e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to, 706eea8f41cSTejun Heo struct blkg_rwstat *from) 707eea8f41cSTejun Heo { 708ddc21231SArnd Bergmann u64 sum[BLKG_RWSTAT_NR]; 709eea8f41cSTejun Heo int i; 710eea8f41cSTejun Heo 711eea8f41cSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 712ddc21231SArnd Bergmann sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]); 713ddc21231SArnd Bergmann 714ddc21231SArnd Bergmann for (i = 0; i < BLKG_RWSTAT_NR; i++) 715ddc21231SArnd Bergmann atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]), 716e6269c44STejun Heo &to->aux_cnt[i]); 717eea8f41cSTejun Heo } 718eea8f41cSTejun Heo 719ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING 720ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 721ae118896STejun Heo struct bio *bio); 722ae118896STejun Heo #else 723ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 724ae118896STejun Heo struct bio *bio) { return false; } 725ae118896STejun Heo #endif 726ae118896STejun Heo 727d3f77dfdSTejun Heo bool __blkcg_punt_bio_submit(struct bio *bio); 728d3f77dfdSTejun Heo 729d3f77dfdSTejun Heo static inline bool blkcg_punt_bio_submit(struct bio *bio) 730d3f77dfdSTejun Heo { 731d3f77dfdSTejun Heo if (bio->bi_opf & REQ_CGROUP_PUNT) 732d3f77dfdSTejun Heo return __blkcg_punt_bio_submit(bio); 733d3f77dfdSTejun Heo else 734d3f77dfdSTejun Heo return false; 735d3f77dfdSTejun Heo } 736e439bedfSDennis Zhou 737e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) 738e439bedfSDennis Zhou { 739e439bedfSDennis Zhou bio_issue_init(&bio->bi_issue, bio_sectors(bio)); 740e439bedfSDennis Zhou } 741e439bedfSDennis Zhou 742ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 743ae118896STejun Heo struct bio *bio) 744ae118896STejun Heo { 745ae118896STejun Heo struct blkcg_gq *blkg; 746ae118896STejun Heo bool throtl = false; 747ae118896STejun Heo 7484705de73SDennis Zhou rcu_read_lock(); 7494705de73SDennis Zhou 7505cdf2e3fSDennis Zhou if (!bio->bi_blkg) { 7515cdf2e3fSDennis Zhou char b[BDEVNAME_SIZE]; 752ae118896STejun Heo 7535cdf2e3fSDennis Zhou WARN_ONCE(1, 7545cdf2e3fSDennis Zhou "no blkg associated for bio on block-device: %s\n", 7555cdf2e3fSDennis Zhou bio_devname(bio, b)); 7565cdf2e3fSDennis Zhou bio_associate_blkg(bio); 7575cdf2e3fSDennis Zhou } 7585cdf2e3fSDennis Zhou 7595cdf2e3fSDennis Zhou blkg = bio->bi_blkg; 760ae118896STejun Heo 761ae118896STejun Heo throtl = blk_throtl_bio(q, blkg, bio); 762ae118896STejun Heo 76377ea7338STejun Heo if (!throtl) { 764*f7331648STejun Heo struct blkg_iostat_set *bis; 765*f7331648STejun Heo int rwd, cpu; 766*f7331648STejun Heo 767*f7331648STejun Heo if (op_is_discard(bio->bi_opf)) 768*f7331648STejun Heo rwd = BLKG_IOSTAT_DISCARD; 769*f7331648STejun Heo else if (op_is_write(bio->bi_opf)) 770*f7331648STejun Heo rwd = BLKG_IOSTAT_WRITE; 771*f7331648STejun Heo else 772*f7331648STejun Heo rwd = BLKG_IOSTAT_READ; 773*f7331648STejun Heo 774*f7331648STejun Heo cpu = get_cpu(); 775*f7331648STejun Heo bis = per_cpu_ptr(blkg->iostat_cpu, cpu); 776*f7331648STejun Heo u64_stats_update_begin(&bis->sync); 777*f7331648STejun Heo 778c454edc2SJosef Bacik /* 779c454edc2SJosef Bacik * If the bio is flagged with BIO_QUEUE_ENTERED it means this 780c454edc2SJosef Bacik * is a split bio and we would have already accounted for the 781c454edc2SJosef Bacik * size of the bio. 782c454edc2SJosef Bacik */ 783c454edc2SJosef Bacik if (!bio_flagged(bio, BIO_QUEUE_ENTERED)) 784*f7331648STejun Heo bis->cur.bytes[rwd] += bio->bi_iter.bi_size; 785*f7331648STejun Heo bis->cur.ios[rwd]++; 786*f7331648STejun Heo 787*f7331648STejun Heo u64_stats_update_end(&bis->sync); 788*f7331648STejun Heo cgroup_rstat_updated(blkg->blkcg->css.cgroup, cpu); 789*f7331648STejun Heo put_cpu(); 79077ea7338STejun Heo } 79177ea7338STejun Heo 792e439bedfSDennis Zhou blkcg_bio_issue_init(bio); 793e439bedfSDennis Zhou 7944705de73SDennis Zhou rcu_read_unlock(); 795ae118896STejun Heo return !throtl; 796ae118896STejun Heo } 797ae118896STejun Heo 798d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 799d09d8df3SJosef Bacik { 800d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 801d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 802d09d8df3SJosef Bacik } 803d09d8df3SJosef Bacik 804d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 805d09d8df3SJosef Bacik { 806d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 807d09d8df3SJosef Bacik 808d09d8df3SJosef Bacik if (old == 0) 809d09d8df3SJosef Bacik return 0; 810d09d8df3SJosef Bacik 811d09d8df3SJosef Bacik /* 812d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 813d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 814d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 815d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 816d09d8df3SJosef Bacik * congestion count on the cgroup. 817d09d8df3SJosef Bacik */ 818d09d8df3SJosef Bacik while (old) { 819d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 820d09d8df3SJosef Bacik if (cur == old) 821d09d8df3SJosef Bacik break; 822d09d8df3SJosef Bacik old = cur; 823d09d8df3SJosef Bacik } 824d09d8df3SJosef Bacik 825d09d8df3SJosef Bacik if (old == 0) 826d09d8df3SJosef Bacik return 0; 827d09d8df3SJosef Bacik if (old == 1) 828d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 829d09d8df3SJosef Bacik return 1; 830d09d8df3SJosef Bacik } 831d09d8df3SJosef Bacik 832d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 833d09d8df3SJosef Bacik { 834d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 835d09d8df3SJosef Bacik if (!old) 836d09d8df3SJosef Bacik return; 837d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 838d09d8df3SJosef Bacik while (old) { 839d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); 840d09d8df3SJosef Bacik if (cur == old) { 841d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 842d09d8df3SJosef Bacik break; 843d09d8df3SJosef Bacik } 844d09d8df3SJosef Bacik old = cur; 845d09d8df3SJosef Bacik } 846d09d8df3SJosef Bacik } 847d09d8df3SJosef Bacik 848d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 849d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 850d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 851eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 852eea8f41cSTejun Heo 853efa7d1c7STejun Heo struct blkcg { 854efa7d1c7STejun Heo }; 855eea8f41cSTejun Heo 856eea8f41cSTejun Heo struct blkg_policy_data { 857eea8f41cSTejun Heo }; 858eea8f41cSTejun Heo 859e4bc13adSLinus Torvalds struct blkcg_policy_data { 860e4bc13adSLinus Torvalds }; 861e4bc13adSLinus Torvalds 862eea8f41cSTejun Heo struct blkcg_gq { 863eea8f41cSTejun Heo }; 864eea8f41cSTejun Heo 865eea8f41cSTejun Heo struct blkcg_policy { 866eea8f41cSTejun Heo }; 867eea8f41cSTejun Heo 868496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 869496d5e75STejun Heo 870d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 871d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 872d09d8df3SJosef Bacik 873efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 874efa7d1c7STejun Heo 875d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 876d09d8df3SJosef Bacik 877eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 878b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 879b86d865cSBart Van Assche { return NULL; } 880eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 881eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { } 882eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 883eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 884eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 885eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 886eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 887eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 888eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 889eea8f41cSTejun Heo 8900fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; } 891eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 892eea8f41cSTejun Heo 893eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 894eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 895eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 896eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 897eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 898eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 899eea8f41cSTejun Heo 900d3f77dfdSTejun Heo static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; } 901e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) { } 902ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 903ae118896STejun Heo struct bio *bio) { return true; } 904ae118896STejun Heo 905eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 906eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 907eea8f41cSTejun Heo 908efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 909eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 910eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 911