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> 24eea8f41cSTejun Heo 2524bdb8efSTejun Heo /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */ 2624bdb8efSTejun Heo #define BLKG_STAT_CPU_BATCH (INT_MAX / 2) 2724bdb8efSTejun Heo 28eea8f41cSTejun Heo /* Max limits for throttle policy */ 29eea8f41cSTejun Heo #define THROTL_IOPS_MAX UINT_MAX 30eea8f41cSTejun Heo 31eea8f41cSTejun Heo #ifdef CONFIG_BLK_CGROUP 32eea8f41cSTejun Heo 33eea8f41cSTejun Heo enum blkg_rwstat_type { 34eea8f41cSTejun Heo BLKG_RWSTAT_READ, 35eea8f41cSTejun Heo BLKG_RWSTAT_WRITE, 36eea8f41cSTejun Heo BLKG_RWSTAT_SYNC, 37eea8f41cSTejun Heo BLKG_RWSTAT_ASYNC, 38636620b6STejun Heo BLKG_RWSTAT_DISCARD, 39eea8f41cSTejun Heo 40eea8f41cSTejun Heo BLKG_RWSTAT_NR, 41eea8f41cSTejun Heo BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR, 42eea8f41cSTejun Heo }; 43eea8f41cSTejun Heo 44eea8f41cSTejun Heo struct blkcg_gq; 45eea8f41cSTejun Heo 46eea8f41cSTejun Heo struct blkcg { 47eea8f41cSTejun Heo struct cgroup_subsys_state css; 48eea8f41cSTejun Heo spinlock_t lock; 49eea8f41cSTejun Heo 50eea8f41cSTejun Heo struct radix_tree_root blkg_tree; 5155679c8dSBart Van Assche struct blkcg_gq __rcu *blkg_hint; 52eea8f41cSTejun Heo struct hlist_head blkg_list; 53eea8f41cSTejun Heo 5481437648STejun Heo struct blkcg_policy_data *cpd[BLKCG_MAX_POLS]; 5552ebea74STejun Heo 567876f930STejun Heo struct list_head all_blkcgs_node; 5752ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 5852ebea74STejun Heo struct list_head cgwb_list; 5959b57717SDennis Zhou (Facebook) refcount_t cgwb_refcnt; 6052ebea74STejun Heo #endif 61eea8f41cSTejun Heo }; 62eea8f41cSTejun Heo 63e6269c44STejun Heo /* 64e6269c44STejun Heo * blkg_[rw]stat->aux_cnt is excluded for local stats but included for 6524bdb8efSTejun Heo * recursive. Used to carry stats of dead children, and, for blkg_rwstat, 6624bdb8efSTejun Heo * to carry result values from read and sum operations. 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 78eea8f41cSTejun Heo /* 79eea8f41cSTejun Heo * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a 80eea8f41cSTejun Heo * request_queue (q). This is used by blkcg policies which need to track 81eea8f41cSTejun Heo * information per blkcg - q pair. 82eea8f41cSTejun Heo * 83001bea73STejun Heo * There can be multiple active blkcg policies and each blkg:policy pair is 84001bea73STejun Heo * represented by a blkg_policy_data which is allocated and freed by each 85001bea73STejun Heo * policy's pd_alloc/free_fn() methods. A policy can allocate private data 86001bea73STejun Heo * area by allocating larger data structure which embeds blkg_policy_data 87001bea73STejun Heo * at the beginning. 88eea8f41cSTejun Heo */ 89eea8f41cSTejun Heo struct blkg_policy_data { 90eea8f41cSTejun Heo /* the blkg and policy id this per-policy data belongs to */ 91eea8f41cSTejun Heo struct blkcg_gq *blkg; 92eea8f41cSTejun Heo int plid; 93eea8f41cSTejun Heo }; 94eea8f41cSTejun Heo 95e4bc13adSLinus Torvalds /* 96e4a9bde9STejun Heo * Policies that need to keep per-blkcg data which is independent from any 97e4a9bde9STejun Heo * request_queue associated to it should implement cpd_alloc/free_fn() 98e4a9bde9STejun Heo * methods. A policy can allocate private data area by allocating larger 99e4a9bde9STejun Heo * data structure which embeds blkcg_policy_data at the beginning. 100e4a9bde9STejun Heo * cpd_init() is invoked to let each policy handle per-blkcg data. 101e4bc13adSLinus Torvalds */ 102e4bc13adSLinus Torvalds struct blkcg_policy_data { 10381437648STejun Heo /* the blkcg and policy id this per-policy data belongs to */ 10481437648STejun Heo struct blkcg *blkcg; 105e4bc13adSLinus Torvalds int plid; 106e4bc13adSLinus Torvalds }; 107e4bc13adSLinus Torvalds 108eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */ 109eea8f41cSTejun Heo struct blkcg_gq { 110eea8f41cSTejun Heo /* Pointer to the associated request_queue */ 111eea8f41cSTejun Heo struct request_queue *q; 112eea8f41cSTejun Heo struct list_head q_node; 113eea8f41cSTejun Heo struct hlist_node blkcg_node; 114eea8f41cSTejun Heo struct blkcg *blkcg; 115eea8f41cSTejun Heo 116ce7acfeaSTejun Heo /* 117ce7acfeaSTejun Heo * Each blkg gets congested separately and the congestion state is 118ce7acfeaSTejun Heo * propagated to the matching bdi_writeback_congested. 119ce7acfeaSTejun Heo */ 120ce7acfeaSTejun Heo struct bdi_writeback_congested *wb_congested; 121ce7acfeaSTejun Heo 122eea8f41cSTejun Heo /* all non-root blkcg_gq's are guaranteed to have access to parent */ 123eea8f41cSTejun Heo struct blkcg_gq *parent; 124eea8f41cSTejun Heo 125eea8f41cSTejun Heo /* reference count */ 126b5f2954dSDennis Zhou atomic_t refcnt; 127eea8f41cSTejun Heo 128eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 129eea8f41cSTejun Heo bool online; 130eea8f41cSTejun Heo 13177ea7338STejun Heo struct blkg_rwstat stat_bytes; 13277ea7338STejun Heo struct blkg_rwstat stat_ios; 13377ea7338STejun Heo 134eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 135eea8f41cSTejun Heo 136eea8f41cSTejun Heo struct rcu_head rcu_head; 137d09d8df3SJosef Bacik 138d09d8df3SJosef Bacik atomic_t use_delay; 139d09d8df3SJosef Bacik atomic64_t delay_nsec; 140d09d8df3SJosef Bacik atomic64_t delay_start; 141d09d8df3SJosef Bacik u64 last_delay; 142d09d8df3SJosef Bacik int last_use; 143eea8f41cSTejun Heo }; 144eea8f41cSTejun Heo 145e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 14681437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 147e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 14869d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 149001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node); 150a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 151a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 152a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 153001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 154a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 155903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 156903d23f0SJosef Bacik size_t size); 157eea8f41cSTejun Heo 158eea8f41cSTejun Heo struct blkcg_policy { 159eea8f41cSTejun Heo int plid; 160eea8f41cSTejun Heo /* cgroup files for the policy */ 1612ee867dcSTejun Heo struct cftype *dfl_cftypes; 162880f50e2STejun Heo struct cftype *legacy_cftypes; 163eea8f41cSTejun Heo 164eea8f41cSTejun Heo /* operations */ 165e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 166e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 167e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 16869d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 169e4a9bde9STejun Heo 170001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 171eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 172eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 173eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 174001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 175eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 176903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 177eea8f41cSTejun Heo }; 178eea8f41cSTejun Heo 179eea8f41cSTejun Heo extern struct blkcg blkcg_root; 180496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 181eea8f41cSTejun Heo 18224f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 18324f29046STejun Heo struct request_queue *q, bool update_hint); 184eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, 185eea8f41cSTejun Heo struct request_queue *q); 186eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q); 187eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q); 188eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q); 189eea8f41cSTejun Heo 190eea8f41cSTejun Heo /* Blkio controller policy registration */ 191eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol); 192eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol); 193eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q, 194eea8f41cSTejun Heo const struct blkcg_policy *pol); 195eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q, 196eea8f41cSTejun Heo const struct blkcg_policy *pol); 197eea8f41cSTejun Heo 198dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg); 199eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, 200eea8f41cSTejun Heo u64 (*prfill)(struct seq_file *, 201eea8f41cSTejun Heo struct blkg_policy_data *, int), 202eea8f41cSTejun Heo const struct blkcg_policy *pol, int data, 203eea8f41cSTejun Heo bool show_total); 204eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); 205eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 206eea8f41cSTejun Heo const struct blkg_rwstat *rwstat); 207eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off); 208eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 209eea8f41cSTejun Heo int off); 21077ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v); 21177ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v); 21277ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v); 21377ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v); 214eea8f41cSTejun Heo 215f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg, 216f12c74caSTejun Heo struct blkcg_policy *pol, int off); 217f12c74caSTejun Heo struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, 218f12c74caSTejun Heo struct blkcg_policy *pol, int off); 219eea8f41cSTejun Heo 220eea8f41cSTejun Heo struct blkg_conf_ctx { 221eea8f41cSTejun Heo struct gendisk *disk; 222eea8f41cSTejun Heo struct blkcg_gq *blkg; 22336aa9e5fSTejun Heo char *body; 224eea8f41cSTejun Heo }; 225eea8f41cSTejun Heo 226eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, 22736aa9e5fSTejun Heo char *input, struct blkg_conf_ctx *ctx); 228eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx); 229eea8f41cSTejun Heo 23027e6fa99SDennis Zhou (Facebook) 231eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 232eea8f41cSTejun Heo { 233eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 234eea8f41cSTejun Heo } 235eea8f41cSTejun Heo 23627e6fa99SDennis Zhou (Facebook) static inline struct blkcg *bio_blkcg(struct bio *bio) 23727e6fa99SDennis Zhou (Facebook) { 238b5f2954dSDennis Zhou struct cgroup_subsys_state *css; 239b5f2954dSDennis Zhou 240b5f2954dSDennis Zhou if (bio && bio->bi_css) 241b5f2954dSDennis Zhou return css_to_blkcg(bio->bi_css); 242b5f2954dSDennis Zhou css = kthread_blkcg(); 243b5f2954dSDennis Zhou if (css) 244b5f2954dSDennis Zhou return css_to_blkcg(css); 245b5f2954dSDennis Zhou return css_to_blkcg(task_css(current, io_cgrp_id)); 246fd383c2dSTejun Heo } 247fd383c2dSTejun Heo 248d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 249d09d8df3SJosef Bacik { 250d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 251d09d8df3SJosef Bacik bool ret = false; 252d09d8df3SJosef Bacik 253d09d8df3SJosef Bacik rcu_read_lock(); 254d09d8df3SJosef Bacik css = kthread_blkcg(); 255d09d8df3SJosef Bacik if (!css) 256d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 257d09d8df3SJosef Bacik while (css) { 258d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 259d09d8df3SJosef Bacik ret = true; 260d09d8df3SJosef Bacik break; 261d09d8df3SJosef Bacik } 262d09d8df3SJosef Bacik css = css->parent; 263d09d8df3SJosef Bacik } 264d09d8df3SJosef Bacik rcu_read_unlock(); 265d09d8df3SJosef Bacik return ret; 266d09d8df3SJosef Bacik } 267d09d8df3SJosef Bacik 268eea8f41cSTejun Heo /** 269c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 270c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 271c7c98fd3SJosef Bacik * 272c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 273c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 274c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 275c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 276c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 277c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 278c7c98fd3SJosef Bacik */ 279c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 280c7c98fd3SJosef Bacik { 2810d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 282c7c98fd3SJosef Bacik } 283c7c98fd3SJosef Bacik 284c7c98fd3SJosef Bacik /** 285eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 286eea8f41cSTejun Heo * @blkcg: blkcg of interest 287eea8f41cSTejun Heo * 288eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 289eea8f41cSTejun Heo */ 290eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 291eea8f41cSTejun Heo { 292eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 293eea8f41cSTejun Heo } 294eea8f41cSTejun Heo 295eea8f41cSTejun Heo /** 29624f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 29724f29046STejun Heo * @blkcg: blkcg of interest 29824f29046STejun Heo * @q: request_queue of interest 29924f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 30024f29046STejun Heo * 30124f29046STejun Heo * This is internal version and shouldn't be used by policy 30224f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 30324f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 30424f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 30524f29046STejun Heo */ 30624f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 30724f29046STejun Heo struct request_queue *q, 30824f29046STejun Heo bool update_hint) 30924f29046STejun Heo { 31024f29046STejun Heo struct blkcg_gq *blkg; 31124f29046STejun Heo 31285b6bc9dSTejun Heo if (blkcg == &blkcg_root) 31385b6bc9dSTejun Heo return q->root_blkg; 31485b6bc9dSTejun Heo 31524f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 31624f29046STejun Heo if (blkg && blkg->q == q) 31724f29046STejun Heo return blkg; 31824f29046STejun Heo 31924f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 32024f29046STejun Heo } 32124f29046STejun Heo 32224f29046STejun Heo /** 32324f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 32424f29046STejun Heo * @blkcg: blkcg of interest 32524f29046STejun Heo * @q: request_queue of interest 32624f29046STejun Heo * 32724f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 3288f4236d9SChristoph Hellwig * under RCU read loc. 32924f29046STejun Heo */ 33024f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 33124f29046STejun Heo struct request_queue *q) 33224f29046STejun Heo { 33324f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 33424f29046STejun Heo return __blkg_lookup(blkcg, q, false); 33524f29046STejun Heo } 33624f29046STejun Heo 33724f29046STejun Heo /** 338b86d865cSBart Van Assche * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair 3396bad9b21SBart Van Assche * @q: request_queue of interest 3406bad9b21SBart Van Assche * 3416bad9b21SBart Van Assche * Lookup blkg for @q at the root level. See also blkg_lookup(). 3426bad9b21SBart Van Assche */ 343b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 3446bad9b21SBart Van Assche { 345b86d865cSBart Van Assche return q->root_blkg; 3466bad9b21SBart Van Assche } 3476bad9b21SBart Van Assche 3486bad9b21SBart Van Assche /** 349eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 350eea8f41cSTejun Heo * @blkg: blkg of interest 351eea8f41cSTejun Heo * @pol: policy of interest 352eea8f41cSTejun Heo * 353eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 354eea8f41cSTejun Heo */ 355eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 356eea8f41cSTejun Heo struct blkcg_policy *pol) 357eea8f41cSTejun Heo { 358eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 359eea8f41cSTejun Heo } 360eea8f41cSTejun Heo 361e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 362e4bc13adSLinus Torvalds struct blkcg_policy *pol) 363e4bc13adSLinus Torvalds { 36481437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 365e4bc13adSLinus Torvalds } 366e4bc13adSLinus Torvalds 367eea8f41cSTejun Heo /** 368eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 369eea8f41cSTejun Heo * @pd: policy private data of interest 370eea8f41cSTejun Heo * 371eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 372eea8f41cSTejun Heo */ 373eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 374eea8f41cSTejun Heo { 375eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 376eea8f41cSTejun Heo } 377eea8f41cSTejun Heo 37881437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 37981437648STejun Heo { 38081437648STejun Heo return cpd ? cpd->blkcg : NULL; 38181437648STejun Heo } 38281437648STejun Heo 38359b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg); 38459b57717SDennis Zhou (Facebook) 38559b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK 38659b57717SDennis Zhou (Facebook) 38759b57717SDennis Zhou (Facebook) /** 38859b57717SDennis Zhou (Facebook) * blkcg_cgwb_get - get a reference for blkcg->cgwb_list 38959b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 39059b57717SDennis Zhou (Facebook) * 39159b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 39259b57717SDennis Zhou (Facebook) */ 39359b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) 39459b57717SDennis Zhou (Facebook) { 39559b57717SDennis Zhou (Facebook) refcount_inc(&blkcg->cgwb_refcnt); 39659b57717SDennis Zhou (Facebook) } 39759b57717SDennis Zhou (Facebook) 39859b57717SDennis Zhou (Facebook) /** 39959b57717SDennis Zhou (Facebook) * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list 40059b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 40159b57717SDennis Zhou (Facebook) * 40259b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 40359b57717SDennis Zhou (Facebook) * When this count goes to zero, all active wb has finished so the 40459b57717SDennis Zhou (Facebook) * blkcg can continue destruction by calling blkcg_destroy_blkgs(). 40559b57717SDennis Zhou (Facebook) * This work may occur in cgwb_release_workfn() on the cgwb_release 40659b57717SDennis Zhou (Facebook) * workqueue. 40759b57717SDennis Zhou (Facebook) */ 40859b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 40959b57717SDennis Zhou (Facebook) { 41059b57717SDennis Zhou (Facebook) if (refcount_dec_and_test(&blkcg->cgwb_refcnt)) 41159b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 41259b57717SDennis Zhou (Facebook) } 41359b57717SDennis Zhou (Facebook) 41459b57717SDennis Zhou (Facebook) #else 41559b57717SDennis Zhou (Facebook) 41659b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { } 41759b57717SDennis Zhou (Facebook) 41859b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 41959b57717SDennis Zhou (Facebook) { 42059b57717SDennis Zhou (Facebook) /* wb isn't being accounted, so trigger destruction right away */ 42159b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 42259b57717SDennis Zhou (Facebook) } 42359b57717SDennis Zhou (Facebook) 42459b57717SDennis Zhou (Facebook) #endif 42559b57717SDennis Zhou (Facebook) 426eea8f41cSTejun Heo /** 427eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 428eea8f41cSTejun Heo * @blkg: blkg of interest 429eea8f41cSTejun Heo * @buf: target buffer 430eea8f41cSTejun Heo * @buflen: target buffer length 431eea8f41cSTejun Heo * 432eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 433eea8f41cSTejun Heo */ 434eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 435eea8f41cSTejun Heo { 4364c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 437eea8f41cSTejun Heo } 438eea8f41cSTejun Heo 439eea8f41cSTejun Heo /** 440eea8f41cSTejun Heo * blkg_get - get a blkg reference 441eea8f41cSTejun Heo * @blkg: blkg to get 442eea8f41cSTejun Heo * 443eea8f41cSTejun Heo * The caller should be holding an existing reference. 444eea8f41cSTejun Heo */ 445eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 446eea8f41cSTejun Heo { 447b5f2954dSDennis Zhou WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0); 448b5f2954dSDennis Zhou atomic_inc(&blkg->refcnt); 449eea8f41cSTejun Heo } 450eea8f41cSTejun Heo 451d09d8df3SJosef Bacik /** 452b5f2954dSDennis Zhou * blkg_try_get - try and get a blkg reference 453d09d8df3SJosef Bacik * @blkg: blkg to get 454d09d8df3SJosef Bacik * 455d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 456d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 457d09d8df3SJosef Bacik */ 458b5f2954dSDennis Zhou static inline struct blkcg_gq *blkg_try_get(struct blkcg_gq *blkg) 459d09d8df3SJosef Bacik { 460b5f2954dSDennis Zhou if (atomic_inc_not_zero(&blkg->refcnt)) 46107b05bccSDennis Zhou (Facebook) return blkg; 462b5f2954dSDennis Zhou return NULL; 46307b05bccSDennis Zhou (Facebook) } 464d09d8df3SJosef Bacik 465b5f2954dSDennis Zhou 466b5f2954dSDennis Zhou void __blkg_release_rcu(struct rcu_head *rcu); 467b5f2954dSDennis Zhou 468eea8f41cSTejun Heo /** 469eea8f41cSTejun Heo * blkg_put - put a blkg reference 470eea8f41cSTejun Heo * @blkg: blkg to put 471eea8f41cSTejun Heo */ 472eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 473eea8f41cSTejun Heo { 474b5f2954dSDennis Zhou WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0); 475b5f2954dSDennis Zhou if (atomic_dec_and_test(&blkg->refcnt)) 476b5f2954dSDennis Zhou call_rcu(&blkg->rcu_head, __blkg_release_rcu); 477eea8f41cSTejun Heo } 478eea8f41cSTejun Heo 479eea8f41cSTejun Heo /** 480eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 481eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 482eea8f41cSTejun Heo * @pos_css: used for iteration 483eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 484eea8f41cSTejun Heo * 485eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 486eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 487eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 488eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 489eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 490eea8f41cSTejun Heo */ 491eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 492eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 493eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 494eea8f41cSTejun Heo (p_blkg)->q, false))) 495eea8f41cSTejun Heo 496eea8f41cSTejun Heo /** 497eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 498eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 499eea8f41cSTejun Heo * @pos_css: used for iteration 500eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 501eea8f41cSTejun Heo * 502eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 503eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 504eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 505eea8f41cSTejun Heo */ 506eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 507eea8f41cSTejun Heo css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ 508eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 509eea8f41cSTejun Heo (p_blkg)->q, false))) 510eea8f41cSTejun Heo 51124bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp) 512eea8f41cSTejun Heo { 51324bdb8efSTejun Heo int ret; 51424bdb8efSTejun Heo 51524bdb8efSTejun Heo ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp); 51624bdb8efSTejun Heo if (ret) 51724bdb8efSTejun Heo return ret; 51824bdb8efSTejun Heo 519e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 52024bdb8efSTejun Heo return 0; 52124bdb8efSTejun Heo } 52224bdb8efSTejun Heo 52324bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat) 52424bdb8efSTejun Heo { 52524bdb8efSTejun Heo percpu_counter_destroy(&stat->cpu_cnt); 526eea8f41cSTejun Heo } 527eea8f41cSTejun Heo 528eea8f41cSTejun Heo /** 529eea8f41cSTejun Heo * blkg_stat_add - add a value to a blkg_stat 530eea8f41cSTejun Heo * @stat: target blkg_stat 531eea8f41cSTejun Heo * @val: value to add 532eea8f41cSTejun Heo * 53324bdb8efSTejun Heo * Add @val to @stat. The caller must ensure that IRQ on the same CPU 53424bdb8efSTejun Heo * don't re-enter this function for the same counter. 535eea8f41cSTejun Heo */ 536eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val) 537eea8f41cSTejun Heo { 538104b4e51SNikolay Borisov percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH); 539eea8f41cSTejun Heo } 540eea8f41cSTejun Heo 541eea8f41cSTejun Heo /** 542eea8f41cSTejun Heo * blkg_stat_read - read the current value of a blkg_stat 543eea8f41cSTejun Heo * @stat: blkg_stat to read 544eea8f41cSTejun Heo */ 545eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat) 546eea8f41cSTejun Heo { 54724bdb8efSTejun Heo return percpu_counter_sum_positive(&stat->cpu_cnt); 548eea8f41cSTejun Heo } 549eea8f41cSTejun Heo 550eea8f41cSTejun Heo /** 551eea8f41cSTejun Heo * blkg_stat_reset - reset a blkg_stat 552eea8f41cSTejun Heo * @stat: blkg_stat to reset 553eea8f41cSTejun Heo */ 554eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat) 555eea8f41cSTejun Heo { 55624bdb8efSTejun Heo percpu_counter_set(&stat->cpu_cnt, 0); 557e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 558eea8f41cSTejun Heo } 559eea8f41cSTejun Heo 560eea8f41cSTejun Heo /** 561e6269c44STejun Heo * blkg_stat_add_aux - add a blkg_stat into another's aux count 562eea8f41cSTejun Heo * @to: the destination blkg_stat 563eea8f41cSTejun Heo * @from: the source 564eea8f41cSTejun Heo * 565e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 566eea8f41cSTejun Heo */ 567e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to, 568e6269c44STejun Heo struct blkg_stat *from) 569eea8f41cSTejun Heo { 570e6269c44STejun Heo atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt), 571e6269c44STejun Heo &to->aux_cnt); 572eea8f41cSTejun Heo } 573eea8f41cSTejun Heo 57424bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp) 57524bdb8efSTejun Heo { 57624bdb8efSTejun Heo int i, ret; 57724bdb8efSTejun Heo 57824bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 57924bdb8efSTejun Heo ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp); 58024bdb8efSTejun Heo if (ret) { 58124bdb8efSTejun Heo while (--i >= 0) 58224bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 58324bdb8efSTejun Heo return ret; 58424bdb8efSTejun Heo } 58524bdb8efSTejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 58624bdb8efSTejun Heo } 58724bdb8efSTejun Heo return 0; 58824bdb8efSTejun Heo } 58924bdb8efSTejun Heo 59024bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat) 591eea8f41cSTejun Heo { 592e6269c44STejun Heo int i; 593e6269c44STejun Heo 594e6269c44STejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 59524bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 596eea8f41cSTejun Heo } 597eea8f41cSTejun Heo 598eea8f41cSTejun Heo /** 599eea8f41cSTejun Heo * blkg_rwstat_add - add a value to a blkg_rwstat 600eea8f41cSTejun Heo * @rwstat: target blkg_rwstat 601ef295ecfSChristoph Hellwig * @op: REQ_OP and flags 602eea8f41cSTejun Heo * @val: value to add 603eea8f41cSTejun Heo * 604eea8f41cSTejun Heo * Add @val to @rwstat. The counters are chosen according to @rw. The 605eea8f41cSTejun Heo * caller is responsible for synchronizing calls to this function. 606eea8f41cSTejun Heo */ 607eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, 608ef295ecfSChristoph Hellwig unsigned int op, uint64_t val) 609eea8f41cSTejun Heo { 61024bdb8efSTejun Heo struct percpu_counter *cnt; 611eea8f41cSTejun Heo 612636620b6STejun Heo if (op_is_discard(op)) 613636620b6STejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD]; 614636620b6STejun Heo else if (op_is_write(op)) 61524bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE]; 616eea8f41cSTejun Heo else 61724bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ]; 618eea8f41cSTejun Heo 619104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 62024bdb8efSTejun Heo 621d71d9ae1SChristoph Hellwig if (op_is_sync(op)) 62224bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC]; 62324bdb8efSTejun Heo else 62424bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC]; 62524bdb8efSTejun Heo 626104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 627eea8f41cSTejun Heo } 628eea8f41cSTejun Heo 629eea8f41cSTejun Heo /** 630eea8f41cSTejun Heo * blkg_rwstat_read - read the current values of a blkg_rwstat 631eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 632eea8f41cSTejun Heo * 63324bdb8efSTejun Heo * Read the current snapshot of @rwstat and return it in the aux counts. 634eea8f41cSTejun Heo */ 635eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat) 636eea8f41cSTejun Heo { 63724bdb8efSTejun Heo struct blkg_rwstat result; 63824bdb8efSTejun Heo int i; 639eea8f41cSTejun Heo 64024bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 64124bdb8efSTejun Heo atomic64_set(&result.aux_cnt[i], 64224bdb8efSTejun Heo percpu_counter_sum_positive(&rwstat->cpu_cnt[i])); 64324bdb8efSTejun Heo return result; 644eea8f41cSTejun Heo } 645eea8f41cSTejun Heo 646eea8f41cSTejun Heo /** 647eea8f41cSTejun Heo * blkg_rwstat_total - read the total count of a blkg_rwstat 648eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 649eea8f41cSTejun Heo * 650eea8f41cSTejun Heo * Return the total count of @rwstat regardless of the IO direction. This 651eea8f41cSTejun Heo * function can be called without synchronization and takes care of u64 652eea8f41cSTejun Heo * atomicity. 653eea8f41cSTejun Heo */ 654eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat) 655eea8f41cSTejun Heo { 656eea8f41cSTejun Heo struct blkg_rwstat tmp = blkg_rwstat_read(rwstat); 657eea8f41cSTejun Heo 65824bdb8efSTejun Heo return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + 65924bdb8efSTejun Heo atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); 660eea8f41cSTejun Heo } 661eea8f41cSTejun Heo 662eea8f41cSTejun Heo /** 663eea8f41cSTejun Heo * blkg_rwstat_reset - reset a blkg_rwstat 664eea8f41cSTejun Heo * @rwstat: blkg_rwstat to reset 665eea8f41cSTejun Heo */ 666eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) 667eea8f41cSTejun Heo { 668e6269c44STejun Heo int i; 669e6269c44STejun Heo 67024bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 67124bdb8efSTejun Heo percpu_counter_set(&rwstat->cpu_cnt[i], 0); 672e6269c44STejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 673eea8f41cSTejun Heo } 67424bdb8efSTejun Heo } 675eea8f41cSTejun Heo 676eea8f41cSTejun Heo /** 677e6269c44STejun Heo * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count 678eea8f41cSTejun Heo * @to: the destination blkg_rwstat 679eea8f41cSTejun Heo * @from: the source 680eea8f41cSTejun Heo * 681e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 682eea8f41cSTejun Heo */ 683e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to, 684eea8f41cSTejun Heo struct blkg_rwstat *from) 685eea8f41cSTejun Heo { 686ddc21231SArnd Bergmann u64 sum[BLKG_RWSTAT_NR]; 687eea8f41cSTejun Heo int i; 688eea8f41cSTejun Heo 689eea8f41cSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 690ddc21231SArnd Bergmann sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]); 691ddc21231SArnd Bergmann 692ddc21231SArnd Bergmann for (i = 0; i < BLKG_RWSTAT_NR; i++) 693ddc21231SArnd Bergmann atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]), 694e6269c44STejun Heo &to->aux_cnt[i]); 695eea8f41cSTejun Heo } 696eea8f41cSTejun Heo 697ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING 698ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 699ae118896STejun Heo struct bio *bio); 700ae118896STejun Heo #else 701ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 702ae118896STejun Heo struct bio *bio) { return false; } 703ae118896STejun Heo #endif 704ae118896STejun Heo 705ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 706ae118896STejun Heo struct bio *bio) 707ae118896STejun Heo { 708b5f2954dSDennis Zhou struct blkcg *blkcg; 709ae118896STejun Heo struct blkcg_gq *blkg; 710ae118896STejun Heo bool throtl = false; 711ae118896STejun Heo 712ae118896STejun Heo rcu_read_lock(); 713b5f2954dSDennis Zhou blkcg = bio_blkcg(bio); 714ae118896STejun Heo 715b5f2954dSDennis Zhou /* associate blkcg if bio hasn't attached one */ 716b5f2954dSDennis Zhou bio_associate_blkcg(bio, &blkcg->css); 717b5f2954dSDennis Zhou 718b5f2954dSDennis Zhou blkg = blkg_lookup(blkcg, q); 719b5f2954dSDennis Zhou if (unlikely(!blkg)) { 720*0d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock); 721b5f2954dSDennis Zhou blkg = blkg_lookup_create(blkcg, q); 722b5f2954dSDennis Zhou if (IS_ERR(blkg)) 723b5f2954dSDennis Zhou blkg = NULL; 724*0d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock); 725b5f2954dSDennis Zhou } 726ae118896STejun Heo 727ae118896STejun Heo throtl = blk_throtl_bio(q, blkg, bio); 728ae118896STejun Heo 72977ea7338STejun Heo if (!throtl) { 730b5f2954dSDennis Zhou blkg = blkg ?: q->root_blkg; 731c454edc2SJosef Bacik /* 732c454edc2SJosef Bacik * If the bio is flagged with BIO_QUEUE_ENTERED it means this 733c454edc2SJosef Bacik * is a split bio and we would have already accounted for the 734c454edc2SJosef Bacik * size of the bio. 735c454edc2SJosef Bacik */ 736c454edc2SJosef Bacik if (!bio_flagged(bio, BIO_QUEUE_ENTERED)) 737ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf, 73877ea7338STejun Heo bio->bi_iter.bi_size); 739ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1); 74077ea7338STejun Heo } 74177ea7338STejun Heo 742ae118896STejun Heo rcu_read_unlock(); 743ae118896STejun Heo return !throtl; 744ae118896STejun Heo } 745ae118896STejun Heo 746d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 747d09d8df3SJosef Bacik { 748d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 749d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 750d09d8df3SJosef Bacik } 751d09d8df3SJosef Bacik 752d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 753d09d8df3SJosef Bacik { 754d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 755d09d8df3SJosef Bacik 756d09d8df3SJosef Bacik if (old == 0) 757d09d8df3SJosef Bacik return 0; 758d09d8df3SJosef Bacik 759d09d8df3SJosef Bacik /* 760d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 761d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 762d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 763d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 764d09d8df3SJosef Bacik * congestion count on the cgroup. 765d09d8df3SJosef Bacik */ 766d09d8df3SJosef Bacik while (old) { 767d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 768d09d8df3SJosef Bacik if (cur == old) 769d09d8df3SJosef Bacik break; 770d09d8df3SJosef Bacik old = cur; 771d09d8df3SJosef Bacik } 772d09d8df3SJosef Bacik 773d09d8df3SJosef Bacik if (old == 0) 774d09d8df3SJosef Bacik return 0; 775d09d8df3SJosef Bacik if (old == 1) 776d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 777d09d8df3SJosef Bacik return 1; 778d09d8df3SJosef Bacik } 779d09d8df3SJosef Bacik 780d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 781d09d8df3SJosef Bacik { 782d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 783d09d8df3SJosef Bacik if (!old) 784d09d8df3SJosef Bacik return; 785d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 786d09d8df3SJosef Bacik while (old) { 787d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); 788d09d8df3SJosef Bacik if (cur == old) { 789d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 790d09d8df3SJosef Bacik break; 791d09d8df3SJosef Bacik } 792d09d8df3SJosef Bacik old = cur; 793d09d8df3SJosef Bacik } 794d09d8df3SJosef Bacik } 795d09d8df3SJosef Bacik 796d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 797d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 798d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 799eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 800eea8f41cSTejun Heo 801efa7d1c7STejun Heo struct blkcg { 802efa7d1c7STejun Heo }; 803eea8f41cSTejun Heo 804eea8f41cSTejun Heo struct blkg_policy_data { 805eea8f41cSTejun Heo }; 806eea8f41cSTejun Heo 807e4bc13adSLinus Torvalds struct blkcg_policy_data { 808e4bc13adSLinus Torvalds }; 809e4bc13adSLinus Torvalds 810eea8f41cSTejun Heo struct blkcg_gq { 811eea8f41cSTejun Heo }; 812eea8f41cSTejun Heo 813eea8f41cSTejun Heo struct blkcg_policy { 814eea8f41cSTejun Heo }; 815eea8f41cSTejun Heo 816496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 817496d5e75STejun Heo 818d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 819d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 820d09d8df3SJosef Bacik 821efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 822efa7d1c7STejun Heo 823d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 824d09d8df3SJosef Bacik 825eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 826b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 827b86d865cSBart Van Assche { return NULL; } 828eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 829eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { } 830eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 831eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 832eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 833eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 834eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 835eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 836eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 837eea8f41cSTejun Heo 838eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 839eea8f41cSTejun Heo 840eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 841eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 842eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 843eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 844eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 845eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 846eea8f41cSTejun Heo 847ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 848ae118896STejun Heo struct bio *bio) { return true; } 849ae118896STejun Heo 850eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 851eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 852eea8f41cSTejun Heo 853efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 854eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 855eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 856