fs: reduce work in fdget_pos()
authorMateusz Guzik <mjguzik@gmail.com>
Wed, 19 Mar 2025 21:58:01 +0000 (22:58 +0100)
committerChristian Brauner <brauner@kernel.org>
Thu, 20 Mar 2025 08:45:39 +0000 (09:45 +0100)
1. predict the file was found
2. explicitly compare the ref to "one", ignoring the dead zone

The latter arguably improves the behavior to begin with. Suppose the
count turned bad -- the previously used ref routine is going to check
for it and return 0, indicating the count does not necessitate taking
->f_pos_lock. But there very well may be several users.

i.e. not paying for special-casing the dead zone improves semantics.

While here spell out each condition in a dedicated if statement. This
has no effect on generated code.

Sizes are as follows (in bytes; gcc 13, x86-64):
stock: 321
likely():  298
likely()+ref: 280

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/r/20250319215801.1870660-1-mjguzik@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/file.c
include/linux/file_ref.h

index 3fef798b96e5b67627182061e650d4b81092bbf0..134274446bf63b00311bcc947984b3daeb8d6904 100644 (file)
--- a/fs/file.c
+++ b/fs/file.c
@@ -1183,8 +1183,13 @@ struct fd fdget_raw(unsigned int fd)
  */
 static inline bool file_needs_f_pos_lock(struct file *file)
 {
-       return (file->f_mode & FMODE_ATOMIC_POS) &&
-               (file_count(file) > 1 || file->f_op->iterate_shared);
+       if (!(file->f_mode & FMODE_ATOMIC_POS))
+               return false;
+       if (__file_ref_read_raw(&file->f_ref) != FILE_REF_ONEREF)
+               return true;
+       if (file->f_op->iterate_shared)
+               return true;
+       return false;
 }
 
 struct fd fdget_pos(unsigned int fd)
@@ -1192,7 +1197,7 @@ struct fd fdget_pos(unsigned int fd)
        struct fd f = fdget(fd);
        struct file *file = fd_file(f);
 
-       if (file && file_needs_f_pos_lock(file)) {
+       if (likely(file) && file_needs_f_pos_lock(file)) {
                f.word |= FDPUT_POS_UNLOCK;
                mutex_lock(&file->f_pos_lock);
        }
index 6ef92d765a6684cb2b5ec632f7106c00ba00eee3..7db62fbc0500b009059520502a441cee471fa74f 100644 (file)
@@ -208,4 +208,18 @@ static inline unsigned long file_ref_read(file_ref_t *ref)
        return c >= FILE_REF_RELEASED ? 0 : c + 1;
 }
 
+/*
+ * __file_ref_read_raw - Return the value stored in ref->refcnt
+ * @ref: Pointer to the reference count
+ *
+ * Return: The raw value found in the counter
+ *
+ * A hack for file_needs_f_pos_lock(), you probably want to use
+ * file_ref_read() instead.
+ */
+static inline unsigned long __file_ref_read_raw(file_ref_t *ref)
+{
+       return atomic_long_read(&ref->refcnt);
+}
+
 #endif