Skip to contentSkip to conversation

fs: don't needlessly acquire f_lock

PATCH In review Christian Brauner 5 participants 26 messages Feb 7, 2025 – Jun 16, 2025 VFS

lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

PATCH 1/1 fs: don't needlessly acquire f_lock

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

---
 fs/read_write.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
fs/read_write.c +15 -5
index a6133241dfb8..816189f9c56d 100644
@@ -168,13 +168,23 @@ generic_file_llseek_size(struct file *file, loff_t offset, int whence,
 		return offset;
 
 	if (whence == SEEK_CUR) {
+		bool locked;
+
 		/*
-		 * f_lock protects against read/modify/write race with
-		 * other SEEK_CURs. Note that parallel writes and reads
-		 * behave like SEEK_SET.
+		 * If the file requires locking via f_pos_lock we know
+		 * that mutual exclusion for SEEK_CUR on the same file
+		 * is guaranteed. If the file isn't locked, we take
+		 * f_lock to protect against f_pos races with other
+		 * SEEK_CURs.
 		 */
-		guard(spinlock)(&file->f_lock);
-		return vfs_setpos(file, file->f_pos + offset, maxsize);
+		locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+			 file->f_op->iterate_shared;
+		if (!locked)
+			spin_lock(&file->f_lock);
+		offset = vfs_setpos(file, file->f_pos + offset, maxsize);
+		if (!locked)
+			spin_unlock(&file->f_lock);
+		return offset;
 	}
 
 	return vfs_setpos(file, offset, maxsize);
Signature · 1 line
2.47.2
#1 lore raw
Quoted 6 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Quoted 8 lines

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

What workloads benefit from this optimisation?

reviewer
#2 lore raw
In reply to Christian Brauner
Quoted 16 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

...

Quoted 7 lines
fs/read_write.c +2 -3
 	if (whence == SEEK_CUR) {
+		bool locked;
+
 		/*
-		 * f_lock protects against read/modify/write race with
-		 * other SEEK_CURs. Note that parallel writes and reads
-		 * behave like SEEK_SET.

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence, · line 181

Quoted 10 lines
fs/read_write.c +7 -2
+		 * If the file requires locking via f_pos_lock we know
+		 * that mutual exclusion for SEEK_CUR on the same file
+		 * is guaranteed. If the file isn't locked, we take
+		 * f_lock to protect against f_pos races with other
+		 * SEEK_CURs.
 		 */
-		guard(spinlock)(&file->f_lock);
-		return vfs_setpos(file, file->f_pos + offset, maxsize);
+		locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+			 file->f_op->iterate_shared;

As far as I understand the rationale this should match to file_needs_f_pos_lock() (or it can possibly be weaker) but it isn't obvious to me that's the case. After thinking about possibilities, I could convince myself that what you suggest is indeed safe but the condition being in two completely independent places and leading to subtle bugs if it gets out of sync seems a bit fragile to me.

Honza

Quoted 12 lines
fs/read_write.c +6 -0
+		if (!locked)
+			spin_lock(&file->f_lock);
+		offset = vfs_setpos(file, file->f_pos + offset, maxsize);
+		if (!locked)
+			spin_unlock(&file->f_lock);
+		return offset;
 	}
 
 	return vfs_setpos(file, offset, maxsize);
Signature · 1 line
2.47.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
Quoted 39 lines · Jan Kara
Quoted 16 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

...

Quoted 7 lines
      if (whence == SEEK_CUR) {
fs/read_write.c +2 -3
+             bool locked;
+
              /*
-              * f_lock protects against read/modify/write race with
-              * other SEEK_CURs. Note that parallel writes and reads
-              * behave like SEEK_SET.

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence, · line 181

Quoted 10 lines
fs/read_write.c +7 -2
+              * If the file requires locking via f_pos_lock we know
+              * that mutual exclusion for SEEK_CUR on the same file
+              * is guaranteed. If the file isn't locked, we take
+              * f_lock to protect against f_pos races with other
+              * SEEK_CURs.
               */
-             guard(spinlock)(&file->f_lock);
-             return vfs_setpos(file, file->f_pos + offset, maxsize);
+             locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+                      file->f_op->iterate_shared;
Quoted 7 lines

As far as I understand the rationale this should match to file_needs_f_pos_lock() (or it can possibly be weaker) but it isn't obvious to me that's the case. After thinking about possibilities, I could convince myself that what you suggest is indeed safe but the condition being in two completely independent places and leading to subtle bugs if it gets out of sync seems a bit fragile to me.

A debug-only assert that the lock is held when expected should sort it out?

Quoted 9 lines

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence, · line 190

Quoted 9 lines
fs/read_write.c +6 -0
+             if (!locked)
+                     spin_lock(&file->f_lock);
+             offset = vfs_setpos(file, file->f_pos + offset, maxsize);
+             if (!locked)
+                     spin_unlock(&file->f_lock);
+             return offset;
      }
 
      return vfs_setpos(file, offset, maxsize);

btw I ran this sucker over a kernel build:

bpftrace -e 'kprobe:generic_file_llseek_size { @[((struct file *)arg0)->f_mode & (1 << 15), ((struct file *)arg0)->f_op->iterate_shared, arg2] = count(); }
'
Attaching 1 probe...
^C

@[32768, 0x0, 2]: 9
@[32768, 0x0, 1]: 171797
@[32768, 0x0, 0]: 660866

SEEK_CUR is 1, so this *does* show up.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
author maintainer
#4 lore raw
Quoted 48 lines · Mateusz Guzik
Quoted 39 lines · Jan Kara
Quoted 16 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

...

Quoted 7 lines
      if (whence == SEEK_CUR) {
fs/read_write.c +2 -3
+             bool locked;
+
              /*
-              * f_lock protects against read/modify/write race with
-              * other SEEK_CURs. Note that parallel writes and reads
-              * behave like SEEK_SET.

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence, · line 181

Quoted 10 lines
fs/read_write.c +7 -2
+              * If the file requires locking via f_pos_lock we know
+              * that mutual exclusion for SEEK_CUR on the same file
+              * is guaranteed. If the file isn't locked, we take
+              * f_lock to protect against f_pos races with other
+              * SEEK_CURs.
               */
-             guard(spinlock)(&file->f_lock);
-             return vfs_setpos(file, file->f_pos + offset, maxsize);
+             locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+                      file->f_op->iterate_shared;
Quoted 7 lines

As far as I understand the rationale this should match to file_needs_f_pos_lock() (or it can possibly be weaker) but it isn't obvious to me that's the case. After thinking about possibilities, I could convince myself that what you suggest is indeed safe but the condition being in two completely independent places and leading to subtle bugs if it gets out of sync seems a bit fragile to me.

Quoted 1 line

A debug-only assert that the lock is held when expected should sort it out?

Good idea. Let me reuse the newly added infra:
VFS_WARN_ON_ONCE(locked && !mutex_is_locked(&file->f_pos_lock));

Quoted 20 lines
Quoted 10 lines

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence, · line 190

Quoted 10 lines
fs/read_write.c +6 -0
+             if (!locked)
+                     spin_lock(&file->f_lock);
+             offset = vfs_setpos(file, file->f_pos + offset, maxsize);
+             if (!locked)
+                     spin_unlock(&file->f_lock);
+             return offset;
      }
 
      return vfs_setpos(file, offset, maxsize);

btw I ran this sucker over a kernel build:

bpftrace -e 'kprobe:generic_file_llseek_size { @[((struct file *)arg0)->f_mode & (1 << 15), ((struct file *)arg0)->f_op->iterate_shared, arg2] = count(); }
'
Attaching 1 probe...
^C

Quoted 5 lines

@[32768, 0x0, 2]: 9
@[32768, 0x0, 1]: 171797
@[32768, 0x0, 0]: 660866

SEEK_CUR is 1, so this *does* show up.

Thanks for the perf.

reviewer
#5 lore raw
Quoted 49 lines · Christian Brauner
Quoted 48 lines · Mateusz Guzik
Quoted 46 lines · Jan Kara
Quoted 16 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

...

Quoted 7 lines
      if (whence == SEEK_CUR) {
fs/read_write.c +2 -3
+             bool locked;
+
              /*
-              * f_lock protects against read/modify/write race with
-              * other SEEK_CURs. Note that parallel writes and reads
-              * behave like SEEK_SET.

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence, · line 181

Quoted 10 lines
fs/read_write.c +7 -2
+              * If the file requires locking via f_pos_lock we know
+              * that mutual exclusion for SEEK_CUR on the same file
+              * is guaranteed. If the file isn't locked, we take
+              * f_lock to protect against f_pos races with other
+              * SEEK_CURs.
               */
-             guard(spinlock)(&file->f_lock);
-             return vfs_setpos(file, file->f_pos + offset, maxsize);
+             locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+                      file->f_op->iterate_shared;

As far as I understand the rationale this should match to file_needs_f_pos_lock() (or it can possibly be weaker) but it isn't obvious to me that's the case. After thinking about possibilities, I could convince myself that what you suggest is indeed safe but the condition being in two completely independent places and leading to subtle bugs if it gets out of sync seems a bit fragile to me.

Quoted 4 lines
Quoted 1 line

A debug-only assert that the lock is held when expected should sort it out?

Good idea. Let me reuse the newly added infra:
VFS_WARN_ON_ONCE(locked && !mutex_is_locked(&file->f_pos_lock));

Fine, but won't this actually trigger? file_needs_f_pos_lock() is:

        return (file->f_mode & FMODE_ATOMIC_POS) &&
                (file_count(file) > 1 || file->f_op->iterate_shared);

and here you have:

        locked = (file->f_mode & FMODE_ATOMIC_POS) ||
                  file->f_op->iterate_shared;

So if (file->f_mode & FMODE_ATOMIC_POS) and (file_count(file) == 1), file_needs_f_pos_lock() returns false but locked is true?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#6 lore raw
Quoted 60 lines · Jan Kara
Quoted 49 lines · Christian Brauner
Quoted 48 lines · Mateusz Guzik
Quoted 46 lines · Jan Kara
Quoted 16 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

...

Quoted 7 lines
      if (whence == SEEK_CUR) {
(quoted diff) +2 -3
+             bool locked;
+
              /*
-              * f_lock protects against read/modify/write race with
-              * other SEEK_CURs. Note that parallel writes and reads
-              * behave like SEEK_SET.

Review

Quoted 10 lines
(quoted diff) +7 -2
+              * If the file requires locking via f_pos_lock we know
+              * that mutual exclusion for SEEK_CUR on the same file
+              * is guaranteed. If the file isn't locked, we take
+              * f_lock to protect against f_pos races with other
+              * SEEK_CURs.
               */
-             guard(spinlock)(&file->f_lock);
-             return vfs_setpos(file, file->f_pos + offset, maxsize);
+             locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+                      file->f_op->iterate_shared;

As far as I understand the rationale this should match to file_needs_f_pos_lock() (or it can possibly be weaker) but it isn't obvious to me that's the case. After thinking about possibilities, I could convince myself that what you suggest is indeed safe but the condition being in two completely independent places and leading to subtle bugs if it gets out of sync seems a bit fragile to me.

Quoted 4 lines
Quoted 1 line

A debug-only assert that the lock is held when expected should sort it out?

Good idea. Let me reuse the newly added infra:
VFS_WARN_ON_ONCE(locked && !mutex_is_locked(&file->f_pos_lock));

Fine, but won't this actually trigger? file_needs_f_pos_lock() is:

        return (file->f_mode & FMODE_ATOMIC_POS) &&
                (file_count(file) > 1 || file->f_op->iterate_shared);
Quoted 7 lines

and here you have:

        locked = (file->f_mode & FMODE_ATOMIC_POS) ||
                  file->f_op->iterate_shared;

So if (file->f_mode & FMODE_ATOMIC_POS) and (file_count(file) == 1), file_needs_f_pos_lock() returns false but locked is true?

Sorry, I got lost in other mails.
Yes, you're right. I had changed the patch in my tree to fix that. I'll append it here. Sorry about that. Tell me if that looks sane ok to you now.

From 1534e4816a5c128dbf6e6942ab43b780ec51b336 Mon Sep 17 00:00:00 2001
From: Christian Brauner <[email protected]>
Date: Fri, 7 Feb 2025 15:10:33 +0100
Subject: [PATCH] fs: don't needlessly acquire f_lock

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

---
 fs/file.c       | 10 ++++++++++
 fs/internal.h   |  1 +
 fs/read_write.c | 13 +++++++++----
 3 files changed, 20 insertions(+), 4 deletions(-)
fs/file.c +10 -0
index d868cdb95d1e..44efdc8c1e27 100644
@@ -1182,6 +1182,16 @@ static inline bool file_needs_f_pos_lock(struct file *file)
 		(file_count(file) > 1 || file->f_op->iterate_shared);
 }
 
+bool file_seek_cur_needs_f_lock(struct file *file)
+{
+	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
+		return false;
+
+	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
+			 !mutex_is_locked(&file->f_pos_lock));
+	return true;
+}
+
 struct fd fdget_pos(unsigned int fd)
 {
 	struct fd f = fdget(fd);
fs/internal.h +1 -0
index 84607e7b05dc..1cb85a62c07f 100644
@@ -338,3 +338,4 @@ static inline bool path_mounted(const struct path *path)
 	return path->mnt->mnt_root == path->dentry;
 }
 void file_f_owner_release(struct file *file);
+bool file_seek_cur_needs_f_lock(struct file *file);
fs/read_write.c +9 -4
index a6133241dfb8..bb0ed26a0b3a 100644
@@ -169,11 +169,16 @@ generic_file_llseek_size(struct file *file, loff_t offset, int whence,
 
 	if (whence == SEEK_CUR) {
 		/*
-		 * f_lock protects against read/modify/write race with
-		 * other SEEK_CURs. Note that parallel writes and reads
-		 * behave like SEEK_SET.
+		 * If the file requires locking via f_pos_lock we know
+		 * that mutual exclusion for SEEK_CUR on the same file
+		 * is guaranteed. If the file isn't locked, we take
+		 * f_lock to protect against f_pos races with other
+		 * SEEK_CURs.
 		 */
-		guard(spinlock)(&file->f_lock);
+		if (file_seek_cur_needs_f_lock(file)) {
+			guard(spinlock)(&file->f_lock);
+			return vfs_setpos(file, file->f_pos + offset, maxsize);
+		}
 		return vfs_setpos(file, file->f_pos + offset, maxsize);
 	}
 
Signature · 1 line
2.47.2
reviewer Reviewed-by
#7 lore raw
Quoted 66 lines · Christian Brauner
Quoted 65 lines · Jan Kara
Quoted 49 lines · Christian Brauner
Quoted 48 lines · Mateusz Guzik
Quoted 46 lines · Jan Kara
Quoted 16 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

...

Quoted 7 lines
      if (whence == SEEK_CUR) {
fs/read_write.c +2 -3
+             bool locked;
+
              /*
-              * f_lock protects against read/modify/write race with
-              * other SEEK_CURs. Note that parallel writes and reads
-              * behave like SEEK_SET.

fs/read_write.c generic_file_llseek_size(struct file *file, loff_t offset, int whence,

Quoted 10 lines
fs/read_write.c +7 -2
+              * If the file requires locking via f_pos_lock we know
+              * that mutual exclusion for SEEK_CUR on the same file
+              * is guaranteed. If the file isn't locked, we take
+              * f_lock to protect against f_pos races with other
+              * SEEK_CURs.
               */
-             guard(spinlock)(&file->f_lock);
-             return vfs_setpos(file, file->f_pos + offset, maxsize);
+             locked = (file->f_mode & FMODE_ATOMIC_POS) ||
+                      file->f_op->iterate_shared;

As far as I understand the rationale this should match to file_needs_f_pos_lock() (or it can possibly be weaker) but it isn't obvious to me that's the case. After thinking about possibilities, I could convince myself that what you suggest is indeed safe but the condition being in two completely independent places and leading to subtle bugs if it gets out of sync seems a bit fragile to me.

Quoted 4 lines
Quoted 1 line

A debug-only assert that the lock is held when expected should sort it out?

Good idea. Let me reuse the newly added infra:
VFS_WARN_ON_ONCE(locked && !mutex_is_locked(&file->f_pos_lock));

Fine, but won't this actually trigger? file_needs_f_pos_lock() is:

        return (file->f_mode & FMODE_ATOMIC_POS) &&
                (file_count(file) > 1 || file->f_op->iterate_shared);

and here you have:

        locked = (file->f_mode & FMODE_ATOMIC_POS) ||
                  file->f_op->iterate_shared;
Quoted 7 lines
Quoted 2 lines

So if (file->f_mode & FMODE_ATOMIC_POS) and (file_count(file) == 1), file_needs_f_pos_lock() returns false but locked is true?

Sorry, I got lost in other mails.
Yes, you're right. I had changed the patch in my tree to fix that. I'll append it here. Sorry about that. Tell me if that looks sane ok to you now.

Looks good to me! Feel free to add:

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#8 lore raw
In reply to Christian Brauner

Hi Christian,

Quoted 6 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Quoted 8 lines

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

I'm seeing the splat below with current master. It's unlikely to be related with this patch, but with recent overlayfs changes. I'm just dropping it here before looking, as maybe it has already been reported.

Cheers,

Signature · 41 lines
Luís

[  133.133745] ------------[ cut here ]------------
[  133.133855] WARNING: CPU: 6 PID: 246 at fs/file.c:1201 file_seek_cur_needs_f_lock+0x4a/0x60
[  133.133940] Modules linked in: virtiofs fuse
[  133.134009] CPU: 6 UID: 1000 PID: 246 Comm: ld Not tainted 6.15.0+ #124 PREEMPT(full) 
[  133.134110] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[  133.134235] RIP: 0010:file_seek_cur_needs_f_lock+0x4a/0x60
[  133.134286] Code: 00 48 ba fe ff ff ff ff ff ff bf 48 83 e8 01 48 39 c2 73 06 b8 01 00 00 00 c3 48 81 c7 90 00 00 00 e8 da 0e db ff 84 c0 75 ea <0f> 0b b8 01 00 00 00 c3 31 c0 c3 66 66 2e 0f 1f 84 00 00 00 00 00
[  133.134471] RSP: 0018:ffffc90000e67ea0 EFLAGS: 00010246
[  133.134526] RAX: 0000000000000000 RBX: fffffffffffffc01 RCX: 7fffffffffffffff
[  133.134683] RDX: bffffffffffffffe RSI: fffffffffffffc01 RDI: ffff888101bd1e90
[  133.135430] RBP: ffff888101bd1e00 R08: 00000000002a3988 R09: 0000000000000000
[  133.136172] R10: ffffc90000e67ed0 R11: 0000000000000000 R12: 7fffffffffffffff
[  133.136351] R13: ffff888101bd1e00 R14: ffff888105d823c0 R15: 0000000000000001
[  133.136433] FS:  00007fd7880d2b28(0000) GS:ffff8884ad411000(0000) knlGS:0000000000000000
[  133.136516] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  133.136586] CR2: 0000559b3af3a520 CR3: 0000000103cb1000 CR4: 0000000000750eb0
[  133.136667] PKRU: 55555554
[  133.136694] Call Trace:
[  133.136720]  <TASK>
[  133.136747]  generic_file_llseek_size+0x93/0x120
[  133.136802]  ovl_llseek+0x86/0xf0
[  133.136844]  ksys_lseek+0x39/0x90
[  133.136884]  do_syscall_64+0x73/0x2c0
[  133.136932]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[  133.136994] RIP: 0033:0x7fd788098262
[  133.137034] Code: 48 63 d2 48 63 ff 4d 63 c0 b8 09 01 00 00 0f 05 48 89 c7 e8 8a 80 fd ff 48 83 c4 08 c3 48 63 ff 48 63 d2 b8 08 00 00 00 0f 05 <48> 89 c7 e9 70 80 fd ff 8d 47 27 53 89 fb 83 f8 4e 76 27 b8 ec ff
[  133.137223] RSP: 002b:00007fffffaf82c8 EFLAGS: 00000283 ORIG_RAX: 0000000000000008
[  133.137302] RAX: ffffffffffffffda RBX: 00007fd787ba1010 RCX: 00007fd788098262
[  133.137385] RDX: 0000000000000001 RSI: fffffffffffffc01 RDI: 000000000000000f
[  133.137465] RBP: 0000000000000000 R08: 0000000000000064 R09: 00007fd787c3c6a0
[  133.137545] R10: 000000000000000e R11: 0000000000000283 R12: 00007fffffafa694
[  133.137625] R13: 0000000000000039 R14: 0000000000000038 R15: 00007fffffafaa79
[  133.137708]  </TASK>
[  133.137736] irq event stamp: 1034649
[  133.137776] hardirqs last  enabled at (1034657): [<ffffffff8133c642>] __up_console_sem+0x52/0x60
[  133.137872] hardirqs last disabled at (1034664): [<ffffffff8133c627>] __up_console_sem+0x37/0x60
[  133.137966] softirqs last  enabled at (1012640): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138064] softirqs last disabled at (1012633): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138161] ---[ end trace 0000000000000000 ]---
#9 lore raw
Quoted 16 lines · Luis Henriques

Hi Christian,

Quoted 12 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

Quoted 6 lines
Quoted 2 lines

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

I'm seeing the splat below with current master. It's unlikely to be related with this patch, but with recent overlayfs changes. I'm just dropping it here before looking, as maybe it has already been reported.

OK, just to confirm that it looks like this is indeed due to this patch. I can reproduce it easily, and I'm not sure why I haven't seen it before.

Cheers,

Signature · 1 line
Luís
Quoted 44 lines

Cheers,

Signature · 41 lines
Luís

[  133.133745] ------------[ cut here ]------------
[  133.133855] WARNING: CPU: 6 PID: 246 at fs/file.c:1201 file_seek_cur_needs_f_lock+0x4a/0x60
[  133.133940] Modules linked in: virtiofs fuse
[  133.134009] CPU: 6 UID: 1000 PID: 246 Comm: ld Not tainted 6.15.0+ #124 PREEMPT(full) 
[  133.134110] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[  133.134235] RIP: 0010:file_seek_cur_needs_f_lock+0x4a/0x60
[  133.134286] Code: 00 48 ba fe ff ff ff ff ff ff bf 48 83 e8 01 48 39 c2 73 06 b8 01 00 00 00 c3 48 81 c7 90 00 00 00 e8 da 0e db ff 84 c0 75 ea <0f> 0b b8 01 00 00 00 c3 31 c0 c3 66 66 2e 0f 1f 84 00 00 00 00 00
[  133.134471] RSP: 0018:ffffc90000e67ea0 EFLAGS: 00010246
[  133.134526] RAX: 0000000000000000 RBX: fffffffffffffc01 RCX: 7fffffffffffffff
[  133.134683] RDX: bffffffffffffffe RSI: fffffffffffffc01 RDI: ffff888101bd1e90
[  133.135430] RBP: ffff888101bd1e00 R08: 00000000002a3988 R09: 0000000000000000
[  133.136172] R10: ffffc90000e67ed0 R11: 0000000000000000 R12: 7fffffffffffffff
[  133.136351] R13: ffff888101bd1e00 R14: ffff888105d823c0 R15: 0000000000000001
[  133.136433] FS:  00007fd7880d2b28(0000) GS:ffff8884ad411000(0000) knlGS:0000000000000000
[  133.136516] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  133.136586] CR2: 0000559b3af3a520 CR3: 0000000103cb1000 CR4: 0000000000750eb0
[  133.136667] PKRU: 55555554
[  133.136694] Call Trace:
[  133.136720]  <TASK>
[  133.136747]  generic_file_llseek_size+0x93/0x120
[  133.136802]  ovl_llseek+0x86/0xf0
[  133.136844]  ksys_lseek+0x39/0x90
[  133.136884]  do_syscall_64+0x73/0x2c0
[  133.136932]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[  133.136994] RIP: 0033:0x7fd788098262
[  133.137034] Code: 48 63 d2 48 63 ff 4d 63 c0 b8 09 01 00 00 0f 05 48 89 c7 e8 8a 80 fd ff 48 83 c4 08 c3 48 63 ff 48 63 d2 b8 08 00 00 00 0f 05 <48> 89 c7 e9 70 80 fd ff 8d 47 27 53 89 fb 83 f8 4e 76 27 b8 ec ff
[  133.137223] RSP: 002b:00007fffffaf82c8 EFLAGS: 00000283 ORIG_RAX: 0000000000000008
[  133.137302] RAX: ffffffffffffffda RBX: 00007fd787ba1010 RCX: 00007fd788098262
[  133.137385] RDX: 0000000000000001 RSI: fffffffffffffc01 RDI: 000000000000000f
[  133.137465] RBP: 0000000000000000 R08: 0000000000000064 R09: 00007fd787c3c6a0
[  133.137545] R10: 000000000000000e R11: 0000000000000283 R12: 00007fffffafa694
[  133.137625] R13: 0000000000000039 R14: 0000000000000038 R15: 00007fffffafaa79
[  133.137708]  </TASK>
[  133.137736] irq event stamp: 1034649
[  133.137776] hardirqs last  enabled at (1034657): [<ffffffff8133c642>] __up_console_sem+0x52/0x60
[  133.137872] hardirqs last disabled at (1034664): [<ffffffff8133c627>] __up_console_sem+0x37/0x60
[  133.137966] softirqs last  enabled at (1012640): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138064] softirqs last disabled at (1012633): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138161] ---[ end trace 0000000000000000 ]---
reviewer
#10 lore raw
Quoted 20 lines · Luis Henriques
Quoted 19 lines · Luis Henriques

Hi Christian,

Quoted 14 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

Quoted 6 lines
Quoted 3 lines

I'm seeing the splat below with current master. It's unlikely to be related with this patch, but with recent overlayfs changes. I'm just dropping it here before looking, as maybe it has already been reported.

OK, just to confirm that it looks like this is indeed due to this patch. I can reproduce it easily, and I'm not sure why I haven't seen it before.

Thanks for report! Curious. This is:

        VFS_WARN_ON_ONCE((file_count(file) > 1) &&
                         !mutex_is_locked(&file->f_pos_lock));

Based on the fact this is ld(1) I expect this is a regular file. Christian, cannot it happen that we race with dup2() so file_count is increased after we've checked it in fdget_pos() and before we get to this assert?

Honza

Quoted 39 lines
Quoted 39 lines
[  133.133745] ------------[ cut here ]------------
[  133.133855] WARNING: CPU: 6 PID: 246 at fs/file.c:1201 file_seek_cur_needs_f_lock+0x4a/0x60
[  133.133940] Modules linked in: virtiofs fuse
[  133.134009] CPU: 6 UID: 1000 PID: 246 Comm: ld Not tainted 6.15.0+ #124 PREEMPT(full) 
[  133.134110] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[  133.134235] RIP: 0010:file_seek_cur_needs_f_lock+0x4a/0x60
[  133.134286] Code: 00 48 ba fe ff ff ff ff ff ff bf 48 83 e8 01 48 39 c2 73 06 b8 01 00 00 00 c3 48 81 c7 90 00 00 00 e8 da 0e db ff 84 c0 75 ea <0f> 0b b8 01 00 00 00 c3 31 c0 c3 66 66 2e 0f 1f 84 00 00 00 00 00
[  133.134471] RSP: 0018:ffffc90000e67ea0 EFLAGS: 00010246
[  133.134526] RAX: 0000000000000000 RBX: fffffffffffffc01 RCX: 7fffffffffffffff
[  133.134683] RDX: bffffffffffffffe RSI: fffffffffffffc01 RDI: ffff888101bd1e90
[  133.135430] RBP: ffff888101bd1e00 R08: 00000000002a3988 R09: 0000000000000000
[  133.136172] R10: ffffc90000e67ed0 R11: 0000000000000000 R12: 7fffffffffffffff
[  133.136351] R13: ffff888101bd1e00 R14: ffff888105d823c0 R15: 0000000000000001
[  133.136433] FS:  00007fd7880d2b28(0000) GS:ffff8884ad411000(0000) knlGS:0000000000000000
[  133.136516] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  133.136586] CR2: 0000559b3af3a520 CR3: 0000000103cb1000 CR4: 0000000000750eb0
[  133.136667] PKRU: 55555554
[  133.136694] Call Trace:
[  133.136720]  <TASK>
[  133.136747]  generic_file_llseek_size+0x93/0x120
[  133.136802]  ovl_llseek+0x86/0xf0
[  133.136844]  ksys_lseek+0x39/0x90
[  133.136884]  do_syscall_64+0x73/0x2c0
[  133.136932]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[  133.136994] RIP: 0033:0x7fd788098262
[  133.137034] Code: 48 63 d2 48 63 ff 4d 63 c0 b8 09 01 00 00 0f 05 48 89 c7 e8 8a 80 fd ff 48 83 c4 08 c3 48 63 ff 48 63 d2 b8 08 00 00 00 0f 05 <48> 89 c7 e9 70 80 fd ff 8d 47 27 53 89 fb 83 f8 4e 76 27 b8 ec ff
[  133.137223] RSP: 002b:00007fffffaf82c8 EFLAGS: 00000283 ORIG_RAX: 0000000000000008
[  133.137302] RAX: ffffffffffffffda RBX: 00007fd787ba1010 RCX: 00007fd788098262
[  133.137385] RDX: 0000000000000001 RSI: fffffffffffffc01 RDI: 000000000000000f
[  133.137465] RBP: 0000000000000000 R08: 0000000000000064 R09: 00007fd787c3c6a0
[  133.137545] R10: 000000000000000e R11: 0000000000000283 R12: 00007fffffafa694
[  133.137625] R13: 0000000000000039 R14: 0000000000000038 R15: 00007fffffafaa79
[  133.137708]  </TASK>
[  133.137736] irq event stamp: 1034649
[  133.137776] hardirqs last  enabled at (1034657): [<ffffffff8133c642>] __up_console_sem+0x52/0x60
[  133.137872] hardirqs last disabled at (1034664): [<ffffffff8133c627>] __up_console_sem+0x37/0x60
[  133.137966] softirqs last  enabled at (1012640): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138064] softirqs last disabled at (1012633): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138161] ---[ end trace 0000000000000000 ]---
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#11 lore raw
Quoted 30 lines · Jan Kara
Quoted 20 lines · Luis Henriques
Quoted 19 lines · Luis Henriques

Hi Christian,

Quoted 14 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

Quoted 6 lines
Quoted 3 lines

I'm seeing the splat below with current master. It's unlikely to be related with this patch, but with recent overlayfs changes. I'm just dropping it here before looking, as maybe it has already been reported.

OK, just to confirm that it looks like this is indeed due to this patch. I can reproduce it easily, and I'm not sure why I haven't seen it before.

Thanks for report! Curious. This is:

Quoted 7 lines
        VFS_WARN_ON_ONCE((file_count(file) > 1) &&
                         !mutex_is_locked(&file->f_pos_lock));

Based on the fact this is ld(1) I expect this is a regular file. Christian, cannot it happen that we race with dup2() so file_count is increased after we've checked it in fdget_pos() and before we get to this assert?

Yes I somehow thought the two of us had already discussed this and either concluded to change it or drop the assert. Maybe I forgot that? I'll remove the assert.

Quoted 44 lines

Honza

Quoted 39 lines
Quoted 39 lines
[  133.133745] ------------[ cut here ]------------
[  133.133855] WARNING: CPU: 6 PID: 246 at fs/file.c:1201 file_seek_cur_needs_f_lock+0x4a/0x60
[  133.133940] Modules linked in: virtiofs fuse
[  133.134009] CPU: 6 UID: 1000 PID: 246 Comm: ld Not tainted 6.15.0+ #124 PREEMPT(full) 
[  133.134110] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[  133.134235] RIP: 0010:file_seek_cur_needs_f_lock+0x4a/0x60
[  133.134286] Code: 00 48 ba fe ff ff ff ff ff ff bf 48 83 e8 01 48 39 c2 73 06 b8 01 00 00 00 c3 48 81 c7 90 00 00 00 e8 da 0e db ff 84 c0 75 ea <0f> 0b b8 01 00 00 00 c3 31 c0 c3 66 66 2e 0f 1f 84 00 00 00 00 00
[  133.134471] RSP: 0018:ffffc90000e67ea0 EFLAGS: 00010246
[  133.134526] RAX: 0000000000000000 RBX: fffffffffffffc01 RCX: 7fffffffffffffff
[  133.134683] RDX: bffffffffffffffe RSI: fffffffffffffc01 RDI: ffff888101bd1e90
[  133.135430] RBP: ffff888101bd1e00 R08: 00000000002a3988 R09: 0000000000000000
[  133.136172] R10: ffffc90000e67ed0 R11: 0000000000000000 R12: 7fffffffffffffff
[  133.136351] R13: ffff888101bd1e00 R14: ffff888105d823c0 R15: 0000000000000001
[  133.136433] FS:  00007fd7880d2b28(0000) GS:ffff8884ad411000(0000) knlGS:0000000000000000
[  133.136516] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  133.136586] CR2: 0000559b3af3a520 CR3: 0000000103cb1000 CR4: 0000000000750eb0
[  133.136667] PKRU: 55555554
[  133.136694] Call Trace:
[  133.136720]  <TASK>
[  133.136747]  generic_file_llseek_size+0x93/0x120
[  133.136802]  ovl_llseek+0x86/0xf0
[  133.136844]  ksys_lseek+0x39/0x90
[  133.136884]  do_syscall_64+0x73/0x2c0
[  133.136932]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[  133.136994] RIP: 0033:0x7fd788098262
[  133.137034] Code: 48 63 d2 48 63 ff 4d 63 c0 b8 09 01 00 00 0f 05 48 89 c7 e8 8a 80 fd ff 48 83 c4 08 c3 48 63 ff 48 63 d2 b8 08 00 00 00 0f 05 <48> 89 c7 e9 70 80 fd ff 8d 47 27 53 89 fb 83 f8 4e 76 27 b8 ec ff
[  133.137223] RSP: 002b:00007fffffaf82c8 EFLAGS: 00000283 ORIG_RAX: 0000000000000008
[  133.137302] RAX: ffffffffffffffda RBX: 00007fd787ba1010 RCX: 00007fd788098262
[  133.137385] RDX: 0000000000000001 RSI: fffffffffffffc01 RDI: 000000000000000f
[  133.137465] RBP: 0000000000000000 R08: 0000000000000064 R09: 00007fd787c3c6a0
[  133.137545] R10: 000000000000000e R11: 0000000000000283 R12: 00007fffffafa694
[  133.137625] R13: 0000000000000039 R14: 0000000000000038 R15: 00007fffffafaa79
[  133.137708]  </TASK>
[  133.137736] irq event stamp: 1034649
[  133.137776] hardirqs last  enabled at (1034657): [<ffffffff8133c642>] __up_console_sem+0x52/0x60
[  133.137872] hardirqs last disabled at (1034664): [<ffffffff8133c627>] __up_console_sem+0x37/0x60
[  133.137966] softirqs last  enabled at (1012640): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138064] softirqs last disabled at (1012633): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138161] ---[ end trace 0000000000000000 ]---
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
reviewer
#12 lore raw
Quoted 34 lines · Christian Brauner
Quoted 33 lines · Jan Kara
Quoted 20 lines · Luis Henriques
Quoted 19 lines · Luis Henriques

Hi Christian,

Quoted 14 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

Quoted 6 lines
Quoted 3 lines

I'm seeing the splat below with current master. It's unlikely to be related with this patch, but with recent overlayfs changes. I'm just dropping it here before looking, as maybe it has already been reported.

OK, just to confirm that it looks like this is indeed due to this patch. I can reproduce it easily, and I'm not sure why I haven't seen it before.

Thanks for report! Curious. This is:

        VFS_WARN_ON_ONCE((file_count(file) > 1) &&
                         !mutex_is_locked(&file->f_pos_lock));
Quoted 8 lines
Quoted 4 lines

Based on the fact this is ld(1) I expect this is a regular file. Christian, cannot it happen that we race with dup2() so file_count is increased after we've checked it in fdget_pos() and before we get to this assert?

Yes I somehow thought the two of us had already discussed this and either concluded to change it or drop the assert. Maybe I forgot that? I'll remove the assert.

I don't remember discussing this particular assert, I think it was a different one of a similar kind :). Nevertheless I agree removing the assert here is the right thing to do, it doesn't make too much sense in this context.
Honza

Quoted 45 lines
Quoted 45 lines

Honza

Quoted 39 lines
Quoted 39 lines
[  133.133745] ------------[ cut here ]------------
[  133.133855] WARNING: CPU: 6 PID: 246 at fs/file.c:1201 file_seek_cur_needs_f_lock+0x4a/0x60
[  133.133940] Modules linked in: virtiofs fuse
[  133.134009] CPU: 6 UID: 1000 PID: 246 Comm: ld Not tainted 6.15.0+ #124 PREEMPT(full) 
[  133.134110] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[  133.134235] RIP: 0010:file_seek_cur_needs_f_lock+0x4a/0x60
[  133.134286] Code: 00 48 ba fe ff ff ff ff ff ff bf 48 83 e8 01 48 39 c2 73 06 b8 01 00 00 00 c3 48 81 c7 90 00 00 00 e8 da 0e db ff 84 c0 75 ea <0f> 0b b8 01 00 00 00 c3 31 c0 c3 66 66 2e 0f 1f 84 00 00 00 00 00
[  133.134471] RSP: 0018:ffffc90000e67ea0 EFLAGS: 00010246
[  133.134526] RAX: 0000000000000000 RBX: fffffffffffffc01 RCX: 7fffffffffffffff
[  133.134683] RDX: bffffffffffffffe RSI: fffffffffffffc01 RDI: ffff888101bd1e90
[  133.135430] RBP: ffff888101bd1e00 R08: 00000000002a3988 R09: 0000000000000000
[  133.136172] R10: ffffc90000e67ed0 R11: 0000000000000000 R12: 7fffffffffffffff
[  133.136351] R13: ffff888101bd1e00 R14: ffff888105d823c0 R15: 0000000000000001
[  133.136433] FS:  00007fd7880d2b28(0000) GS:ffff8884ad411000(0000) knlGS:0000000000000000
[  133.136516] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  133.136586] CR2: 0000559b3af3a520 CR3: 0000000103cb1000 CR4: 0000000000750eb0
[  133.136667] PKRU: 55555554
[  133.136694] Call Trace:
[  133.136720]  <TASK>
[  133.136747]  generic_file_llseek_size+0x93/0x120
[  133.136802]  ovl_llseek+0x86/0xf0
[  133.136844]  ksys_lseek+0x39/0x90
[  133.136884]  do_syscall_64+0x73/0x2c0
[  133.136932]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[  133.136994] RIP: 0033:0x7fd788098262
[  133.137034] Code: 48 63 d2 48 63 ff 4d 63 c0 b8 09 01 00 00 0f 05 48 89 c7 e8 8a 80 fd ff 48 83 c4 08 c3 48 63 ff 48 63 d2 b8 08 00 00 00 0f 05 <48> 89 c7 e9 70 80 fd ff 8d 47 27 53 89 fb 83 f8 4e 76 27 b8 ec ff
[  133.137223] RSP: 002b:00007fffffaf82c8 EFLAGS: 00000283 ORIG_RAX: 0000000000000008
[  133.137302] RAX: ffffffffffffffda RBX: 00007fd787ba1010 RCX: 00007fd788098262
[  133.137385] RDX: 0000000000000001 RSI: fffffffffffffc01 RDI: 000000000000000f
[  133.137465] RBP: 0000000000000000 R08: 0000000000000064 R09: 00007fd787c3c6a0
[  133.137545] R10: 000000000000000e R11: 0000000000000283 R12: 00007fffffafa694
[  133.137625] R13: 0000000000000039 R14: 0000000000000038 R15: 00007fffffafaa79
[  133.137708]  </TASK>
[  133.137736] irq event stamp: 1034649
[  133.137776] hardirqs last  enabled at (1034657): [<ffffffff8133c642>] __up_console_sem+0x52/0x60
[  133.137872] hardirqs last disabled at (1034664): [<ffffffff8133c627>] __up_console_sem+0x37/0x60
[  133.137966] softirqs last  enabled at (1012640): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138064] softirqs last disabled at (1012633): [<ffffffff812c4884>] irq_exit_rcu+0x74/0x110
[  133.138161] ---[ end trace 0000000000000000 ]---
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw
Quoted 40 lines · Jan Kara
Quoted 39 lines · Christian Brauner
Quoted 37 lines · Jan Kara
Quoted 20 lines · Luis Henriques
Quoted 19 lines · Luis Henriques

Hi Christian,

Quoted 14 lines · Christian Brauner

Before 2011 there was no meaningful synchronization between read/readdir/write/seek. Only in commit
ef3d0fd27e90 ("vfs: do (nearly) lockless generic_file_llseek") synchronization was added for SEEK_CUR by taking f_lock around vfs_setpos().

Then in 2014 full synchronization between read/readdir/write/seek was added in commit 9c225f2655e3 ("vfs: atomic f_pos accesses as per POSIX") by introducing f_pos_lock for regular files with FMODE_ATOMIC_POS and for directories. At that point taking f_lock became unnecessary for such files.

So only acquire f_lock for SEEK_CUR if this isn't a file that would have acquired f_pos_lock if necessary.

Quoted 6 lines
Quoted 3 lines

I'm seeing the splat below with current master. It's unlikely to be related with this patch, but with recent overlayfs changes. I'm just dropping it here before looking, as maybe it has already been reported.

OK, just to confirm that it looks like this is indeed due to this patch. I can reproduce it easily, and I'm not sure why I haven't seen it before.

Thanks for report! Curious. This is:

        VFS_WARN_ON_ONCE((file_count(file) > 1) &&
                         !mutex_is_locked(&file->f_pos_lock));

Based on the fact this is ld(1) I expect this is a regular file. Christian, cannot it happen that we race with dup2() so file_count is increased after we've checked it in fdget_pos() and before we get to this assert?

Quoted 8 lines
Quoted 3 lines

Yes I somehow thought the two of us had already discussed this and either concluded to change it or drop the assert. Maybe I forgot that? I'll remove the assert.

I don't remember discussing this particular assert, I think it was a different one of a similar kind :). Nevertheless I agree removing the assert here is the right thing to do, it doesn't make too much sense in this context.

Awesome, thanks for looking into this Jan and Christian.

Cheers,

Signature · 1 line
Luís
#14 lore raw

PATCH 1/1 fs: drop assert in file_seek_cur_needs_f_lock

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi Christian,

It wasn't clear whether you'd be queueing this fix yourself. Since I don't see it on vfs.git, I decided to explicitly send the patch so that it doesn't slip through the cracks.

Cheers,

Signature · 4 lines
Luis

 fs/file.c | 2 --
 1 file changed, 2 deletions(-)
fs/file.c +0 -2
index 3a3146664cf3..075f07bdc977 100644
@@ -1198,8 +1198,6 @@ bool file_seek_cur_needs_f_lock(struct file *file)
 	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
 		return false;
 
-	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-			 !mutex_is_locked(&file->f_pos_lock));
 	return true;
 }
 
reviewer Reviewed-by on v1 1/1
#15 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

Quoted 6 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

Looks good. Feel free to add:

Honza

Quoted 27 lines

---
Hi Christian,

It wasn't clear whether you'd be queueing this fix yourself. Since I don't see it on vfs.git, I decided to explicitly send the patch so that it doesn't slip through the cracks.

Cheers,

Signature · 4 lines
Luis

 fs/file.c | 2 --
 1 file changed, 2 deletions(-)
fs/file.c +0 -2
index 3a3146664cf3..075f07bdc977 100644
@@ -1198,8 +1198,6 @@ bool file_seek_cur_needs_f_lock(struct file *file)
 	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
 		return false;
 
-	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-			 !mutex_is_locked(&file->f_pos_lock));
 	return true;
 }
 
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer Applied
#16 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

In reply to Luis Henriques
Quoted 5 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] fs: drop assert in file_seek_cur_needs_f_lock

      https://git.kernel.org/vfs/vfs/c/ec86bba684b1
#17 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

In reply to Luis Henriques
Quoted 25 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi Christian,

It wasn't clear whether you'd be queueing this fix yourself. Since I don't see it on vfs.git, I decided to explicitly send the patch so that it doesn't slip through the cracks.

Cheers,

Signature · 4 lines
Luis

 fs/file.c | 2 --
 1 file changed, 2 deletions(-)
fs/file.c +0 -0
index 3a3146664cf3..075f07bdc977 100644

fs/file.c bool file_seek_cur_needs_f_lock(struct file *file) · line 1203

Quoted 8 lines
fs/file.c +0 -2
@@ -1198,8 +1198,6 @@ bool file_seek_cur_needs_f_lock(struct file *file)
 	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
 		return false;
 
-	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-			 !mutex_is_locked(&file->f_pos_lock));
 	return true;
 }
 

There this justifies the change.

fdget_pos() can only legally skip locking if it determines to be in position where nobody else can operate on the same file obj, meaning file_count(file) == 1 and it can't go up. Otherwise the lock is taken.

Or to put it differently, fdget_pos() NOT taking the lock and new refs showing up later is a bug.

I don't believe anything of the sort is happening here.

Instead, overlayfs is playing games and *NOT* going through fdget_pos():

	ovl_inode_lock(inode);
        realfile = ovl_real_file(file);
	[..]
        ret = vfs_llseek(realfile, offset, whence);

Given the custom inode locking around the call, it may be any other locking is unnecessary and the code happens to be correct despite the splat.

I think the safest way out with some future-proofing is to in fact *add* the locking in ovl_llseek() to shut up the assert -- personally I find it uneasy there is some underlying file obj flying around.

Even if ultimately the assert has to go, the proposed commit message does not justify it.

#18 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

Quoted 30 lines · Mateusz Guzik
Quoted 28 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi Christian,

It wasn't clear whether you'd be queueing this fix yourself. Since I don't see it on vfs.git, I decided to explicitly send the patch so that it doesn't slip through the cracks.

Cheers,
--
Luis

 fs/file.c | 2 --
 1 file changed, 2 deletions(-)
fs/file.c +0 -0
index 3a3146664cf3..075f07bdc977 100644
@@ -1198,8 +1198,6 @@ bool file_seek_cur_needs_f_lock(struct file *file)
      if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
              return false;
 
Quoted 8 lines

fs/file.c bool file_seek_cur_needs_f_lock(struct file *file) · line 1203

Quoted 5 lines
fs/file.c +0 -2
-     VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-                      !mutex_is_locked(&file->f_pos_lock));
      return true;
 }
 

There this justifies the change.

Huh. scratch this sentence. I stand by the rest. :)

Quoted 26 lines

fdget_pos() can only legally skip locking if it determines to be in position where nobody else can operate on the same file obj, meaning file_count(file) == 1 and it can't go up. Otherwise the lock is taken.

Or to put it differently, fdget_pos() NOT taking the lock and new refs showing up later is a bug.

I don't believe anything of the sort is happening here.

Instead, overlayfs is playing games and *NOT* going through fdget_pos():

        ovl_inode_lock(inode);
        realfile = ovl_real_file(file);
        [..]
        ret = vfs_llseek(realfile, offset, whence);

Given the custom inode locking around the call, it may be any other locking is unnecessary and the code happens to be correct despite the splat.

I think the safest way out with some future-proofing is to in fact *add* the locking in ovl_llseek() to shut up the assert -- personally I find it uneasy there is some underlying file obj flying around.

Even if ultimately the assert has to go, the proposed commit message does not justify it.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
reviewer
#19 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

In reply to Mateusz Guzik
Quoted 38 lines · Mateusz Guzik
Quoted 25 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi Christian,

It wasn't clear whether you'd be queueing this fix yourself. Since I don't see it on vfs.git, I decided to explicitly send the patch so that it doesn't slip through the cracks.

Cheers,

Signature · 4 lines
Luis

 fs/file.c | 2 --
 1 file changed, 2 deletions(-)
fs/file.c +0 -0
index 3a3146664cf3..075f07bdc977 100644

fs/file.c bool file_seek_cur_needs_f_lock(struct file *file) · line 1202

Quoted 7 lines
fs/file.c +0 -2
@@ -1198,8 +1198,6 @@ bool file_seek_cur_needs_f_lock(struct file *file)
 	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
 		return false;
 
-	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-			 !mutex_is_locked(&file->f_pos_lock));
 	return true;
 }

fdget_pos() can only legally skip locking if it determines to be in position where nobody else can operate on the same file obj, meaning file_count(file) == 1 and it can't go up. Otherwise the lock is taken.

Quoted 2 lines

Or to put it differently, fdget_pos() NOT taking the lock and new refs showing up later is a bug.

I mostly agree and as I've checked again, this indeed seems to be the case as fdget() will increment f_ref if file table is shared with another thread and thus file_needs_f_pos_lock() returns true whenever there are more threads sharing the file table or if the struct file is dupped to another fd. That being said I find the assertion in file_seek_cur_needs_f_lock() misplaced - it just doesn't make sense in that place to me.

Quoted 4 lines

I don't believe anything of the sort is happening here.

Instead, overlayfs is playing games and *NOT* going through fdget_pos():

Quoted 8 lines
	ovl_inode_lock(inode);
        realfile = ovl_real_file(file);
	[..]
        ret = vfs_llseek(realfile, offset, whence);

Given the custom inode locking around the call, it may be any other locking is unnecessary and the code happens to be correct despite the splat.

Right and good spotting. That's indeed more likely explanation than mine. Actually custom locking around llseek isn't all that uncommon (mostly for historical reasons AFAIK but that's another story).

Quoted 3 lines

I think the safest way out with some future-proofing is to in fact *add* the locking in ovl_llseek() to shut up the assert -- personally I find it uneasy there is some underlying file obj flying around.

Well, if you grep for vfs_llseek(), you'll see there are much more calls to it in the kernel than overlayfs. These callers outside of fs/read_write.c are responsible for their locking. So I don't think keeping the assert in file_seek_cur_needs_f_lock() makes any sense. If anything I'd be open to putting it in fdput_pos() or something like that.

Quoted 2 lines

Even if ultimately the assert has to go, the proposed commit message does not justify it.

I guess the commit message could be improved. Something like:

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#20 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

Quoted 70 lines · Jan Kara
Quoted 38 lines · Mateusz Guzik
Quoted 25 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because, as Jan Kara suggested, the file reference may get incremented after checking it with fdget_pos().

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi Christian,

It wasn't clear whether you'd be queueing this fix yourself. Since I don't see it on vfs.git, I decided to explicitly send the patch so that it doesn't slip through the cracks.

Cheers,

Signature · 4 lines
Luis

 fs/file.c | 2 --
 1 file changed, 2 deletions(-)
fs/file.c +0 -0
index 3a3146664cf3..075f07bdc977 100644

fs/file.c bool file_seek_cur_needs_f_lock(struct file *file) · line 1202

Quoted 7 lines
fs/file.c +0 -2
@@ -1198,8 +1198,6 @@ bool file_seek_cur_needs_f_lock(struct file *file)
 	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
 		return false;
 
-	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-			 !mutex_is_locked(&file->f_pos_lock));
 	return true;
 }

fdget_pos() can only legally skip locking if it determines to be in position where nobody else can operate on the same file obj, meaning file_count(file) == 1 and it can't go up. Otherwise the lock is taken.

Quoted 2 lines

Or to put it differently, fdget_pos() NOT taking the lock and new refs showing up later is a bug.

I mostly agree and as I've checked again, this indeed seems to be the case as fdget() will increment f_ref if file table is shared with another thread and thus file_needs_f_pos_lock() returns true whenever there are more threads sharing the file table or if the struct file is dupped to another fd. That being said I find the assertion in file_seek_cur_needs_f_lock() misplaced - it just doesn't make sense in that place to me.

Quoted 4 lines

I don't believe anything of the sort is happening here.

Instead, overlayfs is playing games and *NOT* going through fdget_pos():

Quoted 8 lines
	ovl_inode_lock(inode);
        realfile = ovl_real_file(file);
	[..]
        ret = vfs_llseek(realfile, offset, whence);

Given the custom inode locking around the call, it may be any other locking is unnecessary and the code happens to be correct despite the splat.

Right and good spotting. That's indeed more likely explanation than mine. Actually custom locking around llseek isn't all that uncommon (mostly for historical reasons AFAIK but that's another story).

Quoted 3 lines

I think the safest way out with some future-proofing is to in fact *add* the locking in ovl_llseek() to shut up the assert -- personally I find it uneasy there is some underlying file obj flying around.

Quoted 5 lines

Well, if you grep for vfs_llseek(), you'll see there are much more calls to it in the kernel than overlayfs. These callers outside of fs/read_write.c are responsible for their locking. So I don't think keeping the assert in file_seek_cur_needs_f_lock() makes any sense. If anything I'd be open to putting it in fdput_pos() or something like that.

Thank you Mateusz and Honza for looking into this. Overlayfs was indeed my initial suspect, but I had two reasons for thinking that the assert was the problem: 1) that code was there for quite some time and 2) nobody else was reporting this issue.

Quoted 9 lines
Quoted 2 lines

Even if ultimately the assert has to go, the proposed commit message does not justify it.

I guess the commit message could be improved. Something like:

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Thanks, makes more sense.

Christian, do you prefer me to resend the patch or is it easier for you to just amend the commit? (Though, to be fair, the authorship could also be changed as I mostly reported the issue and tested!)

Cheers,

Signature · 1 line
Luís
#21 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

Quoted 7 lines · Luis Henriques
Quoted 6 lines

I guess the commit message could be improved. Something like:

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Quoted 6 lines

Thanks, makes more sense.

Christian, do you prefer me to resend the patch or is it easier for you to just amend the commit? (Though, to be fair, the authorship could also be changed as I mostly reported the issue and tested!)

How about leaving a trace in the code.

For example a comment of this sort in place of the assert:
Note that we are not guaranteed to be called after fdget_pos() on this file obj, in which case the caller is expected to provide the appropriate locking.

I find it fishy af that a rando fs is playing games with the file obj *and* the fact that games are played is not assertable, but at least people can be warned.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
reviewer
#22 lore raw

Re: [PATCH] fs: drop assert in file_seek_cur_needs_f_lock

Quoted 15 lines · Mateusz Guzik
Quoted 7 lines · Luis Henriques
Quoted 6 lines

I guess the commit message could be improved. Something like:

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Quoted 6 lines

Thanks, makes more sense.

Christian, do you prefer me to resend the patch or is it easier for you to just amend the commit? (Though, to be fair, the authorship could also be changed as I mostly reported the issue and tested!)

Quoted 6 lines

How about leaving a trace in the code.

For example a comment of this sort in place of the assert:
Note that we are not guaranteed to be called after fdget_pos() on this file obj, in which case the caller is expected to provide the appropriate locking.

I'm not opposed to that.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#23 lore raw

PATCH v2 1/1 fs: drop assert in file_seek_cur_needs_f_lock

In reply to Mateusz Guzik

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi!

As suggested by Mateusz, I'm adding a comment (also suggested by him!) to replace the assertion. I'm also adding the 'Suggested-by:' tags, although I'm not sure it's the correct tag to use -- the authorship of this patch isn't really clear at this point :-)

 fs/file.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/file.c +6 -2
index 3a3146664cf3..b6db031545e6 100644
@@ -1198,8 +1198,12 @@ bool file_seek_cur_needs_f_lock(struct file *file)
 	if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
 		return false;
 
-	VFS_WARN_ON_ONCE((file_count(file) > 1) &&
-			 !mutex_is_locked(&file->f_pos_lock));
+	/*
+	 * Note that we are not guaranteed to be called after fdget_pos() on
+	 * this file obj, in which case the caller is expected to provide the
+	 * appropriate locking.
+	 */
+
 	return true;
 }
 
#24 lore raw

Re: [PATCH v2] fs: drop assert in file_seek_cur_needs_f_lock

Quoted 30 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Fixes da06e3c51794 ("fs: don't needlessly acquire f_lock")

---
Hi!

As suggested by Mateusz, I'm adding a comment (also suggested by him!) to replace the assertion. I'm also adding the 'Suggested-by:' tags, although I'm not sure it's the correct tag to use -- the authorship of this patch isn't really clear at this point :-)

 fs/file.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/file.c +0 -1
index 3a3146664cf3..b6db031545e6 100644
@@ -1198,8 +1198,12 @@ bool file_seek_cur_needs_f_lock(struct file *file)
        if (!(file->f_mode & FMODE_ATOMIC_POS) && !file->f_op->iterate_shared)
                return false;
 
-       VFS_WARN_ON_ONCE((file_count(file) > 1) &&

fs/file.c bool file_seek_cur_needs_f_lock(struct file *file) · line 1209

Quoted 10 lines
fs/file.c +6 -1
-                        !mutex_is_locked(&file->f_pos_lock));
+       /*
+        * Note that we are not guaranteed to be called after fdget_pos() on
+        * this file obj, in which case the caller is expected to provide the
+        * appropriate locking.
+        */
+
        return true;
 }
 

well i think this is fine, obviously ;-)

I was hoping a native speaker would do some touch ups on the comment though.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
author maintainer Applied
#25 lore raw

Re: [PATCH v2] fs: drop assert in file_seek_cur_needs_f_lock

In reply to Luis Henriques
Quoted 6 lines · Luis Henriques

The assert in function file_seek_cur_needs_f_lock() can be triggered very easily because there are many users of vfs_llseek() (such as overlayfs) that do their custom locking around llseek instead of relying on fdget_pos(). Just drop the overzealous assertion.

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] fs: drop assert in file_seek_cur_needs_f_lock

      https://git.kernel.org/vfs/vfs/c/dd2d6b7f6f51