Skip to contentSkip to conversation

f2fs: fix retry logic in f2fs_write_cache_pages()

PATCH Superseded Sahitya Tummala 3 participants 7 messages May 27, 2020 – Jun 1, 2020 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: fix retry logic in f2fs_write_cache_pages()

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +1 -1
index 4af5fcd..bfd1df4 100644
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;
+		end = retry ? -1 : writeback_index - 1;
 		goto retry;
 	}
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
Signature · 9 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 19 lines · Sahitya Tummala

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +0 -0
index 4af5fcd..bfd1df4 100644

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3024

Quoted 1 line
fs/f2fs/data.c +0 -0
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {

IMO, we add retry logic in wrong place, you can see that cycled value is zero only if wbc->range_cyclic is true, in that case writeback_index is valid.

However if retry is true and wbc->range_cyclic is false, then writeback_index would be uninitialized variable.

Thoughts?

Thanks,

Quoted 8 lines
fs/f2fs/data.c +1 -1
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;
+		end = retry ? -1 : writeback_index - 1;
 		goto retry;
 	}
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
#2 lore raw
Quoted 28 lines · Chao Yu
Quoted 19 lines · Sahitya Tummala

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +0 -0
index 4af5fcd..bfd1df4 100644

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3024

Quoted 1 line
fs/f2fs/data.c +0 -0
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {

IMO, we add retry logic in wrong place, you can see that cycled value is zero only if wbc->range_cyclic is true, in that case writeback_index is valid.

However if retry is true and wbc->range_cyclic is false, then writeback_index would be uninitialized variable.

Quoted 7 lines

Thoughts?

Thanks,

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3027

Quoted 3 lines
fs/f2fs/data.c +0 -1
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;

BTW, I notice that range_cyclic writeback flow was refactored in below commit, and skeleton of f2fs.writepages was copied from mm/page-writeback.c::write_cache_pages(), I guess we need follow that change.

64081362e8ff ("mm/page-writeback.c: fix range_cyclic writeback vs writepages deadlock")

Thanks,

Quoted 13 lines

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3030

Quoted 5 lines
fs/f2fs/data.c +1 -0
+		end = retry ? -1 : writeback_index - 1;
 		goto retry;
 	}
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
.

maintainer
#3 lore raw
Quoted 41 lines · Chao Yu
Quoted 28 lines · Chao Yu
Quoted 19 lines · Sahitya Tummala

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +0 -0
index 4af5fcd..bfd1df4 100644

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3024

Quoted 1 line
fs/f2fs/data.c +0 -0
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {

IMO, we add retry logic in wrong place, you can see that cycled value is zero only if wbc->range_cyclic is true, in that case writeback_index is valid.

However if retry is true and wbc->range_cyclic is false, then writeback_index would be uninitialized variable.

Quoted 7 lines

Thoughts?

Thanks,

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3027

Quoted 3 lines
fs/f2fs/data.c +0 -1
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;

BTW, I notice that range_cyclic writeback flow was refactored in below commit, and skeleton of f2fs.writepages was copied from mm/page-writeback.c::write_cache_pages(), I guess we need follow that change.

Quoted 1 line

64081362e8ff ("mm/page-writeback.c: fix range_cyclic writeback vs writepages deadlock")

Is that something like this?

---
 fs/f2fs/data.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
fs/f2fs/data.c +2 -9
index 48a622b95b76e..28fcdf0d4dcb9 100644
@@ -2861,7 +2861,6 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	pgoff_t index;
 	pgoff_t end;		/* Inclusive */
 	pgoff_t done_index;
-	int cycled;
 	int range_whole = 0;
 	xa_mark_t tag;
 	int nwritten = 0;
@@ -2879,17 +2878,12 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if (wbc->range_cyclic) {
 		writeback_index = mapping->writeback_index; /* prev offset */
 		index = writeback_index;
-		if (index == 0)
-			cycled = 1;
-		else
-			cycled = 0;
 		end = -1;
 	} else {
 		index = wbc->range_start >> PAGE_SHIFT;
 		end = wbc->range_end >> PAGE_SHIFT;
 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
 			range_whole = 1;
-		cycled = 1; /* ignore range_cyclic tests */
 	}
 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
 		tag = PAGECACHE_TAG_TOWRITE;
@@ -3054,10 +3048,9 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 		}
 	}
 #endif
-	if ((!cycled && !done) || retry) {
-		cycled = 1;
+	if (retry) {
 		index = 0;
-		end = writeback_index - 1;
+		end = -1;
 		goto retry;
 	}
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
Signature · 1 line
2.27.0.rc0.183.gde8f92d652-goog
#4 lore raw

Hi Chao,

Can you please help review below diff given by Jaegeuk? If it looks good, I can send a v2.

Thanks,

Quoted 96 lines · Jaegeuk Kim
Quoted 41 lines · Chao Yu
Quoted 28 lines · Chao Yu
Quoted 19 lines · Sahitya Tummala

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +0 -0
index 4af5fcd..bfd1df4 100644

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3024

Quoted 1 line
fs/f2fs/data.c +0 -0
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {

IMO, we add retry logic in wrong place, you can see that cycled value is zero only if wbc->range_cyclic is true, in that case writeback_index is valid.

However if retry is true and wbc->range_cyclic is false, then writeback_index would be uninitialized variable.

Quoted 7 lines

Thoughts?

Thanks,

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3060

Quoted 3 lines
fs/f2fs/data.c +0 -1
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;

BTW, I notice that range_cyclic writeback flow was refactored in below commit, and skeleton of f2fs.writepages was copied from mm/page-writeback.c::write_cache_pages(), I guess we need follow that change.

Quoted 1 line

64081362e8ff ("mm/page-writeback.c: fix range_cyclic writeback vs writepages deadlock")

Is that something like this?

---
 fs/f2fs/data.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
fs/f2fs/data.c +2 -9
index 48a622b95b76e..28fcdf0d4dcb9 100644
@@ -2861,7 +2861,6 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	pgoff_t index;
 	pgoff_t end;		/* Inclusive */
 	pgoff_t done_index;
-	int cycled;
 	int range_whole = 0;
 	xa_mark_t tag;
 	int nwritten = 0;
@@ -2879,17 +2878,12 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if (wbc->range_cyclic) {
 		writeback_index = mapping->writeback_index; /* prev offset */
 		index = writeback_index;
-		if (index == 0)
-			cycled = 1;
-		else
-			cycled = 0;
 		end = -1;
 	} else {
 		index = wbc->range_start >> PAGE_SHIFT;
 		end = wbc->range_end >> PAGE_SHIFT;
 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
 			range_whole = 1;
-		cycled = 1; /* ignore range_cyclic tests */
 	}
 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
 		tag = PAGECACHE_TAG_TOWRITE;
@@ -3054,10 +3048,9 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 		}
 	}
 #endif
-	if ((!cycled && !done) || retry) {
-		cycled = 1;
+	if (retry) {
 		index = 0;
-		end = writeback_index - 1;
+		end = -1;
 		goto retry;
 	}
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
Signature · 1 line
2.27.0.rc0.183.gde8f92d652-goog
Signature · 3 lines
--
Sent by a consultant of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#5 lore raw
In reply to Jaegeuk Kim

Hi Chao,

Can you please help review the diff given by Jaegeuk below? If it looks good, I can post a v2.

Thanks,

Quoted 96 lines · Jaegeuk Kim
Quoted 41 lines · Chao Yu
Quoted 28 lines · Chao Yu
Quoted 19 lines · Sahitya Tummala

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +0 -0
index 4af5fcd..bfd1df4 100644

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3024

Quoted 1 line
fs/f2fs/data.c +0 -0
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {

IMO, we add retry logic in wrong place, you can see that cycled value is zero only if wbc->range_cyclic is true, in that case writeback_index is valid.

However if retry is true and wbc->range_cyclic is false, then writeback_index would be uninitialized variable.

Quoted 7 lines

Thoughts?

Thanks,

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3060

Quoted 3 lines
fs/f2fs/data.c +0 -1
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;

BTW, I notice that range_cyclic writeback flow was refactored in below commit, and skeleton of f2fs.writepages was copied from mm/page-writeback.c::write_cache_pages(), I guess we need follow that change.

Quoted 1 line

64081362e8ff ("mm/page-writeback.c: fix range_cyclic writeback vs writepages deadlock")

Is that something like this?

---
 fs/f2fs/data.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
fs/f2fs/data.c +2 -9
index 48a622b95b76e..28fcdf0d4dcb9 100644
@@ -2861,7 +2861,6 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	pgoff_t index;
 	pgoff_t end;		/* Inclusive */
 	pgoff_t done_index;
-	int cycled;
 	int range_whole = 0;
 	xa_mark_t tag;
 	int nwritten = 0;
@@ -2879,17 +2878,12 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if (wbc->range_cyclic) {
 		writeback_index = mapping->writeback_index; /* prev offset */
 		index = writeback_index;
-		if (index == 0)
-			cycled = 1;
-		else
-			cycled = 0;
 		end = -1;
 	} else {
 		index = wbc->range_start >> PAGE_SHIFT;
 		end = wbc->range_end >> PAGE_SHIFT;
 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
 			range_whole = 1;
-		cycled = 1; /* ignore range_cyclic tests */
 	}
 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
 		tag = PAGECACHE_TAG_TOWRITE;
@@ -3054,10 +3048,9 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 		}
 	}
 #endif
-	if ((!cycled && !done) || retry) {
-		cycled = 1;
+	if (retry) {
 		index = 0;
-		end = writeback_index - 1;
+		end = -1;
 		goto retry;
 	}
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
Signature · 1 line
2.27.0.rc0.183.gde8f92d652-goog
Signature · 9 lines
--
Sent by a consultant of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.


_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#6 lore raw
Quoted 4 lines · Sahitya Tummala

Hi Chao,

Can you please help review the diff given by Jaegeuk below? If it looks good, I can post a v2.

Oops, I missed to review that patch...

Quoted 88 lines

Thanks,

Quoted 84 lines · Jaegeuk Kim
Quoted 41 lines · Chao Yu
Quoted 28 lines · Chao Yu
Quoted 19 lines · Sahitya Tummala

In case a compressed file is getting overwritten, the current retry logic doesn't include the current page to be retried now as it sets the new start index as 0 and new end index as writeback_index - 1. This causes the corresponding cluster to be uncompressed and written as normal pages without compression. Fix this by allowing writeback to be retried for the current page as well (in case of compressed page getting retried due to index mismatch with cluster index). So that this cluster can be written compressed in case of overwrite.

---
 fs/f2fs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/data.c +0 -0
index 4af5fcd..bfd1df4 100644

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3024

Quoted 1 line
fs/f2fs/data.c +0 -0
@@ -3024,7 +3024,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if ((!cycled && !done) || retry) {

IMO, we add retry logic in wrong place, you can see that cycled value is zero only if wbc->range_cyclic is true, in that case writeback_index is valid.

However if retry is true and wbc->range_cyclic is false, then writeback_index would be uninitialized variable.

Quoted 7 lines

Thoughts?

Thanks,

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3060

Quoted 3 lines
fs/f2fs/data.c +0 -1
 		cycled = 1;
 		index = 0;
-		end = writeback_index - 1;

BTW, I notice that range_cyclic writeback flow was refactored in below commit, and skeleton of f2fs.writepages was copied from mm/page-writeback.c::write_cache_pages(), I guess we need follow that change.

Quoted 1 line

64081362e8ff ("mm/page-writeback.c: fix range_cyclic writeback vs writepages deadlock")

Is that something like this?

---
 fs/f2fs/data.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
fs/f2fs/data.c +0 -6
index 48a622b95b76e..28fcdf0d4dcb9 100644
@@ -2861,7 +2861,6 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	pgoff_t index;
 	pgoff_t end;		/* Inclusive */
 	pgoff_t done_index;
-	int cycled;
 	int range_whole = 0;
 	xa_mark_t tag;
 	int nwritten = 0;
@@ -2879,17 +2878,12 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 	if (wbc->range_cyclic) {
 		writeback_index = mapping->writeback_index; /* prev offset */
 		index = writeback_index;
-		if (index == 0)
-			cycled = 1;
-		else
-			cycled = 0;
 		end = -1;
 	} else {
 		index = wbc->range_start >> PAGE_SHIFT;
 		end = wbc->range_end >> PAGE_SHIFT;
 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
 			range_whole = 1;
-		cycled = 1; /* ignore range_cyclic tests */
 	}
 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
 		tag = PAGECACHE_TAG_TOWRITE;
@@ -3054,10 +3048,9 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
 		}
 	}
 #endif
Quoted 8 lines

fs/f2fs/data.c static int f2fs_write_cache_pages(struct address_space *mapping, · line 3055

Quoted 8 lines
fs/f2fs/data.c +2 -3
-	if ((!cycled && !done) || retry) {
-		cycled = 1;
+	if (retry) {
 		index = 0;
-		end = writeback_index - 1;
+		end = -1;
 		goto retry;
 	}

+ if (wbc->range_cyclic && !done)
+ done_index = 0;

Thanks,

Quoted 5 lines
Quoted 4 lines
fs/f2fs/data.c +0 -0
 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
Signature · 1 line
2.27.0.rc0.183.gde8f92d652-goog