util.c 12 KB

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