Skip to content

Commit e48453c

Browse files
ariavaaxboe
authored andcommitted
block, cgroup: implement policy-specific per-blkcg data
The block IO (blkio) controller enables the block layer to provide service guarantees in a hierarchical fashion. Specifically, service guarantees are provided by registered request-accounting policies. As of now, a proportional-share and a throttling policy are available. They are implemented, respectively, by the CFQ I/O scheduler and the blk-throttle subsystem. Unfortunately, as for adding new policies, the current implementation of the block IO controller is only halfway ready to allow new policies to be plugged in. This commit provides a solution to make the block IO controller fully ready to handle new policies. In what follows, we first describe briefly the current state, and then list the changes made by this commit. The throttling policy does not need any per-cgroup information to perform its task. In contrast, the proportional share policy uses, for each cgroup, both the weight assigned by the user to the cgroup, and a set of dynamically- computed weights, one for each device. The first, user-defined weight is stored in the blkcg data structure: the block IO controller allocates a private blkcg data structure for each cgroup in the blkio cgroups hierarchy (regardless of which policy is active). In other words, the block IO controller internally mirrors the blkio cgroups with private blkcg data structures. On the other hand, for each cgroup and device, the corresponding dynamically- computed weight is maintained in the following, different way. For each device, the block IO controller keeps a private blkcg_gq structure for each cgroup in blkio. In other words, block IO also keeps one private mirror copy of the blkio cgroups hierarchy for each device, made of blkcg_gq structures. Each blkcg_gq structure keeps per-policy information in a generic array of dynamically-allocated 'dedicated' data structures, one for each registered policy (so currently the array contains two elements). To be inserted into the generic array, each dedicated data structure embeds a generic blkg_policy_data structure. Consider now the array contained in the blkcg_gq structure corresponding to a given pair of cgroup and device: one of the elements of the array contains the dedicated data structure for the proportional-share policy, and this dedicated data structure contains the dynamically-computed weight for that pair of cgroup and device. The generic strategy adopted for storing per-policy data in blkcg_gq structures is already capable of handling new policies, whereas the one adopted with blkcg structures is not, because per-policy data are hard-coded in the blkcg structures themselves (currently only data related to the proportional- share policy). This commit addresses the above issues through the following changes: . It generalizes blkcg structures so that per-policy data are stored in the same way as in blkcg_gq structures. Specifically, it lets also the blkcg structure store per-policy data in a generic array of dynamically-allocated dedicated data structures. We will refer to these data structures as blkcg dedicated data structures, to distinguish them from the dedicated data structures inserted in the generic arrays kept by blkcg_gq structures. To allow blkcg dedicated data structures to be inserted in the generic array inside a blkcg structure, this commit also introduces a new blkcg_policy_data structure, which is the equivalent of blkg_policy_data for blkcg dedicated data structures. . It adds to the blkcg_policy structure, i.e., to the descriptor of a policy, a cpd_size field and a cpd_init field, to be initialized by the policy with, respectively, the size of the blkcg dedicated data structures, and the address of a constructor function for blkcg dedicated data structures. . It moves the CFQ-specific fields embedded in the blkcg data structure (i.e., the fields related to the proportional-share policy), into a new blkcg dedicated data structure called cfq_group_data. Signed-off-by: Paolo Valente <paolo.valente@unimore.it> Signed-off-by: Arianna Avanzini <avanzini.arianna@gmail.com> Acked-by: Tejun Heo <tj@kernel.org> Cc: Jens Axboe <axboe@fb.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 41c0126 commit e48453c

File tree

3 files changed

+173
-29
lines changed

3 files changed

+173
-29
lines changed

block/blk-cgroup.c

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*
1010
* Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
1111
* Nauman Rafique <nauman@google.com>
12+
*
13+
* For policy-specific per-blkcg data:
14+
* Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
15+
* Arianna Avanzini <avanzini.arianna@gmail.com>
1216
*/
1317
#include <linux/ioprio.h>
1418
#include <linux/kdev_t.h>
@@ -26,8 +30,7 @@
2630

2731
static DEFINE_MUTEX(blkcg_pol_mutex);
2832

29-
struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
30-
.cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
33+
struct blkcg blkcg_root;
3134
EXPORT_SYMBOL_GPL(blkcg_root);
3235

3336
static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
@@ -823,24 +826,58 @@ static struct cgroup_subsys_state *
823826
blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
824827
{
825828
struct blkcg *blkcg;
829+
struct cgroup_subsys_state *ret;
830+
int i;
826831

827832
if (!parent_css) {
828833
blkcg = &blkcg_root;
829834
goto done;
830835
}
831836

832837
blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
833-
if (!blkcg)
834-
return ERR_PTR(-ENOMEM);
838+
if (!blkcg) {
839+
ret = ERR_PTR(-ENOMEM);
840+
goto free_blkcg;
841+
}
842+
843+
for (i = 0; i < BLKCG_MAX_POLS ; i++) {
844+
struct blkcg_policy *pol = blkcg_policy[i];
845+
struct blkcg_policy_data *cpd;
846+
847+
/*
848+
* If the policy hasn't been attached yet, wait for it
849+
* to be attached before doing anything else. Otherwise,
850+
* check if the policy requires any specific per-cgroup
851+
* data: if it does, allocate and initialize it.
852+
*/
853+
if (!pol || !pol->cpd_size)
854+
continue;
855+
856+
BUG_ON(blkcg->pd[i]);
857+
cpd = kzalloc(pol->cpd_size, GFP_KERNEL);
858+
if (!cpd) {
859+
ret = ERR_PTR(-ENOMEM);
860+
goto free_pd_blkcg;
861+
}
862+
blkcg->pd[i] = cpd;
863+
cpd->plid = i;
864+
pol->cpd_init_fn(blkcg);
865+
}
835866

836-
blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
837-
blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
838867
done:
839868
spin_lock_init(&blkcg->lock);
840869
INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
841870
INIT_HLIST_HEAD(&blkcg->blkg_list);
842871

843872
return &blkcg->css;
873+
874+
free_pd_blkcg:
875+
for (i--; i >= 0; i--)
876+
kfree(blkcg->pd[i]);
877+
878+
free_blkcg:
879+
kfree(blkcg);
880+
return ret;
844881
}
845882

846883
/**
@@ -958,8 +995,10 @@ int blkcg_activate_policy(struct request_queue *q,
958995
const struct blkcg_policy *pol)
959996
{
960997
LIST_HEAD(pds);
998+
LIST_HEAD(cpds);
961999
struct blkcg_gq *blkg, *new_blkg;
962-
struct blkg_policy_data *pd, *n;
1000+
struct blkg_policy_data *pd, *nd;
1001+
struct blkcg_policy_data *cpd, *cnd;
9631002
int cnt = 0, ret;
9641003
bool preloaded;
9651004

@@ -1003,34 +1042,61 @@ int blkcg_activate_policy(struct request_queue *q,
10031042

10041043
spin_unlock_irq(q->queue_lock);
10051044

1006-
/* allocate policy_data for all existing blkgs */
1045+
/*
1046+
* Allocate per-blkg and per-blkcg policy data
1047+
* for all existing blkgs.
1048+
*/
10071049
while (cnt--) {
10081050
pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
10091051
if (!pd) {
10101052
ret = -ENOMEM;
10111053
goto out_free;
10121054
}
10131055
list_add_tail(&pd->alloc_node, &pds);
1056+
1057+
if (!pol->cpd_size)
1058+
continue;
1059+
cpd = kzalloc_node(pol->cpd_size, GFP_KERNEL, q->node);
1060+
if (!cpd) {
1061+
ret = -ENOMEM;
1062+
goto out_free;
1063+
}
1064+
list_add_tail(&cpd->alloc_node, &cpds);
10141065
}
10151066

10161067
/*
1017-
* Install the allocated pds. With @q bypassing, no new blkg
1068+
* Install the allocated pds and cpds. With @q bypassing, no new blkg
10181069
* should have been created while the queue lock was dropped.
10191070
*/
10201071
spin_lock_irq(q->queue_lock);
10211072

10221073
list_for_each_entry(blkg, &q->blkg_list, q_node) {
1023-
if (WARN_ON(list_empty(&pds))) {
1074+
if (WARN_ON(list_empty(&pds)) ||
1075+
WARN_ON(pol->cpd_size && list_empty(&cpds))) {
10241076
/* umm... this shouldn't happen, just abort */
10251077
ret = -ENOMEM;
10261078
goto out_unlock;
10271079
}
1080+
cpd = list_first_entry(&cpds, struct blkcg_policy_data,
1081+
alloc_node);
1082+
list_del_init(&cpd->alloc_node);
10281083
pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
10291084
list_del_init(&pd->alloc_node);
10301085

10311086
/* grab blkcg lock too while installing @pd on @blkg */
10321087
spin_lock(&blkg->blkcg->lock);
10331088

1089+
if (!pol->cpd_size)
1090+
goto no_cpd;
1091+
if (!blkg->blkcg->pd[pol->plid]) {
1092+
/* Per-policy per-blkcg data */
1093+
blkg->blkcg->pd[pol->plid] = cpd;
1094+
cpd->plid = pol->plid;
1095+
pol->cpd_init_fn(blkg->blkcg);
1096+
} else { /* must free it as it has already been extracted */
1097+
kfree(cpd);
1098+
}
1099+
no_cpd:
10341100
blkg->pd[pol->plid] = pd;
10351101
pd->blkg = blkg;
10361102
pd->plid = pol->plid;
@@ -1045,8 +1111,10 @@ int blkcg_activate_policy(struct request_queue *q,
10451111
spin_unlock_irq(q->queue_lock);
10461112
out_free:
10471113
blk_queue_bypass_end(q);
1048-
list_for_each_entry_safe(pd, n, &pds, alloc_node)
1114+
list_for_each_entry_safe(pd, nd, &pds, alloc_node)
10491115
kfree(pd);
1116+
list_for_each_entry_safe(cpd, cnd, &cpds, alloc_node)
1117+
kfree(cpd);
10501118
return ret;
10511119
}
10521120
EXPORT_SYMBOL_GPL(blkcg_activate_policy);
@@ -1087,6 +1155,8 @@ void blkcg_deactivate_policy(struct request_queue *q,
10871155

10881156
kfree(blkg->pd[pol->plid]);
10891157
blkg->pd[pol->plid] = NULL;
1158+
kfree(blkg->blkcg->pd[pol->plid]);
1159+
blkg->blkcg->pd[pol->plid] = NULL;
10901160

10911161
spin_unlock(&blkg->blkcg->lock);
10921162
}

block/blk-cgroup.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
/* Max limits for throttle policy */
2424
#define THROTL_IOPS_MAX UINT_MAX
2525

26-
/* CFQ specific, out here for blkcg->cfq_weight */
27-
#define CFQ_WEIGHT_MIN 10
28-
#define CFQ_WEIGHT_MAX 1000
29-
#define CFQ_WEIGHT_DEFAULT 500
30-
3126
#ifdef CONFIG_BLK_CGROUP
3227

3328
enum blkg_rwstat_type {
@@ -50,9 +45,7 @@ struct blkcg {
5045
struct blkcg_gq *blkg_hint;
5146
struct hlist_head blkg_list;
5247

53-
/* TODO: per-policy storage in blkcg */
54-
unsigned int cfq_weight; /* belongs to cfq */
55-
unsigned int cfq_leaf_weight;
48+
struct blkcg_policy_data *pd[BLKCG_MAX_POLS];
5649
};
5750

5851
struct blkg_stat {
@@ -87,6 +80,24 @@ struct blkg_policy_data {
8780
struct list_head alloc_node;
8881
};
8982

83+
/*
84+
* Policies that need to keep per-blkcg data which is independent
85+
* from any request_queue associated to it must specify its size
86+
* with the cpd_size field of the blkcg_policy structure and
87+
* embed a blkcg_policy_data in it. blkcg core allocates
88+
* policy-specific per-blkcg structures lazily the first time
89+
* they are actually needed, so it handles them together with
90+
* blkgs. cpd_init() is invoked to let each policy handle
91+
* per-blkcg data.
92+
*/
93+
struct blkcg_policy_data {
94+
/* the policy id this per-policy data belongs to */
95+
int plid;
96+
97+
/* used during policy activation */
98+
struct list_head alloc_node;
99+
};
100+
90101
/* association between a blk cgroup and a request queue */
91102
struct blkcg_gq {
92103
/* Pointer to the associated request_queue */
@@ -112,6 +123,7 @@ struct blkcg_gq {
112123
struct rcu_head rcu_head;
113124
};
114125

126+
typedef void (blkcg_pol_init_cpd_fn)(const struct blkcg *blkcg);
115127
typedef void (blkcg_pol_init_pd_fn)(struct blkcg_gq *blkg);
116128
typedef void (blkcg_pol_online_pd_fn)(struct blkcg_gq *blkg);
117129
typedef void (blkcg_pol_offline_pd_fn)(struct blkcg_gq *blkg);
@@ -122,10 +134,13 @@ struct blkcg_policy {
122134
int plid;
123135
/* policy specific private data size */
124136
size_t pd_size;
137+
/* policy specific per-blkcg data size */
138+
size_t cpd_size;
125139
/* cgroup files for the policy */
126140
struct cftype *cftypes;
127141

128142
/* operations */
143+
blkcg_pol_init_cpd_fn *cpd_init_fn;
129144
blkcg_pol_init_pd_fn *pd_init_fn;
130145
blkcg_pol_online_pd_fn *pd_online_fn;
131146
blkcg_pol_offline_pd_fn *pd_offline_fn;
@@ -218,6 +233,12 @@ static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
218233
return blkg ? blkg->pd[pol->plid] : NULL;
219234
}
220235

236+
static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
237+
struct blkcg_policy *pol)
238+
{
239+
return blkcg ? blkcg->pd[pol->plid] : NULL;
240+
}
241+
221242
/**
222243
* pdata_to_blkg - get blkg associated with policy private data
223244
* @pd: policy private data of interest
@@ -564,6 +585,9 @@ struct blkcg;
564585
struct blkg_policy_data {
565586
};
566587

588+
struct blkcg_policy_data {
589+
};
590+
567591
struct blkcg_gq {
568592
};
569593

0 commit comments

Comments
 (0)