Re: [PATCH v4 2/7] mm/mprotect: Push mmu notifier to PUDs

Sean Christopherson
2024-08-08T15:34:01Z

On Wed, Aug 07, 2024, Peter Xu wrote:
> mprotect() does mmu notifiers in PMD levels. It's there since 2014 of
> commit a5338093bfb4 ("mm: move mmu notifier call from change_protection to
> change_pmd_range").
>
> At that time, the issue was that NUMA balancing can be applied on a huge
> range of VM memory, even if nothing was populated. The notification can be
> avoided in this case if no valid pmd detected, which includes either THP or
> a PTE pgtable page.
>
> Now to pave way for PUD handling, this isn't enough. We need to generate
> mmu notifications even on PUD entries properly. mprotect() is currently
> broken on PUD (e.g., one can easily trigger kernel error with dax 1G
> mappings already), this is the start to fix it.
>
> To fix that, this patch proposes to push such notifications to the PUD
> layers.
>
> There is risk on regressing the problem Rik wanted to resolve before, but I
> think it shouldn't really happen, and I still chose this solution because
> of a few reasons:
>
> 1) Consider a large VM that should definitely contain more than GBs of
> memory, it's highly likely that PUDs are also none. In this case there

I don't follow this. Did you mean to say it's highly likely that PUDs are *NOT*
none?

> will have no regression.
>
> 2) KVM has evolved a lot over the years to get rid of rmap walks, which
> might be the major cause of the previous soft-lockup. At least TDP MMU
> already got rid of rmap as long as not nested (which should be the major
> use case, IIUC), then the TDP MMU pgtable walker will simply see empty VM
> pgtable (e.g. EPT on x86), the invalidation of a full empty region in
> most cases could be pretty fast now, comparing to 2014.

The TDP MMU will indeed be a-ok. It only zaps leaf SPTEs in response to
mmu_notifier invalidations, and checks NEED_RESCHED after processing each SPTE,
i.e. KVM won't zap an entire PUD and get stuck processing all its children.

I doubt the shadow MMU will fair much better than it did years ago though, AFAICT
the relevant code hasn't changed. E.g. when zapping a large range in response to
an mmu_notifier invalidation, KVM never yields even if blocking is allowed. That
said, it is stupidly easy to fix the soft lockup problem in the shadow MMU. KVM
already has an rmap walk path that plays nice with NEED_RESCHED *and* zaps rmaps,
but because of how things grew organically over the years, KVM never adopted the
cond_resched() logic for the mmu_notifier path.

As a bonus, now the .change_pte() is gone, the only other usage of x86's
kvm_handle_gfn_range() is for the aging mmu_notifiers, and I want to move those
to their own flow too[*], i.e. kvm_handle_gfn_range() in its current form can
be removed entirely.

I'll post a separate series, I don't think it needs to block this work, and I'm
fairly certain I can get this done for 6.12 (shouldn't be a large or scary series,
though I may tack on my lockless aging idea as an RFC).