15d197571b4d57b0a8d3fc1f726f9ab64341ee11
2 #include <linux/slab.h>
3 #include <linux/string.h>
4 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <asm/uaccess.h>
10 * kstrdup - allocate space for and copy an existing string
11 * @s: the string to duplicate
12 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
14 char *kstrdup(const char *s
, gfp_t gfp
)
23 buf
= kmalloc_track_caller(len
, gfp
);
28 EXPORT_SYMBOL(kstrdup
);
31 * 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
36 char *kstrndup(const char *s
, size_t max
, gfp_t gfp
)
44 len
= strnlen(s
, max
);
45 buf
= kmalloc_track_caller(len
+1, gfp
);
52 EXPORT_SYMBOL(kstrndup
);
55 * kmemdup - duplicate region of memory
57 * @src: memory region to duplicate
58 * @len: memory region length
59 * @gfp: GFP mask to use
61 void *kmemdup(const void *src
, size_t len
, gfp_t gfp
)
65 p
= kmalloc_track_caller(len
, gfp
);
70 EXPORT_SYMBOL(kmemdup
);
73 * memdup_user - duplicate memory region from user space
75 * @src: source address in user space
76 * @len: number of bytes to copy
78 * Returns an ERR_PTR() on failure.
80 void *memdup_user(const void __user
*src
, size_t len
)
85 * Always use GFP_KERNEL, since copy_from_user() can sleep and
86 * cause pagefault, which makes it pointless to use GFP_NOFS
89 p
= kmalloc_track_caller(len
, GFP_KERNEL
);
91 return ERR_PTR(-ENOMEM
);
93 if (copy_from_user(p
, src
, len
)) {
95 return ERR_PTR(-EFAULT
);
100 EXPORT_SYMBOL(memdup_user
);
103 * __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.
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.
112 void *__krealloc(const void *p
, size_t new_size
, gfp_t flags
)
117 if (unlikely(!new_size
))
118 return ZERO_SIZE_PTR
;
126 ret
= kmalloc_track_caller(new_size
, flags
);
132 EXPORT_SYMBOL(__krealloc
);
135 * 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.
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.
145 void *krealloc(const void *p
, size_t new_size
, gfp_t flags
)
149 if (unlikely(!new_size
)) {
151 return ZERO_SIZE_PTR
;
154 ret
= __krealloc(p
, new_size
, flags
);
160 EXPORT_SYMBOL(krealloc
);
163 * kzfree - like kfree but zero memory
164 * @p: object to free memory of
166 * The memory of the object @p points to is zeroed before freed.
167 * If @p is %NULL, kzfree() does nothing.
169 * Note: this function zeroes the whole allocated buffer which can be a good
170 * deal bigger than the requested buffer size passed to kmalloc(). So be
171 * careful when using this function in performance sensitive code.
173 void kzfree(const void *p
)
176 void *mem
= (void *)p
;
178 if (unlikely(ZERO_OR_NULL_PTR(mem
)))
184 EXPORT_SYMBOL(kzfree
);
187 * strndup_user - duplicate an existing string from user space
188 * @s: The string to duplicate
189 * @n: Maximum number of bytes to copy, including the trailing NUL.
191 char *strndup_user(const char __user
*s
, long n
)
196 length
= strnlen_user(s
, n
);
199 return ERR_PTR(-EFAULT
);
202 return ERR_PTR(-EINVAL
);
204 p
= kmalloc(length
, GFP_KERNEL
);
207 return ERR_PTR(-ENOMEM
);
209 if (copy_from_user(p
, s
, length
)) {
211 return ERR_PTR(-EFAULT
);
214 p
[length
- 1] = '\0';
218 EXPORT_SYMBOL(strndup_user
);
220 #ifndef HAVE_ARCH_PICK_MMAP_LAYOUT
221 void arch_pick_mmap_layout(struct mm_struct
*mm
)
223 mm
->mmap_base
= TASK_UNMAPPED_BASE
;
224 mm
->get_unmapped_area
= arch_get_unmapped_area
;
225 mm
->unmap_area
= arch_unmap_area
;
230 * get_user_pages_fast() - pin user pages in memory
231 * @start: starting user address
232 * @nr_pages: number of pages from start to pin
233 * @write: whether pages will be written to
234 * @pages: array that receives pointers to the pages pinned.
235 * Should be at least nr_pages long.
237 * Returns number of pages pinned. This may be fewer than the number
238 * requested. If nr_pages is 0 or negative, returns 0. If no pages
239 * were pinned, returns -errno.
241 * get_user_pages_fast provides equivalent functionality to get_user_pages,
242 * operating on current and current->mm, with force=0 and vma=NULL. However
243 * unlike get_user_pages, it must be called without mmap_sem held.
245 * get_user_pages_fast may take mmap_sem and page table locks, so no
246 * assumptions can be made about lack of locking. get_user_pages_fast is to be
247 * implemented in a way that is advantageous (vs get_user_pages()) when the
248 * user memory area is already faulted in and present in ptes. However if the
249 * pages have to be faulted in, it may turn out to be slightly slower so
250 * callers need to carefully consider what to use. On many architectures,
251 * get_user_pages_fast simply falls back to get_user_pages.
253 int __attribute__((weak
)) get_user_pages_fast(unsigned long start
,
254 int nr_pages
, int write
, struct page
**pages
)
256 struct mm_struct
*mm
= current
->mm
;
259 down_read(&mm
->mmap_sem
);
260 ret
= get_user_pages(current
, mm
, start
, nr_pages
,
261 write
, 0, pages
, NULL
);
262 up_read(&mm
->mmap_sem
);
266 EXPORT_SYMBOL_GPL(get_user_pages_fast
);
268 /* Tracepoints definitions. */
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
);
This page took 0.042272 seconds and 4 git commands to generate.