blob: c8fc9792ac776d333f5696662cf2e5404571774f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Vivek Goyal31e4c282009-12-03 12:59:42 -05002#ifndef _BLK_CGROUP_H
3#define _BLK_CGROUP_H
4/*
5 * Common Block IO controller cgroup interface
6 *
7 * Based on ideas and code from CFQ, CFS and BFQ:
8 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9 *
10 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
11 * Paolo Valente <paolo.valente@unimore.it>
12 *
13 * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
14 * Nauman Rafique <nauman@google.com>
15 */
16
17#include <linux/cgroup.h>
Tejun Heof7331642019-11-07 11:18:03 -080018#include <linux/percpu.h>
Tejun Heo24bdb8e2015-08-18 14:55:22 -070019#include <linux/percpu_counter.h>
Tejun Heof7331642019-11-07 11:18:03 -080020#include <linux/u64_stats_sync.h>
Tejun Heo829fdb52012-04-01 14:38:43 -070021#include <linux/seq_file.h>
Tejun Heoa6371202012-04-19 16:29:24 -070022#include <linux/radix-tree.h>
Tejun Heoa0516612012-06-26 15:05:44 -070023#include <linux/blkdev.h>
Tejun Heoa5049a82014-06-19 17:42:57 -040024#include <linux/atomic.h>
Shaohua Li902ec5b2017-09-14 14:02:06 -070025#include <linux/kthread.h>
Dennis Zhou5cdf2e32018-12-05 12:10:31 -050026#include <linux/fs.h>
Vivek Goyal31e4c282009-12-03 12:59:42 -050027
Tejun Heo24bdb8e2015-08-18 14:55:22 -070028/* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
29#define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
30
Vivek Goyal9355aed2010-10-01 21:16:41 +020031/* Max limits for throttle policy */
32#define THROTL_IOPS_MAX UINT_MAX
33
Tejun Heof48ec1d2012-04-13 13:11:25 -070034#ifdef CONFIG_BLK_CGROUP
35
Tejun Heof7331642019-11-07 11:18:03 -080036enum blkg_iostat_type {
37 BLKG_IOSTAT_READ,
38 BLKG_IOSTAT_WRITE,
39 BLKG_IOSTAT_DISCARD,
40
41 BLKG_IOSTAT_NR,
42};
43
Tejun Heoa6371202012-04-19 16:29:24 -070044struct blkcg_gq;
45
Tejun Heo3c798392012-04-16 13:57:25 -070046struct blkcg {
Tejun Heo36558c82012-04-16 13:57:24 -070047 struct cgroup_subsys_state css;
48 spinlock_t lock;
Tejun Heod866dbf2019-07-24 10:37:22 -070049 refcount_t online_pin;
Tejun Heoa6371202012-04-19 16:29:24 -070050
51 struct radix_tree_root blkg_tree;
Bart Van Assche55679c82016-09-23 09:07:56 -070052 struct blkcg_gq __rcu *blkg_hint;
Tejun Heo36558c82012-04-16 13:57:24 -070053 struct hlist_head blkg_list;
Tejun Heo9a9e8a22012-03-19 15:10:56 -070054
Tejun Heo81437642015-08-18 14:55:15 -070055 struct blkcg_policy_data *cpd[BLKCG_MAX_POLS];
Tejun Heo52ebea72015-05-22 17:13:37 -040056
Tejun Heo7876f932015-07-09 16:39:49 -040057 struct list_head all_blkcgs_node;
Tejun Heo52ebea72015-05-22 17:13:37 -040058#ifdef CONFIG_CGROUP_WRITEBACK
59 struct list_head cgwb_list;
60#endif
Vivek Goyal31e4c282009-12-03 12:59:42 -050061};
62
Tejun Heof7331642019-11-07 11:18:03 -080063struct blkg_iostat {
64 u64 bytes[BLKG_IOSTAT_NR];
65 u64 ios[BLKG_IOSTAT_NR];
66};
67
68struct blkg_iostat_set {
69 struct u64_stats_sync sync;
70 struct blkg_iostat cur;
71 struct blkg_iostat last;
72};
73
Tejun Heoe6269c42015-08-18 14:55:21 -070074/*
Tejun Heof95a04a2012-04-16 13:57:26 -070075 * 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 *
Tejun Heo001bea72015-08-18 14:55:11 -070079 * 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.
Tejun Heof95a04a2012-04-16 13:57:26 -070084 */
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};
90
Arianna Avanzinie48453c2015-06-05 23:38:42 +020091/*
Tejun Heoe4a9bde2015-08-18 14:55:16 -070092 * 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.
Arianna Avanzinie48453c2015-06-05 23:38:42 +020097 */
98struct blkcg_policy_data {
Tejun Heo81437642015-08-18 14:55:15 -070099 /* the blkcg and policy id this per-policy data belongs to */
100 struct blkcg *blkcg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200101 int plid;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200102};
103
Tejun Heo3c798392012-04-16 13:57:25 -0700104/* association between a blk cgroup and a request queue */
105struct blkcg_gq {
Tejun Heoc875f4d2012-03-05 13:15:22 -0800106 /* Pointer to the associated request_queue */
Tejun Heo36558c82012-04-16 13:57:24 -0700107 struct request_queue *q;
108 struct list_head q_node;
109 struct hlist_node blkcg_node;
Tejun Heo3c798392012-04-16 13:57:25 -0700110 struct blkcg *blkcg;
Tejun Heo3c547862013-01-09 08:05:10 -0800111
112 /* all non-root blkcg_gq's are guaranteed to have access to parent */
113 struct blkcg_gq *parent;
114
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800115 /* reference count */
Dennis Zhou7fcf2b02018-12-05 12:10:38 -0500116 struct percpu_ref refcnt;
Vivek Goyal22084192009-12-03 12:59:49 -0500117
Tejun Heof427d902013-01-09 08:05:12 -0800118 /* is this blkg online? protected by both blkcg and q locks */
119 bool online;
120
Tejun Heof7331642019-11-07 11:18:03 -0800121 struct blkg_iostat_set __percpu *iostat_cpu;
122 struct blkg_iostat_set iostat;
Tejun Heo77ea7332015-08-18 14:55:24 -0700123
Tejun Heo36558c82012-04-16 13:57:24 -0700124 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800125
Tejun Heod3f77df2019-06-27 13:39:52 -0700126 spinlock_t async_bio_lock;
127 struct bio_list async_bios;
128 struct work_struct async_bio_work;
Josef Bacikd09d8df2018-07-03 11:14:55 -0400129
130 atomic_t use_delay;
131 atomic64_t delay_nsec;
132 atomic64_t delay_start;
133 u64 last_delay;
134 int last_use;
Tejun Heod3f77df2019-06-27 13:39:52 -0700135
136 struct rcu_head rcu_head;
Vivek Goyal31e4c282009-12-03 12:59:42 -0500137};
138
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700139typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
Tejun Heo81437642015-08-18 14:55:15 -0700140typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700141typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
Tejun Heo69d7fde2015-08-18 14:55:36 -0700142typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
Tejun Heocf09a8e2019-08-28 15:05:51 -0700143typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp,
144 struct request_queue *q, struct blkcg *blkcg);
Tejun Heoa9520cd2015-08-18 14:55:14 -0700145typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
146typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
147typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
Tejun Heo001bea72015-08-18 14:55:11 -0700148typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
Tejun Heoa9520cd2015-08-18 14:55:14 -0700149typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
Josef Bacik903d23f2018-07-03 11:14:52 -0400150typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf,
151 size_t size);
Vivek Goyal3e252062009-12-04 10:36:42 -0500152
Tejun Heo3c798392012-04-16 13:57:25 -0700153struct blkcg_policy {
Tejun Heo36558c82012-04-16 13:57:24 -0700154 int plid;
Tejun Heo36558c82012-04-16 13:57:24 -0700155 /* cgroup files for the policy */
Tejun Heo2ee867dc2015-08-18 14:55:34 -0700156 struct cftype *dfl_cftypes;
Tejun Heo880f50e2015-08-18 14:55:30 -0700157 struct cftype *legacy_cftypes;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700158
159 /* operations */
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700160 blkcg_pol_alloc_cpd_fn *cpd_alloc_fn;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200161 blkcg_pol_init_cpd_fn *cpd_init_fn;
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700162 blkcg_pol_free_cpd_fn *cpd_free_fn;
Tejun Heo69d7fde2015-08-18 14:55:36 -0700163 blkcg_pol_bind_cpd_fn *cpd_bind_fn;
Tejun Heoe4a9bde2015-08-18 14:55:16 -0700164
Tejun Heo001bea72015-08-18 14:55:11 -0700165 blkcg_pol_alloc_pd_fn *pd_alloc_fn;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700166 blkcg_pol_init_pd_fn *pd_init_fn;
Tejun Heof427d902013-01-09 08:05:12 -0800167 blkcg_pol_online_pd_fn *pd_online_fn;
168 blkcg_pol_offline_pd_fn *pd_offline_fn;
Tejun Heo001bea72015-08-18 14:55:11 -0700169 blkcg_pol_free_pd_fn *pd_free_fn;
Tejun Heof9fcc2d2012-04-16 13:57:27 -0700170 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
Josef Bacik903d23f2018-07-03 11:14:52 -0400171 blkcg_pol_stat_pd_fn *pd_stat_fn;
Vivek Goyal3e252062009-12-04 10:36:42 -0500172};
173
Tejun Heo3c798392012-04-16 13:57:25 -0700174extern struct blkcg blkcg_root;
Tejun Heo496d5e72015-05-22 17:13:21 -0400175extern struct cgroup_subsys_state * const blkcg_root_css;
Tejun Heo07b0fde2019-07-16 07:58:31 -0700176extern bool blkcg_debug_stats;
Tejun Heo36558c82012-04-16 13:57:24 -0700177
Tejun Heo24f29042015-08-18 14:55:17 -0700178struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
179 struct request_queue *q, bool update_hint);
Tejun Heo36558c82012-04-16 13:57:24 -0700180int blkcg_init_queue(struct request_queue *q);
Tejun Heo36558c82012-04-16 13:57:24 -0700181void blkcg_exit_queue(struct request_queue *q);
Tejun Heo5efd6112012-03-05 13:15:12 -0800182
Vivek Goyal3e252062009-12-04 10:36:42 -0500183/* Blkio controller policy registration */
Jens Axboed5bf0292014-06-22 16:31:56 -0600184int blkcg_policy_register(struct blkcg_policy *pol);
Tejun Heo3c798392012-04-16 13:57:25 -0700185void blkcg_policy_unregister(struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700186int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700187 const struct blkcg_policy *pol);
Tejun Heo36558c82012-04-16 13:57:24 -0700188void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700189 const struct blkcg_policy *pol);
Vivek Goyal3e252062009-12-04 10:36:42 -0500190
Tejun Heodd165eb2015-08-18 14:55:33 -0700191const char *blkg_dev_name(struct blkcg_gq *blkg);
Tejun Heo3c798392012-04-16 13:57:25 -0700192void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
Tejun Heof95a04a2012-04-16 13:57:26 -0700193 u64 (*prfill)(struct seq_file *,
194 struct blkg_policy_data *, int),
Tejun Heo3c798392012-04-16 13:57:25 -0700195 const struct blkcg_policy *pol, int data,
Tejun Heoec399342012-04-13 13:11:27 -0700196 bool show_total);
Tejun Heof95a04a2012-04-16 13:57:26 -0700197u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
Tejun Heo16b3de62013-01-09 08:05:12 -0800198
Tejun Heo829fdb52012-04-01 14:38:43 -0700199struct blkg_conf_ctx {
Tejun Heo36558c82012-04-16 13:57:24 -0700200 struct gendisk *disk;
Tejun Heo3c798392012-04-16 13:57:25 -0700201 struct blkcg_gq *blkg;
Tejun Heo36aa9e52015-08-18 14:55:31 -0700202 char *body;
Tejun Heo829fdb52012-04-01 14:38:43 -0700203};
204
Tejun Heo015d2542019-08-28 15:05:53 -0700205struct gendisk *blkcg_conf_get_disk(char **inputp);
Tejun Heo3c798392012-04-16 13:57:25 -0700206int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
Tejun Heo36aa9e52015-08-18 14:55:31 -0700207 char *input, struct blkg_conf_ctx *ctx);
Tejun Heo829fdb52012-04-01 14:38:43 -0700208void blkg_conf_finish(struct blkg_conf_ctx *ctx);
209
Dennis Zhou0fe061b2018-12-05 12:10:26 -0500210/**
211 * blkcg_css - find the current css
212 *
213 * Find the css associated with either the kthread or the current task.
214 * This may return a dying css, so it is up to the caller to use tryget logic
215 * to confirm it is alive and well.
216 */
217static inline struct cgroup_subsys_state *blkcg_css(void)
218{
219 struct cgroup_subsys_state *css;
220
221 css = kthread_blkcg();
222 if (css)
223 return css;
224 return task_css(current, io_cgrp_id);
225}
226
Tejun Heoa7c6d552013-08-08 20:11:23 -0400227static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
228{
229 return css ? container_of(css, struct blkcg, css) : NULL;
230}
231
Dennis Zhou0fe061b2018-12-05 12:10:26 -0500232/**
233 * __bio_blkcg - internal, inconsistent version to get blkcg
234 *
235 * DO NOT USE.
236 * This function is inconsistent and consequently is dangerous to use. The
237 * first part of the function returns a blkcg where a reference is owned by the
238 * bio. This means it does not need to be rcu protected as it cannot go away
239 * with the bio owning a reference to it. However, the latter potentially gets
240 * it from task_css(). This can race against task migration and the cgroup
241 * dying. It is also semantically different as it must be called rcu protected
242 * and is susceptible to failure when trying to get a reference to it.
243 * Therefore, it is not ok to assume that *_get() will always succeed on the
244 * blkcg returned here.
245 */
246static inline struct blkcg *__bio_blkcg(struct bio *bio)
Dennis Zhou (Facebook)27e6fa92018-09-11 14:41:26 -0400247{
Dennis Zhoudb6638d2018-12-05 12:10:35 -0500248 if (bio && bio->bi_blkg)
249 return bio->bi_blkg->blkcg;
Dennis Zhou0fe061b2018-12-05 12:10:26 -0500250 return css_to_blkcg(blkcg_css());
251}
252
253/**
254 * bio_blkcg - grab the blkcg associated with a bio
255 * @bio: target bio
256 *
257 * This returns the blkcg associated with a bio, %NULL if not associated.
258 * Callers are expected to either handle %NULL or know association has been
259 * done prior to calling this.
260 */
261static inline struct blkcg *bio_blkcg(struct bio *bio)
262{
Dennis Zhoudb6638d2018-12-05 12:10:35 -0500263 if (bio && bio->bi_blkg)
264 return bio->bi_blkg->blkcg;
Dennis Zhou0fe061b2018-12-05 12:10:26 -0500265 return NULL;
Tejun Heofd383c22015-05-22 17:13:23 -0400266}
267
Josef Bacikd09d8df2018-07-03 11:14:55 -0400268static inline bool blk_cgroup_congested(void)
269{
270 struct cgroup_subsys_state *css;
271 bool ret = false;
272
273 rcu_read_lock();
274 css = kthread_blkcg();
275 if (!css)
276 css = task_css(current, io_cgrp_id);
277 while (css) {
278 if (atomic_read(&css->cgroup->congestion_count)) {
279 ret = true;
280 break;
281 }
282 css = css->parent;
283 }
284 rcu_read_unlock();
285 return ret;
286}
287
Tejun Heo03814112012-03-05 13:15:14 -0800288/**
Josef Bacikc7c98fd2018-07-03 11:14:51 -0400289 * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
290 * @return: true if this bio needs to be submitted with the root blkg context.
291 *
292 * In order to avoid priority inversions we sometimes need to issue a bio as if
293 * it were attached to the root blkg, and then backcharge to the actual owning
294 * blkg. The idea is we do bio_blkcg() to look up the actual context for the
295 * bio and attach the appropriate blkg to the bio. Then we call this helper and
296 * if it is true run with the root blkg for that queue and then do any
297 * backcharging to the originating cgroup once the io is complete.
298 */
299static inline bool bio_issue_as_root_blkg(struct bio *bio)
300{
Josef Bacik0d1e0c72018-07-03 11:14:53 -0400301 return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
Josef Bacikc7c98fd2018-07-03 11:14:51 -0400302}
303
304/**
Tejun Heo3c547862013-01-09 08:05:10 -0800305 * blkcg_parent - get the parent of a blkcg
306 * @blkcg: blkcg of interest
307 *
308 * Return the parent blkcg of @blkcg. Can be called anytime.
309 */
310static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
311{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400312 return css_to_blkcg(blkcg->css.parent);
Tejun Heo3c547862013-01-09 08:05:10 -0800313}
314
315/**
Tejun Heo24f29042015-08-18 14:55:17 -0700316 * __blkg_lookup - internal version of blkg_lookup()
317 * @blkcg: blkcg of interest
318 * @q: request_queue of interest
319 * @update_hint: whether to update lookup hint with the result or not
320 *
321 * This is internal version and shouldn't be used by policy
322 * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
323 * @q's bypass state. If @update_hint is %true, the caller should be
324 * holding @q->queue_lock and lookup hint is updated on success.
325 */
326static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
327 struct request_queue *q,
328 bool update_hint)
329{
330 struct blkcg_gq *blkg;
331
Tejun Heo85b6bc92015-08-18 14:55:18 -0700332 if (blkcg == &blkcg_root)
333 return q->root_blkg;
334
Tejun Heo24f29042015-08-18 14:55:17 -0700335 blkg = rcu_dereference(blkcg->blkg_hint);
336 if (blkg && blkg->q == q)
337 return blkg;
338
339 return blkg_lookup_slowpath(blkcg, q, update_hint);
340}
341
342/**
343 * blkg_lookup - lookup blkg for the specified blkcg - q pair
344 * @blkcg: blkcg of interest
345 * @q: request_queue of interest
346 *
347 * Lookup blkg for the @blkcg - @q pair. This function should be called
Bart Van Assche012d4a62019-08-01 15:39:07 -0700348 * under RCU read lock.
Tejun Heo24f29042015-08-18 14:55:17 -0700349 */
350static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
351 struct request_queue *q)
352{
353 WARN_ON_ONCE(!rcu_read_lock_held());
Tejun Heo24f29042015-08-18 14:55:17 -0700354 return __blkg_lookup(blkcg, q, false);
355}
356
357/**
Bart Van Asscheb86d8652018-08-10 13:28:07 -0700358 * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair
Bart Van Assche6bad9b22018-08-09 07:53:36 -0700359 * @q: request_queue of interest
360 *
361 * Lookup blkg for @q at the root level. See also blkg_lookup().
362 */
Bart Van Asscheb86d8652018-08-10 13:28:07 -0700363static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
Bart Van Assche6bad9b22018-08-09 07:53:36 -0700364{
Bart Van Asscheb86d8652018-08-10 13:28:07 -0700365 return q->root_blkg;
Bart Van Assche6bad9b22018-08-09 07:53:36 -0700366}
367
368/**
Tejun Heo03814112012-03-05 13:15:14 -0800369 * blkg_to_pdata - get policy private data
370 * @blkg: blkg of interest
371 * @pol: policy of interest
372 *
373 * Return pointer to private data associated with the @blkg-@pol pair.
374 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700375static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
376 struct blkcg_policy *pol)
Tejun Heo03814112012-03-05 13:15:14 -0800377{
Tejun Heof95a04a2012-04-16 13:57:26 -0700378 return blkg ? blkg->pd[pol->plid] : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800379}
380
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200381static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
382 struct blkcg_policy *pol)
383{
Tejun Heo81437642015-08-18 14:55:15 -0700384 return blkcg ? blkcg->cpd[pol->plid] : NULL;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200385}
386
Tejun Heo03814112012-03-05 13:15:14 -0800387/**
388 * pdata_to_blkg - get blkg associated with policy private data
Tejun Heof95a04a2012-04-16 13:57:26 -0700389 * @pd: policy private data of interest
Tejun Heo03814112012-03-05 13:15:14 -0800390 *
Tejun Heof95a04a2012-04-16 13:57:26 -0700391 * @pd is policy private data. Determine the blkg it's associated with.
Tejun Heo03814112012-03-05 13:15:14 -0800392 */
Tejun Heof95a04a2012-04-16 13:57:26 -0700393static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
Tejun Heo03814112012-03-05 13:15:14 -0800394{
Tejun Heof95a04a2012-04-16 13:57:26 -0700395 return pd ? pd->blkg : NULL;
Tejun Heo03814112012-03-05 13:15:14 -0800396}
397
Tejun Heo81437642015-08-18 14:55:15 -0700398static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
399{
400 return cpd ? cpd->blkcg : NULL;
401}
402
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400403extern void blkcg_destroy_blkgs(struct blkcg *blkcg);
404
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400405/**
Tejun Heod866dbf2019-07-24 10:37:22 -0700406 * blkcg_pin_online - pin online state
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400407 * @blkcg: blkcg of interest
408 *
Tejun Heod866dbf2019-07-24 10:37:22 -0700409 * While pinned, a blkcg is kept online. This is primarily used to
410 * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline
411 * while an associated cgwb is still active.
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400412 */
Tejun Heod866dbf2019-07-24 10:37:22 -0700413static inline void blkcg_pin_online(struct blkcg *blkcg)
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400414{
Tejun Heod866dbf2019-07-24 10:37:22 -0700415 refcount_inc(&blkcg->online_pin);
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400416}
417
418/**
Tejun Heod866dbf2019-07-24 10:37:22 -0700419 * blkcg_unpin_online - unpin online state
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400420 * @blkcg: blkcg of interest
421 *
Tejun Heod866dbf2019-07-24 10:37:22 -0700422 * This is primarily used to impedance-match blkg and cgwb lifetimes so
423 * that blkg doesn't go offline while an associated cgwb is still active.
424 * When this count goes to zero, all active cgwbs have finished so the
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400425 * blkcg can continue destruction by calling blkcg_destroy_blkgs().
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400426 */
Tejun Heod866dbf2019-07-24 10:37:22 -0700427static inline void blkcg_unpin_online(struct blkcg *blkcg)
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400428{
Tejun Heo4308a432019-07-24 10:37:55 -0700429 do {
430 if (!refcount_dec_and_test(&blkcg->online_pin))
431 break;
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400432 blkcg_destroy_blkgs(blkcg);
Tejun Heo4308a432019-07-24 10:37:55 -0700433 blkcg = blkcg_parent(blkcg);
434 } while (blkcg);
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400435}
436
Tejun Heo54e7ed12012-04-16 13:57:23 -0700437/**
438 * blkg_path - format cgroup path of blkg
439 * @blkg: blkg of interest
440 * @buf: target buffer
441 * @buflen: target buffer length
442 *
443 * Format the path of the cgroup of @blkg into @buf.
444 */
Tejun Heo3c798392012-04-16 13:57:25 -0700445static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
Vivek Goyalafc24d42010-04-26 19:27:56 +0200446{
Tejun Heo4c737b42016-08-10 11:23:44 -0400447 return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
Vivek Goyalafc24d42010-04-26 19:27:56 +0200448}
449
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800450/**
451 * blkg_get - get a blkg reference
452 * @blkg: blkg to get
453 *
Tejun Heoa5049a82014-06-19 17:42:57 -0400454 * The caller should be holding an existing reference.
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800455 */
Tejun Heo3c798392012-04-16 13:57:25 -0700456static inline void blkg_get(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800457{
Dennis Zhou7fcf2b02018-12-05 12:10:38 -0500458 percpu_ref_get(&blkg->refcnt);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800459}
460
Josef Bacikd09d8df2018-07-03 11:14:55 -0400461/**
Dennis Zhou7754f662018-12-05 12:10:39 -0500462 * blkg_tryget - try and get a blkg reference
Josef Bacikd09d8df2018-07-03 11:14:55 -0400463 * @blkg: blkg to get
464 *
465 * This is for use when doing an RCU lookup of the blkg. We may be in the midst
466 * of freeing this blkg, so we can only use it if the refcnt is not zero.
467 */
Dennis Zhou7754f662018-12-05 12:10:39 -0500468static inline bool blkg_tryget(struct blkcg_gq *blkg)
Josef Bacikd09d8df2018-07-03 11:14:55 -0400469{
Dennis Zhou6ab21872018-12-19 16:43:21 -0600470 return blkg && percpu_ref_tryget(&blkg->refcnt);
Josef Bacikd09d8df2018-07-03 11:14:55 -0400471}
472
Dennis Zhoubeea9da2018-12-05 12:10:28 -0500473/**
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800474 * blkg_put - put a blkg reference
475 * @blkg: blkg to put
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800476 */
Tejun Heo3c798392012-04-16 13:57:25 -0700477static inline void blkg_put(struct blkcg_gq *blkg)
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800478{
Dennis Zhou7fcf2b02018-12-05 12:10:38 -0500479 percpu_ref_put(&blkg->refcnt);
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800480}
481
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700482/**
483 * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
484 * @d_blkg: loop cursor pointing to the current descendant
Tejun Heo492eb212013-08-08 20:11:25 -0400485 * @pos_css: used for iteration
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700486 * @p_blkg: target blkg to walk descendants of
487 *
488 * Walk @c_blkg through the descendants of @p_blkg. Must be used with RCU
489 * read locked. If called under either blkcg or queue lock, the iteration
490 * is guaranteed to include all and only online blkgs. The caller may
Tejun Heo492eb212013-08-08 20:11:25 -0400491 * update @pos_css by calling css_rightmost_descendant() to skip subtree.
Tejun Heobd8815a2013-08-08 20:11:27 -0400492 * @p_blkg is included in the iteration and the first node to be visited.
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700493 */
Tejun Heo492eb212013-08-08 20:11:25 -0400494#define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
495 css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
496 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
Tejun Heodd4a4ff2013-05-14 13:52:30 -0700497 (p_blkg)->q, false)))
498
Tejun Heoedcb0722012-04-01 14:38:42 -0700499/**
Tejun Heoaa539cb2013-05-14 13:52:31 -0700500 * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
501 * @d_blkg: loop cursor pointing to the current descendant
Tejun Heo492eb212013-08-08 20:11:25 -0400502 * @pos_css: used for iteration
Tejun Heoaa539cb2013-05-14 13:52:31 -0700503 * @p_blkg: target blkg to walk descendants of
504 *
505 * Similar to blkg_for_each_descendant_pre() but performs post-order
Tejun Heobd8815a2013-08-08 20:11:27 -0400506 * traversal instead. Synchronization rules are the same. @p_blkg is
507 * included in the iteration and the last node to be visited.
Tejun Heoaa539cb2013-05-14 13:52:31 -0700508 */
Tejun Heo492eb212013-08-08 20:11:25 -0400509#define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
510 css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
511 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
Tejun Heoaa539cb2013-05-14 13:52:31 -0700512 (p_blkg)->q, false)))
513
Tejun Heod3f77df2019-06-27 13:39:52 -0700514bool __blkcg_punt_bio_submit(struct bio *bio);
515
516static inline bool blkcg_punt_bio_submit(struct bio *bio)
517{
518 if (bio->bi_opf & REQ_CGROUP_PUNT)
519 return __blkcg_punt_bio_submit(bio);
520 else
521 return false;
522}
Dennis Zhoue439bed2018-12-05 12:10:32 -0500523
524static inline void blkcg_bio_issue_init(struct bio *bio)
525{
526 bio_issue_init(&bio->bi_issue, bio_sectors(bio));
527}
528
Josef Bacikd09d8df2018-07-03 11:14:55 -0400529static inline void blkcg_use_delay(struct blkcg_gq *blkg)
530{
Tejun Heo54c52e12020-04-13 12:27:55 -0400531 if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
532 return;
Josef Bacikd09d8df2018-07-03 11:14:55 -0400533 if (atomic_add_return(1, &blkg->use_delay) == 1)
534 atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
535}
536
537static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
538{
539 int old = atomic_read(&blkg->use_delay);
540
Tejun Heo54c52e12020-04-13 12:27:55 -0400541 if (WARN_ON_ONCE(old < 0))
542 return 0;
Josef Bacikd09d8df2018-07-03 11:14:55 -0400543 if (old == 0)
544 return 0;
545
546 /*
547 * We do this song and dance because we can race with somebody else
548 * adding or removing delay. If we just did an atomic_dec we'd end up
549 * negative and we'd already be in trouble. We need to subtract 1 and
550 * then check to see if we were the last delay so we can drop the
551 * congestion count on the cgroup.
552 */
553 while (old) {
554 int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
555 if (cur == old)
556 break;
557 old = cur;
558 }
559
560 if (old == 0)
561 return 0;
562 if (old == 1)
563 atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
564 return 1;
565}
566
Tejun Heo54c52e12020-04-13 12:27:55 -0400567/**
568 * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
569 * @blkg: target blkg
570 * @delay: delay duration in nsecs
571 *
572 * When enabled with this function, the delay is not decayed and must be
573 * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
574 * blkcg_[un]use_delay() and blkcg_add_delay() usages.
575 */
576static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
577{
578 int old = atomic_read(&blkg->use_delay);
579
580 /* We only want 1 person setting the congestion count for this blkg. */
581 if (!old && atomic_cmpxchg(&blkg->use_delay, old, -1) == old)
582 atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
583
584 atomic64_set(&blkg->delay_nsec, delay);
585}
586
587/**
588 * blkcg_clear_delay - Disable allocator delay mechanism
589 * @blkg: target blkg
590 *
591 * Disable use_delay mechanism. See blkcg_set_delay().
592 */
Josef Bacikd09d8df2018-07-03 11:14:55 -0400593static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
594{
595 int old = atomic_read(&blkg->use_delay);
Tejun Heo54c52e12020-04-13 12:27:55 -0400596
Josef Bacikd09d8df2018-07-03 11:14:55 -0400597 /* We only want 1 person clearing the congestion count for this blkg. */
Tejun Heo54c52e12020-04-13 12:27:55 -0400598 if (old && atomic_cmpxchg(&blkg->use_delay, old, 0) == old)
599 atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
Josef Bacikd09d8df2018-07-03 11:14:55 -0400600}
601
Christoph Hellwigdb18a532020-06-27 09:31:58 +0200602void blk_cgroup_bio_start(struct bio *bio);
Josef Bacikd09d8df2018-07-03 11:14:55 -0400603void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
604void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
605void blkcg_maybe_throttle_current(void);
Tejun Heo36558c82012-04-16 13:57:24 -0700606#else /* CONFIG_BLK_CGROUP */
607
Tejun Heoefa7d1c2015-05-22 17:13:18 -0400608struct blkcg {
609};
Jens Axboe2f5ea472009-12-03 21:06:43 +0100610
Tejun Heof95a04a2012-04-16 13:57:26 -0700611struct blkg_policy_data {
612};
613
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200614struct blkcg_policy_data {
615};
616
Tejun Heo3c798392012-04-16 13:57:25 -0700617struct blkcg_gq {
Jens Axboe2f5ea472009-12-03 21:06:43 +0100618};
619
Tejun Heo3c798392012-04-16 13:57:25 -0700620struct blkcg_policy {
Vivek Goyal3e252062009-12-04 10:36:42 -0500621};
622
Tejun Heo496d5e72015-05-22 17:13:21 -0400623#define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
624
Josef Bacikd09d8df2018-07-03 11:14:55 -0400625static inline void blkcg_maybe_throttle_current(void) { }
626static inline bool blk_cgroup_congested(void) { return false; }
627
Tejun Heoefa7d1c2015-05-22 17:13:18 -0400628#ifdef CONFIG_BLOCK
629
Josef Bacikd09d8df2018-07-03 11:14:55 -0400630static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
631
Tejun Heo3c798392012-04-16 13:57:25 -0700632static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
Bart Van Asscheb86d8652018-08-10 13:28:07 -0700633static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
634{ return NULL; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800635static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
Tejun Heo5efd6112012-03-05 13:15:12 -0800636static inline void blkcg_exit_queue(struct request_queue *q) { }
Jens Axboed5bf0292014-06-22 16:31:56 -0600637static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
Tejun Heo3c798392012-04-16 13:57:25 -0700638static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
Tejun Heoa2b16932012-04-13 13:11:33 -0700639static inline int blkcg_activate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700640 const struct blkcg_policy *pol) { return 0; }
Tejun Heoa2b16932012-04-13 13:11:33 -0700641static inline void blkcg_deactivate_policy(struct request_queue *q,
Tejun Heo3c798392012-04-16 13:57:25 -0700642 const struct blkcg_policy *pol) { }
Vivek Goyal3e252062009-12-04 10:36:42 -0500643
Dennis Zhou0fe061b2018-12-05 12:10:26 -0500644static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; }
Tejun Heob1208b52012-06-04 20:40:57 -0700645static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
Tejun Heoa0516612012-06-26 15:05:44 -0700646
Tejun Heof95a04a2012-04-16 13:57:26 -0700647static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
648 struct blkcg_policy *pol) { return NULL; }
649static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
Tejun Heo3c798392012-04-16 13:57:25 -0700650static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
651static inline void blkg_get(struct blkcg_gq *blkg) { }
652static inline void blkg_put(struct blkcg_gq *blkg) { }
Vivek Goyalafc24d42010-04-26 19:27:56 +0200653
Tejun Heod3f77df2019-06-27 13:39:52 -0700654static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; }
Dennis Zhoue439bed2018-12-05 12:10:32 -0500655static inline void blkcg_bio_issue_init(struct bio *bio) { }
Christoph Hellwigdb18a532020-06-27 09:31:58 +0200656static inline void blk_cgroup_bio_start(struct bio *bio) { }
Tejun Heoae118892015-08-18 14:55:20 -0700657
Tejun Heoa0516612012-06-26 15:05:44 -0700658#define blk_queue_for_each_rl(rl, q) \
659 for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
660
Tejun Heoefa7d1c2015-05-22 17:13:18 -0400661#endif /* CONFIG_BLOCK */
Tejun Heo36558c82012-04-16 13:57:24 -0700662#endif /* CONFIG_BLK_CGROUP */
663#endif /* _BLK_CGROUP_H */