From patchwork Thu Oct 12 14:07:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 152020 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp1257897vqb; Thu, 12 Oct 2023 07:08:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7JTOjfVUydl6gb5n5cz3LYpEu14TFJQ7kcALLk1IE1A8P+5g/zxNfeaV3KfFH3i86DOxR X-Received: by 2002:a05:6e02:2185:b0:357:5188:1f83 with SMTP id j5-20020a056e02218500b0035751881f83mr4531995ila.1.1697119711852; Thu, 12 Oct 2023 07:08:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697119711; cv=none; d=google.com; s=arc-20160816; b=wMW8zEQoLe0k/i2TcJBWmpuiUjDZcj6RuFVPCLHye+npef7yMqL32TVlnNfZe4xy1p qMW5lIwoUMYf4+9Nq7WWQ9AV5RME/VJC164P/raMoDX8R/aR0NmG3W53hfQqNXHtv4Vv rGdTYXk5CBFp4/uk8EuamFN8RDsDezY1VBMQ1aATNS21ov6hiEALSam6I7RWRbXduz8S CmqUPG6Icn/awFXneZKbPyLX1MUSHwhrOiAXqUp4RAUYHoBdgKnovv6Hbe3HsCTbZpaO wyDrorF5ZWHX5DwgYhhf7cMZMkk4zLg40aOuPl8pZhkGZIocgAwwaOw4bDlcqzpi0n/L 3hNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=qlCZfTkqNOxM/nWc4shz1NXl1XUnP9vA0JMHEFMp3Tg=; fh=V2vC7s/1M2hH18+2ttRiDZaHrE0o8WE7Q/UWhHIMKLY=; b=efZMevRzyYpOHVWY2Xd8kbiC4XiscgqZHn3AM3HA6wUVi27+QIYmzU5OSGqL+HkbFV Fl8dZsaK4jRZjKka4UfHgZz28JKApivYThrwt4kRf6AAql4BPfAOEf95O6FVAY0HxvTf V8legUAmUr9Z/u2AgvyXSOeFrchXKpUtuWzOWduVEy2XOd/S4ykNZgezYWvZeH7DwPjK 51c/dacJkfUicHMdPnsKUNcf1ttg6Nh1oSEcYl7bSG6PCfvVQeD7ZRmGZK6sLoYOMHxT AwAOVboGYl/yOV6cFRjjmzIxybcfcw6HGraod90fhKxOAnBea5up4k7deETy1rkpuy0N 5rCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="dJhF/7Pi"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id g7-20020a056a0023c700b0068e2f6feab4si4253259pfc.374.2023.10.12.07.08.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 07:08:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="dJhF/7Pi"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 1DB9483F1CE8; Thu, 12 Oct 2023 07:08:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343952AbjJLOIJ (ORCPT + 19 others); Thu, 12 Oct 2023 10:08:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235188AbjJLOII (ORCPT ); Thu, 12 Oct 2023 10:08:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4039D91 for ; Thu, 12 Oct 2023 07:07:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697119639; h=from:from: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:content-transfer-encoding; bh=qlCZfTkqNOxM/nWc4shz1NXl1XUnP9vA0JMHEFMp3Tg=; b=dJhF/7PiC1vmN+XyO086Bkx87YTHyM/3HP7skiLltfR+fgeQr4wHp+zHWOhkVmBngaEyBq vRAUfYZipFrRlsLeXugOJWmQshjaOhZGAmQp3YM8MUndNJPf+S8K2pFYLN5FoPORtogPps pn9jqR3gFBN0lqOfeu/CKzY31fNtlGc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-340-OOkfY9pBPmuKOeVgxWMc5Q-1; Thu, 12 Oct 2023 10:07:17 -0400 X-MC-Unique: OOkfY9pBPmuKOeVgxWMc5Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D37858DBAEF; Thu, 12 Oct 2023 14:07:15 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id B656E4EA48; Thu, 12 Oct 2023 14:07:15 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH gmem FIXUP] selftests/kvm: guestmem: check fstat results on guestmem fd Date: Thu, 12 Oct 2023 10:07:15 -0400 Message-Id: <20231012140715.2445237-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 12 Oct 2023 07:08:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779558999224531889 X-GMAIL-MSGID: 1779558999224531889 Do a basic sanity cheeck for the st_size and st_ino fields of a guestmem file descriptor. The test would fail for example if guestmem.c used the innocuous-sounding function anon_inode_getfile() instead of the correct one, anon_inode_getfile_secure(). Signed-off-by: Paolo Bonzini --- .../testing/selftests/kvm/guest_memfd_test.c | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index 75073645aaa1..9805b0f8f26a 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -91,6 +91,31 @@ 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_multiple(struct kvm_vm *vm) +{ + int fd1, fd2, ret; + struct stat st1, st2; + + fd1 = __vm_create_guest_memfd(vm, 4096, 0); + TEST_ASSERT(fd1 != -1, "memfd creation should succeed"); + + ret = fstat(fd1, &st1); + TEST_ASSERT(ret != -1, "memfd fstat should succeed"); + TEST_ASSERT(st1.st_size == 4096, "memfd st_size should match requested size"); + + fd2 = __vm_create_guest_memfd(vm, 8192, 0); + TEST_ASSERT(fd2 != -1, "memfd creation should succeed"); + + ret = fstat(fd2, &st2); + TEST_ASSERT(ret != -1, "memfd fstat should succeed"); + TEST_ASSERT(st2.st_size == 8192, "second memfd st_size should match requested size"); + + ret = fstat(fd1, &st1); + TEST_ASSERT(ret != -1, "memfd fstat should succeed"); + TEST_ASSERT(st1.st_size == 4096, "first memfd st_size should still match requested size"); + TEST_ASSERT(st1.st_ino != st2.st_ino, "different memfd should have different inode numbers"); +} + static void test_create_guest_memfd_invalid(struct kvm_vm *vm) { uint64_t valid_flags = 0; @@ -153,6 +178,7 @@ int main(int argc, char *argv[]) vm = vm_create_barebones(); test_create_guest_memfd_invalid(vm); + test_create_guest_memfd_multiple(vm); fd = vm_create_guest_memfd(vm, total_size, 0);