util.c 6.5 KB

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