diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 6625061348c0..83c461bb7fc2 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -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; }