Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1786974pxb; Wed, 20 Oct 2021 11:43:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyeBtB0rS5V6g4RfQX6fMRFvjETCGlKHjiPDD+8URAJVG/Axvqu0hvTgpro8lB/WwszEJwH X-Received: by 2002:a17:907:3f9c:: with SMTP id hr28mr1151155ejc.246.1634755427542; Wed, 20 Oct 2021 11:43:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634755427; cv=none; d=google.com; s=arc-20160816; b=Mq7xFaqpHtWDgHjQGucWf14Zeohqj5bmwmEoqfjW95yM3JqpMUZeIZQfS+YjdRCIlZ QpD2OoSocnLOtbnEZKzda8XbAeEm0BUoSldaYFtWQD5Yd+OcnCKPtFgG4azQeZi+Kg7f /qw8VvHrZQov427qlomq5bEEYGqzW8g2L9i/6R6SDPhh9DnkIGmSGRf4fo8/zpBwbpdm iZx+NAaSfYIsGsq2zfjdLqWxdiLhsd1ifs+Mg9+qcxWWApIoMM8TYvfJjazOthHC0l0+ CXBWtc7YZj1Lt0k1zssZKzz1pzaY1mFvWyWMkCQ2jkBvvBu5GZla3kd2/Fg3W8KudBXP 7I5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to; bh=wDXA091VMLKPNo2l6t3dwg1mZwWX+vkBr1j4nu6BfX4=; b=GkdAbCBoVN75OwJ3qZ6zS9UFvg/4H+bgNI+TztNxOju6fR/YOLGtoPzzAISEC1i3+w uVRmaWn0tNCs6ow5kFhgWiTVWXk6FS+6QOoM1DMEAmMo6Y2CwrvJi+Jk4ubXZg5sB7Pa APx8l80NgUnHPQNylJMP/ZT2mKchtaYTjgZjYtej9FheY1kaPD9XhgQuRMdyPATZduL3 U4fPsPKBzapMKuNVeivL6e9w0yjdu4JlAUCbLRRG8PvJ5WB1TXu3yv7bsGjeQqeRoHMl h3F1IXGjQiJKZxy950OqE6cbXM3e5eWQ/P1czXEf/3u4LMOqX55QfjRJd67g+XVumjZ+ rrNw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v20si7023827edc.234.2021.10.20.11.43.18; Wed, 20 Oct 2021 11:43:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230325AbhJTSmx (ORCPT + 99 others); Wed, 20 Oct 2021 14:42:53 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:40788 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231305AbhJTSmq (ORCPT ); Wed, 20 Oct 2021 14:42:46 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1mdGVk-0002da-PZ; Wed, 20 Oct 2021 20:40:24 +0200 To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Igor Mammedov , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Christian Borntraeger , Janosch Frank , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: From: "Maciej S. Szmigiero" Subject: Re: [PATCH v5 01/13] KVM: x86: Cache total page count to avoid traversing the memslot array Message-ID: <59217787-416c-fc04-f69f-61801477b2ea@maciej.szmigiero.name> Date: Wed, 20 Oct 2021 20:40:19 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 20.10.2021 00:31, Sean Christopherson wrote: > On Tue, Oct 19, 2021, Sean Christopherson wrote: >> On Mon, Sep 20, 2021, Maciej S. Szmigiero wrote: >>> From: "Maciej S. Szmigiero" >>> >>> There is no point in recalculating from scratch the total number of pages >>> in all memslots each time a memslot is created or deleted. >>> >>> Just cache the value and update it accordingly on each such operation so >>> the code doesn't need to traverse the whole memslot array each time. >>> >>> Signed-off-by: Maciej S. Szmigiero >>> --- >>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >>> index 28ef14155726..65fdf27b9423 100644 >>> --- a/arch/x86/kvm/x86.c >>> +++ b/arch/x86/kvm/x86.c >>> @@ -11609,9 +11609,23 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, >>> const struct kvm_memory_slot *new, >>> enum kvm_mr_change change) >>> { >>> - if (!kvm->arch.n_requested_mmu_pages) >>> - kvm_mmu_change_mmu_pages(kvm, >>> - kvm_mmu_calculate_default_mmu_pages(kvm)); >>> + if (change == KVM_MR_CREATE) >>> + kvm->arch.n_memslots_pages += new->npages; >>> + else if (change == KVM_MR_DELETE) { >>> + WARN_ON(kvm->arch.n_memslots_pages < old->npages); >>> + kvm->arch.n_memslots_pages -= old->npages; >>> + } >>> + >>> + if (!kvm->arch.n_requested_mmu_pages) { >> >> Hmm, once n_requested_mmu_pages is set it can't be unset. That means this can be >> further optimized to skip avoid taking mmu_lock on flags-only changes (and >> memslot movement). E.g. >> >> if (!kvm->arch.n_requested_mmu_pages && >> (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { >> >> } >> >> It's a little risky, but kvm_vm_ioctl_set_nr_mmu_pages() would need to be modified >> to allow clearing n_requested_mmu_pages and it already takes slots_lock, so IMO >> it's ok to force kvm_vm_ioctl_set_nr_mmu_pages() to recalculate pages if it wants >> to allow reverting back to the default. > > Doh, and then I read patch 2... > > I would swap the order of patch 2 and patch 1, that way the optimization patch is > super simple, and you don't end up reworking a bunch of code that was added in the > immediately preceding patch. E.g. as a first patch > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 28ef14155726..f3b1aed08566 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -11609,7 +11609,8 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, > const struct kvm_memory_slot *new, > enum kvm_mr_change change) > { > - if (!kvm->arch.n_requested_mmu_pages) > + 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)); > > > Will do. Thanks, Maciej