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 277DC1DEFE4; Tue, 8 Oct 2024 12:27:53 +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=1728390473; cv=none; b=tzBF3wL3ChkOjPapMU4qpXC6NtyC5/c8gvErnJA4tgn38VGWYyRfKXmlrGkspx+x5ab5I/8wBkGw+potdPr30ECeI8jdGBCuzqmuiSKOAXyzEw/Kkl7tnSCo62qHzHGR+eEaFKo1Ed9E+xzs+ArEudUp2xOyUwD+Jvxz7PdfhZA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728390473; c=relaxed/simple; bh=f6VHJpZGwmjZeCi0ukJuKH19S2yJ7mUB/azYCUngjS8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RoTfeRzEj7K2K9xP/IeK/ga1fUzpfIC41Kf1t6osUvTWePeqUeu6p6ADKK3B9ebo0rWxNs50b9FJKL6DLrB2Nkg6a6/6i3rIcAYxvkP4QmrW+med/ff23E1Y/gcNWfVu/+xJS/NOYCEO/n33ICxzXiPUQ8T6v4vC9bEm7fSLTKg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=lugk0kGv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="lugk0kGv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E447C4CECF; Tue, 8 Oct 2024 12:27:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1728390473; bh=f6VHJpZGwmjZeCi0ukJuKH19S2yJ7mUB/azYCUngjS8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lugk0kGvCLZzcuZwlcnq/qHoOnuVHLa0UJU3f8sqNP30E7oixmbnp9IlNhkttYExM BxSQtAeZRT92vgkPDJWAXCaffsWoa+sN/GkyFtT489J9SLAhi4LdgXjJ+l1yNNVeli BbV9aVytmT2BvMQdxwQClnhCiT4q/3hYDJ/0vesE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alice Ryhl , Boqun Feng , Gary Guo , Miguel Ojeda Subject: [PATCH 6.10 300/482] rust: sync: require `T: Sync` for `LockedBy::access` Date: Tue, 8 Oct 2024 14:06:03 +0200 Message-ID: <20241008115700.090419466@linuxfoundation.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241008115648.280954295@linuxfoundation.org> References: <20241008115648.280954295@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alice Ryhl commit a8ee30f45d5d57467ddb7877ed6914d0eba0af7f upstream. The `LockedBy::access` method only requires a shared reference to the owner, so if we have shared access to the `LockedBy` from several threads at once, then two threads could call `access` in parallel and both obtain a shared reference to the inner value. Thus, require that `T: Sync` when calling the `access` method. An alternative is to require `T: Sync` in the `impl Sync for LockedBy`. This patch does not choose that approach as it gives up the ability to use `LockedBy` with `!Sync` types, which is okay as long as you only use `access_mut`. Cc: stable@vger.kernel.org Fixes: 7b1f55e3a984 ("rust: sync: introduce `LockedBy`") Signed-off-by: Alice Ryhl Suggested-by: Boqun Feng Reviewed-by: Gary Guo Link: https://lore.kernel.org/r/20240915-locked-by-sync-fix-v2-1-1a8d89710392@google.com Signed-off-by: Miguel Ojeda Signed-off-by: Greg Kroah-Hartman --- rust/kernel/sync/locked_by.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) --- a/rust/kernel/sync/locked_by.rs +++ b/rust/kernel/sync/locked_by.rs @@ -83,8 +83,12 @@ pub struct LockedBy Send for LockedBy {} -// SAFETY: `LockedBy` serialises the interior mutability it provides, so it is `Sync` as long as the -// data it protects is `Send`. +// SAFETY: If `T` is not `Sync`, then parallel shared access to this `LockedBy` allows you to use +// `access_mut` to hand out `&mut T` on one thread at the time. The requirement that `T: Send` is +// sufficient to allow that. +// +// If `T` is `Sync`, then the `access` method also becomes available, which allows you to obtain +// several `&T` from several threads at once. However, this is okay as `T` is `Sync`. unsafe impl Sync for LockedBy {} impl LockedBy { @@ -118,7 +122,10 @@ impl LockedBy { /// /// Panics if `owner` is different from the data protected by the lock used in /// [`new`](LockedBy::new). - pub fn access<'a>(&'a self, owner: &'a U) -> &'a T { + pub fn access<'a>(&'a self, owner: &'a U) -> &'a T + where + T: Sync, + { build_assert!( size_of::() > 0, "`U` cannot be a ZST because `owner` wouldn't be unique" @@ -127,7 +134,10 @@ impl LockedBy { panic!("mismatched owners"); } - // SAFETY: `owner` is evidence that the owner is locked. + // SAFETY: `owner` is evidence that there are only shared references to the owner for the + // duration of 'a, so it's not possible to use `Self::access_mut` to obtain a mutable + // reference to the inner value that aliases with this shared reference. The type is `Sync` + // so there are no other requirements. unsafe { &*self.data.get() } }