From patchwork Mon Aug 21 19:44:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 136572 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3782174vqi; Tue, 22 Aug 2023 10:34:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGqCZ3mBb5FIvuWaAMDpi/nI84ceJ/F5IFtft8UX+6nEO3qOp8k2NsqG0g1Pj3gw3Duk0fC X-Received: by 2002:a17:902:db09:b0:1bf:8132:d19f with SMTP id m9-20020a170902db0900b001bf8132d19fmr11090086plx.27.1692725681370; Tue, 22 Aug 2023 10:34:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692725681; cv=none; d=google.com; s=arc-20160816; b=EwJTqMNoPg1Vc5pheXLCQ94KNZ13dp1kOLARhPK9+f/Xxa9CHIG+CaRGBLxjoHEl1a rnkMi/NbBJgXcXdcbZ6MFunxnhMgFtbQaL88YQBbXqBlgHp/foFg7/hVh8FsH9BoNcFn PKS4mtFzD2ntJpuRh9fckEppsONVwRGpVwSKdc4a1eqlQUJV7snv0DRdX67+Xyumgq1f hbijRtBf/pmbubDZQk6eHSaZRescuMbEKi0nelnItf2SvwpV2E9IHW2PQ8XfQoduivyz ob7/mFbPlZIhych+d7xfaF5zvj/AAjbili+iPljMOrxR+tmwqFwBkE9CpNMyYggqVLwy Kn7g== 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:mime-version:date :dkim-signature; bh=SSCma3xFa9r8jkJRJYtQ3UJXMDuR6ih/O0h93W+17hY=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=BDbTOd35f53ZcTX+A4vT+yV8a8bik2qyZ+8lEu4nZXQ4H0Im3GiIfHJHatb9kPA89/ 5wS+7oxgP0+4MvRoH2+P+9i38YaNolf+xLUmfbgxbzvZo8ZeroJFs15FTGeAGa4tfBNx 7Nsatl09BfBxURB+r9HeERikBppigb8RujxH6cvmYpvFU6d3OK5+kXrHyvJz1zAl4A3H t18XZuz+bxlv8SU/dBDaRLBwjPz51aLO1s53RjCM9nayQo6pmiQTJtAk/ye4kcY5x5wq LimMM3ILmMQsfB6+h+OiECOrhOVYDnqlFCHJnz1UffVfvVBV5gh3PMHL63XPVIth7i2u RYVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=PGySCjo2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id kz6-20020a170902f9c600b001b85ca73574si8874727plb.285.2023.08.22.10.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 10:34:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=PGySCjo2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D0D454499; Tue, 22 Aug 2023 10:15:50 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230076AbjHUToW (ORCPT + 99 others); Mon, 21 Aug 2023 15:44:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230056AbjHUToV (ORCPT ); Mon, 21 Aug 2023 15:44:21 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2887CC for ; Mon, 21 Aug 2023 12:44:19 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1bde651db83so43901625ad.3 for ; Mon, 21 Aug 2023 12:44:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692647059; x=1693251859; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=SSCma3xFa9r8jkJRJYtQ3UJXMDuR6ih/O0h93W+17hY=; b=PGySCjo26AZB9cqV8KpF/JMeY7vWef/kDAGS6lGDo0Q2RuJIf9UzSHhuepJrUdYeDP 3wsRtqVlyyhBzLg4NgnhNCDBql5wFYZXsWqJsTcRl1mU2NMggXSYZeYvlEGM5PpwIXvP Iielz22yjvHt+QiS4NJj5jraczBUg9QlX3xA9A8GDGKLxHpG8GP/uEWad2wSPgfT93sp EZ7hJaU0K7Ub0I5m2uJSamCTcj1KKMUbq+P7B36rHl21GsY7ypgdNkMCI3OMsw5Bbwl2 k8PVgGtQS/r/6BfdZ40O7B8paUefkaPLaVdXZYLeI194w5SGzeP5V2ybLicvfal8Jy53 Rw+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692647059; x=1693251859; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=SSCma3xFa9r8jkJRJYtQ3UJXMDuR6ih/O0h93W+17hY=; b=BjUoDwaiH7P5oTrtVWA9Ba8FqXJ1NHaMOkjpOAKov7Y17fDisB/fraj5cFdAUUL5hR JAFQzDKvCVpd0wI73GHFapSwmcwitevf1ZmvdpmbRUdv6vpDul1LAGpsARhu/jw1iOO/ hzzS4HEnCgC8Hbm57rr5in6cjYTOwoxht0x5ABUjDEQOUnf1VuX8StWI4TyaD7unWK7/ K2fmqB5siF/0JQK/x2zEfM7RrhN/kFwA40TSS6waGDI1v2hE4EDRbYEmIR0Hzyv9hz2D yxX6UeallWVlkns6ymyNOXyvh+Dfj0w7FPbKqJfRHvkWcQag2aWvO/OsoIlurbe39HEx XgUA== X-Gm-Message-State: AOJu0YxpYj8IITumeIi66Jof4llTl9HaHhTGxUr4DN1XhgQaR7q0RwC/ lVleavSYf5K/+1fFH6MBB8/sBSyHE72GV8Hnpg== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:903:41d0:b0:1b8:84d9:dea6 with SMTP id u16-20020a17090341d000b001b884d9dea6mr3274875ple.12.1692647059281; Mon, 21 Aug 2023 12:44:19 -0700 (PDT) Date: Mon, 21 Aug 2023 19:44:11 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Message-ID: <20230821194411.2165757-1-ackerleytng@google.com> Subject: [PATCH] KVM: selftests: Add tests - invalid inputs for KVM_CREATE_GUEST_MEMFD 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: 1774951524023142459 X-GMAIL-MSGID: 1774951524023142459 Test that invalid inputs for KVM_CREATE_GUEST_MEMFD, such as non-page-aligned page size and invalid flags, are rejected by the KVM_CREATE_GUEST_MEMFD with EINVAL Signed-off-by: Ackerley Tng --- .../testing/selftests/kvm/guest_memfd_test.c | 33 +++++++++++++++++++ .../selftests/kvm/include/kvm_util_base.h | 11 +++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index eb93c608a7e0..a8e37f001297 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -90,6 +90,37 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size) TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed"); } +static void test_create_guest_memfd_invalid(struct kvm_vm *vm, size_t page_size) +{ + int fd; + uint64_t size; + uint64_t flags; + uint64_t valid_flags = 0; + + for (size = 1; size < page_size; size++) { + fd = __vm_create_guest_memfd(vm, size, 0); + TEST_ASSERT( + fd == -1, + "Creating guest memfds with non-page-aligned page sizes should fail"); + TEST_ASSERT(errno == EINVAL, "... and errno should be set to EINVAL"); + } + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + valid_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; +#endif + + for (flags = 1; flags; flags <<= 1) { + if (flags & valid_flags) + continue; + + fd = __vm_create_guest_memfd(vm, page_size, flags); + TEST_ASSERT( + fd == -1, + "Creating guest memfds with invalid flags should fail"); + TEST_ASSERT(errno == EINVAL, "... and errno should be set to EINVAL"); + } +} + int main(int argc, char *argv[]) { @@ -103,6 +134,8 @@ int main(int argc, char *argv[]) vm = vm_create_barebones(); + test_create_guest_memfd_invalid(vm, page_size); + fd = vm_create_guest_memfd(vm, total_size, 0); test_file_read_write(fd); diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 39b38c75b99c..8bdfadd72349 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -474,7 +474,8 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) } void vm_create_irqchip(struct kvm_vm *vm); -static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, + +static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, uint64_t flags) { struct kvm_create_guest_memfd gmem = { @@ -482,7 +483,13 @@ static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, .flags = flags, }; - int fd = __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem); + return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem); +} + +static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, + uint64_t flags) +{ + int fd = __vm_create_guest_memfd(vm, size, flags); TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_GUEST_MEMFD, fd)); return fd;