From: Sean Christopherson <seanjc@google.com>
To: David Woodhouse <dwmw2@infradead.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Mushahid Hussain <hmushi@amazon.co.uk>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>,
Joerg Roedel <joro@8bytes.org>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Mingwei Zhang <mizhang@google.com>,
Maxim Levitsky <mlevitsk@redhat.com>
Subject: Re: [PATCH v3 4/5] KVM: pfncache: Move invalidation to invalidate_range_end hook
Date: Tue, 15 Oct 2024 17:17:08 -0700 [thread overview]
Message-ID: <Zw8GBOtvNhPhSzuw@google.com> (raw)
In-Reply-To: <20240821202814.711673-4-dwmw2@infradead.org>
On Wed, Aug 21, 2024, David Woodhouse wrote:
> From: David Woodhouse <dwmw@amazon.co.uk>
>
> By performing the invalidation from the 'end' hook, the process is a lot
> cleaner and simpler because it is guaranteed that ->needs_invalidation
> will be cleared after hva_to_pfn() has been called, so the only thing
> that hva_to_pfn_retry() needs to do is *wait* for there to be no pending
> invalidations.
>
> Another false positive on the range based checks is thus removed as well.
...
> @@ -261,6 +239,14 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)
> goto out_error;
> }
>
> + /*
> + * Avoid populating a GPC with a user address which is already
> + * being invalidated, if the invalidate_range_start() notifier
> + * has already been called. The actual invalidation happens in
> + * the invalidate_range_end() callback, so wait until there are
> + * no active invalidations (for the relevant HVA).
> + */
> + gpc_wait_for_invalidations(gpc);
I'm not convinced scheduling out the vCPU is actually a good idea. At first
blush, it sounds good, e.g. why consume CPU cycles when forward progress is
blocked?
But scheduling out the vCPU will likely negatively impact latency, and KVM can't
predict when the invalidation will complete. E.g. the refresh() could have come
along at the start of the invalidation, but it also could have arrived at the
tail end.
And if the vCPU is the only runnable task on the CPU, and the system is under enough
load to trigger an invalidation, then scheduling out won't provide any benefit
because odds are good the processor won't be able to get into a deep enough sleep
state to allow other logical CPUs to hit higher turbo bins.
The wait+schedule() logic for the memslots update is purely about being deliberately
_unfair_ to avoid a deadlock (the original idea was to simply use a r/w semapahore
to block memslot updates, but lock fairness lead to a possible deadlock).
If we want to not busy wait, then we should probably do something along the lines
of halt-polling, e.g. busy wait for a bit and then schedule() out. But even that
would require tuning, and I highly doubt that there will be enough colliding
invalidations and retries to build sufficient state to allow KVM to make a "good"
decision.
If you (or anyone else) have numbers to show that blocking until the invalidation
goes away provides meaningful value in most cases, then by all means. But without
numbers, I don't think we should go this route as it's not a clear win.
> write_lock_irq(&gpc->lock);
>
> @@ -269,6 +255,13 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)
> * attempting to refresh.
> */
> WARN_ON_ONCE(gpc->valid);
> +
> + /*
> + * Since gfn_to_pfn_cache_invalidate() is called from the
> + * kvm_mmu_notifier_invalidate_range_end() callback, it can
> + * invalidate the GPC the moment after hva_to_pfn() returned
> + * a valid PFN. If that happens, retry.
> + */
> } while (!gpc->needs_invalidation);
>
> gpc->valid = true;
> --
> 2.44.0
>
next prev parent reply other threads:[~2024-10-16 0:17 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-21 20:28 [PATCH v3 1/5] KVM: pfncache: Add needs_invalidation flag to gfn_to_pfn_cache David Woodhouse
2024-08-21 20:28 ` [PATCH v3 2/5] KVM: pfncache: Implement range-based invalidation check for hva_to_pfn_retry() David Woodhouse
2024-10-16 0:04 ` Sean Christopherson
2024-08-21 20:28 ` [PATCH v3 3/5] KVM: pfncache: Wait for pending invalidations instead of spinning David Woodhouse
2024-08-21 20:28 ` [PATCH v3 4/5] KVM: pfncache: Move invalidation to invalidate_range_end hook David Woodhouse
2024-10-16 0:17 ` Sean Christopherson [this message]
2024-08-21 20:28 ` [PATCH v3 5/5] DO NOT MERGE: Hack-a-test to verify gpc invalidation+refresh David Woodhouse
2024-09-19 11:13 ` [PATCH v3 1/5] KVM: pfncache: Add needs_invalidation flag to gfn_to_pfn_cache David Woodhouse
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=Zw8GBOtvNhPhSzuw@google.com \
--to=seanjc@google.com \
--cc=dwmw2@infradead.org \
--cc=hmushi@amazon.co.uk \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mizhang@google.com \
--cc=mlevitsk@redhat.com \
--cc=pbonzini@redhat.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.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