kernfs: Avoid dynamic memory allocation for small read buffers

Same concept as in 5a072ccda87e9d0903312e0d72580697f698d523

Signed-off-by: Yaroslav Furman <yaro330@gmail.com>
Signed-off-by: UtsavBalar1231 <utsavbalar1231@gmail.com>
This commit is contained in:
Yaroslav Furman
2020-08-22 13:27:24 +03:00
committed by UtsavBalar1231
parent 1b002a4ab4
commit efdb96d663

View File

@@ -195,16 +195,22 @@ static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
loff_t *ppos)
{
ssize_t len = min_t(size_t, count, PAGE_SIZE);
char buf_onstack[SZ_2K + 1] __aligned(sizeof(long));
const struct kernfs_ops *ops;
char *buf;
buf = of->prealloc_buf;
if (buf)
mutex_lock(&of->prealloc_mutex);
else
buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
else {
if (len < sizeof(buf_onstack)) {
buf = buf_onstack;
} else {
buf = kmalloc(len + 1, GFP_KERNEL);
if (!buf)
return -ENOMEM;
}
}
/*
* @of->mutex nests outside active ref and is used both to ensure that
@@ -240,7 +246,7 @@ static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
out_free:
if (buf == of->prealloc_buf)
mutex_unlock(&of->prealloc_mutex);
else
else if (buf != buf_onstack)
kfree(buf);
return len;
}