Skip to content

Commit 5f46990

Browse files
committed
cgroup: update the meaning of cftype->max_write_len
cftype->max_write_len is used to extend the maximum size of writes. It's interpreted in such a way that the actual maximum size is one less than the specified value. The default size is defined by CGROUP_LOCAL_BUFFER_SIZE. Its interpretation is quite confusing - its value is decremented by 1 and then compared for equality with max size, which means that the actual default size is CGROUP_LOCAL_BUFFER_SIZE - 2, which is 62 chars. There's no point in having a limit that low. Update its definition so that it means the actual string length sans termination and anything below PAGE_SIZE-1 is treated as PAGE_SIZE-1. .max_write_len for "release_agent" is updated to PATH_MAX-1 and cgroup_release_agent_write() is updated so that the redundant strlen() check is removed and it uses strlcpy() instead of strcpy(). .max_write_len initializations in blk-throttle.c and cfq-iosched.c are no longer necessary and removed. The one in cpuset is kept unchanged as it's an approximated value to begin with. This will also make transition to kernfs smoother. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Li Zefan <lizefan@huawei.com>
1 parent de00ffa commit 5f46990

4 files changed

Lines changed: 11 additions & 19 deletions

File tree

‎block/blk-throttle.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,28 +1425,24 @@ static struct cftype throtl_files[] = {
14251425
.private = offsetof(struct throtl_grp, bps[READ]),
14261426
.seq_show = tg_print_conf_u64,
14271427
.write_string = tg_set_conf_u64,
1428-
.max_write_len = 256,
14291428
},
14301429
{
14311430
.name = "throttle.write_bps_device",
14321431
.private = offsetof(struct throtl_grp, bps[WRITE]),
14331432
.seq_show = tg_print_conf_u64,
14341433
.write_string = tg_set_conf_u64,
1435-
.max_write_len = 256,
14361434
},
14371435
{
14381436
.name = "throttle.read_iops_device",
14391437
.private = offsetof(struct throtl_grp, iops[READ]),
14401438
.seq_show = tg_print_conf_uint,
14411439
.write_string = tg_set_conf_uint,
1442-
.max_write_len = 256,
14431440
},
14441441
{
14451442
.name = "throttle.write_iops_device",
14461443
.private = offsetof(struct throtl_grp, iops[WRITE]),
14471444
.seq_show = tg_print_conf_uint,
14481445
.write_string = tg_set_conf_uint,
1449-
.max_write_len = 256,
14501446
},
14511447
{
14521448
.name = "throttle.io_service_bytes",

‎block/cfq-iosched.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,6 @@ static struct cftype cfq_blkcg_files[] = {
18381838
.flags = CFTYPE_ONLY_ON_ROOT,
18391839
.seq_show = cfqg_print_leaf_weight_device,
18401840
.write_string = cfqg_set_leaf_weight_device,
1841-
.max_write_len = 256,
18421841
},
18431842
{
18441843
.name = "weight",
@@ -1853,7 +1852,6 @@ static struct cftype cfq_blkcg_files[] = {
18531852
.flags = CFTYPE_NOT_ON_ROOT,
18541853
.seq_show = cfqg_print_weight_device,
18551854
.write_string = cfqg_set_weight_device,
1856-
.max_write_len = 256,
18571855
},
18581856
{
18591857
.name = "weight",
@@ -1866,7 +1864,6 @@ static struct cftype cfq_blkcg_files[] = {
18661864
.name = "leaf_weight_device",
18671865
.seq_show = cfqg_print_leaf_weight_device,
18681866
.write_string = cfqg_set_leaf_weight_device,
1869-
.max_write_len = 256,
18701867
},
18711868
{
18721869
.name = "leaf_weight",

‎include/linux/cgroup.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,9 @@ struct cftype {
400400
umode_t mode;
401401

402402
/*
403-
* If non-zero, defines the maximum length of string that can
404-
* be passed to write_string; defaults to 64
403+
* The maximum length of string, excluding trailing nul, that can
404+
* be passed to write_string. If < PAGE_SIZE-1, PAGE_SIZE-1 is
405+
* assumed.
405406
*/
406407
size_t max_write_len;
407408

‎kernel/cgroup.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,13 +2213,14 @@ static int cgroup_procs_write(struct cgroup_subsys_state *css,
22132213
static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
22142214
struct cftype *cft, const char *buffer)
22152215
{
2216-
BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
2217-
if (strlen(buffer) >= PATH_MAX)
2218-
return -EINVAL;
2216+
struct cgroupfs_root *root = css->cgroup->root;
2217+
2218+
BUILD_BUG_ON(sizeof(root->release_agent_path) < PATH_MAX);
22192219
if (!cgroup_lock_live_group(css->cgroup))
22202220
return -ENODEV;
22212221
spin_lock(&release_agent_path_lock);
2222-
strcpy(css->cgroup->root->release_agent_path, buffer);
2222+
strlcpy(root->release_agent_path, buffer,
2223+
sizeof(root->release_agent_path));
22232224
spin_unlock(&release_agent_path_lock);
22242225
mutex_unlock(&cgroup_mutex);
22252226
return 0;
@@ -2245,20 +2246,17 @@ static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
22452246
return 0;
22462247
}
22472248

2248-
/* A buffer size big enough for numbers or short strings */
2249-
#define CGROUP_LOCAL_BUFFER_SIZE 64
2250-
22512249
static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
22522250
size_t nbytes, loff_t *ppos)
22532251
{
22542252
struct cfent *cfe = __d_cfe(file->f_dentry);
22552253
struct cftype *cft = __d_cft(file->f_dentry);
22562254
struct cgroup_subsys_state *css = cfe->css;
2257-
size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2255+
size_t max_bytes = max(cft->max_write_len, PAGE_SIZE);
22582256
char *buf;
22592257
int ret;
22602258

2261-
if (nbytes >= max_bytes)
2259+
if (nbytes > max_bytes)
22622260
return -E2BIG;
22632261

22642262
buf = kmalloc(nbytes + 1, GFP_KERNEL);
@@ -3919,7 +3917,7 @@ static struct cftype cgroup_base_files[] = {
39193917
.flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
39203918
.seq_show = cgroup_release_agent_show,
39213919
.write_string = cgroup_release_agent_write,
3922-
.max_write_len = PATH_MAX,
3920+
.max_write_len = PATH_MAX - 1,
39233921
},
39243922
{ } /* terminate */
39253923
};

0 commit comments

Comments
 (0)