From c7f6fcf55b7ce3cf61e0953a18e0ab9e70421a47 Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Tue, 3 Feb 2004 18:49:05 -0800 Subject: [PATCH] [PATCH] rate limit nr_free_pages From: Jes Sorensen nr_free_pages() is expensive, especially on large SMP machines. The patch changes the memory overcommit code so that it only calls nr_free_pages() is we're about to fail the allocation attempt. --- security/commoncap.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/security/commoncap.c b/security/commoncap.c index 355533d68b914..f80b1a17bbe4b 100644 --- a/security/commoncap.c +++ b/security/commoncap.c @@ -321,8 +321,9 @@ int cap_vm_enough_memory(long pages) return 0; if (sysctl_overcommit_memory == 0) { + unsigned long n; + free = get_page_cache_size(); - free += nr_free_pages(); free += nr_swap_pages; /* @@ -339,6 +340,18 @@ int cap_vm_enough_memory(long pages) if (!capable(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(); + if (!capable(CAP_SYS_ADMIN)) + n -= n / 32; + free += n; + if (free > pages) return 0; vm_unacct_memory(pages); -- 2.53.0