Skip to content

Commit 5370b43

Browse files
mjguzikbrauner
authored andcommitted
fs: reduce work in fdget_pos()
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent dba2e3b commit 5370b43

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

fs/file.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,16 +1183,21 @@ struct fd fdget_raw(unsigned int fd)
11831183
*/
11841184
static inline bool file_needs_f_pos_lock(struct file *file)
11851185
{
1186-
return (file->f_mode & FMODE_ATOMIC_POS) &&
1187-
(file_count(file) > 1 || file->f_op->iterate_shared);
1186+
if (!(file->f_mode & FMODE_ATOMIC_POS))
1187+
return false;
1188+
if (__file_ref_read_raw(&file->f_ref) != FILE_REF_ONEREF)
1189+
return true;
1190+
if (file->f_op->iterate_shared)
1191+
return true;
1192+
return false;
11881193
}
11891194

11901195
struct fd fdget_pos(unsigned int fd)
11911196
{
11921197
struct fd f = fdget(fd);
11931198
struct file *file = fd_file(f);
11941199

1195-
if (file && file_needs_f_pos_lock(file)) {
1200+
if (likely(file) && file_needs_f_pos_lock(file)) {
11961201
f.word |= FDPUT_POS_UNLOCK;
11971202
mutex_lock(&file->f_pos_lock);
11981203
}

include/linux/file_ref.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,18 @@ static inline unsigned long file_ref_read(file_ref_t *ref)
208208
return c >= FILE_REF_RELEASED ? 0 : c + 1;
209209
}
210210

211+
/*
212+
* __file_ref_read_raw - Return the value stored in ref->refcnt
213+
* @ref: Pointer to the reference count
214+
*
215+
* Return: The raw value found in the counter
216+
*
217+
* A hack for file_needs_f_pos_lock(), you probably want to use
218+
* file_ref_read() instead.
219+
*/
220+
static inline unsigned long __file_ref_read_raw(file_ref_t *ref)
221+
{
222+
return atomic_long_read(&ref->refcnt);
223+
}
224+
211225
#endif

0 commit comments

Comments
 (0)