Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3751387rdg; Wed, 18 Oct 2023 05:15:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrzfZrUgia7UTE/E0vlklRvfpvlVAJKohBlMdoIlHs69qLc+624j7OlJknXqJhJdiPi/cx X-Received: by 2002:a17:90b:3c4e:b0:27d:1862:a494 with SMTP id pm14-20020a17090b3c4e00b0027d1862a494mr4555892pjb.11.1697631348043; Wed, 18 Oct 2023 05:15:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697631348; cv=none; d=google.com; s=arc-20160816; b=OsyEuMmZKAB3hM22i8Xf3EnywbDQULz36xGVe+kepybOO4e/8B5ddMRnd4UiN6Ajpn ykqT9OE2hSOlETxl9PpyQXzWR8j4K/83Qp3VZzLLFbwaoeOWWvK70PiQmuOyv3Ch8iOH loQhupxuehbhn5VtqgBbg5uQuK0r4Po2EzLNmtNevZFJxxWCCHlUaKw8mMiaxqI2PvlT dg4T0jxcB0Zdq28CTk+2/OFT/ICeNtwRt4Tacdg/lb5XnK+pCnPXcVPs+LcWdo++wMp3 5KaVj/wX2b5qC3TpjfXQvqnwuMlKR7mBq+h5+MV3DC9OqWxq8reTmovz4QZlreWQk7FD gUVg== 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=wlmUOMPIHXCoA1mbbwcwugrmWOtlaocEiaphBwyu/sU=; fh=nzKsBgWTayDZeLT+2QeVjDtJqAbCmI+Z4Pl3/plqRts=; b=jyP32gsM323UwWd2trvqq11YdpUi1T55mVCHjzHdHKzEc66VeCM1oVgYLxqgCdYLC2 Ly95qyFyOdGaaOcHCgUOgVxVBi8xN9hID4dfAF4LAOUSgQ3ezB+xrGHMfrkqTs9Fi/yY tOSMiW0Q8LPPxMyVrD/vRZECz3eM60QJR90dEhc9z0WrjnzOzq9ItekyelCHZ4oLidQd bpj81KoG5hUjgq8WTpnnwFS75gDNy2yhMf8OgrL0OxBN4AAVsyFqVF52TcdwBsVLHGhw dXPZxVgMKSRO2BPsmWvAGPVc/a2vfWotm4YDbmL47UBid/CvaLEcKLkm0CO4Q4QJKQAZ vF5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Jb1bmiTy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id bs2-20020a632802000000b00566016fc08csi1847828pgb.83.2023.10.18.05.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 05:15:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Jb1bmiTy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 0C936807600C; Wed, 18 Oct 2023 05:15:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231687AbjJRMPF (ORCPT + 99 others); Wed, 18 Oct 2023 08:15:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjJRMPD (ORCPT ); Wed, 18 Oct 2023 08:15:03 -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 56DAF98 for ; Wed, 18 Oct 2023 05:15:01 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-53d9b94731aso11882478a12.1 for ; Wed, 18 Oct 2023 05:15:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697631300; x=1698236100; 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=wlmUOMPIHXCoA1mbbwcwugrmWOtlaocEiaphBwyu/sU=; b=Jb1bmiTySefYgjyJybzoZPzTFMI+nOgWLruOyuOQKcilMaJpbB6Vwa6tTNyiN8PT13 huhFy9JMkJNzgRB+uN9aAk6c+GabE2aKRn55VDPIzXmGxANr8nPLwaM9yYnX+RgiWvXT OYgpsYRnERtagi4FriIIkaLvd8B8vSkczYjtuQqmcmX3KYyyxxgbSrGwAW7MwR3aVOrc AsP2d1XDAuC1tDKVLl33XZyy8QJwAxUwV5tWER6jvyBhLgSJrd+tUidBo1GigagIrhIC XpKiKei0RLEr5zM+3Zc+ddXhgwVm5QSKOq4WyOeUjkNa+MWytvzNSAHNHOsCsF162OfY jy5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697631300; x=1698236100; 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=wlmUOMPIHXCoA1mbbwcwugrmWOtlaocEiaphBwyu/sU=; b=tLEZKVMwZIYhqHtTsKfRQ8FwMdYj0G4wRfBHQhEZl2aKRntg6jTQKZtrTAveScJmgl Pi9GEMzTRmXlm8FFrOg13rP/KLnxOC3CIwU0d9IVoqaEVASw4HvZgflXMoPjPd/5YiNl wzFYdSJrK09aShIafyVaQOIF8BUqxJIF4vuaQgNTOGEFNQZWpWIvF8WffryMeBcW117s e4RCxA93iQmPf3AP68OFZkKvyx8kLVUW1cxBM2g+PQUnDkGvOANgm0E+QY67S8VBuG6A CEUVoqhcc/vTYw3vTs91bK6j8opDICWw7b/5aVolxedLjV+uRSIa5+MQmmTBYtJCIRTj EtwQ== X-Gm-Message-State: AOJu0YypRwvSv1Rr3+XxkIbWjL73VT2scPSiQsxSAFwg8ReQV8XHwRIq j8n5Z8vJOrQ6/U7rZKhg/e77Bn/mUrHr9y06EUg= X-Received: by 2002:a50:d709:0:b0:53e:1825:be92 with SMTP id t9-20020a50d709000000b0053e1825be92mr3567800edi.31.1697631299479; Wed, 18 Oct 2023 05:14:59 -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 14:14:47 +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: 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 agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 18 Oct 2023 05:15:12 -0700 (PDT) On Wed, Oct 18, 2023 at 12:54=E2=80=AFPM Nadav Amit wrot= e: > > > > > 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/common.= 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.curre= nt_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 pa= tch I created > a new compilation unit which only defined the alias. > > Since there might be additional problems (any =E2=80=9Ccurrent=E2=80=9D u= se in common.c is > dangerous, even in included files), I think that while there may be addit= ional > solutions, defining the alias in a separate compilation unit - as I did b= efore - > 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 T= U. 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. Uros.