Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp888248pxb; Tue, 19 Oct 2021 15:26:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKaD/FQXU66BpXMu4Nm6Co4POkQ5A2CMAUv8aSzxSvcqjSaQGP8DW9ND00JMfhWVaPQ2uG X-Received: by 2002:a62:31c5:0:b0:447:cd37:61f8 with SMTP id x188-20020a6231c5000000b00447cd3761f8mr2528953pfx.29.1634682383893; Tue, 19 Oct 2021 15:26:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634682383; cv=none; d=google.com; s=arc-20160816; b=E7UXBGvIi2+xXB7CPpuSwYvNIOUgyCISGEkoXa4vsFJtA+RtdfQ8MdAxigXUc7olr3 1hnTU3EyQ8lgiULgwPW95av9BolWF+USP0culfjkjFawG9Fqsur9pyhl4z9wYJvPmdlA tG5/yvnzVsNE1GT19D8eRLeTVP/iFRPFZm+aCLBVP0reV4sqgQH/qSBz+cHvgMqd/gDG TW3BakhpaIeK9rIVwjx+qQbdqkGATDhqJqHIuVSdTN9I6ExaBAyuwsOdoaHUQ1vpESK7 gJJsW6LkRB7uRQJmSAAi3HW5y7nut5Z43BUqfM9rQEIZ623F2iVf2ysiRJlaf84D2ZK1 xPlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7DYCUknc3dQUBnoaGeUnyUU/CuK4FgSm2oYsWvbJD5E=; b=Z1L3L6mhYAsz1hPs33F+7BuBzl1uyvj49E3U79aFx+ng4fykbpwoLLKFx4nmdDkU67 W08gJSfGDiprP/gzJpT7cmZAgJj++yXHWUmBIxJzistiT4he1mC7u7dEgs+DEvCb0DeU 9TbKWWyc66HnXjDrvvwqxEzowEW9vSwZQPccdZ8nbsLXGpH7XlcASN013dJNRWo+CBGA 9QSMXm0ehfrX8E74//vkNoEyiY1yOr03GO8IorXR4IDd03DOZAvRzdh0tC00rQIanvnk wDKFtiszHsPTBb5Cs1cd6riSKww6IzUi2O34+93OvfYWu2s1LVpKPiJYz/u8Jd+6P+5Y Iwrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ba33n7vX; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q20si462384pls.53.2021.10.19.15.26.11; Tue, 19 Oct 2021 15:26:23 -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; dkim=pass header.i=@google.com header.s=20210112 header.b=ba33n7vX; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229924AbhJSW0c (ORCPT + 99 others); Tue, 19 Oct 2021 18:26:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbhJSW0a (ORCPT ); Tue, 19 Oct 2021 18:26:30 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B618C061749 for ; Tue, 19 Oct 2021 15:24:17 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id om14so1015637pjb.5 for ; Tue, 19 Oct 2021 15:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=7DYCUknc3dQUBnoaGeUnyUU/CuK4FgSm2oYsWvbJD5E=; b=ba33n7vXoirIraKwU+JkwtO9KxLQ6aPywRIkMDrDzPTXzSRhapzDByeCLM0PAr0rKm vAsZVDuWksxGPfUulEA8y14nUsrf6mN7IMNQVc0ccewhv6VJ6gv8mM/dVArwPTmVQAQM yw+YYdfPHR8gYaTyH+R7PtJlIPir+cfv2J89mVEQCGGlpR2nGup3mHTZeZAG03rsmqVB eyW9W0lbCChrj1bhkyGJd8Ln1F9rQurWt5kc24B9cMXANjtlvOK4eOitvh/itFg08B1j BZ4GAgVTcElDI/DWXfuf4kD+zYv9qK76JsHp5fmrqkGTf6YK/D9r+jMY8uCFR36v2AC9 /9NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=7DYCUknc3dQUBnoaGeUnyUU/CuK4FgSm2oYsWvbJD5E=; b=h04AsfiYSTPoOivCGFlA4scW3zZVkGUnFmgfr6f73IrhDcuVgIaSol+SjxdHzp+XE3 P7bAtQA9VLjOu1sOHYtuqVf+T9FJcmBfnLOSX5dLwchKiN/XFwaH7rP/+vT+k0d9bLvl OmOpuzQslAUbGsGTjYKqLJL8mFdNo39gWAPBHyv+TwJ9plaobrIrZap0VL/5qSTYePS5 vWaV/k+giFmrKoMoBasHgBP9YTFSvwqh0CNvPG6r4nCMUdgNh9MNwyuLMzZHKI0lacsE 3CkQ53k6GycJ/wPyLeFaT0NYzSRAyTQSmOTYXB4pGJmPos6cL6t7Qa+M5q9ba+RnmvoQ yDRA== X-Gm-Message-State: AOAM5329jhGVHqO0UnaffEiEwBQN06CwyA9sPph80NptQqZSFXporq7a jZz1dUAtvTU1XXLJ7kV5CwXOZA== X-Received: by 2002:a17:90a:d48e:: with SMTP id s14mr3026552pju.49.1634682256598; Tue, 19 Oct 2021 15:24:16 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id z13sm217243pfq.130.2021.10.19.15.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Oct 2021 15:24:15 -0700 (PDT) Date: Tue, 19 Oct 2021 22:24:11 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" 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 Subject: Re: [PATCH v5 01/13] KVM: x86: Cache total page count to avoid traversing the memslot array Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > + 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); 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); >