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 6624bdb8efSTejun Heo * recursive. Used to carry stats of dead children, and, for blkg_rwstat, 6724bdb8efSTejun Heo * to carry result values from read and sum operations. 68e6269c44STejun Heo */ 69eea8f41cSTejun Heo struct blkg_stat { 7024bdb8efSTejun Heo struct percpu_counter cpu_cnt; 71e6269c44STejun Heo atomic64_t aux_cnt; 72eea8f41cSTejun Heo }; 73eea8f41cSTejun Heo 74eea8f41cSTejun Heo struct blkg_rwstat { 7524bdb8efSTejun Heo struct percpu_counter cpu_cnt[BLKG_RWSTAT_NR]; 76e6269c44STejun Heo atomic64_t aux_cnt[BLKG_RWSTAT_NR]; 77eea8f41cSTejun Heo }; 78eea8f41cSTejun Heo 79eea8f41cSTejun Heo /* 80eea8f41cSTejun Heo * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a 81eea8f41cSTejun Heo * request_queue (q). This is used by blkcg policies which need to track 82eea8f41cSTejun Heo * information per blkcg - q pair. 83eea8f41cSTejun Heo * 84001bea73STejun Heo * There can be multiple active blkcg policies and each blkg:policy pair is 85001bea73STejun Heo * represented by a blkg_policy_data which is allocated and freed by each 86001bea73STejun Heo * policy's pd_alloc/free_fn() methods. A policy can allocate private data 87001bea73STejun Heo * area by allocating larger data structure which embeds blkg_policy_data 88001bea73STejun Heo * at the beginning. 89eea8f41cSTejun Heo */ 90eea8f41cSTejun Heo struct blkg_policy_data { 91eea8f41cSTejun Heo /* the blkg and policy id this per-policy data belongs to */ 92eea8f41cSTejun Heo struct blkcg_gq *blkg; 93eea8f41cSTejun Heo int plid; 94eea8f41cSTejun Heo }; 95eea8f41cSTejun Heo 96e4bc13adSLinus Torvalds /* 97e4a9bde9STejun Heo * Policies that need to keep per-blkcg data which is independent from any 98e4a9bde9STejun Heo * request_queue associated to it should implement cpd_alloc/free_fn() 99e4a9bde9STejun Heo * methods. A policy can allocate private data area by allocating larger 100e4a9bde9STejun Heo * data structure which embeds blkcg_policy_data at the beginning. 101e4a9bde9STejun Heo * cpd_init() is invoked to let each policy handle per-blkcg data. 102e4bc13adSLinus Torvalds */ 103e4bc13adSLinus Torvalds struct blkcg_policy_data { 10481437648STejun Heo /* the blkcg and policy id this per-policy data belongs to */ 10581437648STejun Heo struct blkcg *blkcg; 106e4bc13adSLinus Torvalds int plid; 107e4bc13adSLinus Torvalds }; 108e4bc13adSLinus Torvalds 109eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */ 110eea8f41cSTejun Heo struct blkcg_gq { 111eea8f41cSTejun Heo /* Pointer to the associated request_queue */ 112eea8f41cSTejun Heo struct request_queue *q; 113eea8f41cSTejun Heo struct list_head q_node; 114eea8f41cSTejun Heo struct hlist_node blkcg_node; 115eea8f41cSTejun Heo struct blkcg *blkcg; 116eea8f41cSTejun Heo 117ce7acfeaSTejun Heo /* 118ce7acfeaSTejun Heo * Each blkg gets congested separately and the congestion state is 119ce7acfeaSTejun Heo * propagated to the matching bdi_writeback_congested. 120ce7acfeaSTejun Heo */ 121ce7acfeaSTejun Heo struct bdi_writeback_congested *wb_congested; 122ce7acfeaSTejun Heo 123eea8f41cSTejun Heo /* all non-root blkcg_gq's are guaranteed to have access to parent */ 124eea8f41cSTejun Heo struct blkcg_gq *parent; 125eea8f41cSTejun Heo 126eea8f41cSTejun Heo /* reference count */ 1277fcf2b03SDennis Zhou struct percpu_ref refcnt; 128eea8f41cSTejun Heo 129eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 130eea8f41cSTejun Heo bool online; 131eea8f41cSTejun Heo 13277ea7338STejun Heo struct blkg_rwstat stat_bytes; 13377ea7338STejun Heo struct blkg_rwstat stat_ios; 13477ea7338STejun Heo 135eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 136eea8f41cSTejun Heo 137eea8f41cSTejun Heo struct rcu_head rcu_head; 138d09d8df3SJosef Bacik 139d09d8df3SJosef Bacik atomic_t use_delay; 140d09d8df3SJosef Bacik atomic64_t delay_nsec; 141d09d8df3SJosef Bacik atomic64_t delay_start; 142d09d8df3SJosef Bacik u64 last_delay; 143d09d8df3SJosef Bacik int last_use; 144eea8f41cSTejun Heo }; 145eea8f41cSTejun Heo 146e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 14781437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 148e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 14969d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 150001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node); 151a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 152a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 153a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 154001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 155a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 156903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 157903d23f0SJosef Bacik size_t size); 158eea8f41cSTejun Heo 159eea8f41cSTejun Heo struct blkcg_policy { 160eea8f41cSTejun Heo int plid; 161eea8f41cSTejun Heo /* cgroup files for the policy */ 1622ee867dcSTejun Heo struct cftype *dfl_cftypes; 163880f50e2STejun Heo struct cftype *legacy_cftypes; 164eea8f41cSTejun Heo 165eea8f41cSTejun Heo /* operations */ 166e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 167e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 168e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 16969d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 170e4a9bde9STejun Heo 171001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 172eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 173eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 174eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 175001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 176eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 177903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 178eea8f41cSTejun Heo }; 179eea8f41cSTejun Heo 180eea8f41cSTejun Heo extern struct blkcg blkcg_root; 181496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 182eea8f41cSTejun Heo 18324f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 18424f29046STejun Heo struct request_queue *q, bool update_hint); 185b978962aSDennis Zhou struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg, 186b978962aSDennis Zhou struct request_queue *q); 187eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, 188eea8f41cSTejun Heo struct request_queue *q); 189eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q); 190eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q); 191eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q); 192eea8f41cSTejun Heo 193eea8f41cSTejun Heo /* Blkio controller policy registration */ 194eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol); 195eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol); 196eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q, 197eea8f41cSTejun Heo const struct blkcg_policy *pol); 198eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q, 199eea8f41cSTejun Heo const struct blkcg_policy *pol); 200eea8f41cSTejun Heo 201dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg); 202eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, 203eea8f41cSTejun Heo u64 (*prfill)(struct seq_file *, 204eea8f41cSTejun Heo struct blkg_policy_data *, int), 205eea8f41cSTejun Heo const struct blkcg_policy *pol, int data, 206eea8f41cSTejun Heo bool show_total); 207eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); 208eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 209eea8f41cSTejun Heo const struct blkg_rwstat *rwstat); 210eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off); 211eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 212eea8f41cSTejun Heo int off); 21377ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v); 21477ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v); 21577ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v); 21677ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v); 217eea8f41cSTejun Heo 218f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg, 219f12c74caSTejun Heo struct blkcg_policy *pol, int off); 220f12c74caSTejun Heo struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, 221f12c74caSTejun Heo struct blkcg_policy *pol, int off); 222eea8f41cSTejun Heo 223eea8f41cSTejun Heo struct blkg_conf_ctx { 224eea8f41cSTejun Heo struct gendisk *disk; 225eea8f41cSTejun Heo struct blkcg_gq *blkg; 22636aa9e5fSTejun Heo char *body; 227eea8f41cSTejun Heo }; 228eea8f41cSTejun Heo 229eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, 23036aa9e5fSTejun Heo char *input, struct blkg_conf_ctx *ctx); 231eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx); 232eea8f41cSTejun Heo 2330fe061b9SDennis Zhou /** 2340fe061b9SDennis Zhou * blkcg_css - find the current css 2350fe061b9SDennis Zhou * 2360fe061b9SDennis Zhou * Find the css associated with either the kthread or the current task. 2370fe061b9SDennis Zhou * This may return a dying css, so it is up to the caller to use tryget logic 2380fe061b9SDennis Zhou * to confirm it is alive and well. 2390fe061b9SDennis Zhou */ 2400fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_css(void) 2410fe061b9SDennis Zhou { 2420fe061b9SDennis Zhou struct cgroup_subsys_state *css; 2430fe061b9SDennis Zhou 2440fe061b9SDennis Zhou css = kthread_blkcg(); 2450fe061b9SDennis Zhou if (css) 2460fe061b9SDennis Zhou return css; 2470fe061b9SDennis Zhou return task_css(current, io_cgrp_id); 2480fe061b9SDennis Zhou } 2490fe061b9SDennis Zhou 250eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 251eea8f41cSTejun Heo { 252eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 253eea8f41cSTejun Heo } 254eea8f41cSTejun Heo 2550fe061b9SDennis Zhou /** 2560fe061b9SDennis Zhou * __bio_blkcg - internal, inconsistent version to get blkcg 2570fe061b9SDennis Zhou * 2580fe061b9SDennis Zhou * DO NOT USE. 2590fe061b9SDennis Zhou * This function is inconsistent and consequently is dangerous to use. The 2600fe061b9SDennis Zhou * first part of the function returns a blkcg where a reference is owned by the 2610fe061b9SDennis Zhou * bio. This means it does not need to be rcu protected as it cannot go away 2620fe061b9SDennis Zhou * with the bio owning a reference to it. However, the latter potentially gets 2630fe061b9SDennis Zhou * it from task_css(). This can race against task migration and the cgroup 2640fe061b9SDennis Zhou * dying. It is also semantically different as it must be called rcu protected 2650fe061b9SDennis Zhou * and is susceptible to failure when trying to get a reference to it. 2660fe061b9SDennis Zhou * Therefore, it is not ok to assume that *_get() will always succeed on the 2670fe061b9SDennis Zhou * blkcg returned here. 2680fe061b9SDennis Zhou */ 2690fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) 27027e6fa99SDennis Zhou (Facebook) { 271db6638d7SDennis Zhou if (bio && bio->bi_blkg) 272db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2730fe061b9SDennis Zhou return css_to_blkcg(blkcg_css()); 2740fe061b9SDennis Zhou } 2750fe061b9SDennis Zhou 2760fe061b9SDennis Zhou /** 2770fe061b9SDennis Zhou * bio_blkcg - grab the blkcg associated with a bio 2780fe061b9SDennis Zhou * @bio: target bio 2790fe061b9SDennis Zhou * 2800fe061b9SDennis Zhou * This returns the blkcg associated with a bio, %NULL if not associated. 2810fe061b9SDennis Zhou * Callers are expected to either handle %NULL or know association has been 2820fe061b9SDennis Zhou * done prior to calling this. 2830fe061b9SDennis Zhou */ 2840fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio) 2850fe061b9SDennis Zhou { 286db6638d7SDennis Zhou if (bio && bio->bi_blkg) 287db6638d7SDennis Zhou return bio->bi_blkg->blkcg; 2880fe061b9SDennis Zhou return NULL; 289fd383c2dSTejun Heo } 290fd383c2dSTejun Heo 291d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 292d09d8df3SJosef Bacik { 293d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 294d09d8df3SJosef Bacik bool ret = false; 295d09d8df3SJosef Bacik 296d09d8df3SJosef Bacik rcu_read_lock(); 297d09d8df3SJosef Bacik css = kthread_blkcg(); 298d09d8df3SJosef Bacik if (!css) 299d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 300d09d8df3SJosef Bacik while (css) { 301d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 302d09d8df3SJosef Bacik ret = true; 303d09d8df3SJosef Bacik break; 304d09d8df3SJosef Bacik } 305d09d8df3SJosef Bacik css = css->parent; 306d09d8df3SJosef Bacik } 307d09d8df3SJosef Bacik rcu_read_unlock(); 308d09d8df3SJosef Bacik return ret; 309d09d8df3SJosef Bacik } 310d09d8df3SJosef Bacik 311eea8f41cSTejun Heo /** 312c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 313c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 314c7c98fd3SJosef Bacik * 315c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 316c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 317c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 318c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 319c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 320c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 321c7c98fd3SJosef Bacik */ 322c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 323c7c98fd3SJosef Bacik { 3240d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 325c7c98fd3SJosef Bacik } 326c7c98fd3SJosef Bacik 327c7c98fd3SJosef Bacik /** 328eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 329eea8f41cSTejun Heo * @blkcg: blkcg of interest 330eea8f41cSTejun Heo * 331eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 332eea8f41cSTejun Heo */ 333eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 334eea8f41cSTejun Heo { 335eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 336eea8f41cSTejun Heo } 337eea8f41cSTejun Heo 338eea8f41cSTejun Heo /** 33924f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 34024f29046STejun Heo * @blkcg: blkcg of interest 34124f29046STejun Heo * @q: request_queue of interest 34224f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 34324f29046STejun Heo * 34424f29046STejun Heo * This is internal version and shouldn't be used by policy 34524f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 34624f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 34724f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 34824f29046STejun Heo */ 34924f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 35024f29046STejun Heo struct request_queue *q, 35124f29046STejun Heo bool update_hint) 35224f29046STejun Heo { 35324f29046STejun Heo struct blkcg_gq *blkg; 35424f29046STejun Heo 35585b6bc9dSTejun Heo if (blkcg == &blkcg_root) 35685b6bc9dSTejun Heo return q->root_blkg; 35785b6bc9dSTejun Heo 35824f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 35924f29046STejun Heo if (blkg && blkg->q == q) 36024f29046STejun Heo return blkg; 36124f29046STejun Heo 36224f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 36324f29046STejun Heo } 36424f29046STejun Heo 36524f29046STejun Heo /** 36624f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 36724f29046STejun Heo * @blkcg: blkcg of interest 36824f29046STejun Heo * @q: request_queue of interest 36924f29046STejun Heo * 37024f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 3718f4236d9SChristoph Hellwig * under RCU read loc. 37224f29046STejun Heo */ 37324f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 37424f29046STejun Heo struct request_queue *q) 37524f29046STejun Heo { 37624f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 37724f29046STejun Heo return __blkg_lookup(blkcg, q, false); 37824f29046STejun Heo } 37924f29046STejun Heo 38024f29046STejun Heo /** 381b86d865cSBart Van Assche * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair 3826bad9b21SBart Van Assche * @q: request_queue of interest 3836bad9b21SBart Van Assche * 3846bad9b21SBart Van Assche * Lookup blkg for @q at the root level. See also blkg_lookup(). 3856bad9b21SBart Van Assche */ 386b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 3876bad9b21SBart Van Assche { 388b86d865cSBart Van Assche return q->root_blkg; 3896bad9b21SBart Van Assche } 3906bad9b21SBart Van Assche 3916bad9b21SBart Van Assche /** 392eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 393eea8f41cSTejun Heo * @blkg: blkg of interest 394eea8f41cSTejun Heo * @pol: policy of interest 395eea8f41cSTejun Heo * 396eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 397eea8f41cSTejun Heo */ 398eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 399eea8f41cSTejun Heo struct blkcg_policy *pol) 400eea8f41cSTejun Heo { 401eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 402eea8f41cSTejun Heo } 403eea8f41cSTejun Heo 404e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 405e4bc13adSLinus Torvalds struct blkcg_policy *pol) 406e4bc13adSLinus Torvalds { 40781437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 408e4bc13adSLinus Torvalds } 409e4bc13adSLinus Torvalds 410eea8f41cSTejun Heo /** 411eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 412eea8f41cSTejun Heo * @pd: policy private data of interest 413eea8f41cSTejun Heo * 414eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 415eea8f41cSTejun Heo */ 416eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 417eea8f41cSTejun Heo { 418eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 419eea8f41cSTejun Heo } 420eea8f41cSTejun Heo 42181437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 42281437648STejun Heo { 42381437648STejun Heo return cpd ? cpd->blkcg : NULL; 42481437648STejun Heo } 42581437648STejun Heo 42659b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg); 42759b57717SDennis Zhou (Facebook) 42859b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK 42959b57717SDennis Zhou (Facebook) 43059b57717SDennis Zhou (Facebook) /** 43159b57717SDennis Zhou (Facebook) * blkcg_cgwb_get - get a reference for blkcg->cgwb_list 43259b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 43359b57717SDennis Zhou (Facebook) * 43459b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 43559b57717SDennis Zhou (Facebook) */ 43659b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) 43759b57717SDennis Zhou (Facebook) { 43859b57717SDennis Zhou (Facebook) refcount_inc(&blkcg->cgwb_refcnt); 43959b57717SDennis Zhou (Facebook) } 44059b57717SDennis Zhou (Facebook) 44159b57717SDennis Zhou (Facebook) /** 44259b57717SDennis Zhou (Facebook) * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list 44359b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 44459b57717SDennis Zhou (Facebook) * 44559b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 44659b57717SDennis Zhou (Facebook) * When this count goes to zero, all active wb has finished so the 44759b57717SDennis Zhou (Facebook) * blkcg can continue destruction by calling blkcg_destroy_blkgs(). 44859b57717SDennis Zhou (Facebook) * This work may occur in cgwb_release_workfn() on the cgwb_release 44959b57717SDennis Zhou (Facebook) * workqueue. 45059b57717SDennis Zhou (Facebook) */ 45159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 45259b57717SDennis Zhou (Facebook) { 45359b57717SDennis Zhou (Facebook) if (refcount_dec_and_test(&blkcg->cgwb_refcnt)) 45459b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 45559b57717SDennis Zhou (Facebook) } 45659b57717SDennis Zhou (Facebook) 45759b57717SDennis Zhou (Facebook) #else 45859b57717SDennis Zhou (Facebook) 45959b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { } 46059b57717SDennis Zhou (Facebook) 46159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 46259b57717SDennis Zhou (Facebook) { 46359b57717SDennis Zhou (Facebook) /* wb isn't being accounted, so trigger destruction right away */ 46459b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 46559b57717SDennis Zhou (Facebook) } 46659b57717SDennis Zhou (Facebook) 46759b57717SDennis Zhou (Facebook) #endif 46859b57717SDennis Zhou (Facebook) 469eea8f41cSTejun Heo /** 470eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 471eea8f41cSTejun Heo * @blkg: blkg of interest 472eea8f41cSTejun Heo * @buf: target buffer 473eea8f41cSTejun Heo * @buflen: target buffer length 474eea8f41cSTejun Heo * 475eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 476eea8f41cSTejun Heo */ 477eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 478eea8f41cSTejun Heo { 4794c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 480eea8f41cSTejun Heo } 481eea8f41cSTejun Heo 482eea8f41cSTejun Heo /** 483eea8f41cSTejun Heo * blkg_get - get a blkg reference 484eea8f41cSTejun Heo * @blkg: blkg to get 485eea8f41cSTejun Heo * 486eea8f41cSTejun Heo * The caller should be holding an existing reference. 487eea8f41cSTejun Heo */ 488eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 489eea8f41cSTejun Heo { 4907fcf2b03SDennis Zhou percpu_ref_get(&blkg->refcnt); 491eea8f41cSTejun Heo } 492eea8f41cSTejun Heo 493d09d8df3SJosef Bacik /** 4947754f669SDennis Zhou * blkg_tryget - try and get a blkg reference 495d09d8df3SJosef Bacik * @blkg: blkg to get 496d09d8df3SJosef Bacik * 497d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 498d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 499d09d8df3SJosef Bacik */ 5007754f669SDennis Zhou static inline bool blkg_tryget(struct blkcg_gq *blkg) 501d09d8df3SJosef Bacik { 502*6ab21879SDennis Zhou return blkg && percpu_ref_tryget(&blkg->refcnt); 50307b05bccSDennis Zhou (Facebook) } 504d09d8df3SJosef Bacik 505beea9da0SDennis Zhou /** 5067754f669SDennis Zhou * blkg_tryget_closest - try and get a blkg ref on the closet blkg 507beea9da0SDennis Zhou * @blkg: blkg to get 508beea9da0SDennis Zhou * 509*6ab21879SDennis Zhou * This needs to be called rcu protected. As the failure mode here is to walk 510*6ab21879SDennis Zhou * up the blkg tree, this ensure that the blkg->parent pointers are always 511*6ab21879SDennis Zhou * valid. This returns the blkg that it ended up taking a reference on or %NULL 512*6ab21879SDennis Zhou * if no reference was taken. 513beea9da0SDennis Zhou */ 5147754f669SDennis Zhou static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg) 515beea9da0SDennis Zhou { 516*6ab21879SDennis Zhou struct blkcg_gq *ret_blkg = NULL; 517beea9da0SDennis Zhou 518*6ab21879SDennis Zhou WARN_ON_ONCE(!rcu_read_lock_held()); 519*6ab21879SDennis Zhou 520*6ab21879SDennis Zhou while (blkg) { 521*6ab21879SDennis Zhou if (blkg_tryget(blkg)) { 522*6ab21879SDennis Zhou ret_blkg = blkg; 523*6ab21879SDennis Zhou break; 524*6ab21879SDennis Zhou } 525*6ab21879SDennis Zhou blkg = blkg->parent; 526*6ab21879SDennis Zhou } 527*6ab21879SDennis Zhou 528*6ab21879SDennis Zhou return ret_blkg; 529beea9da0SDennis Zhou } 530b5f2954dSDennis Zhou 531eea8f41cSTejun Heo /** 532eea8f41cSTejun Heo * blkg_put - put a blkg reference 533eea8f41cSTejun Heo * @blkg: blkg to put 534eea8f41cSTejun Heo */ 535eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 536eea8f41cSTejun Heo { 5377fcf2b03SDennis Zhou percpu_ref_put(&blkg->refcnt); 538eea8f41cSTejun Heo } 539eea8f41cSTejun Heo 540eea8f41cSTejun Heo /** 541eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 542eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 543eea8f41cSTejun Heo * @pos_css: used for iteration 544eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 545eea8f41cSTejun Heo * 546eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 547eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 548eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 549eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 550eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 551eea8f41cSTejun Heo */ 552eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 553eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 554eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 555eea8f41cSTejun Heo (p_blkg)->q, false))) 556eea8f41cSTejun Heo 557eea8f41cSTejun Heo /** 558eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 559eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 560eea8f41cSTejun Heo * @pos_css: used for iteration 561eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 562eea8f41cSTejun Heo * 563eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 564eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 565eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 566eea8f41cSTejun Heo */ 567eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 568eea8f41cSTejun Heo css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ 569eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 570eea8f41cSTejun Heo (p_blkg)->q, false))) 571eea8f41cSTejun Heo 57224bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp) 573eea8f41cSTejun Heo { 57424bdb8efSTejun Heo int ret; 57524bdb8efSTejun Heo 57624bdb8efSTejun Heo ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp); 57724bdb8efSTejun Heo if (ret) 57824bdb8efSTejun Heo return ret; 57924bdb8efSTejun Heo 580e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 58124bdb8efSTejun Heo return 0; 58224bdb8efSTejun Heo } 58324bdb8efSTejun Heo 58424bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat) 58524bdb8efSTejun Heo { 58624bdb8efSTejun Heo percpu_counter_destroy(&stat->cpu_cnt); 587eea8f41cSTejun Heo } 588eea8f41cSTejun Heo 589eea8f41cSTejun Heo /** 590eea8f41cSTejun Heo * blkg_stat_add - add a value to a blkg_stat 591eea8f41cSTejun Heo * @stat: target blkg_stat 592eea8f41cSTejun Heo * @val: value to add 593eea8f41cSTejun Heo * 59424bdb8efSTejun Heo * Add @val to @stat. The caller must ensure that IRQ on the same CPU 59524bdb8efSTejun Heo * don't re-enter this function for the same counter. 596eea8f41cSTejun Heo */ 597eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val) 598eea8f41cSTejun Heo { 599104b4e51SNikolay Borisov percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH); 600eea8f41cSTejun Heo } 601eea8f41cSTejun Heo 602eea8f41cSTejun Heo /** 603eea8f41cSTejun Heo * blkg_stat_read - read the current value of a blkg_stat 604eea8f41cSTejun Heo * @stat: blkg_stat to read 605eea8f41cSTejun Heo */ 606eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat) 607eea8f41cSTejun Heo { 60824bdb8efSTejun Heo return percpu_counter_sum_positive(&stat->cpu_cnt); 609eea8f41cSTejun Heo } 610eea8f41cSTejun Heo 611eea8f41cSTejun Heo /** 612eea8f41cSTejun Heo * blkg_stat_reset - reset a blkg_stat 613eea8f41cSTejun Heo * @stat: blkg_stat to reset 614eea8f41cSTejun Heo */ 615eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat) 616eea8f41cSTejun Heo { 61724bdb8efSTejun Heo percpu_counter_set(&stat->cpu_cnt, 0); 618e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 619eea8f41cSTejun Heo } 620eea8f41cSTejun Heo 621eea8f41cSTejun Heo /** 622e6269c44STejun Heo * blkg_stat_add_aux - add a blkg_stat into another's aux count 623eea8f41cSTejun Heo * @to: the destination blkg_stat 624eea8f41cSTejun Heo * @from: the source 625eea8f41cSTejun Heo * 626e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 627eea8f41cSTejun Heo */ 628e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to, 629e6269c44STejun Heo struct blkg_stat *from) 630eea8f41cSTejun Heo { 631e6269c44STejun Heo atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt), 632e6269c44STejun Heo &to->aux_cnt); 633eea8f41cSTejun Heo } 634eea8f41cSTejun Heo 63524bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp) 63624bdb8efSTejun Heo { 63724bdb8efSTejun Heo int i, ret; 63824bdb8efSTejun Heo 63924bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 64024bdb8efSTejun Heo ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp); 64124bdb8efSTejun Heo if (ret) { 64224bdb8efSTejun Heo while (--i >= 0) 64324bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 64424bdb8efSTejun Heo return ret; 64524bdb8efSTejun Heo } 64624bdb8efSTejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 64724bdb8efSTejun Heo } 64824bdb8efSTejun Heo return 0; 64924bdb8efSTejun Heo } 65024bdb8efSTejun Heo 65124bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat) 652eea8f41cSTejun Heo { 653e6269c44STejun Heo int i; 654e6269c44STejun Heo 655e6269c44STejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 65624bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 657eea8f41cSTejun Heo } 658eea8f41cSTejun Heo 659eea8f41cSTejun Heo /** 660eea8f41cSTejun Heo * blkg_rwstat_add - add a value to a blkg_rwstat 661eea8f41cSTejun Heo * @rwstat: target blkg_rwstat 662ef295ecfSChristoph Hellwig * @op: REQ_OP and flags 663eea8f41cSTejun Heo * @val: value to add 664eea8f41cSTejun Heo * 665eea8f41cSTejun Heo * Add @val to @rwstat. The counters are chosen according to @rw. The 666eea8f41cSTejun Heo * caller is responsible for synchronizing calls to this function. 667eea8f41cSTejun Heo */ 668eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, 669ef295ecfSChristoph Hellwig unsigned int op, uint64_t val) 670eea8f41cSTejun Heo { 67124bdb8efSTejun Heo struct percpu_counter *cnt; 672eea8f41cSTejun Heo 673636620b6STejun Heo if (op_is_discard(op)) 674636620b6STejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD]; 675636620b6STejun Heo else if (op_is_write(op)) 67624bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE]; 677eea8f41cSTejun Heo else 67824bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ]; 679eea8f41cSTejun Heo 680104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 68124bdb8efSTejun Heo 682d71d9ae1SChristoph Hellwig if (op_is_sync(op)) 68324bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC]; 68424bdb8efSTejun Heo else 68524bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC]; 68624bdb8efSTejun Heo 687104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 688eea8f41cSTejun Heo } 689eea8f41cSTejun Heo 690eea8f41cSTejun Heo /** 691eea8f41cSTejun Heo * blkg_rwstat_read - read the current values of a blkg_rwstat 692eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 693eea8f41cSTejun Heo * 69424bdb8efSTejun Heo * Read the current snapshot of @rwstat and return it in the aux counts. 695eea8f41cSTejun Heo */ 696eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat) 697eea8f41cSTejun Heo { 69824bdb8efSTejun Heo struct blkg_rwstat result; 69924bdb8efSTejun Heo int i; 700eea8f41cSTejun Heo 70124bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 70224bdb8efSTejun Heo atomic64_set(&result.aux_cnt[i], 70324bdb8efSTejun Heo percpu_counter_sum_positive(&rwstat->cpu_cnt[i])); 70424bdb8efSTejun Heo return result; 705eea8f41cSTejun Heo } 706eea8f41cSTejun Heo 707eea8f41cSTejun Heo /** 708eea8f41cSTejun Heo * blkg_rwstat_total - read the total count of a blkg_rwstat 709eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 710eea8f41cSTejun Heo * 711eea8f41cSTejun Heo * Return the total count of @rwstat regardless of the IO direction. This 712eea8f41cSTejun Heo * function can be called without synchronization and takes care of u64 713eea8f41cSTejun Heo * atomicity. 714eea8f41cSTejun Heo */ 715eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat) 716eea8f41cSTejun Heo { 717eea8f41cSTejun Heo struct blkg_rwstat tmp = blkg_rwstat_read(rwstat); 718eea8f41cSTejun Heo 71924bdb8efSTejun Heo return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + 72024bdb8efSTejun Heo atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); 721eea8f41cSTejun Heo } 722eea8f41cSTejun Heo 723eea8f41cSTejun Heo /** 724eea8f41cSTejun Heo * blkg_rwstat_reset - reset a blkg_rwstat 725eea8f41cSTejun Heo * @rwstat: blkg_rwstat to reset 726eea8f41cSTejun Heo */ 727eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) 728eea8f41cSTejun Heo { 729e6269c44STejun Heo int i; 730e6269c44STejun Heo 73124bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 73224bdb8efSTejun Heo percpu_counter_set(&rwstat->cpu_cnt[i], 0); 733e6269c44STejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 734eea8f41cSTejun Heo } 73524bdb8efSTejun Heo } 736eea8f41cSTejun Heo 737eea8f41cSTejun Heo /** 738e6269c44STejun Heo * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count 739eea8f41cSTejun Heo * @to: the destination blkg_rwstat 740eea8f41cSTejun Heo * @from: the source 741eea8f41cSTejun Heo * 742e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 743eea8f41cSTejun Heo */ 744e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to, 745eea8f41cSTejun Heo struct blkg_rwstat *from) 746eea8f41cSTejun Heo { 747ddc21231SArnd Bergmann u64 sum[BLKG_RWSTAT_NR]; 748eea8f41cSTejun Heo int i; 749eea8f41cSTejun Heo 750eea8f41cSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 751ddc21231SArnd Bergmann sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]); 752ddc21231SArnd Bergmann 753ddc21231SArnd Bergmann for (i = 0; i < BLKG_RWSTAT_NR; i++) 754ddc21231SArnd Bergmann atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]), 755e6269c44STejun Heo &to->aux_cnt[i]); 756eea8f41cSTejun Heo } 757eea8f41cSTejun Heo 758ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING 759ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 760ae118896STejun Heo struct bio *bio); 761ae118896STejun Heo #else 762ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 763ae118896STejun Heo struct bio *bio) { return false; } 764ae118896STejun Heo #endif 765ae118896STejun Heo 766e439bedfSDennis Zhou 767e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) 768e439bedfSDennis Zhou { 769e439bedfSDennis Zhou bio_issue_init(&bio->bi_issue, bio_sectors(bio)); 770e439bedfSDennis Zhou } 771e439bedfSDennis Zhou 772ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 773ae118896STejun Heo struct bio *bio) 774ae118896STejun Heo { 775ae118896STejun Heo struct blkcg_gq *blkg; 776ae118896STejun Heo bool throtl = false; 777ae118896STejun Heo 7784705de73SDennis Zhou rcu_read_lock(); 7794705de73SDennis Zhou 7805cdf2e3fSDennis Zhou if (!bio->bi_blkg) { 7815cdf2e3fSDennis Zhou char b[BDEVNAME_SIZE]; 782ae118896STejun Heo 7835cdf2e3fSDennis Zhou WARN_ONCE(1, 7845cdf2e3fSDennis Zhou "no blkg associated for bio on block-device: %s\n", 7855cdf2e3fSDennis Zhou bio_devname(bio, b)); 7865cdf2e3fSDennis Zhou bio_associate_blkg(bio); 7875cdf2e3fSDennis Zhou } 7885cdf2e3fSDennis Zhou 7895cdf2e3fSDennis Zhou blkg = bio->bi_blkg; 790ae118896STejun Heo 791ae118896STejun Heo throtl = blk_throtl_bio(q, blkg, bio); 792ae118896STejun Heo 79377ea7338STejun Heo if (!throtl) { 794c454edc2SJosef Bacik /* 795c454edc2SJosef Bacik * If the bio is flagged with BIO_QUEUE_ENTERED it means this 796c454edc2SJosef Bacik * is a split bio and we would have already accounted for the 797c454edc2SJosef Bacik * size of the bio. 798c454edc2SJosef Bacik */ 799c454edc2SJosef Bacik if (!bio_flagged(bio, BIO_QUEUE_ENTERED)) 800ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf, 80177ea7338STejun Heo bio->bi_iter.bi_size); 802ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1); 80377ea7338STejun Heo } 80477ea7338STejun Heo 805e439bedfSDennis Zhou blkcg_bio_issue_init(bio); 806e439bedfSDennis Zhou 8074705de73SDennis Zhou rcu_read_unlock(); 808ae118896STejun Heo return !throtl; 809ae118896STejun Heo } 810ae118896STejun Heo 811d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 812d09d8df3SJosef Bacik { 813d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 814d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 815d09d8df3SJosef Bacik } 816d09d8df3SJosef Bacik 817d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 818d09d8df3SJosef Bacik { 819d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 820d09d8df3SJosef Bacik 821d09d8df3SJosef Bacik if (old == 0) 822d09d8df3SJosef Bacik return 0; 823d09d8df3SJosef Bacik 824d09d8df3SJosef Bacik /* 825d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 826d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 827d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 828d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 829d09d8df3SJosef Bacik * congestion count on the cgroup. 830d09d8df3SJosef Bacik */ 831d09d8df3SJosef Bacik while (old) { 832d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 833d09d8df3SJosef Bacik if (cur == old) 834d09d8df3SJosef Bacik break; 835d09d8df3SJosef Bacik old = cur; 836d09d8df3SJosef Bacik } 837d09d8df3SJosef Bacik 838d09d8df3SJosef Bacik if (old == 0) 839d09d8df3SJosef Bacik return 0; 840d09d8df3SJosef Bacik if (old == 1) 841d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 842d09d8df3SJosef Bacik return 1; 843d09d8df3SJosef Bacik } 844d09d8df3SJosef Bacik 845d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 846d09d8df3SJosef Bacik { 847d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 848d09d8df3SJosef Bacik if (!old) 849d09d8df3SJosef Bacik return; 850d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 851d09d8df3SJosef Bacik while (old) { 852d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); 853d09d8df3SJosef Bacik if (cur == old) { 854d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 855d09d8df3SJosef Bacik break; 856d09d8df3SJosef Bacik } 857d09d8df3SJosef Bacik old = cur; 858d09d8df3SJosef Bacik } 859d09d8df3SJosef Bacik } 860d09d8df3SJosef Bacik 861d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 862d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 863d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 864eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 865eea8f41cSTejun Heo 866efa7d1c7STejun Heo struct blkcg { 867efa7d1c7STejun Heo }; 868eea8f41cSTejun Heo 869eea8f41cSTejun Heo struct blkg_policy_data { 870eea8f41cSTejun Heo }; 871eea8f41cSTejun Heo 872e4bc13adSLinus Torvalds struct blkcg_policy_data { 873e4bc13adSLinus Torvalds }; 874e4bc13adSLinus Torvalds 875eea8f41cSTejun Heo struct blkcg_gq { 876eea8f41cSTejun Heo }; 877eea8f41cSTejun Heo 878eea8f41cSTejun Heo struct blkcg_policy { 879eea8f41cSTejun Heo }; 880eea8f41cSTejun Heo 881496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 882496d5e75STejun Heo 883d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 884d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 885d09d8df3SJosef Bacik 886efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 887efa7d1c7STejun Heo 888d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 889d09d8df3SJosef Bacik 890eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 891b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 892b86d865cSBart Van Assche { return NULL; } 893eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 894eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { } 895eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 896eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 897eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 898eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 899eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 900eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 901eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 902eea8f41cSTejun Heo 9030fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; } 904eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 905eea8f41cSTejun Heo 906eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 907eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 908eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 909eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 910eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 911eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 912eea8f41cSTejun Heo 913e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) { } 914ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 915ae118896STejun Heo struct bio *bio) { return true; } 916ae118896STejun Heo 917eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 918eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 919eea8f41cSTejun Heo 920efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 921eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 922eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 923