From patchwork Fri Oct 27 18:22:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13439033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C601C25B47 for ; Fri, 27 Oct 2023 18:29:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 33CAF8002B; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ECBE80018; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B44F8002B; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0CE8B80018 for ; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DA35A1A092B for ; Fri, 27 Oct 2023 18:29:34 +0000 (UTC) X-FDA: 81392079468.20.5C0ABF0 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 1C0B340025 for ; Fri, 27 Oct 2023 18:29:32 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="YKK//JI5"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com designates 209.85.166.201 as permitted sender) smtp.mailfrom=3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698431373; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yVf4MoQ+pgFottQ0gtqF7KbCgtXqLOOMhwiDUmO9tI4=; b=oi8cZsYPcqE7Dvuop6Slx4tXmuWMh6CzC1dYoja7WiZ9GdUQEYrj1YKIsayET6+zRNizMr twlDG/kNLNZJpPcD1iCSPpmVgM1x2nVDYYRwPPtTkp32Yrz3FG9LbEpTeakNtmhDjU5v5Y f6FV6sMgWxpG2Mq7+s3EQoUd3yRhKsc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="YKK//JI5"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com designates 209.85.166.201 as permitted sender) smtp.mailfrom=3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431373; a=rsa-sha256; cv=none; b=n2LHAnIn3Ns5haZmo2sLxJdVzkYYgghfKXoTFvRDCW8p9KGOiGEDhH3mnsBlXicQPaHRWK 1SSkNA9/4JzXPUXVGg3A8FL37bV93vowO7ySPNhbXgjBWj0Yc7bTLKQQWEa1qVQBHjIM6S 17e4KRTr6tthcr3giOmMMzrVZboU/dY= Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-3574c545ad2so21299755ab.3 for ; Fri, 27 Oct 2023 11:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431372; x=1699036172; darn=kvack.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=yVf4MoQ+pgFottQ0gtqF7KbCgtXqLOOMhwiDUmO9tI4=; b=YKK//JI5LP2toIf6VFKMzOYEsybIF7rZQ/tikhT4/gwaDGLP8lEJI7JMTPL2SZ3Jf9 qhc6yS+gjcESfKGOz9FMcw76mbYlJPUso3ToyfeWvEQlhyg3rpsLe+loHvoxHJ7dUHgu /Qkl8r/KvYgeCsM3xDneKuSeUvSvs9iK75BQ5auIk57NJXxzrkAy4m430t4gWdRO8WET y4/WjYVLoQnvvC1LcDX188Q0/LKb/RpaMTj0pq2YaQ5k4d2AG/1xI6GsdbhtrdSWminx MmYby5rbOYxowxLdi909BWySJUnb/wBmCw5tn8ZcTXJTA/CT23y/vPcncQWFGervesbe aA9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431372; x=1699036172; 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=yVf4MoQ+pgFottQ0gtqF7KbCgtXqLOOMhwiDUmO9tI4=; b=LnOlPW+mczYAdtP/ByYTdWtnCCS2CCmgJspDEFhorPuDIV6B+cH9OutOimojyofpEt /pHVNdSxkt1uVBOVMtV5q6W1AE6+okoE9xyGTIVEWiY3vvTqH71MhPz7005zsN5JTNMO +Ne+NICRPSGzbaG0/3Bj3Nnm0zNDVFoeljG+FvOGeyLYugGYKn+rj8IrPEKR7FtuQCIr XKHgQZXLgJGvS7D++J6RBhyH3wlPC17MzJAvW+mrBs3fJVwU6We0kArNz8walwuUHMlE s/gSGniQtzFcR5buDvT0DVkdovzj2g+fFxBYOZlCJo2E4Zmt3zqTb+p/hocuol1L/Sd9 LAsw== X-Gm-Message-State: AOJu0YwtqL9HTxXoTFgzIqlBTWzcgDAz4EZkXM9w2hu95kxrqCkHXtEJ 6UGq5appcp27AneZItviVcqT/Dbi16Y= X-Google-Smtp-Source: AGHT+IG9XwjMpXerybQ74hZWKPyWlQIkLL60UP7vz/JRGdDaUmVGZC9suFSomOjVdAtgwjkxyK2jTqS148U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:2fce:0:b0:d9a:f3dc:7d19 with SMTP id v197-20020a252fce000000b00d9af3dc7d19mr60503ybv.11.1698430998960; Fri, 27 Oct 2023 11:23:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:09 -0700 In-Reply-To: <20231027182217.3615211-1-seanjc@google.com> Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231027182217.3615211-28-seanjc@google.com> Subject: [PATCH v13 27/35] 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 , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton 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-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , " =?utf-8?q?Micka=C3=ABl_Sala?= =?utf-8?q?=C3=BCn?= " , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 1C0B340025 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6bxnriazjmiym9sr9gpe3c68c6bfxgyk X-HE-Tag: 1698431372-499442 X-HE-Meta: U2FsdGVkX1+gZSbfnkF8H0WTuBIzrskvEesomiPrgZIvbJdnu+Ldf8QgfHTupbUtJZ/g0WZeUd5DFhBqDUXa0En1ekD0ff2ti/rlx66T0zRTLA+uk0ZMDE6SlzcwKqsc25WelEsA5rF04Wd+nZP2n2PhofC6nivKRyycRSMPO+Enw60g3GQMRweXn+dGgdGsqW/1eyZxT5ftL87IAeGHaUvPma/uw0ISSQ0m6+7vfOBWM3UD1Ch6KPs8CsTlMaMiChxj1Dx7mYbDdrXEYRvs7jOJvYe0+IubfHy2Wi1LAL2QNexop1S6V8UiH6MeE2EteMBsD04njpbWSrSr4WpBW71SWRd/4OgF3UPdGj4jpOM4m0h3E7vM/W1CH/v4p7dVresoK6cFPBrUbEq538ybsSuwSRqJ+2ARH01+go0nolemoudupIyRtxz0R7FcoHJ5ltPLhDXBfBhcYFhGfTxqDsjt5Dc4cqPpP1stMBnXkaaGN2omK6yYUg2GdBh3HPOx9Cr97Y2RKYlpQHchzqH3p97+9s3MHXcpas4gPuqZkpRv2CPWOJK8Su3Fubv+3T0h+BOpmUgz9Mn+UbdDOWMH3QHU+r7xZf5jWV75OFEfZgH6lWmTV5M9/zCTbdrlt7sqLfY1jTdU55QPFmcVqndLJWQT+ANnsItV2U7wrcMO31u4zCefUxMpfN3FqLEqkNsv49HNtsc2Oop6JtF1EU/sNs4cakv3vOlvmEp+ryogB+kU55ZSSZL3hxHEKGo1tKuH0B8IaqrIJWKJuIBz6TP/ptRcBYc6HTg+dZ+zK0iZwbSSY4TlSvnFBNeHUYUww7z2A5pHxvfjT6boEsNGwzzYD8wuYaQifxar870DIBxuRQ/4Thkw+KzRBtrF591S8S63gWHSXWudoQLURT8gtkFXVd3UZc4WlHYvNx1vJx8yLLhEv9U+0q9LdqSlGiFsNj3kA8EcHJqmykFxaeaZSPB 8phULkVv ywDkf5UkKE4VO6EFQT/+olCECjEB5a90L1wnkNmw5Lk4b1D/G7w5EfOtfP62cBSWh4NjPsxOtNxjsVsioo6Vfq56OXvPp03F9qOePHlzVZ97szFHHE3B3lt0VjH/YGYkaN0wC1nC6653C9P9SQ3UeiDUQ/2xv+C2DKEPfMU2ki31Phfqz4MJ4y0RpMaakEUalwp1EEy9fBCp0l1+wGmcBp46nzAs48xB0kLHnoMr+xBsK9lsWxy2NNEdhZ6f/8RoJj9kRaqk8SIY2ysyUj3wQOvmGGOAB2ooCZDsucle76+cDYKG7Um7mSp+wzwvz6UMEsUs+y6GZ3LtfdOFy5eABirWZx0gRIJJiVAAj3eSVM/ixxUHKmmx5pPKnO9FF0YtOqPR2bTHUfeZnC9LNDCChTk7mgqdDeVG2Me8EROD3XZUqbzo9yZLeC/94NPsF6Vy3mgrEXUHqtPBxcCZYt4uFv7qdmp94VeJDytujKO6lKZCpOQhYWVzAXnjG4H0GBL5UmrP/cDC+d/GA69KIcMySwOyfLygvJaL1uFr72JLLOOPXbz0iruh0o9ulM1vR2ywvpyVuOJ9PY8iNChtmnXjijYe9LblcjbwoVk5Dpu4Bsk1GCVb388l6cg3Z1h8eQhAeJAkSwbcjobhtR2ryQVVJ08U5qclWxbDhNL+/tpIE7HbQq67VM2L5V9MANK+QBlieuuXMJxfhD9W52TxIBMzvrx+KIBPX6cVouQLgO3X18E3fcmLpqa2O0E8DspfbQAlP2inF8c/v0TTddh3JFvwq3/ZJsxBYfPgytWeygsXCiPpfRA/GGi2Owr9/nQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. 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 Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- .../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 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 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, uint32_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 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 = base + size; + uint64_t gpa, len; + off_t fd_offset; + int ret; + + for (gpa = base; gpa < end; gpa += len) { + uint64_t offset; + + 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); + + offset = (gpa - region->region.guest_phys_addr); + fd_offset = region->region.guest_memfd_offset + offset; + len = min_t(uint64_t, end - gpa, region->region.memory_size - offset); + + ret = fallocate(region->region.guest_memfd, 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.guest_memfd, mode, fd_offset); + } +} + /* Returns the size of a vCPU's kvm_run structure. */ static int vcpu_mmap_sz(void) {