From patchwork Thu Sep 14 01:55:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384038 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 84FBBEE0203 for ; Thu, 14 Sep 2023 01:57:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA8F88D000E; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E58FE8D0001; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCE4E8D000E; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B8D098D0001 for ; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 97E4C1CABF9 for ; Thu, 14 Sep 2023 01:56:40 +0000 (UTC) X-FDA: 81233538960.10.235C4AD Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf26.hostedemail.com (Postfix) with ESMTP id C214F14000E for ; Thu, 14 Sep 2023 01:56:38 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jeSNNMeO; spf=pass (imf26.hostedemail.com: domain of 3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@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=1694656598; 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=mhn7+u5bXSh3DxSOzG9/ewBd6nzzrjcseQEqhhMfZJw=; b=bIkje3ah0kLP5JlzcEuwCfC/1FzlT+FtEy+S+2437bBFHaG7BoT+0zA4GXvwF1BgwzowdD +9Mdiu635PKwy3bjCn/5ig82D2ljbeL+4vPRQEga0JkjnaVtykP+0Vk3yp4iWNmgxREwFc E1V45zoqhwGQwrLu88GPJm4LW54x4ww= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656598; a=rsa-sha256; cv=none; b=UjR8dIrqp+Dz6oo43boxeBXBsUF5hjz43kJvKH4a2zrHmeSKMLalKZJA6mRDQ3WJKfvuoR UN/ocTJR0kNHL0k6KWTOrTwcNnXErpD0QxvjeLiOkSIyUox6+OG0VA0QCCLzVdKR60UkeP VL9p7swwxm7tJYGcW/e7nqQL5X94/oQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jeSNNMeO; spf=pass (imf26.hostedemail.com: domain of 3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59bee08c13aso2140047b3.0 for ; Wed, 13 Sep 2023 18:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656598; x=1695261398; 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=mhn7+u5bXSh3DxSOzG9/ewBd6nzzrjcseQEqhhMfZJw=; b=jeSNNMeOwPDEI439ZkaD8Veh+Zzf3ivDDCOkpxBAoIDHbv4+al/Nr4qA9oLMXXK3mC D1BO+rQTupV8q284zWFblIeQNUzdFSmhbRp+kr3HDRXF+SWZIAZuWvGVDHdXbOKz9kBL 3Vo/77BA/tZOZ73I2m/6FWG+QzOZRMxHeeYtqx7mnRIYgoog3ta8N9Yyww5tfVYNKqW5 kdbsVn4MOrCtzcN6SCWbMqCGCHEorSofL0cyWtDNq3EkAJ2t7oxmc/aD3l9eROCSX/EW HbwCptTrXIXdxgYZtJneTbBK16wg4ZUFQ0XBY7xDxgIhfBImeUn+OCKU6HQgREYXxFsu pXPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656598; x=1695261398; 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=mhn7+u5bXSh3DxSOzG9/ewBd6nzzrjcseQEqhhMfZJw=; b=jn3J4WcZ8LBtDalFLlJ6HfJUpz8baNLm1+kgFBcK51OztDzAMp6OlVTdHE5YzMr+1l vwOmS4xWXYCoosqnhkyhl2Dmaf6TCw9/MdZSVdIVwEIrPBC39hqd+LH2Wr7IeAhDCfKa l39W2MRaNb0Q71t1ccQicYQ1QGztgGP6Pm5HhMLrEtap/p/z1qrqcEHshiVClFfKA+Uq B8VsI8v4GSHi4zyuz8e9GLbR9OZwykNmE292hmHAK/dgfQoSxuySKr7yy/q6ElcyflzS bHcCvP4B0HOr2ojhj1riq6cBWXOZ/LypZKawOERDNpP23qmjowNXSrh220NXMbCmZNey dErA== X-Gm-Message-State: AOJu0YwL/Xpvgq4dzL8mWF1Bxjbqp6swLTk/f6Qg0dc/2d4EUGdlk0n8 4lh4Rq81J3k97BIqWIv9yieMsVN9QNI= X-Google-Smtp-Source: AGHT+IECmRUJsED8Us3qWlJnugvslNzA7dXsGOQfEOYZbUPHi7DQFFf33qpMB4ILXjFkyWuOYhA2SvRB6XQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:e809:0:b0:59b:ebe0:9fcd with SMTP id a9-20020a81e809000000b0059bebe09fcdmr13568ywm.7.1694656597965; Wed, 13 Sep 2023 18:56:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:29 -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-32-seanjc@google.com> Subject: [RFC PATCH v12 31/33] KVM: selftests: Expand set_memory_region_test to validate guest_memfd() 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 .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MOjAxK3GDf4262i3Oh1MBVHLbd5/lDuX PnfI2Ic5 xZ4ZnPpJ9CfvkCLIkcT8OcjvEXVNJECyDmMak2kkcKRjVUhnBTelWF8RpxGrlaOt6L2PG/fBLrQ2l9zJ4DX/+94FJXcxNDuXdW02j1bnMzXKjki+jF81D8XMDxAr8JDEFf2V58Sukq3EIy2UrErRjceqXpNU/mHjQD9oQ14H/x/ynoncrQzgLby7a408ul5+87K7VaV8eBctAuMrr5XcdWs63N1+9ybTb0LHmx3Yt7yRRx3k+mIQQrG6M9P7cqw5JvVCNSkzc0viGZGyVJbEHdwfJF5ZfwgXa1FSBpl13D4sITwAfA9Q6xJg03tTqyVrNc/CNNktqvvIp6JV7YCGIGA4aurlGITPu2Soz4u68rdworD2UQf7n5AxKtONouVtrxR6Uz7GsBRLw6r0K7vuooFjC7M9p32nkY05c/GuChQw3gnOq9f+xerI1ObX5sHhJS+/M56/dtiOvpehCiJOv00f5XSO/D7xKWRZR2YvtMxr66LbZQocJdhhlX+bbe/Hukc4r1a4WkgPAHijMjgav79wiAIk+KqfjU3/GwXfUMV4f9EhnQ4cNVzKkduLo6Dh8MiEBdICqxoEKQMi1C+nbji5vkrRtpiPC0fbpPAvem4ezxtMBeK7LXyMjlbuk6eNJGnGrGrHnVvOPeHKD0WRQNPVDvyF3Z9eEc51vpzYNQ9DGCh27G9yIJhWjNaYdybulyKDt39bUAki+DS6s0ahELJuse2a5ffu8pVVqjQqAoG5oUg4E7Yq7HzzhwICRYr7RMezJp7cuhuQC9fViDvr6mPYm9SNZyJ36GNZ+OYbxVxmFcG9/ngflyQa1dql0ZeszG6dWGZfD8QqTh3bBg2qrNCmMrw== 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: Chao Peng Expand set_memory_region_test to exercise various positive and negative testcases for private memory. - Non-guest_memfd() file descriptor for private memory - guest_memfd() from different VM - Overlapping bindings - Unaligned bindings Signed-off-by: Chao Peng Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng [sean: trim the testcases to remove duplicate coverage] Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 10 ++ .../selftests/kvm/set_memory_region_test.c | 100 ++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index edc0f380acc0..ac9356108df6 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -819,6 +819,16 @@ static inline struct kvm_vm *vm_create_barebones(void) return ____vm_create(VM_SHAPE_DEFAULT); } +static inline struct kvm_vm *vm_create_barebones_protected_vm(void) +{ + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, + }; + + return ____vm_create(shape); +} + static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) { return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0); diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index b32960189f5f..ca83e3307a98 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -385,6 +385,98 @@ static void test_add_max_memory_regions(void) kvm_vm_free(vm); } + +static void test_invalid_guest_memfd(struct kvm_vm *vm, int memfd, + size_t offset, const char *msg) +{ + int r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, + 0, memfd, offset); + TEST_ASSERT(r == -1 && errno == EINVAL, "%s", msg); +} + +static void test_add_private_memory_region(void) +{ + struct kvm_vm *vm, *vm2; + int memfd, i; + + pr_info("Testing ADD of KVM_MEM_PRIVATE memory regions\n"); + + vm = vm_create_barebones_protected_vm(); + + test_invalid_guest_memfd(vm, vm->kvm_fd, 0, "KVM fd should fail"); + test_invalid_guest_memfd(vm, vm->fd, 0, "VM's fd should fail"); + + memfd = kvm_memfd_alloc(MEM_REGION_SIZE, false); + test_invalid_guest_memfd(vm, memfd, 0, "Regular memfd() should fail"); + close(memfd); + + vm2 = vm_create_barebones_protected_vm(); + memfd = vm_create_guest_memfd(vm2, MEM_REGION_SIZE, 0); + test_invalid_guest_memfd(vm, memfd, 0, "Other VM's guest_memfd() should fail"); + + vm_set_user_memory_region2(vm2, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + close(memfd); + kvm_vm_free(vm2); + + memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE, 0); + for (i = 1; i < PAGE_SIZE; i++) + test_invalid_guest_memfd(vm, memfd, i, "Unaligned offset should fail"); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + close(memfd); + + kvm_vm_free(vm); +} + +static void test_add_overlapping_private_memory_regions(void) +{ + struct kvm_vm *vm; + int memfd; + int r; + + pr_info("Testing ADD of overlapping KVM_MEM_PRIVATE memory regions\n"); + + vm = vm_create_barebones_protected_vm(); + + memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE * 4, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE * 2, 0, memfd, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT + 1, KVM_MEM_PRIVATE, + MEM_REGION_GPA * 2, MEM_REGION_SIZE * 2, + 0, memfd, MEM_REGION_SIZE * 2); + + /* + * Delete the first memslot, and then attempt to recreate it except + * with a "bad" offset that results in overlap in the guest_memfd(). + */ + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, 0, NULL, -1, 0); + + /* Overlap the front half of the other slot. */ + r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA * 2 - MEM_REGION_SIZE, + MEM_REGION_SIZE * 2, + 0, memfd, 0); + TEST_ASSERT(r == -1 && errno == EEXIST, "%s", + "Overlapping guest_memfd() bindings should fail with EEXIST"); + + /* And now the back half of the other slot. */ + r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA * 2 + MEM_REGION_SIZE, + MEM_REGION_SIZE * 2, + 0, memfd, 0); + TEST_ASSERT(r == -1 && errno == EEXIST, "%s", + "Overlapping guest_memfd() bindings should fail with EEXIST"); + + close(memfd); + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { #ifdef __x86_64__ @@ -401,6 +493,14 @@ int main(int argc, char *argv[]) test_add_max_memory_regions(); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) && + (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) { + test_add_private_memory_region(); + test_add_overlapping_private_memory_regions(); + } else { + pr_info("Skipping tests for KVM_MEM_PRIVATE memory regions\n"); + } + #ifdef __x86_64__ if (argc > 1) loops = atoi_positive("Number of iterations", argv[1]);