blkcg: restructure blkio_group configruation setting
[platform/adaptation/renesas_rcar/renesas_kernel.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 /* cft->private [un]packing for stat printing */
32 #define BLKCG_STAT_PRIV(pol, off)       (((unsigned)(pol) << 16) | (off))
33 #define BLKCG_STAT_POL(prv)             ((unsigned)(prv) >> 16)
34 #define BLKCG_STAT_OFF(prv)             ((unsigned)(prv) & 0xffff)
35
36 enum blkg_rwstat_type {
37         BLKG_RWSTAT_READ,
38         BLKG_RWSTAT_WRITE,
39         BLKG_RWSTAT_SYNC,
40         BLKG_RWSTAT_ASYNC,
41
42         BLKG_RWSTAT_NR,
43         BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
44 };
45
46 /* blkg state flags */
47 enum blkg_state_flags {
48         BLKG_waiting = 0,
49         BLKG_idling,
50         BLKG_empty,
51 };
52
53 struct blkio_cgroup {
54         struct cgroup_subsys_state css;
55         unsigned int weight;
56         spinlock_t lock;
57         struct hlist_head blkg_list;
58
59         /* for policies to test whether associated blkcg has changed */
60         uint64_t id;
61 };
62
63 struct blkg_stat {
64         struct u64_stats_sync           syncp;
65         uint64_t                        cnt;
66 };
67
68 struct blkg_rwstat {
69         struct u64_stats_sync           syncp;
70         uint64_t                        cnt[BLKG_RWSTAT_NR];
71 };
72
73 struct blkio_group_stats {
74         /* number of ios merged */
75         struct blkg_rwstat              merged;
76         /* total time spent on device in ns, may not be accurate w/ queueing */
77         struct blkg_rwstat              service_time;
78         /* total time spent waiting in scheduler queue in ns */
79         struct blkg_rwstat              wait_time;
80         /* number of IOs queued up */
81         struct blkg_rwstat              queued;
82         /* total disk time and nr sectors dispatched by this group */
83         struct blkg_stat                time;
84 #ifdef CONFIG_DEBUG_BLK_CGROUP
85         /* time not charged to this cgroup */
86         struct blkg_stat                unaccounted_time;
87         /* sum of number of ios queued across all samples */
88         struct blkg_stat                avg_queue_size_sum;
89         /* count of samples taken for average */
90         struct blkg_stat                avg_queue_size_samples;
91         /* how many times this group has been removed from service tree */
92         struct blkg_stat                dequeue;
93         /* total time spent waiting for it to be assigned a timeslice. */
94         struct blkg_stat                group_wait_time;
95         /* time spent idling for this blkio_group */
96         struct blkg_stat                idle_time;
97         /* total time with empty current active q with other requests queued */
98         struct blkg_stat                empty_time;
99         /* fields after this shouldn't be cleared on stat reset */
100         uint64_t                        start_group_wait_time;
101         uint64_t                        start_idle_time;
102         uint64_t                        start_empty_time;
103         uint16_t                        flags;
104 #endif
105 };
106
107 /* Per cpu blkio group stats */
108 struct blkio_group_stats_cpu {
109         /* total bytes transferred */
110         struct blkg_rwstat              service_bytes;
111         /* total IOs serviced, post merge */
112         struct blkg_rwstat              serviced;
113         /* total sectors transferred */
114         struct blkg_stat                sectors;
115 };
116
117 struct blkio_group_conf {
118         unsigned int weight;
119         u64 iops[2];
120         u64 bps[2];
121 };
122
123 /* per-blkg per-policy data */
124 struct blkg_policy_data {
125         /* the blkg this per-policy data belongs to */
126         struct blkio_group *blkg;
127
128         /* Configuration */
129         struct blkio_group_conf conf;
130
131         struct blkio_group_stats stats;
132         /* Per cpu stats pointer */
133         struct blkio_group_stats_cpu __percpu *stats_cpu;
134
135         /* pol->pdata_size bytes of private data used by policy impl */
136         char pdata[] __aligned(__alignof__(unsigned long long));
137 };
138
139 struct blkio_group {
140         /* Pointer to the associated request_queue */
141         struct request_queue *q;
142         struct list_head q_node;
143         struct hlist_node blkcg_node;
144         struct blkio_cgroup *blkcg;
145         /* Store cgroup path */
146         char path[128];
147         /* reference count */
148         int refcnt;
149
150         struct blkg_policy_data *pd[BLKIO_NR_POLICIES];
151
152         /* List of blkg waiting for per cpu stats memory to be allocated */
153         struct list_head alloc_node;
154         struct rcu_head rcu_head;
155 };
156
157 typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
158 typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
159                         struct blkio_group *blkg, unsigned int weight);
160 typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
161                         struct blkio_group *blkg, u64 read_bps);
162 typedef void (blkio_update_group_write_bps_fn)(struct request_queue *q,
163                         struct blkio_group *blkg, u64 write_bps);
164 typedef void (blkio_update_group_read_iops_fn)(struct request_queue *q,
165                         struct blkio_group *blkg, unsigned int read_iops);
166 typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
167                         struct blkio_group *blkg, unsigned int write_iops);
168
169 struct blkio_policy_ops {
170         blkio_init_group_fn *blkio_init_group_fn;
171         blkio_update_group_weight_fn *blkio_update_group_weight_fn;
172         blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
173         blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
174         blkio_update_group_read_iops_fn *blkio_update_group_read_iops_fn;
175         blkio_update_group_write_iops_fn *blkio_update_group_write_iops_fn;
176 };
177
178 struct blkio_policy_type {
179         struct list_head list;
180         struct blkio_policy_ops ops;
181         enum blkio_policy_id plid;
182         size_t pdata_size;              /* policy specific private data size */
183 };
184
185 extern int blkcg_init_queue(struct request_queue *q);
186 extern void blkcg_drain_queue(struct request_queue *q);
187 extern void blkcg_exit_queue(struct request_queue *q);
188
189 /* Blkio controller policy registration */
190 extern void blkio_policy_register(struct blkio_policy_type *);
191 extern void blkio_policy_unregister(struct blkio_policy_type *);
192 extern void blkg_destroy_all(struct request_queue *q, bool destroy_root);
193 extern void update_root_blkg_pd(struct request_queue *q,
194                                 enum blkio_policy_id plid);
195
196 /**
197  * blkg_to_pdata - get policy private data
198  * @blkg: blkg of interest
199  * @pol: policy of interest
200  *
201  * Return pointer to private data associated with the @blkg-@pol pair.
202  */
203 static inline void *blkg_to_pdata(struct blkio_group *blkg,
204                               struct blkio_policy_type *pol)
205 {
206         return blkg ? blkg->pd[pol->plid]->pdata : NULL;
207 }
208
209 /**
210  * pdata_to_blkg - get blkg associated with policy private data
211  * @pdata: policy private data of interest
212  *
213  * @pdata is policy private data.  Determine the blkg it's associated with.
214  */
215 static inline struct blkio_group *pdata_to_blkg(void *pdata)
216 {
217         if (pdata) {
218                 struct blkg_policy_data *pd =
219                         container_of(pdata, struct blkg_policy_data, pdata);
220                 return pd->blkg;
221         }
222         return NULL;
223 }
224
225 static inline char *blkg_path(struct blkio_group *blkg)
226 {
227         return blkg->path;
228 }
229
230 /**
231  * blkg_get - get a blkg reference
232  * @blkg: blkg to get
233  *
234  * The caller should be holding queue_lock and an existing reference.
235  */
236 static inline void blkg_get(struct blkio_group *blkg)
237 {
238         lockdep_assert_held(blkg->q->queue_lock);
239         WARN_ON_ONCE(!blkg->refcnt);
240         blkg->refcnt++;
241 }
242
243 void __blkg_release(struct blkio_group *blkg);
244
245 /**
246  * blkg_put - put a blkg reference
247  * @blkg: blkg to put
248  *
249  * The caller should be holding queue_lock.
250  */
251 static inline void blkg_put(struct blkio_group *blkg)
252 {
253         lockdep_assert_held(blkg->q->queue_lock);
254         WARN_ON_ONCE(blkg->refcnt <= 0);
255         if (!--blkg->refcnt)
256                 __blkg_release(blkg);
257 }
258
259 /**
260  * blkg_stat_add - add a value to a blkg_stat
261  * @stat: target blkg_stat
262  * @val: value to add
263  *
264  * Add @val to @stat.  The caller is responsible for synchronizing calls to
265  * this function.
266  */
267 static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
268 {
269         u64_stats_update_begin(&stat->syncp);
270         stat->cnt += val;
271         u64_stats_update_end(&stat->syncp);
272 }
273
274 /**
275  * blkg_stat_read - read the current value of a blkg_stat
276  * @stat: blkg_stat to read
277  *
278  * Read the current value of @stat.  This function can be called without
279  * synchroniztion and takes care of u64 atomicity.
280  */
281 static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
282 {
283         unsigned int start;
284         uint64_t v;
285
286         do {
287                 start = u64_stats_fetch_begin(&stat->syncp);
288                 v = stat->cnt;
289         } while (u64_stats_fetch_retry(&stat->syncp, start));
290
291         return v;
292 }
293
294 /**
295  * blkg_stat_reset - reset a blkg_stat
296  * @stat: blkg_stat to reset
297  */
298 static inline void blkg_stat_reset(struct blkg_stat *stat)
299 {
300         stat->cnt = 0;
301 }
302
303 /**
304  * blkg_rwstat_add - add a value to a blkg_rwstat
305  * @rwstat: target blkg_rwstat
306  * @rw: mask of REQ_{WRITE|SYNC}
307  * @val: value to add
308  *
309  * Add @val to @rwstat.  The counters are chosen according to @rw.  The
310  * caller is responsible for synchronizing calls to this function.
311  */
312 static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
313                                    int rw, uint64_t val)
314 {
315         u64_stats_update_begin(&rwstat->syncp);
316
317         if (rw & REQ_WRITE)
318                 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
319         else
320                 rwstat->cnt[BLKG_RWSTAT_READ] += val;
321         if (rw & REQ_SYNC)
322                 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
323         else
324                 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
325
326         u64_stats_update_end(&rwstat->syncp);
327 }
328
329 /**
330  * blkg_rwstat_read - read the current values of a blkg_rwstat
331  * @rwstat: blkg_rwstat to read
332  *
333  * Read the current snapshot of @rwstat and return it as the return value.
334  * This function can be called without synchronization and takes care of
335  * u64 atomicity.
336  */
337 static struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
338 {
339         unsigned int start;
340         struct blkg_rwstat tmp;
341
342         do {
343                 start = u64_stats_fetch_begin(&rwstat->syncp);
344                 tmp = *rwstat;
345         } while (u64_stats_fetch_retry(&rwstat->syncp, start));
346
347         return tmp;
348 }
349
350 /**
351  * blkg_rwstat_sum - read the total count of a blkg_rwstat
352  * @rwstat: blkg_rwstat to read
353  *
354  * Return the total count of @rwstat regardless of the IO direction.  This
355  * function can be called without synchronization and takes care of u64
356  * atomicity.
357  */
358 static inline uint64_t blkg_rwstat_sum(struct blkg_rwstat *rwstat)
359 {
360         struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
361
362         return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
363 }
364
365 /**
366  * blkg_rwstat_reset - reset a blkg_rwstat
367  * @rwstat: blkg_rwstat to reset
368  */
369 static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
370 {
371         memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
372 }
373
374 #else
375
376 struct blkio_group {
377 };
378
379 struct blkio_policy_type {
380 };
381
382 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
383 static inline void blkcg_drain_queue(struct request_queue *q) { }
384 static inline void blkcg_exit_queue(struct request_queue *q) { }
385 static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
386 static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
387 static inline void blkg_destroy_all(struct request_queue *q,
388                                     bool destory_root) { }
389 static inline void update_root_blkg_pd(struct request_queue *q,
390                                        enum blkio_policy_id plid) { }
391
392 static inline void *blkg_to_pdata(struct blkio_group *blkg,
393                                 struct blkio_policy_type *pol) { return NULL; }
394 static inline struct blkio_group *pdata_to_blkg(void *pdata,
395                                 struct blkio_policy_type *pol) { return NULL; }
396 static inline char *blkg_path(struct blkio_group *blkg) { return NULL; }
397 static inline void blkg_get(struct blkio_group *blkg) { }
398 static inline void blkg_put(struct blkio_group *blkg) { }
399
400 #endif
401
402 #define BLKIO_WEIGHT_MIN        10
403 #define BLKIO_WEIGHT_MAX        1000
404 #define BLKIO_WEIGHT_DEFAULT    500
405
406 #ifdef CONFIG_DEBUG_BLK_CGROUP
407 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
408                                          struct blkio_policy_type *pol);
409 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
410                                   struct blkio_policy_type *pol,
411                                   unsigned long dequeue);
412 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
413                                         struct blkio_policy_type *pol);
414 void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
415                                     struct blkio_policy_type *pol);
416 void blkiocg_set_start_empty_time(struct blkio_group *blkg,
417                                   struct blkio_policy_type *pol);
418
419 #define BLKG_FLAG_FNS(name)                                             \
420 static inline void blkio_mark_blkg_##name(                              \
421                 struct blkio_group_stats *stats)                        \
422 {                                                                       \
423         stats->flags |= (1 << BLKG_##name);                             \
424 }                                                                       \
425 static inline void blkio_clear_blkg_##name(                             \
426                 struct blkio_group_stats *stats)                        \
427 {                                                                       \
428         stats->flags &= ~(1 << BLKG_##name);                            \
429 }                                                                       \
430 static inline int blkio_blkg_##name(struct blkio_group_stats *stats)    \
431 {                                                                       \
432         return (stats->flags & (1 << BLKG_##name)) != 0;                \
433 }                                                                       \
434
435 BLKG_FLAG_FNS(waiting)
436 BLKG_FLAG_FNS(idling)
437 BLKG_FLAG_FNS(empty)
438 #undef BLKG_FLAG_FNS
439 #else
440 static inline void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
441                         struct blkio_policy_type *pol) { }
442 static inline void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
443                         struct blkio_policy_type *pol, unsigned long dequeue) { }
444 static inline void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
445                         struct blkio_policy_type *pol) { }
446 static inline void blkiocg_update_idle_time_stats(struct blkio_group *blkg,
447                         struct blkio_policy_type *pol) { }
448 static inline void blkiocg_set_start_empty_time(struct blkio_group *blkg,
449                         struct blkio_policy_type *pol) { }
450 #endif
451
452 #ifdef CONFIG_BLK_CGROUP
453 extern struct blkio_cgroup blkio_root_cgroup;
454 extern struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup);
455 extern struct blkio_cgroup *bio_blkio_cgroup(struct bio *bio);
456 extern struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
457                                        struct request_queue *q);
458 struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
459                                        struct request_queue *q,
460                                        bool for_root);
461 void blkiocg_update_timeslice_used(struct blkio_group *blkg,
462                                    struct blkio_policy_type *pol,
463                                    unsigned long time,
464                                    unsigned long unaccounted_time);
465 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
466                                    struct blkio_policy_type *pol,
467                                    uint64_t bytes, bool direction, bool sync);
468 void blkiocg_update_completion_stats(struct blkio_group *blkg,
469                                      struct blkio_policy_type *pol,
470                                      uint64_t start_time,
471                                      uint64_t io_start_time, bool direction,
472                                      bool sync);
473 void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
474                                     struct blkio_policy_type *pol,
475                                     bool direction, bool sync);
476 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
477                                  struct blkio_policy_type *pol,
478                                  struct blkio_group *curr_blkg, bool direction,
479                                  bool sync);
480 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
481                                     struct blkio_policy_type *pol,
482                                     bool direction, bool sync);
483 #else
484 struct cgroup;
485 static inline struct blkio_cgroup *
486 cgroup_to_blkio_cgroup(struct cgroup *cgroup) { return NULL; }
487 static inline struct blkio_cgroup *
488 bio_blkio_cgroup(struct bio *bio) { return NULL; }
489
490 static inline struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
491                                               void *key) { return NULL; }
492 static inline void blkiocg_update_timeslice_used(struct blkio_group *blkg,
493                         struct blkio_policy_type *pol, unsigned long time,
494                         unsigned long unaccounted_time) { }
495 static inline void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
496                         struct blkio_policy_type *pol, uint64_t bytes,
497                         bool direction, bool sync) { }
498 static inline void blkiocg_update_completion_stats(struct blkio_group *blkg,
499                         struct blkio_policy_type *pol, uint64_t start_time,
500                         uint64_t io_start_time, bool direction, bool sync) { }
501 static inline void blkiocg_update_io_merged_stats(struct blkio_group *blkg,
502                         struct blkio_policy_type *pol, bool direction,
503                         bool sync) { }
504 static inline void blkiocg_update_io_add_stats(struct blkio_group *blkg,
505                         struct blkio_policy_type *pol,
506                         struct blkio_group *curr_blkg, bool direction,
507                         bool sync) { }
508 static inline void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
509                         struct blkio_policy_type *pol, bool direction,
510                         bool sync) { }
511 #endif
512 #endif /* _BLK_CGROUP_H */