* [PATCH v2] fs: move fd_install() slowpath into a dedicated routine and provide commentary
@ 2025-11-10 9:56 Mateusz Guzik
2025-11-12 9:30 ` Christian Brauner
0 siblings, 1 reply; 3+ messages in thread
From: Mateusz Guzik @ 2025-11-10 9:56 UTC (permalink / raw)
To: viro; +Cc: brauner, jack, linux-kernel, linux-fsdevel, Mateusz Guzik
On stock kernel gcc 14 emits avoidable register spillage:
endbr64
call ffffffff81374630 <__fentry__>
push %r13
push %r12
push %rbx
sub $0x8,%rsp
[snip]
Total fast path is 99 bytes.
Moving the slowpath out avoids it and shortens the fast path to 74
bytes.
Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
---
v2:
- keep rcu_read_unlock_sched in fd_install
does not alter the fast path
fs/file.c | 35 +++++++++++++++++++++++++++++------
1 file changed, 29 insertions(+), 6 deletions(-)
diff --git a/fs/file.c b/fs/file.c
index 28743b742e3c..3f56890068aa 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -641,6 +641,34 @@ void put_unused_fd(unsigned int fd)
EXPORT_SYMBOL(put_unused_fd);
+/*
+ * Install a file pointer in the fd array while it is being resized.
+ *
+ * We need to make sure our update to the array does not get lost as the resizing
+ * thread can be copying the content as we modify it.
+ *
+ * We have two ways to do it:
+ * - go off CPU waiting for resize_in_progress to clear
+ * - take the spin lock
+ *
+ * The latter is trivial to implement and saves us from having to might_sleep()
+ * for debugging purposes.
+ *
+ * This is moved out of line from fd_install() to convince gcc to optimize that
+ * routine better.
+ */
+static void noinline fd_install_slowpath(unsigned int fd, struct file *file)
+{
+ struct files_struct *files = current->files;
+ struct fdtable *fdt;
+
+ spin_lock(&files->file_lock);
+ fdt = files_fdtable(files);
+ VFS_BUG_ON(rcu_access_pointer(fdt->fd[fd]) != NULL);
+ rcu_assign_pointer(fdt->fd[fd], file);
+ spin_unlock(&files->file_lock);
+}
+
/**
* fd_install - install a file pointer in the fd array
* @fd: file descriptor to install the file in
@@ -658,14 +686,9 @@ void fd_install(unsigned int fd, struct file *file)
return;
rcu_read_lock_sched();
-
if (unlikely(files->resize_in_progress)) {
rcu_read_unlock_sched();
- spin_lock(&files->file_lock);
- fdt = files_fdtable(files);
- VFS_BUG_ON(rcu_access_pointer(fdt->fd[fd]) != NULL);
- rcu_assign_pointer(fdt->fd[fd], file);
- spin_unlock(&files->file_lock);
+ fd_install_slowpath(fd, file);
return;
}
/* coupled with smp_wmb() in expand_fdtable() */
--
2.48.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] fs: move fd_install() slowpath into a dedicated routine and provide commentary
2025-11-10 9:56 [PATCH v2] fs: move fd_install() slowpath into a dedicated routine and provide commentary Mateusz Guzik
@ 2025-11-12 9:30 ` Christian Brauner
2025-11-12 10:31 ` Mateusz Guzik
0 siblings, 1 reply; 3+ messages in thread
From: Christian Brauner @ 2025-11-12 9:30 UTC (permalink / raw)
To: Mateusz Guzik; +Cc: Christian Brauner, jack, linux-kernel, linux-fsdevel, viro
On Mon, 10 Nov 2025 10:56:34 +0100, Mateusz Guzik wrote:
> On stock kernel gcc 14 emits avoidable register spillage:
> endbr64
> call ffffffff81374630 <__fentry__>
> push %r13
> push %r12
> push %rbx
> sub $0x8,%rsp
> [snip]
>
> [...]
Applied to the vfs-6.19.misc branch of the vfs/vfs.git tree.
Patches in the vfs-6.19.misc 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-6.19.misc
[1/1] fs: move fd_install() slowpath into a dedicated routine and provide commentary
https://git.kernel.org/vfs/vfs/c/6ecf5292038e
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] fs: move fd_install() slowpath into a dedicated routine and provide commentary
2025-11-12 9:30 ` Christian Brauner
@ 2025-11-12 10:31 ` Mateusz Guzik
0 siblings, 0 replies; 3+ messages in thread
From: Mateusz Guzik @ 2025-11-12 10:31 UTC (permalink / raw)
To: Christian Brauner; +Cc: jack, linux-kernel, linux-fsdevel, viro
this may get the dreaded missing arg description working, how about this on top:
diff --git a/fs/file.c b/fs/file.c
index 3f56890068aa..022e98525c65 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -641,8 +641,10 @@ void put_unused_fd(unsigned int fd)
EXPORT_SYMBOL(put_unused_fd);
-/*
- * Install a file pointer in the fd array while it is being resized.
+/**
+ * fd_install_slowpath - install a file pointer in the fd array while
it is being resized
+ * @fd: file descriptor to install the file in
+ * @file: the file to install
*
* We need to make sure our update to the array does not get lost as
the resizing
* thread can be copying the content as we modify it.
On Wed, Nov 12, 2025 at 10:30 AM Christian Brauner <brauner@kernel.org> wrote:
>
> On Mon, 10 Nov 2025 10:56:34 +0100, Mateusz Guzik wrote:
> > On stock kernel gcc 14 emits avoidable register spillage:
> > endbr64
> > call ffffffff81374630 <__fentry__>
> > push %r13
> > push %r12
> > push %rbx
> > sub $0x8,%rsp
> > [snip]
> >
> > [...]
>
> Applied to the vfs-6.19.misc branch of the vfs/vfs.git tree.
> Patches in the vfs-6.19.misc 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-6.19.misc
>
> [1/1] fs: move fd_install() slowpath into a dedicated routine and provide commentary
> https://git.kernel.org/vfs/vfs/c/6ecf5292038e
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-12 10:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-10 9:56 [PATCH v2] fs: move fd_install() slowpath into a dedicated routine and provide commentary Mateusz Guzik
2025-11-12 9:30 ` Christian Brauner
2025-11-12 10:31 ` Mateusz Guzik
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).