Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4723827pxb; Tue, 31 Aug 2021 11:41:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrqcBUKcw5Bh1qosOa21i9KItdTnFu/sDu8rOz+lJ9I8dmT+J1FFLEX/3SOa6NBxvA61Id X-Received: by 2002:a6b:f919:: with SMTP id j25mr24471425iog.2.1630435306209; Tue, 31 Aug 2021 11:41:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630435306; cv=none; d=google.com; s=arc-20160816; b=m2CgEQVBGJlosayj9RGxTr7Y7sAHk073cAOe+4yw3fGk07v5gfG49xVy3ai4XIgltO ar8CbjdurKUmJXUpWJH8xkIzLVNGNvOs1ajt18uAi8AqaUTbNp4yqxi/WjBvS/LMJT0+ TiA8qKiCHqU8qxHaQC8aL5U0Nq22fm2PakxAwtKAvfoIAb+f6sccQmkO06sWQkzV3hyu cgxYoxHp+tvZSV2rKLr85nBLQjT2kxoW0cVFtfMstFl0FsrfHi+lYeB2LwjWro4Rj5zG zrY3OVD/7Gnt4qqElFPFNpQUaIShEFc3jFeevP6l/6Zb4b+Yb8TzmgFhvEDst4/rCh+M 0pQQ== 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=yoxDkwSPPxsmyf7mXCynO1q5gaWrFpb5i/hujqYhsBs=; b=VNTNhQ8JZoFnj4n0EZnHb7j1HlgzOLJxf1NDSrBnrTw21iy0qDTEQuiyIhIxUVcy3k zCfQ4Fljre15bH4F6mCGl5DhwOhFOjYICz3wbRifNSMwEeTaK3sLoIifYo+LrG3mLzn9 7t+T44V/pX5bJUj8vwrx9Fc+hVK9STg1gsV1g4eZNoOklBNd66nO5/POHIJPUMMvRTh8 cekc+YuEGUtQSlP7unIrSVbGXKkr5/0uO3LxDxDzZjGRWIS2H/8YM6hhhIV57DuouPNY Zse3qj8KPIhTA7SJF4TJc+8eVahOtPgKYJ15uAfjjUDHIAAPPpMhWMUIqxAfepIboC3Y 62DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=O2pODWoq; 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 a21si18074773iot.52.2021.08.31.11.41.34; Tue, 31 Aug 2021 11:41:46 -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=20161025 header.b=O2pODWoq; 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 S240599AbhHaRwU (ORCPT + 99 others); Tue, 31 Aug 2021 13:52:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240595AbhHaRwT (ORCPT ); Tue, 31 Aug 2021 13:52:19 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 215AAC061575 for ; Tue, 31 Aug 2021 10:51:24 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id d3-20020a17090ae28300b0019629c96f25so71879pjz.2 for ; Tue, 31 Aug 2021 10:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yoxDkwSPPxsmyf7mXCynO1q5gaWrFpb5i/hujqYhsBs=; b=O2pODWoqqsNj/p4WTUJbV4lar0JcXGfgwDTHEUGL2RU4L+GBcOYTLBxson/5XwfsrW wRzGoX8V2rWLPNNTzqVpxok+shflkU7KTu2z3pWOeTcM6I9VymQZ2p8Y4PQhe1pGDYVT k5UyS/skvNOOZ/pjChjwaF00EVqJmmtt7aq6vpaHeb3odgVvrRpJlWnobNEhM3bmI+j/ ZPQ8HndQm2Lava2GYqe3M8IqCN+G4+HzogYck6plsEkUbF9/DbIQ3nn256vv60uCZOHw Zm6GwkEtJ5J7UH+l10d5iHOlIBewN8S7OLnFip6IumGr6hTfzj/BoeEW7gpM8FehVrhQ kxxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yoxDkwSPPxsmyf7mXCynO1q5gaWrFpb5i/hujqYhsBs=; b=L1nPjBaetxmZM/GSmCgptc55mbJniLw7dwdckUZ+SNszbiM/QPXQ0Tt1R/ykz7WAf9 5ccWz83pQch9+Bx73A6pxvltrq+ALnrDyGggRtsXBr85sTt3DiRoB4razGMdZcyWBLtN flhw2ieNbZ9JIupo14bIOz/Me2L8vDXHV26u7a3RwoYAVcWkafg5rXWpHvuJqNNL3GDv 6oPR/lVdOeXJBwpxzrPWKgx4owSED5MSApVDm5aWLdYUv4xSELYhajFprQ1bqVCtXVXC dJRPUaAZvHD/XCzVZTlCpxHZ2wts+pX/x/22g3cgh9sAC4q8E1qkXXUZhUep59tbcyAW SwsA== X-Gm-Message-State: AOAM530KeDerqWLd0jtBVn978Ldx2Ige/UVeNJAOvwZq7Q02Ukwqnw+T Q/TcoHC2KlcxF3HYUzaei7tq9z+iEgA= X-Received: by 2002:a17:902:968a:b0:133:e2c5:4908 with SMTP id n10-20020a170902968a00b00133e2c54908mr5831904plp.2.1630432283480; Tue, 31 Aug 2021 10:51:23 -0700 (PDT) Received: from localhost ([47.251.3.230]) by smtp.gmail.com with ESMTPSA id c9sm21280505pgq.58.2021.08.31.10.51.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Aug 2021 10:51:23 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH 09/24] x86/traps: Add fence_swapgs_{user,kernel}_entry() Date: Wed, 1 Sep 2021 01:50:10 +0800 Message-Id: <20210831175025.27570-10-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20210831175025.27570-1-jiangshanlai@gmail.com> References: <20210831175025.27570-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 traps.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 non-swapgs code path, to prevent the swapgs from getting speculatively skipped when coming from user space. Signed-off-by: Lai Jiangshan --- arch/x86/entry/traps.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/x86/entry/traps.c b/arch/x86/entry/traps.c index 9b7d0f15402e..efec3b4eaa5f 100644 --- a/arch/x86/entry/traps.c +++ b/arch/x86/entry/traps.c @@ -789,6 +789,26 @@ 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. + * + * fence_swapgs_kernel_entry is used in the kernel entry non-swapgs code path, + * 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); +} #endif static bool is_sysenter_singlestep(struct pt_regs *regs) -- 2.19.1.6.gb485710b