Re: [PATCH 1/3] rust: implement `kernel::sync::Refcount`

From: Greg KH
Date: Sat Oct 05 2024 - 03:41:07 EST


On Fri, Oct 04, 2024 at 04:52:22PM +0100, Gary Guo wrote:
> This is a wrapping layer of `include/linux/refcount.h`. Currently only
> the most basic operations (read/set/inc/dec/dec_and_test) are implemented,
> additional methods can be implemented when they are needed.
>
> Currently the kernel refcount has already been used in `Arc`, however it
> calls into FFI directly.
>
> Cc: Will Deacon <will@xxxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Boqun Feng <boqun.feng@xxxxxxxxx>
> Cc: Mark Rutland <mark.rutland@xxxxxxx>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
> ---
> rust/helpers/refcount.c | 15 ++++++
> rust/kernel/sync.rs | 2 +
> rust/kernel/sync/refcount.rs | 94 ++++++++++++++++++++++++++++++++++++
> 3 files changed, 111 insertions(+)
> create mode 100644 rust/kernel/sync/refcount.rs
>
> diff --git a/rust/helpers/refcount.c b/rust/helpers/refcount.c
> index f47afc148ec3..39649443426b 100644
> --- a/rust/helpers/refcount.c
> +++ b/rust/helpers/refcount.c
> @@ -8,11 +8,26 @@ refcount_t rust_helper_REFCOUNT_INIT(int n)
> return (refcount_t)REFCOUNT_INIT(n);
> }
>
> +unsigned int rust_helper_refcount_read(refcount_t *r)
> +{
> + return refcount_read(r);
> +}

Reading a refcount is almost always a wrong thing to do (it can change
right after you read it), and I don't see any of the later patches in
this series use this call, so can you just drop this?

thanks,

greg k-h