diff mbox series

[02/11] KVM: selftests: Add vm_mem_region_get_src_fd library function

Message ID 20220311060207.2438667-3-ricarkol@google.com (mailing list archive)
State New, archived
Headers show
Series KVM: selftests: Add aarch64/page_fault_test | expand

Commit Message

Ricardo Koller March 11, 2022, 6:01 a.m. UTC
Add a library function to get the backing source FD of a memslot.

Signed-off-by: Ricardo Koller <ricarkol@google.com>
---
 .../selftests/kvm/include/kvm_util_base.h     |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 23 +++++++++++++++++++
 2 files changed, 24 insertions(+)

Comments

Ben Gardon March 16, 2022, 6:08 p.m. UTC | #1
On Fri, Mar 11, 2022 at 12:02 AM Ricardo Koller <ricarkol@google.com> wrote:
>
> Add a library function to get the backing source FD of a memslot.
>
> Signed-off-by: Ricardo Koller <ricarkol@google.com>

This appears to be dead code as of this commit, would recommend
merging it into the commit in which it's actually used.

> ---
>  .../selftests/kvm/include/kvm_util_base.h     |  1 +
>  tools/testing/selftests/kvm/lib/kvm_util.c    | 23 +++++++++++++++++++
>  2 files changed, 24 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
> index 4ed6aa049a91..d6acec0858c0 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util_base.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
> @@ -163,6 +163,7 @@ int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
>  void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
>  void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
>  void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
> +int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot);
>  void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid);
>  vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
>  vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index d8cf851ab119..64ef245b73de 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -580,6 +580,29 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
>         return &region->region;
>  }
>
> +/*
> + * KVM Userspace Memory Get Backing Source FD
> + *
> + * Input Args:
> + *   vm - Virtual Machine
> + *   memslot - KVM memory slot ID
> + *
> + * Output Args: None
> + *
> + * Return:
> + *   Backing source file descriptor, -1 if the memslot is an anonymous region.
> + *
> + * Returns the backing source fd of a memslot, so tests can use it to punch
> + * holes, or to setup permissions.
> + */
> +int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot)
> +{
> +       struct userspace_mem_region *region;
> +
> +       region = memslot2region(vm, memslot);
> +       return region->fd;
> +}
> +
>  /*
>   * VCPU Find
>   *
> --
> 2.35.1.723.g4982287a31-goog
>
Ricardo Koller March 18, 2022, 8:30 p.m. UTC | #2
On Wed, Mar 16, 2022 at 12:08:23PM -0600, Ben Gardon wrote:
> On Fri, Mar 11, 2022 at 12:02 AM Ricardo Koller <ricarkol@google.com> wrote:
> >
> > Add a library function to get the backing source FD of a memslot.
> >
> > Signed-off-by: Ricardo Koller <ricarkol@google.com>
> 
> This appears to be dead code as of this commit, would recommend
> merging it into the commit in which it's actually used.

I was trying to separate lib changes (which are mostly arch independent)
with the actual test. Would move the commit to be right before the one
that uses be better? and maybe add a commit comment mentioning how it's
going to be used.

> 
> > ---
> >  .../selftests/kvm/include/kvm_util_base.h     |  1 +
> >  tools/testing/selftests/kvm/lib/kvm_util.c    | 23 +++++++++++++++++++
> >  2 files changed, 24 insertions(+)
> >
> > diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
> > index 4ed6aa049a91..d6acec0858c0 100644
> > --- a/tools/testing/selftests/kvm/include/kvm_util_base.h
> > +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
> > @@ -163,6 +163,7 @@ int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
> >  void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
> >  void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
> >  void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
> > +int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot);
> >  void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid);
> >  vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
> >  vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
> > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> > index d8cf851ab119..64ef245b73de 100644
> > --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> > @@ -580,6 +580,29 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
> >         return &region->region;
> >  }
> >
> > +/*
> > + * KVM Userspace Memory Get Backing Source FD
> > + *
> > + * Input Args:
> > + *   vm - Virtual Machine
> > + *   memslot - KVM memory slot ID
> > + *
> > + * Output Args: None
> > + *
> > + * Return:
> > + *   Backing source file descriptor, -1 if the memslot is an anonymous region.
> > + *
> > + * Returns the backing source fd of a memslot, so tests can use it to punch
> > + * holes, or to setup permissions.
> > + */
> > +int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot)
> > +{
> > +       struct userspace_mem_region *region;
> > +
> > +       region = memslot2region(vm, memslot);
> > +       return region->fd;
> > +}
> > +
> >  /*
> >   * VCPU Find
> >   *
> > --
> > 2.35.1.723.g4982287a31-goog
> >
Ben Gardon March 21, 2022, 5:01 p.m. UTC | #3
On Fri, Mar 18, 2022 at 1:30 PM Ricardo Koller <ricarkol@google.com> wrote:
>
> On Wed, Mar 16, 2022 at 12:08:23PM -0600, Ben Gardon wrote:
> > On Fri, Mar 11, 2022 at 12:02 AM Ricardo Koller <ricarkol@google.com> wrote:
> > >
> > > Add a library function to get the backing source FD of a memslot.
> > >
> > > Signed-off-by: Ricardo Koller <ricarkol@google.com>
> >
> > This appears to be dead code as of this commit, would recommend
> > merging it into the commit in which it's actually used.
>
> I was trying to separate lib changes (which are mostly arch independent)
> with the actual test. Would move the commit to be right before the one
> that uses be better? and maybe add a commit comment mentioning how it's
> going to be used.

Ah, that makes sense, I can see why you'd want to separate them.
Moving it right before the commit where it's used sounds fine to me.
Thanks!

>
>
> >
> > > ---
> > >  .../selftests/kvm/include/kvm_util_base.h     |  1 +
> > >  tools/testing/selftests/kvm/lib/kvm_util.c    | 23 +++++++++++++++++++
> > >  2 files changed, 24 insertions(+)
> > >
> > > diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
> > > index 4ed6aa049a91..d6acec0858c0 100644
> > > --- a/tools/testing/selftests/kvm/include/kvm_util_base.h
> > > +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
> > > @@ -163,6 +163,7 @@ int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
> > >  void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
> > >  void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
> > >  void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
> > > +int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot);
> > >  void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid);
> > >  vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
> > >  vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
> > > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> > > index d8cf851ab119..64ef245b73de 100644
> > > --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> > > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> > > @@ -580,6 +580,29 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
> > >         return &region->region;
> > >  }
> > >
> > > +/*
> > > + * KVM Userspace Memory Get Backing Source FD
> > > + *
> > > + * Input Args:
> > > + *   vm - Virtual Machine
> > > + *   memslot - KVM memory slot ID
> > > + *
> > > + * Output Args: None
> > > + *
> > > + * Return:
> > > + *   Backing source file descriptor, -1 if the memslot is an anonymous region.
> > > + *
> > > + * Returns the backing source fd of a memslot, so tests can use it to punch
> > > + * holes, or to setup permissions.
> > > + */
> > > +int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot)
> > > +{
> > > +       struct userspace_mem_region *region;
> > > +
> > > +       region = memslot2region(vm, memslot);
> > > +       return region->fd;
> > > +}
> > > +
> > >  /*
> > >   * VCPU Find
> > >   *
> > > --
> > > 2.35.1.723.g4982287a31-goog
> > >
diff mbox series

Patch

diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 4ed6aa049a91..d6acec0858c0 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -163,6 +163,7 @@  int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
+int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot);
 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid);
 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
 vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index d8cf851ab119..64ef245b73de 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -580,6 +580,29 @@  kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
 	return &region->region;
 }
 
+/*
+ * KVM Userspace Memory Get Backing Source FD
+ *
+ * Input Args:
+ *   vm - Virtual Machine
+ *   memslot - KVM memory slot ID
+ *
+ * Output Args: None
+ *
+ * Return:
+ *   Backing source file descriptor, -1 if the memslot is an anonymous region.
+ *
+ * Returns the backing source fd of a memslot, so tests can use it to punch
+ * holes, or to setup permissions.
+ */
+int vm_mem_region_get_src_fd(struct kvm_vm *vm, uint32_t memslot)
+{
+	struct userspace_mem_region *region;
+
+	region = memslot2region(vm, memslot);
+	return region->fd;
+}
+
 /*
  * VCPU Find
  *