1672fdcf0SMing Lei /* SPDX-License-Identifier: GPL-2.0 */
2672fdcf0SMing Lei #ifndef _BLK_CGROUP_PRIVATE_H
3672fdcf0SMing Lei #define _BLK_CGROUP_PRIVATE_H
4672fdcf0SMing Lei /*
5672fdcf0SMing Lei * block cgroup private header
6672fdcf0SMing Lei *
7672fdcf0SMing Lei * Based on ideas and code from CFQ, CFS and BFQ:
8672fdcf0SMing Lei * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9672fdcf0SMing Lei *
10672fdcf0SMing Lei * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
11672fdcf0SMing Lei * Paolo Valente <paolo.valente@unimore.it>
12672fdcf0SMing Lei *
13672fdcf0SMing Lei * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
14672fdcf0SMing Lei * Nauman Rafique <nauman@google.com>
15672fdcf0SMing Lei */
16672fdcf0SMing Lei
17672fdcf0SMing Lei #include <linux/blk-cgroup.h>
18c97ab271SChristoph Hellwig #include <linux/cgroup.h>
19c97ab271SChristoph Hellwig #include <linux/kthread.h>
206b2b0459STejun Heo #include <linux/blk-mq.h>
213b8cc629SWaiman Long #include <linux/llist.h>
22672fdcf0SMing Lei
23dec223c9SChristoph Hellwig struct blkcg_gq;
24dec223c9SChristoph Hellwig struct blkg_policy_data;
25dec223c9SChristoph Hellwig
26dec223c9SChristoph Hellwig
27672fdcf0SMing Lei /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
28672fdcf0SMing Lei #define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
29672fdcf0SMing Lei
30672fdcf0SMing Lei #ifdef CONFIG_BLK_CGROUP
31bbb1ebe7SChristoph Hellwig
32bbb1ebe7SChristoph Hellwig enum blkg_iostat_type {
33bbb1ebe7SChristoph Hellwig BLKG_IOSTAT_READ,
34bbb1ebe7SChristoph Hellwig BLKG_IOSTAT_WRITE,
35bbb1ebe7SChristoph Hellwig BLKG_IOSTAT_DISCARD,
36bbb1ebe7SChristoph Hellwig
37bbb1ebe7SChristoph Hellwig BLKG_IOSTAT_NR,
38bbb1ebe7SChristoph Hellwig };
39bbb1ebe7SChristoph Hellwig
40bbb1ebe7SChristoph Hellwig struct blkg_iostat {
41bbb1ebe7SChristoph Hellwig u64 bytes[BLKG_IOSTAT_NR];
42bbb1ebe7SChristoph Hellwig u64 ios[BLKG_IOSTAT_NR];
43bbb1ebe7SChristoph Hellwig };
44bbb1ebe7SChristoph Hellwig
45bbb1ebe7SChristoph Hellwig struct blkg_iostat_set {
46bbb1ebe7SChristoph Hellwig struct u64_stats_sync sync;
473b8cc629SWaiman Long struct blkcg_gq *blkg;
483b8cc629SWaiman Long struct llist_node lnode;
493b8cc629SWaiman Long int lqueued; /* queued in llist */
50bbb1ebe7SChristoph Hellwig struct blkg_iostat cur;
51bbb1ebe7SChristoph Hellwig struct blkg_iostat last;
52bbb1ebe7SChristoph Hellwig };
53bbb1ebe7SChristoph Hellwig
54bbb1ebe7SChristoph Hellwig /* association between a blk cgroup and a request queue */
55bbb1ebe7SChristoph Hellwig struct blkcg_gq {
56a06377c5SChristoph Hellwig /* Pointer to the associated request_queue */
57a06377c5SChristoph Hellwig struct request_queue *q;
581231039dSChristoph Hellwig struct list_head q_node;
59bbb1ebe7SChristoph Hellwig struct hlist_node blkcg_node;
60bbb1ebe7SChristoph Hellwig struct blkcg *blkcg;
61bbb1ebe7SChristoph Hellwig
62bbb1ebe7SChristoph Hellwig /* all non-root blkcg_gq's are guaranteed to have access to parent */
63bbb1ebe7SChristoph Hellwig struct blkcg_gq *parent;
64bbb1ebe7SChristoph Hellwig
65bbb1ebe7SChristoph Hellwig /* reference count */
66bbb1ebe7SChristoph Hellwig struct percpu_ref refcnt;
67bbb1ebe7SChristoph Hellwig
68bbb1ebe7SChristoph Hellwig /* is this blkg online? protected by both blkcg and q locks */
69bbb1ebe7SChristoph Hellwig bool online;
70bbb1ebe7SChristoph Hellwig
71bbb1ebe7SChristoph Hellwig struct blkg_iostat_set __percpu *iostat_cpu;
72bbb1ebe7SChristoph Hellwig struct blkg_iostat_set iostat;
73bbb1ebe7SChristoph Hellwig
74bbb1ebe7SChristoph Hellwig struct blkg_policy_data *pd[BLKCG_MAX_POLS];
752c275afeSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
76bbb1ebe7SChristoph Hellwig spinlock_t async_bio_lock;
77bbb1ebe7SChristoph Hellwig struct bio_list async_bios;
782c275afeSChristoph Hellwig #endif
79bbb1ebe7SChristoph Hellwig union {
80bbb1ebe7SChristoph Hellwig struct work_struct async_bio_work;
81bbb1ebe7SChristoph Hellwig struct work_struct free_work;
82bbb1ebe7SChristoph Hellwig };
83bbb1ebe7SChristoph Hellwig
84bbb1ebe7SChristoph Hellwig atomic_t use_delay;
85bbb1ebe7SChristoph Hellwig atomic64_t delay_nsec;
86bbb1ebe7SChristoph Hellwig atomic64_t delay_start;
87bbb1ebe7SChristoph Hellwig u64 last_delay;
88bbb1ebe7SChristoph Hellwig int last_use;
89bbb1ebe7SChristoph Hellwig
90bbb1ebe7SChristoph Hellwig struct rcu_head rcu_head;
91bbb1ebe7SChristoph Hellwig };
92bbb1ebe7SChristoph Hellwig
93dec223c9SChristoph Hellwig struct blkcg {
94dec223c9SChristoph Hellwig struct cgroup_subsys_state css;
95dec223c9SChristoph Hellwig spinlock_t lock;
96dec223c9SChristoph Hellwig refcount_t online_pin;
97dec223c9SChristoph Hellwig
98dec223c9SChristoph Hellwig struct radix_tree_root blkg_tree;
99dec223c9SChristoph Hellwig struct blkcg_gq __rcu *blkg_hint;
100dec223c9SChristoph Hellwig struct hlist_head blkg_list;
101dec223c9SChristoph Hellwig
102dec223c9SChristoph Hellwig struct blkcg_policy_data *cpd[BLKCG_MAX_POLS];
103dec223c9SChristoph Hellwig
104dec223c9SChristoph Hellwig struct list_head all_blkcgs_node;
1053b8cc629SWaiman Long
1063b8cc629SWaiman Long /*
1073b8cc629SWaiman Long * List of updated percpu blkg_iostat_set's since the last flush.
1083b8cc629SWaiman Long */
1093b8cc629SWaiman Long struct llist_head __percpu *lhead;
1103b8cc629SWaiman Long
111dec223c9SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_FC_APPID
112dec223c9SChristoph Hellwig char fc_app_id[FC_APPID_LEN];
113dec223c9SChristoph Hellwig #endif
114dec223c9SChristoph Hellwig #ifdef CONFIG_CGROUP_WRITEBACK
115dec223c9SChristoph Hellwig struct list_head cgwb_list;
116dec223c9SChristoph Hellwig #endif
117dec223c9SChristoph Hellwig };
118dec223c9SChristoph Hellwig
css_to_blkcg(struct cgroup_subsys_state * css)119dec223c9SChristoph Hellwig static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
120dec223c9SChristoph Hellwig {
121dec223c9SChristoph Hellwig return css ? container_of(css, struct blkcg, css) : NULL;
122dec223c9SChristoph Hellwig }
123672fdcf0SMing Lei
124672fdcf0SMing Lei /*
125672fdcf0SMing Lei * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
126672fdcf0SMing Lei * request_queue (q). This is used by blkcg policies which need to track
127672fdcf0SMing Lei * information per blkcg - q pair.
128672fdcf0SMing Lei *
129672fdcf0SMing Lei * There can be multiple active blkcg policies and each blkg:policy pair is
130672fdcf0SMing Lei * represented by a blkg_policy_data which is allocated and freed by each
131672fdcf0SMing Lei * policy's pd_alloc/free_fn() methods. A policy can allocate private data
132672fdcf0SMing Lei * area by allocating larger data structure which embeds blkg_policy_data
133672fdcf0SMing Lei * at the beginning.
134672fdcf0SMing Lei */
135672fdcf0SMing Lei struct blkg_policy_data {
136672fdcf0SMing Lei /* the blkg and policy id this per-policy data belongs to */
137672fdcf0SMing Lei struct blkcg_gq *blkg;
138672fdcf0SMing Lei int plid;
139dfd6200aSYu Kuai bool online;
140672fdcf0SMing Lei };
141672fdcf0SMing Lei
142672fdcf0SMing Lei /*
143672fdcf0SMing Lei * Policies that need to keep per-blkcg data which is independent from any
144672fdcf0SMing Lei * request_queue associated to it should implement cpd_alloc/free_fn()
145672fdcf0SMing Lei * methods. A policy can allocate private data area by allocating larger
146672fdcf0SMing Lei * data structure which embeds blkcg_policy_data at the beginning.
147672fdcf0SMing Lei * cpd_init() is invoked to let each policy handle per-blkcg data.
148672fdcf0SMing Lei */
149672fdcf0SMing Lei struct blkcg_policy_data {
150672fdcf0SMing Lei /* the blkcg and policy id this per-policy data belongs to */
151672fdcf0SMing Lei struct blkcg *blkcg;
152672fdcf0SMing Lei int plid;
153672fdcf0SMing Lei };
154672fdcf0SMing Lei
155672fdcf0SMing Lei typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
156672fdcf0SMing Lei typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
157672fdcf0SMing Lei typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
158672fdcf0SMing Lei typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
1590a0b4f79SChristoph Hellwig typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(struct gendisk *disk,
1600a0b4f79SChristoph Hellwig struct blkcg *blkcg, gfp_t gfp);
161672fdcf0SMing Lei typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
162672fdcf0SMing Lei typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
163672fdcf0SMing Lei typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
164672fdcf0SMing Lei typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
165672fdcf0SMing Lei typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
1663607849dSWolfgang Bumiller typedef void (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd,
167672fdcf0SMing Lei struct seq_file *s);
168672fdcf0SMing Lei
169672fdcf0SMing Lei struct blkcg_policy {
170672fdcf0SMing Lei int plid;
171672fdcf0SMing Lei /* cgroup files for the policy */
172672fdcf0SMing Lei struct cftype *dfl_cftypes;
173672fdcf0SMing Lei struct cftype *legacy_cftypes;
174672fdcf0SMing Lei
175672fdcf0SMing Lei /* operations */
176672fdcf0SMing Lei blkcg_pol_alloc_cpd_fn *cpd_alloc_fn;
177672fdcf0SMing Lei blkcg_pol_free_cpd_fn *cpd_free_fn;
178672fdcf0SMing Lei
179672fdcf0SMing Lei blkcg_pol_alloc_pd_fn *pd_alloc_fn;
180672fdcf0SMing Lei blkcg_pol_init_pd_fn *pd_init_fn;
181672fdcf0SMing Lei blkcg_pol_online_pd_fn *pd_online_fn;
182672fdcf0SMing Lei blkcg_pol_offline_pd_fn *pd_offline_fn;
183672fdcf0SMing Lei blkcg_pol_free_pd_fn *pd_free_fn;
184672fdcf0SMing Lei blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
185672fdcf0SMing Lei blkcg_pol_stat_pd_fn *pd_stat_fn;
186672fdcf0SMing Lei };
187672fdcf0SMing Lei
188672fdcf0SMing Lei extern struct blkcg blkcg_root;
189672fdcf0SMing Lei extern bool blkcg_debug_stats;
190672fdcf0SMing Lei
191*740ffad9SMing Lei void blkg_init_queue(struct request_queue *q);
1929823538fSChristoph Hellwig int blkcg_init_disk(struct gendisk *disk);
1939823538fSChristoph Hellwig void blkcg_exit_disk(struct gendisk *disk);
194672fdcf0SMing Lei
195672fdcf0SMing Lei /* Blkio controller policy registration */
196672fdcf0SMing Lei int blkcg_policy_register(struct blkcg_policy *pol);
197672fdcf0SMing Lei void blkcg_policy_unregister(struct blkcg_policy *pol);
19840e4996eSChristoph Hellwig int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol);
19940e4996eSChristoph Hellwig void blkcg_deactivate_policy(struct gendisk *disk,
200672fdcf0SMing Lei const struct blkcg_policy *pol);
201672fdcf0SMing Lei
202672fdcf0SMing Lei const char *blkg_dev_name(struct blkcg_gq *blkg);
203672fdcf0SMing Lei void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
204672fdcf0SMing Lei u64 (*prfill)(struct seq_file *,
205672fdcf0SMing Lei struct blkg_policy_data *, int),
206672fdcf0SMing Lei const struct blkcg_policy *pol, int data,
207672fdcf0SMing Lei bool show_total);
208672fdcf0SMing Lei u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
209672fdcf0SMing Lei
210672fdcf0SMing Lei struct blkg_conf_ctx {
211faffaab2STejun Heo char *input;
212faffaab2STejun Heo char *body;
213672fdcf0SMing Lei struct block_device *bdev;
214672fdcf0SMing Lei struct blkcg_gq *blkg;
215672fdcf0SMing Lei };
216672fdcf0SMing Lei
217faffaab2STejun Heo void blkg_conf_init(struct blkg_conf_ctx *ctx, char *input);
218faffaab2STejun Heo int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx);
219672fdcf0SMing Lei int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
220faffaab2STejun Heo struct blkg_conf_ctx *ctx);
221faffaab2STejun Heo void blkg_conf_exit(struct blkg_conf_ctx *ctx);
222672fdcf0SMing Lei
223672fdcf0SMing Lei /**
224672fdcf0SMing Lei * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
225672fdcf0SMing Lei * @return: true if this bio needs to be submitted with the root blkg context.
226672fdcf0SMing Lei *
227672fdcf0SMing Lei * In order to avoid priority inversions we sometimes need to issue a bio as if
228672fdcf0SMing Lei * it were attached to the root blkg, and then backcharge to the actual owning
229bbb1ebe7SChristoph Hellwig * blkg. The idea is we do bio_blkcg_css() to look up the actual context for
230bbb1ebe7SChristoph Hellwig * the bio and attach the appropriate blkg to the bio. Then we call this helper
231bbb1ebe7SChristoph Hellwig * and if it is true run with the root blkg for that queue and then do any
232672fdcf0SMing Lei * backcharging to the originating cgroup once the io is complete.
233672fdcf0SMing Lei */
bio_issue_as_root_blkg(struct bio * bio)234672fdcf0SMing Lei static inline bool bio_issue_as_root_blkg(struct bio *bio)
235672fdcf0SMing Lei {
236672fdcf0SMing Lei return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
237672fdcf0SMing Lei }
238672fdcf0SMing Lei
239672fdcf0SMing Lei /**
2409a9c261eSChristoph Hellwig * blkg_lookup - lookup blkg for the specified blkcg - q pair
241672fdcf0SMing Lei * @blkcg: blkcg of interest
2429a9c261eSChristoph Hellwig * @q: request_queue of interest
243672fdcf0SMing Lei *
2449a9c261eSChristoph Hellwig * Lookup blkg for the @blkcg - @q pair.
2454a69f325SChristoph Hellwig
2464a69f325SChristoph Hellwig * Must be called in a RCU critical section.
247672fdcf0SMing Lei */
blkg_lookup(struct blkcg * blkcg,struct request_queue * q)2484a69f325SChristoph Hellwig static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
2499a9c261eSChristoph Hellwig struct request_queue *q)
250672fdcf0SMing Lei {
251672fdcf0SMing Lei struct blkcg_gq *blkg;
252672fdcf0SMing Lei
253672fdcf0SMing Lei if (blkcg == &blkcg_root)
2549a9c261eSChristoph Hellwig return q->root_blkg;
255672fdcf0SMing Lei
256a33c741cSMing Lei blkg = rcu_dereference_check(blkcg->blkg_hint,
257a33c741cSMing Lei lockdep_is_held(&q->queue_lock));
258a06377c5SChristoph Hellwig if (blkg && blkg->q == q)
259672fdcf0SMing Lei return blkg;
260672fdcf0SMing Lei
2619a9c261eSChristoph Hellwig blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
262a06377c5SChristoph Hellwig if (blkg && blkg->q != q)
2634a69f325SChristoph Hellwig blkg = NULL;
2644a69f325SChristoph Hellwig return blkg;
265672fdcf0SMing Lei }
266672fdcf0SMing Lei
267672fdcf0SMing Lei /**
268672fdcf0SMing Lei * blkg_to_pdata - get policy private data
269672fdcf0SMing Lei * @blkg: blkg of interest
270672fdcf0SMing Lei * @pol: policy of interest
271672fdcf0SMing Lei *
272672fdcf0SMing Lei * Return pointer to private data associated with the @blkg-@pol pair.
273672fdcf0SMing Lei */
blkg_to_pd(struct blkcg_gq * blkg,struct blkcg_policy * pol)274672fdcf0SMing Lei static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
275672fdcf0SMing Lei struct blkcg_policy *pol)
276672fdcf0SMing Lei {
277672fdcf0SMing Lei return blkg ? blkg->pd[pol->plid] : NULL;
278672fdcf0SMing Lei }
279672fdcf0SMing Lei
blkcg_to_cpd(struct blkcg * blkcg,struct blkcg_policy * pol)280672fdcf0SMing Lei static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
281672fdcf0SMing Lei struct blkcg_policy *pol)
282672fdcf0SMing Lei {
283672fdcf0SMing Lei return blkcg ? blkcg->cpd[pol->plid] : NULL;
284672fdcf0SMing Lei }
285672fdcf0SMing Lei
286672fdcf0SMing Lei /**
287672fdcf0SMing Lei * pdata_to_blkg - get blkg associated with policy private data
288672fdcf0SMing Lei * @pd: policy private data of interest
289672fdcf0SMing Lei *
290672fdcf0SMing Lei * @pd is policy private data. Determine the blkg it's associated with.
291672fdcf0SMing Lei */
pd_to_blkg(struct blkg_policy_data * pd)292672fdcf0SMing Lei static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
293672fdcf0SMing Lei {
294672fdcf0SMing Lei return pd ? pd->blkg : NULL;
295672fdcf0SMing Lei }
296672fdcf0SMing Lei
cpd_to_blkcg(struct blkcg_policy_data * cpd)297672fdcf0SMing Lei static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
298672fdcf0SMing Lei {
299672fdcf0SMing Lei return cpd ? cpd->blkcg : NULL;
300672fdcf0SMing Lei }
301672fdcf0SMing Lei
302672fdcf0SMing Lei /**
303672fdcf0SMing Lei * blkg_path - format cgroup path of blkg
304672fdcf0SMing Lei * @blkg: blkg of interest
305672fdcf0SMing Lei * @buf: target buffer
306672fdcf0SMing Lei * @buflen: target buffer length
307672fdcf0SMing Lei *
308672fdcf0SMing Lei * Format the path of the cgroup of @blkg into @buf.
309672fdcf0SMing Lei */
blkg_path(struct blkcg_gq * blkg,char * buf,int buflen)310672fdcf0SMing Lei static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
311672fdcf0SMing Lei {
312672fdcf0SMing Lei return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
313672fdcf0SMing Lei }
314672fdcf0SMing Lei
315672fdcf0SMing Lei /**
316672fdcf0SMing Lei * blkg_get - get a blkg reference
317672fdcf0SMing Lei * @blkg: blkg to get
318672fdcf0SMing Lei *
319672fdcf0SMing Lei * The caller should be holding an existing reference.
320672fdcf0SMing Lei */
blkg_get(struct blkcg_gq * blkg)321672fdcf0SMing Lei static inline void blkg_get(struct blkcg_gq *blkg)
322672fdcf0SMing Lei {
323672fdcf0SMing Lei percpu_ref_get(&blkg->refcnt);
324672fdcf0SMing Lei }
325672fdcf0SMing Lei
326672fdcf0SMing Lei /**
327672fdcf0SMing Lei * blkg_tryget - try and get a blkg reference
328672fdcf0SMing Lei * @blkg: blkg to get
329672fdcf0SMing Lei *
330672fdcf0SMing Lei * This is for use when doing an RCU lookup of the blkg. We may be in the midst
331672fdcf0SMing Lei * of freeing this blkg, so we can only use it if the refcnt is not zero.
332672fdcf0SMing Lei */
blkg_tryget(struct blkcg_gq * blkg)333672fdcf0SMing Lei static inline bool blkg_tryget(struct blkcg_gq *blkg)
334672fdcf0SMing Lei {
335672fdcf0SMing Lei return blkg && percpu_ref_tryget(&blkg->refcnt);
336672fdcf0SMing Lei }
337672fdcf0SMing Lei
338672fdcf0SMing Lei /**
339672fdcf0SMing Lei * blkg_put - put a blkg reference
340672fdcf0SMing Lei * @blkg: blkg to put
341672fdcf0SMing Lei */
blkg_put(struct blkcg_gq * blkg)342672fdcf0SMing Lei static inline void blkg_put(struct blkcg_gq *blkg)
343672fdcf0SMing Lei {
344672fdcf0SMing Lei percpu_ref_put(&blkg->refcnt);
345672fdcf0SMing Lei }
346672fdcf0SMing Lei
347672fdcf0SMing Lei /**
348672fdcf0SMing Lei * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
349672fdcf0SMing Lei * @d_blkg: loop cursor pointing to the current descendant
350672fdcf0SMing Lei * @pos_css: used for iteration
351672fdcf0SMing Lei * @p_blkg: target blkg to walk descendants of
352672fdcf0SMing Lei *
353672fdcf0SMing Lei * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
354672fdcf0SMing Lei * read locked. If called under either blkcg or queue lock, the iteration
355672fdcf0SMing Lei * is guaranteed to include all and only online blkgs. The caller may
356672fdcf0SMing Lei * update @pos_css by calling css_rightmost_descendant() to skip subtree.
357672fdcf0SMing Lei * @p_blkg is included in the iteration and the first node to be visited.
358672fdcf0SMing Lei */
359672fdcf0SMing Lei #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
360672fdcf0SMing Lei css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
36179fcc5beSChristoph Hellwig if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css), \
362a06377c5SChristoph Hellwig (p_blkg)->q)))
363672fdcf0SMing Lei
364672fdcf0SMing Lei /**
365672fdcf0SMing Lei * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
366672fdcf0SMing Lei * @d_blkg: loop cursor pointing to the current descendant
367672fdcf0SMing Lei * @pos_css: used for iteration
368672fdcf0SMing Lei * @p_blkg: target blkg to walk descendants of
369672fdcf0SMing Lei *
370672fdcf0SMing Lei * Similar to blkg_for_each_descendant_pre() but performs post-order
371672fdcf0SMing Lei * traversal instead. Synchronization rules are the same. @p_blkg is
372672fdcf0SMing Lei * included in the iteration and the last node to be visited.
373672fdcf0SMing Lei */
374672fdcf0SMing Lei #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
375672fdcf0SMing Lei css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
37679fcc5beSChristoph Hellwig if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css), \
377a06377c5SChristoph Hellwig (p_blkg)->q)))
378672fdcf0SMing Lei
blkcg_bio_issue_init(struct bio * bio)379672fdcf0SMing Lei static inline void blkcg_bio_issue_init(struct bio *bio)
380672fdcf0SMing Lei {
381672fdcf0SMing Lei bio_issue_init(&bio->bi_issue, bio_sectors(bio));
382672fdcf0SMing Lei }
383672fdcf0SMing Lei
blkcg_use_delay(struct blkcg_gq * blkg)384672fdcf0SMing Lei static inline void blkcg_use_delay(struct blkcg_gq *blkg)
385672fdcf0SMing Lei {
386672fdcf0SMing Lei if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
387672fdcf0SMing Lei return;
388672fdcf0SMing Lei if (atomic_add_return(1, &blkg->use_delay) == 1)
389672fdcf0SMing Lei atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
390672fdcf0SMing Lei }
391672fdcf0SMing Lei
blkcg_unuse_delay(struct blkcg_gq * blkg)392672fdcf0SMing Lei static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
393672fdcf0SMing Lei {
394672fdcf0SMing Lei int old = atomic_read(&blkg->use_delay);
395672fdcf0SMing Lei
396672fdcf0SMing Lei if (WARN_ON_ONCE(old < 0))
397672fdcf0SMing Lei return 0;
398672fdcf0SMing Lei if (old == 0)
399672fdcf0SMing Lei return 0;
400672fdcf0SMing Lei
401672fdcf0SMing Lei /*
402672fdcf0SMing Lei * We do this song and dance because we can race with somebody else
403672fdcf0SMing Lei * adding or removing delay. If we just did an atomic_dec we'd end up
404672fdcf0SMing Lei * negative and we'd already be in trouble. We need to subtract 1 and
405672fdcf0SMing Lei * then check to see if we were the last delay so we can drop the
406672fdcf0SMing Lei * congestion count on the cgroup.
407672fdcf0SMing Lei */
40896388f57SUros Bizjak while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
40996388f57SUros Bizjak ;
410672fdcf0SMing Lei
411672fdcf0SMing Lei if (old == 0)
412672fdcf0SMing Lei return 0;
413672fdcf0SMing Lei if (old == 1)
414672fdcf0SMing Lei atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
415672fdcf0SMing Lei return 1;
416672fdcf0SMing Lei }
417672fdcf0SMing Lei
418672fdcf0SMing Lei /**
419672fdcf0SMing Lei * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
420672fdcf0SMing Lei * @blkg: target blkg
421672fdcf0SMing Lei * @delay: delay duration in nsecs
422672fdcf0SMing Lei *
423672fdcf0SMing Lei * When enabled with this function, the delay is not decayed and must be
424672fdcf0SMing Lei * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
425672fdcf0SMing Lei * blkcg_[un]use_delay() and blkcg_add_delay() usages.
426672fdcf0SMing Lei */
blkcg_set_delay(struct blkcg_gq * blkg,u64 delay)427672fdcf0SMing Lei static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
428672fdcf0SMing Lei {
429672fdcf0SMing Lei int old = atomic_read(&blkg->use_delay);
430672fdcf0SMing Lei
431672fdcf0SMing Lei /* We only want 1 person setting the congestion count for this blkg. */
43296388f57SUros Bizjak if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
433672fdcf0SMing Lei atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
434672fdcf0SMing Lei
435672fdcf0SMing Lei atomic64_set(&blkg->delay_nsec, delay);
436672fdcf0SMing Lei }
437672fdcf0SMing Lei
438672fdcf0SMing Lei /**
439672fdcf0SMing Lei * blkcg_clear_delay - Disable allocator delay mechanism
440672fdcf0SMing Lei * @blkg: target blkg
441672fdcf0SMing Lei *
442672fdcf0SMing Lei * Disable use_delay mechanism. See blkcg_set_delay().
443672fdcf0SMing Lei */
blkcg_clear_delay(struct blkcg_gq * blkg)444672fdcf0SMing Lei static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
445672fdcf0SMing Lei {
446672fdcf0SMing Lei int old = atomic_read(&blkg->use_delay);
447672fdcf0SMing Lei
448672fdcf0SMing Lei /* We only want 1 person clearing the congestion count for this blkg. */
44996388f57SUros Bizjak if (old && atomic_try_cmpxchg(&blkg->use_delay, &old, 0))
450672fdcf0SMing Lei atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
451672fdcf0SMing Lei }
452672fdcf0SMing Lei
4536b2b0459STejun Heo /**
4546b2b0459STejun Heo * blk_cgroup_mergeable - Determine whether to allow or disallow merges
4556b2b0459STejun Heo * @rq: request to merge into
4566b2b0459STejun Heo * @bio: bio to merge
4576b2b0459STejun Heo *
4586b2b0459STejun Heo * @bio and @rq should belong to the same cgroup and their issue_as_root should
4596b2b0459STejun Heo * match. The latter is necessary as we don't want to throttle e.g. a metadata
4606b2b0459STejun Heo * update because it happens to be next to a regular IO.
4616b2b0459STejun Heo */
blk_cgroup_mergeable(struct request * rq,struct bio * bio)4626b2b0459STejun Heo static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio)
4636b2b0459STejun Heo {
4646b2b0459STejun Heo return rq->bio->bi_blkg == bio->bi_blkg &&
4656b2b0459STejun Heo bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio);
4666b2b0459STejun Heo }
4676b2b0459STejun Heo
468672fdcf0SMing Lei void blk_cgroup_bio_start(struct bio *bio);
469672fdcf0SMing Lei void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
470672fdcf0SMing Lei #else /* CONFIG_BLK_CGROUP */
471672fdcf0SMing Lei
472672fdcf0SMing Lei struct blkg_policy_data {
473672fdcf0SMing Lei };
474672fdcf0SMing Lei
475672fdcf0SMing Lei struct blkcg_policy_data {
476672fdcf0SMing Lei };
477672fdcf0SMing Lei
478672fdcf0SMing Lei struct blkcg_policy {
479672fdcf0SMing Lei };
480672fdcf0SMing Lei
481bbb1ebe7SChristoph Hellwig struct blkcg {
482bbb1ebe7SChristoph Hellwig };
483bbb1ebe7SChristoph Hellwig
blkg_lookup(struct blkcg * blkcg,void * key)484672fdcf0SMing Lei static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
blkg_init_queue(struct request_queue * q)485*740ffad9SMing Lei static inline void blkg_init_queue(struct request_queue *q) { }
blkcg_init_disk(struct gendisk * disk)4869823538fSChristoph Hellwig static inline int blkcg_init_disk(struct gendisk *disk) { return 0; }
blkcg_exit_disk(struct gendisk * disk)4879823538fSChristoph Hellwig static inline void blkcg_exit_disk(struct gendisk *disk) { }
blkcg_policy_register(struct blkcg_policy * pol)488672fdcf0SMing Lei static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
blkcg_policy_unregister(struct blkcg_policy * pol)489672fdcf0SMing Lei static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
blkcg_activate_policy(struct gendisk * disk,const struct blkcg_policy * pol)49040e4996eSChristoph Hellwig static inline int blkcg_activate_policy(struct gendisk *disk,
491672fdcf0SMing Lei const struct blkcg_policy *pol) { return 0; }
blkcg_deactivate_policy(struct gendisk * disk,const struct blkcg_policy * pol)49240e4996eSChristoph Hellwig static inline void blkcg_deactivate_policy(struct gendisk *disk,
493672fdcf0SMing Lei const struct blkcg_policy *pol) { }
494672fdcf0SMing Lei
blkg_to_pd(struct blkcg_gq * blkg,struct blkcg_policy * pol)495672fdcf0SMing Lei static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
496672fdcf0SMing Lei struct blkcg_policy *pol) { return NULL; }
pd_to_blkg(struct blkg_policy_data * pd)497672fdcf0SMing Lei static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
blkg_path(struct blkcg_gq * blkg)498672fdcf0SMing Lei static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
blkg_get(struct blkcg_gq * blkg)499672fdcf0SMing Lei static inline void blkg_get(struct blkcg_gq *blkg) { }
blkg_put(struct blkcg_gq * blkg)500672fdcf0SMing Lei static inline void blkg_put(struct blkcg_gq *blkg) { }
blkcg_bio_issue_init(struct bio * bio)501672fdcf0SMing Lei static inline void blkcg_bio_issue_init(struct bio *bio) { }
blk_cgroup_bio_start(struct bio * bio)502672fdcf0SMing Lei static inline void blk_cgroup_bio_start(struct bio *bio) { }
blk_cgroup_mergeable(struct request * rq,struct bio * bio)5036b2b0459STejun Heo static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; }
504672fdcf0SMing Lei
505672fdcf0SMing Lei #define blk_queue_for_each_rl(rl, q) \
506672fdcf0SMing Lei for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
507672fdcf0SMing Lei
508672fdcf0SMing Lei #endif /* CONFIG_BLK_CGROUP */
509672fdcf0SMing Lei
510672fdcf0SMing Lei #endif /* _BLK_CGROUP_PRIVATE_H */
511