From: David Woodhouse <dwmw2@infradead.org>
To: Sean Christopherson <seanjc@google.com>
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] KVM: Move gfn_to_pfn_cache invalidation to invalidate_range_end hook
Date: Tue, 06 Aug 2024 10:06:07 +0100 [thread overview]
Message-ID: <dd6ca54cfd23dba0d3cba7c1ceefea1fdfcdecbe.camel@infradead.org> (raw)
In-Reply-To: <ZrFyM8rJZYjfFawx@google.com>
[-- Attachment #1: Type: text/plain, Size: 3517 bytes --]
On Mon, 2024-08-05 at 17:45 -0700, Sean Christopherson wrote:
> On Mon, Aug 05, 2024, David Woodhouse wrote:
> > From: David Woodhouse <dwmw@amazon.co.uk>
> >
> > The existing retry loop in hva_to_pfn_retry() is extremely pessimistic.
> > If there is an invalidation running concurrently, it is effectively just
> > a complex busy wait loop because its local mmu_notifier_retry_cache()
> > function will always return true.
> >
> > It ends up functioning as a very unfair read/write lock. If userspace is
> > acting as a 'writer', performing many unrelated MM changes, then the
> > hva_to_pfn_retry() function acting as the 'reader' just backs off and
> > keep retrying for ever, not making any progress.
> >
> > Solve this by introducing a separate 'validating' flag to the GPC, so
> > that it can be marked invalid before it's even mapped. This allows the
> > invalidation to be moved to the range_end hook, and the retry loop in
> > hva_to_pfn_retry() can be changed to loop only if its particular uHVA
> > has been affected.
>
> I think I'm missing something. How does allowing hva_to_pfn_retry() allow KVM
> as a whole to make forward progress? Doesn't getting past hva_to_pfn_retry()
> just move the problem to kvm_gpc_check()?
>
> kvm_gpc_refresh() can't be called with gpc->lock held, and nor does it return
> with gpc->lock held, so a racing mmu_notifier invalidation can/will acquire
> gpc->lock and clear gpc->active, no?
>
> Oh, by "unrelated", you mean _completely_ unrelated? As in, KVM happens to do a
> refresh when userspace is blasting MADV_DONTNEED, and gets stuck retrying for
> no good reason?
Right. And again, I have no idea why userspace is doing that, and we
need to make it stop. But that's not really the point; the kernel
should be able to make progress anyway.
> Servicing guest pages faults has the same problem, which is why
> mmu_invalidate_retry_gfn() was added. Supporting hva-only GPCs made our lives a
> little harder, but not horrifically so (there are ordering differences regardless).
>
> Woefully incomplete, but I think this is the gist of what you want:
Hm, maybe. It does mean that migration occurring all through memory
(indeed, just one at top and bottom of guest memory space) would
perturb GPCs which remain present.
> > @@ -849,6 +837,8 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
> > wake = !kvm->mn_active_invalidate_count;
> > spin_unlock(&kvm->mn_invalidate_lock);
> >
> > + gfn_to_pfn_cache_invalidate(kvm, range->start, range->end);
>
> We can't do this. The contract with mmu_notifiers is that secondary MMUs must
> unmap the hva before returning from invalidate_range_start(), and must not create
> new mappings until invalidate_range_end().
But in the context of the GPC, it is only "mapped" when the ->valid bit is set.
Even the invalidation callback just clears the valid bit, and that
means nobody is allowed to dereference the ->khva any more. It doesn't
matter that the underlying (stale) PFN is still kmapped.
Can we not apply the same logic to the hva_to_pfn_retry() loop? Yes, it
might kmap a page that gets removed, but it's not actually created a
new mapping if it hasn't set the ->valid bit.
I don't think this version quite meets the constraints, and I might
need to hook *both* the start and end notifiers, and might not like it
once I get there. But I'll have a go...
[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]
next prev parent reply other threads:[~2024-08-06 9:06 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-27 1:39 [PATCH v2 0/8] KVM: Fix mmu_notifier vs. pfncache vs. pfncache races Sean Christopherson
2022-04-27 1:39 ` [PATCH v2 1/8] Revert "KVM: Do not speculatively mark pfn cache valid to "fix" race" Sean Christopherson
2022-04-27 1:39 ` [PATCH v2 2/8] Revert "KVM: Fix race between mmu_notifier invalidation and pfncache refresh" Sean Christopherson
2022-04-27 1:39 ` [PATCH v2 3/8] KVM: Drop unused @gpa param from gfn=>pfn cache's __release_gpc() helper Sean Christopherson
2022-04-27 1:40 ` [PATCH v2 4/8] KVM: Put the extra pfn reference when reusing a pfn in the gpc cache Sean Christopherson
2022-04-27 1:40 ` [PATCH v2 5/8] KVM: Do not incorporate page offset into gfn=>pfn cache user address Sean Christopherson
2022-04-27 1:40 ` [PATCH v2 6/8] KVM: Fix multiple races in gfn=>pfn cache refresh Sean Christopherson
2022-04-27 14:10 ` Sean Christopherson
2022-04-28 3:39 ` Lai Jiangshan
2022-04-28 14:33 ` Sean Christopherson
2022-05-20 14:49 ` Paolo Bonzini
2022-05-20 14:58 ` Paolo Bonzini
2022-05-20 15:02 ` Sean Christopherson
2022-05-20 14:59 ` Sean Christopherson
2024-08-05 11:04 ` David Woodhouse
2024-08-05 11:08 ` [PATCH] KVM: Move gfn_to_pfn_cache invalidation to invalidate_range_end hook David Woodhouse
2024-08-06 0:45 ` Sean Christopherson
2024-08-06 9:06 ` David Woodhouse [this message]
2024-08-06 14:03 ` Sean Christopherson
2024-08-06 14:24 ` David Woodhouse
2024-08-06 15:57 ` Sean Christopherson
2024-08-06 16:40 ` David Woodhouse
2024-08-22 9:00 ` David Woodhouse
2022-04-27 1:40 ` [PATCH v2 7/8] KVM: Do not pin pages tracked by gfn=>pfn caches Sean Christopherson
2022-04-27 1:40 ` [PATCH v2 8/8] DO NOT MERGE: Hack-a-test to verify gpc invalidation+refresh Sean Christopherson
2022-04-27 15:17 ` David Woodhouse
2022-04-27 20:23 ` Sean Christopherson
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=dd6ca54cfd23dba0d3cba7c1ceefea1fdfcdecbe.camel@infradead.org \
--to=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=seanjc@google.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
Powered by JetHome