[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <buw7x4axu2vojhr5mtjtrfnbynianng42atvzz7gk2t2tbjvqc@tvfo7bbjkgsx>
Date: Fri, 22 Aug 2025 16:20:57 +0200
From: Jan Kara <jack@...e.cz>
To: Uros Bizjak <ubizjak@...il.com>
Cc: linux-fsdevel@...r.kernel.org, linux-kernel@...r.kernel.org,
Alexander Viro <viro@...iv.linux.org.uk>, Christian Brauner <brauner@...nel.org>, Jan Kara <jack@...e.cz>
Subject: Re: [PATCH] fs: Use try_cmpxchg() in start_dir_add()
On Mon 11-08-25 14:52:38, Uros Bizjak wrote:
> Use try_cmpxchg() instead of cmpxchg(*ptr, old, new) == old.
>
> The x86 CMPXCHG instruction returns success in the ZF flag,
> so this change saves a compare after CMPXCHG (and related
> move instruction in front of CMPXCHG).
>
> Note that the value from *ptr should be read using READ_ONCE() to
> prevent the compiler from merging, refetching or reordering the read.
>
> No functional change intended.
>
> Signed-off-by: Uros Bizjak <ubizjak@...il.com>
> Cc: Alexander Viro <viro@...iv.linux.org.uk>
> Cc: Christian Brauner <brauner@...nel.org>
> Cc: Jan Kara <jack@...e.cz>
Looks good. Feel free to add:
Reviewed-by: Jan Kara <jack@...e.cz>
Honza
> ---
> fs/dcache.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/dcache.c b/fs/dcache.c
> index 60046ae23d51..336bdb4c4b1f 100644
> --- a/fs/dcache.c
> +++ b/fs/dcache.c
> @@ -2509,8 +2509,8 @@ static inline unsigned start_dir_add(struct inode *dir)
> {
> preempt_disable_nested();
> for (;;) {
> - unsigned n = dir->i_dir_seq;
> - if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
> + unsigned n = READ_ONCE(dir->i_dir_seq);
> + if (!(n & 1) && try_cmpxchg(&dir->i_dir_seq, &n, n + 1))
> return n;
> cpu_relax();
> }
> --
> 2.50.1
>
--
Jan Kara <jack@...e.com>
SUSE Labs, CR
Powered by blists - more mailing lists