Commit a742c59d authored by Tejun Heo's avatar Tejun Heo

cgroup: unify cgroup_write_X64() and cgroup_write_string()

cgroup_write_X64() and cgroup_write_string() both implement about the
same buffering logic.  Unify the two into cgroup_file_write() which
always allocates dynamic buffer for simplicity and uses kstrto*()
instead of simple_strto*().

This patch doesn't make any visible behavior changes except for
possibly different error value from kstrsto*().
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarLi Zefan <lizefan@huawei.com>
parent 6e0755b0
...@@ -2249,90 +2249,50 @@ static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css, ...@@ -2249,90 +2249,50 @@ static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
/* A buffer size big enough for numbers or short strings */ /* A buffer size big enough for numbers or short strings */
#define CGROUP_LOCAL_BUFFER_SIZE 64 #define CGROUP_LOCAL_BUFFER_SIZE 64
static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css, static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
struct cftype *cft, struct file *file, size_t nbytes, loff_t *ppos)
const char __user *userbuf, size_t nbytes,
loff_t *unused_ppos)
{
char buffer[CGROUP_LOCAL_BUFFER_SIZE];
int retval = 0;
char *end;
if (!nbytes)
return -EINVAL;
if (nbytes >= sizeof(buffer))
return -E2BIG;
if (copy_from_user(buffer, userbuf, nbytes))
return -EFAULT;
buffer[nbytes] = 0; /* nul-terminate */
if (cft->write_u64) {
u64 val = simple_strtoull(strstrip(buffer), &end, 0);
if (*end)
return -EINVAL;
retval = cft->write_u64(css, cft, val);
} else {
s64 val = simple_strtoll(strstrip(buffer), &end, 0);
if (*end)
return -EINVAL;
retval = cft->write_s64(css, cft, val);
}
if (!retval)
retval = nbytes;
return retval;
}
static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
struct cftype *cft, struct file *file,
const char __user *userbuf, size_t nbytes,
loff_t *unused_ppos)
{ {
char local_buffer[CGROUP_LOCAL_BUFFER_SIZE]; struct cfent *cfe = __d_cfe(file->f_dentry);
int retval = 0; struct cftype *cft = __d_cft(file->f_dentry);
size_t max_bytes = cft->max_write_len; struct cgroup_subsys_state *css = cfe->css;
char *buffer = local_buffer; size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
char *buf;
int ret;
if (!max_bytes)
max_bytes = sizeof(local_buffer) - 1;
if (nbytes >= max_bytes) if (nbytes >= max_bytes)
return -E2BIG; return -E2BIG;
/* Allocate a dynamic buffer if we need one */
if (nbytes >= sizeof(local_buffer)) {
buffer = kmalloc(nbytes + 1, GFP_KERNEL);
if (buffer == NULL)
return -ENOMEM;
}
if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
retval = -EFAULT;
goto out;
}
buffer[nbytes] = 0; /* nul-terminate */ buf = kmalloc(nbytes + 1, GFP_KERNEL);
retval = cft->write_string(css, cft, strstrip(buffer)); if (!buf)
if (!retval) return -ENOMEM;
retval = nbytes;
out:
if (buffer != local_buffer)
kfree(buffer);
return retval;
}
static ssize_t cgroup_file_write(struct file *file, const char __user *buf, if (copy_from_user(buf, userbuf, nbytes)) {
size_t nbytes, loff_t *ppos) ret = -EFAULT;
{ goto out_free;
struct cfent *cfe = __d_cfe(file->f_dentry); }
struct cftype *cft = __d_cft(file->f_dentry);
struct cgroup_subsys_state *css = cfe->css;
if (cft->write_u64 || cft->write_s64) buf[nbytes] = '\0';
return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
if (cft->write_string) if (cft->write_string) {
return cgroup_write_string(css, cft, file, buf, nbytes, ppos); ret = cft->write_string(css, cft, strstrip(buf));
if (cft->trigger) { } else if (cft->write_u64) {
int ret = cft->trigger(css, (unsigned int)cft->private); unsigned long long v;
return ret ? ret : nbytes; ret = kstrtoull(buf, 0, &v);
if (!ret)
ret = cft->write_u64(css, cft, v);
} else if (cft->write_s64) {
long long v;
ret = kstrtoll(buf, 0, &v);
if (!ret)
ret = cft->write_s64(css, cft, v);
} else if (cft->trigger) {
ret = cft->trigger(css, (unsigned int)cft->private);
} else {
ret = -EINVAL;
} }
return -EINVAL; out_free:
kfree(buf);
return ret ?: nbytes;
} }
static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css, static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment