util.c 8.9 KB

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