Skip to content

Commit 8d7e6fb

Browse files
committed
cgroup: update cgroup name handling
Straightforward updates to cgroup name handling in preparation of kernfs conversion. * cgroup_alloc_name() is updated to take const char * isntead of dentry * for name source. * cgroup name formatting is separated out into cgroup_file_name(). While at it, buffer length protection is added. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Li Zefan <lizefan@huawei.com>
1 parent d427dfe commit 8d7e6fb

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

‎kernel/cgroup.c‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
*/
6868
#define CGROUP_PIDLIST_DESTROY_DELAY HZ
6969

70+
#define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
71+
MAX_CFTYPE_NAME + 2)
72+
7073
/*
7174
* cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
7275
* creation/removal and hierarchy changing operations including cgroup
@@ -799,17 +802,29 @@ static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
799802
return inode;
800803
}
801804

802-
static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
805+
static struct cgroup_name *cgroup_alloc_name(const char *name_str)
803806
{
804807
struct cgroup_name *name;
805808

806-
name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
809+
name = kmalloc(sizeof(*name) + strlen(name_str) + 1, GFP_KERNEL);
807810
if (!name)
808811
return NULL;
809-
strcpy(name->name, dentry->d_name.name);
812+
strcpy(name->name, name_str);
810813
return name;
811814
}
812815

816+
static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft,
817+
char *buf)
818+
{
819+
if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
820+
!(cgrp->root->flags & CGRP_ROOT_NOPREFIX))
821+
snprintf(buf, CGROUP_FILE_NAME_MAX, "%s.%s",
822+
cft->ss->name, cft->name);
823+
else
824+
strncpy(buf, cft->name, CGROUP_FILE_NAME_MAX);
825+
return buf;
826+
}
827+
813828
static void cgroup_free_fn(struct work_struct *work)
814829
{
815830
struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
@@ -2437,7 +2452,7 @@ static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
24372452
if (cgroup_sane_behavior(cgrp))
24382453
return -EPERM;
24392454

2440-
name = cgroup_alloc_name(new_dentry);
2455+
name = cgroup_alloc_name(new_dentry->d_name.name);
24412456
if (!name)
24422457
return -ENOMEM;
24432458

@@ -2613,21 +2628,15 @@ static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
26132628
struct cfent *cfe;
26142629
int error;
26152630
umode_t mode;
2616-
char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2617-
2618-
if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2619-
!(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2620-
strcpy(name, cft->ss->name);
2621-
strcat(name, ".");
2622-
}
2623-
strcat(name, cft->name);
2631+
char name[CGROUP_FILE_NAME_MAX];
26242632

26252633
BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
26262634

26272635
cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
26282636
if (!cfe)
26292637
return -ENOMEM;
26302638

2639+
cgroup_file_name(cgrp, cft, name);
26312640
dentry = lookup_one_len(name, dir, strlen(name));
26322641
if (IS_ERR(dentry)) {
26332642
error = PTR_ERR(dentry);
@@ -4135,7 +4144,7 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
41354144
if (!cgrp)
41364145
return -ENOMEM;
41374146

4138-
name = cgroup_alloc_name(dentry);
4147+
name = cgroup_alloc_name(dentry->d_name.name);
41394148
if (!name) {
41404149
err = -ENOMEM;
41414150
goto err_free_cgrp;

0 commit comments

Comments
 (0)