Skip to content

Commit dc677f6

Browse files
committed
improve memfile a bit with missing slab and dyn_arr_reserve write out of mem error check
And don't do wrong deadlock on write error.
1 parent c3a6c7e commit dc677f6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

kernel_modules/memfile.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include <linux/kernel.h> /* min */
77
#include <linux/module.h>
88
#include <linux/printk.h> /* printk */
9-
#include <linux/string.h> /* strcpy */
9+
#include <linux/slab.h> /* kvzalloc, kvrealloc, kvfree */
10+
#include <linux/string.h> /* memset */
1011
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
1112
#include <linux/rwsem.h>
1213
#include <uapi/linux/stat.h> /* S_IRUSR */
@@ -43,7 +44,7 @@ int dyn_arr_reserve(dyn_arr_t *a, size_t off, size_t len);
4344
int dyn_arr_reserve(dyn_arr_t *a, size_t off, size_t len)
4445
{
4546
size_t new_used, new_size;
46-
47+
4748
new_used = off + len;
4849
if (new_used > a->_size) {
4950
new_size = new_used * 2;
@@ -99,10 +100,12 @@ static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off
99100
ret = min(len, data.used - (size_t)*off);
100101
if (copy_to_user(buf, data.buf + *off, ret)) {
101102
ret = -EFAULT;
103+
goto out;
102104
} else {
103105
*off += ret;
104106
}
105107
}
108+
out:
106109
up_read(&rwsem);
107110
if (log) pr_info("read ret:=%lld\n", (long long)ret);
108111
return ret;
@@ -114,15 +117,21 @@ static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff
114117

115118
if (log) pr_info("write len=%zu off=%lld\n", len, (long long)*off);
116119
down_write(&rwsem);
117-
dyn_arr_reserve(&data, *off, len);
120+
ret = dyn_arr_reserve(&data, *off, len);
121+
if (ret) {
122+
ret = -ENOSPC;
123+
goto out;
124+
}
118125
if (copy_from_user(data.buf + *off, buf, len)) {
119126
ret = -EFAULT;
127+
goto out;
120128
} else {
121129
ret = len;
122130
*off += ret;
123131
}
124-
up_write(&rwsem);
132+
out:
125133
if (log) pr_info("write ret:=%lld\n", (long long)ret);
134+
up_write(&rwsem);
126135
return ret;
127136
}
128137

@@ -168,7 +177,7 @@ static const struct file_operations fops = {
168177
static int myinit(void)
169178
{
170179
int ret;
171-
180+
172181
ret = dyn_arr_init(&data, 1);
173182
if (ret)
174183
return ret;

0 commit comments

Comments
 (0)