1
2#ifndef _BLK_CGROUP_H
3#define _BLK_CGROUP_H
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/cgroup.h>
18#include <linux/percpu.h>
19#include <linux/percpu_counter.h>
20#include <linux/u64_stats_sync.h>
21#include <linux/seq_file.h>
22#include <linux/radix-tree.h>
23#include <linux/blkdev.h>
24#include <linux/atomic.h>
25#include <linux/kthread.h>
26#include <linux/fs.h>
27
28
29#define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
30
31
32#define THROTL_IOPS_MAX UINT_MAX
33
34#ifdef CONFIG_BLK_CGROUP
35
36enum blkg_iostat_type {
37 BLKG_IOSTAT_READ,
38 BLKG_IOSTAT_WRITE,
39 BLKG_IOSTAT_DISCARD,
40
41 BLKG_IOSTAT_NR,
42};
43
44struct blkcg_gq;
45
46struct blkcg {
47 struct cgroup_subsys_state css;
48 spinlock_t lock;
49 refcount_t online_pin;
50
51 struct radix_tree_root blkg_tree;
52 struct blkcg_gq __rcu *blkg_hint;
53 struct hlist_head blkg_list;
54
55 struct blkcg_policy_data *cpd[BLKCG_MAX_POLS];
56
57 struct list_head all_blkcgs_node;
58#ifdef CONFIG_CGROUP_WRITEBACK
59 struct list_head cgwb_list;
60#endif
61};
62
63struct 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
74
75
76
77
78
79
80
81
82
83
84
85struct blkg_policy_data {
86
87 struct blkcg_gq *blkg;
88 int plid;
89};
90
91
92
93
94
95
96
97
98struct blkcg_policy_data {
99
100 struct blkcg *blkcg;
101 int plid;
102};
103
104
105struct blkcg_gq {
106
107 struct request_queue *q;
108 struct list_head q_node;
109 struct hlist_node blkcg_node;
110 struct blkcg *blkcg;
111
112
113
114
115
116 struct bdi_writeback_congested *wb_congested;
117
118
119 struct blkcg_gq *parent;
120
121
122 struct percpu_ref refcnt;
123
124
125 bool online;
126
127 struct blkg_iostat_set __percpu *iostat_cpu;
128 struct blkg_iostat_set iostat;
129
130 struct blkg_policy_data *pd[BLKCG_MAX_POLS];
131
132 spinlock_t async_bio_lock;
133 struct bio_list async_bios;
134 struct work_struct async_bio_work;
135
136 atomic_t use_delay;
137 atomic64_t delay_nsec;
138 atomic64_t delay_start;
139 u64 last_delay;
140 int last_use;
141
142 struct rcu_head rcu_head;
143};
144
145typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
146typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
147typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
148typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
149typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp,
150 struct request_queue *q, struct blkcg *blkcg);
151typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
152typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
153typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
154typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
155typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
156typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf,
157 size_t size);
158
159struct blkcg_policy {
160 int plid;
161
162 struct cftype *dfl_cftypes;
163 struct cftype *legacy_cftypes;
164
165
166 blkcg_pol_alloc_cpd_fn *cpd_alloc_fn;
167 blkcg_pol_init_cpd_fn *cpd_init_fn;
168 blkcg_pol_free_cpd_fn *cpd_free_fn;
169 blkcg_pol_bind_cpd_fn *cpd_bind_fn;
170
171 blkcg_pol_alloc_pd_fn *pd_alloc_fn;
172 blkcg_pol_init_pd_fn *pd_init_fn;
173 blkcg_pol_online_pd_fn *pd_online_fn;
174 blkcg_pol_offline_pd_fn *pd_offline_fn;
175 blkcg_pol_free_pd_fn *pd_free_fn;
176 blkcg_pol_reset_pd_stats_fn *pd_reset_stats_fn;
177 blkcg_pol_stat_pd_fn *pd_stat_fn;
178};
179
180extern struct blkcg blkcg_root;
181extern struct cgroup_subsys_state * const blkcg_root_css;
182extern bool blkcg_debug_stats;
183
184struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
185 struct request_queue *q, bool update_hint);
186struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
187 struct request_queue *q);
188struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
189 struct request_queue *q);
190int blkcg_init_queue(struct request_queue *q);
191void blkcg_exit_queue(struct request_queue *q);
192
193
194int blkcg_policy_register(struct blkcg_policy *pol);
195void blkcg_policy_unregister(struct blkcg_policy *pol);
196int blkcg_activate_policy(struct request_queue *q,
197 const struct blkcg_policy *pol);
198void blkcg_deactivate_policy(struct request_queue *q,
199 const struct blkcg_policy *pol);
200
201const char *blkg_dev_name(struct blkcg_gq *blkg);
202void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
203 u64 (*prfill)(struct seq_file *,
204 struct blkg_policy_data *, int),
205 const struct blkcg_policy *pol, int data,
206 bool show_total);
207u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
208
209struct blkg_conf_ctx {
210 struct gendisk *disk;
211 struct blkcg_gq *blkg;
212 char *body;
213};
214
215struct gendisk *blkcg_conf_get_disk(char **inputp);
216int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
217 char *input, struct blkg_conf_ctx *ctx);
218void blkg_conf_finish(struct blkg_conf_ctx *ctx);
219
220
221
222
223
224
225
226
227static inline struct cgroup_subsys_state *blkcg_css(void)
228{
229 struct cgroup_subsys_state *css;
230
231 css = kthread_blkcg();
232 if (css)
233 return css;
234 return task_css(current, io_cgrp_id);
235}
236
237static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
238{
239 return css ? container_of(css, struct blkcg, css) : NULL;
240}
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256static inline struct blkcg *__bio_blkcg(struct bio *bio)
257{
258 if (bio && bio->bi_blkg)
259 return bio->bi_blkg->blkcg;
260 return css_to_blkcg(blkcg_css());
261}
262
263
264
265
266
267
268
269
270
271static inline struct blkcg *bio_blkcg(struct bio *bio)
272{
273 if (bio && bio->bi_blkg)
274 return bio->bi_blkg->blkcg;
275 return NULL;
276}
277
278static inline bool blk_cgroup_congested(void)
279{
280 struct cgroup_subsys_state *css;
281 bool ret = false;
282
283 rcu_read_lock();
284 css = kthread_blkcg();
285 if (!css)
286 css = task_css(current, io_cgrp_id);
287 while (css) {
288 if (atomic_read(&css->cgroup->congestion_count)) {
289 ret = true;
290 break;
291 }
292 css = css->parent;
293 }
294 rcu_read_unlock();
295 return ret;
296}
297
298
299
300
301
302
303
304
305
306
307
308
309static inline bool bio_issue_as_root_blkg(struct bio *bio)
310{
311 return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
312}
313
314
315
316
317
318
319
320static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
321{
322 return css_to_blkcg(blkcg->css.parent);
323}
324
325
326
327
328
329
330
331
332
333
334
335
336static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
337 struct request_queue *q,
338 bool update_hint)
339{
340 struct blkcg_gq *blkg;
341
342 if (blkcg == &blkcg_root)
343 return q->root_blkg;
344
345 blkg = rcu_dereference(blkcg->blkg_hint);
346 if (blkg && blkg->q == q)
347 return blkg;
348
349 return blkg_lookup_slowpath(blkcg, q, update_hint);
350}
351
352
353
354
355
356
357
358
359
360static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
361 struct request_queue *q)
362{
363 WARN_ON_ONCE(!rcu_read_lock_held());
364 return __blkg_lookup(blkcg, q, false);
365}
366
367
368
369
370
371
372
373static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
374{
375 return q->root_blkg;
376}
377
378
379
380
381
382
383
384
385static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
386 struct blkcg_policy *pol)
387{
388 return blkg ? blkg->pd[pol->plid] : NULL;
389}
390
391static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
392 struct blkcg_policy *pol)
393{
394 return blkcg ? blkcg->cpd[pol->plid] : NULL;
395}
396
397
398
399
400
401
402
403static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
404{
405 return pd ? pd->blkg : NULL;
406}
407
408static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
409{
410 return cpd ? cpd->blkcg : NULL;
411}
412
413extern void blkcg_destroy_blkgs(struct blkcg *blkcg);
414
415
416
417
418
419
420
421
422
423static inline void blkcg_pin_online(struct blkcg *blkcg)
424{
425 refcount_inc(&blkcg->online_pin);
426}
427
428
429
430
431
432
433
434
435
436
437static inline void blkcg_unpin_online(struct blkcg *blkcg)
438{
439 do {
440 if (!refcount_dec_and_test(&blkcg->online_pin))
441 break;
442 blkcg_destroy_blkgs(blkcg);
443 blkcg = blkcg_parent(blkcg);
444 } while (blkcg);
445}
446
447
448
449
450
451
452
453
454
455static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
456{
457 return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
458}
459
460
461
462
463
464
465
466static inline void blkg_get(struct blkcg_gq *blkg)
467{
468 percpu_ref_get(&blkg->refcnt);
469}
470
471
472
473
474
475
476
477
478static inline bool blkg_tryget(struct blkcg_gq *blkg)
479{
480 return blkg && percpu_ref_tryget(&blkg->refcnt);
481}
482
483
484
485
486
487
488
489
490
491
492static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg)
493{
494 struct blkcg_gq *ret_blkg = NULL;
495
496 WARN_ON_ONCE(!rcu_read_lock_held());
497
498 while (blkg) {
499 if (blkg_tryget(blkg)) {
500 ret_blkg = blkg;
501 break;
502 }
503 blkg = blkg->parent;
504 }
505
506 return ret_blkg;
507}
508
509
510
511
512
513static inline void blkg_put(struct blkcg_gq *blkg)
514{
515 percpu_ref_put(&blkg->refcnt);
516}
517
518
519
520
521
522
523
524
525
526
527
528
529
530#define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg) \
531 css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css) \
532 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
533 (p_blkg)->q, false)))
534
535
536
537
538
539
540
541
542
543
544
545#define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg) \
546 css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css) \
547 if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css), \
548 (p_blkg)->q, false)))
549
550#ifdef CONFIG_BLK_DEV_THROTTLING
551extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
552 struct bio *bio);
553#else
554static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
555 struct bio *bio) { return false; }
556#endif
557
558bool __blkcg_punt_bio_submit(struct bio *bio);
559
560static inline bool blkcg_punt_bio_submit(struct bio *bio)
561{
562 if (bio->bi_opf & REQ_CGROUP_PUNT)
563 return __blkcg_punt_bio_submit(bio);
564 else
565 return false;
566}
567
568static inline void blkcg_bio_issue_init(struct bio *bio)
569{
570 bio_issue_init(&bio->bi_issue, bio_sectors(bio));
571}
572
573static inline bool blkcg_bio_issue_check(struct request_queue *q,
574 struct bio *bio)
575{
576 struct blkcg_gq *blkg;
577 bool throtl = false;
578
579 rcu_read_lock();
580
581 if (!bio->bi_blkg) {
582 char b[BDEVNAME_SIZE];
583
584 WARN_ONCE(1,
585 "no blkg associated for bio on block-device: %s\n",
586 bio_devname(bio, b));
587 bio_associate_blkg(bio);
588 }
589
590 blkg = bio->bi_blkg;
591
592 throtl = blk_throtl_bio(q, blkg, bio);
593
594 if (!throtl) {
595 struct blkg_iostat_set *bis;
596 int rwd, cpu;
597
598 if (op_is_discard(bio->bi_opf))
599 rwd = BLKG_IOSTAT_DISCARD;
600 else if (op_is_write(bio->bi_opf))
601 rwd = BLKG_IOSTAT_WRITE;
602 else
603 rwd = BLKG_IOSTAT_READ;
604
605 cpu = get_cpu();
606 bis = per_cpu_ptr(blkg->iostat_cpu, cpu);
607 u64_stats_update_begin(&bis->sync);
608
609
610
611
612
613
614 if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
615 bio_set_flag(bio, BIO_CGROUP_ACCT);
616 bis->cur.bytes[rwd] += bio->bi_iter.bi_size;
617 }
618 bis->cur.ios[rwd]++;
619
620 u64_stats_update_end(&bis->sync);
621 if (cgroup_subsys_on_dfl(io_cgrp_subsys))
622 cgroup_rstat_updated(blkg->blkcg->css.cgroup, cpu);
623 put_cpu();
624 }
625
626 blkcg_bio_issue_init(bio);
627
628 rcu_read_unlock();
629 return !throtl;
630}
631
632static inline void blkcg_use_delay(struct blkcg_gq *blkg)
633{
634 if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
635 return;
636 if (atomic_add_return(1, &blkg->use_delay) == 1)
637 atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
638}
639
640static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
641{
642 int old = atomic_read(&blkg->use_delay);
643
644 if (WARN_ON_ONCE(old < 0))
645 return 0;
646 if (old == 0)
647 return 0;
648
649
650
651
652
653
654
655
656 while (old) {
657 int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
658 if (cur == old)
659 break;
660 old = cur;
661 }
662
663 if (old == 0)
664 return 0;
665 if (old == 1)
666 atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
667 return 1;
668}
669
670
671
672
673
674
675
676
677
678
679static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
680{
681 int old = atomic_read(&blkg->use_delay);
682
683
684 if (!old && atomic_cmpxchg(&blkg->use_delay, old, -1) == old)
685 atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
686
687 atomic64_set(&blkg->delay_nsec, delay);
688}
689
690
691
692
693
694
695
696static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
697{
698 int old = atomic_read(&blkg->use_delay);
699
700
701 if (old && atomic_cmpxchg(&blkg->use_delay, old, 0) == old)
702 atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
703}
704
705void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
706void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
707void blkcg_maybe_throttle_current(void);
708#else
709
710struct blkcg {
711};
712
713struct blkg_policy_data {
714};
715
716struct blkcg_policy_data {
717};
718
719struct blkcg_gq {
720};
721
722struct blkcg_policy {
723};
724
725#define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
726
727static inline void blkcg_maybe_throttle_current(void) { }
728static inline bool blk_cgroup_congested(void) { return false; }
729
730#ifdef CONFIG_BLOCK
731
732static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
733
734static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
735static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
736{ return NULL; }
737static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
738static inline void blkcg_exit_queue(struct request_queue *q) { }
739static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
740static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
741static inline int blkcg_activate_policy(struct request_queue *q,
742 const struct blkcg_policy *pol) { return 0; }
743static inline void blkcg_deactivate_policy(struct request_queue *q,
744 const struct blkcg_policy *pol) { }
745
746static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; }
747static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
748
749static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
750 struct blkcg_policy *pol) { return NULL; }
751static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
752static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
753static inline void blkg_get(struct blkcg_gq *blkg) { }
754static inline void blkg_put(struct blkcg_gq *blkg) { }
755
756static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; }
757static inline void blkcg_bio_issue_init(struct bio *bio) { }
758static inline bool blkcg_bio_issue_check(struct request_queue *q,
759 struct bio *bio) { return true; }
760
761#define blk_queue_for_each_rl(rl, q) \
762 for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
763
764#endif
765#endif
766#endif
767