1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BLK_CGROUP_PRIVATE_H
3 #define _BLK_CGROUP_PRIVATE_H
5 * block cgroup private header
7 * Based on ideas and code from CFQ, CFS and BFQ:
8 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
10 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
11 * Paolo Valente <paolo.valente@unimore.it>
13 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
14 * Nauman Rafique <nauman@google.com>
17 #include <linux/blk-cgroup.h>
18 #include <linux/blk-mq.h>
20 /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
21 #define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
23 #ifdef CONFIG_BLK_CGROUP
26 * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
27 * request_queue (q). This is used by blkcg policies which need to track
28 * information per blkcg - q pair.
30 * There can be multiple active blkcg policies and each blkg:policy pair is
31 * represented by a blkg_policy_data which is allocated and freed by each
32 * policy's pd_alloc/free_fn() methods. A policy can allocate private data
33 * area by allocating larger data structure which embeds blkg_policy_data
36 struct blkg_policy_data {
37 /* the blkg and policy id this per-policy data belongs to */
38 struct blkcg_gq *blkg;
43 * Policies that need to keep per-blkcg data which is independent from any
44 * request_queue associated to it should implement cpd_alloc/free_fn()
45 * methods. A policy can allocate private data area by allocating larger
46 * data structure which embeds blkcg_policy_data at the beginning.
47 * cpd_init() is invoked to let each policy handle per-blkcg data.
49 struct blkcg_policy_data {
50 /* the blkcg and policy id this per-policy data belongs to */
55 typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
56 typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
57 typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
58 typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
59 typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp,
60 struct request_queue *q, struct blkcg *blkcg);
61 typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
62 typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
63 typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
64 typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
65 typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
66 typedef bool (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd,
71 /* cgroup files for the policy */
72 struct cftype *dfl_cftypes;
73 struct cftype *legacy_cftypes;
76 blkcg_pol_alloc_cpd_fn *cpd_alloc_fn;
77 blkcg_pol_init_cpd_fn *cpd_init_fn;
78 blkcg_pol_free_cpd_fn *cpd_free_fn;
79 blkcg_pol_bind_cpd_fn *cpd_bind_fn;
81 blkcg_pol_alloc_pd_fn *pd_alloc_fn;
82 blkcg_pol_init_pd_fn *pd_init_fn;
83 blkcg_pol_online_pd_fn *pd_online_fn;
84 blkcg_pol_offline_pd_fn *pd_offline_fn;
85 blkcg_pol_free_pd_fn *pd_free_fn;
86 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
87 blkcg_pol_stat_pd_fn *pd_stat_fn;
90 extern struct blkcg blkcg_root;
91 extern bool blkcg_debug_stats;
93 struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
94 struct request_queue *q, bool update_hint);
95 int blkcg_init_queue(struct request_queue *q);
96 void blkcg_exit_queue(struct request_queue *q);
98 /* Blkio controller policy registration */
99 int blkcg_policy_register(struct blkcg_policy *pol);
100 void blkcg_policy_unregister(struct blkcg_policy *pol);
101 int blkcg_activate_policy(struct request_queue *q,
102 const struct blkcg_policy *pol);
103 void blkcg_deactivate_policy(struct request_queue *q,
104 const struct blkcg_policy *pol);
106 const char *blkg_dev_name(struct blkcg_gq *blkg);
107 void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
108 u64 (*prfill)(struct seq_file *,
109 struct blkg_policy_data *, int),
110 const struct blkcg_policy *pol, int data,
112 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
114 struct blkg_conf_ctx {
115 struct block_device *bdev;
116 struct blkcg_gq *blkg;
120 struct block_device *blkcg_conf_open_bdev(char **inputp);
121 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
122 char *input, struct blkg_conf_ctx *ctx);
123 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
126 * blkcg_css - find the current css
128 * Find the css associated with either the kthread or the current task.
129 * This may return a dying css, so it is up to the caller to use tryget logic
130 * to confirm it is alive and well.
132 static inline struct cgroup_subsys_state *blkcg_css(void)
134 struct cgroup_subsys_state *css;
136 css = kthread_blkcg();
139 return task_css(current, io_cgrp_id);
143 * __bio_blkcg - internal, inconsistent version to get blkcg
146 * This function is inconsistent and consequently is dangerous to use. The
147 * first part of the function returns a blkcg where a reference is owned by the
148 * bio. This means it does not need to be rcu protected as it cannot go away
149 * with the bio owning a reference to it. However, the latter potentially gets
150 * it from task_css(). This can race against task migration and the cgroup
151 * dying. It is also semantically different as it must be called rcu protected
152 * and is susceptible to failure when trying to get a reference to it.
153 * Therefore, it is not ok to assume that *_get() will always succeed on the
154 * blkcg returned here.
156 static inline struct blkcg *__bio_blkcg(struct bio *bio)
158 if (bio && bio->bi_blkg)
159 return bio->bi_blkg->blkcg;
160 return css_to_blkcg(blkcg_css());
164 * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
165 * @return: true if this bio needs to be submitted with the root blkg context.
167 * In order to avoid priority inversions we sometimes need to issue a bio as if
168 * it were attached to the root blkg, and then backcharge to the actual owning
169 * blkg. The idea is we do bio_blkcg() to look up the actual context for the
170 * bio and attach the appropriate blkg to the bio. Then we call this helper and
171 * if it is true run with the root blkg for that queue and then do any
172 * backcharging to the originating cgroup once the io is complete.
174 static inline bool bio_issue_as_root_blkg(struct bio *bio)
176 return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
180 * __blkg_lookup - internal version of blkg_lookup()
181 * @blkcg: blkcg of interest
182 * @q: request_queue of interest
183 * @update_hint: whether to update lookup hint with the result or not
185 * This is internal version and shouldn't be used by policy
186 * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
187 * @q's bypass state. If @update_hint is %true, the caller should be
188 * holding @q->queue_lock and lookup hint is updated on success.
190 static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
191 struct request_queue *q,
194 struct blkcg_gq *blkg;
196 if (blkcg == &blkcg_root)
199 blkg = rcu_dereference(blkcg->blkg_hint);
200 if (blkg && blkg->q == q)
203 return blkg_lookup_slowpath(blkcg, q, update_hint);
207 * blkg_lookup - lookup blkg for the specified blkcg - q pair
208 * @blkcg: blkcg of interest
209 * @q: request_queue of interest
211 * Lookup blkg for the @blkcg - @q pair. This function should be called
212 * under RCU read lock.
214 static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
215 struct request_queue *q)
217 WARN_ON_ONCE(!rcu_read_lock_held());
218 return __blkg_lookup(blkcg, q, false);
222 * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair
223 * @q: request_queue of interest
225 * Lookup blkg for @q at the root level. See also blkg_lookup().
227 static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
233 * blkg_to_pdata - get policy private data
234 * @blkg: blkg of interest
235 * @pol: policy of interest
237 * Return pointer to private data associated with the @blkg-@pol pair.
239 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
240 struct blkcg_policy *pol)
242 return blkg ? blkg->pd[pol->plid] : NULL;
245 static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
246 struct blkcg_policy *pol)
248 return blkcg ? blkcg->cpd[pol->plid] : NULL;
252 * pdata_to_blkg - get blkg associated with policy private data
253 * @pd: policy private data of interest
255 * @pd is policy private data. Determine the blkg it's associated with.
257 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
259 return pd ? pd->blkg : NULL;
262 static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
264 return cpd ? cpd->blkcg : NULL;
268 * blkg_path - format cgroup path of blkg
269 * @blkg: blkg of interest
270 * @buf: target buffer
271 * @buflen: target buffer length
273 * Format the path of the cgroup of @blkg into @buf.
275 static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
277 return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
281 * blkg_get - get a blkg reference
284 * The caller should be holding an existing reference.
286 static inline void blkg_get(struct blkcg_gq *blkg)
288 percpu_ref_get(&blkg->refcnt);
292 * blkg_tryget - try and get a blkg reference
295 * This is for use when doing an RCU lookup of the blkg. We may be in the midst
296 * of freeing this blkg, so we can only use it if the refcnt is not zero.
298 static inline bool blkg_tryget(struct blkcg_gq *blkg)
300 return blkg && percpu_ref_tryget(&blkg->refcnt);
304 * blkg_put - put a blkg reference
307 static inline void blkg_put(struct blkcg_gq *blkg)
309 percpu_ref_put(&blkg->refcnt);
313 * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
314 * @d_blkg: loop cursor pointing to the current descendant
315 * @pos_css: used for iteration
316 * @p_blkg: target blkg to walk descendants of
318 * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
319 * read locked. If called under either blkcg or queue lock, the iteration
320 * is guaranteed to include all and only online blkgs. The caller may
321 * update @pos_css by calling css_rightmost_descendant() to skip subtree.
322 * @p_blkg is included in the iteration and the first node to be visited.
324 #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
325 css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
326 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
327 (p_blkg)->q, false)))
330 * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
331 * @d_blkg: loop cursor pointing to the current descendant
332 * @pos_css: used for iteration
333 * @p_blkg: target blkg to walk descendants of
335 * Similar to blkg_for_each_descendant_pre() but performs post-order
336 * traversal instead. Synchronization rules are the same. @p_blkg is
337 * included in the iteration and the last node to be visited.
339 #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
340 css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
341 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
342 (p_blkg)->q, false)))
344 bool __blkcg_punt_bio_submit(struct bio *bio);
346 static inline bool blkcg_punt_bio_submit(struct bio *bio)
348 if (bio->bi_opf & REQ_CGROUP_PUNT)
349 return __blkcg_punt_bio_submit(bio);
354 static inline void blkcg_bio_issue_init(struct bio *bio)
356 bio_issue_init(&bio->bi_issue, bio_sectors(bio));
359 static inline void blkcg_use_delay(struct blkcg_gq *blkg)
361 if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
363 if (atomic_add_return(1, &blkg->use_delay) == 1)
364 atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
367 static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
369 int old = atomic_read(&blkg->use_delay);
371 if (WARN_ON_ONCE(old < 0))
377 * We do this song and dance because we can race with somebody else
378 * adding or removing delay. If we just did an atomic_dec we'd end up
379 * negative and we'd already be in trouble. We need to subtract 1 and
380 * then check to see if we were the last delay so we can drop the
381 * congestion count on the cgroup.
384 int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
393 atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
398 * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
400 * @delay: delay duration in nsecs
402 * When enabled with this function, the delay is not decayed and must be
403 * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
404 * blkcg_[un]use_delay() and blkcg_add_delay() usages.
406 static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
408 int old = atomic_read(&blkg->use_delay);
410 /* We only want 1 person setting the congestion count for this blkg. */
411 if (!old && atomic_cmpxchg(&blkg->use_delay, old, -1) == old)
412 atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
414 atomic64_set(&blkg->delay_nsec, delay);
418 * blkcg_clear_delay - Disable allocator delay mechanism
421 * Disable use_delay mechanism. See blkcg_set_delay().
423 static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
425 int old = atomic_read(&blkg->use_delay);
427 /* We only want 1 person clearing the congestion count for this blkg. */
428 if (old && atomic_cmpxchg(&blkg->use_delay, old, 0) == old)
429 atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
433 * blk_cgroup_mergeable - Determine whether to allow or disallow merges
434 * @rq: request to merge into
437 * @bio and @rq should belong to the same cgroup and their issue_as_root should
438 * match. The latter is necessary as we don't want to throttle e.g. a metadata
439 * update because it happens to be next to a regular IO.
441 static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio)
443 return rq->bio->bi_blkg == bio->bi_blkg &&
444 bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio);
447 void blk_cgroup_bio_start(struct bio *bio);
448 void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
449 #else /* CONFIG_BLK_CGROUP */
451 struct blkg_policy_data {
454 struct blkcg_policy_data {
457 struct blkcg_policy {
462 static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
463 static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
465 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
466 static inline void blkcg_exit_queue(struct request_queue *q) { }
467 static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
468 static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
469 static inline int blkcg_activate_policy(struct request_queue *q,
470 const struct blkcg_policy *pol) { return 0; }
471 static inline void blkcg_deactivate_policy(struct request_queue *q,
472 const struct blkcg_policy *pol) { }
474 static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; }
476 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
477 struct blkcg_policy *pol) { return NULL; }
478 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
479 static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
480 static inline void blkg_get(struct blkcg_gq *blkg) { }
481 static inline void blkg_put(struct blkcg_gq *blkg) { }
483 static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; }
484 static inline void blkcg_bio_issue_init(struct bio *bio) { }
485 static inline void blk_cgroup_bio_start(struct bio *bio) { }
486 static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; }
488 #define blk_queue_for_each_rl(rl, q) \
489 for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
491 #endif /* CONFIG_BLOCK */
492 #endif /* CONFIG_BLK_CGROUP */
494 #endif /* _BLK_CGROUP_PRIVATE_H */