util.c 7.7 KB

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