Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa82126d authored by Sultan Alsawaf's avatar Sultan Alsawaf Committed by Gagan Malvi
Browse files

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: default avatarSultan Alsawaf <sultan@kerneltoast.com>
parent 67ebf95f
Loading
Loading
Loading
Loading
+12 −6
Original line number Original line Diff line number Diff line
@@ -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,
static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
				size_t count, loff_t *ppos)
				size_t count, loff_t *ppos)
{
{
	char buf_onstack[SZ_4K + 1] __aligned(sizeof(long));
	struct kernfs_open_file *of = kernfs_of(file);
	struct kernfs_open_file *of = kernfs_of(file);
	const struct kernfs_ops *ops;
	const struct kernfs_ops *ops;
	ssize_t len;
	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;
	buf = of->prealloc_buf;
	if (buf)
	if (buf) {
		mutex_lock(&of->prealloc_mutex);
		mutex_lock(&of->prealloc_mutex);
	else
	} else {
		if (len < ARRAY_SIZE(buf_onstack)) {
			buf = buf_onstack;
		} else {
			buf = kmalloc(len + 1, GFP_KERNEL);
			buf = kmalloc(len + 1, GFP_KERNEL);
			if (!buf)
			if (!buf)
				return -ENOMEM;
				return -ENOMEM;
		}
	}


	if (copy_from_user(buf, user_buf, len)) {
	if (copy_from_user(buf, user_buf, len)) {
		len = -EFAULT;
		len = -EFAULT;
@@ -326,7 +332,7 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
out_free:
out_free:
	if (buf == of->prealloc_buf)
	if (buf == of->prealloc_buf)
		mutex_unlock(&of->prealloc_mutex);
		mutex_unlock(&of->prealloc_mutex);
	else
	else if (buf != buf_onstack)
		kfree(buf);
		kfree(buf);
	return len;
	return len;
}
}