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, 38eea8f41cSTejun Heo 39eea8f41cSTejun Heo BLKG_RWSTAT_NR, 40eea8f41cSTejun Heo BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR, 41eea8f41cSTejun Heo }; 42eea8f41cSTejun Heo 43eea8f41cSTejun Heo struct blkcg_gq; 44eea8f41cSTejun Heo 45eea8f41cSTejun Heo struct blkcg { 46eea8f41cSTejun Heo struct cgroup_subsys_state css; 47eea8f41cSTejun Heo spinlock_t lock; 48eea8f41cSTejun Heo 49eea8f41cSTejun Heo struct radix_tree_root blkg_tree; 5055679c8dSBart Van Assche struct blkcg_gq __rcu *blkg_hint; 51eea8f41cSTejun Heo struct hlist_head blkg_list; 52eea8f41cSTejun Heo 5381437648STejun Heo struct blkcg_policy_data *cpd[BLKCG_MAX_POLS]; 5452ebea74STejun Heo 557876f930STejun Heo struct list_head all_blkcgs_node; 5652ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK 5752ebea74STejun Heo struct list_head cgwb_list; 5852ebea74STejun Heo #endif 59eea8f41cSTejun Heo }; 60eea8f41cSTejun Heo 61e6269c44STejun Heo /* 62e6269c44STejun Heo * blkg_[rw]stat->aux_cnt is excluded for local stats but included for 6324bdb8efSTejun Heo * recursive. Used to carry stats of dead children, and, for blkg_rwstat, 6424bdb8efSTejun Heo * to carry result values from read and sum operations. 65e6269c44STejun Heo */ 66eea8f41cSTejun Heo struct blkg_stat { 6724bdb8efSTejun Heo struct percpu_counter cpu_cnt; 68e6269c44STejun Heo atomic64_t aux_cnt; 69eea8f41cSTejun Heo }; 70eea8f41cSTejun Heo 71eea8f41cSTejun Heo struct blkg_rwstat { 7224bdb8efSTejun Heo struct percpu_counter cpu_cnt[BLKG_RWSTAT_NR]; 73e6269c44STejun Heo atomic64_t aux_cnt[BLKG_RWSTAT_NR]; 74eea8f41cSTejun Heo }; 75eea8f41cSTejun Heo 76eea8f41cSTejun Heo /* 77eea8f41cSTejun Heo * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a 78eea8f41cSTejun Heo * request_queue (q). This is used by blkcg policies which need to track 79eea8f41cSTejun Heo * information per blkcg - q pair. 80eea8f41cSTejun Heo * 81001bea73STejun Heo * There can be multiple active blkcg policies and each blkg:policy pair is 82001bea73STejun Heo * represented by a blkg_policy_data which is allocated and freed by each 83001bea73STejun Heo * policy's pd_alloc/free_fn() methods. A policy can allocate private data 84001bea73STejun Heo * area by allocating larger data structure which embeds blkg_policy_data 85001bea73STejun Heo * at the beginning. 86eea8f41cSTejun Heo */ 87eea8f41cSTejun Heo struct blkg_policy_data { 88eea8f41cSTejun Heo /* the blkg and policy id this per-policy data belongs to */ 89eea8f41cSTejun Heo struct blkcg_gq *blkg; 90eea8f41cSTejun Heo int plid; 914c699480SJoseph Qi bool offline; 92eea8f41cSTejun Heo }; 93eea8f41cSTejun Heo 94e4bc13adSLinus Torvalds /* 95e4a9bde9STejun Heo * Policies that need to keep per-blkcg data which is independent from any 96e4a9bde9STejun Heo * request_queue associated to it should implement cpd_alloc/free_fn() 97e4a9bde9STejun Heo * methods. A policy can allocate private data area by allocating larger 98e4a9bde9STejun Heo * data structure which embeds blkcg_policy_data at the beginning. 99e4a9bde9STejun Heo * cpd_init() is invoked to let each policy handle per-blkcg data. 100e4bc13adSLinus Torvalds */ 101e4bc13adSLinus Torvalds struct blkcg_policy_data { 10281437648STejun Heo /* the blkcg and policy id this per-policy data belongs to */ 10381437648STejun Heo struct blkcg *blkcg; 104e4bc13adSLinus Torvalds int plid; 105e4bc13adSLinus Torvalds }; 106e4bc13adSLinus Torvalds 107eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */ 108eea8f41cSTejun Heo struct blkcg_gq { 109eea8f41cSTejun Heo /* Pointer to the associated request_queue */ 110eea8f41cSTejun Heo struct request_queue *q; 111eea8f41cSTejun Heo struct list_head q_node; 112eea8f41cSTejun Heo struct hlist_node blkcg_node; 113eea8f41cSTejun Heo struct blkcg *blkcg; 114eea8f41cSTejun Heo 115ce7acfeaSTejun Heo /* 116ce7acfeaSTejun Heo * Each blkg gets congested separately and the congestion state is 117ce7acfeaSTejun Heo * propagated to the matching bdi_writeback_congested. 118ce7acfeaSTejun Heo */ 119ce7acfeaSTejun Heo struct bdi_writeback_congested *wb_congested; 120ce7acfeaSTejun Heo 121eea8f41cSTejun Heo /* all non-root blkcg_gq's are guaranteed to have access to parent */ 122eea8f41cSTejun Heo struct blkcg_gq *parent; 123eea8f41cSTejun Heo 124eea8f41cSTejun Heo /* request allocation list for this blkcg-q pair */ 125eea8f41cSTejun Heo struct request_list rl; 126eea8f41cSTejun Heo 127eea8f41cSTejun Heo /* reference count */ 128eea8f41cSTejun Heo atomic_t refcnt; 129eea8f41cSTejun Heo 130eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 131eea8f41cSTejun Heo bool online; 132eea8f41cSTejun Heo 13377ea7338STejun Heo struct blkg_rwstat stat_bytes; 13477ea7338STejun Heo struct blkg_rwstat stat_ios; 13577ea7338STejun Heo 136eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 137eea8f41cSTejun Heo 138eea8f41cSTejun Heo struct rcu_head rcu_head; 139*d09d8df3SJosef Bacik 140*d09d8df3SJosef Bacik atomic_t use_delay; 141*d09d8df3SJosef Bacik atomic64_t delay_nsec; 142*d09d8df3SJosef Bacik atomic64_t delay_start; 143*d09d8df3SJosef Bacik u64 last_delay; 144*d09d8df3SJosef Bacik int last_use; 145eea8f41cSTejun Heo }; 146eea8f41cSTejun Heo 147e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 14881437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 149e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 15069d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 151001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node); 152a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 153a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 154a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 155001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 156a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 157903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 158903d23f0SJosef Bacik size_t size); 159eea8f41cSTejun Heo 160eea8f41cSTejun Heo struct blkcg_policy { 161eea8f41cSTejun Heo int plid; 162eea8f41cSTejun Heo /* cgroup files for the policy */ 1632ee867dcSTejun Heo struct cftype *dfl_cftypes; 164880f50e2STejun Heo struct cftype *legacy_cftypes; 165eea8f41cSTejun Heo 166eea8f41cSTejun Heo /* operations */ 167e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 168e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 169e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 17069d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 171e4a9bde9STejun Heo 172001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 173eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 174eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 175eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 176001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 177eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 178903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 179eea8f41cSTejun Heo }; 180eea8f41cSTejun Heo 181eea8f41cSTejun Heo extern struct blkcg blkcg_root; 182496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 183eea8f41cSTejun Heo 18424f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 18524f29046STejun Heo struct request_queue *q, bool update_hint); 186eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, 187eea8f41cSTejun Heo struct request_queue *q); 188eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q); 189eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q); 190eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q); 191eea8f41cSTejun Heo 192eea8f41cSTejun Heo /* Blkio controller policy registration */ 193eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol); 194eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol); 195eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q, 196eea8f41cSTejun Heo const struct blkcg_policy *pol); 197eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q, 198eea8f41cSTejun Heo const struct blkcg_policy *pol); 199eea8f41cSTejun Heo 200dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg); 201eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, 202eea8f41cSTejun Heo u64 (*prfill)(struct seq_file *, 203eea8f41cSTejun Heo struct blkg_policy_data *, int), 204eea8f41cSTejun Heo const struct blkcg_policy *pol, int data, 205eea8f41cSTejun Heo bool show_total); 206eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); 207eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 208eea8f41cSTejun Heo const struct blkg_rwstat *rwstat); 209eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off); 210eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, 211eea8f41cSTejun Heo int off); 21277ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v); 21377ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v); 21477ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v); 21577ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v); 216eea8f41cSTejun Heo 217f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg, 218f12c74caSTejun Heo struct blkcg_policy *pol, int off); 219f12c74caSTejun Heo struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, 220f12c74caSTejun Heo struct blkcg_policy *pol, int off); 221eea8f41cSTejun Heo 222eea8f41cSTejun Heo struct blkg_conf_ctx { 223eea8f41cSTejun Heo struct gendisk *disk; 224eea8f41cSTejun Heo struct blkcg_gq *blkg; 22536aa9e5fSTejun Heo char *body; 226eea8f41cSTejun Heo }; 227eea8f41cSTejun Heo 228eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, 22936aa9e5fSTejun Heo char *input, struct blkg_conf_ctx *ctx); 230eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx); 231eea8f41cSTejun Heo 232eea8f41cSTejun Heo 233eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 234eea8f41cSTejun Heo { 235eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 236eea8f41cSTejun Heo } 237eea8f41cSTejun Heo 238eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) 239eea8f41cSTejun Heo { 240902ec5b6SShaohua Li struct cgroup_subsys_state *css; 241902ec5b6SShaohua Li 242eea8f41cSTejun Heo if (bio && bio->bi_css) 243eea8f41cSTejun Heo return css_to_blkcg(bio->bi_css); 244902ec5b6SShaohua Li css = kthread_blkcg(); 245902ec5b6SShaohua Li if (css) 246902ec5b6SShaohua Li return css_to_blkcg(css); 247902ec5b6SShaohua Li return css_to_blkcg(task_css(current, io_cgrp_id)); 248fd383c2dSTejun Heo } 249fd383c2dSTejun Heo 250*d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 251*d09d8df3SJosef Bacik { 252*d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 253*d09d8df3SJosef Bacik bool ret = false; 254*d09d8df3SJosef Bacik 255*d09d8df3SJosef Bacik rcu_read_lock(); 256*d09d8df3SJosef Bacik css = kthread_blkcg(); 257*d09d8df3SJosef Bacik if (!css) 258*d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 259*d09d8df3SJosef Bacik while (css) { 260*d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 261*d09d8df3SJosef Bacik ret = true; 262*d09d8df3SJosef Bacik break; 263*d09d8df3SJosef Bacik } 264*d09d8df3SJosef Bacik css = css->parent; 265*d09d8df3SJosef Bacik } 266*d09d8df3SJosef Bacik rcu_read_unlock(); 267*d09d8df3SJosef Bacik return ret; 268*d09d8df3SJosef Bacik } 269*d09d8df3SJosef Bacik 270eea8f41cSTejun Heo /** 271c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 272c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 273c7c98fd3SJosef Bacik * 274c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 275c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 276c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 277c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 278c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 279c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 280c7c98fd3SJosef Bacik */ 281c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 282c7c98fd3SJosef Bacik { 2830d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 284c7c98fd3SJosef Bacik } 285c7c98fd3SJosef Bacik 286c7c98fd3SJosef Bacik /** 287eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 288eea8f41cSTejun Heo * @blkcg: blkcg of interest 289eea8f41cSTejun Heo * 290eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 291eea8f41cSTejun Heo */ 292eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 293eea8f41cSTejun Heo { 294eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 295eea8f41cSTejun Heo } 296eea8f41cSTejun Heo 297eea8f41cSTejun Heo /** 29824f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 29924f29046STejun Heo * @blkcg: blkcg of interest 30024f29046STejun Heo * @q: request_queue of interest 30124f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 30224f29046STejun Heo * 30324f29046STejun Heo * This is internal version and shouldn't be used by policy 30424f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 30524f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 30624f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 30724f29046STejun Heo */ 30824f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 30924f29046STejun Heo struct request_queue *q, 31024f29046STejun Heo bool update_hint) 31124f29046STejun Heo { 31224f29046STejun Heo struct blkcg_gq *blkg; 31324f29046STejun Heo 31485b6bc9dSTejun Heo if (blkcg == &blkcg_root) 31585b6bc9dSTejun Heo return q->root_blkg; 31685b6bc9dSTejun Heo 31724f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 31824f29046STejun Heo if (blkg && blkg->q == q) 31924f29046STejun Heo return blkg; 32024f29046STejun Heo 32124f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 32224f29046STejun Heo } 32324f29046STejun Heo 32424f29046STejun Heo /** 32524f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 32624f29046STejun Heo * @blkcg: blkcg of interest 32724f29046STejun Heo * @q: request_queue of interest 32824f29046STejun Heo * 32924f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 33024f29046STejun Heo * under RCU read lock and is guaranteed to return %NULL if @q is bypassing 33124f29046STejun Heo * - see blk_queue_bypass_start() for details. 33224f29046STejun Heo */ 33324f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 33424f29046STejun Heo struct request_queue *q) 33524f29046STejun Heo { 33624f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 33724f29046STejun Heo 33824f29046STejun Heo if (unlikely(blk_queue_bypass(q))) 33924f29046STejun Heo return NULL; 34024f29046STejun Heo return __blkg_lookup(blkcg, q, false); 34124f29046STejun Heo } 34224f29046STejun Heo 34324f29046STejun Heo /** 344eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 345eea8f41cSTejun Heo * @blkg: blkg of interest 346eea8f41cSTejun Heo * @pol: policy of interest 347eea8f41cSTejun Heo * 348eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 349eea8f41cSTejun Heo */ 350eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 351eea8f41cSTejun Heo struct blkcg_policy *pol) 352eea8f41cSTejun Heo { 353eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 354eea8f41cSTejun Heo } 355eea8f41cSTejun Heo 356e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 357e4bc13adSLinus Torvalds struct blkcg_policy *pol) 358e4bc13adSLinus Torvalds { 35981437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 360e4bc13adSLinus Torvalds } 361e4bc13adSLinus Torvalds 362eea8f41cSTejun Heo /** 363eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 364eea8f41cSTejun Heo * @pd: policy private data of interest 365eea8f41cSTejun Heo * 366eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 367eea8f41cSTejun Heo */ 368eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 369eea8f41cSTejun Heo { 370eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 371eea8f41cSTejun Heo } 372eea8f41cSTejun Heo 37381437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 37481437648STejun Heo { 37581437648STejun Heo return cpd ? cpd->blkcg : NULL; 37681437648STejun Heo } 37781437648STejun Heo 378eea8f41cSTejun Heo /** 379eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 380eea8f41cSTejun Heo * @blkg: blkg of interest 381eea8f41cSTejun Heo * @buf: target buffer 382eea8f41cSTejun Heo * @buflen: target buffer length 383eea8f41cSTejun Heo * 384eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 385eea8f41cSTejun Heo */ 386eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 387eea8f41cSTejun Heo { 3884c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 389eea8f41cSTejun Heo } 390eea8f41cSTejun Heo 391eea8f41cSTejun Heo /** 392eea8f41cSTejun Heo * blkg_get - get a blkg reference 393eea8f41cSTejun Heo * @blkg: blkg to get 394eea8f41cSTejun Heo * 395eea8f41cSTejun Heo * The caller should be holding an existing reference. 396eea8f41cSTejun Heo */ 397eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 398eea8f41cSTejun Heo { 399eea8f41cSTejun Heo WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0); 400eea8f41cSTejun Heo atomic_inc(&blkg->refcnt); 401eea8f41cSTejun Heo } 402eea8f41cSTejun Heo 403*d09d8df3SJosef Bacik /** 404*d09d8df3SJosef Bacik * blkg_try_get - try and get a blkg reference 405*d09d8df3SJosef Bacik * @blkg: blkg to get 406*d09d8df3SJosef Bacik * 407*d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 408*d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 409*d09d8df3SJosef Bacik */ 410*d09d8df3SJosef Bacik static inline struct blkcg_gq *blkg_try_get(struct blkcg_gq *blkg) 411*d09d8df3SJosef Bacik { 412*d09d8df3SJosef Bacik if (atomic_inc_not_zero(&blkg->refcnt)) 413*d09d8df3SJosef Bacik return blkg; 414*d09d8df3SJosef Bacik return NULL; 415*d09d8df3SJosef Bacik } 416*d09d8df3SJosef Bacik 417*d09d8df3SJosef Bacik 418eea8f41cSTejun Heo void __blkg_release_rcu(struct rcu_head *rcu); 419eea8f41cSTejun Heo 420eea8f41cSTejun Heo /** 421eea8f41cSTejun Heo * blkg_put - put a blkg reference 422eea8f41cSTejun Heo * @blkg: blkg to put 423eea8f41cSTejun Heo */ 424eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 425eea8f41cSTejun Heo { 426eea8f41cSTejun Heo WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0); 427eea8f41cSTejun Heo if (atomic_dec_and_test(&blkg->refcnt)) 428eea8f41cSTejun Heo call_rcu(&blkg->rcu_head, __blkg_release_rcu); 429eea8f41cSTejun Heo } 430eea8f41cSTejun Heo 431eea8f41cSTejun Heo /** 432eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 433eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 434eea8f41cSTejun Heo * @pos_css: used for iteration 435eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 436eea8f41cSTejun Heo * 437eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 438eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 439eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 440eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 441eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 442eea8f41cSTejun Heo */ 443eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 444eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 445eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 446eea8f41cSTejun Heo (p_blkg)->q, false))) 447eea8f41cSTejun Heo 448eea8f41cSTejun Heo /** 449eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 450eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 451eea8f41cSTejun Heo * @pos_css: used for iteration 452eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 453eea8f41cSTejun Heo * 454eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 455eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 456eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 457eea8f41cSTejun Heo */ 458eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 459eea8f41cSTejun Heo css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ 460eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 461eea8f41cSTejun Heo (p_blkg)->q, false))) 462eea8f41cSTejun Heo 463eea8f41cSTejun Heo /** 464eea8f41cSTejun Heo * blk_get_rl - get request_list to use 465eea8f41cSTejun Heo * @q: request_queue of interest 466eea8f41cSTejun Heo * @bio: bio which will be attached to the allocated request (may be %NULL) 467eea8f41cSTejun Heo * 468eea8f41cSTejun Heo * The caller wants to allocate a request from @q to use for @bio. Find 469eea8f41cSTejun Heo * the request_list to use and obtain a reference on it. Should be called 470eea8f41cSTejun Heo * under queue_lock. This function is guaranteed to return non-%NULL 471eea8f41cSTejun Heo * request_list. 472eea8f41cSTejun Heo */ 473eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q, 474eea8f41cSTejun Heo struct bio *bio) 475eea8f41cSTejun Heo { 476eea8f41cSTejun Heo struct blkcg *blkcg; 477eea8f41cSTejun Heo struct blkcg_gq *blkg; 478eea8f41cSTejun Heo 479eea8f41cSTejun Heo rcu_read_lock(); 480eea8f41cSTejun Heo 481eea8f41cSTejun Heo blkcg = bio_blkcg(bio); 482eea8f41cSTejun Heo 483eea8f41cSTejun Heo /* bypass blkg lookup and use @q->root_rl directly for root */ 484eea8f41cSTejun Heo if (blkcg == &blkcg_root) 485eea8f41cSTejun Heo goto root_rl; 486eea8f41cSTejun Heo 487eea8f41cSTejun Heo /* 488eea8f41cSTejun Heo * Try to use blkg->rl. blkg lookup may fail under memory pressure 489eea8f41cSTejun Heo * or if either the blkcg or queue is going away. Fall back to 490eea8f41cSTejun Heo * root_rl in such cases. 491eea8f41cSTejun Heo */ 492ae118896STejun Heo blkg = blkg_lookup(blkcg, q); 493ae118896STejun Heo if (unlikely(!blkg)) 494eea8f41cSTejun Heo goto root_rl; 495eea8f41cSTejun Heo 496eea8f41cSTejun Heo blkg_get(blkg); 497eea8f41cSTejun Heo rcu_read_unlock(); 498eea8f41cSTejun Heo return &blkg->rl; 499eea8f41cSTejun Heo root_rl: 500eea8f41cSTejun Heo rcu_read_unlock(); 501eea8f41cSTejun Heo return &q->root_rl; 502eea8f41cSTejun Heo } 503eea8f41cSTejun Heo 504eea8f41cSTejun Heo /** 505eea8f41cSTejun Heo * blk_put_rl - put request_list 506eea8f41cSTejun Heo * @rl: request_list to put 507eea8f41cSTejun Heo * 508eea8f41cSTejun Heo * Put the reference acquired by blk_get_rl(). Should be called under 509eea8f41cSTejun Heo * queue_lock. 510eea8f41cSTejun Heo */ 511eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl) 512eea8f41cSTejun Heo { 513401efbf8STejun Heo if (rl->blkg->blkcg != &blkcg_root) 514eea8f41cSTejun Heo blkg_put(rl->blkg); 515eea8f41cSTejun Heo } 516eea8f41cSTejun Heo 517eea8f41cSTejun Heo /** 518eea8f41cSTejun Heo * blk_rq_set_rl - associate a request with a request_list 519eea8f41cSTejun Heo * @rq: request of interest 520eea8f41cSTejun Heo * @rl: target request_list 521eea8f41cSTejun Heo * 522eea8f41cSTejun Heo * Associate @rq with @rl so that accounting and freeing can know the 523eea8f41cSTejun Heo * request_list @rq came from. 524eea8f41cSTejun Heo */ 525eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) 526eea8f41cSTejun Heo { 527eea8f41cSTejun Heo rq->rl = rl; 528eea8f41cSTejun Heo } 529eea8f41cSTejun Heo 530eea8f41cSTejun Heo /** 531eea8f41cSTejun Heo * blk_rq_rl - return the request_list a request came from 532eea8f41cSTejun Heo * @rq: request of interest 533eea8f41cSTejun Heo * 534eea8f41cSTejun Heo * Return the request_list @rq is allocated from. 535eea8f41cSTejun Heo */ 536eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq) 537eea8f41cSTejun Heo { 538eea8f41cSTejun Heo return rq->rl; 539eea8f41cSTejun Heo } 540eea8f41cSTejun Heo 541eea8f41cSTejun Heo struct request_list *__blk_queue_next_rl(struct request_list *rl, 542eea8f41cSTejun Heo struct request_queue *q); 543eea8f41cSTejun Heo /** 544eea8f41cSTejun Heo * blk_queue_for_each_rl - iterate through all request_lists of a request_queue 545eea8f41cSTejun Heo * 546eea8f41cSTejun Heo * Should be used under queue_lock. 547eea8f41cSTejun Heo */ 548eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 549eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q))) 550eea8f41cSTejun Heo 55124bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp) 552eea8f41cSTejun Heo { 55324bdb8efSTejun Heo int ret; 55424bdb8efSTejun Heo 55524bdb8efSTejun Heo ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp); 55624bdb8efSTejun Heo if (ret) 55724bdb8efSTejun Heo return ret; 55824bdb8efSTejun Heo 559e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 56024bdb8efSTejun Heo return 0; 56124bdb8efSTejun Heo } 56224bdb8efSTejun Heo 56324bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat) 56424bdb8efSTejun Heo { 56524bdb8efSTejun Heo percpu_counter_destroy(&stat->cpu_cnt); 566eea8f41cSTejun Heo } 567eea8f41cSTejun Heo 568eea8f41cSTejun Heo /** 569eea8f41cSTejun Heo * blkg_stat_add - add a value to a blkg_stat 570eea8f41cSTejun Heo * @stat: target blkg_stat 571eea8f41cSTejun Heo * @val: value to add 572eea8f41cSTejun Heo * 57324bdb8efSTejun Heo * Add @val to @stat. The caller must ensure that IRQ on the same CPU 57424bdb8efSTejun Heo * don't re-enter this function for the same counter. 575eea8f41cSTejun Heo */ 576eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val) 577eea8f41cSTejun Heo { 578104b4e51SNikolay Borisov percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH); 579eea8f41cSTejun Heo } 580eea8f41cSTejun Heo 581eea8f41cSTejun Heo /** 582eea8f41cSTejun Heo * blkg_stat_read - read the current value of a blkg_stat 583eea8f41cSTejun Heo * @stat: blkg_stat to read 584eea8f41cSTejun Heo */ 585eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat) 586eea8f41cSTejun Heo { 58724bdb8efSTejun Heo return percpu_counter_sum_positive(&stat->cpu_cnt); 588eea8f41cSTejun Heo } 589eea8f41cSTejun Heo 590eea8f41cSTejun Heo /** 591eea8f41cSTejun Heo * blkg_stat_reset - reset a blkg_stat 592eea8f41cSTejun Heo * @stat: blkg_stat to reset 593eea8f41cSTejun Heo */ 594eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat) 595eea8f41cSTejun Heo { 59624bdb8efSTejun Heo percpu_counter_set(&stat->cpu_cnt, 0); 597e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 598eea8f41cSTejun Heo } 599eea8f41cSTejun Heo 600eea8f41cSTejun Heo /** 601e6269c44STejun Heo * blkg_stat_add_aux - add a blkg_stat into another's aux count 602eea8f41cSTejun Heo * @to: the destination blkg_stat 603eea8f41cSTejun Heo * @from: the source 604eea8f41cSTejun Heo * 605e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 606eea8f41cSTejun Heo */ 607e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to, 608e6269c44STejun Heo struct blkg_stat *from) 609eea8f41cSTejun Heo { 610e6269c44STejun Heo atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt), 611e6269c44STejun Heo &to->aux_cnt); 612eea8f41cSTejun Heo } 613eea8f41cSTejun Heo 61424bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp) 61524bdb8efSTejun Heo { 61624bdb8efSTejun Heo int i, ret; 61724bdb8efSTejun Heo 61824bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 61924bdb8efSTejun Heo ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp); 62024bdb8efSTejun Heo if (ret) { 62124bdb8efSTejun Heo while (--i >= 0) 62224bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 62324bdb8efSTejun Heo return ret; 62424bdb8efSTejun Heo } 62524bdb8efSTejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 62624bdb8efSTejun Heo } 62724bdb8efSTejun Heo return 0; 62824bdb8efSTejun Heo } 62924bdb8efSTejun Heo 63024bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat) 631eea8f41cSTejun Heo { 632e6269c44STejun Heo int i; 633e6269c44STejun Heo 634e6269c44STejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 63524bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 636eea8f41cSTejun Heo } 637eea8f41cSTejun Heo 638eea8f41cSTejun Heo /** 639eea8f41cSTejun Heo * blkg_rwstat_add - add a value to a blkg_rwstat 640eea8f41cSTejun Heo * @rwstat: target blkg_rwstat 641ef295ecfSChristoph Hellwig * @op: REQ_OP and flags 642eea8f41cSTejun Heo * @val: value to add 643eea8f41cSTejun Heo * 644eea8f41cSTejun Heo * Add @val to @rwstat. The counters are chosen according to @rw. The 645eea8f41cSTejun Heo * caller is responsible for synchronizing calls to this function. 646eea8f41cSTejun Heo */ 647eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, 648ef295ecfSChristoph Hellwig unsigned int op, uint64_t val) 649eea8f41cSTejun Heo { 65024bdb8efSTejun Heo struct percpu_counter *cnt; 651eea8f41cSTejun Heo 65263a4cc24SMike Christie if (op_is_write(op)) 65324bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE]; 654eea8f41cSTejun Heo else 65524bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ]; 656eea8f41cSTejun Heo 657104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 65824bdb8efSTejun Heo 659d71d9ae1SChristoph Hellwig if (op_is_sync(op)) 66024bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC]; 66124bdb8efSTejun Heo else 66224bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC]; 66324bdb8efSTejun Heo 664104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 665eea8f41cSTejun Heo } 666eea8f41cSTejun Heo 667eea8f41cSTejun Heo /** 668eea8f41cSTejun Heo * blkg_rwstat_read - read the current values of a blkg_rwstat 669eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 670eea8f41cSTejun Heo * 67124bdb8efSTejun Heo * Read the current snapshot of @rwstat and return it in the aux counts. 672eea8f41cSTejun Heo */ 673eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat) 674eea8f41cSTejun Heo { 67524bdb8efSTejun Heo struct blkg_rwstat result; 67624bdb8efSTejun Heo int i; 677eea8f41cSTejun Heo 67824bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 67924bdb8efSTejun Heo atomic64_set(&result.aux_cnt[i], 68024bdb8efSTejun Heo percpu_counter_sum_positive(&rwstat->cpu_cnt[i])); 68124bdb8efSTejun Heo return result; 682eea8f41cSTejun Heo } 683eea8f41cSTejun Heo 684eea8f41cSTejun Heo /** 685eea8f41cSTejun Heo * blkg_rwstat_total - read the total count of a blkg_rwstat 686eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 687eea8f41cSTejun Heo * 688eea8f41cSTejun Heo * Return the total count of @rwstat regardless of the IO direction. This 689eea8f41cSTejun Heo * function can be called without synchronization and takes care of u64 690eea8f41cSTejun Heo * atomicity. 691eea8f41cSTejun Heo */ 692eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat) 693eea8f41cSTejun Heo { 694eea8f41cSTejun Heo struct blkg_rwstat tmp = blkg_rwstat_read(rwstat); 695eea8f41cSTejun Heo 69624bdb8efSTejun Heo return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + 69724bdb8efSTejun Heo atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); 698eea8f41cSTejun Heo } 699eea8f41cSTejun Heo 700eea8f41cSTejun Heo /** 701eea8f41cSTejun Heo * blkg_rwstat_reset - reset a blkg_rwstat 702eea8f41cSTejun Heo * @rwstat: blkg_rwstat to reset 703eea8f41cSTejun Heo */ 704eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) 705eea8f41cSTejun Heo { 706e6269c44STejun Heo int i; 707e6269c44STejun Heo 70824bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 70924bdb8efSTejun Heo percpu_counter_set(&rwstat->cpu_cnt[i], 0); 710e6269c44STejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 711eea8f41cSTejun Heo } 71224bdb8efSTejun Heo } 713eea8f41cSTejun Heo 714eea8f41cSTejun Heo /** 715e6269c44STejun Heo * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count 716eea8f41cSTejun Heo * @to: the destination blkg_rwstat 717eea8f41cSTejun Heo * @from: the source 718eea8f41cSTejun Heo * 719e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 720eea8f41cSTejun Heo */ 721e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to, 722eea8f41cSTejun Heo struct blkg_rwstat *from) 723eea8f41cSTejun Heo { 724ddc21231SArnd Bergmann u64 sum[BLKG_RWSTAT_NR]; 725eea8f41cSTejun Heo int i; 726eea8f41cSTejun Heo 727eea8f41cSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 728ddc21231SArnd Bergmann sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]); 729ddc21231SArnd Bergmann 730ddc21231SArnd Bergmann for (i = 0; i < BLKG_RWSTAT_NR; i++) 731ddc21231SArnd Bergmann atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]), 732e6269c44STejun Heo &to->aux_cnt[i]); 733eea8f41cSTejun Heo } 734eea8f41cSTejun Heo 735ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING 736ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 737ae118896STejun Heo struct bio *bio); 738ae118896STejun Heo #else 739ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 740ae118896STejun Heo struct bio *bio) { return false; } 741ae118896STejun Heo #endif 742ae118896STejun Heo 743ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 744ae118896STejun Heo struct bio *bio) 745ae118896STejun Heo { 746ae118896STejun Heo struct blkcg *blkcg; 747ae118896STejun Heo struct blkcg_gq *blkg; 748ae118896STejun Heo bool throtl = false; 749ae118896STejun Heo 750ae118896STejun Heo rcu_read_lock(); 751ae118896STejun Heo blkcg = bio_blkcg(bio); 752ae118896STejun Heo 753007cc56bSShaohua Li /* associate blkcg if bio hasn't attached one */ 754007cc56bSShaohua Li bio_associate_blkcg(bio, &blkcg->css); 755007cc56bSShaohua Li 756ae118896STejun Heo blkg = blkg_lookup(blkcg, q); 757ae118896STejun Heo if (unlikely(!blkg)) { 758ae118896STejun Heo spin_lock_irq(q->queue_lock); 759ae118896STejun Heo blkg = blkg_lookup_create(blkcg, q); 760ae118896STejun Heo if (IS_ERR(blkg)) 761ae118896STejun Heo blkg = NULL; 762ae118896STejun Heo spin_unlock_irq(q->queue_lock); 763ae118896STejun Heo } 764ae118896STejun Heo 765ae118896STejun Heo throtl = blk_throtl_bio(q, blkg, bio); 766ae118896STejun Heo 76777ea7338STejun Heo if (!throtl) { 76877ea7338STejun Heo blkg = blkg ?: q->root_blkg; 769ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf, 77077ea7338STejun Heo bio->bi_iter.bi_size); 771ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1); 77277ea7338STejun Heo } 77377ea7338STejun Heo 774ae118896STejun Heo rcu_read_unlock(); 775ae118896STejun Heo return !throtl; 776ae118896STejun Heo } 777ae118896STejun Heo 778*d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 779*d09d8df3SJosef Bacik { 780*d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 781*d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 782*d09d8df3SJosef Bacik } 783*d09d8df3SJosef Bacik 784*d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 785*d09d8df3SJosef Bacik { 786*d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 787*d09d8df3SJosef Bacik 788*d09d8df3SJosef Bacik if (old == 0) 789*d09d8df3SJosef Bacik return 0; 790*d09d8df3SJosef Bacik 791*d09d8df3SJosef Bacik /* 792*d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 793*d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 794*d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 795*d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 796*d09d8df3SJosef Bacik * congestion count on the cgroup. 797*d09d8df3SJosef Bacik */ 798*d09d8df3SJosef Bacik while (old) { 799*d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 800*d09d8df3SJosef Bacik if (cur == old) 801*d09d8df3SJosef Bacik break; 802*d09d8df3SJosef Bacik old = cur; 803*d09d8df3SJosef Bacik } 804*d09d8df3SJosef Bacik 805*d09d8df3SJosef Bacik if (old == 0) 806*d09d8df3SJosef Bacik return 0; 807*d09d8df3SJosef Bacik if (old == 1) 808*d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 809*d09d8df3SJosef Bacik return 1; 810*d09d8df3SJosef Bacik } 811*d09d8df3SJosef Bacik 812*d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 813*d09d8df3SJosef Bacik { 814*d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 815*d09d8df3SJosef Bacik if (!old) 816*d09d8df3SJosef Bacik return; 817*d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 818*d09d8df3SJosef Bacik while (old) { 819*d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); 820*d09d8df3SJosef Bacik if (cur == old) { 821*d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 822*d09d8df3SJosef Bacik break; 823*d09d8df3SJosef Bacik } 824*d09d8df3SJosef Bacik old = cur; 825*d09d8df3SJosef Bacik } 826*d09d8df3SJosef Bacik } 827*d09d8df3SJosef Bacik 828*d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 829*d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 830*d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 831eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 832eea8f41cSTejun Heo 833efa7d1c7STejun Heo struct blkcg { 834efa7d1c7STejun Heo }; 835eea8f41cSTejun Heo 836eea8f41cSTejun Heo struct blkg_policy_data { 837eea8f41cSTejun Heo }; 838eea8f41cSTejun Heo 839e4bc13adSLinus Torvalds struct blkcg_policy_data { 840e4bc13adSLinus Torvalds }; 841e4bc13adSLinus Torvalds 842eea8f41cSTejun Heo struct blkcg_gq { 843eea8f41cSTejun Heo }; 844eea8f41cSTejun Heo 845eea8f41cSTejun Heo struct blkcg_policy { 846eea8f41cSTejun Heo }; 847eea8f41cSTejun Heo 848496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 849496d5e75STejun Heo 850*d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 851*d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 852*d09d8df3SJosef Bacik 853efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 854efa7d1c7STejun Heo 855*d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 856*d09d8df3SJosef Bacik 857eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 858eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 859eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { } 860eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 861eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 862eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 863eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 864eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 865eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 866eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 867eea8f41cSTejun Heo 868eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 869eea8f41cSTejun Heo 870eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 871eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 872eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 873eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 874eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 875eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 876eea8f41cSTejun Heo 877eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q, 878eea8f41cSTejun Heo struct bio *bio) { return &q->root_rl; } 879eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl) { } 880eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { } 881eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; } 882eea8f41cSTejun Heo 883ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 884ae118896STejun Heo struct bio *bio) { return true; } 885ae118896STejun Heo 886eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 887eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 888eea8f41cSTejun Heo 889efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 890eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 891eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 892