Skip to content

Commit c15bef3

Browse files
Dmitry Finktorvalds
authored andcommitted
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: Dmitry Fink <dmitry.fink@palm.com> Reviewed-by: Minchan Kim <minchan.kim@gmail.com> Acked-by: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c9d8c3d commit c15bef3

2 files changed

Lines changed: 26 additions & 42 deletions

File tree

‎mm/mmap.c‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,17 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
122122
return 0;
123123

124124
if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
125-
unsigned long n;
125+
free = global_page_state(NR_FREE_PAGES);
126+
free += global_page_state(NR_FILE_PAGES);
127+
128+
/*
129+
* shmem pages shouldn't be counted as free in this
130+
* case, they can't be purged, only swapped out, and
131+
* that won't affect the overall amount of available
132+
* memory in the system.
133+
*/
134+
free -= global_page_state(NR_SHMEM);
126135

127-
free = global_page_state(NR_FILE_PAGES);
128136
free += nr_swap_pages;
129137

130138
/*
@@ -135,35 +143,19 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
135143
*/
136144
free += global_page_state(NR_SLAB_RECLAIMABLE);
137145

138-
/*
139-
* Leave the last 3% for root
140-
*/
141-
if (!cap_sys_admin)
142-
free -= free / 32;
143-
144-
if (free > pages)
145-
return 0;
146-
147-
/*
148-
* nr_free_pages() is very expensive on large systems,
149-
* only call if we're about to fail.
150-
*/
151-
n = nr_free_pages();
152-
153146
/*
154147
* Leave reserved pages. The pages are not for anonymous pages.
155148
*/
156-
if (n <= totalreserve_pages)
149+
if (free <= totalreserve_pages)
157150
goto error;
158151
else
159-
n -= totalreserve_pages;
152+
free -= totalreserve_pages;
160153

161154
/*
162155
* Leave the last 3% for root
163156
*/
164157
if (!cap_sys_admin)
165-
n -= n / 32;
166-
free += n;
158+
free -= free / 32;
167159

168160
if (free > pages)
169161
return 0;

‎mm/nommu.c‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,9 +1884,17 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
18841884
return 0;
18851885

18861886
if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1887-
unsigned long n;
1887+
free = global_page_state(NR_FREE_PAGES);
1888+
free += global_page_state(NR_FILE_PAGES);
1889+
1890+
/*
1891+
* shmem pages shouldn't be counted as free in this
1892+
* case, they can't be purged, only swapped out, and
1893+
* that won't affect the overall amount of available
1894+
* memory in the system.
1895+
*/
1896+
free -= global_page_state(NR_SHMEM);
18881897

1889-
free = global_page_state(NR_FILE_PAGES);
18901898
free += nr_swap_pages;
18911899

18921900
/*
@@ -1897,35 +1905,19 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
18971905
*/
18981906
free += global_page_state(NR_SLAB_RECLAIMABLE);
18991907

1900-
/*
1901-
* Leave the last 3% for root
1902-
*/
1903-
if (!cap_sys_admin)
1904-
free -= free / 32;
1905-
1906-
if (free > pages)
1907-
return 0;
1908-
1909-
/*
1910-
* nr_free_pages() is very expensive on large systems,
1911-
* only call if we're about to fail.
1912-
*/
1913-
n = nr_free_pages();
1914-
19151908
/*
19161909
* Leave reserved pages. The pages are not for anonymous pages.
19171910
*/
1918-
if (n <= totalreserve_pages)
1911+
if (free <= totalreserve_pages)
19191912
goto error;
19201913
else
1921-
n -= totalreserve_pages;
1914+
free -= totalreserve_pages;
19221915

19231916
/*
19241917
* Leave the last 3% for root
19251918
*/
19261919
if (!cap_sys_admin)
1927-
n -= n / 32;
1928-
free += n;
1920+
free -= free / 32;
19291921

19301922
if (free > pages)
19311923
return 0;

0 commit comments

Comments
 (0)