mirror of
https://github.com/rd-stuffs/msm-4.14.git
synced 2025-02-20 11:45:48 +08:00
kernfs: Avoid dynamic memory allocation for small write buffers
Most write buffers are rather small and can fit on the stack, eliminating the need to allocate them dynamically. Reserve a 4 KiB stack buffer for this purpose to avoid the overhead of dynamic memory allocation. Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com> Signed-off-by: azrim <mirzaspc@gmail.com>
This commit is contained in:
parent
875f389399
commit
132d8d5380
@ -273,6 +273,7 @@ static ssize_t kernfs_fop_read(struct file *file, char __user *user_buf,
|
||||
static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
|
||||
size_t count, loff_t *ppos)
|
||||
{
|
||||
char buf_onstack[SZ_4K + 1] __aligned(sizeof(long));
|
||||
struct kernfs_open_file *of = kernfs_of(file);
|
||||
const struct kernfs_ops *ops;
|
||||
ssize_t len;
|
||||
@ -287,12 +288,17 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
|
||||
}
|
||||
|
||||
buf = of->prealloc_buf;
|
||||
if (buf)
|
||||
if (buf) {
|
||||
mutex_lock(&of->prealloc_mutex);
|
||||
else
|
||||
buf = kmalloc(len + 1, GFP_KERNEL);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
} else {
|
||||
if (len < ARRAY_SIZE(buf_onstack)) {
|
||||
buf = buf_onstack;
|
||||
} else {
|
||||
buf = kmalloc(len + 1, GFP_KERNEL);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
if (copy_from_user(buf, user_buf, len)) {
|
||||
len = -EFAULT;
|
||||
@ -326,7 +332,7 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
|
||||
out_free:
|
||||
if (buf == of->prealloc_buf)
|
||||
mutex_unlock(&of->prealloc_mutex);
|
||||
else
|
||||
else if (buf != buf_onstack)
|
||||
kfree(buf);
|
||||
return len;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user