public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fs: Fix data race in inode_set_ctime_to_ts
@ 2024-11-20  2:43 Hao-ran Zheng
  2024-11-21 11:35 ` Jan Kara
  0 siblings, 1 reply; 32+ messages in thread
From: Hao-ran Zheng @ 2024-11-20  2:43 UTC (permalink / raw)
  To: viro, brauner, jack, linux-fsdevel, linux-kernel
  Cc: baijiaju1990, zhenghaoran, 21371365

A data race may occur when the function `inode_set_ctime_to_ts()` and
the function `inode_get_ctime_sec()` are executed concurrently. When
two threads call `aio_read` and `aio_write` respectively, they will
be distributed to the read and write functions of the corresponding
file system respectively. Taking the btrfs file system as an example,
the `btrfs_file_read_iter` and `btrfs_file_write_iter` functions are
finally called. These two functions created a data race when they
finally called `inode_get_ctime_sec()` and `inode_set_ctime_to_ns()`.
The specific call stack that appears during testing is as follows:

```
============DATA_RACE============
btrfs_delayed_update_inode+0x1f61/0x7ce0 [btrfs]
btrfs_update_inode+0x45e/0xbb0 [btrfs]
btrfs_dirty_inode+0x2b8/0x530 [btrfs]
btrfs_update_time+0x1ad/0x230 [btrfs]
touch_atime+0x211/0x440
filemap_read+0x90f/0xa20
btrfs_file_read_iter+0xeb/0x580 [btrfs]
aio_read+0x275/0x3a0
io_submit_one+0xd22/0x1ce0
__se_sys_io_submit+0xb3/0x250
do_syscall_64+0xc1/0x190
entry_SYSCALL_64_after_hwframe+0x77/0x7f
============OTHER_INFO============
btrfs_write_check+0xa15/0x1390 [btrfs]
btrfs_buffered_write+0x52f/0x29d0 [btrfs]
btrfs_do_write_iter+0x53d/0x1590 [btrfs]
btrfs_file_write_iter+0x41/0x60 [btrfs]
aio_write+0x41e/0x5f0
io_submit_one+0xd42/0x1ce0
__se_sys_io_submit+0xb3/0x250
do_syscall_64+0xc1/0x190
entry_SYSCALL_64_after_hwframe+0x77/0x7f
```

The call chain after traceability is as follows:

```
Thread1:
btrfs_delayed_update_inode() ->
fill_stack_inode_item() ->
inode_get_ctime_sec()

Thread2:
btrfs_write_check() ->
update_time_for_write() ->
inode_set_ctime_to_ts()
```

To address this issue, it is recommended to
add WRITE_ONCE when writing the `inode->i_ctime_sec` variable.

Signed-off-by: Hao-ran Zheng <zhenghaoran@buaa.edu.cn>
---
 include/linux/fs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3559446279c1..d11b257a35e1 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1674,8 +1674,8 @@ static inline struct timespec64 inode_get_ctime(const struct inode *inode)
 static inline struct timespec64 inode_set_ctime_to_ts(struct inode *inode,
 						      struct timespec64 ts)
 {
-	inode->i_ctime_sec = ts.tv_sec;
-	inode->i_ctime_nsec = ts.tv_nsec;
+	WRITE_ONCE(inode->i_ctime_sec, ts.tv_sec);
+	WRITE_ONCE(inode->i_ctime_nsec, ts.tv_nsec);
 	return ts;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread
* Re: [RFC] metadata updates vs. fetches (was Re: [PATCH v4] fs: Fix data race in inode_set_ctime_to_ts)
@ 2024-11-26  6:44 郑浩然
  0 siblings, 0 replies; 32+ messages in thread
From: 郑浩然 @ 2024-11-26  6:44 UTC (permalink / raw)
  To: torvalds, viro, brauner, mjguzik, willy, linux, djwong, jack,
	linux-fsdevel, linux-kernel
  Cc: 21371365, baijiaju1990, zhenghaoran

Sorry for the previous email in html format, here is the resent 
email in plain ASCII text format.

Thanks for your replies. During further testing, I found that the 
same problem also occurred with `mtime` and `atime`. I already 
understand that this bug may have limited impact, but should I do 
something else to deal with this series of timestamp-related issues?

The new call stack is as follows
============DATA_RACE============
 btrfs_write_check+0x841/0x13f0 [btrfs]
 btrfs_buffered_write+0x6a9/0x2c90 [btrfs]
 btrfs_do_write_iter+0x4b7/0x16d0 [btrfs]
 btrfs_file_write_iter+0x41/0x60 [btrfs]
 aio_write+0x445/0x600
 io_submit_one+0xd68/0x1cf0
 __se_sys_io_submit+0xc4/0x270
 do_syscall_64+0xc9/0x1a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
 0x0
============OTHER_INFO============
 btrfs_delayed_update_inode+0x1e24/0x80e0 [btrfs]
 btrfs_update_inode+0x478/0xbc0 [btrfs]
 btrfs_finish_one_ordered+0x24d6/0x36a0 [btrfs]
 btrfs_finish_ordered_io+0x37/0x60 [btrfs]
 finish_ordered_fn+0x3e/0x50 [btrfs]
 btrfs_work_helper+0x9c9/0x27a0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

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

end of thread, other threads:[~2024-11-26  6:45 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-20  2:43 [PATCH] fs: Fix data race in inode_set_ctime_to_ts Hao-ran Zheng
2024-11-21 11:35 ` Jan Kara
2024-11-22  3:51   ` [PATCH v2] " Hao-ran Zheng
2024-11-22 11:13     ` Christian Brauner
2024-11-22 11:22     ` Jan Kara
2024-11-22 11:48       ` 郑浩然
2024-11-22 13:06       ` [PATCH v3] " Hao-ran Zheng
2024-11-23 14:01         ` Jeff Layton
2024-11-24  8:46           ` 郑浩然
2024-11-24  9:42           ` [PATCH v4] " Hao-ran Zheng
2024-11-24 17:44             ` Darrick J. Wong
2024-11-24 17:56               ` Mateusz Guzik
2024-11-24 18:34                 ` Darrick J. Wong
2024-11-24 21:50                 ` [RFC] metadata updates vs. fetches (was Re: [PATCH v4] fs: Fix data race in inode_set_ctime_to_ts) Al Viro
2024-11-24 22:10                   ` Linus Torvalds
2024-11-24 22:24                     ` Al Viro
2024-11-24 22:34                       ` Matthew Wilcox
2024-11-24 22:43                         ` Linus Torvalds
2024-11-24 23:53                           ` Matthew Wilcox
2024-11-25  0:53                             ` Linus Torvalds
2024-11-25  1:02                               ` Linus Torvalds
2024-11-25  1:15                               ` Matthew Wilcox
2024-11-25  1:26                                 ` Linus Torvalds
2024-11-24 22:40                       ` Linus Torvalds
2024-11-24 23:05                     ` Mateusz Guzik
2024-11-24 23:19                       ` Mateusz Guzik
2024-11-24 23:41                         ` Al Viro
2024-11-24 23:38                       ` Al Viro
2024-11-25 12:20                     ` Christian Brauner
2024-11-24 22:10                   ` Dr. David Alan Gilbert
2024-11-24 22:19                     ` Matthew Wilcox
  -- strict thread matches above, loose matches on Subject: below --
2024-11-26  6:44 郑浩然

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox