Message ID | 20211104002531.1176691-21-seanjc@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: Scalable memslots implementation | expand |
On 04.11.2021 01:25, Sean Christopherson wrote: > From: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> > > There is no point in recalculating from scratch the total number of pages > in all memslots each time a memslot is created or deleted. Use KVM's > cached nr_memslot_pages to compute the default max number of MMU pages. > > Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> > [sean: use common KVM field and rework changelog accordingly] > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > arch/x86/include/asm/kvm_host.h | 1 - > arch/x86/kvm/mmu/mmu.c | 24 ------------------------ > arch/x86/kvm/x86.c | 11 ++++++++--- > 3 files changed, 8 insertions(+), 28 deletions(-) > (..) > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -11837,9 +11837,14 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, > enum kvm_mr_change change) > { > if (!kvm->arch.n_requested_mmu_pages && > - (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) > - kvm_mmu_change_mmu_pages(kvm, > - kvm_mmu_calculate_default_mmu_pages(kvm)); > + (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { > + unsigned long nr_mmu_pages; > + > + nr_mmu_pages = kvm->nr_memslot_pages * KVM_PERMILLE_MMU_PAGES; Unfortunately, even if kvm->nr_memslot_pages is capped at ULONG_MAX then this value multiplied by 20 can still overflow an unsigned long variable. Thanks, Maciej
On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson wrote: > > From: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> > > > > There is no point in recalculating from scratch the total number of pages > > in all memslots each time a memslot is created or deleted. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson <seanjc@google.com> > > --- > > arch/x86/include/asm/kvm_host.h | 1 - > > arch/x86/kvm/mmu/mmu.c | 24 ------------------------ > > arch/x86/kvm/x86.c | 11 ++++++++--- > > 3 files changed, 8 insertions(+), 28 deletions(-) > > > (..) > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -11837,9 +11837,14 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, > > enum kvm_mr_change change) > > { > > if (!kvm->arch.n_requested_mmu_pages && > > - (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) > > - kvm_mmu_change_mmu_pages(kvm, > > - kvm_mmu_calculate_default_mmu_pages(kvm)); > > + (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { > > + unsigned long nr_mmu_pages; > > + > > + nr_mmu_pages = kvm->nr_memslot_pages * KVM_PERMILLE_MMU_PAGES; > > Unfortunately, even if kvm->nr_memslot_pages is capped at ULONG_MAX then > this value multiplied by 20 can still overflow an unsigned long variable. Doh. And that likely subtly avoided by the compiler collapsing the "* 20 / 1000" into "/ 50". Any objection to adding a patch to cut out the multiplication entirely? Well, cut it from the source code, looks like gcc generates some fancy SHR+MUL to do the divide. I'm thinking this: #define KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO 50 ... nr_mmu_pages = nr_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO;
On 09.11.2021 02:34, Sean Christopherson wrote: > On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: >> On 04.11.2021 01:25, Sean Christopherson wrote: >>> From: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> >>> >>> There is no point in recalculating from scratch the total number of pages >>> in all memslots each time a memslot is created or deleted. Use KVM's >>> cached nr_memslot_pages to compute the default max number of MMU pages. >>> >>> Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> >>> [sean: use common KVM field and rework changelog accordingly] > > Heh, and I forgot to add "and introduce bugs" > >>> Signed-off-by: Sean Christopherson <seanjc@google.com> >>> --- >>> arch/x86/include/asm/kvm_host.h | 1 - >>> arch/x86/kvm/mmu/mmu.c | 24 ------------------------ >>> arch/x86/kvm/x86.c | 11 ++++++++--- >>> 3 files changed, 8 insertions(+), 28 deletions(-) >>> >> (..) >>> --- a/arch/x86/kvm/x86.c >>> +++ b/arch/x86/kvm/x86.c >>> @@ -11837,9 +11837,14 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, >>> enum kvm_mr_change change) >>> { >>> if (!kvm->arch.n_requested_mmu_pages && >>> - (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) >>> - kvm_mmu_change_mmu_pages(kvm, >>> - kvm_mmu_calculate_default_mmu_pages(kvm)); >>> + (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { >>> + unsigned long nr_mmu_pages; >>> + >>> + nr_mmu_pages = kvm->nr_memslot_pages * KVM_PERMILLE_MMU_PAGES; >> >> Unfortunately, even if kvm->nr_memslot_pages is capped at ULONG_MAX then >> this value multiplied by 20 can still overflow an unsigned long variable. > > Doh. And that likely subtly avoided by the compiler collapsing the "* 20 / 1000" > into "/ 50". Unfortunately in this case (but fortunately in general) C has well-defined unsigned overflow behavior so if this code has an overflow by its naive reading the compiler is not allowed to transform it to a form which doesn't have this overflow (as it will result in a different value). > Any objection to adding a patch to cut out the multiplication entirely? Well, cut > it from the source code, looks like gcc generates some fancy SHR+MUL to do the > divide. > > I'm thinking this: > > #define KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO 50 > > > ... > > nr_mmu_pages = nr_pages / KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO; > > I agree this would be a good solution to our problem, since this macro is unlikely to be converted into something user-configurable in the future. So its units don't matter. Thanks, Maciej
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 88fce6ab4bbd..3fe155ece015 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1582,7 +1582,6 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm, const struct kvm_memory_slot *memslot); void kvm_mmu_zap_all(struct kvm *kvm); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); -unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm); void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages); int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 354d2ca92df4..564781585fd2 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6141,30 +6141,6 @@ int kvm_mmu_module_init(void) return ret; } -/* - * Calculate mmu pages needed for kvm. - */ -unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm) -{ - unsigned long nr_mmu_pages; - unsigned long nr_pages = 0; - struct kvm_memslots *slots; - struct kvm_memory_slot *memslot; - int i; - - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { - slots = __kvm_memslots(kvm, i); - - kvm_for_each_memslot(memslot, slots) - nr_pages += memslot->npages; - } - - nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; - nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES); - - return nr_mmu_pages; -} - void kvm_mmu_destroy(struct kvm_vcpu *vcpu) { kvm_mmu_unload(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4b0cb7390902..9a0440e22ede 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11837,9 +11837,14 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, enum kvm_mr_change change) { if (!kvm->arch.n_requested_mmu_pages && - (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) - kvm_mmu_change_mmu_pages(kvm, - kvm_mmu_calculate_default_mmu_pages(kvm)); + (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { + unsigned long nr_mmu_pages; + + nr_mmu_pages = kvm->nr_memslot_pages * KVM_PERMILLE_MMU_PAGES; + nr_mmu_pages /= 1000; + nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES); + kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); + } kvm_mmu_slot_apply_flags(kvm, old, new, change);