From patchwork Sun Jun 3 05:23:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 10445189 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2C796602BC for ; Sun, 3 Jun 2018 05:33:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F2C527E63 for ; Sun, 3 Jun 2018 05:33:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1407E289E4; Sun, 3 Jun 2018 05:33:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 72D0A27E63 for ; Sun, 3 Jun 2018 05:33:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E2DE6B026B; Sun, 3 Jun 2018 01:33:20 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 091046B026D; Sun, 3 Jun 2018 01:33:20 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E768C6B026E; Sun, 3 Jun 2018 01:33:19 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg0-f71.google.com (mail-pg0-f71.google.com [74.125.83.71]) by kanga.kvack.org (Postfix) with ESMTP id A28026B026B for ; Sun, 3 Jun 2018 01:33:19 -0400 (EDT) Received: by mail-pg0-f71.google.com with SMTP id f6-v6so8297374pgs.13 for ; Sat, 02 Jun 2018 22:33:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:subject:from :to:cc:date:message-id:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=u/ABAONYiwS16QexpJOhte01EVKTs25+nvITkxvwc+g=; b=LkVFdgTAR5g8AOHy6mHXHXkOMsFd0d7ytsbYWs9Pf+1htLkJZgkLZRRlp8qaswpTpL NRA/+8Ggx0gzld0L1U3PgXWx+CPTbPne/WqemmK9PzZbACu4Ra1QYpM/wj0NApW3wV4P Yw5KbMXbH054BDgQmicrepbUU4bWDHZxezEOR7xhtALCi6l6socJkTTbg488Rq07yGXJ elXN5HFaUqoDtow+jNk5puLo/sZZVKRuNoK0jQFy/AIHqHmra2+zaZjoDces9XsKrRi4 RGAwkVAH3F0ipusv/nIGZKRpFxWAt4YMaPKZON9Weln+J7BRA/9ZX4wdT6Xb3K8Jx43w B9eQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: ALKqPwdgEJqXgEp8P8IJVnDKp/Xe8SCwhKUHmqj+G7Hu5E2KWk1/dTYI 9k1LuhMN8YOc4fcEPeVti2UBIbPMYa+3xPdkR4ulqxreajLJjPJU2nNXCriZd521XErZPWdHtLY bG2fVuZ8ETT/H+LqvyBHrIVmWHO++z661unyEt/4Ouz4d634FXYFulkdxY0zGWxDqZQ== X-Received: by 2002:a17:902:26a:: with SMTP id 97-v6mr17292067plc.367.1528003999339; Sat, 02 Jun 2018 22:33:19 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJt7Y+HONJQV4J6rwl8FhFEuV1E7oEdYug8e2+GNU5GmHAKAlbuCpyw8JivK97nfjpbefAH X-Received: by 2002:a17:902:26a:: with SMTP id 97-v6mr17292048plc.367.1528003998571; Sat, 02 Jun 2018 22:33:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528003998; cv=none; d=google.com; s=arc-20160816; b=Izb3D+FNPp3AVKPHJBfD9A30SLxTD1BUfvSMkHJj0nPCPt0pWYwkfga43pbxoJvUhi yMlZRDs9p8XS4zCTjq5Q8U9ovioA3OC02A9Z4ATHnzSvRqzHHddwdZ0cTeW4yZDdZyN9 2JYPVCXLh14uw3qnvl+VQBlUbZGwZoZZ7Z1XF8N4gkV3WxzBy2J7D14ZZ5Vi/h/nOZ3f fZAuZUr1LxtGq+wqVCWJo/6M4vkkPrcO3Kl8uewL3//tScEqI3zhPLAd0j2E3nR72xAN L73Bbmc2EvKLNmPeivxO0UPc7nz2zc0BMQekazQeEr3zeJ0OZFq5d+HXerbiPaqCIgmG aJxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject :arc-authentication-results; bh=u/ABAONYiwS16QexpJOhte01EVKTs25+nvITkxvwc+g=; b=rUkqp3/hl6pknY3gpT3wAKKFXnZKddJqd6yy2Dt792kuwr12tvkBRq1xZ6WJaeXhn3 PWBCOakQ4J69TccrJSYLT/IjV7ZOs2XtU/zhXlXV2XJyBBmnxbbMny19bvSYlD4LqIlS OlgswgFEEuOajbL+ijGll5S1nCnLaw5Q7COHq6tAQRRJIW70whIyGEd69bFkkyI9S05/ DWdsFXtFPotMWJDu+tHyzfN1pq4oa7TIpFn/kdSTHy9yES9eTl+CyqM/W9UAJrdqUR5s 4iJz3xDEHvQONx2G25AlfQIXXpFlu2R85aDkxH/JfYMrYjERfHwrrxQeeMakbTbyrDa7 purw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga14.intel.com (mga14.intel.com. [192.55.52.115]) by mx.google.com with ESMTPS id u3-v6si44613626plb.2.2018.06.02.22.33.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 02 Jun 2018 22:33:18 -0700 (PDT) Received-SPF: pass (google.com: domain of dan.j.williams@intel.com designates 192.55.52.115 as permitted sender) client-ip=192.55.52.115; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dan.j.williams@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=dan.j.williams@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Jun 2018 22:33:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,472,1520924400"; d="scan'208";a="229476124" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga005.jf.intel.com with ESMTP; 02 Jun 2018 22:33:17 -0700 Subject: [PATCH v2 07/11] x86, memory_failure: Introduce {set, clear}_mce_nospec() From: Dan Williams To: linux-nvdimm@lists.01.org Cc: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Tony Luck , Borislav Petkov , linux-edac@vger.kernel.org, x86@kernel.org, hch@lst.de, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, jack@suse.cz Date: Sat, 02 Jun 2018 22:23:20 -0700 Message-ID: <152800340082.17112.1154560126059273408.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <152800336321.17112.3300876636370683279.stgit@dwillia2-desk3.amr.corp.intel.com> References: <152800336321.17112.3300876636370683279.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-2-gc94f MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Currently memory_failure() returns zero if the error was handled. On that result mce_unmap_kpfn() is called to zap the page out of the kernel linear mapping to prevent speculative fetches of potentially poisoned memory. However, in the case of dax mapped devmap pages the page may be in active permanent use by the device driver, so it cannot be unmapped from the kernel. Instead of marking the page not present, marking the page UC should be sufficient for preventing poison from being pre-fetched into the cache. Convert mce_unmap_pfn() to set_mce_nospec() remapping the page as UC, to hide it from speculative accesses. Given that that persistent memory errors can be cleared by the driver, include a facility to restore the page to cacheable operation, clear_mce_nospec(). Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Borislav Petkov Cc: Cc: Signed-off-by: Dan Williams --- arch/x86/include/asm/set_memory.h | 29 ++++++++++++++++++++++ arch/x86/kernel/cpu/mcheck/mce-internal.h | 15 ----------- arch/x86/kernel/cpu/mcheck/mce.c | 38 ++--------------------------- include/linux/set_memory.h | 14 +++++++++++ 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index bd090367236c..debc1fee1457 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -88,4 +88,33 @@ extern int kernel_set_to_readonly; void set_kernel_text_rw(void); void set_kernel_text_ro(void); +#ifdef CONFIG_X86_64 +/* + * Mark the linear address as UC to disable speculative pre-fetches into + * potentially poisoned memory. + */ +static inline int set_mce_nospec(unsigned long pfn) +{ + int rc; + + rc = set_memory_uc((unsigned long) __va(PFN_PHYS(pfn)), 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) __va(PFN_PHYS(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/kernel/cpu/mcheck/mce-internal.h b/arch/x86/kernel/cpu/mcheck/mce-internal.h index 374d1aa66952..ceb67cd5918f 100644 --- a/arch/x86/kernel/cpu/mcheck/mce-internal.h +++ b/arch/x86/kernel/cpu/mcheck/mce-internal.h @@ -113,21 +113,6 @@ static inline void mce_register_injector_chain(struct notifier_block *nb) { } static inline void mce_unregister_injector_chain(struct notifier_block *nb) { } #endif -#ifndef CONFIG_X86_64 -/* - * On 32-bit systems it would be difficult to safely unmap a poison page - * from the kernel 1:1 map because there are no non-canonical addresses that - * we can use to refer to the address without risking a speculative access. - * However, this isn't much of an issue because: - * 1) Few unmappable pages are in the 1:1 map. Most are in HIGHMEM which - * are only mapped into the kernel as needed - * 2) 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. - */ -static inline void mce_unmap_kpfn(unsigned long pfn) {} -#define mce_unmap_kpfn mce_unmap_kpfn -#endif - struct mca_config { bool dont_log_ce; bool cmci_disabled; diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c index 42cf2880d0ed..a0fbf0a8b7e6 100644 --- a/arch/x86/kernel/cpu/mcheck/mce.c +++ b/arch/x86/kernel/cpu/mcheck/mce.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -50,7 +51,6 @@ #include #include #include -#include #include "mce-internal.h" @@ -108,10 +108,6 @@ static struct irq_work mce_irq_work; static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs); -#ifndef mce_unmap_kpfn -static void mce_unmap_kpfn(unsigned long pfn); -#endif - /* * CPU/chipset specific EDAC code can register a notifier call here to print * MCE errors in a human-readable form. @@ -602,7 +598,7 @@ static int srao_decode_notifier(struct notifier_block *nb, unsigned long val, if (mce_usable_address(mce) && (mce->severity == MCE_AO_SEVERITY)) { pfn = mce->addr >> PAGE_SHIFT; if (!memory_failure(pfn, 0)) - mce_unmap_kpfn(pfn); + set_mce_nospec(pfn); } return NOTIFY_OK; @@ -1070,38 +1066,10 @@ static int do_memory_failure(struct mce *m) if (ret) pr_err("Memory error not recovered"); else - mce_unmap_kpfn(m->addr >> PAGE_SHIFT); + set_mce_nospec(m->addr >> PAGE_SHIFT); return ret; } -#ifndef mce_unmap_kpfn -static void mce_unmap_kpfn(unsigned long pfn) -{ - unsigned long decoy_addr; - - /* - * Unmap this page from the kernel 1:1 mappings to make sure - * we don't log more errors because of speculative access to - * the page. - * We would like to just call: - * set_memory_np((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_np() not checking whether we passed - * a legal address. - */ - - decoy_addr = (pfn << PAGE_SHIFT) + (PAGE_OFFSET ^ BIT(63)); - - if (set_memory_np(decoy_addr, 1)) - pr_warn("Could not invalidate pfn=0x%lx from 1:1 map\n", pfn); -} -#endif - /* * The actual machine check handler. This only handles real * exceptions when something got corrupted coming in through int 18. diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h index da5178216da5..2a986d282a97 100644 --- a/include/linux/set_memory.h +++ b/include/linux/set_memory.h @@ -17,6 +17,20 @@ static inline int set_memory_x(unsigned long addr, int numpages) { return 0; } static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; } #endif +#ifndef set_mce_nospec +static inline int set_mce_nospec(unsigned long pfn) +{ + return 0; +} +#endif + +#ifndef clear_mce_nospec +static inline int clear_mce_nospec(unsigned long pfn) +{ + return 0; +} +#endif + #ifndef CONFIG_ARCH_HAS_MEM_ENCRYPT static inline int set_memory_encrypted(unsigned long addr, int numpages) {