Skip to contentSkip to conversation

KVM: x86/mmu: Grab nx_lpage_splits as an unsigned long before division

PATCH Applied Sean Christopherson 3 participants 3 messages Jun 15, 2021 – Jun 17, 2021

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86/mmu: Grab nx_lpage_splits as an unsigned long before division

Snapshot kvm->stats.nx_lpage_splits into a local unsigned long to avoid 64-bit division on 32-bit kernels. Casting to an unsigned long is safe because the maximum number of shadow pages, n_max_mmu_pages, is also an unsigned long, i.e. KVM will start recycling shadow pages before the number of splits can exceed a 32-bit value.

ERROR: modpost: "__udivdi3" [arch/x86/kvm/kvm.ko] undefined!

Fixes 7ee093d4f3f5 ("KVM: switch per-VM stats to u64")
---
 arch/x86/kvm/mmu/mmu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +2 -1
index 720ceb0a1f5c..7d3e57678d34 100644
@@ -6043,6 +6043,7 @@ static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel
 
 static void kvm_recover_nx_lpages(struct kvm *kvm)
 {
+	unsigned long nx_lpage_splits = kvm->stat.nx_lpage_splits;
 	int rcu_idx;
 	struct kvm_mmu_page *sp;
 	unsigned int ratio;
@@ -6054,7 +6055,7 @@ static void kvm_recover_nx_lpages(struct kvm *kvm)
 	write_lock(&kvm->mmu_lock);
 
 	ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
-	to_zap = ratio ? DIV_ROUND_UP(kvm->stat.nx_lpage_splits, ratio) : 0;
+	to_zap = ratio ? DIV_ROUND_UP(nx_lpage_splits, ratio) : 0;
 	for ( ; to_zap; --to_zap) {
 		if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages))
 			break;
Signature · 1 line
2.32.0.272.g935e593368-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Sean Christopherson

Snapshot kvm->stats.nx_lpage_splits into a local unsigned long to avoid 64-bit division on 32-bit kernels. Casting to an unsigned long is safe because the maximum number of shadow pages, n_max_mmu_pages, is also an unsigned long, i.e. KVM will start recycling shadow pages before the number of splits can exceed a 32-bit value.

ERROR: modpost: "__udivdi3" [arch/x86/kvm/kvm.ko] undefined!

Fixes 7ee093d4f3f5 ("KVM: switch per-VM stats to u64")
---
 arch/x86/kvm/mmu/mmu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -0
index 720ceb0a1f5c..7d3e57678d34 100644
@@ -6043,6 +6043,7 @@ static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel
 
 static void kvm_recover_nx_lpages(struct kvm *kvm)
 {
+	unsigned long nx_lpage_splits = kvm->stat.nx_lpage_splits;
 	int rcu_idx;
 	struct kvm_mmu_page *sp;
 	unsigned int ratio;

arch/x86/kvm/mmu/mmu.c static void kvm_recover_nx_lpages(struct kvm *kvm) · line 6061

Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -6054,7 +6055,7 @@ static void kvm_recover_nx_lpages(struct kvm *kvm)
 	write_lock(&kvm->mmu_lock);
 
 	ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
-	to_zap = ratio ? DIV_ROUND_UP(kvm->stat.nx_lpage_splits, ratio) : 0;
+	to_zap = ratio ? DIV_ROUND_UP(nx_lpage_splits, ratio) : 0;
 	for ( ; to_zap; --to_zap) {
 		if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages))
 			break;

Best regards,
Maxim Levitsky

maintainer Applied
#2 lore raw
In reply to Sean Christopherson
Quoted 28 lines · Sean Christopherson

Snapshot kvm->stats.nx_lpage_splits into a local unsigned long to avoid 64-bit division on 32-bit kernels. Casting to an unsigned long is safe because the maximum number of shadow pages, n_max_mmu_pages, is also an unsigned long, i.e. KVM will start recycling shadow pages before the number of splits can exceed a 32-bit value.

   ERROR: modpost: "__udivdi3" [arch/x86/kvm/kvm.ko] undefined!
Fixes 7ee093d4f3f5 ("KVM: switch per-VM stats to u64")
---
  arch/x86/kvm/mmu/mmu.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -0
index 720ceb0a1f5c..7d3e57678d34 100644
@@ -6043,6 +6043,7 @@ static int set_nx_huge_pages_recovery_ratio(const char *val, const struct kernel
 
  static void kvm_recover_nx_lpages(struct kvm *kvm)
  {
+	unsigned long nx_lpage_splits = kvm->stat.nx_lpage_splits;
  	int rcu_idx;
  	struct kvm_mmu_page *sp;
  	unsigned int ratio;

arch/x86/kvm/mmu/mmu.c static void kvm_recover_nx_lpages(struct kvm *kvm) · line 6061

Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -6054,7 +6055,7 @@ static void kvm_recover_nx_lpages(struct kvm *kvm)
  	write_lock(&kvm->mmu_lock);
 
  	ratio = READ_ONCE(nx_huge_pages_recovery_ratio);
-	to_zap = ratio ? DIV_ROUND_UP(kvm->stat.nx_lpage_splits, ratio) : 0;
+	to_zap = ratio ? DIV_ROUND_UP(nx_lpage_splits, ratio) : 0;
  	for ( ; to_zap; --to_zap) {
  		if (list_empty(&kvm->arch.lpage_disallowed_mmu_pages))
  			break;

Queued, thanks.

Paolo