From patchwork Mon Jun 5 14:08:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 103281 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2713480vqr; Mon, 5 Jun 2023 07:10:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ78S/gmKxv3DjkE4qePBpnNere4eEkFTMsRzZP/iMlb+vrMTmzJUK0S0N876c2hDOQyDO5a X-Received: by 2002:a17:90a:d244:b0:253:283e:be53 with SMTP id o4-20020a17090ad24400b00253283ebe53mr3561161pjw.42.1685974256532; Mon, 05 Jun 2023 07:10:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685974256; cv=none; d=google.com; s=arc-20160816; b=qPvDuyzOTueUqGjo8mhDqQuH6asinwHWvBqzjuzTlWDxiyXxAiW8/VRQ+jHtAeAEJx PWuLrl9HEqtCUOUjFr1ZoBO1MVm4OSNwQ/yLXteOdj2A46elV5FxfjzdJYFYRt5drKzm 5G+OLa8X9fiB0T4YkyuaGh/Gfap1HhGJbBJDRnLumoE8KQjZyDjyji6SuPHdNfd1FaOc NxGOKzXfz4JpU0cHyzeNDvrg1hbQddFVLxhjDTdpGd0kANW0rxJ0ci2L4sJaK9OakpUr 1sbL5S8Z8XZtB/MWDJeBhAvaFKYwRHLV27kgYk1hkS7vCpIwKRIF3y0MfjFuIKE354ie MyIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=Jjr2ZcMBypnD5EDokqM52pjUqBzKKRIB3j2hdweyNvM=; b=jiYYYveoEHcmLC8sENIbgNSS2pMOpFtwNYtsHIuU7McxPvXPD1gn+8n14oBrgWjuWK yzRu9lInrv6Zk0RQXH1Jgc0rTvwk/bS1xnVbJOHHC+8dW7y0glQJg+i23JGeOCmql/G1 nG3eQS2oRU3DZFXP6w6gPL2burNprauu67jjZAYB14zlPGzAIG8Z8HaOFwIYlKbVpEKP 7zk7A8g/HqlkAj+he+Xa6wsOGV7EZ/0cuQvH5OcLNSVTld9myAxs3piBVNZ89ySac4JT 1DkDtPCa1HGakHMf97P+23Gj2vJWNBN8QqHcfsKgmwD9KLmGCogMks/Jl4zgLOE8DJqi Kurw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=RkCFuQjx; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=lc6zQE+q; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nh2-20020a17090b364200b002562c9b3e2dsi7140075pjb.96.2023.06.05.07.10.40; Mon, 05 Jun 2023 07:10:56 -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=@linutronix.de header.s=2020 header.b=RkCFuQjx; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=lc6zQE+q; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232973AbjFEOJ2 (ORCPT + 99 others); Mon, 5 Jun 2023 10:09:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231432AbjFEOIf (ORCPT ); Mon, 5 Jun 2023 10:08:35 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1D7DE51; Mon, 5 Jun 2023 07:08:18 -0700 (PDT) Date: Mon, 05 Jun 2023 14:08:16 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1685974097; h=from:from:sender:sender:reply-to: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=Jjr2ZcMBypnD5EDokqM52pjUqBzKKRIB3j2hdweyNvM=; b=RkCFuQjxCmlaT5PRef2YE3Z/tVbblUnvWJpiUk6vXER8erEmP7cjj3sc18uvyGlF+6Rea0 MzhzjO0FLFxbCeUJq4zH1zWqdnJ7Kz0XlS8qqcf7ZwH/x6l9r/HGid5ZlXRPbzuMPugzes smu4ajG7twrHLQ6EeZEx3AVhLEnx/8VCZLgHwf0JnDVmf741GFbyE7Ed+ByENNNU3gXLgw awC/Mfh0VwFOGiSaoaw1FSxyJsPLDelFCCgHfIbkCmNV1MnAkb8KOPP6GXdvsCbeb/jp52 zIOKa+Ia5qpi/pwu+F3yVEks0XPhZ/MbgtaknLx9Bv+RewHNLJkZ6LopRSJXuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1685974097; h=from:from:sender:sender:reply-to: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=Jjr2ZcMBypnD5EDokqM52pjUqBzKKRIB3j2hdweyNvM=; b=lc6zQE+qPeqIeQjtuUqjTiOMoP46YV/JgKpKMaufhw3IKEpJ8XQfpBJK2PZwM3MG/UfhcG fEBkAGviaKIql6CA== From: "tip-bot2 for Juergen Gross" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/mtrr] x86/xen: Set MTRR state when running as Xen PV initial domain Cc: Juergen Gross , "Borislav Petkov (AMD)" , Boris Ostrovsky , Michael Kelley , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230502120931.20719-6-jgross@suse.com> References: <20230502120931.20719-6-jgross@suse.com> MIME-Version: 1.0 Message-ID: <168597409652.404.16424399109459449233.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767872141929881744?= X-GMAIL-MSGID: =?utf-8?q?1767872141929881744?= The following commit has been merged into the x86/mtrr branch of tip: Commit-ID: a153f254e5cdf8fa3a1df90a6ffed3063fede154 Gitweb: https://git.kernel.org/tip/a153f254e5cdf8fa3a1df90a6ffed3063fede154 Author: Juergen Gross AuthorDate: Tue, 02 May 2023 14:09:20 +02:00 Committer: Borislav Petkov (AMD) CommitterDate: Thu, 01 Jun 2023 15:04:32 +02:00 x86/xen: Set MTRR state when running as Xen PV initial domain When running as Xen PV initial domain (aka dom0), MTRRs are disabled by the hypervisor, but the system should nevertheless use correct cache memory types. This has always kind of worked, as disabled MTRRs resulted in disabled PAT, too, so that the kernel avoided code paths resulting in inconsistencies. This bypassed all of the sanity checks the kernel is doing with enabled MTRRs in order to avoid memory mappings with conflicting memory types. This has been changed recently, leading to PAT being accepted to be enabled, while MTRRs stayed disabled. The result is that mtrr_type_lookup() no longer is accepting all memory type requests, but started to return WB even if UC- was requested. This led to driver failures during initialization of some devices. In reality MTRRs are still in effect, but they are under complete control of the Xen hypervisor. It is possible, however, to retrieve the MTRR settings from the hypervisor. In order to fix those problems, overwrite the MTRR state via mtrr_overwrite_state() with the MTRR data from the hypervisor, if the system is running as a Xen dom0. Fixes: 72cbc8f04fe2 ("x86/PAT: Have pat_enabled() properly reflect state when running on Xen") Signed-off-by: Juergen Gross Signed-off-by: Borislav Petkov (AMD) Reviewed-by: Boris Ostrovsky Tested-by: Michael Kelley Link: https://lore.kernel.org/r/20230502120931.20719-6-jgross@suse.com Signed-off-by: Borislav Petkov (AMD) --- arch/x86/xen/enlighten_pv.c | 52 ++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+) diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index 093b78c..8732b85 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -68,6 +68,7 @@ #include #include #include +#include #include #include #include @@ -119,6 +120,54 @@ static int __init parse_xen_msr_safe(char *str) } early_param("xen_msr_safe", parse_xen_msr_safe); +/* Get MTRR settings from Xen and put them into mtrr_state. */ +static void __init xen_set_mtrr_data(void) +{ +#ifdef CONFIG_MTRR + struct xen_platform_op op = { + .cmd = XENPF_read_memtype, + .interface_version = XENPF_INTERFACE_VERSION, + }; + unsigned int reg; + unsigned long mask; + uint32_t eax, width; + static struct mtrr_var_range var[MTRR_MAX_VAR_RANGES] __initdata; + + /* Get physical address width (only 64-bit cpus supported). */ + width = 36; + eax = cpuid_eax(0x80000000); + if ((eax >> 16) == 0x8000 && eax >= 0x80000008) { + eax = cpuid_eax(0x80000008); + width = eax & 0xff; + } + + for (reg = 0; reg < MTRR_MAX_VAR_RANGES; reg++) { + op.u.read_memtype.reg = reg; + if (HYPERVISOR_platform_op(&op)) + break; + + /* + * Only called in dom0, which has all RAM PFNs mapped at + * RAM MFNs, and all PCI space etc. is identity mapped. + * This means we can treat MFN == PFN regarding MTRR settings. + */ + var[reg].base_lo = op.u.read_memtype.type; + var[reg].base_lo |= op.u.read_memtype.mfn << PAGE_SHIFT; + var[reg].base_hi = op.u.read_memtype.mfn >> (32 - PAGE_SHIFT); + mask = ~((op.u.read_memtype.nr_mfns << PAGE_SHIFT) - 1); + mask &= (1UL << width) - 1; + if (mask) + mask |= MTRR_PHYSMASK_V; + var[reg].mask_lo = mask; + var[reg].mask_hi = mask >> 32; + } + + /* Only overwrite MTRR state if any MTRR could be got from Xen. */ + if (reg) + mtrr_overwrite_state(var, reg, MTRR_TYPE_UNCACHABLE); +#endif +} + static void __init xen_pv_init_platform(void) { /* PV guests can't operate virtio devices without grants. */ @@ -135,6 +184,9 @@ static void __init xen_pv_init_platform(void) /* pvclock is in shared info area */ xen_init_time_ops(); + + if (xen_initial_domain()) + xen_set_mtrr_data(); } static void __init xen_pv_guest_late_init(void)