Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5397687rwl; Tue, 21 Mar 2023 18:36:39 -0700 (PDT) X-Google-Smtp-Source: AK7set8OLMeIgS6SdbK+r9zUfkb4+xlj2P7sMQ3zO97+pphB6oKQRst1iLp5mp7Cbk87LSqHlVeY X-Received: by 2002:a17:906:7485:b0:8b1:2614:dea6 with SMTP id e5-20020a170906748500b008b12614dea6mr4786041ejl.1.1679448998945; Tue, 21 Mar 2023 18:36:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679448998; cv=none; d=google.com; s=arc-20160816; b=v+kLh5lqvHoxg7cVkpgwGneQ82ktD9nyqOIDEjUr2SE2YFv8YoP55/A3FVlK7nj629 apBKXxECDMxZtDvsRAaT33qmEotaefUKgsY1haxFRUB6xKXBNux8LbsQLa+a2dFk+Kr2 oQZ3E0RVRXZTqI35GTfbJjcv+2VvD43PFKMJ9vKFHwaHjpdU+kf9ka+182U9Fqdyj+Qi Cxyr9HGlGCZjZ3/bTbIHMx/yXWQsDhYqz4v9eu56bYFSPbOJb/qSAUFh9p3im2IkSjX+ Xr8p2FXy3G75XN3WI/6VT7jfthkgxKjbN1tPWVOhet746tr1uQZv6ISpzCDFRC0UJHb6 k9Og== 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 :dkim-signature; bh=xKfWyPdWdSRYOzolgK9AgL1vcMAf7TEF7SLZEQ3lWXY=; b=bvQWk4ETmLT/b4jAVVmy6JNRmtrmlTEiL9FK3AsCYtAqsRHC5xES2kxVnkkieyutF+ wVEvqNQMyV58dh2Qg/4Rk35Sa/DJk1/ernus7o2EV5xEwGEk4Nucb/AH+yoSoOS8ixuy 9JXcviFZYGdCTySs0lYLjzAxAnDdVJ0V1A4Owc5+B6UEMNjL83udsCHgZkKCR+hAVY/o ohkVYmww1nIfOtaKpmEz2lXz1tI5ZQKDxYZCoFyL+ZiPMPyB42slyjbT2P/UgTTd7MxN QyhrNck9EKu8yrsEjYe63YAgBsJXfSjD1cs2zuB3gLU70pIpUQDryDhwL/NTZZ6K4w/W zezA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@grsecurity.net header.s=grsec header.b=n43NCSzm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=grsecurity.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c12-20020a170906340c00b008b17e6556c3si13879817ejb.587.2023.03.21.18.36.14; Tue, 21 Mar 2023 18:36:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@grsecurity.net header.s=grsec header.b=n43NCSzm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=grsecurity.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229999AbjCVBel (ORCPT + 99 others); Tue, 21 Mar 2023 21:34:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229672AbjCVBec (ORCPT ); Tue, 21 Mar 2023 21:34:32 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47189521CC for ; Tue, 21 Mar 2023 18:34:31 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id ek18so66973975edb.6 for ; Tue, 21 Mar 2023 18:34:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; t=1679448870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xKfWyPdWdSRYOzolgK9AgL1vcMAf7TEF7SLZEQ3lWXY=; b=n43NCSzmVBHNDH1pvKVD3UejAZKe7GdCOXtD9IjVu53D9AoBVfyWszbm25SGWXrMBS gNNQEyrbYCPzPMTSdK3/fNXpCSlYogOcsWkp8CT5gcecpkllLzJdxGIeu98pdRU5HKrC M+pQV9qrY1LrzOODh4Kc58Y+wAjPdbJv+hRZliV/jG1zZxu2Habt3ZK0imwGGehhMvek d7/HAhPiChwz9xz/oH7WsEqxKJe0DIMxoTWy3q8BjyvjmauMYwkigyDGHnOtChcSVJZs 2tIdy1Psh9ZIDTD8ESiix4IYNQj20L/N6gERJWKcWfazGuwHX0KnIgw+CnQG2OJq1VmM /Grg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679448870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xKfWyPdWdSRYOzolgK9AgL1vcMAf7TEF7SLZEQ3lWXY=; b=WzAI6u6HMHJbkzcVk43fQzZKxTOykKAARwblvBuWD+S7yMt20B5cWV+NZl8XKLvS7W h1Eo1TS9TbTFLl6g1NgmD69ezWiKN5yyF6RMFv2AFvE0SRrum3/V2Ad/Nm480L5pZzBX k9z1zpp94fAq2/dJc055KGRdOH6NV4VuzMlhYkaXVo5Byls8mJz0UoaKTQWJQ285lvGy PptQYlhKaFBiHZYSfzB2sJlncgv0X6yuVpyK0SQs7B2x/jiJiMY7LzcDMB3bf8AO9Ooe BV2O2O/XQMJ/fVPB+qqMkXo4JXW6MKB+6FDTAppGhqcuCgwio9mEK2aFVbl1jXmBzs54 CDrw== X-Gm-Message-State: AO0yUKWdxdb4loTTg0qdfKDWBQQ3E85CeQIXOEqq76CoOlFDLXrTolrC LajjSMuI9nvxvZZNE49YgjWx1A== X-Received: by 2002:a17:906:b084:b0:931:75f5:36db with SMTP id x4-20020a170906b08400b0093175f536dbmr5007473ejy.11.1679448869752; Tue, 21 Mar 2023 18:34:29 -0700 (PDT) Received: from nuc.fritz.box (p200300f6af344e007f5e5982a136b54e.dip0.t-ipconnect.de. [2003:f6:af34:4e00:7f5e:5982:a136:b54e]) by smtp.gmail.com with ESMTPSA id w11-20020a1709067c8b00b009231714b3d4sm6356260ejo.151.2023.03.21.18.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Mar 2023 18:34:29 -0700 (PDT) From: Mathias Krause To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Sean Christopherson , Paolo Bonzini , Mathias Krause Subject: [PATCH v4 3/6] KVM: x86: Ignore CR0.WP toggles in non-paging mode Date: Wed, 22 Mar 2023 02:37:28 +0100 Message-Id: <20230322013731.102955-4-minipli@grsecurity.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230322013731.102955-1-minipli@grsecurity.net> References: <20230322013731.102955-1-minipli@grsecurity.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If paging is disabled, there are no permission bits to emulate. Micro-optimize this case to avoid unnecessary work. Suggested-and-co-developed-by: Sean Christopherson Signed-off-by: Mathias Krause --- arch/x86/kvm/x86.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c6d909778b2c..8a66ac7a4878 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -908,14 +908,20 @@ void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned lon { /* * CR0.WP is incorporated into the MMU role, but only for non-nested, - * indirect shadow MMUs. If TDP is enabled, the MMU's metadata needs - * to be updated, e.g. so that emulating guest translations does the - * right thing, but there's no need to unload the root as CR0.WP - * doesn't affect SPTEs. + * indirect shadow MMUs. If paging is disabled, no updates are needed + * as there are no permission bits to emulate. If TDP is enabled, the + * MMU's metadata needs to be updated, e.g. so that emulating guest + * translations does the right thing, but there's no need to unload the + * root as CR0.WP doesn't affect SPTEs. */ - if (tdp_enabled && (cr0 ^ old_cr0) == X86_CR0_WP) { - kvm_init_mmu(vcpu); - return; + if ((cr0 ^ old_cr0) == X86_CR0_WP) { + if (!(cr0 & X86_CR0_PG)) + return; + + if (tdp_enabled) { + kvm_init_mmu(vcpu); + return; + } } if ((cr0 ^ old_cr0) & X86_CR0_PG) { -- 2.39.2