Re: [PATCH v2] rust: sync: require `T: Sync` for `LockedBy::access`

From: Gary Guo
Date: Sun Sep 15 2024 - 11:36:40 EST


On Sun, 15 Sep 2024 14:41:28 +0000
Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:

> 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@xxxxxxxxxxxxxxx
> Fixes: 7b1f55e3a984 ("rust: sync: introduce `LockedBy`")
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>

You probably also want to have a Suggested-by to credit Boqun for
suggesting the current implementation.

Best,
Gary

> ---
> Changes in v2:
> - Use a `where T: Sync` on `access` instead of changing `impl Sync for
> LockedBy`.
> - Link to v1: https://lore.kernel.org/r/20240912-locked-by-sync-fix-v1-1-26433cbccbd2@xxxxxxxxxx
> ---
> rust/kernel/sync/locked_by.rs | 18 ++++++++++++++----
> 1 file changed, 14 insertions(+), 4 deletions(-)