Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp956270pxb; Thu, 9 Sep 2021 16:18:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0pojtkdG01p/xKLZG6gfzBlQTLkG9FLIo+27jxJ5hsNL1AWTjIaKTHJAaM74M9FPBIK/+ X-Received: by 2002:a92:c051:: with SMTP id o17mr3973729ilf.308.1631229500332; Thu, 09 Sep 2021 16:18:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631229500; cv=none; d=google.com; s=arc-20160816; b=ZzBbZ8DBjYGZ7HpAHpmhnZ9IYC7ky5Ms8HDXDJba76OgQFsBERDexKOZOoGkjA7ogW F8cu5y5yVwClPXgtUfU/iCucIpt5KOLlSrqP0V0Kmli5+cNv0UxRr8nOsFlt4wUcyzG1 RVs3qS8VVUX1vDyP6dyHSAvcMO2tjhHEFk2nquUwJyz/KajUl9DYoEV7oVsX9cfmfrWY TzP6PpwFUCGZWM8pUUTh7Wwi0s0bm4kL2xNCnxL3X6UfLuVKJ/Ade6zqoXznkmATyL2B xPIIX2hE1N5ZyCQ+QWOAZxUnUz/vgig3HMhM22P+VdLBD22ct3NUIV7FfNBl9jhW3LK/ x33w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:in-reply-to:mime-version :user-agent:date:message-id:from:references:cc:to:subject :dkim-signature:dkim-filter; bh=um8aeCfl6b9E+G9YaI17v2BeFtCJRFzEbr9r+28x1bU=; b=A5iVF/T7/lRkkGo+jKgMQGuhqCu6seOkEDo1P03+6QAunl7meYXV1rMMPkQynhhpFc vzKOrXLo8ebnkOG89b92YzACyZztASo4Gn0wN5Qjq2nO0tiGwxR/IhYG7C+ZQF+axMBE WuAoMk15qfzDBZtNIItBPigpZNK1jPbKPsmAQ0zQ5t/RO/J5YdZIXhDNDm4Cvb+w7odJ 44ugBzxH174KmFaz6nxt1KLEfnBDyllfZWMJb3u8G+YUmiI+doKQvDHSWCzVKq7ZlAqc gO+d2lu4YT4+zKLjFIYaGYQCrL3b3X7MGtRAkd6tNomeiZbv9vgveNcTkPVvH132CJd8 Cdmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2021083001 header.b=W9DHJYR6; 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=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si3191961ilh.176.2021.09.09.16.18.08; Thu, 09 Sep 2021 16:18:20 -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=@zytor.com header.s=2021083001 header.b=W9DHJYR6; 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=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348314AbhIIXSe (ORCPT + 99 others); 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--