From patchwork Thu Dec 28 12:58:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 183725 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp1981555dyb; Thu, 28 Dec 2023 05:10:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLAquReKosEgwH3tBVvGM8FlYRR5EQwBT5eJ1Y1dzX8HWe6wfs1L8e6RNULx2QzyG3q0Cf X-Received: by 2002:a05:6e02:160a:b0:35f:a0b8:9df8 with SMTP id t10-20020a056e02160a00b0035fa0b89df8mr9884102ilu.123.1703769007211; Thu, 28 Dec 2023 05:10:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703769007; cv=none; d=google.com; s=arc-20160816; b=01PFQUv6xCGilUQ6OWnp5eOvrSaYy/49dcAXaO3a+exHYYo2DnQf9xNoA+/CKk7XF7 RCYSjIf06ZLLwjDyar/Ad0K3t11CTg3teiBOKfgyGAd4wMY2bxaar60rRW0a7YkIqK6S ah/U+3Up1G1FlICljA+RO09C409ZUcFXAlXH4ZojDW9ieSQnDdzwUcgx9ZtkeaMpNyXg qBLpqFrLCtqunNuWbp4znNUg/8ParCtkoQ0yIbTW5vWM0gEo2gFC9lChgvChE/C08gB7 /rJjs8+0rmiWrwL0kM6BsVjgTr1ijcQG8dhetfVJKPsDrn3rcsN/Hl/LiBj/9jmcPxIK gulw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=rLIZ9pfafsupaG3vN7BQTHPExOdf0+plx9g1YbHQm4Y=; fh=e5OOrCjmc87s/YZszbOQCKg9A11Jj7/axfrwJ2Itr5A=; b=MkSCRS/5K2C816krEld9YzQqUZLJqLelGX8TL+FuSIWuWT8TwDN/3ePNwiaxMTnq+V Otj2q9dH7Z5hDujOx0eDoqDYjhOtpU8zosfxNWR+hblTIUsKlwHe+xyeudAQOUV39IyS qZHRK+jLy4Smfj0f+wyMH6QQZt9p1P14q2pg4T1ITzkpgy4BUJiuMOyah0+KteubjDYG 4/zpZkNdR06qjEbi9ToRidHwmmokIbqTATbP8sou+jRRidm+UfFHk1qDaAonKNyYruzu CL0+ValhSswIJIIntAZPQFOi0hzu7OfmyQf+JJEaTmhfPTfGeAFiqrNk/T0AnbcXTvpm qAvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NcCKsvfT; spf=pass (google.com: domain of linux-kernel+bounces-12662-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12662-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id g66-20020a636b45000000b005bdff97f97bsi12939812pgc.92.2023.12.28.05.10.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 05:10:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12662-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NcCKsvfT; spf=pass (google.com: domain of linux-kernel+bounces-12662-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12662-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 396E92887E3 for ; Thu, 28 Dec 2023 13:03:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0FADD8829; Thu, 28 Dec 2023 12:59:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NcCKsvfT" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C53679CB for ; Thu, 28 Dec 2023 12:58:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1703768340; x=1735304340; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=+eXzxuR0pHiDls2fQHe9OlVg3qaFTNYjViRLmdU/tM0=; b=NcCKsvfTBI5PElsSE/oVssckKKe5l3gvXjGqhCeH4N7+wX0RmdnVS+7C U7Bs4BcoYHTvyRXWqfTSFPmK54NBhhlRN+RaDPNhjqP7R+8IdDnrMCCX/ 11LH0K/nW6O0QWZWQAvY8/jDG6s8t7Jd6s28h82c/hVdVQ7myMDO4S5S0 nHZ4cWkaYhKhlotieWet90mLYiHP/sdnvg8msKUBs9nsmSiVGVarPZalQ x1pHAKGtkN1mkf/S1uVyCNpi0OMZsrSsvxwjJPHJrBY892EOiXIPKNxau CA5PIWJ6xJxLJ2WWm7oOwZqQOLJqnY52MmB1bN8sm+NR0v2xOXX1JeFYu A==; X-IronPort-AV: E=McAfee;i="6600,9927,10936"; a="386978786" X-IronPort-AV: E=Sophos;i="6.04,312,1695711600"; d="scan'208";a="386978786" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Dec 2023 04:58:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10936"; a="812788717" X-IronPort-AV: E=Sophos;i="6.04,312,1695711600"; d="scan'208";a="812788717" Received: from fjohanne-mobl.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.35.92]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Dec 2023 04:58:54 -0800 Received: by box.shutemov.name (Postfix, from userid 1000) id BEA4210A536; Thu, 28 Dec 2023 15:58:51 +0300 (+03) From: "Kirill A. Shutemov" To: Dave Hansen , Andy Lutomirski , Peter Zijlstra Cc: Isaku Yamahata , x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Juergen Gross Subject: [PATCH] x86/pat: Simplifying the PAT programming protocol Date: Thu, 28 Dec 2023 15:58:47 +0300 Message-ID: <20231228125847.12842-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.41.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786531290598979468 X-GMAIL-MSGID: 1786531290598979468 The programming protocol for the PAT MSR follows the MTRR programming protocol. However, this protocol is cumbersome and requires disabling caching (CR0.CD=1), which is not possible on some platforms. Specifically, a TDX guest is not allowed to set CR0.CD. It triggers a #VE exception. Turned out the requirement to follow the MTRR programming protocol for PAT programming is unnecessarily strict. The new Intel Software Developer Manual[1] (December 2023) relaxes this requirement. Please refer to the section titled "Programming the PAT" for more information. The AMD documentation does not link PAT programming to MTRR. The kernel only needs to flush the TLB after updating the PAT MSR. The set_memory code already takes care of flushing the TLB and cache when changing the memory type of a page. [1] http://www.intel.com/sdm Signed-off-by: Kirill A. Shutemov Cc: Juergen Gross Reviewed-by: Juergen Gross --- arch/x86/kernel/cpu/cacheinfo.c | 7 ++++--- arch/x86/mm/pat/memtype.c | 9 +++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/cpu/cacheinfo.c b/arch/x86/kernel/cpu/cacheinfo.c index c131c412db89..78afad50a7c0 100644 --- a/arch/x86/kernel/cpu/cacheinfo.c +++ b/arch/x86/kernel/cpu/cacheinfo.c @@ -1118,15 +1118,16 @@ static void cache_cpu_init(void) unsigned long flags; local_irq_save(flags); - cache_disable(); - if (memory_caching_control & CACHE_MTRR) + if (memory_caching_control & CACHE_MTRR) { + cache_disable(); mtrr_generic_set_state(); + cache_enable(); + } if (memory_caching_control & CACHE_PAT) pat_cpu_init(); - cache_enable(); local_irq_restore(flags); } diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index de10800cd4dd..7cac1b56c5e7 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -240,6 +240,8 @@ void pat_cpu_init(void) } wrmsrl(MSR_IA32_CR_PAT, pat_msr_val); + + __flush_tlb_all(); } /** @@ -296,13 +298,8 @@ void __init pat_bp_init(void) /* * Xen PV doesn't allow to set PAT MSR, but all cache modes are * supported. - * When running as TDX guest setting the PAT MSR won't work either - * due to the requirement to set CR0.CD when doing so. Rely on - * firmware to have set the PAT MSR correctly. */ - if (pat_disabled || - cpu_feature_enabled(X86_FEATURE_XENPV) || - cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) { + if (pat_disabled || cpu_feature_enabled(X86_FEATURE_XENPV)) { init_cache_modes(pat_msr_val); return; }