From nobody Fri Jul 24 23:30:19 2026 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" Content-Transfer-Encoding: quoted-printable 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/pro= cess.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( =20 let poll =3D PollCondVarBox::new(c"Process::poll", kernel:= :static_lock_class!())?; // Reuse our existing lock to synchronize callers initiali= zing. - let _guard =3D this.node_refs.lock(); - this.poll.populate(poll); + let guard =3D this.node_refs.lock(); + let _ret =3D this.poll.populate(poll); + drop(guard); }; =20 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 =3D unsafe { &*((*param).__bindgen_anon_1.arg.cast::= >()) }; =20 - container - .populate(new_value) - .then_some(0) - .ok_or(kernel::error::code::EEXIST) + match container.populate(new_value) { + Ok(_) =3D> Ok(0), + Err(_) =3D> Err(EEXIST), + } }) } =20 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 =3D 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 =3D 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> { =20 /// 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 att= empted 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 i= s now valid for shared // access. self.init.store(2, Release); - true + // SAFETY: By the type invariants of `Self`, the value is init= ialized and will stay + // that way. + Ok(unsafe { &*self.value.get().cast() }) } else { - false + Err(value) } } =20 --=20 2.55.0.229.g6434b31f56-goog From nobody Fri Jul 24 23:30:19 2026 Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (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 9FAB143D4EA for ; Wed, 22 Jul 2026 09:16:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784711779; cv=none; b=MyhMB0T/yupNVE5R46Yc/go7RlM6ZIOsilipUv65Q/xx9vXh2leR3iFYzcuktJqOLtxJzmuBbPT8xs7RnLirr6i7jEZQHXuFKci+5sZoq3ERLhyCNn85LgFRKtS1Q0NelIkBd7P6m5DrNrurbaahdBwphEjfdLqKMs+7pkuZudA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784711779; c=relaxed/simple; bh=YS3Fmynk4Vu065pQWcNaIi3zvRzKJ7fDLNwk2VrvdWo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qxwIujuIx0yV1QABFWsz4rBfswzLTkyVNuNxTJnVGs7Y7556ktqXbbrZuFRLd+wd0mxohCamStFAZBxcfXwlJHWnSUlLalnHq5BEtTg26iVBNkhNzgHmd8mI6Oy+N+4cUWkBX2+Q5GeV9IcyBQm4kJ8KgAm1PraV/mPimnHyPUQ= 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=SSP5xUb8; arc=none smtp.client-ip=209.85.208.70 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="SSP5xUb8" Received: by mail-ed1-f70.google.com with SMTP id 4fb4d7f45d1cf-6983e384c66so8057998a12.1 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=fy37Ri9YIz6ZPyFsdkNre42EGivj6QW6c9T4HBOMel8=; b=SSP5xUb8jBbiBRcl2OSRVX98UaqLtcRAehMeYD/uFSIovNEuZ5wMdUY5HDWzQuYME8 L3Q6ZtGmOCA21ZF2YBnRfyNmrbULNOFf+eXcr0rTf0a7KZ/JJRsJ3LFCm7044BVXNBgD UHlEVxqG88YIpwkvOLm38WCj0IZhnmnqo1yHlf/FiyK3OocMO6Dh2Z8UqgaKWQ2F0Do8 ptpjrOAUjPSeE92yQhGFNSCtFur172iUa4yVrjI7yU1LU11thaIC9l1U1KywEbhw7cOL uESVLkRwsnOHfaudGvQhVnOB/r8LEsjZxs6LP2x5xqOu0gHRnkaxW6Nfb7BcdQOIFtqQ AdGw== 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=fy37Ri9YIz6ZPyFsdkNre42EGivj6QW6c9T4HBOMel8=; b=XQe/DOECCeMQZmLXwpSdVeNDFSS8RbjPPK/cyHWo+GhfxpbaXgEJpdOZIMJeJOV3BP BgZ4mTGiU7HxjSzHg+smsaT6FDB6N6FUJ3m0UzuhTqPXTIlDe8eS9NKCW9qWXf4c7djN OblXWfZtVb4Qep2ifsMn62CBv1UNHgwJhLmrht7tbOYNbuXUo8RDyVNbeN8irXvLjm3G q1eXoXbCOdFDsA00EmDK95/E4h6Terzz8oMtlbkZUid3gnHYpz4aBbdIB0WBiSgKZZXV xHzqTd4CLgPmlfghWLpoo2PyqXUde+5N99By6ARx3lMNgFaCZmfXX3w2eD+r+TG5VcBR efFQ== X-Forwarded-Encrypted: i=1; AHgh+RoTa0EXElXu79mpcVTTXjNLWqF/PLnsm/i/NVjyp4plZo2F+nrTwPMmNhD7EgnQuMHFvsPXv8pB9ELFSFU=@vger.kernel.org X-Gm-Message-State: AOJu0Yzl7d1LjFJa7Z634jxZB2t992pxpe5H2eGuPFCXZQYjwuNfGZyO UWWE7QRBGqm49ulXrjV30YFjXzma4ZZfxjliMWWRo7O86U/eHhkoiBXeKPjwjSWf5VN9d0sQakB 5sbh2Zl/lNMNUlRh8Qw== X-Received: from ejfx13.prod.google.com ([2002:a17:906:a4d:b0:c12:697d:b9f0]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:60cc:b0:c1c:3705:dde9 with SMTP id a640c23a62f3a-c1c3705e054mr91273766b.64.1784711775265; Wed, 22 Jul 2026 02:16:15 -0700 (PDT) Date: Wed, 22 Jul 2026 09:16:03 +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=3202; i=aliceryhl@google.com; h=from:subject:message-id; bh=YS3Fmynk4Vu065pQWcNaIi3zvRzKJ7fDLNwk2VrvdWo=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBqYIpZlnR5r3GHmC1/FYMSNnDKWRI2sTd2Ccy2c Wmxh6BdjE6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCamCKWQAKCRAEWL7uWMY5 Rg9OD/wIyDyEpfmqc72tRFWwWjifv2M5J+dXWO5bI1owZFLjoN+qloMJOeq+T9l3ELsHO2qW/2c NywU9nNrj1DVaZ2mgsdfH+P8TKEKOYvZjfKqCSfPfwmZcrM5j+M9FC6QNA8Wqlg+qg1Jn+3iPgH znS4RrASUWE+ns3BSx79z6svYO5KUCJefKa9LHO6eab9N1+hc5r48t70jQJvbdsJG6dZHgVWaqv HdCwAScNPhtl6r3kikGp3XIBOOEwAOs6joXmPXKvuJxpncn65IO5ZU/w3zmJtPZh4jOOK4AXvHs lqb3Vxv0vidryGAqKq3365w7pXbGmJ4pjjocjC4/DyFzvfqYFeeu1rTQ5gbS33R2F+z3a29L4vr dBG2FTgKtmV2LkrHgIn4F986mDrop4xGWfqK4PCfEeKV08DZ52cSFL1fukMR6epA3f1xPcv49PX BJjG87jJWm/pz+6dSoQ4+G+8JhNWl/737CBWcjqRYQO5YJUi0+/RcOc5EHR4ijw3ZiUjOinYm4+ qUEH+q+cdq9JgGN9mlxFAfGKdrHrS6XF2ohMRDO/HPU4EljxWalnBrveDrKb+c6tFMxBIpp3QXs 52JFVAi9DR1R8qZ9nwGAr8dkGZYqwsPqTuvNWxoWLPDfBzPeS1xiAqdAQih9qhEuWQTRH9Ahk0z 2a1Z7WAPSU5/nwQ== X-Mailer: b4 0.14.3 Message-ID: <20260722-setonce-populate-v1-2-fa7455c26c42@google.com> Subject: [PATCH 2/3] rust: sync: add SetOnce::try_get_or_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" Content-Transfer-Encoding: quoted-printable The SetOnce::populate() method does not internally synchronize callers that fail to populate the value with the successful call. This means that naive loops using as_ref() and populate() can lead to spinning on the initialization, which is best avoided. Thus, provide a helper that avoids this issue using a user-provided lock. One potential alternative is to change populate() so that the failing caller actually does synchronize with the successful call to populate(). However, this is somewhat tricky: * There are users of SetOnce that construct it in const context, and we currently don't have the ability to do that for most locks, so we cannot easily add a lock to SetOnce. * Just spinning on the atomic is undesirable unless we disable preemption in the success path. If we do disable preemption, then that raises complications for handling the PREEMPT_RT case. * It also raises questions about deadlocks if populate() is called from irqs. By using a user-provided lock, we do not have to worry about these issues inside SetOnce. Signed-off-by: Alice Ryhl --- rust/kernel/sync/set_once.rs | 43 +++++++++++++++++++++++++++++++++++++++-= --- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs index a78f8c8e87db..d9cc598a8d78 100644 --- a/rust/kernel/sync/set_once.rs +++ b/rust/kernel/sync/set_once.rs @@ -2,11 +2,18 @@ =20 //! A container that can be initialized at most once. =20 -use super::atomic::{ - ordering::{Acquire, Relaxed, Release}, - Atomic, -}; use core::{cell::UnsafeCell, mem::MaybeUninit}; +use kernel::sync::{ + atomic::{ + ordering::{ + Acquire, + Relaxed, + Release, // + }, + Atomic, // + }, + lock, // +}; =20 /// A container that can be populated at most once. Thread safe. /// @@ -104,6 +111,34 @@ pub fn populate(&self, value: T) -> Result<&T, T> { } } =20 + /// Get the value, or populate it if it's missing. + /// + /// This method is useful to avoid spinning on the internal atomic sta= te. If all writers call + /// this method with the same lock, then they are synchronized with ea= ch other and it's + /// guaranteed that no caller will attempt to invoke [`SetOnce::popula= te`] more than once. + pub fn try_get_or_populate(&self, lock: &lock::Lock,= f: F) -> Result<&T, E> + where + B: lock::Backend, + F: FnOnce() -> Result, + { + if let Some(value) =3D self.as_ref() { + return Ok(value); + } + + let mut to_insert =3D f()?; + loop { + if let Some(value) =3D self.as_ref() { + return Ok(value); + } + + let _guard =3D lock.lock(); + match self.populate(to_insert) { + Ok(value) =3D> return Ok(value), + Err(ret) =3D> to_insert =3D ret, + } + } + } + /// Get a copy of the contained object. /// /// Returns [`None`] if the [`SetOnce`] is empty. --=20 2.55.0.229.g6434b31f56-goog From nobody Fri Jul 24 23:30:19 2026 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.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 64FD13BE659 for ; Wed, 22 Jul 2026 09:16:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784711782; cv=none; b=V+EkG6MSPSfEXCuTZdU1v5E+Ub1EKdwQje8o/+6J5ve4Qr+20pDMbMSBFq2qV6OVxUgYZoNs3Gh9hNgfjWozG8FlcCkDEQc04tINYtZ6llnIiEqV7Cmg9GlsmIXi2BTM9PIb5LoO0SVvcGa9QZiWC5QsP2r1t83EwMF4dWgIzi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784711782; c=relaxed/simple; bh=2b6L2eXi0rZnUWrbz0nUqiyHf39Qr9F7UnkV1u9przE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g9LbfaY7dzCfGAb4DKEn0DHQ+cLUrqudEOL+4UDK194Z9/4VGJb5FbVKZRzMIpvD4tOUxXrXzOk2eV0S6RSp/oqBaoK218IrWHPiLDNxGZafx1G1YBLGVl144g4Ecs1Lx7NMYniy/d4u+mIGnS+dSlZL1MzbR2DgyQhZ7tCJnzg= 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=RkGtCW52; arc=none smtp.client-ip=209.85.128.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="RkGtCW52" Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-495474a5fbcso63076665e9.1 for ; Wed, 22 Jul 2026 02:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784711777; x=1785316577; 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=HHBSei75Jv7bmuOghRwfWU/0mZN/rXuNg2tLxsZDtvs=; b=RkGtCW52Lh97+/lqJjt2q8htzOHbLIaMTcE77s/okbcJo2zxSS4lpjwcuMhNz6pqJd nUm4zVLIwOewVmROGJSiMbFgDfiA2QQ25fwv2ycWx/D0bowgFjsPsAvdhQm+YVM+iP5z PtDD2SrtQrjlJiBHFTovLJFyOra+c3sRT0153dzBKgQ6ZcJ5D1ahimEyeHc0Fx/D7uAV VHttnOZCYegD9ZjXMiVSg0EoDBxosnvsOvMebKOp1RJkx9jdu1UqJ1X/26f6N5KESr+a TiD3pVZA0IRF2GIIBAiVrKtEevzLJAtG7r4pQdbRlWNfszVuZAproZEFupss7zBp7pXm UHEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784711777; x=1785316577; 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=HHBSei75Jv7bmuOghRwfWU/0mZN/rXuNg2tLxsZDtvs=; b=SZNnr517Br+RuOWeDX3eu+p1rqLqueujml+HM0ZiR4XEyWwpDsVq16FOBQmWhbaeDO YJP3DVuLB8J3/YfTgqvwvMidwObRICESTjG408AvA6Nc2LmRsUSCdFzZuB6eIrCsTlqO MWYK6e8l/3iz5vEsyKcFAJnuEAZQpj1e8jjQFsOTMiADCoJXtoDXC5k2C9gPtogkYmWY Mc0SdDO+yzCcyFwj9q7a3K4GnhC+4VvyfRcrwdJYg9GxL4guImzX5u9dzY1vPwgExBkb aGfCUUtQkfGtJkVwkdcmHyioVtSwEUC//rhnJnMKr3KDJ+0+LDxsF3EVa7FQemzzf1ql 8/Uw== X-Forwarded-Encrypted: i=1; AHgh+RqwrpFtVb/7AEWLTvb1B4Z8O6VCUFMoz20iOA1db/Uiq8C/Nznq7RwspY5o7PYLbMV7aXg1S1dQ0z+VGck=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6slTIGv4lgvuzML33ekv44xB8i6c/VqG2dOaZTqGtw+PyU+lS veBmJzh9LnvcMQ2ifiTX6c3RWgQuxI/fhxyTlYGUKPC4aMcGZAwXhwa38dk5g3zVj4xdYSDsY0t 4bzwFGynwGH/ZIcJZnw== X-Received: from wmbhi18.prod.google.com ([2002:a05:600c:5352:b0:495:5422:15e3]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3512:b0:495:4572:21af with SMTP id 5b1f17b1804b1-4954a3ef7dbmr238679855e9.9.1784711777195; Wed, 22 Jul 2026 02:16:17 -0700 (PDT) Date: Wed, 22 Jul 2026 09:16:04 +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=1836; i=aliceryhl@google.com; h=from:subject:message-id; bh=2b6L2eXi0rZnUWrbz0nUqiyHf39Qr9F7UnkV1u9przE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBqYIpas4y/7rCjWTJa1nAy74sXllLbFrMDrj0Ke vy4i59SgbuJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCamCKWgAKCRAEWL7uWMY5 Rg83D/91oaUidxNFz+BG7bXj+iXTwCt0prL+qV9Z7JDRS4YVNLFzSMLysmQnwkAs89FOGktC1bX /LF47L1f+xCN7sQnmt52IHGocGVMMX8ntn8IRGVYfyzAK5vU3UD9UJMgOkopKOM/jmHyk1yi/Ez /x316krIN2LBbvT31uAdrWfdshY0UxPyJDd58Rucq4jox6877lYKvjXDQ6Krnyah77n/TrBM5YE pueY2ruXcv7io/gR9bbzzMmhyWm3kddrl/72hIiKZ+5NgdPEo9zhTBsFZl1v9957VcJsLZxVvYw /Ztvs3ZgG2rhYsGqZnUEmEDJhxwqnkB7X3l5o/5YRF8byR+JxcCAiilGgSzfEA6UBF4Cd+JnxZz WbL3MRgOUY99fMXLEisT/jtUrbPEY1KkdiQhnh7MgOdeZ3bukpp7+spkRlJ/v2R1V8hAdPR+08G bFxjIX/tM4/Y8POiwXPJcRHPqz7qk9uRgG6stA6kuN1/aHXs41H8uzk1HuCMy8divuBw2ysSt3B bazrD+E9Pr4eBVw/iMpyaJYPscnjw3uOnNcw1ljY2KAAmpHxCqGO+Kb40Yumnfg29AwJvqAVlDF IyAODTBskJo4ITfA5eaNB1GcLEhLSqVTPHaO2U3IhHdqf6n8FYJe0XIu9fD0voijdMnT+NbGI/M 1gpBYqrmeRvAKhg== X-Mailer: b4 0.14.3 Message-ID: <20260722-setonce-populate-v1-3-fa7455c26c42@google.com> Subject: [PATCH 3/3] rust_binder: use SetOnce::try_get_or_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" Content-Transfer-Encoding: quoted-printable Since this method has just been added, use it instead of open coding the loop. This also has the side effect of dropping the PollCondVarBox outside of the node_refs lock when two threads initialize it in parallel. Suggested-by: Boqun Feng Link: https://lore.kernel.org/all/alJHCkMIcnXYPNoJ@tardis.local/ Signed-off-by: Alice Ryhl --- drivers/android/binder/process.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/pro= cess.rs index d486bf7c0b8a..5f8779badd3d 100644 --- a/drivers/android/binder/process.rs +++ b/drivers/android/binder/process.rs @@ -1793,21 +1793,12 @@ pub(crate) fn poll( table: PollTable<'_>, ) -> Result { let thread =3D this.get_current_thread()?; - { - let poll =3D loop { - if let Some(poll) =3D this.poll.as_ref() { - break poll; - } =20 - let poll =3D PollCondVarBox::new(c"Process::poll", kernel:= :static_lock_class!())?; - // Reuse our existing lock to synchronize callers initiali= zing. - let guard =3D this.node_refs.lock(); - let _ret =3D this.poll.populate(poll); - drop(guard); - }; + let poll =3D this.poll.try_get_or_populate(&this.node_refs, || { + PollCondVarBox::new(c"Process::poll", kernel::static_lock_clas= s!()) + })?; + table.register_wait(file, poll); =20 - table.register_wait(file, poll); - } let (from_proc, mut mask) =3D thread.poll()?; if mask =3D=3D 0 && from_proc && !this.inner.lock().work.is_empty(= ) { mask |=3D bindings::POLLIN; --=20 2.55.0.229.g6434b31f56-goog