util.c 10 KB

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