Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp328924rdg; Tue, 10 Oct 2023 11:23:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGl712vc35AGhzAnhjqB3e01fGPjefCOIWBYDBSFXoTHnKK92RDMvt7Xx20fYYBI/Kuqp1O X-Received: by 2002:a05:6808:2189:b0:3a9:7634:23f9 with SMTP id be9-20020a056808218900b003a9763423f9mr26534604oib.12.1696962193717; Tue, 10 Oct 2023 11:23:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696962193; cv=none; d=google.com; s=arc-20160816; b=txcmlALqJN5w3HS32albKi7QO4cHHY8tdDGBNAfmzwcavPIFw6visawy4qCVUMKoz8 LeJ4WictdAq0L3UkPUX2Bptf6RgJiCekTKY9cDGi4DfdSTTVfMFJohVGTa1ddb/zc9E+ OTt0k0NsP0cjBqSSe0Vdr978f75L1+kxGIc6fbM0BuIHs5tjV6mkzdibEk58OCROqSaw sVS4V65D4HE2fG7oQ+DXxR9wJz/WRr93EPd7C+dNoCrNqOoArMbYD7QjvC1bU8hAKR2H FgJXKaggyG2+4HrCIum2GizNMZTXgDdMT2OUpWmuvknjs96BcobT4q7st1rS9JueTteg bT+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=fs+iXb7PDw/X1rjcS4aBKNQ3kcels/TfVBa5gZG+NI4=; fh=tF7riTkuBa8O3vMSvJNFl/jVCIdpn/sknKnZT2xFBLQ=; b=SFblq40v8h3tnyillw6KSjTrNqL5BkF+rEQtqwiD5H4YCg8OcyYrr4jK6Qf32VPHRA trSVffSRuG93D3Q80WsNIQoGf/mzcJo1nvlQYhH4gRqkteMbQcfeYM0If6ExU1HfgtTS MOZAUj/KpauWuhCXiHdllJEsfBqVW7BR7WFVtXtCdbFqp9oFS12PP6QF7wTpkY/Zoykr bCPgPxSHzoU0lcCNl9Z3yRnHqLJwCg5Ia+X0VChi6CgIZ2BmVFK0x7GPC7Mqjxed5i5s 4HHpeHh5UPrfIeEsIKrmM0tLEh9jwVwZik9qNuB9jG2J3ER6LyT+3rIH8ZKCXlUq3Yrg ShnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DsujLZyC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id 129-20020a630187000000b00589867d220asi2340274pgb.254.2023.10.10.11.23.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 11:23:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DsujLZyC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id E9514807AC73; Tue, 10 Oct 2023 11:23:10 -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 S233931AbjJJSW6 (ORCPT + 99 others); Tue, 10 Oct 2023 14:22:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233117AbjJJSW5 (ORCPT ); Tue, 10 Oct 2023 14:22:57 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6C299E for ; Tue, 10 Oct 2023 11:22:55 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-5384975e34cso10690937a12.0 for ; Tue, 10 Oct 2023 11:22:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696962174; x=1697566974; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fs+iXb7PDw/X1rjcS4aBKNQ3kcels/TfVBa5gZG+NI4=; b=DsujLZyCXs/SQEXdOCEMzdj66yidKMaCoTQzhK3YvUCHFDE66PArl87OSmffiylGx6 HSGe0N14gKrieAXK9NM3Xeh8SxZDEkrLkt9tfkj3pOKEOTtmpBDnwMbDc1REzxfbxod7 uDPfzvaqDuZIii0z5SHYSPQz+riL1UL/PjhnfDFdzx35XDPuRBG6qrOfGsEOU2ztRie7 4mHzabs0AogP9i4HC/03XJFPk2+U6yf5h0tsMWHoGgKLSDuDwmf4B3Com0Q1YzwXAFrP /aQ5YdcR5xWiKE2ysxJwqKrkQmD3EgzCnJJtrpCQ9Nen7V6oehDdSQRLizXIWgr1vmaI roug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696962174; x=1697566974; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fs+iXb7PDw/X1rjcS4aBKNQ3kcels/TfVBa5gZG+NI4=; b=WzNdT5E79fmxJ/RIHhIP/TWEtx6fvI3PGxhlRtZvL+MpbrYJo80NGlB3aMVTzezFGQ ws/H1BFQkbh5hy58WcYU9jIxqfGm3i1NYEdLuXUkZ0gdljZpBLDri4gccJlIiOZnBqQ7 qtlYnwerQlGrEcpIXyU04b9M21oWi51LyGVvt7NoRkLmNcREVgA2zwRL4y8orELFS5me h81Rd+DdJ3dPJCA2duoH4vEm2qOwlPPq/V4yQjoKGy6tq/O+i2ILyaad3ljB/TWR9snN f4O/yyI9wEYDhP59Abrom0VS0x/tCDwxHk6g5B/q5C/dxBWZQeFQq5eW3CUOUJ/s2c3e TvYw== X-Gm-Message-State: AOJu0YwZjdnKlSfg7+LJz/Jvd41RqKj64ULgNOnJzH8iat3rCunr1iS3 d5suQo0NOFwJ1ZTwP0dMeMFwvzXZ4/2HKKs0TN8= X-Received: by 2002:aa7:c493:0:b0:532:c690:92c8 with SMTP id m19-20020aa7c493000000b00532c69092c8mr16291264edq.31.1696962173905; Tue, 10 Oct 2023 11:22:53 -0700 (PDT) MIME-Version: 1.0 References: <20231010164234.140750-1-ubizjak@gmail.com> In-Reply-To: From: Uros Bizjak Date: Tue, 10 Oct 2023 20:22:43 +0200 Message-ID: Subject: Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr() To: Linus Torvalds Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Nadav Amit , Andy Lutomirski , Brian Gerst , Denys Vlasenko , "H . Peter Anvin" , Peter Zijlstra , Thomas Gleixner , Josh Poimboeuf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no 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]); Tue, 10 Oct 2023 11:23:11 -0700 (PDT) X-Spam-Level: ** On Tue, Oct 10, 2023 at 7:32=E2=80=AFPM Linus Torvalds wrote: > > On Tue, 10 Oct 2023 at 09:43, Uros Bizjak wrote: > > > > Implementing arch_raw_cpu_ptr() in C, allows the compiler to perform > > better optimizations, such as setting an appropriate base to compute > > the address instead of an add instruction. > > Hmm. I wonder.. > > > + tcp_ptr__ =3D __raw_cpu_read(, this_cpu_off) + (unsigned long)(= ptr); \ > > Do we really even want to use __raw_cpu_read(this_cpu_off) at all? Please note that besides propagation of the addition into address, the patch also exposes memory load to the compiler, with the anticipation that the compiler CSEs the load from this_cpu_off from eventual multiple addresses. For this to work, we have to get rid of the asms. It is important that the compiler knows that this is a memory load, so it can also apply other compiler magic to it. BTW: A follow-up patch will also use__raw_cpu_read to implement this_cpu_read_stable. We can then read "const aliased" current_task to CSE the load even more, something similar to [1]. [1] https://lore.kernel.org/lkml/20190823224424.15296-8-namit@vmware.com/ Uros. > On my machines (I tested an Intel 8th gen laptop, and my AMD Zen 2 > Threadripper machine), 'rdgsbase' seems to be basically two cycles. > > I wonder if we'd be better off using that, rather than doing the load. > > Yes, a load that hits in L1D$ will schedule better, so it's "cheaper" > in that sense. 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... > > Yes, yes, we'd have to make it an alternative, and do something like > > static __always_inline unsigned long new_cpu_offset(void) > { > unsigned long res; > asm(ALTERNATIVE( > "movq %%gs:this_cpu_off,%0", > "rdgsbase %0", > X86_FEATURE_FSGSBASE) > : "=3Dr" (res)); > return res; > } > > but it still seems fairly straightforward. Hmm? > > Linus