]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/page_alloc.c: calculate 'available' memory in a separate function
authorIgor Redko <redkoi@virtuozzo.com>
Thu, 17 Mar 2016 21:19:05 +0000 (14:19 -0700)
committerBrian Maly <brian.maly@oracle.com>
Wed, 7 Aug 2019 22:00:11 +0000 (18:00 -0400)
Add a new field, VIRTIO_BALLOON_S_AVAIL, to virtio_balloon memory
statistics protocol, corresponding to 'Available' in /proc/meminfo.

It indicates to the hypervisor how big the balloon can be inflated
without pushing the guest system to swap.  This metric would be very
useful in VM orchestration software to improve memory management of
different VMs under overcommit.

This patch (of 2):

Factor out calculation of the available memory counter into a separate
exportable function, in order to be able to use it in other parts of the
kernel.

In particular, it appears a relevant metric to report to the hypervisor
via virtio-balloon statistics interface (in a followup patch).

Signed-off-by: Igor Redko <redkoi@virtuozzo.com>
Signed-off-by: Denis V. Lunev <den@openvz.org>
Reviewed-by: Roman Kagan <rkagan@virtuozzo.com>
Cc: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit d02bd27bd33dd7e8d22594cd568b81be0cb584cd)

Orabug: 30073695

Need this to provide si_mem_available() for the next patch

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Reviewed-by: Patrick Colp <patrick.colp@oracle.com>
Signed-off-by: Brian Maly <brian.maly@oracle.com>
Conflicts:
fs/proc/meminfo.c: we don't have commit 84ad5802a33a ("proc:
        meminfo: estimate available memory more conservatively") and
        even though it looks reasonable and simple I didn't want to
        include it because this would change a user-visible attribute.

Signed-off-by: Brian Maly <brian.maly@oracle.com>
fs/proc/meminfo.c
include/linux/mm.h
mm/page_alloc.c

index d3ebf2e618535bd2fa7daa0bfe948cb1a9bfc062..826e5892d080d94a759d6b86e934c967d4d1cf96 100644 (file)
@@ -30,10 +30,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
        struct vmalloc_info vmi;
        long cached;
        long available;
-       unsigned long pagecache;
-       unsigned long wmark_low = 0;
        unsigned long pages[NR_LRU_LISTS];
-       struct zone *zone;
        int lru;
 
 /*
@@ -54,36 +51,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
        for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
                pages[lru] = global_page_state(NR_LRU_BASE + lru);
 
-       for_each_zone(zone)
-               wmark_low += zone->watermark[WMARK_LOW];
-
-       /*
-        * Estimate the amount of memory available for userspace allocations,
-        * without causing swapping.
-        *
-        * Free memory cannot be taken below the low watermark, before the
-        * system starts swapping.
-        */
-       available = i.freeram - wmark_low;
-
-       /*
-        * Not all the page cache can be freed, otherwise the system will
-        * start swapping. Assume at least half of the page cache, or the
-        * low watermark worth of cache, needs to stay.
-        */
-       pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
-       pagecache -= min(pagecache / 2, wmark_low);
-       available += pagecache;
-
-       /*
-        * Part of the reclaimable slab consists of items that are in use,
-        * and cannot be freed. Cap this estimate at the low watermark.
-        */
-       available += global_page_state(NR_SLAB_RECLAIMABLE) -
-                    min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low);
-
-       if (available < 0)
-               available = 0;
+       available = si_mem_available();
 
        /*
         * Tagged format, for easy grepping and expansion.
index 5befc80071edb0dac313f55817a0a17ad1d65559..34c8505646aca0b3e6403afac63f3e1dca97a983 100644 (file)
@@ -1804,6 +1804,7 @@ extern int __meminit init_per_zone_wmark_min(void);
 extern void mem_init(void);
 extern void __init mmap_init(void);
 extern void show_mem(unsigned int flags);
+extern long si_mem_available(void);
 extern void si_meminfo(struct sysinfo * val);
 extern void si_meminfo_node(struct sysinfo *val, int nid);
 
index 79943256004b0e7ec81e05ad39f466c4364ea8a6..65d740c1cb640b29cc892232ce4a7673564a4767 100644 (file)
@@ -3175,6 +3175,52 @@ static inline void show_node(struct zone *zone)
                printk("Node %d ", zone_to_nid(zone));
 }
 
+long si_mem_available(void)
+{
+       long available;
+       unsigned long pagecache;
+       unsigned long wmark_low = 0;
+       unsigned long pages[NR_LRU_LISTS];
+       struct zone *zone;
+       int lru;
+
+       for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
+               pages[lru] = global_page_state(NR_LRU_BASE + lru);
+
+       for_each_zone(zone)
+               wmark_low += zone->watermark[WMARK_LOW];
+
+       /*
+        * Estimate the amount of memory available for userspace allocations,
+        * without causing swapping.
+        *
+        * Free memory cannot be taken below the low watermark, before the
+        * ystem starts swapping.
+        */
+       available = global_page_state(NR_FREE_PAGES) - wmark_low;
+
+       /*
+        * Not all the page cache can be freed, otherwise the system will
+        * start swapping. Assume at least half of the page cache, or the
+        * low watermark worth of cache, needs to stay.
+        */
+       pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
+       pagecache -= min(pagecache / 2, wmark_low);
+       available += pagecache;
+
+       /*
+        * Part of the reclaimable slab consists of items that are in use,
+        * and cannot be freed. Cap this estimate at the low watermark.
+        */
+       available += global_page_state(NR_SLAB_RECLAIMABLE) -
+                    min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low);
+
+       if (available < 0)
+               available = 0;
+       return available;
+}
+EXPORT_SYMBOL_GPL(si_mem_available);
+
 void si_meminfo(struct sysinfo *val)
 {
        val->totalram = totalram_pages;