slab: document kzfree() zeroing behavior
[safe/jmp/linux-2.6] / mm / util.c
1 #include <linux/mm.h>
2 #include <linux/slab.h>
3 #include <linux/string.h>
4 #include <linux/module.h>
5 #include <linux/err.h>
6 #include <linux/sched.h>
7 #include <linux/tracepoint.h>
8 #include <asm/uaccess.h>
9
10 /**
11  * kstrdup - allocate space for and copy an existing string
12  * @s: the string to duplicate
13  * @gfp: the GFP mask used in the kmalloc() call when allocating memory
14  */
15 char *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;
24         buf = kmalloc_track_caller(len, gfp);
25         if (buf)
26                 memcpy(buf, s, len);
27         return buf;
28 }
29 EXPORT_SYMBOL(kstrdup);
30
31 /**
32  * 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  */
37 char *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 }
53 EXPORT_SYMBOL(kstrndup);
54
55 /**
56  * kmemdup - duplicate region of memory
57  *
58  * @src: memory region to duplicate
59  * @len: memory region length
60  * @gfp: GFP mask to use
61  */
62 void *kmemdup(const void *src, size_t len, gfp_t gfp)
63 {
64         void *p;
65
66         p = kmalloc_track_caller(len, gfp);
67         if (p)
68                 memcpy(p, src, len);
69         return p;
70 }
71 EXPORT_SYMBOL(kmemdup);
72
73 /**
74  * 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  */
81 void *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 }
101 EXPORT_SYMBOL(memdup_user);
102
103 /**
104  * __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  */
113 void *__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 }
133 EXPORT_SYMBOL(__krealloc);
134
135 /**
136  * 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  */
146 void *krealloc(const void *p, size_t new_size, gfp_t flags)
147 {
148         void *ret;
149
150         if (unlikely(!new_size)) {
151                 kfree(p);
152                 return ZERO_SIZE_PTR;
153         }
154
155         ret = __krealloc(p, new_size, flags);
156         if (ret && p != ret)
157                 kfree(p);
158
159         return ret;
160 }
161 EXPORT_SYMBOL(krealloc);
162
163 /**
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  *
170  * Note: this function zeroes the whole allocated buffer which can be a good
171  * deal bigger than the requested buffer size passed to kmalloc(). So be
172  * careful when using this function in performance sensitive code.
173  */
174 void kzfree(const void *p)
175 {
176         size_t ks;
177         void *mem = (void *)p;
178
179         if (unlikely(ZERO_OR_NULL_PTR(mem)))
180                 return;
181         ks = ksize(mem);
182         memset(mem, 0, ks);
183         kfree(mem);
184 }
185 EXPORT_SYMBOL(kzfree);
186
187 /*
188  * strndup_user - duplicate an existing string from user space
189  * @s: The string to duplicate
190  * @n: Maximum number of bytes to copy, including the trailing NUL.
191  */
192 char *strndup_user(const char __user *s, long n)
193 {
194         char *p;
195         long length;
196
197         length = strnlen_user(s, n);
198
199         if (!length)
200                 return ERR_PTR(-EFAULT);
201
202         if (length > n)
203                 return ERR_PTR(-EINVAL);
204
205         p = kmalloc(length, GFP_KERNEL);
206
207         if (!p)
208                 return ERR_PTR(-ENOMEM);
209
210         if (copy_from_user(p, s, length)) {
211                 kfree(p);
212                 return ERR_PTR(-EFAULT);
213         }
214
215         p[length - 1] = '\0';
216
217         return p;
218 }
219 EXPORT_SYMBOL(strndup_user);
220
221 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
222 void arch_pick_mmap_layout(struct mm_struct *mm)
223 {
224         mm->mmap_base = TASK_UNMAPPED_BASE;
225         mm->get_unmapped_area = arch_get_unmapped_area;
226         mm->unmap_area = arch_unmap_area;
227 }
228 #endif
229
230 /**
231  * get_user_pages_fast() - pin user pages in memory
232  * @start:      starting user address
233  * @nr_pages:   number of pages from start to pin
234  * @write:      whether pages will be written to
235  * @pages:      array that receives pointers to the pages pinned.
236  *              Should be at least nr_pages long.
237  *
238  * Attempt to pin user pages in memory without taking mm->mmap_sem.
239  * If not successful, it will fall back to taking the lock and
240  * calling get_user_pages().
241  *
242  * Returns number of pages pinned. This may be fewer than the number
243  * requested. If nr_pages is 0 or negative, returns 0. If no pages
244  * were pinned, returns -errno.
245  */
246 int __attribute__((weak)) get_user_pages_fast(unsigned long start,
247                                 int nr_pages, int write, struct page **pages)
248 {
249         struct mm_struct *mm = current->mm;
250         int ret;
251
252         down_read(&mm->mmap_sem);
253         ret = get_user_pages(current, mm, start, nr_pages,
254                                         write, 0, pages, NULL);
255         up_read(&mm->mmap_sem);
256
257         return ret;
258 }
259 EXPORT_SYMBOL_GPL(get_user_pages_fast);
260
261 /* Tracepoints definitions. */
262 DEFINE_TRACE(kmalloc);
263 DEFINE_TRACE(kmem_cache_alloc);
264 DEFINE_TRACE(kmalloc_node);
265 DEFINE_TRACE(kmem_cache_alloc_node);
266 DEFINE_TRACE(kfree);
267 DEFINE_TRACE(kmem_cache_free);
268
269 EXPORT_TRACEPOINT_SYMBOL(kmalloc);
270 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
271 EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
272 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
273 EXPORT_TRACEPOINT_SYMBOL(kfree);
274 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);