Received: by 2002:a05:7208:70d5:b0:7f:5597:fa5c with SMTP id q21csp2252611rba; Sun, 24 Mar 2024 08:23:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVc7N+qdn0BHwaUyn42gUqthqVtoaOpW11KIJ5X+L50lM25GKPB5YfCNY0/1IQVsKHb+ilHs3Ac0vQE/BH9xnlcTFaJCPO4Y5SFud5JXQ== X-Google-Smtp-Source: AGHT+IHBWwwgZSi924gWvTZw0tTaOzp+CZABO3g20wcRh1sfT82SfYnaqErq9SEgmcCSsXOjEUnt X-Received: by 2002:a17:90b:4017:b0:2a0:3fe1:2e1 with SMTP id ie23-20020a17090b401700b002a03fe102e1mr3245432pjb.6.1711293779854; Sun, 24 Mar 2024 08:22:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711293779; cv=pass; d=google.com; s=arc-20160816; b=h5OXTFNOAEH2ecmtTs0tABXLeiB2SSViy/gqH4L8TMzyUqxJS9nkwuTo59J1Dx1NLT oMO7GIKEtqRIWmgp4bllbV5bSDAxrE7vZirS/QvZr1BRkFqA5iCtALCf3+gCXRmcW2kU EiiDMYw4/CRwFYsgDuYGdxAGWIBnsXjAXcnRMnXmt/ax+1Ag4t5SLqFJ6AVK159bgZMl woKxaWW89dNiMhamlaRMjMiWa9QaeLIk81Eipi78EJd1vcyOSadRY08aZYRyrGplZb39 0Rk3J6kNXgVHF/iq5oAAtQRZIiIquNL/cXJnjrZaU2FUmi2tghIXXx5jQyX7UDGaWX1N udrQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date; bh=k+lqy8rJf/0ht065TcV9Q4f9hNTSVybS0XFFiL9nKz4=; fh=jdJaKvenPblFb+ssCrHVTP/eEv64u/rWN/heIfIpC6w=; b=JGVqHOicg0i4XYQuWf8VSDx1x9qo+vC90brWqSjjo0gev/VV1q850/un5rNrQepMel /5sP74cBLfoAk+rubOhbmvueKFKWztGfuS4Dnd46WSpIhuRQz7njE4PUkud0Uv4LJ++S x5Yo6rwS9pbMflV4jTYd7RQC14IzSM4jFa1rUX406whpWLvb4rds5qBBelIZXWcJjENf 4iZXm1vksH8Pr7ky6GcREIiDuPiTqpVKSGmxQarf2bBv9UFnuUW5oRV61DMbBlYTxAaa fbiwS0QOUsw8quLsnHYhkUePG927VklxXyhvyv/ftOK0JjjauiJGGSW9fSAN0RHaKaO5 D2NA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=netrider.rowland.org); spf=pass (google.com: domain of linux-kernel+bounces-112725-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id nl10-20020a17090b384a00b0029b85203a6dsi6309511pjb.136.2024.03.24.08.22.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 08:22:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112725-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=netrider.rowland.org); spf=pass (google.com: domain of linux-kernel+bounces-112725-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112725-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 88C7BB20E47 for ; Sun, 24 Mar 2024 15:22:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7AC5718EB1; Sun, 24 Mar 2024 15:22:45 +0000 (UTC) Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by smtp.subspace.kernel.org (Postfix) with SMTP id BF9D71862F for ; Sun, 24 Mar 2024 15:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.131.102.5 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711293764; cv=none; b=EhmoeCxNugq/GAGzzZw/4fwkGTrdjNonELM+hv36/zBkpvbNjuIeKR0ui36eNXSARGUSqxiDf6xmoKvp3wGEygMi2EaZvKLSsmqR31RWk09yeXN9LfmEH3q01Nf7sgInMN8YFZbKH+xbKM6TmH7VoBit05woKySQwfkiW5EKBmc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711293764; c=relaxed/simple; bh=SuH1hMVA2o74FCs6AvGI7dXDMoRLsBXOu5zkwoZfD8Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=paZMTEiA6+s/fwFzF4qP2nOmUsZTs7NyAreV2doy+2D64sZRYgSZhoZWJRkrp1D3lUgSUkJ5rrRL7vxwSqk2mDCZGptJsJkZgsOXfi1C0nBlMJww8vioO2prYY9kkgTEV7eFR3pl8vOckKWAi17a1gs+T7G44mJDYirci6o2Jm4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu; spf=pass smtp.mailfrom=netrider.rowland.org; arc=none smtp.client-ip=192.131.102.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netrider.rowland.org Received: (qmail 820911 invoked by uid 1000); 24 Mar 2024 11:22:41 -0400 Date: Sun, 24 Mar 2024 11:22:41 -0400 From: Alan Stern To: comex Cc: Linus Torvalds , Kent Overstreet , Boqun Feng , rust-for-linux , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Andrea Parri , Will Deacon , Peter Zijlstra , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Mark Rutland , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Catalin Marinas , linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org Subject: Re: [WIP 0/3] Memory model and atomic API in Rust Message-ID: <174272a1-e21f-4d85-94ab-f0457bd1c93b@rowland.harvard.edu> References: <20240322233838.868874-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Sat, Mar 23, 2024 at 05:40:23PM -0400, comex wrote: > That may be true, but the LLVM issue you cited isn’t a good example. > In that issue, the function being miscompiled doesn’t actually use any > barriers or atomics itself; only the scaffolding around it does. The > same issue would happen even if the scaffolding used LKMM atomics. > > For anyone curious: The problematic optimization involves an > allocation (‘p’) that is initially private to the function, but is > returned at the end of the function. LLVM moves a non-atomic store to > that allocation across an external function call (to ‘foo’). This > reordering would be blatantly invalid if any other code could observe > the contents of the allocation, but is valid if the allocation is > private to the function. LLVM assumes the latter: after all, the > pointer to it hasn’t escaped. Yet. Except that in a weak memory > model, the escape can ‘time travel’... It's hard to understand exactly what you mean, but consider the following example: int *globalptr; int x; int *f() { int *p = kzalloc(sizeof(int)); L1: *p = 1; L2: foo(); return p; } void foo() { smp_store_release(&x, 2); } void thread0() { WRITE_ONCE(globalptr, f()); } void thread1() { int m, n; int *q; m = smp_load_acquire(&x); q = READ_ONCE(globalptr); if (m && q) n = *q; } (If you like, pretend each of these function definitions lives in a different source file -- it doesn't matter.) With no optimization, whenever thread1() reads *q it will always obtain 1, thanks to the store-release in foo() and the load-acquire() in thread1(). But if the compiler swaps L1 and L2 in f() then this is not guaranteed. On a weakly ordered architecture, thread1() could then get 0 from *q. I don't know if this is what you meant by "in a weak memory model, the escape can ‘time travel'". Regardless, it seems very clear that any compiler which swaps L1 and L2 in f() has a genuine bug. Alan Stern