linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: don't needlessly acquire f_lock
@ 2025-02-07 14:10 Christian Brauner
  2025-02-07 14:18 ` Matthew Wilcox
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Christian Brauner @ 2025-02-07 14:10 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Christian Brauner, Jeff Layton, Jan Kara, Alexander Viro,
	Amir Goldstein, Mateusz Guzik

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.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/read_write.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index a6133241dfb8..816189f9c56d 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -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);
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2025-06-16  7:59 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-07 14:10 [PATCH] fs: don't needlessly acquire f_lock Christian Brauner
2025-02-07 14:18 ` Matthew Wilcox
2025-02-07 15:50 ` Jan Kara
2025-02-07 16:42   ` Mateusz Guzik
2025-02-10 12:01     ` Christian Brauner
2025-02-10 14:58       ` Jan Kara
2025-02-13 15:10         ` Christian Brauner
2025-02-13 16:17           ` Jan Kara
2025-06-02  9:47 ` Luis Henriques
2025-06-02 15:52   ` Luis Henriques
2025-06-03  9:34     ` Jan Kara
2025-06-04  8:33       ` Christian Brauner
2025-06-04  9:53         ` Jan Kara
2025-06-05  7:35           ` Luis Henriques
2025-06-12  9:41             ` [PATCH] fs: drop assert in file_seek_cur_needs_f_lock Luis Henriques
2025-06-12 11:08               ` Jan Kara
2025-06-12 12:33               ` Christian Brauner
2025-06-12 13:55               ` Mateusz Guzik
2025-06-12 13:59                 ` Mateusz Guzik
2025-06-12 16:23                 ` Jan Kara
2025-06-12 18:07                   ` Luis Henriques
2025-06-12 21:35                     ` Mateusz Guzik
2025-06-13 10:05                       ` Jan Kara
2025-06-13 10:11                       ` [PATCH v2] " Luis Henriques
2025-06-13 10:35                         ` Mateusz Guzik
2025-06-16  7:59                         ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).