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 /* request allocation list for this blkcg-q pair */ 126eea8f41cSTejun Heo struct request_list rl; 127eea8f41cSTejun Heo 128eea8f41cSTejun Heo /* reference count */ 129eea8f41cSTejun Heo atomic_t refcnt; 130eea8f41cSTejun Heo 131eea8f41cSTejun Heo /* is this blkg online? protected by both blkcg and q locks */ 132eea8f41cSTejun Heo bool online; 133eea8f41cSTejun Heo 13477ea7338STejun Heo struct blkg_rwstat stat_bytes; 13577ea7338STejun Heo struct blkg_rwstat stat_ios; 13677ea7338STejun Heo 137eea8f41cSTejun Heo struct blkg_policy_data *pd[BLKCG_MAX_POLS]; 138eea8f41cSTejun Heo 139eea8f41cSTejun Heo struct rcu_head rcu_head; 140d09d8df3SJosef Bacik 141d09d8df3SJosef Bacik atomic_t use_delay; 142d09d8df3SJosef Bacik atomic64_t delay_nsec; 143d09d8df3SJosef Bacik atomic64_t delay_start; 144d09d8df3SJosef Bacik u64 last_delay; 145d09d8df3SJosef Bacik int last_use; 146eea8f41cSTejun Heo }; 147eea8f41cSTejun Heo 148e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); 14981437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); 150e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); 15169d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); 152001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node); 153a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); 154a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); 155a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); 156001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); 157a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); 158903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, 159903d23f0SJosef Bacik size_t size); 160eea8f41cSTejun Heo 161eea8f41cSTejun Heo struct blkcg_policy { 162eea8f41cSTejun Heo int plid; 163eea8f41cSTejun Heo /* cgroup files for the policy */ 1642ee867dcSTejun Heo struct cftype *dfl_cftypes; 165880f50e2STejun Heo struct cftype *legacy_cftypes; 166eea8f41cSTejun Heo 167eea8f41cSTejun Heo /* operations */ 168e4a9bde9STejun Heo blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; 169e4bc13adSLinus Torvalds blkcg_pol_init_cpd_fn *cpd_init_fn; 170e4a9bde9STejun Heo blkcg_pol_free_cpd_fn *cpd_free_fn; 17169d7fde5STejun Heo blkcg_pol_bind_cpd_fn *cpd_bind_fn; 172e4a9bde9STejun Heo 173001bea73STejun Heo blkcg_pol_alloc_pd_fn *pd_alloc_fn; 174eea8f41cSTejun Heo blkcg_pol_init_pd_fn *pd_init_fn; 175eea8f41cSTejun Heo blkcg_pol_online_pd_fn *pd_online_fn; 176eea8f41cSTejun Heo blkcg_pol_offline_pd_fn *pd_offline_fn; 177001bea73STejun Heo blkcg_pol_free_pd_fn *pd_free_fn; 178eea8f41cSTejun Heo blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; 179903d23f0SJosef Bacik blkcg_pol_stat_pd_fn *pd_stat_fn; 180eea8f41cSTejun Heo }; 181eea8f41cSTejun Heo 182eea8f41cSTejun Heo extern struct blkcg blkcg_root; 183496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css; 184eea8f41cSTejun Heo 18524f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, 18624f29046STejun Heo struct request_queue *q, bool update_hint); 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 233*27e6fa99SDennis Zhou (Facebook) /** 234*27e6fa99SDennis Zhou (Facebook) * blkcg_css - find the current css 235*27e6fa99SDennis Zhou (Facebook) * 236*27e6fa99SDennis Zhou (Facebook) * Find the css associated with either the kthread or the current task. 237*27e6fa99SDennis Zhou (Facebook) * This may return a dying css, so it is up to the caller to use tryget logic 238*27e6fa99SDennis Zhou (Facebook) * to confirm it is alive and well. 239*27e6fa99SDennis Zhou (Facebook) */ 240*27e6fa99SDennis Zhou (Facebook) static inline struct cgroup_subsys_state *blkcg_css(void) 241*27e6fa99SDennis Zhou (Facebook) { 242*27e6fa99SDennis Zhou (Facebook) struct cgroup_subsys_state *css; 243*27e6fa99SDennis Zhou (Facebook) 244*27e6fa99SDennis Zhou (Facebook) css = kthread_blkcg(); 245*27e6fa99SDennis Zhou (Facebook) if (css) 246*27e6fa99SDennis Zhou (Facebook) return css; 247*27e6fa99SDennis Zhou (Facebook) return task_css(current, io_cgrp_id); 248*27e6fa99SDennis Zhou (Facebook) } 249*27e6fa99SDennis Zhou (Facebook) 250*27e6fa99SDennis Zhou (Facebook) /** 251*27e6fa99SDennis Zhou (Facebook) * blkcg_get_css - find and get a reference to the css 252*27e6fa99SDennis Zhou (Facebook) * 253*27e6fa99SDennis Zhou (Facebook) * Find the css associated with either the kthread or the current task. 254*27e6fa99SDennis Zhou (Facebook) * This takes a reference on the blkcg which will need to be managed by the 255*27e6fa99SDennis Zhou (Facebook) * caller. 256*27e6fa99SDennis Zhou (Facebook) */ 257*27e6fa99SDennis Zhou (Facebook) static inline struct cgroup_subsys_state *blkcg_get_css(void) 258*27e6fa99SDennis Zhou (Facebook) { 259*27e6fa99SDennis Zhou (Facebook) struct cgroup_subsys_state *css; 260*27e6fa99SDennis Zhou (Facebook) 261*27e6fa99SDennis Zhou (Facebook) rcu_read_lock(); 262*27e6fa99SDennis Zhou (Facebook) 263*27e6fa99SDennis Zhou (Facebook) css = kthread_blkcg(); 264*27e6fa99SDennis Zhou (Facebook) if (css) { 265*27e6fa99SDennis Zhou (Facebook) css_get(css); 266*27e6fa99SDennis Zhou (Facebook) } else { 267*27e6fa99SDennis Zhou (Facebook) /* 268*27e6fa99SDennis Zhou (Facebook) * This is a bit complicated. It is possible task_css is seeing 269*27e6fa99SDennis Zhou (Facebook) * an old css pointer here. This is caused by the current 270*27e6fa99SDennis Zhou (Facebook) * thread migrating away from this cgroup and this cgroup dying. 271*27e6fa99SDennis Zhou (Facebook) * css_tryget() will fail when trying to take a ref on a cgroup 272*27e6fa99SDennis Zhou (Facebook) * that's ref count has hit 0. 273*27e6fa99SDennis Zhou (Facebook) * 274*27e6fa99SDennis Zhou (Facebook) * Therefore, if it does fail, this means current must have 275*27e6fa99SDennis Zhou (Facebook) * been swapped away already and this is waiting for it to 276*27e6fa99SDennis Zhou (Facebook) * propagate on the polling cpu. Hence the use of cpu_relax(). 277*27e6fa99SDennis Zhou (Facebook) */ 278*27e6fa99SDennis Zhou (Facebook) while (true) { 279*27e6fa99SDennis Zhou (Facebook) css = task_css(current, io_cgrp_id); 280*27e6fa99SDennis Zhou (Facebook) if (likely(css_tryget(css))) 281*27e6fa99SDennis Zhou (Facebook) break; 282*27e6fa99SDennis Zhou (Facebook) cpu_relax(); 283*27e6fa99SDennis Zhou (Facebook) } 284*27e6fa99SDennis Zhou (Facebook) } 285*27e6fa99SDennis Zhou (Facebook) 286*27e6fa99SDennis Zhou (Facebook) rcu_read_unlock(); 287*27e6fa99SDennis Zhou (Facebook) 288*27e6fa99SDennis Zhou (Facebook) return css; 289*27e6fa99SDennis Zhou (Facebook) } 290eea8f41cSTejun Heo 291eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) 292eea8f41cSTejun Heo { 293eea8f41cSTejun Heo return css ? container_of(css, struct blkcg, css) : NULL; 294eea8f41cSTejun Heo } 295eea8f41cSTejun Heo 296*27e6fa99SDennis Zhou (Facebook) /** 297*27e6fa99SDennis Zhou (Facebook) * __bio_blkcg - internal version of bio_blkcg for bfq and cfq 298*27e6fa99SDennis Zhou (Facebook) * 299*27e6fa99SDennis Zhou (Facebook) * DO NOT USE. 300*27e6fa99SDennis Zhou (Facebook) * There is a flaw using this version of the function. In particular, this was 301*27e6fa99SDennis Zhou (Facebook) * used in a broken paradigm where association was called on the given css. It 302*27e6fa99SDennis Zhou (Facebook) * is possible though that the returned css from task_css() is in the process 303*27e6fa99SDennis Zhou (Facebook) * of dying due to migration of the current task. So it is improper to assume 304*27e6fa99SDennis Zhou (Facebook) * *_get() is going to succeed. Both BFQ and CFQ rely on this logic and will 305*27e6fa99SDennis Zhou (Facebook) * take additional work to handle more gracefully. 306*27e6fa99SDennis Zhou (Facebook) */ 307*27e6fa99SDennis Zhou (Facebook) static inline struct blkcg *__bio_blkcg(struct bio *bio) 308eea8f41cSTejun Heo { 309eea8f41cSTejun Heo if (bio && bio->bi_css) 310eea8f41cSTejun Heo return css_to_blkcg(bio->bi_css); 311*27e6fa99SDennis Zhou (Facebook) return css_to_blkcg(blkcg_css()); 312*27e6fa99SDennis Zhou (Facebook) } 313*27e6fa99SDennis Zhou (Facebook) 314*27e6fa99SDennis Zhou (Facebook) /** 315*27e6fa99SDennis Zhou (Facebook) * bio_blkcg - grab the blkcg associated with a bio 316*27e6fa99SDennis Zhou (Facebook) * @bio: target bio 317*27e6fa99SDennis Zhou (Facebook) * 318*27e6fa99SDennis Zhou (Facebook) * This returns the blkcg associated with a bio, NULL if not associated. 319*27e6fa99SDennis Zhou (Facebook) * Callers are expected to either handle NULL or know association has been 320*27e6fa99SDennis Zhou (Facebook) * done prior to calling this. 321*27e6fa99SDennis Zhou (Facebook) */ 322*27e6fa99SDennis Zhou (Facebook) static inline struct blkcg *bio_blkcg(struct bio *bio) 323*27e6fa99SDennis Zhou (Facebook) { 324*27e6fa99SDennis Zhou (Facebook) if (bio && bio->bi_css) 325*27e6fa99SDennis Zhou (Facebook) return css_to_blkcg(bio->bi_css); 326*27e6fa99SDennis Zhou (Facebook) return NULL; 327fd383c2dSTejun Heo } 328fd383c2dSTejun Heo 329d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) 330d09d8df3SJosef Bacik { 331d09d8df3SJosef Bacik struct cgroup_subsys_state *css; 332d09d8df3SJosef Bacik bool ret = false; 333d09d8df3SJosef Bacik 334d09d8df3SJosef Bacik rcu_read_lock(); 335d09d8df3SJosef Bacik css = kthread_blkcg(); 336d09d8df3SJosef Bacik if (!css) 337d09d8df3SJosef Bacik css = task_css(current, io_cgrp_id); 338d09d8df3SJosef Bacik while (css) { 339d09d8df3SJosef Bacik if (atomic_read(&css->cgroup->congestion_count)) { 340d09d8df3SJosef Bacik ret = true; 341d09d8df3SJosef Bacik break; 342d09d8df3SJosef Bacik } 343d09d8df3SJosef Bacik css = css->parent; 344d09d8df3SJosef Bacik } 345d09d8df3SJosef Bacik rcu_read_unlock(); 346d09d8df3SJosef Bacik return ret; 347d09d8df3SJosef Bacik } 348d09d8df3SJosef Bacik 349eea8f41cSTejun Heo /** 350c7c98fd3SJosef Bacik * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg 351c7c98fd3SJosef Bacik * @return: true if this bio needs to be submitted with the root blkg context. 352c7c98fd3SJosef Bacik * 353c7c98fd3SJosef Bacik * In order to avoid priority inversions we sometimes need to issue a bio as if 354c7c98fd3SJosef Bacik * it were attached to the root blkg, and then backcharge to the actual owning 355c7c98fd3SJosef Bacik * blkg. The idea is we do bio_blkcg() to look up the actual context for the 356c7c98fd3SJosef Bacik * bio and attach the appropriate blkg to the bio. Then we call this helper and 357c7c98fd3SJosef Bacik * if it is true run with the root blkg for that queue and then do any 358c7c98fd3SJosef Bacik * backcharging to the originating cgroup once the io is complete. 359c7c98fd3SJosef Bacik */ 360c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio) 361c7c98fd3SJosef Bacik { 3620d1e0c7cSJosef Bacik return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; 363c7c98fd3SJosef Bacik } 364c7c98fd3SJosef Bacik 365c7c98fd3SJosef Bacik /** 366eea8f41cSTejun Heo * blkcg_parent - get the parent of a blkcg 367eea8f41cSTejun Heo * @blkcg: blkcg of interest 368eea8f41cSTejun Heo * 369eea8f41cSTejun Heo * Return the parent blkcg of @blkcg. Can be called anytime. 370eea8f41cSTejun Heo */ 371eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) 372eea8f41cSTejun Heo { 373eea8f41cSTejun Heo return css_to_blkcg(blkcg->css.parent); 374eea8f41cSTejun Heo } 375eea8f41cSTejun Heo 376eea8f41cSTejun Heo /** 37724f29046STejun Heo * __blkg_lookup - internal version of blkg_lookup() 37824f29046STejun Heo * @blkcg: blkcg of interest 37924f29046STejun Heo * @q: request_queue of interest 38024f29046STejun Heo * @update_hint: whether to update lookup hint with the result or not 38124f29046STejun Heo * 38224f29046STejun Heo * This is internal version and shouldn't be used by policy 38324f29046STejun Heo * implementations. Looks up blkgs for the @blkcg - @q pair regardless of 38424f29046STejun Heo * @q's bypass state. If @update_hint is %true, the caller should be 38524f29046STejun Heo * holding @q->queue_lock and lookup hint is updated on success. 38624f29046STejun Heo */ 38724f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, 38824f29046STejun Heo struct request_queue *q, 38924f29046STejun Heo bool update_hint) 39024f29046STejun Heo { 39124f29046STejun Heo struct blkcg_gq *blkg; 39224f29046STejun Heo 39385b6bc9dSTejun Heo if (blkcg == &blkcg_root) 39485b6bc9dSTejun Heo return q->root_blkg; 39585b6bc9dSTejun Heo 39624f29046STejun Heo blkg = rcu_dereference(blkcg->blkg_hint); 39724f29046STejun Heo if (blkg && blkg->q == q) 39824f29046STejun Heo return blkg; 39924f29046STejun Heo 40024f29046STejun Heo return blkg_lookup_slowpath(blkcg, q, update_hint); 40124f29046STejun Heo } 40224f29046STejun Heo 40324f29046STejun Heo /** 40424f29046STejun Heo * blkg_lookup - lookup blkg for the specified blkcg - q pair 40524f29046STejun Heo * @blkcg: blkcg of interest 40624f29046STejun Heo * @q: request_queue of interest 40724f29046STejun Heo * 40824f29046STejun Heo * Lookup blkg for the @blkcg - @q pair. This function should be called 40924f29046STejun Heo * under RCU read lock and is guaranteed to return %NULL if @q is bypassing 41024f29046STejun Heo * - see blk_queue_bypass_start() for details. 41124f29046STejun Heo */ 41224f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, 41324f29046STejun Heo struct request_queue *q) 41424f29046STejun Heo { 41524f29046STejun Heo WARN_ON_ONCE(!rcu_read_lock_held()); 41624f29046STejun Heo 41724f29046STejun Heo if (unlikely(blk_queue_bypass(q))) 41824f29046STejun Heo return NULL; 41924f29046STejun Heo return __blkg_lookup(blkcg, q, false); 42024f29046STejun Heo } 42124f29046STejun Heo 42224f29046STejun Heo /** 423b86d865cSBart Van Assche * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair 4246bad9b21SBart Van Assche * @q: request_queue of interest 4256bad9b21SBart Van Assche * 4266bad9b21SBart Van Assche * Lookup blkg for @q at the root level. See also blkg_lookup(). 4276bad9b21SBart Van Assche */ 428b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 4296bad9b21SBart Van Assche { 430b86d865cSBart Van Assche return q->root_blkg; 4316bad9b21SBart Van Assche } 4326bad9b21SBart Van Assche 4336bad9b21SBart Van Assche /** 434eea8f41cSTejun Heo * blkg_to_pdata - get policy private data 435eea8f41cSTejun Heo * @blkg: blkg of interest 436eea8f41cSTejun Heo * @pol: policy of interest 437eea8f41cSTejun Heo * 438eea8f41cSTejun Heo * Return pointer to private data associated with the @blkg-@pol pair. 439eea8f41cSTejun Heo */ 440eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 441eea8f41cSTejun Heo struct blkcg_policy *pol) 442eea8f41cSTejun Heo { 443eea8f41cSTejun Heo return blkg ? blkg->pd[pol->plid] : NULL; 444eea8f41cSTejun Heo } 445eea8f41cSTejun Heo 446e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, 447e4bc13adSLinus Torvalds struct blkcg_policy *pol) 448e4bc13adSLinus Torvalds { 44981437648STejun Heo return blkcg ? blkcg->cpd[pol->plid] : NULL; 450e4bc13adSLinus Torvalds } 451e4bc13adSLinus Torvalds 452eea8f41cSTejun Heo /** 453eea8f41cSTejun Heo * pdata_to_blkg - get blkg associated with policy private data 454eea8f41cSTejun Heo * @pd: policy private data of interest 455eea8f41cSTejun Heo * 456eea8f41cSTejun Heo * @pd is policy private data. Determine the blkg it's associated with. 457eea8f41cSTejun Heo */ 458eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) 459eea8f41cSTejun Heo { 460eea8f41cSTejun Heo return pd ? pd->blkg : NULL; 461eea8f41cSTejun Heo } 462eea8f41cSTejun Heo 46381437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) 46481437648STejun Heo { 46581437648STejun Heo return cpd ? cpd->blkcg : NULL; 46681437648STejun Heo } 46781437648STejun Heo 46859b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg); 46959b57717SDennis Zhou (Facebook) 47059b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK 47159b57717SDennis Zhou (Facebook) 47259b57717SDennis Zhou (Facebook) /** 47359b57717SDennis Zhou (Facebook) * blkcg_cgwb_get - get a reference for blkcg->cgwb_list 47459b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 47559b57717SDennis Zhou (Facebook) * 47659b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 47759b57717SDennis Zhou (Facebook) */ 47859b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) 47959b57717SDennis Zhou (Facebook) { 48059b57717SDennis Zhou (Facebook) refcount_inc(&blkcg->cgwb_refcnt); 48159b57717SDennis Zhou (Facebook) } 48259b57717SDennis Zhou (Facebook) 48359b57717SDennis Zhou (Facebook) /** 48459b57717SDennis Zhou (Facebook) * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list 48559b57717SDennis Zhou (Facebook) * @blkcg: blkcg of interest 48659b57717SDennis Zhou (Facebook) * 48759b57717SDennis Zhou (Facebook) * This is used to track the number of active wb's related to a blkcg. 48859b57717SDennis Zhou (Facebook) * When this count goes to zero, all active wb has finished so the 48959b57717SDennis Zhou (Facebook) * blkcg can continue destruction by calling blkcg_destroy_blkgs(). 49059b57717SDennis Zhou (Facebook) * This work may occur in cgwb_release_workfn() on the cgwb_release 49159b57717SDennis Zhou (Facebook) * workqueue. 49259b57717SDennis Zhou (Facebook) */ 49359b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 49459b57717SDennis Zhou (Facebook) { 49559b57717SDennis Zhou (Facebook) if (refcount_dec_and_test(&blkcg->cgwb_refcnt)) 49659b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 49759b57717SDennis Zhou (Facebook) } 49859b57717SDennis Zhou (Facebook) 49959b57717SDennis Zhou (Facebook) #else 50059b57717SDennis Zhou (Facebook) 50159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { } 50259b57717SDennis Zhou (Facebook) 50359b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg) 50459b57717SDennis Zhou (Facebook) { 50559b57717SDennis Zhou (Facebook) /* wb isn't being accounted, so trigger destruction right away */ 50659b57717SDennis Zhou (Facebook) blkcg_destroy_blkgs(blkcg); 50759b57717SDennis Zhou (Facebook) } 50859b57717SDennis Zhou (Facebook) 50959b57717SDennis Zhou (Facebook) #endif 51059b57717SDennis Zhou (Facebook) 511eea8f41cSTejun Heo /** 512eea8f41cSTejun Heo * blkg_path - format cgroup path of blkg 513eea8f41cSTejun Heo * @blkg: blkg of interest 514eea8f41cSTejun Heo * @buf: target buffer 515eea8f41cSTejun Heo * @buflen: target buffer length 516eea8f41cSTejun Heo * 517eea8f41cSTejun Heo * Format the path of the cgroup of @blkg into @buf. 518eea8f41cSTejun Heo */ 519eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) 520eea8f41cSTejun Heo { 5214c737b41STejun Heo return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); 522eea8f41cSTejun Heo } 523eea8f41cSTejun Heo 524eea8f41cSTejun Heo /** 525eea8f41cSTejun Heo * blkg_get - get a blkg reference 526eea8f41cSTejun Heo * @blkg: blkg to get 527eea8f41cSTejun Heo * 528eea8f41cSTejun Heo * The caller should be holding an existing reference. 529eea8f41cSTejun Heo */ 530eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) 531eea8f41cSTejun Heo { 532eea8f41cSTejun Heo WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0); 533eea8f41cSTejun Heo atomic_inc(&blkg->refcnt); 534eea8f41cSTejun Heo } 535eea8f41cSTejun Heo 536d09d8df3SJosef Bacik /** 537d09d8df3SJosef Bacik * blkg_try_get - try and get a blkg reference 538d09d8df3SJosef Bacik * @blkg: blkg to get 539d09d8df3SJosef Bacik * 540d09d8df3SJosef Bacik * This is for use when doing an RCU lookup of the blkg. We may be in the midst 541d09d8df3SJosef Bacik * of freeing this blkg, so we can only use it if the refcnt is not zero. 542d09d8df3SJosef Bacik */ 543d09d8df3SJosef Bacik static inline struct blkcg_gq *blkg_try_get(struct blkcg_gq *blkg) 544d09d8df3SJosef Bacik { 545d09d8df3SJosef Bacik if (atomic_inc_not_zero(&blkg->refcnt)) 546d09d8df3SJosef Bacik return blkg; 547d09d8df3SJosef Bacik return NULL; 548d09d8df3SJosef Bacik } 549d09d8df3SJosef Bacik 550d09d8df3SJosef Bacik 551eea8f41cSTejun Heo void __blkg_release_rcu(struct rcu_head *rcu); 552eea8f41cSTejun Heo 553eea8f41cSTejun Heo /** 554eea8f41cSTejun Heo * blkg_put - put a blkg reference 555eea8f41cSTejun Heo * @blkg: blkg to put 556eea8f41cSTejun Heo */ 557eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) 558eea8f41cSTejun Heo { 559eea8f41cSTejun Heo WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0); 560eea8f41cSTejun Heo if (atomic_dec_and_test(&blkg->refcnt)) 561eea8f41cSTejun Heo call_rcu(&blkg->rcu_head, __blkg_release_rcu); 562eea8f41cSTejun Heo } 563eea8f41cSTejun Heo 564eea8f41cSTejun Heo /** 565eea8f41cSTejun Heo * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants 566eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 567eea8f41cSTejun Heo * @pos_css: used for iteration 568eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 569eea8f41cSTejun Heo * 570eea8f41cSTejun Heo * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU 571eea8f41cSTejun Heo * read locked. If called under either blkcg or queue lock, the iteration 572eea8f41cSTejun Heo * is guaranteed to include all and only online blkgs. The caller may 573eea8f41cSTejun Heo * update @pos_css by calling css_rightmost_descendant() to skip subtree. 574eea8f41cSTejun Heo * @p_blkg is included in the iteration and the first node to be visited. 575eea8f41cSTejun Heo */ 576eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ 577eea8f41cSTejun Heo css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ 578eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 579eea8f41cSTejun Heo (p_blkg)->q, false))) 580eea8f41cSTejun Heo 581eea8f41cSTejun Heo /** 582eea8f41cSTejun Heo * blkg_for_each_descendant_post - post-order walk of a blkg's descendants 583eea8f41cSTejun Heo * @d_blkg: loop cursor pointing to the current descendant 584eea8f41cSTejun Heo * @pos_css: used for iteration 585eea8f41cSTejun Heo * @p_blkg: target blkg to walk descendants of 586eea8f41cSTejun Heo * 587eea8f41cSTejun Heo * Similar to blkg_for_each_descendant_pre() but performs post-order 588eea8f41cSTejun Heo * traversal instead. Synchronization rules are the same. @p_blkg is 589eea8f41cSTejun Heo * included in the iteration and the last node to be visited. 590eea8f41cSTejun Heo */ 591eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ 592eea8f41cSTejun Heo css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ 593eea8f41cSTejun Heo if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ 594eea8f41cSTejun Heo (p_blkg)->q, false))) 595eea8f41cSTejun Heo 596eea8f41cSTejun Heo /** 597eea8f41cSTejun Heo * blk_get_rl - get request_list to use 598eea8f41cSTejun Heo * @q: request_queue of interest 599eea8f41cSTejun Heo * @bio: bio which will be attached to the allocated request (may be %NULL) 600eea8f41cSTejun Heo * 601eea8f41cSTejun Heo * The caller wants to allocate a request from @q to use for @bio. Find 602eea8f41cSTejun Heo * the request_list to use and obtain a reference on it. Should be called 603eea8f41cSTejun Heo * under queue_lock. This function is guaranteed to return non-%NULL 604eea8f41cSTejun Heo * request_list. 605eea8f41cSTejun Heo */ 606eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q, 607eea8f41cSTejun Heo struct bio *bio) 608eea8f41cSTejun Heo { 609eea8f41cSTejun Heo struct blkcg *blkcg; 610eea8f41cSTejun Heo struct blkcg_gq *blkg; 611eea8f41cSTejun Heo 612eea8f41cSTejun Heo rcu_read_lock(); 613eea8f41cSTejun Heo 614eea8f41cSTejun Heo blkcg = bio_blkcg(bio); 615*27e6fa99SDennis Zhou (Facebook) if (blkcg) 616*27e6fa99SDennis Zhou (Facebook) css_get(&blkcg->css); 617*27e6fa99SDennis Zhou (Facebook) else 618*27e6fa99SDennis Zhou (Facebook) blkcg = css_to_blkcg(blkcg_get_css()); 619eea8f41cSTejun Heo 620eea8f41cSTejun Heo /* bypass blkg lookup and use @q->root_rl directly for root */ 621eea8f41cSTejun Heo if (blkcg == &blkcg_root) 622eea8f41cSTejun Heo goto root_rl; 623eea8f41cSTejun Heo 624eea8f41cSTejun Heo /* 625eea8f41cSTejun Heo * Try to use blkg->rl. blkg lookup may fail under memory pressure 626eea8f41cSTejun Heo * or if either the blkcg or queue is going away. Fall back to 627eea8f41cSTejun Heo * root_rl in such cases. 628eea8f41cSTejun Heo */ 629ae118896STejun Heo blkg = blkg_lookup(blkcg, q); 630ae118896STejun Heo if (unlikely(!blkg)) 631eea8f41cSTejun Heo goto root_rl; 632eea8f41cSTejun Heo 633eea8f41cSTejun Heo blkg_get(blkg); 634eea8f41cSTejun Heo rcu_read_unlock(); 635eea8f41cSTejun Heo return &blkg->rl; 636eea8f41cSTejun Heo root_rl: 637eea8f41cSTejun Heo rcu_read_unlock(); 638eea8f41cSTejun Heo return &q->root_rl; 639eea8f41cSTejun Heo } 640eea8f41cSTejun Heo 641eea8f41cSTejun Heo /** 642eea8f41cSTejun Heo * blk_put_rl - put request_list 643eea8f41cSTejun Heo * @rl: request_list to put 644eea8f41cSTejun Heo * 645eea8f41cSTejun Heo * Put the reference acquired by blk_get_rl(). Should be called under 646eea8f41cSTejun Heo * queue_lock. 647eea8f41cSTejun Heo */ 648eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl) 649eea8f41cSTejun Heo { 650*27e6fa99SDennis Zhou (Facebook) /* an additional ref is always taken for rl */ 651*27e6fa99SDennis Zhou (Facebook) css_put(&rl->blkg->blkcg->css); 652401efbf8STejun Heo if (rl->blkg->blkcg != &blkcg_root) 653eea8f41cSTejun Heo blkg_put(rl->blkg); 654eea8f41cSTejun Heo } 655eea8f41cSTejun Heo 656eea8f41cSTejun Heo /** 657eea8f41cSTejun Heo * blk_rq_set_rl - associate a request with a request_list 658eea8f41cSTejun Heo * @rq: request of interest 659eea8f41cSTejun Heo * @rl: target request_list 660eea8f41cSTejun Heo * 661eea8f41cSTejun Heo * Associate @rq with @rl so that accounting and freeing can know the 662eea8f41cSTejun Heo * request_list @rq came from. 663eea8f41cSTejun Heo */ 664eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) 665eea8f41cSTejun Heo { 666eea8f41cSTejun Heo rq->rl = rl; 667eea8f41cSTejun Heo } 668eea8f41cSTejun Heo 669eea8f41cSTejun Heo /** 670eea8f41cSTejun Heo * blk_rq_rl - return the request_list a request came from 671eea8f41cSTejun Heo * @rq: request of interest 672eea8f41cSTejun Heo * 673eea8f41cSTejun Heo * Return the request_list @rq is allocated from. 674eea8f41cSTejun Heo */ 675eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq) 676eea8f41cSTejun Heo { 677eea8f41cSTejun Heo return rq->rl; 678eea8f41cSTejun Heo } 679eea8f41cSTejun Heo 680eea8f41cSTejun Heo struct request_list *__blk_queue_next_rl(struct request_list *rl, 681eea8f41cSTejun Heo struct request_queue *q); 682eea8f41cSTejun Heo /** 683eea8f41cSTejun Heo * blk_queue_for_each_rl - iterate through all request_lists of a request_queue 684eea8f41cSTejun Heo * 685eea8f41cSTejun Heo * Should be used under queue_lock. 686eea8f41cSTejun Heo */ 687eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 688eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q))) 689eea8f41cSTejun Heo 69024bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp) 691eea8f41cSTejun Heo { 69224bdb8efSTejun Heo int ret; 69324bdb8efSTejun Heo 69424bdb8efSTejun Heo ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp); 69524bdb8efSTejun Heo if (ret) 69624bdb8efSTejun Heo return ret; 69724bdb8efSTejun Heo 698e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 69924bdb8efSTejun Heo return 0; 70024bdb8efSTejun Heo } 70124bdb8efSTejun Heo 70224bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat) 70324bdb8efSTejun Heo { 70424bdb8efSTejun Heo percpu_counter_destroy(&stat->cpu_cnt); 705eea8f41cSTejun Heo } 706eea8f41cSTejun Heo 707eea8f41cSTejun Heo /** 708eea8f41cSTejun Heo * blkg_stat_add - add a value to a blkg_stat 709eea8f41cSTejun Heo * @stat: target blkg_stat 710eea8f41cSTejun Heo * @val: value to add 711eea8f41cSTejun Heo * 71224bdb8efSTejun Heo * Add @val to @stat. The caller must ensure that IRQ on the same CPU 71324bdb8efSTejun Heo * don't re-enter this function for the same counter. 714eea8f41cSTejun Heo */ 715eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val) 716eea8f41cSTejun Heo { 717104b4e51SNikolay Borisov percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH); 718eea8f41cSTejun Heo } 719eea8f41cSTejun Heo 720eea8f41cSTejun Heo /** 721eea8f41cSTejun Heo * blkg_stat_read - read the current value of a blkg_stat 722eea8f41cSTejun Heo * @stat: blkg_stat to read 723eea8f41cSTejun Heo */ 724eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat) 725eea8f41cSTejun Heo { 72624bdb8efSTejun Heo return percpu_counter_sum_positive(&stat->cpu_cnt); 727eea8f41cSTejun Heo } 728eea8f41cSTejun Heo 729eea8f41cSTejun Heo /** 730eea8f41cSTejun Heo * blkg_stat_reset - reset a blkg_stat 731eea8f41cSTejun Heo * @stat: blkg_stat to reset 732eea8f41cSTejun Heo */ 733eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat) 734eea8f41cSTejun Heo { 73524bdb8efSTejun Heo percpu_counter_set(&stat->cpu_cnt, 0); 736e6269c44STejun Heo atomic64_set(&stat->aux_cnt, 0); 737eea8f41cSTejun Heo } 738eea8f41cSTejun Heo 739eea8f41cSTejun Heo /** 740e6269c44STejun Heo * blkg_stat_add_aux - add a blkg_stat into another's aux count 741eea8f41cSTejun Heo * @to: the destination blkg_stat 742eea8f41cSTejun Heo * @from: the source 743eea8f41cSTejun Heo * 744e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 745eea8f41cSTejun Heo */ 746e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to, 747e6269c44STejun Heo struct blkg_stat *from) 748eea8f41cSTejun Heo { 749e6269c44STejun Heo atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt), 750e6269c44STejun Heo &to->aux_cnt); 751eea8f41cSTejun Heo } 752eea8f41cSTejun Heo 75324bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp) 75424bdb8efSTejun Heo { 75524bdb8efSTejun Heo int i, ret; 75624bdb8efSTejun Heo 75724bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 75824bdb8efSTejun Heo ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp); 75924bdb8efSTejun Heo if (ret) { 76024bdb8efSTejun Heo while (--i >= 0) 76124bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 76224bdb8efSTejun Heo return ret; 76324bdb8efSTejun Heo } 76424bdb8efSTejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 76524bdb8efSTejun Heo } 76624bdb8efSTejun Heo return 0; 76724bdb8efSTejun Heo } 76824bdb8efSTejun Heo 76924bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat) 770eea8f41cSTejun Heo { 771e6269c44STejun Heo int i; 772e6269c44STejun Heo 773e6269c44STejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 77424bdb8efSTejun Heo percpu_counter_destroy(&rwstat->cpu_cnt[i]); 775eea8f41cSTejun Heo } 776eea8f41cSTejun Heo 777eea8f41cSTejun Heo /** 778eea8f41cSTejun Heo * blkg_rwstat_add - add a value to a blkg_rwstat 779eea8f41cSTejun Heo * @rwstat: target blkg_rwstat 780ef295ecfSChristoph Hellwig * @op: REQ_OP and flags 781eea8f41cSTejun Heo * @val: value to add 782eea8f41cSTejun Heo * 783eea8f41cSTejun Heo * Add @val to @rwstat. The counters are chosen according to @rw. The 784eea8f41cSTejun Heo * caller is responsible for synchronizing calls to this function. 785eea8f41cSTejun Heo */ 786eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, 787ef295ecfSChristoph Hellwig unsigned int op, uint64_t val) 788eea8f41cSTejun Heo { 78924bdb8efSTejun Heo struct percpu_counter *cnt; 790eea8f41cSTejun Heo 791636620b6STejun Heo if (op_is_discard(op)) 792636620b6STejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD]; 793636620b6STejun Heo else if (op_is_write(op)) 79424bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE]; 795eea8f41cSTejun Heo else 79624bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ]; 797eea8f41cSTejun Heo 798104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 79924bdb8efSTejun Heo 800d71d9ae1SChristoph Hellwig if (op_is_sync(op)) 80124bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC]; 80224bdb8efSTejun Heo else 80324bdb8efSTejun Heo cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC]; 80424bdb8efSTejun Heo 805104b4e51SNikolay Borisov percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH); 806eea8f41cSTejun Heo } 807eea8f41cSTejun Heo 808eea8f41cSTejun Heo /** 809eea8f41cSTejun Heo * blkg_rwstat_read - read the current values of a blkg_rwstat 810eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 811eea8f41cSTejun Heo * 81224bdb8efSTejun Heo * Read the current snapshot of @rwstat and return it in the aux counts. 813eea8f41cSTejun Heo */ 814eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat) 815eea8f41cSTejun Heo { 81624bdb8efSTejun Heo struct blkg_rwstat result; 81724bdb8efSTejun Heo int i; 818eea8f41cSTejun Heo 81924bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 82024bdb8efSTejun Heo atomic64_set(&result.aux_cnt[i], 82124bdb8efSTejun Heo percpu_counter_sum_positive(&rwstat->cpu_cnt[i])); 82224bdb8efSTejun Heo return result; 823eea8f41cSTejun Heo } 824eea8f41cSTejun Heo 825eea8f41cSTejun Heo /** 826eea8f41cSTejun Heo * blkg_rwstat_total - read the total count of a blkg_rwstat 827eea8f41cSTejun Heo * @rwstat: blkg_rwstat to read 828eea8f41cSTejun Heo * 829eea8f41cSTejun Heo * Return the total count of @rwstat regardless of the IO direction. This 830eea8f41cSTejun Heo * function can be called without synchronization and takes care of u64 831eea8f41cSTejun Heo * atomicity. 832eea8f41cSTejun Heo */ 833eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat) 834eea8f41cSTejun Heo { 835eea8f41cSTejun Heo struct blkg_rwstat tmp = blkg_rwstat_read(rwstat); 836eea8f41cSTejun Heo 83724bdb8efSTejun Heo return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + 83824bdb8efSTejun Heo atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); 839eea8f41cSTejun Heo } 840eea8f41cSTejun Heo 841eea8f41cSTejun Heo /** 842eea8f41cSTejun Heo * blkg_rwstat_reset - reset a blkg_rwstat 843eea8f41cSTejun Heo * @rwstat: blkg_rwstat to reset 844eea8f41cSTejun Heo */ 845eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) 846eea8f41cSTejun Heo { 847e6269c44STejun Heo int i; 848e6269c44STejun Heo 84924bdb8efSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) { 85024bdb8efSTejun Heo percpu_counter_set(&rwstat->cpu_cnt[i], 0); 851e6269c44STejun Heo atomic64_set(&rwstat->aux_cnt[i], 0); 852eea8f41cSTejun Heo } 85324bdb8efSTejun Heo } 854eea8f41cSTejun Heo 855eea8f41cSTejun Heo /** 856e6269c44STejun Heo * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count 857eea8f41cSTejun Heo * @to: the destination blkg_rwstat 858eea8f41cSTejun Heo * @from: the source 859eea8f41cSTejun Heo * 860e6269c44STejun Heo * Add @from's count including the aux one to @to's aux count. 861eea8f41cSTejun Heo */ 862e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to, 863eea8f41cSTejun Heo struct blkg_rwstat *from) 864eea8f41cSTejun Heo { 865ddc21231SArnd Bergmann u64 sum[BLKG_RWSTAT_NR]; 866eea8f41cSTejun Heo int i; 867eea8f41cSTejun Heo 868eea8f41cSTejun Heo for (i = 0; i < BLKG_RWSTAT_NR; i++) 869ddc21231SArnd Bergmann sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]); 870ddc21231SArnd Bergmann 871ddc21231SArnd Bergmann for (i = 0; i < BLKG_RWSTAT_NR; i++) 872ddc21231SArnd Bergmann atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]), 873e6269c44STejun Heo &to->aux_cnt[i]); 874eea8f41cSTejun Heo } 875eea8f41cSTejun Heo 876ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING 877ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 878ae118896STejun Heo struct bio *bio); 879ae118896STejun Heo #else 880ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, 881ae118896STejun Heo struct bio *bio) { return false; } 882ae118896STejun Heo #endif 883ae118896STejun Heo 884ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 885ae118896STejun Heo struct bio *bio) 886ae118896STejun Heo { 887ae118896STejun Heo struct blkcg *blkcg; 888ae118896STejun Heo struct blkcg_gq *blkg; 889ae118896STejun Heo bool throtl = false; 890ae118896STejun Heo 891ae118896STejun Heo rcu_read_lock(); 892ae118896STejun Heo 893007cc56bSShaohua Li /* associate blkcg if bio hasn't attached one */ 894*27e6fa99SDennis Zhou (Facebook) bio_associate_blkcg(bio, NULL); 895*27e6fa99SDennis Zhou (Facebook) blkcg = bio_blkcg(bio); 896007cc56bSShaohua Li 897ae118896STejun Heo blkg = blkg_lookup(blkcg, q); 898ae118896STejun Heo if (unlikely(!blkg)) { 899ae118896STejun Heo spin_lock_irq(q->queue_lock); 900ae118896STejun Heo blkg = blkg_lookup_create(blkcg, q); 901ae118896STejun Heo if (IS_ERR(blkg)) 902ae118896STejun Heo blkg = NULL; 903ae118896STejun Heo spin_unlock_irq(q->queue_lock); 904ae118896STejun Heo } 905ae118896STejun Heo 906ae118896STejun Heo throtl = blk_throtl_bio(q, blkg, bio); 907ae118896STejun Heo 90877ea7338STejun Heo if (!throtl) { 90977ea7338STejun Heo blkg = blkg ?: q->root_blkg; 910c454edc2SJosef Bacik /* 911c454edc2SJosef Bacik * If the bio is flagged with BIO_QUEUE_ENTERED it means this 912c454edc2SJosef Bacik * is a split bio and we would have already accounted for the 913c454edc2SJosef Bacik * size of the bio. 914c454edc2SJosef Bacik */ 915c454edc2SJosef Bacik if (!bio_flagged(bio, BIO_QUEUE_ENTERED)) 916ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf, 91777ea7338STejun Heo bio->bi_iter.bi_size); 918ef295ecfSChristoph Hellwig blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1); 91977ea7338STejun Heo } 92077ea7338STejun Heo 921ae118896STejun Heo rcu_read_unlock(); 922ae118896STejun Heo return !throtl; 923ae118896STejun Heo } 924ae118896STejun Heo 925d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg) 926d09d8df3SJosef Bacik { 927d09d8df3SJosef Bacik if (atomic_add_return(1, &blkg->use_delay) == 1) 928d09d8df3SJosef Bacik atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); 929d09d8df3SJosef Bacik } 930d09d8df3SJosef Bacik 931d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) 932d09d8df3SJosef Bacik { 933d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 934d09d8df3SJosef Bacik 935d09d8df3SJosef Bacik if (old == 0) 936d09d8df3SJosef Bacik return 0; 937d09d8df3SJosef Bacik 938d09d8df3SJosef Bacik /* 939d09d8df3SJosef Bacik * We do this song and dance because we can race with somebody else 940d09d8df3SJosef Bacik * adding or removing delay. If we just did an atomic_dec we'd end up 941d09d8df3SJosef Bacik * negative and we'd already be in trouble. We need to subtract 1 and 942d09d8df3SJosef Bacik * then check to see if we were the last delay so we can drop the 943d09d8df3SJosef Bacik * congestion count on the cgroup. 944d09d8df3SJosef Bacik */ 945d09d8df3SJosef Bacik while (old) { 946d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); 947d09d8df3SJosef Bacik if (cur == old) 948d09d8df3SJosef Bacik break; 949d09d8df3SJosef Bacik old = cur; 950d09d8df3SJosef Bacik } 951d09d8df3SJosef Bacik 952d09d8df3SJosef Bacik if (old == 0) 953d09d8df3SJosef Bacik return 0; 954d09d8df3SJosef Bacik if (old == 1) 955d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 956d09d8df3SJosef Bacik return 1; 957d09d8df3SJosef Bacik } 958d09d8df3SJosef Bacik 959d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg) 960d09d8df3SJosef Bacik { 961d09d8df3SJosef Bacik int old = atomic_read(&blkg->use_delay); 962d09d8df3SJosef Bacik if (!old) 963d09d8df3SJosef Bacik return; 964d09d8df3SJosef Bacik /* We only want 1 person clearing the congestion count for this blkg. */ 965d09d8df3SJosef Bacik while (old) { 966d09d8df3SJosef Bacik int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); 967d09d8df3SJosef Bacik if (cur == old) { 968d09d8df3SJosef Bacik atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); 969d09d8df3SJosef Bacik break; 970d09d8df3SJosef Bacik } 971d09d8df3SJosef Bacik old = cur; 972d09d8df3SJosef Bacik } 973d09d8df3SJosef Bacik } 974d09d8df3SJosef Bacik 975d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); 976d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); 977d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void); 978eea8f41cSTejun Heo #else /* CONFIG_BLK_CGROUP */ 979eea8f41cSTejun Heo 980efa7d1c7STejun Heo struct blkcg { 981efa7d1c7STejun Heo }; 982eea8f41cSTejun Heo 983eea8f41cSTejun Heo struct blkg_policy_data { 984eea8f41cSTejun Heo }; 985eea8f41cSTejun Heo 986e4bc13adSLinus Torvalds struct blkcg_policy_data { 987e4bc13adSLinus Torvalds }; 988e4bc13adSLinus Torvalds 989eea8f41cSTejun Heo struct blkcg_gq { 990eea8f41cSTejun Heo }; 991eea8f41cSTejun Heo 992eea8f41cSTejun Heo struct blkcg_policy { 993eea8f41cSTejun Heo }; 994eea8f41cSTejun Heo 995496d5e75STejun Heo #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) 996496d5e75STejun Heo 997d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { } 998d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; } 999d09d8df3SJosef Bacik 1000efa7d1c7STejun Heo #ifdef CONFIG_BLOCK 1001efa7d1c7STejun Heo 1002d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } 1003d09d8df3SJosef Bacik 1004eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } 1005b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) 1006b86d865cSBart Van Assche { return NULL; } 1007eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; } 1008eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { } 1009eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { } 1010eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } 1011eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } 1012eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q, 1013eea8f41cSTejun Heo const struct blkcg_policy *pol) { return 0; } 1014eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q, 1015eea8f41cSTejun Heo const struct blkcg_policy *pol) { } 1016eea8f41cSTejun Heo 1017*27e6fa99SDennis Zhou (Facebook) static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; } 1018eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } 1019eea8f41cSTejun Heo 1020eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, 1021eea8f41cSTejun Heo struct blkcg_policy *pol) { return NULL; } 1022eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } 1023eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } 1024eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { } 1025eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { } 1026eea8f41cSTejun Heo 1027eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q, 1028eea8f41cSTejun Heo struct bio *bio) { return &q->root_rl; } 1029eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl) { } 1030eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { } 1031eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; } 1032eea8f41cSTejun Heo 1033ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q, 1034ae118896STejun Heo struct bio *bio) { return true; } 1035ae118896STejun Heo 1036eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q) \ 1037eea8f41cSTejun Heo for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) 1038eea8f41cSTejun Heo 1039efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */ 1040eea8f41cSTejun Heo #endif /* CONFIG_BLK_CGROUP */ 1041eea8f41cSTejun Heo #endif /* _BLK_CGROUP_H */ 1042