[PATCH] xen/grant-table: fix resource leak in gnttab_init()
From: Haoxiang Li
Date: Tue Nov 17 2026 - 03:45:14 EST
For x86 PV guests, arch_gnttab_init() allocates virtual address areas
and PTE pointer arrays for the shared and status frames. These
resources are not released if the subsequent gnttab_setup() call fails.
Add arch_gnttab_free() and call it on this failure path before the
existing cleanup. Provide an empty implementation for ARM, which
is also used by ARM64.
Fixes: 162e37171276 ("x86/xen: safely map and unmap grant frames when in atomic context")
Signed-off-by: Haoxiang Li <haoxiang_li2024@xxxxxxx>
---
arch/arm/xen/grant-table.c | 4 ++++
arch/x86/xen/grant-table.c | 9 +++++++++
drivers/xen/grant-table.c | 5 ++++-
include/xen/grant_table.h | 1 +
4 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/arch/arm/xen/grant-table.c b/arch/arm/xen/grant-table.c
index 91cf08ba1e95..69db80877bb5 100644
--- a/arch/arm/xen/grant-table.c
+++ b/arch/arm/xen/grant-table.c
@@ -56,3 +56,7 @@ int arch_gnttab_init(unsigned long nr_shared, unsigned long nr_status)
{
return 0;
}
+
+void arch_gnttab_free(void)
+{
+}
diff --git a/arch/x86/xen/grant-table.c b/arch/x86/xen/grant-table.c
index 5f4060b5a40d..0a89e43d804d 100644
--- a/arch/x86/xen/grant-table.c
+++ b/arch/x86/xen/grant-table.c
@@ -149,6 +149,15 @@ int arch_gnttab_init(unsigned long nr_shared, unsigned long nr_status)
return -ENOMEM;
}
+void arch_gnttab_free(void)
+{
+ if (!xen_pv_domain())
+ return;
+
+ arch_gnttab_vfree(&gnttab_shared_vm_area);
+ arch_gnttab_vfree(&gnttab_status_vm_area);
+}
+
#ifdef CONFIG_XEN_PVH
#include <xen/events.h>
#include <xen/xen-ops.h>
diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c
index 957500dd309a..fc18fe4cf7a3 100644
--- a/drivers/xen/grant-table.c
+++ b/drivers/xen/grant-table.c
@@ -1696,7 +1696,7 @@ int gnttab_init(void)
if (gnttab_setup() < 0) {
ret = -ENODEV;
- goto ini_nomem;
+ goto free_gnttab;
}
gnttab_size = nr_grant_frames * gnttab_interface->grefs_per_grant_frame;
@@ -1707,6 +1707,9 @@ int gnttab_init(void)
printk("Grant table initialized\n");
return 0;
+ free_gnttab:
+ arch_gnttab_free();
+
ini_nomem:
for (i--; i >= 0; i--)
free_page((unsigned long)gnttab_list[i]);
diff --git a/include/xen/grant_table.h b/include/xen/grant_table.h
index a33a60a2ea72..a635cadb61f1 100644
--- a/include/xen/grant_table.h
+++ b/include/xen/grant_table.h
@@ -203,6 +203,7 @@ gnttab_set_unmap_op(struct gnttab_unmap_grant_ref *unmap, phys_addr_t addr,
}
int arch_gnttab_init(unsigned long nr_shared, unsigned long nr_status);
+void arch_gnttab_free(void);
int arch_gnttab_map_shared(xen_pfn_t *frames, unsigned long nr_gframes,
unsigned long max_nr_gframes,
void **__shared);
--
2.25.1