From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C1EDC0015E for ; Tue, 11 Jul 2023 23:01:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231648AbjGKXBm (ORCPT ); Tue, 11 Jul 2023 19:01:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231308AbjGKXBi (ORCPT ); Tue, 11 Jul 2023 19:01:38 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A24FE60 for ; Tue, 11 Jul 2023 16:01:37 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-57059e6f9c7so2307857b3.0 for ; Tue, 11 Jul 2023 16:01:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116496; x=1691708496; 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=wQ6W4kIntv9wFXwEtPmIvIpacLwVSVo7NJzU3VtiE1w=; b=3ORxn9Vhd4WXF0g7JDL6HBWL9k07DS8mYs9fn0OJUVMYO7awAduaCjkyXa3c4zcUWP CiMyMGWKk7gpHnFuYLbMUK9kvuawoMxaIU5xCb4PbrzBwNxGeIU03YPFn53IH9fPrqeX 4FmTRkGhTHmOdCaZo/KIiAYJHZJ7xAvzMNbMTF2zTxH+n26utIZyh9RcbGfVY0YuOUAY y1agF/SkbXq6mvekFU03VqWiBTZiO0EljkyR/3Myi3Flrjtcedmj8KWUU1JHb75p6S/R VDL1tRai9UtK+iTVKTCqi+bXRmThYcXrJnJmqF9LyRxNIYLV4jA/f3UBqV+05g/WhPYh 3mJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116496; x=1691708496; 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=wQ6W4kIntv9wFXwEtPmIvIpacLwVSVo7NJzU3VtiE1w=; b=JvEWcqBN1NOWgWEVaSfuEcPDYtY7Xnn5OJxYDnKtcxfAL7kFlcN5S1ztdESz7brogf WZR6iCS82BIXWN3LpOr8mpKcJC+1uwyt+FvoKc1k6t2Yx2onoqR7f59E+9TtUShA9Ab8 P56v0EiDoTEC78oCO3JNKBuhdKu0tI8ocLuwL33u2uUX2SUevrBg8djkoxowhXNisSDo Mmw2WWpjBqpN+LdXxV+1wXIGXVV7wRkTuaiU0fBX37FjNx2u9vUC+uLUNmANFer3U0Ft GLCrj3719dpQf8gDU6A+wT9zVkPooUcgRbgJZfnLj5kky0U6CuPpA55QGpBjJLHkCBCS sF0g== X-Gm-Message-State: ABy/qLbYE7Ru9IrYpzmv9UggOWLpE+j6jQf0kVT8opo2MxlihvqVbmVG txTkVRFT35wFz9YXEx6jYNLlcsm83ks= X-Google-Smtp-Source: APBJJlHY1zUMZmD84o0W3pNWXRl+ifCASv/QArdWkFNwOfiiRusxKodO28LqgUUoEWUMxCkFV3iHUrKcU5w= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:af18:0:b0:576:e268:903d with SMTP id n24-20020a81af18000000b00576e268903dmr3211ywh.2.1689116496718; Tue, 11 Jul 2023 16:01:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:25 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-2-seanjc@google.com> Subject: [PATCH 1/7] KVM: Grab a reference to KVM for VM and vCPU stats file descriptors From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Grab a reference to KVM prior to installing VM and vCPU stats file descriptors to ensure the underlying VM and vCPU objects are not freed until the last reference to any and all stats fds are dropped. Note, the stats paths manually invoke fd_install() and so don't need to grab a reference before creating the file. Fixes: ce55c049459c ("KVM: stats: Support binary stats retrieval for a VCPU= ") Fixes: fcfe1baeddbf ("KVM: stats: Support binary stats retrieval for a VM") Reported-by: Zheng Zhang Closes: https://lore.kernel.org/all/CAC_GQSr3xzZaeZt85k_RCBd5kfiOve8qXo7a81= Cq53LuVQ5r=3DQ@mail.gmail.com Cc: stable@vger.kernel.org Cc: Kees Cook Signed-off-by: Sean Christopherson Reviewed-by: Kees Cook --- virt/kvm/kvm_main.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index b838c8f71349..312a8d9184fe 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4032,8 +4032,17 @@ static ssize_t kvm_vcpu_stats_read(struct file *file= , char __user *user_buffer, sizeof(vcpu->stat), user_buffer, size, offset); } =20 +static int kvm_vcpu_stats_release(struct inode *inode, struct file *file) +{ + struct kvm_vcpu *vcpu =3D file->private_data; + + kvm_put_kvm(vcpu->kvm); + return 0; +} + static const struct file_operations kvm_vcpu_stats_fops =3D { .read =3D kvm_vcpu_stats_read, + .release =3D kvm_vcpu_stats_release, .llseek =3D noop_llseek, }; =20 @@ -4054,6 +4063,9 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcp= u *vcpu) put_unused_fd(fd); return PTR_ERR(file); } + + kvm_get_kvm(vcpu->kvm); + file->f_mode |=3D FMODE_PREAD; fd_install(fd, file); =20 @@ -4698,8 +4710,17 @@ static ssize_t kvm_vm_stats_read(struct file *file, = char __user *user_buffer, sizeof(kvm->stat), user_buffer, size, offset); } =20 +static int kvm_vm_stats_release(struct inode *inode, struct file *file) +{ + struct kvm *kvm =3D file->private_data; + + kvm_put_kvm(kvm); + return 0; +} + static const struct file_operations kvm_vm_stats_fops =3D { .read =3D kvm_vm_stats_read, + .release =3D kvm_vm_stats_release, .llseek =3D noop_llseek, }; =20 @@ -4718,6 +4739,9 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm) put_unused_fd(fd); return PTR_ERR(file); } + + kvm_get_kvm(kvm); + file->f_mode |=3D FMODE_PREAD; fd_install(fd, file); =20 --=20 2.41.0.255.g8b1d071c50-goog From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF6A2C001B0 for ; Tue, 11 Jul 2023 23:01:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232065AbjGKXBo (ORCPT ); Tue, 11 Jul 2023 19:01:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231742AbjGKXBk (ORCPT ); Tue, 11 Jul 2023 19:01:40 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10F4710D8 for ; Tue, 11 Jul 2023 16:01:39 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1b8a4571c1aso64623065ad.0 for ; Tue, 11 Jul 2023 16:01:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116498; x=1691708498; 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=UMgWqyV1osh/Eb0aP0u4bhUqRrnmL/HIe/0o+d3Tstg=; b=ZjSlV+P+WjR6ipP1RlWNPxFrwdcht0OxmLKa0QLn3xaLeCwtB9UDH/7OWOaXSa1z1+ e2Xp3FRdI2MWx37ejpgHg8bgTtmoMYn4siDuMjw83SChlUw9YLK/9sO4njF70fv1cBBN 0VDi+v5aqplRYzKmOv5koReQ1cr5yZJ/6qo31Jrb6tT3f3Q3npKiGZNGkE29jcxFW4iS Tv/jkowMfOnaNQdgZQCVAO6/XIDOIM/d2JLTw6PD01ywwtTBefmUWejntl+JNTuU0uXW 19H67WBVIO5m3ryNK9unbbDJgSJcn40VHQ/GW1DoqjAjEfRv8j+KkQcj6nbnVMLcta6Y SgHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116498; x=1691708498; 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=UMgWqyV1osh/Eb0aP0u4bhUqRrnmL/HIe/0o+d3Tstg=; b=IsjFdGWKP6SGb1MLTJV09qqmn+I1lcAkWSywDHHnj1NOu+dCtzGtQBZIeKnhyNy7Pg UblkzVnaX9n/Qu9QM/7pljeMkR01jktsbZA4+zOG45Nd6tnKJmbzG8Zr+YkIJtL9JDus 41O+wg80V9lnBv7naiFgF7HZz+2D9l1lGsMU38W5zO2qOl0pjL3vbX0y08XkKhkU0uWa hQuzg9+VaSdOcw9I6gtgP/mvacIBKc8mu3TwbvLdLdymM52/Muwv9rGOjXng1HW7rbz0 C7TBxE4Owf6YBSVDRIB3FgMf940Cje4OmIUr46QopQ+ARfi/EgFQqmbbXvHYhUK9Fgqn agYQ== X-Gm-Message-State: ABy/qLYVr8QemvxDY7O8ZxxBjwN2yfNZCtwKL7CSlbgTW/k3VoeqerLp lt1vAsI0clPO2W293HKDZWcAwLPRDoo= X-Google-Smtp-Source: APBJJlHIsJhSGesygyR2pvIXL2h+l2QVdYqlNq2kM9qsrLj2xYFk6PgbKKr53V9ZXQsNbOblHHioGHN/RSI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:da92:b0:1b8:95a2:38ab with SMTP id j18-20020a170902da9200b001b895a238abmr13020184plx.8.1689116498492; Tue, 11 Jul 2023 16:01:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:26 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-3-seanjc@google.com> Subject: [PATCH 2/7] KVM: selftests: Use pread() to read binary stats header From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use pread() with an explicit offset when reading the header and the header name for a binary stats fd so that the common helper and the binary stats test don't subtly rely on the file effectively being untouched, e.g. to allow multiple reads of the header, name, etc. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/include/kvm_util_base.h | 6 ++++-- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/te= sting/selftests/kvm/include/kvm_util_base.h index 07732a157ccd..eb1ff597bcca 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -362,8 +362,10 @@ static inline void read_stats_header(int stats_fd, str= uct kvm_stats_header *head { ssize_t ret; =20 - ret =3D read(stats_fd, header, sizeof(*header)); - TEST_ASSERT(ret =3D=3D sizeof(*header), "Read stats header"); + ret =3D pread(stats_fd, header, sizeof(*header), 0); + TEST_ASSERT(ret =3D=3D sizeof(*header), + "Failed to read '%lu' header bytes, ret =3D '%ld'", + sizeof(*header), ret); } =20 struct kvm_stats_desc *read_stats_descriptors(int stats_fd, diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/te= sting/selftests/kvm/kvm_binary_stats_test.c index a7001e29dc06..eae99d0e8377 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -43,8 +43,10 @@ static void stats_test(int stats_fd) id =3D malloc(header.name_size); TEST_ASSERT(id, "Allocate memory for id string"); =20 - ret =3D read(stats_fd, id, header.name_size); - TEST_ASSERT(ret =3D=3D header.name_size, "Read id string"); + ret =3D pread(stats_fd, id, header.name_size, sizeof(header)); + TEST_ASSERT(ret =3D=3D header.name_size, + "Expected header size '%u', read '%lu' bytes", + header.name_size, ret); =20 /* Check id string, that should start with "kvm" */ TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size, --=20 2.41.0.255.g8b1d071c50-goog From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E91ACEB64DD for ; Tue, 11 Jul 2023 23:01:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232096AbjGKXBt (ORCPT ); Tue, 11 Jul 2023 19:01:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232026AbjGKXBm (ORCPT ); Tue, 11 Jul 2023 19:01:42 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3818310D8 for ; Tue, 11 Jul 2023 16:01:41 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-262d69faef9so6960784a91.3 for ; Tue, 11 Jul 2023 16:01:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116500; x=1691708500; 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=8VQapMKBQBMTZxsjOULvb/Z6n7A3B0Prs/TRyhAaINo=; b=0meBJIEhxTgxt0w5IKnl8Z7teH02rv9JZxqyDtEuF1SoUNM0EhdKFpSC+T8V5Zcc4f ItM4ARCIWVT/rgEt8uYkBwBE/REmEktJEKeeLxdD+HYTwWEZGeMbWRig1L8QTKsIQ5T2 hVkbXXKQSkWbxGGMsrcm56i8+aD/5dB7DOJP97opcnl2Ry4E6RHruR/qykibFq0/aUfW extko0Q/JcbDrL4/gyZcVcBTXaYze++0rQtwMUtlL1UnwshBD8Q5le30OExohxvBKRNa iLoDSD7fcIE/6uWCbzRK5jSM2JFe8rTxZeHBnCvka8ar7fO6J7NhQ6ilvUovBerrQ8U2 Jhvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116500; x=1691708500; 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=8VQapMKBQBMTZxsjOULvb/Z6n7A3B0Prs/TRyhAaINo=; b=H9NxWJiyzBg0uq0tCgVMftQ4zTYVB6mT0QJeDae9gtXzEF01SmscsWyDvKRSuB7j0/ 5p5fwTa5wmogFDBpewq/Jji4TlItgC3hRHSuXGH7UysMec1+cJKXOUqfnGvstc467bUt eRL53sWNIgfug/ruRuTgsCrLl5trZoU3AUnHkhIWsR/IibGKMbFwfQN7Oe+ob7kYwbcn HpW7DvzZ/F7bWNs7OOVYSYvdIk+eIt94XIWxsA+paofF1DEhICHssL0zjxrDesLvRMux BAV9gO/8Bf85a6oow+e6UISQ+52wbYWW3J+tfOHPfd3XkL5EwjLk3ENgPl5wR6n5cjUn RVTg== X-Gm-Message-State: ABy/qLZ0hLRIoLry0OG5rH20qfLr4O60MGOuM4Ha3Ra8H+UWWT7vAGrW 4RnQTmwty40nhdjJU5A4PeJ/rDwU9nY= X-Google-Smtp-Source: APBJJlGManPSJIk0kdX94lHfSRSwM/QUvQS1ucWRoZhgJW/JZxlZW9IEoHaKwptTif9cwXe6p+xwpmckpJs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:a417:b0:1b8:2055:fc1f with SMTP id p23-20020a170902a41700b001b82055fc1fmr13104726plq.2.1689116500737; Tue, 11 Jul 2023 16:01:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:27 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-4-seanjc@google.com> Subject: [PATCH 3/7] KVM: selftests: Clean up stats fd in common stats_test() helper From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the stats fd cleanup code into stats_test() and drop the superfluous vm_stats_test() and vcpu_stats_test() helpers in order to decouple creation of the stats file from consuming/testing the file (deduping code is a bonus). This will make it easier to test various edge cases related to stats, e.g. that userspace can dup() a stats fd, that userspace can have multiple stats files for a singleVM/vCPU, etc. Signed-off-by: Sean Christopherson --- .../selftests/kvm/kvm_binary_stats_test.c | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/te= sting/selftests/kvm/kvm_binary_stats_test.c index eae99d0e8377..f02663711c90 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -167,23 +167,7 @@ static void stats_test(int stats_fd) free(stats_data); free(stats_desc); free(id); -} =20 - -static void vm_stats_test(struct kvm_vm *vm) -{ - int stats_fd =3D vm_get_stats_fd(vm); - - stats_test(stats_fd); - close(stats_fd); - TEST_ASSERT(fcntl(stats_fd, F_GETFD) =3D=3D -1, "Stats fd not freed"); -} - -static void vcpu_stats_test(struct kvm_vcpu *vcpu) -{ - int stats_fd =3D vcpu_get_stats_fd(vcpu); - - stats_test(stats_fd); close(stats_fd); TEST_ASSERT(fcntl(stats_fd, F_GETFD) =3D=3D -1, "Stats fd not freed"); } @@ -241,9 +225,11 @@ int main(int argc, char *argv[]) =20 /* Check stats read for every VM and VCPU */ for (i =3D 0; i < max_vm; ++i) { - vm_stats_test(vms[i]); + stats_test(vm_get_stats_fd(vms[i])); + for (j =3D 0; j < max_vcpu; ++j) - vcpu_stats_test(vcpus[i * max_vcpu + j]); + stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j])); + ksft_test_result_pass("vm%i\n", i); } =20 --=20 2.41.0.255.g8b1d071c50-goog From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F7CCEB64DC for ; Tue, 11 Jul 2023 23:01:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231494AbjGKXBx (ORCPT ); Tue, 11 Jul 2023 19:01:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232033AbjGKXBn (ORCPT ); Tue, 11 Jul 2023 19:01:43 -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 DEF2410CF for ; Tue, 11 Jul 2023 16:01:42 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1b89f6463deso90969695ad.1 for ; Tue, 11 Jul 2023 16:01:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116502; x=1691708502; 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=5/AE+ITsqvmioGa40XEXVYetYnCHAyNbSGcDIdfkEo0=; b=UGGyrEU2rRB+Omiva8nPATSlSEqaSxk63p3TInzxbWo+/6QMUITBxd8zKcCdqBQOgv ZvTdd4ljaWKsFJkcCTatWT46sovnTwclM3/vSjyjtmHtJXe6znNmbMjUusE9neFjUoLS rju2Mx09S26eioR9MSOvX+6ArkkP7bT0Fe9xCiK+OJw4y45IjAylZheoBTmlkqcUjpKK CkVn/r4bAFP2KnbOGhGWi2PXasUE0PPcGlwau50UwGsgu855fnirqWiooWSEhgOX5N0o LcJPUsnrx5OW/JMAi9yuRFoKReQL0RUmtSL7gKmnWL3uIoDU2UcuhPkyf1kgGdnpORmW Vt1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116502; x=1691708502; 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=5/AE+ITsqvmioGa40XEXVYetYnCHAyNbSGcDIdfkEo0=; b=YZ4Ss6Akr36Z+Jq4s0JKiaQGbbV0oVCIkeAmn/0GsHw4KN65BVCNeup2tUTLa7POvQ 21hzs4PAjtjwPIXE+P/48Ug5HUChutpazkcZnLj9FlvV0tvfoxPNVds15OMxQ4/SpmCe UhKI6QEfOVdg28X8LAixDPSHyBJ+0TZLyQAw6hdNm5DcM594rY+noX/Qsb+d8XQ3VlHk JpLu2juV/lYjJBnp32Wv0QMQUGcBezVwcqvteW9+oTCAQEFcYFAHEIKY9LTkzv0sKOYu 2hXp9QkVG7MLfwVURlyWl3WUqt4xg4qMM1XhMsCx+YlmjJj4A5gZgTBdtP/inHmBabBG ZEcg== X-Gm-Message-State: ABy/qLZ/s+SIg44GKwS9sZxzQwL84/aEHhIxGkXjoncYcJHmLKBwRP7y HYmTbXjL9XwAxk7Lzb9CTx1CyYnR9NA= X-Google-Smtp-Source: APBJJlFn9f27q2unG9NlnsNp+cUz++DpEnxoagI+8yGC0xjV9dz5nGMU5qsqNYDVIwOYj7tKo4m8T++oD5g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ee55:b0:1b8:2cee:946b with SMTP id 21-20020a170902ee5500b001b82cee946bmr13684768plo.11.1689116502354; Tue, 11 Jul 2023 16:01:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:28 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-5-seanjc@google.com> Subject: [PATCH 4/7] KVM: selftests: Explicitly free vcpus array in binary stats test From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Explicitly free the all-encompassing vcpus array in the binary stats test so that the test is consistent with respect to freeing all dynamically allocated resources (versus letting them be freed on exit). Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/te= sting/selftests/kvm/kvm_binary_stats_test.c index f02663711c90..874fa5092551 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -236,6 +236,7 @@ int main(int argc, char *argv[]) for (i =3D 0; i < max_vm; ++i) kvm_vm_free(vms[i]); free(vms); + free(vcpus); =20 ksft_finished(); /* Print results and exit() accordingly */ } --=20 2.41.0.255.g8b1d071c50-goog From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF314EB64DC for ; Tue, 11 Jul 2023 23:02:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230394AbjGKXCD (ORCPT ); Tue, 11 Jul 2023 19:02:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232068AbjGKXB5 (ORCPT ); Tue, 11 Jul 2023 19:01:57 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D08F01984 for ; Tue, 11 Jul 2023 16:01:45 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c6dd0e46a52so5107017276.2 for ; Tue, 11 Jul 2023 16:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116505; x=1691708505; 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=GyUg5dlzIa8McwdzPE+wxpmpG2aTO24P2TiMipK75HY=; b=Y2C7A9LJxKi26AKZCOedeQsoU3MSTc2nCv9iMnVsDgIcycFwF2iW/mzm4WJG9duMfW E6cxW6CLJjgVxajIFrFH1vILGdm2llWGYIwxULVvyPOHOSPSf45rTDmJlnDVcJW2DpSV yFj3uOhDqKIzO5tGc1CYv5gEHb47dgZeH2zz/FzRNqmF/yGQv6AT83/BxCF+asTB7gCi lGmI4ZTZRIWM1VXqLMA1+zHzXj5Rj7+cVH9NOZqZqCjtP2TsuqP/6PzCFvhNvuKWFY+N rwxN6Cl/NT3yjxO74GFQR/LkqBACrzmAydFPX2wn1C1SNALxw4OYgP0PMjKBYvu23ErB KtcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116505; x=1691708505; 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=GyUg5dlzIa8McwdzPE+wxpmpG2aTO24P2TiMipK75HY=; b=jyHqMrrVy/+D4bMBZfou7DV3drDfVvwUgz6dLi14ECEitgyHCXJpJbVSolG2/WSpKY HQRtEYDUHJZr1pTkc0GmyQ+jnFoF/f35WSUcBJB0Afnj6v+aMPJDPM4Eq+2B+b4g8FTl CEexcIIh+EoVurLmCqGhXycjc44Cs1AhCPhPY8TGcUpup/1E1UIDwwxlo3fYPRSp7nXb Q7c0xZ7cxdcFozM+NlLV3x/vPyYYot7tIjbsmXqJF/pTpcqVxnOv9ODCB0hPuTDPaxzZ dC907bgG55m0OxmlH5qtYDHGUpu23CgmyAvZvGb4WRMZf/QyIM9SrAUc7ZlAo8hqQB3h WGgQ== X-Gm-Message-State: ABy/qLYqyssbOUXle3jkKH7u/po1IQEHBCsLYcmg0ecsiUl5hbHv5rGd AouBKEHUjFkFlUj5QonoZPCzJ+BcMvU= X-Google-Smtp-Source: APBJJlFUFLR8L4iRRHt9rKjYaaqegk++QZhf19a4JUv2eiG3mJ24F0+9Bee8MHPbDKZ6dEhXoMkWSkYNdZ0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:508:b0:c4b:6ed6:6147 with SMTP id x8-20020a056902050800b00c4b6ed66147mr155293ybs.9.1689116505063; Tue, 11 Jul 2023 16:01:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:29 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-6-seanjc@google.com> Subject: [PATCH 5/7] KVM: selftests: Verify userspace can create "redundant" binary stats files From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Verify that KVM doesn't artificially limit KVM_GET_STATS_FD to a single file per VM/vCPU. There's no known use case for getting multiple stats fds, but it should work, and more importantly creating multiple files will make it easier to test that KVM correct manages VM refcounts for stats files. Signed-off-by: Sean Christopherson --- .../selftests/kvm/kvm_binary_stats_test.c | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/te= sting/selftests/kvm/kvm_binary_stats_test.c index 874fa5092551..653f10d8fb7c 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -185,6 +185,7 @@ static void stats_test(int stats_fd) =20 int main(int argc, char *argv[]) { + int vm_stats_fds, *vcpu_stats_fds; int i, j; struct kvm_vcpu **vcpus; struct kvm_vm **vms; @@ -217,18 +218,37 @@ int main(int argc, char *argv[]) vcpus =3D malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu); TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers"); =20 + /* + * Not per-VM as the array is populated, used, and invalidated within a + * single for-loop iteration. + */ + vcpu_stats_fds =3D calloc(max_vm, sizeof(*vcpu_stats_fds)); + TEST_ASSERT(vcpu_stats_fds, "Allocate memory for VM stats fds"); + for (i =3D 0; i < max_vm; ++i) { vms[i] =3D vm_create_barebones(); for (j =3D 0; j < max_vcpu; ++j) vcpus[i * max_vcpu + j] =3D __vm_vcpu_add(vms[i], j); } =20 - /* Check stats read for every VM and VCPU */ + /* + * Check stats read for every VM and vCPU, with a variety of testcases. + * Note, stats_test() closes the passed in stats fd. + */ for (i =3D 0; i < max_vm; ++i) { + vm_stats_fds =3D vm_get_stats_fd(vms[i]); + + /* Verify userspace can instantiate multiple stats files. */ stats_test(vm_get_stats_fd(vms[i])); =20 - for (j =3D 0; j < max_vcpu; ++j) + for (j =3D 0; j < max_vcpu; ++j) { + vcpu_stats_fds[j] =3D vcpu_get_stats_fd(vcpus[i * max_vcpu + j]); stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j])); + } + + stats_test(vm_stats_fds); + for (j =3D 0; j < max_vcpu; ++j) + stats_test(vcpu_stats_fds[j]); =20 ksft_test_result_pass("vm%i\n", i); } @@ -237,6 +257,7 @@ int main(int argc, char *argv[]) kvm_vm_free(vms[i]); free(vms); free(vcpus); + free(vcpu_stats_fds); =20 ksft_finished(); /* Print results and exit() accordingly */ } --=20 2.41.0.255.g8b1d071c50-goog From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9DA5EB64DC for ; Tue, 11 Jul 2023 23:02:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232180AbjGKXCG (ORCPT ); Tue, 11 Jul 2023 19:02:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232116AbjGKXB6 (ORCPT ); Tue, 11 Jul 2023 19:01:58 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72AC7199E for ; Tue, 11 Jul 2023 16:01:47 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2631fc29e8aso6932728a91.3 for ; Tue, 11 Jul 2023 16:01:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116507; x=1691708507; 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=qyvtqeRPq/MlF0ZTdDehI+RMIDCPhIW4IOYWhk2FAaM=; b=QEG302AqsBou4e85I0bF1Fz1XHfj0kuK+s+Tyb/kkQXroB3QCJtAhiMap8KwBAPra/ 6dcmyFBYaZJ8RT7RQa7OY1Sv8Bq/HZxHa/hQ/i0T48PfCJX4o82eWzWooZWWpf9I/9hI BmJU4cP7FdnXukTED5kPtBE1V7fVEVH3jgT7bjT7vwZcpSYJXmNuAefLnVlGTauwTbFN J2hChuMEbuHZ4Lg75AxBrMxefqcIHUIiyVrsJFOykmBCruaMa6gD+2f0zzjDugYF/Nby c66Uj630xv5E9wJ2kPs2CBKz123BxpbrZJ38lJWWHiYCINz+XSK+zr8bzynn2LI3K0tL ynqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116507; x=1691708507; 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=qyvtqeRPq/MlF0ZTdDehI+RMIDCPhIW4IOYWhk2FAaM=; b=T1Q0SyVL+uByS9olfs11gdeL0V9OG6pF59jGD3acb6zNDIXA+wUF+kOmbbhJYpEx9T qvdI4XQhXVAaHkzZQXI1xBwFj0Y96ERCrl+QURJo3gCPQI2vhXu/I3k0mYv8981VKpDk QHWnT0xMkND4UnTtvAIqfYdidfQUocydjXq5kZH3YANKURGrbJHZJm0WykVqKjM3eef8 4FWOHaZX7GdzKV8pQLM08t8BywNiAKM2pv8M/f2D0OiEY0neiwZRQzfqrxPNQPwJ7SiK qfexKdR/dp10CPG60MeofWq7Rbr4fAahkadmia52jOx5zW/J5JH1lKbAXF5BLwQPOF7p sx8Q== X-Gm-Message-State: ABy/qLboIQp4w5dy6a1slXOAqcy0z5pKJVaqE1IJBLCYkRYFViEyXrCw 5f91T2xPGRnXAZw/CeADYe1Mf2MEV/k= X-Google-Smtp-Source: APBJJlFssnO9DQk9xNcC8/Dw0TPX6R+aq2Z9D0TlOLfkADLXcqzjIuHJH8FHS9An3QkxmoLlpbPu6f6OJtY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ead1:b0:1b9:e8e5:b0a4 with SMTP id p17-20020a170902ead100b001b9e8e5b0a4mr2142642pld.8.1689116506840; Tue, 11 Jul 2023 16:01:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:30 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-7-seanjc@google.com> Subject: [PATCH 6/7] KVM: selftests: Verify stats fd can be dup()'d and read From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expand the binary stats test to verify that a stats fd can be dup()'d and read, to (very) roughly simulate userspace passing around the file. Adding the dup() test is primarily an intermediate step towards verifying that userspace can read VM/vCPU stats before _and_ after userspace closes its copy of the VM fd; the dup() test itself is only mildly interesting. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/te= sting/selftests/kvm/kvm_binary_stats_test.c index 653f10d8fb7c..5317e27b77d0 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -232,17 +232,23 @@ int main(int argc, char *argv[]) } =20 /* - * Check stats read for every VM and vCPU, with a variety of testcases. + * Check stats read for every VM and vCPU, with a variety of flavors. * Note, stats_test() closes the passed in stats fd. */ for (i =3D 0; i < max_vm; ++i) { + /* + * Verify that creating multiple userspace references to a + * single stats file works and doesn't cause explosions. + */ vm_stats_fds =3D vm_get_stats_fd(vms[i]); + stats_test(dup(vm_stats_fds)); =20 /* Verify userspace can instantiate multiple stats files. */ stats_test(vm_get_stats_fd(vms[i])); =20 for (j =3D 0; j < max_vcpu; ++j) { vcpu_stats_fds[j] =3D vcpu_get_stats_fd(vcpus[i * max_vcpu + j]); + stats_test(dup(vcpu_stats_fds[j])); stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j])); } =20 --=20 2.41.0.255.g8b1d071c50-goog From nobody Sat Feb 7 22:21:11 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46BF9EB64DD for ; Tue, 11 Jul 2023 23:02:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232113AbjGKXCO (ORCPT ); Tue, 11 Jul 2023 19:02:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232146AbjGKXCB (ORCPT ); Tue, 11 Jul 2023 19:02:01 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A4751BD6 for ; Tue, 11 Jul 2023 16:01:49 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1b8a4571c1aso64624805ad.0 for ; Tue, 11 Jul 2023 16:01:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689116509; x=1691708509; 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=NZYUXnjHPmQ/WMoe3NgtL+hMKetCK3WyAi/ewwv+qe0=; b=l44qBzgB6Q7SSsjQQ711GNRtQ8qlOMLsmMgf6zreaVte1Im4NUmW6FKj+LT1tEJ8RO hR+9DexXG3PZhGuIm5YgC4YyxlYUsdSIakurMN/gb2LCu+2WXYx03xTMfim8bjxZ0MYJ m05at+g01AIaxdj8VVc8YP/xE43ba1hHeUTyXRsLXEg1grEM7ikivejicwH32E5WoOaK xbHxFXfDCDUskgotNbrIwV9pvIwQPxYbD6bFoYSJVzsadr7oUWIhKGvNKaFOa9dSEjXT lHfnSzEhc0UQqSEf7zRwCljA/cQwTMMzZz+1ywwFNxq2HQZtxYqFqzU8XPhuHaCYEruo KE1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689116509; x=1691708509; 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=NZYUXnjHPmQ/WMoe3NgtL+hMKetCK3WyAi/ewwv+qe0=; b=T5Kymj9H7A2EMoozb+HFyWYCDS1q2jKNvv9Y31uJrxImiMVg8fhJcg/R51H2dBIw7b McqphPriR5vsTFM0dTXC8kNLX1ZrlcSgAJJbGEtLx/Ivi8HTiy4YEgO/a60vs9R8qxeY 7UJlmESNaUviZ7rAW8jfCTLDY349fgq5tMs2liie9tWuUku4cAb5lJysCb533l3O8h3R qyewYSPxm5w/Odzxi2EVzkXD4rofgILAddgbxmkDJ29Gp9/J7WIKkb/9oWxyGluu0pO8 s1jyXySK+JXWXH+tQ7hwO3d9eWF+txz6lyQuf72jQ3gMcZiZGTvCahRSCrysPp2u9ctY PrQA== X-Gm-Message-State: ABy/qLZCdOr18KxtaEvMIURReTUEuAk1fFI+wP5l4Cv/+X81XsfvQLwN 5Eoil/8I5pA6HhqRTalFB/cKndUk3rE= X-Google-Smtp-Source: APBJJlHpwQnUAsfgftTMh1px84YuyX4vmpkN3OpQhjOCUnpxZyihOxV+4Zc6ADNt1Gp/Iop1b2qhlEf0Vs8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:a417:b0:1b8:8cd9:5355 with SMTP id p23-20020a170902a41700b001b88cd95355mr12083294plq.6.1689116508764; Tue, 11 Jul 2023 16:01:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 11 Jul 2023 16:01:31 -0700 In-Reply-To: <20230711230131.648752-1-seanjc@google.com> Mime-Version: 1.0 References: <20230711230131.648752-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711230131.648752-8-seanjc@google.com> Subject: [PATCH 7/7] KVM: selftests: Verify stats fd is usable after VM fd has been closed From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zheng Zhang , Kees Cook , Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Verify that VM and vCPU binary stats files are usable even after userspace has put its last direct reference to the VM. This is a regression test for a UAF bug where KVM didn't gift the stats files a reference to the VM. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/te= sting/selftests/kvm/kvm_binary_stats_test.c index 5317e27b77d0..698c1cfa3111 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -252,6 +252,14 @@ int main(int argc, char *argv[]) stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j])); } =20 + /* + * Close the VM fd and redo the stats tests. KVM should gift a + * reference (to the VM) to each stats fd, i.e. stats should + * still be accessible even after userspace has put its last + * _direct_ reference to the VM. + */ + kvm_vm_free(vms[i]); + stats_test(vm_stats_fds); for (j =3D 0; j < max_vcpu; ++j) stats_test(vcpu_stats_fds[j]); @@ -259,8 +267,6 @@ int main(int argc, char *argv[]) ksft_test_result_pass("vm%i\n", i); } =20 - for (i =3D 0; i < max_vm; ++i) - kvm_vm_free(vms[i]); free(vms); free(vcpus); free(vcpu_stats_fds); --=20 2.41.0.255.g8b1d071c50-goog