[PATCH v2] mm: do not access page->mapping directly on page_endio

From: Minchan Kim
Date: Thu Nov 03 2016 - 20:12:39 EST

With rw_page, page_endio is used for completing IO on a page
and it propagates write error to the address space if the IO
fails. The problem is it accesses page->mapping directly which
might be okay for file-backed pages but it shouldn't for
anonymous page. Otherwise, it can corrupt one of field from
anon_vma under us and system goes panic randomly.


I encountered the BUG during developing new zram feature and
it was really hard to figure it out because it made random
crash, somtime mmap_sem lockdep, sometime other places where
places never related to zram/zsmalloc, and not reproducible
with some configuration.

When I consider how that bug is subtle and people do fast-swap
test with brd, it's worth to add stable mark, I think.

Fixes: dd6bd0d9c7db ("swap: use bdev_read_page() / bdev_write_page()")
Cc: Michal Hocko <mhocko@xxxxxxxxxx>
Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx>
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Minchan Kim <minchan@xxxxxxxxxx>
* from v1
* add more detailed description with Fix tag - Michal

mm/filemap.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 2ba46f410c7c..1944c631e3e6 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1008,9 +1008,12 @@ void page_endio(struct page *page, bool is_write, int err)
} else {
if (err) {
+ struct address_space *mapping;
- if (page->mapping)
- mapping_set_error(page->mapping, err);
+ mapping = page_mapping(page);
+ if (mapping)
+ mapping_set_error(mapping, err);