Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2050978rdf; Mon, 6 Nov 2023 03:25:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgfp8iE5zeJAjoK6nC6zGIPWl+qDQlMr3900qfuDip2zTGDyFEt6l2QeD9uFizCE2Q2OXW X-Received: by 2002:a17:90a:35c:b0:27f:fa4c:ebaf with SMTP id 28-20020a17090a035c00b0027ffa4cebafmr21799345pjf.14.1699269954174; Mon, 06 Nov 2023 03:25:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699269954; cv=none; d=google.com; s=arc-20160816; b=uAaqGSwuqQW70h8FNsZLyItu55KCovElCbWk8Rjjoi6iDhWjCDke7CYWIZDFC/WBzC ojGhjsXV0/e63Hbcj9lEyfEmj2kv/vafETuBKFvRaFyheLyLkFONc2xkSqcVh3wzAlG9 SZgwif5He2U+CjmqzhvaEohvtzlSzjDFe/ukk1y9swn5GMfE5KxvjnGnHYO2VQBQ9ygZ gSGfteVatbj9IMieZ5B1ccybK1iEWCHXyth2VWUC5bfCGmXL9ze4rI3/BkmY/L8ztYZo gBMIa/is2Fk//ZX/LVLfqh+Nzn3XwjYhAf96/f3mpxqYg7cwWIIOk+qiGDvhdLWhSkrt 9Low== 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=8FgFIIfzRIOk5S3UyWAM1hrnYX4eHCPdXXTgA8O3M1A=; fh=6NDLbPegWN39ihzRI+XD38qv7OUBTPlYZUUvtWMZU2Q=; b=p8+XB9lSUEmt/nWogBlnjKa//12mr9rVi0PhRpVEZk9FQs8RNpvxzUoOq4L0m4TkHW UYmtq2uoFa0laHXe05afZY5WQQiDkhd3rsUmXOzd1QcTXOThfvwjc4rMIz1e3x7b2W68 ALihsfotYBs18lQUwSPrsD1YbHF0oN7SszOGIRMEIHngPp7nUKRwcxxU7dxp78wd+37U rA7vLi/BuLiTrWJE/EtDb3BaArcoGgdGkC3RBcVN3HngjmPgIgBoZodaLDQAFzfRvvK/ jCkG/sB+nBDIAQGiv2P4gqbJgcQ+NhmwfIwgjvjD+e8hh6NAGUnxUeUC9qwhdgO8Vas3 j54Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CSAKZPPU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id a11-20020a17090abe0b00b00278f6d616aasi7966445pjs.71.2023.11.06.03.25.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 03:25:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CSAKZPPU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 6379B806500C; Mon, 6 Nov 2023 03:25:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230522AbjKFLZg (ORCPT + 99 others); Mon, 6 Nov 2023 06:25:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbjKFLZf (ORCPT ); Mon, 6 Nov 2023 06:25:35 -0500 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1211EBB for ; Mon, 6 Nov 2023 03:25:32 -0800 (PST) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-778999c5ecfso283870085a.2 for ; Mon, 06 Nov 2023 03:25:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699269931; x=1699874731; 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=8FgFIIfzRIOk5S3UyWAM1hrnYX4eHCPdXXTgA8O3M1A=; b=CSAKZPPUhSeyEd+FVcFc/wE7BH7/lltaIavlc/tV7hlCdyKd83MMBTrV4+2QdjaVI5 re4Un19fI8++TRdDNzRRKVcWvhkLL5hRE/PqjHicq6Yg52UVbIXndvrRV6Y+TqCqW1KP qwPYKB/RiUmn66O/9+Cxdqjq0ID/uK5pDYKZLyw9rZRF4ogasppO4z/41nUiWVolLMta dMqk+CU3+vSnN3anBCRC9fxV98Q/ooEzqgWQN4zqGELxiSKQ8Q3rnh5J5cUjgD/5TWLv B1bAVx7MMqrfiqcClz5zDUFdg87t03JoNLveKQUJkQ3+GPd+OuaUIRgcoruGyCpDK+Lr x1cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699269931; x=1699874731; 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=8FgFIIfzRIOk5S3UyWAM1hrnYX4eHCPdXXTgA8O3M1A=; b=TZGniFU+h3aSkGdPa+yzWaKJ/iipCpQR4AdXLXU+IK6djsqgqdvaHQCZACdujBHncp pnrsfwAluXCqtlC8yuK4nSfCFGuYDXKtykCLEejHPzcoFPXqkCsCz/Y8HMVoVG+X2eEP /X81kgBdR9m7opAo2AbQI3WCt/pNH0I9c66EJx5aeiTqJlhd7+YFgXszHpwPucPNVdFM fufzPNjj83e1ELM1Wp2i9tRK6yAE+WxqeGywewRa9k1pNXiCYu6T26S4E4V+dIBoHSbr jsrS57jtQI7OvHZwJAD/BOZV7IhUcUvTuN0Bbyp6WUIhtbdAk1LTO3uxowvZBPJ93t3B +6lg== X-Gm-Message-State: AOJu0YyiFCVbLHCPGOHm2LX7e+KOequJniK2nJWnaGezHtNORk8uJqF+ vSycJN6CWy0i0FAhZEsTAMhP0JPuCM5qRF78mujhCQ== X-Received: by 2002:a05:6214:ac9:b0:671:7312:bf5 with SMTP id g9-20020a0562140ac900b0067173120bf5mr32557664qvi.17.1699269931062; Mon, 06 Nov 2023 03:25:31 -0800 (PST) MIME-Version: 1.0 References: <20231105163040.14904-1-pbonzini@redhat.com> <20231105163040.14904-26-pbonzini@redhat.com> In-Reply-To: <20231105163040.14904-26-pbonzini@redhat.com> From: Fuad Tabba Date: Mon, 6 Nov 2023 11:24:54 +0000 Message-ID: Subject: Re: [PATCH 25/34] KVM: selftests: Add helpers to convert guest memory b/w private and shared To: Paolo Bonzini Cc: Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , 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=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Mon, 06 Nov 2023 03:25:46 -0800 (PST) On Sun, Nov 5, 2023 at 4:34=E2=80=AFPM Paolo Bonzini = 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. nit: 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 > Message-Id: <20231027182217.3615211-28-seanjc@google.com> > Signed-off-by: Paolo Bonzini > --- > .../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 b63500fca627..95a553400ea9 100644 > --- a/tools/testing/selftests/kvm/lib/kvm_util.c > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c > @@ -1167,6 +1167,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_GUES= T_MEMFD, > + "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); > + } > +} > + > /* Returns the size of a vCPU's kvm_run structure. */ > static int vcpu_mmap_sz(void) > { Nits aside: Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba Cheers, /fuad > -- > 2.39.1 > >