Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3788563rdg; Wed, 18 Oct 2023 06:16:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH828zwOyKWpozQ5JItnkCaeQLa8aFMr5fupLhfZts0VdoerRXHYuSzTPIEuh+nQY1Eib/x X-Received: by 2002:a92:d405:0:b0:351:5acb:281 with SMTP id q5-20020a92d405000000b003515acb0281mr5095112ilm.31.1697634980197; Wed, 18 Oct 2023 06:16:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697634980; cv=none; d=google.com; s=arc-20160816; b=uMLPZknEUc/TnPrc5WAMKIc26/4iV9HeBKAkfxTNm1+3vbXHLFHyKa0rhoJslmp3c6 J5rSN1FZZmXQWvrGMr6kblWrBGBqtGEuNQJNo26POPNfP7q+tofUaIzdYs9QFStA7PoQ a3KbqbcT8i2aQL+E6qRIvuKc/Rd6HfPSJNXbOAqoGAadRA5wh/p5p0g4Mi92ZK0TBMIw ec8wRyfjgyju2EB2e/4ospNP8U61JnmyNRG1qLPQLyXy1HRCafljF3wxHHX62++XXyjI GACvSMeUTc3hbgM5xxnTOKyoTzvHBUZrhOt6PmjajdlT9uQChWA/kaEvHAGJvG4BSEM6 /wUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=i6qXUWupsnmAOKBRrGWDX4ua1G1ZUx1AreCL3neAh1g=; fh=nzKsBgWTayDZeLT+2QeVjDtJqAbCmI+Z4Pl3/plqRts=; b=UOisw2k3EyvN6nnh0Nwdu188WpmQ0eXHF3BNUzn917JRgy+ShYRJxW4tg1fOc6G24/ LXuNXqfhtJxFCU+UHUdsjQwi8Fo8nCbVtuhf9XG2bDzkfLMiu7Ml29XvJTgh0QdtG45N jdvuTYqVaaWJskvf/XfpkMpIKP5ZiIQbY93XNXjuWEQtHL0QVkZwueJMEgWeCzVgYLGE 7NSaD5uSI3xNvb75EsNawtkdrwy63JrDz3CUPTwS08XQLJTaTzj2hlmlooC6UKEWwill HTxMtXbsMJ/oQxHoCmLmWnOyDtQJNSCcjl5oqu3hagMAV6GY2Dx0saj9OMeLHZgKzjh8 RpRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cJB4r0Ts; 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 r11-20020a65498b000000b005ad176a9d2bsi2048025pgs.240.2023.10.18.06.16.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 06:16:20 -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=cJB4r0Ts; 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 962178087DC6; Wed, 18 Oct 2023 06:15:43 -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 S231213AbjJRNP3 (ORCPT + 99 others); Wed, 18 Oct 2023 09:15:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230480AbjJRNP1 (ORCPT ); Wed, 18 Oct 2023 09:15:27 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FC7394 for ; Wed, 18 Oct 2023 06:15:25 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-53ed4688b9fso5322010a12.0 for ; Wed, 18 Oct 2023 06:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697634924; x=1698239724; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=i6qXUWupsnmAOKBRrGWDX4ua1G1ZUx1AreCL3neAh1g=; b=cJB4r0TsVa4RJfbuhDcQnhRBeXjyq54nYTheaqTzm500tiC6EC5TmhGd0mPo/290iL TQpR/KabrBiSGYebD3+o2TQR7fexdRpzSZcnsfgucZztu0KvNkOuMXugLjl79of6/JWh UWZWEp/rMLZDuJ6FackC10K2qH9eEfIN56eipWFmlJfvG23vucC9qb/N+sPmlgxHWq9g +wFo31ujc6hZ+Cku0o9VYENGWgxMDaA+r1yH0RZJCwxyGB3py6nYk37tsKiMIxI2Jctj t4MfENdMNGr5QReJS6Na9cHJSnarBlnvBperbTx7QdiVRI2Xfq/9uHOf1Coe/bA6nGFt 0VZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697634924; x=1698239724; h=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=i6qXUWupsnmAOKBRrGWDX4ua1G1ZUx1AreCL3neAh1g=; b=cVQE5hVNKVeMkXY0lz6Ldo7zL7wFOSzByNYellTGFMYV9I998TQL6Y8rdF3ppH3hxD xmGpWcXrkkPxNKwghhIodyTKiwuYHMBW5DpUevIXACz2ELZ8Jc8t0jE6ZVSa+DKtsEAy Di5zmt1YZMNizrtEPpUTw4xP9DeB4mUapDeNUBEh89aqN8OdDod00rybwHnQqGT5kANd jVkiD+/tlL9+auldvfRfHvrsPZnQYBYn3SbK+xhUxC35kZamv7Xy5duWeyevfhwU4hSY kzrqVj5BxPO3ywDRCS77GiPWOFJumqE2SwobLi01lBnRIxpK7+Bxt2YgSIrsj/TRu3vk oVdQ== X-Gm-Message-State: AOJu0YxxavqGeyO6PHI4KpiOtN2lCegrxnPAnxvf/PtmU41PgxkXxDsw 8uF4n3qbQgRqbwRzPKsgWfo8+Bicr+qlB8xk7gg= X-Received: by 2002:aa7:cd4d:0:b0:53e:1e7f:4835 with SMTP id v13-20020aa7cd4d000000b0053e1e7f4835mr3803494edw.32.1697634923796; Wed, 18 Oct 2023 06:15:23 -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> In-Reply-To: From: Uros Bizjak Date: Wed, 18 Oct 2023 15:15:11 +0200 Message-ID: Subject: Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr() To: Nadav Amit Cc: Linus Torvalds , "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: multipart/mixed; boundary="00000000000097e5270607fd6e2d" 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 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]); Wed, 18 Oct 2023 06:15:44 -0700 (PDT) --00000000000097e5270607fd6e2d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Oct 18, 2023 at 2:14=E2=80=AFPM Uros Bizjak wro= te: > > On Wed, Oct 18, 2023 at 12:54=E2=80=AFPM Nadav Amit wr= ote: > > > > > > > > > On Oct 18, 2023, at 12:04 PM, Uros Bizjak wrote: > > > > > > Solved. > > > > > > All that is needed is to patch cpu_init() from > > > arch/x86/kernel/cpu/common.c with: > > > > > > --cut here-- > > > diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/commo= n.c > > > index b14fc8c1c953..61b6fcdf6937 100644 > > > --- a/arch/x86/kernel/cpu/common.c > > > +++ b/arch/x86/kernel/cpu/common.c > > > @@ -2228,7 +2232,7 @@ void cpu_init_exception_handling(void) > > > */ > > > void cpu_init(void) > > > { > > > - struct task_struct *cur =3D current; > > > + struct task_struct *cur =3D this_cpu_read_stable(pcpu_hot.cur= rent_task); > > > int cpu =3D raw_smp_processor_id(); > > > > Thanks for solving that, and sorry that I missed it. > > > > The reason I didn=E2=80=99t encounter it before is that in my original = patch I created > > a new compilation unit which only defined the alias. > > > > Since there might be additional problems (any =E2=80=9Ccurrent=E2=80=9D= use in common.c is > > dangerous, even in included files), I think that while there may be add= itional > > solutions, defining the alias in a separate compilation unit - as I did= before - > > is the safest. > > What happens here can be illustrated with the following testcase: > > --cut here-- > int init_mm; > > struct task_struct > { > int *active_mm; > }; > > struct task_struct init_task; > > struct pcpu_hot > { > struct task_struct *current_task; > }; > > struct pcpu_hot pcpu_hot =3D { .current_task =3D &init_task }; > > extern const struct pcpu_hot __seg_gs const_pcpu_hot > __attribute__((alias("pcpu_hot"))); > > void foo (void) > { > struct task_struct *cur =3D const_pcpu_hot.current_task; > > cur->active_mm =3D &init_mm; > } > --cut here-- > > gcc -O2 -S: > > foo: > movq $init_mm, init_task(%rip) > ret > > Here, gcc optimizes the access to generic address space, which is > allowed to, since *we set the alias to pcpu_hot*, which is in the > generic address space. The compiler doesn't care that we actually > want: > > foo: > movq %gs:const_pcpu_hot(%rip), %rax > movq $init_mm, (%rax) > > So yes, to prevent the optimization, we have to hide the alias in another= TU. > > BTW: Clang creates: > > foo: > movq %gs:pcpu_hot(%rip), %rax > movq $init_mm, (%rax) > retq > > It is a bit more conservative and retains the address space of the > aliasing symbol. > > Looks like another case of underspecified functionality where both > compilers differ. Luckily, both DTRT when aliases are hidden in > another TU. Attached is the prototype patch that works for me (together with Linus' FPU switching patch). Uros. --00000000000097e5270607fd6e2d Content-Type: text/plain; charset="US-ASCII"; name="current.diff.txt" Content-Disposition: attachment; filename="current.diff.txt" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_lnvrz6c80 ZGlmZiAtLWdpdCBhL2FyY2gveDg2L2luY2x1ZGUvYXNtL2N1cnJlbnQuaCBiL2FyY2gveDg2L2lu Y2x1ZGUvYXNtL2N1cnJlbnQuaAppbmRleCBhMTE2OGU3YjY5ZTUuLjIxZThiZDRlYTQ0ZSAxMDA2 NDQKLS0tIGEvYXJjaC94ODYvaW5jbHVkZS9hc20vY3VycmVudC5oCisrKyBiL2FyY2gveDg2L2lu Y2x1ZGUvYXNtL2N1cnJlbnQuaApAQCAtMzYsMTAgKzM2LDIzIEBAIHN0YXRpY19hc3NlcnQoc2l6 ZW9mKHN0cnVjdCBwY3B1X2hvdCkgPT0gNjQpOwogCiBERUNMQVJFX1BFUl9DUFVfQUxJR05FRChz dHJ1Y3QgcGNwdV9ob3QsIHBjcHVfaG90KTsKIAorLyoKKyAqCisgKi8KK0RFQ0xBUkVfUEVSX0NQ VV9BTElHTkVEKGNvbnN0IHN0cnVjdC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