kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Axel Rasmussen <axelrasmussen@google.com>
To: Aaron Lewis <aaronlewis@google.com>,
	Alexander Graf <graf@amazon.com>,
	Andrew Jones <drjones@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Ben Gardon <bgardon@google.com>,
	Emanuele Giuseppe Esposito <eesposit@redhat.com>,
	Eric Auger <eric.auger@redhat.com>,
	Jacob Xu <jacobhxu@google.com>,
	Makarand Sonare <makarandsonare@google.com>,
	Oliver Upton <oupton@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Yanan Wang <wangyanan55@huawei.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	Axel Rasmussen <axelrasmussen@google.com>
Subject: [PATCH v2 08/10] KVM: selftests: create alias mappings when using shared memory
Date: Wed, 19 May 2021 13:03:37 -0700	[thread overview]
Message-ID: <20210519200339.829146-9-axelrasmussen@google.com> (raw)
In-Reply-To: <20210519200339.829146-1-axelrasmussen@google.com>

When a memory region is added with a src_type specifying that it should
use some kind of shared memory, also create an alias mapping to the same
underlying physical pages.

And, add an API so tests can get access to these alias addresses.
Basically, for a guest physical address, let us look up the analogous
host *alias* address.

In a future commit, we'll modify the demand paging test to take
advantage of this to exercise UFFD minor faults. The idea is, we
pre-fault the underlying pages *via the alias*. When the *guest*
faults, it gets a "minor" fault (PTEs don't exist yet, but a page is
already in the page cache). Then, the userfaultfd theads can handle the
fault: they could potentially modify the underlying memory *via the
alias* if they wanted to, and then they install the PTEs and let the
guest carry on via a UFFDIO_CONTINUE ioctl.

Reviewed-by: Ben Gardon <bgardon@google.com>
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
---
 .../testing/selftests/kvm/include/kvm_util.h  |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 51 +++++++++++++++++++
 .../selftests/kvm/lib/kvm_util_internal.h     |  2 +
 3 files changed, 54 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index a8f022794ce3..0624f25a6803 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -146,6 +146,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa);
 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva);
+void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa);
 
 /*
  * Address Guest Virtual to Guest Physical
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index e4a8d0c43c5e..0b88d1bbc1e0 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -811,6 +811,19 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 
 	/* Add to linked-list of memory regions. */
 	list_add(&region->list, &vm->userspace_mem_regions);
+
+	/* If shared memory, create an alias. */
+	if (region->fd >= 0) {
+		region->mmap_alias = mmap(NULL, region->mmap_size,
+					  PROT_READ | PROT_WRITE,
+					  vm_mem_backing_src_alias(src_type)->flag,
+					  region->fd, 0);
+		TEST_ASSERT(region->mmap_alias != MAP_FAILED,
+			    "mmap of alias failed, errno: %i", errno);
+
+		/* Align host alias address */
+		region->host_alias = align(region->mmap_alias, alignment);
+	}
 }
 
 /*
@@ -1239,6 +1252,44 @@ vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
 	return -1;
 }
 
+/*
+ * Address VM physical to Host Virtual *alias*.
+ *
+ * Input Args:
+ *   vm - Virtual Machine
+ *   gpa - VM physical address
+ *
+ * Output Args: None
+ *
+ * Return:
+ *   Equivalent address within the host virtual *alias* area, or NULL
+ *   (without failing the test) if the guest memory is not shared (so
+ *   no alias exists).
+ *
+ * When vm_create() and related functions are called with a shared memory
+ * src_type, we also create a writable, shared alias mapping of the
+ * underlying guest memory. This allows the host to manipulate guest memory
+ * without mapping that memory in the guest's address space. And, for
+ * userfaultfd-based demand paging, we can do so without triggering userfaults.
+ */
+void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)
+{
+	struct userspace_mem_region *region;
+
+	list_for_each_entry(region, &vm->userspace_mem_regions, list) {
+		if (!region->host_alias)
+			continue;
+
+		if ((gpa >= region->region.guest_phys_addr)
+			&& (gpa <= (region->region.guest_phys_addr
+				+ region->region.memory_size - 1)))
+			return (void *) ((uintptr_t) region->host_alias
+				+ (gpa - region->region.guest_phys_addr));
+	}
+
+	return NULL;
+}
+
 /*
  * VM Create IRQ Chip
  *
diff --git a/tools/testing/selftests/kvm/lib/kvm_util_internal.h b/tools/testing/selftests/kvm/lib/kvm_util_internal.h
index 91ce1b5d480b..a25af33d4a9c 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util_internal.h
+++ b/tools/testing/selftests/kvm/lib/kvm_util_internal.h
@@ -16,7 +16,9 @@ struct userspace_mem_region {
 	int fd;
 	off_t offset;
 	void *host_mem;
+	void *host_alias;
 	void *mmap_start;
+	void *mmap_alias;
 	size_t mmap_size;
 	struct list_head list;
 };
-- 
2.31.1.751.gd2f1c929bd-goog


  parent reply	other threads:[~2021-05-19 20:04 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-19 20:03 [PATCH v2 00/10] KVM: selftests: exercise userfaultfd minor faults Axel Rasmussen
2021-05-19 20:03 ` [PATCH v2 01/10] KVM: selftests: trivial comment/logging fixes Axel Rasmussen
2021-05-19 21:41   ` Ben Gardon
2021-05-19 20:03 ` [PATCH v2 02/10] KVM: selftests: simplify setup_demand_paging error handling Axel Rasmussen
2021-05-19 21:45   ` Ben Gardon
2021-05-19 22:14     ` Axel Rasmussen
2021-05-19 22:23       ` Ben Gardon
2021-05-24 13:25       ` Paolo Bonzini
2021-05-19 20:03 ` [PATCH v2 03/10] KVM: selftests: print a message when skipping KVM tests Axel Rasmussen
2021-05-19 21:49   ` Ben Gardon
2021-05-24 13:23     ` Paolo Bonzini
2021-05-19 20:03 ` [PATCH v2 04/10] KVM: selftests: compute correct demand paging size Axel Rasmussen
2021-05-19 21:51   ` Ben Gardon
2021-05-19 20:03 ` [PATCH v2 05/10] KVM: selftests: allow different backing source types Axel Rasmussen
2021-05-19 21:53   ` Ben Gardon
2021-05-19 20:03 ` [PATCH v2 06/10] KVM: selftests: refactor vm_mem_backing_src_type flags Axel Rasmussen
2021-05-19 22:02   ` Ben Gardon
2021-05-19 22:16     ` Axel Rasmussen
2021-05-19 22:25       ` Ben Gardon
2021-05-19 20:03 ` [PATCH v2 07/10] KVM: selftests: add shmem backing source type Axel Rasmussen
2021-05-19 22:03   ` Ben Gardon
2021-05-19 20:03 ` Axel Rasmussen [this message]
2021-05-25 23:49   ` [PATCH v2 08/10] KVM: selftests: create alias mappings when using shared memory David Matlack
2021-05-26 17:22     ` Axel Rasmussen
2021-05-26 18:31       ` Paolo Bonzini
2021-05-19 20:03 ` [PATCH v2 09/10] KVM: selftests: allow using UFFD minor faults for demand paging Axel Rasmussen
2021-05-19 22:20   ` Ben Gardon
2021-05-19 22:34     ` Axel Rasmussen
2021-05-24 13:36     ` Paolo Bonzini
2021-05-19 20:03 ` [PATCH v2 10/10] KVM: selftests: add shared hugetlbfs backing source type Axel Rasmussen
2021-05-19 22:22   ` Ben Gardon
2021-05-24 13:38 ` [PATCH v2 00/10] KVM: selftests: exercise userfaultfd minor faults Paolo Bonzini

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=20210519200339.829146-9-axelrasmussen@google.com \
    --to=axelrasmussen@google.com \
    --cc=aaronlewis@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=bgardon@google.com \
    --cc=drjones@redhat.com \
    --cc=eesposit@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=graf@amazon.com \
    --cc=jacobhxu@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=makarandsonare@google.com \
    --cc=oupton@google.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=shuah@kernel.org \
    --cc=wangyanan55@huawei.com \
    /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;
as well as URLs for NNTP newsgroup(s).