Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp187851pxb; Tue, 7 Sep 2021 21:47:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKfFYROmTg+tbRUfd0nAQh8rgw0XiN9otnbCWAJBD3CsJGimdd5O+wVY5CXW6tPTVj6wWj X-Received: by 2002:aa7:dd48:: with SMTP id o8mr1888268edw.44.1631076425906; Tue, 07 Sep 2021 21:47:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631076425; cv=none; d=google.com; s=arc-20160816; b=tEiuQQkiBM9Jv9E0MlSixCFi8MPbw6TU+431WasLUTqzaPOPxICoZCmE/s8adLum2M 1RQ8y9Rr0rOviLlenZ2pC1VDBILY2jH+XgZXAjBgfrh/YDK9WYDi8caPDgU2aZFj1i+S e3vcGEbTP0nyD5BxlqUpHKVQDRXqIoTU5g9CI7zHEkXo3G0kZ/GrC9Je9hKW3iPyPYWv MMjCb/xSax6VFp4a48MoVn1K1s+gCUiu4Mzrn2R30a3cPOOXbLIWDXmc+RTgdcJPA9xF DmaJptmB+cxJJSTvN/KCBa0RRN1Zd6HA8UQXlSHUGXseHfztPCXFYkZ15ZxccBWNaYq6 kagQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject:dkim-signature:dkim-filter; bh=DnirN5yaca29dOLsh4n68jgYNnFGlrSSGZfm3YYegs0=; b=D9MA0zZMgEdZqh0VQIl3jSci0PhhrajWWXeC2qYHupgFjdUJWDj/1jaKXDL379TY8e VTeBvtCzp0jyPdZCHI1dHC01PitS2GFO0ZhVHr3otwT3KUZ2ropF32oqgqzG1ZEROdlv O1oo3sMBSw+TdAYlkChYCnRQ79bJRlvkCMGRzLtxS1TrLaxpdnItsTzu52S4zFrwSF5d MoyVC8SIzOFHmBFkHCh/viUrZuCCUo30b7NLI7RyNWC6VnTtiOqo9un5jemZmV66mYqm PesfBXcjVUQHEsqbPS4clbUhQ610HD1MnFnsMhRVjxhpPQQVk9XKY13RphLXuTkYPKr5 QoDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2021083001 header.b=dqSY2N0Q; 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 w18si946151ejv.669.2021.09.07.21.46.42; Tue, 07 Sep 2021 21:47:05 -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=dqSY2N0Q; 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 S230233AbhIHEoX (ORCPT + 99 others); Wed, 8 Sep 2021 00:44:23 -0400 Received: from terminus.zytor.com ([198.137.202.136]:51857 "EHLO mail.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229988AbhIHEoW (ORCPT ); Wed, 8 Sep 2021 00:44:22 -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 1884gmMC4009948 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Tue, 7 Sep 2021 21:42:49 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 1884gmMC4009948 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2021083001; t=1631076170; bh=DnirN5yaca29dOLsh4n68jgYNnFGlrSSGZfm3YYegs0=; h=Subject:From:To:Cc:References:Date:In-Reply-To:From; b=dqSY2N0Q/8vJvbsnX0Yj8YIcg68vKIE2atnp7dU++urrugHLoq+EJt2Dwiqs/oLu0 O6qVPRkdxLd2Hf+ncBpvXsw3E8ion33RdFiLF5YVFu8gvWNx2BXMJmn02WzqySmxY0 k+/VdnhEa7jMqT135Fvcwhhon/BVkrnBioUGAbFXzIk1+7gaQVcQyZrjlJJ3xSxp0t MXrbWtBLbxe9y1SD02YduZIRukR8wjKj/J30OEN3MsFABLKrT/nBzZwAxMUWZQq/PC XuInz6eI5u2rpJCp7oM1zv9M7OHKBcXSc60ecHryni4PmTwSR9/KG3xH+gtiUtyXm1 osfH0g8eQ/n5A== Subject: Re: [PATCH 25/24] x86/traps: Rewrite native_load_gs_index in C code From: "H. Peter Anvin" To: Lai Jiangshan , linux-kernel@vger.kernel.org Cc: Lai Jiangshan , 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> Message-ID: Date: Tue, 7 Sep 2021 21:42:43 -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: <9fdb04b1-dbb8-069d-f5ef-d4e8c0f2a83e@zytor.com> 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 9/7/21 6:38 PM, H. Peter Anvin wrote: > On 9/2/21 3:50 AM, Lai Jiangshan wrote: >> From: Lai Jiangshan >> >> There is no constrain/limition to force native_load_gs_index() to be in >> ASM code. > > Hi, > > First of all, let me say I really like your patchset, and I will try to > review it in detail ASAP (most of the initial read pass looks very sane > to me. > > However, I would like to object in part this specific patch. It adds a > fair bit of extra code to the exception path, and adds jumps between > files which makes the code much harder to read. > > You end up doing one swapgs in assembly and one in C, which would seem > to be a very good indication that really isn't an improvement. > > Note that this entire sequence is scheduled to be obsoleted by a single > atomic hardware instruction, LKGS, which will replace ALL of > native_load_gs_index(); it will no longer be necessary even to disable > interrupts as there is no non-atomic state. In that sense, doing this as > an out-of-line C function (with some inline assembly) is great, because > it makes it far easier to use LKGS as an alternative; the only (small) > disadvantage is that it ends up clobbering a lot of registers > unnecessarily (in assembly it can be implemented clobbering only two > registers; one if one uses pushf/popf to save the interrupt flag.) > OK, here is a version which actually compiles: (It makes me wonder if it would be useful to have an _ASM_EXTABLE_GOTO() macro to highlight the use of an asm goto C label.) noinstr void native_load_gs_index(unsigned int selector) { unsigned long flags; local_irq_save(flags); native_swapgs(); do_mov_gs: asm_volatile_goto("1: mov %[seg],%%gs\n" "2:\n" _ASM_EXTABLE(1b,%l[bad_seg]) : : [seg] "r" (selector) : : bad_seg); alternative("", "mfence", X86_BUG_SWAPGS_FENCE); native_swapgs(); local_irq_restore(flags); return; bad_seg: selector = 0; /* The exception dispatch will have restored kernel GS */ native_swapgs(); if (static_cpu_has_bug(X86_BUG_NULL_SEG)) asm volatile("mov %[seg],%%gs" : : [seg] "r" (__USER_DS)); goto do_mov_gs; }