Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2051558rdf; Mon, 6 Nov 2023 03:27:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjxwgEZ360i458WeghwM4yN7ax9CepLwtXVDtJVta/FQAYHVD1xAX873mcD5mm1WwySALJ X-Received: by 2002:a17:903:68d:b0:1c9:e0f9:a676 with SMTP id ki13-20020a170903068d00b001c9e0f9a676mr19662799plb.6.1699270031279; Mon, 06 Nov 2023 03:27:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699270031; cv=none; d=google.com; s=arc-20160816; b=bdkOpq1C84zrTdTS/PwvgTn+dQk0TQ6FEGn3TK7NFp3jEF48xH/S6qg4ETihDm6ItI cwHgDxjkQXlTCTSHIjdGjjE20Vz10ZHfr5eFiHai7bW3veYKD8wVsLz9KIfaNUt50S7I qtmSYauZs10jfspqzhuf7bYu7PBu/c/L18kAaS/Ao956lKIBaJIg/+sdH5jVX4U/ibBY kEra9kFeubFPqetIdinE27bV/eNFhcsqz1rGpmSMKKeVB8OHBE2QqvvOgQPp3vZufsN8 COYNtA52b4cN7k7SRW0nP/Oiz70w0vbIo8JL8ET4rtF4eE5Oyr1q02H+aXOEgl4+zHA8 QEPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=bcdCS6aA0hU/P/1Lxpxkfas8L67FF+zkKmd2vDLbKHk=; fh=xK+wiX2TTq7O2X4TlNj4MXVc7h6ma8eyuy1Ur/RMLHE=; b=Rxd2lOCYsg9vXffdWf+rdNieg5dJqv4NwA0/d8V4l5BoHcamnCxGEcTXuAEmVPbGoK PvtCWL7G4+LnMm7x4I284W057yT8udTSQoEfzPWNpV9YINb41UTljhfgvNRftPecQJSk YrEShQtnELA3F9GpfptvHCXF9cDToISTKjAhTCsCFjEujAmqHCW9osGiPLKk+dLw+PZn dOa2mZaq8BCGqZtHZTmq3EbxpH1AZrLZbITnyI8s/V+SMAzyt4H14topqfKEBvD7m7wB ERrEhqlT8/kRCzFySGwFdGg1VrlNXA+WwGiR0KEW9Yfd95K9PSMm9gBWzyaxpRjs1lRI KGYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hmug/IcM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id ix19-20020a170902f81300b001c9bdaa81e3si7697259plb.633.2023.11.06.03.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:27:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Hmug/IcM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 5974180657D7; Mon, 6 Nov 2023 03:26:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229583AbjKFL04 (ORCPT + 99 others); Mon, 6 Nov 2023 06:26:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229974AbjKFL0y (ORCPT ); Mon, 6 Nov 2023 06:26:54 -0500 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0A97BE for ; Mon, 6 Nov 2023 03:26:50 -0800 (PST) Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-66fa16092c0so30688196d6.0 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=IPJcKPNCt8BVgu4FKXFaC/CYyhTNXbd5fXZMfo3KRmr4BffzMHvpjtVSpYDuvW6gvt AFQDSRgIpMdWR5xAa+riafAkoZjEvIbgFx1nglxF73M0sP0QLMwFCcNqx04GkA/qkGz8 jnmSTplfbnI+fiz8oMcjLaXJFH2pIiPpvMvtE//R3GR/5ApK5Sk5pAESJOe0HlBKhHE+ aOKroow1SaUFup+SU8zTGKdOuG1onBWa5Vd++38jkj3B0xud8ocF63RlIkKbiywYPkqi sh6qgP2TJH+hH4ZXmWUwxp/HRmXQrzN0xU7QLI3Z1DBZNrojLlGrwUb+LxE1SZBG1o3F NhpQ== X-Gm-Message-State: AOJu0Yy6mpxao+cpZ5cV7myLjOxqX70TcPkFLAQL8Qs8iMwnPYpWCVUr HKrACevMJH3ZJAcyobtIBlseo+sHPcaG9UT4ln90Iw== 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) 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 X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 03:26:58 -0800 (PST) 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 >