Skip to content

Commit

Permalink
fs: fall back to file_ref_put() for non-last reference
Browse files Browse the repository at this point in the history
This reduces the slowdown in face of multiple callers issuing close on
what turns out to not be the last reference.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/20250418125756.59677-1-mjguzik@gmail.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202504171513.6d6f8a16-lkp@intel.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
  • Loading branch information
Mateusz Guzik authored and Christian Brauner committed Apr 22, 2025
1 parent 53f7eed commit d1f7256
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
2 changes: 1 addition & 1 deletion fs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "internal.h"

bool __file_ref_put_badval(file_ref_t *ref, unsigned long cnt)
static noinline bool __file_ref_put_badval(file_ref_t *ref, unsigned long cnt)
{
/*
* If the reference count was already in the dead zone, then this
Expand Down
19 changes: 6 additions & 13 deletions include/linux/file_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ static inline void file_ref_init(file_ref_t *ref, unsigned long cnt)
atomic_long_set(&ref->refcnt, cnt - 1);
}

bool __file_ref_put_badval(file_ref_t *ref, unsigned long cnt);
bool __file_ref_put(file_ref_t *ref, unsigned long cnt);

/**
Expand Down Expand Up @@ -178,20 +177,14 @@ static __always_inline __must_check bool file_ref_put(file_ref_t *ref)
*/
static __always_inline __must_check bool file_ref_put_close(file_ref_t *ref)
{
long old, new;
long old;

old = atomic_long_read(&ref->refcnt);
do {
if (unlikely(old < 0))
return __file_ref_put_badval(ref, old);

if (old == FILE_REF_ONEREF)
new = FILE_REF_DEAD;
else
new = old - 1;
} while (!atomic_long_try_cmpxchg(&ref->refcnt, &old, new));

return new == FILE_REF_DEAD;
if (likely(old == FILE_REF_ONEREF)) {
if (likely(atomic_long_try_cmpxchg(&ref->refcnt, &old, FILE_REF_DEAD)))
return true;
}
return file_ref_put(ref);
}

/**
Expand Down

0 comments on commit d1f7256

Please sign in to comment.