avoid the extra atomic on a ref when closing a fd
cover avoid the extra atomic on a ref when closing a fd
The stock kernel transitioning the file to no refs held penalizes the caller with an extra atomic to block any increments.
For cases where the file is highly likely to be going away this is easily avoidable.
The obvious place is the close() system call, patched in 2/2.
But part from that possible callers (to be evaluated):
- clean up after failed open (common)
- close_range
- close on exec
- process exit
So 1/1 adds some routines and 2/2 uses one of them. The rest can be tackled on later or I can send a v2 with more places sorted out.
In the open+close case the win is very modest because of the following problems:
- kmem and memcg having terrible performance
- putname using an atomic (I have a wip to whack that)
- open performing an extra ref/unref on the dentry (there are patches to do it, including by Al. I'll be mailing about it separately)
- creds using atomics (I have a wip to whack that)
- apparmor using atomics (ditto, same mechanism)
On top of that I have a WIP patch to dodge some of the work at lookup itself.
All in all there is several % avoidably lost here.
Mateusz Guzik (2):
file: add fput and file_ref_put routines optimized for use when
closing a fd
fs: use fput_close() in close()
fs/file.c | 75 ++++++++++++++++++++++++++++++---------- fs/file_table.c | 72 +++++++++++++++++++++++++++----------- fs/open.c | 2 +- include/linux/file.h | 2 ++ include/linux/file_ref.h | 1 + 5 files changed, 112 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 file: add fput and file_ref_put routines optimized for use when closing a fd
Vast majority of the time closing a file descriptor also operates on the last reference, where a regular fput usage will result in 2 atomics. This can be changed to only suffer 1.
See commentary above file_ref_put_close() for more information.
--- fs/file.c | 75 ++++++++++++++++++++++++++++++---------- fs/file_table.c | 72 +++++++++++++++++++++++++++----------- include/linux/file.h | 2 ++ include/linux/file_ref.h | 1 + 4 files changed, 111 insertions(+), 39 deletions(-)
fs/file.c +57 -18
@@ -26,6 +26,28 @@#include "internal.h" +static bool __file_ref_put_badval (file_ref_t *ref,unsigned long cnt) +{ +/* +* If the reference count was already in the dead zone, then this +* put() operation is imbalanced. Warn, put the reference count back to +* DEAD and tell the caller to not deconstruct the object. +*/ +if (WARN_ONCE (cnt >=FILE_REF_RELEASED ,"imbalanced put on file reference count" )) { +atomic_long_set (&ref->refcnt,FILE_REF_DEAD ); +return false; + } + +/* +* This is a put() operation on a saturated refcount. Restore the +* mean saturation value and tell the caller to not deconstruct the +* object. +*/ +if (cnt >FILE_REF_MAXREF ) +atomic_long_set (&ref->refcnt,FILE_REF_SATURATED ); +return false; +} +/** * __file_ref_put - Slowpath of file_ref_put() * @ref: Pointer to the reference count
@@ -67,27 +89,44 @@ bool __file_ref_put(file_ref_t *ref, unsigned long cnt)return true; } -/* -* If the reference count was already in the dead zone, then this -* put() operation is imbalanced. Warn, put the reference count back to -* DEAD and tell the caller to not deconstruct the object. -*/ -if (WARN_ONCE (cnt >=FILE_REF_RELEASED ,"imbalanced put on file reference count" )) { -atomic_long_set (&ref->refcnt,FILE_REF_DEAD ); -return false; - } - -/* -* This is a put() operation on a saturated refcount. Restore the -* mean saturation value and tell the caller to not deconstruct the -* object. -*/ -if (cnt >FILE_REF_MAXREF ) -atomic_long_set (&ref->refcnt,FILE_REF_SATURATED ); -return false; +return __file_ref_put_badval (ref, cnt); }EXPORT_SYMBOL_GPL (__file_ref_put); +/** +* file_ref_put_close - drop a reference expecting it would transition to FILE_REF_NOREF +* @ref: Pointer to the reference count +* +* Semantically it is equivalent to calling file_ref_put(), but it trades lower +* performance in face of other CPUs also modifying the refcount for higher +* performance when this happens to be the last reference. +* +* For the last reference file_ref_put() issues 2 atomics. One to drop the +* reference and another to transition it to FILE_REF_DEAD. This routine does +* the work in one step, but in order to do it has to pre-read the variable which +* decreases scalability. +* +* Use with close() et al, stick to file_ref_put() by default. +*/ +bool file_ref_put_close (file_ref_t *ref) +{ +long old, new; + + 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 ; +} +EXPORT_SYMBOL_GPL (file_ref_put_close); +unsigned int sysctl_nr_open__read_mostly =1024 *1024 ;unsigned int sysctl_nr_open_min =BITS_PER_LONG ;/* our min() is unusable in constant expressions ;-/ */
fs/file_table.c +51 -21
92 lines not on this page · raw message
include/linux/file.h +2 -0
@@ -124,6 +124,8 @@ int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);extern void flush_delayed_fput (void );extern void __fput_sync (struct file *); +void fput_close_sync (struct file *); +void fput_close (struct file *);extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
include/linux/file_ref.h +1 -0
@@ -62,6 +62,7 @@ static inline void file_ref_init(file_ref_t *ref, unsigned long cnt) }bool __file_ref_put (file_ref_t *ref,unsigned long cnt); +bool file_ref_put_close (file_ref_t *ref);/** * file_ref_get - Acquire one reference on a file
Signature · 1 line
2.43.0
PATCH 2/2 fs: use fput_close() in close()
In reply to Mateusz GuzikThis bumps open+close rate by 1% on Sapphire Rapids by eliding one atomic.
It would be higher if it was not for several other slowdowns of the same nature.
--- fs/open.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/open.c +1 -1
@@ -1577,7 +1577,7 @@ SYSCALL_DEFINE1(close, unsigned int, fd)* We're returning to user space. Don't bother * with any delayed fput() cases. */ -__fput_sync (file); +fput_close_sync (file);if (likely (retval ==0 ))return 0 ;
Signature · 1 line
2.43.0