[PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}

Mateusz Guzik posted 1 patch 9 months ago
fs/inode.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
[PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Mateusz Guzik 9 months ago
While this may sound like a pedantic clean up, it does in fact impact
code generation -- the patched add routine is slightly smaller.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
---

Below is disasm before/after. I did not want to pull this into the
commit message because of the total length vs long term usefulness ratio.

can be moved up into the commit message no problem if someone insists on
it:

(gdb) disassemble inode_sb_list_add
before:
 <+0>:     endbr64
 <+4>:     call   0xffffffff8130e9b0 <__fentry__>
 <+9>:     push   %rbx
 <+10>:    mov    0x28(%rdi),%rax
 <+14>:    mov    %rdi,%rbx
 <+17>:    lea    0x540(%rax),%rdi
 <+24>:    call   0xffffffff8225cf20 <_raw_spin_lock>
 <+29>:    mov    0x28(%rbx),%rax
 <+33>:    lea    0x110(%rbx),%rdx
 <+40>:    mov    0x548(%rax),%rcx
 <+47>:    mov    %rdx,0x8(%rcx)
 <+51>:    mov    %rcx,0x110(%rbx)
 <+58>:    lea    0x548(%rax),%rcx
 <+65>:    mov    %rcx,0x118(%rbx)
 <+72>:    mov    %rdx,0x548(%rax)
 <+79>:    mov    0x28(%rbx),%rdi
 <+83>:    pop    %rbx
 <+84>:    add    $0x540,%rdi
 <+91>:    jmp    0xffffffff8225d020 <_raw_spin_unlock>

after:
 <+0>:     endbr64
 <+4>:     call   0xffffffff8130e9b0 <__fentry__>
 <+9>:     push   %r12
 <+11>:    push   %rbp
 <+12>:    push   %rbx
 <+13>:    mov    0x28(%rdi),%rbp
 <+17>:    mov    %rdi,%rbx
 <+20>:    lea    0x540(%rbp),%r12
 <+27>:    mov    %r12,%rdi
 <+30>:    call   0xffffffff8225cf20 <_raw_spin_lock>
 <+35>:    mov    0x548(%rbp),%rdx
 <+42>:    lea    0x110(%rbx),%rax
 <+49>:    mov    %r12,%rdi
 <+52>:    mov    %rax,0x8(%rdx)
 <+56>:    mov    %rdx,0x110(%rbx)
 <+63>:    lea    0x548(%rbp),%rdx
 <+70>:    mov    %rdx,0x118(%rbx)
 <+77>:    mov    %rax,0x548(%rbp)
 <+84>:    pop    %rbx
 <+85>:    pop    %rbp
 <+86>:    pop    %r12
 <+88>:    jmp    0xffffffff8225d020 <_raw_spin_unlock>

 fs/inode.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/fs/inode.c b/fs/inode.c
index 10121fc7b87e..e188bb1eb07a 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -623,18 +623,22 @@ static void inode_wait_for_lru_isolating(struct inode *inode)
  */
 void inode_sb_list_add(struct inode *inode)
 {
-	spin_lock(&inode->i_sb->s_inode_list_lock);
-	list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
-	spin_unlock(&inode->i_sb->s_inode_list_lock);
+	struct super_block *sb = inode->i_sb;
+
+	spin_lock(&sb->s_inode_list_lock);
+	list_add(&inode->i_sb_list, &sb->s_inodes);
+	spin_unlock(&sb->s_inode_list_lock);
 }
 EXPORT_SYMBOL_GPL(inode_sb_list_add);
 
 static inline void inode_sb_list_del(struct inode *inode)
 {
+	struct super_block *sb = inode->i_sb;
+
 	if (!list_empty(&inode->i_sb_list)) {
-		spin_lock(&inode->i_sb->s_inode_list_lock);
+		spin_lock(&sb->s_inode_list_lock);
 		list_del_init(&inode->i_sb_list);
-		spin_unlock(&inode->i_sb->s_inode_list_lock);
+		spin_unlock(&sb->s_inode_list_lock);
 	}
 }
 
-- 
2.43.0
Re: [PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Jan Kara 9 months ago
On Wed 19-03-25 01:46:35, Mateusz Guzik wrote:
> While this may sound like a pedantic clean up, it does in fact impact
> code generation -- the patched add routine is slightly smaller.
> 
> Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>

I'm surprised it matters for the compiler but as Christian wrote, why not.
Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
> 
> Below is disasm before/after. I did not want to pull this into the
> commit message because of the total length vs long term usefulness ratio.
> 
> can be moved up into the commit message no problem if someone insists on
> it:
> 
> (gdb) disassemble inode_sb_list_add
> before:
>  <+0>:     endbr64
>  <+4>:     call   0xffffffff8130e9b0 <__fentry__>
>  <+9>:     push   %rbx
>  <+10>:    mov    0x28(%rdi),%rax
>  <+14>:    mov    %rdi,%rbx
>  <+17>:    lea    0x540(%rax),%rdi
>  <+24>:    call   0xffffffff8225cf20 <_raw_spin_lock>
>  <+29>:    mov    0x28(%rbx),%rax
>  <+33>:    lea    0x110(%rbx),%rdx
>  <+40>:    mov    0x548(%rax),%rcx
>  <+47>:    mov    %rdx,0x8(%rcx)
>  <+51>:    mov    %rcx,0x110(%rbx)
>  <+58>:    lea    0x548(%rax),%rcx
>  <+65>:    mov    %rcx,0x118(%rbx)
>  <+72>:    mov    %rdx,0x548(%rax)
>  <+79>:    mov    0x28(%rbx),%rdi
>  <+83>:    pop    %rbx
>  <+84>:    add    $0x540,%rdi
>  <+91>:    jmp    0xffffffff8225d020 <_raw_spin_unlock>
> 
> after:
>  <+0>:     endbr64
>  <+4>:     call   0xffffffff8130e9b0 <__fentry__>
>  <+9>:     push   %r12
>  <+11>:    push   %rbp
>  <+12>:    push   %rbx
>  <+13>:    mov    0x28(%rdi),%rbp
>  <+17>:    mov    %rdi,%rbx
>  <+20>:    lea    0x540(%rbp),%r12
>  <+27>:    mov    %r12,%rdi
>  <+30>:    call   0xffffffff8225cf20 <_raw_spin_lock>
>  <+35>:    mov    0x548(%rbp),%rdx
>  <+42>:    lea    0x110(%rbx),%rax
>  <+49>:    mov    %r12,%rdi
>  <+52>:    mov    %rax,0x8(%rdx)
>  <+56>:    mov    %rdx,0x110(%rbx)
>  <+63>:    lea    0x548(%rbp),%rdx
>  <+70>:    mov    %rdx,0x118(%rbx)
>  <+77>:    mov    %rax,0x548(%rbp)
>  <+84>:    pop    %rbx
>  <+85>:    pop    %rbp
>  <+86>:    pop    %r12
>  <+88>:    jmp    0xffffffff8225d020 <_raw_spin_unlock>
> 
>  fs/inode.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/inode.c b/fs/inode.c
> index 10121fc7b87e..e188bb1eb07a 100644
> --- a/fs/inode.c
> +++ b/fs/inode.c
> @@ -623,18 +623,22 @@ static void inode_wait_for_lru_isolating(struct inode *inode)
>   */
>  void inode_sb_list_add(struct inode *inode)
>  {
> -	spin_lock(&inode->i_sb->s_inode_list_lock);
> -	list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
> -	spin_unlock(&inode->i_sb->s_inode_list_lock);
> +	struct super_block *sb = inode->i_sb;
> +
> +	spin_lock(&sb->s_inode_list_lock);
> +	list_add(&inode->i_sb_list, &sb->s_inodes);
> +	spin_unlock(&sb->s_inode_list_lock);
>  }
>  EXPORT_SYMBOL_GPL(inode_sb_list_add);
>  
>  static inline void inode_sb_list_del(struct inode *inode)
>  {
> +	struct super_block *sb = inode->i_sb;
> +
>  	if (!list_empty(&inode->i_sb_list)) {
> -		spin_lock(&inode->i_sb->s_inode_list_lock);
> +		spin_lock(&sb->s_inode_list_lock);
>  		list_del_init(&inode->i_sb_list);
> -		spin_unlock(&inode->i_sb->s_inode_list_lock);
> +		spin_unlock(&sb->s_inode_list_lock);
>  	}
>  }
>  
> -- 
> 2.43.0
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Mateusz Guzik 9 months ago
On Wed, Mar 19, 2025 at 5:11 PM Jan Kara <jack@suse.cz> wrote:
>
> On Wed 19-03-25 01:46:35, Mateusz Guzik wrote:
> > While this may sound like a pedantic clean up, it does in fact impact
> > code generation -- the patched add routine is slightly smaller.
> >
> > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
>
> I'm surprised it matters for the compiler but as Christian wrote, why not.
> Feel free to add:
>

In stock code the fence in spin_lock forces the compiler to load
->i_sb again -- as far as it knows it could have changed.

On the other this patch forces the compiler to remember the value for
the same reason, which turns out to produce less code.

-- 
Mateusz Guzik <mjguzik gmail.com>
Re: [PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Jan Kara 9 months ago
On Wed 19-03-25 17:16:06, Mateusz Guzik wrote:
> On Wed, Mar 19, 2025 at 5:11 PM Jan Kara <jack@suse.cz> wrote:
> >
> > On Wed 19-03-25 01:46:35, Mateusz Guzik wrote:
> > > While this may sound like a pedantic clean up, it does in fact impact
> > > code generation -- the patched add routine is slightly smaller.
> > >
> > > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
> >
> > I'm surprised it matters for the compiler but as Christian wrote, why not.
> > Feel free to add:
> >
> 
> In stock code the fence in spin_lock forces the compiler to load
> ->i_sb again -- as far as it knows it could have changed.
> 
> On the other this patch forces the compiler to remember the value for
> the same reason, which turns out to produce less code.

I see. Thanks for explanation!

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Matthew Wilcox 9 months ago
On Wed, Mar 19, 2025 at 05:11:25PM +0100, Jan Kara wrote:
> On Wed 19-03-25 01:46:35, Mateusz Guzik wrote:
> > While this may sound like a pedantic clean up, it does in fact impact
> > code generation -- the patched add routine is slightly smaller.
> > 
> > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
> 
> I'm surprised it matters for the compiler but as Christian wrote, why not.
> Feel free to add:

I suspect it's because there's a spin_lock() involved.  The barriers
we have probably mean we've told the compiler that it can't cache the
value loaded before the spin_lock().
Re: [PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Christian Brauner 9 months ago
On Wed, Mar 19, 2025 at 01:46:35AM +0100, Mateusz Guzik wrote:
> While this may sound like a pedantic clean up, it does in fact impact

It also makes it easier to read. So +1.
Re: [PATCH] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
Posted by Christian Brauner 9 months ago
On Wed, 19 Mar 2025 01:46:35 +0100, Mateusz Guzik wrote:
> While this may sound like a pedantic clean up, it does in fact impact
> code generation -- the patched add routine is slightly smaller.
> 
> 

Applied to the vfs-6.15.misc branch of the vfs/vfs.git tree.
Patches in the vfs-6.15.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.15.misc

[1/1] fs: load the ->i_sb pointer once in inode_sb_list_{add,del}
      https://git.kernel.org/vfs/vfs/c/5a607aa94398