blob: 2599e83eea17a9c9673c26507541b12581e952f0 [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>
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02007#include <linux/tracepoint.h>
Davi Arnaut96840aa2006-03-24 03:18:42 -08008#include <asm/uaccess.h>
Matt Mackall30992c92006-01-08 01:01:43 -08009
10/**
Matt Mackall30992c92006-01-08 01:01:43 -080011 * kstrdup - allocate space for and copy an existing string
Matt Mackall30992c92006-01-08 01:01:43 -080012 * @s: the string to duplicate
13 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
14 */
15char *kstrdup(const char *s, gfp_t gfp)
16{
17 size_t len;
18 char *buf;
19
20 if (!s)
21 return NULL;
22
23 len = strlen(s) + 1;
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070024 buf = kmalloc_track_caller(len, gfp);
Matt Mackall30992c92006-01-08 01:01:43 -080025 if (buf)
26 memcpy(buf, s, len);
27 return buf;
28}
29EXPORT_SYMBOL(kstrdup);
Davi Arnaut96840aa2006-03-24 03:18:42 -080030
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070031/**
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -070032 * kstrndup - allocate space for and copy an existing string
33 * @s: the string to duplicate
34 * @max: read at most @max chars from @s
35 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
36 */
37char *kstrndup(const char *s, size_t max, gfp_t gfp)
38{
39 size_t len;
40 char *buf;
41
42 if (!s)
43 return NULL;
44
45 len = strnlen(s, max);
46 buf = kmalloc_track_caller(len+1, gfp);
47 if (buf) {
48 memcpy(buf, s, len);
49 buf[len] = '\0';
50 }
51 return buf;
52}
53EXPORT_SYMBOL(kstrndup);
54
55/**
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070056 * kmemdup - duplicate region of memory
57 *
58 * @src: memory region to duplicate
59 * @len: memory region length
60 * @gfp: GFP mask to use
61 */
62void *kmemdup(const void *src, size_t len, gfp_t gfp)
63{
64 void *p;
65
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070066 p = kmalloc_track_caller(len, gfp);
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070067 if (p)
68 memcpy(p, src, len);
69 return p;
70}
71EXPORT_SYMBOL(kmemdup);
72
Christoph Lameteref2ad802007-07-17 04:03:21 -070073/**
Li Zefan610a77e2009-03-31 15:23:16 -070074 * memdup_user - duplicate memory region from user space
75 *
76 * @src: source address in user space
77 * @len: number of bytes to copy
78 *
79 * Returns an ERR_PTR() on failure.
80 */
81void *memdup_user(const void __user *src, size_t len)
82{
83 void *p;
84
85 /*
86 * Always use GFP_KERNEL, since copy_from_user() can sleep and
87 * cause pagefault, which makes it pointless to use GFP_NOFS
88 * or GFP_ATOMIC.
89 */
90 p = kmalloc_track_caller(len, GFP_KERNEL);
91 if (!p)
92 return ERR_PTR(-ENOMEM);
93
94 if (copy_from_user(p, src, len)) {
95 kfree(p);
96 return ERR_PTR(-EFAULT);
97 }
98
99 return p;
100}
101EXPORT_SYMBOL(memdup_user);
102
103/**
Pekka Enberg93bc4e82008-07-26 17:49:33 -0700104 * __krealloc - like krealloc() but don't free @p.
105 * @p: object to reallocate memory for.
106 * @new_size: how many bytes of memory are required.
107 * @flags: the type of memory to allocate.
108 *
109 * This function is like krealloc() except it never frees the originally
110 * allocated buffer. Use this if you don't want to free the buffer immediately
111 * like, for example, with RCU.
112 */
113void *__krealloc(const void *p, size_t new_size, gfp_t flags)
114{
115 void *ret;
116 size_t ks = 0;
117
118 if (unlikely(!new_size))
119 return ZERO_SIZE_PTR;
120
121 if (p)
122 ks = ksize(p);
123
124 if (ks >= new_size)
125 return (void *)p;
126
127 ret = kmalloc_track_caller(new_size, flags);
128 if (ret && p)
129 memcpy(ret, p, ks);
130
131 return ret;
132}
133EXPORT_SYMBOL(__krealloc);
134
135/**
Christoph Lameteref2ad802007-07-17 04:03:21 -0700136 * krealloc - reallocate memory. The contents will remain unchanged.
137 * @p: object to reallocate memory for.
138 * @new_size: how many bytes of memory are required.
139 * @flags: the type of memory to allocate.
140 *
141 * The contents of the object pointed to are preserved up to the
142 * lesser of the new and old sizes. If @p is %NULL, krealloc()
143 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
144 * %NULL pointer, the object pointed to is freed.
145 */
146void *krealloc(const void *p, size_t new_size, gfp_t flags)
147{
148 void *ret;
Christoph Lameteref2ad802007-07-17 04:03:21 -0700149
150 if (unlikely(!new_size)) {
151 kfree(p);
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700152 return ZERO_SIZE_PTR;
Christoph Lameteref2ad802007-07-17 04:03:21 -0700153 }
154
Pekka Enberg93bc4e82008-07-26 17:49:33 -0700155 ret = __krealloc(p, new_size, flags);
156 if (ret && p != ret)
Christoph Lameteref2ad802007-07-17 04:03:21 -0700157 kfree(p);
Pekka Enberg93bc4e82008-07-26 17:49:33 -0700158
Christoph Lameteref2ad802007-07-17 04:03:21 -0700159 return ret;
160}
161EXPORT_SYMBOL(krealloc);
162
Johannes Weiner3ef0e5b2009-02-20 15:38:41 -0800163/**
164 * kzfree - like kfree but zero memory
165 * @p: object to free memory of
166 *
167 * The memory of the object @p points to is zeroed before freed.
168 * If @p is %NULL, kzfree() does nothing.
169 */
170void kzfree(const void *p)
171{
172 size_t ks;
173 void *mem = (void *)p;
174
175 if (unlikely(ZERO_OR_NULL_PTR(mem)))
176 return;
177 ks = ksize(mem);
178 memset(mem, 0, ks);
179 kfree(mem);
180}
181EXPORT_SYMBOL(kzfree);
182
Davi Arnaut96840aa2006-03-24 03:18:42 -0800183/*
184 * strndup_user - duplicate an existing string from user space
Davi Arnaut96840aa2006-03-24 03:18:42 -0800185 * @s: The string to duplicate
186 * @n: Maximum number of bytes to copy, including the trailing NUL.
187 */
188char *strndup_user(const char __user *s, long n)
189{
190 char *p;
191 long length;
192
193 length = strnlen_user(s, n);
194
195 if (!length)
196 return ERR_PTR(-EFAULT);
197
198 if (length > n)
199 return ERR_PTR(-EINVAL);
200
201 p = kmalloc(length, GFP_KERNEL);
202
203 if (!p)
204 return ERR_PTR(-ENOMEM);
205
206 if (copy_from_user(p, s, length)) {
207 kfree(p);
208 return ERR_PTR(-EFAULT);
209 }
210
211 p[length - 1] = '\0';
212
213 return p;
214}
215EXPORT_SYMBOL(strndup_user);
Andrew Morton16d69262008-07-25 19:44:36 -0700216
217#ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
218void arch_pick_mmap_layout(struct mm_struct *mm)
219{
220 mm->mmap_base = TASK_UNMAPPED_BASE;
221 mm->get_unmapped_area = arch_get_unmapped_area;
222 mm->unmap_area = arch_unmap_area;
223}
224#endif
Rusty Russell912985d2008-08-12 17:52:52 -0500225
226int __attribute__((weak)) get_user_pages_fast(unsigned long start,
227 int nr_pages, int write, struct page **pages)
228{
229 struct mm_struct *mm = current->mm;
230 int ret;
231
232 down_read(&mm->mmap_sem);
233 ret = get_user_pages(current, mm, start, nr_pages,
234 write, 0, pages, NULL);
235 up_read(&mm->mmap_sem);
236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(get_user_pages_fast);
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +0200240
241/* Tracepoints definitions. */
242DEFINE_TRACE(kmalloc);
243DEFINE_TRACE(kmem_cache_alloc);
244DEFINE_TRACE(kmalloc_node);
245DEFINE_TRACE(kmem_cache_alloc_node);
246DEFINE_TRACE(kfree);
247DEFINE_TRACE(kmem_cache_free);
248
249EXPORT_TRACEPOINT_SYMBOL(kmalloc);
250EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
251EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
252EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
253EXPORT_TRACEPOINT_SYMBOL(kfree);
254EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);