From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (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 BD6CC476CFB for ; Wed, 22 Jul 2026 09:16:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.72 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784711779; cv=none; b=THWkJMBCyVzkXq3EfFAxNwjAX3Nm//KyJnLfiwntu3ESyEu9HeZ4mRTbBvrBaygigfholByKVuMh+PzZt/p8Ckze0RZSglNiLxuiRolEbCCOQY5OsdF3znWLc7hHYyE4nSGrjMdNAUaIkygfyLhSzKnhjoXQDgevigCxg1jyyzE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784711779; c=relaxed/simple; bh=u4jslRub/PBLu8EohlDte+iH8SJg/CCqcBJNUYKCNsc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lFKSYRTdb2uRD/0m51bQk7L8WoEcSGGtOIlgYxTHtcpNQrkR9T7jlRtG/tuhSxLpo/nzcx9EjUrJf2YPCWJJwj2zhWYlAU3paqGuzUknyaFVa9mJzY17xF1fNCxbM7D/qXr0BmjndlZeQ/vDeSLmonQRgru75zWcJBCvoP/W1Qc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sZjO3thq; arc=none smtp.client-ip=209.85.208.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sZjO3thq" Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-6983e9c028dso10674473a12.3 for ; Wed, 22 Jul 2026 02:16:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784711776; x=1785316576; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=V6wf+HdlcLWSsWAu2Uuz3MUCnLI4Q7ndCuVYUgFzi1s=; b=sZjO3thqtdj4nAwsIzEsaG3vNemylX4sNtZYgNTEkFk74LoUJes2z11X1MNN8G9BwT fH7SBXDI/YS4+4gPmWd8eyawRZc+xXiNH2Xx03v9fifh4v/xmAfPMGJi8K6Qu4HR/y/X ItEjQayJ/0alLDlZQutyCi7Lo6gprmx+CQZ6T9ZEYiMOnveTauF7P3dE6rO/Hqv13aB7 yXuycY+iH20Vpe6PPlbPTC6n0gv3t6OMgkF9tdzp4WzI2IVJA14NMV0RlfwxgDEzTd+v 1ASoBBD8X7YHhYrP2j52y4ac8XGeCQrXA91IA4G56nOCVgA04+CtahB5S2EPUNwJGEnr 2HPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784711776; x=1785316576; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=V6wf+HdlcLWSsWAu2Uuz3MUCnLI4Q7ndCuVYUgFzi1s=; b=rJSX+/AAr7Lh+qd7jU/j0RuyZUfKjp65ViCl2EKx6Qscy7bdE8o1HC9oAJzHpejCup V9lTjVt4wS22y5EmjcZKgnijcLaOxZXnaAE3swK5BabcQ/Tqn8/odDgXEbtwKKq3OXpl dMNOpB0nSGTeemhokWwbnrnF27dw3oBn5auXsLDtA71ALiTsetIQRu8PXvudeUwg6SRf J/BeoQWikZhnVuDVmeZEo5icoL9Eme+cXOTZgn6dyX/IPFrejhutzL0WTqA/BFDA9aQf Zd2oZqI7vmtTQZGbUWSDUX63tSiT3Ckiq82IjeRrbjMTSeDORNr631sWx2nE3F9U4Ik6 O4XA== X-Forwarded-Encrypted: i=1; AHgh+Rrcm374ET0z6oK4+ndwEy3Mi2ggtDAzxl2SCPJ15V2Gml+ZeiYv9JEBI2UkSUvkQMI4DIvb0wmJpJUqh9w=@vger.kernel.org X-Gm-Message-State: AOJu0YzaRestY7xGhmQQWoiEVAKrypQ8kLrFBVwtYNDsUPZ+Z7w5i+Bd pHK+jX57ddvcCgNp/2ozgQgGOKyG2DGQRse+fok1UXzTJruOUHTf0wVRc4PJl5FpCzWLjLPa/Po OxpUEy7ie2HopOg9wCg== X-Received: from wmjq24.prod.google.com ([2002:a7b:ce98:0:b0:495:58ee:fab7]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e88:b0:495:636b:e519 with SMTP id 5b1f17b1804b1-495636bea4dmr113274575e9.21.1784711773573; Wed, 22 Jul 2026 02:16:13 -0700 (PDT) Date: Wed, 22 Jul 2026 09:16:02 +0000 In-Reply-To: <20260722-setonce-populate-v1-0-fa7455c26c42@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260722-setonce-populate-v1-0-fa7455c26c42@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4250; i=aliceryhl@google.com; h=from:subject:message-id; bh=u4jslRub/PBLu8EohlDte+iH8SJg/CCqcBJNUYKCNsc=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBqYIpZN1A+6gDJtKxTSNtQ+QUZnSQ+eyH+mifN5 gc2kpqtgZaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCamCKWQAKCRAEWL7uWMY5 RtptD/0am6a0c66NvBFWZW2Nw8qvMbfsQ7z5ejBurSxnIC0W3Fk12HDapsg1fBGeEQnnZq+U7oD mARdTk+ifzUqVJwefmcgX5u6Mzvcm0g6K6sEar9EPQr4aVUtJ2yEAUuvEGeVyx1szusd6eID5Ri WPn6hfzvW9YMeiCNBeYEbJBgp+wvzV/WLwHAlPrLlVqH9/sVE1DF1UEkO0CqE89tWnZRWCsaBiJ sg7PcJlDEtZl1FkfrW3bKw+lwPut7frasv4fmMnDwHdrC7XhqH5MdEwqzSIDgMFv82wD7Uh63zA SeP+oZpv3HyeXDDoZburITwY/sqqsc0dM+yVFjctlPGPFMpkwqw6TT9YFgCv3IU8+C9wVvNixrd ppEbEukJ0c3qjKt1FiqiONqdCpGF5O62Lt1nxPyDiTTCo91h12tkjCZjgNskMRvxRJkwm1Y8lat NZ1tB2XfqgsXshennGUZ1tiZRjPqTCG/bkbsqe2jyJqNOzCF/qZ6QpwHxo/eex6c7kIxdgcfFpq dHVXKBlk4FN4t9WKsFe+UIDJUHQDY/zfpTo4cT4OvDy29HiHv5eNdsFjshzOTCBsHcNmQ6kp0HI uFUaOLIXr9/2wX0Q1ahBHb6/X792S4D25LN7purjrRPSgeuoBVTj7E5fNb5q6u8mDI7ahfLzJqc wRbsdDsVNi1ezwQ== X-Mailer: b4 0.14.3 Message-ID: <20260722-setonce-populate-v1-1-fa7455c26c42@google.com> Subject: [PATCH 1/3] rust: sync: return `Result<&T, T>` from `SetOnce::populate()` From: Alice Ryhl To: Boqun Feng , Gary Guo , Lyude Paul , Daniel Almeida , "=?utf-8?q?Onur_=C3=96zkan?=" , Greg Kroah-Hartman , Carlos Llamas Cc: Luis Chamberlain , Petr Pavlu , Daniel Gomez , Sami Tolvanen , Aaron Tomlin , Miguel Ojeda , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Alexandre Courbot , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" When `populate()` succeeds, there's no way infallible way to get the value that was just inserted. By returning &T in this case, such infallible access methods become possible. Additionally, when `populate()` fails, the provided value is dropped. This has two disadvantages: 1. If the caller holds a lock, the value is dropped under said lock. 2. If the caller wishes to use the same value for something else, they can't, because it's lost. Changing the return value to Result<&T, T> handles all of these cases. Rust Binder is updated to avoid a warning about an unused Result. Additionally, ModuleParam is updated to correctly translate the new return value to the right target values. Signed-off-by: Alice Ryhl --- drivers/android/binder/process.rs | 5 +++-- rust/kernel/module_param.rs | 8 ++++---- rust/kernel/sync/set_once.rs | 15 +++++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs index 1778628d8acd..d486bf7c0b8a 100644 --- a/drivers/android/binder/process.rs +++ b/drivers/android/binder/process.rs @@ -1801,8 +1801,9 @@ pub(crate) fn poll( let poll = PollCondVarBox::new(c"Process::poll", kernel::static_lock_class!())?; // Reuse our existing lock to synchronize callers initializing. - let _guard = this.node_refs.lock(); - this.poll.populate(poll); + let guard = this.node_refs.lock(); + let _ret = this.poll.populate(poll); + drop(guard); }; table.register_wait(file, poll); diff --git a/rust/kernel/module_param.rs b/rust/kernel/module_param.rs index 6541af218390..8f0bd085badf 100644 --- a/rust/kernel/module_param.rs +++ b/rust/kernel/module_param.rs @@ -77,10 +77,10 @@ pub trait ModuleParam: Sized + Copy { // SAFETY: By function safety requirements, this access is safe. let container = unsafe { &*((*param).__bindgen_anon_1.arg.cast::>()) }; - container - .populate(new_value) - .then_some(0) - .ok_or(kernel::error::code::EEXIST) + match container.populate(new_value) { + Ok(_) => Ok(0), + Err(_) => Err(EEXIST), + } }) } diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs index 139cef05e935..a78f8c8e87db 100644 --- a/rust/kernel/sync/set_once.rs +++ b/rust/kernel/sync/set_once.rs @@ -31,12 +31,12 @@ /// assert_eq!(None, value.as_ref()); /// /// let status = value.populate(42u8); -/// assert_eq!(true, status); +/// assert_eq!(Ok(&42u8), status); /// assert_eq!(Some(&42u8), value.as_ref()); /// assert_eq!(Some(42u8), value.copy()); /// /// let status = value.populate(101u8); -/// assert_eq!(false, status); +/// assert_eq!(Err(101u8), status); /// assert_eq!(Some(&42u8), value.as_ref()); /// assert_eq!(Some(42u8), value.copy()); /// ``` @@ -78,8 +78,9 @@ pub fn as_ref(&self) -> Option<&T> { /// Populate the [`SetOnce`]. /// - /// Returns `true` if the [`SetOnce`] was successfully populated. - pub fn populate(&self, value: T) -> bool { + /// Returns `Ok(value)` if the [`SetOnce`] was successfully populated with the provided value. + /// Otherwise returns an error containing the value that this call attempted to insert. + pub fn populate(&self, value: T) -> Result<&T, T> { // INVARIANT: If the swap succeeds: // - We increase `init`. // - We write the valid value `1` to `init`. @@ -95,9 +96,11 @@ pub fn populate(&self, value: T) -> bool { // - We release our exclusive access to `self.value` and it is now valid for shared // access. self.init.store(2, Release); - true + // SAFETY: By the type invariants of `Self`, the value is initialized and will stay + // that way. + Ok(unsafe { &*self.value.get().cast() }) } else { - false + Err(value) } } -- 2.55.0.229.g6434b31f56-goog