util.c 20 KB

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