Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1787366pxb; Wed, 20 Oct 2021 11:44:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYwwkGPqb4eT20qpTh0a6VNfj+Jalsz8CHXXMD8d5qKuJJH/oMmeOYGYqtRaTGk4bcr2Kx X-Received: by 2002:a17:906:4e4a:: with SMTP id g10mr1243127ejw.524.1634755453199; Wed, 20 Oct 2021 11:44:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634755453; cv=none; d=google.com; s=arc-20160816; b=qg0Xj78LQcmThdmkoBBIQZiRW5PeL5cVezRixvjq2fnhGJ9Kf/x6+6Fw3aGAHBFUWt YnseRAvgWv0mt1ETivsyPjOBJvyz0pjXbsscdOS+J7K9rVXMqDI85hibrKipH/tx5fIN V24RRb9XspVyC/QZ9mG39qM5Ept6dfMUCcMc4HQrY56FSW7jJbKn5q24B2PPF/aEW4e4 AyNOuog07LyxCR+j6U6wwsFS2dpXAiiLI36mJcjcmThCVq5cI1Gn1SKucwPDGMhibJ5K 9Qe6TC6ArQxWaKVkzLz/xjLUmjxuHPyL61gWpDgom8y/hCg9iF6UMpsGGhJdPFm0VKan 5kcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to; bh=MctvyDlM213NR/QnkHn/82OWe8VsCz/OK/n+ZQ5uLnM=; b=R6m3RM8hJDzSIVo96QGIF+o36ftwuTAHpIegtUXnwTNCTfcDfMWD5nh7fjcod7hSjy AT1pC6cG5OtzQ7E1E8tt31Q03IPoTr0FWkBs+yUcYpt04ncqymoXLhuxrL+Bmw32uuSd rzf7KarlA/kF09jpWE3hLzlwkIrsR+TGMXu4l6bNWanu3u8sMGwZpEwSTKvZ2VPnDLOi RdMK+owRYM4QjraHV/PDrCaWYzQuxfSxV9if+BmVGlbXVMxYazQH6oqG5LOxFgw7GD37 b9cfJpXDnxN5W07Tsl5oa0XoW4Jnkjp7bsE8f6Usdl06/FmAKa5CTLRWYjO7EcmcIf2D PvRQ== 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 c31si3760907edf.459.2021.10.20.11.43.48; Wed, 20 Oct 2021 11:44:13 -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 S231354AbhJTSnk (ORCPT + 99 others); Wed, 20 Oct 2021 14:43:40 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:40862 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231352AbhJTSnj (ORCPT ); Wed, 20 Oct 2021 14:43:39 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1mdGWd-0002ei-Ab; Wed, 20 Oct 2021 20:41:19 +0200 To: Sean Christopherson 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 References: <2a4ceee16546deeab7090efea2ee9c0db5444b84.1632171479.git.maciej.szmigiero@oracle.com> From: "Maciej S. Szmigiero" Subject: Re: [PATCH v5 04/13] KVM: x86: Move n_memslots_pages recalc to kvm_arch_prepare_memory_region() Message-ID: <9fc65b5d-a0fe-4ff2-1683-99ce9ec072bf@maciej.szmigiero.name> Date: Wed, 20 Oct 2021 20:41:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 20.10.2021 00:38, Sean Christopherson wrote: > On Mon, Sep 20, 2021, Maciej S. Szmigiero wrote: >> 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; > > This is not worth the churn. In a way, it's worse because userspace can spam > the living snot out of the kernel log by retrying the ioctl(). > > Since underflow can happen if and only if there's a KVM bug, and a pretty bad one > at that, just make the original WARN_ON a KVM_BUG_ON. That will kill the VM and > also provide the WARN_ON_ONCE behavior that we probably want. Will do. Thanks, Maciej