Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp50321pxb; Thu, 21 Apr 2022 16:44:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxr+vXpWsEoyxbiTDmbDpjYdxup8DwW80Rxshmx+XJKY2OQyPFboPG/qHOf2z+AUIFV3F69 X-Received: by 2002:a17:90a:d3d1:b0:1bb:fdc5:182 with SMTP id d17-20020a17090ad3d100b001bbfdc50182mr13132992pjw.206.1650584646008; Thu, 21 Apr 2022 16:44:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650584646; cv=none; d=google.com; s=arc-20160816; b=Jj4tEvLaVQcSis8ovNW5TZu/VmYwiuKgOjwsGmg18qZneybInUMomD2wlESMuagiZY cANUczFmwe76mLFGTsxlBebVp23HcQcr8aHCxpI93G/Kri5neZGGRGhpy1NOL3dPgqFp mGuMBa2Kh3LwAMZ/fSa94DaqnZwu/X3f1CaJDAbtulD4RJq6L6SL91t7iF+qUizBbZV5 RBw5R0P34OdZCHxzTEa3NurfptnzIno+bLPDDni9We/a5aWMLQSQsH/1VMVZ4dAvtTOm pugfteBNEgnODRYBGmwPvZd+FMNHXXbujcG9Mg6pjRHsY5fXPPZlR57C0wqxV5dDgK3h 2u6w== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Ix7WNToZskVb+WBrHx5KWoQurfZDgUPtNlpfuVx/Ygw=; b=D0ICPJ/HFVnkRJebXaGW/9ZeoBam2pXOvgy97Xwsc0c2j4FRvGeegBDB+HhZoR0Zwd JAbCtihW/ERBtdy6m1hfVEegQXzbI4Me24ZLJCN3+f9p4u2dnwyCp7CNJhOgICmbFW8q DCD0N+lvIrQED1aLZdPdX9j8V775w94ot3BYYVoDViwbulTP+a7QBTVotyn9cNmcQ7a/ IRpxEiNx4cUaaq2q4dyhcwyXerOkik/uMTpm1w5YRH1azw1dghhiAnnwgIWk81SNDXVg c5OEnUyYSquL07cgJsTtcmimNz2aDoIuO87sSflFerHUgZPwYxE2oAzpateWg++hqbJE c6+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=v4Zau8MR; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j71-20020a63804a000000b003aa7ba8c4dasi2748893pgd.392.2022.04.21.16.43.50; Thu, 21 Apr 2022 16:44:05 -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=@linuxfoundation.org header.s=korg header.b=v4Zau8MR; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240477AbiDRNz7 (ORCPT + 99 others); Mon, 18 Apr 2022 09:55:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244864AbiDRNa7 (ORCPT ); Mon, 18 Apr 2022 09:30:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC2A91EED6; Mon, 18 Apr 2022 05:56:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8B44060FD9; Mon, 18 Apr 2022 12:56:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7EC6FC385A7; Mon, 18 Apr 2022 12:56:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286592; bh=SGp5aKNkWF7bwgMl1K4wWeG8G9L0uda+KQ4PEeM4IRU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v4Zau8MR497HC33Rkq9n+hC9O39rD4zR0lBmc1A9OjvyXJD6vJ43Rm+b7x73jg2C/ /y7c2jtG8MPrhBvTvZ/IoFlnplQwiTaXegrdDPOXjEzfwxSWaAj26dxXHvRuEz0alE hPIuqMSFI87h50bRE3hNaLZCLyOAZPzV7a60GCoc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ben Gardon , David Matlack , Paolo Bonzini Subject: [PATCH 4.14 184/284] KVM: Prevent module exit until all VMs are freed Date: Mon, 18 Apr 2022 14:12:45 +0200 Message-Id: <20220418121216.970237804@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: David Matlack commit 5f6de5cbebee925a612856fce6f9182bb3eee0db upstream. Tie the lifetime the KVM module to the lifetime of each VM via kvm.users_count. This way anything that grabs a reference to the VM via kvm_get_kvm() cannot accidentally outlive the KVM module. Prior to this commit, the lifetime of the KVM module was tied to the lifetime of /dev/kvm file descriptors, VM file descriptors, and vCPU file descriptors by their respective file_operations "owner" field. This approach is insufficient because references grabbed via kvm_get_kvm() do not prevent closing any of the aforementioned file descriptors. This fixes a long standing theoretical bug in KVM that at least affects async page faults. kvm_setup_async_pf() grabs a reference via kvm_get_kvm(), and drops it in an asynchronous work callback. Nothing prevents the VM file descriptor from being closed and the KVM module from being unloaded before this callback runs. Fixes: af585b921e5d ("KVM: Halt vcpu if page it tries to access is swapped out") Fixes: 3d3aab1b973b ("KVM: set owner of cpu and vm file operations") Cc: stable@vger.kernel.org Suggested-by: Ben Gardon [ Based on a patch from Ben implemented for Google's kernel. ] Signed-off-by: David Matlack Message-Id: <20220303183328.1499189-2-dmatlack@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- virt/kvm/kvm_main.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -112,6 +112,8 @@ EXPORT_SYMBOL_GPL(kvm_debugfs_dir); static int kvm_debugfs_num_entries; static const struct file_operations *stat_fops_per_vm[]; +static struct file_operations kvm_chardev_ops; + static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, unsigned long arg); #ifdef CONFIG_KVM_COMPAT @@ -716,6 +718,16 @@ static struct kvm *kvm_create_vm(unsigne preempt_notifier_inc(); + /* + * When the fd passed to this ioctl() is opened it pins the module, + * but try_module_get() also prevents getting a reference if the module + * is in MODULE_STATE_GOING (e.g. if someone ran "rmmod --wait"). + */ + if (!try_module_get(kvm_chardev_ops.owner)) { + r = -ENODEV; + goto out_err; + } + return kvm; out_err: @@ -792,6 +804,7 @@ static void kvm_destroy_vm(struct kvm *k preempt_notifier_dec(); hardware_disable_all(); mmdrop(mm); + module_put(kvm_chardev_ops.owner); } void kvm_get_kvm(struct kvm *kvm)