blob: 07a32b813ed897d2610fc4abcc205dc6f5f0f0af [file] [log] [blame]
Vivek Goyal31e4c282009-12-03 12:59:42 -05001#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>
Vivek Goyal575969a2011-05-19 15:38:29 -040017#include <linux/u64_stats_sync.h>
Tejun Heo829fdb52012-04-01 14:38:43 -070018#include <linux/seq_file.h>
Tejun Heoa6371202012-04-19 16:29:24 -070019#include <linux/radix-tree.h>
Tejun Heoa0516612012-06-26 15:05:44 -070020#include <linux/blkdev.h>
Tejun Heoa5049a82014-06-19 17:42:57 -040021#include <linux/atomic.h>
Vivek Goyal31e4c282009-12-03 12:59:42 -050022
Vivek Goyal9355aed2010-10-01 21:16:41 +020023/* Max limits for throttle policy */
24#define THROTL_IOPS_MAX UINT_MAX
25
Tejun Heo3381cb82012-04-01 14:38:44 -070026/* CFQ specific, out here for blkcg->cfq_weight */
27#define CFQ_WEIGHT_MIN 10
28#define CFQ_WEIGHT_MAX 1000
29#define CFQ_WEIGHT_DEFAULT 500
30
Tejun Heof48ec1d2012-04-13 13:11:25 -070031#ifdef CONFIG_BLK_CGROUP
32
Tejun Heoedcb0722012-04-01 14:38:42 -070033enum blkg_rwstat_type {
34 BLKG_RWSTAT_READ,
35 BLKG_RWSTAT_WRITE,
36 BLKG_RWSTAT_SYNC,
37 BLKG_RWSTAT_ASYNC,
38
39 BLKG_RWSTAT_NR,
40 BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
Divyesh Shah303a3ac2010-04-01 15:01:24 -070041};
42
Tejun Heoa6371202012-04-19 16:29:24 -070043struct blkcg_gq;
44
Tejun Heo3c798392012-04-16 13:57:25 -070045struct blkcg {
Tejun Heo36558c82012-04-16 13:57:24 -070046 struct cgroup_subsys_state css;
47 spinlock_t lock;
Tejun Heoa6371202012-04-19 16:29:24 -070048
49 struct radix_tree_root blkg_tree;
50 struct blkcg_gq *blkg_hint;
Tejun Heo36558c82012-04-16 13:57:24 -070051 struct hlist_head blkg_list;
Tejun Heo9a9e8a22012-03-19 15:10:56 -070052
Tejun Heo3c798392012-04-16 13:57:25 -070053 /* TODO: per-policy storage in blkcg */
Tejun Heo36558c82012-04-16 13:57:24 -070054 unsigned int cfq_weight; /* belongs to cfq */
Tejun Heoe71357e2013-01-09 08:05:10 -080055 unsigned int cfq_leaf_weight;
Tejun Heo52ebea72015-05-22 17:13:37 -040056
57#ifdef CONFIG_CGROUP_WRITEBACK
58 struct list_head cgwb_list;
59#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -050060};
61
Tejun Heoedcb0722012-04-01 14:38:42 -070062struct blkg_stat {
63 struct u64_stats_sync syncp;
64 uint64_t cnt;
65};
66
67struct blkg_rwstat {
68 struct u64_stats_sync syncp;
69 uint64_t cnt[BLKG_RWSTAT_NR];
70};
71
Tejun Heof95a04a2012-04-16 13:57:26 -070072/*
73 * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
74 * request_queue (q). This is used by blkcg policies which need to track
75 * information per blkcg - q pair.
76 *
77 * There can be multiple active blkcg policies and each has its private
78 * data on each blkg, the size of which is determined by
79 * blkcg_policy->pd_size. blkcg core allocates and frees such areas
80 * together with blkg and invokes pd_init/exit_fn() methods.
81 *
82 * Such private data must embed struct blkg_policy_data (pd) at the
83 * beginning and pd_size can't be smaller than pd.
84 */
Tejun Heo03814112012-03-05 13:15:14 -080085struct blkg_policy_data {
Tejun Heob276a872013-01-09 08:05:12 -080086 /* the blkg and policy id this per-policy data belongs to */
Tejun Heo3c798392012-04-16 13:57:25 -070087 struct blkcg_gq *blkg;
Tejun Heob276a872013-01-09 08:05:12 -080088 int plid;
Tejun Heo03814112012-03-05 13:15:14 -080089
Tejun Heoa2b16932012-04-13 13:11:33 -070090 /* used during policy activation */
Tejun Heo36558c82012-04-16 13:57:24 -070091 struct list_head alloc_node;
Tejun Heo03814112012-03-05 13:15:14 -080092};
93
Tejun Heo3c798392012-04-16 13:57:25 -070094/* association between a blk cgroup and a request queue */
95struct blkcg_gq {
Tejun Heoc875f4d2012-03-05 13:15:22 -080096 /* Pointer to the associated request_queue */
Tejun Heo36558c82012-04-16 13:57:24 -070097 struct request_queue *q;
98 struct list_head q_node;
99 struct hlist_node blkcg_node;
Tejun Heo3c798392012-04-16 13:57:25 -0700100 struct blkcg *blkcg;
Tejun Heo3c547862013-01-09 08:05:10 -0800101
Tejun Heoce7acfe2015-05-22 17:13:38 -0400102 /*
103 * Each blkg gets congested separately and the congestion state is
104 * propagated to the matching bdi_writeback_congested.
105 */
106 struct bdi_writeback_congested *wb_congested;
107
Tejun Heo3c547862013-01-09 08:05:10 -0800108 /* all non-root blkcg_gq's are guaranteed to have access to parent */
109 struct blkcg_gq *parent;
110
Tejun Heoa0516612012-06-26 15:05:44 -0700111 /* request allocation list for this blkcg-q pair */
112 struct request_list rl;
Tejun Heo3c547862013-01-09 08:05:10 -0800113
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800114 /* reference count */
Tejun Heoa5049a82014-06-19 17:42:57 -0400115 atomic_t refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -0500116
Tejun Heof427d902013-01-09 08:05:12 -0800117 /* is this blkg online? protected by both blkcg and q locks */
118 bool online;
119
Tejun Heo36558c82012-04-16 13:57:24 -0700120 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800121
Tejun Heo36558c82012-04-16 13:57:24 -0700122 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500123};
124
Tejun Heo3c798392012-04-16 13:57:25 -0700125typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
Tejun Heof427d902013-01-09 08:05:12 -0800126typedef void (blkcg_pol_online_pd_fn)(struct blkcg_gq *blkg);
127typedef void (blkcg_pol_offline_pd_fn)(struct blkcg_gq *blkg);
Tejun Heo3c798392012-04-16 13:57:25 -0700128typedef void (blkcg_pol_exit_pd_fn)(struct blkcg_gq *blkg);
129typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkcg_gq *blkg);
Vivek Goyal3e252062009-12-04 10:36:42 -0500130
Tejun Heo3c798392012-04-16 13:57:25 -0700131struct blkcg_policy {
Tejun Heo36558c82012-04-16 13:57:24 -0700132 int plid;
133 /* policy specific private data size */
Tejun Heof95a04a2012-04-16 13:57:26 -0700134 size_t pd_size;
Tejun Heo36558c82012-04-16 13:57:24 -0700135 /* cgroup files for the policy */
136 struct cftype *cftypes;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700137
138 /* operations */
139 blkcg_pol_init_pd_fn *pd_init_fn;
Tejun Heof427d902013-01-09 08:05:12 -0800140 blkcg_pol_online_pd_fn *pd_online_fn;
141 blkcg_pol_offline_pd_fn *pd_offline_fn;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700142 blkcg_pol_exit_pd_fn *pd_exit_fn;
143 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -0500144};
145
Tejun Heo3c798392012-04-16 13:57:25 -0700146extern struct blkcg blkcg_root;
Tejun Heo496d5e72015-05-22 17:13:21 -0400147extern struct cgroup_subsys_state * const blkcg_root_css;
Tejun Heo36558c82012-04-16 13:57:24 -0700148
Tejun Heo3c798392012-04-16 13:57:25 -0700149struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q);
150struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
151 struct request_queue *q);
Tejun Heo36558c82012-04-16 13:57:24 -0700152int blkcg_init_queue(struct request_queue *q);
153void blkcg_drain_queue(struct request_queue *q);
154void blkcg_exit_queue(struct request_queue *q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800155
Vivek Goyal3e252062009-12-04 10:36:42 -0500156/* Blkio controller policy registration */
Jens Axboed5bf0292014-06-22 16:31:56 -0600157int blkcg_policy_register(struct blkcg_policy *pol);
Tejun Heo3c798392012-04-16 13:57:25 -0700158void blkcg_policy_unregister(struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700159int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700160 const struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700161void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700162 const struct blkcg_policy *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500163
Tejun Heo3c798392012-04-16 13:57:25 -0700164void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700165 u64 (*prfill)(struct seq_file *,
166 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700167 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700168 bool show_total);
Tejun Heof95a04a2012-04-16 13:57:26 -0700169u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
170u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo829fdb52012-04-01 14:38:43 -0700171 const struct blkg_rwstat *rwstat);
Tejun Heof95a04a2012-04-16 13:57:26 -0700172u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
173u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
174 int off);
Tejun Heo829fdb52012-04-01 14:38:43 -0700175
Tejun Heo16b3de62013-01-09 08:05:12 -0800176u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off);
177struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
178 int off);
179
Tejun Heo829fdb52012-04-01 14:38:43 -0700180struct blkg_conf_ctx {
Tejun Heo36558c82012-04-16 13:57:24 -0700181 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700182 struct blkcg_gq *blkg;
Tejun Heo36558c82012-04-16 13:57:24 -0700183 u64 v;
Tejun Heo829fdb52012-04-01 14:38:43 -0700184};
185
Tejun Heo3c798392012-04-16 13:57:25 -0700186int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
187 const char *input, struct blkg_conf_ctx *ctx);
Tejun Heo829fdb52012-04-01 14:38:43 -0700188void blkg_conf_finish(struct blkg_conf_ctx *ctx);
189
190
Tejun Heoa7c6d552013-08-08 20:11:23 -0400191static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
192{
193 return css ? container_of(css, struct blkcg, css) : NULL;
194}
195
Tejun Heob1208b52012-06-04 20:40:57 -0700196static inline struct blkcg *task_blkcg(struct task_struct *tsk)
197{
Tejun Heo073219e2014-02-08 10:36:58 -0500198 return css_to_blkcg(task_css(tsk, blkio_cgrp_id));
Tejun Heob1208b52012-06-04 20:40:57 -0700199}
200
201static inline struct blkcg *bio_blkcg(struct bio *bio)
202{
203 if (bio && bio->bi_css)
Tejun Heoa7c6d552013-08-08 20:11:23 -0400204 return css_to_blkcg(bio->bi_css);
Tejun Heob1208b52012-06-04 20:40:57 -0700205 return task_blkcg(current);
206}
207
Tejun Heofd383c22015-05-22 17:13:23 -0400208static inline struct cgroup_subsys_state *
209task_get_blkcg_css(struct task_struct *task)
210{
211 return task_get_css(task, blkio_cgrp_id);
212}
213
Tejun Heo03814112012-03-05 13:15:14 -0800214/**
Tejun Heo3c547862013-01-09 08:05:10 -0800215 * blkcg_parent - get the parent of a blkcg
216 * @blkcg: blkcg of interest
217 *
218 * Return the parent blkcg of @blkcg. Can be called anytime.
219 */
220static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
221{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400222 return css_to_blkcg(blkcg->css.parent);
Tejun Heo3c547862013-01-09 08:05:10 -0800223}
224
225/**
Tejun Heo03814112012-03-05 13:15:14 -0800226 * blkg_to_pdata - get policy private data
227 * @blkg: blkg of interest
228 * @pol: policy of interest
229 *
230 * Return pointer to private data associated with the @blkg-@pol pair.
231 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700232static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
233 struct blkcg_policy *pol)
Tejun Heo03814112012-03-05 13:15:14 -0800234{
Tejun Heof95a04a2012-04-16 13:57:26 -0700235 return blkg ? blkg->pd[pol->plid] : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800236}
237
238/**
239 * pdata_to_blkg - get blkg associated with policy private data
Tejun Heof95a04a2012-04-16 13:57:26 -0700240 * @pd: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800241 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700242 * @pd is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800243 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700244static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
Tejun Heo03814112012-03-05 13:15:14 -0800245{
Tejun Heof95a04a2012-04-16 13:57:26 -0700246 return pd ? pd->blkg : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800247}
248
Tejun Heo54e7ed12012-04-16 13:57:23 -0700249/**
250 * blkg_path - format cgroup path of blkg
251 * @blkg: blkg of interest
252 * @buf: target buffer
253 * @buflen: target buffer length
254 *
255 * Format the path of the cgroup of @blkg into @buf.
256 */
Tejun Heo3c798392012-04-16 13:57:25 -0700257static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200258{
Tejun Heoe61734c2014-02-12 09:29:50 -0500259 char *p;
Tejun Heo54e7ed12012-04-16 13:57:23 -0700260
Tejun Heoe61734c2014-02-12 09:29:50 -0500261 p = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
262 if (!p) {
Tejun Heo54e7ed12012-04-16 13:57:23 -0700263 strncpy(buf, "<unavailable>", buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500264 return -ENAMETOOLONG;
265 }
266
267 memmove(buf, p, buf + buflen - p);
268 return 0;
Vivek Goyalafc24d42010-04-26 19:27:56 +0200269}
270
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800271/**
272 * blkg_get - get a blkg reference
273 * @blkg: blkg to get
274 *
Tejun Heoa5049a82014-06-19 17:42:57 -0400275 * The caller should be holding an existing reference.
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800276 */
Tejun Heo3c798392012-04-16 13:57:25 -0700277static inline void blkg_get(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800278{
Tejun Heoa5049a82014-06-19 17:42:57 -0400279 WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
280 atomic_inc(&blkg->refcnt);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800281}
282
Tejun Heo2a4fd072013-05-14 13:52:31 -0700283void __blkg_release_rcu(struct rcu_head *rcu);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800284
285/**
286 * blkg_put - put a blkg reference
287 * @blkg: blkg to put
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800288 */
Tejun Heo3c798392012-04-16 13:57:25 -0700289static inline void blkg_put(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800290{
Tejun Heoa5049a82014-06-19 17:42:57 -0400291 WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
292 if (atomic_dec_and_test(&blkg->refcnt))
Tejun Heo2a4fd072013-05-14 13:52:31 -0700293 call_rcu(&blkg->rcu_head, __blkg_release_rcu);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800294}
295
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700296struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, struct request_queue *q,
297 bool update_hint);
298
299/**
300 * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
301 * @d_blkg: loop cursor pointing to the current descendant
Tejun Heo492eb212013-08-08 20:11:25 -0400302 * @pos_css: used for iteration
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700303 * @p_blkg: target blkg to walk descendants of
304 *
305 * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
306 * read locked. If called under either blkcg or queue lock, the iteration
307 * is guaranteed to include all and only online blkgs. The caller may
Tejun Heo492eb212013-08-08 20:11:25 -0400308 * update @pos_css by calling css_rightmost_descendant() to skip subtree.
Tejun Heobd8815a2013-08-08 20:11:27 -0400309 * @p_blkg is included in the iteration and the first node to be visited.
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700310 */
Tejun Heo492eb212013-08-08 20:11:25 -0400311#define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
312 css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
313 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700314 (p_blkg)->q, false)))
315
Tejun Heoedcb0722012-04-01 14:38:42 -0700316/**
Tejun Heoaa539cb2013-05-14 13:52:31 -0700317 * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
318 * @d_blkg: loop cursor pointing to the current descendant
Tejun Heo492eb212013-08-08 20:11:25 -0400319 * @pos_css: used for iteration
Tejun Heoaa539cb2013-05-14 13:52:31 -0700320 * @p_blkg: target blkg to walk descendants of
321 *
322 * Similar to blkg_for_each_descendant_pre() but performs post-order
Tejun Heobd8815a2013-08-08 20:11:27 -0400323 * traversal instead. Synchronization rules are the same. @p_blkg is
324 * included in the iteration and the last node to be visited.
Tejun Heoaa539cb2013-05-14 13:52:31 -0700325 */
Tejun Heo492eb212013-08-08 20:11:25 -0400326#define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
327 css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
328 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
Tejun Heoaa539cb2013-05-14 13:52:31 -0700329 (p_blkg)->q, false)))
330
331/**
Tejun Heoa0516612012-06-26 15:05:44 -0700332 * blk_get_rl - get request_list to use
333 * @q: request_queue of interest
334 * @bio: bio which will be attached to the allocated request (may be %NULL)
335 *
336 * The caller wants to allocate a request from @q to use for @bio. Find
337 * the request_list to use and obtain a reference on it. Should be called
338 * under queue_lock. This function is guaranteed to return non-%NULL
339 * request_list.
340 */
341static inline struct request_list *blk_get_rl(struct request_queue *q,
342 struct bio *bio)
343{
344 struct blkcg *blkcg;
345 struct blkcg_gq *blkg;
346
347 rcu_read_lock();
348
349 blkcg = bio_blkcg(bio);
350
351 /* bypass blkg lookup and use @q->root_rl directly for root */
352 if (blkcg == &blkcg_root)
353 goto root_rl;
354
355 /*
356 * Try to use blkg->rl. blkg lookup may fail under memory pressure
357 * or if either the blkcg or queue is going away. Fall back to
358 * root_rl in such cases.
359 */
360 blkg = blkg_lookup_create(blkcg, q);
361 if (unlikely(IS_ERR(blkg)))
362 goto root_rl;
363
364 blkg_get(blkg);
365 rcu_read_unlock();
366 return &blkg->rl;
367root_rl:
368 rcu_read_unlock();
369 return &q->root_rl;
370}
371
372/**
373 * blk_put_rl - put request_list
374 * @rl: request_list to put
375 *
376 * Put the reference acquired by blk_get_rl(). Should be called under
377 * queue_lock.
378 */
379static inline void blk_put_rl(struct request_list *rl)
380{
381 /* root_rl may not have blkg set */
382 if (rl->blkg && rl->blkg->blkcg != &blkcg_root)
383 blkg_put(rl->blkg);
384}
385
386/**
387 * blk_rq_set_rl - associate a request with a request_list
388 * @rq: request of interest
389 * @rl: target request_list
390 *
391 * Associate @rq with @rl so that accounting and freeing can know the
392 * request_list @rq came from.
393 */
394static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
395{
396 rq->rl = rl;
397}
398
399/**
400 * blk_rq_rl - return the request_list a request came from
401 * @rq: request of interest
402 *
403 * Return the request_list @rq is allocated from.
404 */
405static inline struct request_list *blk_rq_rl(struct request *rq)
406{
407 return rq->rl;
408}
409
410struct request_list *__blk_queue_next_rl(struct request_list *rl,
411 struct request_queue *q);
412/**
413 * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
414 *
415 * Should be used under queue_lock.
416 */
417#define blk_queue_for_each_rl(rl, q) \
418 for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
419
Peter Zijlstra90d38392013-11-12 19:42:14 -0800420static inline void blkg_stat_init(struct blkg_stat *stat)
421{
422 u64_stats_init(&stat->syncp);
423}
424
Tejun Heoa0516612012-06-26 15:05:44 -0700425/**
Tejun Heoedcb0722012-04-01 14:38:42 -0700426 * blkg_stat_add - add a value to a blkg_stat
427 * @stat: target blkg_stat
428 * @val: value to add
429 *
430 * Add @val to @stat. The caller is responsible for synchronizing calls to
431 * this function.
432 */
433static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
434{
435 u64_stats_update_begin(&stat->syncp);
436 stat->cnt += val;
437 u64_stats_update_end(&stat->syncp);
438}
439
440/**
441 * blkg_stat_read - read the current value of a blkg_stat
442 * @stat: blkg_stat to read
443 *
444 * Read the current value of @stat. This function can be called without
445 * synchroniztion and takes care of u64 atomicity.
446 */
447static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
448{
449 unsigned int start;
450 uint64_t v;
451
452 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700453 start = u64_stats_fetch_begin_irq(&stat->syncp);
Tejun Heoedcb0722012-04-01 14:38:42 -0700454 v = stat->cnt;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700455 } while (u64_stats_fetch_retry_irq(&stat->syncp, start));
Tejun Heoedcb0722012-04-01 14:38:42 -0700456
457 return v;
458}
459
460/**
461 * blkg_stat_reset - reset a blkg_stat
462 * @stat: blkg_stat to reset
463 */
464static inline void blkg_stat_reset(struct blkg_stat *stat)
465{
466 stat->cnt = 0;
467}
468
469/**
Tejun Heo16b3de62013-01-09 08:05:12 -0800470 * blkg_stat_merge - merge a blkg_stat into another
471 * @to: the destination blkg_stat
472 * @from: the source
473 *
474 * Add @from's count to @to.
475 */
476static inline void blkg_stat_merge(struct blkg_stat *to, struct blkg_stat *from)
477{
478 blkg_stat_add(to, blkg_stat_read(from));
479}
480
Peter Zijlstra90d38392013-11-12 19:42:14 -0800481static inline void blkg_rwstat_init(struct blkg_rwstat *rwstat)
482{
483 u64_stats_init(&rwstat->syncp);
484}
485
Tejun Heo16b3de62013-01-09 08:05:12 -0800486/**
Tejun Heoedcb0722012-04-01 14:38:42 -0700487 * blkg_rwstat_add - add a value to a blkg_rwstat
488 * @rwstat: target blkg_rwstat
489 * @rw: mask of REQ_{WRITE|SYNC}
490 * @val: value to add
491 *
492 * Add @val to @rwstat. The counters are chosen according to @rw. The
493 * caller is responsible for synchronizing calls to this function.
494 */
495static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
496 int rw, uint64_t val)
497{
498 u64_stats_update_begin(&rwstat->syncp);
499
500 if (rw & REQ_WRITE)
501 rwstat->cnt[BLKG_RWSTAT_WRITE] += val;
502 else
503 rwstat->cnt[BLKG_RWSTAT_READ] += val;
504 if (rw & REQ_SYNC)
505 rwstat->cnt[BLKG_RWSTAT_SYNC] += val;
506 else
507 rwstat->cnt[BLKG_RWSTAT_ASYNC] += val;
508
509 u64_stats_update_end(&rwstat->syncp);
510}
511
512/**
513 * blkg_rwstat_read - read the current values of a blkg_rwstat
514 * @rwstat: blkg_rwstat to read
515 *
516 * Read the current snapshot of @rwstat and return it as the return value.
517 * This function can be called without synchronization and takes care of
518 * u64 atomicity.
519 */
Tejun Heoc94bed892012-04-16 13:57:22 -0700520static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700521{
522 unsigned int start;
523 struct blkg_rwstat tmp;
524
525 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700526 start = u64_stats_fetch_begin_irq(&rwstat->syncp);
Tejun Heoedcb0722012-04-01 14:38:42 -0700527 tmp = *rwstat;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700528 } while (u64_stats_fetch_retry_irq(&rwstat->syncp, start));
Tejun Heoedcb0722012-04-01 14:38:42 -0700529
530 return tmp;
531}
532
533/**
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800534 * blkg_rwstat_total - read the total count of a blkg_rwstat
Tejun Heoedcb0722012-04-01 14:38:42 -0700535 * @rwstat: blkg_rwstat to read
536 *
537 * Return the total count of @rwstat regardless of the IO direction. This
538 * function can be called without synchronization and takes care of u64
539 * atomicity.
540 */
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800541static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
Tejun Heoedcb0722012-04-01 14:38:42 -0700542{
543 struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
544
545 return tmp.cnt[BLKG_RWSTAT_READ] + tmp.cnt[BLKG_RWSTAT_WRITE];
546}
547
548/**
549 * blkg_rwstat_reset - reset a blkg_rwstat
550 * @rwstat: blkg_rwstat to reset
551 */
552static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
553{
554 memset(rwstat->cnt, 0, sizeof(rwstat->cnt));
555}
556
Tejun Heo16b3de62013-01-09 08:05:12 -0800557/**
558 * blkg_rwstat_merge - merge a blkg_rwstat into another
559 * @to: the destination blkg_rwstat
560 * @from: the source
561 *
562 * Add @from's counts to @to.
563 */
564static inline void blkg_rwstat_merge(struct blkg_rwstat *to,
565 struct blkg_rwstat *from)
566{
567 struct blkg_rwstat v = blkg_rwstat_read(from);
568 int i;
569
570 u64_stats_update_begin(&to->syncp);
571 for (i = 0; i < BLKG_RWSTAT_NR; i++)
572 to->cnt[i] += v.cnt[i];
573 u64_stats_update_end(&to->syncp);
574}
575
Tejun Heo36558c82012-04-16 13:57:24 -0700576#else /* CONFIG_BLK_CGROUP */
577
Tejun Heoefa7d1c2015-05-22 17:13:18 -0400578struct blkcg {
579};
Jens Axboe2f5ea472009-12-03 21:06:43 +0100580
Tejun Heof95a04a2012-04-16 13:57:26 -0700581struct blkg_policy_data {
582};
583
Tejun Heo3c798392012-04-16 13:57:25 -0700584struct blkcg_gq {
Jens Axboe2f5ea472009-12-03 21:06:43 +0100585};
586
Tejun Heo3c798392012-04-16 13:57:25 -0700587struct blkcg_policy {
Vivek Goyal3e252062009-12-04 10:36:42 -0500588};
589
Tejun Heo496d5e72015-05-22 17:13:21 -0400590#define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
591
Tejun Heofd383c22015-05-22 17:13:23 -0400592static inline struct cgroup_subsys_state *
593task_get_blkcg_css(struct task_struct *task)
594{
595 return NULL;
596}
597
Tejun Heoefa7d1c2015-05-22 17:13:18 -0400598#ifdef CONFIG_BLOCK
599
Tejun Heo3c798392012-04-16 13:57:25 -0700600static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800601static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
602static inline void blkcg_drain_queue(struct request_queue *q) { }
603static inline void blkcg_exit_queue(struct request_queue *q) { }
Jens Axboed5bf0292014-06-22 16:31:56 -0600604static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
Tejun Heo3c798392012-04-16 13:57:25 -0700605static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700606static inline int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700607 const struct blkcg_policy *pol) { return 0; }
Tejun Heoa2b16932012-04-13 13:11:33 -0700608static inline void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700609 const struct blkcg_policy *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500610
Tejun Heob1208b52012-06-04 20:40:57 -0700611static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
Tejun Heoa0516612012-06-26 15:05:44 -0700612
Tejun Heof95a04a2012-04-16 13:57:26 -0700613static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
614 struct blkcg_policy *pol) { return NULL; }
615static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
Tejun Heo3c798392012-04-16 13:57:25 -0700616static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
617static inline void blkg_get(struct blkcg_gq *blkg) { }
618static inline void blkg_put(struct blkcg_gq *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200619
Tejun Heoa0516612012-06-26 15:05:44 -0700620static inline struct request_list *blk_get_rl(struct request_queue *q,
621 struct bio *bio) { return &q->root_rl; }
622static inline void blk_put_rl(struct request_list *rl) { }
623static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { }
624static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; }
625
626#define blk_queue_for_each_rl(rl, q) \
627 for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
628
Tejun Heoefa7d1c2015-05-22 17:13:18 -0400629#endif /* CONFIG_BLOCK */
Tejun Heo36558c82012-04-16 13:57:24 -0700630#endif /* CONFIG_BLK_CGROUP */
631#endif /* _BLK_CGROUP_H */