From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC35BC433EF for ; Mon, 27 Sep 2021 01:10:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7EB3761002 for ; Mon, 27 Sep 2021 01:10:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232328AbhI0BMb (ORCPT ); Sun, 26 Sep 2021 21:12:31 -0400 Received: from out30-44.freemail.mail.aliyun.com ([115.124.30.44]:50997 "EHLO out30-44.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231907AbhI0BMa (ORCPT ); Sun, 26 Sep 2021 21:12:30 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R941e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04357;MF=laijs@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0UpfZeZ3_1632705049; Received: from C02XQCBJJG5H.local(mailfrom:laijs@linux.alibaba.com fp:SMTPD_---0UpfZeZ3_1632705049) by smtp.aliyun-inc.com(127.0.0.1); Mon, 27 Sep 2021 09:10:50 +0800 Subject: Re: [PATCH V2 01/41] x86/entry: Fix swapgs fence To: Thomas Gleixner , Lai Jiangshan , linux-kernel@vger.kernel.org Cc: Josh Poimboeuf , "Chang S . Bae" , Sasha Levin , Andy Lutomirski , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" References: <20210926150838.197719-1-jiangshanlai@gmail.com> <20210926150838.197719-2-jiangshanlai@gmail.com> <87r1dbawzq.ffs@tglx> From: Lai Jiangshan Message-ID: <9312a767-f1d3-d283-80a9-e6b3854252e1@linux.alibaba.com> Date: Mon, 27 Sep 2021 09:10:49 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <87r1dbawzq.ffs@tglx> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/9/27 04:43, Thomas Gleixner wrote: > Lai, > > On Sun, Sep 26 2021 at 23:07, Lai Jiangshan wrote: >> --- a/arch/x86/entry/entry_64.S >> +++ b/arch/x86/entry/entry_64.S >> @@ -898,17 +898,12 @@ SYM_CODE_START_LOCAL(paranoid_entry) >> rdmsr >> testl %edx, %edx >> jns .Lparanoid_entry_swapgs >> + FENCE_SWAPGS_KERNEL_ENTRY > > Good catch. > >> ret >> >> .Lparanoid_entry_swapgs: >> swapgs >> - >> - /* >> - * The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an >> - * unconditional CR3 write, even in the PTI case. So do an lfence >> - * to prevent GS speculation, regardless of whether PTI is enabled. >> - */ >> - FENCE_SWAPGS_KERNEL_ENTRY >> + FENCE_SWAPGS_USER_ENTRY > > This change is wrong. > > In the paranoid entry path even if user GS base is set then the entry > does not necessarily come from user space so there is no guarantee that > there was a CR3 write on PTI enabled systems before the SWAPGS. > > FENCE_SWAPGS_USER_ENTRY does not emit a LFENCE when PTI is enabled, so > both the comment and FENCE_SWAPGS_KERNEL_ENTRY which emits LFENCE on > affected CPUs unconditionaly are correct. Though the comment could do > with some polishing to make this entirely clear. I didn't notice FENCE_SWAPGS_USER_ENTRY depends on PTI. I will add FENCE_SWAPGS_KERNEL_ENTRY only on the kernel path. Thanks Lai