diff mbox series

[f2fs-dev] f2fs: fix inconsistent dirty state of atomic file

Message ID 20250123071417.253019-1-huangjianan@xiaomi.com (mailing list archive)
State Superseded
Headers show
Series [f2fs-dev] f2fs: fix inconsistent dirty state of atomic file | expand

Commit Message

Jianan Huang Jan. 23, 2025, 7:14 a.m. UTC
When testing the atomic write fix patches, the f2fs_bug_on was
triggered as below:

------------[ cut here ]------------
kernel BUG at fs/f2fs/inode.c:935!
Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
CPU: 3 UID: 0 PID: 257 Comm: bash Not tainted 6.13.0-rc1-00033-gc283a70d3497 #5
RIP: 0010:f2fs_evict_inode+0x50f/0x520
Call Trace:
 <TASK>
 ? __die_body+0x65/0xb0
 ? die+0x9f/0xc0
 ? do_trap+0xa1/0x170
 ? f2fs_evict_inode+0x50f/0x520
 ? f2fs_evict_inode+0x50f/0x520
 ? handle_invalid_op+0x65/0x80
 ? f2fs_evict_inode+0x50f/0x520
 ? exc_invalid_op+0x39/0x50
 ? asm_exc_invalid_op+0x1a/0x20
 ? __pfx_f2fs_get_dquots+0x10/0x10
 ? f2fs_evict_inode+0x50f/0x520
 ? f2fs_evict_inode+0x2e5/0x520
 evict+0x186/0x2f0
 prune_icache_sb+0x75/0xb0
 super_cache_scan+0x1a8/0x200
 do_shrink_slab+0x163/0x320
 shrink_slab+0x2fc/0x470
 drop_slab+0x82/0xf0
 drop_caches_sysctl_handler+0x4e/0xb0
 proc_sys_call_handler+0x183/0x280
 vfs_write+0x36d/0x450
 ksys_write+0x68/0xd0
 do_syscall_64+0xc8/0x1a0
 ? arch_exit_to_user_mode_prepare+0x11/0x60
 ? irqentry_exit_to_user_mode+0x7e/0xa0

The root cause is: f2fs uses FI_ATOMIC_DIRTIED to indicate dirty
atomic files during commit. If the inode is dirtied during commit,
such as by f2fs_i_pino_write, the vfs inode keeps clean and the
f2fs inode is set to FI_DIRTY_INODE. The FI_DIRTY_INODE flag cann't
be cleared by write_inode later due to the clean vfs inode. Finally,
f2fs_bug_on is triggered due to this inconsistent state when evict.

To reproduce this situation:
- fd = open("/mnt/test.db", O_WRONLY)
- ioctl(fd, F2FS_IOC_START_ATOMIC_WRITE)
- mv /mnt/test.db /mnt/test1.db
- ioctl(fd, F2FS_IOC_COMMIT_ATOMIC_WRITE)
- echo 3 > /proc/sys/vm/drop_caches

To fix this problem, clear FI_DIRTY_INODE after commit, then
f2fs_mark_inode_dirty_sync will ensure a consistent dirty state.

Fixes: fccaa81de87e ("f2fs: prevent atomic file from being dirtied before commit")
Signed-off-by: Yunlei He <heyunlei@xiaomi.com>
Signed-off-by: Jianan Huang <huangjianan@xiaomi.com>
---
 fs/f2fs/segment.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Chao Yu Jan. 23, 2025, 9:49 a.m. UTC | #1
On 1/23/25 15:14, Jianan Huang via Linux-f2fs-devel wrote:
> When testing the atomic write fix patches, the f2fs_bug_on was
> triggered as below:
> 
> ------------[ cut here ]------------
> kernel BUG at fs/f2fs/inode.c:935!
> Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
> CPU: 3 UID: 0 PID: 257 Comm: bash Not tainted 6.13.0-rc1-00033-gc283a70d3497 #5
> RIP: 0010:f2fs_evict_inode+0x50f/0x520
> Call Trace:
>  <TASK>
>  ? __die_body+0x65/0xb0
>  ? die+0x9f/0xc0
>  ? do_trap+0xa1/0x170
>  ? f2fs_evict_inode+0x50f/0x520
>  ? f2fs_evict_inode+0x50f/0x520
>  ? handle_invalid_op+0x65/0x80
>  ? f2fs_evict_inode+0x50f/0x520
>  ? exc_invalid_op+0x39/0x50
>  ? asm_exc_invalid_op+0x1a/0x20
>  ? __pfx_f2fs_get_dquots+0x10/0x10
>  ? f2fs_evict_inode+0x50f/0x520
>  ? f2fs_evict_inode+0x2e5/0x520
>  evict+0x186/0x2f0
>  prune_icache_sb+0x75/0xb0
>  super_cache_scan+0x1a8/0x200
>  do_shrink_slab+0x163/0x320
>  shrink_slab+0x2fc/0x470
>  drop_slab+0x82/0xf0
>  drop_caches_sysctl_handler+0x4e/0xb0
>  proc_sys_call_handler+0x183/0x280
>  vfs_write+0x36d/0x450
>  ksys_write+0x68/0xd0
>  do_syscall_64+0xc8/0x1a0
>  ? arch_exit_to_user_mode_prepare+0x11/0x60
>  ? irqentry_exit_to_user_mode+0x7e/0xa0
> 
> The root cause is: f2fs uses FI_ATOMIC_DIRTIED to indicate dirty
> atomic files during commit. If the inode is dirtied during commit,
> such as by f2fs_i_pino_write, the vfs inode keeps clean and the
> f2fs inode is set to FI_DIRTY_INODE. The FI_DIRTY_INODE flag cann't
> be cleared by write_inode later due to the clean vfs inode. Finally,
> f2fs_bug_on is triggered due to this inconsistent state when evict.
> 
> To reproduce this situation:
> - fd = open("/mnt/test.db", O_WRONLY)
> - ioctl(fd, F2FS_IOC_START_ATOMIC_WRITE)
> - mv /mnt/test.db /mnt/test1.db
> - ioctl(fd, F2FS_IOC_COMMIT_ATOMIC_WRITE)
> - echo 3 > /proc/sys/vm/drop_caches
> 
> To fix this problem, clear FI_DIRTY_INODE after commit, then
> f2fs_mark_inode_dirty_sync will ensure a consistent dirty state.
> 
> Fixes: fccaa81de87e ("f2fs: prevent atomic file from being dirtied before commit")
> Signed-off-by: Yunlei He <heyunlei@xiaomi.com>
> Signed-off-by: Jianan Huang <huangjianan@xiaomi.com>
> ---
>  fs/f2fs/segment.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index dc1b47f9269a..71b509a31eae 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -201,6 +201,7 @@ void f2fs_abort_atomic_write(struct inode *inode, bool clean)
>  	clear_inode_flag(inode, FI_ATOMIC_FILE);
>  	if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) {
>  		clear_inode_flag(inode, FI_ATOMIC_DIRTIED);
> +		clear_inode_flag(inode, FI_DIRTY_INODE);

Jianan,

Can you please add comments for this change? otherwise it looks good to me.

Thanks,

>  		f2fs_mark_inode_dirty_sync(inode, true);
>  	}
>  	stat_dec_atomic_inode(inode);
Jianan Huang Jan. 24, 2025, 1:51 a.m. UTC | #2
On 2025/1/23 17:49, Chao Yu wrote:
> [外部邮件] 此邮件来源于小米公司外部,请谨慎处理。若对邮件安全性存疑,请将邮件转发给misec@xiaomi.com进行反馈
> 
> On 1/23/25 15:14, Jianan Huang via Linux-f2fs-devel wrote:
>> When testing the atomic write fix patches, the f2fs_bug_on was
>> triggered as below:
>>
>> ------------[ cut here ]------------
>> kernel BUG at fs/f2fs/inode.c:935!
>> Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
>> CPU: 3 UID: 0 PID: 257 Comm: bash Not tainted 6.13.0-rc1-00033-gc283a70d3497 #5
>> RIP: 0010:f2fs_evict_inode+0x50f/0x520
>> Call Trace:
>>   <TASK>
>>   ? __die_body+0x65/0xb0
>>   ? die+0x9f/0xc0
>>   ? do_trap+0xa1/0x170
>>   ? f2fs_evict_inode+0x50f/0x520
>>   ? f2fs_evict_inode+0x50f/0x520
>>   ? handle_invalid_op+0x65/0x80
>>   ? f2fs_evict_inode+0x50f/0x520
>>   ? exc_invalid_op+0x39/0x50
>>   ? asm_exc_invalid_op+0x1a/0x20
>>   ? __pfx_f2fs_get_dquots+0x10/0x10
>>   ? f2fs_evict_inode+0x50f/0x520
>>   ? f2fs_evict_inode+0x2e5/0x520
>>   evict+0x186/0x2f0
>>   prune_icache_sb+0x75/0xb0
>>   super_cache_scan+0x1a8/0x200
>>   do_shrink_slab+0x163/0x320
>>   shrink_slab+0x2fc/0x470
>>   drop_slab+0x82/0xf0
>>   drop_caches_sysctl_handler+0x4e/0xb0
>>   proc_sys_call_handler+0x183/0x280
>>   vfs_write+0x36d/0x450
>>   ksys_write+0x68/0xd0
>>   do_syscall_64+0xc8/0x1a0
>>   ? arch_exit_to_user_mode_prepare+0x11/0x60
>>   ? irqentry_exit_to_user_mode+0x7e/0xa0
>>
>> The root cause is: f2fs uses FI_ATOMIC_DIRTIED to indicate dirty
>> atomic files during commit. If the inode is dirtied during commit,
>> such as by f2fs_i_pino_write, the vfs inode keeps clean and the
>> f2fs inode is set to FI_DIRTY_INODE. The FI_DIRTY_INODE flag cann't
>> be cleared by write_inode later due to the clean vfs inode. Finally,
>> f2fs_bug_on is triggered due to this inconsistent state when evict.
>>
>> To reproduce this situation:
>> - fd = open("/mnt/test.db", O_WRONLY)
>> - ioctl(fd, F2FS_IOC_START_ATOMIC_WRITE)
>> - mv /mnt/test.db /mnt/test1.db
>> - ioctl(fd, F2FS_IOC_COMMIT_ATOMIC_WRITE)
>> - echo 3 > /proc/sys/vm/drop_caches
>>
>> To fix this problem, clear FI_DIRTY_INODE after commit, then
>> f2fs_mark_inode_dirty_sync will ensure a consistent dirty state.
>>
>> Fixes: fccaa81de87e ("f2fs: prevent atomic file from being dirtied before commit")
>> Signed-off-by: Yunlei He <heyunlei@xiaomi.com>
>> Signed-off-by: Jianan Huang <huangjianan@xiaomi.com>
>> ---
>>   fs/f2fs/segment.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
>> index dc1b47f9269a..71b509a31eae 100644
>> --- a/fs/f2fs/segment.c
>> +++ b/fs/f2fs/segment.c
>> @@ -201,6 +201,7 @@ void f2fs_abort_atomic_write(struct inode *inode, bool clean)
>>        clear_inode_flag(inode, FI_ATOMIC_FILE);
>>        if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) {
>>                clear_inode_flag(inode, FI_ATOMIC_DIRTIED);
>> +             clear_inode_flag(inode, FI_DIRTY_INODE);
> 
> Jianan,
> 
> Can you please add comments for this change? otherwise it looks good to me.

Updated in v2.

Thanks,

> 
> Thanks,
> 
>>                f2fs_mark_inode_dirty_sync(inode, true);
>>        }
>>        stat_dec_atomic_inode(inode);
>
diff mbox series

Patch

diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index dc1b47f9269a..71b509a31eae 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -201,6 +201,7 @@  void f2fs_abort_atomic_write(struct inode *inode, bool clean)
 	clear_inode_flag(inode, FI_ATOMIC_FILE);
 	if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) {
 		clear_inode_flag(inode, FI_ATOMIC_DIRTIED);
+		clear_inode_flag(inode, FI_DIRTY_INODE);
 		f2fs_mark_inode_dirty_sync(inode, true);
 	}
 	stat_dec_atomic_inode(inode);