mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yan Zhao <yan.y.zhao@intel.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Zhenyu Wang <zhenyuw@linux.intel.com>,
	Zhi Wang <zhi.a.wang@intel.com>, <kvm@vger.kernel.org>,
	<intel-gvt-dev@lists.freedesktop.org>,
	<intel-gfx@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>,
	Ben Gardon <bgardon@google.com>
Subject: Re: [PATCH v2 10/27] drm/i915/gvt: Protect gfn hash table with vgpu_lock
Date: Fri, 17 Mar 2023 14:21:29 +0800	[thread overview]
Message-ID: <ZBQG6SIS2a4WBjNc@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <20230311002258.852397-11-seanjc@google.com>

Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>

On Fri, Mar 10, 2023 at 04:22:41PM -0800, Sean Christopherson wrote:
> Use vgpu_lock instead of KVM's mmu_lock to protect accesses to the hash
> table used to track which gfns are write-protected when shadowing the
> guest's GTT, and hoist the acquisition of vgpu_lock from
> intel_vgpu_page_track_handler() out to its sole caller,
> kvmgt_page_track_write().
> 
> This fixes a bug where kvmgt_page_track_write(), which doesn't hold
> kvm->mmu_lock, could race with intel_gvt_page_track_remove() and trigger
> a use-after-free.
> 
> Fixing kvmgt_page_track_write() by taking kvm->mmu_lock is not an option
> as mmu_lock is a r/w spinlock, and intel_vgpu_page_track_handler() might
> sleep when acquiring vgpu->cache_lock deep down the callstack:
> 
>   intel_vgpu_page_track_handler()
>   |
>   |->  page_track->handler / ppgtt_write_protection_handler()
>        |
>        |-> ppgtt_handle_guest_write_page_table_bytes()
>            |
>            |->  ppgtt_handle_guest_write_page_table()
>                 |
>                 |-> ppgtt_handle_guest_entry_removal()
>                     |
>                     |-> ppgtt_invalidate_pte()
>                         |
>                         |-> intel_gvt_dma_unmap_guest_page()
>                             |
>                             |-> mutex_lock(&vgpu->cache_lock);
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>  drivers/gpu/drm/i915/gvt/kvmgt.c      | 55 +++++++++++++++------------
>  drivers/gpu/drm/i915/gvt/page_track.c | 10 +----
>  2 files changed, 33 insertions(+), 32 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
> index 68be66395598..9824d075562e 100644
> --- a/drivers/gpu/drm/i915/gvt/kvmgt.c
> +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
> @@ -366,6 +366,8 @@ __kvmgt_protect_table_find(struct intel_vgpu *info, gfn_t gfn)
>  {
>  	struct kvmgt_pgfn *p, *res = NULL;
>  
> +	lockdep_assert_held(&info->vgpu_lock);
> +
>  	hash_for_each_possible(info->ptable, p, hnode, gfn) {
>  		if (gfn == p->gfn) {
>  			res = p;
> @@ -1567,6 +1569,9 @@ int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
>  	if (!test_bit(INTEL_VGPU_STATUS_ATTACHED, info->status))
>  		return -ESRCH;
>  
> +	if (kvmgt_gfn_is_write_protected(info, gfn))
> +		return 0;
> +
>  	idx = srcu_read_lock(&kvm->srcu);
>  	slot = gfn_to_memslot(kvm, gfn);
>  	if (!slot) {
> @@ -1575,16 +1580,12 @@ int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
>  	}
>  
>  	write_lock(&kvm->mmu_lock);
> -
> -	if (kvmgt_gfn_is_write_protected(info, gfn))
> -		goto out;
> -
>  	kvm_slot_page_track_add_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
> +	write_unlock(&kvm->mmu_lock);
> +
> +	srcu_read_unlock(&kvm->srcu, idx);
> +
>  	kvmgt_protect_table_add(info, gfn);
> -
> -out:
> -	write_unlock(&kvm->mmu_lock);
> -	srcu_read_unlock(&kvm->srcu, idx);
>  	return 0;
>  }
>  
> @@ -1597,24 +1598,22 @@ int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn)
>  	if (!test_bit(INTEL_VGPU_STATUS_ATTACHED, info->status))
>  		return -ESRCH;
>  
> -	idx = srcu_read_lock(&kvm->srcu);
> -	slot = gfn_to_memslot(kvm, gfn);
> -	if (!slot) {
> -		srcu_read_unlock(&kvm->srcu, idx);
> -		return -EINVAL;
> -	}
> -
> -	write_lock(&kvm->mmu_lock);
> -
>  	if (!kvmgt_gfn_is_write_protected(info, gfn))
> -		goto out;
> +		return 0;
>  
> +	idx = srcu_read_lock(&kvm->srcu);
> +	slot = gfn_to_memslot(kvm, gfn);
> +	if (!slot) {
> +		srcu_read_unlock(&kvm->srcu, idx);
> +		return -EINVAL;
> +	}
> +
> +	write_lock(&kvm->mmu_lock);
>  	kvm_slot_page_track_remove_page(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE);
> +	write_unlock(&kvm->mmu_lock);
> +	srcu_read_unlock(&kvm->srcu, idx);
> +
>  	kvmgt_protect_table_del(info, gfn);
> -
> -out:
> -	write_unlock(&kvm->mmu_lock);
> -	srcu_read_unlock(&kvm->srcu, idx);
>  	return 0;
>  }
>  
> @@ -1625,9 +1624,13 @@ static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
>  	struct intel_vgpu *info =
>  		container_of(node, struct intel_vgpu, track_node);
>  
> +	mutex_lock(&info->vgpu_lock);
> +
>  	if (kvmgt_gfn_is_write_protected(info, gpa_to_gfn(gpa)))
>  		intel_vgpu_page_track_handler(info, gpa,
>  						     (void *)val, len);
> +
> +	mutex_unlock(&info->vgpu_lock);
>  }
>  
>  static void kvmgt_page_track_flush_slot(struct kvm *kvm,
> @@ -1639,16 +1642,20 @@ static void kvmgt_page_track_flush_slot(struct kvm *kvm,
>  	struct intel_vgpu *info =
>  		container_of(node, struct intel_vgpu, track_node);
>  
> -	write_lock(&kvm->mmu_lock);
> +	mutex_lock(&info->vgpu_lock);
> +
>  	for (i = 0; i < slot->npages; i++) {
>  		gfn = slot->base_gfn + i;
>  		if (kvmgt_gfn_is_write_protected(info, gfn)) {
> +			write_lock(&kvm->mmu_lock);
>  			kvm_slot_page_track_remove_page(kvm, slot, gfn,
>  						KVM_PAGE_TRACK_WRITE);
> +			write_unlock(&kvm->mmu_lock);
> +
>  			kvmgt_protect_table_del(info, gfn);
>  		}
>  	}
> -	write_unlock(&kvm->mmu_lock);
> +	mutex_unlock(&info->vgpu_lock);
>  }
>  
>  void intel_vgpu_detach_regions(struct intel_vgpu *vgpu)
> diff --git a/drivers/gpu/drm/i915/gvt/page_track.c b/drivers/gpu/drm/i915/gvt/page_track.c
> index df34e73cba41..60a65435556d 100644
> --- a/drivers/gpu/drm/i915/gvt/page_track.c
> +++ b/drivers/gpu/drm/i915/gvt/page_track.c
> @@ -162,13 +162,9 @@ int intel_vgpu_page_track_handler(struct intel_vgpu *vgpu, u64 gpa,
>  	struct intel_vgpu_page_track *page_track;
>  	int ret = 0;
>  
> -	mutex_lock(&vgpu->vgpu_lock);
> -
>  	page_track = intel_vgpu_find_page_track(vgpu, gpa >> PAGE_SHIFT);
> -	if (!page_track) {
> -		ret = -ENXIO;
> -		goto out;
> -	}
> +	if (!page_track)
> +		return -ENXIO;
>  
>  	if (unlikely(vgpu->failsafe)) {
>  		/* Remove write protection to prevent furture traps. */
> @@ -179,7 +175,5 @@ int intel_vgpu_page_track_handler(struct intel_vgpu *vgpu, u64 gpa,
>  			gvt_err("guest page write error, gpa %llx\n", gpa);
>  	}
>  
> -out:
> -	mutex_unlock(&vgpu->vgpu_lock);
>  	return ret;
>  }
> -- 
> 2.40.0.rc1.284.g88254d51c5-goog
> 

  reply	other threads:[~2023-03-17  6:46 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-11  0:22 [PATCH v2 00/27] drm/i915/gvt: KVM: KVMGT fixes and page-track cleanups Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 01/27] drm/i915/gvt: Verify pfn is "valid" before dereferencing "struct page" Sean Christopherson
2023-03-13 15:37   ` Wang, Wei W
2023-03-15 18:13     ` [Intel-gfx] " Andrzej Hajda
2023-03-15 19:23       ` Sean Christopherson
2023-03-17  4:20   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 02/27] KVM: x86/mmu: Factor out helper to get max mapping size of a memslot Sean Christopherson
2023-03-13 15:37   ` Wang, Wei W
2023-03-11  0:22 ` [PATCH v2 03/27] drm/i915/gvt: remove interface intel_gvt_is_valid_gfn Sean Christopherson
2023-03-17  4:26   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 04/27] drm/i915/gvt: Incorporate KVM memslot info into check for 2MiB GTT entry Sean Christopherson
2023-03-14  3:09   ` Yan Zhao
2023-03-14 17:13     ` Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 05/27] drm/i915/gvt: Verify VFIO-pinned page is THP when shadowing 2M gtt entry Sean Christopherson
2023-03-17  5:33   ` Yan Zhao
2023-05-04 20:41     ` Sean Christopherson
2023-05-06  6:35       ` Yan Zhao
2023-05-06 10:57         ` Yan Zhao
2023-05-08 14:05           ` Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 06/27] drm/i915/gvt: Put the page reference obtained by KVM's gfn_to_pfn() Sean Christopherson
2023-03-17  6:18   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 07/27] drm/i915/gvt: Don't rely on KVM's gfn_to_pfn() to query possible 2M GTT Sean Christopherson
2023-03-17  5:37   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 08/27] drm/i915/gvt: Use an "unsigned long" to iterate over memslot gfns Sean Christopherson
2023-03-17  6:19   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 09/27] drm/i915/gvt: Drop unused helper intel_vgpu_reset_gtt() Sean Christopherson
2023-03-17  6:20   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 10/27] drm/i915/gvt: Protect gfn hash table with vgpu_lock Sean Christopherson
2023-03-17  6:21   ` Yan Zhao [this message]
2023-03-11  0:22 ` [PATCH v2 11/27] KVM: x86/mmu: Don't rely on page-track mechanism to flush on memslot change Sean Christopherson
2023-03-15  1:08   ` Yan Zhao
2023-03-15 15:32     ` Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 12/27] KVM: x86/mmu: Don't bounce through page-track mechanism for guest PTEs Sean Christopherson
2023-03-17  6:37   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 13/27] KVM: drm/i915/gvt: Drop @vcpu from KVM's ->track_write() hook Sean Christopherson
2023-03-17  7:28   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 14/27] KVM: x86: Reject memslot MOVE operations if KVMGT is attached Sean Christopherson
2023-03-15  8:03   ` Yan Zhao
2023-03-15 15:43     ` Sean Christopherson
2023-03-16  9:27       ` Yan Zhao
2023-03-17  7:29   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 15/27] drm/i915/gvt: Don't bother removing write-protection on to-be-deleted slot Sean Christopherson
2023-03-17  7:30   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 16/27] KVM: x86: Add a new page-track hook to handle memslot deletion Sean Christopherson
2023-03-17  7:43   ` Yan Zhao
2023-03-17 16:20     ` Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 17/27] drm/i915/gvt: switch from ->track_flush_slot() to ->track_remove_region() Sean Christopherson
2023-03-17  7:45   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 18/27] KVM: x86: Remove the unused page-track hook track_flush_slot() Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 19/27] KVM: x86/mmu: Move KVM-only page-track declarations to internal header Sean Christopherson
2023-03-15  8:44   ` Yan Zhao
2023-03-15 15:13     ` Sean Christopherson
2023-03-16  9:19       ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 20/27] KVM: x86/mmu: Use page-track notifiers iff there are external users Sean Christopherson
2023-03-15  9:34   ` Yan Zhao
2023-03-15 16:21     ` Sean Christopherson
2023-03-16  9:29       ` Yan Zhao
2023-03-15 10:36   ` Yan Zhao
2023-03-15 16:54     ` Sean Christopherson
2023-05-04 19:54       ` Sean Christopherson
2023-05-06  1:08         ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 21/27] KVM: x86/mmu: Drop infrastructure for multiple page-track modes Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 22/27] KVM: x86/mmu: Rename page-track APIs to reflect the new reality Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 23/27] KVM: x86/mmu: Assert that correct locks are held for page write-tracking Sean Christopherson
2023-03-17  7:55   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 24/27] KVM: x86/mmu: Bug the VM if write-tracking is used but not enabled Sean Christopherson
2023-03-11  0:22 ` [PATCH v2 25/27] KVM: x86/mmu: Drop @slot param from exported/external page-track APIs Sean Christopherson
2023-03-17  8:28   ` Yan Zhao
2023-03-23  8:50     ` Yan Zhao
2023-05-03 23:16       ` Sean Christopherson
2023-05-04  2:17         ` Yan Zhao
2023-05-08  1:15           ` Yan Zhao
2023-05-11 22:39             ` Sean Christopherson
2023-05-12  2:58               ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 26/27] KVM: x86/mmu: Handle KVM bookkeeping in page-track APIs, not callers Sean Christopherson
2023-03-17  8:52   ` Yan Zhao
2023-03-11  0:22 ` [PATCH v2 27/27] drm/i915/gvt: Drop final dependencies on KVM internal details Sean Christopherson
2023-03-17  8:58   ` Yan Zhao
2023-03-13  9:58 ` [PATCH v2 00/27] drm/i915/gvt: KVM: KVMGT fixes and page-track cleanups Yan Zhao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZBQG6SIS2a4WBjNc@yzhao56-desk.sh.intel.com \
    --to=yan.y.zhao@intel.com \
    --cc=bgardon@google.com \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-gvt-dev@lists.freedesktop.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=zhenyuw@linux.intel.com \
    --cc=zhi.a.wang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®