diff options
Diffstat (limited to 'mm/util.c')
-rw-r--r-- | mm/util.c | 47 |
1 files changed, 42 insertions, 5 deletions
@@ -1,20 +1,22 @@ | |||
1 | #include <linux/slab.h> | 1 | #include <linux/slab.h> |
2 | #include <linux/string.h> | 2 | #include <linux/string.h> |
3 | #include <linux/module.h> | 3 | #include <linux/module.h> |
4 | #include <linux/err.h> | ||
5 | #include <asm/uaccess.h> | ||
4 | 6 | ||
5 | /** | 7 | /** |
6 | * kzalloc - allocate memory. The memory is set to zero. | 8 | * __kzalloc - allocate memory. The memory is set to zero. |
7 | * @size: how many bytes of memory are required. | 9 | * @size: how many bytes of memory are required. |
8 | * @flags: the type of memory to allocate. | 10 | * @flags: the type of memory to allocate. |
9 | */ | 11 | */ |
10 | void *kzalloc(size_t size, gfp_t flags) | 12 | void *__kzalloc(size_t size, gfp_t flags) |
11 | { | 13 | { |
12 | void *ret = kmalloc(size, flags); | 14 | void *ret = ____kmalloc(size, flags); |
13 | if (ret) | 15 | if (ret) |
14 | memset(ret, 0, size); | 16 | memset(ret, 0, size); |
15 | return ret; | 17 | return ret; |
16 | } | 18 | } |
17 | EXPORT_SYMBOL(kzalloc); | 19 | EXPORT_SYMBOL(__kzalloc); |
18 | 20 | ||
19 | /* | 21 | /* |
20 | * kstrdup - allocate space for and copy an existing string | 22 | * kstrdup - allocate space for and copy an existing string |
@@ -31,9 +33,44 @@ char *kstrdup(const char *s, gfp_t gfp) | |||
31 | return NULL; | 33 | return NULL; |
32 | 34 | ||
33 | len = strlen(s) + 1; | 35 | len = strlen(s) + 1; |
34 | buf = kmalloc(len, gfp); | 36 | buf = ____kmalloc(len, gfp); |
35 | if (buf) | 37 | if (buf) |
36 | memcpy(buf, s, len); | 38 | memcpy(buf, s, len); |
37 | return buf; | 39 | return buf; |
38 | } | 40 | } |
39 | EXPORT_SYMBOL(kstrdup); | 41 | EXPORT_SYMBOL(kstrdup); |
42 | |||
43 | /* | ||
44 | * strndup_user - duplicate an existing string from user space | ||
45 | * | ||
46 | * @s: The string to duplicate | ||
47 | * @n: Maximum number of bytes to copy, including the trailing NUL. | ||
48 | */ | ||
49 | char *strndup_user(const char __user *s, long n) | ||
50 | { | ||
51 | char *p; | ||
52 | long length; | ||
53 | |||
54 | length = strnlen_user(s, n); | ||
55 | |||
56 | if (!length) | ||
57 | return ERR_PTR(-EFAULT); | ||
58 | |||
59 | if (length > n) | ||
60 | return ERR_PTR(-EINVAL); | ||
61 | |||
62 | p = kmalloc(length, GFP_KERNEL); | ||
63 | |||
64 | if (!p) | ||
65 | return ERR_PTR(-ENOMEM); | ||
66 | |||
67 | if (copy_from_user(p, s, length)) { | ||
68 | kfree(p); | ||
69 | return ERR_PTR(-EFAULT); | ||
70 | } | ||
71 | |||
72 | p[length - 1] = '\0'; | ||
73 | |||
74 | return p; | ||
75 | } | ||
76 | EXPORT_SYMBOL(strndup_user); | ||