Skip to contentSkip to conversation

btrfs: fix a bug that btrfs_invalidapge() can double account ordered extent for subpage

PATCH In review Qu Wenruo 4 participants 4 messages Jan 27, 2021 – Jan 28, 2021 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: fix a bug that btrfs_invalidapge() can double account ordered extent for subpage

Commit dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page") make btrfs_invalidapage() to search all ordered extents.

The offending code looks like this:

again:

	start = page_start;
	ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
	if (ordred) {
		end = min(page_end,
			  ordered->file_offset + ordered->num_bytes - 1);

		/* Do the cleanup */

		start = end + 1;
		if (start < page_end)
			goto again;
	}

The behavior is indeed necessary for the incoming subpage support, but when it iterate through all the ordered extents, it also resets the search range @start.

This means, for the following cases, we can double account the ordered extents, causing its bytes_left underflow:

	Page offset
	0		16K		32K
	|<--- OE 1  --->|<--- OE 2 ---->|

As the first iteration will find OE 1, which doesn't cover the full page, thus after cleanup code, we need to retry again.
But again label will reset start to page_start, and we got OE 1 again, which causes double account on OE1, and cause OE1's byte_left to underflow.

The only good news is, this problem can only happen for subpage case, as for regular sectorsize == PAGE_SIZE case, we will always find a OE ends at or after page end, thus no way to trigger the problem.

This patch will move the again label after start = page_start, to fix the bug.
This is just a quick fix, which is easy to backport.

There will be more comprehensive rework to convert the open coded loop to a proper while loop.

Fixes dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page")
---
 fs/btrfs/inode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +2 -1
index ef6cb7b620d0..2eea7d22405a 100644
@@ -8184,8 +8184,9 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
 
 	if (!inode_evicting)
 		lock_extent_bits(tree, page_start, page_end, &cached_state);
-again:
+
 	start = page_start;
+again:
 	ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
 	if (ordered) {
 		found_ordered = true;
Signature · 1 line
2.30.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 38 lines · Qu Wenruo

Commit dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page") make btrfs_invalidapage() to search all ordered extents.

The offending code looks like this:

again:

        start = page_start;
        ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
        if (ordred) {
                end = min(page_end,
                          ordered->file_offset + ordered->num_bytes - 1);

                /* Do the cleanup */

                start = end + 1;
                if (start < page_end)
                        goto again;
        }

The behavior is indeed necessary for the incoming subpage support, but when it iterate through all the ordered extents, it also resets the search range @start.

This means, for the following cases, we can double account the ordered extents, causing its bytes_left underflow:

Page offset
0 16K 32K
|<--- OE 1 --->|<--- OE 2 ---->|

As the first iteration will find OE 1, which doesn't cover the full page, thus after cleanup code, we need to retry again.
But again label will reset start to page_start, and we got OE 1 again, which causes double account on OE1, and cause OE1's byte_left to underflow.

Quoted 7 lines

The only good news is, this problem can only happen for subpage case, as for regular sectorsize == PAGE_SIZE case, we will always find a OE ends at or after page end, thus no way to trigger the problem.

This patch will move the again label after start = page_start, to fix the bug.
This is just a quick fix, which is easy to backport.

Hum? Why does it need to be backported?
There are no kernel releases that support subpage sector size yet and this does not affect the case where sector size is the same as the page size.

Quoted 6 lines

There will be more comprehensive rework to convert the open coded loop to a proper while loop.

Fixes dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page")

Anyway, it looks good to me, thanks.

Quoted 22 lines
---
 fs/btrfs/inode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +2 -2
index ef6cb7b620d0..2eea7d22405a 100644
@@ -8184,8 +8184,9 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
 
        if (!inode_evicting)
                lock_extent_bits(tree, page_start, page_end, &cached_state);
-again:
+
        start = page_start;
+again:
        ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
        if (ordered) {
                found_ordered = true;
--

2.30.0

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#2 lore raw
Quoted 43 lines · Filipe Manana
Quoted 42 lines · Qu Wenruo

Commit dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page") make btrfs_invalidapage() to search all ordered extents.

The offending code looks like this:

again:

         start = page_start;
         ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
         if (ordred) {
                 end = min(page_end,
                           ordered->file_offset + ordered->num_bytes - 1);

                 /* Do the cleanup */

                 start = end + 1;
                 if (start < page_end)
                         goto again;
         }

The behavior is indeed necessary for the incoming subpage support, but when it iterate through all the ordered extents, it also resets the search range @start.

This means, for the following cases, we can double account the ordered extents, causing its bytes_left underflow:

Page offset
0 16K 32K
|<--- OE 1 --->|<--- OE 2 ---->|

As the first iteration will find OE 1, which doesn't cover the full page, thus after cleanup code, we need to retry again.
But again label will reset start to page_start, and we got OE 1 again, which causes double account on OE1, and cause OE1's byte_left to underflow.

The only good news is, this problem can only happen for subpage case, as for regular sectorsize == PAGE_SIZE case, we will always find a OE ends at or after page end, thus no way to trigger the problem.

Quoted 8 lines
Quoted 3 lines

This patch will move the again label after start = page_start, to fix the bug.
This is just a quick fix, which is easy to backport.

Hum? Why does it need to be backported?
There are no kernel releases that support subpage sector size yet and this does not affect the case where sector size is the same as the page size.

Hmmm, right, there is no need to backport.

Hopes David can remove that line when merging.

Thanks,
Qu

Quoted 36 lines
Quoted 7 lines

There will be more comprehensive rework to convert the open coded loop to a proper while loop.

Fixes dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page")

Anyway, it looks good to me, thanks.

Quoted 22 lines
---
  fs/btrfs/inode.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/inode.c +2 -2
index ef6cb7b620d0..2eea7d22405a 100644
@@ -8184,8 +8184,9 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
 
         if (!inode_evicting)
                 lock_extent_bits(tree, page_start, page_end, &cached_state);
-again:
+
         start = page_start;
+again:
         ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
         if (ordered) {
                 found_ordered = true;
--

2.30.0

#3 lore raw
Quoted 22 lines · Qu Wenruo
Quoted 11 lines · Filipe Manana
Quoted 10 lines · Qu Wenruo

As the first iteration will find OE 1, which doesn't cover the full page, thus after cleanup code, we need to retry again.
But again label will reset start to page_start, and we got OE 1 again, which causes double account on OE1, and cause OE1's byte_left to underflow.

The only good news is, this problem can only happen for subpage case, as for regular sectorsize == PAGE_SIZE case, we will always find a OE ends at or after page end, thus no way to trigger the problem.

Quoted 8 lines
Quoted 3 lines

This patch will move the again label after start = page_start, to fix the bug.
This is just a quick fix, which is easy to backport.

Hum? Why does it need to be backported?
There are no kernel releases that support subpage sector size yet and this does not affect the case where sector size is the same as the page size.

Quoted 3 lines

Hmmm, right, there is no need to backport.

Hopes David can remove that line when merging.

Yes, I've dropped the 'backport' line and slightly changed the wording so it sounds like it's preparatory patch.