util.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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/sched/mm.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/security.h>
  11. #include <linux/swap.h>
  12. #include <linux/swapops.h>
  13. #include <linux/mman.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/userfaultfd_k.h>
  17. #include <asm/sections.h>
  18. #include <linux/uaccess.h>
  19. #include "internal.h"
  20. static inline int is_kernel_rodata(unsigned long addr)
  21. {
  22. return addr >= (unsigned long)__start_rodata &&
  23. addr < (unsigned long)__end_rodata;
  24. }
  25. /**
  26. * kfree_const - conditionally free memory
  27. * @x: pointer to the memory
  28. *
  29. * Function calls kfree only if @x is not in .rodata section.
  30. */
  31. void kfree_const(const void *x)
  32. {
  33. if (!is_kernel_rodata((unsigned long)x))
  34. kfree(x);
  35. }
  36. EXPORT_SYMBOL(kfree_const);
  37. /**
  38. * kstrdup - allocate space for and copy an existing string
  39. * @s: the string to duplicate
  40. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  41. */
  42. char *kstrdup(const char *s, gfp_t gfp)
  43. {
  44. size_t len;
  45. char *buf;
  46. if (!s)
  47. return NULL;
  48. len = strlen(s) + 1;
  49. buf = kmalloc_track_caller(len, gfp);
  50. if (buf)
  51. memcpy(buf, s, len);
  52. return buf;
  53. }
  54. EXPORT_SYMBOL(kstrdup);
  55. /**
  56. * kstrdup_const - conditionally duplicate an existing const string
  57. * @s: the string to duplicate
  58. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  59. *
  60. * Function returns source string if it is in .rodata section otherwise it
  61. * fallbacks to kstrdup.
  62. * Strings allocated by kstrdup_const should be freed by kfree_const.
  63. */
  64. const char *kstrdup_const(const char *s, gfp_t gfp)
  65. {
  66. if (is_kernel_rodata((unsigned long)s))
  67. return s;
  68. return kstrdup(s, gfp);
  69. }
  70. EXPORT_SYMBOL(kstrdup_const);
  71. /**
  72. * kstrndup - allocate space for and copy an existing string
  73. * @s: the string to duplicate
  74. * @max: read at most @max chars from @s
  75. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  76. */
  77. char *kstrndup(const char *s, size_t max, gfp_t gfp)
  78. {
  79. size_t len;
  80. char *buf;
  81. if (!s)
  82. return NULL;
  83. len = strnlen(s, max);
  84. buf = kmalloc_track_caller(len+1, gfp);
  85. if (buf) {
  86. memcpy(buf, s, len);
  87. buf[len] = '\0';
  88. }
  89. return buf;
  90. }
  91. EXPORT_SYMBOL(kstrndup);
  92. /**
  93. * kmemdup - duplicate region of memory
  94. *
  95. * @src: memory region to duplicate
  96. * @len: memory region length
  97. * @gfp: GFP mask to use
  98. */
  99. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  100. {
  101. void *p;
  102. p = kmalloc_track_caller(len, gfp);
  103. if (p)
  104. memcpy(p, src, len);
  105. return p;
  106. }
  107. EXPORT_SYMBOL(kmemdup);
  108. /**
  109. * memdup_user - duplicate memory region from user space
  110. *
  111. * @src: source address in user space
  112. * @len: number of bytes to copy
  113. *
  114. * Returns an ERR_PTR() on failure.
  115. */
  116. void *memdup_user(const void __user *src, size_t len)
  117. {
  118. void *p;
  119. /*
  120. * Always use GFP_KERNEL, since copy_from_user() can sleep and
  121. * cause pagefault, which makes it pointless to use GFP_NOFS
  122. * or GFP_ATOMIC.
  123. */
  124. p = kmalloc_track_caller(len, GFP_KERNEL);
  125. if (!p)
  126. return ERR_PTR(-ENOMEM);
  127. if (copy_from_user(p, src, len)) {
  128. kfree(p);
  129. return ERR_PTR(-EFAULT);
  130. }
  131. return p;
  132. }
  133. EXPORT_SYMBOL(memdup_user);
  134. /*
  135. * strndup_user - duplicate an existing string from user space
  136. * @s: The string to duplicate
  137. * @n: Maximum number of bytes to copy, including the trailing NUL.
  138. */
  139. char *strndup_user(const char __user *s, long n)
  140. {
  141. char *p;
  142. long length;
  143. length = strnlen_user(s, n);
  144. if (!length)
  145. return ERR_PTR(-EFAULT);
  146. if (length > n)
  147. return ERR_PTR(-EINVAL);
  148. p = memdup_user(s, length);
  149. if (IS_ERR(p))
  150. return p;
  151. p[length - 1] = '\0';
  152. return p;
  153. }
  154. EXPORT_SYMBOL(strndup_user);
  155. /**
  156. * memdup_user_nul - duplicate memory region from user space and NUL-terminate
  157. *
  158. * @src: source address in user space
  159. * @len: number of bytes to copy
  160. *
  161. * Returns an ERR_PTR() on failure.
  162. */
  163. void *memdup_user_nul(const void __user *src, size_t len)
  164. {
  165. char *p;
  166. /*
  167. * Always use GFP_KERNEL, since copy_from_user() can sleep and
  168. * cause pagefault, which makes it pointless to use GFP_NOFS
  169. * or GFP_ATOMIC.
  170. */
  171. p = kmalloc_track_caller(len + 1, GFP_KERNEL);
  172. if (!p)
  173. return ERR_PTR(-ENOMEM);
  174. if (copy_from_user(p, src, len)) {
  175. kfree(p);
  176. return ERR_PTR(-EFAULT);
  177. }
  178. p[len] = '\0';
  179. return p;
  180. }
  181. EXPORT_SYMBOL(memdup_user_nul);
  182. void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  183. struct vm_area_struct *prev, struct rb_node *rb_parent)
  184. {
  185. struct vm_area_struct *next;
  186. vma->vm_prev = prev;
  187. if (prev) {
  188. next = prev->vm_next;
  189. prev->vm_next = vma;
  190. } else {
  191. mm->mmap = vma;
  192. if (rb_parent)
  193. next = rb_entry(rb_parent,
  194. struct vm_area_struct, vm_rb);
  195. else
  196. next = NULL;
  197. }
  198. vma->vm_next = next;
  199. if (next)
  200. next->vm_prev = vma;
  201. }
  202. /* Check if the vma is being used as a stack by this task */
  203. int vma_is_stack_for_current(struct vm_area_struct *vma)
  204. {
  205. struct task_struct * __maybe_unused t = current;
  206. return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
  207. }
  208. #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
  209. void arch_pick_mmap_layout(struct mm_struct *mm)
  210. {
  211. mm->mmap_base = TASK_UNMAPPED_BASE;
  212. mm->get_unmapped_area = arch_get_unmapped_area;
  213. }
  214. #endif
  215. /*
  216. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  217. * back to the regular GUP.
  218. * If the architecture not support this function, simply return with no
  219. * page pinned
  220. */
  221. int __weak __get_user_pages_fast(unsigned long start,
  222. int nr_pages, int write, struct page **pages)
  223. {
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(__get_user_pages_fast);
  227. /**
  228. * get_user_pages_fast() - pin user pages in memory
  229. * @start: starting user address
  230. * @nr_pages: number of pages from start to pin
  231. * @write: whether pages will be written to
  232. * @pages: array that receives pointers to the pages pinned.
  233. * Should be at least nr_pages long.
  234. *
  235. * Returns number of pages pinned. This may be fewer than the number
  236. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  237. * were pinned, returns -errno.
  238. *
  239. * get_user_pages_fast provides equivalent functionality to get_user_pages,
  240. * operating on current and current->mm, with force=0 and vma=NULL. However
  241. * unlike get_user_pages, it must be called without mmap_sem held.
  242. *
  243. * get_user_pages_fast may take mmap_sem and page table locks, so no
  244. * assumptions can be made about lack of locking. get_user_pages_fast is to be
  245. * implemented in a way that is advantageous (vs get_user_pages()) when the
  246. * user memory area is already faulted in and present in ptes. However if the
  247. * pages have to be faulted in, it may turn out to be slightly slower so
  248. * callers need to carefully consider what to use. On many architectures,
  249. * get_user_pages_fast simply falls back to get_user_pages.
  250. */
  251. int __weak get_user_pages_fast(unsigned long start,
  252. int nr_pages, int write, struct page **pages)
  253. {
  254. return get_user_pages_unlocked(start, nr_pages, pages,
  255. write ? FOLL_WRITE : 0);
  256. }
  257. EXPORT_SYMBOL_GPL(get_user_pages_fast);
  258. unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
  259. unsigned long len, unsigned long prot,
  260. unsigned long flag, unsigned long pgoff)
  261. {
  262. unsigned long ret;
  263. struct mm_struct *mm = current->mm;
  264. unsigned long populate;
  265. LIST_HEAD(uf);
  266. ret = security_mmap_file(file, prot, flag);
  267. if (!ret) {
  268. if (down_write_killable(&mm->mmap_sem))
  269. return -EINTR;
  270. ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
  271. &populate, &uf);
  272. up_write(&mm->mmap_sem);
  273. userfaultfd_unmap_complete(mm, &uf);
  274. if (populate)
  275. mm_populate(ret, populate);
  276. }
  277. return ret;
  278. }
  279. unsigned long vm_mmap(struct file *file, unsigned long addr,
  280. unsigned long len, unsigned long prot,
  281. unsigned long flag, unsigned long offset)
  282. {
  283. if (unlikely(offset + PAGE_ALIGN(len) < offset))
  284. return -EINVAL;
  285. if (unlikely(offset_in_page(offset)))
  286. return -EINVAL;
  287. return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
  288. }
  289. EXPORT_SYMBOL(vm_mmap);
  290. /**
  291. * kvmalloc_node - attempt to allocate physically contiguous memory, but upon
  292. * failure, fall back to non-contiguous (vmalloc) allocation.
  293. * @size: size of the request.
  294. * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.
  295. * @node: numa node to allocate from
  296. *
  297. * Uses kmalloc to get the memory but if the allocation fails then falls back
  298. * to the vmalloc allocator. Use kvfree for freeing the memory.
  299. *
  300. * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
  301. * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
  302. * preferable to the vmalloc fallback, due to visible performance drawbacks.
  303. *
  304. * Any use of gfp flags outside of GFP_KERNEL should be consulted with mm people.
  305. */
  306. void *kvmalloc_node(size_t size, gfp_t flags, int node)
  307. {
  308. gfp_t kmalloc_flags = flags;
  309. void *ret;
  310. /*
  311. * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
  312. * so the given set of flags has to be compatible.
  313. */
  314. WARN_ON_ONCE((flags & GFP_KERNEL) != GFP_KERNEL);
  315. /*
  316. * We want to attempt a large physically contiguous block first because
  317. * it is less likely to fragment multiple larger blocks and therefore
  318. * contribute to a long term fragmentation less than vmalloc fallback.
  319. * However make sure that larger requests are not too disruptive - no
  320. * OOM killer and no allocation failure warnings as we have a fallback.
  321. */
  322. if (size > PAGE_SIZE) {
  323. kmalloc_flags |= __GFP_NOWARN;
  324. if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
  325. kmalloc_flags |= __GFP_NORETRY;
  326. }
  327. ret = kmalloc_node(size, kmalloc_flags, node);
  328. /*
  329. * It doesn't really make sense to fallback to vmalloc for sub page
  330. * requests
  331. */
  332. if (ret || size <= PAGE_SIZE)
  333. return ret;
  334. return __vmalloc_node_flags_caller(size, node, flags,
  335. __builtin_return_address(0));
  336. }
  337. EXPORT_SYMBOL(kvmalloc_node);
  338. void kvfree(const void *addr)
  339. {
  340. if (is_vmalloc_addr(addr))
  341. vfree(addr);
  342. else
  343. kfree(addr);
  344. }
  345. EXPORT_SYMBOL(kvfree);
  346. static inline void *__page_rmapping(struct page *page)
  347. {
  348. unsigned long mapping;
  349. mapping = (unsigned long)page->mapping;
  350. mapping &= ~PAGE_MAPPING_FLAGS;
  351. return (void *)mapping;
  352. }
  353. /* Neutral page->mapping pointer to address_space or anon_vma or other */
  354. void *page_rmapping(struct page *page)
  355. {
  356. page = compound_head(page);
  357. return __page_rmapping(page);
  358. }
  359. /*
  360. * Return true if this page is mapped into pagetables.
  361. * For compound page it returns true if any subpage of compound page is mapped.
  362. */
  363. bool page_mapped(struct page *page)
  364. {
  365. int i;
  366. if (likely(!PageCompound(page)))
  367. return atomic_read(&page->_mapcount) >= 0;
  368. page = compound_head(page);
  369. if (atomic_read(compound_mapcount_ptr(page)) >= 0)
  370. return true;
  371. if (PageHuge(page))
  372. return false;
  373. for (i = 0; i < hpage_nr_pages(page); i++) {
  374. if (atomic_read(&page[i]._mapcount) >= 0)
  375. return true;
  376. }
  377. return false;
  378. }
  379. EXPORT_SYMBOL(page_mapped);
  380. struct anon_vma *page_anon_vma(struct page *page)
  381. {
  382. unsigned long mapping;
  383. page = compound_head(page);
  384. mapping = (unsigned long)page->mapping;
  385. if ((mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
  386. return NULL;
  387. return __page_rmapping(page);
  388. }
  389. struct address_space *page_mapping(struct page *page)
  390. {
  391. struct address_space *mapping;
  392. page = compound_head(page);
  393. /* This happens if someone calls flush_dcache_page on slab page */
  394. if (unlikely(PageSlab(page)))
  395. return NULL;
  396. if (unlikely(PageSwapCache(page))) {
  397. swp_entry_t entry;
  398. entry.val = page_private(page);
  399. return swap_address_space(entry);
  400. }
  401. mapping = page->mapping;
  402. if ((unsigned long)mapping & PAGE_MAPPING_ANON)
  403. return NULL;
  404. return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
  405. }
  406. EXPORT_SYMBOL(page_mapping);
  407. /* Slow path of page_mapcount() for compound pages */
  408. int __page_mapcount(struct page *page)
  409. {
  410. int ret;
  411. ret = atomic_read(&page->_mapcount) + 1;
  412. /*
  413. * For file THP page->_mapcount contains total number of mapping
  414. * of the page: no need to look into compound_mapcount.
  415. */
  416. if (!PageAnon(page) && !PageHuge(page))
  417. return ret;
  418. page = compound_head(page);
  419. ret += atomic_read(compound_mapcount_ptr(page)) + 1;
  420. if (PageDoubleMap(page))
  421. ret--;
  422. return ret;
  423. }
  424. EXPORT_SYMBOL_GPL(__page_mapcount);
  425. int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
  426. int sysctl_overcommit_ratio __read_mostly = 50;
  427. unsigned long sysctl_overcommit_kbytes __read_mostly;
  428. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  429. unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
  430. unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
  431. int overcommit_ratio_handler(struct ctl_table *table, int write,
  432. void __user *buffer, size_t *lenp,
  433. loff_t *ppos)
  434. {
  435. int ret;
  436. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  437. if (ret == 0 && write)
  438. sysctl_overcommit_kbytes = 0;
  439. return ret;
  440. }
  441. int overcommit_kbytes_handler(struct ctl_table *table, int write,
  442. void __user *buffer, size_t *lenp,
  443. loff_t *ppos)
  444. {
  445. int ret;
  446. ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  447. if (ret == 0 && write)
  448. sysctl_overcommit_ratio = 0;
  449. return ret;
  450. }
  451. /*
  452. * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
  453. */
  454. unsigned long vm_commit_limit(void)
  455. {
  456. unsigned long allowed;
  457. if (sysctl_overcommit_kbytes)
  458. allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
  459. else
  460. allowed = ((totalram_pages - hugetlb_total_pages())
  461. * sysctl_overcommit_ratio / 100);
  462. allowed += total_swap_pages;
  463. return allowed;
  464. }
  465. /*
  466. * Make sure vm_committed_as in one cacheline and not cacheline shared with
  467. * other variables. It can be updated by several CPUs frequently.
  468. */
  469. struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
  470. /*
  471. * The global memory commitment made in the system can be a metric
  472. * that can be used to drive ballooning decisions when Linux is hosted
  473. * as a guest. On Hyper-V, the host implements a policy engine for dynamically
  474. * balancing memory across competing virtual machines that are hosted.
  475. * Several metrics drive this policy engine including the guest reported
  476. * memory commitment.
  477. */
  478. unsigned long vm_memory_committed(void)
  479. {
  480. return percpu_counter_read_positive(&vm_committed_as);
  481. }
  482. EXPORT_SYMBOL_GPL(vm_memory_committed);
  483. /*
  484. * Check that a process has enough memory to allocate a new virtual
  485. * mapping. 0 means there is enough memory for the allocation to
  486. * succeed and -ENOMEM implies there is not.
  487. *
  488. * We currently support three overcommit policies, which are set via the
  489. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  490. *
  491. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  492. * Additional code 2002 Jul 20 by Robert Love.
  493. *
  494. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  495. *
  496. * Note this is a helper function intended to be used by LSMs which
  497. * wish to use this logic.
  498. */
  499. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  500. {
  501. long free, allowed, reserve;
  502. VM_WARN_ONCE(percpu_counter_read(&vm_committed_as) <
  503. -(s64)vm_committed_as_batch * num_online_cpus(),
  504. "memory commitment underflow");
  505. vm_acct_memory(pages);
  506. /*
  507. * Sometimes we want to use more memory than we have
  508. */
  509. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  510. return 0;
  511. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  512. free = global_page_state(NR_FREE_PAGES);
  513. free += global_node_page_state(NR_FILE_PAGES);
  514. /*
  515. * shmem pages shouldn't be counted as free in this
  516. * case, they can't be purged, only swapped out, and
  517. * that won't affect the overall amount of available
  518. * memory in the system.
  519. */
  520. free -= global_node_page_state(NR_SHMEM);
  521. free += get_nr_swap_pages();
  522. /*
  523. * Any slabs which are created with the
  524. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  525. * which are reclaimable, under pressure. The dentry
  526. * cache and most inode caches should fall into this
  527. */
  528. free += global_page_state(NR_SLAB_RECLAIMABLE);
  529. /*
  530. * Leave reserved pages. The pages are not for anonymous pages.
  531. */
  532. if (free <= totalreserve_pages)
  533. goto error;
  534. else
  535. free -= totalreserve_pages;
  536. /*
  537. * Reserve some for root
  538. */
  539. if (!cap_sys_admin)
  540. free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
  541. if (free > pages)
  542. return 0;
  543. goto error;
  544. }
  545. allowed = vm_commit_limit();
  546. /*
  547. * Reserve some for root
  548. */
  549. if (!cap_sys_admin)
  550. allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
  551. /*
  552. * Don't let a single process grow so big a user can't recover
  553. */
  554. if (mm) {
  555. reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
  556. allowed -= min_t(long, mm->total_vm / 32, reserve);
  557. }
  558. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  559. return 0;
  560. error:
  561. vm_unacct_memory(pages);
  562. return -ENOMEM;
  563. }
  564. /**
  565. * get_cmdline() - copy the cmdline value to a buffer.
  566. * @task: the task whose cmdline value to copy.
  567. * @buffer: the buffer to copy to.
  568. * @buflen: the length of the buffer. Larger cmdline values are truncated
  569. * to this length.
  570. * Returns the size of the cmdline field copied. Note that the copy does
  571. * not guarantee an ending NULL byte.
  572. */
  573. int get_cmdline(struct task_struct *task, char *buffer, int buflen)
  574. {
  575. int res = 0;
  576. unsigned int len;
  577. struct mm_struct *mm = get_task_mm(task);
  578. unsigned long arg_start, arg_end, env_start, env_end;
  579. if (!mm)
  580. goto out;
  581. if (!mm->arg_end)
  582. goto out_mm; /* Shh! No looking before we're done */
  583. down_read(&mm->mmap_sem);
  584. arg_start = mm->arg_start;
  585. arg_end = mm->arg_end;
  586. env_start = mm->env_start;
  587. env_end = mm->env_end;
  588. up_read(&mm->mmap_sem);
  589. len = arg_end - arg_start;
  590. if (len > buflen)
  591. len = buflen;
  592. res = access_process_vm(task, arg_start, buffer, len, FOLL_FORCE);
  593. /*
  594. * If the nul at the end of args has been overwritten, then
  595. * assume application is using setproctitle(3).
  596. */
  597. if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
  598. len = strnlen(buffer, res);
  599. if (len < res) {
  600. res = len;
  601. } else {
  602. len = env_end - env_start;
  603. if (len > buflen - res)
  604. len = buflen - res;
  605. res += access_process_vm(task, env_start,
  606. buffer+res, len,
  607. FOLL_FORCE);
  608. res = strnlen(buffer, res);
  609. }
  610. }
  611. out_mm:
  612. mmput(mm);
  613. out:
  614. return res;
  615. }