From patchwork Sat Apr 2 17:40:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12799389 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B6749C433F5 for ; Sat, 2 Apr 2022 17:42:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=l94r8WpzvtxQPIEAizx/WHMNSK9ElvWV37JHLkNfSfg=; b=Vj/dllvUBPpWhZRBGoFnmwJqsU KFqbVpwE/585PlZvX6lbvoPMPKzX1X8uY47LWOo4YDZu8zl8PEYr+loSCGoaN0cbQF2oScHlbSn0K hPCELhv8nhGz4qiZhip8ptbJXevJsdg/2E7Q78mu/3TewpebzVrYEpWCLeZNp3YUdZpmNC/v8ULM6 wJApYPWVcXJNNZ3vcJU0nFm75Qb7v0MPJB+qvFBl6b9hgizJ60VNQu2iYXVoElsmLAmKJaqn3CCEX gePKVBibyZaD0oZhWThIWk1g9Nv32jNk55kWaD/HoHRY+jhxlcEGr+eHG55/TAeM+TwuHWc8weIhu 0MQfN0/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahkE-009jf0-TR; Sat, 02 Apr 2022 17:41:03 +0000 Received: from mail-il1-x14a.google.com ([2607:f8b0:4864:20::14a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahk3-009jZC-Ms for linux-arm-kernel@lists.infradead.org; Sat, 02 Apr 2022 17:40:52 +0000 Received: by mail-il1-x14a.google.com with SMTP id t16-20020a056e02061000b002c7ddaa0006so3739657ils.7 for ; Sat, 02 Apr 2022 10:40:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=gTC6wbFjulJGpnpdlR2cK65pyxNM9FH8zxgBPKIedMA=; b=IWfMP6PPwTJ7suUb8PtMClWLhu9Q7lmuLZmLxHUiZhB7qG4ONiUnCn55RSteRI6Lj5 WnEriAXO/P4p+M1csdNAB/x8uJiummx6Y0/TAZvBpSRquz7KG9vvI9Z6+/yhFBG80bUu 39n7syXyAT1fsyP29qR+jYISRWRnLync2LwfFqLxLJaprc1qfO6clU6/t+z5WaogkP/5 pF2RgGe/XeZ5vptUf4kb+iPKSZrA25QUiVI1NECCavK79JLnaNP1nDRCJ8PGXkQUIXcp 9MIuPc1dQkdbiiC/Rw1s3I6v/S07jCmPozzTf7mjzKbEIjMM3oXDFA7NbxxInFHCcbGP OarA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=gTC6wbFjulJGpnpdlR2cK65pyxNM9FH8zxgBPKIedMA=; b=awLixpBKdnov1u+IwU1wLecwhkqJSYy8+MLJIIuwS+lo3PixvliwT/zN+vzsq45T/i xKGNKCbeLund5EH8xnRkDSslUc94EaywUTfOzE84KO7tSG3u2dz3i3JrFby5Q5XtCctG VplWucNd3cfWtygmxKWk9zLGJZE8ocLhDVMwKZ38aSb28F6mlVBTtMOR+4w4rp0zwYpF EkmQyCYq8KD2Hmk0K+PceLFkKrzf+fmOtKPS23yvOV8LLt3LEK2D/0t/ywQH1s/V5KRC 3V8/zM6/5UIt8puy0PyX8ik+A5c4dymPja3Ewx3wAL+Fy9VjQfexGLw3mutN6UzrlSoK AZ/g== X-Gm-Message-State: AOAM530oTy2gr5bauZ4yO7DGH5/tNPfnSeHPQSY3G7J575TNKSMQhVGH pqj7QhgIE/pprlDzKIA5dxXlFUpRwJo= X-Google-Smtp-Source: ABdhPJxh22igukLOyB7xjQIkB7xrRakZ5rtTFEJOHvXCH6QTQaKRf4Vev65C2RCw3Khe1CzIiG7PGVWShMA= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a92:6810:0:b0:2ca:1ff:e32e with SMTP id d16-20020a926810000000b002ca01ffe32emr2313380ilc.212.1648921249504; Sat, 02 Apr 2022 10:40:49 -0700 (PDT) Date: Sat, 2 Apr 2022 17:40:41 +0000 In-Reply-To: <20220402174044.2263418-1-oupton@google.com> Message-Id: <20220402174044.2263418-2-oupton@google.com> Mime-Version: 1.0 References: <20220402174044.2263418-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH 1/4] KVM: arm64: vgic: Don't assume the VM debugfs directory exists From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Paolo Bonzini , Sean Christopherson , Oliver Upton , stable@kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220402_104051_781070_C78C7CD7 X-CRM114-Status: GOOD ( 12.97 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Unfortunately, there is no guarantee that KVM was able to instantiate a debugfs directory for a particular VM. To that end, KVM shouldn't even attempt to create new debugfs files in this case. If the specified parent dentry is NULL, debugfs_create_file() will instantiate files at the root of debugfs. Since it is possible to create the vgic-state file outside of a VM directory, the file is not cleaned up when a VM is destroyed. Nonetheless, the corresponding struct kvm is freed when the VM is destroyed. Plug the use-after-free by plainly refusing to create vgic-state when KVM fails to create a VM debugfs dir. Cc: stable@kernel.org Fixes: 929f45e32499 ("kvm: no need to check return value of debugfs_create functions") Signed-off-by: Oliver Upton --- arch/arm64/kvm/vgic/vgic-debug.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/arm64/kvm/vgic/vgic-debug.c b/arch/arm64/kvm/vgic/vgic-debug.c index f38c40a76251..cf1364a6fabc 100644 --- a/arch/arm64/kvm/vgic/vgic-debug.c +++ b/arch/arm64/kvm/vgic/vgic-debug.c @@ -271,6 +271,9 @@ DEFINE_SEQ_ATTRIBUTE(vgic_debug); void vgic_debug_init(struct kvm *kvm) { + if (!kvm->debugfs_dentry) + return; + debugfs_create_file("vgic-state", 0444, kvm->debugfs_dentry, kvm, &vgic_debug_fops); } From patchwork Sat Apr 2 17:40:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12799392 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E6F1AC433F5 for ; Sat, 2 Apr 2022 17:42:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=MjoCjizv8UEei5J+58AvStOPk9fZg7Im9qqGdmNRMXE=; b=mjf2Vv6OKxihRpO0cFVJmSiIBM 9NwRt4FBOAzuXs2YPVisoIkBcDsFKZPjgyLG+fLgEZqoIXXkGOk5kiasIuy5xTQN7oaEGDdTmXf7o mikp9bsSocIVJC6+8MDOBmf/gldnJ9jyCAYVIaMkz8VVdDpjejg9VEsSSY04Zp6TzYp/hNnARDZMw usKtzdBYKSgXfRTPzuVl3TPdoqxY1VfQzv6ReYE2dzJUATuceAm/8B6dT2YfhToAFofqOeB0UiwMV 0ycFoZRImyUQ00DdMob3BBXSaWeYj1K0PKp+6dHYRYaYxfjjBYTbae2K0L+CVUeQsC0lGVTSrnfI1 F4Dn8xPA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahkV-009jkQ-UC; Sat, 02 Apr 2022 17:41:20 +0000 Received: from mail-io1-xd4a.google.com ([2607:f8b0:4864:20::d4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahk5-009jZD-7i for linux-arm-kernel@lists.infradead.org; Sat, 02 Apr 2022 17:40:54 +0000 Received: by mail-io1-xd4a.google.com with SMTP id d19-20020a0566022bf300b00645eba5c992so3638954ioy.4 for ; Sat, 02 Apr 2022 10:40:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EDhTuekQtNdm8MWzZ4rgwl9Zuy6sown5y5t0K60oNeM=; b=B8kXE1B9TLeU0fkKkRjLEwnu0j64XSm51isipBSezaI+aW4AtZHnIwm8TGZj11qhjH fZQIml2v0tbIQEfE+Rdi8YKENmjgZx3JLnbDQpunt+Lhb02W6Vt4aMuEdjjmWuAMIpU/ qpWP9KDaobTe+W3uOxEt+HSwNp7VpmQMyoKm+GSu+CZFLH6hjzjaKFfyU2AcF0kukXvm QDYThdKrd3I540B7S51JbBoxsphYIIH++HAYKwcqXN8Ak3NZOiSQbOk768apsr0G4RTv RVSLI2Pn5gd7HrzXW15CZPCcJCKrOG3MkeCDtxEhDwRuVNo6aCWkbdOKa9xeJ/7BVvad afww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EDhTuekQtNdm8MWzZ4rgwl9Zuy6sown5y5t0K60oNeM=; b=rc9KEVHKfoiGe5EAk03km2VnlJXuvjKe3Ahn5xztTAHy5bDj1he+kmBMq+ilS+VUXZ SGlPLRBzUpKct5nSv53JpHVhkp1UCTCd/Wa59kBQIrsIgIhAxRRWcCmqvKa/Kpt0RX2O hZBrPBEggIpVjKJV1Pe/xjD8UIbKb7WtBnKuvEZ3+13n6BFkrZMjSK0CitNd0ojT46So OEcRXPIrJr0sUZJA5KsM8Z27Hbxgrm/h7fAhcM4RrUAgjltst2AqJBRAo7Zk8Dk7rq1R sPAh8pVgAMghy7PSSTHO7nhn8LHsnnAnZJbx0uJBUQAOWe9zDzDuzGSGb/X5xPVO9/KD W13g== X-Gm-Message-State: AOAM533XsoGc/ci9R0nXgOBqzNVKPm0rqdKEEpyLGo5qTFaJh5PpWt/4 DgPuVe6/5Sifvd0kOK9Adkr98gInthA= X-Google-Smtp-Source: ABdhPJwx6B48zoyhRzOI+UWoY4wiun1eWGRgsGjvJOKksNdUx4f7qTvuv/CNaHpOREGjQo9q8Bk2CyZ29n4= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6602:2c8b:b0:649:e67c:9202 with SMTP id i11-20020a0566022c8b00b00649e67c9202mr2148843iow.75.1648921250592; Sat, 02 Apr 2022 10:40:50 -0700 (PDT) Date: Sat, 2 Apr 2022 17:40:42 +0000 In-Reply-To: <20220402174044.2263418-1-oupton@google.com> Message-Id: <20220402174044.2263418-3-oupton@google.com> Mime-Version: 1.0 References: <20220402174044.2263418-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH 2/4] KVM: Only log about debugfs directory collision once From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Paolo Bonzini , Sean Christopherson , Oliver Upton , stable@kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220402_104053_315044_EF56C584 X-CRM114-Status: GOOD ( 12.27 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In all likelihood, a debugfs directory name collision is the result of a userspace bug. If userspace closes the VM fd without releasing all references to said VM then the debugfs directory is never cleaned. Even a ratelimited print statement can fill up dmesg, making it particularly annoying for the person debugging what exactly went wrong. Furthermore, a userspace that wants to be a nuisance could clog up the logs by deliberately holding a VM reference after closing the VM fd. Dial back logging to print at most once, given that userspace is most likely to blame. Leave the statement in place for the small chance that KVM actually got it wrong. Cc: stable@kernel.org Fixes: 85cd39af14f4 ("KVM: Do not leak memory for duplicate debugfs directories") Signed-off-by: Oliver Upton --- virt/kvm/kvm_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 69c318fdff61..38b30bd60f34 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -959,7 +959,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) mutex_lock(&kvm_debugfs_lock); dent = debugfs_lookup(dir_name, kvm_debugfs_dir); if (dent) { - pr_warn_ratelimited("KVM: debugfs: duplicate directory %s\n", dir_name); + pr_warn_once("KVM: debugfs: duplicate directory %s\n", dir_name); dput(dent); mutex_unlock(&kvm_debugfs_lock); return 0; From patchwork Sat Apr 2 17:40:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12799391 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79722C433FE for ; Sat, 2 Apr 2022 17:42:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=bPTplbbg8lWxoF2VU+Cv4MoWXR2/yePJd7l73fSxKGI=; b=o0l3zne2/Kp18q3rqgztI3X+68 POcsVyfi7n3dHOjL64rZexk4gvRmfLHRAIqpre8N6QChrPHYBF9i/sBZZQwPXlHVJGlJPHVzQMVZc bQG652GNuN64XDygWhVn8PVruKwue17Rs8tdO2Y18TWk/n7er70uMvvGPPUi1W8MyEvfHRcLKYiXp 9nHKsmIZPpm2LLCGr/ORyRp3uKzYVfV/SZmfjEmJSa3pNovluwePHHGtCl5uZ8WqrF3OKwejfKMSy QK8VQC1uTlMaxkXpzdDH89MVuWCVDa06YTv8v/aPlrkn6TaQIehxMTFdmbJptyOiM9B4j6WGVp8y+ 2qSlfXOA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahkN-009jiK-IG; Sat, 02 Apr 2022 17:41:11 +0000 Received: from mail-il1-x149.google.com ([2607:f8b0:4864:20::149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahk4-009jZt-Tq for linux-arm-kernel@lists.infradead.org; Sat, 02 Apr 2022 17:40:54 +0000 Received: by mail-il1-x149.google.com with SMTP id x1-20020a056e020f0100b002c98fce9c13so3749769ilj.3 for ; Sat, 02 Apr 2022 10:40:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=LigwUYYC4VhhxtS09KVFjs0sG5saYpBwvbWZHNyydng=; b=NKUIbjH0SKkdci4xkrqxucxb4e10J6YzgfEfrWlPOqo3uYmRUoKeqDouP/VcaTUvyo IBtov8VN/WlMjY1AwlrTF/UlDsXZZhQIup+60SHoXNF+IBnrEq80Jm/xGAPE6FtblLO8 S7FspLtgNxHx0//bc+A+whljvxEEpt6UgvRU4fLs4jyX+w5O1S1NuhIVpg2TKj6f0vB2 vSN32ViMyY0L3YTcACI6hYyldUmccE3BfHkJJoXNav/9Q6QCb7WI7RCLQ1wiPhvL36TZ fM4kSaF2LZOqxDIoXOO1LhhCS324eDYXPaixU4V3ueEMIRLgJBAfcTEA6elYTHEqMW23 f95g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=LigwUYYC4VhhxtS09KVFjs0sG5saYpBwvbWZHNyydng=; b=gzD5GR2W8Y8alS611Dn2XITmdMxRwpVbS61JeYTL4RvOtXhCMYd5cSjqa/JjWxOuz3 chuswxLbONCIxzBNKvb+MkCV537I1UqIr+RqYdFgv2MK7z2U6uOMAmvnC34JaXJO7b1K Ip6g4jJ17Pm8IXc87iu9ien5sScocYjQQwe1678fPJA6YUT6JqEL7VABhQlAts6Yfx2a XokZiRK5bOFmXLxx8r32p6Ofbxij09GKjtkKaXVWXOZSDPN9r7KldKG7WmDdLDGpMItC IfPaSESx4mxCsMo/w5SZW/jvK0BAIuaibSWWf8XYgJtQ3f54WXf4l28VIMF+YN8Z/OkG 7CdA== X-Gm-Message-State: AOAM530AyEyGBBwW2T5CgRH2wnpoIRVht0wG4OxWIrNyn5Srcw1ArDSX x0m2qu0vMW9677O7el0jZZxCtCuPoC0= X-Google-Smtp-Source: ABdhPJzrUyhT2WOyKdGPJLj7xi4+5aFDvMGMKgqgDavuwSTfoHM1dfGkKHZDJs1ky+LEGuWCsDpRGcVe1NE= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6e02:d88:b0:2c8:713f:dcff with SMTP id i8-20020a056e020d8800b002c8713fdcffmr2189656ilj.289.1648921251615; Sat, 02 Apr 2022 10:40:51 -0700 (PDT) Date: Sat, 2 Apr 2022 17:40:43 +0000 In-Reply-To: <20220402174044.2263418-1-oupton@google.com> Message-Id: <20220402174044.2263418-4-oupton@google.com> Mime-Version: 1.0 References: <20220402174044.2263418-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH 3/4] selftests: KVM: Don't leak GIC FD across dirty log test iterations From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Paolo Bonzini , Sean Christopherson , Oliver Upton , Jing Zhang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220402_104053_001238_020C558B X-CRM114-Status: GOOD ( 15.95 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org dirty_log_perf_test instantiates a VGICv3 for the guest (if supported by hardware) to reduce the overhead of guest exits. However, the test does not actually close the GIC fd when cleaning up the VM between test iterations, meaning that the VM is never actually destroyed in the kernel. While this is generally a bad idea, the bug was detected from the kernel spewing about duplicate debugfs entries as subsequent VMs happen to reuse the same FD even though the debugfs directory is still present. Abstract away the notion of setup/cleanup of the GIC FD from the test by creating arch-specific helpers for test setup/cleanup. Close the GIC FD on VM cleanup and do nothing for the other architectures. Fixes: c340f7899af6 ("KVM: selftests: Add vgic initialization for dirty log perf test for ARM") Cc: Jing Zhang Signed-off-by: Oliver Upton Reviewed-by: Jing Zhang --- .../selftests/kvm/dirty_log_perf_test.c | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index c9d9e513ca04..7b47ae4f952e 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -18,11 +18,40 @@ #include "test_util.h" #include "perf_test_util.h" #include "guest_modes.h" + #ifdef __aarch64__ #include "aarch64/vgic.h" #define GICD_BASE_GPA 0x8000000ULL #define GICR_BASE_GPA 0x80A0000ULL + +static int gic_fd; + +static void arch_setup_vm(struct kvm_vm *vm, unsigned int nr_vcpus) +{ + /* + * The test can still run even if hardware does not support GICv3, as it + * is only an optimization to reduce guest exits. + */ + gic_fd = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA); +} + +static void arch_cleanup_vm(struct kvm_vm *vm) +{ + if (gic_fd > 0) + close(gic_fd); +} + +#else /* __aarch64__ */ + +static void arch_setup_vm(struct kvm_vm *vm, unsigned int nr_vcpus) +{ +} + +static void arch_cleanup_vm(struct kvm_vm *vm) +{ +} + #endif /* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ @@ -206,9 +235,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) vm_enable_cap(vm, &cap); } -#ifdef __aarch64__ - vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA); -#endif + arch_setup_vm(vm, nr_vcpus); /* Start the iterations */ iteration = 0; @@ -302,6 +329,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) } free_bitmaps(bitmaps, p->slots); + arch_cleanup_vm(vm); perf_test_destroy_vm(vm); } From patchwork Sat Apr 2 17:40:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12799393 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C5216C433F5 for ; Sat, 2 Apr 2022 17:42:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ssHatV6mExTlE70diwGNZLg7Pkz4zWwPhaO9GcOilro=; b=ns7YNEDDWXhWA+lP/2FVDbB34J TgAs4Jbc26LNdnEz2ykx7Cm163+ExI5mBj6Ii7iCanL8iT0qxMSge96MGKoFS/brI17r8gPbp7H5V sTqW6a1Ob9pIBV5jCYuGeq8LoGVi/RBXHclFQzaztj+lumBWfh7PgrJNYiDk5kzai3WFMwEKhmM3A r39TAak9JI9VqpZmNm/vV7OcgqGCmxPTzb0wmKpUTfWMVJUgFDkF/VHAF8Mgr96VBA8tO0AsTwdBD kib5cPKp5jBvmc0W8MxhgZIpPtUJthH7s9KisiWpKRauo+ezHjzPpzak2BZXiRy/aYnBL6pf6h7NA 6FXTAylA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahke-009jmF-JH; Sat, 02 Apr 2022 17:41:28 +0000 Received: from mail-io1-xd4a.google.com ([2607:f8b0:4864:20::d4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nahk5-009ja0-Qi for linux-arm-kernel@lists.infradead.org; Sat, 02 Apr 2022 17:40:55 +0000 Received: by mail-io1-xd4a.google.com with SMTP id g16-20020a05660226d000b00638d8e1828bso3613713ioo.13 for ; Sat, 02 Apr 2022 10:40:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uRmXUJJ9UlZ6s6G2CwiIknxpFh1oZyxCOBIJvLxa8r4=; b=pRdr/FNL4DJ4+FDZfYXkJBtQDCsAq7uj4Emtow3IUaukx62QJi2YKu7pn/kogEODph Y+Ut0ZuenWviBdtaf3uK2ywut5KwfSCOLvuj4E4oRTQycEzaxb0DzPh8RMPK+LNVr1U9 BeWnTU5Vzf7eMYH9M9yl+lnM/35IO89F+KiuVVUfxbFmL7DCH09Jixfk7rVjhOmUP+j6 a/towLy19ro+gsEvXWFYQ/JDk+Ch7sQoSUNbTLyRyCqqiA9WSF46VI49YlqOoWC3xzcx XazJkCyLa9KKCF8rswY5B8VWHE3+GnUCNK6RnnP18kj5JH5MFdP1r61Sm0hEYI+A6ayv 064w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=uRmXUJJ9UlZ6s6G2CwiIknxpFh1oZyxCOBIJvLxa8r4=; b=eVXU1f9PygqVgByzSXZEBwQ0S5cSuZ4bqXQ7xZB2WKvmeF35txCoEu4E88zPLQM09h VllxrWOG5FdmAFtr66jFQGmCZlyJleSggY2G/A/FPbO130WUO7upozduebsY2d/sF6uk YssoLuVYVdzv4hXDr06C1kysFk/Ida9W1TDAJyF+I6Tm/7bt1PfF2iV9iNm7LESFA8Pg ouXFW0wTywAL0jW5JDSITf3UpRUiBdFKMw31KVxCqlvHns/FZ/yD3xDsSxKrYqpViDYN vsPkFtLD91t0nQFeYxZ2gujTSGZ2CrNSHbpf4kVHAyusgyHcgUCZ/cH4HVwegX2BnKWa OcAA== X-Gm-Message-State: AOAM531n1aj6zd2BrFe12UdgrC9psfHTzmEwg8na2op+z2kX2Co+0u99 ic9/FvAoL/DYh1/7DAYDUwntOGDdcv0= X-Google-Smtp-Source: ABdhPJxfhOuV+H0OWvhKyqVH+yGUG1Gyfq61aVtKlvvdZjUHlwISHe0FlEzqyL1csiERacH6j/kPsSDcnBY= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a6b:8d8b:0:b0:645:eb9e:6765 with SMTP id p133-20020a6b8d8b000000b00645eb9e6765mr2056322iod.215.1648921252570; Sat, 02 Apr 2022 10:40:52 -0700 (PDT) Date: Sat, 2 Apr 2022 17:40:44 +0000 In-Reply-To: <20220402174044.2263418-1-oupton@google.com> Message-Id: <20220402174044.2263418-5-oupton@google.com> Mime-Version: 1.0 References: <20220402174044.2263418-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.1094.g7c7d902a7c-goog Subject: [PATCH 4/4] selftests: KVM: Free the GIC FD when cleaning up in arch_timer From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Paolo Bonzini , Sean Christopherson , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220402_104053_922963_E2E2399F X-CRM114-Status: GOOD ( 11.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In order to correctly destroy a VM, all references to the VM must be freed. The arch_timer selftest creates a VGIC for the guest, which itself holds a reference to the VM. Close the GIC FD when cleaning up a VM. Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/aarch64/arch_timer.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/aarch64/arch_timer.c b/tools/testing/selftests/kvm/aarch64/arch_timer.c index b08d30bf71c5..3b940a101bc0 100644 --- a/tools/testing/selftests/kvm/aarch64/arch_timer.c +++ b/tools/testing/selftests/kvm/aarch64/arch_timer.c @@ -362,11 +362,12 @@ static void test_init_timer_irq(struct kvm_vm *vm) pr_debug("ptimer_irq: %d; vtimer_irq: %d\n", ptimer_irq, vtimer_irq); } +static int gic_fd; + static struct kvm_vm *test_vm_create(void) { struct kvm_vm *vm; unsigned int i; - int ret; int nr_vcpus = test_args.nr_vcpus; vm = vm_create_default_with_vcpus(nr_vcpus, 0, 0, guest_code, NULL); @@ -383,8 +384,8 @@ static struct kvm_vm *test_vm_create(void) ucall_init(vm, NULL); test_init_timer_irq(vm); - ret = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA); - if (ret < 0) { + gic_fd = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA); + if (gic_fd < 0) { print_skip("Failed to create vgic-v3"); exit(KSFT_SKIP); } @@ -395,6 +396,12 @@ static struct kvm_vm *test_vm_create(void) return vm; } +static void test_vm_cleanup(struct kvm_vm *vm) +{ + close(gic_fd); + kvm_vm_free(vm); +} + static void test_print_help(char *name) { pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n", @@ -478,7 +485,7 @@ int main(int argc, char *argv[]) vm = test_vm_create(); test_run(vm); - kvm_vm_free(vm); + test_vm_cleanup(vm); return 0; }