Received: by 2002:a4a:311b:0:0:0:0:0 with SMTP id k27-v6csp4805999ooa; Tue, 14 Aug 2018 10:48:22 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwldUGYrcz0qslktb++NV8PN3zOzFtZdQxNcooXVZfcZ/FRVsKPb+RmVB3soNDIqRe0ceyz X-Received: by 2002:a62:4f5b:: with SMTP id d88-v6mr24540004pfb.225.1534268902725; Tue, 14 Aug 2018 10:48:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534268902; cv=none; d=google.com; s=arc-20160816; b=Sun1ZVm2uDrfD7mQ+PmV+vlmym0nlDtL0uuXSn6W8Nw7/MWFsuR7zbECz9I3BH5BwZ Ev6xQg7bSinafVAxslmBJT8IkNxXhhrXK85+r34PTfyQo539GicoIz9HVe3sE9RetqHU QUGBdhBjuHU6asVyr9dBm1/WMWGcSRJMFiBOb3hR7jnVHjmWyExHnsVaiVRk0EUTpgxT sKnZRbCL2yYEg/joPXqfLjCPh5IjILH35ve/xgNJcxOH2b5TpnX7DXVs1Z3GGE5292Ih f39836cO30Hs4l6BDoFv4/ZGjKPix9H9uvbmhz+lFsHmS8ADfSeWDufXehiHPGLnMV9n zsvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=tw66v61vDLoFeRDWH68MuAd3xAqiF9IZ1Vd5fRMgXEU=; b=jleUXdjsSBSM0sGOoOCUubVNiVmpWzbntl1NiP94fgt2fCywHhNK/SQDeJuQkX1W/J Igs6DwNPmEJOx/GOIs/fti4giN1kUligtezHo94hCoIIFpcXdE9AIwZrsrTgphE1hwOc jNSER0tff8lsoZwsfHPTqIeNUY0FiKwE29oH07wzaSNPqenmcG9Mg0f3K7UTWhMPjIHM YaP1DJ2NxoqV3i5Cbofy9NNZ5OJGoZRfEMPNf49z0wvlZdv7NBSBcvx2QbFT9yFlKDtX 6n0sq6s1c63nDl2tvHHL5/cm6K6lwFZMQ0abICanxXQYl2YTTOJmmpDFIe7KG8wypfdc 31tw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k7-v6si19989693pgk.104.2018.08.14.10.48.07; Tue, 14 Aug 2018 10:48:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403760AbeHNUfN (ORCPT + 99 others); Tue, 14 Aug 2018 16:35:13 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:60830 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390379AbeHNUfM (ORCPT ); Tue, 14 Aug 2018 16:35:12 -0400 Received: from localhost (unknown [194.244.16.108]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 12DCBC74; Tue, 14 Aug 2018 17:46:56 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Andi Kleen , Thomas Gleixner , Josh Poimboeuf , Michal Hocko , Vlastimil Babka , Dave Hansen , David Woodhouse , Guenter Roeck Subject: [PATCH 4.4 26/43] x86/speculation/l1tf: Protect swap entries against L1TF Date: Tue, 14 Aug 2018 19:18:02 +0200 Message-Id: <20180814171518.823441897@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180814171517.014285600@linuxfoundation.org> References: <20180814171517.014285600@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Linus Torvalds commit 2f22b4cd45b67b3496f4aa4c7180a1271c6452f6 upstream With L1 terminal fault the CPU speculates into unmapped PTEs, and resulting side effects allow to read the memory the PTE is pointing too, if its values are still in the L1 cache. For swapped out pages Linux uses unmapped PTEs and stores a swap entry into them. To protect against L1TF it must be ensured that the swap entry is not pointing to valid memory, which requires setting higher bits (between bit 36 and bit 45) that are inside the CPUs physical address space, but outside any real memory. To do this invert the offset to make sure the higher bits are always set, as long as the swap file is not too big. Note there is no workaround for 32bit !PAE, or on systems which have more than MAX_PA/2 worth of memory. The later case is very unlikely to happen on real systems. [AK: updated description and minor tweaks by. Split out from the original patch ] Signed-off-by: Linus Torvalds Signed-off-by: Andi Kleen Signed-off-by: Thomas Gleixner Tested-by: Andi Kleen Reviewed-by: Josh Poimboeuf Acked-by: Michal Hocko Acked-by: Vlastimil Babka Acked-by: Dave Hansen Signed-off-by: David Woodhouse Signed-off-by: Guenter Roeck Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/pgtable_64.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/arch/x86/include/asm/pgtable_64.h +++ b/arch/x86/include/asm/pgtable_64.h @@ -168,7 +168,7 @@ static inline int pgd_large(pgd_t pgd) { * * | ... | 11| 10| 9|8|7|6|5| 4| 3|2| 1|0| <- bit number * | ... |SW3|SW2|SW1|G|L|D|A|CD|WT|U| W|P| <- bit names - * | TYPE (59-63) | OFFSET (9-58) |0|0|X|X| X| X|X|SD|0| <- swp entry + * | TYPE (59-63) | ~OFFSET (9-58) |0|0|X|X| X| X|X|SD|0| <- swp entry * * G (8) is aliased and used as a PROT_NONE indicator for * !present ptes. We need to start storing swap entries above @@ -181,6 +181,9 @@ static inline int pgd_large(pgd_t pgd) { * * Bit 7 in swp entry should be 0 because pmd_present checks not only P, * but also L and G. + * + * The offset is inverted by a binary not operation to make the high + * physical bits set. */ #define SWP_TYPE_BITS 5 @@ -195,13 +198,15 @@ static inline int pgd_large(pgd_t pgd) { #define __swp_type(x) ((x).val >> (64 - SWP_TYPE_BITS)) /* Shift up (to get rid of type), then down to get value */ -#define __swp_offset(x) ((x).val << SWP_TYPE_BITS >> SWP_OFFSET_SHIFT) +#define __swp_offset(x) (~(x).val << SWP_TYPE_BITS >> SWP_OFFSET_SHIFT) /* * Shift the offset up "too far" by TYPE bits, then down again + * The offset is inverted by a binary not operation to make the high + * physical bits set. */ #define __swp_entry(type, offset) ((swp_entry_t) { \ - ((unsigned long)(offset) << SWP_OFFSET_SHIFT >> SWP_TYPE_BITS) \ + (~(unsigned long)(offset) << SWP_OFFSET_SHIFT >> SWP_TYPE_BITS) \ | ((unsigned long)(type) << (64-SWP_TYPE_BITS)) }) #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val((pte)) })