util.c 9.6 KB

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