Commit c15bef30 authored by Dmitry Fink's avatar Dmitry Fink Committed by Linus Torvalds

mmap: fix and tidy up overcommit page arithmetic

- shmem pages are not immediately available, but they are not
  potentially available either, even if we swap them out, they will just
  relocate from memory into swap, total amount of immediate and
  potentially available memory is not going to be affected, so we
  shouldn't count them as potentially free in the first place.

- nr_free_pages() is not an expensive operation anymore, there is no
  need to split the decision making in two halves and repeat code.
Signed-off-by: default avatarDmitry Fink <dmitry.fink@palm.com>
Reviewed-by: default avatarMinchan Kim <minchan.kim@gmail.com>
Acked-by: default avatarHugh Dickins <hughd@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent c9d8c3d0
...@@ -122,9 +122,17 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) ...@@ -122,9 +122,17 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
return 0; return 0;
if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
unsigned long n; free = global_page_state(NR_FREE_PAGES);
free += global_page_state(NR_FILE_PAGES);
/*
* shmem pages shouldn't be counted as free in this
* case, they can't be purged, only swapped out, and
* that won't affect the overall amount of available
* memory in the system.
*/
free -= global_page_state(NR_SHMEM);
free = global_page_state(NR_FILE_PAGES);
free += nr_swap_pages; free += nr_swap_pages;
/* /*
...@@ -135,35 +143,19 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) ...@@ -135,35 +143,19 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
*/ */
free += global_page_state(NR_SLAB_RECLAIMABLE); free += global_page_state(NR_SLAB_RECLAIMABLE);
/*
* Leave the last 3% for root
*/
if (!cap_sys_admin)
free -= free / 32;
if (free > pages)
return 0;
/*
* nr_free_pages() is very expensive on large systems,
* only call if we're about to fail.
*/
n = nr_free_pages();
/* /*
* Leave reserved pages. The pages are not for anonymous pages. * Leave reserved pages. The pages are not for anonymous pages.
*/ */
if (n <= totalreserve_pages) if (free <= totalreserve_pages)
goto error; goto error;
else else
n -= totalreserve_pages; free -= totalreserve_pages;
/* /*
* Leave the last 3% for root * Leave the last 3% for root
*/ */
if (!cap_sys_admin) if (!cap_sys_admin)
n -= n / 32; free -= free / 32;
free += n;
if (free > pages) if (free > pages)
return 0; return 0;
......
...@@ -1884,9 +1884,17 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) ...@@ -1884,9 +1884,17 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
return 0; return 0;
if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
unsigned long n; free = global_page_state(NR_FREE_PAGES);
free += global_page_state(NR_FILE_PAGES);
/*
* shmem pages shouldn't be counted as free in this
* case, they can't be purged, only swapped out, and
* that won't affect the overall amount of available
* memory in the system.
*/
free -= global_page_state(NR_SHMEM);
free = global_page_state(NR_FILE_PAGES);
free += nr_swap_pages; free += nr_swap_pages;
/* /*
...@@ -1897,35 +1905,19 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) ...@@ -1897,35 +1905,19 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
*/ */
free += global_page_state(NR_SLAB_RECLAIMABLE); free += global_page_state(NR_SLAB_RECLAIMABLE);
/*
* Leave the last 3% for root
*/
if (!cap_sys_admin)
free -= free / 32;
if (free > pages)
return 0;
/*
* nr_free_pages() is very expensive on large systems,
* only call if we're about to fail.
*/
n = nr_free_pages();
/* /*
* Leave reserved pages. The pages are not for anonymous pages. * Leave reserved pages. The pages are not for anonymous pages.
*/ */
if (n <= totalreserve_pages) if (free <= totalreserve_pages)
goto error; goto error;
else else
n -= totalreserve_pages; free -= totalreserve_pages;
/* /*
* Leave the last 3% for root * Leave the last 3% for root
*/ */
if (!cap_sys_admin) if (!cap_sys_admin)
n -= n / 32; free -= free / 32;
free += n;
if (free > pages) if (free > pages)
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment