Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2374871pxb; Mon, 20 Sep 2021 20:36:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwok1q8lDV7BdYg+W3xlejNowAi3ultfnEGc1Hj/SmSBRu318RbBQWpjjeI/lV1Mb62EdQ0 X-Received: by 2002:a50:f145:: with SMTP id z5mr33224251edl.4.1632195400962; Mon, 20 Sep 2021 20:36:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632195400; cv=none; d=google.com; s=arc-20160816; b=o3NvVbRQslSFTvYeJVsv8/hGCymQDOGKZX7yEG4y0U57wHoLwubHnGibMWOV9B9GHX VgPD5rJXCmfOGhRKYoz5byFxwl73xHJV1IS0ppNoZISOPn5tQYYXyg+cRSC69rncaVDe PkekOPdbxCgX6mj2vwo3SMnJw8QE3GfEFMioTEdXwzZZ/Mu3VVNnZ/f3gKdCQBTDFCcI VJDGOoHRceEmKjvm+Ecv5zsm05TkznYxYaY3R/KCi09yhu8/yVarK9YPXb98P3rmHEAc GXMQ+NHCp+MS0HITGD6DRS/2FIjL26qnoQhFAX4qucFBFWHaFbrFPqUI3Oj8Wq/eoC3Z 8HRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=HeWfCVF/Shm6howj+S2jl1J9xwk5vYNwevo35h+qq9o=; b=FyVp87KSQk5nNu6R6kfEl6uYabFKajg816vRvX98c6QrjzwQeVchFYdu2/UVIyZtYu QMA66AB0JzBURet3xtIHTCWq/ZL5pcxV7tpfxF8FH8qMoer82S70X9qmpa+A4emC1ttA ovgQRV2Ofevq6esgYa3KfOZwzqRTzKjpWjwIXNWhHMVuTW2FUbXHktBQpDbXOz7aVkCB vjWjQAyIwSJyvVG4AK263XRmiNRQEKOBmMgcFNmSzkS/B7EvMYDpQ6htFBhQOGsc5gZa o4Nj6Yx7dpHhM0UVJ86qxdUJ0CBp54ZHmnyYt5F6ZPOH6x+XVXaMzBKUBliWEk+LYm4v Mo7Q== 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 v9si17596564eds.43.2021.09.20.20.36.18; Mon, 20 Sep 2021 20:36:40 -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 S239058AbhITVnX (ORCPT + 99 others); Mon, 20 Sep 2021 17:43:23 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:45414 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233066AbhITVlS (ORCPT ); Mon, 20 Sep 2021 17:41:18 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mSR0a-0006PW-Mj; Mon, 20 Sep 2021 23:39:28 +0200 From: "Maciej S. Szmigiero" To: Paolo Bonzini , Vitaly Kuznetsov Cc: Sean Christopherson , 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: [PATCH v5 04/13] KVM: x86: Move n_memslots_pages recalc to kvm_arch_prepare_memory_region() Date: Mon, 20 Sep 2021 23:38:52 +0200 Message-Id: <2a4ceee16546deeab7090efea2ee9c0db5444b84.1632171479.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Maciej S. Szmigiero" This allows us to return a proper error code in case we spot an underflow. Signed-off-by: Maciej S. Szmigiero --- arch/x86/kvm/x86.c | 49 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 97d86223427d..0fffb8414009 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11511,9 +11511,23 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, const struct kvm_userspace_memory_region *mem, enum kvm_mr_change change) { - if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) - return kvm_alloc_memslot_metadata(kvm, new, - mem->memory_size >> PAGE_SHIFT); + if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) { + int ret; + + ret = kvm_alloc_memslot_metadata(kvm, new, + mem->memory_size >> PAGE_SHIFT); + if (ret) + return ret; + + if (change == KVM_MR_CREATE) + kvm->arch.n_memslots_pages += new->npages; + } else if (change == KVM_MR_DELETE) { + if (WARN_ON(kvm->arch.n_memslots_pages < old->npages)) + return -EIO; + + kvm->arch.n_memslots_pages -= old->npages; + } + return 0; } @@ -11610,24 +11624,17 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, const struct kvm_memory_slot *new, enum kvm_mr_change change) { - if (change == KVM_MR_CREATE || change == KVM_MR_DELETE) { - 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; - } - - 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); - kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); - } + /* Only CREATE or DELETE affects n_memslots_pages */ + if ((change == KVM_MR_CREATE || change == KVM_MR_DELETE) && + !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); + kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); } kvm_mmu_slot_apply_flags(kvm, old, new, change);