From nobody Tue Jun 23 16:15:16 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 8FC98C433F5 for ; Wed, 2 Mar 2022 17:43:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244015AbiCBRoL (ORCPT ); Wed, 2 Mar 2022 12:44:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232288AbiCBRoJ (ORCPT ); Wed, 2 Mar 2022 12:44:09 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E3ADDB1080 for ; Wed, 2 Mar 2022 09:43:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646243005; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=vcyMploRmI5e6q7GMWaNDxko2AIloAHvg72NerFccOw=; b=Kl+onUPZO7TLk+SN/6fRXQVJ6wFKUrvC9baY6FralAvasEZNWjWUoCFcSqooz+ad1/Fav9 DnUNTSD69dNgVCA3XOX0tQhJ3Hry2jlvtOWkIYV8ZEGeaR83n40PqhFcdaj4LfFtG6i63H ok7WeZxQ4qIfaZe06uaU4+WLxyRzvSM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-510-2EbYghR7PDOg3nFu_-wxoQ-1; Wed, 02 Mar 2022 12:43:23 -0500 X-MC-Unique: 2EbYghR7PDOg3nFu_-wxoQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B82621854E21; Wed, 2 Mar 2022 17:43:22 +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 356ED34664; Wed, 2 Mar 2022 17:43:22 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: dmatlack@google.com, seanjc@google.com Subject: [PATCH] KVM: allow struct kvm to outlive the file descriptors Date: Wed, 2 Mar 2022 12:43:21 -0500 Message-Id: <20220302174321.326189-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Right now, the kvm module is kept alive by VFS via fops_get/fops_put, but t= here may be cases in which a kvm_get_kvm's matching kvm_put_kvm happens after the file descriptor is closed. One case that will be introduced soon is when work is delegated to the system work queue; the worker might be a bit late and the file descriptor can be closed in the meantime. Ensure that the module has not gone away by tying a module reference explicitly to the lifetime of the struct kvm. Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 64eb99444688..e3f37fc2ebf1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1131,6 +1131,9 @@ static struct kvm *kvm_create_vm(unsigned long type) preempt_notifier_inc(); kvm_init_pm_notifier(kvm); =20 + /* This is safe, since we have a reference from open(). */ + __module_get(THIS_MODULE); + return kvm; =20 out_err: @@ -1220,6 +1223,7 @@ static void kvm_destroy_vm(struct kvm *kvm) preempt_notifier_dec(); hardware_disable_all(); mmdrop(mm); + module_put(THIS_MODULE); } =20 void kvm_get_kvm(struct kvm *kvm) --=20 2.31.1