util.c 9.5 KB

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