Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3484274pxu; Tue, 8 Dec 2020 13:20:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPMB6zGYOoCPDaFxGaIxtjQAj7YYEF4iiyWL0f6l9mgVRn2kKSll1x1hQH43fJbgJuMiAP X-Received: by 2002:a05:6402:404:: with SMTP id q4mr9239630edv.295.1607462410944; Tue, 08 Dec 2020 13:20:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607462410; cv=none; d=google.com; s=arc-20160816; b=SNU2V9BuduDaITHJTO8wTPi3P3c8TfX6a9BCa5CTQr3RV4gl0oMKvz8ReHxj/5jiMS VrfNNvB4ESa6TpLUUW0gWWeAqetx39NxsvkD9JLM3uMsprfXy1rJR2nXbMwFX6mIR4P2 W4pUXKS+e/pxe2xCMIndlaZNy+6eUvcwuqLY60GtUAxtY5emLL/9OGQROThuOE/qV82u pK3OVSTyxFT4FAxOmBCamK2NhdED+KDTd6RdpFPgLrpWCYcj5bbLiJuYH19jRIqIGLb5 ID1/vRrs90NKKKn28YXH6DF78WDpl+5TY2f/DugZwldFUBJ1UXZMRAk9cTcCXyzFXtfh qfYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=O9P7Yx8VFzWCCAYZubuM8Kn2akF3BiN8tXjMtys17+Y=; b=NMvNFuwH944jsKI9I7uBcWcpryIyKjOqDJq0BRZsEOQgNKnnjWzKlw/8ozyM+Z8n9F rxgsDqd5q6QadaqIWfJGsztHw51Ystasg/xXNioYFZ5oiwNjHmwF7AOeSkwf5iRVknvU BKKlVUC40xEyxBfRJs+R7dQPtXtH3sT8jljkVMCQu0ykzVrKwGfVOUDzLZHTMQscFfOz N7xPnW/UQpONqF9UYdsHl8M4Ix9kNjSHvoAyFY5AudfZwTB5iUCInBMBs3FTri3796o5 aQrCyX5kCwkVhSfVE3AbphAHamGcPXfjn7Adn2932jhfF+hGftOgIiVbIok64pOmRKbG Sujw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="L5cS/IQR"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y22si5148970ejc.25.2020.12.08.13.19.45; Tue, 08 Dec 2020 13:20:10 -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=@google.com header.s=20161025 header.b="L5cS/IQR"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728606AbgLHVNr (ORCPT + 99 others); Tue, 8 Dec 2020 16:13:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727998AbgLHVNr (ORCPT ); Tue, 8 Dec 2020 16:13:47 -0500 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53FC1C061793 for ; Tue, 8 Dec 2020 13:13:07 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id w6so15132622pfu.1 for ; Tue, 08 Dec 2020 13:13:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=O9P7Yx8VFzWCCAYZubuM8Kn2akF3BiN8tXjMtys17+Y=; b=L5cS/IQRJsFrNE1wrnlNkM2ax4xafyjL+6GZOGgovgRJBSpvz0hAYrBACzsPeS+Oe1 G+lrmZaSukgwT1/7j8rHbSsWhYOQKfZMOLW5X+orEKQebaSPLNbZ90tJ7dcY8uabz+oM GVFBb3UPecdyX2mm3bPruW0W1wTqUaR+KaMVXJ1EdzldtzAQGhw1Zx6zioznTtzqLwNF ef1pnyDhD8EhyXZQhKynYBdZmx2mTBS71Zbyyz/uSIETyOTBUpce4bDId9MjFr1NrkVA ON5lAohMLdtifRwHLF3jJlee6nRyHc6J0jng9qRhWSk+tklg8Pxmftm4LwEu9TLd603p DwNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=O9P7Yx8VFzWCCAYZubuM8Kn2akF3BiN8tXjMtys17+Y=; b=LSh59dGUDkh0PreW+dTXO65lrQ+RBPwgVyTS4ZC342HHDNF6QWKN3Pe54wM1y0ZZyn 5L7txZkF8QhxIr1XCRy4T9CVIIObrNGQCusxkccNC5u00xD6gURCLvBzb0X2fz/jHxPd cWt++dNNRye+clTzeTLKhRxaQmfywf9fpHBtWaEYQLvG3/XLAZm5cvqnf0jhDfQxJnP8 K4I8BKZGwwaN23jSNI2HqNzCKutrVaBdwcq2qPwhaB+pi9Z+5WjR4HfRte+B1s4QuSxY e7FgFoDaMCmauqIY+QR2kqXovWTpmFUpiZfnUl8BZT1ZNCZECRHa9avP9Tu5+70WsoFb dpiA== X-Gm-Message-State: AOAM532n7pa8Bu1G0IYIgunSDGBXJMr6Z8fdIcYvaE97hzCxAZNYn/sO XSQ/vDEmQ2ebPPXZBuzkfnkUdA== X-Received: by 2002:a63:4b22:: with SMTP id y34mr340pga.214.1607461986630; Tue, 08 Dec 2020 13:13:06 -0800 (PST) Received: from google.com ([2620:15c:f:10:1ea0:b8ff:fe73:50f5]) by smtp.gmail.com with ESMTPSA id x4sm15739367pgg.94.2020.12.08.13.13.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Dec 2020 13:13:05 -0800 (PST) Date: Tue, 8 Dec 2020 13:12:59 -0800 From: Sean Christopherson To: Paolo Bonzini Cc: "Maciej S. Szmigiero" , Joerg Roedel , Jim Mattson , Wanpeng Li , Vitaly Kuznetsov , Jonathan Corbet , linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: mmu: Fix SPTE encoding of MMIO generation upper half Message-ID: References: <156700708db2a5296c5ed7a8b9ac71f1e9765c85.1607129096.git.maciej.szmigiero@oracle.com> <370db207-7216-ae26-0c33-dab61e0fdaab@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <370db207-7216-ae26-0c33-dab61e0fdaab@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Dec 06, 2020, Paolo Bonzini wrote: > On 05/12/20 01:48, Maciej S. Szmigiero wrote: > > From: "Maciej S. Szmigiero" > > > > Commit cae7ed3c2cb0 ("KVM: x86: Refactor the MMIO SPTE generation handling") > > cleaned up the computation of MMIO generation SPTE masks, however it > > introduced a bug how the upper part was encoded: > > SPTE bits 52-61 were supposed to contain bits 10-19 of the current > > generation number, however a missing shift encoded bits 1-10 there instead > > (mostly duplicating the lower part of the encoded generation number that > > then consisted of bits 1-9). > > > > In the meantime, the upper part was shrunk by one bit and moved by > > subsequent commits to become an upper half of the encoded generation number > > (bits 9-17 of bits 0-17 encoded in a SPTE). > > > > In addition to the above, commit 56871d444bc4 ("KVM: x86: fix overlap between SPTE_MMIO_MASK and generation") > > has changed the SPTE bit range assigned to encode the generation number and > > the total number of bits encoded but did not update them in the comment > > attached to their defines, nor in the KVM MMU doc. > > Let's do it here, too, since it is too trivial thing to warrant a separate > > commit. > > > > Fixes: cae7ed3c2cb0 ("KVM: x86: Refactor the MMIO SPTE generation handling") > > Signed-off-by: Maciej S. Szmigiero > > --- > > > Good catch. Indeed! I hate this code... :-) > What do you think about this alternative definition? It computes everything > from the bit ranges. This has my vote, I was going to suggest something similar for the shifts to minimize the magic. > #define MMIO_SPTE_GEN_LOW_START 3 > #define MMIO_SPTE_GEN_LOW_END 11 > > #define MMIO_SPTE_GEN_HIGH_START PT64_SECOND_AVAIL_BITS_SHIFT > #define MMIO_SPTE_GEN_HIGH_END 62 > > #define MMIO_SPTE_GEN_LOW_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_END, MMIO_SPTE_GEN_LOW_START) > #define MMIO_SPTE_GEN_HIGH_MASK GENMASK_ULL(MMIO_SPTE_GEN_HIGH_END, MMIO_SPTE_GEN_HIGH_START) > > #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) > > #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) > > #define MMIO_SPTE_GEN_MASK GENMASK_ULL(MMIO_SPTE_GEN_LOW_BITS + MMIO_SPTE_GEN_HIGH_BITS - 1, 0) What if we leave MMIO_SPTE_GEN_MASK as is, GENMASK_ULL(17, 0), and instead add a BUILD_BUG_ON() to assert that it matches the above logic? It's really easy to get lost when reading through the chain of defines, I find the explicit mask helps provide an anchor/reference for understand what's going on. It'll require an update if/when PT64_SECOND_AVAIL_BITS_SHIFT, but that's not necessarily a bad thing, e.g. the comment above this block will also be stale.