Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1691341pxj; Wed, 19 May 2021 11:34:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztzx2Ooe7Vrj1ZUsRLQKSnfvfvOChKI8VwaJASF1kluJZIv4JeyUAOliOF2snSiP6sswVC X-Received: by 2002:a17:906:dc4c:: with SMTP id yz12mr507937ejb.364.1621449243978; Wed, 19 May 2021 11:34:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621449243; cv=none; d=google.com; s=arc-20160816; b=cDpMUB7SZOpX5sSYluEoqU1G/IZjswgGE1WUBtMq9ZkaJudCZAgVcmIB9scYl81m+g oijHXCnVJHRB9/27j91a2W0mZtzPa0qn2ZhoBePepYyJpEqDTvFsHOYEBg9jypgmfoAZ ++w/kL4rO2KafXksUggVeJSA5Lws5i2vTgz9OwsQSSehh5zKxPqSq8S/bt6qmdFjJ7QO xM7ByZAoU02cWrLh75ztGWdPZLrL+c9iN1S4DV2WSDBJVerAPhqZhSvAOMRwXQ76zSp5 evUHh9UJuTTaIdqKXZoHSVKObeFi3EVDxmg6r8Yoz9xPGUxWhtAOowOBZ3nQCIEOVol+ 6MoA== 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=yfdC9MEFNEaHgaQGSz7w95epIWVLZWnSDFN+a7r21Fc=; b=oYI4BDhbDfQAYLqb7DVk4Alu5sdc7guZcbodb45iv8V9hKFlHyMePvX9uEIaxDCzdl 45emIaDCYvYx4g5PLLLyfYX5CD57aFyZiH4UxNUfPcqhiIjWA2b64TuI61pqyCLVnZj0 zoGBabtLgEDOW278IAD+6TTVV1HBVDGCuFg8a2kiufTVzXcYxeKBicYWvWi/rcCj5ZOq 3+DE1DZIJV7YSnZU1iD9+C9/CN19/PQS1OcVwd7uPAmCYTC2NmRWLoaAsiSp5gI+eh9V 8+zek97meLKMN2ak3qCN+8yzkOcv6hw+Y0ESBkoX966qw8okgQ0uA4estbtd/dxntfb3 DGOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=G624B0FU; 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 qp22si364721ejb.190.2021.05.19.11.33.39; Wed, 19 May 2021 11:34:03 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=G624B0FU; 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 S236311AbhERUZF (ORCPT + 99 others); Tue, 18 May 2021 16:25:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236196AbhERUZF (ORCPT ); Tue, 18 May 2021 16:25:05 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F11C4C061573 for ; Tue, 18 May 2021 13:23:46 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id b7so1600884plg.0 for ; Tue, 18 May 2021 13:23:46 -0700 (PDT) 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=yfdC9MEFNEaHgaQGSz7w95epIWVLZWnSDFN+a7r21Fc=; b=G624B0FUpxD2VDJtkGQ6d5UsyqFf+SBfNBxc1MwrEdRGX0Vn+uRdHMutZTSFZY+eSw wpbXB8g0u0hQRR6wVHn7OMK8fuEIM9e8XR+uVxDbWLnjK6VDApDXj34aKfFOdE3qRbzP rR0ieRntsugS3as9T1zXf4lMSCZNwYGcDA1AQNupZhaBplXwtEpdE8nQlSvSBPIU6t5U 4d0Wcf4NYCkaXC2lQePwIQilMuFlylYi7As4oCKqDAG2QiaB6sWH9KZ/QoL6SKEyw1c4 oXx23p2ECBSYar69X+iu+lbtWdp3IVHZ6qsVtORrxGLbMqHpyn/bF35eVkBnmSZNKwNi zbKQ== 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=yfdC9MEFNEaHgaQGSz7w95epIWVLZWnSDFN+a7r21Fc=; b=cuSqbeT2MVir2rS4BbD2/9/3VuVtm78jNT7mgWXJoN3dsg7cWKWVAff6ry/81hMg8F OY3wPAt3npmtWiDlgyI1fQnNQW+/mXMnkTZoIkbh+otIWXOkahBUz9Iq6Z3RbfONub3+ FT2mig/MWV/hRwEavgujabgTwR4ukKX6SdBS3AYEjUNK5036CFKfrxXN/AVKwqGRXZ9u Yr1fFJvi4QTqQxTCgO+NPuevcyKALUI0s0xsC0eO8Wim/mbB+1trcM7WyKSEChZMa0Yq neInF6XmX6N+7MJZipZslYheygxourgFXGjqZ7azJcHoh4VV5CaWct1UREvjCSOtxiyC qZlA== X-Gm-Message-State: AOAM5306eYMR79T4tuIMgkwuCvQ9xToaQi9KnP+B1uJ7idcQulQPAIC6 qeTxsi0k78BB33SsaJlGtkC+Xg== X-Received: by 2002:a17:90a:5d93:: with SMTP id t19mr7294307pji.116.1621369426302; Tue, 18 May 2021 13:23:46 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id y13sm8198630pgp.16.2021.05.18.13.23.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 May 2021 13:23:45 -0700 (PDT) Date: Tue, 18 May 2021 20:23:42 +0000 From: Sean Christopherson To: Reiji Watanabe Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 06/43] KVM: x86: Properly reset MMU context at vCPU RESET/INIT Message-ID: References: <20210424004645.3950558-1-seanjc@google.com> <20210424004645.3950558-7-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 17, 2021, Reiji Watanabe wrote: > > void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) > > { > > + unsigned long old_cr0 = kvm_read_cr0(vcpu); > > + unsigned long old_cr4 = kvm_read_cr4(vcpu); > > + > > kvm_lapic_reset(vcpu, init_event); > > > > vcpu->arch.hflags = 0; > > @@ -10483,6 +10485,10 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) > > vcpu->arch.ia32_xss = 0; > > > > static_call(kvm_x86_vcpu_reset)(vcpu, init_event); > > + > > + if (kvm_cr0_mmu_role_changed(old_cr0, kvm_read_cr0(vcpu)) || > > + kvm_cr4_mmu_role_changed(old_cr4, kvm_read_cr4(vcpu))) > > + kvm_mmu_reset_context(vcpu); > > } > > I'm wondering if kvm_vcpu_reset() should call kvm_mmu_reset_context() > for a change in EFER.NX as well. Oooh. So there _should_ be no need. Paging has to be enabled for EFER.NX to be relevant, and INIT toggles CR0.PG 1=>0 if paging was enabled and so is guaranteed to trigger a context reset. And we do want to skip the context reset, e.g. INIT-SIPI-SIPI when the vCPU has paging disabled should continue using the same MMU. But, kvm_calc_mmu_role_common() neglects to ignore NX if CR0.PG=0, and so the MMU role will be stale if INIT clears EFER.NX without forcing a context reset. However, that's benign from a functionality perspective because the context itself correctly incorporates CR0.PG, it's only the role that's borked. I.e. KVM will fail to reuse a page/context due to the spurious role.nxe, but the permission checks are always be correct. I'll add a comment here and send a patch to fix the role calculation.