blob: 7c122e49f769ccea98ae105746e0238acc1652ce [file] [log] [blame]
Andrew Morton16d69262008-07-25 19:44:36 -07001#include <linux/mm.h>
Matt Mackall30992c92006-01-08 01:01:43 -08002#include <linux/slab.h>
3#include <linux/string.h>
4#include <linux/module.h>
Davi Arnaut96840aa2006-03-24 03:18:42 -08005#include <linux/err.h>
Adrian Bunk3b8f14b2008-07-26 15:22:28 -07006#include <linux/sched.h>
Davi Arnaut96840aa2006-03-24 03:18:42 -08007#include <asm/uaccess.h>
Matt Mackall30992c92006-01-08 01:01:43 -08008
9/**
Matt Mackall30992c92006-01-08 01:01:43 -080010 * kstrdup - allocate space for and copy an existing string
Matt Mackall30992c92006-01-08 01:01:43 -080011 * @s: the string to duplicate
12 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
13 */
14char *kstrdup(const char *s, gfp_t gfp)
15{
16 size_t len;
17 char *buf;
18
19 if (!s)
20 return NULL;
21
22 len = strlen(s) + 1;
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070023 buf = kmalloc_track_caller(len, gfp);
Matt Mackall30992c92006-01-08 01:01:43 -080024 if (buf)
25 memcpy(buf, s, len);
26 return buf;
27}
28EXPORT_SYMBOL(kstrdup);
Davi Arnaut96840aa2006-03-24 03:18:42 -080029
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070030/**
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -070031 * kstrndup - allocate space for and copy an existing string
32 * @s: the string to duplicate
33 * @max: read at most @max chars from @s
34 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
35 */
36char *kstrndup(const char *s, size_t max, gfp_t gfp)
37{
38 size_t len;
39 char *buf;
40
41 if (!s)
42 return NULL;
43
44 len = strnlen(s, max);
45 buf = kmalloc_track_caller(len+1, gfp);
46 if (buf) {
47 memcpy(buf, s, len);
48 buf[len] = '\0';
49 }
50 return buf;
51}
52EXPORT_SYMBOL(kstrndup);
53
54/**
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070055 * kmemdup - duplicate region of memory
56 *
57 * @src: memory region to duplicate
58 * @len: memory region length
59 * @gfp: GFP mask to use
60 */
61void *kmemdup(const void *src, size_t len, gfp_t gfp)
62{
63 void *p;
64
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070065 p = kmalloc_track_caller(len, gfp);
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070066 if (p)
67 memcpy(p, src, len);
68 return p;
69}
70EXPORT_SYMBOL(kmemdup);
71
Christoph Lameteref2ad802007-07-17 04:03:21 -070072/**
Li Zefan610a77e2009-03-31 15:23:16 -070073 * memdup_user - duplicate memory region from user space
74 *
75 * @src: source address in user space
76 * @len: number of bytes to copy
77 *
78 * Returns an ERR_PTR() on failure.
79 */
80void *memdup_user(const void __user *src, size_t len)
81{
82 void *p;
83
84 /*
85 * Always use GFP_KERNEL, since copy_from_user() can sleep and
86 * cause pagefault, which makes it pointless to use GFP_NOFS
87 * or GFP_ATOMIC.
88 */
89 p = kmalloc_track_caller(len, GFP_KERNEL);
90 if (!p)
91 return ERR_PTR(-ENOMEM);
92
93 if (copy_from_user(p, src, len)) {
94 kfree(p);
95 return ERR_PTR(-EFAULT);
96 }
97
98 return p;
99}
100EXPORT_SYMBOL(memdup_user);
101
102/**
Pekka Enberg93bc4e82008-07-26 17:49:33 -0700103 * __krealloc - like krealloc() but don't free @p.
104 * @p: object to reallocate memory for.
105 * @new_size: how many bytes of memory are required.
106 * @flags: the type of memory to allocate.
107 *
108 * This function is like krealloc() except it never frees the originally
109 * allocated buffer. Use this if you don't want to free the buffer immediately
110 * like, for example, with RCU.
111 */
112void *__krealloc(const void *p, size_t new_size, gfp_t flags)
113{
114 void *ret;
115 size_t ks = 0;
116
117 if (unlikely(!new_size))
118 return ZERO_SIZE_PTR;
119
120 if (p)
121 ks = ksize(p);
122
123 if (ks >= new_size)
124 return (void *)p;
125
126 ret = kmalloc_track_caller(new_size, flags);
127 if (ret && p)
128 memcpy(ret, p, ks);
129
130 return ret;
131}
132EXPORT_SYMBOL(__krealloc);
133
134/**
Christoph Lameteref2ad802007-07-17 04:03:21 -0700135 * krealloc - reallocate memory. The contents will remain unchanged.
136 * @p: object to reallocate memory for.
137 * @new_size: how many bytes of memory are required.
138 * @flags: the type of memory to allocate.
139 *
140 * The contents of the object pointed to are preserved up to the
141 * lesser of the new and old sizes. If @p is %NULL, krealloc()
142 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
143 * %NULL pointer, the object pointed to is freed.
144 */
145void *krealloc(const void *p, size_t new_size, gfp_t flags)
146{
147 void *ret;
Christoph Lameteref2ad802007-07-17 04:03:21 -0700148
149 if (unlikely(!new_size)) {
150 kfree(p);
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700151 return ZERO_SIZE_PTR;
Christoph Lameteref2ad802007-07-17 04:03:21 -0700152 }
153
Pekka Enberg93bc4e82008-07-26 17:49:33 -0700154 ret = __krealloc(p, new_size, flags);
155 if (ret && p != ret)
Christoph Lameteref2ad802007-07-17 04:03:21 -0700156 kfree(p);
Pekka Enberg93bc4e82008-07-26 17:49:33 -0700157
Christoph Lameteref2ad802007-07-17 04:03:21 -0700158 return ret;
159}
160EXPORT_SYMBOL(krealloc);
161
Johannes Weiner3ef0e5b2009-02-20 15:38:41 -0800162/**
163 * kzfree - like kfree but zero memory
164 * @p: object to free memory of
165 *
166 * The memory of the object @p points to is zeroed before freed.
167 * If @p is %NULL, kzfree() does nothing.
168 */
169void kzfree(const void *p)
170{
171 size_t ks;
172 void *mem = (void *)p;
173
174 if (unlikely(ZERO_OR_NULL_PTR(mem)))
175 return;
176 ks = ksize(mem);
177 memset(mem, 0, ks);
178 kfree(mem);
179}
180EXPORT_SYMBOL(kzfree);
181
Davi Arnaut96840aa2006-03-24 03:18:42 -0800182/*
183 * strndup_user - duplicate an existing string from user space
Davi Arnaut96840aa2006-03-24 03:18:42 -0800184 * @s: The string to duplicate
185 * @n: Maximum number of bytes to copy, including the trailing NUL.
186 */
187char *strndup_user(const char __user *s, long n)
188{
189 char *p;
190 long length;
191
192 length = strnlen_user(s, n);
193
194 if (!length)
195 return ERR_PTR(-EFAULT);
196
197 if (length > n)
198 return ERR_PTR(-EINVAL);
199
200 p = kmalloc(length, GFP_KERNEL);
201
202 if (!p)
203 return ERR_PTR(-ENOMEM);
204
205 if (copy_from_user(p, s, length)) {
206 kfree(p);
207 return ERR_PTR(-EFAULT);
208 }
209
210 p[length - 1] = '\0';
211
212 return p;
213}
214EXPORT_SYMBOL(strndup_user);
Andrew Morton16d69262008-07-25 19:44:36 -0700215
216#ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
217void arch_pick_mmap_layout(struct mm_struct *mm)
218{
219 mm->mmap_base = TASK_UNMAPPED_BASE;
220 mm->get_unmapped_area = arch_get_unmapped_area;
221 mm->unmap_area = arch_unmap_area;
222}
223#endif
Rusty Russell912985d2008-08-12 17:52:52 -0500224
225int __attribute__((weak)) get_user_pages_fast(unsigned long start,
226 int nr_pages, int write, struct page **pages)
227{
228 struct mm_struct *mm = current->mm;
229 int ret;
230
231 down_read(&mm->mmap_sem);
232 ret = get_user_pages(current, mm, start, nr_pages,
233 write, 0, pages, NULL);
234 up_read(&mm->mmap_sem);
235
236 return ret;
237}
238EXPORT_SYMBOL_GPL(get_user_pages_fast);