KVM: x86/mmu: Rework TDP MMU eager page splitting SP allocations
v4 cover KVM: x86/mmu: Rework TDP MMU eager page splitting SP allocations
Rework the TDP MMU eager page splitting code to always drop mmu_lock when allocation shadow pages, to avoid causing lock contention with vCPU threads during CLEAR_DIRTY_LOG (where mmu_lock is held for write).
The first patch changes KVM to always drop mmu_lock lock and the subsequent patches clean up and simplify the code now that conditional locking is gone.
v4:
- Use kmem_cache_zalloc() instead of __GFP_ZERO [Sean]
- Move mmu_lock and RCU acquire/release into the loop [Sean]
- Avoid unnecessary reaquire of RCU read lock [Sean]
v3: https://lore.kernel.org/kvm/[email protected]/
- Only drop mmu_lock during TDP MMU eager page splitting. This fixes the performance regression without regressing CLEAR_DIRTY_LOG performance on other architectures from frequent lock dropping.
v2: https://lore.kernel.org/kvm/[email protected]/
- Rebase onto kvm/queue [Marc]
v1: https://lore.kernel.org/kvm/[email protected]/
David Matlack (4):
KVM: x86/mmu: Always drop mmu_lock to allocate TDP MMU SPs for eager
splitting
KVM: x86/mmu: Hard code GFP flags for TDP MMU eager split allocations
KVM: x86/mmu: Unnest TDP MMU helpers to allocate SPs for eager
splitting
KVM: x86/mmu: Avoid reacquiring RCU if TDP MMU fails to allocate an SP
arch/x86/kvm/mmu/tdp_mmu.c | 78 ++++++++++++-------------------------- 1 file changed, 24 insertions(+), 54 deletions(-)
base-commit: af0903ab52ee6d6f0f63af67fa73d5eb00f79b9a
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
PATCH v4 1/4 KVM: x86/mmu: Always drop mmu_lock to allocate TDP MMU SPs for eager splitting
Always drop mmu_lock to allocate shadow pages in the TDP MMU when doing eager page splitting. Dropping mmu_lock during eager page splitting is cheap since KVM does not have to flush remote TLBs, and avoids stalling vCPU threads that are taking page faults while KVM is eager splitting under mmu_lock held for write.
This change reduces 20%+ dips in MySQL throughput during live migration in a 160 vCPU VM while userspace is issuing CLEAR_DIRTY_LOG ioctls (tested with 1GiB and 8GiB CLEARs). Userspace could issue finer-grained CLEARs, which would also reduce contention on mmu_lock, but doing so will increase the rate of remote TLB flushing, since KVM must flush TLBs before returning from CLEAR_DITY_LOG.
When there isn't contention on mmu_lock[1], this change does not regress the time it takes to perform eager page splitting.
[1] Tested with dirty_log_perf_test, which does not run vCPUs during eager page splitting, and with a 16 vCPU VM Live Migration with manual-protect disabled (where mmu_lock is held for read).
--- arch/x86/kvm/mmu/tdp_mmu.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +1 -15
@@ -1366,19 +1366,6 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,kvm_lockdep_assert_mmu_lock_held (kvm, shared); -/* -* Since we are allocating while under the MMU lock we have to be -* careful about GFP flags. Use GFP_NOWAIT to avoid blocking on direct -* reclaim and to avoid making any filesystem callbacks (which can end -* up invoking KVM MMU notifiers, resulting in a deadlock). -* -* If this allocation fails we drop the lock and retry with reclaim -* allowed. -*/ - sp =__tdp_mmu_alloc_sp_for_split (GFP_NOWAIT | __GFP_ACCOUNT); -if (sp) -return sp; -rcu_read_unlock ();if (shared)
@@ -1478,8 +1465,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,break ; } -if (iter.yielded) -continue ; +continue ; }tdp_mmu_init_child_sp (sp, &iter);
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
PATCH v4 2/4 KVM: x86/mmu: Hard code GFP flags for TDP MMU eager split allocations
In reply to David MatlackHard code GFP_KERNEL_ACCOUNT when allocating shadow pages during eager page splitting in the TDP MMU. Opportunistically replace use of __GFP_ZERO with allocations that zero to improve readability.
No functional change intended.
--- arch/x86/kvm/mmu/tdp_mmu.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +4 -6
@@ -1339,17 +1339,15 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split (gfp_t gfp) +static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split (void ) {struct kvm_mmu_page *sp; - gfp |= __GFP_ZERO; - - sp =kmem_cache_alloc (mmu_page_header_cache, gfp); + sp =kmem_cache_zalloc (mmu_page_header_cache,GFP_KERNEL_ACCOUNT );if (!sp)return NULL ; - sp->spt = (void *)__get_free_page (gfp); + sp->spt = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!sp->spt) {kmem_cache_free (mmu_page_header_cache, sp);return NULL ;
@@ -1374,7 +1372,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,write_unlock (&kvm->mmu_lock); iter->yielded = true; - sp =__tdp_mmu_alloc_sp_for_split (GFP_KERNEL_ACCOUNT ); + sp =__tdp_mmu_alloc_sp_for_split ();if (shared)read_lock (&kvm->mmu_lock);
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
PATCH v4 3/4 KVM: x86/mmu: Unnest TDP MMU helpers to allocate SPs for eager splitting
In reply to David MatlackMove the implementation of tdp_mmu_alloc_sp_for_split() to its one and only caller to reduce unnecessary nesting and make it more clear why the eager split loop continues after allocating a new SP.
Opportunistically drop the double-underscores from __tdp_mmu_alloc_sp_for_split() now that its parent is gone.
No functional change intended.
--- arch/x86/kvm/mmu/tdp_mmu.c | 48 ++++++++++++++------------------------ 1 file changed, 18 insertions(+), 30 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +18 -30
@@ -1339,7 +1339,7 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,return spte_set; } -static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split (void ) +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (void ) {struct kvm_mmu_page *sp;
@@ -1356,34 +1356,6 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(void)return sp; } -static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split (struct kvm *kvm, -struct tdp_iter *iter, -bool shared) -{ -struct kvm_mmu_page *sp; - -kvm_lockdep_assert_mmu_lock_held (kvm, shared); - -rcu_read_unlock (); - -if (shared) -read_unlock (&kvm->mmu_lock); -else -write_unlock (&kvm->mmu_lock); - - iter->yielded = true; - sp =__tdp_mmu_alloc_sp_for_split (); - -if (shared) -read_lock (&kvm->mmu_lock); -else -write_lock (&kvm->mmu_lock); - -rcu_read_lock (); - -return sp; -} -/* Note, the caller is responsible for initializing @sp. */ static int tdp_mmu_split_huge_page (struct kvm *kvm,struct tdp_iter *iter,struct kvm_mmu_page *sp,bool shared)
@@ -1454,7 +1426,22 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,continue ;if (!sp) { - sp =tdp_mmu_alloc_sp_for_split (kvm, &iter, shared); +rcu_read_unlock (); + +if (shared) +read_unlock (&kvm->mmu_lock); +else +write_unlock (&kvm->mmu_lock); + + sp =tdp_mmu_alloc_sp_for_split (); + +if (shared) +read_lock (&kvm->mmu_lock); +else +write_lock (&kvm->mmu_lock); + +rcu_read_lock (); +if (!sp) { ret = -ENOMEM ; trace_kvm_mmu_split_huge_page(iter.gfn,
@@ -1463,6 +1450,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,break ; } + iter.yielded = true;continue ; }
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
PATCH v4 4/4 KVM: x86/mmu: Avoid reacquiring RCU if TDP MMU fails to allocate an SP
In reply to David MatlackAvoid needlessly reacquiring the RCU read lock if the TDP MMU fails to allocate a shadow page during eager page splitting. Opportunistically drop the local variable ret as well now that it's no longer necessary.
--- arch/x86/kvm/mmu/tdp_mmu.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +5 -7
@@ -1402,7 +1402,6 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm, {struct kvm_mmu_page *sp =NULL ;struct tdp_iter iter; -int ret =0 ;rcu_read_lock ();
@@ -1440,16 +1439,15 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,else write_lock (&kvm->mmu_lock); -rcu_read_lock (); -if (!sp) { - ret = -ENOMEM ;trace_kvm_mmu_split_huge_page (iter.gfn, iter.old_spte, - iter.level, ret); -break ; + iter.level, -ENOMEM ); +return -ENOMEM ; } +rcu_read_lock (); + iter.yielded = true;continue ; }
@@ -1472,7 +1470,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,if (sp)tdp_mmu_free_sp (sp); -return ret; +return 0 ; }
Signature · 1 line
2.45.2.505.gda0bf45e8d-goog
Quoted 9 lines · David Matlack
Rework the TDP MMU eager page splitting code to always drop
mmu_lockwhen allocation shadow pages, to avoid causing lock contention with vCPU threads duringCLEAR_DIRTY_LOG(wheremmu_lockis held for write).The first patch changes KVM to always drop
mmu_locklock and the subsequent patches clean up and simplify the code now that conditional locking is gone.[...]
Applied to kvm-x86 mmu, thanks!
[1/4] KVM: x86/mmu: Always drop mmu_lock to allocate TDP MMU SPs for eager splitting
https://github.com/kvm-x86/linux/commit/cf3ff0ee24d6 [2/4] KVM: x86/mmu: Hard code GFP flags for TDP MMU eager split allocations https://github.com/kvm-x86/linux/commit/e1c04f7a9f42 [3/4] KVM: x86/mmu: Unnest TDP MMU helpers to allocate SPs for eager splitting https://github.com/kvm-x86/linux/commit/3d4a5a45ca26 [4/4] KVM: x86/mmu: Avoid reacquiring RCU if TDP MMU fails to allocate an SP https://github.com/kvm-x86/linux/commit/0089c055b560