mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Michael Roth <michael.roth@amd.com>
Subject: [PATCH gmem 1/5] KVM: selftests: Rework fallocate() helper to work across multiple memslots
Date: Mon, 23 Oct 2023 12:15:28 -0700	[thread overview]
Message-ID: <20231023191532.2405326-2-seanjc@google.com> (raw)
In-Reply-To: <20231023191532.2405326-1-seanjc@google.com>

Rework vm_guest_mem_fallocate() to play nice with ranges that cover more
than one memslot.  Converting a range that covers multiple memslots is
most definitely an interesting testcase, and there's no reason to force
the caller to manually shatter the range, especially since the size of
the region might not be known by the caller.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 34 ++++++++++++----------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 8fc70c021c1c..33bc2c6be970 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1206,30 +1206,32 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)
 	__vm_mem_region_delete(vm, memslot2region(vm, slot), true);
 }
 
-void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t size,
+void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size,
 			    bool punch_hole)
 {
+	const int mode = FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_PUNCH_HOLE : 0);
 	struct userspace_mem_region *region;
-	uint64_t end = gpa + size - 1;
+	uint64_t end = base + size;
+	uint64_t gpa, len;
 	off_t fd_offset;
-	int mode, ret;
+	int ret;
 
-	region = userspace_mem_region_find(vm, gpa, gpa);
-	TEST_ASSERT(region && region->region.flags & KVM_MEM_PRIVATE,
-		    "Private memory region not found for GPA 0x%lx", gpa);
+	for (gpa = base; gpa < end; gpa += len) {
+		uint64_t offset;
 
-	TEST_ASSERT(region == userspace_mem_region_find(vm, end, end),
-		    "fallocate() for guest_memfd must act on a single memslot");
+		region = userspace_mem_region_find(vm, gpa, gpa);
+		TEST_ASSERT(region && region->region.flags & KVM_MEM_PRIVATE,
+			    "Private memory region not found for GPA 0x%lx", gpa);
 
-	fd_offset = region->region.gmem_offset +
-		    (gpa - region->region.guest_phys_addr);
+		offset = (gpa - region->region.guest_phys_addr);
+		fd_offset = region->region.gmem_offset + offset;
+		len = min_t(uint64_t, end - gpa, region->region.memory_size - offset);
 
-	mode = FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_PUNCH_HOLE : 0);
-
-	ret = fallocate(region->region.gmem_fd, mode, fd_offset, size);
-	TEST_ASSERT(!ret, "fallocate() failed to %s at %lx[%lu], fd = %d, mode = %x, offset = %lx\n",
-		     punch_hole ? "punch hole" : "allocate", gpa, size,
-		     region->region.gmem_fd, mode, fd_offset);
+		ret = fallocate(region->region.gmem_fd, mode, fd_offset, len);
+		TEST_ASSERT(!ret, "fallocate() failed to %s at %lx (len = %lu), fd = %d, mode = %x, offset = %lx\n",
+			    punch_hole ? "punch hole" : "allocate", gpa, len,
+			    region->region.gmem_fd, mode, fd_offset);
+	}
 }
 
 /* Returns the size of a vCPU's kvm_run structure. */
-- 
2.42.0.758.gaed0368e0e-goog


  reply	other threads:[~2023-10-23 19:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23 19:15 [PATCH gmem 0/5] KVM: selftests: Fix multiple memslots in conv test Sean Christopherson
2023-10-23 19:15 ` Sean Christopherson [this message]
2023-10-23 19:15 ` [PATCH gmem 2/5] KVM: selftests: Handle memslot splits in private mem conversions test Sean Christopherson
2023-10-23 19:15 ` [PATCH gmem 3/5] KVM: selftests: Let user specify nr of memslots in private mem conversion Sean Christopherson
2023-10-23 19:15 ` [PATCH gmem 4/5] KVM: selftests: Use dedicated pattern for testing that mem is shared by default Sean Christopherson
2023-10-23 19:15 ` [PATCH gmem 5/5] KVM: selftests: Verify default pattern was written in private mem conversion Sean Christopherson

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=20231023191532.2405326-2-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.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

all inboxes | Powered by JetHome®