track memory used by balloon drivers
Subsystems (11)
v2 cover track memory used by balloon drivers
This series introduces a way to track memory used by balloon drivers.
Add a NR_BALLOON_PAGES counter to track how many pages are reclaimed by the balloon drivers. First add the accounting, then updates the balloon drivers (virtio, Hyper-V, VMware, Pseries-cmm, and Xen) to maintain this counter. The virtio, Vmware, and pseries-cmm balloon drivers utilize the balloon_compaction interface to allocate and free balloon pages. Other balloon drivers will have to maintain this counter manually.
This makes the information visible in memory reporting interfaces like /proc/meminfo, show_mem, and OOM reporting.
This provides admins visibility into their VM balloon sizes without requiring different virtualization tooling. Furthermore, this information is helpful when debugging an OOM inside a VM.
V2 changes:
- Add counter to the balloon_compaction interface
- Dropped patches for virtio and VMware as they use balloon_compaction interface
Tested: virtio_balloon, run stress-ng, inflate balloon, oom prints
Nico Pache (4):
meminfo: add a per node counter for balloon drivers
balloon_compaction: update the NR_BALLOON_PAGES state
hv_balloon: update the NR_BALLOON_PAGES state
xen: balloon: update the NR_BALLOON_PAGES state
drivers/hv/hv_balloon.c | 2 ++ drivers/xen/balloon.c | 4 ++++ fs/proc/meminfo.c | 2 ++ include/linux/mmzone.h | 1 + mm/balloon_compaction.c | 2 ++ mm/show_mem.c | 4 +++- mm/vmstat.c | 1 + 7 files changed, 15 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1
PATCH v2 1/4 meminfo: add a per node counter for balloon drivers
Add NR_BALLOON_PAGES counter to track memory used by balloon drivers and expose it through /proc/meminfo and other memory reporting interfaces.
--- fs/proc/meminfo.c | 2 ++ include/linux/mmzone.h | 1 + mm/show_mem.c | 4 +++- mm/vmstat.c | 1 + 4 files changed, 7 insertions(+), 1 deletion(-)
fs/proc/meminfo.c +2 -0
@@ -162,6 +162,8 @@ static int meminfo_proc_show(struct seq_file *m, void *v)show_val_kb (m,"Unaccepted: " ,global_zone_page_state (NR_UNACCEPTED ));#endif +show_val_kb (m,"Balloon: " , +global_node_page_state (NR_BALLOON_PAGES ));hugetlb_report_meminfo (m);
include/linux/mmzone.h +1 -0
@@ -223,6 +223,7 @@ enum node_stat_item {#ifdef CONFIG_HUGETLB_PAGE NR_HUGETLB , #endif +NR_BALLOON_PAGES ,NR_VM_NODE_STAT_ITEMS };
mm/show_mem.c +3 -1
@@ -260,6 +260,7 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z" pagetables:%lukB" " sec_pagetables:%lukB" " all_unreclaimable? %s" +" Balloon:%lukB" "\n" , pgdat->node_id, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
@@ -285,7 +286,8 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z#endif K (node_page_state (pgdat,NR_PAGETABLE )),K (node_page_state (pgdat,NR_SECONDARY_PAGETABLE )), - str_yes_no(pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)); +str_yes_no (pgdat->kswapd_failures >=MAX_RECLAIM_RETRIES ), + K(node_page_state(pgdat, NR_BALLOON_PAGES))); }for_each_populated_zone (zone) {
mm/vmstat.c +1 -0
@@ -1276,6 +1276,7 @@ const char * const vmstat_text[] = {#ifdef CONFIG_HUGETLB_PAGE "nr_hugetlb" , #endif +"nr_balloon_pages" ,/* system-wide enum vm_stat_item counters */ "nr_dirty_threshold" , "nr_dirty_background_threshold",
Signature · 1 line
2.48.1
Andrew Morton
maintainerRe: [PATCH v2 1/4] meminfo: add a per node counter for balloon drivers
fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, void *v) · line 166
Quoted 13 lines · On Fri, 14 Mar 2025 15:37:54 -0600 Nico Pache <[email protected]> wrote:
Add
NR_BALLOON_PAGEScounter to track memory used by balloon drivers and expose it through/proc/meminfoand other memory reporting interfaces....
fs/proc/meminfo.c +2
-0@@ -162,6 +162,8 @@ static int meminfo_proc_show(struct seq_file *m, void *v)show_val_kb (m,"Unaccepted: " ,global_zone_page_state (NR_UNACCEPTED ));#endif +show_val_kb (m,"Balloon: " , +global_node_page_state (NR_BALLOON_PAGES ));
Please update Documentation/filesystems/proc.rst for this.
Re: [PATCH v2 1/4] meminfo: add a per node counter for balloon drivers
Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Fri, 14 Mar 2025 15:37:54 -0600 Nico Pache <[email protected]> wrote:
Add
NR_BALLOON_PAGEScounter to track memory used by balloon drivers and expose it through/proc/meminfoand other memory reporting interfaces....
Quoted 10 lines
fs/proc/meminfo.c static int meminfo_proc_show(struct seq_file *m, void *v) · line 166
Quoted 8 lines
fs/proc/meminfo.c +2
-0@@ -162,6 +162,8 @@ static int meminfo_proc_show(struct seq_file *m, void *v)show_val_kb (m,"Unaccepted: " ,global_zone_page_state (NR_UNACCEPTED ));#endif +show_val_kb (m,"Balloon: " , +global_node_page_state (NR_BALLOON_PAGES ));Please update
Documentation/filesystems/proc.rstfor this.
@Andrew
Can you please squash the following?
From b1b379a32752e64c60b5e3b6365c93db8e1daf9f Mon Sep 17 00:00:00 2001
From: Nico Pache <[email protected]>
Date: Mon, 17 Mar 2025 16:07:18 -0600
Subject: [PATCH] Documentation: document Balloon Meminfo entry
Add a Balloon entry to the Meminfo documention.
--- Documentation/filesystems/proc.rst | 3 +++ 1 file changed, 3 insertions(+)
Documentation/filesystems/proc.rst +3 -0
@@ -1060,6 +1060,7 @@ Example output. You may not have all of these fields. FilePmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB + Balloon: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0
@@ -1228,6 +1229,8 @@ CmaTotal Memory reserved for the Contiguous Memory Allocator (CMA) CmaFree Free remaining memory in the CMA reserves +Balloon + Memory returned to Host by VM Balloon Drivers HugePages_Total, HugePages_Free, HugePages_Rsvd, HugePages_Surp, Hugepagesize,
Hugetlb
See Documentation/admin-guide/mm/hugetlbpage.rst.
DirectMap4k, DirectMap2M, DirectMap1G
Signature · 1 line
2.48.1
Quoted 1 line
PATCH v2 2/4 balloon_compaction: update the NR_BALLOON_PAGES state
In reply to Nico PacheUpdate the NR_BALLOON_PAGES counter when pages are added or removed using the balloon compaction interface.
The virtio, Vmware, and pseries-cmm balloon drivers utilize the balloon_compaction interface to allocate and free balloon pages. Other balloon drivers will have to maintain this counter manually.
--- mm/balloon_compaction.c | 2 ++ 1 file changed, 2 insertions(+)
mm/balloon_compaction.c +2 -0
@@ -24,6 +24,7 @@ static void balloon_page_enqueue_one(struct balloon_dev_info *b_dev_info,balloon_page_insert (b_dev_info, page);unlock_page (page);__count_vm_event (BALLOON_INFLATE ); +inc_node_page_state (page,NR_BALLOON_PAGES ); }/**
@@ -103,6 +104,7 @@ size_t balloon_page_list_dequeue(struct balloon_dev_info *b_dev_info,__count_vm_event (BALLOON_DEFLATE );list_add (&page->lru, pages);unlock_page (page); +dec_node_page_state (page,NR_BALLOON_PAGES ); n_pages++; }spin_unlock_irqrestore (&b_dev_info->pages_lock, flags);
Signature · 1 line
2.48.1
PATCH v2 3/4 hv_balloon: update the NR_BALLOON_PAGES state
In reply to Nico PacheUpdate the NR_BALLOON_PAGES counter when pages are added to or removed from the Hyper-V balloon.
--- drivers/hv/hv_balloon.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/hv/hv_balloon.c +2 -0
@@ -1192,6 +1192,7 @@ static void free_balloon_pages(struct hv_dynmem_device *dm,__ClearPageOffline (pg);__free_page (pg); dm->num_pages_ballooned--; +mod_node_page_state (page_pgdat (pg),NR_BALLOON_PAGES ,-1 );adjust_managed_page_count (pg,1 ); } }
@@ -1221,6 +1222,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,return i * alloc_unit; dm->num_pages_ballooned += alloc_unit; +mod_node_page_state (page_pgdat (pg),NR_BALLOON_PAGES , alloc_unit);/* * If we allocatted 2M pages; split them so we
Signature · 1 line
2.48.1
RE: [PATCH v2 3/4] hv_balloon: update the NR_BALLOON_PAGES state
From: Nico Pache <[email protected]> Sent: Friday, March 14, 2025 2:38 PM
Quoted 30 lines
Update the
NR_BALLOON_PAGEScounter when pages are added to or removed from the Hyper-V balloon.Signed-off-by Nico Pache <[email protected]>--- drivers/hv/hv_balloon.c | 2 ++ 1 file changed, 2 insertions(+)drivers/hv/hv_balloon.c +2
-1@@ -1192,6 +1192,7 @@ static void free_balloon_pages(struct hv_dynmem_device *dm,__ClearPageOffline (pg);__free_page (pg); dm->num_pages_ballooned--; +mod_node_page_state (page_pgdat (pg),NR_BALLOON_PAGES ,-1 );adjust_managed_page_count (pg,1 ); } }@@ -1221,6 +1222,7 @@ static unsigned int alloc_balloon_pages(struct hv_dynmem_device *dm,return i * alloc_unit; dm->num_pages_ballooned += alloc_unit; +mod_node_page_state (page_pgdat (pg),NR_BALLOON_PAGES , alloc_unit);/* * If we allocatted 2M pages; split them so we --
Quoted 2 lines
2.48.1
As is evident from the code, the hv_balloon driver already has accounting for the number of pages that have been ballooned out of the VM. This accounting and other details of the hv_balloon driver state is available in /sys/kernel/debug/hv-balloon when CONFIG_DEBUGFS=y. But it seems OK to also report the # of pages ballooned through /proc/meminfo, which works even if CONFIG_DEBUGFS=n.
PATCH v2 4/4 xen: balloon: update the NR_BALLOON_PAGES state
In reply to Nico PacheUpdate the NR_BALLOON_PAGES counter when pages are added to or removed from the Xen balloon.
--- drivers/xen/balloon.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/xen/balloon.c +4 -0
@@ -157,6 +157,8 @@ static void balloon_append(struct page *page)list_add (&page->lru, &ballooned_pages); balloon_stats.balloon_low++; } +inc_node_page_state (page,NR_BALLOON_PAGES ); +wake_up (&balloon_wq); }
@@ -179,6 +181,8 @@ static struct page *balloon_retrieve(bool require_lowmem) balloon_stats.balloon_low--;__ClearPageOffline (page); +dec_node_page_state (page,NR_BALLOON_PAGES ); +return page; }
Signature · 1 line
2.48.1
Re: [PATCH v2 4/4] xen: balloon: update the NR_BALLOON_PAGES state
Quoted 4 lines · Nico Pache
Update the
NR_BALLOON_PAGEScounter when pages are added to or removed from the Xen balloon.Signed-off-by Nico Pache <[email protected]>
Juergen