xref: /linux/block/blk-cgroup.h (revision 4def096314cf29fb6940b74c087a0b923185580e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BLK_CGROUP_PRIVATE_H
3 #define _BLK_CGROUP_PRIVATE_H
4 /*
5  * block cgroup private header
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/blk-cgroup.h>
18 #include <linux/cgroup.h>
19 #include <linux/kthread.h>
20 #include <linux/blk-mq.h>
21 #include <linux/llist.h>
22 #include "blk.h"
23 
24 struct blkcg_gq;
25 struct blkg_policy_data;
26 
27 
28 /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
29 #define BLKG_STAT_CPU_BATCH	(INT_MAX / 2)
30 
31 #ifdef CONFIG_BLK_CGROUP
32 
33 enum blkg_iostat_type {
34 	BLKG_IOSTAT_READ,
35 	BLKG_IOSTAT_WRITE,
36 	BLKG_IOSTAT_DISCARD,
37 
38 	BLKG_IOSTAT_NR,
39 };
40 
41 struct blkg_iostat {
42 	u64				bytes[BLKG_IOSTAT_NR];
43 	u64				ios[BLKG_IOSTAT_NR];
44 };
45 
46 struct blkg_iostat_set {
47 	struct u64_stats_sync		sync;
48 	struct blkcg_gq		       *blkg;
49 	struct llist_node		lnode;
50 	int				lqueued;	/* queued in llist */
51 	struct blkg_iostat		cur;
52 	struct blkg_iostat		last;
53 };
54 
55 /* association between a blk cgroup and a request queue */
56 struct blkcg_gq {
57 	/* Pointer to the associated request_queue */
58 	struct request_queue		*q;
59 	struct list_head		q_node;
60 	struct hlist_node		blkcg_node;
61 	struct blkcg			*blkcg;
62 
63 	/* all non-root blkcg_gq's are guaranteed to have access to parent */
64 	struct blkcg_gq			*parent;
65 
66 	/* reference count */
67 	struct percpu_ref		refcnt;
68 
69 	/* is this blkg online? protected by both blkcg and q locks */
70 	bool				online;
71 
72 	struct blkg_iostat_set __percpu	*iostat_cpu;
73 	struct blkg_iostat_set		iostat;
74 
75 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
76 #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
77 	spinlock_t			async_bio_lock;
78 	struct bio_list			async_bios;
79 	struct work_struct		async_bio_work;
80 #endif
81 	struct work_struct		free_work;
82 
83 	atomic_t			use_delay;
84 	atomic64_t			delay_nsec;
85 	atomic64_t			delay_start;
86 	u64				last_delay;
87 	int				last_use;
88 
89 	struct rcu_head			rcu_head;
90 };
91 
92 struct blkcg {
93 	struct cgroup_subsys_state	css;
94 	spinlock_t			lock;
95 	refcount_t			online_pin;
96 	/* If there is block congestion on this cgroup. */
97 	atomic_t			congestion_count;
98 
99 	struct radix_tree_root		blkg_tree;
100 	struct blkcg_gq	__rcu		*blkg_hint;
101 	struct hlist_head		blkg_list;
102 
103 	struct blkcg_policy_data	*cpd[BLKCG_MAX_POLS];
104 
105 	struct list_head		all_blkcgs_node;
106 
107 	/*
108 	 * List of updated percpu blkg_iostat_set's since the last flush.
109 	 */
110 	struct llist_head __percpu	*lhead;
111 
112 #ifdef CONFIG_BLK_CGROUP_FC_APPID
113 	char                            fc_app_id[FC_APPID_LEN];
114 #endif
115 #ifdef CONFIG_CGROUP_WRITEBACK
116 	struct list_head		cgwb_list;
117 #endif
118 };
119 
120 static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
121 {
122 	return css ? container_of(css, struct blkcg, css) : NULL;
123 }
124 
125 /*
126  * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
127  * request_queue (q).  This is used by blkcg policies which need to track
128  * information per blkcg - q pair.
129  *
130  * There can be multiple active blkcg policies and each blkg:policy pair is
131  * represented by a blkg_policy_data which is allocated and freed by each
132  * policy's pd_alloc/free_fn() methods.  A policy can allocate private data
133  * area by allocating larger data structure which embeds blkg_policy_data
134  * at the beginning.
135  */
136 struct blkg_policy_data {
137 	/* the blkg and policy id this per-policy data belongs to */
138 	struct blkcg_gq			*blkg;
139 	int				plid;
140 	bool				online;
141 
142 	struct rcu_head			rcu_head;
143 };
144 
145 /*
146  * Policies that need to keep per-blkcg data which is independent from any
147  * request_queue associated to it should implement cpd_alloc/free_fn()
148  * methods.  A policy can allocate private data area by allocating larger
149  * data structure which embeds blkcg_policy_data at the beginning.
150  * cpd_init() is invoked to let each policy handle per-blkcg data.
151  */
152 struct blkcg_policy_data {
153 	/* the blkcg and policy id this per-policy data belongs to */
154 	struct blkcg			*blkcg;
155 	int				plid;
156 };
157 
158 typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
159 typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
160 typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
161 typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
162 typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(struct gendisk *disk,
163 		struct blkcg *blkcg, gfp_t gfp);
164 typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
165 typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
166 typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
167 typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
168 typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
169 typedef void (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd,
170 				struct seq_file *s);
171 
172 struct blkcg_policy {
173 	int				plid;
174 	/* cgroup files for the policy */
175 	struct cftype			*dfl_cftypes;
176 	struct cftype			*legacy_cftypes;
177 
178 	/* operations */
179 	blkcg_pol_alloc_cpd_fn		*cpd_alloc_fn;
180 	blkcg_pol_free_cpd_fn		*cpd_free_fn;
181 
182 	blkcg_pol_alloc_pd_fn		*pd_alloc_fn;
183 	blkcg_pol_init_pd_fn		*pd_init_fn;
184 	blkcg_pol_online_pd_fn		*pd_online_fn;
185 	blkcg_pol_offline_pd_fn		*pd_offline_fn;
186 	blkcg_pol_free_pd_fn		*pd_free_fn;
187 	blkcg_pol_reset_pd_stats_fn	*pd_reset_stats_fn;
188 	blkcg_pol_stat_pd_fn		*pd_stat_fn;
189 };
190 
191 extern struct blkcg blkcg_root;
192 extern bool blkcg_debug_stats;
193 
194 void blkg_init_queue(struct request_queue *q);
195 int blkcg_init_disk(struct gendisk *disk);
196 void blkcg_exit_disk(struct gendisk *disk);
197 
198 /* Blkio controller policy registration */
199 int blkcg_policy_register(struct blkcg_policy *pol);
200 void blkcg_policy_unregister(struct blkcg_policy *pol);
201 int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol);
202 void blkcg_deactivate_policy(struct gendisk *disk,
203 			     const struct blkcg_policy *pol);
204 
205 const char *blkg_dev_name(struct blkcg_gq *blkg);
206 void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
207 		       u64 (*prfill)(struct seq_file *,
208 				     struct blkg_policy_data *, int),
209 		       const struct blkcg_policy *pol, int data,
210 		       bool show_total);
211 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
212 
213 struct blkg_conf_ctx {
214 	char				*input;
215 	char				*body;
216 	struct block_device		*bdev;
217 	struct blkcg_gq			*blkg;
218 };
219 
220 void blkg_conf_init(struct blkg_conf_ctx *ctx, char *input);
221 int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx)
222 	__cond_acquires(0, &ctx->bdev->bd_queue->rq_qos_mutex);
223 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
224 		   struct blkg_conf_ctx *ctx)
225 	__cond_acquires(0, &ctx->bdev->bd_disk->queue->queue_lock);
226 void blkg_conf_unprep(struct blkg_conf_ctx *ctx)
227 	__releases(ctx->bdev->bd_disk->queue->queue_lock);
228 void blkg_conf_close_bdev(struct blkg_conf_ctx *ctx)
229 	__releases(&ctx->bdev->bd_queue->rq_qos_mutex);
230 
231 /**
232  * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
233  * @bio: the target &bio
234  *
235  * Return: true if this bio needs to be submitted with the root blkg context.
236  *
237  * In order to avoid priority inversions we sometimes need to issue a bio as if
238  * it were attached to the root blkg, and then backcharge to the actual owning
239  * blkg.  The idea is we do bio_blkcg_css() to look up the actual context for
240  * the bio and attach the appropriate blkg to the bio.  Then we call this helper
241  * and if it is true run with the root blkg for that queue and then do any
242  * backcharging to the originating cgroup once the io is complete.
243  */
244 static inline bool bio_issue_as_root_blkg(struct bio *bio)
245 {
246 	return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
247 }
248 
249 /**
250  * blkg_lookup - lookup blkg for the specified blkcg - q pair
251  * @blkcg: blkcg of interest
252  * @q: request_queue of interest
253  *
254  * Lookup blkg for the @blkcg - @q pair.
255  *
256  * Must be called in a RCU critical section.
257  */
258 static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
259 					   struct request_queue *q)
260 {
261 	struct blkcg_gq *blkg;
262 
263 	if (blkcg == &blkcg_root)
264 		return q->root_blkg;
265 
266 	blkg = rcu_dereference_check(blkcg->blkg_hint,
267 			lockdep_is_held(&q->queue_lock));
268 	if (blkg && blkg->q == q)
269 		return blkg;
270 
271 	blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
272 	if (blkg && blkg->q != q)
273 		blkg = NULL;
274 	return blkg;
275 }
276 
277 /**
278  * blkg_to_pd - get policy private data
279  * @blkg: blkg of interest
280  * @pol: policy of interest
281  *
282  * Return pointer to private data associated with the @blkg-@pol pair.
283  */
284 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
285 						  const struct blkcg_policy *pol)
286 {
287 	return blkg ? READ_ONCE(blkg->pd[pol->plid]) : NULL;
288 }
289 
290 static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
291 						     struct blkcg_policy *pol)
292 {
293 	return blkcg ? blkcg->cpd[pol->plid] : NULL;
294 }
295 
296 /**
297  * pd_to_blkg - get blkg associated with policy private data
298  * @pd: policy private data of interest
299  *
300  * @pd is policy private data.  Determine the blkg it's associated with.
301  */
302 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
303 {
304 	return pd ? pd->blkg : NULL;
305 }
306 
307 static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
308 {
309 	return cpd ? cpd->blkcg : NULL;
310 }
311 
312 /**
313  * blkg_get - get a blkg reference
314  * @blkg: blkg to get
315  *
316  * The caller should be holding an existing reference.
317  */
318 static inline void blkg_get(struct blkcg_gq *blkg)
319 {
320 	percpu_ref_get(&blkg->refcnt);
321 }
322 
323 /**
324  * blkg_tryget - try and get a blkg reference
325  * @blkg: blkg to get
326  *
327  * This is for use when doing an RCU lookup of the blkg.  We may be in the midst
328  * of freeing this blkg, so we can only use it if the refcnt is not zero.
329  */
330 static inline bool blkg_tryget(struct blkcg_gq *blkg)
331 {
332 	return blkg && percpu_ref_tryget(&blkg->refcnt);
333 }
334 
335 /**
336  * blkg_put - put a blkg reference
337  * @blkg: blkg to put
338  */
339 static inline void blkg_put(struct blkcg_gq *blkg)
340 {
341 	percpu_ref_put(&blkg->refcnt);
342 }
343 
344 /**
345  * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
346  * @d_blkg: loop cursor pointing to the current descendant
347  * @pos_css: used for iteration
348  * @p_blkg: target blkg to walk descendants of
349  *
350  * Walk @c_blkg through the descendants of @p_blkg.  Must be used with RCU
351  * read locked.  If called under either blkcg or queue lock, the iteration
352  * is guaranteed to include all and only online blkgs.  The caller may
353  * update @pos_css by calling css_rightmost_descendant() to skip subtree.
354  * @p_blkg is included in the iteration and the first node to be visited.
355  */
356 #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg)		\
357 	css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css)	\
358 		if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css),	\
359 					    (p_blkg)->q)))
360 
361 /**
362  * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
363  * @d_blkg: loop cursor pointing to the current descendant
364  * @pos_css: used for iteration
365  * @p_blkg: target blkg to walk descendants of
366  *
367  * Similar to blkg_for_each_descendant_pre() but performs post-order
368  * traversal instead.  Synchronization rules are the same.  @p_blkg is
369  * included in the iteration and the last node to be visited.
370  */
371 #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg)		\
372 	css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css)	\
373 		if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css),	\
374 					    (p_blkg)->q)))
375 
376 /*
377  * blkcg_nr_congested gates the hierarchy walk in blk_cgroup_congested().
378  * These two helpers keep it in step with each blkcg's congestion_count in
379  * normal operation; blkcg_css_free() drops a residual count as a backstop.
380  */
381 static inline void blkcg_inc_congestion_count(struct blkcg *blkcg)
382 {
383 	if (atomic_inc_return(&blkcg->congestion_count) == 1)
384 		atomic_inc(&blkcg_nr_congested);
385 }
386 
387 static inline void blkcg_dec_congestion_count(struct blkcg *blkcg)
388 {
389 	if (atomic_dec_and_test(&blkcg->congestion_count))
390 		atomic_dec(&blkcg_nr_congested);
391 }
392 
393 static inline void blkcg_use_delay(struct blkcg_gq *blkg)
394 {
395 	if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
396 		return;
397 	if (atomic_add_return(1, &blkg->use_delay) == 1)
398 		blkcg_inc_congestion_count(blkg->blkcg);
399 }
400 
401 static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
402 {
403 	int old = atomic_read(&blkg->use_delay);
404 
405 	if (WARN_ON_ONCE(old < 0))
406 		return 0;
407 	if (old == 0)
408 		return 0;
409 
410 	/*
411 	 * We do this song and dance because we can race with somebody else
412 	 * adding or removing delay.  If we just did an atomic_dec we'd end up
413 	 * negative and we'd already be in trouble.  We need to subtract 1 and
414 	 * then check to see if we were the last delay so we can drop the
415 	 * congestion count on the cgroup.
416 	 */
417 	while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
418 		;
419 
420 	if (old == 0)
421 		return 0;
422 	if (old == 1)
423 		blkcg_dec_congestion_count(blkg->blkcg);
424 	return 1;
425 }
426 
427 /**
428  * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
429  * @blkg: target blkg
430  * @delay: delay duration in nsecs
431  *
432  * When enabled with this function, the delay is not decayed and must be
433  * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
434  * blkcg_[un]use_delay() and blkcg_add_delay() usages.
435  */
436 static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
437 {
438 	int old = atomic_read(&blkg->use_delay);
439 
440 	/* We only want 1 person setting the congestion count for this blkg. */
441 	if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
442 		blkcg_inc_congestion_count(blkg->blkcg);
443 
444 	atomic64_set(&blkg->delay_nsec, delay);
445 }
446 
447 /**
448  * blkcg_clear_delay - Disable allocator delay mechanism
449  * @blkg: target blkg
450  *
451  * Disable use_delay mechanism. See blkcg_set_delay().
452  */
453 static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
454 {
455 	int old = atomic_read(&blkg->use_delay);
456 
457 	/* We only want 1 person clearing the congestion count for this blkg. */
458 	if (old && atomic_try_cmpxchg(&blkg->use_delay, &old, 0))
459 		blkcg_dec_congestion_count(blkg->blkcg);
460 }
461 
462 /**
463  * blk_cgroup_mergeable - Determine whether to allow or disallow merges
464  * @rq: request to merge into
465  * @bio: bio to merge
466  *
467  * @bio and @rq should belong to the same cgroup and their issue_as_root should
468  * match. The latter is necessary as we don't want to throttle e.g. a metadata
469  * update because it happens to be next to a regular IO.
470  */
471 static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio)
472 {
473 	return rq->bio->bi_blkg == bio->bi_blkg &&
474 		bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio);
475 }
476 
477 static inline bool blkcg_policy_enabled(struct request_queue *q,
478 				const struct blkcg_policy *pol)
479 {
480 	return pol && test_bit(pol->plid, q->blkcg_pols);
481 }
482 
483 void blk_cgroup_bio_start(struct bio *bio);
484 void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
485 #else	/* CONFIG_BLK_CGROUP */
486 
487 struct blkg_policy_data {
488 };
489 
490 struct blkcg_policy_data {
491 };
492 
493 struct blkcg_policy {
494 };
495 
496 struct blkcg {
497 };
498 
499 static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
500 static inline void blkg_init_queue(struct request_queue *q) { }
501 static inline int blkcg_init_disk(struct gendisk *disk) { return 0; }
502 static inline void blkcg_exit_disk(struct gendisk *disk) { }
503 static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
504 static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
505 static inline int blkcg_activate_policy(struct gendisk *disk,
506 					const struct blkcg_policy *pol) { return 0; }
507 static inline void blkcg_deactivate_policy(struct gendisk *disk,
508 					   const struct blkcg_policy *pol) { }
509 
510 static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
511 						  const struct blkcg_policy *pol) { return NULL; }
512 static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
513 static inline void blkg_get(struct blkcg_gq *blkg) { }
514 static inline void blkg_put(struct blkcg_gq *blkg) { }
515 static inline void blk_cgroup_bio_start(struct bio *bio) { }
516 static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; }
517 
518 #define blk_queue_for_each_rl(rl, q)	\
519 	for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
520 
521 #endif	/* CONFIG_BLK_CGROUP */
522 
523 #endif /* _BLK_CGROUP_PRIVATE_H */
524