]> git.karo-electronics.de Git - karo-tx-linux.git/blob - block/blk-cgroup.h
block: make block cgroup policies follow bio task association
[karo-tx-linux.git] / block / blk-cgroup.h
1 #ifndef _BLK_CGROUP_H
2 #define _BLK_CGROUP_H
3 /*
4  * Common Block IO controller cgroup interface
5  *
6  * Based on ideas and code from CFQ, CFS and BFQ:
7  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
8  *
9  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
10  *                    Paolo Valente <paolo.valente@unimore.it>
11  *
12  * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
13  *                    Nauman Rafique <nauman@google.com>
14  */
15
16 #include <linux/cgroup.h>
17 #include <linux/u64_stats_sync.h>
18
19 enum blkio_policy_id {
20         BLKIO_POLICY_PROP = 0,          /* Proportional Bandwidth division */
21         BLKIO_POLICY_THROTL,            /* Throttling */
22
23         BLKIO_NR_POLICIES,
24 };
25
26 /* Max limits for throttle policy */
27 #define THROTL_IOPS_MAX         UINT_MAX
28
29 #ifdef CONFIG_BLK_CGROUP
30
31 enum stat_type {
32         /* Total time spent (in ns) between request dispatch to the driver and
33          * request completion for IOs doen by this cgroup. This may not be
34          * accurate when NCQ is turned on. */
35         BLKIO_STAT_SERVICE_TIME = 0,
36         /* Total time spent waiting in scheduler queue in ns */
37         BLKIO_STAT_WAIT_TIME,
38         /* Number of IOs queued up */
39         BLKIO_STAT_QUEUED,
40         /* All the single valued stats go below this */
41         BLKIO_STAT_TIME,
42 #ifdef CONFIG_DEBUG_BLK_CGROUP
43         /* Time not charged to this cgroup */
44         BLKIO_STAT_UNACCOUNTED_TIME,
45         BLKIO_STAT_AVG_QUEUE_SIZE,
46         BLKIO_STAT_IDLE_TIME,
47         BLKIO_STAT_EMPTY_TIME,
48         BLKIO_STAT_GROUP_WAIT_TIME,
49         BLKIO_STAT_DEQUEUE
50 #endif
51 };
52
53 /* Per cpu stats */
54 enum stat_type_cpu {
55         BLKIO_STAT_CPU_SECTORS,
56         /* Total bytes transferred */
57         BLKIO_STAT_CPU_SERVICE_BYTES,
58         /* Total IOs serviced, post merge */
59         BLKIO_STAT_CPU_SERVICED,
60         /* Number of IOs merged */
61         BLKIO_STAT_CPU_MERGED,
62         BLKIO_STAT_CPU_NR
63 };
64
65 enum stat_sub_type {
66         BLKIO_STAT_READ = 0,
67         BLKIO_STAT_WRITE,
68         BLKIO_STAT_SYNC,
69         BLKIO_STAT_ASYNC,
70         BLKIO_STAT_TOTAL
71 };
72
73 /* blkg state flags */
74 enum blkg_state_flags {
75         BLKG_waiting = 0,
76         BLKG_idling,
77         BLKG_empty,
78 };
79
80 /* cgroup files owned by proportional weight policy */
81 enum blkcg_file_name_prop {
82         BLKIO_PROP_weight = 1,
83         BLKIO_PROP_weight_device,
84         BLKIO_PROP_io_service_bytes,
85         BLKIO_PROP_io_serviced,
86         BLKIO_PROP_time,
87         BLKIO_PROP_sectors,
88         BLKIO_PROP_unaccounted_time,
89         BLKIO_PROP_io_service_time,
90         BLKIO_PROP_io_wait_time,
91         BLKIO_PROP_io_merged,
92         BLKIO_PROP_io_queued,
93         BLKIO_PROP_avg_queue_size,
94         BLKIO_PROP_group_wait_time,
95         BLKIO_PROP_idle_time,
96         BLKIO_PROP_empty_time,
97         BLKIO_PROP_dequeue,
98 };
99
100 /* cgroup files owned by throttle policy */
101 enum blkcg_file_name_throtl {
102         BLKIO_THROTL_read_bps_device,
103         BLKIO_THROTL_write_bps_device,
104         BLKIO_THROTL_read_iops_device,
105         BLKIO_THROTL_write_iops_device,
106         BLKIO_THROTL_io_service_bytes,
107         BLKIO_THROTL_io_serviced,
108 };
109
110 struct blkio_cgroup {
111         struct cgroup_subsys_state css;
112         unsigned int weight;
113         spinlock_t lock;
114         struct hlist_head blkg_list;
115 };
116
117 struct blkio_group_stats {
118         /* total disk time and nr sectors dispatched by this group */
119         uint64_t time;
120         uint64_t stat_arr[BLKIO_STAT_QUEUED + 1][BLKIO_STAT_TOTAL];
121 #ifdef CONFIG_DEBUG_BLK_CGROUP
122         /* Time not charged to this cgroup */
123         uint64_t unaccounted_time;
124
125         /* Sum of number of IOs queued across all samples */
126         uint64_t avg_queue_size_sum;
127         /* Count of samples taken for average */
128         uint64_t avg_queue_size_samples;
129         /* How many times this group has been removed from service tree */
130         unsigned long dequeue;
131
132         /* Total time spent waiting for it to be assigned a timeslice. */
133         uint64_t group_wait_time;
134         uint64_t start_group_wait_time;
135
136         /* Time spent idling for this blkio_group */
137         uint64_t idle_time;
138         uint64_t start_idle_time;
139         /*
140          * Total time when we have requests queued and do not contain the
141          * current active queue.
142          */
143         uint64_t empty_time;
144         uint64_t start_empty_time;
145         uint16_t flags;
146 #endif
147 };
148
149 /* Per cpu blkio group stats */
150 struct blkio_group_stats_cpu {
151         uint64_t sectors;
152         uint64_t stat_arr_cpu[BLKIO_STAT_CPU_NR][BLKIO_STAT_TOTAL];
153         struct u64_stats_sync syncp;
154 };
155
156 struct blkio_group_conf {
157         unsigned int weight;
158         unsigned int iops[2];
159         u64 bps[2];
160 };
161
162 /* per-blkg per-policy data */
163 struct blkg_policy_data {
164         /* the blkg this per-policy data belongs to */
165         struct blkio_group *blkg;
166
167         /* Configuration */
168         struct blkio_group_conf conf;
169
170         struct blkio_group_stats stats;
171         /* Per cpu stats pointer */
172         struct blkio_group_stats_cpu __percpu *stats_cpu;
173
174         /* pol->pdata_size bytes of private data used by policy impl */
175         char pdata[] __aligned(__alignof__(unsigned long long));
176 };
177
178 struct blkio_group {
179         /* Pointer to the associated request_queue */
180         struct request_queue *q;
181         struct list_head q_node;
182         struct hlist_node blkcg_node;
183         struct blkio_cgroup *blkcg;
184         /* Store cgroup path */
185         char path[128];
186         /* reference count */
187         int refcnt;
188
189         /* Need to serialize the stats in the case of reset/update */
190         spinlock_t stats_lock;
191         struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
192
193         struct rcu_head rcu_head;
194 };
195
196 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
197 typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
198                         struct blkio_group *blkg, unsigned int weight);
199 typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
200                         struct blkio_group *blkg, u64 read_bps);
201 typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
202                         struct blkio_group *blkg, u64 write_bps);
203 typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
204                         struct blkio_group *blkg, unsigned int read_iops);
205 typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
206                         struct blkio_group *blkg, unsigned int write_iops);
207
208 struct blkio_policy_ops {
209         blkio_init_group_fn *blkio_init_group_fn;
210         blkio_update_group_weight_fn *blkio_update_group_weight_fn;
211         blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
212         blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
213         blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
214         blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
215 };
216
217 struct blkio_policy_type {
218         struct list_head list;
219         struct blkio_policy_ops ops;
220         enum blkio_policy_id plid;
221         size_t pdata_size;              /* policy specific private data size */
222 };
223
224 extern int blkcg_init_queue(struct request_queue *q);
225 extern void blkcg_drain_queue(struct request_queue *q);
226 extern void blkcg_exit_queue(struct request_queue *q);
227
228 /* Blkio controller policy registration */
229 extern void blkio_policy_register(struct blkio_policy_type *);
230 extern void blkio_policy_unregister(struct blkio_policy_type *);
231 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
232 extern void update_root_blkg_pd(struct request_queue *q,
233                                 enum blkio_policy_id plid);
234
235 /**
236  * blkg_to_pdata - get policy private data
237  * @blkg: blkg of interest
238  * @pol: policy of interest
239  *
240  * Return pointer to private data associated with the @blkg-@pol pair.
241  */
242 static inline void *blkg_to_pdata(struct blkio_group *blkg,
243                               struct blkio_policy_type *pol)
244 {
245         return blkg ? blkg->pd[pol->plid]->pdata : NULL;
246 }
247
248 /**
249  * pdata_to_blkg - get blkg associated with policy private data
250  * @pdata: policy private data of interest
251  * @pol: policy @pdata is for
252  *
253  * @pdata is policy private data for @pol.  Determine the blkg it's
254  * associated with.
255  */
256 static inline struct blkio_group *pdata_to_blkg(void *pdata,
257                                                 struct blkio_policy_type *pol)
258 {
259         if (pdata) {
260                 struct blkg_policy_data *pd =
261                         container_of(pdata, struct blkg_policy_data, pdata);
262                 return pd->blkg;
263         }
264         return NULL;
265 }
266
267 static inline char *blkg_path(struct blkio_group *blkg)
268 {
269         return blkg->path;
270 }
271
272 /**
273  * blkg_get - get a blkg reference
274  * @blkg: blkg to get
275  *
276  * The caller should be holding queue_lock and an existing reference.
277  */
278 static inline void blkg_get(struct blkio_group *blkg)
279 {
280         lockdep_assert_held(blkg->q->queue_lock);
281         WARN_ON_ONCE(!blkg->refcnt);
282         blkg->refcnt++;
283 }
284
285 void __blkg_release(struct blkio_group *blkg);
286
287 /**
288  * blkg_put - put a blkg reference
289  * @blkg: blkg to put
290  *
291  * The caller should be holding queue_lock.
292  */
293 static inline void blkg_put(struct blkio_group *blkg)
294 {
295         lockdep_assert_held(blkg->q->queue_lock);
296         WARN_ON_ONCE(blkg->refcnt <= 0);
297         if (!--blkg->refcnt)
298                 __blkg_release(blkg);
299 }
300
301 #else
302
303 struct blkio_group {
304 };
305
306 struct blkio_policy_type {
307 };
308
309 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
310 static inline void blkcg_drain_queue(struct request_queue *q) { }
311 static inline void blkcg_exit_queue(struct request_queue *q) { }
312 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
313 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
314 static inline void blkg_destroy_all(struct request_queue *q,
315                                     bool destory_root) { }
316 static inline void update_root_blkg_pd(struct request_queue *q,
317                                        enum blkio_policy_id plid) { }
318
319 static inline void *blkg_to_pdata(struct blkio_group *blkg,
320                                 struct blkio_policy_type *pol) { return NULL; }
321 static inline struct blkio_group *pdata_to_blkg(void *pdata,
322                                 struct blkio_policy_type *pol) { return NULL; }
323 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
324 static inline void blkg_get(struct blkio_group *blkg) { }
325 static inline void blkg_put(struct blkio_group *blkg) { }
326
327 #endif
328
329 #define BLKIO_WEIGHT_MIN        10
330 #define BLKIO_WEIGHT_MAX        1000
331 #define BLKIO_WEIGHT_DEFAULT    500
332
333 #ifdef CONFIG_DEBUG_BLK_CGROUP
334 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
335                                          struct blkio_policy_type *pol);
336 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
337                                   struct blkio_policy_type *pol,
338                                   unsigned long dequeue);
339 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
340                                         struct blkio_policy_type *pol);
341 void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
342                                     struct blkio_policy_type *pol);
343 void blkiocg_set_start_empty_time(struct blkio_group *blkg,
344                                   struct blkio_policy_type *pol);
345
346 #define BLKG_FLAG_FNS(name)                                             \
347 static inline void blkio_mark_blkg_##name(                              \
348                 struct blkio_group_stats *stats)                        \
349 {                                                                       \
350         stats->flags |= (1 << BLKG_##name);                             \
351 }                                                                       \
352 static inline void blkio_clear_blkg_##name(                             \
353                 struct blkio_group_stats *stats)                        \
354 {                                                                       \
355         stats->flags &= ~(1 << BLKG_##name);                            \
356 }                                                                       \
357 static inline int blkio_blkg_##name(struct blkio_group_stats *stats)    \
358 {                                                                       \
359         return (stats->flags & (1 << BLKG_##name)) != 0;                \
360 }                                                                       \
361
362 BLKG_FLAG_FNS(waiting)
363 BLKG_FLAG_FNS(idling)
364 BLKG_FLAG_FNS(empty)
365 #undef BLKG_FLAG_FNS
366 #else
367 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
368                         struct blkio_policy_type *pol) { }
369 static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
370                         struct blkio_policy_type *pol, unsigned long dequeue) { }
371 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
372                         struct blkio_policy_type *pol) { }
373 static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
374                         struct blkio_policy_type *pol) { }
375 static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
376                         struct blkio_policy_type *pol) { }
377 #endif
378
379 #ifdef CONFIG_BLK_CGROUP
380 extern struct blkio_cgroup blkio_root_cgroup;
381 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
382 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
383 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
384                                        struct request_queue *q);
385 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
386                                        struct request_queue *q,
387                                        enum blkio_policy_id plid,
388                                        bool for_root);
389 void blkiocg_update_timeslice_used(struct blkio_group *blkg,
390                                    struct blkio_policy_type *pol,
391                                    unsigned long time,
392                                    unsigned long unaccounted_time);
393 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
394                                    struct blkio_policy_type *pol,
395                                    uint64_t bytes, bool direction, bool sync);
396 void blkiocg_update_completion_stats(struct blkio_group *blkg,
397                                      struct blkio_policy_type *pol,
398                                      uint64_t start_time,
399                                      uint64_t io_start_time, bool direction,
400                                      bool sync);
401 void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
402                                     struct blkio_policy_type *pol,
403                                     bool direction, bool sync);
404 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
405                                  struct blkio_policy_type *pol,
406                                  struct blkio_group *curr_blkg, bool direction,
407                                  bool sync);
408 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
409                                     struct blkio_policy_type *pol,
410                                     bool direction, bool sync);
411 #else
412 struct cgroup;
413 static inline struct blkio_cgroup *
414 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
415 static inline struct blkio_cgroup *
416 bio_blkio_cgroup(struct bio *bio) { return NULL; }
417
418 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
419                                               void *key) { return NULL; }
420 static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
421                         struct blkio_policy_type *pol, unsigned long time,
422                         unsigned long unaccounted_time) { }
423 static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
424                         struct blkio_policy_type *pol, uint64_t bytes,
425                         bool direction, bool sync) { }
426 static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
427                         struct blkio_policy_type *pol, uint64_t start_time,
428                         uint64_t io_start_time, bool direction, bool sync) { }
429 static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
430                         struct blkio_policy_type *pol, bool direction,
431                         bool sync) { }
432 static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
433                         struct blkio_policy_type *pol,
434                         struct blkio_group *curr_blkg, bool direction,
435                         bool sync) { }
436 static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
437                         struct blkio_policy_type *pol, bool direction,
438                         bool sync) { }
439 #endif
440 #endif /* _BLK_CGROUP_H */