From patchwork Thu Sep 14 01:55:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384032 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 68953EE0216 for ; Thu, 14 Sep 2023 01:56:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 974F28D0008; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FD0B8D0001; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 776F38D0008; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 612808D0001 for ; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 38B26A0F68 for ; Thu, 14 Sep 2023 01:56:29 +0000 (UTC) X-FDA: 81233538498.16.EB5ECC5 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 6921F10000F for ; Thu, 14 Sep 2023 01:56:27 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3X6aX5Rp; spf=pass (imf14.hostedemail.com: domain of 3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656587; a=rsa-sha256; cv=none; b=SqMD+DMw0qRwBViguO3eas3kGFsQO71UWM1nYixhew8cSn7W3zyJ1cYe/4HKpH4jfpTZoa NGPZ4ktneUdofqBTLbrsmbUNiDQ9aMzIBmavvtP6KxnAhTLPp6NDj5UcpSRe3aGA76igT4 b0QoeonGzP6QJmRp0erE8hPehSHLcbQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3X6aX5Rp; spf=pass (imf14.hostedemail.com: domain of 3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656587; 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=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=c3lOEsnsiR5MeuR6iK2svQjC8KA5f69TKEux77dAx7MWtYd9VIKMzcNOlq/laJnhPCEwr7 Y1I5zFZaPUygdtH/Fud7GCtGX9tKcLCB2h9RubVDFrsrDTyCK/wLPzpMNg0eOf20w9x/lF 8SlQ03bTcEOcR53Gbcsefof7hwLfHM8= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c31359832dso3886945ad.3 for ; Wed, 13 Sep 2023 18:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656586; x=1695261386; 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=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=3X6aX5Rp8IHnakrOo7UZRNJ81RAwrIfX4kNwtG2m9Ubw9orEr8da3+gw+azoMJfHaE +0wMpDQTCLmuaaGYvtGcxgwmBSm8p2nQuT9HM25exPoT3dDG9/tAPSF3R6ujP39Ynkgo 0Tu9jJHiJVDmjEKazC4Iv56ziXvoVUXmF6VQKFvsk52FPj+z7HcfSYDKoMQ5+ZvmSYkr kB6HlndLMgzNqodfGjkPYwQaP9NvIZM8m4WlAvyLm2gHfojtkPKh9Ml0WEp27KEAqmOi n4HYZ6xxLSLS3pRahSM6ZdLi78vRRox5I6uZ358UMpykfO+v56k2+S5uh4GL6cJdUZKK gy6g== 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=iO3IczdtMxBhIYAmKS6e26cWYhhPw/QwSBfxC3hHHGekhKANGMujCHCr557gEnqSJU F+YXBGJ3kAVL+vQBNwKuNvJjRJ291jfZ10UQPv96gSJalHFpniOLlxQG91ZUbeg8IMel 2A2YBXDIqecW4SiQSYPte9M+fLDAUzfwl9ZMeco9h5QwoZxjIDhTdj2X/6ObJmgrMme8 18XQdM79yKXmzV7nv6c9FX488WCDf+Hlh+YtIUSvueUTFAs/wmOQChW9huqzM5Dsx2Zv JjlsTK92EmltwZYsxK6u25qYl9fdb4mjQEhJPtS4jKcXbYb957txqIvnECsGthTp+PHm +1HA== X-Gm-Message-State: AOJu0YzQWhj2JYb6AXQerbrx+MwmYJpqUqGrQcVopjl/eTHrviVInexL lrj7bmjg1+ovas2mL3uXgmhu7EraT3I= X-Google-Smtp-Source: AGHT+IFAHhW5BZGO1uppfpot1pEr0aAhGs0ZmH0MKLDsA+afIocjRRcxvAD33MR5tv+EpN9UJvITdXcpd9o= 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" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 6921F10000F X-Stat-Signature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u83QRUHRi/71raob+7qiwmE0RD/AFrMt4LAXhi08Er2czs9qCGTR+gUxwaVehFzseqVA3vk/sjsLIVNLKxk7QyV3arY+7UqDURmAqD77CLu7ozCeh8PhuXFPidiIW5o60nJAmZiCdkzjB//V+wTsVegkK+iK+9HdHMLpPnMJ5WZ0mquoV8WRTesbPCaasX2OcaVVYWH7v9YEpnWuJYf9nIju4rBPhQurTP9aPW78jUTMTqbc1K6U+nRpPMt6KVYLdYF1ke63dhoHhXFbX0Q+QCVpIoP6E/Y+mPVMOOARYsrfN5kwq0Q1ufcEf8D35GTuPvsRvLe9EzDEeGF4yarMQiqr4Fl3qZi8k8OxBU03YYruf1ndbaNe+dE4oynBpaYPmVjv57yDilerA43c8MUQum4eepR02nLJmi+23c9oxoCHmW9sUd5XnLXZlYWrclQ65NVsuVoJQzihiplp5299yff5ObIituqON1bx5QxRQkjG/TVT83Q2T9VfIpnzwlAjk+WTN764W0y1vL2KbfMCe5TKZ83z0BdTJUu5ICbQFkt9U8sagHRTMYASNBf4tDO65pNcG14F/V2yXhAju57exs3nNEVOjLSwHSPuWKD0SsFbNt5AeImdJAd1xR0hUSh6GDughRRiPKETU5+nMYLt3k9JK53xfYed5RRN0X8iziXFrdpW9AJoufDshnbIwFJZIz2CoH0zVIoCWsIp7mv7ZZQjc+vqblq4VfcnFQzgRViz+7zEN6MGm+FwHKilGAZ1I8ibmg= 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: 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) {