diff mbox series

[RFC,v2,04/13] KVM: Add fd-based memslot data structure and utils

Message ID 20211119134739.20218-5-chao.p.peng@linux.intel.com (mailing list archive)
State New
Headers show
Series KVM: mm: fd-based approach for supporting KVM guest private memory | expand

Commit Message

Chao Peng Nov. 19, 2021, 1:47 p.m. UTC
For fd-based memslot store the file references for shared fd and the
private fd (if any) in the memslot structure. Since there is no 'hva'
concept we cannot call hva_to_pfn() to get a pfn, instead kvm_memfd_ops
is added to get_pfn/put_pfn from the memory backing stores that provide
these fds.

Signed-off-by: Yu Zhang <yu.c.zhang@linux.intel.com>
Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>
---
 include/linux/kvm_host.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

Paolo Bonzini Nov. 23, 2021, 8:41 a.m. UTC | #1
On 11/19/21 14:47, Chao Peng wrote:
> For fd-based memslot store the file references for shared fd and the
> private fd (if any) in the memslot structure. Since there is no 'hva'
> concept we cannot call hva_to_pfn() to get a pfn, instead kvm_memfd_ops
> is added to get_pfn/put_pfn from the memory backing stores that provide
> these fds.
> 
> Signed-off-by: Yu Zhang<yu.c.zhang@linux.intel.com>
> Signed-off-by: Chao Peng<chao.p.peng@linux.intel.com>
> ---

What about kvm_read/write_guest?  Maybe the proposal which kept 
userspace_addr for the shared fd is more doable (it would be great to 
ultimately remove the mandatory userspace mapping for the shared fd, but 
I think KVM is not quite ready for that).

Paolo
Chao Peng Nov. 23, 2021, 2:30 p.m. UTC | #2
On Tue, Nov 23, 2021 at 09:41:34AM +0100, Paolo Bonzini wrote:
> On 11/19/21 14:47, Chao Peng wrote:
> > For fd-based memslot store the file references for shared fd and the
> > private fd (if any) in the memslot structure. Since there is no 'hva'
> > concept we cannot call hva_to_pfn() to get a pfn, instead kvm_memfd_ops
> > is added to get_pfn/put_pfn from the memory backing stores that provide
> > these fds.
> > 
> > Signed-off-by: Yu Zhang<yu.c.zhang@linux.intel.com>
> > Signed-off-by: Chao Peng<chao.p.peng@linux.intel.com>
> > ---
> 
> What about kvm_read/write_guest? 

Hmm, that would be another area KVM needs to change. Not totally
undoable.

> Maybe the proposal which kept
> userspace_addr for the shared fd is more doable (it would be great to
> ultimately remove the mandatory userspace mapping for the shared fd, but I
> think KVM is not quite ready for that).

Agree for short term keeping shared part unchanged would be making work
easy:) Let me try that to see if any blocker.

> 
> Paolo
diff mbox series

Patch

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 99e9f9969703..1d4ac0c9b63b 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -424,6 +424,12 @@  static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
  */
 #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
 
+struct kvm_memfd_ops {
+	kvm_pfn_t (*get_pfn)(struct kvm_memory_slot *slot, struct file *file,
+			     gfn_t gfn, bool alloc, int *order);
+	void (*put_pfn)(kvm_pfn_t pfn);
+};
+
 struct kvm_memory_slot {
 	gfn_t base_gfn;
 	unsigned long npages;
@@ -433,6 +439,9 @@  struct kvm_memory_slot {
 	u32 flags;
 	short id;
 	u16 as_id;
+	struct file *file;
+	struct file *priv_file;
+	struct kvm_memfd_ops *memfd_ops;
 };
 
 static inline bool kvm_slot_dirty_track_enabled(struct kvm_memory_slot *slot)
@@ -1310,6 +1319,20 @@  static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
 	return gfn_to_memslot(kvm, gfn)->id;
 }
 
+static inline bool memslot_is_memfd(const struct kvm_memory_slot *slot)
+{
+	if (slot && slot->memfd_ops)
+		return true;
+	return false;
+}
+
+static inline bool memslot_has_private(const struct kvm_memory_slot *slot)
+{
+	if (slot && slot->priv_file)
+		return true;
+	return false;
+}
+
 static inline gfn_t
 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
 {