Skip to contentSkip to conversation

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

PATCH Applied Mateusz Guzik 4 participants 7 messages Mar 19, 2025 VFS

lore
Share & tools
lore
#0 lore raw

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

While this may sound like a pedantic clean up, it does in fact impact code generation -- the patched add routine is slightly smaller.

---

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(-)
fs/inode.c +9 -5
index 10121fc7b87e..e188bb1eb07a 100644
@@ -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);
 	}
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 1 line · Mateusz Guzik

While this may sound like a pedantic clean up, it does in fact impact

It also makes it easier to read. So +1.

maintainer Applied
#2 lore raw
In reply to Mateusz Guzik
Quoted 4 lines · Mateusz Guzik

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
reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Mateusz Guzik
Quoted 4 lines · Mateusz Guzik

While this may sound like a pedantic clean up, it does in fact impact code generation -- the patched add routine is slightly smaller.

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

Honza

Quoted 92 lines

---

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(-)
fs/inode.c +9 -5
index 10121fc7b87e..e188bb1eb07a 100644
@@ -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);
 	}
 }
 
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 8 lines · Jan Kara
Quoted 4 lines · Mateusz Guzik

While this may sound like a pedantic clean up, it does in fact impact code generation -- the patched add routine is slightly smaller.

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().

#5 lore raw
In reply to Jan Kara
Quoted 10 lines · Jan Kara
Quoted 4 lines · Mateusz Guzik

While this may sound like a pedantic clean up, it does in fact impact code generation -- the patched add routine is slightly smaller.

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.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
reviewer
#6 lore raw
Quoted 12 lines · Mateusz Guzik
Quoted 10 lines · Jan Kara
Quoted 4 lines · Mateusz Guzik

While this may sound like a pedantic clean up, it does in fact impact code generation -- the patched add routine is slightly smaller.

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

Quoted 5 lines

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

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