From: Liju-clr Chen <liju-clr.chen@mediatek.com>
To: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
"Catalin Marinas" <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
"Steven Rostedt" <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Richard Cochran <richardcochran@gmail.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>,
Liju-clr Chen <Liju-clr.Chen@mediatek.com>,
Yingshiuan Pan <Yingshiuan.Pan@mediatek.com>,
Ze-yu Wang <Ze-yu.Wang@mediatek.com>
Cc: <devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-doc@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-trace-kernel@vger.kernel.org>, <netdev@vger.kernel.org>,
<linux-mediatek@lists.infradead.org>,
Shawn Hsiao <shawn.hsiao@mediatek.com>,
PeiLun Suei <PeiLun.Suei@mediatek.com>,
Chi-shen Yeh <Chi-shen.Yeh@mediatek.com>,
Kevenny Hsieh <Kevenny.Hsieh@mediatek.com>
Subject: [PATCH v12 18/24] virt: geniezone: Add memory relinquish support
Date: Tue, 30 Jul 2024 16:24:30 +0800 [thread overview]
Message-ID: <20240730082436.9151-19-liju-clr.chen@mediatek.com> (raw)
In-Reply-To: <20240730082436.9151-1-liju-clr.chen@mediatek.com>
From: Jerry Wang <ze-yu.wang@mediatek.com>
Unpin the pages when VM relinquish the pages or is destroyed.
Signed-off-by: Jerry Wang <ze-yu.wang@mediatek.com>
Co-developed-by: Yingshiuan Pan <yingshiuan.pan@mediatek.com>
Signed-off-by: Yingshiuan Pan <yingshiuan.pan@mediatek.com>
Signed-off-by: Yi-De Wu <yi-de.wu@mediatek.com>
Signed-off-by: Liju Chen <liju-clr.chen@mediatek.com>
---
drivers/virt/geniezone/gzvm_exception.c | 23 ++++++++++++
drivers/virt/geniezone/gzvm_mmu.c | 49 +++++++++++++++++++++++++
drivers/virt/geniezone/gzvm_vcpu.c | 6 ++-
include/linux/soc/mediatek/gzvm_drv.h | 2 +
include/uapi/linux/gzvm.h | 5 +++
5 files changed, 83 insertions(+), 2 deletions(-)
diff --git a/drivers/virt/geniezone/gzvm_exception.c b/drivers/virt/geniezone/gzvm_exception.c
index 2f5c05045e61..cdfc99d24ded 100644
--- a/drivers/virt/geniezone/gzvm_exception.c
+++ b/drivers/virt/geniezone/gzvm_exception.c
@@ -37,3 +37,26 @@ bool gzvm_handle_guest_exception(struct gzvm_vcpu *vcpu)
else
return false;
}
+
+/**
+ * gzvm_handle_guest_hvc() - Handle guest hvc
+ * @vcpu: Pointer to struct gzvm_vcpu struct
+ * Return:
+ * * true - This hvc has been processed, no need to back to VMM.
+ * * false - This hvc has not been processed, require userspace.
+ */
+bool gzvm_handle_guest_hvc(struct gzvm_vcpu *vcpu)
+{
+ unsigned long ipa;
+ int ret;
+
+ switch (vcpu->run->hypercall.args[0]) {
+ case GZVM_HVC_MEM_RELINQUISH:
+ ipa = vcpu->run->hypercall.args[1];
+ ret = gzvm_handle_relinquish(vcpu, ipa);
+ return (ret == 0) ? true : false;
+ default:
+ break;
+ }
+ return false;
+}
diff --git a/drivers/virt/geniezone/gzvm_mmu.c b/drivers/virt/geniezone/gzvm_mmu.c
index f6e4aca63f86..1c6966a0ed8e 100644
--- a/drivers/virt/geniezone/gzvm_mmu.c
+++ b/drivers/virt/geniezone/gzvm_mmu.c
@@ -29,6 +29,36 @@ static int gzvm_insert_ppage(struct gzvm *vm, struct gzvm_pinned_page *ppage)
return 0;
}
+static int rb_ppage_cmp(const void *key, const struct rb_node *node)
+{
+ struct gzvm_pinned_page *p = container_of(node,
+ struct gzvm_pinned_page,
+ node);
+ phys_addr_t ipa = (phys_addr_t)key;
+
+ return (ipa < p->ipa) ? -1 : (ipa > p->ipa);
+}
+
+/* Invoker of this function is responsible for locking */
+static int gzvm_remove_ppage(struct gzvm *vm, phys_addr_t ipa)
+{
+ struct gzvm_pinned_page *ppage;
+ struct rb_node *node;
+
+ node = rb_find((void *)ipa, &vm->pinned_pages, rb_ppage_cmp);
+
+ if (node)
+ rb_erase(node, &vm->pinned_pages);
+ else
+ return 0;
+
+ ppage = container_of(node, struct gzvm_pinned_page, node);
+ unpin_user_pages_dirty_lock(&ppage->page, 1, true);
+ kfree(ppage);
+
+ return 0;
+}
+
static int pin_one_page(struct gzvm *vm, unsigned long hva, u64 gpa,
struct page **out_page)
{
@@ -77,6 +107,25 @@ static int pin_one_page(struct gzvm *vm, unsigned long hva, u64 gpa,
return ret;
}
+/**
+ * gzvm_handle_relinquish() - Handle memory relinquish request from hypervisor
+ *
+ * @vcpu: Pointer to struct gzvm_vcpu_run in userspace
+ * @ipa: Start address(gpa) of a reclaimed page
+ *
+ * Return: Always return 0 because there are no cases of failure
+ */
+int gzvm_handle_relinquish(struct gzvm_vcpu *vcpu, phys_addr_t ipa)
+{
+ struct gzvm *vm = vcpu->gzvm;
+
+ mutex_lock(&vm->mem_lock);
+ gzvm_remove_ppage(vm, ipa);
+ mutex_unlock(&vm->mem_lock);
+
+ return 0;
+}
+
int gzvm_vm_allocate_guest_page(struct gzvm *vm, struct gzvm_memslot *slot,
u64 gfn, u64 *pfn)
{
diff --git a/drivers/virt/geniezone/gzvm_vcpu.c b/drivers/virt/geniezone/gzvm_vcpu.c
index 3f6bffbafc7a..b0dc3e4f47d7 100644
--- a/drivers/virt/geniezone/gzvm_vcpu.c
+++ b/drivers/virt/geniezone/gzvm_vcpu.c
@@ -112,12 +112,14 @@ static long gzvm_vcpu_run(struct gzvm_vcpu *vcpu, void __user *argp)
* it's geniezone's responsibility to fill corresponding data
* structure
*/
+ case GZVM_EXIT_HYPERCALL:
+ if (!gzvm_handle_guest_hvc(vcpu))
+ need_userspace = true;
+ break;
case GZVM_EXIT_EXCEPTION:
if (!gzvm_handle_guest_exception(vcpu))
need_userspace = true;
break;
- case GZVM_EXIT_HYPERCALL:
- fallthrough;
case GZVM_EXIT_DEBUG:
fallthrough;
case GZVM_EXIT_FAIL_ENTRY:
diff --git a/include/linux/soc/mediatek/gzvm_drv.h b/include/linux/soc/mediatek/gzvm_drv.h
index 7c6c1cde0d8d..b8cec9717606 100644
--- a/include/linux/soc/mediatek/gzvm_drv.h
+++ b/include/linux/soc/mediatek/gzvm_drv.h
@@ -208,6 +208,8 @@ int gzvm_arch_inform_exit(u16 vm_id);
int gzvm_find_memslot(struct gzvm *vm, u64 gpa);
int gzvm_handle_page_fault(struct gzvm_vcpu *vcpu);
bool gzvm_handle_guest_exception(struct gzvm_vcpu *vcpu);
+int gzvm_handle_relinquish(struct gzvm_vcpu *vcpu, phys_addr_t ipa);
+bool gzvm_handle_guest_hvc(struct gzvm_vcpu *vcpu);
int gzvm_arch_create_device(u16 vm_id, struct gzvm_create_device *gzvm_dev);
int gzvm_arch_inject_irq(struct gzvm *gzvm, unsigned int vcpu_idx,
diff --git a/include/uapi/linux/gzvm.h b/include/uapi/linux/gzvm.h
index 0d38a0963cb7..5411357ec05e 100644
--- a/include/uapi/linux/gzvm.h
+++ b/include/uapi/linux/gzvm.h
@@ -195,6 +195,11 @@ enum {
GZVM_EXCEPTION_PAGE_FAULT = 0x1,
};
+/* hypercall definitions of GZVM_EXIT_HYPERCALL */
+enum {
+ GZVM_HVC_MEM_RELINQUISH = 0xc6000009,
+};
+
/**
* struct gzvm_vcpu_run: Same purpose as kvm_run, this struct is
* shared between userspace, kernel and
--
2.18.0
next prev parent reply other threads:[~2024-07-30 8:24 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-30 8:24 [PATCH v12 00/24] GenieZone hypervisor drivers Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 01/24] virt: geniezone: enable gzvm-ko in defconfig Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 02/24] docs: geniezone: Introduce GenieZone hypervisor Liju-clr Chen
2024-07-30 14:13 ` Randy Dunlap
2024-10-22 2:21 ` Liju-clr Chen (陳麗如)
2024-07-30 8:24 ` [PATCH v12 03/24] dt-bindings: hypervisor: Add MediaTek " Liju-clr Chen
2024-08-01 10:36 ` Hillf Danton
2024-10-22 2:47 ` Liju-clr Chen (陳麗如)
2024-07-30 8:24 ` [PATCH v12 04/24] virt: geniezone: Add GenieZone hypervisor driver Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 05/24] virt: geniezone: Add vm support Liju-clr Chen
2024-08-01 10:50 ` Hillf Danton
2024-10-22 3:10 ` Liju-clr Chen (陳麗如)
2024-07-30 8:24 ` [PATCH v12 06/24] virt: geniezone: Add set_user_memory_region for vm Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 07/24] virt: geniezone: Add vm capability check Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 08/24] virt: geniezone: Add vcpu support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 09/24] virt: geniezone: Add irqchip support for virtual interrupt injection Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 10/24] virt: geniezone: Add irqfd support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 11/24] virt: geniezone: Add ioeventfd support Liju-clr Chen
2024-07-31 8:57 ` Simon Horman
2024-07-30 8:24 ` [PATCH v12 12/24] virt: geniezone: Add memory region purpose for hypervisor Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 13/24] virt: geniezone: Add dtb config support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 14/24] virt: geniezone: Optimize performance of protected VM memory Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 15/24] virt: geniezone: Add memory pin/unpin support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 16/24] virt: geniezone: Add demand paging support Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 17/24] virt: geniezone: Add block-based " Liju-clr Chen
2024-07-30 8:24 ` Liju-clr Chen [this message]
2024-07-30 8:24 ` [PATCH v12 19/24] virt: geniezone: Provide individual VM memory statistics within debugfs Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 20/24] virt: geniezone: Add tracing support for hyp call and vcpu exit_reason Liju-clr Chen
2024-08-07 20:02 ` Steven Rostedt
2024-07-30 8:24 ` [PATCH v12 21/24] virt: geniezone: Enable PTP for synchronizing time between host and guest VMs Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 22/24] virt: geniezone: Add support for virtual timer migration Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 23/24] virt: geniezone: Add support for guest VM CPU idle Liju-clr Chen
2024-07-30 8:24 ` [PATCH v12 24/24] virt: geniezone: Emulate IPI for guest VM Liju-clr Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240730082436.9151-19-liju-clr.chen@mediatek.com \
--to=liju-clr.chen@mediatek.com \
--cc=Chi-shen.Yeh@mediatek.com \
--cc=Kevenny.Hsieh@mediatek.com \
--cc=PeiLun.Suei@mediatek.com \
--cc=Yingshiuan.Pan@mediatek.com \
--cc=Ze-yu.Wang@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=catalin.marinas@arm.com \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=matthias.bgg@gmail.com \
--cc=mhiramat@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=richardcochran@gmail.com \
--cc=robh@kernel.org \
--cc=rostedt@goodmis.org \
--cc=shawn.hsiao@mediatek.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®