Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4454272imw; Tue, 19 Jul 2022 06:58:31 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tdHzeDD6kGqfBYQOACuia85ztQoUcoHRicWA7mQeUUfJd1A4qxZvxABNB21Fi1N/Rr7PSe X-Received: by 2002:a05:6402:d53:b0:43b:a0cf:d970 with SMTP id ec19-20020a0564020d5300b0043ba0cfd970mr2543431edb.277.1658239111686; Tue, 19 Jul 2022 06:58:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658239111; cv=none; d=google.com; s=arc-20160816; b=jvCLHwDl2aK3IwQsWrMCFAwTPQIotyljlmYpwDDjiqiZiZydD9fg2xCNLEFw/fpWQH aLztK5YxYt2OlFIWmxNJNQzYyRSuQaFwunIksy/bC0DuuMUzGfxvjaboLouK57dGcsuz i09chFhRJvbNkRw5gL4VFn0xxwBf5uuZj5Gvc+uHdv2ADyN4NPPxezaqdleAWRSaJPbo 3JtA1Sdvou7PzRZrWh9qNwh9tLSj8XeWttKamwmVcjS3l/InMMAsUF01eFHNFSNhpki3 cdXy46cs9nI1pX6XRK8B/dU32fw5lb1Zv7uoR5aFtnXC72yCrDFM1hmfRe/p6w7Apzv8 sLZQ== 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=pmY7Cad3g4gud0hox8xIXblJyFSUDl7tFU0yyVV47PBERnzcLlHv5XpH+BghFPP94Z 3reBCiihATqWzu6n+tgAzsn8ITJv17ohRLPKSrRbLHpoBi1W3wbpTW1lea0adyjXasLG tmX55a1W1RJoJacpEkAOI449BWiU7UOgj2/CIIDR35+5iXod5nUUxgcMKs9H7Cn/uLL7 Yl22xXUaDYgl6XlFFFLviZJ+CrOFdXxto3hCyc0KmRahtM0q5bL2c1Hol4AOykSncWms O6iwyHqUKeUuZbNuYi0XQMsECEFDbuTw4JCfeB/wzmcnQ/w5dnoqTRPwTx2M4omGDSzI vyhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="x/PweP/J"; 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 mp3-20020a1709071b0300b0072ab880b38esi19555478ejc.764.2022.07.19.06.58.00; Tue, 19 Jul 2022 06:58:31 -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="x/PweP/J"; 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 S241636AbiGSMrl (ORCPT + 99 others); Tue, 19 Jul 2022 08:47:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241701AbiGSMqn (ORCPT ); Tue, 19 Jul 2022 08:46:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDB188AB02; Tue, 19 Jul 2022 05:18:27 -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 2C3E06182A; Tue, 19 Jul 2022 12:18:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBF2FC341CA; Tue, 19 Jul 2022 12:18:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658233082; bh=B5R6E+ddq8m5/lQLkLjA5+soKj5kmq3p6rjJ4fQbuHU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x/PweP/JoFVLgoeBmOhS744n19CQIjgrti/wD+kzCJTup5mckjpH6yLkoYQOd1WCl ID7CSOGyEbNLa5P5IYLWMCfQ+yamHp+Aal6/dRr+IH5axDUxA2ylYxApmYiit00R89 01OvUzrjxrkq7PibKtjjLy9+wiL2MR5x//y5PhYM= 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.15 167/167] x86/pat: Fix x86_has_pat_wp() Date: Tue, 19 Jul 2022 13:54:59 +0200 Message-Id: <20220719114712.619651806@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220719114656.750574879@linuxfoundation.org> References: <20220719114656.750574879@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)