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