Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37550979rwd; Tue, 11 Jul 2023 16:17:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlFDCQf26M4lmW519Bx7kJ9Hgd890XUaNbsIG3z9w60Vfwbw/R69qW5DmnlcH1v/diyXB22u X-Received: by 2002:aa7:dd0f:0:b0:51e:eba:55ba with SMTP id i15-20020aa7dd0f000000b0051e0eba55bamr10994605edv.32.1689117468066; Tue, 11 Jul 2023 16:17:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689117468; cv=none; d=google.com; s=arc-20160816; b=FoFwxzLOlsSxgljg8Wi6p58niayWF/E5qG1kQ+t3FTSQuN6hPcej9vxrsa/l7K5GqT 3In3g9Kf01ME6SmJIba5grYizvRQm20XvGnCIaZT6vkheaELqA8nPmws2dhqpSX2SdQz QTHMX7sbDzRtfQHEPTSQEk/Uikx7sNHZlsuHH1rWGwB3KJXiQQdIHU27MBJ2SdN73D6T ICmRYcCEvrdC5G3PlqjNkRvNiw1rx1TpPHCm4TmupSnRD1IBfjqgTMTB8J3aJbFh9vTA RwmsQKTihJAEG6VXESW+U2zURjw2FsnCrNuHCFM9CRJJzRbhL+l2htoF1FPEI/h+M7ov tSew== 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:reply-to:dkim-signature; bh=wQ6W4kIntv9wFXwEtPmIvIpacLwVSVo7NJzU3VtiE1w=; fh=WlkBVuaco6NrS/z+1f/wCzwszSt/RBqjyVC/qitxnws=; b=fGaucYanTkhNh2Y5eFD9UyxWO2s1uUgdJScQgmuRbDG6e+zz8r77oFPTaO/kveJY8i YziqyRaYKiK3dXWMGO3iKaMtTgDoFu3X+mxh8UYvKmQwG1cH6mlOExdn0b6EXAfU74hw d2tYJ1OxFrSIIc0IAB1UOVwZZcpCVeAiY9odsZ1HslzczkRLKP795ZEvjXlok+P7UgHd CdQdPDWL6Sr7Gtdi3SRJNKpJADlAyrWmNebcp1M0mKYNgPqZ1X0M1zYh3LU36MtEegEE D6bBWKJfza8ovaRhaS9iyJCZXiDLfEBkDfO6UqkUJgNtM8DnkHcsjYlLtUWT2z313toO WlKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3ORxn9Vh; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x19-20020aa7d393000000b0051e167b996asi3017974edq.656.2023.07.11.16.17.23; Tue, 11 Jul 2023 16:17:48 -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=3ORxn9Vh; 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 S231852AbjGKXBk (ORCPT + 99 others); Tue, 11 Jul 2023 19:01:40 -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-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 Content-Type: text/plain; charset="UTF-8" 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 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_RCBd5kfiOve8qXo7a81Cq53LuVQ5r=Q@mail.gmail.com Cc: stable@vger.kernel.org Cc: Kees Cook Signed-off-by: Sean Christopherson --- 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); } +static int kvm_vcpu_stats_release(struct inode *inode, struct file *file) +{ + struct kvm_vcpu *vcpu = file->private_data; + + kvm_put_kvm(vcpu->kvm); + return 0; +} + static const struct file_operations kvm_vcpu_stats_fops = { .read = kvm_vcpu_stats_read, + .release = kvm_vcpu_stats_release, .llseek = noop_llseek, }; @@ -4054,6 +4063,9 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) put_unused_fd(fd); return PTR_ERR(file); } + + kvm_get_kvm(vcpu->kvm); + file->f_mode |= FMODE_PREAD; fd_install(fd, file); @@ -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); } +static int kvm_vm_stats_release(struct inode *inode, struct file *file) +{ + struct kvm *kvm = file->private_data; + + kvm_put_kvm(kvm); + return 0; +} + static const struct file_operations kvm_vm_stats_fops = { .read = kvm_vm_stats_read, + .release = kvm_vm_stats_release, .llseek = noop_llseek, }; @@ -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 |= FMODE_PREAD; fd_install(fd, file); -- 2.41.0.255.g8b1d071c50-goog