From patchwork Mon Aug 7 23:01:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1761931vqr; Mon, 7 Aug 2023 16:08:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuYTJ5zDEksrjcEGqgRPOm1s57oL5pti1ofmhp7Pvh86akP6ZWhxXdTTMiKfqL0Fxyya7L X-Received: by 2002:a05:6a20:3d94:b0:140:a6ec:b55f with SMTP id s20-20020a056a203d9400b00140a6ecb55fmr8364794pzi.16.1691449683380; Mon, 07 Aug 2023 16:08:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691449683; cv=none; d=google.com; s=arc-20160816; b=D/rgRWg5Nm0L/rL5bNQ+6+LRHiizNh4O03/SV+9BaCF5Xtv7GGL4U5G+ACehI6nJMz sJrHNB8+9xKw5wXyWKKy4PNpSBDSUVIghtsmM1xx5dRRC104VABTU+gKbZiVKrzQmf9i kjwXpYMXPh/UeRYyq7wgfhXbXy7o75A/qXnMs8U3b8oj6X98uBNvunkTfvQEZ+lNwTjj ka9DMYXiawTFsgs+ldRyu7gyLuyr+I8Y4c9lcUAygmJwqTYW9ivd4Vo0cqDGZKOSHWtv vP44PlU5a4dfLKy/z9bDzZqmbJnLbYP4+GdId/c+w08HMN5wx8MH1FFc1p1EwR+EwMqS J9lQ== 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:dkim-signature; bh=OyOCciFW2Ckh/B5HTo1lu/EKNDU2b8Raj4KNQZ3bNy8=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=FaZUeXRQmt3nq9K1fObMX696FcX7VAzyLz84vVBvyHAKhlx3kmDU7x1ctS687D0ARO zJpfzf4HU6LzFhl1iU45ABd1YjlEKHxEbFHaJaGqrtkvk/Gi0TqZCEO1I7th4L62ciyU dJgSY54wqM37AV33uLBNCbcjVv58QA8aAj5JchriSNt9Slf1t5vzA2yP2pRJqlMXpIz5 AAVHhdEvhhN2jCj2qv/3KeGyuZ2uUA6IRih3MdDzu1yWPNcwFk8t5brGOoZC5qHORYx9 7m9pR1DRvMAriBxJw5SS0JEc1qahqbYK27lBjr2B8zmHMXBwpB3pyYNTb5k+KMu+2Cl9 vAKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=uJOBS3ug; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bg26-20020a056a02011a00b0056428dcea78si3511960pgb.17.2023.08.07.16.07.50; Mon, 07 Aug 2023 16:08:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=uJOBS3ug; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230473AbjHGXBv (ORCPT + 99 others); Mon, 7 Aug 2023 19:01:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230133AbjHGXBg (ORCPT ); Mon, 7 Aug 2023 19:01:36 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17E9910FE for ; Mon, 7 Aug 2023 16:01:35 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-55c04f5827eso3412790a12.1 for ; Mon, 07 Aug 2023 16:01:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449294; x=1692054094; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OyOCciFW2Ckh/B5HTo1lu/EKNDU2b8Raj4KNQZ3bNy8=; b=uJOBS3ugweQYN52NY8pPJP4fC3zUz1c9ojo4XDlVUsFi/jjOlgJHXUyLp5Nc+LyD6W wx7xXrNG1HaypGEEtSSWkQYc/mZdvXmHOXd+WmY7Nmgt6Ohwr2RGh+lKOC5XbxZgSPxh LafphqQEjrKPgmBjxtLTK1R4CSWGCA7SbhmNpnkrvdmR0ni/LKS9/zt7P8JZ6kqTpjqP FaCeA+MEOK1Xfexus/CBFMyDCLf9cu+S4Tcvnqkbomm6V4EaSh8N9J5m511j9MKKq5Kd vdxNFuYw5vsbZDO4/RjPf9H4UmvjjuYAFcn2lbbHHKsW/C0CL1KLHTL0cyNTNOObV5Xi QjNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449294; x=1692054094; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OyOCciFW2Ckh/B5HTo1lu/EKNDU2b8Raj4KNQZ3bNy8=; b=dKmdyKLLJPfsKCsZTFi9afcyodafr9b3zu4z56sV6lHWWjJla5o/lhDntYyRqZ1tdH 0uK7qDI4Cu2/oR0p9lNk93MWFI1f+CtFl0P79pO8BAtqA+27YTItH5HMGN5G1zoRJGoZ DUT+UFMrqdaFRl4JuDM4umTOLUWLygfOcjvOmzEWUBZGLcGt1H6bmtfcx+d7e8TGHj19 NQZZpzKw5ymDshPIKz9ZnJiDxuqgt9AJjGVlBbvW+LaKVJ5egtuIXC4oNr15f21/W3Ee 1yjoiyETR1Z2sJf4mL/0cRuCVEpcbkP5b9A9B6i/fLe5HrA7B6L2QeMwQs9xTf/uB0Vr T8RQ== X-Gm-Message-State: AOJu0YxSGS2oFy3CznnYSerPPT2UY8XfEnb4OruTHYt9rrmfTSj0iOj7 d/dhgmrpOEUL82c2eiq6pm7ftxvCJrS6lFqdIw== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a63:b512:0:b0:564:9785:75c with SMTP id y18-20020a63b512000000b005649785075cmr35489pge.10.1691449293726; Mon, 07 Aug 2023 16:01:33 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:06 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: Subject: [RFC PATCH 03/11] KVM: selftests: Add tests for KVM_LINK_GUEST_MEMFD ioctl From: Ackerley Tng To: pbonzini@redhat.com, seanjc@google.com, tglx@linutronix.de, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, shuah@kernel.org, andrew.jones@linux.dev, ricarkol@google.com, chao.p.peng@linux.intel.com, tabba@google.com, jarkko@kernel.org, yu.c.zhang@linux.intel.com, vannapurve@google.com, ackerleytng@google.com, erdemaktas@google.com, mail@maciej.szmigiero.name, vbabka@suse.cz, david@redhat.com, qperret@google.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773613543255943494 X-GMAIL-MSGID: 1773613543255943494 Test that + Invalid inputs should be rejected with EINVAL + Successful inputs return a new (destination) fd + Destination and source fds have the same inode number + No crash on program exit Signed-off-by: Ackerley Tng --- .../testing/selftests/kvm/guest_memfd_test.c | 42 +++++++++++++++++++ .../selftests/kvm/include/kvm_util_base.h | 18 ++++++++ 2 files changed, 60 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ad20f11b2d2c..38fe96ea60f9 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -105,6 +105,47 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm, size_t page_size) ASSERT_EQ(errno, EINVAL); } +static void test_link(struct kvm_vm *src_vm, int src_fd, size_t total_size) +{ + int ret; + int dst_fd; + struct kvm_vm *dst_vm; + struct stat src_stat; + struct stat dst_stat; + + dst_vm = vm_create_barebones(); + + /* Linking with a nonexistent fd */ + dst_fd = __vm_link_guest_memfd(dst_vm, 99, 0); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + /* Linking with a non-gmem fd */ + dst_fd = __vm_link_guest_memfd(dst_vm, 0, 1); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + /* Linking with invalid flags */ + dst_fd = __vm_link_guest_memfd(dst_vm, src_fd, 1); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + /* Linking with an already-associated vm */ + dst_fd = __vm_link_guest_memfd(src_vm, src_fd, 1); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + dst_fd = __vm_link_guest_memfd(dst_vm, src_fd, 0); + TEST_ASSERT(dst_vm > 0, "linking should succeed with valid inputs"); + TEST_ASSERT(src_fd != dst_fd, "linking should return a different fd"); + + ret = fstat(src_fd, &src_stat); + ASSERT_EQ(ret, 0); + ret = fstat(dst_fd, &dst_stat); + ASSERT_EQ(ret, 0); + TEST_ASSERT(src_stat.st_ino == dst_stat.st_ino, + "src and dst files should have the same inode number"); +} int main(int argc, char *argv[]) { @@ -126,6 +167,7 @@ int main(int argc, char *argv[]) test_mmap(fd, page_size); test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); + test_link(vm, fd, total_size); close(fd); } diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 8bdfadd72349..868925b26a7b 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -495,6 +495,24 @@ static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, return fd; } +static inline int __vm_link_guest_memfd(struct kvm_vm *vm, int fd, uint64_t flags) +{ + struct kvm_link_guest_memfd params = { + .fd = fd, + .flags = flags, + }; + + return __vm_ioctl(vm, KVM_LINK_GUEST_MEMFD, ¶ms); +} + +static inline int vm_link_guest_memfd(struct kvm_vm *vm, int fd, uint64_t flags) +{ + int new_fd = __vm_link_guest_memfd(vm, fd, flags); + + TEST_ASSERT(new_fd >= 0, KVM_IOCTL_ERROR(KVM_LINK_GUEST_MEMFD, new_fd)); + return new_fd; +} + void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva); int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,