util.c 20 KB

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