Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp1082674pxb; Fri, 13 Aug 2021 13:06:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsoAnpm1b+KdUlTK6c+cUDTa7Y1pLcFLIi+fLtpikYg9zypWsV/gVHKouPK5YW0HRIwnU7 X-Received: by 2002:a17:907:1b22:: with SMTP id mp34mr4247746ejc.408.1628885218237; Fri, 13 Aug 2021 13:06:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628885218; cv=none; d=google.com; s=arc-20160816; b=yN0qVdWNVyZxWkw4/v+o4n5TNNe+C/+WKKuEJfBvSNp47sby39YgTl/f/9RGBz7JPO S2aLdAuFxuGUIG4R+ashhx6SYEAw6WrQzXvjhxjmyz687fVbMauAoE16RIHzB5elt+Hb cUZSibtS7kvGagwDySaqthy+cszdRLSYw+goBwRhayh2JEE+AhtGA5I2SF9vUVCOc1vx rUjmPyqqmhKaBeGP5AAaoajjOL67KpC9yPOVEuF8dZQVaG6oaHgaxm6eKnurFpEA/HBI PbpFK/5UsLQxP4y7ngOa1CAT44nZsEYiOfWHxptyDDZzuMIRwye2Eu+O26yxy7m4Lj7i wDHA== 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=D9KTpadl0bASnQ94B7nxeSzZYO9dCeIOYnKDC4uFe3Y=; b=QZxATKcxywf81a4T+na4JJHQfS5cUL/tXGAOI64hTiKU+2TaeA/yxTcAFETRQbnkik 8/0CnYcZ/COAEEt/ACQP3wPee49XsoT9lEEr7cQBqyT/QteR7o5jak4R13gKIcWOkVsX FHIhHDDEDJhC1/IqND9ZGIb9/QEgDGjhDXfXhoP+mJ+b+Zcm3/H7jT1nFv+qFJkwxFkf gPHlFIHjbWVUCcAgbDBB0BuJE7A3M3r2vm0lqUW6OXLjxOqr12SOi1RE0MqgSvDzqNbV GJZs1dVseL+nRfCv4XxWidURVb/nIGWdsGlMsFLtjpbL8F5N3yMM1l3L3CdvnuNZqGXj tFOA== 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 kt23si2462370ejb.749.2021.08.13.13.06.34; Fri, 13 Aug 2021 13:06:58 -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 S234308AbhHMUB4 (ORCPT + 99 others); Fri, 13 Aug 2021 16:01:56 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:50338 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234190AbhHMUBz (ORCPT ); Fri, 13 Aug 2021 16:01:55 -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 1mEcwD-00041q-8d; Fri, 13 Aug 2021 21:33:53 +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 v4 04/13] KVM: x86: Move n_memslots_pages recalc to kvm_arch_prepare_memory_region() Date: Fri, 13 Aug 2021 21:33:17 +0200 Message-Id: X-Mailer: git-send-email 2.32.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 | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2ab0de7483ef..f39bf3c3a054 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11490,9 +11490,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; } @@ -11589,22 +11603,15 @@ 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) { - unsigned long 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) { + unsigned long nr_mmu_pages; - nr_mmu_pages = kvm->arch.n_memslots_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); - } + nr_mmu_pages = kvm->arch.n_memslots_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_slot_apply_flags(kvm, old, new, change);