Re: [PATCH] fs: dodge an atomic in putname if ref == 1

From: Jens Axboe
Date: Wed Mar 12 2025 - 12:08:35 EST


On 3/11/25 12:18 PM, Mateusz Guzik wrote:
> diff --git a/fs/namei.c b/fs/namei.c
> index 06765d320e7e..add90981cfcd 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -275,14 +275,19 @@ EXPORT_SYMBOL(getname_kernel);
>
> void putname(struct filename *name)
> {
> + int refcnt;
> +
> if (IS_ERR_OR_NULL(name))
> return;
>
> - if (WARN_ON_ONCE(!atomic_read(&name->refcnt)))
> - return;
> + refcnt = atomic_read(&name->refcnt);
> + if (refcnt != 1) {
> + if (WARN_ON_ONCE(!refcnt))
> + return;
>
> - if (!atomic_dec_and_test(&name->refcnt))
> - return;
> + if (!atomic_dec_and_test(&name->refcnt))
> + return;
> + }

Looks good to me, I use this trick with bitops too all the time, to
avoid a RMW when possible.

Reviewed-by: Jens Axboe <axboe@xxxxxxxxx>

--
Jens Axboe