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 | ||
c4682aec TH |
1118 | /* for propio conf */ |
1119 | static u64 blkg_prfill_weight_device(struct seq_file *sf, | |
1120 | struct blkg_policy_data *pd, int off) | |
34d0f179 | 1121 | { |
c4682aec TH |
1122 | if (!pd->conf.weight) |
1123 | return 0; | |
1124 | return __blkg_prfill_u64(sf, pd, pd->conf.weight); | |
062a644d | 1125 | } |
34d0f179 | 1126 | |
c4682aec TH |
1127 | static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft, |
1128 | struct seq_file *sf) | |
34d0f179 | 1129 | { |
c4682aec TH |
1130 | blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), |
1131 | blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0, | |
1132 | false); | |
1133 | return 0; | |
062a644d VG |
1134 | } |
1135 | ||
c4682aec TH |
1136 | static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft, |
1137 | struct seq_file *sf) | |
062a644d | 1138 | { |
c4682aec | 1139 | seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight); |
062a644d VG |
1140 | return 0; |
1141 | } | |
1142 | ||
627f29f4 | 1143 | static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val) |
062a644d | 1144 | { |
627f29f4 | 1145 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp); |
062a644d VG |
1146 | struct blkio_group *blkg; |
1147 | struct hlist_node *n; | |
062a644d VG |
1148 | |
1149 | if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX) | |
1150 | return -EINVAL; | |
1151 | ||
1152 | spin_lock(&blkio_list_lock); | |
1153 | spin_lock_irq(&blkcg->lock); | |
1154 | blkcg->weight = (unsigned int)val; | |
1155 | ||
549d3aa8 | 1156 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
627f29f4 | 1157 | struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP]; |
549d3aa8 | 1158 | |
627f29f4 TH |
1159 | if (pd && !pd->conf.weight) |
1160 | blkio_update_group_weight(blkg, BLKIO_POLICY_PROP, | |
1161 | blkcg->weight); | |
549d3aa8 | 1162 | } |
062a644d | 1163 | |
062a644d VG |
1164 | spin_unlock_irq(&blkcg->lock); |
1165 | spin_unlock(&blkio_list_lock); | |
1166 | return 0; | |
1167 | } | |
1168 | ||
c4682aec TH |
1169 | /* for blk-throttle conf */ |
1170 | #ifdef CONFIG_BLK_DEV_THROTTLING | |
1171 | static u64 blkg_prfill_conf_u64(struct seq_file *sf, | |
1172 | struct blkg_policy_data *pd, int off) | |
1173 | { | |
1174 | u64 v = *(u64 *)((void *)&pd->conf + off); | |
1175 | ||
1176 | if (!v) | |
1177 | return 0; | |
1178 | return __blkg_prfill_u64(sf, pd, v); | |
1179 | } | |
062a644d | 1180 | |
c4682aec TH |
1181 | static int blkcg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft, |
1182 | struct seq_file *sf) | |
1183 | { | |
1184 | int off; | |
062a644d | 1185 | |
c4682aec TH |
1186 | switch (BLKIOFILE_ATTR(cft->private)) { |
1187 | case BLKIO_THROTL_read_bps_device: | |
1188 | off = offsetof(struct blkio_group_conf, bps[READ]); | |
1189 | break; | |
1190 | case BLKIO_THROTL_write_bps_device: | |
1191 | off = offsetof(struct blkio_group_conf, bps[WRITE]); | |
1192 | break; | |
1193 | case BLKIO_THROTL_read_iops_device: | |
1194 | off = offsetof(struct blkio_group_conf, iops[READ]); | |
1195 | break; | |
1196 | case BLKIO_THROTL_write_iops_device: | |
1197 | off = offsetof(struct blkio_group_conf, iops[WRITE]); | |
062a644d VG |
1198 | break; |
1199 | default: | |
c4682aec | 1200 | return -EINVAL; |
062a644d | 1201 | } |
c4682aec TH |
1202 | |
1203 | blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), | |
1204 | blkg_prfill_conf_u64, BLKIO_POLICY_THROTL, | |
1205 | off, false); | |
062a644d VG |
1206 | return 0; |
1207 | } | |
c4682aec | 1208 | #endif |
062a644d | 1209 | |
31e4c28d | 1210 | struct cftype blkio_files[] = { |
34d0f179 GJ |
1211 | { |
1212 | .name = "weight_device", | |
062a644d VG |
1213 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP, |
1214 | BLKIO_PROP_weight_device), | |
c4682aec | 1215 | .read_seq_string = blkcg_print_weight_device, |
062a644d | 1216 | .write_string = blkiocg_file_write, |
34d0f179 GJ |
1217 | .max_write_len = 256, |
1218 | }, | |
31e4c28d VG |
1219 | { |
1220 | .name = "weight", | |
c4682aec | 1221 | .read_seq_string = blkcg_print_weight, |
627f29f4 | 1222 | .write_u64 = blkcg_set_weight, |
31e4c28d | 1223 | }, |
22084190 VG |
1224 | { |
1225 | .name = "time", | |
d3d32e69 TH |
1226 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1227 | offsetof(struct blkio_group_stats, time)), | |
1228 | .read_seq_string = blkcg_print_stat, | |
22084190 VG |
1229 | }, |
1230 | { | |
1231 | .name = "sectors", | |
d3d32e69 TH |
1232 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1233 | offsetof(struct blkio_group_stats_cpu, sectors)), | |
1234 | .read_seq_string = blkcg_print_cpu_stat, | |
303a3acb DS |
1235 | }, |
1236 | { | |
1237 | .name = "io_service_bytes", | |
d3d32e69 TH |
1238 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1239 | offsetof(struct blkio_group_stats_cpu, service_bytes)), | |
1240 | .read_seq_string = blkcg_print_cpu_rwstat, | |
303a3acb DS |
1241 | }, |
1242 | { | |
1243 | .name = "io_serviced", | |
d3d32e69 TH |
1244 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1245 | offsetof(struct blkio_group_stats_cpu, serviced)), | |
1246 | .read_seq_string = blkcg_print_cpu_rwstat, | |
303a3acb DS |
1247 | }, |
1248 | { | |
1249 | .name = "io_service_time", | |
d3d32e69 TH |
1250 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1251 | offsetof(struct blkio_group_stats, service_time)), | |
1252 | .read_seq_string = blkcg_print_rwstat, | |
303a3acb DS |
1253 | }, |
1254 | { | |
1255 | .name = "io_wait_time", | |
d3d32e69 TH |
1256 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1257 | offsetof(struct blkio_group_stats, wait_time)), | |
1258 | .read_seq_string = blkcg_print_rwstat, | |
84c124da | 1259 | }, |
812d4026 DS |
1260 | { |
1261 | .name = "io_merged", | |
d3d32e69 TH |
1262 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1263 | offsetof(struct blkio_group_stats, merged)), | |
1264 | .read_seq_string = blkcg_print_rwstat, | |
812d4026 | 1265 | }, |
cdc1184c DS |
1266 | { |
1267 | .name = "io_queued", | |
d3d32e69 TH |
1268 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1269 | offsetof(struct blkio_group_stats, queued)), | |
1270 | .read_seq_string = blkcg_print_rwstat, | |
cdc1184c | 1271 | }, |
84c124da DS |
1272 | { |
1273 | .name = "reset_stats", | |
1274 | .write_u64 = blkiocg_reset_stats, | |
22084190 | 1275 | }, |
13f98250 VG |
1276 | #ifdef CONFIG_BLK_DEV_THROTTLING |
1277 | { | |
1278 | .name = "throttle.read_bps_device", | |
1279 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1280 | BLKIO_THROTL_read_bps_device), | |
c4682aec | 1281 | .read_seq_string = blkcg_print_conf_u64, |
13f98250 VG |
1282 | .write_string = blkiocg_file_write, |
1283 | .max_write_len = 256, | |
1284 | }, | |
1285 | ||
1286 | { | |
1287 | .name = "throttle.write_bps_device", | |
1288 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1289 | BLKIO_THROTL_write_bps_device), | |
c4682aec | 1290 | .read_seq_string = blkcg_print_conf_u64, |
13f98250 VG |
1291 | .write_string = blkiocg_file_write, |
1292 | .max_write_len = 256, | |
1293 | }, | |
1294 | ||
1295 | { | |
1296 | .name = "throttle.read_iops_device", | |
1297 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1298 | BLKIO_THROTL_read_iops_device), | |
c4682aec | 1299 | .read_seq_string = blkcg_print_conf_u64, |
13f98250 VG |
1300 | .write_string = blkiocg_file_write, |
1301 | .max_write_len = 256, | |
1302 | }, | |
1303 | ||
1304 | { | |
1305 | .name = "throttle.write_iops_device", | |
1306 | .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL, | |
1307 | BLKIO_THROTL_write_iops_device), | |
c4682aec | 1308 | .read_seq_string = blkcg_print_conf_u64, |
13f98250 VG |
1309 | .write_string = blkiocg_file_write, |
1310 | .max_write_len = 256, | |
1311 | }, | |
1312 | { | |
1313 | .name = "throttle.io_service_bytes", | |
d3d32e69 TH |
1314 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
1315 | offsetof(struct blkio_group_stats_cpu, service_bytes)), | |
1316 | .read_seq_string = blkcg_print_cpu_rwstat, | |
13f98250 VG |
1317 | }, |
1318 | { | |
1319 | .name = "throttle.io_serviced", | |
d3d32e69 TH |
1320 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_THROTL, |
1321 | offsetof(struct blkio_group_stats_cpu, serviced)), | |
1322 | .read_seq_string = blkcg_print_cpu_rwstat, | |
13f98250 VG |
1323 | }, |
1324 | #endif /* CONFIG_BLK_DEV_THROTTLING */ | |
1325 | ||
22084190 | 1326 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
cdc1184c DS |
1327 | { |
1328 | .name = "avg_queue_size", | |
d3d32e69 | 1329 | .read_seq_string = blkcg_print_avg_queue_size, |
cdc1184c | 1330 | }, |
812df48d DS |
1331 | { |
1332 | .name = "group_wait_time", | |
d3d32e69 TH |
1333 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1334 | offsetof(struct blkio_group_stats, group_wait_time)), | |
1335 | .read_seq_string = blkcg_print_stat, | |
812df48d DS |
1336 | }, |
1337 | { | |
1338 | .name = "idle_time", | |
d3d32e69 TH |
1339 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1340 | offsetof(struct blkio_group_stats, idle_time)), | |
1341 | .read_seq_string = blkcg_print_stat, | |
812df48d DS |
1342 | }, |
1343 | { | |
1344 | .name = "empty_time", | |
d3d32e69 TH |
1345 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1346 | offsetof(struct blkio_group_stats, empty_time)), | |
1347 | .read_seq_string = blkcg_print_stat, | |
812df48d | 1348 | }, |
cdc1184c | 1349 | { |
22084190 | 1350 | .name = "dequeue", |
d3d32e69 TH |
1351 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1352 | offsetof(struct blkio_group_stats, dequeue)), | |
1353 | .read_seq_string = blkcg_print_stat, | |
cdc1184c | 1354 | }, |
9026e521 JT |
1355 | { |
1356 | .name = "unaccounted_time", | |
d3d32e69 TH |
1357 | .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP, |
1358 | offsetof(struct blkio_group_stats, unaccounted_time)), | |
1359 | .read_seq_string = blkcg_print_stat, | |
9026e521 | 1360 | }, |
22084190 | 1361 | #endif |
4baf6e33 | 1362 | { } /* terminate */ |
31e4c28d VG |
1363 | }; |
1364 | ||
9f13ef67 TH |
1365 | /** |
1366 | * blkiocg_pre_destroy - cgroup pre_destroy callback | |
9f13ef67 TH |
1367 | * @cgroup: cgroup of interest |
1368 | * | |
1369 | * This function is called when @cgroup is about to go away and responsible | |
1370 | * for shooting down all blkgs associated with @cgroup. blkgs should be | |
1371 | * removed while holding both q and blkcg locks. As blkcg lock is nested | |
1372 | * inside q lock, this function performs reverse double lock dancing. | |
1373 | * | |
1374 | * This is the blkcg counterpart of ioc_release_fn(). | |
1375 | */ | |
959d851c | 1376 | static int blkiocg_pre_destroy(struct cgroup *cgroup) |
31e4c28d VG |
1377 | { |
1378 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
b1c35769 | 1379 | |
9f13ef67 | 1380 | spin_lock_irq(&blkcg->lock); |
7ee9c562 | 1381 | |
9f13ef67 TH |
1382 | while (!hlist_empty(&blkcg->blkg_list)) { |
1383 | struct blkio_group *blkg = hlist_entry(blkcg->blkg_list.first, | |
1384 | struct blkio_group, blkcg_node); | |
c875f4d0 | 1385 | struct request_queue *q = blkg->q; |
b1c35769 | 1386 | |
9f13ef67 TH |
1387 | if (spin_trylock(q->queue_lock)) { |
1388 | blkg_destroy(blkg); | |
1389 | spin_unlock(q->queue_lock); | |
1390 | } else { | |
1391 | spin_unlock_irq(&blkcg->lock); | |
9f13ef67 | 1392 | cpu_relax(); |
a5567932 | 1393 | spin_lock_irq(&blkcg->lock); |
0f3942a3 | 1394 | } |
9f13ef67 | 1395 | } |
b1c35769 | 1396 | |
9f13ef67 | 1397 | spin_unlock_irq(&blkcg->lock); |
7ee9c562 TH |
1398 | return 0; |
1399 | } | |
1400 | ||
959d851c | 1401 | static void blkiocg_destroy(struct cgroup *cgroup) |
7ee9c562 TH |
1402 | { |
1403 | struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup); | |
1404 | ||
67523c48 BB |
1405 | if (blkcg != &blkio_root_cgroup) |
1406 | kfree(blkcg); | |
31e4c28d VG |
1407 | } |
1408 | ||
761b3ef5 | 1409 | static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup) |
31e4c28d | 1410 | { |
9a9e8a26 | 1411 | static atomic64_t id_seq = ATOMIC64_INIT(0); |
0341509f LZ |
1412 | struct blkio_cgroup *blkcg; |
1413 | struct cgroup *parent = cgroup->parent; | |
31e4c28d | 1414 | |
0341509f | 1415 | if (!parent) { |
31e4c28d VG |
1416 | blkcg = &blkio_root_cgroup; |
1417 | goto done; | |
1418 | } | |
1419 | ||
31e4c28d VG |
1420 | blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL); |
1421 | if (!blkcg) | |
1422 | return ERR_PTR(-ENOMEM); | |
1423 | ||
1424 | blkcg->weight = BLKIO_WEIGHT_DEFAULT; | |
9a9e8a26 | 1425 | blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */ |
31e4c28d VG |
1426 | done: |
1427 | spin_lock_init(&blkcg->lock); | |
1428 | INIT_HLIST_HEAD(&blkcg->blkg_list); | |
1429 | ||
1430 | return &blkcg->css; | |
1431 | } | |
1432 | ||
5efd6113 TH |
1433 | /** |
1434 | * blkcg_init_queue - initialize blkcg part of request queue | |
1435 | * @q: request_queue to initialize | |
1436 | * | |
1437 | * Called from blk_alloc_queue_node(). Responsible for initializing blkcg | |
1438 | * part of new request_queue @q. | |
1439 | * | |
1440 | * RETURNS: | |
1441 | * 0 on success, -errno on failure. | |
1442 | */ | |
1443 | int blkcg_init_queue(struct request_queue *q) | |
1444 | { | |
923adde1 TH |
1445 | int ret; |
1446 | ||
5efd6113 TH |
1447 | might_sleep(); |
1448 | ||
923adde1 TH |
1449 | ret = blk_throtl_init(q); |
1450 | if (ret) | |
1451 | return ret; | |
1452 | ||
1453 | mutex_lock(&all_q_mutex); | |
1454 | INIT_LIST_HEAD(&q->all_q_node); | |
1455 | list_add_tail(&q->all_q_node, &all_q_list); | |
1456 | mutex_unlock(&all_q_mutex); | |
1457 | ||
1458 | return 0; | |
5efd6113 TH |
1459 | } |
1460 | ||
1461 | /** | |
1462 | * blkcg_drain_queue - drain blkcg part of request_queue | |
1463 | * @q: request_queue to drain | |
1464 | * | |
1465 | * Called from blk_drain_queue(). Responsible for draining blkcg part. | |
1466 | */ | |
1467 | void blkcg_drain_queue(struct request_queue *q) | |
1468 | { | |
1469 | lockdep_assert_held(q->queue_lock); | |
1470 | ||
1471 | blk_throtl_drain(q); | |
1472 | } | |
1473 | ||
1474 | /** | |
1475 | * blkcg_exit_queue - exit and release blkcg part of request_queue | |
1476 | * @q: request_queue being released | |
1477 | * | |
1478 | * Called from blk_release_queue(). Responsible for exiting blkcg part. | |
1479 | */ | |
1480 | void blkcg_exit_queue(struct request_queue *q) | |
1481 | { | |
923adde1 TH |
1482 | mutex_lock(&all_q_mutex); |
1483 | list_del_init(&q->all_q_node); | |
1484 | mutex_unlock(&all_q_mutex); | |
1485 | ||
e8989fae TH |
1486 | blkg_destroy_all(q, true); |
1487 | ||
5efd6113 TH |
1488 | blk_throtl_exit(q); |
1489 | } | |
1490 | ||
31e4c28d VG |
1491 | /* |
1492 | * We cannot support shared io contexts, as we have no mean to support | |
1493 | * two tasks with the same ioc in two different groups without major rework | |
1494 | * of the main cic data structures. For now we allow a task to change | |
1495 | * its cgroup only if it's the only owner of its ioc. | |
1496 | */ | |
761b3ef5 | 1497 | static int blkiocg_can_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
31e4c28d | 1498 | { |
bb9d97b6 | 1499 | struct task_struct *task; |
31e4c28d VG |
1500 | struct io_context *ioc; |
1501 | int ret = 0; | |
1502 | ||
1503 | /* task_lock() is needed to avoid races with exit_io_context() */ | |
bb9d97b6 TH |
1504 | cgroup_taskset_for_each(task, cgrp, tset) { |
1505 | task_lock(task); | |
1506 | ioc = task->io_context; | |
1507 | if (ioc && atomic_read(&ioc->nr_tasks) > 1) | |
1508 | ret = -EINVAL; | |
1509 | task_unlock(task); | |
1510 | if (ret) | |
1511 | break; | |
1512 | } | |
31e4c28d VG |
1513 | return ret; |
1514 | } | |
1515 | ||
923adde1 TH |
1516 | static void blkcg_bypass_start(void) |
1517 | __acquires(&all_q_mutex) | |
1518 | { | |
1519 | struct request_queue *q; | |
1520 | ||
1521 | mutex_lock(&all_q_mutex); | |
1522 | ||
1523 | list_for_each_entry(q, &all_q_list, all_q_node) { | |
1524 | blk_queue_bypass_start(q); | |
e8989fae | 1525 | blkg_destroy_all(q, false); |
923adde1 TH |
1526 | } |
1527 | } | |
1528 | ||
1529 | static void blkcg_bypass_end(void) | |
1530 | __releases(&all_q_mutex) | |
1531 | { | |
1532 | struct request_queue *q; | |
1533 | ||
1534 | list_for_each_entry(q, &all_q_list, all_q_node) | |
1535 | blk_queue_bypass_end(q); | |
1536 | ||
1537 | mutex_unlock(&all_q_mutex); | |
1538 | } | |
1539 | ||
676f7c8f TH |
1540 | struct cgroup_subsys blkio_subsys = { |
1541 | .name = "blkio", | |
1542 | .create = blkiocg_create, | |
1543 | .can_attach = blkiocg_can_attach, | |
959d851c | 1544 | .pre_destroy = blkiocg_pre_destroy, |
676f7c8f | 1545 | .destroy = blkiocg_destroy, |
676f7c8f | 1546 | .subsys_id = blkio_subsys_id, |
4baf6e33 | 1547 | .base_cftypes = blkio_files, |
676f7c8f TH |
1548 | .module = THIS_MODULE, |
1549 | }; | |
1550 | EXPORT_SYMBOL_GPL(blkio_subsys); | |
1551 | ||
3e252066 VG |
1552 | void blkio_policy_register(struct blkio_policy_type *blkiop) |
1553 | { | |
e8989fae TH |
1554 | struct request_queue *q; |
1555 | ||
923adde1 | 1556 | blkcg_bypass_start(); |
3e252066 | 1557 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1558 | |
1559 | BUG_ON(blkio_policy[blkiop->plid]); | |
1560 | blkio_policy[blkiop->plid] = blkiop; | |
3e252066 | 1561 | list_add_tail(&blkiop->list, &blkio_list); |
035d10b2 | 1562 | |
3e252066 | 1563 | spin_unlock(&blkio_list_lock); |
e8989fae TH |
1564 | list_for_each_entry(q, &all_q_list, all_q_node) |
1565 | update_root_blkg_pd(q, blkiop->plid); | |
923adde1 | 1566 | blkcg_bypass_end(); |
3e252066 VG |
1567 | } |
1568 | EXPORT_SYMBOL_GPL(blkio_policy_register); | |
1569 | ||
1570 | void blkio_policy_unregister(struct blkio_policy_type *blkiop) | |
1571 | { | |
e8989fae TH |
1572 | struct request_queue *q; |
1573 | ||
923adde1 | 1574 | blkcg_bypass_start(); |
3e252066 | 1575 | spin_lock(&blkio_list_lock); |
035d10b2 TH |
1576 | |
1577 | BUG_ON(blkio_policy[blkiop->plid] != blkiop); | |
1578 | blkio_policy[blkiop->plid] = NULL; | |
3e252066 | 1579 | list_del_init(&blkiop->list); |
035d10b2 | 1580 | |
3e252066 | 1581 | spin_unlock(&blkio_list_lock); |
e8989fae TH |
1582 | list_for_each_entry(q, &all_q_list, all_q_node) |
1583 | update_root_blkg_pd(q, blkiop->plid); | |
923adde1 | 1584 | blkcg_bypass_end(); |
3e252066 VG |
1585 | } |
1586 | EXPORT_SYMBOL_GPL(blkio_policy_unregister); |