Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 2 | #ifndef _BLK_CGROUP_H |
| 3 | #define _BLK_CGROUP_H |
| 4 | /* |
| 5 | * Common Block IO controller cgroup interface |
| 6 | * |
| 7 | * Based on ideas and code from CFQ, CFS and BFQ: |
| 8 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> |
| 9 | * |
| 10 | * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it> |
| 11 | * Paolo Valente <paolo.valente@unimore.it> |
| 12 | * |
| 13 | * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com> |
| 14 | * Nauman Rafique <nauman@google.com> |
| 15 | */ |
| 16 | |
| 17 | #include <linux/cgroup.h> |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 18 | #include <linux/percpu.h> |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 19 | #include <linux/percpu_counter.h> |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 20 | #include <linux/u64_stats_sync.h> |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 22 | #include <linux/radix-tree.h> |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 23 | #include <linux/blkdev.h> |
Tejun Heo | a5049a8 | 2014-06-19 17:42:57 -0400 | [diff] [blame] | 24 | #include <linux/atomic.h> |
Shaohua Li | 902ec5b | 2017-09-14 14:02:06 -0700 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Dennis Zhou | 5cdf2e3 | 2018-12-05 12:10:31 -0500 | [diff] [blame] | 26 | #include <linux/fs.h> |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 27 | |
Tejun Heo | 24bdb8e | 2015-08-18 14:55:22 -0700 | [diff] [blame] | 28 | /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */ |
| 29 | #define BLKG_STAT_CPU_BATCH (INT_MAX / 2) |
| 30 | |
Vivek Goyal | 9355aed | 2010-10-01 21:16:41 +0200 | [diff] [blame] | 31 | /* Max limits for throttle policy */ |
| 32 | #define THROTL_IOPS_MAX UINT_MAX |
| 33 | |
Tejun Heo | f48ec1d | 2012-04-13 13:11:25 -0700 | [diff] [blame] | 34 | #ifdef CONFIG_BLK_CGROUP |
| 35 | |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 36 | enum blkg_iostat_type { |
| 37 | BLKG_IOSTAT_READ, |
| 38 | BLKG_IOSTAT_WRITE, |
| 39 | BLKG_IOSTAT_DISCARD, |
| 40 | |
| 41 | BLKG_IOSTAT_NR, |
| 42 | }; |
| 43 | |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 44 | struct blkcg_gq; |
| 45 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 46 | struct blkcg { |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 47 | struct cgroup_subsys_state css; |
| 48 | spinlock_t lock; |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 49 | refcount_t online_pin; |
Tejun Heo | a637120 | 2012-04-19 16:29:24 -0700 | [diff] [blame] | 50 | |
| 51 | struct radix_tree_root blkg_tree; |
Bart Van Assche | 55679c8 | 2016-09-23 09:07:56 -0700 | [diff] [blame] | 52 | struct blkcg_gq __rcu *blkg_hint; |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 53 | struct hlist_head blkg_list; |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 54 | |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 55 | struct blkcg_policy_data *cpd[BLKCG_MAX_POLS]; |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 56 | |
Tejun Heo | 7876f93 | 2015-07-09 16:39:49 -0400 | [diff] [blame] | 57 | struct list_head all_blkcgs_node; |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 58 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 59 | struct list_head cgwb_list; |
| 60 | #endif |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 63 | struct blkg_iostat { |
| 64 | u64 bytes[BLKG_IOSTAT_NR]; |
| 65 | u64 ios[BLKG_IOSTAT_NR]; |
| 66 | }; |
| 67 | |
| 68 | struct blkg_iostat_set { |
| 69 | struct u64_stats_sync sync; |
| 70 | struct blkg_iostat cur; |
| 71 | struct blkg_iostat last; |
| 72 | }; |
| 73 | |
Tejun Heo | e6269c4 | 2015-08-18 14:55:21 -0700 | [diff] [blame] | 74 | /* |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 75 | * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a |
| 76 | * request_queue (q). This is used by blkcg policies which need to track |
| 77 | * information per blkcg - q pair. |
| 78 | * |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 79 | * There can be multiple active blkcg policies and each blkg:policy pair is |
| 80 | * represented by a blkg_policy_data which is allocated and freed by each |
| 81 | * policy's pd_alloc/free_fn() methods. A policy can allocate private data |
| 82 | * area by allocating larger data structure which embeds blkg_policy_data |
| 83 | * at the beginning. |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 84 | */ |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 85 | struct blkg_policy_data { |
Tejun Heo | b276a87 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 86 | /* the blkg and policy id this per-policy data belongs to */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 87 | struct blkcg_gq *blkg; |
Tejun Heo | b276a87 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 88 | int plid; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 91 | /* |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 92 | * Policies that need to keep per-blkcg data which is independent from any |
| 93 | * request_queue associated to it should implement cpd_alloc/free_fn() |
| 94 | * methods. A policy can allocate private data area by allocating larger |
| 95 | * data structure which embeds blkcg_policy_data at the beginning. |
| 96 | * cpd_init() is invoked to let each policy handle per-blkcg data. |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 97 | */ |
| 98 | struct blkcg_policy_data { |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 99 | /* the blkcg and policy id this per-policy data belongs to */ |
| 100 | struct blkcg *blkcg; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 101 | int plid; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 104 | /* association between a blk cgroup and a request queue */ |
| 105 | struct blkcg_gq { |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 106 | /* Pointer to the associated request_queue */ |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 107 | struct request_queue *q; |
| 108 | struct list_head q_node; |
| 109 | struct hlist_node blkcg_node; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 110 | struct blkcg *blkcg; |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 111 | |
Tejun Heo | ce7acfe | 2015-05-22 17:13:38 -0400 | [diff] [blame] | 112 | /* |
| 113 | * Each blkg gets congested separately and the congestion state is |
| 114 | * propagated to the matching bdi_writeback_congested. |
| 115 | */ |
| 116 | struct bdi_writeback_congested *wb_congested; |
| 117 | |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 118 | /* all non-root blkcg_gq's are guaranteed to have access to parent */ |
| 119 | struct blkcg_gq *parent; |
| 120 | |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 121 | /* reference count */ |
Dennis Zhou | 7fcf2b0 | 2018-12-05 12:10:38 -0500 | [diff] [blame] | 122 | struct percpu_ref refcnt; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 123 | |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 124 | /* is this blkg online? protected by both blkcg and q locks */ |
| 125 | bool online; |
| 126 | |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 127 | struct blkg_iostat_set __percpu *iostat_cpu; |
| 128 | struct blkg_iostat_set iostat; |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 129 | |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 130 | struct blkg_policy_data *pd[BLKCG_MAX_POLS]; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 131 | |
Tejun Heo | d3f77df | 2019-06-27 13:39:52 -0700 | [diff] [blame] | 132 | spinlock_t async_bio_lock; |
| 133 | struct bio_list async_bios; |
| 134 | struct work_struct async_bio_work; |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 135 | |
| 136 | atomic_t use_delay; |
| 137 | atomic64_t delay_nsec; |
| 138 | atomic64_t delay_start; |
| 139 | u64 last_delay; |
| 140 | int last_use; |
Tejun Heo | d3f77df | 2019-06-27 13:39:52 -0700 | [diff] [blame] | 141 | |
| 142 | struct rcu_head rcu_head; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 145 | typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp); |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 146 | typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd); |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 147 | typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd); |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 148 | typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd); |
Tejun Heo | cf09a8e | 2019-08-28 15:05:51 -0700 | [diff] [blame] | 149 | typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, |
| 150 | struct request_queue *q, struct blkcg *blkcg); |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 151 | typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd); |
| 152 | typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd); |
| 153 | typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd); |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 154 | typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd); |
Tejun Heo | a9520cd | 2015-08-18 14:55:14 -0700 | [diff] [blame] | 155 | typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd); |
Josef Bacik | 903d23f | 2018-07-03 11:14:52 -0400 | [diff] [blame] | 156 | typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf, |
| 157 | size_t size); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 158 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 159 | struct blkcg_policy { |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 160 | int plid; |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 161 | /* cgroup files for the policy */ |
Tejun Heo | 2ee867dc | 2015-08-18 14:55:34 -0700 | [diff] [blame] | 162 | struct cftype *dfl_cftypes; |
Tejun Heo | 880f50e | 2015-08-18 14:55:30 -0700 | [diff] [blame] | 163 | struct cftype *legacy_cftypes; |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 164 | |
| 165 | /* operations */ |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 166 | blkcg_pol_alloc_cpd_fn *cpd_alloc_fn; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 167 | blkcg_pol_init_cpd_fn *cpd_init_fn; |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 168 | blkcg_pol_free_cpd_fn *cpd_free_fn; |
Tejun Heo | 69d7fde | 2015-08-18 14:55:36 -0700 | [diff] [blame] | 169 | blkcg_pol_bind_cpd_fn *cpd_bind_fn; |
Tejun Heo | e4a9bde | 2015-08-18 14:55:16 -0700 | [diff] [blame] | 170 | |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 171 | blkcg_pol_alloc_pd_fn *pd_alloc_fn; |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 172 | blkcg_pol_init_pd_fn *pd_init_fn; |
Tejun Heo | f427d90 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 173 | blkcg_pol_online_pd_fn *pd_online_fn; |
| 174 | blkcg_pol_offline_pd_fn *pd_offline_fn; |
Tejun Heo | 001bea7 | 2015-08-18 14:55:11 -0700 | [diff] [blame] | 175 | blkcg_pol_free_pd_fn *pd_free_fn; |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 176 | blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn; |
Josef Bacik | 903d23f | 2018-07-03 11:14:52 -0400 | [diff] [blame] | 177 | blkcg_pol_stat_pd_fn *pd_stat_fn; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 178 | }; |
| 179 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 180 | extern struct blkcg blkcg_root; |
Tejun Heo | 496d5e7 | 2015-05-22 17:13:21 -0400 | [diff] [blame] | 181 | extern struct cgroup_subsys_state * const blkcg_root_css; |
Tejun Heo | 07b0fde | 2019-07-16 07:58:31 -0700 | [diff] [blame] | 182 | extern bool blkcg_debug_stats; |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 183 | |
Tejun Heo | 24f2904 | 2015-08-18 14:55:17 -0700 | [diff] [blame] | 184 | struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, |
| 185 | struct request_queue *q, bool update_hint); |
Dennis Zhou | b978962 | 2018-12-05 12:10:27 -0500 | [diff] [blame] | 186 | struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg, |
| 187 | struct request_queue *q); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 188 | struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, |
| 189 | struct request_queue *q); |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 190 | int blkcg_init_queue(struct request_queue *q); |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 191 | void blkcg_exit_queue(struct request_queue *q); |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 192 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 193 | /* Blkio controller policy registration */ |
Jens Axboe | d5bf029 | 2014-06-22 16:31:56 -0600 | [diff] [blame] | 194 | int blkcg_policy_register(struct blkcg_policy *pol); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 195 | void blkcg_policy_unregister(struct blkcg_policy *pol); |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 196 | int blkcg_activate_policy(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 197 | const struct blkcg_policy *pol); |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 198 | void blkcg_deactivate_policy(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 199 | const struct blkcg_policy *pol); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 200 | |
Tejun Heo | dd165eb | 2015-08-18 14:55:33 -0700 | [diff] [blame] | 201 | const char *blkg_dev_name(struct blkcg_gq *blkg); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 202 | void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg, |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 203 | u64 (*prfill)(struct seq_file *, |
| 204 | struct blkg_policy_data *, int), |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 205 | const struct blkcg_policy *pol, int data, |
Tejun Heo | ec39934 | 2012-04-13 13:11:27 -0700 | [diff] [blame] | 206 | bool show_total); |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 207 | u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v); |
Tejun Heo | 16b3de6 | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 208 | |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 209 | struct blkg_conf_ctx { |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 210 | struct gendisk *disk; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 211 | struct blkcg_gq *blkg; |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 212 | char *body; |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Tejun Heo | 015d254 | 2019-08-28 15:05:53 -0700 | [diff] [blame] | 215 | struct gendisk *blkcg_conf_get_disk(char **inputp); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 216 | int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, |
Tejun Heo | 36aa9e5 | 2015-08-18 14:55:31 -0700 | [diff] [blame] | 217 | char *input, struct blkg_conf_ctx *ctx); |
Tejun Heo | 829fdb5 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 218 | void blkg_conf_finish(struct blkg_conf_ctx *ctx); |
| 219 | |
Dennis Zhou | 0fe061b | 2018-12-05 12:10:26 -0500 | [diff] [blame] | 220 | /** |
| 221 | * blkcg_css - find the current css |
| 222 | * |
| 223 | * Find the css associated with either the kthread or the current task. |
| 224 | * This may return a dying css, so it is up to the caller to use tryget logic |
| 225 | * to confirm it is alive and well. |
| 226 | */ |
| 227 | static inline struct cgroup_subsys_state *blkcg_css(void) |
| 228 | { |
| 229 | struct cgroup_subsys_state *css; |
| 230 | |
| 231 | css = kthread_blkcg(); |
| 232 | if (css) |
| 233 | return css; |
| 234 | return task_css(current, io_cgrp_id); |
| 235 | } |
| 236 | |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 237 | static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css) |
| 238 | { |
| 239 | return css ? container_of(css, struct blkcg, css) : NULL; |
| 240 | } |
| 241 | |
Dennis Zhou | 0fe061b | 2018-12-05 12:10:26 -0500 | [diff] [blame] | 242 | /** |
| 243 | * __bio_blkcg - internal, inconsistent version to get blkcg |
| 244 | * |
| 245 | * DO NOT USE. |
| 246 | * This function is inconsistent and consequently is dangerous to use. The |
| 247 | * first part of the function returns a blkcg where a reference is owned by the |
| 248 | * bio. This means it does not need to be rcu protected as it cannot go away |
| 249 | * with the bio owning a reference to it. However, the latter potentially gets |
| 250 | * it from task_css(). This can race against task migration and the cgroup |
| 251 | * dying. It is also semantically different as it must be called rcu protected |
| 252 | * and is susceptible to failure when trying to get a reference to it. |
| 253 | * Therefore, it is not ok to assume that *_get() will always succeed on the |
| 254 | * blkcg returned here. |
| 255 | */ |
| 256 | static inline struct blkcg *__bio_blkcg(struct bio *bio) |
Dennis Zhou (Facebook) | 27e6fa9 | 2018-09-11 14:41:26 -0400 | [diff] [blame] | 257 | { |
Dennis Zhou | db6638d | 2018-12-05 12:10:35 -0500 | [diff] [blame] | 258 | if (bio && bio->bi_blkg) |
| 259 | return bio->bi_blkg->blkcg; |
Dennis Zhou | 0fe061b | 2018-12-05 12:10:26 -0500 | [diff] [blame] | 260 | return css_to_blkcg(blkcg_css()); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * bio_blkcg - grab the blkcg associated with a bio |
| 265 | * @bio: target bio |
| 266 | * |
| 267 | * This returns the blkcg associated with a bio, %NULL if not associated. |
| 268 | * Callers are expected to either handle %NULL or know association has been |
| 269 | * done prior to calling this. |
| 270 | */ |
| 271 | static inline struct blkcg *bio_blkcg(struct bio *bio) |
| 272 | { |
Dennis Zhou | db6638d | 2018-12-05 12:10:35 -0500 | [diff] [blame] | 273 | if (bio && bio->bi_blkg) |
| 274 | return bio->bi_blkg->blkcg; |
Dennis Zhou | 0fe061b | 2018-12-05 12:10:26 -0500 | [diff] [blame] | 275 | return NULL; |
Tejun Heo | fd383c2 | 2015-05-22 17:13:23 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 278 | static inline bool blk_cgroup_congested(void) |
| 279 | { |
| 280 | struct cgroup_subsys_state *css; |
| 281 | bool ret = false; |
| 282 | |
| 283 | rcu_read_lock(); |
| 284 | css = kthread_blkcg(); |
| 285 | if (!css) |
| 286 | css = task_css(current, io_cgrp_id); |
| 287 | while (css) { |
| 288 | if (atomic_read(&css->cgroup->congestion_count)) { |
| 289 | ret = true; |
| 290 | break; |
| 291 | } |
| 292 | css = css->parent; |
| 293 | } |
| 294 | rcu_read_unlock(); |
| 295 | return ret; |
| 296 | } |
| 297 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 298 | /** |
Josef Bacik | c7c98fd | 2018-07-03 11:14:51 -0400 | [diff] [blame] | 299 | * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg |
| 300 | * @return: true if this bio needs to be submitted with the root blkg context. |
| 301 | * |
| 302 | * In order to avoid priority inversions we sometimes need to issue a bio as if |
| 303 | * it were attached to the root blkg, and then backcharge to the actual owning |
| 304 | * blkg. The idea is we do bio_blkcg() to look up the actual context for the |
| 305 | * bio and attach the appropriate blkg to the bio. Then we call this helper and |
| 306 | * if it is true run with the root blkg for that queue and then do any |
| 307 | * backcharging to the originating cgroup once the io is complete. |
| 308 | */ |
| 309 | static inline bool bio_issue_as_root_blkg(struct bio *bio) |
| 310 | { |
Josef Bacik | 0d1e0c7 | 2018-07-03 11:14:53 -0400 | [diff] [blame] | 311 | return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0; |
Josef Bacik | c7c98fd | 2018-07-03 11:14:51 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 315 | * blkcg_parent - get the parent of a blkcg |
| 316 | * @blkcg: blkcg of interest |
| 317 | * |
| 318 | * Return the parent blkcg of @blkcg. Can be called anytime. |
| 319 | */ |
| 320 | static inline struct blkcg *blkcg_parent(struct blkcg *blkcg) |
| 321 | { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 322 | return css_to_blkcg(blkcg->css.parent); |
Tejun Heo | 3c54786 | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
Tejun Heo | 24f2904 | 2015-08-18 14:55:17 -0700 | [diff] [blame] | 326 | * __blkg_lookup - internal version of blkg_lookup() |
| 327 | * @blkcg: blkcg of interest |
| 328 | * @q: request_queue of interest |
| 329 | * @update_hint: whether to update lookup hint with the result or not |
| 330 | * |
| 331 | * This is internal version and shouldn't be used by policy |
| 332 | * implementations. Looks up blkgs for the @blkcg - @q pair regardless of |
| 333 | * @q's bypass state. If @update_hint is %true, the caller should be |
| 334 | * holding @q->queue_lock and lookup hint is updated on success. |
| 335 | */ |
| 336 | static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, |
| 337 | struct request_queue *q, |
| 338 | bool update_hint) |
| 339 | { |
| 340 | struct blkcg_gq *blkg; |
| 341 | |
Tejun Heo | 85b6bc9 | 2015-08-18 14:55:18 -0700 | [diff] [blame] | 342 | if (blkcg == &blkcg_root) |
| 343 | return q->root_blkg; |
| 344 | |
Tejun Heo | 24f2904 | 2015-08-18 14:55:17 -0700 | [diff] [blame] | 345 | blkg = rcu_dereference(blkcg->blkg_hint); |
| 346 | if (blkg && blkg->q == q) |
| 347 | return blkg; |
| 348 | |
| 349 | return blkg_lookup_slowpath(blkcg, q, update_hint); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * blkg_lookup - lookup blkg for the specified blkcg - q pair |
| 354 | * @blkcg: blkcg of interest |
| 355 | * @q: request_queue of interest |
| 356 | * |
| 357 | * Lookup blkg for the @blkcg - @q pair. This function should be called |
Bart Van Assche | 012d4a6 | 2019-08-01 15:39:07 -0700 | [diff] [blame] | 358 | * under RCU read lock. |
Tejun Heo | 24f2904 | 2015-08-18 14:55:17 -0700 | [diff] [blame] | 359 | */ |
| 360 | static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, |
| 361 | struct request_queue *q) |
| 362 | { |
| 363 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Tejun Heo | 24f2904 | 2015-08-18 14:55:17 -0700 | [diff] [blame] | 364 | return __blkg_lookup(blkcg, q, false); |
| 365 | } |
| 366 | |
| 367 | /** |
Bart Van Assche | b86d865 | 2018-08-10 13:28:07 -0700 | [diff] [blame] | 368 | * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair |
Bart Van Assche | 6bad9b2 | 2018-08-09 07:53:36 -0700 | [diff] [blame] | 369 | * @q: request_queue of interest |
| 370 | * |
| 371 | * Lookup blkg for @q at the root level. See also blkg_lookup(). |
| 372 | */ |
Bart Van Assche | b86d865 | 2018-08-10 13:28:07 -0700 | [diff] [blame] | 373 | static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) |
Bart Van Assche | 6bad9b2 | 2018-08-09 07:53:36 -0700 | [diff] [blame] | 374 | { |
Bart Van Assche | b86d865 | 2018-08-10 13:28:07 -0700 | [diff] [blame] | 375 | return q->root_blkg; |
Bart Van Assche | 6bad9b2 | 2018-08-09 07:53:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 379 | * blkg_to_pdata - get policy private data |
| 380 | * @blkg: blkg of interest |
| 381 | * @pol: policy of interest |
| 382 | * |
| 383 | * Return pointer to private data associated with the @blkg-@pol pair. |
| 384 | */ |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 385 | static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, |
| 386 | struct blkcg_policy *pol) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 387 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 388 | return blkg ? blkg->pd[pol->plid] : NULL; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 391 | static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg, |
| 392 | struct blkcg_policy *pol) |
| 393 | { |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 394 | return blkcg ? blkcg->cpd[pol->plid] : NULL; |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 397 | /** |
| 398 | * pdata_to_blkg - get blkg associated with policy private data |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 399 | * @pd: policy private data of interest |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 400 | * |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 401 | * @pd is policy private data. Determine the blkg it's associated with. |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 402 | */ |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 403 | static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 404 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 405 | return pd ? pd->blkg : NULL; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Tejun Heo | 8143764 | 2015-08-18 14:55:15 -0700 | [diff] [blame] | 408 | static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd) |
| 409 | { |
| 410 | return cpd ? cpd->blkcg : NULL; |
| 411 | } |
| 412 | |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 413 | extern void blkcg_destroy_blkgs(struct blkcg *blkcg); |
| 414 | |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 415 | /** |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 416 | * blkcg_pin_online - pin online state |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 417 | * @blkcg: blkcg of interest |
| 418 | * |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 419 | * While pinned, a blkcg is kept online. This is primarily used to |
| 420 | * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline |
| 421 | * while an associated cgwb is still active. |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 422 | */ |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 423 | static inline void blkcg_pin_online(struct blkcg *blkcg) |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 424 | { |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 425 | refcount_inc(&blkcg->online_pin); |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 429 | * blkcg_unpin_online - unpin online state |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 430 | * @blkcg: blkcg of interest |
| 431 | * |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 432 | * This is primarily used to impedance-match blkg and cgwb lifetimes so |
| 433 | * that blkg doesn't go offline while an associated cgwb is still active. |
| 434 | * When this count goes to zero, all active cgwbs have finished so the |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 435 | * blkcg can continue destruction by calling blkcg_destroy_blkgs(). |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 436 | */ |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 437 | static inline void blkcg_unpin_online(struct blkcg *blkcg) |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 438 | { |
Tejun Heo | d866dbf | 2019-07-24 10:37:22 -0700 | [diff] [blame^] | 439 | if (refcount_dec_and_test(&blkcg->online_pin)) |
Dennis Zhou (Facebook) | 59b5771 | 2018-08-31 16:22:43 -0400 | [diff] [blame] | 440 | blkcg_destroy_blkgs(blkcg); |
| 441 | } |
| 442 | |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 443 | /** |
| 444 | * blkg_path - format cgroup path of blkg |
| 445 | * @blkg: blkg of interest |
| 446 | * @buf: target buffer |
| 447 | * @buflen: target buffer length |
| 448 | * |
| 449 | * Format the path of the cgroup of @blkg into @buf. |
| 450 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 451 | static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen) |
Vivek Goyal | afc24d4 | 2010-04-26 19:27:56 +0200 | [diff] [blame] | 452 | { |
Tejun Heo | 4c737b4 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 453 | return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen); |
Vivek Goyal | afc24d4 | 2010-04-26 19:27:56 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 456 | /** |
| 457 | * blkg_get - get a blkg reference |
| 458 | * @blkg: blkg to get |
| 459 | * |
Tejun Heo | a5049a8 | 2014-06-19 17:42:57 -0400 | [diff] [blame] | 460 | * The caller should be holding an existing reference. |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 461 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 462 | static inline void blkg_get(struct blkcg_gq *blkg) |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 463 | { |
Dennis Zhou | 7fcf2b0 | 2018-12-05 12:10:38 -0500 | [diff] [blame] | 464 | percpu_ref_get(&blkg->refcnt); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 467 | /** |
Dennis Zhou | 7754f66 | 2018-12-05 12:10:39 -0500 | [diff] [blame] | 468 | * blkg_tryget - try and get a blkg reference |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 469 | * @blkg: blkg to get |
| 470 | * |
| 471 | * This is for use when doing an RCU lookup of the blkg. We may be in the midst |
| 472 | * of freeing this blkg, so we can only use it if the refcnt is not zero. |
| 473 | */ |
Dennis Zhou | 7754f66 | 2018-12-05 12:10:39 -0500 | [diff] [blame] | 474 | static inline bool blkg_tryget(struct blkcg_gq *blkg) |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 475 | { |
Dennis Zhou | 6ab2187 | 2018-12-19 16:43:21 -0600 | [diff] [blame] | 476 | return blkg && percpu_ref_tryget(&blkg->refcnt); |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Dennis Zhou | beea9da | 2018-12-05 12:10:28 -0500 | [diff] [blame] | 479 | /** |
Dennis Zhou | 7754f66 | 2018-12-05 12:10:39 -0500 | [diff] [blame] | 480 | * blkg_tryget_closest - try and get a blkg ref on the closet blkg |
Dennis Zhou | beea9da | 2018-12-05 12:10:28 -0500 | [diff] [blame] | 481 | * @blkg: blkg to get |
| 482 | * |
Dennis Zhou | 6ab2187 | 2018-12-19 16:43:21 -0600 | [diff] [blame] | 483 | * This needs to be called rcu protected. As the failure mode here is to walk |
| 484 | * up the blkg tree, this ensure that the blkg->parent pointers are always |
| 485 | * valid. This returns the blkg that it ended up taking a reference on or %NULL |
| 486 | * if no reference was taken. |
Dennis Zhou | beea9da | 2018-12-05 12:10:28 -0500 | [diff] [blame] | 487 | */ |
Dennis Zhou | 7754f66 | 2018-12-05 12:10:39 -0500 | [diff] [blame] | 488 | static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg) |
Dennis Zhou | beea9da | 2018-12-05 12:10:28 -0500 | [diff] [blame] | 489 | { |
Dennis Zhou | 6ab2187 | 2018-12-19 16:43:21 -0600 | [diff] [blame] | 490 | struct blkcg_gq *ret_blkg = NULL; |
Dennis Zhou | beea9da | 2018-12-05 12:10:28 -0500 | [diff] [blame] | 491 | |
Dennis Zhou | 6ab2187 | 2018-12-19 16:43:21 -0600 | [diff] [blame] | 492 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 493 | |
| 494 | while (blkg) { |
| 495 | if (blkg_tryget(blkg)) { |
| 496 | ret_blkg = blkg; |
| 497 | break; |
| 498 | } |
| 499 | blkg = blkg->parent; |
| 500 | } |
| 501 | |
| 502 | return ret_blkg; |
Dennis Zhou | beea9da | 2018-12-05 12:10:28 -0500 | [diff] [blame] | 503 | } |
Dennis Zhou (Facebook) | 07b05bc | 2018-09-11 14:41:28 -0400 | [diff] [blame] | 504 | |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 505 | /** |
| 506 | * blkg_put - put a blkg reference |
| 507 | * @blkg: blkg to put |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 508 | */ |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 509 | static inline void blkg_put(struct blkcg_gq *blkg) |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 510 | { |
Dennis Zhou | 7fcf2b0 | 2018-12-05 12:10:38 -0500 | [diff] [blame] | 511 | percpu_ref_put(&blkg->refcnt); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Tejun Heo | dd4a4ff | 2013-05-14 13:52:30 -0700 | [diff] [blame] | 514 | /** |
| 515 | * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants |
| 516 | * @d_blkg: loop cursor pointing to the current descendant |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 517 | * @pos_css: used for iteration |
Tejun Heo | dd4a4ff | 2013-05-14 13:52:30 -0700 | [diff] [blame] | 518 | * @p_blkg: target blkg to walk descendants of |
| 519 | * |
| 520 | * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU |
| 521 | * read locked. If called under either blkcg or queue lock, the iteration |
| 522 | * is guaranteed to include all and only online blkgs. The caller may |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 523 | * update @pos_css by calling css_rightmost_descendant() to skip subtree. |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 524 | * @p_blkg is included in the iteration and the first node to be visited. |
Tejun Heo | dd4a4ff | 2013-05-14 13:52:30 -0700 | [diff] [blame] | 525 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 526 | #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \ |
| 527 | css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \ |
| 528 | if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ |
Tejun Heo | dd4a4ff | 2013-05-14 13:52:30 -0700 | [diff] [blame] | 529 | (p_blkg)->q, false))) |
| 530 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 531 | /** |
Tejun Heo | aa539cb | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 532 | * blkg_for_each_descendant_post - post-order walk of a blkg's descendants |
| 533 | * @d_blkg: loop cursor pointing to the current descendant |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 534 | * @pos_css: used for iteration |
Tejun Heo | aa539cb | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 535 | * @p_blkg: target blkg to walk descendants of |
| 536 | * |
| 537 | * Similar to blkg_for_each_descendant_pre() but performs post-order |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 538 | * traversal instead. Synchronization rules are the same. @p_blkg is |
| 539 | * included in the iteration and the last node to be visited. |
Tejun Heo | aa539cb | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 540 | */ |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 541 | #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \ |
| 542 | css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \ |
| 543 | if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \ |
Tejun Heo | aa539cb | 2013-05-14 13:52:31 -0700 | [diff] [blame] | 544 | (p_blkg)->q, false))) |
| 545 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 546 | #ifdef CONFIG_BLK_DEV_THROTTLING |
| 547 | extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, |
| 548 | struct bio *bio); |
| 549 | #else |
| 550 | static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg, |
| 551 | struct bio *bio) { return false; } |
| 552 | #endif |
| 553 | |
Tejun Heo | d3f77df | 2019-06-27 13:39:52 -0700 | [diff] [blame] | 554 | bool __blkcg_punt_bio_submit(struct bio *bio); |
| 555 | |
| 556 | static inline bool blkcg_punt_bio_submit(struct bio *bio) |
| 557 | { |
| 558 | if (bio->bi_opf & REQ_CGROUP_PUNT) |
| 559 | return __blkcg_punt_bio_submit(bio); |
| 560 | else |
| 561 | return false; |
| 562 | } |
Dennis Zhou | e439bed | 2018-12-05 12:10:32 -0500 | [diff] [blame] | 563 | |
| 564 | static inline void blkcg_bio_issue_init(struct bio *bio) |
| 565 | { |
| 566 | bio_issue_init(&bio->bi_issue, bio_sectors(bio)); |
| 567 | } |
| 568 | |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 569 | static inline bool blkcg_bio_issue_check(struct request_queue *q, |
| 570 | struct bio *bio) |
| 571 | { |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 572 | struct blkcg_gq *blkg; |
| 573 | bool throtl = false; |
| 574 | |
Dennis Zhou | 4705de7 | 2018-12-06 12:49:38 -0500 | [diff] [blame] | 575 | rcu_read_lock(); |
| 576 | |
Dennis Zhou | 5cdf2e3 | 2018-12-05 12:10:31 -0500 | [diff] [blame] | 577 | if (!bio->bi_blkg) { |
| 578 | char b[BDEVNAME_SIZE]; |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 579 | |
Dennis Zhou | 5cdf2e3 | 2018-12-05 12:10:31 -0500 | [diff] [blame] | 580 | WARN_ONCE(1, |
| 581 | "no blkg associated for bio on block-device: %s\n", |
| 582 | bio_devname(bio, b)); |
| 583 | bio_associate_blkg(bio); |
| 584 | } |
| 585 | |
| 586 | blkg = bio->bi_blkg; |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 587 | |
| 588 | throtl = blk_throtl_bio(q, blkg, bio); |
| 589 | |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 590 | if (!throtl) { |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 591 | struct blkg_iostat_set *bis; |
| 592 | int rwd, cpu; |
| 593 | |
| 594 | if (op_is_discard(bio->bi_opf)) |
| 595 | rwd = BLKG_IOSTAT_DISCARD; |
| 596 | else if (op_is_write(bio->bi_opf)) |
| 597 | rwd = BLKG_IOSTAT_WRITE; |
| 598 | else |
| 599 | rwd = BLKG_IOSTAT_READ; |
| 600 | |
| 601 | cpu = get_cpu(); |
| 602 | bis = per_cpu_ptr(blkg->iostat_cpu, cpu); |
| 603 | u64_stats_update_begin(&bis->sync); |
| 604 | |
Josef Bacik | c454edc | 2018-07-30 10:10:01 -0400 | [diff] [blame] | 605 | /* |
| 606 | * If the bio is flagged with BIO_QUEUE_ENTERED it means this |
| 607 | * is a split bio and we would have already accounted for the |
| 608 | * size of the bio. |
| 609 | */ |
| 610 | if (!bio_flagged(bio, BIO_QUEUE_ENTERED)) |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 611 | bis->cur.bytes[rwd] += bio->bi_iter.bi_size; |
| 612 | bis->cur.ios[rwd]++; |
| 613 | |
| 614 | u64_stats_update_end(&bis->sync); |
Tejun Heo | 496074f | 2019-11-14 14:31:28 -0800 | [diff] [blame] | 615 | if (cgroup_subsys_on_dfl(io_cgrp_subsys)) |
| 616 | cgroup_rstat_updated(blkg->blkcg->css.cgroup, cpu); |
Tejun Heo | f733164 | 2019-11-07 11:18:03 -0800 | [diff] [blame] | 617 | put_cpu(); |
Tejun Heo | 77ea733 | 2015-08-18 14:55:24 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Dennis Zhou | e439bed | 2018-12-05 12:10:32 -0500 | [diff] [blame] | 620 | blkcg_bio_issue_init(bio); |
| 621 | |
Dennis Zhou | 4705de7 | 2018-12-06 12:49:38 -0500 | [diff] [blame] | 622 | rcu_read_unlock(); |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 623 | return !throtl; |
| 624 | } |
| 625 | |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 626 | static inline void blkcg_use_delay(struct blkcg_gq *blkg) |
| 627 | { |
| 628 | if (atomic_add_return(1, &blkg->use_delay) == 1) |
| 629 | atomic_inc(&blkg->blkcg->css.cgroup->congestion_count); |
| 630 | } |
| 631 | |
| 632 | static inline int blkcg_unuse_delay(struct blkcg_gq *blkg) |
| 633 | { |
| 634 | int old = atomic_read(&blkg->use_delay); |
| 635 | |
| 636 | if (old == 0) |
| 637 | return 0; |
| 638 | |
| 639 | /* |
| 640 | * We do this song and dance because we can race with somebody else |
| 641 | * adding or removing delay. If we just did an atomic_dec we'd end up |
| 642 | * negative and we'd already be in trouble. We need to subtract 1 and |
| 643 | * then check to see if we were the last delay so we can drop the |
| 644 | * congestion count on the cgroup. |
| 645 | */ |
| 646 | while (old) { |
| 647 | int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1); |
| 648 | if (cur == old) |
| 649 | break; |
| 650 | old = cur; |
| 651 | } |
| 652 | |
| 653 | if (old == 0) |
| 654 | return 0; |
| 655 | if (old == 1) |
| 656 | atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | static inline void blkcg_clear_delay(struct blkcg_gq *blkg) |
| 661 | { |
| 662 | int old = atomic_read(&blkg->use_delay); |
| 663 | if (!old) |
| 664 | return; |
| 665 | /* We only want 1 person clearing the congestion count for this blkg. */ |
| 666 | while (old) { |
| 667 | int cur = atomic_cmpxchg(&blkg->use_delay, old, 0); |
| 668 | if (cur == old) { |
| 669 | atomic_dec(&blkg->blkcg->css.cgroup->congestion_count); |
| 670 | break; |
| 671 | } |
| 672 | old = cur; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta); |
| 677 | void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay); |
| 678 | void blkcg_maybe_throttle_current(void); |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 679 | #else /* CONFIG_BLK_CGROUP */ |
| 680 | |
Tejun Heo | efa7d1c | 2015-05-22 17:13:18 -0400 | [diff] [blame] | 681 | struct blkcg { |
| 682 | }; |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 683 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 684 | struct blkg_policy_data { |
| 685 | }; |
| 686 | |
Arianna Avanzini | e48453c | 2015-06-05 23:38:42 +0200 | [diff] [blame] | 687 | struct blkcg_policy_data { |
| 688 | }; |
| 689 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 690 | struct blkcg_gq { |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 691 | }; |
| 692 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 693 | struct blkcg_policy { |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 694 | }; |
| 695 | |
Tejun Heo | 496d5e7 | 2015-05-22 17:13:21 -0400 | [diff] [blame] | 696 | #define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL)) |
| 697 | |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 698 | static inline void blkcg_maybe_throttle_current(void) { } |
| 699 | static inline bool blk_cgroup_congested(void) { return false; } |
| 700 | |
Tejun Heo | efa7d1c | 2015-05-22 17:13:18 -0400 | [diff] [blame] | 701 | #ifdef CONFIG_BLOCK |
| 702 | |
Josef Bacik | d09d8df | 2018-07-03 11:14:55 -0400 | [diff] [blame] | 703 | static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { } |
| 704 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 705 | static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; } |
Bart Van Assche | b86d865 | 2018-08-10 13:28:07 -0700 | [diff] [blame] | 706 | static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q) |
| 707 | { return NULL; } |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 708 | static inline int blkcg_init_queue(struct request_queue *q) { return 0; } |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 709 | static inline void blkcg_exit_queue(struct request_queue *q) { } |
Jens Axboe | d5bf029 | 2014-06-22 16:31:56 -0600 | [diff] [blame] | 710 | static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; } |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 711 | static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { } |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 712 | static inline int blkcg_activate_policy(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 713 | const struct blkcg_policy *pol) { return 0; } |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 714 | static inline void blkcg_deactivate_policy(struct request_queue *q, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 715 | const struct blkcg_policy *pol) { } |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 716 | |
Dennis Zhou | 0fe061b | 2018-12-05 12:10:26 -0500 | [diff] [blame] | 717 | static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; } |
Tejun Heo | b1208b5 | 2012-06-04 20:40:57 -0700 | [diff] [blame] | 718 | static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; } |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 719 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 720 | static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg, |
| 721 | struct blkcg_policy *pol) { return NULL; } |
| 722 | static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; } |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 723 | static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; } |
| 724 | static inline void blkg_get(struct blkcg_gq *blkg) { } |
| 725 | static inline void blkg_put(struct blkcg_gq *blkg) { } |
Vivek Goyal | afc24d4 | 2010-04-26 19:27:56 +0200 | [diff] [blame] | 726 | |
Tejun Heo | d3f77df | 2019-06-27 13:39:52 -0700 | [diff] [blame] | 727 | static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; } |
Dennis Zhou | e439bed | 2018-12-05 12:10:32 -0500 | [diff] [blame] | 728 | static inline void blkcg_bio_issue_init(struct bio *bio) { } |
Tejun Heo | ae11889 | 2015-08-18 14:55:20 -0700 | [diff] [blame] | 729 | static inline bool blkcg_bio_issue_check(struct request_queue *q, |
| 730 | struct bio *bio) { return true; } |
| 731 | |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 732 | #define blk_queue_for_each_rl(rl, q) \ |
| 733 | for ((rl) = &(q)->root_rl; (rl); (rl) = NULL) |
| 734 | |
Tejun Heo | efa7d1c | 2015-05-22 17:13:18 -0400 | [diff] [blame] | 735 | #endif /* CONFIG_BLOCK */ |
Tejun Heo | 36558c8 | 2012-04-16 13:57:24 -0700 | [diff] [blame] | 736 | #endif /* CONFIG_BLK_CGROUP */ |
| 737 | #endif /* _BLK_CGROUP_H */ |