kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Ben Gardon <bgardon@google.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	Peter Shier <pshier@google.com>,
	"Maciej S . Szmigiero" <maciej.szmigiero@oracle.com>,
	Leo Hou <leohou1402@gmail.com>
Subject: Re: [PATCH v2 1/2] KVM: x86/mmu: Ensure TDP MMU roots are freed after yield
Date: Wed, 6 Jan 2021 13:29:50 -0800	[thread overview]
Message-ID: <X/Yrzgli82BOBgiJ@google.com> (raw)
In-Reply-To: <20210106185951.2966575-1-bgardon@google.com>

On Wed, Jan 06, 2021, Ben Gardon wrote:
> Many TDP MMU functions which need to perform some action on all TDP MMU
> roots hold a reference on that root so that they can safely drop the MMU
> lock in order to yield to other threads. However, when releasing the
> reference on the root, there is a bug: the root will not be freed even
> if its reference count (root_count) is reduced to 0.
> 
> To simplify acquiring and releasing references on TDP MMU root pages, and
> to ensure that these roots are properly freed, move the get/put operations
> into the TDP MMU root iterator macro. Not all functions which use the macro
> currently get and put a reference to the root, but adding this behavior is
> harmless.

I wouldn't say it's harmless, it creates the potential for refcount leaks where
they otherwise wouldn't be possible (the early loop exit scenario).  Not saying
this is the wrong approach, just that it's not without downsides.

Maybe preemptively add tdp_mmu_root_iter_break(), which would just be a wrapper
around kvm_mmu_put_root(), but might help readability (if it's ever needed)?
Not sure that's a good idea, someone will probably just remove the dead code in
the future :-)

> Moving the get/put operations into the iterator macro also helps
> simplify control flow when a root does need to be freed. Note that using
> the list_for_each_entry_unsafe macro would not have been appropriate in

s/list_for_each_entry_unsafe/list_for_each_entry_safe

> this situation because it could keep a reference to the next root across
> an MMU lock release + reacquire.

Use of "reference" is a confusing; above it means refcounts, here it means a
pointer _without_ an elevated refcount.  Something like this?

  ... would not have been apprporiate in this situation because it could keep
  a pointer to the next root across an MMU lock release + reacquire without
  pinning the next root.

> Reported-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Fixes: faaf05b00aec ("kvm: x86/mmu: Support zapping SPTEs in the TDP MMU")
> Fixes: 063afacd8730 ("kvm: x86/mmu: Support invalidate range MMU notifier for TDP MMU")
> Fixes: a6a0b05da9f3 ("kvm: x86/mmu: Support dirty logging for the TDP MMU")
> Fixes: 14881998566d ("kvm: x86/mmu: Support disabling dirty logging for the tdp MMU")
> Signed-off-by: Ben Gardon <bgardon@google.com>
> ---
>  arch/x86/kvm/mmu/tdp_mmu.c | 97 +++++++++++++++++---------------------
>  1 file changed, 44 insertions(+), 53 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 75db27fda8f3..6e076b66973c 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -44,8 +44,44 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
>  	WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots));
>  }
>  
> -#define for_each_tdp_mmu_root(_kvm, _root)			    \
> -	list_for_each_entry(_root, &_kvm->arch.tdp_mmu_roots, link)
> +static void tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root)
> +{
> +	if (kvm_mmu_put_root(kvm, root))
> +		kvm_tdp_mmu_free_root(kvm, root);
> +}
> +
> +static inline bool tdp_mmu_next_root_valid(struct kvm *kvm,
> +					   struct kvm_mmu_page *root)
> +{

Maybe add lockdep annotations here?  A couple callers already have 'em.

> +	if (list_entry_is_head(root, &kvm->arch.tdp_mmu_roots, link))
> +		return false;
> +
> +	kvm_mmu_get_root(kvm, root);
> +	return true;
> +
> +}
> +
> +static inline struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm,
> +						     struct kvm_mmu_page *root)
> +{
> +	struct kvm_mmu_page *next_root;
> +
> +	next_root = list_next_entry(root, link);
> +	tdp_mmu_put_root(kvm, root);
> +	return next_root;
> +}
> +
> +/*
> + * Note: this iterator gets and puts references to the roots it iterates over.
> + * This makes it safe to release the MMU lock and yield within the loop, but
> + * if exiting the loop early, the caller must drop the reference to the most
> + * recent root. (Unless keeping a live reference is desirable.)
> + */
> +#define for_each_tdp_mmu_root(_kvm, _root)				\
> +	for (_root = list_first_entry(&_kvm->arch.tdp_mmu_roots,	\
> +				      typeof(*_root), link);		\
> +	     tdp_mmu_next_root_valid(_kvm, _root);			\
> +	     _root = tdp_mmu_next_root(_kvm, _root))
>  
>  bool is_tdp_mmu_root(struct kvm *kvm, hpa_t hpa)
>  {
> @@ -128,7 +164,11 @@ static struct kvm_mmu_page *get_tdp_mmu_vcpu_root(struct kvm_vcpu *vcpu)
>  	/* Check for an existing root before allocating a new one. */
>  	for_each_tdp_mmu_root(kvm, root) {
>  		if (root->role.word == role.word) {
> -			kvm_mmu_get_root(kvm, root);
> +			/*
> +			 * The iterator already acquired a reference to this
> +			 * root, so simply return early without dropping the
> +			 * reference.
> +			 */
>  			spin_unlock(&kvm->mmu_lock);

I vote to open code use of list_for_each_entry() for this one specific case,
it's very much a one-off flow (relative to the other iteration scenarios).

>  			return root;
>  		}

      parent reply	other threads:[~2021-01-06 21:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-06 18:59 [PATCH v2 1/2] KVM: x86/mmu: Ensure TDP MMU roots are freed after yield Ben Gardon
2021-01-06 18:59 ` [PATCH v2 2/2] KVM: x86/mmu: Clarify TDP MMU page list invariants Ben Gardon
2021-01-06 19:03 ` [PATCH v2 1/2] KVM: x86/mmu: Ensure TDP MMU roots are freed after yield Ben Gardon
2021-01-06 22:13   ` Maciej S. Szmigiero
2021-01-06 21:29 ` Sean Christopherson [this message]

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=X/Yrzgli82BOBgiJ@google.com \
    --to=seanjc@google.com \
    --cc=bgardon@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=leohou1402@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maciej.szmigiero@oracle.com \
    --cc=pbonzini@redhat.com \
    --cc=pshier@google.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;
as well as URLs for NNTP newsgroup(s).