Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp633809rdg; Wed, 11 Oct 2023 00:28:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEn1cR5exsteptGBleW4TjhoL0SQ1p6n4OobwbNh/xvpu8QGtK05eBpVyFfwJgqdz+ZEwcn X-Received: by 2002:aca:2309:0:b0:3ae:a81:55ba with SMTP id e9-20020aca2309000000b003ae0a8155bamr23453143oie.22.1697009312465; Wed, 11 Oct 2023 00:28:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697009312; cv=none; d=google.com; s=arc-20160816; b=aRk9CAm4jtcH/tUsitQ5mAcb/4/n9y/xY8uakEj8lnmCFFi7XyErML9hrPdmZWJIjF e+kWvLTQSKC/ak97LuoTtxWI4aXHfR7D9qnVTBM/vMRwTB7bBxSYWdQ8lhjbpPqLzbgE B9JU7jtYng0Mhn86L+oFi+PfqZ7zBH10HF7BTS584bFkCYOiq7XyVPaVSdh81qrjvbe0 ereevjjauWy4Y4E4QksZDVdnuwAwWCghXvF7ZfqebPlz4GXvLB+3hLnOqfaz+GtcR64f D/O0gMNkggWkrRgtvwAIkx6gooJ9g6oal1DjjPXJjwpupZ+0xpp7ltOeOuaZRCa/BuXg xsjg== 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=gQq/ChY71TVgF5NA2DHBJ9cqMTnGx9eF+5R9iQkkS5E=; fh=tF7riTkuBa8O3vMSvJNFl/jVCIdpn/sknKnZT2xFBLQ=; b=nVsRuv4X7TqzhVOMpwgTpLuV/Spc9chtT2TzfnIzgLI0FQv/XTIFWhh5dGNoph9gca craz2qKCwD23wjQARZWRZ+xgLlcEG78hqmADz6wM/nN0DBRB3yTPHI7XK97DNQn6vbBv pC4pmME9Exe4NZ7BRE1qgzU953ae6XSnZUI6k47ycmJoer65bPxr7OX2O/KK8Sp7PQK/ UHEJBxv4+qcnb0cP/i3711K/qjlJj06lKUzyV/H6L8GadgQEaCZw1jKaMn8S3mwd/gLK g8A8OGQ08p5QEYbNtmUpRkrzMuULWOolP2oxCxDjwPbPP7CBzVxyxl0imYNGtVh2zqYW 3nqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TOIuiTAA; 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 j193-20020a638bca000000b00584dd505b1dsi13133200pge.427.2023.10.11.00.28.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 00:28:32 -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=TOIuiTAA; 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 2153E8226F5A; Wed, 11 Oct 2023 00:28:25 -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 S1344796AbjJKH1q (ORCPT + 99 others); Wed, 11 Oct 2023 03:27:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344888AbjJKH1o (ORCPT ); Wed, 11 Oct 2023 03:27:44 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69638AC for ; Wed, 11 Oct 2023 00:27:37 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-534659061afso10724744a12.3 for ; Wed, 11 Oct 2023 00:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697009256; x=1697614056; 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=gQq/ChY71TVgF5NA2DHBJ9cqMTnGx9eF+5R9iQkkS5E=; b=TOIuiTAABMMIT1DI1tDpk2PPC3aeESIFE2/zN9OLEEaI4ZXNnEIYOrwhNsHr7YYtrn 1dNJf+UryTOKM1Kyg784yV9fvzEXDiXT6qYYU75UxNr+rPQqeoq85HwUU1QdCNqHoYqm kg4BZX8Ez5BUVEdWSjv1sSibDX5x4QiXukCZ3tMoE6OQPewykalf2JuE6Gpad55c+Yf1 nAbHDSLhC99JvvMUw4BJ0A5+diXC1OOq7v3LLV40/gmi9RwGZ7N639HlfdCraSgCPYnt LRICBH6cJEMVSjr6eVlSWMeaRMATq0jtwiyTKvVuDwVYHZ52TJSOlSZtunlaLv5tuCxk OmRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697009256; x=1697614056; 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=gQq/ChY71TVgF5NA2DHBJ9cqMTnGx9eF+5R9iQkkS5E=; b=P5FAgkFVmDxvARlAPr7RCN6Xh1oC0ZgvMxgvioUEF8tV7PXemf/PaijXXJr2ZGiyOS l4IVJ1jMAaNkqtuppCVpmF3xo/xalkr5vF0aIttHZ5IDfAqpRIBXAzVqqJqq8bDFW6yy liVtmznQB4h6/FhChx9CvHDZXHVy0q5f1sg1+YjeiQxiW6FSUBHkhHU77IMY1ecDqGII lZHJIOEWaQ287brMoh+N6QIJ1nX2hpgQtjv38AmYSmPHj7iiSjeBeLoUMUk3A69G3XQA +EJwnwQGuKOlUIlSkdI7JfOGdzLAnVIx0p/xZQuMKSoUkVD/5HxYSa+eFZ2Fb9BYTtI4 9IOw== X-Gm-Message-State: AOJu0YzfbzxIG8vLl1IxkjUSNZPNWCtP8CKUlEnOG8K5VlQim1YvPQ6i pvPtEmQw2J6k2w3A0wC+2U9mvev14Ef+I5Cdlvs= X-Received: by 2002:a50:ee8b:0:b0:530:e180:ab9a with SMTP id f11-20020a50ee8b000000b00530e180ab9amr18062798edr.3.1697009255571; Wed, 11 Oct 2023 00:27:35 -0700 (PDT) MIME-Version: 1.0 References: <20231010164234.140750-1-ubizjak@gmail.com> In-Reply-To: From: Uros Bizjak Date: Wed, 11 Oct 2023 09:27:24 +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 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, 11 Oct 2023 00:28:25 -0700 (PDT) X-Spam-Level: ** On Tue, Oct 10, 2023 at 8:52=E2=80=AFPM Linus Torvalds wrote: > > On Tue, 10 Oct 2023 at 11:41, Uros Bizjak wrote: > > > > Yes, but does it CSE the load from multiple addresses? > > Yes, it should do that just right, because the *asm* itself is > identical, just the offsets (that gcc then adds separately) would be > different. Indeed. To illustrate the question with an example, foo() and bar() should compile to the same assembly, and there should be only one read form m resp. n: --cut here-- __seg_gs int m; int foo (void) { return m + m; } int n; static inline int get (int *m) { int res; asm ("mov %%gs:%1, %0" : "=3Dr"(res) : "m"(*m)); return res; } int bar (void) { return get (&n) + get (&n); } --cut here-- And they do: 0000000000000000 : 0: 65 8b 05 00 00 00 00 mov %gs:0x0(%rip),%eax # 7 7: 01 c0 add %eax,%eax 9: c3 retq 0000000000000010 : 10: 65 8b 05 00 00 00 00 mov %gs:0x0(%rip),%eax # 17 17: 01 c0 add %eax,%eax 19: c3 retq > > This is not unlike how we depend on gcc CSE'ing the "current" part > when doing multiple accesses of different members off that: > > static __always_inline struct task_struct *get_current(void) > { > return this_cpu_read_stable(pcpu_hot.current_task); > } > > with this_cpu_read_stable() being an inline asm that lacks the memory > component (the same way the fallback hides it by just using > "%%gs:this_cpu_off" directly inside the asm, instead of exposing it as > a memory access to gcc). > > Of course, I think that with the "__seg_gs" patches, we *could* expose > the "%%gs:this_cpu_off" part to gcc, since gcc hopefully then can do > the alias analysis on that side and see that it can CSE the thing > anyway. > > That might be a better choice than __FORCE_ORDER, in fact. > > IOW, something like > > static __always_inline unsigned long new_cpu_offset(void) > { > unsigned long res; > asm(ALTERNATIVE( > "movq " __percpu_arg(1) ",%0", > "rdgsbase %0", > X86_FEATURE_FSGSBASE) > : "=3Dr" (res) > : "m" (this_cpu_off)); > return res; > } > > would presumably work together with your __seg_gs stuff. I have zero experience with rdgsbase insn, but the above is not dependent on __seg_gs, so (the movq part at least) would also work in the current mainline. To work together with __seg_gs stuff, this_cpu_offset should be enclosed in __my_cpu_var. Also, if rdgsbase is substituted with rdfsbase, it will also work for 32-bit targets. Uros. > UNTESTED!! > > Linus