[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250424141341.841734-25-karim.manaouil@linaro.org>
Date: Thu, 24 Apr 2025 15:13:31 +0100
From: Karim Manaouil <karim.manaouil@...aro.org>
To: linux-kernel@...r.kernel.org,
kvm@...r.kernel.org,
linux-arm-msm@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org,
kvmarm@...ts.linux.dev
Cc: Karim Manaouil <karim.manaouil@...aro.org>,
Alexander Graf <graf@...zon.com>,
Alex Elder <elder@...nel.org>,
Catalin Marinas <catalin.marinas@....com>,
Fuad Tabba <tabba@...gle.com>,
Joey Gouly <joey.gouly@....com>,
Jonathan Corbet <corbet@....net>,
Marc Zyngier <maz@...nel.org>,
Mark Brown <broonie@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Oliver Upton <oliver.upton@...ux.dev>,
Paolo Bonzini <pbonzini@...hat.com>,
Prakruthi Deepak Heragu <quic_pheragu@...cinc.com>,
Quentin Perret <qperret@...gle.com>,
Rob Herring <robh@...nel.org>,
Srinivas Kandagatla <srini@...nel.org>,
Srivatsa Vaddagiri <quic_svaddagi@...cinc.com>,
Will Deacon <will@...nel.org>,
Haripranesh S <haripran@....qualcomm.com>,
Carl van Schaik <cvanscha@....qualcomm.com>,
Murali Nalajala <mnalajal@...cinc.com>,
Sreenivasulu Chalamcharla <sreeniva@....qualcomm.com>,
Trilok Soni <tsoni@...cinc.com>,
Stefan Schmidt <stefan.schmidt@...aro.org>,
Elliot Berman <quic_eberman@...cinc.com>
Subject: [RFC PATCH 24/34] gunyah: Add platform ops on mem_lend/mem_reclaim
From: Elliot Berman <quic_eberman@...cinc.com>
On Qualcomm platforms, there is a firmware entity which controls access
to physical pages. In order to share memory with another VM, this entity
needs to be informed that the guest VM should have access to the memory.
Co-developed-by: Prakruthi Deepak Heragu <quic_pheragu@...cinc.com>
Signed-off-by: Prakruthi Deepak Heragu <quic_pheragu@...cinc.com>
Signed-off-by: Elliot Berman <quic_eberman@...cinc.com>
Signed-off-by: Karim Manaouil <karim.manaouil@...aro.org>
---
arch/arm64/kvm/gunyah.c | 29 ++++-
drivers/virt/gunyah/Kconfig | 4 +
drivers/virt/gunyah/Makefile | 1 +
drivers/virt/gunyah/gunyah_platform_hooks.c | 117 ++++++++++++++++++++
drivers/virt/gunyah/rsc_mgr_rpc.c | 19 +++-
include/linux/gunyah.h | 63 +++++++++--
include/linux/gunyah_rsc_mgr.h | 10 ++
7 files changed, 226 insertions(+), 17 deletions(-)
create mode 100644 drivers/virt/gunyah/gunyah_platform_hooks.c
diff --git a/arch/arm64/kvm/gunyah.c b/arch/arm64/kvm/gunyah.c
index 5d18d133df50..7216db642174 100644
--- a/arch/arm64/kvm/gunyah.c
+++ b/arch/arm64/kvm/gunyah.c
@@ -488,7 +488,7 @@ static int gunyah_memory_provide_folio(struct gunyah_vm *ghvm,
size_t size = folio_size(folio);
enum gunyah_error gunyah_error;
unsigned long tag = 0;
- int ret;
+ int ret, tmp;
if (share) {
guest_extent = __first_resource(&ghvm->guest_shared_extent_ticket);
@@ -521,6 +521,11 @@ static int gunyah_memory_provide_folio(struct gunyah_vm *ghvm,
else /* !share && !write */
access = GUNYAH_PAGETABLE_ACCESS_RX;
+ ret = gunyah_rm_platform_pre_demand_page(ghvm->rm, ghvm->vmid, access,
+ folio);
+ if (ret)
+ return ret;
+
gunyah_error = gunyah_hypercall_memextent_donate(donate_flags(share),
host_extent->capid,
guest_extent->capid,
@@ -528,7 +533,8 @@ static int gunyah_memory_provide_folio(struct gunyah_vm *ghvm,
if (gunyah_error != GUNYAH_ERROR_OK) {
pr_err("Failed to donate memory for guest address 0x%016llx: %d\n",
gpa, gunyah_error);
- return gunyah_error_remap(gunyah_error);
+ ret = gunyah_error_remap(gunyah_error);
+ goto platform_release;
}
extent_attrs =
@@ -556,6 +562,14 @@ static int gunyah_memory_provide_folio(struct gunyah_vm *ghvm,
if (gunyah_error != GUNYAH_ERROR_OK)
pr_err("Failed to reclaim memory donation for guest address 0x%016llx: %d\n",
gpa, gunyah_error);
+platform_release:
+ tmp = gunyah_rm_platform_reclaim_demand_page(ghvm->rm, ghvm->vmid,
+ access, folio);
+ if (tmp) {
+ pr_err("Platform failed to reclaim memory for guest address 0x%016llx: %d",
+ gpa, tmp);
+ return ret;
+ }
return ret;
}
@@ -565,6 +579,7 @@ static int gunyah_memory_reclaim_folio(struct gunyah_vm *ghvm,
u32 map_flags = BIT(GUNYAH_ADDRSPACE_MAP_FLAG_PARTIAL);
struct gunyah_resource *guest_extent, *host_extent, *addrspace;
enum gunyah_error gunyah_error;
+ enum gunyah_pagetable_access access;
phys_addr_t pa;
size_t size;
int ret;
@@ -604,6 +619,16 @@ static int gunyah_memory_reclaim_folio(struct gunyah_vm *ghvm,
goto err;
}
+ access = GUNYAH_PAGETABLE_ACCESS_RWX;
+
+ ret = gunyah_rm_platform_reclaim_demand_page(ghvm->rm, ghvm->vmid, access, folio);
+ if (ret) {
+ pr_err_ratelimited(
+ "Platform failed to reclaim memory for guest address 0x%016llx: %d",
+ gfn_to_gpa(gfn), ret);
+ goto err;
+ }
+
return 0;
err:
return ret;
diff --git a/drivers/virt/gunyah/Kconfig b/drivers/virt/gunyah/Kconfig
index 6f4c85db80b5..23ba523d25dc 100644
--- a/drivers/virt/gunyah/Kconfig
+++ b/drivers/virt/gunyah/Kconfig
@@ -3,6 +3,7 @@
config GUNYAH
tristate "Gunyah Virtualization drivers"
depends on ARM64
+ select GUNYAH_PLATFORM_HOOKS
help
The Gunyah drivers are the helper interfaces that run in a guest VM
such as basic inter-VM IPC and signaling mechanisms, and higher level
@@ -10,3 +11,6 @@ config GUNYAH
Say Y/M here to enable the drivers needed to interact in a Gunyah
virtual environment.
+
+config GUNYAH_PLATFORM_HOOKS
+ tristate
diff --git a/drivers/virt/gunyah/Makefile b/drivers/virt/gunyah/Makefile
index b1bdf3e84155..45cabba3110c 100644
--- a/drivers/virt/gunyah/Makefile
+++ b/drivers/virt/gunyah/Makefile
@@ -3,3 +3,4 @@
gunyah_rsc_mgr-y += rsc_mgr.o rsc_mgr_rpc.o
obj-$(CONFIG_GUNYAH) += gunyah.o gunyah_rsc_mgr.o
+obj-$(CONFIG_GUNYAH_PLATFORM_HOOKS) += gunyah_platform_hooks.o
diff --git a/drivers/virt/gunyah/gunyah_platform_hooks.c b/drivers/virt/gunyah/gunyah_platform_hooks.c
new file mode 100644
index 000000000000..8a1af171e4c9
--- /dev/null
+++ b/drivers/virt/gunyah/gunyah_platform_hooks.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#include <linux/device.h>
+#include <linux/gunyah.h>
+#include <linux/module.h>
+#include <linux/rwsem.h>
+
+#include <linux/gunyah_rsc_mgr.h>
+
+static const struct gunyah_rm_platform_ops *rm_platform_ops;
+static DECLARE_RWSEM(rm_platform_ops_lock);
+
+int gunyah_rm_platform_pre_mem_share(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->pre_mem_share)
+ ret = rm_platform_ops->pre_mem_share(rm, mem_parcel);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_pre_mem_share);
+
+int gunyah_rm_platform_post_mem_reclaim(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->post_mem_reclaim)
+ ret = rm_platform_ops->post_mem_reclaim(rm, mem_parcel);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_post_mem_reclaim);
+
+int gunyah_rm_platform_pre_demand_page(struct gunyah_rm *rm, u16 vmid,
+ enum gunyah_pagetable_access access,
+ struct folio *folio)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->pre_demand_page)
+ ret = rm_platform_ops->pre_demand_page(rm, vmid, access, folio);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_pre_demand_page);
+
+int gunyah_rm_platform_reclaim_demand_page(struct gunyah_rm *rm, u16 vmid,
+ enum gunyah_pagetable_access access,
+ struct folio *folio)
+{
+ int ret = 0;
+
+ down_read(&rm_platform_ops_lock);
+ if (rm_platform_ops && rm_platform_ops->pre_demand_page)
+ ret = rm_platform_ops->release_demand_page(rm, vmid, access,
+ folio);
+ up_read(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_platform_reclaim_demand_page);
+
+int gunyah_rm_register_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+ int ret = 0;
+
+ down_write(&rm_platform_ops_lock);
+ if (!rm_platform_ops)
+ rm_platform_ops = platform_ops;
+ else
+ ret = -EEXIST;
+ up_write(&rm_platform_ops_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_register_platform_ops);
+
+void gunyah_rm_unregister_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+ down_write(&rm_platform_ops_lock);
+ if (rm_platform_ops == platform_ops)
+ rm_platform_ops = NULL;
+ up_write(&rm_platform_ops_lock);
+}
+EXPORT_SYMBOL_GPL(gunyah_rm_unregister_platform_ops);
+
+static void _devm_gunyah_rm_unregister_platform_ops(void *data)
+{
+ gunyah_rm_unregister_platform_ops(
+ (const struct gunyah_rm_platform_ops *)data);
+}
+
+int devm_gunyah_rm_register_platform_ops(
+ struct device *dev, const struct gunyah_rm_platform_ops *ops)
+{
+ int ret;
+
+ ret = gunyah_rm_register_platform_ops(ops);
+ if (ret)
+ return ret;
+
+ return devm_add_action(dev, _devm_gunyah_rm_unregister_platform_ops,
+ (void *)ops);
+}
+EXPORT_SYMBOL_GPL(devm_gunyah_rm_register_platform_ops);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Gunyah Platform Hooks");
diff --git a/drivers/virt/gunyah/rsc_mgr_rpc.c b/drivers/virt/gunyah/rsc_mgr_rpc.c
index 0266c2a8d583..ec187d116dd7 100644
--- a/drivers/virt/gunyah/rsc_mgr_rpc.c
+++ b/drivers/virt/gunyah/rsc_mgr_rpc.c
@@ -212,6 +212,12 @@ int gunyah_rm_mem_share(struct gunyah_rm *rm, struct gunyah_rm_mem_parcel *p)
if (!msg)
return -ENOMEM;
+ ret = gunyah_rm_platform_pre_mem_share(rm, p);
+ if (ret) {
+ kfree(msg);
+ return ret;
+ }
+
req_header = msg;
acl = (void *)req_header + sizeof(*req_header);
mem = (void *)acl + acl_size;
@@ -237,8 +243,10 @@ int gunyah_rm_mem_share(struct gunyah_rm *rm, struct gunyah_rm_mem_parcel *p)
&resp_size);
kfree(msg);
- if (ret)
+ if (ret) {
+ gunyah_rm_platform_post_mem_reclaim(rm, p);
return ret;
+ }
p->mem_handle = le32_to_cpu(*resp);
kfree(resp);
@@ -270,9 +278,14 @@ int gunyah_rm_mem_reclaim(struct gunyah_rm *rm,
struct gunyah_rm_mem_release_req req = {
.mem_handle = cpu_to_le32(parcel->mem_handle),
};
+ int ret;
+
+ ret = gunyah_rm_call(rm, GUNYAH_RM_RPC_MEM_RECLAIM, &req, sizeof(req),
+ NULL, NULL);
+ if (ret)
+ return ret;
- return gunyah_rm_call(rm, GUNYAH_RM_RPC_MEM_RECLAIM, &req, sizeof(req),
- NULL, NULL);
+ return gunyah_rm_platform_post_mem_reclaim(rm, parcel);
}
ALLOW_ERROR_INJECTION(gunyah_rm_mem_reclaim, ERRNO);
diff --git a/include/linux/gunyah.h b/include/linux/gunyah.h
index d1048d0fc246..1d363ab8967a 100644
--- a/include/linux/gunyah.h
+++ b/include/linux/gunyah.h
@@ -158,6 +158,57 @@ struct gunyah_vcpu {
struct gunyah_vm_resource_ticket ticket;
};
+enum gunyah_pagetable_access {
+ /* clang-format off */
+ GUNYAH_PAGETABLE_ACCESS_NONE = 0,
+ GUNYAH_PAGETABLE_ACCESS_X = 1,
+ GUNYAH_PAGETABLE_ACCESS_W = 2,
+ GUNYAH_PAGETABLE_ACCESS_R = 4,
+ GUNYAH_PAGETABLE_ACCESS_RX = 5,
+ GUNYAH_PAGETABLE_ACCESS_RW = 6,
+ GUNYAH_PAGETABLE_ACCESS_RWX = 7,
+ /* clang-format on */
+};
+
+struct gunyah_rm_platform_ops {
+ int (*pre_mem_share)(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel);
+ int (*post_mem_reclaim)(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel);
+
+ int (*pre_demand_page)(struct gunyah_rm *rm, u16 vmid,
+ enum gunyah_pagetable_access access,
+ struct folio *folio);
+ int (*release_demand_page)(struct gunyah_rm *rm, u16 vmid,
+ enum gunyah_pagetable_access access,
+ struct folio *folio);
+};
+
+#if IS_ENABLED(CONFIG_GUNYAH_PLATFORM_HOOKS)
+int gunyah_rm_register_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops);
+void gunyah_rm_unregister_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops);
+int devm_gunyah_rm_register_platform_ops(
+ struct device *dev, const struct gunyah_rm_platform_ops *ops);
+#else
+static inline int gunyah_rm_register_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+ return 0;
+}
+static inline void gunyah_rm_unregister_platform_ops(
+ const struct gunyah_rm_platform_ops *platform_ops)
+{
+}
+static inline int
+devm_gunyah_rm_register_platform_ops(struct device *dev,
+ const struct gunyah_rm_platform_ops *ops)
+{
+ return 0;
+}
+#endif
+
/******************************************************************************/
/* Common arch-independent definitions for Gunyah hypercalls */
#define GUNYAH_CAPID_INVAL U64_MAX
@@ -278,18 +329,6 @@ enum gunyah_error gunyah_hypercall_msgq_recv(u64 capid, void *buff, size_t size,
#define GUNYAH_ADDRSPACE_SELF_CAP 0
-enum gunyah_pagetable_access {
- /* clang-format off */
- GUNYAH_PAGETABLE_ACCESS_NONE = 0,
- GUNYAH_PAGETABLE_ACCESS_X = 1,
- GUNYAH_PAGETABLE_ACCESS_W = 2,
- GUNYAH_PAGETABLE_ACCESS_R = 4,
- GUNYAH_PAGETABLE_ACCESS_RX = 5,
- GUNYAH_PAGETABLE_ACCESS_RW = 6,
- GUNYAH_PAGETABLE_ACCESS_RWX = 7,
- /* clang-format on */
-};
-
/* clang-format off */
#define GUNYAH_MEMEXTENT_MAPPING_USER_ACCESS GENMASK_ULL(2, 0)
#define GUNYAH_MEMEXTENT_MAPPING_KERNEL_ACCESS GENMASK_ULL(6, 4)
diff --git a/include/linux/gunyah_rsc_mgr.h b/include/linux/gunyah_rsc_mgr.h
index c42a0cb42ba6..fb3feee73490 100644
--- a/include/linux/gunyah_rsc_mgr.h
+++ b/include/linux/gunyah_rsc_mgr.h
@@ -155,4 +155,14 @@ void gunyah_rm_free_resource(struct gunyah_resource *ghrsc);
int gunyah_rm_call(struct gunyah_rm *rsc_mgr, u32 message_id,
const void *req_buf, size_t req_buf_size, void **resp_buf,
size_t *resp_buf_size);
+
+int gunyah_rm_platform_pre_mem_share(struct gunyah_rm *rm,
+ struct gunyah_rm_mem_parcel *mem_parcel);
+int gunyah_rm_platform_post_mem_reclaim(
+ struct gunyah_rm *rm, struct gunyah_rm_mem_parcel *mem_parcel);
+
+int gunyah_rm_platform_pre_demand_page(struct gunyah_rm *rm, u16 vmid,
+ u32 flags, struct folio *folio);
+int gunyah_rm_platform_reclaim_demand_page(struct gunyah_rm *rm, u16 vmid,
+ u32 flags, struct folio *folio);
#endif
--
2.39.5
Powered by blists - more mailing lists