Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1060577rdg; Fri, 13 Oct 2023 09:05:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG1vL59xWQC1RVwyofL6DUFYiXnr5yllF0do0cdoC7CiWTME0+LKNpCOkATEyEUvPOq6i/C X-Received: by 2002:a05:6a21:181:b0:15e:dca8:1224 with SMTP id le1-20020a056a21018100b0015edca81224mr29729380pzb.55.1697213108590; Fri, 13 Oct 2023 09:05:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697213108; cv=none; d=google.com; s=arc-20160816; b=gIf/m0cjReIsFBdEIVQnkK69pBvC0tDgufbLBJCRpKI95HYW1Vy0ITcEG9LJTLInbW /c6FH/J4U1ZriCl7rwAqRids4qqnZP5Jt+bDZpwfd88Z8FEmUwokWGso1LeOxyjL2dxf Z1Gttg9dzrb7VveovXLMT3gBZ06LTX+EpQZ8Q//lOooEd1Mt/rYPtZTjJKJBejKPVwVi 3SSV8aWdKzmtWKBueNZ61tK8WNvlHhIAi7b1qejygDig6qz494ArCPpmoRYre6Ip7Lv1 tFixNkekAAjBOAhOnrds4iaD+aXfc67WvR3Mqh4eabJnsqCrB6bCGo6bZ96BJbACCr2I 9ozQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=ADc+BeDNpPzla2tCfp6/O/BMhR8EnDpGynEOWTUgWb8=; fh=J8duQSDL0w/CFgy0sRZWddo0zMt/r9VCVuDyIxZshFU=; b=BPub3Yz2HYGa6b8se5UU8/ylsgnxpFdLZvsQyVkQXg1xwrqqZO8UOtY9lDehIsFV4r +GhKt2MHkiSw44RVfhv6NG2exSx7DWpuYHH1H2FoMiyh0B/qelRQ5joSOIi3jSrK0wBN ck+EhwKwZK7HrHKg93aA0LF/ycvwueFSOFAhpMLoVPa0FXof92qONlfZdGRYMn+O8Gdd /u0QAGULhmCeXS2QZaCN5V58p5ZXeFYmAC91wl4pWJAZXtDEdRPM9WQgkDMRJ/tw57fr srKQ/Yz/gI7HJ6bgaBZB8qGyMwZjViHjbm3upLXsR83Y1XXy89uY7xCZAc9uMlxg38bC 3NmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gxm26gU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id t16-20020a056a0021d000b00690cc6f6f38si17562117pfj.76.2023.10.13.09.05.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 09:05:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gxm26gU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C2C51826EC8B; Fri, 13 Oct 2023 09:05:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232505AbjJMQE6 (ORCPT + 99 others); Fri, 13 Oct 2023 12:04:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232444AbjJMQE4 (ORCPT ); Fri, 13 Oct 2023 12:04:56 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 459C7FF for ; Fri, 13 Oct 2023 09:04:54 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-27d10ef87caso1894493a91.0 for ; Fri, 13 Oct 2023 09:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697213093; x=1697817893; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ADc+BeDNpPzla2tCfp6/O/BMhR8EnDpGynEOWTUgWb8=; b=gxm26gU4AUd54mmMxn/0KxpyCzlPKtW4pmThbi2fgl6sxzH1iQW7Z89EFXyJskB01A J+Dh7icAWML1mszE/kYY9Cyn0N3c6PeSAo0XWwwdYyaDMkut9tRS+SxEnqgdAcw3bJ30 1zau014ut4E36eDXgXawVX/wRRyWbe9ozn8drAz1w3t81aLkrVnlyKWEAD4VlnmgGh1o pfdeDNpj477+RdcU086FTEg7poBqrZ7UwmlnaqEdiy0siNVDropCFYn926f6iujgTpEe vDCXms9EowmxBBFTauKIJip7ALDd66qEKzKzUecT9aWRCVZY9rIeNoeHvxM7pWQDbXa9 XtvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697213093; x=1697817893; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ADc+BeDNpPzla2tCfp6/O/BMhR8EnDpGynEOWTUgWb8=; b=syHDV2mhmLWcRNPGWmK4XRbXg/3+GRiIkVuOKxvl/yoUQKWXEiFkRd/c1Y0mj7SSJo C3ypb2XB5+DwHrPj93wdJ8FLwA1UZ2Un4GtElsk+KIX2nA9PC+dHKSV6ubPerqZPP2J9 Z96VOO46rHa6oXu8+pjhtw/JTud4qFmpq0epQshBHPrN7nS4Gvv9D3U/8FCr7NxNqnfg E1jhMk3zvEnxyfUH+f7imydJdlaj4uzUOuMq3wAB4kukrJNXJAuuU2liDr4hQjjiwTYF cCdcmAhKCvYBoy2PP9b08QWFClJxREagdlc0Mgpm8PFf/950Gh7DK28JKeSK3X8K6lew Dv+w== X-Gm-Message-State: AOJu0Yw2piWOlAr2suiOsTPGoJQgLXN1Y4TLGJ2Vv9hu01t0nLQMhaf4 smBQPuaBEWpVjKyGXV4Y6WaTldiKxhI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ab47:b0:1ca:1a30:cf34 with SMTP id ij7-20020a170902ab4700b001ca1a30cf34mr14339plb.11.1697213093615; Fri, 13 Oct 2023 09:04:53 -0700 (PDT) Date: Fri, 13 Oct 2023 09:04:51 -0700 In-Reply-To: <20231011204150.51166-1-ubizjak@gmail.com> Mime-Version: 1.0 References: <20231011204150.51166-1-ubizjak@gmail.com> Message-ID: Subject: Re: [PATCH tip] x86/percpu: Rewrite arch_raw_cpu_ptr() From: Sean Christopherson To: Uros Bizjak Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds , Nadav Amit , Ingo Molnar , Andy Lutomirski , Brian Gerst , Denys Vlasenko , "H . Peter Anvin" , Peter Zijlstra , Thomas Gleixner , Josh Poimboeuf Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 13 Oct 2023 09:05:06 -0700 (PDT) On Wed, Oct 11, 2023, Uros Bizjak wrote: > Additionaly, the patch introduces 'rdgsbase' alternative for CPUs with > X86_FEATURE_FSGSBASE. The rdgsbase instruction *probably* will end up > only decoding in the first decoder etc. But we're talking single-cycle > kind of effects, and the rdgsbase case should be much better from > a cache perspective and might use fewer memory pipeline resources to > offset the fact that it uses an unusual front end decoder resource... The switch to RDGSBASE should be a separate patch, and should come with actual performance numbers. A significant percentage of data accesses in Intel's TDX-Module[*] use this pattern, e.g. even global data is relative to GS.base in the module due its rather odd and restricted environment. Back in the early days of TDX, the module used RD{FS,GS}BASE instead of prefixes to get pointers to per-CPU and global data structures in the TDX-Module. It's been a few years so I forget the exact numbers, but at the time a single transition between guest and host would have something like ~100 reads of FS.base or GS.base. Switching from RD{FS,GS}BASE to prefixed accesses reduced the latency for a guest<->host transition through the TDX-Module by several thousand cycles, as every RD{FS,GS}BASE had a latency of ~18 cycles (again, going off 3+ year old memories). The TDX-Module code is pretty much a pathological worth case scenario, but I suspect its usage is very similar to most usage of raw_cpu_ptr(), e.g. get a pointer to some data structure and then do multiple reads/writes from/to that data structure. The other wrinkle with RD{FS,FS}GSBASE is that they are trivially easy to emulate. If a hypervisor/VMM is advertising FSGSBASE even when it's not supported by hardware, e.g. to migrate VMs to older hardware, then every RDGSBASE will end up taking a few thousand cycles (#UD -> VM-Exit -> emulate). I would be surprised if any hypervisor actually does this as it would be easier/smarter to simply not advertise FSGSBASE if migrating to older hardware might be necessary, e.g. KVM doesn't support emulating RD{FS,GS}BASE. But at the same time, the whole reason I stumbled on the TDX-Module's sub-optimal RD{FS,GS}BASE usage was because I had hacked KVM to emulate RD{FS,GS}BASE so that I could do KVM TDX development on older hardware. I.e. it's not impossible that this code could run on hardware where RDGSBASE is emulated in software. [*] https://www.intel.com/content/www/us/en/download/738875/intel-trust-domain-extension-intel-tdx-module.html