Data races in ext4
Hi,
I've encountered several data races while running custom test cases on ext4 (kernel 3.13.5), inside a QEMU based VM, but I'm not sure whether all of these data races are harmful.
Bellow I'm listing the data races summary, including the variable name, IP addresses, function names and source code files/line numbers. In addition, the pastbin links include snippets of the code at those locations and also include examples of racing pairs of instructions (which can be useful when there are more than two instructions racing). Several of the races reported affect either the function generic_fillattr() or the function ext4_do_update_inode(), so I'm grouping them bellow to simplify the analysis. Feel free to ask for more information in case it's needed.
I was hoping someone could have a look at these data races and let me know if they're going to be fixed.
Thanks,
Pedro
** Data races:
Quoted 34 lines
Variable:
mapping->nrpagesAddresses:c107d9f3c110c8d7c107d9f3add_to_page_cache_locked/linux-3.13.5/mm/filemap.c:487c110c8d7ext4_writepages/linux-3.13.5/fs/ext4/inode.c:2413Variable:
raw_inode->i_size_loAddresses:c110abf0c110abdbc110abf0ext4_isize_set/linux-3.13.5/fs/ext4/ext4.h:2395c110abdbext4_isize/linux-3.13.5/fs/ext4/ext4.h:2387Variable:
mapping->writeback_indexAddresses:c108320fc108320fwrite_cache_pages/linux-3.13.5/mm/page-writeback.c:2012Variable:
ei->i_datasync_tidAddresses:c110adb3c110adb3ext4_update_inode_fsync_trans/linux-3.13.5/fs/ext4/ext4_jbd2.h:384Variable:
ei->i_sync_tidAddresses:c112ae80c110ae6fc112ae80ext4_update_inode_fsync_trans/linux-3.13.5/fs/ext4/ext4_jbd2.h:382c110ae6fext4_update_inode_fsync_trans/linux-3.13.5/fs/ext4/ext4_jbd2.h:382Variable: kaddr/link Addresses:
c10ade1ac12d40ebc10ade1ageneric_readlinkat()->page_follow_link_light()->page_getlink()->nd_terminate_link /linux-3.13.5/include/linux/namei.h:98c12d40ebgeneric_readlinkat()->vfs_readlink()->strlen /linux-3.13.5/arch/x86/lib/string_32.c:168Variable:
journal->j_running_transactionAddresses:c1138ca1c1136b02c1136ca9c1136ca9jbd2_get_transaction/linux-3.13.5/fs/jbd2/transaction.c:103c1138ca1jbd2_journal_commit_transaction/linux-3.13.5/fs/jbd2/commit.c:539c1136b02start_this_handle/linux-3.13.5/fs/jbd2/transaction.c:280Variable:
inode->i_stateAddresses:c10c035ec10c03cbc10c0352c10c093ac10bffe2c10c0352__writeback_single_inode/linux-3.13.5/fs/fs-writeback.c:473c10bffe2inode_sync_complete/linux-3.13.5/fs/fs-writeback.c:217c10c035e__writeback_single_inode/linux-3.13.5/fs/fs-writeback.c:475c10c093awriteback_sb_inodes/linux-3.13.5/fs/fs-writeback.c:666c10c03cb__mark_inode_dirty/linux-3.13.5/fs/fs-writeback.c:1149
Quoted 3 lines
Variable:
raw_inode->i_size_highAddresses:c110abf3c110abdec110abdeext4_isize/linux-3.13.5/fs/ext4/ext4.h:2387c110abf3ext4_isize_set/linux-3.13.5/fs/ext4/ext4.h:2396
** Races involving/related to the function generic_fillattr():
Quoted 26 lines
Variable:
inode->i_blocksAddresses:c10aa04bc10aa891c10a9f72c10a9f72__inode_sub_bytes/linux-3.13.5/fs/stat.c:471c10aa891__inode_add_bytes/linux-3.13.5/fs/stat.c:451c10aa04bgeneric_fillattr/linux-3.13.5/fs/stat.c:35Variable:
inode->i_ctimeAddresses:c10aa032c110b231c10aa032generic_fillattr/linux-3.13.5/fs/stat.c:33c110b231ext4_current_time/linux-3.13.5/fs/ext4/inode.c:3769Variable:
inode->i_blocksAddresses:c10a9f75c110ab50c10aa048c10aa894c10a9f75__inode_sub_bytes/linux-3.13.5/fs/stat.c:471c110ab50ext4_inode_blocks_set/linux-3.13.5/fs/ext4/inode.c:4238c10aa048generic_fillattr/linux-3.13.5/fs/stat.c:35c10aa894__inode_add_bytes/linux-3.13.5/fs/stat.c:451Variable:
inode->i_ctimeAddresses:c10b79e9c110b22bc10aa02fc110b22bext4_current_time/linux-3.13.5/fs/ext4/inode.c:3769c10b79e9update_time/linux-3.13.5/fs/inode.c:1510c10aa02fgeneric_fillattr/linux-3.13.5/fs/stat.c:33Variable:
inode->i_atimeAddresses:c10b7f62c10b79a9c10aa017c10b7fcbc10b7f62timespec_compare/linux-3.13.5/include/linux/time.h:35c10b7fcbtimespec_equal/linux-3.13.5/include/linux/time.h:25c10aa017generic_fillattr/linux-3.13.5/fs/stat.c:31c10b79a9update_time/linux-3.13.5/fs/inode.c:1506
Quoted 4 lines
Variable:
inode->i_mtimeAddresses:c10b79f9c10aa023c110b237c110b237ext4_truncate/linux-3.13.5/fs/ext4/inode.c:3769c10b79f9update_time/linux-3.13.5/fs/inode.c:1512c10aa023generic_fillattr/linux-3.13.5/fs/stat.c:32
** Races involving/related to the function ext4_do_update_inode():
Quoted 43 lines
Variable:
inode->i_ctimeAddresses:c110aac1c10b79ecc110aac1ext4_encode_extra_time/linux-3.13.5/fs/ext4/ext4.h:729c10b79ecupdate_time/linux-3.13.5/fs/inode.c:1510Variable:
i_crtimeAddresses:c110ab4ac110ab4aext4_encode_extra_time/linux-3.13.5/fs/ext4/ext4.h:729Variable:
ei->i_disksizeAddresses:c110c813c110ac06c110c813ext4_wb_update_i_disksize/linux-3.13.5/fs/ext4/ext4.h:2476c110ac06ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4342Variable:
raw_inode->i_gid_highAddresses:c110aa6ac110aa6aext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4311Variable:
raw_inode->i_dtimeAddresses:c110adf2c110adf2ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4331Variable:
i_mtimeAddresses:c110aac7c110aac7ext4_encode_extra_time/linux-3.13.5/fs/ext4/ext4.h:729Variable:
i_mtimeAddresses:c110aad0c110aad0ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4325Variable:
raw_inode->i_file_acl_highAddresses:c110abbfc110abbfext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4336Variable:
raw_inode->i_flagsAddresses:c110adf8c110adf8ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4332Variable:
raw_inode->i_file_acl_loAddresses:c110abc6c110abc6ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4337Variable:
raw_inode->i_blocks_loAddresses:c110ab5ec110ab5eext4_inode_blocks_set/linux-3.13.5/fs/ext4/inode.c:4246Variable:
raw_inode->i_links_countAddresses:c110aaa6c110aaa6ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4322Variable:
raw_inode->i_modeAddresses:c110aa52c110aa3ac110abc9c110aa3aext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4297c110aa52ext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4301c110abc9ext4_isize/linux-3.13.5/fs/ext4/ext4.h:2386
Quoted 5 lines
Variable: EXT4_I(inode)->i_disksize Addresses:
c110c810c110c147c110abcbc110c7d2c110c810ext4_wb_update_i_disksize/linux-3.13.5/fs/ext4/ext4.h:2476c110abcbext4_do_update_inode/linux-3.13.5/fs/ext4/inode.c:4338c110c7d2mpage_map_and_submit_extent/linux-3.13.5/fs/ext4/inode.c:2236c110c147ext4_da_write_end/linux-3.13.5/fs/ext4/inode.c:2758
Quoted 8 lines · Pedro Fonseca
Bellow I'm listing the data races summary, including the variable name, IP addresses, function names and source code files/line numbers. In addition, the pastbin links include snippets of the code at those locations and also include examples of racing pairs of instructions (which can be useful when there are more than two instructions racing). Several of the races reported affect either the function
generic_fillattr()or the functionext4_do_update_inode(), so I'm grouping them bellow to simplify the analysis. Feel free to ask for more information in case it's needed.
The ones relating to ext4_do_update_inode() and generic_fillattr() look fine to me. Basically the first may happen if there are two CPU's simultaneously trying to update the on-disk data structure for an inode from the in-memory data structure, and that's not a problem since the in-memory data structure is always authoratative. The second happens if someone tries to stat(2) an inode while some inode field is getting updated. Most of the inode field updates will be for things like mtime and atime updates, or a chown or chmod, where a single field is getting updated, and that's not a problem since there is no guarantee which version of the inode information you'll get when you call stat(2) while the inode is getting modified out from under you. It is possible that the userspace might see the i_blocks and i_size be out of sync, but I really can't bring myself to care about that.
As for the rest, some are obviously false positives. For example, if you take a look at:
Variable: journal->j_running_transaction Addresses: c1138ca1 c1136b02 c1136ca9c1136ca9 jbd2_get_transaction /linux-3.13.5/fs/jbd2/transaction.c:103 c1138ca1 jbd2_journal_commit_transaction /linux-3.13.5/fs/jbd2/commit.c:539 c1136b02 start_this_handle /linux-3.13.5/fs/jbd2/transaction.c:280
It's obvious from the code path that start_this_handle() is extremely careful to always revalidate j_running_transaction after either (a) taking a read lock on the j_state_lock for the first time, or (b) after dropping the read_lock and grabbing a write_lock on j_state_lock, and if things have changed, it loops and tries again.
There are a couple that require some closer examination; for some of them, for example the two reported cases of ext4_isize_set() vs ext4_isize(), having the stack trace would be really helpful.
Cheers,
- Ted
Quoted 6 lines · Theodore Ts'o
The ones relating to
ext4_do_update_inode()andgeneric_fillattr()look fine to me. Basically the first may happen if there are two CPU's simultaneously trying to update the on-disk data structure for an inode from the in-memory data structure, and that's not a problem since the in-memory data structure is always authoratative. The second happens if someone tries to stat(2) an inode while some inode
Quoted 8 lines
field is getting updated. Most of the inode field updates will be for things like mtime and atime updates, or a chown or chmod, where a single field is getting updated, and that's not a problem since there is no guarantee which version of the inode information you'll get when you call stat(2) while the inode is getting modified out from under you. It is possible that the userspace might see the
i_blocksandi_sizebe out of sync, but I really can't bring myself to care about that.
Regarding the generic_fillattr() function, apart from the inconsistency between i_blocks/i_size, it looks like it can also cause wrong values to be returned because several of those fields are 64 bits. This means that each assignment gets converted into multiple instructions (in 32-bit architectures), allowing other instructions to interleave in between. For example, this is the assembly code I get when compiling generic_fillattr():
Quoted 15 lines
stat->atime = inode->i_atime;
c10aa017: 8b 48 3c mov 0x3c(%eax),%ecx
c10aa01a: 8b 58 40 mov 0x40(%eax),%ebx
c10aa01d: 89 4a 28 mov %ecx,0x28(%edx)
c10aa020: 89 5a 2c mov %ebx,0x2c(%edx)
stat->mtime = inode->i_mtime;
c10aa023: 8b 48 44 mov 0x44(%eax),%ecx
c10aa026: 8b 58 48 mov 0x48(%eax),%ebx
c10aa029: 89 4a 30 mov %ecx,0x30(%edx)
c10aa02c: 89 5a 34 mov %ebx,0x34(%edx)
stat->ctime = inode->i_ctime;
c10aa02f: 8b 48 4c mov 0x4c(%eax),%ecx
c10aa032: 8b 58 50 mov 0x50(%eax),%ebx
c10aa035: 89 4a 38 mov %ecx,0x38(%edx)
c10aa038: 89 5a 3c mov %ebx,0x3c(%edx)...
Quoted 5 lines
stat->blocks = inode->i_blocks; c10aa048: 8b 58 60 mov 0x60(%eax),%ebx c10aa04b: 8b 48 5c mov 0x5c(%eax),%ecx c10aa04e: 89 5a 48 mov %ebx,0x48(%edx) c10aa051: 89 4a 44 mov %ecx,0x44(%edx)
I still don't understand why the races in ext4_do_update_inode() won't cause problems. Similarly to generic_fillattr(), in ext4_do_update_inode() wrong values can end up being written to the inode fields. Is there some mechanism to recover/ignore these values that I'm missing?
Quoted 8 lines
As for the rest, some are obviously false positives. For example, if you take a look at:
Variable:
journal->j_running_transactionAddresses:c1138ca1c1136b02c1136ca9c1136ca9jbd2_get_transaction/linux-3.13.5/fs/jbd2/transaction.c:103c1138ca1jbd2_journal_commit_transaction/linux-3.13.5/fs/jbd2/commit.c:539c1136b02start_this_handle/linux-3.13.5/fs/jbd2/transaction.c:280
Quoted 5 lines
It's obvious from the code path that
start_this_handle()is extremely careful to always revalidatej_running_transactionafter either (a) taking a read lock on thej_state_lockfor the first time, or (b) after dropping theread_lockand grabbing awrite_lockonj_state_lock, and if things have changed, it loops and tries again.
You're right, start_this_handle() revalidates the value read so I also don't see harm in this race.
Quoted 3 lines
There are a couple that require some closer examination; for some of them, for example the two reported cases of
ext4_isize_set()vsext4_isize(), having the stack trace would be really helpful.
Those two pairs of functions were actually also called from the ext4_do_update_inode() function:
Quoted 4 lines
4338 if (
ei->i_disksize!=ext4_isize(raw_inode)) {
4339ext4_isize_set(raw_inode, ei->i_disksize);
4340need_datasync= 1;
4341 }
So the top part of the stack trace is:
ext4_mark_inode_dirty() -> ext4_mark_iloc_dirty() ->ext4_do_update_inode() -> ext4_isize()
ext4_mark_inode_dirty() -> ext4_mark_iloc_dirty() ->ext4_do_update_inode() -> ext4_isize_set()
Thinking about this some more, you're right. We should probably use a spinlock to regulate access to the raw inode in ext4_do_update_inode(). The main reason why we haven't noticed that most of these fields don't change very often, and so in practice it's not really problem. The one that _is_ critical is i_disksize / raw_inode->i_size, and that's supposed to be protected by i_data_sem. (The reason why that one showed up in your analysis is because we did screw up the locking in mpage_map_and_submit_extent.)
Quoted 3 lines
Regarding the
generic_fillattr()function, apart from the inconsistency between i_blocks/i_size, it looks like it can also cause wrong values to be returned because several of those fields are 64 bits.
Well, it's actually a 32-bit seconds and 32-bit nanoseconds field. So we could theoretically get a torn value, but if the ns field is from a previous update, the worst that could happen under those circumstances is that two quick successive stat() commands could potentially result in end up with an apparent "time moving backwards" in the nanoseconds field. If someone was constantly calling utimes(2) to modify the mtime/atime at high rate, that might cause a more serious wrong value, but that's an argument you should take up with the generic VFS folks as to whether it's important enough that they would want to care about it.
Quoted 8 lines
Quoted 8 lines
As for the rest, some are obviously false positives. For example, if you take a look at:
Variable:
journal->j_running_transactionAddresses:c1138ca1c1136b02c1136ca9c1136ca9jbd2_get_transaction/linux-3.13.5/fs/jbd2/transaction.c:103c1138ca1jbd2_journal_commit_transaction/linux-3.13.5/fs/jbd2/commit.c:539c1136b02start_this_handle/linux-3.13.5/fs/jbd2/transaction.c:280
Quoted 7 lines
Quoted 5 lines
It's obvious from the code path that
start_this_handle()is extremely careful to always revalidatej_running_transactionafter either (a) taking a read lock on thej_state_lockfor the first time, or (b) after dropping theread_lockand grabbing awrite_lockonj_state_lock, and if things have changed, it loops and tries again.You're right,
start_this_handle()revalidates the value read so I also don't see harm in this race.
Well, I suppose technically it's a race, but in it was quite deliberate, because the goal was to minimize pressure on the j_state_lock.
Quoted 6 lines
Quoted 3 lines
There are a couple that require some closer examination; for some of them, for example the two reported cases of
ext4_isize_set()vsext4_isize(), having the stack trace would be really helpful.Those two pairs of functions were actually also called from the
ext4_do_update_inode()function:
OK, and I think this is ultimately caused by a screwup in how mpage_map_and_submit_extent() which I mentioned earlier.
Thanks for your work,
- Ted
P.S. What did you use to generate these reports? Is it something that can be easily replicated by others?
Quoted 4 lines
Quoted 4 lines
Regarding the
generic_fillattr()function, apart from the inconsistency between i_blocks/i_size, it looks like it can also cause wrong values to be returned because several of those fields are 64 bits.
Quoted 10 lines
Well, it's actually a 32-bit seconds and 32-bit nanoseconds field. So we could theoretically get a torn value, but if the ns field is from a previous update, the worst that could happen under those circumstances is that two quick successive
stat()commands could potentially result in end up with an apparent "time moving backwards" in the nanoseconds field. If someone was constantly calling utimes(2) to modify the mtime/atime at high rate, that might cause a more serious wrong value, but that's an argument you should take up with the generic VFS folks as to whether it's important enough that they would want to care about it.
Note that the races in stat() also involve the "inode->i_blocks" field, which is also 64-bits. So, as far as I understand, this field too can get wrapped, although it should only affect very large files. (On the other hand, the field inode->i_size seems to be protected.)
I'll follow your suggestion and let the VFS developers know about this.
Quoted 2 lines
P.S. What did you use to generate these reports? Is it something that can be easily replicated by others?
We're currently developing an experimental tool to test the kernel for concurrency bugs. It's not yet publicly available, but we hope it will become available at some point in the future.
Thanks,
Pedro