Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp3206138pxb; Sun, 26 Sep 2021 08:13:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMMXUHL/8NNUFYf6ig+11+DacwKfstAOGXew+oH4qPDc1ZSU5LBdrWJBKvzIFMG4IdX8NM X-Received: by 2002:a50:d803:: with SMTP id o3mr17585240edj.91.1632669215179; Sun, 26 Sep 2021 08:13:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632669215; cv=none; d=google.com; s=arc-20160816; b=dBgU5ELOldojihozPwO9e9Es8LhFu2ga3hY6zeoxyKXVEsXUBa97y/Bbef1GEH9KB0 k9nmjUQoMlflVkr5RoecJeNzocpOyLRTliG5N+2YapMmFqTiJuZajvXeZLe9J/a5xv9S 7BE0o33g57FXDbtX6UQ+fEnofvvg8wnv4dyjuVO8T3mqtyOUoh5k4I2qm2/aOKEt8k3c ChlbN+ZLu0/09JcDNtaU9CJDANaBHZKnMeCgxf5XL/UxcNzopmrmH+DyncoCrvyV+Grq +LeDny86RDJ1YQDd78+LZ1P3hde0zY1xSBxWxE6j55v8IsZ7nSm2aHvITBHDU6g9z5Eo Xz8Q== 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=BO5PiqDJYvzBl23hUDS9RR5Uds/bZ0R6MkZ46+3NKDo=; b=WyNU4ED9RNUI6S1AEdr2Ba4l1w5T8/t5MHJXpyyPwPEoY15iA9jT63dh5V2vtwl3oR Ok3iJA+RmgPdU6Ufynj3DKiecilfG8qC0IvBwsRr3ONPvmfa+bHedat+8Jc7QmvOSs8K RxdxIaEMNqzqdmO2f1GP2ab0WUzeQGlhVg092pqvLqGFlTKoBeyzuAyB26+dng+cWqMl 0iL6SO4KjLwCYpiwZKA+63ohYW7bIQw3Yn48Boc7avDb9Anwh7lWlvzjl9pKBBvjXXtF 1VWHhU/gyKPHQqJBq/nv/kX4HwBFbRaib16P5zy3OtXCXywbFCTF2QZ91jGmJSMsyr6M PmJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=pqUw4XBG; 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 j17si15387535ejm.446.2021.09.26.08.13.11; Sun, 26 Sep 2021 08:13:35 -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=pqUw4XBG; 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 S232035AbhIZPLh (ORCPT + 99 others); Sun, 26 Sep 2021 11:11:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232036AbhIZPLa (ORCPT ); Sun, 26 Sep 2021 11:11:30 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6363FC061740 for ; Sun, 26 Sep 2021 08:09:53 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id lb1-20020a17090b4a4100b001993f863df2so11319927pjb.5 for ; Sun, 26 Sep 2021 08:09:53 -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=BO5PiqDJYvzBl23hUDS9RR5Uds/bZ0R6MkZ46+3NKDo=; b=pqUw4XBG8TXpuQUYvqsg9Th8g7ghhf4jYR3R54PjUSQJlceMYrNV9DkzJpRLZXlCuc VVTCxjnXqbNGY2SOH5mto67ZKFldoSMCzxL6RPeLASxFRC2+ivzj7YfE2EcGdITTUhwC +lmWKeFuFCQex5sR8ayh/San2b9Qc4mqkYy7K6/Wj8b6C/uJbCi5xKVJhanB7JtS8BCP F44AgpxWJXHaSW9UwylC+azOvS86sxaRZq9JlEIhAghA5o0td7uY0bCwV/0B0c5ESE/O A9QoAUUYGgkUcBx0V4b0UPz/Z/djFNzjgYtDTKJZiw2YeZV1LXEakcQfIfHovIywwVla IK2A== 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=BO5PiqDJYvzBl23hUDS9RR5Uds/bZ0R6MkZ46+3NKDo=; b=JQOPI+92+0OH/Tf7Wu3TsONkuaxsJFdFj5v6vs77SmXVkOXTnO6JgAaYc2VTBGTR3d DVhpVPEfW5TH2M1QswrLlOZnJIefVleQ8Owyoh17Vi04oM9ELgDRiKSfBrnRoKUrNha2 qX8wvJish0lXq7WBLoUgWhmfgWcOYFkJbSl08kirAmjlosl9vR5VUUAW5c4bHxitTCjd Ab+204/PdcZQQVZWQotn5moZIvGIxf5NEhGIHDa8t/Bd624UiueUSHEZbFYjKdKaVcmA BdcDh69/gxRFojK3APWboiLKWkq0XXPhk7JJAxrOgLZTR26wviNU+StO3aoTmiPM0weo RhRg== X-Gm-Message-State: AOAM532VxszrjApNHTvszI6D1h4WhvHhPsuJ5TYBsj5Jo/Ur8eTUkGv5 NXcd5aKJrJ8zkg+0jRoxcDcpj7EbUyngJw== X-Received: by 2002:a17:90b:3ca:: with SMTP id go10mr24867pjb.37.1632668992768; Sun, 26 Sep 2021 08:09:52 -0700 (PDT) Received: from localhost ([47.88.60.64]) by smtp.gmail.com with ESMTPSA id k22sm14662115pfi.149.2021.09.26.08.09.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Sep 2021 08:09:52 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Lai Jiangshan , Josh Poimboeuf , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Subject: [PATCH V2 11/41] x86/entry: Add C user_entry_swapgs_and_fence() and kernel_entry_fence_no_swapgs() Date: Sun, 26 Sep 2021 23:08:08 +0800 Message-Id: <20210926150838.197719-12-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20210926150838.197719-1-jiangshanlai@gmail.com> References: <20210926150838.197719-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 The C user_entry_swapgs_and_fence() implements the ASM code: swapgs FENCE_SWAPGS_USER_ENTRY It will be used in the user entry swapgs code path, doing the swapgs and lfence to prevent a speculative swapgs when coming from kernel space. The C kernel_entry_fence_no_swapgs() implements the ASM code: FENCE_SWAPGS_KERNEL_ENTRY It will be used in the kernel entry non-swapgs 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 77838e19f1ac..dafae60d31f9 100644 --- a/arch/x86/entry/entry64.c +++ b/arch/x86/entry/entry64.c @@ -35,3 +35,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. + * + * user_entry_swapgs_and_fence is used in the user entry swapgs code path, + * to prevent a speculative swapgs when coming from kernel space. + * + * kernel_entry_fence_no_swapgs 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 user_entry_swapgs_and_fence(void) +{ + native_swapgs(); + alternative("", "lfence", X86_FEATURE_FENCE_SWAPGS_USER); +} + +static __always_inline void kernel_entry_fence_no_swapgs(void) +{ + alternative("", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL); +} -- 2.19.1.6.gb485710b