Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4058901rdb; Thu, 14 Sep 2023 10:26:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHFKDSeEvUHNs77xUFwexKEyGt3842abdeA04QR9ZJc0zNb4BWPM+/RMaw1+Eu2qtWAHiBt X-Received: by 2002:a17:903:507:b0:1bb:b91b:2b3c with SMTP id jn7-20020a170903050700b001bbb91b2b3cmr5715074plb.34.1694712377832; Thu, 14 Sep 2023 10:26:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694712377; cv=none; d=google.com; s=arc-20160816; b=RBHtaezmVe/HL3Yy6chLoTi67Kza0BdbZf5q6qCncKiIl8kxK7zp9Emu9U41soo754 7RYHj5rcWpiy2BGxfgpwoUwSUgtXcJNos9Mn5iUG0ADBk/RoowUY5gkShtoIcLW3tlQb kc0pn22VJdFsWxQ6XVCRA4xYRSjUFDBkWgQMWKBs1/MOe4DLFjO+7VLhK6auuv9U2fVK u/kumLxrx/7Hy4fljP1JLS23azWmZAwaqpUjkEfmXTNOejjtAg6EeWlDZPXozPoHwv3L Dh9XIcVJ1IHJIhEze2pkBLeJXr4BUrV3IZbMkcpVBEyfciMEfqILmIfRt9ajYVnTyrt8 pQdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; fh=61hsfVoef5Tbbo+Rm06/Hxsz4fAtyORDF8Po5ZVRZDI=; b=agKwPdb7colJT3I1PVHIMAkGqmvgauXkEt0AwL6qrzK6WZAz3FXN+yUCgvf6A6VVzD wCX9EAkbdaaaAaHd8DkUCvC7TtMdRFimJYdcHlP6DYCpAUKQjC4DhuvRr/uTvaS6eDxJ M7ry+EnHSCGINxtck99n0CtlUY7VSHB0lo1DUytVtmvv16i8FNDsBbVerlFQaDseocpT 1vy/qm1XTgf65GPmD1ye3xDoFl+AWoPdKoWj4QzqiQ9cJV6ueWYd9iA1BqHutRXFcHlv K2vWLpDrvxOSy9R/uoirAWiNglTBpkC5OouN9qXfvh6ZL2vyIyiLQVCstD1wWxQUPC96 uxwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TdMYbtuF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id kn7-20020a170903078700b001b67bdc438csi1866280plb.376.2023.09.14.10.26.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 10:26:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TdMYbtuF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id AF34E827142C; Wed, 13 Sep 2023 19:01:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234686AbjINCAr (ORCPT + 99 others); Wed, 13 Sep 2023 22:00:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234497AbjINB7r (ORCPT ); Wed, 13 Sep 2023 21:59:47 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB8AD3AA0 for ; Wed, 13 Sep 2023 18:56:26 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1c31359832dso3886955ad.3 for ; Wed, 13 Sep 2023 18:56:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656586; x=1695261386; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=TdMYbtuFLGzrg9+YhxzJTvOj/Bg4p2VNaAzd3I3qEOPE5gO/Tf07JdQStwpG7ufQ0L FShE8UrgqImO8b6MqtuI7k0aw8W1SvFDDmRqx2wvqCl31oqLexq3VGFfJhCwu87BND9T A376jl/pWr4/WoD3DrXDCcRZsgscbNXeIG7CbXXHInwEPhw2/ULsvkOBDhkhGszV6QWM 9S7y1JtR4OQoCC4BskE2ncXU+M2CWBXHs6KmUlb8+dIFavtm6JYXnJCyg1JkpE3Ng/MY HGLgwkPYvQ1gLm+sjzgQBN0rnwlLXn12EKVuKLCwtV89+M4pYF2/3nE0s1jWvoQhmoQL v7iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656586; x=1695261386; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=iHM0LjnVDKy+a5SRX/W6alkoxMFHD4GB8QIvXpAvsJxgOb1arK/WSAMztImmOOJ2lD OFrDuUeZarNXx4aqWRVaidEDlFlbt+sYQuyjosya5CWkyRkxKq9IuYqAh8cp21UKB7Yx sa/bIOeFWonBqWCwFYvLl+h3hvcDR92EN+N+Oi+mM7m39H3Bvy2+zeKURoWCDOIrkU0h mGOcOw5gLI0ScOWJITFgulKYr0Vqgkg4WoPAcbJeAYWUWjvur5S537iZeUYAr3o9np5w rif8/T8VcvWTo+EdND6iP2dx6GoqwKxx/EWggbTJE69N9S4QhKEx2Pz+NXHItfkKIAJO 0j9A== X-Gm-Message-State: AOJu0YyVAQRmdD1EInYi7JMa4nXUVLaLKNl90Le0jOliPypyJDCDgFY2 fTcD2/5h41GCvpQ34c5Bkh5TgrX6vQk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:da8e:b0:1c0:e87e:52b9 with SMTP id j14-20020a170902da8e00b001c0e87e52b9mr204011plx.12.1694656585587; Wed, 13 Sep 2023 18:56:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:23 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-26-seanjc@google.com> Subject: [RFC PATCH v12 25/33] KVM: selftests: Add helpers to convert guest memory b/w private and shared From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: 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-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , 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" 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 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 19:01:00 -0700 (PDT) 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. The fallocate() helpers are provided 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 | 26 ++++++++++ 2 files changed, 74 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 47ea25f9dc97..a0315503ac3e 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 attributes) +{ + struct kvm_memory_attributes attr = { + .attributes = attributes, + .address = gpa, + .size = size, + .flags = 0, + }; + + /* + * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows + * need significant enhancements to support multiple attributes. + */ + TEST_ASSERT(!attributes || attributes == 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_PRIVATE); +} + +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 size, + bool punch_hole); + +static inline void vm_guest_mem_punch_hole(struct kvm_vm *vm, uint64_t gpa, + 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/selftests/kvm/lib/kvm_util.c index 127f44c6c83c..bf2bd5c39a96 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1176,6 +1176,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, + bool punch_hole) +{ + struct userspace_mem_region *region; + uint64_t end = gpa + size - 1; + off_t fd_offset; + int mode, 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); + + TEST_ASSERT(region == userspace_mem_region_find(vm, end, end), + "fallocate() for guest_memfd must act on a single memslot"); + + fd_offset = region->region.gmem_offset + + (gpa - region->region.guest_phys_addr); + + 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); +} + /* Returns the size of a vCPU's kvm_run structure. */ static int vcpu_mmap_sz(void) { -- 2.42.0.283.g2d96d420d3-goog