Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp893114pxb; Tue, 19 Oct 2021 15:33:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqhdA/KKsfgdYZeFA6zeXXhLBa9dKkdadLKnS+nwsxFLcjQ+2Lq/OY4vx7svxeimmzxi2n X-Received: by 2002:a17:903:22c6:b0:13f:3e6:8dd4 with SMTP id y6-20020a17090322c600b0013f03e68dd4mr35808161plg.23.1634682822520; Tue, 19 Oct 2021 15:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634682822; cv=none; d=google.com; s=arc-20160816; b=nEO3Ag+cXptCXhn4f3WW3+qFlwbht22/oNrLJvqSHgVJA6TfZKv1AhfM81SVS3/Zu3 61h5siBk3eLDXLqjoQil5tmlOqx8Kgrltob+WpT4fPPnbLbLSM1YX+7R2vBD9+F2l65L 676MtnzCVrUoYL1cI18uxq01cuTiqQom4R8XwDCbOufqmn1GNhQOU48eFAX1Sc0FBJQL D6cjl0UtZQv20dn3midbZK/vv5ctKqIKWuao0sXNykxgi1KPSw3kspmF7WOqZ/psy9CB 6KYjIOKvCCzr/q/vcrvrYghDXs+BrnnbtxVJVeaTXfl/C84umrmrBMtfj7Sc7OXzaKuM qERQ== 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=TNqWms8buUxG/jqA44+qJhAYnqd4fFLNt4zhtuGeXTE=; b=mk0b51P7+ijybLuW9Enl9iwETHKNVUnljdMq2IOgT76Fy3cMVbEvBM+e81ru/FOOnq D4imSzIZkauShGgIODZ2DoQiY2xfC300I2jVNc9Qnrdty4ErdRv/vtBut3hkWQ+aCe/i vldMhjDdxU/Lcqt5PICTPQ1ELFavW4oEXNiU1FKjorj0cOcWzEHyyaPvRTo+c/2Tx8Le eWScIPnLX+quNAltHpcuLbmXW3IDR3EsiEG0vVTTgOwq2xTg6D3F+kNkoJ06v+zHJLK3 zjgbW1wm62cHxIGTZ41PwIKD3Vibmv3hZ9P7erkBa6KuvNwwR7MB8KAJPwVVDv+OLT/4 WAzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Gl4nSNEh; 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.33.29; Tue, 19 Oct 2021 15:33:42 -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=Gl4nSNEh; 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 S229764AbhJSWdg (ORCPT + 99 others); Tue, 19 Oct 2021 18:33:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229734AbhJSWd3 (ORCPT ); Tue, 19 Oct 2021 18:33:29 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E6A5C06161C for ; Tue, 19 Oct 2021 15:31:16 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id m14so1208112pfc.9 for ; Tue, 19 Oct 2021 15:31:16 -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=TNqWms8buUxG/jqA44+qJhAYnqd4fFLNt4zhtuGeXTE=; b=Gl4nSNEhpjn0aav7KlAVCR962auTultauSxUShiqk8e5HA2wOZngXCoWiyjfyXJKzX AYqNOYJx/vAMBWzjKhWDRX7NlZ+XNxfW+7rXvOeMoTCvafq5sIM034mm1XZEF938Nh/m y1qqV1QvhM2PCUXJxahSAOIo9rrfLcAB8unYztkcK84Ptqr0cvvpq09UdoQf8C57q0Ly mIgpI/hRGKAuQlFxKmzK4X+EcOgvV82pNJQrJ8mAr+srT/pGtunKDovVOlTtAn+L1CHw ktA/Rs8XfRWUDO1ukbvvdUsnELBw7OCZy6VfIlQN8zbhzLhV5M0mrns4hUSGzMawNu2a U3GQ== 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=TNqWms8buUxG/jqA44+qJhAYnqd4fFLNt4zhtuGeXTE=; b=a+/knYBNhzxsAR83XF2CvtFLXP5htlOuLKMYfxVEaCjyR8BJb+uewb9SmnzFLANSaK sFd4EXnJZomY5bt6IxYipmFzleYY5c9ewyax1HQsWRQEc+6yVTTVVBIHuXbwu3SZs9jH PWyl8kPZnNlqy5WmFQ9qm7Zq7StYFUnO9zS6IAw9s/0aZEynsHKCO5NFv7tXUxnpNMqW qn+riC4PvflbwH6apGSUzSCA3IqwAoXXWbMGvlfWDRJEqGQ7a5QpelUx3iuRYExnpR+1 snApOQFn9gh5YkS3Z5GTxxxmpelkV4ZSQhgZoz3NJ+6K4q3xRN6uwG/5i882gMwPHeXU aavA== X-Gm-Message-State: AOAM530lP6n6Cq9NjoEtEfaDPQVcckp2EPP+pXP9J8kcm/EcWwDM5B6L gzM4Ezd73nYS7nMOBl6zNV0AtQ== X-Received: by 2002:a63:f306:: with SMTP id l6mr30167959pgh.72.1634682675889; Tue, 19 Oct 2021 15:31:15 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id on17sm3844560pjb.47.2021.10.19.15.31.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Oct 2021 15:31:15 -0700 (PDT) Date: Tue, 19 Oct 2021 22:31: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 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));