Skip to contentSkip to conversation

erofs: fix folio reuse from a different address_space in erofs_bread()

PATCH Superseded Binglei Wang 3 participants 8 messages Sep 30 – Oct 10 EROFS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs: fix folio reuse from a different address_space in erofs_bread()

From: Binglei Wang <[email protected]>

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch
while buf->page still belongs to the old address_space.
The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>
---
 fs/erofs/data.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/erofs/data.c +6 -2
index be63b89f0862..d8b6523bc218 100644
@@ -33,8 +33,13 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 	if (buf->page) {
 		folio = page_folio(buf->page);
-		if (folio_file_page(folio, index) != buf->page)
+		if (folio->mapping != buf->mapping) {
+			/* the cached folio belongs to another address_space */
+			erofs_put_metabuf(buf);
+			folio = NULL;
+		} else if (folio_file_page(folio, index) != buf->page) {
 			erofs_unmap_metabuf(buf);
+		}
 	}
 	if (!folio || !folio_contains(folio, index)) {
 		erofs_put_metabuf(buf);
--

2.43.0

maintainer
#1 lore raw
Quoted 3 lines · Binglei Wang

From: Binglei Wang <[email protected]>

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

Each line in the commit message should not exceed 72 chars.

Quoted 21 lines

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch
while buf->page still belongs to the old address_space.
The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>
---
 fs/erofs/data.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/erofs/data.c +0 -0
index be63b89f0862..d8b6523bc218 100644

fs/erofs/data.c void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap) · line 38

Quoted 7 lines
fs/erofs/data.c +4 -1
@@ -33,8 +33,13 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 	if (buf->page) {
 		folio = page_folio(buf->page);
-		if (folio_file_page(folio, index) != buf->page)
+		if (folio->mapping != buf->mapping) {
+			/* the cached folio belongs to another address_space */
+			erofs_put_metabuf(buf);
+			folio = NULL;

As I said, you could just drop erofs_put_metabuf(buf); here, and folio = NULL will do the correct thing instead.

Thanks,
Gao Xiang

Quoted 10 lines
fs/erofs/data.c +2 -1
+		} else if (folio_file_page(folio, index) != buf->page) {
 			erofs_unmap_metabuf(buf);
+		}
 	}
 	if (!folio || !folio_contains(folio, index)) {
 		erofs_put_metabuf(buf);
--

2.43.0

#2 lore raw

PATCH v2 1/1 erofs: fix folio reuse from a different address_space in erofs_bread()

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch while buf->page still belongs to the old address_space. The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>

---
Changes since v1:
- Drop the redundant erofs_put_metabuf() call; setting folio to NULL is enough since the slow path already releases the cached folio (Gao Xiang).
- Rewrap the commit message to 72 columns.

 fs/erofs/data.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/erofs/data.c +5 -1
index be63b89f0862..4b9fe670fdda 100644
@@ -33,8 +33,12 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 	if (buf->page) {
 		folio = page_folio(buf->page);
-		if (folio_file_page(folio, index) != buf->page)
+		if (folio->mapping != buf->mapping) {
+			/* the cached folio belongs to another address_space */
+			folio = NULL;
+		} else if (folio_file_page(folio, index) != buf->page) {
 			erofs_unmap_metabuf(buf);
+		}
 	}
 	if (!folio || !folio_contains(folio, index)) {
 		erofs_put_metabuf(buf);
maintainer
#3 lore raw
Quoted 9 lines · Binglei Wang

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch while buf->page still belongs to the old address_space. The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

Quoted 6 lines

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>

I have to tell you that the patch is still broken, and I failed to apply like this:

Applying: erofs: fix folio reuse from a different address_space in erofs_bread() Using index info to reconstruct a base tree...
error: patch failed: fs/erofs/data.c:33
error: fs/erofs/data.c: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 erofs: fix folio reuse from a different address_space in erofs_bread()

Could you just apply locally before sending out a version? I could make a patch for you manually, but if you apply more patches later, the broken email client should be fixed.

Thanks,
Gao Xiang

#4 lore raw

PATCH v2 1/1 erofs: fix folio reuse from a different address_space in erofs_bread()

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch while buf->page still belongs to the old address_space. The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>

---
Changes since v1:
- Drop the redundant erofs_put_metabuf() call; setting folio to NULL is enough since the slow path already releases the cached folio (Gao Xiang).
- Rewrap the commit message to 72 columns.

 fs/erofs/data.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/erofs/data.c +5 -1
index be63b89f0862..4b9fe670fdda 100644
@@ -33,8 +33,12 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 
 	if (buf->page) {
 		folio = page_folio(buf->page);
-		if (folio_file_page(folio, index) != buf->page)
+		if (folio->mapping != buf->mapping) {
+			/* the cached folio belongs to another address_space */
+			folio = NULL;
+		} else if (folio_file_page(folio, index) != buf->page) {
 			erofs_unmap_metabuf(buf);
+		}
 	}
 	if (!folio || !folio_contains(folio, index)) {
 		erofs_put_metabuf(buf);
maintainer Reviewed-by on v2 1/1
#5 lore raw
Quoted 9 lines · Binglei Wang

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch while buf->page still belongs to the old address_space. The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

Quoted 6 lines

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>

I applied this patch with the updated subject:

erofs: fix xattrs handling when metadata compression is used

Thanks,
Gao Xiang

#6 lore raw

Thanks, happy National Day.

Quoted 26 lines · Gao Xiang <[email protected]> 于2026年10月3日周六 00:01写道:
Quoted 9 lines · Binglei Wang

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch while buf->page still belongs to the old address_space. The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

Quoted 6 lines

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>

I applied this patch with the updated subject:

erofs: fix xattrs handling when metadata compression is used

Thanks,
Gao Xiang

maintainer Reviewed-by on v2 1/1
#7 lore raw
In reply to Binglei Wang
Quoted 9 lines · Binglei Wang

erofs_bread() reuses a cached folio on page index match without checking folio->mapping.

xattr.c calls erofs_init_metabuf() twice on the same buffer without erofs_put_metabuf(); the two in_metabox sources differ, so buf->mapping can switch while buf->page still belongs to the old address_space. The next erofs_bread() then reads the wrong mapping, dropping shared xattrs with METABOX.

Quoted 6 lines

We require folio->mapping == buf->mapping in the reuse check; otherwise drop the cached folio and re-read via the slow path.

Fixes 414091322c63 ("erofs: implement metadata compression")
Cc Bo Liu (OpenAnolis) <[email protected]>

Thanks,