Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2428449pxb; Tue, 9 Mar 2021 02:13:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJyW/xJwdTGLICPV2YHPT7NPhdCjCizn4Kau7czPjMvdMVoZz7wcyTga40owF5EygxITvEae X-Received: by 2002:a17:906:bccc:: with SMTP id lw12mr19231796ejb.268.1615284794524; Tue, 09 Mar 2021 02:13:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615284794; cv=none; d=google.com; s=arc-20160816; b=myLEDc+p4sXjtLwAflWhkARHS85yYXDXuHjOTrAJoUQQxPYWzT+hk+Ultj7uZbzPMm vK/7NQflopgU1Cr0BDe96SFyV/HvyfT2dwQ2xh6gb4DwThRZiWCn70XMhyV6p+X6OfbP yl5QN2sT64VononMAbXAvHkSSSooyEDtEF+6b5RjruUMNUrXqCa/AHo+hQhVWvAa9De3 FoMoxscF+ifoqFy+Zs2n8gb5a3TErZ5c+pXYkx3YHtrMsHQOKmFmJmXY0/V7zWS/6XXv scJUw8EuPEpsb/5u7hs/iHzPMywFtJn0/4CiW7VdrOHwkOG+AmzYWQb9BCFkWwiYvCSh lZmg== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=E7+PqvKTFHdaKCmM0AiAKUNzRu+ZOxj1wno09Cr/Az0=; b=iQ/ktQKseJo77KI5iGaYlJe9JgQ8s90JAAaPS9shb+qLIhIx/S7tdfZ5j9psgl+vB+ 6XINsEzIOi6RQ2oHL+rUBnsIlECB3amz/nEM+Cwb+sksndbpHGXnYhqP52WUDxpA/YKk TeH14mQHmxW3QDEp+ebVzfO9riSduWSFC2cp6GxMD92AIL4l86ISw8aE+sg4hRI1fr6m NqwV8HAJImvITl+a2+cQEjH9HxQCc+Ae25L1xEBPX5xDoB1Ai3KH5x4DRvVq5P2YoxVM RPCnJRAQNlxHczzjdzlIKbi35ZaMmwHz8Cu21XqETiNWWMtp1JVUGWLyKc0A1Ezf235e gxpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="EafcPI/o"; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y7si8909708ejd.159.2021.03.09.02.12.52; Tue, 09 Mar 2021 02:13:14 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b="EafcPI/o"; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230319AbhCIKKF (ORCPT + 99 others); Tue, 9 Mar 2021 05:10:05 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:28308 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230084AbhCIKJu (ORCPT ); Tue, 9 Mar 2021 05:09:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615284589; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E7+PqvKTFHdaKCmM0AiAKUNzRu+ZOxj1wno09Cr/Az0=; b=EafcPI/op90kkAEWnqO5qUK04T8DRdXfLZ25Mn7dzuykRSN8rxl8l4giBTO60DRkhZ9A8t 3dVs/nOTSD2QP8OXYdsb5Em4/pgZzRxOCmzLmrFR7Jv9SAwaUL9ZvPf8PPyn9M52zN1yHO E/TsT002iY851k0NSnmttVmJXrVK8K8= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-544-BGkCS5InMzyjBBKcXi9jAA-1; Tue, 09 Mar 2021 05:09:47 -0500 X-MC-Unique: BGkCS5InMzyjBBKcXi9jAA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 59DB11005D45; Tue, 9 Mar 2021 10:09:46 +0000 (UTC) Received: from starship (unknown [10.35.206.156]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB0935D6D7; Tue, 9 Mar 2021 10:09:43 +0000 (UTC) Message-ID: <785c17c307e66b9d7b422cc577499d284cfb6e7b.camel@redhat.com> Subject: Re: [PATCH 2/2] KVM: x86/mmu: Exclude the MMU_PRESENT bit from MMIO SPTE's generation From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tom Lendacky Date: Tue, 09 Mar 2021 12:09:41 +0200 In-Reply-To: <20210309021900.1001843-3-seanjc@google.com> References: <20210309021900.1001843-1-seanjc@google.com> <20210309021900.1001843-3-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2021-03-08 at 18:19 -0800, Sean Christopherson wrote: > Drop bit 11, used for the MMU_PRESENT flag, from the set of bits used to > store the generation number in MMIO SPTEs. MMIO SPTEs with bit 11 set, > which occurs when userspace creates 128+ memslots in an address space, > get false positives for is_shadow_present_spte(), which lead to a variety > of fireworks, crashes KVM, and likely hangs the host kernel. > > Fixes: b14e28f37e9b ("KVM: x86/mmu: Use a dedicated bit to track shadow/MMU-present SPTEs") > Reported-by: Tom Lendacky > Reported-by: Paolo Bonzini > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/mmu/spte.h | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h > index b53036d9ddf3..bca0ba11cccf 100644 > --- a/arch/x86/kvm/mmu/spte.h > +++ b/arch/x86/kvm/mmu/spte.h > @@ -101,11 +101,11 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK)); > #undef SHADOW_ACC_TRACK_SAVED_MASK > > /* > - * Due to limited space in PTEs, the MMIO generation is a 20 bit subset of > + * Due to limited space in PTEs, the MMIO generation is a 19 bit subset of > * the memslots generation and is derived as follows: > * > - * Bits 0-8 of the MMIO generation are propagated to spte bits 3-11 > - * Bits 9-19 of the MMIO generation are propagated to spte bits 52-62 > + * Bits 0-7 of the MMIO generation are propagated to spte bits 3-10 > + * Bits 8-18 of the MMIO generation are propagated to spte bits 52-62 > * > * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in > * the MMIO generation number, as doing so would require stealing a bit from > @@ -116,7 +116,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK)); > */ > > #define MMIO_SPTE_GEN_LOW_START 3 > -#define MMIO_SPTE_GEN_LOW_END 11 > +#define MMIO_SPTE_GEN_LOW_END 10 > > #define MMIO_SPTE_GEN_HIGH_START 52 > #define MMIO_SPTE_GEN_HIGH_END 62 > @@ -125,12 +125,14 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK)); > MMIO_SPTE_GEN_LOW_START) > #define MMIO_SPTE_GEN_HIGH_MASK GENMASK_ULL(MMIO_SPTE_GEN_HIGH_END, \ > MMIO_SPTE_GEN_HIGH_START) > +static_assert(!(SPTE_MMU_PRESENT_MASK & > + (MMIO_SPTE_GEN_LOW_MASK | MMIO_SPTE_GEN_HIGH_MASK))); > > #define MMIO_SPTE_GEN_LOW_BITS (MMIO_SPTE_GEN_LOW_END - MMIO_SPTE_GEN_LOW_START + 1) > #define MMIO_SPTE_GEN_HIGH_BITS (MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1) > > /* remember to adjust the comment above as well if you change these */ > -static_assert(MMIO_SPTE_GEN_LOW_BITS == 9 && MMIO_SPTE_GEN_HIGH_BITS == 11); > +static_assert(MMIO_SPTE_GEN_LOW_BITS == 8 && MMIO_SPTE_GEN_HIGH_BITS == 11); > > #define MMIO_SPTE_GEN_LOW_SHIFT (MMIO_SPTE_GEN_LOW_START - 0) > #define MMIO_SPTE_GEN_HIGH_SHIFT (MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS) I bisected this and I reached the same conclusion that bit 11 has to be removed from mmio generation mask. Reviewed-by: Maxim Levitsky I do wonder, why do we need 19 (and now 18 bits) for the mmio generation: What happens if mmio generation overflows (e.g if userspace keeps on updating the memslots)? In theory if we have a SPTE with a stale generation, it can became valid, no? I think that we should in the case of the overflow zap all mmio sptes. What do you think? Best regards, Maxim Levitsky