[RFC PATCH 05/42] KVM: Embed "arch" object and call arch init/destroy in TDP FD

Yan Zhao
2023-12-02T09:46:03Z

Embed "arch" object in private "kvm_exported_tdp" object of KVM TDP FD
object in order to associate a TDP page table to this private object.

With later patches for arch x86, the overall data structure hierarchy on
x86 for TDP FD to export TDP is outlined below for preview.

kvm_tdp_fd
.------
| ops-|-->kvm_exported_tdp_ops
| file | public
-----------------------------------------------------------------------
| priv-|-->kvm_exported_tdp private
'------' .-----------.
| tdp_fd |
| as_id |
| kvm |
| importers |
| arch -|-->kvm_arch_exported_tdp
| list_node | .------.
'-----------' | mmu -|--> kvm_exported_tdp_mmu
| meta | .-----------.
'--|---' | common -|--> kvm_mmu_common
| | root_page |
| '-----------'
|
|
|
+-->kvm_exported_tdp_meta_vmx
.--------------------.
| type |
| level |
| root_hpa |
| max_huge_page_level|
| rsvd_bits_mask |
'--------------------'

Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
---
include/linux/kvm_host.h | 17 +++++++++++++++++
virt/kvm/tdp_fd.c | 12 +++++++++---
2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 122f47c94ecae..5a74b2b0ac81f 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2327,6 +2327,9 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)
#ifdef CONFIG_HAVE_KVM_EXPORTED_TDP

struct kvm_exported_tdp {
+#ifdef __KVM_HAVE_ARCH_EXPORTED_TDP
+ struct kvm_arch_exported_tdp arch;
+#endif
struct kvm_tdp_fd *tdp_fd;

struct kvm *kvm;
@@ -2335,5 +2338,19 @@ struct kvm_exported_tdp {
struct list_head list_node;
};

+#ifdef __KVM_HAVE_ARCH_EXPORTED_TDP
+int kvm_arch_exported_tdp_init(struct kvm *kvm, struct kvm_exported_tdp *tdp);
+void kvm_arch_exported_tdp_destroy(struct kvm_exported_tdp *tdp);
+#else
+static inline int kvm_arch_exported_tdp_init(struct kvm *kvm,
+ struct kvm_exported_tdp *tdp)
+{
+ return -EOPNOTSUPP;
+}
+static inline void kvm_arch_exported_tdp_destroy(struct kvm_exported_tdp *tdp)
+{
+}
+#endif /* __KVM_HAVE_ARCH_EXPORTED_TDP */
+
#endif /* CONFIG_HAVE_KVM_EXPORTED_TDP */
#endif
diff --git a/virt/kvm/tdp_fd.c b/virt/kvm/tdp_fd.c
index a5c4c3597e94f..7e68199ea9643 100644
--- a/virt/kvm/tdp_fd.c
+++ b/virt/kvm/tdp_fd.c
@@ -52,17 +52,20 @@ int kvm_create_tdp_fd(struct kvm *kvm, struct kvm_create_tdp_fd *ct)
goto out;
}
tdp->kvm = kvm;
+ ret = kvm_arch_exported_tdp_init(kvm, tdp);
+ if (ret)
+ goto out;

tdp_fd->file = anon_inode_getfile("tdp_fd", &kvm_tdp_fd_fops,
tdp_fd, O_RDWR | O_CLOEXEC);
if (!tdp_fd->file) {
ret = -EFAULT;
- goto out;
+ goto out_uninit;
}

fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
if (fd < 0)
- goto out;
+ goto out_uninit;

fd_install(fd, tdp_fd->file);
ct->fd = fd;
@@ -73,10 +76,12 @@ int kvm_create_tdp_fd(struct kvm *kvm, struct kvm_create_tdp_fd *ct)
spin_unlock(&kvm->exported_tdplist_lock);
return 0;

-out:
+out_uninit:
if (tdp_fd->file)
fput(tdp_fd->file);

+ kvm_arch_exported_tdp_destroy(tdp);
+out:
if (tdp->kvm)
kvm_put_kvm_no_destroy(tdp->kvm);
kfree(tdp);
@@ -102,6 +107,7 @@ static int kvm_tdp_fd_release(struct inode *inode, struct file *file)
list_del(&tdp->list_node);
spin_unlock(&tdp->kvm->exported_tdplist_lock);

+ kvm_arch_exported_tdp_destroy(tdp);
kvm_put_kvm(tdp->kvm);
kfree(tdp);
kfree(tdp_fd);
--
2.17.1