From patchwork Sat Mar 11 00:22:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13170533 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 8401BC6FD1F for ; Sat, 11 Mar 2023 00:23:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0DFB610EA38; Sat, 11 Mar 2023 00:23:59 +0000 (UTC) Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by gabe.freedesktop.org (Postfix) with ESMTPS id D30BC10EA66 for ; Sat, 11 Mar 2023 00:23:55 +0000 (UTC) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5411f21f849so10413517b3.16 for ; Fri, 10 Mar 2023 16:23:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678494235; 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=XkHJWZn0+/j+Dc2+hutiMpToPlFiQhTuav6vYTVTolU=; b=gdrr7PItFLFdwPfpHqXJmv50annnGEpbjPSGdch9nZXKd4H7535ILNZLpgHxGyanPv 4PfZkQ2LA6uDbbO+FsDi0qCrKD6YSwrPqe+e2TT6K8ATaMViQQZ01b8ngB4KtiYI5sOz VXmqdM7c3DZtX11oiLbCJpM8nbKraZjMSrPE35acuOv4s1T/tYJHUT8HHRBpCRFbkptv qpexrRNaTTbCcJHqF/fEHI/B8MfZcPe3qALNen7BnIwR4oYbEpMPKzNDyYZmh5N9kppH W6BbFX7SJ23fqsQcJZc6LcQV5sLax8oa9ybavP2sV/6SXLdcy62FLgcAa++18kxWj98y 8bhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678494235; 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=XkHJWZn0+/j+Dc2+hutiMpToPlFiQhTuav6vYTVTolU=; b=opaAoaqnVjS75rmfxcWnInPYnxKj2ApC3npyvtzmZpeD0u4WfIXKLrXGym7eDJMDcZ 6+HcIOfZ+R3WeggwDPfDBp2KPZsFs2dC8Zw9SOW/U5zGiNz9dbEpfK2XY2x4/Vzbo99d qbLlgjqQX4aOI50imnLOk+gwjmrG8VYgS/fD9d5IYKspRxGrKR4aIZgBBEUwQHAZwmN3 zR5MxZdTHK+RPBBf19u28VmyZtLNQmqVrJvFmjWAUHU/bzktQMtohB1BG/GDnxemAdRj q6m8eI5bA/P9aMNZuxPo3yYfOg88UPMk5qUgL4spMXXw+srpxBjHY27xSqGiuoFQrAqj HwgA== X-Gm-Message-State: AO0yUKXIehGvIALhVgIvaMRHJIKRW0hkqZWR0jYMqJHdnq+sxpRkpJna jyVGfxvU8EgVg7byK3Y2c3RRjSBrxtQ= X-Google-Smtp-Source: AK7set+6NoHsiNHsMvupMYZrAlbJ87RkPYjZTlRkM1NQ5sVcTEuhofQMf2bVYio2oEjx6ARu7FfP8A3iuIY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:8d89:0:b0:b33:531b:3dd4 with SMTP id o9-20020a258d89000000b00b33531b3dd4mr1051323ybl.1.1678494235094; Fri, 10 Mar 2023 16:23:55 -0800 (PST) Date: Fri, 10 Mar 2023 16:22:57 -0800 In-Reply-To: <20230311002258.852397-1-seanjc@google.com> Mime-Version: 1.0 References: <20230311002258.852397-1-seanjc@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Message-ID: <20230311002258.852397-27-seanjc@google.com> From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Zhenyu Wang , Zhi Wang Subject: [Intel-gfx] [PATCH v2 26/27] KVM: x86/mmu: Handle KVM bookkeeping in page-track APIs, not callers X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Cc: Yan Zhao , kvm@vger.kernel.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, Ben Gardon , intel-gvt-dev@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Get/put references to KVM when a page-track notifier is (un)registered instead of relying on the caller to do so. Forcing the caller to do the bookkeeping is unnecessary and adds one more thing for users to get wrong, e.g. see commit 9ed1fdee9ee3 ("drm/i915/gvt: Get reference to KVM iff attachment to VM is successful"). Signed-off-by: Sean Christopherson Reviewed-by: Yan Zhao --- arch/x86/include/asm/kvm_page_track.h | 10 ++++------ arch/x86/kvm/mmu/page_track.c | 18 ++++++++++++------ drivers/gpu/drm/i915/gvt/kvmgt.c | 17 +++++++---------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/arch/x86/include/asm/kvm_page_track.h b/arch/x86/include/asm/kvm_page_track.h index 415537ce45b4..66a0d7c34311 100644 --- a/arch/x86/include/asm/kvm_page_track.h +++ b/arch/x86/include/asm/kvm_page_track.h @@ -47,12 +47,10 @@ struct kvm_page_track_notifier_node { enum pg_level kvm_page_track_max_mapping_level(struct kvm *kvm, gfn_t gfn, enum pg_level max_level); -void -kvm_page_track_register_notifier(struct kvm *kvm, - struct kvm_page_track_notifier_node *n); -void -kvm_page_track_unregister_notifier(struct kvm *kvm, - struct kvm_page_track_notifier_node *n); +int kvm_page_track_register_notifier(struct kvm *kvm, + struct kvm_page_track_notifier_node *n); +void kvm_page_track_unregister_notifier(struct kvm *kvm, + struct kvm_page_track_notifier_node *n); int kvm_write_track_add_gfn(struct kvm *kvm, gfn_t gfn); int kvm_write_track_remove_gfn(struct kvm *kvm, gfn_t gfn); diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c index 69b6431b394b..6ca644d3c926 100644 --- a/arch/x86/kvm/mmu/page_track.c +++ b/arch/x86/kvm/mmu/page_track.c @@ -157,17 +157,22 @@ int kvm_page_track_init(struct kvm *kvm) * register the notifier so that event interception for the tracked guest * pages can be received. */ -void -kvm_page_track_register_notifier(struct kvm *kvm, - struct kvm_page_track_notifier_node *n) +int kvm_page_track_register_notifier(struct kvm *kvm, + struct kvm_page_track_notifier_node *n) { struct kvm_page_track_notifier_head *head; + if (!kvm || kvm->mm != current->mm) + return -ESRCH; + + kvm_get_kvm(kvm); + head = &kvm->arch.track_notifier_head; write_lock(&kvm->mmu_lock); hlist_add_head_rcu(&n->node, &head->track_notifier_list); write_unlock(&kvm->mmu_lock); + return 0; } EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier); @@ -175,9 +180,8 @@ EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier); * stop receiving the event interception. It is the opposed operation of * kvm_page_track_register_notifier(). */ -void -kvm_page_track_unregister_notifier(struct kvm *kvm, - struct kvm_page_track_notifier_node *n) +void kvm_page_track_unregister_notifier(struct kvm *kvm, + struct kvm_page_track_notifier_node *n) { struct kvm_page_track_notifier_head *head; @@ -187,6 +191,8 @@ kvm_page_track_unregister_notifier(struct kvm *kvm, hlist_del_rcu(&n->node); write_unlock(&kvm->mmu_lock); synchronize_srcu(&head->track_srcu); + + kvm_put_kvm(kvm); } EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier); diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 898f1f1d308d..d16aced134b4 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -668,21 +668,19 @@ static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu) static int intel_vgpu_open_device(struct vfio_device *vfio_dev) { struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev); - - if (!vgpu->vfio_device.kvm || - vgpu->vfio_device.kvm->mm != current->mm) { - gvt_vgpu_err("KVM is required to use Intel vGPU\n"); - return -ESRCH; - } + int ret; if (__kvmgt_vgpu_exist(vgpu)) return -EEXIST; vgpu->track_node.track_write = kvmgt_page_track_write; vgpu->track_node.track_remove_region = kvmgt_page_track_remove_region; - kvm_get_kvm(vgpu->vfio_device.kvm); - kvm_page_track_register_notifier(vgpu->vfio_device.kvm, - &vgpu->track_node); + ret = kvm_page_track_register_notifier(vgpu->vfio_device.kvm, + &vgpu->track_node); + if (ret) { + gvt_vgpu_err("KVM is required to use Intel vGPU\n"); + return ret; + } set_bit(INTEL_VGPU_STATUS_ATTACHED, vgpu->status); @@ -717,7 +715,6 @@ 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);