Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3085811iob; Mon, 16 May 2022 12:45:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwr9nfYbnAAPi+39xYtzI6RcxIsKmqB/2DApsCG+k8svS729LJS5wvx3ML1AxEkUAGwGJQ X-Received: by 2002:a05:6402:1941:b0:413:2b7e:676e with SMTP id f1-20020a056402194100b004132b7e676emr15343058edz.114.1652730324686; Mon, 16 May 2022 12:45:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652730324; cv=none; d=google.com; s=arc-20160816; b=GUl5yizIyZEr8I21uVmAUzqdnmbYWw01HaZbhj8MFG+wrT2Hr/Jnf1GNZ3LuZyyAht eDPAYPppk2dmT77vi6vk+WCexqjS4WhuNvb5Qv4n8rrVdX8PJA0t41KmqvQ8J7yNim+S tP9wp8XYJ6Y1kgAaQFxsxS79v60TIAtbumJ49yoVg492GIn4i2WiTgLd5IF64ARA8zrh W+kadnNVJ7SExGM8AYhl3HSsEPassXrnvr6/H6P48TewX/4u+7PMaJ+umo7RtrPlJPi4 CqJXcyp1a5z5hYJVLrwfuAeiTdeInGjqnbjgP+odVj3wd7YK3sbHzDgdDBFyP/I3xBpW qhVQ== 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:cc:to:from :subject:dkim-signature; bh=yhvLd+Ty0pJCDaj6jnHXmYaBVepxTXuV2QCwhtJqKBw=; b=Yyl5dsE1TZqqpTJJlfp3szWRnNZ5aAgtIecdJHfRdFqzneKunQVuhuuvxJ7aqsqInI BUqh34XWLdzvNB/syamQNMQdj7VTDizF6BMgd/uXFRUebn5GKMW95+gljmPohEtZ/8L5 TyB7rAr1HawtOtdub9IRz3ifML03qGncO1Ul4erzWiVe592ZfHCnYbjmRjFHeKI+5Hl+ E/inuqLTmNEHy5yqoS7lTrEJjFbMWr5xnqcZHyRCzTCX0fbkagYS7Mhha5VWhonCL3dM aDlTDerJzGjxCQhXIBPgvu60ErakeAzhtIa84a3EWMWJHeyE46v3bnfxxYNWzAzjhfPb G1pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LalQesig; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z3-20020a056402274300b0041d70e6ea5bsi13922281edd.533.2022.05.16.12.44.58; Mon, 16 May 2022 12:45:24 -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=@intel.com header.s=Intel header.b=LalQesig; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344708AbiEPSWQ (ORCPT + 99 others); Mon, 16 May 2022 14:22:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232736AbiEPSWL (ORCPT ); Mon, 16 May 2022 14:22:11 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46C232B27D; Mon, 16 May 2022 11:22:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1652725329; x=1684261329; h=subject:from:to:cc:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oLHQEvkPPnYrdI/9V6HkCwio6IKvCNslsChgOGiucHs=; b=LalQesig/VJuMFnKwAbkpin4NJPs+KeNgSPaO09/9ETboqoZ9babFYCN f5KxSMtKUmc4/NekXBgztrTKEokSDh1k9gUyAZ+1Acw/93V+LjLDre18G tcOopP4fNYTK2P3Pys8aCuLpYojbl2ifTsRMt4Y9cy8qkB1dLoRPgnZgu yX6wzgs8aNQ2GkLG4MOJ/uV4TBHM+35DqltIAU4IXnrEKTO5BQOcfP7Qg 1W02eIZrn7fYoZ0gdt4W0qo0acmd/JpIT55sJt6tItiCn3RN/Yg2YtOtr H8LKBQKJVLSIqsL2VHAd6PhMe767zlRfcY9Os+Sn8pXKNwSdXlulXq2Pt Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10349"; a="270866548" X-IronPort-AV: E=Sophos;i="5.91,230,1647327600"; d="scan'208";a="270866548" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2022 11:21:47 -0700 X-IronPort-AV: E=Sophos;i="5.91,230,1647327600"; d="scan'208";a="816519835" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.25]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2022 11:21:46 -0700 Subject: [PATCH v11 2/7] x86/mce: relocate set{clear}_mce_nospec() functions From: Dan Williams To: linux-kernel@vger.kernel.org Cc: Christoph Hellwig , Jane Chu , Borislav Petkov , Stephen Rothwell , x86@kernel.org, nvdimm@lists.linux.dev, linux-fsdevel@vger.kernel.org Date: Mon, 16 May 2022 11:21:46 -0700 Message-ID: <165272527328.90175.8336008202048685278.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <165247798860.4117683.4554602198740624216.stgit@dwillia2-desk3.amr.corp.intel.com> References: <165247798860.4117683.4554602198740624216.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-3-g996c MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,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 From: Jane Chu Relocate the twin mce functions to arch/x86/mm/pat/set_memory.c file where they belong. While at it, fixup a function name in a comment. Reviewed-by: Christoph Hellwig Reviewed-by: Dan Williams Signed-off-by: Jane Chu Acked-by: Borislav Petkov Cc: Stephen Rothwell [sfr: gate {set,clear}_mce_nospec() by CONFIG_X86_64] Signed-off-by: Dan Williams --- Changes since v10: - Fix a compile error with 32-bit builds (Stephen) arch/x86/include/asm/set_memory.h | 52 ------------------------------------- arch/x86/mm/pat/set_memory.c | 50 ++++++++++++++++++++++++++++++++++-- include/linux/set_memory.h | 8 +++--- 3 files changed, 52 insertions(+), 58 deletions(-) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index 78ca53512486..b45c4d27fd46 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -86,56 +86,4 @@ bool kernel_page_present(struct page *page); extern int kernel_set_to_readonly; -#ifdef CONFIG_X86_64 -/* - * Prevent speculative access to the page by either unmapping - * it (if we do not require access to any part of the page) or - * marking it uncacheable (if we want to try to retrieve data - * from non-poisoned lines in the page). - */ -static inline int set_mce_nospec(unsigned long pfn, bool unmap) -{ - unsigned long decoy_addr; - int rc; - - /* SGX pages are not in the 1:1 map */ - if (arch_is_platform_page(pfn << PAGE_SHIFT)) - return 0; - /* - * We would like to just call: - * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1); - * but doing that would radically increase the odds of a - * speculative access to the poison page because we'd have - * the virtual address of the kernel 1:1 mapping sitting - * around in registers. - * Instead we get tricky. We create a non-canonical address - * that looks just like the one we want, but has bit 63 flipped. - * This relies on set_memory_XX() properly sanitizing any __pa() - * results with __PHYSICAL_MASK or PTE_PFN_MASK. - */ - decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63)); - - if (unmap) - rc = set_memory_np(decoy_addr, 1); - else - rc = set_memory_uc(decoy_addr, 1); - if (rc) - pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn); - return rc; -} -#define set_mce_nospec set_mce_nospec - -/* Restore full speculative operation to the pfn. */ -static inline int clear_mce_nospec(unsigned long pfn) -{ - return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1); -} -#define clear_mce_nospec clear_mce_nospec -#else -/* - * Few people would run a 32-bit kernel on a machine that supports - * recoverable errors because they have too much memory to boot 32-bit. - */ -#endif - #endif /* _ASM_X86_SET_MEMORY_H */ diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index abf5ed76e4b7..0caf4b0edcbc 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -29,7 +30,6 @@ #include #include #include -#include #include #include @@ -1816,7 +1816,7 @@ static inline int cpa_clear_pages_array(struct page **pages, int numpages, } /* - * _set_memory_prot is an internal helper for callers that have been passed + * __set_memory_prot is an internal helper for callers that have been passed * a pgprot_t value from upper layers and a reservation has already been taken. * If you want to set the pgprot to a specific page protocol, use the * set_memory_xx() functions. @@ -1925,6 +1925,52 @@ int set_memory_wb(unsigned long addr, int numpages) } EXPORT_SYMBOL(set_memory_wb); +/* + * Prevent speculative access to the page by either unmapping + * it (if we do not require access to any part of the page) or + * marking it uncacheable (if we want to try to retrieve data + * from non-poisoned lines in the page). + */ +#ifdef CONFIG_X86_64 +int set_mce_nospec(unsigned long pfn, bool unmap) +{ + unsigned long decoy_addr; + int rc; + + /* SGX pages are not in the 1:1 map */ + if (arch_is_platform_page(pfn << PAGE_SHIFT)) + return 0; + /* + * We would like to just call: + * set_memory_XX((unsigned long)pfn_to_kaddr(pfn), 1); + * but doing that would radically increase the odds of a + * speculative access to the poison page because we'd have + * the virtual address of the kernel 1:1 mapping sitting + * around in registers. + * Instead we get tricky. We create a non-canonical address + * that looks just like the one we want, but has bit 63 flipped. + * This relies on set_memory_XX() properly sanitizing any __pa() + * results with __PHYSICAL_MASK or PTE_PFN_MASK. + */ + decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63)); + + if (unmap) + rc = set_memory_np(decoy_addr, 1); + else + rc = set_memory_uc(decoy_addr, 1); + if (rc) + pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn); + return rc; +} + +/* Restore full speculative operation to the pfn. */ +int clear_mce_nospec(unsigned long pfn) +{ + return set_memory_wb((unsigned long) pfn_to_kaddr(pfn), 1); +} +EXPORT_SYMBOL_GPL(clear_mce_nospec); +#endif /* CONFIG_X86_64 */ + int set_memory_x(unsigned long addr, int numpages) { if (!(__supported_pte_mask & _PAGE_NX)) diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h index f36be5166c19..683a6c3f7179 100644 --- a/include/linux/set_memory.h +++ b/include/linux/set_memory.h @@ -42,14 +42,14 @@ static inline bool can_set_direct_map(void) #endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ -#ifndef set_mce_nospec +#ifdef CONFIG_X86_64 +int set_mce_nospec(unsigned long pfn, bool unmap); +int clear_mce_nospec(unsigned long pfn); +#else static inline int set_mce_nospec(unsigned long pfn, bool unmap) { return 0; } -#endif - -#ifndef clear_mce_nospec static inline int clear_mce_nospec(unsigned long pfn) { return 0;