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> 1824bdb8efSTejun Heo #include <linux/percpu_counter.h> 19eea8f41cSTejun Heo #include <linux/seq_file.h> 20eea8f41cSTejun Heo #include <linux/radix-tree.h> 21eea8f41cSTejun Heo #include <linux/blkdev.h> 22eea8f41cSTejun Heo #include <linux/atomic.h> 23902ec5b6SShaohua Li #include <linux/kthread.h> 245cdf2e3fSDennis Zhou #include <linux/fs.h> 25eea8f41cSTejun Heo 2624bdb8efSTejun Heo /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */ 2724bdb8efSTejun Heo #define BLKG_STAT_CPU_BATCH (INT_MAX / 2) 2824bdb8efSTejun Heo 29eea8f41cSTejun Heo /* Max limits for throttle policy */ 30eea8f41cSTejun Heo #define THROTL_IOPS_MAX UINT_MAX 31eea8f41cSTejun Heo 32eea8f41cSTejun Heo #ifdef CONFIG_BLK_CGROUP 33eea8f41cSTejun Heo 34eea8f41cSTejun Heo enum blkg_rwstat_type { 35eea8f41cSTejun Heo BLKG_RWSTAT_READ, 36eea8f41cSTejun Heo BLKG_RWSTAT_WRITE, 37eea8f41cSTejun Heo BLKG_RWSTAT_SYNC, 38eea8f41cSTejun Heo BLKG_RWSTAT_ASYNC, 39636620b6STejun Heo BLKG_RWSTAT_DISCARD, 40eea8f41cSTejun Heo 41eea8f41cSTejun Heo BLKG_RWSTAT_NR, 42eea8f41cSTejun Heo BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR, 43eea8f41cSTejun Heo }; 44eea8f41cSTejun Heo 45eea8f41cSTejun Heo struct blkcg_gq; 46eea8f41cSTejun Heo 47eea8f41cSTejun Heo struct blkcg { 48eea8f41cSTejun Heo struct cgroup_subsys_state css; 49eea8f41cSTejun Heo spinlock_t lock; 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; 6059b57717SDennis Zhou (Facebook) refcount_t cgwb_refcnt; 6152ebea74STejun Heo #endif 62eea8f41cSTejun Heo }; 63eea8f41cSTejun Heo 64e6269c44STejun Heo /* 65e6269c44STejun Heo * blkg_[rw]stat->aux_cnt is excluded for local stats but included for 66*7af6fd91SChristoph Hellwig * recursive. Used to carry stats of dead children. 67e6269c44STejun Heo */ 68eea8f41cSTejun Heo struct blkg_stat { 6924bdb8efSTejun Heo struct percpu_counter cpu_cnt; 70e6269c44STejun Heo atomic64_t aux_cnt; 71eea8f41cSTejun Heo }; 72eea8f41cSTejun Heo 73eea8f41cSTejun Heo struct blkg_rwstat { 7424bdb8efSTejun Heo struct percpu_counter cpu_cnt[BLKG_RWSTAT_NR]; 75e6269c44STejun Heo atomic64_t aux_cnt[BLKG_RWSTAT_NR]; 76eea8f41cSTejun Heo }; 77eea8f41cSTejun Heo 78*7af6fd91SChristoph Hellwig struct blkg_rwstat_sample { 79*7af6fd91SChristoph Hellwig u64 cnt[BLKG_RWSTAT_NR]; 80*7af6fd91SChristoph Hellwig }; 81*7af6fd91SChristoph Hellwig 82eea8f41cSTejun Heo /* 83eea8f41cSTejun Heo * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a 84eea8f41cSTejun Heo * request_queue (q). This is used by blkcg policies which need to track 85eea8f41cSTejun Heo * information per blkcg - q pair. 86eea8f41cSTejun Heo * 87001bea73STejun Heo * There can be multiple active blkcg policies and each blkg:policy pair is 88001bea73STejun Heo * represented by a blkg_policy_data which is allocated and freed by each 89001bea73STejun Heo * policy's pd_alloc/free_fn() methods. A policy can allocate private data 90001bea73STejun Heo * area by allocating larger data structure which embeds blkg_policy_data 91001bea73STejun Heo * at the beginning. 92eea8f41cSTejun Heo */ 93eea8f41cSTejun Heo struct blkg_policy_data { 94eea8f41cSTejun Heo /* the blkg and policy id this per-policy data belongs to */ 95eea8f41cSTejun Heo struct blkcg_gq *blkg; 96eea8f41cSTejun Heo int plid; 97eea8f41cSTejun Heo }; 98eea8f41cSTejun Heo 99e4bc13adSLinus Torvalds /* 100e4a9bde9STejun Heo * Policies that need to keep per-blkcg data which is independent from any 101e4a9bde9STejun Heo * request_queue associated to it should implement cpd_alloc/free_fn() 102e4a9bde9STejun Heo * methods. A policy can allocate private data area by allocating larger 103e4a9bde9STejun Heo * data structure which embeds blkcg_policy_data at the beginning. 104e4a9bde9STejun Heo * cpd_init() is invoked to let each policy handle per-blkcg data. 105e4bc13adSLinus Torvalds */ 106e4bc13adSLinus Torvalds struct blkcg_policy_data { 10781437648STejun Heo /* the blkcg and policy id this per-policy data belongs to */ 10881437648STejun Heo struct blkcg *blkcg; 109e4bc13adSLinus Torvalds int plid; 110e4bc13adSLinus Torvalds }; 111e4bc13adSLinus Torvalds 112eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */ 113eea8f41cSTejun Heo struct blkcg_gq { 114eea8f41cSTejun Heo /* Pointer to the associated request_queue */ 115eea8f41cSTejun Heo struct request_queue *q; 116eea8f41cSTejun Heo struct list_head q_node; 117eea8f41cSTejun Heo struct hlist_node blkcg_node; 118eea8f41cSTejun Heo struct blkcg *blkcg; 119eea8f41cSTejun Heo 120ce7acfeaSTejun Heo /* 121ce7acfeaSTejun Heo * Each blkg gets congested separately and the congestion state is 122ce7acfeaSTejun Heo * propagated to the matching bdi_writeback_congested. 123ce7acfeaSTejun Heo */ 124ce7acfeaSTejun Heo struct bdi_writeback_congested *wb_congested; 125ce7acfeaSTejun Heo 126eea8f41cSTejun Heo /* all non-root blkcg_gq's are guaranteed to have access to parent */ 127eea8f41cSTejun Heo struct blkcg_gq *parent; 128eea8f41cSTejun Heo 129eea8f41cSTejun Heo /* reference count */ 1307fcf2b03SDennis Zhou struct percpu_ref refcnt; 131eea8f41cSTejun Heo 132eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 133eea8f41cSTejun Heo bool online; 134eea8f41cSTejun Heo 13577ea7338STejun Heo struct blkg_rwstat stat_bytes; 13677ea7338STejun Heo struct blkg_rwstat stat_ios; 13777ea7338STejun Heo 138eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 139eea8f41cSTejun Heo 140eea8f41cSTejun Heo struct rcu_head rcu_head; 141d09d8df3SJosef Bacik 142d09d8df3SJosef Bacik atomic_t use_delay; 143d09d8df3SJosef Bacik atomic64_t delay_nsec; 144d09d8df3SJosef Bacik atomic64_t delay_start; 145d09d8df3SJosef Bacik u64 last_delay; 146d09d8df3SJosef Bacik int last_use; 147eea8f41cSTejun Heo }; 148eea8f41cSTejun Heo 149e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 15081437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 151e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 15269d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 153001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node); 154a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 155a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 156a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 157001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 158a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 159903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 160903d23f0SJosef Bacik size_t size); 161eea8f41cSTejun Heo 162eea8f41cSTejun Heo struct blkcg_policy { 163eea8f41cSTejun Heo int plid; 164eea8f41cSTejun Heo /* cgroup files for the policy */ 1652ee867dcSTejun Heo struct cftype *dfl_cftypes; 166880f50e2STejun Heo struct cftype *legacy_cftypes; 167eea8f41cSTejun Heo 168eea8f41cSTejun Heo /* operations */ 169e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 170e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 171e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 17269d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 173e4a9bde9STejun Heo 174001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 175eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 176eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 177eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 178001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 179eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 180903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 181eea8f41cSTejun Heo }; 182eea8f41cSTejun Heo 183eea8f41cSTejun Heo extern struct blkcg blkcg_root; 184496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 185eea8f41cSTejun Heo 18624f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 18724f29046STejun Heo struct request_queue *q, bool update_hint); 188b978962aSDennis Zhou struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg, 189b978962aSDennis Zhou struct request_queue *q); 190eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, 191eea8f41cSTejun Heo struct request_queue *q); 192eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q); 193eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q); 194eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q); 195eea8f41cSTejun Heo 196eea8f41cSTejun Heo /* Blkio controller policy registration */ 197eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol); 198eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol); 199eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q, 200eea8f41cSTejun Heo const struct blkcg_policy *pol); 201eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q, 202eea8f41cSTejun Heo const struct blkcg_policy *pol); 203eea8f41cSTejun Heo 204239eeb08SChristoph Hellwig static inline u64 blkg_rwstat_read_counter(struct blkg_rwstat *rwstat, 205239eeb08SChristoph Hellwig unsigned int idx) 206239eeb08SChristoph Hellwig { 207239eeb08SChristoph Hellwig return atomic64_read(&rwstat->aux_cnt[idx]) + 208239eeb08SChristoph Hellwig percpu_counter_sum_positive(&rwstat->cpu_cnt[idx]); 209239eeb08SChristoph Hellwig } 210239eeb08SChristoph Hellwig 211dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg); 212eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, 213eea8f41cSTejun Heo u64 (*prfill)(struct seq_file *, 214eea8f41cSTejun Heo struct blkg_policy_data *, int), 215eea8f41cSTejun Heo const struct blkcg_policy *pol, int data, 216eea8f41cSTejun Heo bool show_total); 217eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); 218eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 219*7af6fd91SChristoph Hellwig const struct blkg_rwstat_sample *rwstat); 220eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off); 221eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 222eea8f41cSTejun Heo int off); 22377ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v); 22477ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v); 22577ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v); 22677ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v); 227eea8f41cSTejun Heo 228f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg, 229f12c74caSTejun Heo struct blkcg_policy *pol, int off); 2305d0b6e48SChristoph Hellwig void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol, 231*7af6fd91SChristoph Hellwig int off, struct blkg_rwstat_sample *sum); 232eea8f41cSTejun Heo 233eea8f41cSTejun Heo struct blkg_conf_ctx { 234eea8f41cSTejun Heo struct gendisk *disk; 235eea8f41cSTejun Heo struct blkcg_gq *blkg; 23636aa9e5fSTejun Heo char *body; 237eea8f41cSTejun Heo }; 238eea8f41cSTejun Heo 239eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, 24036aa9e5fSTejun Heo char *input, struct blkg_conf_ctx *ctx); 241eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx); 242eea8f41cSTejun Heo 2430fe061b9SDennis Zhou /** 2440fe061b9SDennis Zhou * blkcg_css - find the current css 2450fe061b9SDennis Zhou * 2460fe061b9SDennis Zhou * Find the css associated with either the kthread or the current task. 2470fe061b9SDennis Zhou * This may return a dying css, so it is up to the caller to use tryget logic 2480fe061b9SDennis Zhou * to confirm it is alive and well. 2490fe061b9SDennis Zhou */ 2500fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_css(void) 2510fe061b9SDennis Zhou { 2520fe061b9SDennis Zhou struct cgroup_subsys_state *css; 2530fe061b9SDennis Zhou 2540fe061b9SDennis Zhou css = kthread_blkcg(); 2550fe061b9SDennis Zhou if (css) 2560fe061b9SDennis Zhou return css; 2570fe061b9SDennis Zhou return task_css(current, io_cgrp_id); 2580fe061b9SDennis Zhou } 2590fe061b9SDennis Zhou 260eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 261eea8f41cSTejun Heo { 262eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 263eea8f41cSTejun Heo } 264eea8f41cSTejun Heo 2650fe061b9SDennis Zhou /** 2660fe061b9SDennis Zhou * __bio_blkcg - internal, inconsistent version to get blkcg 2670fe061b9SDennis Zhou * 2680fe061b9SDennis Zhou * DO NOT USE. 2690fe061b9SDennis Zhou * This function is inconsistent and consequently is dangerous to use. The 2700fe061b9SDennis Zhou * first part of the function returns a blkcg where a reference is owned by the 2710fe061b9SDennis Zhou * bio. This means it does not need to be rcu protected as it cannot go away 2720fe061b9SDennis Zhou * with the bio owning a reference to it. However, the latter potentially gets 2730fe061b9SDennis Zhou * it from task_css(). This can race against task migration and the cgroup 2740fe061b9SDennis Zhou * dying. It is also semantically different as it must be called rcu protected 2750fe061b9SDennis Zhou * and is susceptible to failure when trying to get a reference to it. 2760fe061b9SDennis Zhou * Therefore, it is not ok to assume that *_get() will always succeed on the 2770fe061b9SDennis Zhou * blkcg returned here. 2780fe061b9SDennis Zhou */ 2790fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) 28027e6fa99SDennis Zhou (Facebook) { 281db6638d7SDennis Zhou if (bio && bio->bi_blkg) 282db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2830fe061b9SDennis Zhou return css_to_blkcg(blkcg_css()); 2840fe061b9SDennis Zhou } 2850fe061b9SDennis Zhou 2860fe061b9SDennis Zhou /** 2870fe061b9SDennis Zhou * bio_blkcg - grab the blkcg associated with a bio 2880fe061b9SDennis Zhou * @bio: target bio 2890fe061b9SDennis Zhou * 2900fe061b9SDennis Zhou * This returns the blkcg associated with a bio, %NULL if not associated. 2910fe061b9SDennis Zhou * Callers are expected to either handle %NULL or know association has been 2920fe061b9SDennis Zhou * done prior to calling this. 2930fe061b9SDennis Zhou */ 2940fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio) 2950fe061b9SDennis Zhou { 296db6638d7SDennis Zhou if (bio && bio->bi_blkg) 297db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2980fe061b9SDennis Zhou return NULL; 299fd383c2dSTejun Heo } 300fd383c2dSTejun Heo 301d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 302d09d8df3SJosef Bacik { 303d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 304d09d8df3SJosef Bacik bool ret = false; 305d09d8df3SJosef Bacik 306d09d8df3SJosef Bacik rcu_read_lock(); 307d09d8df3SJosef Bacik css = kthread_blkcg(); 308d09d8df3SJosef Bacik if (!css) 309d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 310d09d8df3SJosef Bacik while (css) { 311d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 312d09d8df3SJosef Bacik ret = true; 313d09d8df3SJosef Bacik break; 314d09d8df3SJosef Bacik } 315d09d8df3SJosef Bacik css = css->parent; 316d09d8df3SJosef Bacik } 317d09d8df3SJosef Bacik rcu_read_unlock(); 318d09d8df3SJosef Bacik return ret; 319d09d8df3SJosef Bacik } 320d09d8df3SJosef Bacik 321eea8f41cSTejun Heo /** 322c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 323c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 324c7c98fd3SJosef Bacik * 325c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 326c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 327c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 328c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 329c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 330c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 331c7c98fd3SJosef Bacik */ 332c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 333c7c98fd3SJosef Bacik { 3340d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 335c7c98fd3SJosef Bacik } 336c7c98fd3SJosef Bacik 337c7c98fd3SJosef Bacik /** 338eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 339eea8f41cSTejun Heo * @blkcg: blkcg of interest 340eea8f41cSTejun Heo * 341eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 342eea8f41cSTejun Heo */ 343eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 344eea8f41cSTejun Heo { 345eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 346eea8f41cSTejun Heo } 347eea8f41cSTejun Heo 348eea8f41cSTejun Heo /** 34924f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 35024f29046STejun Heo * @blkcg: blkcg of interest 35124f29046STejun Heo * @q: request_queue of interest 35224f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 35324f29046STejun Heo * 35424f29046STejun Heo * This is internal version and shouldn't be used by policy 35524f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 35624f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 35724f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 35824f29046STejun Heo */ 35924f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 36024f29046STejun Heo struct request_queue *q, 36124f29046STejun Heo bool update_hint) 36224f29046STejun Heo { 36324f29046STejun Heo struct blkcg_gq *blkg; 36424f29046STejun Heo 36585b6bc9dSTejun Heo if (blkcg == &blkcg_root) 36685b6bc9dSTejun Heo return q->root_blkg; 36785b6bc9dSTejun Heo 36824f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 36924f29046STejun Heo if (blkg && blkg->q == q) 37024f29046STejun Heo return blkg; 37124f29046STejun Heo 37224f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 37324f29046STejun Heo } 37424f29046STejun Heo 37524f29046STejun Heo /** 37624f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 37724f29046STejun Heo * @blkcg: blkcg of interest 37824f29046STejun Heo * @q: request_queue of interest 37924f29046STejun Heo * 38024f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 3818f4236d9SChristoph Hellwig * under RCU read loc. 38224f29046STejun Heo */ 38324f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 38424f29046STejun Heo struct request_queue *q) 38524f29046STejun Heo { 38624f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 38724f29046STejun Heo return __blkg_lookup(blkcg, q, false); 38824f29046STejun Heo } 38924f29046STejun Heo 39024f29046STejun Heo /** 391b86d865cSBart Van Assche * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair 3926bad9b21SBart Van Assche * @q: request_queue of interest 3936bad9b21SBart Van Assche * 3946bad9b21SBart Van Assche * Lookup blkg for @q at the root level. See also blkg_lookup(). 3956bad9b21SBart Van Assche */ 396b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 3976bad9b21SBart Van Assche { 398b86d865cSBart Van Assche return q->root_blkg; 3996bad9b21SBart Van Assche } 4006bad9b21SBart Van Assche 4016bad9b21SBart Van Assche /** 402eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 403eea8f41cSTejun Heo * @blkg: blkg of interest 404eea8f41cSTejun Heo * @pol: policy of interest 405eea8f41cSTejun Heo * 406eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 407eea8f41cSTejun Heo */ 408eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 409eea8f41cSTejun Heo struct blkcg_policy *pol) 410eea8f41cSTejun Heo { 411eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 412eea8f41cSTejun Heo } 413eea8f41cSTejun Heo 414e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 415e4bc13adSLinus Torvalds struct blkcg_policy *pol) 416e4bc13adSLinus Torvalds { 41781437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 418e4bc13adSLinus Torvalds } 419e4bc13adSLinus Torvalds 420eea8f41cSTejun Heo /** 421eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 422eea8f41cSTejun Heo * @pd: policy private data of interest 423eea8f41cSTejun Heo * 424eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 425eea8f41cSTejun Heo */ 426eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 427eea8f41cSTejun Heo { 428eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 429eea8f41cSTejun Heo } 430eea8f41cSTejun Heo 43181437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 43281437648STejun Heo { 43381437648STejun Heo return cpd ? cpd->blkcg : NULL; 43481437648STejun Heo } 43581437648STejun Heo 43659b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg); 43759b57717SDennis Zhou (Facebook) 43859b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK 43959b57717SDennis Zhou (Facebook) 44059b57717SDennis Zhou (Facebook) /** 44159b57717SDennis Zhou (Facebook) * blkcg_cgwb_get - get a reference for blkcg->cgwb_list 44259b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 44359b57717SDennis Zhou (Facebook) * 44459b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 44559b57717SDennis Zhou (Facebook) */ 44659b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) 44759b57717SDennis Zhou (Facebook) { 44859b57717SDennis Zhou (Facebook) refcount_inc(&blkcg->cgwb_refcnt); 44959b57717SDennis Zhou (Facebook) } 45059b57717SDennis Zhou (Facebook) 45159b57717SDennis Zhou (Facebook) /** 45259b57717SDennis Zhou (Facebook) * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list 45359b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 45459b57717SDennis Zhou (Facebook) * 45559b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 45659b57717SDennis Zhou (Facebook) * When this count goes to zero, all active wb has finished so the 45759b57717SDennis Zhou (Facebook) * blkcg can continue destruction by calling blkcg_destroy_blkgs(). 45859b57717SDennis Zhou (Facebook) * This work may occur in cgwb_release_workfn() on the cgwb_release 45959b57717SDennis Zhou (Facebook) * workqueue. 46059b57717SDennis Zhou (Facebook) */ 46159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 46259b57717SDennis Zhou (Facebook) { 46359b57717SDennis Zhou (Facebook) if (refcount_dec_and_test(&blkcg->cgwb_refcnt)) 46459b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 46559b57717SDennis Zhou (Facebook) } 46659b57717SDennis Zhou (Facebook) 46759b57717SDennis Zhou (Facebook) #else 46859b57717SDennis Zhou (Facebook) 46959b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { } 47059b57717SDennis Zhou (Facebook) 47159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 47259b57717SDennis Zhou (Facebook) { 47359b57717SDennis Zhou (Facebook) /* wb isn't being accounted, so trigger destruction right away */ 47459b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 47559b57717SDennis Zhou (Facebook) } 47659b57717SDennis Zhou (Facebook) 47759b57717SDennis Zhou (Facebook) #endif 47859b57717SDennis Zhou (Facebook) 479eea8f41cSTejun Heo /** 480eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 481eea8f41cSTejun Heo * @blkg: blkg of interest 482eea8f41cSTejun Heo * @buf: target buffer 483eea8f41cSTejun Heo * @buflen: target buffer length 484eea8f41cSTejun Heo * 485eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 486eea8f41cSTejun Heo */ 487eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 488eea8f41cSTejun Heo { 4894c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 490eea8f41cSTejun Heo } 491eea8f41cSTejun Heo 492eea8f41cSTejun Heo /** 493eea8f41cSTejun Heo * blkg_get - get a blkg reference 494eea8f41cSTejun Heo * @blkg: blkg to get 495eea8f41cSTejun Heo * 496eea8f41cSTejun Heo * The caller should be holding an existing reference. 497eea8f41cSTejun Heo */ 498eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 499eea8f41cSTejun Heo { 5007fcf2b03SDennis Zhou percpu_ref_get(&blkg->refcnt); 501eea8f41cSTejun Heo } 502eea8f41cSTejun Heo 503d09d8df3SJosef Bacik /** 5047754f669SDennis Zhou * blkg_tryget - try and get a blkg reference 505d09d8df3SJosef Bacik * @blkg: blkg to get 506d09d8df3SJosef Bacik * 507d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 508d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 509d09d8df3SJosef Bacik */ 5107754f669SDennis Zhou static inline bool blkg_tryget(struct blkcg_gq *blkg) 511d09d8df3SJosef Bacik { 5126ab21879SDennis Zhou return blkg && percpu_ref_tryget(&blkg->refcnt); 51307b05bccSDennis Zhou (Facebook) } 514d09d8df3SJosef Bacik 515beea9da0SDennis Zhou /** 5167754f669SDennis Zhou * blkg_tryget_closest - try and get a blkg ref on the closet blkg 517beea9da0SDennis Zhou * @blkg: blkg to get 518beea9da0SDennis Zhou * 5196ab21879SDennis Zhou * This needs to be called rcu protected. As the failure mode here is to walk 5206ab21879SDennis Zhou * up the blkg tree, this ensure that the blkg->parent pointers are always 5216ab21879SDennis Zhou * valid. This returns the blkg that it ended up taking a reference on or %NULL 5226ab21879SDennis Zhou * if no reference was taken. 523beea9da0SDennis Zhou */ 5247754f669SDennis Zhou static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg) 525beea9da0SDennis Zhou { 5266ab21879SDennis Zhou struct blkcg_gq *ret_blkg = NULL; 527beea9da0SDennis Zhou 5286ab21879SDennis Zhou WARN_ON_ONCE(!rcu_read_lock_held()); 5296ab21879SDennis Zhou 5306ab21879SDennis Zhou while (blkg) { 5316ab21879SDennis Zhou if (blkg_tryget(blkg)) { 5326ab21879SDennis Zhou ret_blkg = blkg; 5336ab21879SDennis Zhou break; 5346ab21879SDennis Zhou } 5356ab21879SDennis Zhou blkg = blkg->parent; 5366ab21879SDennis Zhou } 5376ab21879SDennis Zhou 5386ab21879SDennis Zhou return ret_blkg; 539beea9da0SDennis Zhou } 540b5f2954dSDennis Zhou 541eea8f41cSTejun Heo /** 542eea8f41cSTejun Heo * blkg_put - put a blkg reference 543eea8f41cSTejun Heo * @blkg: blkg to put 544eea8f41cSTejun Heo */ 545eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 546eea8f41cSTejun Heo { 5477fcf2b03SDennis Zhou percpu_ref_put(&blkg->refcnt); 548eea8f41cSTejun Heo } 549eea8f41cSTejun Heo 550eea8f41cSTejun Heo /** 551eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 552eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 553eea8f41cSTejun Heo * @pos_css: used for iteration 554eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 555eea8f41cSTejun Heo * 556eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 557eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 558eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 559eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 560eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 561eea8f41cSTejun Heo */ 562eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 563eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 564eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 565eea8f41cSTejun Heo (p_blkg)->q, false))) 566eea8f41cSTejun Heo 567eea8f41cSTejun Heo /** 568eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 569eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 570eea8f41cSTejun Heo * @pos_css: used for iteration 571eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 572eea8f41cSTejun Heo * 573eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 574eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 575eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 576eea8f41cSTejun Heo */ 577eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 578eea8f41cSTejun Heo css_for_each_descendant_post((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 58224bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp) 583eea8f41cSTejun Heo { 58424bdb8efSTejun Heo int ret; 58524bdb8efSTejun Heo 58624bdb8efSTejun Heo ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp); 58724bdb8efSTejun Heo if (ret) 58824bdb8efSTejun Heo return ret; 58924bdb8efSTejun Heo 590e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 59124bdb8efSTejun Heo return 0; 59224bdb8efSTejun Heo } 59324bdb8efSTejun Heo 59424bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat) 59524bdb8efSTejun Heo { 59624bdb8efSTejun Heo percpu_counter_destroy(&stat->cpu_cnt); 597eea8f41cSTejun Heo } 598eea8f41cSTejun Heo 599eea8f41cSTejun Heo /** 600eea8f41cSTejun Heo * blkg_stat_add - add a value to a blkg_stat 601eea8f41cSTejun Heo * @stat: target blkg_stat 602eea8f41cSTejun Heo * @val: value to add 603eea8f41cSTejun Heo * 60424bdb8efSTejun Heo * Add @val to @stat. The caller must ensure that IRQ on the same CPU 60524bdb8efSTejun Heo * don't re-enter this function for the same counter. 606eea8f41cSTejun Heo */ 607eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val) 608eea8f41cSTejun Heo { 609104b4e51SNikolay Borisov percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH); 610eea8f41cSTejun Heo } 611eea8f41cSTejun Heo 612eea8f41cSTejun Heo /** 613eea8f41cSTejun Heo * blkg_stat_read - read the current value of a blkg_stat 614eea8f41cSTejun Heo * @stat: blkg_stat to read 615eea8f41cSTejun Heo */ 616eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat) 617eea8f41cSTejun Heo { 61824bdb8efSTejun Heo return percpu_counter_sum_positive(&stat->cpu_cnt); 619eea8f41cSTejun Heo } 620eea8f41cSTejun Heo 621eea8f41cSTejun Heo /** 622eea8f41cSTejun Heo * blkg_stat_reset - reset a blkg_stat 623eea8f41cSTejun Heo * @stat: blkg_stat to reset 624eea8f41cSTejun Heo */ 625eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat) 626eea8f41cSTejun Heo { 62724bdb8efSTejun Heo percpu_counter_set(&stat->cpu_cnt, 0); 628e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 629eea8f41cSTejun Heo } 630eea8f41cSTejun Heo 631eea8f41cSTejun Heo /** 632e6269c44STejun Heo * blkg_stat_add_aux - add a blkg_stat into another's aux count 633eea8f41cSTejun Heo * @to: the destination blkg_stat 634eea8f41cSTejun Heo * @from: the source 635eea8f41cSTejun Heo * 636e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 637eea8f41cSTejun Heo */ 638e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to, 639e6269c44STejun Heo struct blkg_stat *from) 640eea8f41cSTejun Heo { 641e6269c44STejun Heo atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt), 642e6269c44STejun Heo &to->aux_cnt); 643eea8f41cSTejun Heo } 644eea8f41cSTejun Heo 64524bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp) 64624bdb8efSTejun Heo { 64724bdb8efSTejun Heo int i, ret; 64824bdb8efSTejun Heo 64924bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 65024bdb8efSTejun Heo ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp); 65124bdb8efSTejun Heo if (ret) { 65224bdb8efSTejun Heo while (--i >= 0) 65324bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 65424bdb8efSTejun Heo return ret; 65524bdb8efSTejun Heo } 65624bdb8efSTejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 65724bdb8efSTejun Heo } 65824bdb8efSTejun Heo return 0; 65924bdb8efSTejun Heo } 66024bdb8efSTejun Heo 66124bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat) 662eea8f41cSTejun Heo { 663e6269c44STejun Heo int i; 664e6269c44STejun Heo 665e6269c44STejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 66624bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 667eea8f41cSTejun Heo } 668eea8f41cSTejun Heo 669eea8f41cSTejun Heo /** 670eea8f41cSTejun Heo * blkg_rwstat_add - add a value to a blkg_rwstat 671eea8f41cSTejun Heo * @rwstat: target blkg_rwstat 672ef295ecfSChristoph Hellwig * @op: REQ_OP and flags 673eea8f41cSTejun Heo * @val: value to add 674eea8f41cSTejun Heo * 675eea8f41cSTejun Heo * Add @val to @rwstat. The counters are chosen according to @rw. The 676eea8f41cSTejun Heo * caller is responsible for synchronizing calls to this function. 677eea8f41cSTejun Heo */ 678eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, 679ef295ecfSChristoph Hellwig unsigned int op, uint64_t val) 680eea8f41cSTejun Heo { 68124bdb8efSTejun Heo struct percpu_counter *cnt; 682eea8f41cSTejun Heo 683636620b6STejun Heo if (op_is_discard(op)) 684636620b6STejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD]; 685636620b6STejun Heo else if (op_is_write(op)) 68624bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE]; 687eea8f41cSTejun Heo else 68824bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ]; 689eea8f41cSTejun Heo 690104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 69124bdb8efSTejun Heo 692d71d9ae1SChristoph Hellwig if (op_is_sync(op)) 69324bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC]; 69424bdb8efSTejun Heo else 69524bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC]; 69624bdb8efSTejun Heo 697104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 698eea8f41cSTejun Heo } 699eea8f41cSTejun Heo 700eea8f41cSTejun Heo /** 701eea8f41cSTejun Heo * blkg_rwstat_read - read the current values of a blkg_rwstat 702eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 703eea8f41cSTejun Heo * 70424bdb8efSTejun Heo * Read the current snapshot of @rwstat and return it in the aux counts. 705eea8f41cSTejun Heo */ 7065d0b6e48SChristoph Hellwig static inline void blkg_rwstat_read(struct blkg_rwstat *rwstat, 707*7af6fd91SChristoph Hellwig struct blkg_rwstat_sample *result) 708eea8f41cSTejun Heo { 70924bdb8efSTejun Heo int i; 710eea8f41cSTejun Heo 71124bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 712*7af6fd91SChristoph Hellwig result->cnt[i] = 713*7af6fd91SChristoph Hellwig percpu_counter_sum_positive(&rwstat->cpu_cnt[i]); 714eea8f41cSTejun Heo } 715eea8f41cSTejun Heo 716eea8f41cSTejun Heo /** 717eea8f41cSTejun Heo * blkg_rwstat_total - read the total count of a blkg_rwstat 718eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 719eea8f41cSTejun Heo * 720eea8f41cSTejun Heo * Return the total count of @rwstat regardless of the IO direction. This 721eea8f41cSTejun Heo * function can be called without synchronization and takes care of u64 722eea8f41cSTejun Heo * atomicity. 723eea8f41cSTejun Heo */ 724eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat) 725eea8f41cSTejun Heo { 726*7af6fd91SChristoph Hellwig struct blkg_rwstat_sample tmp = { }; 727eea8f41cSTejun Heo 7285d0b6e48SChristoph Hellwig blkg_rwstat_read(rwstat, &tmp); 729*7af6fd91SChristoph Hellwig return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]; 730eea8f41cSTejun Heo } 731eea8f41cSTejun Heo 732eea8f41cSTejun Heo /** 733eea8f41cSTejun Heo * blkg_rwstat_reset - reset a blkg_rwstat 734eea8f41cSTejun Heo * @rwstat: blkg_rwstat to reset 735eea8f41cSTejun Heo */ 736eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) 737eea8f41cSTejun Heo { 738e6269c44STejun Heo int i; 739e6269c44STejun Heo 74024bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 74124bdb8efSTejun Heo percpu_counter_set(&rwstat->cpu_cnt[i], 0); 742e6269c44STejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 743eea8f41cSTejun Heo } 74424bdb8efSTejun Heo } 745eea8f41cSTejun Heo 746eea8f41cSTejun Heo /** 747e6269c44STejun Heo * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count 748eea8f41cSTejun Heo * @to: the destination blkg_rwstat 749eea8f41cSTejun Heo * @from: the source 750eea8f41cSTejun Heo * 751e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 752eea8f41cSTejun Heo */ 753e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to, 754eea8f41cSTejun Heo struct blkg_rwstat *from) 755eea8f41cSTejun Heo { 756ddc21231SArnd Bergmann u64 sum[BLKG_RWSTAT_NR]; 757eea8f41cSTejun Heo int i; 758eea8f41cSTejun Heo 759eea8f41cSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 760ddc21231SArnd Bergmann sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]); 761ddc21231SArnd Bergmann 762ddc21231SArnd Bergmann for (i = 0; i < BLKG_RWSTAT_NR; i++) 763ddc21231SArnd Bergmann atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]), 764e6269c44STejun Heo &to->aux_cnt[i]); 765eea8f41cSTejun Heo } 766eea8f41cSTejun Heo 767ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING 768ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 769ae118896STejun Heo struct bio *bio); 770ae118896STejun Heo #else 771ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 772ae118896STejun Heo struct bio *bio) { return false; } 773ae118896STejun Heo #endif 774ae118896STejun Heo 775e439bedfSDennis Zhou 776e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) 777e439bedfSDennis Zhou { 778e439bedfSDennis Zhou bio_issue_init(&bio->bi_issue, bio_sectors(bio)); 779e439bedfSDennis Zhou } 780e439bedfSDennis Zhou 781ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 782ae118896STejun Heo struct bio *bio) 783ae118896STejun Heo { 784ae118896STejun Heo struct blkcg_gq *blkg; 785ae118896STejun Heo bool throtl = false; 786ae118896STejun Heo 7874705de73SDennis Zhou rcu_read_lock(); 7884705de73SDennis Zhou 7895cdf2e3fSDennis Zhou if (!bio->bi_blkg) { 7905cdf2e3fSDennis Zhou char b[BDEVNAME_SIZE]; 791ae118896STejun Heo 7925cdf2e3fSDennis Zhou WARN_ONCE(1, 7935cdf2e3fSDennis Zhou "no blkg associated for bio on block-device: %s\n", 7945cdf2e3fSDennis Zhou bio_devname(bio, b)); 7955cdf2e3fSDennis Zhou bio_associate_blkg(bio); 7965cdf2e3fSDennis Zhou } 7975cdf2e3fSDennis Zhou 7985cdf2e3fSDennis Zhou blkg = bio->bi_blkg; 799ae118896STejun Heo 800ae118896STejun Heo throtl = blk_throtl_bio(q, blkg, bio); 801ae118896STejun Heo 80277ea7338STejun Heo if (!throtl) { 803c454edc2SJosef Bacik /* 804c454edc2SJosef Bacik * If the bio is flagged with BIO_QUEUE_ENTERED it means this 805c454edc2SJosef Bacik * is a split bio and we would have already accounted for the 806c454edc2SJosef Bacik * size of the bio. 807c454edc2SJosef Bacik */ 808c454edc2SJosef Bacik if (!bio_flagged(bio, BIO_QUEUE_ENTERED)) 809ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf, 81077ea7338STejun Heo bio->bi_iter.bi_size); 811ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1); 81277ea7338STejun Heo } 81377ea7338STejun Heo 814e439bedfSDennis Zhou blkcg_bio_issue_init(bio); 815e439bedfSDennis Zhou 8164705de73SDennis Zhou rcu_read_unlock(); 817ae118896STejun Heo return !throtl; 818ae118896STejun Heo } 819ae118896STejun Heo 820d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 821d09d8df3SJosef Bacik { 822d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 823d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 824d09d8df3SJosef Bacik } 825d09d8df3SJosef Bacik 826d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 827d09d8df3SJosef Bacik { 828d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 829d09d8df3SJosef Bacik 830d09d8df3SJosef Bacik if (old == 0) 831d09d8df3SJosef Bacik return 0; 832d09d8df3SJosef Bacik 833d09d8df3SJosef Bacik /* 834d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 835d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 836d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 837d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 838d09d8df3SJosef Bacik * congestion count on the cgroup. 839d09d8df3SJosef Bacik */ 840d09d8df3SJosef Bacik while (old) { 841d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 842d09d8df3SJosef Bacik if (cur == old) 843d09d8df3SJosef Bacik break; 844d09d8df3SJosef Bacik old = cur; 845d09d8df3SJosef Bacik } 846d09d8df3SJosef Bacik 847d09d8df3SJosef Bacik if (old == 0) 848d09d8df3SJosef Bacik return 0; 849d09d8df3SJosef Bacik if (old == 1) 850d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 851d09d8df3SJosef Bacik return 1; 852d09d8df3SJosef Bacik } 853d09d8df3SJosef Bacik 854d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 855d09d8df3SJosef Bacik { 856d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 857d09d8df3SJosef Bacik if (!old) 858d09d8df3SJosef Bacik return; 859d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 860d09d8df3SJosef Bacik while (old) { 861d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); 862d09d8df3SJosef Bacik if (cur == old) { 863d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 864d09d8df3SJosef Bacik break; 865d09d8df3SJosef Bacik } 866d09d8df3SJosef Bacik old = cur; 867d09d8df3SJosef Bacik } 868d09d8df3SJosef Bacik } 869d09d8df3SJosef Bacik 870d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 871d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 872d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 873eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 874eea8f41cSTejun Heo 875efa7d1c7STejun Heo struct blkcg { 876efa7d1c7STejun Heo }; 877eea8f41cSTejun Heo 878eea8f41cSTejun Heo struct blkg_policy_data { 879eea8f41cSTejun Heo }; 880eea8f41cSTejun Heo 881e4bc13adSLinus Torvalds struct blkcg_policy_data { 882e4bc13adSLinus Torvalds }; 883e4bc13adSLinus Torvalds 884eea8f41cSTejun Heo struct blkcg_gq { 885eea8f41cSTejun Heo }; 886eea8f41cSTejun Heo 887eea8f41cSTejun Heo struct blkcg_policy { 888eea8f41cSTejun Heo }; 889eea8f41cSTejun Heo 890496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 891496d5e75STejun Heo 892d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 893d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 894d09d8df3SJosef Bacik 895efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 896efa7d1c7STejun Heo 897d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 898d09d8df3SJosef Bacik 899eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 900b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 901b86d865cSBart Van Assche { return NULL; } 902eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 903eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { } 904eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 905eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 906eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 907eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 908eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 909eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 910eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 911eea8f41cSTejun Heo 9120fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; } 913eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 914eea8f41cSTejun Heo 915eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 916eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 917eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 918eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 919eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 920eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 921eea8f41cSTejun Heo 922e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) { } 923ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 924ae118896STejun Heo struct bio *bio) { return true; } 925ae118896STejun Heo 926eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 927eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 928eea8f41cSTejun Heo 929efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 930eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 931eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 932