From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 749C326B08F; Tue, 8 Jul 2025 13:07:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751980020; cv=none; b=g+DtuUE/zCAi9XnQci7yS4U9irU5T1N0/+TrzE1jYHVTDi4bebHXlqz7qtOfwBfkKUpInaV2JRNceoUn4Qzt/MmIuHL0mlCKB+TRrPLgL8Vo+zdECgCHpKMsH1lDp1os7Fa5sEYiHsNo7yEHdlQ2+jG7n0W1THRmpUUeNUnIECQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751980020; c=relaxed/simple; bh=+kO/dnpSF92MEi9qv/Qf3ThC5gVQWquSZ0ZGR98UIOY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=vDiq5PsKKBRuE3+3KeJstSYXhib1hK+lLT32zlyUzbINrs9mVAzpOtG9dt4Pbdeck6tgZsSjROVJBJqjCy88ylVNmHvkVO0QulmT1lTyux++4tohCdsOC5nPLM3R9XKbxvDYF9Ho3i2kG8PPN0ngf9jT/RnPATeuspj5FIVV1Pc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=g8eV6pOW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="g8eV6pOW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11D34C4CEF1; Tue, 8 Jul 2025 13:06:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751980020; bh=+kO/dnpSF92MEi9qv/Qf3ThC5gVQWquSZ0ZGR98UIOY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=g8eV6pOWw3M3BylCmg+NoMONX3t3s4obYesAL20OIOZtFNiur99heJBHWNFbWNgU9 e2GcNowmUb14iJ7OO/hX4fCKohpDslsDW/zWzgv82kV1QUKaG/kxSHgXPEqwtrlFA8 qquXLKlJFkE+PXvFZthqpHxq+4zGftcDmUJQoJf5bD30tY2kBxntTuH/okSKSiG3SW 1ypVNVatE3Kt9eaKBcZJL8WT8NicWppDk+1L5Mm7v5mauzJYxMahh8h8oAYjPvvGX/ Wxi0fT+3eQzDJcZi+6fXov7wLrsWVGh2hZHTM2fsBdke04AucWt0powD+Kgd0SHOEd 1kaZl5tIimi2A== From: Andreas Hindborg To: "Benno Lossin" Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , "Alice Ryhl" , "Masahiro Yamada" , "Nathan Chancellor" , "Luis Chamberlain" , "Danilo Krummrich" , "Nicolas Schier" , "Trevor Gross" , "Adam Bratschi-Kaye" , , , , "Petr Pavlu" , "Sami Tolvanen" , "Daniel Gomez" , "Simona Vetter" , "Greg KH" , "Fiona Behrens" , "Daniel Almeida" , Subject: Re: [PATCH v15 1/7] rust: sync: add `SetOnce` In-Reply-To: (Benno Lossin's message of "Tue, 08 Jul 2025 11:02:17 +0200") References: <20250707-module-params-v3-v15-0-c1f4269a57b9@kernel.org> <20250707-module-params-v3-v15-1-c1f4269a57b9@kernel.org> User-Agent: mu4e 1.12.9; emacs 30.1 Date: Tue, 08 Jul 2025 15:06:48 +0200 Message-ID: <87v7o2omqf.fsf@kernel.org> 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-Transfer-Encoding: quoted-printable "Benno Lossin" writes: > On Mon Jul 7, 2025 at 3:29 PM CEST, Andreas Hindborg wrote: >> Introduce the `SetOnce` type, a container that can only be written once. >> The container uses an internal atomic to synchronize writes to the inter= nal >> value. >> >> Signed-off-by: Andreas Hindborg > > One nit and a safety comment fix below. (feel free to ignore the nit) > With the safety comment fixed: > > Reviewed-by: Benno Lossin > >> --- >> rust/kernel/sync.rs | 2 + >> rust/kernel/sync/set_once.rs | 125 ++++++++++++++++++++++++++++++++++++= +++++++ >> 2 files changed, 127 insertions(+) >> >> diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs >> index 81e3a806e57e2..13e6bc7fa87ac 100644 >> --- a/rust/kernel/sync.rs >> +++ b/rust/kernel/sync.rs >> @@ -18,6 +18,7 @@ >> mod locked_by; >> pub mod poll; >> pub mod rcu; >> +mod set_once; > > I would have named this `once`. So module `once` and struct `SetOnce`? Struct name `Once` would lead thoughts to `std::sync::Once`, which is a different thing. > >> pub use arc::{Arc, ArcBorrow, UniqueArc}; >> pub use completion::Completion; > >> + /// Get a reference to the contained object. >> + /// >> + /// Returns [`None`] if this [`SetOnce`] is empty. >> + pub fn as_ref(&self) -> Option<&T> { >> + if self.init.load(Acquire) =3D=3D 2 { >> + // SAFETY: By the type invariants of `Self`, `self.init =3D= =3D 2` means that `self.value` >> + // contains a valid value. > > And the type invariants also ensure that the value of `self.init` > doesn't change. > > So probably > > // SAFETY: By the type invariants of `Self`, `self.init =3D=3D 2` mea= ns that `self.value` > // contains a valid value. They also guarantee that `self.init` doesn= 't change. > Sure =F0=9F=91=8D Best regards, Andreas Hindborg