1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Legacy blkg rwstat helpers enabled by CONFIG_BLK_CGROUP_RWSTAT.
4*4882a593Smuzhiyun * Do not use in new code.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include "blk-cgroup-rwstat.h"
7*4882a593Smuzhiyun
blkg_rwstat_init(struct blkg_rwstat * rwstat,gfp_t gfp)8*4882a593Smuzhiyun int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp)
9*4882a593Smuzhiyun {
10*4882a593Smuzhiyun int i, ret;
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun for (i = 0; i < BLKG_RWSTAT_NR; i++) {
13*4882a593Smuzhiyun ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp);
14*4882a593Smuzhiyun if (ret) {
15*4882a593Smuzhiyun while (--i >= 0)
16*4882a593Smuzhiyun percpu_counter_destroy(&rwstat->cpu_cnt[i]);
17*4882a593Smuzhiyun return ret;
18*4882a593Smuzhiyun }
19*4882a593Smuzhiyun atomic64_set(&rwstat->aux_cnt[i], 0);
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun return 0;
22*4882a593Smuzhiyun }
23*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_rwstat_init);
24*4882a593Smuzhiyun
blkg_rwstat_exit(struct blkg_rwstat * rwstat)25*4882a593Smuzhiyun void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun int i;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun for (i = 0; i < BLKG_RWSTAT_NR; i++)
30*4882a593Smuzhiyun percpu_counter_destroy(&rwstat->cpu_cnt[i]);
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_rwstat_exit);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /**
35*4882a593Smuzhiyun * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
36*4882a593Smuzhiyun * @sf: seq_file to print to
37*4882a593Smuzhiyun * @pd: policy private data of interest
38*4882a593Smuzhiyun * @rwstat: rwstat to print
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * Print @rwstat to @sf for the device assocaited with @pd.
41*4882a593Smuzhiyun */
__blkg_prfill_rwstat(struct seq_file * sf,struct blkg_policy_data * pd,const struct blkg_rwstat_sample * rwstat)42*4882a593Smuzhiyun u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
43*4882a593Smuzhiyun const struct blkg_rwstat_sample *rwstat)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun static const char *rwstr[] = {
46*4882a593Smuzhiyun [BLKG_RWSTAT_READ] = "Read",
47*4882a593Smuzhiyun [BLKG_RWSTAT_WRITE] = "Write",
48*4882a593Smuzhiyun [BLKG_RWSTAT_SYNC] = "Sync",
49*4882a593Smuzhiyun [BLKG_RWSTAT_ASYNC] = "Async",
50*4882a593Smuzhiyun [BLKG_RWSTAT_DISCARD] = "Discard",
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun const char *dname = blkg_dev_name(pd->blkg);
53*4882a593Smuzhiyun u64 v;
54*4882a593Smuzhiyun int i;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (!dname)
57*4882a593Smuzhiyun return 0;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun for (i = 0; i < BLKG_RWSTAT_NR; i++)
60*4882a593Smuzhiyun seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
61*4882a593Smuzhiyun rwstat->cnt[i]);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun v = rwstat->cnt[BLKG_RWSTAT_READ] +
64*4882a593Smuzhiyun rwstat->cnt[BLKG_RWSTAT_WRITE] +
65*4882a593Smuzhiyun rwstat->cnt[BLKG_RWSTAT_DISCARD];
66*4882a593Smuzhiyun seq_printf(sf, "%s Total %llu\n", dname, v);
67*4882a593Smuzhiyun return v;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun * blkg_prfill_rwstat - prfill callback for blkg_rwstat
73*4882a593Smuzhiyun * @sf: seq_file to print to
74*4882a593Smuzhiyun * @pd: policy private data of interest
75*4882a593Smuzhiyun * @off: offset to the blkg_rwstat in @pd
76*4882a593Smuzhiyun *
77*4882a593Smuzhiyun * prfill callback for printing a blkg_rwstat.
78*4882a593Smuzhiyun */
blkg_prfill_rwstat(struct seq_file * sf,struct blkg_policy_data * pd,int off)79*4882a593Smuzhiyun u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
80*4882a593Smuzhiyun int off)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun struct blkg_rwstat_sample rwstat = { };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun blkg_rwstat_read((void *)pd + off, &rwstat);
85*4882a593Smuzhiyun return __blkg_prfill_rwstat(sf, pd, &rwstat);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
91*4882a593Smuzhiyun * @blkg: blkg of interest
92*4882a593Smuzhiyun * @pol: blkcg_policy which contains the blkg_rwstat
93*4882a593Smuzhiyun * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg
94*4882a593Smuzhiyun * @sum: blkg_rwstat_sample structure containing the results
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its
97*4882a593Smuzhiyun * online descendants and their aux counts. The caller must be holding the
98*4882a593Smuzhiyun * queue lock for online tests.
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it
101*4882a593Smuzhiyun * is at @off bytes into @blkg's blkg_policy_data of the policy.
102*4882a593Smuzhiyun */
blkg_rwstat_recursive_sum(struct blkcg_gq * blkg,struct blkcg_policy * pol,int off,struct blkg_rwstat_sample * sum)103*4882a593Smuzhiyun void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol,
104*4882a593Smuzhiyun int off, struct blkg_rwstat_sample *sum)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun struct blkcg_gq *pos_blkg;
107*4882a593Smuzhiyun struct cgroup_subsys_state *pos_css;
108*4882a593Smuzhiyun unsigned int i;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun lockdep_assert_held(&blkg->q->queue_lock);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun memset(sum, 0, sizeof(*sum));
113*4882a593Smuzhiyun rcu_read_lock();
114*4882a593Smuzhiyun blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
115*4882a593Smuzhiyun struct blkg_rwstat *rwstat;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun if (!pos_blkg->online)
118*4882a593Smuzhiyun continue;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (pol)
121*4882a593Smuzhiyun rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off;
122*4882a593Smuzhiyun else
123*4882a593Smuzhiyun rwstat = (void *)pos_blkg + off;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun for (i = 0; i < BLKG_RWSTAT_NR; i++)
126*4882a593Smuzhiyun sum->cnt[i] += blkg_rwstat_read_counter(rwstat, i);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun rcu_read_unlock();
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
131