Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4085710pxb; Wed, 13 Oct 2021 20:37:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxfk5a8Q1unuqVzuNB36qy+F8ceyWWNkcOfLIZC42byC6TBewebvb+aTBH9m13aN42G8xh X-Received: by 2002:a65:62cb:: with SMTP id m11mr2440112pgv.425.1634182622117; Wed, 13 Oct 2021 20:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634182622; cv=none; d=google.com; s=arc-20160816; b=IjAGbpZFELAY7ec0xfTyQMwD9u0SA+osyGVEOtmuUg0nrklpojP11qMzmoWA/1fxO2 quPK1Y5uOBD1qoiOmV5jc6Mjxo0zAsII36iaSDxTZ17yr7tzZD3Va/hsGLTWHTYChA1V ADgMd6WrFxFKoZyojbZCm755DI3/FFwT26K1KqY8uxhVvQnCsXZfbjwK9912BGd/030C nExxzNc7Ueur9g3aGH19Ydqu1x0sHMURDwox16V4TR3JEVhFy2DRJKw8P/DHOhZtqAMs wQyNILv1vtqVO/d0yAGKqlyjef2+9N9+sVTbWaWZp5Rr58gF38swflgm/FHfURUbr5fh vm1A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JXRYlHaZYG2aFI4DCBWmOyqizmtvtzTxNVeiLJn6Liw=; b=XYbFX3hdDqE7ghf3lTIVUxMxYJg6lS/2AJszdB4pKIcMjODsBqIlfyjgE2WE8uRpYC WsVa+wlga7QhBnaQRALAhoAH1S4EOXXsSTnuaWRL4M1h8XoZWsKMaOpFnftykyv8maM4 i4Femfrp2YC1UOIq6x/bpM4z574wejQ6JZWdWt46KnyxR/HFDQavXF+NDiJ2rHA5TFm+ yOL2HZq7qYuDLQjIDQTHxo5JZrzfDl44wh5KJZXgX6wq8mSFaY4AusMBl8MnipX7NH24 3A5xVWTmxsDfoehOvNC7fI4roRiZJQ1hOP6BUPv3X3kJsGVQUgjj4Bmemyr5HSLBQqWA oDqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kEnOGdBX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r34si2090560pgl.489.2021.10.13.20.36.49; Wed, 13 Oct 2021 20:37:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kEnOGdBX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230189AbhJNDhj (ORCPT + 99 others); Wed, 13 Oct 2021 23:37:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230185AbhJNDhi (ORCPT ); Wed, 13 Oct 2021 23:37:38 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B65AC061570 for ; Wed, 13 Oct 2021 20:35:34 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id t11so3194215plq.11 for ; Wed, 13 Oct 2021 20:35:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JXRYlHaZYG2aFI4DCBWmOyqizmtvtzTxNVeiLJn6Liw=; b=kEnOGdBXAv2C6tgG0u+vglG+ysLL1K8zITvn+juHBwyLC05jNu/YCUV88evdITwKkF 3U0ugZdniKczzpkCFsm9AAqG37GZHPipe5shp8ofmRpywch8BjLPIyCNrQd3Lp+1hiQg kDDLWNL9mD81uyJQNajFWTt26wCGn6jED9/0VE3+p6eXcQCh0vJF95hCCaD8tF4e52yS 5M9mv+hkYsbk8xNujn+E2Co+ig/GIo8AurI3APWiT9kzINzATjeWaiiRNMaGJUoVevxl uB2mTMsUk1ksrzGD73SxKDm2SM1qmXERp0P2/mTVRga/r43rX+BKCZeWuH7BGrc8K1av Ym6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JXRYlHaZYG2aFI4DCBWmOyqizmtvtzTxNVeiLJn6Liw=; b=cI52hBJ9D5eZnk1QI/hrns+QS8Wk/KS7G1U5yX+KyG0N6aNXF1z7IQQ78c0Ysg6gpb 6xcsJf8hKQsasuqq+nrdhqqW/eaOwN5cKbWLOIm6wj5ht28E3EIa7IO+6Cj/9KkdudYN jrmUP2POuELNB61HlQQqXRyMghgM7LNZUcPC8/fIt37i9LBQWa6DDFhB3+kuXJRbv+9X nQ3PHmrbOZwACx7v0SKTOZgTGOOxp8Yx6sEvc13dPdViUV8lB0b7MTdDjGmhUmbVG1+A 5s35YV34PWjNgnHITyRVQgJoevaWlW6EqAeqQd3M2e4fJOxwlpMC0qQsQL7la4eDIQBB 4Yxg== X-Gm-Message-State: AOAM531nosOlZHJ2bGmfrY6xoGRw/wS92soe2GLGszQiXvgy/j95WcUF WU+5aUlnbptAnBk9naQHBXhEZsPM2yI= X-Received: by 2002:a17:90a:4812:: with SMTP id a18mr17746442pjh.40.1634182533613; Wed, 13 Oct 2021 20:35:33 -0700 (PDT) Received: from localhost ([198.11.178.15]) by smtp.gmail.com with ESMTPSA id l207sm823700pfd.199.2021.10.13.20.35.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Oct 2021 20:35:33 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, Lai Jiangshan , Josh Poimboeuf , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" Subject: [PATCH V3 16/49] x86/traps: Add fence_swapgs_{user,kernel}_entry() Date: Thu, 14 Oct 2021 11:33:39 +0800 Message-Id: <20211014033414.16321-11-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211014031413.14471-1-jiangshanlai@gmail.com> References: <20211014031413.14471-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lai Jiangshan fence_swapgs_{user,kernel}_entry() in entry64.c are the same as the ASM macro FENCE_SWAPGS_{USER,KERNEL}_ENTRY. fence_swapgs_user_entry is used in the user entry swapgs code path, to prevent a speculative swapgs when coming from kernel space. fence_swapgs_kernel_entry is used in the kernel entry code path, to prevent the swapgs from getting speculatively skipped when coming from user space. Cc: Josh Poimboeuf Signed-off-by: Lai Jiangshan --- arch/x86/entry/entry64.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/x86/entry/entry64.c b/arch/x86/entry/entry64.c index 9a5c535b1ddf..bdc9540f25d3 100644 --- a/arch/x86/entry/entry64.c +++ b/arch/x86/entry/entry64.c @@ -38,3 +38,24 @@ static __always_inline void switch_to_kernel_cr3(void) #else static __always_inline void switch_to_kernel_cr3(void) {} #endif + +/* + * Mitigate Spectre v1 for conditional swapgs code paths. + * + * fence_swapgs_user_entry is used in the user entry swapgs code path, to + * prevent a speculative swapgs when coming from kernel space. It must be + * used with switch_to_kernel_cr3() in the same path. + * + * fence_swapgs_kernel_entry is used in the kernel entry code path without + * CR3 write or with conditinal CR3 write only, to prevent the swapgs from + * getting speculatively skipped when coming from user space. + */ +static __always_inline void fence_swapgs_user_entry(void) +{ + alternative("", "lfence", X86_FEATURE_FENCE_SWAPGS_USER); +} + +static __always_inline void fence_swapgs_kernel_entry(void) +{ + alternative("", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL); +} -- 2.19.1.6.gb485710b