From patchwork Fri Nov 11 00:22:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 18351 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp454047wru; Thu, 10 Nov 2022 16:28:04 -0800 (PST) X-Google-Smtp-Source: AMsMyM6q4L2xW5IC9vNhTAhDiUolB6GDlAW8sM6jU21EWOLNKj6YxT7edeor0CUagJT0gilaXdgZ X-Received: by 2002:a17:90b:2392:b0:20b:a5e:5c40 with SMTP id mr18-20020a17090b239200b0020b0a5e5c40mr2756207pjb.185.1668126483874; Thu, 10 Nov 2022 16:28:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668126483; cv=none; d=google.com; s=arc-20160816; b=dh3c+SDxUjdUTXTrfZ3juvzESJl2kQ6oF8qk6fmhzUKQBy5wJ+w9e6wG+huIJg9ApE nj6bDey4VrSiAH4hCo7G4qnlKRKJqrjpwxKeikhkWiyf3NY+2leSZmPAXB6fF58DRYwa ETIUDqcpLF6xW5TIacFXacQS5LETp08SioE6V1N02ATBhx5tuveDptOJf47GEObGNaMC fx0TVpvanzTNNilHADmCGeaM/WeG9MSnIgI88l2396Ai0+WuRhFMwplrQ0kdHXcBG2YQ SYNiuz8S7UoKmuo8AHdQGYRc5waBh8OZBOajWZWQcQui/IUlwioaPsGHaUu0ROrV0yZi SMDw== 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=/Qtct265AUs3mCRq4YXmCFT3mbV9fW5IFtDc5J2z2zs=; b=Ko8OH7GUI2w4yy7ARrrRZJOCJyyzZiNn3P2A+arvZIRoIFeerc7cwLrszLD3sosv3I MMTp7O0uwLD5+rk/qt0b43GHfjFno0hkv3AVsTfQoNcne4isTopOgf5zxVw7hTokqOxY 2gxPej/UjAP89WFGPNBhx1SOFFToLBRHaQKB7cLQg1xCUOsI7Njwkzrabk6r3jRNYTPP 3O5k5PY018AX/1mCvfqX7dvzWeFxLWmdEvCmYQ0PWl3QXikoZcWSR4umSxuIgecidBEZ UHNxe5DdL2qioe/B5ldMEArxCsNFphNJ5eH5p4D9KHUnMfQV8s+BAjbOANGfma9bzjCn /5qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FBR88osY; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w186-20020a6382c3000000b0046f4d395e6dsi665054pgd.573.2022.11.10.16.27.46; Thu, 10 Nov 2022 16:28:03 -0800 (PST) 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=20210112 header.b=FBR88osY; 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 S231260AbiKKAWf (ORCPT + 99 others); Thu, 10 Nov 2022 19:22:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230055AbiKKAWb (ORCPT ); Thu, 10 Nov 2022 19:22:31 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C04E1F61E for ; Thu, 10 Nov 2022 16:22:30 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id y15-20020aa7804f000000b0056c9b4a58d3so1815404pfm.18 for ; Thu, 10 Nov 2022 16:22:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=/Qtct265AUs3mCRq4YXmCFT3mbV9fW5IFtDc5J2z2zs=; b=FBR88osY8fIlZLbRznAOCa2Xf7BHDJaHVBD3PLmB9KlLJqRyRDDLI3zyzsZcD0Rn5m 5oCwjogt2zecTA7RiD9yeOlvc55mEqbD8BgDuCuDPhgkuTUGCTX+PZfFw8d63rpWQhSP 4YkKHAyfDPUatmW5/skfXM0hFEJa+6kp7KfEuEWXg7E4DNB1ZzIThCPt1PM0BzD8Xdhq A/EPbIV3ybXi65UzJTKQ8T8FJMvIkmMoPVo3GTuq4UapPDYeRJRzaqoquBxRN2AYC9KA A/DU8ldikEieGp5BDIr3DGthufNFJEG9FQ/iYXTHPCiQh1sE8mL3YkDDK1B4aUDeO2MN Z8Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=/Qtct265AUs3mCRq4YXmCFT3mbV9fW5IFtDc5J2z2zs=; b=Q2GPEKqRyYfpm0CtV7yZGcd9IdtIepRJcLdbP83ANdJzwWXPEHV6MaobOQxqp/fVkf W+Kh3gFW16jIca69r94Et6po/u354iSYluEUDrUb/bwM6JmGkiS6l2b5ETu5ePNL5vvJ UqONifhGym6HGbEdWdmclA/zqCVQWsrXle3tlPxrRY49ZVabOIJmVJaUTBwyiEMceNqn MvG8F9A2uQf6YxDRapudwtdXECP/IkJEpDJf6sNmZYbX2f08/9kqCQ3Jv/xnOLFQ9ZBn g18EC20U5n+zE7iRtwlTnF7ohFVTIbHCCsqCAfe1PcTD+u5xUMe4o/EzVq/cMl6yBds7 mzxQ== X-Gm-Message-State: ACrzQf2N/tk4Q9QGjRfUXYyuTqZYEqrR4MrevgQjq3ar7KQsVTc1behQ l9yvh+WMk6IW6irFPmeBSWJ9S6COd1c= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:703:b0:56c:e37d:ec12 with SMTP id 3-20020a056a00070300b0056ce37dec12mr4208245pfl.80.1668126149761; Thu, 10 Nov 2022 16:22:29 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 11 Nov 2022 00:22:24 +0000 In-Reply-To: <20221111002225.2418386-1-seanjc@google.com> Mime-Version: 1.0 References: <20221111002225.2418386-1-seanjc@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221111002225.2418386-2-seanjc@google.com> Subject: [PATCH 1/2] drm/i915/gvt: Get reference to KVM iff attachment to VM is successful From: Sean Christopherson To: Zhenyu Wang , Zhi Wang Cc: intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, Sean Christopherson 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,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749157395968871766?= X-GMAIL-MSGID: =?utf-8?q?1749157395968871766?= Get a reference to KVM if and only if a vGPU is successfully attached to the VM to avoid leaking a reference if there's no available vGPU. On open_device() failure, vfio_device_open() doesn't invoke close_device(). Fixes: 421cfe6596f6 ("vfio: remove VFIO_GROUP_NOTIFY_SET_KVM") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Kevin Tian --- drivers/gpu/drm/i915/gvt/kvmgt.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 7a45e5360caf..e67d5267fde0 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -664,8 +664,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev) return -ESRCH; } - kvm_get_kvm(vgpu->vfio_device.kvm); - if (__kvmgt_vgpu_exist(vgpu)) return -EEXIST; @@ -676,6 +674,7 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev) vgpu->track_node.track_write = kvmgt_page_track_write; vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot; + kvm_get_kvm(vgpu->vfio_device.kvm); kvm_page_track_register_notifier(vgpu->vfio_device.kvm, &vgpu->track_node); From patchwork Fri Nov 11 00:22:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 18352 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp455283wru; Thu, 10 Nov 2022 16:31:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf5rtayfHEK+IsyC7Ir4TllK7b+1NglojdqXVBY28RTimsai9Ib8EVYZcRTQ24qcmCMUqmeg X-Received: by 2002:aa7:9011:0:b0:561:c295:7568 with SMTP id m17-20020aa79011000000b00561c2957568mr87165pfo.35.1668126715420; Thu, 10 Nov 2022 16:31:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668126715; cv=none; d=google.com; s=arc-20160816; b=tLGyKU2oQ9QAG80QU+xSJembPIQiSclXd7DsCrAFYQDwBt1lTJHCJM+AHtNyy/t7iK Zm547E0rJJHihqB9VBj4Pfjcwf0zjwjja0ANSN8VfvQjFZa1Lxj5kiij0t0+rOOrx6VI 06psTKko9Ak3Wm/0+a1waLarnd/DjVtcRFrXJb5hl7CvRBrvnp2zoEybhNIV9OdKGB5a IFxdoWsKZDcKb8SbEneKpFe/X+6jr4kHKzJMJC+mTnOzl38UGDyRzcfWURZj/yr/Sw0L 7y8GLK4Xe6FG5dmKoK4IUv4kMLZO5DXJfn+ZP//n/1OZ0knRVrPkI6BjKuxWpnApDrfb wMXQ== 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=n36J7s+0ilM0Tsn5slAdsXyaRWJfxWCAeT/6FWqeTp8=; b=DMaB05OOB7wlai/yUKS/RV0Fv6LDzidB6b4e/SGSZC5OGiZ/OzhAdnjAxX9vjb3k1P h5TfJtUc3+4R/PFW7uwkgXUpQlSbmLQHNfcOdj006y6FYJhyCI/TZ8Iqch81iwG9msu6 BcJ9DmW+ykC+S+TDFgl4KNVslwB/+QtyHnhJz0HTv9aL15jtq/IK5/ApBfXVHhy8Pmg5 2Pig3yYaPT78h3NHirkRSGZ3LfR40uO9Bp9ghd2YTRwWpYQl7wN74cYC7h3ss6JbTUG5 HB5K1BiE9xE0b5KlieQb+UKUAdza+vrAzYGMRaiGK+XrHf0tIguj9frAeeofxAy76P++ JgmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=TZiKdFTb; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 17-20020a170902ee5100b00178b88d40ebsi805203plo.51.2022.11.10.16.31.34; Thu, 10 Nov 2022 16:31:55 -0800 (PST) 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=20210112 header.b=TZiKdFTb; 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 S231688AbiKKAWj (ORCPT + 99 others); Thu, 10 Nov 2022 19:22:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231207AbiKKAWc (ORCPT ); Thu, 10 Nov 2022 19:22:32 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01B6543872 for ; Thu, 10 Nov 2022 16:22:31 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id o7-20020a170902d4c700b001868cdac9adso2403983plg.13 for ; Thu, 10 Nov 2022 16:22:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; 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=n36J7s+0ilM0Tsn5slAdsXyaRWJfxWCAeT/6FWqeTp8=; b=TZiKdFTbv7ULH977/XXkoaKwCBWIUXcZgjj3kk67qk6G/7NTnWl7f9ErIfa3sgs6Iu PIE2j9lVQnRevVhSmgy5ghpMaowZSmo/JvV26NB/u5XUxIGVX2T3kGhGEB+GX40/BH+e dMm5i34zN0Ep9bgjqiptCmyqKJTPz98+Gc5Erv/2Hk0eC6sx/JrEjrvIb4L//JadZb+D yyrZCQmeoRcq14GM/aKC8IJAyy66Zz5B57Ex00OXo4YACUPRLXDVs9TboavE9uxTi60O H5HIhz1q4T4MEoC0qbI3gIAqHaqRVBYY+wU9Sv6jmDDDGplg8ezaO+Oydl1V1nV+l4ZE GG4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=n36J7s+0ilM0Tsn5slAdsXyaRWJfxWCAeT/6FWqeTp8=; b=voRodq6sa9ttDSJRgPNpQSFapiUVq5vKr4WaGL1Tz95QA2RSGxIIf9eHjmZY7PlzbM 5hpKJ82Kv2d2v/LUixDSZn1oG2C2dxGufMyc67m8hHWuVZlUUfa6dGQLkaOMEmlZ8XxE 8lahKnCF1XIZZZ3b81O/t/XyS3Rqm6co9OxWm5jXnx8pHszY/Iz8JvaDcXtwJcVjcbI8 o3TOJEGHFOP258wbAZ1qQQQ9wyw44tav0aUt0mEYVYBixuj5E9NexuFLgX+sdMYf06lp 3aPx+CfqCfg3gFhzKG5hhsaAMuEehd9yh0HvGvKjkNHkSdDRUw++LCH0ALrBltaXPcnj b70Q== X-Gm-Message-State: ANoB5pmWYz6OvyRyUnS94hPBbkej7FhOaZdfsNcs48ZENNea9+yLeiHa KEznS43UH6qNjZBlZXWG0iFlZwpgT1U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:dac4:b0:186:5f54:18cf with SMTP id q4-20020a170902dac400b001865f5418cfmr4884plx.141.1668126151578; Thu, 10 Nov 2022 16:22:31 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 11 Nov 2022 00:22:25 +0000 In-Reply-To: <20221111002225.2418386-1-seanjc@google.com> Mime-Version: 1.0 References: <20221111002225.2418386-1-seanjc@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221111002225.2418386-3-seanjc@google.com> Subject: [PATCH 2/2] drm/i915/gvt: Unconditionally put reference to KVM when detaching vGPU From: Sean Christopherson To: Zhenyu Wang , Zhi Wang Cc: intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, Sean Christopherson 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,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749157639201959375?= X-GMAIL-MSGID: =?utf-8?q?1749157639201959375?= Always put the KVM reference when closing a vCPU device, as intel_vgpu_open_device() succeeds if and only if the KVM pointer is valid and a reference to KVM is acquired. And if that doesn't hold true, the call to kvm_page_track_unregister_notifier() a few lines earlier is doomed. Signed-off-by: Sean Christopherson Reviewed-by: Kevin Tian --- drivers/gpu/drm/i915/gvt/kvmgt.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index e67d5267fde0..714221f9a131 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -714,15 +714,14 @@ static void intel_vgpu_close_device(struct vfio_device *vfio_dev) kvm_page_track_unregister_notifier(vgpu->vfio_device.kvm, &vgpu->track_node); + kvm_put_kvm(vgpu->vfio_device.kvm); + kvmgt_protect_table_destroy(vgpu); gvt_cache_destroy(vgpu); intel_vgpu_release_msi_eventfd_ctx(vgpu); vgpu->attached = false; - - if (vgpu->vfio_device.kvm) - kvm_put_kvm(vgpu->vfio_device.kvm); } static u64 intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)