Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp774717pxb; Tue, 5 Apr 2022 22:47:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoxRTY2+eRN6+Bl2oj0OiDG+S1kjeO8qdSdCKYV58YjNzydL59NASEVqoSH8bEMwoTEbu9 X-Received: by 2002:a17:90b:17c3:b0:1c6:b0ff:abf with SMTP id me3-20020a17090b17c300b001c6b0ff0abfmr8051127pjb.24.1649224073115; Tue, 05 Apr 2022 22:47:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649224073; cv=none; d=google.com; s=arc-20160816; b=j24CZHCkG5cdZvYoz9Ldf88q1tou+ZfjcH6gsMdUvlTBWePPfCAnJvO/+vGhKCn8VA NAcdzgwApWyCEf5pUG7dDpPWX63pDseuaKtywAb8KhqAp1xaLlnHi+B52vBsRMJ4PpyD h+/KvSPmrNFKxNHcJ9Q8Or/n1a6FCBASoITIlIZrDrBeav86sMhpA3YeGxA6U6spfO64 xOXbg3Hf7dQzpdbkTUhDfAXdQzf+pFlGBrU/aMUZFqLI9XLLJNt46ioml60r1dPIb7RS nI+28diBrVaafwhGT4Pc4ncSx723mUn0fLf+s16LplJQrUCTS2+txtRn6Ia4dnVxk4aK pGPw== 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=YwDCzw2M6FF5F8ngbyVCupIRRQFzrUsNi0YwPIm94B0=; b=fRF17SY62qDRL7x92Fmnq7gO1DLh+RxHIZ+O8umnKoXRXYTSBcpjL1MRboO3V7F+g8 TdUovi4+vQVCFoXL9w7TMi2vehZiMdusyaToPAd+p3RB2N+TAL6/1syfC0+xks46VxCd fLzCz03gJZcjBhgiGCGsd6siy5EhTAT0UhrS3F1ZfNGUZc+EgRpxXEjt1QyrjDfiFf+D 45nhVU0M+Ud0c72Sv9FBMDpunegPBdErWSW7JH+AJ2Nt6jnzhwlyr23jRb1zqFPJ2vPX EII7z1TdJQR/m+SP0Bk4SRMhWKUKIb0Dq5R6ScKAhrfcOU9vEvvLH8ix9mc8uIImbUXt qy1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=M3u7FiQi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h184-20020a6253c1000000b004fa3a8e006dsi14419606pfb.292.2022.04.05.22.47.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 22:47:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=M3u7FiQi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EAE1748EC5E; Tue, 5 Apr 2022 22:13:12 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1574171AbiDEWyv (ORCPT + 99 others); Tue, 5 Apr 2022 18:54:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353922AbiDEKJs (ORCPT ); Tue, 5 Apr 2022 06:09:48 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 693BBC337B; Tue, 5 Apr 2022 02:55:45 -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 sin.source.kernel.org (Postfix) with ESMTPS id D7798CE1BE5; Tue, 5 Apr 2022 09:55:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED766C385A2; Tue, 5 Apr 2022 09:55:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152542; bh=uXiXwt0Ehuv2SehoZcTyEG2r8lb4yLQg/hmZf2p4mKg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M3u7FiQi2JEjD5XbWSuk+sa9XXqdf6o1V7PIzUNZeWciD0xJYvjecH+92NYAFg7K7 jHvE3S+ERsfftUgNlvO0hF4RdxFF3h8l2DJemql9cYAHToXam6WPhcubg9KwWkc1DX 2c16NIBRnyiM7AbjKE0eRZdCXryAm1mVCGPV10W4= 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 5.15 813/913] KVM: Prevent module exit until all VMs are freed Date: Tue, 5 Apr 2022 09:31:15 +0200 Message-Id: <20220405070404.201723088@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 @@ -117,6 +117,8 @@ EXPORT_SYMBOL_GPL(kvm_debugfs_dir); 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 @@ -1099,6 +1101,16 @@ static struct kvm *kvm_create_vm(unsigne preempt_notifier_inc(); kvm_init_pm_notifier(kvm); + /* + * 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: @@ -1188,6 +1200,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)