From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sean Christopherson Date: Tue, 9 Nov 2021 01:34:25 +0000 Subject: [PATCH v5.5 20/30] KVM: x86: Use nr_memslot_pages to avoid traversing the memslots array In-Reply-To: <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-21-seanjc@google.com> <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> Message-ID: List-Id: To: kvm-riscv@lists.infradead.org MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson 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. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson > > --- > > 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; From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sean Christopherson Date: Tue, 09 Nov 2021 01:34:25 +0000 Subject: Re: [PATCH v5.5 20/30] KVM: x86: Use nr_memslot_pages to avoid traversing the memslots array Message-Id: List-Id: References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-21-seanjc@google.com> <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> In-Reply-To: <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: "Maciej S. Szmigiero" Cc: Anup Patel , Wanpeng Li , kvm@vger.kernel.org, David Hildenbrand , linux-kernel@vger.kernel.org, Paul Mackerras , Atish Patra , Ben Gardon , linux-riscv@lists.infradead.org, Claudio Imbrenda , kvmarm@lists.cs.columbia.edu, Janosch Frank , Marc Zyngier , Joerg Roedel , Huacai Chen , Christian Borntraeger , Aleksandar Markovic , Palmer Dabbelt , Albert Ou , kvm-ppc@vger.kernel.org, Paul Walmsley , linux-arm-kernel@lists.infradead.org, Jim Mattson , Cornelia Huck , linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, Paolo Bonzini , Vitaly Kuznetsov On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson 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. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson > > --- > > 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; From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1089BC433F5 for ; Tue, 9 Nov 2021 01:34:35 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id 7C5CE610A3 for ; Tue, 9 Nov 2021 01:34:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7C5CE610A3 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id EE1394B1A3; Mon, 8 Nov 2021 20:34:33 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@google.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id cXq8OTAR4ecb; Mon, 8 Nov 2021 20:34:32 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 87A434B16E; Mon, 8 Nov 2021 20:34:32 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 5E9004B183 for ; Mon, 8 Nov 2021 20:34:31 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pPyksw3srxUi for ; Mon, 8 Nov 2021 20:34:30 -0500 (EST) Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 491BE4B0F5 for ; Mon, 8 Nov 2021 20:34:30 -0500 (EST) Received: by mail-pl1-f173.google.com with SMTP id t21so17929516plr.6 for ; Mon, 08 Nov 2021 17:34:30 -0800 (PST) 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=pNVpk+rCV3HDgb16D6KNssN4BA5U8FFOZNcV6kT2A3hGuTm+OeROOwB3Gc25RBu5yd X4Uhm2QzCn6VeLOQwzmDpP+LvkACbbM7DOiiN1F9FMFmdBDaNGhlXPS6Id8TxlqFveUv ykpl9sKIWGUMRi9cWNOq4NVL7iOrhHfOKikKZQ1aYP9i+ytWzFlBD/awLiOZyTwIbWYd XQYDPLCQNX3Mp2Ht8drmEm0GVWUVRwAlw4IodZrpKgzHqAjOAs0439do5Z+7ssrDyIK1 8md3BDizbcDwWCcQYcShMQ13fICVIj7mwm722Rbu19AAPS5eH2kbVej7sO/fO5BfTAlH wWLQ== 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=V9z3THWa42UaY78kjJb63OyfannsLdoFO1PnI0ZImkWQdqRT1Cmn8ukNZuLqA0bsYJ 12Oj5pYrCAv6kVdR/xue87xGzeuEb5t5cwxYx4TXaSBhRS9tq+1DaQkLy0gzA+ZaOfUa 31NE0DWkxYHCN7DN72Uc5Wf5aWyusOGjfdtPzVHkcXHrvJkywLtirh3vgKpyWgx3RLuL KGOZdWhxDCDE1jzwPW8YHK7Gcu+0pq/l2LM/k5V06R3RXsTMFS03iY689nCj1gGx2ger 3hKxG2zBOdGctNSVoNcyHPXzUkTVgiU0PFBSbWDsD+8YmZjEfimnN4f3vfsFoUqUQAd0 t7nA== X-Gm-Message-State: AOAM533dkMabwpgOqiFQw/S+zeLm5S7BjQIrzF65R8IQO6kkmxgONfY5 W53sHj//KBYVBZlc+IMFFosmjQ== X-Google-Smtp-Source: ABdhPJy0HaVA5X41obakvoewrsg+dddMWtR8SRrwlCTWxcj9Z8BfUyvzJbNA9mrSzEmNcuMJfGzR8Q== X-Received: by 2002:a17:90b:1c02:: with SMTP id oc2mr2968763pjb.65.1636421669237; Mon, 08 Nov 2021 17:34:29 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id h36sm307891pgb.9.2021.11.08.17.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 17:34:28 -0800 (PST) Date: Tue, 9 Nov 2021 01:34:25 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" Subject: Re: [PATCH v5.5 20/30] KVM: x86: Use nr_memslot_pages to avoid traversing the memslots array Message-ID: References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-21-seanjc@google.com> <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> Cc: Anup Patel , Wanpeng Li , kvm@vger.kernel.org, David Hildenbrand , linux-kernel@vger.kernel.org, Paul Mackerras , Atish Patra , Ben Gardon , linux-riscv@lists.infradead.org, Claudio Imbrenda , kvmarm@lists.cs.columbia.edu, Janosch Frank , Marc Zyngier , Joerg Roedel , Huacai Chen , Christian Borntraeger , Aleksandar Markovic , Palmer Dabbelt , Albert Ou , kvm-ppc@vger.kernel.org, Paul Walmsley , linux-arm-kernel@lists.infradead.org, Jim Mattson , Cornelia Huck , linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, Paolo Bonzini , Vitaly Kuznetsov X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson 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. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson > > --- > > 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; _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96650C433F5 for ; Tue, 9 Nov 2021 01:40:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 80195610F7 for ; Tue, 9 Nov 2021 01:40:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241525AbhKIBne (ORCPT ); Mon, 8 Nov 2021 20:43:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230441AbhKIBnV (ORCPT ); Mon, 8 Nov 2021 20:43:21 -0500 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC27CC01CB2B for ; Mon, 8 Nov 2021 17:34:29 -0800 (PST) Received: by mail-pl1-x62c.google.com with SMTP id q17so5849911plr.11 for ; Mon, 08 Nov 2021 17:34:29 -0800 (PST) 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=pNVpk+rCV3HDgb16D6KNssN4BA5U8FFOZNcV6kT2A3hGuTm+OeROOwB3Gc25RBu5yd X4Uhm2QzCn6VeLOQwzmDpP+LvkACbbM7DOiiN1F9FMFmdBDaNGhlXPS6Id8TxlqFveUv ykpl9sKIWGUMRi9cWNOq4NVL7iOrhHfOKikKZQ1aYP9i+ytWzFlBD/awLiOZyTwIbWYd XQYDPLCQNX3Mp2Ht8drmEm0GVWUVRwAlw4IodZrpKgzHqAjOAs0439do5Z+7ssrDyIK1 8md3BDizbcDwWCcQYcShMQ13fICVIj7mwm722Rbu19AAPS5eH2kbVej7sO/fO5BfTAlH wWLQ== 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=bhlYKvmE7fwvE6pGmvJdUuAXWe78StDUrlEvVtU933QByGjD60kaNnT6ynd/WULLFw h3em3uhTWWh2frhqksySmJnJBdBnvVPy6yLN5w8icUBxZ9ez23C2X+kFjU256kkxo+Px JvyYvEdcYwp+DgiKYSCq8BSqC9g7Jdv5bg7rEZ7qav9BbQ3+V4ttv1sTyRKP7iH4Lw58 moedIy5p2aZxG0lfSnqKXPgr+fpf3F2kPJYFpEA7EGayUXlplsy/WKyXEtE6YrF9vDha jtniFLTRA+lXaqoYHWAWf5voeE8st3Kh+wTQxMB017wRH2cS43SLzZmq5Bbn85CNzpOC 1MCg== X-Gm-Message-State: AOAM530617rgrfj3N4oPxtDsRJJyNBs/raSsdH4vsbwvJN/V9VejlCys strXqoQ+5Nlx/5oM9gNb0sAiJA== X-Google-Smtp-Source: ABdhPJy0HaVA5X41obakvoewrsg+dddMWtR8SRrwlCTWxcj9Z8BfUyvzJbNA9mrSzEmNcuMJfGzR8Q== X-Received: by 2002:a17:90b:1c02:: with SMTP id oc2mr2968763pjb.65.1636421669237; Mon, 08 Nov 2021 17:34:29 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id h36sm307891pgb.9.2021.11.08.17.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 17:34:28 -0800 (PST) Date: Tue, 9 Nov 2021 01:34:25 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ben Gardon , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Anup Patel , Paul Mackerras , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Subject: Re: [PATCH v5.5 20/30] KVM: x86: Use nr_memslot_pages to avoid traversing the memslots array Message-ID: References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-21-seanjc@google.com> <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson 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. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson > > --- > > 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; From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A544C433F5 for ; Tue, 9 Nov 2021 01:45:41 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C57F8610F8 for ; Tue, 9 Nov 2021 01:45:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C57F8610F8 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xUVAFxQf4B9muuYzxJHmzbjr5yf73z6uqA+lnXItK84=; b=kZwI/MOlh9FwQj bQGmDeBVeaS5Hy8uiKHrLLI9WmXNOeDLDhba3TsdjeiVaG98dcRtYjwt5nP+fF1mv+bbuIXHgErOB zHeYnUbazF/cJUS1Xu+vB9yfNplmCgVqgQX2lxoCztvc2fKQHTGCs+C4s+qkfZaqtRN4js42NKpJZ iesDTzGx+wYCYuadmpVhOakDnkxtwZC1tGZ4hUYeAOgWmFxBjmOegcS/cEUbIzclJ0r846MUMGhpb rdcS4TCbupPaVv87fgEAYmHtOistDi/8uR5V0eWNp3aAyrVFUdp94RuuZL54/Gu1ur6sIqCdW6UfF aLnW3yCGYd/n7p9RRMqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mkGCY-000SE1-8A; Tue, 09 Nov 2021 01:45:30 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mkG1u-000Nf5-8P for linux-riscv@lists.infradead.org; Tue, 09 Nov 2021 01:34:33 +0000 Received: by mail-pl1-x636.google.com with SMTP id b11so4701141pld.12 for ; Mon, 08 Nov 2021 17:34:29 -0800 (PST) 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=pNVpk+rCV3HDgb16D6KNssN4BA5U8FFOZNcV6kT2A3hGuTm+OeROOwB3Gc25RBu5yd X4Uhm2QzCn6VeLOQwzmDpP+LvkACbbM7DOiiN1F9FMFmdBDaNGhlXPS6Id8TxlqFveUv ykpl9sKIWGUMRi9cWNOq4NVL7iOrhHfOKikKZQ1aYP9i+ytWzFlBD/awLiOZyTwIbWYd XQYDPLCQNX3Mp2Ht8drmEm0GVWUVRwAlw4IodZrpKgzHqAjOAs0439do5Z+7ssrDyIK1 8md3BDizbcDwWCcQYcShMQ13fICVIj7mwm722Rbu19AAPS5eH2kbVej7sO/fO5BfTAlH wWLQ== 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=P0eqrwTWLxBV4wtwu9735yGWgCCZQ4d76NOaGaWhaWJTOFz4lQQYgj7wTYDzXGp7PH F55Rh67KvP5+BfPekbdgEzgNC+u4a6ifFRVPCFfUXvA7fR0e2qTHbJGa8TneaKJts1U5 rQpHv9EqHWQeEHbbFr7AoZQ9cOhUwTD+CLW6iKxkOjayMcZZHxvG+juY1IHWuigCtCWU PhaMjgVvFg5fk066ypDdAcgjOfAUM5b2w41dJ0cKhQGwbAUKCPutuokAXO6TU4qiixIc aEoH5D/e2LIShtw0pJoUKwSI5065dNZ7Wr2LqB/v1WX4emDczpBLlVuX4/jB3xHFUzUy gR4Q== X-Gm-Message-State: AOAM533lcqZgFfSzz0apz0LdEAS6Zb8cHFKjPQvjCMwkssUTkQ2jwYbk R6qjuOdO7aIYCyF+IlcqcrERcw== X-Google-Smtp-Source: ABdhPJy0HaVA5X41obakvoewrsg+dddMWtR8SRrwlCTWxcj9Z8BfUyvzJbNA9mrSzEmNcuMJfGzR8Q== X-Received: by 2002:a17:90b:1c02:: with SMTP id oc2mr2968763pjb.65.1636421669237; Mon, 08 Nov 2021 17:34:29 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id h36sm307891pgb.9.2021.11.08.17.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 17:34:28 -0800 (PST) Date: Tue, 9 Nov 2021 01:34:25 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ben Gardon , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Anup Patel , Paul Mackerras , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Subject: Re: [PATCH v5.5 20/30] KVM: x86: Use nr_memslot_pages to avoid traversing the memslots array Message-ID: References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-21-seanjc@google.com> <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211108_173430_353989_D881AD97 X-CRM114-Status: GOOD ( 17.54 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson 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. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson > > --- > > 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; _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1434C433EF for ; Tue, 9 Nov 2021 01:46:12 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BD8EC610F8 for ; Tue, 9 Nov 2021 01:46:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BD8EC610F8 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=x9iHy1S1qolsWkvb/O5hDGYftnelmTFImijkvbwS/+w=; b=OU9Vp5JqQXUqbu TRLh63TRPq2fHULjwinCELb96pwoA4JxUYT27eOvv/cP7/j/puBeCaNWQ4QB4y0ABsvpgFqfiQkCA cJ3Pmw5lhhLn25hj1KPNk4zzTD5nKci21pO3hFhp93s8P1JTksGryWx0Z49sY7OBKAlUpRM832Hv3 8iI4Ax708GxXnBqTkVUQolYHgY8xNzeFdHX7B2yN7UzbQSfzw5TkVCVQ9hwHyMIokgSFnPGLLKJ2P A2Il4gYb3w7+9WsCK3WD0t8crW67kQEwtd9IF47m1uOEt8dRFm0vZYHp7KHTiK2eAx56frjLSven5 vphcAi65aNhTHRmO2Wzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mkGBO-000Rid-8s; Tue, 09 Nov 2021 01:44:20 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mkG1u-000Nf3-97 for linux-arm-kernel@lists.infradead.org; Tue, 09 Nov 2021 01:34:31 +0000 Received: by mail-pl1-x635.google.com with SMTP id b11so4701136pld.12 for ; Mon, 08 Nov 2021 17:34:29 -0800 (PST) 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=pNVpk+rCV3HDgb16D6KNssN4BA5U8FFOZNcV6kT2A3hGuTm+OeROOwB3Gc25RBu5yd X4Uhm2QzCn6VeLOQwzmDpP+LvkACbbM7DOiiN1F9FMFmdBDaNGhlXPS6Id8TxlqFveUv ykpl9sKIWGUMRi9cWNOq4NVL7iOrhHfOKikKZQ1aYP9i+ytWzFlBD/awLiOZyTwIbWYd XQYDPLCQNX3Mp2Ht8drmEm0GVWUVRwAlw4IodZrpKgzHqAjOAs0439do5Z+7ssrDyIK1 8md3BDizbcDwWCcQYcShMQ13fICVIj7mwm722Rbu19AAPS5eH2kbVej7sO/fO5BfTAlH wWLQ== 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=1YRNoCeeCQqxDvBnoZCtovzT6k/QbqjejEhDlgxJ1xE=; b=EmF66DG/mcZQwn3B+NUrVnzk0sAFOvANh6dVeskIBRgjmvnJ1gS7ZapLCdicAzrFnU 8fOvENHNP1vW3UV1nfXMn3dvkTdSfTDcvj+uV70lWi7iOCcIqEuwcMqJWrjl6UnMGsm2 qCr3zQbm9IFc7wrSwb834722m1xl00o1V95G+FswJarqh/Giur38/e4UqI8ux/PinHPB WXeU44/sPwFKoy5Muigw+SHLk3RoMaUtTA6h7RlcnhyoiZoVN8kT4Gh1WcQaVM3rrQ2I 32PEE9njdIm7ABVju7pTm9WRbjYpAgt1MqXgRsaCPfOYvzKGpuom70zdO2zgrrjLgHbu w8kg== X-Gm-Message-State: AOAM53153OXr8GEiiQiPNCAa80AyvalQi9iSIsxwRyKC1y83/30YJ+2a UIR4DUEed80o4Gg93UrSFFM7/A== X-Google-Smtp-Source: ABdhPJy0HaVA5X41obakvoewrsg+dddMWtR8SRrwlCTWxcj9Z8BfUyvzJbNA9mrSzEmNcuMJfGzR8Q== X-Received: by 2002:a17:90b:1c02:: with SMTP id oc2mr2968763pjb.65.1636421669237; Mon, 08 Nov 2021 17:34:29 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id h36sm307891pgb.9.2021.11.08.17.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Nov 2021 17:34:28 -0800 (PST) Date: Tue, 9 Nov 2021 01:34:25 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ben Gardon , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Anup Patel , Paul Mackerras , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Subject: Re: [PATCH v5.5 20/30] KVM: x86: Use nr_memslot_pages to avoid traversing the memslots array Message-ID: References: <20211104002531.1176691-1-seanjc@google.com> <20211104002531.1176691-21-seanjc@google.com> <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <88d64cd0-4db1-34a8-96af-6661a55e971e@oracle.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211108_173430_351282_64845F83 X-CRM114-Status: GOOD ( 19.06 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, Nov 09, 2021, Maciej S. Szmigiero wrote: > On 04.11.2021 01:25, Sean Christopherson 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. Use KVM's > > cached nr_memslot_pages to compute the default max number of MMU pages. > > > > Signed-off-by: Maciej S. Szmigiero > > [sean: use common KVM field and rework changelog accordingly] Heh, and I forgot to add "and introduce bugs" > > Signed-off-by: Sean Christopherson > > --- > > 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; _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel