[PATCH] xen/grant-table: fix resource leak in gnttab_init()

Haoxiang Li posted 1 patch 6 days, 13 hours ago
There is a newer version of this series
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(-)
[PATCH] xen/grant-table: fix resource leak in gnttab_init()
Posted by Haoxiang Li 6 days, 13 hours ago
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@163.com>
---
 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
Re: [PATCH] xen/grant-table: fix resource leak in gnttab_init()
Posted by Edgecombe, Rick P 3 days, 23 hours ago
On Tue, 2026-11-17 at 16:43 +0800, Haoxiang Li wrote:
> 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@163.com>
> ---

If the PV guest hits this scenario can it survive? I don't know xen pv very
well. Does this means that the PV guest has no disk or network. If so, the user
is probably not concerned with a one time memory leak.
Re: [PATCH] xen/grant-table: fix resource leak in gnttab_init()
Posted by Haoxiang Li 3 days, 16 hours ago

At 2026-11-20 07:18:03, "Edgecombe, Rick P" <rick.p.edgecombe@intel.com> wrote:
>
>If the PV guest hits this scenario can it survive? I don't know xen pv very
>well. Does this means that the PV guest has no disk or network. If so, the user
>is probably not concerned with a one time memory leak.

Thanks for raising this. I tested an x86-64 PV Dom0 under Xen 4.17.7 in QEMU,
using ata_piix for storage and e1000 for networking.

I injected -ENOMEM immediately before the initial MFN-array allocation in
gnttab_map(), after arch_gnttab_init() had succeeded. With and without the
patch, gnttab_init() returned -ENODEV, but Dom0 still booted to userspace.
In both cases, disk I/O worked, and the network interface could reach the
QEMU gateway.

However, I agree that this is a one-time leak with limited impact. I have not
demonstrated any further user-visible consequences from the leak.

Regards,
Haoxiang Li



Re: [PATCH] xen/grant-table: fix resource leak in gnttab_init()
Posted by Edgecombe, Rick P 3 days ago
On Fri, 2026-11-20 at 13:56 +0800, Haoxiang Li wrote:
> 
> At 2026-11-20 07:18:03, "Edgecombe, Rick P" <rick.p.edgecombe@intel.com> wrote:
> > 
> > If the PV guest hits this scenario can it survive? I don't know xen pv very
> > well. Does this means that the PV guest has no disk or network. If so, the user
> > is probably not concerned with a one time memory leak.
> 
> Thanks for raising this. I tested an x86-64 PV Dom0 under Xen 4.17.7 in QEMU,
> using ata_piix for storage and e1000 for networking.

Ah ok, in dom0. (not xen person here)

> 
> I injected -ENOMEM immediately before the initial MFN-array allocation in
> gnttab_map(), after arch_gnttab_init() had succeeded. With and without the
> patch, gnttab_init() returned -ENODEV, but Dom0 still booted to userspace.
> In both cases, disk I/O worked, and the network interface could reach the
> QEMU gateway.
> 
> However, I agree that this is a one-time leak with limited impact. I have not
> demonstrated any further user-visible consequences from the leak.

Not that it's impossible, but at boot we could expect allocations to be less
likely to fail.

I guess since you injected the error you didn't hit this in practice? If so, I'd
think this would be a pretty low priority fix. The combo of rare scenario and
close to zero user impact. Fine to be corrected though.
Re: [PATCH] xen/grant-table: fix resource leak in gnttab_init()
Posted by Haoxiang Li 2 days, 15 hours ago
At 2026-11-21 06:06:58, "Edgecombe, Rick P" <rick.p.edgecombe@intel.com> wrote:
>
>I guess since you injected the error you didn't hit this in practice? If so, I'd
>think this would be a pretty low priority fix. The combo of rare scenario and
>close to zero user impact. Fine to be corrected though.

I agree that this failure is unlikely to happen. I found the missing cleanup using a
static analysis tool and check the leak with fault injection. But hard to prove
that it will happen in normal userspace.

Regards,
Haoxiang Li