Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1788950pxb; Wed, 20 Oct 2021 11:46:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXqsr7W9Tq1MbJAE8XKS9BzXSZlsUiLvME6+uwnahQyZJg2J3cFGp35ptp91H7QTcGNHec X-Received: by 2002:a17:906:68d6:: with SMTP id y22mr1341818ejr.274.1634755571191; Wed, 20 Oct 2021 11:46:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634755571; cv=none; d=google.com; s=arc-20160816; b=RtWf/J6X10FgALZhd1EDcTQQNjQ+VtChjpHBo/a+HVYdVdh/C+SfhbicXZP1O5+kc3 fzbdVyvxSVeHaFXMhpJ3FN1MLRqh1JU+HkiyMArJuo3L0z7jjrnth7m+wTTs/fFJ6y4N wLazxq78w3OGnzAJqGzvx/KQKrnfNc4ZLG8gJtN50UbHc4KbF32OukVB02pPw5riAxeU FnahDPFVy32dcW76Z5yEe2pYtrKLjHBpJMg1RFOGH1W/FLISKW2R9RRWylb1Iwq9bEZ0 E4rjCuVNG/CkwbHq7jbRueJxNzBQjjVyxTUUNZYt2DefqsX4kZKeIaDiRGiU/EoPISLc D55Q== 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=zVYIm+WmOFcXYYbhgyvYAxEDu9nBWXhSTP8d0fHc2Kw=; b=dd6cWNrxgK/TRCg0vtdLkaBmtnnU951sxLMQE4fvpHp9FI/WA7zHlC4IM3EV+A9Uqu +VT/1mdzF6pAawtB6sXqgUB5gclo6pyaQ0sRY4zs+dGuqBs43EgbuMy7id2Va5FUfegU jt41tgGwPaGHW92l61UJaZ9DwhHlCjOlHuDBePkB+/uTY6MWpFvoDvbX6z4pv9DiV7t0 6slwIfdtVxcJbC8uHQEHry0/7pOv858JglxWdNbs8YyWlrPP1Fm7yKp6ubir48I+XWdk xiUTdYZCW/6+MG/mU8YNL0Z3A/6xy4lZIMlEgakPsmnw+28BZiNnxQYazdHOTEajwKpQ JkeQ== 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 dz20si5613560edb.246.2021.10.20.11.45.45; Wed, 20 Oct 2021 11:46:11 -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 S231342AbhJTSna (ORCPT + 99 others); Wed, 20 Oct 2021 14:43:30 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:40836 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231295AbhJTSn3 (ORCPT ); Wed, 20 Oct 2021 14:43:29 -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 1mdGWU-0002ee-EB; Wed, 20 Oct 2021 20:41:10 +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: Date: Wed, 20 Oct 2021 20:41:04 +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:24, 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) { (..) >> + u64 memslots_pages; >> + unsigned long nr_mmu_pages; >> + >> + memslots_pages = kvm->arch.n_memslots_pages * KVM_PERMILLE_MMU_PAGES; >> + do_div(memslots_pages, 1000); >> + nr_mmu_pages = max_t(typeof(nr_mmu_pages), >> + memslots_pages, KVM_MIN_ALLOC_MMU_PAGES); > > "memslots_pages" is a bit of a misnomer. Any objection to avoiding naming problems > by explicitly casting to an "unsigned long" and simply operating on nr_mmu_pages? > > nr_mmu_pages = (unsigned long)kvm->arch.n_memslots_pages; > nr_mmu_pages *= (KVM_PERMILLE_MMU_PAGES / 1000); > nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES); > kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); > > E.g. the whole thing can be > > if (!kvm->arch.n_requested_mmu_pages && > (change == KVM_MR_CREATE || change == KVM_MR_DELETE)) { > unsigned long nr_mmu_pages; > > if (change == KVM_MR_CREATE) { > kvm->arch.n_memslots_pages += new->npages; > } else { > WARN_ON(kvm->arch.n_memslots_pages < old->npages); > kvm->arch.n_memslots_pages -= old->npages; > } > > nr_mmu_pages = (unsigned long)kvm->arch.n_memslots_pages; > nr_mmu_pages *= (KVM_PERMILLE_MMU_PAGES / 1000); The above line will set nr_mmu_pages to zero since KVM_PERMILLE_MMU_PAGES is 20, so when integer-divided by 1000 will result in a multiplication coefficient of zero. > nr_mmu_pages = max(nr_mmu_pages, KVM_MIN_ALLOC_MMU_PAGES); > kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); > } > >> + kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); >> + } >> >> kvm_mmu_slot_apply_flags(kvm, old, new, change); >> Thanks, Maciej