2 #include <linux/slab.h>
3 #include <linux/string.h>
4 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <linux/tracepoint.h>
8 #include <asm/uaccess.h>
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
15 char *kstrdup(const char *s, gfp_t gfp)
24 buf = kmalloc_track_caller(len, gfp);
29 EXPORT_SYMBOL(kstrdup);
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
37 char *kstrndup(const char *s, size_t max, gfp_t gfp)
45 len = strnlen(s, max);
46 buf = kmalloc_track_caller(len+1, gfp);
53 EXPORT_SYMBOL(kstrndup);
56 * kmemdup - duplicate region of memory
58 * @src: memory region to duplicate
59 * @len: memory region length
60 * @gfp: GFP mask to use
62 void *kmemdup(const void *src, size_t len, gfp_t gfp)
66 p = kmalloc_track_caller(len, gfp);
71 EXPORT_SYMBOL(kmemdup);
74 * memdup_user - duplicate memory region from user space
76 * @src: source address in user space
77 * @len: number of bytes to copy
79 * Returns an ERR_PTR() on failure.
81 void *memdup_user(const void __user *src, size_t len)
86 * Always use GFP_KERNEL, since copy_from_user() can sleep and
87 * cause pagefault, which makes it pointless to use GFP_NOFS
90 p = kmalloc_track_caller(len, GFP_KERNEL);
92 return ERR_PTR(-ENOMEM);
94 if (copy_from_user(p, src, len)) {
96 return ERR_PTR(-EFAULT);
101 EXPORT_SYMBOL(memdup_user);
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.
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.
113 void *__krealloc(const void *p, size_t new_size, gfp_t flags)
118 if (unlikely(!new_size))
119 return ZERO_SIZE_PTR;
127 ret = kmalloc_track_caller(new_size, flags);
133 EXPORT_SYMBOL(__krealloc);
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.
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.
146 void *krealloc(const void *p, size_t new_size, gfp_t flags)
150 if (unlikely(!new_size)) {
152 return ZERO_SIZE_PTR;
155 ret = __krealloc(p, new_size, flags);
161 EXPORT_SYMBOL(krealloc);
164 * kzfree - like kfree but zero memory
165 * @p: object to free memory of
167 * The memory of the object @p points to is zeroed before freed.
168 * If @p is %NULL, kzfree() does nothing.
170 void kzfree(const void *p)
173 void *mem = (void *)p;
175 if (unlikely(ZERO_OR_NULL_PTR(mem)))
181 EXPORT_SYMBOL(kzfree);
184 * strndup_user - duplicate an existing string from user space
185 * @s: The string to duplicate
186 * @n: Maximum number of bytes to copy, including the trailing NUL.
188 char *strndup_user(const char __user *s, long n)
193 length = strnlen_user(s, n);
196 return ERR_PTR(-EFAULT);
199 return ERR_PTR(-EINVAL);
201 p = kmalloc(length, GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
206 if (copy_from_user(p, s, length)) {
208 return ERR_PTR(-EFAULT);
211 p[length - 1] = '\0';
215 EXPORT_SYMBOL(strndup_user);
217 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
218 void arch_pick_mmap_layout(struct mm_struct *mm)
220 mm->mmap_base = TASK_UNMAPPED_BASE;
221 mm->get_unmapped_area = arch_get_unmapped_area;
222 mm->unmap_area = arch_unmap_area;
227 * get_user_pages_fast() - pin user pages in memory
228 * @start: starting user address
229 * @nr_pages: number of pages from start to pin
230 * @write: whether pages will be written to
231 * @pages: array that receives pointers to the pages pinned.
232 * Should be at least nr_pages long.
234 * Attempt to pin user pages in memory without taking mm->mmap_sem.
235 * If not successful, it will fall back to taking the lock and
236 * calling get_user_pages().
238 * Returns number of pages pinned. This may be fewer than the number
239 * requested. If nr_pages is 0 or negative, returns 0. If no pages
240 * were pinned, returns -errno.
242 int __attribute__((weak)) get_user_pages_fast(unsigned long start,
243 int nr_pages, int write, struct page **pages)
245 struct mm_struct *mm = current->mm;
248 down_read(&mm->mmap_sem);
249 ret = get_user_pages(current, mm, start, nr_pages,
250 write, 0, pages, NULL);
251 up_read(&mm->mmap_sem);
255 EXPORT_SYMBOL_GPL(get_user_pages_fast);
257 /* Tracepoints definitions. */
258 DEFINE_TRACE(kmalloc);
259 DEFINE_TRACE(kmem_cache_alloc);
260 DEFINE_TRACE(kmalloc_node);
261 DEFINE_TRACE(kmem_cache_alloc_node);
263 DEFINE_TRACE(kmem_cache_free);
265 EXPORT_TRACEPOINT_SYMBOL(kmalloc);
266 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
267 EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
268 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
269 EXPORT_TRACEPOINT_SYMBOL(kfree);
270 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);