Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3980121rdg; Wed, 18 Oct 2023 11:09:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7gz8I+v5EGvi5KAWFZPIUlaPTaMP7/ds6wbMsykXmqp0RLCokBLbP9GcFRkEmurohgpw1 X-Received: by 2002:a05:6a20:7494:b0:171:4921:f1b7 with SMTP id p20-20020a056a20749400b001714921f1b7mr5960981pzd.41.1697652539665; Wed, 18 Oct 2023 11:08:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697652539; cv=none; d=google.com; s=arc-20160816; b=USarIwAt+5C9bX49G0byhiZim+N0I2vT8X1p5KV6k0oW/g5cGUL5UwgdxW2km4HJeF hmub7NomDLhiWAVAwxu7a/GSctggKaVJRs2Ges6G3iOEWpUPcLrvGCfuiaVLlGf2mI4s kML+5J8pS8V2j/JK9Q5wnySMy3mul2bttthNYZTvLET+OCZ7pjcaqmgDckIbXBPFdnrI xsFe1R2gSEk9q7a0T/A7kLdz1RJnmRXFfopL8UwF3wgUJnINnrOsuf5PkmsqUABIXxZc lyFGzRWm5KPKloNGV5/dnqcy0UinqovPrhdeYeo/LNJDZBpuGqlBQpUceF+DUG56Grha p6hQ== 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=LMlAH6aGo5Pg/afWqCBzzwAyO5+Bhz51dc20z6ZimfQ=; fh=/Dnf++/2xG0Xi30TGscvcX3UaoYjzLSxPPap0bUcRsE=; b=JdKs3X08O578wH93DWw6McF+Z0nE87yjQz0EarpNxAyhFeljH76zGoMoDd9qXNe6xr ARrDl/t3VPZktGhmhB5HEHcvEPYV2UaX0rGViqs6StDlBIcz64OusqmG2DEBTJ6j9IH/ kBLPbkKx2kWY0u4NW7+3vaQH2pTu+tqxCTCz3Z7D59hZ2xQtWIsCJDBxNZ/CXGc92dvS zM15OYoWqXnk3qpqawmmP+gkjMQrDYqari8tm6vNEbZCZDo5PjjJEvZJ1CkdIlBTaful pE3WCG58R7mZ5PFvfSOxRaTE60WgdEr/uD3aAMRk0MdTXHhbt0TyYYQVzsieLT85gnhA wgjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nIrsB2iz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id be3-20020a656e43000000b005b7d939c3d9si2683967pgb.651.2023.10.18.11.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 11:08:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=nIrsB2iz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id C5D6E82429B1; Wed, 18 Oct 2023 11:08:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230088AbjJRSIh (ORCPT + 99 others); Wed, 18 Oct 2023 14:08:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229697AbjJRSIg (ORCPT ); Wed, 18 Oct 2023 14:08:36 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD4F8B8 for ; Wed, 18 Oct 2023 11:08:34 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-53dd3f169d8so12069075a12.3 for ; Wed, 18 Oct 2023 11:08:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697652513; x=1698257313; 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=LMlAH6aGo5Pg/afWqCBzzwAyO5+Bhz51dc20z6ZimfQ=; b=nIrsB2izxcIZIwzrssM5o8wlhhFZVKARvua1fR6flKAdkelc32j95c6RBi2FEerapf xL8dAjnOgDhFcAi0Jl0XeMzZc6sYEWkAw4lgFR3ejIy+AmmoF/0//1OVm7a+2VJBEBAW +qXghmKTTw27j37sL0IDWhlk7BMtb8daSGNXHovB79WeBY//9rMiTcd1VD4A+2aGzx3u RXS+/XlVj4qdJWS0wkjWiCQu7eoW4Gjy67p55bcBERCgTKzbzFQuAqeV+mOIxo38dNeg epiBtslpypj07OpNYoIGyOia8i/Yc9ZSYKbhLM/DuZXrmsGbj1fufwbd5LFl/oOo7Q5i IoPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697652513; x=1698257313; 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=LMlAH6aGo5Pg/afWqCBzzwAyO5+Bhz51dc20z6ZimfQ=; b=xUUTh1kVk7xcLzh3TtU7kv/4m2aCznnRBefvGHFBSI9V0G9AAUFbs9IwyYZrAX5XvU K2+LvZ61Tt0JsuV3mBif7UnQ9OfkGbfonccX370HlKaMuzIbuqKg9pFHcmw0sUCmJcnw docbgRtXLSl2B0wgdi3YS+QKz3TsV/WsrrvJrntmzXJ8a9mNBScgB5gyD7FEZlXJCXYi H98fDBL8ZyWnS9zeX2LvGTR07H26Q65EERRKP1ehPhY4zYd6NRw0x1QlWGH9h8D+3EES KRG4Kj546neGt0jS0AG+2QfIKtm3ZNUq2asK1VIBeElwZgB/bKi6GQxDv1FzF4KHFTbg MddQ== X-Gm-Message-State: AOJu0Yx4V7tb60F2Z7Ei9VdDAtkmwH6JS54P90N+1hLNOFVsJon0zHIc D/FtBRUNX6T7OGdwO211a7vbt6ccM3Y3nlnmhP0= X-Received: by 2002:a05:6402:510d:b0:53d:e8a7:57a6 with SMTP id m13-20020a056402510d00b0053de8a757a6mr5185458edd.34.1697652512998; Wed, 18 Oct 2023 11:08:32 -0700 (PDT) MIME-Version: 1.0 References: <20231010164234.140750-1-ubizjak@gmail.com> <0617BB2F-D08F-410F-A6EE-4135BB03863C@vmware.com> <7D77A452-E61E-4B8B-B49C-949E1C8E257C@vmware.com> <9F926586-20D9-4979-AB7A-71124BBAABD3@vmware.com> <3F9D776E-AD7E-4814-9E3C-508550AD9287@vmware.com> <28B9471C-4FB0-4AB0-81DD-4885C3645E95@vmware.com> In-Reply-To: From: Uros Bizjak Date: Wed, 18 Oct 2023 20:08:21 +0200 Message-ID: Subject: Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr() To: Linus Torvalds Cc: Nadav Amit , "the arch/x86 maintainers" , Linux Kernel Mailing List , Andy Lutomirski , Brian Gerst , Denys Vlasenko , "H . Peter Anvin" , Peter Zijlstra , Thomas Gleixner , Josh Poimboeuf , Nick Desaulniers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 18 Oct 2023 11:08:51 -0700 (PDT) On Wed, Oct 18, 2023 at 6:26=E2=80=AFPM Linus Torvalds wrote: > > On Wed, 18 Oct 2023 at 09:03, Nadav Amit wrote: > > > > Having said that, I am not sure what other usages you have in mind. > > =E2=80=9Ccurrent=E2=80=9D is a pretty obvious straight forward case wit= h considerable > > impact on code generation. There may be additional variables, but it is > > likely that there would be more functions/TU in which they would not be > > constant and would require more refined techniques to avoid mistakes > > such as the use of stale cached values. > > Yeah, I don't think there really are other cases. > > We do have things that could be considered stable (like > "smp_processor_id()" which is stable as long as preemption or > migration is disabled (or it's in an irq-off section). > > And it might be lovely to optimize those too, *BUT* that would require > that there be a barrier against that optimization that works. But loads from non-const memory work like the above. Please consider: --cut here-- extern __seg_gs int m; int foo (void) { int r; r =3D m; r +=3D m; asm volatile ("" ::: "memory"); r +=3D m; return r; } int bar (void) { int r; r =3D m; r +=3D m; r +=3D m; return r; } --cut here-- gcc -O2: foo: movl %gs:m(%rip), %eax addl %eax, %eax addl %gs:m(%rip), %eax ret bar: movl %gs:m(%rip), %eax leal (%rax,%rax,2), %eax ret Please note the __barrier(), implemented with asm volatile. > And if there is anything that this thread has made clear, it's that > the whole 'load from a constant section' doesn't seem to have any sane > barriers. > > So while the CSE for inline asm statements is a bit too weak with that > whole "only CSE within a basic block" thing, the CSE of "load a > constant value from memory" is too *strong*, in that we don't seem to > have _any_ sane way to say "now you need to reload". We can use alias to __seg_gs non-const memory, so the value can be accessed without asm. __barrier() will then force reload. Please note that any memory clobber, hidden inside asm will also force reload. > The traditional way we've done that is with our "barrier()" macro, > which does the whole inline asm with a memory clobber, but even that > doesn't act as a barrier for gcc optimizing the constant load. > > Which means that while we'd probably love for the compiere to optimize > smp_processor_id() a bit more, we can't use the 'stable memory > location' trick for it. We should get rid of asm statements, and as shown above, __barrier() will do the trick. > Because I can't think of anything but 'current' that would be _that_ > stable as far as C code is concerned. Uros.