Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 1 | #ifndef _BLK_CGROUP_H |
| 2 | #define _BLK_CGROUP_H |
| 3 | /* |
| 4 | * Common Block IO controller cgroup interface |
| 5 | * |
| 6 | * Based on ideas and code from CFQ, CFS and BFQ: |
| 7 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> |
| 8 | * |
| 9 | * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it> |
| 10 | * Paolo Valente <paolo.valente@unimore.it> |
| 11 | * |
| 12 | * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com> |
| 13 | * Nauman Rafique <nauman@google.com> |
| 14 | */ |
| 15 | |
| 16 | #include <linux/cgroup.h> |
Vivek Goyal | 575969a | 2011-05-19 15:38:29 -0400 | [diff] [blame] | 17 | #include <linux/u64_stats_sync.h> |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 18 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 19 | enum blkio_policy_id { |
| 20 | BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */ |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 21 | BLKIO_POLICY_THROTL, /* Throttling */ |
Tejun Heo | 035d10b | 2012-03-05 13:15:04 -0800 | [diff] [blame] | 22 | |
| 23 | BLKIO_NR_POLICIES, |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 24 | }; |
| 25 | |
Vivek Goyal | 9355aed | 2010-10-01 21:16:41 +0200 | [diff] [blame] | 26 | /* Max limits for throttle policy */ |
| 27 | #define THROTL_IOPS_MAX UINT_MAX |
| 28 | |
Tejun Heo | 32e380a | 2012-03-05 13:14:54 -0800 | [diff] [blame] | 29 | #ifdef CONFIG_BLK_CGROUP |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 30 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 31 | enum stat_type { |
Tejun Heo | 5fe224d | 2012-03-08 10:53:57 -0800 | [diff] [blame] | 32 | /* Number of IOs merged */ |
| 33 | BLKIO_STAT_MERGED, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 34 | /* Total time spent (in ns) between request dispatch to the driver and |
| 35 | * request completion for IOs doen by this cgroup. This may not be |
| 36 | * accurate when NCQ is turned on. */ |
Tejun Heo | 5fe224d | 2012-03-08 10:53:57 -0800 | [diff] [blame] | 37 | BLKIO_STAT_SERVICE_TIME, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 38 | /* Total time spent waiting in scheduler queue in ns */ |
| 39 | BLKIO_STAT_WAIT_TIME, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 40 | /* Number of IOs queued up */ |
| 41 | BLKIO_STAT_QUEUED, |
Tejun Heo | c4c76a0 | 2012-03-08 10:53:59 -0800 | [diff] [blame] | 42 | |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 43 | /* All the single valued stats go below this */ |
| 44 | BLKIO_STAT_TIME, |
Vivek Goyal | a23e686 | 2011-05-19 15:38:20 -0400 | [diff] [blame] | 45 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 46 | /* Time not charged to this cgroup */ |
| 47 | BLKIO_STAT_UNACCOUNTED_TIME, |
Divyesh Shah | cdc1184 | 2010-04-08 21:15:10 -0700 | [diff] [blame] | 48 | BLKIO_STAT_AVG_QUEUE_SIZE, |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 49 | BLKIO_STAT_IDLE_TIME, |
| 50 | BLKIO_STAT_EMPTY_TIME, |
| 51 | BLKIO_STAT_GROUP_WAIT_TIME, |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 52 | BLKIO_STAT_DEQUEUE |
| 53 | #endif |
| 54 | }; |
| 55 | |
Tejun Heo | c4c76a0 | 2012-03-08 10:53:59 -0800 | [diff] [blame] | 56 | /* Types lower than this live in stat_arr and have subtypes */ |
| 57 | #define BLKIO_STAT_ARR_NR (BLKIO_STAT_QUEUED + 1) |
| 58 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 59 | /* Per cpu stats */ |
| 60 | enum stat_type_cpu { |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 61 | /* Total bytes transferred */ |
| 62 | BLKIO_STAT_CPU_SERVICE_BYTES, |
| 63 | /* Total IOs serviced, post merge */ |
| 64 | BLKIO_STAT_CPU_SERVICED, |
Tejun Heo | 2aa4a15 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 65 | |
| 66 | /* All the single valued stats go below this */ |
| 67 | BLKIO_STAT_CPU_SECTORS, |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 68 | }; |
| 69 | |
Tejun Heo | 2aa4a15 | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 70 | #define BLKIO_STAT_CPU_ARR_NR (BLKIO_STAT_CPU_SERVICED + 1) |
| 71 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 72 | enum blkg_rwstat_type { |
| 73 | BLKG_RWSTAT_READ, |
| 74 | BLKG_RWSTAT_WRITE, |
| 75 | BLKG_RWSTAT_SYNC, |
| 76 | BLKG_RWSTAT_ASYNC, |
| 77 | |
| 78 | BLKG_RWSTAT_NR, |
| 79 | BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR, |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 82 | /* blkg state flags */ |
| 83 | enum blkg_state_flags { |
| 84 | BLKG_waiting = 0, |
| 85 | BLKG_idling, |
| 86 | BLKG_empty, |
| 87 | }; |
| 88 | |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 89 | /* cgroup files owned by proportional weight policy */ |
| 90 | enum blkcg_file_name_prop { |
| 91 | BLKIO_PROP_weight = 1, |
| 92 | BLKIO_PROP_weight_device, |
| 93 | BLKIO_PROP_io_service_bytes, |
| 94 | BLKIO_PROP_io_serviced, |
| 95 | BLKIO_PROP_time, |
| 96 | BLKIO_PROP_sectors, |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 97 | BLKIO_PROP_unaccounted_time, |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 98 | BLKIO_PROP_io_service_time, |
| 99 | BLKIO_PROP_io_wait_time, |
| 100 | BLKIO_PROP_io_merged, |
| 101 | BLKIO_PROP_io_queued, |
| 102 | BLKIO_PROP_avg_queue_size, |
| 103 | BLKIO_PROP_group_wait_time, |
| 104 | BLKIO_PROP_idle_time, |
| 105 | BLKIO_PROP_empty_time, |
| 106 | BLKIO_PROP_dequeue, |
| 107 | }; |
| 108 | |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 109 | /* cgroup files owned by throttle policy */ |
| 110 | enum blkcg_file_name_throtl { |
| 111 | BLKIO_THROTL_read_bps_device, |
| 112 | BLKIO_THROTL_write_bps_device, |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 113 | BLKIO_THROTL_read_iops_device, |
| 114 | BLKIO_THROTL_write_iops_device, |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 115 | BLKIO_THROTL_io_service_bytes, |
| 116 | BLKIO_THROTL_io_serviced, |
| 117 | }; |
| 118 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 119 | struct blkio_cgroup { |
| 120 | struct cgroup_subsys_state css; |
| 121 | unsigned int weight; |
| 122 | spinlock_t lock; |
| 123 | struct hlist_head blkg_list; |
Tejun Heo | 9a9e8a2 | 2012-03-19 15:10:56 -0700 | [diff] [blame] | 124 | |
| 125 | /* for policies to test whether associated blkcg has changed */ |
| 126 | uint64_t id; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 129 | struct blkg_stat { |
| 130 | struct u64_stats_sync syncp; |
| 131 | uint64_t cnt; |
| 132 | }; |
| 133 | |
| 134 | struct blkg_rwstat { |
| 135 | struct u64_stats_sync syncp; |
| 136 | uint64_t cnt[BLKG_RWSTAT_NR]; |
| 137 | }; |
| 138 | |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 139 | struct blkio_group_stats { |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 140 | /* number of ios merged */ |
| 141 | struct blkg_rwstat merged; |
| 142 | /* total time spent on device in ns, may not be accurate w/ queueing */ |
| 143 | struct blkg_rwstat service_time; |
| 144 | /* total time spent waiting in scheduler queue in ns */ |
| 145 | struct blkg_rwstat wait_time; |
| 146 | /* number of IOs queued up */ |
| 147 | struct blkg_rwstat queued; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 148 | /* total disk time and nr sectors dispatched by this group */ |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 149 | struct blkg_stat time; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 150 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 151 | /* time not charged to this cgroup */ |
| 152 | struct blkg_stat unaccounted_time; |
| 153 | /* sum of number of ios queued across all samples */ |
| 154 | struct blkg_stat avg_queue_size_sum; |
| 155 | /* count of samples taken for average */ |
| 156 | struct blkg_stat avg_queue_size_samples; |
| 157 | /* how many times this group has been removed from service tree */ |
| 158 | struct blkg_stat dequeue; |
| 159 | /* total time spent waiting for it to be assigned a timeslice. */ |
| 160 | struct blkg_stat group_wait_time; |
| 161 | /* time spent idling for this blkio_group */ |
| 162 | struct blkg_stat idle_time; |
| 163 | /* total time with empty current active q with other requests queued */ |
| 164 | struct blkg_stat empty_time; |
Tejun Heo | 997a026 | 2012-03-08 10:53:58 -0800 | [diff] [blame] | 165 | /* fields after this shouldn't be cleared on stat reset */ |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 166 | uint64_t start_group_wait_time; |
| 167 | uint64_t start_idle_time; |
| 168 | uint64_t start_empty_time; |
| 169 | uint16_t flags; |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 170 | #endif |
| 171 | }; |
| 172 | |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 173 | /* Per cpu blkio group stats */ |
| 174 | struct blkio_group_stats_cpu { |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 175 | /* total bytes transferred */ |
| 176 | struct blkg_rwstat service_bytes; |
| 177 | /* total IOs serviced, post merge */ |
| 178 | struct blkg_rwstat serviced; |
| 179 | /* total sectors transferred */ |
| 180 | struct blkg_stat sectors; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 181 | }; |
| 182 | |
Tejun Heo | e56da7e | 2012-03-05 13:15:07 -0800 | [diff] [blame] | 183 | struct blkio_group_conf { |
| 184 | unsigned int weight; |
| 185 | unsigned int iops[2]; |
| 186 | u64 bps[2]; |
| 187 | }; |
| 188 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 189 | /* per-blkg per-policy data */ |
| 190 | struct blkg_policy_data { |
| 191 | /* the blkg this per-policy data belongs to */ |
| 192 | struct blkio_group *blkg; |
| 193 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 194 | /* Configuration */ |
| 195 | struct blkio_group_conf conf; |
| 196 | |
| 197 | struct blkio_group_stats stats; |
| 198 | /* Per cpu stats pointer */ |
| 199 | struct blkio_group_stats_cpu __percpu *stats_cpu; |
| 200 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 201 | /* pol->pdata_size bytes of private data used by policy impl */ |
| 202 | char pdata[] __aligned(__alignof__(unsigned long long)); |
| 203 | }; |
| 204 | |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 205 | struct blkio_group { |
Tejun Heo | c875f4d | 2012-03-05 13:15:22 -0800 | [diff] [blame] | 206 | /* Pointer to the associated request_queue */ |
| 207 | struct request_queue *q; |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 208 | struct list_head q_node; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 209 | struct hlist_node blkcg_node; |
Tejun Heo | 7ee9c56 | 2012-03-05 13:15:11 -0800 | [diff] [blame] | 210 | struct blkio_cgroup *blkcg; |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 211 | /* Store cgroup path */ |
| 212 | char path[128]; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 213 | /* reference count */ |
| 214 | int refcnt; |
Vivek Goyal | 2208419 | 2009-12-03 12:59:49 -0500 | [diff] [blame] | 215 | |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 216 | struct blkg_policy_data *pd[BLKIO_NR_POLICIES]; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 217 | |
Vivek Goyal | 1cd9e03 | 2012-03-08 10:53:56 -0800 | [diff] [blame] | 218 | /* List of blkg waiting for per cpu stats memory to be allocated */ |
| 219 | struct list_head alloc_node; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 220 | struct rcu_head rcu_head; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 221 | }; |
| 222 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 223 | typedef void (blkio_init_group_fn)(struct blkio_group *blkg); |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 224 | typedef void (blkio_update_group_weight_fn)(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 225 | struct blkio_group *blkg, unsigned int weight); |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 226 | typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 227 | struct blkio_group *blkg, u64 read_bps); |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 228 | typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 229 | struct blkio_group *blkg, u64 write_bps); |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 230 | typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 231 | struct blkio_group *blkg, unsigned int read_iops); |
Tejun Heo | ca32aef | 2012-03-05 13:15:03 -0800 | [diff] [blame] | 232 | typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q, |
Vivek Goyal | fe07143 | 2010-10-01 14:49:49 +0200 | [diff] [blame] | 233 | struct blkio_group *blkg, unsigned int write_iops); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 234 | |
| 235 | struct blkio_policy_ops { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 236 | blkio_init_group_fn *blkio_init_group_fn; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 237 | blkio_update_group_weight_fn *blkio_update_group_weight_fn; |
Vivek Goyal | 4c9eefa | 2010-09-15 17:06:34 -0400 | [diff] [blame] | 238 | blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn; |
| 239 | blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn; |
Vivek Goyal | 7702e8f | 2010-09-15 17:06:36 -0400 | [diff] [blame] | 240 | blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn; |
| 241 | blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | struct blkio_policy_type { |
| 245 | struct list_head list; |
| 246 | struct blkio_policy_ops ops; |
Vivek Goyal | 062a644 | 2010-09-15 17:06:33 -0400 | [diff] [blame] | 247 | enum blkio_policy_id plid; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 248 | size_t pdata_size; /* policy specific private data size */ |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 249 | }; |
| 250 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 251 | extern int blkcg_init_queue(struct request_queue *q); |
| 252 | extern void blkcg_drain_queue(struct request_queue *q); |
| 253 | extern void blkcg_exit_queue(struct request_queue *q); |
| 254 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 255 | /* Blkio controller policy registration */ |
| 256 | extern void blkio_policy_register(struct blkio_policy_type *); |
| 257 | extern void blkio_policy_unregister(struct blkio_policy_type *); |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 258 | extern void blkg_destroy_all(struct request_queue *q, bool destroy_root); |
| 259 | extern void update_root_blkg_pd(struct request_queue *q, |
| 260 | enum blkio_policy_id plid); |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 261 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 262 | /** |
| 263 | * blkg_to_pdata - get policy private data |
| 264 | * @blkg: blkg of interest |
| 265 | * @pol: policy of interest |
| 266 | * |
| 267 | * Return pointer to private data associated with the @blkg-@pol pair. |
| 268 | */ |
| 269 | static inline void *blkg_to_pdata(struct blkio_group *blkg, |
| 270 | struct blkio_policy_type *pol) |
| 271 | { |
Tejun Heo | 549d3aa | 2012-03-05 13:15:16 -0800 | [diff] [blame] | 272 | return blkg ? blkg->pd[pol->plid]->pdata : NULL; |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * pdata_to_blkg - get blkg associated with policy private data |
| 277 | * @pdata: policy private data of interest |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 278 | * |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 279 | * @pdata is policy private data. Determine the blkg it's associated with. |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 280 | */ |
Tejun Heo | aaec55a | 2012-04-01 14:38:42 -0700 | [diff] [blame] | 281 | static inline struct blkio_group *pdata_to_blkg(void *pdata) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 282 | { |
| 283 | if (pdata) { |
| 284 | struct blkg_policy_data *pd = |
| 285 | container_of(pdata, struct blkg_policy_data, pdata); |
| 286 | return pd->blkg; |
| 287 | } |
| 288 | return NULL; |
| 289 | } |
| 290 | |
Vivek Goyal | afc24d4 | 2010-04-26 19:27:56 +0200 | [diff] [blame] | 291 | static inline char *blkg_path(struct blkio_group *blkg) |
| 292 | { |
| 293 | return blkg->path; |
| 294 | } |
| 295 | |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 296 | /** |
| 297 | * blkg_get - get a blkg reference |
| 298 | * @blkg: blkg to get |
| 299 | * |
| 300 | * The caller should be holding queue_lock and an existing reference. |
| 301 | */ |
| 302 | static inline void blkg_get(struct blkio_group *blkg) |
| 303 | { |
| 304 | lockdep_assert_held(blkg->q->queue_lock); |
| 305 | WARN_ON_ONCE(!blkg->refcnt); |
| 306 | blkg->refcnt++; |
| 307 | } |
| 308 | |
| 309 | void __blkg_release(struct blkio_group *blkg); |
| 310 | |
| 311 | /** |
| 312 | * blkg_put - put a blkg reference |
| 313 | * @blkg: blkg to put |
| 314 | * |
| 315 | * The caller should be holding queue_lock. |
| 316 | */ |
| 317 | static inline void blkg_put(struct blkio_group *blkg) |
| 318 | { |
| 319 | lockdep_assert_held(blkg->q->queue_lock); |
| 320 | WARN_ON_ONCE(blkg->refcnt <= 0); |
| 321 | if (!--blkg->refcnt) |
| 322 | __blkg_release(blkg); |
| 323 | } |
| 324 | |
Tejun Heo | edcb072 | 2012-04-01 14:38:42 -0700 | [diff] [blame^] | 325 | /** |
| 326 | * blkg_stat_add - add a value to a blkg_stat |
| 327 | * @stat: target blkg_stat |
| 328 | * @val: value to add |
| 329 | * |
| 330 | * Add @val to @stat. The caller is responsible for synchronizing calls to |
| 331 | * this function. |
| 332 | */ |
| 333 | static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val) |
| 334 | { |
| 335 | u64_stats_update_begin(&stat->syncp); |
| 336 | stat->cnt += val; |
| 337 | u64_stats_update_end(&stat->syncp); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * blkg_stat_read - read the current value of a blkg_stat |
| 342 | * @stat: blkg_stat to read |
| 343 | * |
| 344 | * Read the current value of @stat. This function can be called without |
| 345 | * synchroniztion and takes care of u64 atomicity. |
| 346 | */ |
| 347 | static inline uint64_t blkg_stat_read(struct blkg_stat *stat) |
| 348 | { |
| 349 | unsigned int start; |
| 350 | uint64_t v; |
| 351 | |
| 352 | do { |
| 353 | start = u64_stats_fetch_begin(&stat->syncp); |
| 354 | v = stat->cnt; |
| 355 | } while (u64_stats_fetch_retry(&stat->syncp, start)); |
| 356 | |
| 357 | return v; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * blkg_stat_reset - reset a blkg_stat |
| 362 | * @stat: blkg_stat to reset |
| 363 | */ |
| 364 | static inline void blkg_stat_reset(struct blkg_stat *stat) |
| 365 | { |
| 366 | stat->cnt = 0; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * blkg_rwstat_add - add a value to a blkg_rwstat |
| 371 | * @rwstat: target blkg_rwstat |
| 372 | * @rw: mask of REQ_{WRITE|SYNC} |
| 373 | * @val: value to add |
| 374 | * |
| 375 | * Add @val to @rwstat. The counters are chosen according to @rw. The |
| 376 | * caller is responsible for synchronizing calls to this function. |
| 377 | */ |
| 378 | static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat, |
| 379 | int rw, uint64_t val) |
| 380 | { |
| 381 | u64_stats_update_begin(&rwstat->syncp); |
| 382 | |
| 383 | if (rw & REQ_WRITE) |
| 384 | rwstat->cnt[BLKG_RWSTAT_WRITE] += val; |
| 385 | else |
| 386 | rwstat->cnt[BLKG_RWSTAT_READ] += val; |
| 387 | if (rw & REQ_SYNC) |
| 388 | rwstat->cnt[BLKG_RWSTAT_SYNC] += val; |
| 389 | else |
| 390 | rwstat->cnt[BLKG_RWSTAT_ASYNC] += val; |
| 391 | |
| 392 | u64_stats_update_end(&rwstat->syncp); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * blkg_rwstat_read - read the current values of a blkg_rwstat |
| 397 | * @rwstat: blkg_rwstat to read |
| 398 | * |
| 399 | * Read the current snapshot of @rwstat and return it as the return value. |
| 400 | * This function can be called without synchronization and takes care of |
| 401 | * u64 atomicity. |
| 402 | */ |
| 403 | static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat) |
| 404 | { |
| 405 | unsigned int start; |
| 406 | struct blkg_rwstat tmp; |
| 407 | |
| 408 | do { |
| 409 | start = u64_stats_fetch_begin(&rwstat->syncp); |
| 410 | tmp = *rwstat; |
| 411 | } while (u64_stats_fetch_retry(&rwstat->syncp, start)); |
| 412 | |
| 413 | return tmp; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * blkg_rwstat_sum - read the total count of a blkg_rwstat |
| 418 | * @rwstat: blkg_rwstat to read |
| 419 | * |
| 420 | * Return the total count of @rwstat regardless of the IO direction. This |
| 421 | * function can be called without synchronization and takes care of u64 |
| 422 | * atomicity. |
| 423 | */ |
| 424 | static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat) |
| 425 | { |
| 426 | struct blkg_rwstat tmp = blkg_rwstat_read(rwstat); |
| 427 | |
| 428 | return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE]; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * blkg_rwstat_reset - reset a blkg_rwstat |
| 433 | * @rwstat: blkg_rwstat to reset |
| 434 | */ |
| 435 | static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat) |
| 436 | { |
| 437 | memset(rwstat->cnt, 0, sizeof(rwstat->cnt)); |
| 438 | } |
| 439 | |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 440 | #else |
| 441 | |
| 442 | struct blkio_group { |
| 443 | }; |
| 444 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 445 | struct blkio_policy_type { |
| 446 | }; |
| 447 | |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 448 | static inline int blkcg_init_queue(struct request_queue *q) { return 0; } |
| 449 | static inline void blkcg_drain_queue(struct request_queue *q) { } |
| 450 | static inline void blkcg_exit_queue(struct request_queue *q) { } |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 451 | static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { } |
| 452 | static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { } |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 453 | static inline void blkg_destroy_all(struct request_queue *q, |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 454 | bool destory_root) { } |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 455 | static inline void update_root_blkg_pd(struct request_queue *q, |
| 456 | enum blkio_policy_id plid) { } |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 457 | |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 458 | static inline void *blkg_to_pdata(struct blkio_group *blkg, |
| 459 | struct blkio_policy_type *pol) { return NULL; } |
| 460 | static inline struct blkio_group *pdata_to_blkg(void *pdata, |
| 461 | struct blkio_policy_type *pol) { return NULL; } |
Vivek Goyal | afc24d4 | 2010-04-26 19:27:56 +0200 | [diff] [blame] | 462 | static inline char *blkg_path(struct blkio_group *blkg) { return NULL; } |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 463 | static inline void blkg_get(struct blkio_group *blkg) { } |
| 464 | static inline void blkg_put(struct blkio_group *blkg) { } |
Vivek Goyal | afc24d4 | 2010-04-26 19:27:56 +0200 | [diff] [blame] | 465 | |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 466 | #endif |
| 467 | |
Justin TerAvest | df457f8 | 2011-03-08 19:45:00 +0100 | [diff] [blame] | 468 | #define BLKIO_WEIGHT_MIN 10 |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 469 | #define BLKIO_WEIGHT_MAX 1000 |
| 470 | #define BLKIO_WEIGHT_DEFAULT 500 |
| 471 | |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 472 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 473 | void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg, |
| 474 | struct blkio_policy_type *pol); |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 475 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 476 | struct blkio_policy_type *pol, |
| 477 | unsigned long dequeue); |
| 478 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg, |
| 479 | struct blkio_policy_type *pol); |
| 480 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg, |
| 481 | struct blkio_policy_type *pol); |
| 482 | void blkiocg_set_start_empty_time(struct blkio_group *blkg, |
| 483 | struct blkio_policy_type *pol); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 484 | |
| 485 | #define BLKG_FLAG_FNS(name) \ |
| 486 | static inline void blkio_mark_blkg_##name( \ |
| 487 | struct blkio_group_stats *stats) \ |
| 488 | { \ |
| 489 | stats->flags |= (1 << BLKG_##name); \ |
| 490 | } \ |
| 491 | static inline void blkio_clear_blkg_##name( \ |
| 492 | struct blkio_group_stats *stats) \ |
| 493 | { \ |
| 494 | stats->flags &= ~(1 << BLKG_##name); \ |
| 495 | } \ |
| 496 | static inline int blkio_blkg_##name(struct blkio_group_stats *stats) \ |
| 497 | { \ |
| 498 | return (stats->flags & (1 << BLKG_##name)) != 0; \ |
| 499 | } \ |
| 500 | |
| 501 | BLKG_FLAG_FNS(waiting) |
| 502 | BLKG_FLAG_FNS(idling) |
| 503 | BLKG_FLAG_FNS(empty) |
| 504 | #undef BLKG_FLAG_FNS |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 505 | #else |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 506 | static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg, |
| 507 | struct blkio_policy_type *pol) { } |
Divyesh Shah | 9195291 | 2010-04-01 15:01:41 -0700 | [diff] [blame] | 508 | static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 509 | struct blkio_policy_type *pol, unsigned long dequeue) { } |
| 510 | static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg, |
| 511 | struct blkio_policy_type *pol) { } |
| 512 | static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg, |
| 513 | struct blkio_policy_type *pol) { } |
| 514 | static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg, |
| 515 | struct blkio_policy_type *pol) { } |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 516 | #endif |
| 517 | |
Tejun Heo | 32e380a | 2012-03-05 13:14:54 -0800 | [diff] [blame] | 518 | #ifdef CONFIG_BLK_CGROUP |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 519 | extern struct blkio_cgroup blkio_root_cgroup; |
| 520 | extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup); |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 521 | extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 522 | extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 523 | struct request_queue *q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 524 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
| 525 | struct request_queue *q, |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 526 | bool for_root); |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 527 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 528 | struct blkio_policy_type *pol, |
| 529 | unsigned long time, |
| 530 | unsigned long unaccounted_time); |
| 531 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
| 532 | struct blkio_policy_type *pol, |
| 533 | uint64_t bytes, bool direction, bool sync); |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 534 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 535 | struct blkio_policy_type *pol, |
| 536 | uint64_t start_time, |
| 537 | uint64_t io_start_time, bool direction, |
| 538 | bool sync); |
| 539 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, |
| 540 | struct blkio_policy_type *pol, |
| 541 | bool direction, bool sync); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 542 | void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 543 | struct blkio_policy_type *pol, |
| 544 | struct blkio_group *curr_blkg, bool direction, |
| 545 | bool sync); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 546 | void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 547 | struct blkio_policy_type *pol, |
| 548 | bool direction, bool sync); |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 549 | #else |
Jens Axboe | 2f5ea47 | 2009-12-03 21:06:43 +0100 | [diff] [blame] | 550 | struct cgroup; |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 551 | static inline struct blkio_cgroup * |
| 552 | cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; } |
Vivek Goyal | 70087dc | 2011-05-16 15:24:08 +0200 | [diff] [blame] | 553 | static inline struct blkio_cgroup * |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 554 | bio_blkio_cgroup(struct bio *bio) { return NULL; } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 555 | |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 556 | static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
| 557 | void *key) { return NULL; } |
Divyesh Shah | 303a3ac | 2010-04-01 15:01:24 -0700 | [diff] [blame] | 558 | static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 559 | struct blkio_policy_type *pol, unsigned long time, |
| 560 | unsigned long unaccounted_time) { } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 561 | static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 562 | struct blkio_policy_type *pol, uint64_t bytes, |
| 563 | bool direction, bool sync) { } |
Divyesh Shah | 84c124d | 2010-04-09 08:31:19 +0200 | [diff] [blame] | 564 | static inline void blkiocg_update_completion_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 565 | struct blkio_policy_type *pol, uint64_t start_time, |
| 566 | uint64_t io_start_time, bool direction, bool sync) { } |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 567 | static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 568 | struct blkio_policy_type *pol, bool direction, |
| 569 | bool sync) { } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 570 | static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 571 | struct blkio_policy_type *pol, |
| 572 | struct blkio_group *curr_blkg, bool direction, |
| 573 | bool sync) { } |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 574 | static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
Tejun Heo | c176826 | 2012-03-05 13:15:17 -0800 | [diff] [blame] | 575 | struct blkio_policy_type *pol, bool direction, |
| 576 | bool sync) { } |
Vivek Goyal | 31e4c28 | 2009-12-03 12:59:42 -0500 | [diff] [blame] | 577 | #endif |
| 578 | #endif /* _BLK_CGROUP_H */ |