erofs: fix folio reuse from a different address_space in erofs_bread()
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.
--- fs/erofs/data.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/erofs/data.c +6 -2
@@ -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
Gao Xiang
maintainerQuoted 3 lines · Binglei Wang
From: Binglei Wang <[email protected]>
erofs_bread()reuses a cached folio on page index match without checkingfolio->mapping.
Each line in the commit message should not exceed 72 chars.
Quoted 21 lines
xattr.ccallserofs_init_metabuf()twice on the same buffer withouterofs_put_metabuf(); the twoin_metaboxsources differ, sobuf->mappingcan switch
whilebuf->pagestill belongs to the oldaddress_space.
The nexterofs_bread()then reads the wrong mapping, dropping shared xattrs with METABOX.We require
folio->mapping==buf->mappingin 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]>Signed-off-by Binglei Wang <[email protected]>--- fs/erofs/data.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)fs/erofs/data.c +0
-0
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
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.
---
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
@@ -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);
Gao Xiang
maintainerQuoted 9 lines · Binglei Wang
erofs_bread()reuses a cached folio on page index match without checkingfolio->mapping.
xattr.ccallserofs_init_metabuf()twice on the same buffer withouterofs_put_metabuf(); the twoin_metaboxsources differ, sobuf->mappingcan switch whilebuf->pagestill belongs to the oldaddress_space. The nexterofs_bread()then reads the wrong mapping, dropping shared xattrs with METABOX.
Quoted 6 lines
We require
folio->mapping==buf->mappingin 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]>Signed-off-by Binglei Wang <[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
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.
---
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
@@ -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);
Quoted 9 lines · Binglei Wang
erofs_bread()reuses a cached folio on page index match without checkingfolio->mapping.
xattr.ccallserofs_init_metabuf()twice on the same buffer withouterofs_put_metabuf(); the twoin_metaboxsources differ, sobuf->mappingcan switch whilebuf->pagestill belongs to the oldaddress_space. The nexterofs_bread()then reads the wrong mapping, dropping shared xattrs with METABOX.
Quoted 6 lines
We require
folio->mapping==buf->mappingin 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]>Signed-off-by Binglei Wang <[email protected]>
I applied this patch with the updated subject:
erofs: fix xattrs handling when metadata compression is used
Thanks,
Gao Xiang
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 checkingfolio->mapping.
xattr.ccallserofs_init_metabuf()twice on the same buffer withouterofs_put_metabuf(); the twoin_metaboxsources differ, sobuf->mappingcan switch whilebuf->pagestill belongs to the oldaddress_space. The nexterofs_bread()then reads the wrong mapping, dropping shared xattrs with METABOX.Quoted 6 lines
We require
folio->mapping==buf->mappingin 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]>Signed-off-by Binglei Wang <[email protected]>I applied this patch with the updated subject:
erofs: fix xattrs handling when metadata compression is used
Reviewed-by Gao Xiang <[email protected]>Thanks,
Gao Xiang
Quoted 9 lines · Binglei Wang
erofs_bread()reuses a cached folio on page index match without checkingfolio->mapping.
xattr.ccallserofs_init_metabuf()twice on the same buffer withouterofs_put_metabuf(); the twoin_metaboxsources differ, sobuf->mappingcan switch whilebuf->pagestill belongs to the oldaddress_space. The nexterofs_bread()then reads the wrong mapping, dropping shared xattrs with METABOX.
Quoted 6 lines
We require
folio->mapping==buf->mappingin 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]>Signed-off-by Binglei Wang <[email protected]>
Thanks,