util.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <asm/uaccess.h>
  8. #include "internal.h"
  9. #define CREATE_TRACE_POINTS
  10. #include <trace/events/kmem.h>
  11. /**
  12. * kstrdup - allocate space for and copy an existing string
  13. * @s: the string to duplicate
  14. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  15. */
  16. char *kstrdup(const char *s, gfp_t gfp)
  17. {
  18. size_t len;
  19. char *buf;
  20. if (!s)
  21. return NULL;
  22. len = strlen(s) + 1;
  23. buf = kmalloc_track_caller(len, gfp);
  24. if (buf)
  25. memcpy(buf, s, len);
  26. return buf;
  27. }
  28. EXPORT_SYMBOL(kstrdup);
  29. /**
  30. * kstrndup - allocate space for and copy an existing string
  31. * @s: the string to duplicate
  32. * @max: read at most @max chars from @s
  33. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  34. */
  35. char *kstrndup(const char *s, size_t max, gfp_t gfp)
  36. {
  37. size_t len;
  38. char *buf;
  39. if (!s)
  40. return NULL;
  41. len = strnlen(s, max);
  42. buf = kmalloc_track_caller(len+1, gfp);
  43. if (buf) {
  44. memcpy(buf, s, len);
  45. buf[len] = '\0';
  46. }
  47. return buf;
  48. }
  49. EXPORT_SYMBOL(kstrndup);
  50. /**
  51. * kmemdup - duplicate region of memory
  52. *
  53. * @src: memory region to duplicate
  54. * @len: memory region length
  55. * @gfp: GFP mask to use
  56. */
  57. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  58. {
  59. void *p;
  60. p = kmalloc_track_caller(len, gfp);
  61. if (p)
  62. memcpy(p, src, len);
  63. return p;
  64. }
  65. EXPORT_SYMBOL(kmemdup);
  66. /**
  67. * memdup_user - duplicate memory region from user space
  68. *
  69. * @src: source address in user space
  70. * @len: number of bytes to copy
  71. *
  72. * Returns an ERR_PTR() on failure.
  73. */
  74. void *memdup_user(const void __user *src, size_t len)
  75. {
  76. void *p;
  77. /*
  78. * Always use GFP_KERNEL, since copy_from_user() can sleep and
  79. * cause pagefault, which makes it pointless to use GFP_NOFS
  80. * or GFP_ATOMIC.
  81. */
  82. p = kmalloc_track_caller(len, GFP_KERNEL);
  83. if (!p)
  84. return ERR_PTR(-ENOMEM);
  85. if (copy_from_user(p, src, len)) {
  86. kfree(p);
  87. return ERR_PTR(-EFAULT);
  88. }
  89. return p;
  90. }
  91. EXPORT_SYMBOL(memdup_user);
  92. /**
  93. * __krealloc - like krealloc() but don't free @p.
  94. * @p: object to reallocate memory for.
  95. * @new_size: how many bytes of memory are required.
  96. * @flags: the type of memory to allocate.
  97. *
  98. * This function is like krealloc() except it never frees the originally
  99. * allocated buffer. Use this if you don't want to free the buffer immediately
  100. * like, for example, with RCU.
  101. */
  102. void *__krealloc(const void *p, size_t new_size, gfp_t flags)
  103. {
  104. void *ret;
  105. size_t ks = 0;
  106. if (unlikely(!new_size))
  107. return ZERO_SIZE_PTR;
  108. if (p)
  109. ks = ksize(p);
  110. if (ks >= new_size)
  111. return (void *)p;
  112. ret = kmalloc_track_caller(new_size, flags);
  113. if (ret && p)
  114. memcpy(ret, p, ks);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL(__krealloc);
  118. /**
  119. * krealloc - reallocate memory. The contents will remain unchanged.
  120. * @p: object to reallocate memory for.
  121. * @new_size: how many bytes of memory are required.
  122. * @flags: the type of memory to allocate.
  123. *
  124. * The contents of the object pointed to are preserved up to the
  125. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  126. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  127. * %NULL pointer, the object pointed to is freed.
  128. */
  129. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  130. {
  131. void *ret;
  132. if (unlikely(!new_size)) {
  133. kfree(p);
  134. return ZERO_SIZE_PTR;
  135. }
  136. ret = __krealloc(p, new_size, flags);
  137. if (ret && p != ret)
  138. kfree(p);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(krealloc);
  142. /**
  143. * kzfree - like kfree but zero memory
  144. * @p: object to free memory of
  145. *
  146. * The memory of the object @p points to is zeroed before freed.
  147. * If @p is %NULL, kzfree() does nothing.
  148. *
  149. * Note: this function zeroes the whole allocated buffer which can be a good
  150. * deal bigger than the requested buffer size passed to kmalloc(). So be
  151. * careful when using this function in performance sensitive code.
  152. */
  153. void kzfree(const void *p)
  154. {
  155. size_t ks;
  156. void *mem = (void *)p;
  157. if (unlikely(ZERO_OR_NULL_PTR(mem)))
  158. return;
  159. ks = ksize(mem);
  160. memset(mem, 0, ks);
  161. kfree(mem);
  162. }
  163. EXPORT_SYMBOL(kzfree);
  164. /*
  165. * strndup_user - duplicate an existing string from user space
  166. * @s: The string to duplicate
  167. * @n: Maximum number of bytes to copy, including the trailing NUL.
  168. */
  169. char *strndup_user(const char __user *s, long n)
  170. {
  171. char *p;
  172. long length;
  173. length = strnlen_user(s, n);
  174. if (!length)
  175. return ERR_PTR(-EFAULT);
  176. if (length > n)
  177. return ERR_PTR(-EINVAL);
  178. p = memdup_user(s, length);
  179. if (IS_ERR(p))
  180. return p;
  181. p[length - 1] = '\0';
  182. return p;
  183. }
  184. EXPORT_SYMBOL(strndup_user);
  185. void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  186. struct vm_area_struct *prev, struct rb_node *rb_parent)
  187. {
  188. struct vm_area_struct *next;
  189. vma->vm_prev = prev;
  190. if (prev) {
  191. next = prev->vm_next;
  192. prev->vm_next = vma;
  193. } else {
  194. mm->mmap = vma;
  195. if (rb_parent)
  196. next = rb_entry(rb_parent,
  197. struct vm_area_struct, vm_rb);
  198. else
  199. next = NULL;
  200. }
  201. vma->vm_next = next;
  202. if (next)
  203. next->vm_prev = vma;
  204. }
  205. #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
  206. void arch_pick_mmap_layout(struct mm_struct *mm)
  207. {
  208. mm->mmap_base = TASK_UNMAPPED_BASE;
  209. mm->get_unmapped_area = arch_get_unmapped_area;
  210. mm->unmap_area = arch_unmap_area;
  211. }
  212. #endif
  213. /*
  214. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  215. * back to the regular GUP.
  216. * If the architecture not support this function, simply return with no
  217. * page pinned
  218. */
  219. int __attribute__((weak)) __get_user_pages_fast(unsigned long start,
  220. int nr_pages, int write, struct page **pages)
  221. {
  222. return 0;
  223. }
  224. EXPORT_SYMBOL_GPL(__get_user_pages_fast);
  225. /**
  226. * get_user_pages_fast() - pin user pages in memory
  227. * @start: starting user address
  228. * @nr_pages: number of pages from start to pin
  229. * @write: whether pages will be written to
  230. * @pages: array that receives pointers to the pages pinned.
  231. * Should be at least nr_pages long.
  232. *
  233. * Returns number of pages pinned. This may be fewer than the number
  234. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  235. * were pinned, returns -errno.
  236. *
  237. * get_user_pages_fast provides equivalent functionality to get_user_pages,
  238. * operating on current and current->mm, with force=0 and vma=NULL. However
  239. * unlike get_user_pages, it must be called without mmap_sem held.
  240. *
  241. * get_user_pages_fast may take mmap_sem and page table locks, so no
  242. * assumptions can be made about lack of locking. get_user_pages_fast is to be
  243. * implemented in a way that is advantageous (vs get_user_pages()) when the
  244. * user memory area is already faulted in and present in ptes. However if the
  245. * pages have to be faulted in, it may turn out to be slightly slower so
  246. * callers need to carefully consider what to use. On many architectures,
  247. * get_user_pages_fast simply falls back to get_user_pages.
  248. */
  249. int __attribute__((weak)) get_user_pages_fast(unsigned long start,
  250. int nr_pages, int write, struct page **pages)
  251. {
  252. struct mm_struct *mm = current->mm;
  253. int ret;
  254. down_read(&mm->mmap_sem);
  255. ret = get_user_pages(current, mm, start, nr_pages,
  256. write, 0, pages, NULL);
  257. up_read(&mm->mmap_sem);
  258. return ret;
  259. }
  260. EXPORT_SYMBOL_GPL(get_user_pages_fast);
  261. /* Tracepoints definitions. */
  262. EXPORT_TRACEPOINT_SYMBOL(kmalloc);
  263. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
  264. EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
  265. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
  266. EXPORT_TRACEPOINT_SYMBOL(kfree);
  267. EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);