From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D38C11FECB4; Fri, 11 Jul 2025 21:34:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752269701; cv=none; b=OcdIq67vFRWWJWdyQnqScHVZRLnWrMuWxOgH47UIIne+vOMJN7cWDLrYpL5uIABmXSR8ElSMNuOeuM3iM0yCJjXPV7a3hwMPpQ+sc5+UBb7zDrC3AB2Z+jNXy1RxlU926MuKzofsnZn6RqCMzhymaBBPuc1DtdMc4fNRW8JR1ik= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752269701; c=relaxed/simple; bh=8V8ejNYB3QXOIEBL61iMBdPzyE+6vjhOjCy9tOR4HWg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OorZHFjNaDhsZ89Yl0YvrSKSm5twJWAr06DTlCJHiOhHmFxXITezmEPtI0Vfln87+OL5bFV44C19Bjkh9w/u3RAStzrafmu1YnQAfE+oiid8iRLa6GQuaJRCoR6m4ksivnG3rhtxsih0CZLngQN3VKA6AfLwV0/4SluaTNbziz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jb9k42uB; arc=none smtp.client-ip=209.85.219.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jb9k42uB" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-6fb0eb0f0fbso30311386d6.1; Fri, 11 Jul 2025 14:34:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752269699; x=1752874499; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=Lxb4wH8x/KAvU0TG5QbcaePRN/pYWDrgWrEuOMXDQ90=; b=jb9k42uBzi97howxu1JCRMcCw1eXQaOONQtNIrjwgZRjhdvMPPFrTxITvVNY9b4Ym/ M1c6LiZGsQHmDt1JvVFRdiRd/4Z5tc4b/jVq9p9u6nnre6znGxCf8DqmiywhI830VbmL j7YCdHhdoFQXjvyRy7hyawzGalI7CKVmCcJkH6h6amrQ/MqLxAcjgYOCNBRFzCA5nS4x x3bAtyY5j7v8HaHj+vryV8YbQP2wjkXVWB24gt+J7yPFqDza4s4czsmtq7QPYN9YaKU+ 6FrGeRD47kAwkJfa73Tzb6lRgk66u+ZsjPakQdI13PFORhqcRCz8CDutiOKauziMFcfU jEMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752269699; x=1752874499; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Lxb4wH8x/KAvU0TG5QbcaePRN/pYWDrgWrEuOMXDQ90=; b=nQxFIq8FO+KFj20YUjtDrVlYbV4eramvadHd02HnMpue+TbLvr6mM/CaOicGXfbbvk j9iRTqnHI3Fdb3FoHVNLmq0Z82/+qvDfWhodZc5FjY5DZKflCxCf4Y9SD15kduQ0HiOy V3AywF6hVZ3mAFDMorYLx4A9U3qV1SEFaljyHNpo/aNk7bg7e60GbrfydrZT/ir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 2002:a05:6214:2f85:b0:6fa:b468:caf4 with SMTP id 6a1803df08f44-704a37c07eemr78816456d6.0.1752269698538; Fri, 11 Jul 2025 14:34:58 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-70497d3d3b0sm23705646d6.80.2025.07.11.14.34.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Jul 2025 14:34:58 -0700 (PDT) Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfauth.phl.internal (Postfix) with ESMTP id 77DC2F40066; Fri, 11 Jul 2025 17:34:57 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-06.internal (MEProxy); Fri, 11 Jul 2025 17:34:57 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdeggeegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhnucfh vghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrthhtvg hrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveeiudffiedv necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqh hunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddu jeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvg drnhgrmhgvpdhnsggprhgtphhtthhopedvjedpmhhouggvpehsmhhtphhouhhtpdhrtghp thhtoheplhhoshhsihhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqd hkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehruhhsthdq fhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlkh hmmheslhhishhtshdrlhhinhhugidruggvvhdprhgtphhtthhopehlihhnuhigqdgrrhgt hhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehojhgvuggrsehkvghrnh gvlhdrohhrghdprhgtphhtthhopegrlhgvgidrghgrhihnohhrsehgmhgrihhlrdgtohhm pdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpdhrtghpthhtohepsghjoh hrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 11 Jul 2025 17:34:56 -0400 (EDT) Date: Fri, 11 Jul 2025 14:34:56 -0700 From: Boqun Feng To: Benno Lossin Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, lkmm@lists.linux.dev, linux-arch@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Will Deacon , Peter Zijlstra , Mark Rutland , Wedson Almeida Filho , Viresh Kumar , Lyude Paul , Ingo Molnar , Mitchell Levy , "Paul E. McKenney" , Greg Kroah-Hartman , Linus Torvalds , Thomas Gleixner , Alan Stern Subject: Re: [PATCH v6 8/9] rust: sync: Add memory barriers Message-ID: References: <20250710060052.11955-1-boqun.feng@gmail.com> <20250710060052.11955-9-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-arch@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Fri, Jul 11, 2025 at 11:04:09PM +0200, Benno Lossin wrote: > On Fri Jul 11, 2025 at 9:26 PM CEST, Boqun Feng wrote: > > On Fri, Jul 11, 2025 at 08:57:27PM +0200, Benno Lossin wrote: > >> On Fri Jul 11, 2025 at 3:32 PM CEST, Boqun Feng wrote: > >> > On Fri, Jul 11, 2025 at 10:57:48AM +0200, Benno Lossin wrote: > >> > [...] > >> >> > +} > >> >> > + > >> >> > +/// A full memory barrier. > >> >> > +/// > >> >> > +/// A barrier that prevents compiler and CPU from reordering memory accesses across the barrier. > >> >> > +pub fn smp_mb() { > >> >> > + if cfg!(CONFIG_SMP) { > >> >> > + // SAFETY: `smp_mb()` is safe to call. > >> >> > + unsafe { > >> >> > + bindings::smp_mb(); > >> >> > >> >> Does this really work? How does the Rust compiler know this is a memory > >> >> barrier? > >> >> > >> > > >> > - Without INLINE_HELPER, this is an FFI call, it's safe to assume that > >> > Rust compiler would treat it as a compiler barrier and in smp_mb() a > >> > real memory barrier instruction will be executed. > >> > > >> > - With INLINE_HELPER, this will be inlined as an asm block with "memory" > >> > as clobber, and LLVM will know it's a compiler memory barrier, and the > >> > real memory barrier instruction guarantees it's a memory barrier at > >> > CPU reordering level as well. > >> > > >> > Think about this, SpinLock and Mutex need memory barriers for critical > >> > section, if this doesn't work, then SpinLock and Mutex don't work > >> > either, then we have a bigger problem ;-) > >> > >> By "this not working" I meant that he barrier would be too strong :) > >> > >> So essentially without INLINE_HELPER, all barriers in this file are the > >> same, but with it, we get less strict ones? > > > > Not the same, each barrier function may generate a different hardware > > instruction ;-) > > > > I would say for a Rust function (e.g. smp_mb()), the difference between > > with and without INLINE_HELPER is: > > > > - with INLINE_HELPER enabled, they behave exactly like a C function > > calling a C smp_mb(). > > > > - without INLINE_HELPER enabled, they behave like a C function calling > > a function that never inlined: > > > > void outofline_smp_mb(void) > > { > > smp_mb(); > > } > > > > It might be stronger than the "with INLINE_HELPER" case but both are > > correct regarding memory ordering. > > Yes, this is exactly what I meant with "not working" :) > But be stronger is still "working" ;-) BTW, replying you made me realize I should make these function #[inline(always)] so thank you ;-) Regards, Boqun > --- > Cheers, > Benno >