From 216adf28ee621f5583346bd233f35772ed081b66 Mon Sep 17 00:00:00 2001 From: Elliot Berman Date: Mon, 10 Feb 2025 13:49:26 -0800 Subject: [PATCH] ANDROID: virt: gunyah: Allow host to reclaim memory provided to the guest Introduce an ioctl, GH_VM_RECLAIM_REGION which attempts to reclaim guest memory. If the guest hasn't relinquished the memory, an error is returned. Bug: 395833312 Change-Id: I005c947435f0256f6200c0d7d86eea145e1daa0a Signed-off-by: Elliot Berman --- drivers/virt/gunyah/vm_mgr.c | 16 ++++++++++++++++ include/uapi/linux/gunyah.h | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c index 5d9b4576fe30..3b3f418a3c89 100644 --- a/drivers/virt/gunyah/vm_mgr.c +++ b/drivers/virt/gunyah/vm_mgr.c @@ -923,6 +923,22 @@ static long gunyah_vm_ioctl(struct file *filp, unsigned int cmd, r = gunyah_vm_binding_alloc(ghvm, ®ion, lend); break; } + case GH_VM_RECLAIM_REGION: { + struct gunyah_address_range range; + + /* only allow owner task to remove memory */ + if (ghvm->mm_s != current->mm) + return -EPERM; + if (copy_from_user(&range, argp, sizeof(range))) + return -EFAULT; + if (!PAGE_ALIGNED(range.size) || !PAGE_ALIGNED(range.guest_phys_addr)) + return -EINVAL; + + r = gunyah_vm_reclaim_range(ghvm, + gunyah_gpa_to_gfn(range.guest_phys_addr), + gunyah_gpa_to_gfn(range.size) - 1); + break; + } case GUNYAH_VM_SET_BOOT_CONTEXT: { struct gunyah_vm_boot_context boot_ctx; diff --git a/include/uapi/linux/gunyah.h b/include/uapi/linux/gunyah.h index 93990b20ff34..1140b5742e84 100644 --- a/include/uapi/linux/gunyah.h +++ b/include/uapi/linux/gunyah.h @@ -387,4 +387,13 @@ struct gunyah_vm_firmware_config { #define GH_VM_ANDROID_SET_FW_CONFIG _IOW(GH_ANDROID_IOCTL_TYPE, 0x12, \ struct gunyah_vm_firmware_config) + +struct gunyah_address_range { + __u64 guest_phys_addr; + __u64 size; +}; + +#define GH_VM_RECLAIM_REGION _IOW(GH_ANDROID_IOCTL_TYPE, 0x13, \ + struct gunyah_address_range) + #endif