Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4373945imw; Tue, 19 Jul 2022 05:34:51 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u2HRjMEOoYyJabEFduyjkJ9ovCnuV3GFjFJEurq5PtKk1ZZ6VvUupYeTkOzlnq0Dz0kSX1 X-Received: by 2002:a17:907:3e03:b0:722:e694:438 with SMTP id hp3-20020a1709073e0300b00722e6940438mr30703201ejc.755.1658234091695; Tue, 19 Jul 2022 05:34:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658234091; cv=none; d=google.com; s=arc-20160816; b=kRJ9rSk3mXGZTm635rHD1U3MsX4m39oODPdm3VlYSknF4c9LKGzL6G8F1GYI13bXre ruYxhAD6tguA/3nYFkHChBr5DOedwDCppMV7McuR/VAlJsceps4ZpIccDLDee71U2D8y aKQLs4BMPenPzm7Ur9mZNnAJCPeCaTNapI+YnVudJMzHBsWhSBA/jXI/pRc3fAUTudX2 RGbDO7zphGO04ZueARmAMXZQlX6s0ckYg2jiMcjXheS96EZkrikhPLRcP9JLMWFjE4P8 zRMPt2BCJaHVPqsax/ZEwA2pm0S6BZ6YEr2AcgZdDI3I3OLNmmm44N2fZDpaMrilrGjf Do7g== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=hqG5NO7H2uZecNfNIaZtOfOf8YXIOB+JpUwtY8L2S34=; b=cy3h+K3hK1+2ppn/QnF6dEgLpyKeQo74KfU2+NwrDm62qPRtLcUFJQCeZpEU3i7dk6 q01xSMf4Q3HqkO3YMRuYY9wc7BiYK6ADH1KI488htLPSB3hfGBvrfgwmOe8iJNiTs/hq 8WZ0EveoxDJQ6oBgvr5ZskQktJk7GFUeDv7rNvrJo7rQGksq8QaL3HHI6tptaofyfR4s gxhc2cXbWKPTRM+zwIz8yjcAFUeRgqbe/FUlRzIZOCQ5wgAcFy0Iu0rrXXIuBHUN4xZu 1ieB2kZZwmZIVdXmqsHs5mDXTcqqI1Y9Ks6l0UehO766yPK4PLSv4+gpFUOXb5RLYNX2 TMEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=C8oi1tFa; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g8-20020a1709065d0800b0072b4bdab026si16171292ejt.92.2022.07.19.05.34.26; Tue, 19 Jul 2022 05:34:51 -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=@linuxfoundation.org header.s=korg header.b=C8oi1tFa; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239912AbiGSM1h (ORCPT + 99 others); Tue, 19 Jul 2022 08:27:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239687AbiGSM0H (ORCPT ); Tue, 19 Jul 2022 08:26:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1BB42610F; Tue, 19 Jul 2022 05:10:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9FEB561632; Tue, 19 Jul 2022 12:09:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F6A2C341C6; Tue, 19 Jul 2022 12:09:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658232598; bh=B5R6E+ddq8m5/lQLkLjA5+soKj5kmq3p6rjJ4fQbuHU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C8oi1tFaZsfcMzqM8WaIqeb/VSc/xVLltKz1JJ1s/6EB+NaZLF2RKewwMEzutuN6x onrgPhMmDcwi2901krxbBmtgoc2WA64EqBkpxwAO/kw0VanR2bkTqhRC+5gS0NuQ4q 1RH6xlJrOHj3ys0KIVqfuIYYo5ik9no+cFsCdCes= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Juergen Gross , Borislav Petkov Subject: [PATCH 5.10 112/112] x86/pat: Fix x86_has_pat_wp() Date: Tue, 19 Jul 2022 13:54:45 +0200 Message-Id: <20220719114638.149194238@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220719114626.156073229@linuxfoundation.org> References: <20220719114626.156073229@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 From: Juergen Gross commit 230ec83d4299b30c51a1c133b4f2a669972cc08a upstream. x86_has_pat_wp() is using a wrong test, as it relies on the normal PAT configuration used by the kernel. In case the PAT MSR has been setup by another entity (e.g. Xen hypervisor) it might return false even if the PAT configuration is allowing WP mappings. This due to the fact that when running as Xen PV guest the PAT MSR is setup by the hypervisor and cannot be changed by the guest. This results in the WP related entry to be at a different position when running as Xen PV guest compared to the bare metal or fully virtualized case. The correct way to test for WP support is: 1. Get the PTE protection bits needed to select WP mode by reading __cachemode2pte_tbl[_PAGE_CACHE_MODE_WP] (depending on the PAT MSR setting this might return protection bits for a stronger mode, e.g. UC-) 2. Translate those bits back into the real cache mode selected by those PTE bits by reading __pte2cachemode_tbl[__pte2cm_idx(prot)] 3. Test for the cache mode to be _PAGE_CACHE_MODE_WP Fixes: f88a68facd9a ("x86/mm: Extend early_memremap() support with additional attrs") Signed-off-by: Juergen Gross Signed-off-by: Borislav Petkov Cc: # 4.14 Link: https://lore.kernel.org/r/20220503132207.17234-1-jgross@suse.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/mm/init.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -78,10 +78,20 @@ static uint8_t __pte2cachemode_tbl[8] = [__pte2cm_idx(_PAGE_PWT | _PAGE_PCD | _PAGE_PAT)] = _PAGE_CACHE_MODE_UC, }; -/* Check that the write-protect PAT entry is set for write-protect */ +/* + * Check that the write-protect PAT entry is set for write-protect. + * To do this without making assumptions how PAT has been set up (Xen has + * another layout than the kernel), translate the _PAGE_CACHE_MODE_WP cache + * mode via the __cachemode2pte_tbl[] into protection bits (those protection + * bits will select a cache mode of WP or better), and then translate the + * protection bits back into the cache mode using __pte2cm_idx() and the + * __pte2cachemode_tbl[] array. This will return the really used cache mode. + */ bool x86_has_pat_wp(void) { - return __pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] == _PAGE_CACHE_MODE_WP; + uint16_t prot = __cachemode2pte_tbl[_PAGE_CACHE_MODE_WP]; + + return __pte2cachemode_tbl[__pte2cm_idx(prot)] == _PAGE_CACHE_MODE_WP; } enum page_cache_mode pgprot2cachemode(pgprot_t pgprot)