blkcg: make blkcg_[rw]stat per-cpu
[linux-2.6-block.git] / include / linux / blk-cgroup.h
CommitLineData
31e4c28d
VG
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>
24bdb8ef 17#include <linux/percpu_counter.h>
829fdb50 18#include <linux/seq_file.h>
a637120e 19#include <linux/radix-tree.h>
a051661c 20#include <linux/blkdev.h>
a5049a8a 21#include <linux/atomic.h>
31e4c28d 22
24bdb8ef
TH
23/* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
24#define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
25
9355aede
VG
26/* Max limits for throttle policy */
27#define THROTL_IOPS_MAX UINT_MAX
28
f48ec1d7
TH
29#ifdef CONFIG_BLK_CGROUP
30
edcb0722
TH
31enum blkg_rwstat_type {
32 BLKG_RWSTAT_READ,
33 BLKG_RWSTAT_WRITE,
34 BLKG_RWSTAT_SYNC,
35 BLKG_RWSTAT_ASYNC,
36
37 BLKG_RWSTAT_NR,
38 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
303a3acb
DS
39};
40
a637120e
TH
41struct blkcg_gq;
42
3c798398 43struct blkcg {
36558c8a
TH
44 struct cgroup_subsys_state css;
45 spinlock_t lock;
a637120e
TH
46
47 struct radix_tree_root blkg_tree;
48 struct blkcg_gq *blkg_hint;
36558c8a 49 struct hlist_head blkg_list;
9a9e8a26 50
81437648 51 struct blkcg_policy_data *cpd[BLKCG_MAX_POLS];
52ebea74 52
7876f930 53 struct list_head all_blkcgs_node;
52ebea74
TH
54#ifdef CONFIG_CGROUP_WRITEBACK
55 struct list_head cgwb_list;
56#endif
31e4c28d
VG
57};
58
e6269c44
TH
59/*
60 * blkg_[rw]stat->aux_cnt is excluded for local stats but included for
24bdb8ef
TH
61 * recursive. Used to carry stats of dead children, and, for blkg_rwstat,
62 * to carry result values from read and sum operations.
e6269c44 63 */
edcb0722 64struct blkg_stat {
24bdb8ef 65 struct percpu_counter cpu_cnt;
e6269c44 66 atomic64_t aux_cnt;
edcb0722
TH
67};
68
69struct blkg_rwstat {
24bdb8ef 70 struct percpu_counter cpu_cnt[BLKG_RWSTAT_NR];
e6269c44 71 atomic64_t aux_cnt[BLKG_RWSTAT_NR];
edcb0722
TH
72};
73
f95a04af
TH
74/*
75 * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
76 * request_queue (q). This is used by blkcg policies which need to track
77 * information per blkcg - q pair.
78 *
001bea73
TH
79 * There can be multiple active blkcg policies and each blkg:policy pair is
80 * represented by a blkg_policy_data which is allocated and freed by each
81 * policy's pd_alloc/free_fn() methods. A policy can allocate private data
82 * area by allocating larger data structure which embeds blkg_policy_data
83 * at the beginning.
f95a04af 84 */
0381411e 85struct blkg_policy_data {
b276a876 86 /* the blkg and policy id this per-policy data belongs to */
3c798398 87 struct blkcg_gq *blkg;
b276a876 88 int plid;
0381411e
TH
89};
90
e48453c3 91/*
e4a9bde9
TH
92 * Policies that need to keep per-blkcg data which is independent from any
93 * request_queue associated to it should implement cpd_alloc/free_fn()
94 * methods. A policy can allocate private data area by allocating larger
95 * data structure which embeds blkcg_policy_data at the beginning.
96 * cpd_init() is invoked to let each policy handle per-blkcg data.
e48453c3
AA
97 */
98struct blkcg_policy_data {
81437648
TH
99 /* the blkcg and policy id this per-policy data belongs to */
100 struct blkcg *blkcg;
e48453c3 101 int plid;
e48453c3
AA
102};
103
3c798398
TH
104/* association between a blk cgroup and a request queue */
105struct blkcg_gq {
c875f4d0 106 /* Pointer to the associated request_queue */
36558c8a
TH
107 struct request_queue *q;
108 struct list_head q_node;
109 struct hlist_node blkcg_node;
3c798398 110 struct blkcg *blkcg;
3c547865 111
ce7acfea
TH
112 /*
113 * Each blkg gets congested separately and the congestion state is
114 * propagated to the matching bdi_writeback_congested.
115 */
116 struct bdi_writeback_congested *wb_congested;
117
3c547865
TH
118 /* all non-root blkcg_gq's are guaranteed to have access to parent */
119 struct blkcg_gq *parent;
120
a051661c
TH
121 /* request allocation list for this blkcg-q pair */
122 struct request_list rl;
3c547865 123
1adaf3dd 124 /* reference count */
a5049a8a 125 atomic_t refcnt;
22084190 126
f427d909
TH
127 /* is this blkg online? protected by both blkcg and q locks */
128 bool online;
129
36558c8a 130 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
1adaf3dd 131
36558c8a 132 struct rcu_head rcu_head;
31e4c28d
VG
133};
134
e4a9bde9 135typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
81437648 136typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
e4a9bde9 137typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
001bea73 138typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node);
a9520cd6
TH
139typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
140typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
141typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
001bea73 142typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
a9520cd6 143typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
3e252066 144
3c798398 145struct blkcg_policy {
36558c8a 146 int plid;
36558c8a
TH
147 /* cgroup files for the policy */
148 struct cftype *cftypes;
f9fcc2d3
TH
149
150 /* operations */
e4a9bde9 151 blkcg_pol_alloc_cpd_fn *cpd_alloc_fn;
e48453c3 152 blkcg_pol_init_cpd_fn *cpd_init_fn;
e4a9bde9
TH
153 blkcg_pol_free_cpd_fn *cpd_free_fn;
154
001bea73 155 blkcg_pol_alloc_pd_fn *pd_alloc_fn;
f9fcc2d3 156 blkcg_pol_init_pd_fn *pd_init_fn;
f427d909
TH
157 blkcg_pol_online_pd_fn *pd_online_fn;
158 blkcg_pol_offline_pd_fn *pd_offline_fn;
001bea73 159 blkcg_pol_free_pd_fn *pd_free_fn;
f9fcc2d3 160 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
3e252066
VG
161};
162
3c798398 163extern struct blkcg blkcg_root;
496d5e75 164extern struct cgroup_subsys_state * const blkcg_root_css;
36558c8a 165
24f29046
TH
166struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
167 struct request_queue *q, bool update_hint);
3c798398
TH
168struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
169 struct request_queue *q);
36558c8a
TH
170int blkcg_init_queue(struct request_queue *q);
171void blkcg_drain_queue(struct request_queue *q);
172void blkcg_exit_queue(struct request_queue *q);
5efd6113 173
3e252066 174/* Blkio controller policy registration */
d5bf0291 175int blkcg_policy_register(struct blkcg_policy *pol);
3c798398 176void blkcg_policy_unregister(struct blkcg_policy *pol);
36558c8a 177int blkcg_activate_policy(struct request_queue *q,
3c798398 178 const struct blkcg_policy *pol);
36558c8a 179void blkcg_deactivate_policy(struct request_queue *q,
3c798398 180 const struct blkcg_policy *pol);
3e252066 181
3c798398 182void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
f95a04af
TH
183 u64 (*prfill)(struct seq_file *,
184 struct blkg_policy_data *, int),
3c798398 185 const struct blkcg_policy *pol, int data,
ec399347 186 bool show_total);
f95a04af
TH
187u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
188u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
829fdb50 189 const struct blkg_rwstat *rwstat);
f95a04af
TH
190u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
191u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
192 int off);
829fdb50 193
16b3de66
TH
194u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off);
195struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
196 int off);
197
829fdb50 198struct blkg_conf_ctx {
36558c8a 199 struct gendisk *disk;
3c798398 200 struct blkcg_gq *blkg;
36558c8a 201 u64 v;
829fdb50
TH
202};
203
3c798398
TH
204int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
205 const char *input, struct blkg_conf_ctx *ctx);
829fdb50
TH
206void blkg_conf_finish(struct blkg_conf_ctx *ctx);
207
208
a7c6d554
TH
209static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
210{
211 return css ? container_of(css, struct blkcg, css) : NULL;
212}
213
b1208b56
TH
214static inline struct blkcg *task_blkcg(struct task_struct *tsk)
215{
073219e9 216 return css_to_blkcg(task_css(tsk, blkio_cgrp_id));
b1208b56
TH
217}
218
219static inline struct blkcg *bio_blkcg(struct bio *bio)
220{
221 if (bio && bio->bi_css)
a7c6d554 222 return css_to_blkcg(bio->bi_css);
b1208b56
TH
223 return task_blkcg(current);
224}
225
fd383c2d
TH
226static inline struct cgroup_subsys_state *
227task_get_blkcg_css(struct task_struct *task)
228{
229 return task_get_css(task, blkio_cgrp_id);
230}
231
3c547865
TH
232/**
233 * blkcg_parent - get the parent of a blkcg
234 * @blkcg: blkcg of interest
235 *
236 * Return the parent blkcg of @blkcg. Can be called anytime.
237 */
238static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
239{
5c9d535b 240 return css_to_blkcg(blkcg->css.parent);
3c547865
TH
241}
242
24f29046
TH
243/**
244 * __blkg_lookup - internal version of blkg_lookup()
245 * @blkcg: blkcg of interest
246 * @q: request_queue of interest
247 * @update_hint: whether to update lookup hint with the result or not
248 *
249 * This is internal version and shouldn't be used by policy
250 * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
251 * @q's bypass state. If @update_hint is %true, the caller should be
252 * holding @q->queue_lock and lookup hint is updated on success.
253 */
254static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
255 struct request_queue *q,
256 bool update_hint)
257{
258 struct blkcg_gq *blkg;
259
85b6bc9d
TH
260 if (blkcg == &blkcg_root)
261 return q->root_blkg;
262
24f29046
TH
263 blkg = rcu_dereference(blkcg->blkg_hint);
264 if (blkg && blkg->q == q)
265 return blkg;
266
267 return blkg_lookup_slowpath(blkcg, q, update_hint);
268}
269
270/**
271 * blkg_lookup - lookup blkg for the specified blkcg - q pair
272 * @blkcg: blkcg of interest
273 * @q: request_queue of interest
274 *
275 * Lookup blkg for the @blkcg - @q pair. This function should be called
276 * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
277 * - see blk_queue_bypass_start() for details.
278 */
279static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
280 struct request_queue *q)
281{
282 WARN_ON_ONCE(!rcu_read_lock_held());
283
284 if (unlikely(blk_queue_bypass(q)))
285 return NULL;
286 return __blkg_lookup(blkcg, q, false);
287}
288
0381411e
TH
289/**
290 * blkg_to_pdata - get policy private data
291 * @blkg: blkg of interest
292 * @pol: policy of interest
293 *
294 * Return pointer to private data associated with the @blkg-@pol pair.
295 */
f95a04af
TH
296static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
297 struct blkcg_policy *pol)
0381411e 298{
f95a04af 299 return blkg ? blkg->pd[pol->plid] : NULL;
0381411e
TH
300}
301
e48453c3
AA
302static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
303 struct blkcg_policy *pol)
304{
81437648 305 return blkcg ? blkcg->cpd[pol->plid] : NULL;
e48453c3
AA
306}
307
0381411e
TH
308/**
309 * pdata_to_blkg - get blkg associated with policy private data
f95a04af 310 * @pd: policy private data of interest
0381411e 311 *
f95a04af 312 * @pd is policy private data. Determine the blkg it's associated with.
0381411e 313 */
f95a04af 314static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
0381411e 315{
f95a04af 316 return pd ? pd->blkg : NULL;
0381411e
TH
317}
318
81437648
TH
319static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
320{
321 return cpd ? cpd->blkcg : NULL;
322}
323
54e7ed12
TH
324/**
325 * blkg_path - format cgroup path of blkg
326 * @blkg: blkg of interest
327 * @buf: target buffer
328 * @buflen: target buffer length
329 *
330 * Format the path of the cgroup of @blkg into @buf.
331 */
3c798398 332static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
afc24d49 333{
e61734c5 334 char *p;
54e7ed12 335
e61734c5
TH
336 p = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
337 if (!p) {
54e7ed12 338 strncpy(buf, "<unavailable>", buflen);
e61734c5
TH
339 return -ENAMETOOLONG;
340 }
341
342 memmove(buf, p, buf + buflen - p);
343 return 0;
afc24d49
VG
344}
345
1adaf3dd
TH
346/**
347 * blkg_get - get a blkg reference
348 * @blkg: blkg to get
349 *
a5049a8a 350 * The caller should be holding an existing reference.
1adaf3dd 351 */
3c798398 352static inline void blkg_get(struct blkcg_gq *blkg)
1adaf3dd 353{
a5049a8a
TH
354 WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
355 atomic_inc(&blkg->refcnt);
1adaf3dd
TH
356}
357
2a4fd070 358void __blkg_release_rcu(struct rcu_head *rcu);
1adaf3dd
TH
359
360/**
361 * blkg_put - put a blkg reference
362 * @blkg: blkg to put
1adaf3dd 363 */
3c798398 364static inline void blkg_put(struct blkcg_gq *blkg)
1adaf3dd 365{
a5049a8a
TH
366 WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
367 if (atomic_dec_and_test(&blkg->refcnt))
2a4fd070 368 call_rcu(&blkg->rcu_head, __blkg_release_rcu);
1adaf3dd
TH
369}
370
dd4a4ffc
TH
371/**
372 * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
373 * @d_blkg: loop cursor pointing to the current descendant
492eb21b 374 * @pos_css: used for iteration
dd4a4ffc
TH
375 * @p_blkg: target blkg to walk descendants of
376 *
377 * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
378 * read locked. If called under either blkcg or queue lock, the iteration
379 * is guaranteed to include all and only online blkgs. The caller may
492eb21b 380 * update @pos_css by calling css_rightmost_descendant() to skip subtree.
bd8815a6 381 * @p_blkg is included in the iteration and the first node to be visited.
dd4a4ffc 382 */
492eb21b
TH
383#define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
384 css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
385 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
dd4a4ffc
TH
386 (p_blkg)->q, false)))
387
aa539cb3
TH
388/**
389 * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
390 * @d_blkg: loop cursor pointing to the current descendant
492eb21b 391 * @pos_css: used for iteration
aa539cb3
TH
392 * @p_blkg: target blkg to walk descendants of
393 *
394 * Similar to blkg_for_each_descendant_pre() but performs post-order
bd8815a6
TH
395 * traversal instead. Synchronization rules are the same. @p_blkg is
396 * included in the iteration and the last node to be visited.
aa539cb3 397 */
492eb21b
TH
398#define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
399 css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
400 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
aa539cb3
TH
401 (p_blkg)->q, false)))
402
a051661c
TH
403/**
404 * blk_get_rl - get request_list to use
405 * @q: request_queue of interest
406 * @bio: bio which will be attached to the allocated request (may be %NULL)
407 *
408 * The caller wants to allocate a request from @q to use for @bio. Find
409 * the request_list to use and obtain a reference on it. Should be called
410 * under queue_lock. This function is guaranteed to return non-%NULL
411 * request_list.
412 */
413static inline struct request_list *blk_get_rl(struct request_queue *q,
414 struct bio *bio)
415{
416 struct blkcg *blkcg;
417 struct blkcg_gq *blkg;
418
419 rcu_read_lock();
420
421 blkcg = bio_blkcg(bio);
422
423 /* bypass blkg lookup and use @q->root_rl directly for root */
424 if (blkcg == &blkcg_root)
425 goto root_rl;
426
427 /*
428 * Try to use blkg->rl. blkg lookup may fail under memory pressure
429 * or if either the blkcg or queue is going away. Fall back to
430 * root_rl in such cases.
431 */
ae118896
TH
432 blkg = blkg_lookup(blkcg, q);
433 if (unlikely(!blkg))
a051661c
TH
434 goto root_rl;
435
436 blkg_get(blkg);
437 rcu_read_unlock();
438 return &blkg->rl;
439root_rl:
440 rcu_read_unlock();
441 return &q->root_rl;
442}
443
444/**
445 * blk_put_rl - put request_list
446 * @rl: request_list to put
447 *
448 * Put the reference acquired by blk_get_rl(). Should be called under
449 * queue_lock.
450 */
451static inline void blk_put_rl(struct request_list *rl)
452{
401efbf8 453 if (rl->blkg->blkcg != &blkcg_root)
a051661c
TH
454 blkg_put(rl->blkg);
455}
456
457/**
458 * blk_rq_set_rl - associate a request with a request_list
459 * @rq: request of interest
460 * @rl: target request_list
461 *
462 * Associate @rq with @rl so that accounting and freeing can know the
463 * request_list @rq came from.
464 */
465static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
466{
467 rq->rl = rl;
468}
469
470/**
471 * blk_rq_rl - return the request_list a request came from
472 * @rq: request of interest
473 *
474 * Return the request_list @rq is allocated from.
475 */
476static inline struct request_list *blk_rq_rl(struct request *rq)
477{
478 return rq->rl;
479}
480
481struct request_list *__blk_queue_next_rl(struct request_list *rl,
482 struct request_queue *q);
483/**
484 * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
485 *
486 * Should be used under queue_lock.
487 */
488#define blk_queue_for_each_rl(rl, q) \
489 for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
490
24bdb8ef 491static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp)
90d3839b 492{
24bdb8ef
TH
493 int ret;
494
495 ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp);
496 if (ret)
497 return ret;
498
e6269c44 499 atomic64_set(&stat->aux_cnt, 0);
24bdb8ef
TH
500 return 0;
501}
502
503static inline void blkg_stat_exit(struct blkg_stat *stat)
504{
505 percpu_counter_destroy(&stat->cpu_cnt);
90d3839b
PZ
506}
507
edcb0722
TH
508/**
509 * blkg_stat_add - add a value to a blkg_stat
510 * @stat: target blkg_stat
511 * @val: value to add
512 *
24bdb8ef
TH
513 * Add @val to @stat. The caller must ensure that IRQ on the same CPU
514 * don't re-enter this function for the same counter.
edcb0722
TH
515 */
516static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
517{
24bdb8ef 518 __percpu_counter_add(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH);
edcb0722
TH
519}
520
521/**
522 * blkg_stat_read - read the current value of a blkg_stat
523 * @stat: blkg_stat to read
edcb0722
TH
524 */
525static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
526{
24bdb8ef 527 return percpu_counter_sum_positive(&stat->cpu_cnt);
edcb0722
TH
528}
529
530/**
531 * blkg_stat_reset - reset a blkg_stat
532 * @stat: blkg_stat to reset
533 */
534static inline void blkg_stat_reset(struct blkg_stat *stat)
535{
24bdb8ef 536 percpu_counter_set(&stat->cpu_cnt, 0);
e6269c44 537 atomic64_set(&stat->aux_cnt, 0);
edcb0722
TH
538}
539
16b3de66 540/**
e6269c44 541 * blkg_stat_add_aux - add a blkg_stat into another's aux count
16b3de66
TH
542 * @to: the destination blkg_stat
543 * @from: the source
544 *
e6269c44 545 * Add @from's count including the aux one to @to's aux count.
16b3de66 546 */
e6269c44
TH
547static inline void blkg_stat_add_aux(struct blkg_stat *to,
548 struct blkg_stat *from)
16b3de66 549{
e6269c44
TH
550 atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt),
551 &to->aux_cnt);
16b3de66
TH
552}
553
24bdb8ef 554static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp)
90d3839b 555{
24bdb8ef
TH
556 int i, ret;
557
558 for (i = 0; i < BLKG_RWSTAT_NR; i++) {
559 ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp);
560 if (ret) {
561 while (--i >= 0)
562 percpu_counter_destroy(&rwstat->cpu_cnt[i]);
563 return ret;
564 }
565 atomic64_set(&rwstat->aux_cnt[i], 0);
566 }
567 return 0;
568}
e6269c44 569
24bdb8ef
TH
570static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
571{
572 int i;
e6269c44
TH
573
574 for (i = 0; i < BLKG_RWSTAT_NR; i++)
24bdb8ef 575 percpu_counter_destroy(&rwstat->cpu_cnt[i]);
90d3839b
PZ
576}
577
edcb0722
TH
578/**
579 * blkg_rwstat_add - add a value to a blkg_rwstat
580 * @rwstat: target blkg_rwstat
581 * @rw: mask of REQ_{WRITE|SYNC}
582 * @val: value to add
583 *
584 * Add @val to @rwstat. The counters are chosen according to @rw. The
585 * caller is responsible for synchronizing calls to this function.
586 */
587static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
588 int rw, uint64_t val)
589{
24bdb8ef 590 struct percpu_counter *cnt;
edcb0722
TH
591
592 if (rw & REQ_WRITE)
24bdb8ef 593 cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE];
edcb0722 594 else
24bdb8ef
TH
595 cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ];
596
597 __percpu_counter_add(cnt, val, BLKG_STAT_CPU_BATCH);
598
edcb0722 599 if (rw & REQ_SYNC)
24bdb8ef 600 cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC];
edcb0722 601 else
24bdb8ef 602 cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC];
edcb0722 603
24bdb8ef 604 __percpu_counter_add(cnt, val, BLKG_STAT_CPU_BATCH);
edcb0722
TH
605}
606
607/**
608 * blkg_rwstat_read - read the current values of a blkg_rwstat
609 * @rwstat: blkg_rwstat to read
610 *
24bdb8ef 611 * Read the current snapshot of @rwstat and return it in the aux counts.
edcb0722 612 */
c94bed89 613static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
edcb0722 614{
24bdb8ef
TH
615 struct blkg_rwstat result;
616 int i;
edcb0722 617
24bdb8ef
TH
618 for (i = 0; i < BLKG_RWSTAT_NR; i++)
619 atomic64_set(&result.aux_cnt[i],
620 percpu_counter_sum_positive(&rwstat->cpu_cnt[i]));
621 return result;
edcb0722
TH
622}
623
624/**
4d5e80a7 625 * blkg_rwstat_total - read the total count of a blkg_rwstat
edcb0722
TH
626 * @rwstat: blkg_rwstat to read
627 *
628 * Return the total count of @rwstat regardless of the IO direction. This
629 * function can be called without synchronization and takes care of u64
630 * atomicity.
631 */
4d5e80a7 632static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
edcb0722
TH
633{
634 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
635
24bdb8ef
TH
636 return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
637 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
edcb0722
TH
638}
639
640/**
641 * blkg_rwstat_reset - reset a blkg_rwstat
642 * @rwstat: blkg_rwstat to reset
643 */
644static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
645{
e6269c44
TH
646 int i;
647
24bdb8ef
TH
648 for (i = 0; i < BLKG_RWSTAT_NR; i++) {
649 percpu_counter_set(&rwstat->cpu_cnt[i], 0);
e6269c44 650 atomic64_set(&rwstat->aux_cnt[i], 0);
24bdb8ef 651 }
edcb0722
TH
652}
653
16b3de66 654/**
e6269c44 655 * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count
16b3de66
TH
656 * @to: the destination blkg_rwstat
657 * @from: the source
658 *
e6269c44 659 * Add @from's count including the aux one to @to's aux count.
16b3de66 660 */
e6269c44
TH
661static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to,
662 struct blkg_rwstat *from)
16b3de66
TH
663{
664 struct blkg_rwstat v = blkg_rwstat_read(from);
665 int i;
666
16b3de66 667 for (i = 0; i < BLKG_RWSTAT_NR; i++)
24bdb8ef
TH
668 atomic64_add(atomic64_read(&v.aux_cnt[i]) +
669 atomic64_read(&from->aux_cnt[i]),
e6269c44 670 &to->aux_cnt[i]);
16b3de66
TH
671}
672
ae118896
TH
673#ifdef CONFIG_BLK_DEV_THROTTLING
674extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
675 struct bio *bio);
676#else
677static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
678 struct bio *bio) { return false; }
679#endif
680
681static inline bool blkcg_bio_issue_check(struct request_queue *q,
682 struct bio *bio)
683{
684 struct blkcg *blkcg;
685 struct blkcg_gq *blkg;
686 bool throtl = false;
687
688 rcu_read_lock();
689 blkcg = bio_blkcg(bio);
690
691 blkg = blkg_lookup(blkcg, q);
692 if (unlikely(!blkg)) {
693 spin_lock_irq(q->queue_lock);
694 blkg = blkg_lookup_create(blkcg, q);
695 if (IS_ERR(blkg))
696 blkg = NULL;
697 spin_unlock_irq(q->queue_lock);
698 }
699
700 throtl = blk_throtl_bio(q, blkg, bio);
701
702 rcu_read_unlock();
703 return !throtl;
704}
705
36558c8a
TH
706#else /* CONFIG_BLK_CGROUP */
707
efa7d1c7
TH
708struct blkcg {
709};
2f5ea477 710
f95a04af
TH
711struct blkg_policy_data {
712};
713
e48453c3
AA
714struct blkcg_policy_data {
715};
716
3c798398 717struct blkcg_gq {
2f5ea477
JA
718};
719
3c798398 720struct blkcg_policy {
3e252066
VG
721};
722
496d5e75
TH
723#define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
724
fd383c2d
TH
725static inline struct cgroup_subsys_state *
726task_get_blkcg_css(struct task_struct *task)
727{
728 return NULL;
729}
730
efa7d1c7
TH
731#ifdef CONFIG_BLOCK
732
3c798398 733static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
5efd6113
TH
734static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
735static inline void blkcg_drain_queue(struct request_queue *q) { }
736static inline void blkcg_exit_queue(struct request_queue *q) { }
d5bf0291 737static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
3c798398 738static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
a2b1693b 739static inline int blkcg_activate_policy(struct request_queue *q,
3c798398 740 const struct blkcg_policy *pol) { return 0; }
a2b1693b 741static inline void blkcg_deactivate_policy(struct request_queue *q,
3c798398
TH
742 const struct blkcg_policy *pol) { }
743
b1208b56 744static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
a051661c 745
f95a04af
TH
746static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
747 struct blkcg_policy *pol) { return NULL; }
748static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
3c798398
TH
749static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
750static inline void blkg_get(struct blkcg_gq *blkg) { }
751static inline void blkg_put(struct blkcg_gq *blkg) { }
afc24d49 752
a051661c
TH
753static inline struct request_list *blk_get_rl(struct request_queue *q,
754 struct bio *bio) { return &q->root_rl; }
755static inline void blk_put_rl(struct request_list *rl) { }
756static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { }
757static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; }
758
ae118896
TH
759static inline bool blkcg_bio_issue_check(struct request_queue *q,
760 struct bio *bio) { return true; }
761
a051661c
TH
762#define blk_queue_for_each_rl(rl, q) \
763 for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
764
efa7d1c7 765#endif /* CONFIG_BLOCK */
36558c8a
TH
766#endif /* CONFIG_BLK_CGROUP */
767#endif /* _BLK_CGROUP_H */