From patchwork Thu Sep 14 01:55:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384039 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 43442EE021A for ; Thu, 14 Sep 2023 01:57:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6EB38D000F; Wed, 13 Sep 2023 21:56:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D200E8D0001; Wed, 13 Sep 2023 21:56:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6FB78D000F; Wed, 13 Sep 2023 21:56:42 -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 A0A0B8D0001 for ; Wed, 13 Sep 2023 21:56:42 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7E5301C988A for ; Thu, 14 Sep 2023 01:56:42 +0000 (UTC) X-FDA: 81233539044.21.1BDCE84 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf02.hostedemail.com (Postfix) with ESMTP id AB27880007 for ; Thu, 14 Sep 2023 01:56:40 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O8afp114; spf=pass (imf02.hostedemail.com: domain of 3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656600; a=rsa-sha256; cv=none; b=0aPn3eqqyi7kTh5uy60oM/RQOh+5aaTI1tItkGaxhNGkS2hT14g0U5iiRC6KC+GtQbZ/my M106m9d3mMbUDYfkhFq2WCiM331Z0oUtDKrgwjPrq8kIyFrBKcJWTiwm10UGqv6KIKXS+W CfOSSOmD07P/emOssFkSkgXObyIgMao= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O8afp114; spf=pass (imf02.hostedemail.com: domain of 3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@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=1694656600; 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=i91JvGzfQg7A4MbSmUGyo4+8+JqUux5ifD59BoUsadA=; b=HEb/Grp4ABCkC1pOexN5SsOJgPppuSrdDkDibVkDt0P4UZARBfvYZzwpd8uIB4PhX/sVSR R6Gh0Rwt/WbC/ZCmXGr3fpubwmmcrF2Krv67D/ltU0pgxy5e2M39tlGC88WKoqGRI3WWfl 6/ajBxQoVRKd/3tYuiF5ftLGJMzOf98= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-59beb3a8291so3976617b3.1 for ; Wed, 13 Sep 2023 18:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656600; x=1695261400; 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=i91JvGzfQg7A4MbSmUGyo4+8+JqUux5ifD59BoUsadA=; b=O8afp114x8/7Z3hV+O6wMLCkihDQpUbqrxbYmpskXN2SW0V4flc1/B2gEB+SCCWhmK aK2qE6JZh/L2RmYBvoSRt8RzUDSJeK/ISsUch2JJT7D0Pr0lUbfp5RF6AZ6DSeSx4L3X 0CfpWa3kogr66TbpVu53T9n6s1tvrqbn5lys5T6zznrJ9/ahk4/ZZpfvor+R2iXN3Zw+ ZwiQHQXall7nLwrI4cYvX786Tt8/vvT7Z7CJydnTQqm4VMKnUeymFQRmn/mrwgYHfVLM knriV/gZb017L0RJSkCpgB/5xjKS+11/XknXm7T5ePGWf5KspI17VHaR9h3KHUNsqwz2 /IWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656600; x=1695261400; 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=i91JvGzfQg7A4MbSmUGyo4+8+JqUux5ifD59BoUsadA=; b=EstnEpfPeVl48c7z1DPTWlSI9U+Ko65QvpDLt2+pWRAjI/MOYH2AosXViPN0vyiVDs oyuIcj8Z7fQlJjkxlENyBxllmkZZSCdfv0/+Xm8oyWdA0Y9yak7MkMfJjRZSl+4UojWQ n03brAkbOJyWwy+9Stillu0UwmoRAYS/B9XVJKqZYENm9pfJOyNetlQ/QUaD/nIZ0DuO m3ebxWJ8ksbme8FwyZudNnWMio+Zk+OfSPHYANTV5/C/csUokLYkYEMW3/Pg1v1bHIRK yzhk99xvrOKxhREWs+nnrC7sLIUDr93VJaSQ/ihmAjzUTRwa1/1KI22Ntr8tho2HGnNK VhsQ== X-Gm-Message-State: AOJu0YzDeu45qS2B/UfUsx+Vw0xESXP1948MWi2HcT1vbjfeKQiclOz4 OWcqoRJrJjLGq5EJxn95F6wrTpv+KZU= X-Google-Smtp-Source: AGHT+IEvLzTJgrzr2NLiV8Z9En5+JUJxc1EswgYLaEL3Jr6eWhED9/CQgA0jS1j6Htu+3uQxxbNwCvj4oqA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:400c:0:b0:589:a855:7af with SMTP id l12-20020a81400c000000b00589a85507afmr107432ywn.7.1694656599914; Wed, 13 Sep 2023 18:56:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:30 -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-33-seanjc@google.com> Subject: [RFC PATCH v12 32/33] KVM: selftests: Add basic selftest for 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 . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Que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rbgRzDowbnXvhZR79hIECE0B3kqF0ZHBK/tbtdJ+xJYUDg+Lk8LvZqp7KniYkJxRNJxGRdHkA/l708WA0S+thilUz95F0dnfll4p9t3NWBd5ybIhf8og98QYX8X5JGtrRL/AZqpPUrZruf5Hmr02thInvbQg9Ai3u4QclXAVYN9/1reUBZJVdx6GPvxXabojtbLxm3ckQohbIpQqk6iuWQ0DC8pRE8JzgO5e5vLpdwB7O32xjPzUOLgNtzGNvO8S4lRSu1uPlAzAeQZM5k0ptFTVoXpioJp4yMvdJn8u5GQ/mPYqiNLA4pCzJhPi7Ve6R+CfCDdQbq56G3nPAG05LrSq8fKLeWOHZR98JcaO+P9IckBSvMYGbHk4oju9PzyLEuULgAufaeegviiv0Jwgd6/tgL4jSv7dRVrE1ZWSLr0AK1lfZ2/ptIlBR0hJDtdppr3vf1tzrm4x2rTu0aDWWkWVy6sy3gX/qV7TektKKKQ0rIe0ceucPJWBU+ty+AlE/dhwekGfVoeHFBIWZD83Fu4uujtVAdCSBJha4QRAkET8GJut6bfgG51uuJHIpxp+U2+j6BsNAJ0snCtnp2veoilqap6pHO4T0/5DYvEEgju+bwpe19Z0hobuxFKB02fVYEHPyGwR8QwUJRLwug5j32MBOXuB8XFz1tBN7fMYUM9tHtaxfrvB9EPfD+SdXs+BNPy17ROaR7hDQWzyrvdzmDne7KhmnXUI+vQmL38TYIVulcQNnymbBVKo/D0O1k2mguYKtWuPDGLH358MUdeYK/7RT3ceL0yACoeieVhaCTGq41LXkYmo+UAkkgGD4LNHprv8DPpx+y+LMg== 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 Add a selftest to verify the basic functionality of guest_memfd(): + file descriptor created with the guest_memfd() ioctl does not allow read/write/mmap operations + file size and block size as returned from fstat are as expected + fallocate on the fd checks that offset/length on fallocate(FALLOC_FL_PUNCH_HOLE) should be page aligned + invalid inputs (misaligned size, invalid flags) are rejected Signed-off-by: Chao Peng Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 165 ++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 tools/testing/selftests/kvm/guest_memfd_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index b709a52d5cdb..2b1ef809d73a 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -124,6 +124,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_perf_test +TEST_GEN_PROGS_x86_64 += guest_memfd_test TEST_GEN_PROGS_x86_64 += guest_print_test TEST_GEN_PROGS_x86_64 += hardware_disable_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c new file mode 100644 index 000000000000..75073645aaa1 --- /dev/null +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright Intel Corporation, 2023 + * + * Author: Chao Peng + */ + +#define _GNU_SOURCE +#include "test_util.h" +#include "kvm_util_base.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +static void test_file_read_write(int fd) +{ + char buf[64]; + + TEST_ASSERT(read(fd, buf, sizeof(buf)) < 0, + "read on a guest_mem fd should fail"); + TEST_ASSERT(write(fd, buf, sizeof(buf)) < 0, + "write on a guest_mem fd should fail"); + TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) < 0, + "pread on a guest_mem fd should fail"); + TEST_ASSERT(pwrite(fd, buf, sizeof(buf), 0) < 0, + "pwrite on a guest_mem fd should fail"); +} + +static void test_mmap(int fd, size_t page_size) +{ + char *mem; + + mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT_EQ(mem, MAP_FAILED); +} + +static void test_file_size(int fd, size_t page_size, size_t total_size) +{ + struct stat sb; + int ret; + + ret = fstat(fd, &sb); + TEST_ASSERT(!ret, "fstat should succeed"); + TEST_ASSERT_EQ(sb.st_size, total_size); + TEST_ASSERT_EQ(sb.st_blksize, page_size); +} + +static void test_fallocate(int fd, size_t page_size, size_t total_size) +{ + int ret; + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size); + TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + page_size - 1, page_size); + TEST_ASSERT(ret, "fallocate with unaligned offset should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size); + TEST_ASSERT(ret, "fallocate beginning at total_size should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, page_size); + TEST_ASSERT(ret, "fallocate beginning after total_size should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + total_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + total_size + page_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + page_size, page_size - 1); + TEST_ASSERT(ret, "fallocate with unaligned size should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + page_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size); + TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed"); +} + +static void test_create_guest_memfd_invalid(struct kvm_vm *vm) +{ + uint64_t valid_flags = 0; + size_t page_size = getpagesize(); + uint64_t flag; + size_t size; + int fd; + + for (size = 1; size < page_size; size++) { + fd = __vm_create_guest_memfd(vm, size, 0); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL", + size); + } + + if (thp_configured()) { + for (size = page_size * 2; size < get_trans_hugepagesz(); size += page_size) { + fd = __vm_create_guest_memfd(vm, size, KVM_GUEST_MEMFD_ALLOW_HUGEPAGE); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with non-hugepage-aligned page size '0x%lx' should fail with EINVAL", + size); + } + + valid_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + } + + for (flag = 1; flag; flag <<= 1) { + uint64_t bit; + + if (flag & valid_flags) + continue; + + fd = __vm_create_guest_memfd(vm, page_size, flag); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with flag '0x%lx' should fail with EINVAL", + flag); + + for_each_set_bit(bit, &valid_flags, 64) { + fd = __vm_create_guest_memfd(vm, page_size, flag | BIT_ULL(bit)); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with flags '0x%llx' should fail with EINVAL", + flag | BIT_ULL(bit)); + } + } +} + + +int main(int argc, char *argv[]) +{ + size_t page_size; + size_t total_size; + int fd; + struct kvm_vm *vm; + + TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + + page_size = getpagesize(); + total_size = page_size * 4; + + vm = vm_create_barebones(); + + test_create_guest_memfd_invalid(vm); + + fd = vm_create_guest_memfd(vm, total_size, 0); + + test_file_read_write(fd); + test_mmap(fd, page_size); + test_file_size(fd, page_size, total_size); + test_fallocate(fd, page_size, total_size); + + close(fd); +}