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 X-Spam-Level: X-Spam-Status: No, score=-14.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17B82C433F5 for ; Thu, 9 Sep 2021 23:17:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE4D361074 for ; Thu, 9 Sep 2021 23:17:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348844AbhIIXSe (ORCPT ); Thu, 9 Sep 2021 19:18:34 -0400 Received: from terminus.zytor.com ([198.137.202.136]:57553 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234743AbhIIXSd (ORCPT ); Thu, 9 Sep 2021 19:18:33 -0400 Received: from tazenda.hos.anvin.org ([IPv6:2601:646:8600:3c70:7285:c2ff:fefb:fd4]) (authenticated bits=0) by mail.zytor.com (8.16.1/8.15.2) with ESMTPSA id 189NGtkD254001 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Thu, 9 Sep 2021 16:16:55 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 189NGtkD254001 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2021083001; t=1631229417; bh=um8aeCfl6b9E+G9YaI17v2BeFtCJRFzEbr9r+28x1bU=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=W9DHJYR6P8ex4XsXLT/g29un+MoNFvdlMF3YTA0YsYdQM16sR0UQxsThV4KyOIXUY qshi90Hzl3HqIcUCWpu5SXAoT8EhYOKbEjMWuumJ4AlVbki2pBGPcRHY3dTfeqskpC HaYGqZD/LCF68NhIK8/DpXMpNgm3jZhjU9TjxL/pmRk94cShI9WuAgPsxLjrKbKt3D YJ4i9Ox8TurF8NOM5QQo9NDK/rEFGLxMNn63GqVnr1QirVYPz2uM2xWnMTKK3EFyao eQVMuqTjuaUmGr7pPP+5ZOBSCzuSAt/Gc8vcrS+h+yMWQlRiySJMVC/Qjel6wgrfCY 25gFsq+5rKjOw== Subject: Re: [PATCH 25/24] x86/traps: Rewrite native_load_gs_index in C code To: Lai Jiangshan , Lai Jiangshan , linux-kernel@vger.kernel.org Cc: Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, Dave Hansen , Peter Zijlstra , Dave Jiang , Ben Widawsky , Dan Williams , Arvind Sankar References: <20210831175025.27570-1-jiangshanlai@gmail.com> <20210902105052.2842-1-jiangshanlai@gmail.com> <9fdb04b1-dbb8-069d-f5ef-d4e8c0f2a83e@zytor.com> <638f3b2b-aff9-72e5-3a5d-fff5ef6b88fc@zytor.com> From: "H. Peter Anvin" Message-ID: <43f7935a-e9d9-7bb9-372b-079d638205d8@zytor.com> Date: Thu, 9 Sep 2021 16:16:50 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------B5BB011F5117831604F431EB" Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a multi-part message in MIME format. --------------B5BB011F5117831604F431EB Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Come to think about it, I think it would be better if we were to incorporate the X86_BUG_NULL_SEG null segment load in into the main native_load_gs_index() code while we are swapgs'd anyway. This simplifies the code further, and should avoid calling native_load_gs_index() twice if this bug is enabled. (This is the traps.c code only; removing the now unnecessary instances is left as an exercise for the reader...) -hpa --------------B5BB011F5117831604F431EB Content-Type: text/x-patch; charset=UTF-8; name="traps.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="traps.diff" diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index a58800973aed..5de423a24777 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -671,6 +671,48 @@ DEFINE_IDTENTRY_RAW(exc_int3) } #ifdef CONFIG_X86_64 + +/* + * Reload the %gs selector for user space (setting GS_BASE) with + * exception handling: if the selector is invalid, set %gs to NULL. + * + * selector: new selector + * + * This function is noinstr as it must not be instrumented. + */ +noinstr void native_load_gs_index(unsigned int selector) +{ + unsigned long flags; + + local_irq_save(flags); + +again: + native_swapgs(); + + if (static_cpu_has_bug(X86_BUG_NULL_SEG)) { + if (likely(selector <= 3)) + asm volatile("mov %[seg],%%gs" + : : [seg] "rm" (__USER_DS)); + } + + asm_volatile_goto("1: mov %[seg],%%gs\n" + _ASM_EXTABLE(1b, %l[bad_seg]) + : : [seg] "rm" (selector) + : : bad_seg); + + alternative("", "mfence", X86_BUG_SWAPGS_FENCE); + native_swapgs(); + local_irq_restore(flags); + return; + +bad_seg: + /* + * Invalid selector, set %gs to null (0). + */ + selector = 0; + goto again; +} + /* * Help handler running on a per-cpu (IST or entry trampoline) stack * to switch to the normal thread stack if the interrupted code was in --------------B5BB011F5117831604F431EB--