util.c 9.1 KB

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