From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48B5218C2E for ; Mon, 6 Nov 2023 11:26:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Hmug/IcM" Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FCB5BB for ; Mon, 6 Nov 2023 03:26:50 -0800 (PST) Received: by mail-qv1-xf34.google.com with SMTP id 6a1803df08f44-67131800219so30511136d6.3 for ; Mon, 06 Nov 2023 03:26:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699270010; x=1699874810; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=bcdCS6aA0hU/P/1Lxpxkfas8L67FF+zkKmd2vDLbKHk=; b=Hmug/IcMuWLcWO11j6vSFnt2652G6K+eWeDxe18rWZL9wyC+bPveocbxbcN/JlBsAQ zPDyyPmt6RXGDIF7neCe4+h7HYWvB+obEy0KzLPUxK8QR9h+Ofi1I4c+bR3YKRcmGt6Q 6gOptmV/oUBNO0l3CxF2JfN3xqVVFHMa+UAt9COnCE4SZfF4CwVHoTdAFD8uxN6DEg13 4DeizrP24RbE5SUqomX/sD+ZCBEGQTSGa/vgyLdseUzclzlnePMlmam6vrbOGAoZUtz9 RlE1YWY+ZR2mbZbrYpzQZ9jhkpjVPLbXjARg6Z19SoB9039QBQWNDpiCqqGptotmpSot q3Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699270010; x=1699874810; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bcdCS6aA0hU/P/1Lxpxkfas8L67FF+zkKmd2vDLbKHk=; b=LXWOJOMIyDld/q7HjS5RZ35Tj7+ebSlIMxLwgpu+0AwVFqg9r4T3X0hMOMAGSzBnXC frLmEGHOu5QZ/ilu+eoB8uTeYOQWb+2WeCD9KtlwN/4LJaYmghdCMlTMn4wSjxpgtqrb 2KBRN/ZMpslT6SkRD8MdG1c1ylHR2QRPgK5C89zo/5YEbgKvEVFxvqdGKvxtv9lVosgj KQC6d7buq/mfa//cLoEmUDfscmCoXsjtHCRUJjBP0cZeMu/1rXlu6xLMSs73yDfNxm92 +xH2vdgkFMAlmtdVjxNS6ZVDrZDWiEynq5BnA0/sfOAfx9zLEOZz0Ixtmr9uJKlB9+Au OiLw== X-Gm-Message-State: AOJu0YwWTEaCJo/QsbHCYoj1ePp0JJY/5A31YIA6J4dtNac8GvosJtX6 GMs7SaMCOBP8H0JrrSYSo8oyN7PBlb67bZYPPHispA== X-Google-Smtp-Source: AGHT+IFasxLYHHudhBN/+4eIX1OsOhIRfSB2CWnjQO7Y6r4dz1/Ca+R2Pv6YHi7+A5fsQfNYO2/OlOsYvWwG9CsrzMM= X-Received: by 2002:a05:6214:2506:b0:658:26d7:72e0 with SMTP id gf6-20020a056214250600b0065826d772e0mr20208405qvb.4.1699270009660; Mon, 06 Nov 2023 03:26:49 -0800 (PST) Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> <20231027182217.3615211-28-seanjc@google.com> In-Reply-To: <20231027182217.3615211-28-seanjc@google.com> From: Fuad Tabba Date: Mon, 6 Nov 2023 11:26:13 +0000 Message-ID: Subject: Re: [PATCH v13 27/35] KVM: selftests: Add helpers to convert guest memory b/w private and shared To: Sean Christopherson Cc: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , =?UTF-8?B?TWlja2HDq2wgU2FsYcO8bg==?= , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Oct 27, 2023 at 7:23=E2=80=AFPM Sean Christopherson wrote: > > From: Vishal Annapurve > > Add helpers to convert memory between private and shared via KVM's > memory attributes, as well as helpers to free/allocate guest_memfd memory > via fallocate(). Userspace, i.e. tests, is NOT required to do fallocate(= ) > when converting memory, as the attributes are the single source of true. true->truth > Provide allocate() helpers so that tests can mimic a userspace that frees > private memory on conversion, e.g. to prioritize memory usage over > performance. > > Signed-off-by: Vishal Annapurve > Co-developed-by: Sean Christopherson > Signed-off-by: Sean Christopherson > --- > .../selftests/kvm/include/kvm_util_base.h | 48 +++++++++++++++++++ > tools/testing/selftests/kvm/lib/kvm_util.c | 28 +++++++++++ > 2 files changed, 76 insertions(+) > > diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/= testing/selftests/kvm/include/kvm_util_base.h > index 9f861182c02a..1441fca6c273 100644 > --- a/tools/testing/selftests/kvm/include/kvm_util_base.h > +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h > @@ -333,6 +333,54 @@ static inline void vm_enable_cap(struct kvm_vm *vm, = uint32_t cap, uint64_t arg0) > vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); > } > > +static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t = gpa, > + uint64_t size, uint64_t attri= butes) > +{ > + struct kvm_memory_attributes attr =3D { > + .attributes =3D attributes, > + .address =3D gpa, > + .size =3D size, > + .flags =3D 0, > + }; > + > + /* > + * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These = flows > + * need significant enhancements to support multiple attributes. > + */ > + TEST_ASSERT(!attributes || attributes =3D=3D KVM_MEMORY_ATTRIBUTE= _PRIVATE, > + "Update me to support multiple attributes!"); > + > + vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr); > +} > + > + > +static inline void vm_mem_set_private(struct kvm_vm *vm, uint64_t gpa, > + uint64_t size) > +{ > + vm_set_memory_attributes(vm, gpa, size, KVM_MEMORY_ATTRIBUTE_PRIV= ATE); > +} > + > +static inline void vm_mem_set_shared(struct kvm_vm *vm, uint64_t gpa, > + uint64_t size) > +{ > + vm_set_memory_attributes(vm, gpa, size, 0); > +} > + > +void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t si= ze, > + bool punch_hole); > + > +static inline void vm_guest_mem_punch_hole(struct kvm_vm *vm, uint64_t g= pa, > + uint64_t size) > +{ > + vm_guest_mem_fallocate(vm, gpa, size, true); > +} > + > +static inline void vm_guest_mem_allocate(struct kvm_vm *vm, uint64_t gpa= , > + uint64_t size) > +{ > + vm_guest_mem_fallocate(vm, gpa, size, false); > +} > + > void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); > const char *vm_guest_mode_string(uint32_t i); > > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/s= elftests/kvm/lib/kvm_util.c > index 45050f54701a..a140aee8d0f5 100644 > --- a/tools/testing/selftests/kvm/lib/kvm_util.c > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c > @@ -1176,6 +1176,34 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint3= 2_t slot) > __vm_mem_region_delete(vm, memslot2region(vm, slot), true); > } > > +void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t s= ize, > + bool punch_hole) > +{ > + const int mode =3D FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_= PUNCH_HOLE : 0); > + struct userspace_mem_region *region; > + uint64_t end =3D base + size; > + uint64_t gpa, len; > + off_t fd_offset; > + int ret; > + > + for (gpa =3D base; gpa < end; gpa +=3D len) { > + uint64_t offset; > + > + region =3D userspace_mem_region_find(vm, gpa, gpa); > + TEST_ASSERT(region && region->region.flags & KVM_MEM_PRIV= ATE, > + "Private memory region not found for GPA 0x%l= x", gpa); > + > + offset =3D (gpa - region->region.guest_phys_addr); nit: why the parentheses? > + fd_offset =3D region->region.guest_memfd_offset + offset; > + len =3D min_t(uint64_t, end - gpa, region->region.memory_= size - offset); > + > + ret =3D fallocate(region->region.guest_memfd, mode, fd_of= fset, len); > + TEST_ASSERT(!ret, "fallocate() failed to %s at %lx (len = =3D %lu), fd =3D %d, mode =3D %x, offset =3D %lx\n", > + punch_hole ? "punch hole" : "allocate", gpa, = len, > + region->region.guest_memfd, mode, fd_offset); > + } > +} > + Nits aside: Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba Cheers, /fuad > /* Returns the size of a vCPU's kvm_run structure. */ > static int vcpu_mmap_sz(void) > { > -- > 2.42.0.820.g83a721a137-goog >