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 BD57F1D5150; Tue, 8 Jul 2025 09:07:08 +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=1751965628; cv=none; b=q8epYmUO641nprnnvkqX/XxttNF/iraJH0CkF7tYTT1YqGdRnRXHxG9BdHh4FtCxDxYxPmK1ZpQKpKA+0XkZQk9pOg2GW85NRb+EkzyAZunlOqD5jcDA+AuC3nT+V/H0RNQLA1W9zcAu/arIVzff6I0TdxoD0QYcYXUkzf9EyAg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751965628; c=relaxed/simple; bh=al45WhcYx+cfhiLoHHzrSgYvrkI1MfO5KGvm49P1sUE=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=oT9JyYOgxB2h8VKbfbCCGtXU9XJXbdsuAHAQgMI8ZFlAe83zjMzjvH1wfUeRABcok0ccGjQwv1a+6PRc1tRT6XyNFN/QPczI/fQvj4+GsUpzLYOMBVFxVb+VVHhQMzx+2wHZRv1QZ2CXhZijB///TPFMNgtfay6hOmanKbKRKWs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pbwyDWmg; 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="pbwyDWmg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EDD44C4CEED; Tue, 8 Jul 2025 09:07:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751965628; bh=al45WhcYx+cfhiLoHHzrSgYvrkI1MfO5KGvm49P1sUE=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=pbwyDWmgKqTSNdqYbXxUOy9WkKykYt/5jWR1HUlyYlk0oJwyak86Ui/RB6B8WWrzl qDSSy0XSL+t/91MzhAN9K/rfoSFs/urQh3Q5I43r7EwlNaQpvAunkODmnzcDw996I3 CbwCJzPMXEgmQMC0UaKcGZ+HCyjPU7qPLoSujaf9mJutIaLWF5lGNSOp4ubhonhZSY k1frEetBV/HfVmhBtoEo13AENUWORSqUpl4V0PMHnOlvV1nWEMioqZ4aQpAcWgOHDz bcagQ3QFJooQVFkI4ERB/MY91taVQvEzBmypNcGkren7jngOj09nDEAmbBynVerws2 Hxf5wpJ/HpBrQ== Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 08 Jul 2025 11:07:02 +0200 Message-Id: Cc: "Alice Ryhl" , "Miguel Ojeda" , "Alex Gaynor" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "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` From: "Benno Lossin" To: "Andreas Hindborg" , "Boqun Feng" X-Mailer: aerc 0.20.1 References: <20250707-module-params-v3-v15-0-c1f4269a57b9@kernel.org> <20250707-module-params-v3-v15-1-c1f4269a57b9@kernel.org> <877c0joyfo.fsf@kernel.org> In-Reply-To: <877c0joyfo.fsf@kernel.org> On Tue Jul 8, 2025 at 10:54 AM CEST, Andreas Hindborg wrote: > "Boqun Feng" writes: > >> On Mon, Jul 07, 2025 at 03:38:58PM +0200, Alice Ryhl wrote: >>> On Mon, Jul 7, 2025 at 3:32=E2=80=AFPM Andreas Hindborg wrote: >>> > >>> > Introduce the `SetOnce` type, a container that can only be written on= ce. >>> > The container uses an internal atomic to synchronize writes to the in= ternal >>> > value. >>> > >>> > Signed-off-by: Andreas Hindborg >>> >>> LGTM: >>> Reviewed-by: Alice Ryhl >>> >>> > +impl Drop for SetOnce { >>> > + fn drop(&mut self) { >>> > + 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. We have exclusive access, as = we hold a `mut` reference to >>> > + // `self`. >>> > + unsafe { drop_in_place(self.value.get()) }; >>> >>> This load does not need to be Acquire. It can be a Relaxed load or >>> even an unsynchronized one since the access is exclusive. >> >> Right, I think we can do the similar as Revocable here: >> >> if *self.init.get_mut() =3D=3D 2 { } >> >> Further, with my following Benno's suggestion and making `Atomic` an >> `UnsafeCell: >> >> https://lore.kernel.org/rust-for-linux/aGhh-TvNOWhkt0JG@Mac.home/ >> >> compiler can generate a noalias reference here, which allows further >> optimization. >> > > You would like to remove `PhantomPinned` to enable noalias? I guess that > makes sense in this case. I'll fix that for next spin. I think you two are talking about different things. Boqun is saying that the `Atomic` will use `UnsafeCell` rather than `Opaque`, which will potentially allow more optimizations. But you are talking about `SetOnce`, right? I think it makes more sense for `SetOnce` to use `UnsafeCell>` rather than `Opaque` too. So feel free to change it in the next version. --- Cheers, Benno