Skip to contentSkip to conversation

ext2: take invalidate_lock in ext2_write_failed()

PATCH In review Pedro Falcato 3 participants 5 messages Oct 24 – Nov 3 ext2 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext2: take invalidate_lock in ext2_write_failed()

Currently, ext2_write_failed() takes care of removing extra data (folios & blocks) from the page cache by first truncating the page cache, then truncating the actual inode's blocks.

Understanding that this is correct - namely, that a concurrent page cache lookup can't create stale pages and stale block mappings between truncate_pagecache() and ext2_truncate_blocks() - is non trivial. This is correct for two non-obvious reasons:

1) every caller of ext2_write_failed() holds some sort of exclusion against truncation. In most cases, callers hold (in some way) the inode lock. ext2_write_failed() is only reachable from buffered/direct IO write paths, which hold it. page_symlink() can also call write_begin _without the inode lock_, but at that point the inode is I_NEW, so there are no other users.

2) the page cache is generally extremely careful at never instantiating pages beyond EOF.

This is all quite confusing. Holding the invalidate lock across both operations is largely simpler, and mirrors other bits of other filesystems (like ext4_truncate_failed_write). Do that.

This has the side effect of making it so ext2_evict_inode() doesn't take the invalidate_lock when truncating blocks on inode deletion. This is safe as the inode should not have any users at that point.

---

This is a patch that fell out of trying to understand whether [email protected] was correct. I think it is, for the same reason ext2 is correct. I find this to be easier to reason about if we just take the invalidate lock across both operations.

 fs/ext2/inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ext2/inode.c +2 -2
index 12ac4cfe1500..723b31dc7dac 100644
@@ -58,8 +58,10 @@ void ext2_write_failed(struct address_space *mapping, loff_t to)
 	struct inode *inode = mapping->host;
 
 	if (to > inode->i_size) {
+		filemap_invalidate_lock(mapping);
 		truncate_pagecache(inode, inode->i_size);
 		ext2_truncate_blocks(inode, inode->i_size);
+		filemap_invalidate_unlock(mapping);
 	}
 }
 
@@ -1236,9 +1238,7 @@ static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
 	if (ext2_inode_is_fast_symlink(inode))
 		return;
 
-	filemap_invalidate_lock(inode->i_mapping);
 	__ext2_truncate_blocks(inode, offset);
-	filemap_invalidate_unlock(inode->i_mapping);
 }
 
 static int ext2_setsize(struct inode *inode, loff_t newsize)
Signature · 1 line
2.56.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 23 lines

Currently, ext2_write_failed() takes care of removing extra data (folios & blocks) from the page cache by first truncating the page cache, then truncating the actual inode's blocks.

Understanding that this is correct - namely, that a concurrent page cache lookup can't create stale pages and stale block mappings between truncate_pagecache() and ext2_truncate_blocks() - is non trivial. This is correct for two non-obvious reasons:

1) every caller of ext2_write_failed() holds some sort of exclusion against truncation. In most cases, callers hold (in some way) the inode lock. ext2_write_failed() is only reachable from buffered/direct IO write paths, which hold it. page_symlink() can also call write_begin _without the inode lock_, but at that point the inode is I_NEW, so there are no other users.

2) the page cache is generally extremely careful at never instantiating pages beyond EOF.

This is all quite confusing. Holding the invalidate lock across both operations is largely simpler, and mirrors other bits of other filesystems (like ext4_truncate_failed_write). Do that.

Quoted 5 lines

This has the side effect of making it so ext2_evict_inode() doesn't take the invalidate_lock when truncating blocks on inode deletion. This is safe as the inode should not have any users at that point.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Pedro Falcato
Quoted 23 lines · Pedro Falcato

Currently, ext2_write_failed() takes care of removing extra data (folios & blocks) from the page cache by first truncating the page cache, then truncating the actual inode's blocks.

Understanding that this is correct - namely, that a concurrent page cache lookup can't create stale pages and stale block mappings between truncate_pagecache() and ext2_truncate_blocks() - is non trivial. This is correct for two non-obvious reasons:

1) every caller of ext2_write_failed() holds some sort of exclusion against truncation. In most cases, callers hold (in some way) the inode lock. ext2_write_failed() is only reachable from buffered/direct IO write paths, which hold it. page_symlink() can also call write_begin _without the inode lock_, but at that point the inode is I_NEW, so there are no other users.

2) the page cache is generally extremely careful at never instantiating pages beyond EOF.

This is all quite confusing. Holding the invalidate lock across both operations is largely simpler, and mirrors other bits of other filesystems (like ext4_truncate_failed_write). Do that.

Quoted 5 lines

This has the side effect of making it so ext2_evict_inode() doesn't take the invalidate_lock when truncating blocks on inode deletion. This is safe as the inode should not have any users at that point.

There's actually one more reason - filesystems that don't support hole punching are generally ok with the synchronization provided by inode lock and i_size checks (as within i_size allocated space can never disappear under you). But I agree this is all too subtle so you're change is a good one. Added to my tree.

Honza

Quoted 38 lines

---

This is a patch that fell out of trying to understand whether [email protected] was correct. I think it is, for the same reason ext2 is correct. I find this to be easier to reason about if we just take the invalidate lock across both operations.

 fs/ext2/inode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ext2/inode.c +2 -2
index 12ac4cfe1500..723b31dc7dac 100644
@@ -58,8 +58,10 @@ void ext2_write_failed(struct address_space *mapping, loff_t to)
 	struct inode *inode = mapping->host;
 
 	if (to > inode->i_size) {
+		filemap_invalidate_lock(mapping);
 		truncate_pagecache(inode, inode->i_size);
 		ext2_truncate_blocks(inode, inode->i_size);
+		filemap_invalidate_unlock(mapping);
 	}
 }
 
@@ -1236,9 +1238,7 @@ static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
 	if (ext2_inode_is_fast_symlink(inode))
 		return;
 
-	filemap_invalidate_lock(inode->i_mapping);
 	__ext2_truncate_blocks(inode, offset);
-	filemap_invalidate_unlock(inode->i_mapping);
 }
 
 static int ext2_setsize(struct inode *inode, loff_t newsize)
Signature · 1 line
2.56.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
Quoted 28 lines · Jan Kara
Quoted 27 lines · Pedro Falcato

Currently, ext2_write_failed() takes care of removing extra data (folios & blocks) from the page cache by first truncating the page cache, then truncating the actual inode's blocks.

Understanding that this is correct - namely, that a concurrent page cache lookup can't create stale pages and stale block mappings between truncate_pagecache() and ext2_truncate_blocks() - is non trivial. This is correct for two non-obvious reasons:

1) every caller of ext2_write_failed() holds some sort of exclusion against truncation. In most cases, callers hold (in some way) the inode lock. ext2_write_failed() is only reachable from buffered/direct IO write paths, which hold it. page_symlink() can also call write_begin _without the inode lock_, but at that point the inode is I_NEW, so there are no other users.

2) the page cache is generally extremely careful at never instantiating pages beyond EOF.

This is all quite confusing. Holding the invalidate lock across both operations is largely simpler, and mirrors other bits of other filesystems (like ext4_truncate_failed_write). Do that.

This has the side effect of making it so ext2_evict_inode() doesn't take the invalidate_lock when truncating blocks on inode deletion. This is safe as the inode should not have any users at that point.

Quoted 7 lines
Quoted 1 line

There's actually one more reason - filesystems that don't support hole punching are generally ok with the synchronization provided by inode lock and i_size checks (as within i_size allocated space can never disappear under you). But I agree this is all too subtle so you're change is a good one. Added to my tree.

Thanks!

Right. So I'm wondering if we couldn't assert some locks in truncate code. Something like (in truncate_inode_pages_range):

	/* truncation is only safe in a couple of cases:
	 * 1) caller holds the invalidate_lock. This protects against hole
	 * punching and other truncate-like activities.
	 * 2) caller holds the inode lock. This excludes against truncate-like
	 * operations in a variety of cases.
	 * 3) inode is new or freeing - no need to synchronize anything since
	 * we should be the only user for this inode.
	 */
	lockdep_assert(lockdep_is_held(&mapping->invalidate_lock) ||
			((lockdep_is_held(&mapping->host->i_rwsem) &&
			 (lstart >= i_size_read(mapping->host) ||
			 file_does_not_support_holes) ||
			inode_new_or_freeing);

I'm sure I'm missing some silly edgecase, but it would be nice to have this properly written down and asserted.

Signature · 1 line
Pedro
#4 lore raw
Quoted 55 lines · Pedro Falcato
Quoted 28 lines · Jan Kara
Quoted 27 lines · Pedro Falcato

Currently, ext2_write_failed() takes care of removing extra data (folios & blocks) from the page cache by first truncating the page cache, then truncating the actual inode's blocks.

Understanding that this is correct - namely, that a concurrent page cache lookup can't create stale pages and stale block mappings between truncate_pagecache() and ext2_truncate_blocks() - is non trivial. This is correct for two non-obvious reasons:

1) every caller of ext2_write_failed() holds some sort of exclusion against truncation. In most cases, callers hold (in some way) the inode lock. ext2_write_failed() is only reachable from buffered/direct IO write paths, which hold it. page_symlink() can also call write_begin _without the inode lock_, but at that point the inode is I_NEW, so there are no other users.

2) the page cache is generally extremely careful at never instantiating pages beyond EOF.

This is all quite confusing. Holding the invalidate lock across both operations is largely simpler, and mirrors other bits of other filesystems (like ext4_truncate_failed_write). Do that.

This has the side effect of making it so ext2_evict_inode() doesn't take the invalidate_lock when truncating blocks on inode deletion. This is safe as the inode should not have any users at that point.

Quoted 7 lines
Quoted 1 line

There's actually one more reason - filesystems that don't support hole punching are generally ok with the synchronization provided by inode lock and i_size checks (as within i_size allocated space can never disappear under you). But I agree this is all too subtle so you're change is a good one. Added to my tree.

Thanks!

Right. So I'm wondering if we couldn't assert some locks in truncate code. Something like (in truncate_inode_pages_range):

	/* truncation is only safe in a couple of cases:
	 * 1) caller holds the invalidate_lock. This protects against hole
	 * punching and other truncate-like activities.
	 * 2) caller holds the inode lock. This excludes against truncate-like
	 * operations in a variety of cases.
	 * 3) inode is new or freeing - no need to synchronize anything since
	 * we should be the only user for this inode.
	 */
	lockdep_assert(lockdep_is_held(&mapping->invalidate_lock) ||
			((lockdep_is_held(&mapping->host->i_rwsem) &&
			 (lstart >= i_size_read(mapping->host) ||
			 file_does_not_support_holes) ||
			inode_new_or_freeing);
Quoted 2 lines

I'm sure I'm missing some silly edgecase, but it would be nice to have this properly written down and asserted.

The part "file_does_not_support_holes" will be somewhat difficult to assert but honestly, I'd be willing to transition to a state where all the filesystems in truncate code are required to hold i_rwsem *and* invalidate_lock unless they are working beyond i_size, on new, or on freed inodes.

Honza

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