Commit | Line | Data |
---|---|---|
31e4c28d VG |
1 | /* |
2 | * Common Block IO controller cgroup interface | |
3 | * | |
4 | * Based on ideas and code from CFQ, CFS and BFQ: | |
5 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> | |
6 | * | |
7 | * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it> | |
8 | * Paolo Valente <paolo.valente@unimore.it> | |
9 | * | |
10 | * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com> | |
11 | * Nauman Rafique <nauman@google.com> | |
12 | */ | |
13 | #include <linux/ioprio.h> | |
22084190 VG |
14 | #include <linux/seq_file.h> |
15 | #include <linux/kdev_t.h> | |
9d6a986c | 16 | #include <linux/module.h> |
accee785 | 17 | #include <linux/err.h> |
9195291e | 18 | #include <linux/blkdev.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
34d0f179 | 20 | #include <linux/genhd.h> |
72e06c25 | 21 | #include <linux/delay.h> |
9a9e8a26 | 22 | #include <linux/atomic.h> |
72e06c25 | 23 | #include "blk-cgroup.h" |
5efd6113 | 24 | #include "blk.h" |
3e252066 | 25 | |
84c124da DS |
26 | #define MAX_KEY_LEN 100 |
27 | ||
3e252066 VG |
28 | static DEFINE_SPINLOCK(blkio_list_lock); |
29 | static LIST_HEAD(blkio_list); | |
b1c35769 | 30 | |
923adde1 TH |
31 | static DEFINE_MUTEX(all_q_mutex); |
32 | static LIST_HEAD(all_q_list); | |
33 | ||
1cd9e039 VG |
34 | /* List of groups pending per cpu stats allocation */ |
35 | static DEFINE_SPINLOCK(alloc_list_lock); | |
36 | static LIST_HEAD(alloc_list); | |
37 | ||
38 | static void blkio_stat_alloc_fn(struct work_struct *); | |
39 | static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn); | |
40 | ||
31e4c28d | 41 | struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT }; |
9d6a986c VG |
42 | EXPORT_SYMBOL_GPL(blkio_root_cgroup); |
43 | ||
035d10b2 TH |
44 | static struct blkio_policy_type *blkio_policy[BLKIO_NR_POLICIES]; |
45 | ||
062a644d VG |
46 | /* for encoding cft->private value on file */ |
47 | #define BLKIOFILE_PRIVATE(x, val) (((x) << 16) | (val)) | |
48 | /* What policy owns the file, proportional or throttle */ | |
49 | #define BLKIOFILE_POLICY(val) (((val) >> 16) & 0xffff) | |
50 | #define BLKIOFILE_ATTR(val) ((val) & 0xffff) | |
51 | ||
31e4c28d VG |
52 | struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup) |
53 | { | |
54 | return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id), | |
55 | struct blkio_cgroup, css); | |
56 | } | |
9d6a986c | 57 | EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup); |
31e4c28d | 58 | |
4f85cb96 | 59 | static struct blkio_cgroup *task_blkio_cgroup(struct task_struct *tsk) |
70087dc3 VG |
60 | { |
61 | return container_of(task_subsys_state(tsk, blkio_subsys_id), | |
62 | struct blkio_cgroup, css); | |
63 | } | |
4f85cb96 TH |
64 | |
65 | struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio) | |
66 | { | |
67 | if (bio && bio->bi_css) | |
68 | return container_of(bio->bi_css, struct blkio_cgroup, css); | |
69 | return task_blkio_cgroup(current); | |
70 | } | |
71 | EXPORT_SYMBOL_GPL(bio_blkio_cgroup); | |
70087dc3 | 72 | |
c1768268 TH |
73 | static inline void blkio_update_group_weight(struct blkio_group *blkg, |
74 | int plid, unsigned int weight) | |
062a644d VG |
75 | { |
76 | struct blkio_policy_type *blkiop; | |
77 | ||
78 | list_for_each_entry(blkiop, &blkio_list, list) { | |
79 | /* If this policy does not own the blkg, do not send updates */ | |
c1768268 | 80 | if (blkiop->plid != plid) |
062a644d VG |
81 | continue; |
82 | if (blkiop->ops.blkio_update_group_weight_fn) | |
ca32aefc | 83 | blkiop->ops.blkio_update_group_weight_fn(blkg->q, |
fe071437 | 84 | blkg, weight); |
062a644d VG |
85 | } |
86 | } | |
87 | ||
c1768268 TH |
88 | static inline void blkio_update_group_bps(struct blkio_group *blkg, int plid, |
89 | u64 bps, int fileid) | |
4c9eefa1 VG |
90 | { |
91 | struct blkio_policy_type *blkiop; | |
92 | ||
93 | list_for_each_entry(blkiop, &blkio_list, list) { | |
94 | ||
95 | /* If this policy does not own the blkg, do not send updates */ | |
c1768268 | 96 | if (blkiop->plid != plid) |
4c9eefa1 VG |
97 | continue; |
98 | ||
99 | if (fileid == BLKIO_THROTL_read_bps_device | |
100 | && blkiop->ops.blkio_update_group_read_bps_fn) | |
ca32aefc | 101 | blkiop->ops.blkio_update_group_read_bps_fn(blkg->q, |
fe071437 | 102 | blkg, bps); |
4c9eefa1 VG |
103 | |
104 | if (fileid == BLKIO_THROTL_write_bps_device | |
105 | && blkiop->ops.blkio_update_group_write_bps_fn) | |
ca32aefc | 106 | blkiop->ops.blkio_update_group_write_bps_fn(blkg->q, |
fe071437 | 107 | blkg, bps); |
4c9eefa1 VG |
108 | } |
109 | } | |
110 | ||
7702e8f4 | 111 | static inline void blkio_update_group_iops(struct blkio_group *blkg, |
c1768268 TH |
112 | int plid, unsigned int iops, |
113 | int fileid) | |
7702e8f4 VG |
114 | { |
115 | struct blkio_policy_type *blkiop; | |
116 | ||
117 | list_for_each_entry(blkiop, &blkio_list, list) { | |
118 | ||
119 | /* If this policy does not own the blkg, do not send updates */ | |
c1768268 | 120 | if (blkiop->plid != plid) |
7702e8f4 VG |
121 | continue; |
122 | ||
123 | if (fileid == BLKIO_THROTL_read_iops_device | |
124 | && blkiop->ops.blkio_update_group_read_iops_fn) | |
ca32aefc | 125 | blkiop->ops.blkio_update_group_read_iops_fn(blkg->q, |
fe071437 | 126 | blkg, iops); |
7702e8f4 VG |
127 | |
128 | if (fileid == BLKIO_THROTL_write_iops_device | |
129 | && blkiop->ops.blkio_update_group_write_iops_fn) | |
ca32aefc | 130 | blkiop->ops.blkio_update_group_write_iops_fn(blkg->q, |
fe071437 | 131 | blkg,iops); |
7702e8f4 VG |
132 | } |
133 | } | |
134 | ||
cdc1184c | 135 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
edf1b879 | 136 | /* This should be called with the queue_lock held. */ |
812df48d | 137 | static void blkio_set_start_group_wait_time(struct blkio_group *blkg, |
c1768268 TH |
138 | struct blkio_policy_type *pol, |
139 | struct blkio_group *curr_blkg) | |
812df48d | 140 | { |
c1768268 | 141 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
549d3aa8 TH |
142 | |
143 | if (blkio_blkg_waiting(&pd->stats)) | |
812df48d DS |
144 | return; |
145 | if (blkg == curr_blkg) | |
146 | return; | |
549d3aa8 TH |
147 | pd->stats.start_group_wait_time = sched_clock(); |
148 | blkio_mark_blkg_waiting(&pd->stats); | |
812df48d DS |
149 | } |
150 | ||
edf1b879 | 151 | /* This should be called with the queue_lock held. */ |
812df48d DS |
152 | static void blkio_update_group_wait_time(struct blkio_group_stats *stats) |
153 | { | |
154 | unsigned long long now; | |
155 | ||
156 | if (!blkio_blkg_waiting(stats)) | |
157 | return; | |
158 | ||
159 | now = sched_clock(); | |
160 | if (time_after64(now, stats->start_group_wait_time)) | |
edcb0722 TH |
161 | blkg_stat_add(&stats->group_wait_time, |
162 | now - stats->start_group_wait_time); | |
812df48d DS |
163 | blkio_clear_blkg_waiting(stats); |
164 | } | |
165 | ||
edf1b879 | 166 | /* This should be called with the queue_lock held. */ |
812df48d DS |
167 | static void blkio_end_empty_time(struct blkio_group_stats *stats) |
168 | { | |
169 | unsigned long long now; | |
170 | ||
171 | if (!blkio_blkg_empty(stats)) | |
172 | return; | |
173 | ||
174 | now = sched_clock(); | |
175 | if (time_after64(now, stats->start_empty_time)) | |
edcb0722 TH |
176 | blkg_stat_add(&stats->empty_time, |
177 | now - stats->start_empty_time); | |
812df48d DS |
178 | blkio_clear_blkg_empty(stats); |
179 | } | |
180 | ||
c1768268 TH |
181 | void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg, |
182 | struct blkio_policy_type *pol) | |
812df48d | 183 | { |
edf1b879 | 184 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
812df48d | 185 | |
edf1b879 TH |
186 | lockdep_assert_held(blkg->q->queue_lock); |
187 | BUG_ON(blkio_blkg_idling(stats)); | |
188 | ||
189 | stats->start_idle_time = sched_clock(); | |
190 | blkio_mark_blkg_idling(stats); | |
812df48d DS |
191 | } |
192 | EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats); | |
193 | ||
c1768268 TH |
194 | void blkiocg_update_idle_time_stats(struct blkio_group *blkg, |
195 | struct blkio_policy_type *pol) | |
812df48d | 196 | { |
edf1b879 TH |
197 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
198 | ||
199 | lockdep_assert_held(blkg->q->queue_lock); | |
812df48d | 200 | |
812df48d | 201 | if (blkio_blkg_idling(stats)) { |
edf1b879 TH |
202 | unsigned long long now = sched_clock(); |
203 | ||
edcb0722 TH |
204 | if (time_after64(now, stats->start_idle_time)) |
205 | blkg_stat_add(&stats->idle_time, | |
206 | now - stats->start_idle_time); | |
812df48d DS |
207 | blkio_clear_blkg_idling(stats); |
208 | } | |
812df48d DS |
209 | } |
210 | EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats); | |
211 | ||
c1768268 TH |
212 | void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg, |
213 | struct blkio_policy_type *pol) | |
cdc1184c | 214 | { |
edf1b879 | 215 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
cdc1184c | 216 | |
edf1b879 TH |
217 | lockdep_assert_held(blkg->q->queue_lock); |
218 | ||
edcb0722 TH |
219 | blkg_stat_add(&stats->avg_queue_size_sum, |
220 | blkg_rwstat_sum(&stats->queued)); | |
221 | blkg_stat_add(&stats->avg_queue_size_samples, 1); | |
812df48d | 222 | blkio_update_group_wait_time(stats); |
cdc1184c | 223 | } |
a11cdaa7 DS |
224 | EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats); |
225 | ||
c1768268 TH |
226 | void blkiocg_set_start_empty_time(struct blkio_group *blkg, |
227 | struct blkio_policy_type *pol) | |
28baf442 | 228 | { |
edf1b879 | 229 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
28baf442 | 230 | |
edf1b879 | 231 | lockdep_assert_held(blkg->q->queue_lock); |
28baf442 | 232 | |
edcb0722 | 233 | if (blkg_rwstat_sum(&stats->queued)) |
28baf442 | 234 | return; |
28baf442 DS |
235 | |
236 | /* | |
e5ff082e VG |
237 | * group is already marked empty. This can happen if cfqq got new |
238 | * request in parent group and moved to this group while being added | |
239 | * to service tree. Just ignore the event and move on. | |
28baf442 | 240 | */ |
edf1b879 | 241 | if (blkio_blkg_empty(stats)) |
e5ff082e | 242 | return; |
e5ff082e | 243 | |
28baf442 DS |
244 | stats->start_empty_time = sched_clock(); |
245 | blkio_mark_blkg_empty(stats); | |
28baf442 DS |
246 | } |
247 | EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time); | |
248 | ||
a11cdaa7 | 249 | void blkiocg_update_dequeue_stats(struct blkio_group *blkg, |
c1768268 TH |
250 | struct blkio_policy_type *pol, |
251 | unsigned long dequeue) | |
a11cdaa7 | 252 | { |
c1768268 | 253 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
549d3aa8 | 254 | |
edf1b879 TH |
255 | lockdep_assert_held(blkg->q->queue_lock); |
256 | ||
edcb0722 | 257 | blkg_stat_add(&pd->stats.dequeue, dequeue); |
a11cdaa7 DS |
258 | } |
259 | EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats); | |
812df48d DS |
260 | #else |
261 | static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg, | |
c1768268 TH |
262 | struct blkio_policy_type *pol, |
263 | struct blkio_group *curr_blkg) { } | |
264 | static inline void blkio_end_empty_time(struct blkio_group_stats *stats) { } | |
cdc1184c DS |
265 | #endif |
266 | ||
a11cdaa7 | 267 | void blkiocg_update_io_add_stats(struct blkio_group *blkg, |
c1768268 TH |
268 | struct blkio_policy_type *pol, |
269 | struct blkio_group *curr_blkg, bool direction, | |
270 | bool sync) | |
cdc1184c | 271 | { |
edf1b879 | 272 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
edcb0722 | 273 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
edf1b879 TH |
274 | |
275 | lockdep_assert_held(blkg->q->queue_lock); | |
276 | ||
edcb0722 | 277 | blkg_rwstat_add(&stats->queued, rw, 1); |
edf1b879 | 278 | blkio_end_empty_time(stats); |
c1768268 | 279 | blkio_set_start_group_wait_time(blkg, pol, curr_blkg); |
cdc1184c | 280 | } |
a11cdaa7 | 281 | EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats); |
cdc1184c | 282 | |
a11cdaa7 | 283 | void blkiocg_update_io_remove_stats(struct blkio_group *blkg, |
c1768268 TH |
284 | struct blkio_policy_type *pol, |
285 | bool direction, bool sync) | |
cdc1184c | 286 | { |
edf1b879 | 287 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
edcb0722 | 288 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
edf1b879 TH |
289 | |
290 | lockdep_assert_held(blkg->q->queue_lock); | |
cdc1184c | 291 | |
edcb0722 | 292 | blkg_rwstat_add(&stats->queued, rw, -1); |
cdc1184c | 293 | } |
a11cdaa7 | 294 | EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats); |
cdc1184c | 295 | |
c1768268 TH |
296 | void blkiocg_update_timeslice_used(struct blkio_group *blkg, |
297 | struct blkio_policy_type *pol, | |
298 | unsigned long time, | |
299 | unsigned long unaccounted_time) | |
22084190 | 300 | { |
edf1b879 TH |
301 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
302 | ||
303 | lockdep_assert_held(blkg->q->queue_lock); | |
303a3acb | 304 | |
edcb0722 | 305 | blkg_stat_add(&stats->time, time); |
a23e6869 | 306 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
edcb0722 | 307 | blkg_stat_add(&stats->unaccounted_time, unaccounted_time); |
a23e6869 | 308 | #endif |
22084190 | 309 | } |
303a3acb | 310 | EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used); |
22084190 | 311 | |
5624a4e4 VG |
312 | /* |
313 | * should be called under rcu read lock or queue lock to make sure blkg pointer | |
314 | * is valid. | |
315 | */ | |
84c124da | 316 | void blkiocg_update_dispatch_stats(struct blkio_group *blkg, |
c1768268 TH |
317 | struct blkio_policy_type *pol, |
318 | uint64_t bytes, bool direction, bool sync) | |
9195291e | 319 | { |
edcb0722 | 320 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
c1768268 | 321 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; |
5624a4e4 | 322 | struct blkio_group_stats_cpu *stats_cpu; |
575969a0 VG |
323 | unsigned long flags; |
324 | ||
1cd9e039 VG |
325 | /* If per cpu stats are not allocated yet, don't do any accounting. */ |
326 | if (pd->stats_cpu == NULL) | |
327 | return; | |
328 | ||
575969a0 VG |
329 | /* |
330 | * Disabling interrupts to provide mutual exclusion between two | |
331 | * writes on same cpu. It probably is not needed for 64bit. Not | |
332 | * optimizing that case yet. | |
333 | */ | |
334 | local_irq_save(flags); | |
9195291e | 335 | |
549d3aa8 | 336 | stats_cpu = this_cpu_ptr(pd->stats_cpu); |
5624a4e4 | 337 | |
edcb0722 TH |
338 | blkg_stat_add(&stats_cpu->sectors, bytes >> 9); |
339 | blkg_rwstat_add(&stats_cpu->serviced, rw, 1); | |
340 | blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes); | |
341 | ||
575969a0 | 342 | local_irq_restore(flags); |
9195291e | 343 | } |
84c124da | 344 | EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats); |
9195291e | 345 | |
84c124da | 346 | void blkiocg_update_completion_stats(struct blkio_group *blkg, |
c1768268 TH |
347 | struct blkio_policy_type *pol, |
348 | uint64_t start_time, | |
349 | uint64_t io_start_time, bool direction, | |
350 | bool sync) | |
9195291e | 351 | { |
edf1b879 | 352 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
9195291e | 353 | unsigned long long now = sched_clock(); |
edcb0722 | 354 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
9195291e | 355 | |
edf1b879 TH |
356 | lockdep_assert_held(blkg->q->queue_lock); |
357 | ||
84c124da | 358 | if (time_after64(now, io_start_time)) |
edcb0722 | 359 | blkg_rwstat_add(&stats->service_time, rw, now - io_start_time); |
84c124da | 360 | if (time_after64(io_start_time, start_time)) |
edcb0722 TH |
361 | blkg_rwstat_add(&stats->wait_time, rw, |
362 | io_start_time - start_time); | |
9195291e | 363 | } |
84c124da | 364 | EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats); |
9195291e | 365 | |
317389a7 | 366 | /* Merged stats are per cpu. */ |
c1768268 TH |
367 | void blkiocg_update_io_merged_stats(struct blkio_group *blkg, |
368 | struct blkio_policy_type *pol, | |
369 | bool direction, bool sync) | |
812d4026 | 370 | { |
edf1b879 | 371 | struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; |
edcb0722 | 372 | int rw = (direction ? REQ_WRITE : 0) | (sync ? REQ_SYNC : 0); |
edf1b879 TH |
373 | |
374 | lockdep_assert_held(blkg->q->queue_lock); | |
812d4026 | 375 | |
edcb0722 | 376 | blkg_rwstat_add(&stats->merged, rw, 1); |
812d4026 DS |
377 | } |
378 | EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats); | |
379 | ||
1cd9e039 VG |
380 | /* |
381 | * Worker for allocating per cpu stat for blk groups. This is scheduled on | |
382 | * the system_nrt_wq once there are some groups on the alloc_list waiting | |
383 | * for allocation. | |
384 | */ | |
385 | static void blkio_stat_alloc_fn(struct work_struct *work) | |
386 | { | |
387 | static void *pcpu_stats[BLKIO_NR_POLICIES]; | |
388 | struct delayed_work *dwork = to_delayed_work(work); | |
389 | struct blkio_group *blkg; | |
390 | int i; | |
391 | bool empty = false; | |
392 | ||
393 | alloc_stats: | |
394 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { | |
395 | if (pcpu_stats[i] != NULL) | |
396 | continue; | |
397 | ||
398 | pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu); | |
399 | ||
400 | /* Allocation failed. Try again after some time. */ | |
401 | if (pcpu_stats[i] == NULL) { | |
402 | queue_delayed_work(system_nrt_wq, dwork, | |
403 | msecs_to_jiffies(10)); | |
404 | return; | |
405 | } | |
406 | } | |
407 | ||
408 | spin_lock_irq(&blkio_list_lock); | |
409 | spin_lock(&alloc_list_lock); | |
410 | ||
411 | /* cgroup got deleted or queue exited. */ | |
412 | if (!list_empty(&alloc_list)) { | |
413 | blkg = list_first_entry(&alloc_list, struct blkio_group, | |
414 | alloc_node); | |
415 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { | |
416 | struct blkg_policy_data *pd = blkg->pd[i]; | |
417 | ||
418 | if (blkio_policy[i] && pd && !pd->stats_cpu) | |
419 | swap(pd->stats_cpu, pcpu_stats[i]); | |
420 | } | |
421 | ||
422 | list_del_init(&blkg->alloc_node); | |
423 | } | |
424 | ||
425 | empty = list_empty(&alloc_list); | |
426 | ||
427 | spin_unlock(&alloc_list_lock); | |
428 | spin_unlock_irq(&blkio_list_lock); | |
429 | ||
430 | if (!empty) | |
431 | goto alloc_stats; | |
432 | } | |
433 | ||
0381411e TH |
434 | /** |
435 | * blkg_free - free a blkg | |
436 | * @blkg: blkg to free | |
437 | * | |
438 | * Free @blkg which may be partially allocated. | |
439 | */ | |
440 | static void blkg_free(struct blkio_group *blkg) | |
441 | { | |
e8989fae | 442 | int i; |
549d3aa8 TH |
443 | |
444 | if (!blkg) | |
445 | return; | |
446 | ||
e8989fae TH |
447 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
448 | struct blkg_policy_data *pd = blkg->pd[i]; | |
449 | ||
450 | if (pd) { | |
451 | free_percpu(pd->stats_cpu); | |
452 | kfree(pd); | |
453 | } | |
0381411e | 454 | } |
e8989fae | 455 | |
549d3aa8 | 456 | kfree(blkg); |
0381411e TH |
457 | } |
458 | ||
459 | /** | |
460 | * blkg_alloc - allocate a blkg | |
461 | * @blkcg: block cgroup the new blkg is associated with | |
462 | * @q: request_queue the new blkg is associated with | |
0381411e | 463 | * |
e8989fae | 464 | * Allocate a new blkg assocating @blkcg and @q. |
0381411e TH |
465 | */ |
466 | static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, | |
e8989fae | 467 | struct request_queue *q) |
0381411e TH |
468 | { |
469 | struct blkio_group *blkg; | |
e8989fae | 470 | int i; |
0381411e TH |
471 | |
472 | /* alloc and init base part */ | |
473 | blkg = kzalloc_node(sizeof(*blkg), GFP_ATOMIC, q->node); | |
474 | if (!blkg) | |
475 | return NULL; | |
476 | ||
c875f4d0 | 477 | blkg->q = q; |
e8989fae | 478 | INIT_LIST_HEAD(&blkg->q_node); |
1cd9e039 | 479 | INIT_LIST_HEAD(&blkg->alloc_node); |
0381411e | 480 | blkg->blkcg = blkcg; |
1adaf3dd | 481 | blkg->refcnt = 1; |
0381411e TH |
482 | cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path)); |
483 | ||
e8989fae TH |
484 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
485 | struct blkio_policy_type *pol = blkio_policy[i]; | |
486 | struct blkg_policy_data *pd; | |
0381411e | 487 | |
e8989fae TH |
488 | if (!pol) |
489 | continue; | |
490 | ||
491 | /* alloc per-policy data and attach it to blkg */ | |
492 | pd = kzalloc_node(sizeof(*pd) + pol->pdata_size, GFP_ATOMIC, | |
493 | q->node); | |
494 | if (!pd) { | |
495 | blkg_free(blkg); | |
496 | return NULL; | |
497 | } | |
549d3aa8 | 498 | |
e8989fae TH |
499 | blkg->pd[i] = pd; |
500 | pd->blkg = blkg; | |
0381411e TH |
501 | } |
502 | ||
549d3aa8 | 503 | /* invoke per-policy init */ |
e8989fae TH |
504 | for (i = 0; i < BLKIO_NR_POLICIES; i++) { |
505 | struct blkio_policy_type *pol = blkio_policy[i]; | |
506 | ||
507 | if (pol) | |
508 | pol->ops.blkio_init_group_fn(blkg); | |
509 | } | |
510 | ||
0381411e TH |
511 | return blkg; |
512 | } | |
513 | ||
cd1604fa TH |
514 | struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, |
515 | struct request_queue *q, | |
cd1604fa TH |
516 | bool for_root) |
517 | __releases(q->queue_lock) __acquires(q->queue_lock) | |
5624a4e4 | 518 | { |
1cd9e039 | 519 | struct blkio_group *blkg; |
5624a4e4 | 520 | |
cd1604fa TH |
521 | WARN_ON_ONCE(!rcu_read_lock_held()); |
522 | lockdep_assert_held(q->queue_lock); | |
523 | ||
524 | /* | |
525 | * This could be the first entry point of blkcg implementation and | |
526 | * we shouldn't allow anything to go through for a bypassing queue. | |
527 | * The following can be removed if blkg lookup is guaranteed to | |
528 | * fail on a bypassing queue. | |
529 | */ | |
530 | if (unlikely(blk_queue_bypass(q)) && !for_root) | |
531 | return ERR_PTR(blk_queue_dead(q) ? -EINVAL : -EBUSY); | |
532 | ||
e8989fae | 533 | blkg = blkg_lookup(blkcg, q); |
cd1604fa TH |
534 | if (blkg) |
535 | return blkg; | |
536 | ||
7ee9c562 | 537 | /* blkg holds a reference to blkcg */ |
cd1604fa TH |
538 | if (!css_tryget(&blkcg->css)) |
539 | return ERR_PTR(-EINVAL); | |
540 | ||
541 | /* | |
542 | * Allocate and initialize. | |
cd1604fa | 543 | */ |
1cd9e039 | 544 | blkg = blkg_alloc(blkcg, q); |
cd1604fa TH |
545 | |
546 | /* did alloc fail? */ | |
1cd9e039 | 547 | if (unlikely(!blkg)) { |
cd1604fa TH |
548 | blkg = ERR_PTR(-ENOMEM); |
549 | goto out; | |
550 | } | |
551 | ||
552 | /* insert */ | |
553 | spin_lock(&blkcg->lock); | |
31e4c28d | 554 | hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list); |
e8989fae | 555 | list_add(&blkg->q_node, &q->blkg_list); |
cd1604fa | 556 | spin_unlock(&blkcg->lock); |
1cd9e039 VG |
557 | |
558 | spin_lock(&alloc_list_lock); | |
559 | list_add(&blkg->alloc_node, &alloc_list); | |
560 | /* Queue per cpu stat allocation from worker thread. */ | |
561 | queue_delayed_work(system_nrt_wq, &blkio_stat_alloc_work, 0); | |
562 | spin_unlock(&alloc_list_lock); | |
cd1604fa | 563 | out: |
cd1604fa | 564 | return blkg; |
31e4c28d | 565 | } |
cd1604fa | 566 | EXPORT_SYMBOL_GPL(blkg_lookup_create); |
31e4c28d | 567 | |
31e4c28d | 568 | /* called under rcu_read_lock(). */ |
cd1604fa | 569 | struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg, |
e8989fae | 570 | struct request_queue *q) |
31e4c28d VG |
571 | { |
572 | struct blkio_group *blkg; | |
573 | struct hlist_node *n; | |
31e4c28d | 574 | |
ca32aefc | 575 | hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) |
e8989fae | 576 | if (blkg->q == q) |
31e4c28d | 577 | return blkg; |
31e4c28d VG |
578 | return NULL; |
579 | } | |
cd1604fa | 580 | EXPORT_SYMBOL_GPL(blkg_lookup); |
31e4c28d | 581 | |
e8989fae | 582 | static void blkg_destroy(struct blkio_group *blkg) |
03aa264a TH |
583 | { |
584 | struct request_queue *q = blkg->q; | |
9f13ef67 | 585 | struct blkio_cgroup *blkcg = blkg->blkcg; |
03aa264a TH |
586 | |
587 | lockdep_assert_held(q->queue_lock); | |
9f13ef67 | 588 | lockdep_assert_held(&blkcg->lock); |
03aa264a TH |
589 | |
590 | /* Something wrong if we are trying to remove same group twice */ | |
e8989fae | 591 | WARN_ON_ONCE(list_empty(&blkg->q_node)); |
9f13ef67 | 592 | WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node)); |
e8989fae | 593 | list_del_init(&blkg->q_node); |
9f13ef67 | 594 | hlist_del_init_rcu(&blkg->blkcg_node); |
03aa264a | 595 | |
1cd9e039 VG |
596 | spin_lock(&alloc_list_lock); |
597 | list_del_init(&blkg->alloc_node); | |
598 | spin_unlock(&alloc_list_lock); | |
599 | ||
03aa264a TH |
600 | /* |
601 | * Put the reference taken at the time of creation so that when all | |
602 | * queues are gone, group can be destroyed. | |
603 | */ | |
604 | blkg_put(blkg); | |
605 | } | |
606 | ||
e8989fae TH |
607 | /* |
608 | * XXX: This updates blkg policy data in-place for root blkg, which is | |
609 | * necessary across elevator switch and policy registration as root blkgs | |
610 | * aren't shot down. This broken and racy implementation is temporary. | |
611 | * Eventually, blkg shoot down will be replaced by proper in-place update. | |
612 | */ | |
613 | void update_root_blkg_pd(struct request_queue *q, enum blkio_policy_id plid) | |
614 | { | |
615 | struct blkio_policy_type *pol = blkio_policy[plid]; | |
616 | struct blkio_group *blkg = blkg_lookup(&blkio_root_cgroup, q); | |
617 | struct blkg_policy_data *pd; | |
618 | ||
619 | if (!blkg) | |
620 | return; | |
621 | ||
622 | kfree(blkg->pd[plid]); | |
623 | blkg->pd[plid] = NULL; | |
624 | ||
625 | if (!pol) | |
626 | return; | |
627 | ||
628 | pd = kzalloc(sizeof(*pd) + pol->pdata_size, GFP_KERNEL); | |
629 | WARN_ON_ONCE(!pd); | |
630 | ||
631 | pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); | |
632 | WARN_ON_ONCE(!pd->stats_cpu); | |
633 | ||
634 | blkg->pd[plid] = pd; | |
635 | pd->blkg = blkg; | |
636 | pol->ops.blkio_init_group_fn(blkg); | |
637 | } | |
638 | EXPORT_SYMBOL_GPL(update_root_blkg_pd); | |
639 | ||
9f13ef67 TH |
640 | /** |
641 | * blkg_destroy_all - destroy all blkgs associated with a request_queue | |
642 | * @q: request_queue of interest | |
643 | * @destroy_root: whether to destroy root blkg or not | |
644 | * | |
645 | * Destroy blkgs associated with @q. If @destroy_root is %true, all are | |
646 | * destroyed; otherwise, root blkg is left alone. | |
647 | */ | |
e8989fae | 648 | void blkg_destroy_all(struct request_queue *q, bool destroy_root) |
72e06c25 | 649 | { |
03aa264a | 650 | struct blkio_group *blkg, *n; |
72e06c25 | 651 | |
9f13ef67 | 652 | spin_lock_irq(q->queue_lock); |
72e06c25 | 653 | |
9f13ef67 TH |
654 | list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) { |
655 | struct blkio_cgroup *blkcg = blkg->blkcg; | |
72e06c25 | 656 | |
9f13ef67 TH |
657 | /* skip root? */ |
658 | if (!destroy_root && blkg->blkcg == &blkio_root_cgroup) | |
659 | continue; | |
72e06c25 | 660 | |
9f13ef67 TH |
661 | spin_lock(&blkcg->lock); |
662 | blkg_destroy(blkg); | |
663 | spin_unlock(&blkcg->lock); | |
72e06c25 | 664 | } |
9f13ef67 TH |
665 | |
666 | spin_unlock_irq(q->queue_lock); | |
72e06c25 | 667 | } |
03aa264a | 668 | EXPORT_SYMBOL_GPL(blkg_destroy_all); |
72e06c25 | 669 | |
1adaf3dd TH |
670 | static void blkg_rcu_free(struct rcu_head *rcu_head) |
671 | { | |
672 | blkg_free(container_of(rcu_head, struct blkio_group, rcu_head)); | |
673 | } | |
674 | ||
675 | void __blkg_release(struct blkio_group *blkg) | |
676 | { | |
677 | /* release the extra blkcg reference this blkg has been holding */ | |
678 | css_put(&blkg->blkcg->css); | |
679 | ||
680 | /* | |
681 | * A group is freed in rcu manner. But having an rcu lock does not | |
682 | * mean that one can access all the fields of blkg and assume these | |
683 | * are valid. For example, don't try to follow throtl_data and | |
684 | * request queue links. | |
685 | * | |
686 | * Having a reference to blkg under an rcu allows acess to only | |
687 | * values local to groups like group stats and group rate limits | |
688 | */ | |
689 | call_rcu(&blkg->rcu_head, blkg_rcu_free); | |
690 | } | |
691 | EXPORT_SYMBOL_GPL(__blkg_release); | |
692 | ||
c1768268 | 693 | static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid) |
f0bdc8cd | 694 | { |
c1768268 | 695 | struct blkg_policy_data *pd = blkg->pd[plid]; |
997a026c | 696 | int cpu; |
1cd9e039 VG |
697 | |
698 | if (pd->stats_cpu == NULL) | |
699 | return; | |
997a026c TH |
700 | |
701 | for_each_possible_cpu(cpu) { | |
702 | struct blkio_group_stats_cpu *sc = | |
703 | per_cpu_ptr(pd->stats_cpu, cpu); | |
704 | ||
edcb0722 TH |
705 | blkg_rwstat_reset(&sc->service_bytes); |
706 | blkg_rwstat_reset(&sc->serviced); | |
707 | blkg_stat_reset(&sc->sectors); | |
f0bdc8cd VG |
708 | } |
709 | } | |
710 | ||
303a3acb | 711 | static int |
84c124da | 712 | blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val) |
303a3acb | 713 | { |
997a026c | 714 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); |
303a3acb DS |
715 | struct blkio_group *blkg; |
716 | struct hlist_node *n; | |
303a3acb | 717 | |
e8989fae | 718 | spin_lock(&blkio_list_lock); |
303a3acb | 719 | spin_lock_irq(&blkcg->lock); |
997a026c TH |
720 | |
721 | /* | |
722 | * Note that stat reset is racy - it doesn't synchronize against | |
723 | * stat updates. This is a debug feature which shouldn't exist | |
724 | * anyway. If you get hit by a race, retry. | |
725 | */ | |
303a3acb | 726 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
e8989fae | 727 | struct blkio_policy_type *pol; |
549d3aa8 | 728 | |
e8989fae TH |
729 | list_for_each_entry(pol, &blkio_list, list) { |
730 | struct blkg_policy_data *pd = blkg->pd[pol->plid]; | |
997a026c TH |
731 | struct blkio_group_stats *stats = &pd->stats; |
732 | ||
733 | /* queued stats shouldn't be cleared */ | |
edcb0722 TH |
734 | blkg_rwstat_reset(&stats->merged); |
735 | blkg_rwstat_reset(&stats->service_time); | |
736 | blkg_rwstat_reset(&stats->wait_time); | |
737 | blkg_stat_reset(&stats->time); | |
812df48d | 738 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
edcb0722 TH |
739 | blkg_stat_reset(&stats->unaccounted_time); |
740 | blkg_stat_reset(&stats->avg_queue_size_sum); | |
741 | blkg_stat_reset(&stats->avg_queue_size_samples); | |
742 | blkg_stat_reset(&stats->dequeue); | |
743 | blkg_stat_reset(&stats->group_wait_time); | |
744 | blkg_stat_reset(&stats->idle_time); | |
745 | blkg_stat_reset(&stats->empty_time); | |
812df48d | 746 | #endif |
e8989fae TH |
747 | blkio_reset_stats_cpu(blkg, pol->plid); |
748 | } | |
303a3acb | 749 | } |
f0bdc8cd | 750 | |
303a3acb | 751 | spin_unlock_irq(&blkcg->lock); |
e8989fae | 752 | spin_unlock(&blkio_list_lock); |
303a3acb DS |
753 | return 0; |
754 | } | |
755 | ||
d3d32e69 | 756 | static const char *blkg_dev_name(struct blkio_group *blkg) |
303a3acb | 757 | { |
d3d32e69 TH |
758 | /* some drivers (floppy) instantiate a queue w/o disk registered */ |
759 | if (blkg->q->backing_dev_info.dev) | |
760 | return dev_name(blkg->q->backing_dev_info.dev); | |
761 | return NULL; | |
303a3acb DS |
762 | } |
763 | ||
d3d32e69 TH |
764 | /** |
765 | * blkcg_print_blkgs - helper for printing per-blkg data | |
766 | * @sf: seq_file to print to | |
767 | * @blkcg: blkcg of interest | |
768 | * @prfill: fill function to print out a blkg | |
769 | * @pol: policy in question | |
770 | * @data: data to be passed to @prfill | |
771 | * @show_total: to print out sum of prfill return values or not | |
772 | * | |
773 | * This function invokes @prfill on each blkg of @blkcg if pd for the | |
774 | * policy specified by @pol exists. @prfill is invoked with @sf, the | |
775 | * policy data and @data. If @show_total is %true, the sum of the return | |
776 | * values from @prfill is printed with "Total" label at the end. | |
777 | * | |
778 | * This is to be used to construct print functions for | |
779 | * cftype->read_seq_string method. | |
780 | */ | |
781 | static void blkcg_print_blkgs(struct seq_file *sf, struct blkio_cgroup *blkcg, | |
782 | u64 (*prfill)(struct seq_file *, | |
783 | struct blkg_policy_data *, int), | |
784 | int pol, int data, bool show_total) | |
5624a4e4 | 785 | { |
d3d32e69 TH |
786 | struct blkio_group *blkg; |
787 | struct hlist_node *n; | |
788 | u64 total = 0; | |
5624a4e4 | 789 | |
d3d32e69 TH |
790 | spin_lock_irq(&blkcg->lock); |
791 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) | |
792 | if (blkg->pd[pol]) | |
793 | total += prfill(sf, blkg->pd[pol], data); | |
794 | spin_unlock_irq(&blkcg->lock); | |
795 | ||
796 | if (show_total) | |
797 | seq_printf(sf, "Total %llu\n", (unsigned long long)total); | |
798 | } | |
799 | ||
800 | /** | |
801 | * __blkg_prfill_u64 - prfill helper for a single u64 value | |
802 | * @sf: seq_file to print to | |
803 | * @pd: policy data of interest | |
804 | * @v: value to print | |
805 | * | |
806 | * Print @v to @sf for the device assocaited with @pd. | |
807 | */ | |
808 | static u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, | |
809 | u64 v) | |
810 | { | |
811 | const char *dname = blkg_dev_name(pd->blkg); | |
812 | ||
813 | if (!dname) | |
814 | return 0; | |
815 | ||
816 | seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v); | |
817 | return v; | |
818 | } | |
819 | ||
820 | /** | |
821 | * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat | |
822 | * @sf: seq_file to print to | |
823 | * @pd: policy data of interest | |
824 | * @rwstat: rwstat to print | |
825 | * | |
826 | * Print @rwstat to @sf for the device assocaited with @pd. | |
827 | */ | |
828 | static u64 __blkg_prfill_rwstat(struct seq_file *sf, | |
829 | struct blkg_policy_data *pd, | |
830 | const struct blkg_rwstat *rwstat) | |
831 | { | |
832 | static const char *rwstr[] = { | |
833 | [BLKG_RWSTAT_READ] = "Read", | |
834 | [BLKG_RWSTAT_WRITE] = "Write", | |
835 | [BLKG_RWSTAT_SYNC] = "Sync", | |
836 | [BLKG_RWSTAT_ASYNC] = "Async", | |
837 | }; | |
838 | const char *dname = blkg_dev_name(pd->blkg); | |
839 | u64 v; | |
840 | int i; | |
841 | ||
842 | if (!dname) | |
843 | return 0; | |
844 | ||
845 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
846 | seq_printf(sf, "%s %s %llu\n", dname, rwstr[i], | |
847 | (unsigned long long)rwstat->cnt[i]); | |
848 | ||
849 | v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE]; | |
850 | seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v); | |
851 | return v; | |
852 | } | |
853 | ||
854 | static u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, | |
855 | int off) | |
856 | { | |
857 | return __blkg_prfill_u64(sf, pd, | |
858 | blkg_stat_read((void *)&pd->stats + off)); | |
859 | } | |
860 | ||
861 | static u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd, | |
862 | int off) | |
863 | { | |
864 | struct blkg_rwstat rwstat = blkg_rwstat_read((void *)&pd->stats + off); | |
865 | ||
866 | return __blkg_prfill_rwstat(sf, pd, &rwstat); | |
867 | } | |
868 | ||
869 | /* print blkg_stat specified by BLKCG_STAT_PRIV() */ | |
870 | static int blkcg_print_stat(struct cgroup *cgrp, struct cftype *cft, | |
871 | struct seq_file *sf) | |
872 | { | |
873 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); | |
874 | ||
875 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, | |
876 | BLKCG_STAT_POL(cft->private), | |
877 | BLKCG_STAT_OFF(cft->private), false); | |
878 | return 0; | |
879 | } | |
880 | ||
881 | /* print blkg_rwstat specified by BLKCG_STAT_PRIV() */ | |
882 | static int blkcg_print_rwstat(struct cgroup *cgrp, struct cftype *cft, | |
883 | struct seq_file *sf) | |
884 | { | |
885 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); | |
886 | ||
887 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, | |
888 | BLKCG_STAT_POL(cft->private), | |
889 | BLKCG_STAT_OFF(cft->private), true); | |
890 | return 0; | |
891 | } | |
892 | ||
893 | static u64 blkg_prfill_cpu_stat(struct seq_file *sf, | |
894 | struct blkg_policy_data *pd, int off) | |
895 | { | |
896 | u64 v = 0; | |
897 | int cpu; | |
1cd9e039 | 898 | |
5624a4e4 | 899 | for_each_possible_cpu(cpu) { |
d3d32e69 | 900 | struct blkio_group_stats_cpu *sc = |
edcb0722 | 901 | per_cpu_ptr(pd->stats_cpu, cpu); |
edcb0722 | 902 | |
d3d32e69 | 903 | v += blkg_stat_read((void *)sc + off); |
5624a4e4 VG |
904 | } |
905 | ||
d3d32e69 | 906 | return __blkg_prfill_u64(sf, pd, v); |
5624a4e4 VG |
907 | } |
908 | ||
d3d32e69 TH |
909 | static u64 blkg_prfill_cpu_rwstat(struct seq_file *sf, |
910 | struct blkg_policy_data *pd, int off) | |
5624a4e4 | 911 | { |
d3d32e69 TH |
912 | struct blkg_rwstat rwstat = { }, tmp; |
913 | int i, cpu; | |
914 | ||
915 | for_each_possible_cpu(cpu) { | |
916 | struct blkio_group_stats_cpu *sc = | |
917 | per_cpu_ptr(pd->stats_cpu, cpu); | |
5624a4e4 | 918 | |
d3d32e69 TH |
919 | tmp = blkg_rwstat_read((void *)sc + off); |
920 | for (i = 0; i < BLKG_RWSTAT_NR; i++) | |
921 | rwstat.cnt[i] += tmp.cnt[i]; | |
5624a4e4 VG |
922 | } |
923 | ||
d3d32e69 TH |
924 | return __blkg_prfill_rwstat(sf, pd, &rwstat); |
925 | } | |
5624a4e4 | 926 | |
d3d32e69 TH |
927 | /* print per-cpu blkg_stat specified by BLKCG_STAT_PRIV() */ |
928 | static int blkcg_print_cpu_stat(struct cgroup *cgrp, struct cftype *cft, | |
929 | struct seq_file *sf) | |
930 | { | |
931 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); | |
932 | ||
933 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_stat, | |
934 | BLKCG_STAT_POL(cft->private), | |
935 | BLKCG_STAT_OFF(cft->private), false); | |
936 | return 0; | |
5624a4e4 VG |
937 | } |
938 | ||
d3d32e69 TH |
939 | /* print per-cpu blkg_rwstat specified by BLKCG_STAT_PRIV() */ |
940 | static int blkcg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft, | |
941 | struct seq_file *sf) | |
303a3acb | 942 | { |
d3d32e69 | 943 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
edcb0722 | 944 | |
d3d32e69 TH |
945 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_cpu_rwstat, |
946 | BLKCG_STAT_POL(cft->private), | |
947 | BLKCG_STAT_OFF(cft->private), true); | |
948 | return 0; | |
949 | } | |
303a3acb | 950 | |
d3d32e69 TH |
951 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
952 | static u64 blkg_prfill_avg_queue_size(struct seq_file *sf, | |
953 | struct blkg_policy_data *pd, int off) | |
954 | { | |
955 | u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples); | |
956 | u64 v = 0; | |
c4c76a05 | 957 | |
d3d32e69 TH |
958 | if (samples) { |
959 | v = blkg_stat_read(&pd->stats.avg_queue_size_sum); | |
960 | do_div(v, samples); | |
edcb0722 | 961 | } |
d3d32e69 TH |
962 | __blkg_prfill_u64(sf, pd, v); |
963 | return 0; | |
964 | } | |
c4c76a05 | 965 | |
d3d32e69 TH |
966 | /* print avg_queue_size */ |
967 | static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft, | |
968 | struct seq_file *sf) | |
969 | { | |
970 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); | |
c4c76a05 | 971 | |
d3d32e69 TH |
972 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size, |
973 | BLKIO_POLICY_PROP, 0, false); | |
974 | return 0; | |
303a3acb | 975 | } |
d3d32e69 | 976 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
303a3acb | 977 | |
4bfd482e TH |
978 | static int blkio_policy_parse_and_set(char *buf, enum blkio_policy_id plid, |
979 | int fileid, struct blkio_cgroup *blkcg) | |
34d0f179 | 980 | { |
ece84241 | 981 | struct gendisk *disk = NULL; |
e56da7e2 | 982 | struct blkio_group *blkg = NULL; |
549d3aa8 | 983 | struct blkg_policy_data *pd; |
34d0f179 | 984 | char *s[4], *p, *major_s = NULL, *minor_s = NULL; |
d11bb446 | 985 | unsigned long major, minor; |
ece84241 TH |
986 | int i = 0, ret = -EINVAL; |
987 | int part; | |
34d0f179 | 988 | dev_t dev; |
d11bb446 | 989 | u64 temp; |
34d0f179 GJ |
990 | |
991 | memset(s, 0, sizeof(s)); | |
992 | ||
993 | while ((p = strsep(&buf, " ")) != NULL) { | |
994 | if (!*p) | |
995 | continue; | |
996 | ||
997 | s[i++] = p; | |
998 | ||
999 | /* Prevent from inputing too many things */ | |
1000 | if (i == 3) | |
1001 | break; | |
1002 | } | |
1003 | ||
1004 | if (i != 2) | |
ece84241 | 1005 | goto out; |
34d0f179 GJ |
1006 | |
1007 | p = strsep(&s[0], ":"); | |
1008 | if (p != NULL) | |
1009 | major_s = p; | |
1010 | else | |
ece84241 | 1011 | goto out; |
34d0f179 GJ |
1012 | |
1013 | minor_s = s[0]; | |
1014 | if (!minor_s) | |
ece84241 | 1015 | goto out; |
34d0f179 | 1016 | |
ece84241 TH |
1017 | if (strict_strtoul(major_s, 10, &major)) |
1018 | goto out; | |
34d0f179 | 1019 | |
ece84241 TH |
1020 | if (strict_strtoul(minor_s, 10, &minor)) |
1021 | goto out; | |
34d0f179 GJ |
1022 | |
1023 | dev = MKDEV(major, minor); | |
1024 | ||
ece84241 TH |
1025 | if (strict_strtoull(s[1], 10, &temp)) |
1026 | goto out; | |
34d0f179 | 1027 | |
e56da7e2 | 1028 | disk = get_gendisk(dev, &part); |
4bfd482e | 1029 | if (!disk || part) |
e56da7e2 | 1030 | goto out; |
e56da7e2 TH |
1031 | |
1032 | rcu_read_lock(); | |
1033 | ||
4bfd482e | 1034 | spin_lock_irq(disk->queue->queue_lock); |
aaec55a0 | 1035 | blkg = blkg_lookup_create(blkcg, disk->queue, false); |
4bfd482e | 1036 | spin_unlock_irq(disk->queue->queue_lock); |
e56da7e2 | 1037 | |
4bfd482e TH |
1038 | if (IS_ERR(blkg)) { |
1039 | ret = PTR_ERR(blkg); | |
1040 | goto out_unlock; | |
d11bb446 | 1041 | } |
34d0f179 | 1042 | |
549d3aa8 TH |
1043 | pd = blkg->pd[plid]; |
1044 | ||
062a644d VG |
1045 | switch (plid) { |
1046 | case BLKIO_POLICY_PROP: | |
d11bb446 WG |
1047 | if ((temp < BLKIO_WEIGHT_MIN && temp > 0) || |
1048 | temp > BLKIO_WEIGHT_MAX) | |
e56da7e2 | 1049 | goto out_unlock; |
34d0f179 | 1050 | |
549d3aa8 | 1051 | pd->conf.weight = temp; |
c1768268 | 1052 | blkio_update_group_weight(blkg, plid, temp ?: blkcg->weight); |
4c9eefa1 VG |
1053 | break; |
1054 | case BLKIO_POLICY_THROTL: | |
7702e8f4 VG |
1055 | switch(fileid) { |
1056 | case BLKIO_THROTL_read_bps_device: | |
549d3aa8 | 1057 | pd->conf.bps[READ] = temp; |
c1768268 | 1058 | blkio_update_group_bps(blkg, plid, temp ?: -1, fileid); |
e56da7e2 | 1059 | break; |
7702e8f4 | 1060 | case BLKIO_THROTL_write_bps_device: |
549d3aa8 | 1061 | pd->conf.bps[WRITE] = temp; |
c1768268 | 1062 | blkio_update_group_bps(blkg, plid, temp ?: -1, fileid); |
7702e8f4 VG |
1063 | break; |
1064 | case BLKIO_THROTL_read_iops_device: | |
e56da7e2 TH |
1065 | if (temp > THROTL_IOPS_MAX) |
1066 | goto out_unlock; | |
549d3aa8 | 1067 | pd->conf.iops[READ] = temp; |
c1768268 | 1068 | blkio_update_group_iops(blkg, plid, temp ?: -1, fileid); |
e56da7e2 | 1069 | break; |
7702e8f4 | 1070 | case BLKIO_THROTL_write_iops_device: |
d11bb446 | 1071 | if (temp > THROTL_IOPS_MAX) |
e56da7e2 | 1072 | goto out_unlock; |
549d3aa8 | 1073 | pd->conf.iops[WRITE] = temp; |
c1768268 | 1074 | blkio_update_group_iops(blkg, plid, temp ?: -1, fileid); |
7702e8f4 VG |
1075 | break; |
1076 | } | |
062a644d VG |
1077 | break; |
1078 | default: | |
1079 | BUG(); | |
1080 | } | |
ece84241 | 1081 | ret = 0; |
e56da7e2 TH |
1082 | out_unlock: |
1083 | rcu_read_unlock(); | |
ece84241 TH |
1084 | out: |
1085 | put_disk(disk); | |
e56da7e2 TH |
1086 | |
1087 | /* | |
1088 | * If queue was bypassing, we should retry. Do so after a short | |
1089 | * msleep(). It isn't strictly necessary but queue can be | |
1090 | * bypassing for some time and it's always nice to avoid busy | |
1091 | * looping. | |
1092 | */ | |
1093 | if (ret == -EBUSY) { | |
1094 | msleep(10); | |
1095 | return restart_syscall(); | |
1096 | } | |
ece84241 | 1097 | return ret; |
34d0f179 GJ |
1098 | } |
1099 | ||
062a644d VG |
1100 | static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft, |
1101 | const char *buffer) | |
34d0f179 GJ |
1102 | { |
1103 | int ret = 0; | |
1104 | char *buf; | |
e56da7e2 | 1105 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
062a644d VG |
1106 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); |
1107 | int fileid = BLKIOFILE_ATTR(cft->private); | |
34d0f179 GJ |
1108 | |
1109 | buf = kstrdup(buffer, GFP_KERNEL); | |
1110 | if (!buf) | |
1111 | return -ENOMEM; | |
1112 | ||
4bfd482e | 1113 | ret = blkio_policy_parse_and_set(buf, plid, fileid, blkcg); |
34d0f179 GJ |
1114 | kfree(buf); |
1115 | return ret; | |
1116 | } | |
1117 | ||
4bfd482e TH |
1118 | static void blkio_print_group_conf(struct cftype *cft, struct blkio_group *blkg, |
1119 | struct seq_file *m) | |
34d0f179 | 1120 | { |
c1768268 | 1121 | int plid = BLKIOFILE_POLICY(cft->private); |
4bfd482e | 1122 | int fileid = BLKIOFILE_ATTR(cft->private); |
c1768268 TH |
1123 | struct blkg_policy_data *pd = blkg->pd[plid]; |
1124 | const char *dname = blkg_dev_name(blkg); | |
4bfd482e TH |
1125 | int rw = WRITE; |
1126 | ||
92616b5b VG |
1127 | if (!dname) |
1128 | return; | |
1129 | ||
c1768268 | 1130 | switch (plid) { |
062a644d | 1131 | case BLKIO_POLICY_PROP: |
549d3aa8 | 1132 | if (pd->conf.weight) |
7a4dd281 | 1133 | seq_printf(m, "%s\t%u\n", |
549d3aa8 | 1134 | dname, pd->conf.weight); |
4c9eefa1 VG |
1135 | break; |
1136 | case BLKIO_POLICY_THROTL: | |
4bfd482e | 1137 | switch (fileid) { |
7702e8f4 | 1138 | case BLKIO_THROTL_read_bps_device: |
4bfd482e | 1139 | rw = READ; |
7702e8f4 | 1140 | case BLKIO_THROTL_write_bps_device: |
549d3aa8 | 1141 | if (pd->conf.bps[rw]) |
7a4dd281 | 1142 | seq_printf(m, "%s\t%llu\n", |
549d3aa8 | 1143 | dname, pd->conf.bps[rw]); |
7702e8f4 VG |
1144 | break; |
1145 | case BLKIO_THROTL_read_iops_device: | |
4bfd482e | 1146 | rw = READ; |
7702e8f4 | 1147 | case BLKIO_THROTL_write_iops_device: |
549d3aa8 | 1148 | if (pd->conf.iops[rw]) |
7a4dd281 | 1149 | seq_printf(m, "%s\t%u\n", |
549d3aa8 | 1150 | dname, pd->conf.iops[rw]); |
7702e8f4 VG |
1151 | break; |
1152 | } | |
062a644d VG |
1153 | break; |
1154 | default: | |
1155 | BUG(); | |
1156 | } | |
1157 | } | |
34d0f179 | 1158 | |
062a644d | 1159 | /* cgroup files which read their data from policy nodes end up here */ |
4bfd482e TH |
1160 | static void blkio_read_conf(struct cftype *cft, struct blkio_cgroup *blkcg, |
1161 | struct seq_file *m) | |
34d0f179 | 1162 | { |
4bfd482e TH |
1163 | struct blkio_group *blkg; |
1164 | struct hlist_node *n; | |
34d0f179 | 1165 | |
4bfd482e TH |
1166 | spin_lock_irq(&blkcg->lock); |
1167 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) | |
e8989fae | 1168 | blkio_print_group_conf(cft, blkg, m); |
4bfd482e | 1169 | spin_unlock_irq(&blkcg->lock); |
062a644d VG |
1170 | } |
1171 | ||
1172 | static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft, | |
1173 | struct seq_file *m) | |
1174 | { | |
1175 | struct blkio_cgroup *blkcg; | |
1176 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1177 | int name = BLKIOFILE_ATTR(cft->private); | |
1178 | ||
1179 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1180 | ||
1181 | switch(plid) { | |
1182 | case BLKIO_POLICY_PROP: | |
1183 | switch(name) { | |
1184 | case BLKIO_PROP_weight_device: | |
4bfd482e | 1185 | blkio_read_conf(cft, blkcg, m); |
062a644d VG |
1186 | return 0; |
1187 | default: | |
1188 | BUG(); | |
1189 | } | |
1190 | break; | |
4c9eefa1 VG |
1191 | case BLKIO_POLICY_THROTL: |
1192 | switch(name){ | |
1193 | case BLKIO_THROTL_read_bps_device: | |
1194 | case BLKIO_THROTL_write_bps_device: | |
7702e8f4 VG |
1195 | case BLKIO_THROTL_read_iops_device: |
1196 | case BLKIO_THROTL_write_iops_device: | |
4bfd482e | 1197 | blkio_read_conf(cft, blkcg, m); |
4c9eefa1 VG |
1198 | return 0; |
1199 | default: | |
1200 | BUG(); | |
1201 | } | |
1202 | break; | |
062a644d VG |
1203 | default: |
1204 | BUG(); | |
1205 | } | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
4bfd482e | 1210 | static int blkio_weight_write(struct blkio_cgroup *blkcg, int plid, u64 val) |
062a644d VG |
1211 | { |
1212 | struct blkio_group *blkg; | |
1213 | struct hlist_node *n; | |
062a644d VG |
1214 | |
1215 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) | |
1216 | return -EINVAL; | |
1217 | ||
1218 | spin_lock(&blkio_list_lock); | |
1219 | spin_lock_irq(&blkcg->lock); | |
1220 | blkcg->weight = (unsigned int)val; | |
1221 | ||
549d3aa8 | 1222 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
e8989fae | 1223 | struct blkg_policy_data *pd = blkg->pd[plid]; |
549d3aa8 | 1224 | |
e8989fae | 1225 | if (!pd->conf.weight) |
c1768268 | 1226 | blkio_update_group_weight(blkg, plid, blkcg->weight); |
549d3aa8 | 1227 | } |
062a644d | 1228 | |
062a644d VG |
1229 | spin_unlock_irq(&blkcg->lock); |
1230 | spin_unlock(&blkio_list_lock); | |
1231 | return 0; | |
1232 | } | |
1233 | ||
1234 | static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) { | |
1235 | struct blkio_cgroup *blkcg; | |
1236 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1237 | int name = BLKIOFILE_ATTR(cft->private); | |
1238 | ||
1239 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1240 | ||
1241 | switch(plid) { | |
1242 | case BLKIO_POLICY_PROP: | |
1243 | switch(name) { | |
1244 | case BLKIO_PROP_weight: | |
1245 | return (u64)blkcg->weight; | |
1246 | } | |
1247 | break; | |
1248 | default: | |
1249 | BUG(); | |
1250 | } | |
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | static int | |
1255 | blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val) | |
1256 | { | |
1257 | struct blkio_cgroup *blkcg; | |
1258 | enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private); | |
1259 | int name = BLKIOFILE_ATTR(cft->private); | |
1260 | ||
1261 | blkcg = cgroup_to_blkio_cgroup(cgrp); | |
1262 | ||
1263 | switch(plid) { | |
1264 | case BLKIO_POLICY_PROP: | |
1265 | switch(name) { | |
1266 | case BLKIO_PROP_weight: | |
4bfd482e | 1267 | return blkio_weight_write(blkcg, plid, val); |
062a644d VG |
1268 | } |
1269 | break; | |
1270 | default: | |
1271 | BUG(); | |
1272 | } | |
34d0f179 | 1273 | |
34d0f179 GJ |
1274 | return 0; |
1275 | } | |
1276 | ||
31e4c28d | 1277 | struct cftype blkio_files[] = { |
34d0f179 GJ |
1278 | { |
1279 | .name = "weight_device", | |
062a644d VG |
1280 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1281 | BLKIO_PROP_weight_device), | |
1282 | .read_seq_string = blkiocg_file_read, | |
1283 | .write_string = blkiocg_file_write, | |
34d0f179 GJ |
1284 | .max_write_len = 256, |
1285 | }, | |
31e4c28d VG |
1286 | { |
1287 | .name = "weight", | |
062a644d VG |
1288 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1289 | BLKIO_PROP_weight), | |
1290 | .read_u64 = blkiocg_file_read_u64, | |
1291 | .write_u64 = blkiocg_file_write_u64, | |
31e4c28d | 1292 | }, |
22084190 VG |
1293 | { |
1294 | .name = "time", | |
d3d32e69 TH |
1295 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1296 | offsetof(struct blkio_group_stats, time)), | |
1297 | .read_seq_string = blkcg_print_stat, | |
22084190 VG |
1298 | }, |
1299 | { | |
1300 | .name = "sectors", | |
d3d32e69 TH |
1301 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1302 | offsetof(struct blkio_group_stats_cpu, sectors)), | |
1303 | .read_seq_string = blkcg_print_cpu_stat, | |
303a3acb DS |
1304 | }, |
1305 | { | |
1306 | .name = "io_service_bytes", | |
d3d32e69 TH |
1307 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1308 | offsetof(struct blkio_group_stats_cpu, service_bytes)), | |
1309 | .read_seq_string = blkcg_print_cpu_rwstat, | |
303a3acb DS |
1310 | }, |
1311 | { | |
1312 | .name = "io_serviced", | |
d3d32e69 TH |
1313 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1314 | offsetof(struct blkio_group_stats_cpu, serviced)), | |
1315 | .read_seq_string = blkcg_print_cpu_rwstat, | |
303a3acb DS |
1316 | }, |
1317 | { | |
1318 | .name = "io_service_time", | |
d3d32e69 TH |
1319 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1320 | offsetof(struct blkio_group_stats, service_time)), | |
1321 | .read_seq_string = blkcg_print_rwstat, | |
303a3acb DS |
1322 | }, |
1323 | { | |
1324 | .name = "io_wait_time", | |
d3d32e69 TH |
1325 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1326 | offsetof(struct blkio_group_stats, wait_time)), | |
1327 | .read_seq_string = blkcg_print_rwstat, | |
84c124da | 1328 | }, |
812d4026 DS |
1329 | { |
1330 | .name = "io_merged", | |
d3d32e69 TH |
1331 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1332 | offsetof(struct blkio_group_stats, merged)), | |
1333 | .read_seq_string = blkcg_print_rwstat, | |
812d4026 | 1334 | }, |
cdc1184c DS |
1335 | { |
1336 | .name = "io_queued", | |
d3d32e69 TH |
1337 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1338 | offsetof(struct blkio_group_stats, queued)), | |
1339 | .read_seq_string = blkcg_print_rwstat, | |
cdc1184c | 1340 | }, |
84c124da DS |
1341 | { |
1342 | .name = "reset_stats", | |
1343 | .write_u64 = blkiocg_reset_stats, | |
22084190 | 1344 | }, |
13f98250 VG |
1345 | #ifdef CONFIG_BLK_DEV_THROTTLING |
1346 | { | |
1347 | .name = "throttle.read_bps_device", | |
1348 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1349 | BLKIO_THROTL_read_bps_device), | |
1350 | .read_seq_string = blkiocg_file_read, | |
1351 | .write_string = blkiocg_file_write, | |
1352 | .max_write_len = 256, | |
1353 | }, | |
1354 | ||
1355 | { | |
1356 | .name = "throttle.write_bps_device", | |
1357 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1358 | BLKIO_THROTL_write_bps_device), | |
1359 | .read_seq_string = blkiocg_file_read, | |
1360 | .write_string = blkiocg_file_write, | |
1361 | .max_write_len = 256, | |
1362 | }, | |
1363 | ||
1364 | { | |
1365 | .name = "throttle.read_iops_device", | |
1366 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1367 | BLKIO_THROTL_read_iops_device), | |
1368 | .read_seq_string = blkiocg_file_read, | |
1369 | .write_string = blkiocg_file_write, | |
1370 | .max_write_len = 256, | |
1371 | }, | |
1372 | ||
1373 | { | |
1374 | .name = "throttle.write_iops_device", | |
1375 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1376 | BLKIO_THROTL_write_iops_device), | |
1377 | .read_seq_string = blkiocg_file_read, | |
1378 | .write_string = blkiocg_file_write, | |
1379 | .max_write_len = 256, | |
1380 | }, | |
1381 | { | |
1382 | .name = "throttle.io_service_bytes", | |
d3d32e69 TH |
1383 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
1384 | offsetof(struct blkio_group_stats_cpu, service_bytes)), | |
1385 | .read_seq_string = blkcg_print_cpu_rwstat, | |
13f98250 VG |
1386 | }, |
1387 | { | |
1388 | .name = "throttle.io_serviced", | |
d3d32e69 TH |
1389 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
1390 | offsetof(struct blkio_group_stats_cpu, serviced)), | |
1391 | .read_seq_string = blkcg_print_cpu_rwstat, | |
13f98250 VG |
1392 | }, |
1393 | #endif /* CONFIG_BLK_DEV_THROTTLING */ | |
1394 | ||
22084190 | 1395 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
cdc1184c DS |
1396 | { |
1397 | .name = "avg_queue_size", | |
d3d32e69 | 1398 | .read_seq_string = blkcg_print_avg_queue_size, |
cdc1184c | 1399 | }, |
812df48d DS |
1400 | { |
1401 | .name = "group_wait_time", | |
d3d32e69 TH |
1402 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1403 | offsetof(struct blkio_group_stats, group_wait_time)), | |
1404 | .read_seq_string = blkcg_print_stat, | |
812df48d DS |
1405 | }, |
1406 | { | |
1407 | .name = "idle_time", | |
d3d32e69 TH |
1408 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1409 | offsetof(struct blkio_group_stats, idle_time)), | |
1410 | .read_seq_string = blkcg_print_stat, | |
812df48d DS |
1411 | }, |
1412 | { | |
1413 | .name = "empty_time", | |
d3d32e69 TH |
1414 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1415 | offsetof(struct blkio_group_stats, empty_time)), | |
1416 | .read_seq_string = blkcg_print_stat, | |
812df48d | 1417 | }, |
cdc1184c | 1418 | { |
22084190 | 1419 | .name = "dequeue", |
d3d32e69 TH |
1420 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1421 | offsetof(struct blkio_group_stats, dequeue)), | |
1422 | .read_seq_string = blkcg_print_stat, | |
cdc1184c | 1423 | }, |
9026e521 JT |
1424 | { |
1425 | .name = "unaccounted_time", | |
d3d32e69 TH |
1426 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1427 | offsetof(struct blkio_group_stats, unaccounted_time)), | |
1428 | .read_seq_string = blkcg_print_stat, | |
9026e521 | 1429 | }, |
22084190 | 1430 | #endif |
4baf6e33 | 1431 | { } /* terminate */ |
31e4c28d VG |
1432 | }; |
1433 | ||
9f13ef67 TH |
1434 | /** |
1435 | * blkiocg_pre_destroy - cgroup pre_destroy callback | |
9f13ef67 TH |
1436 | * @cgroup: cgroup of interest |
1437 | * | |
1438 | * This function is called when @cgroup is about to go away and responsible | |
1439 | * for shooting down all blkgs associated with @cgroup. blkgs should be | |
1440 | * removed while holding both q and blkcg locks. As blkcg lock is nested | |
1441 | * inside q lock, this function performs reverse double lock dancing. | |
1442 | * | |
1443 | * This is the blkcg counterpart of ioc_release_fn(). | |
1444 | */ | |
959d851c | 1445 | static int blkiocg_pre_destroy(struct cgroup *cgroup) |
31e4c28d VG |
1446 | { |
1447 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
b1c35769 | 1448 | |
9f13ef67 | 1449 | spin_lock_irq(&blkcg->lock); |
7ee9c562 | 1450 | |
9f13ef67 TH |
1451 | while (!hlist_empty(&blkcg->blkg_list)) { |
1452 | struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first, | |
1453 | struct blkio_group, blkcg_node); | |
c875f4d0 | 1454 | struct request_queue *q = blkg->q; |
b1c35769 | 1455 | |
9f13ef67 TH |
1456 | if (spin_trylock(q->queue_lock)) { |
1457 | blkg_destroy(blkg); | |
1458 | spin_unlock(q->queue_lock); | |
1459 | } else { | |
1460 | spin_unlock_irq(&blkcg->lock); | |
9f13ef67 | 1461 | cpu_relax(); |
a5567932 | 1462 | spin_lock_irq(&blkcg->lock); |
0f3942a3 | 1463 | } |
9f13ef67 | 1464 | } |
b1c35769 | 1465 | |
9f13ef67 | 1466 | spin_unlock_irq(&blkcg->lock); |
7ee9c562 TH |
1467 | return 0; |
1468 | } | |
1469 | ||
959d851c | 1470 | static void blkiocg_destroy(struct cgroup *cgroup) |
7ee9c562 TH |
1471 | { |
1472 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
1473 | ||
67523c48 BB |
1474 | if (blkcg != &blkio_root_cgroup) |
1475 | kfree(blkcg); | |
31e4c28d VG |
1476 | } |
1477 | ||
761b3ef5 | 1478 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup) |
31e4c28d | 1479 | { |
9a9e8a26 | 1480 | static atomic64_t id_seq = ATOMIC64_INIT(0); |
0341509f LZ |
1481 | struct blkio_cgroup *blkcg; |
1482 | struct cgroup *parent = cgroup->parent; | |
31e4c28d | 1483 | |
0341509f | 1484 | if (!parent) { |
31e4c28d VG |
1485 | blkcg = &blkio_root_cgroup; |
1486 | goto done; | |
1487 | } | |
1488 | ||
31e4c28d VG |
1489 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
1490 | if (!blkcg) | |
1491 | return ERR_PTR(-ENOMEM); | |
1492 | ||
1493 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; | |
9a9e8a26 | 1494 | blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */ |
31e4c28d VG |
1495 | done: |
1496 | spin_lock_init(&blkcg->lock); | |
1497 | INIT_HLIST_HEAD(&blkcg->blkg_list); | |
1498 | ||
1499 | return &blkcg->css; | |
1500 | } | |
1501 | ||
5efd6113 TH |
1502 | /** |
1503 | * blkcg_init_queue - initialize blkcg part of request queue | |
1504 | * @q: request_queue to initialize | |
1505 | * | |
1506 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
1507 | * part of new request_queue @q. | |
1508 | * | |
1509 | * RETURNS: | |
1510 | * 0 on success, -errno on failure. | |
1511 | */ | |
1512 | int blkcg_init_queue(struct request_queue *q) | |
1513 | { | |
923adde1 TH |
1514 | int ret; |
1515 | ||
5efd6113 TH |
1516 | might_sleep(); |
1517 | ||
923adde1 TH |
1518 | ret = blk_throtl_init(q); |
1519 | if (ret) | |
1520 | return ret; | |
1521 | ||
1522 | mutex_lock(&all_q_mutex); | |
1523 | INIT_LIST_HEAD(&q->all_q_node); | |
1524 | list_add_tail(&q->all_q_node, &all_q_list); | |
1525 | mutex_unlock(&all_q_mutex); | |
1526 | ||
1527 | return 0; | |
5efd6113 TH |
1528 | } |
1529 | ||
1530 | /** | |
1531 | * blkcg_drain_queue - drain blkcg part of request_queue | |
1532 | * @q: request_queue to drain | |
1533 | * | |
1534 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
1535 | */ | |
1536 | void blkcg_drain_queue(struct request_queue *q) | |
1537 | { | |
1538 | lockdep_assert_held(q->queue_lock); | |
1539 | ||
1540 | blk_throtl_drain(q); | |
1541 | } | |
1542 | ||
1543 | /** | |
1544 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
1545 | * @q: request_queue being released | |
1546 | * | |
1547 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
1548 | */ | |
1549 | void blkcg_exit_queue(struct request_queue *q) | |
1550 | { | |
923adde1 TH |
1551 | mutex_lock(&all_q_mutex); |
1552 | list_del_init(&q->all_q_node); | |
1553 | mutex_unlock(&all_q_mutex); | |
1554 | ||
e8989fae TH |
1555 | blkg_destroy_all(q, true); |
1556 | ||
5efd6113 TH |
1557 | blk_throtl_exit(q); |
1558 | } | |
1559 | ||
31e4c28d VG |
1560 | /* |
1561 | * We cannot support shared io contexts, as we have no mean to support | |
1562 | * two tasks with the same ioc in two different groups without major rework | |
1563 | * of the main cic data structures. For now we allow a task to change | |
1564 | * its cgroup only if it's the only owner of its ioc. | |
1565 | */ | |
761b3ef5 | 1566 | static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
31e4c28d | 1567 | { |
bb9d97b6 | 1568 | struct task_struct *task; |
31e4c28d VG |
1569 | struct io_context *ioc; |
1570 | int ret = 0; | |
1571 | ||
1572 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
bb9d97b6 TH |
1573 | cgroup_taskset_for_each(task, cgrp, tset) { |
1574 | task_lock(task); | |
1575 | ioc = task->io_context; | |
1576 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
1577 | ret = -EINVAL; | |
1578 | task_unlock(task); | |
1579 | if (ret) | |
1580 | break; | |
1581 | } | |
31e4c28d VG |
1582 | return ret; |
1583 | } | |
1584 | ||
923adde1 TH |
1585 | static void blkcg_bypass_start(void) |
1586 | __acquires(&all_q_mutex) | |
1587 | { | |
1588 | struct request_queue *q; | |
1589 | ||
1590 | mutex_lock(&all_q_mutex); | |
1591 | ||
1592 | list_for_each_entry(q, &all_q_list, all_q_node) { | |
1593 | blk_queue_bypass_start(q); | |
e8989fae | 1594 | blkg_destroy_all(q, false); |
923adde1 TH |
1595 | } |
1596 | } | |
1597 | ||
1598 | static void blkcg_bypass_end(void) | |
1599 | __releases(&all_q_mutex) | |
1600 | { | |
1601 | struct request_queue *q; | |
1602 | ||
1603 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1604 | blk_queue_bypass_end(q); | |
1605 | ||
1606 | mutex_unlock(&all_q_mutex); | |
1607 | } | |
1608 | ||
676f7c8f TH |
1609 | struct cgroup_subsys blkio_subsys = { |
1610 | .name = "blkio", | |
1611 | .create = blkiocg_create, | |
1612 | .can_attach = blkiocg_can_attach, | |
959d851c | 1613 | .pre_destroy = blkiocg_pre_destroy, |
676f7c8f | 1614 | .destroy = blkiocg_destroy, |
676f7c8f | 1615 | .subsys_id = blkio_subsys_id, |
4baf6e33 | 1616 | .base_cftypes = blkio_files, |
676f7c8f TH |
1617 | .module = THIS_MODULE, |
1618 | }; | |
1619 | EXPORT_SYMBOL_GPL(blkio_subsys); | |
1620 | ||
3e252066 VG |
1621 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
1622 | { | |
e8989fae TH |
1623 | struct request_queue *q; |
1624 | ||
923adde1 | 1625 | blkcg_bypass_start(); |
3e252066 | 1626 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1627 | |
1628 | BUG_ON(blkio_policy[blkiop->plid]); | |
1629 | blkio_policy[blkiop->plid] = blkiop; | |
3e252066 | 1630 | list_add_tail(&blkiop->list, &blkio_list); |
035d10b2 | 1631 | |
3e252066 | 1632 | spin_unlock(&blkio_list_lock); |
e8989fae TH |
1633 | list_for_each_entry(q, &all_q_list, all_q_node) |
1634 | update_root_blkg_pd(q, blkiop->plid); | |
923adde1 | 1635 | blkcg_bypass_end(); |
3e252066 VG |
1636 | } |
1637 | EXPORT_SYMBOL_GPL(blkio_policy_register); | |
1638 | ||
1639 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) | |
1640 | { | |
e8989fae TH |
1641 | struct request_queue *q; |
1642 | ||
923adde1 | 1643 | blkcg_bypass_start(); |
3e252066 | 1644 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1645 | |
1646 | BUG_ON(blkio_policy[blkiop->plid] != blkiop); | |
1647 | blkio_policy[blkiop->plid] = NULL; | |
3e252066 | 1648 | list_del_init(&blkiop->list); |
035d10b2 | 1649 | |
3e252066 | 1650 | spin_unlock(&blkio_list_lock); |
e8989fae TH |
1651 | list_for_each_entry(q, &all_q_list, all_q_node) |
1652 | update_root_blkg_pd(q, blkiop->plid); | |
923adde1 | 1653 | blkcg_bypass_end(); |
3e252066 VG |
1654 | } |
1655 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |