Skip to content

Commit b9d02f1

Browse files
yang-shitorvalds
authored andcommitted
mm: shmem: don't truncate page if memory failure happens
The current behavior of memory failure is to truncate the page cache regardless of dirty or clean. If the page is dirty the later access will get the obsolete data from disk without any notification to the users. This may cause silent data loss. It is even worse for shmem since shmem is in-memory filesystem, truncating page cache means discarding data blocks. The later read would return all zero. The right approach is to keep the corrupted page in page cache, any later access would return error for syscalls or SIGBUS for page fault, until the file is truncated, hole punched or removed. The regular storage backed filesystems would be more complicated so this patch is focused on shmem. This also unblock the support for soft offlining shmem THP. [[email protected]: fix uninitialized variable use in me_pagecache_clean()] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Yang Shi <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Peter Xu <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent dd0f230 commit b9d02f1

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

mm/memory-failure.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <linux/ratelimit.h>
5959
#include <linux/page-isolation.h>
6060
#include <linux/pagewalk.h>
61+
#include <linux/shmem_fs.h>
6162
#include "internal.h"
6263
#include "ras/ras_event.h"
6364

@@ -867,6 +868,7 @@ static int me_pagecache_clean(struct page_state *ps, struct page *p)
867868
{
868869
int ret;
869870
struct address_space *mapping;
871+
bool extra_pins;
870872

871873
delete_from_lru_cache(p);
872874

@@ -895,18 +897,24 @@ static int me_pagecache_clean(struct page_state *ps, struct page *p)
895897
goto out;
896898
}
897899

900+
/*
901+
* The shmem page is kept in page cache instead of truncating
902+
* so is expected to have an extra refcount after error-handling.
903+
*/
904+
extra_pins = shmem_mapping(mapping);
905+
898906
/*
899907
* Truncation is a bit tricky. Enable it per file system for now.
900908
*
901909
* Open: to take i_rwsem or not for this? Right now we don't.
902910
*/
903911
ret = truncate_error_page(p, page_to_pfn(p), mapping);
912+
if (has_extra_refcount(ps, p, extra_pins))
913+
ret = MF_FAILED;
914+
904915
out:
905916
unlock_page(p);
906917

907-
if (has_extra_refcount(ps, p, false))
908-
ret = MF_FAILED;
909-
910918
return ret;
911919
}
912920

mm/shmem.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,7 @@ shmem_write_begin(struct file *file, struct address_space *mapping,
24542454
struct inode *inode = mapping->host;
24552455
struct shmem_inode_info *info = SHMEM_I(inode);
24562456
pgoff_t index = pos >> PAGE_SHIFT;
2457+
int ret = 0;
24572458

24582459
/* i_rwsem is held by caller */
24592460
if (unlikely(info->seals & (F_SEAL_GROW |
@@ -2464,7 +2465,15 @@ shmem_write_begin(struct file *file, struct address_space *mapping,
24642465
return -EPERM;
24652466
}
24662467

2467-
return shmem_getpage(inode, index, pagep, SGP_WRITE);
2468+
ret = shmem_getpage(inode, index, pagep, SGP_WRITE);
2469+
2470+
if (*pagep && PageHWPoison(*pagep)) {
2471+
unlock_page(*pagep);
2472+
put_page(*pagep);
2473+
ret = -EIO;
2474+
}
2475+
2476+
return ret;
24682477
}
24692478

24702479
static int
@@ -2551,6 +2560,12 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
25512560
if (sgp == SGP_CACHE)
25522561
set_page_dirty(page);
25532562
unlock_page(page);
2563+
2564+
if (PageHWPoison(page)) {
2565+
put_page(page);
2566+
error = -EIO;
2567+
break;
2568+
}
25542569
}
25552570

25562571
/*
@@ -3112,14 +3127,20 @@ static const char *shmem_get_link(struct dentry *dentry,
31123127
page = find_get_page(inode->i_mapping, 0);
31133128
if (!page)
31143129
return ERR_PTR(-ECHILD);
3115-
if (!PageUptodate(page)) {
3130+
if (PageHWPoison(page) ||
3131+
!PageUptodate(page)) {
31163132
put_page(page);
31173133
return ERR_PTR(-ECHILD);
31183134
}
31193135
} else {
31203136
error = shmem_getpage(inode, 0, &page, SGP_READ);
31213137
if (error)
31223138
return ERR_PTR(error);
3139+
if (page && PageHWPoison(page)) {
3140+
unlock_page(page);
3141+
put_page(page);
3142+
return ERR_PTR(-ECHILD);
3143+
}
31233144
unlock_page(page);
31243145
}
31253146
set_delayed_call(done, shmem_put_link, page);
@@ -3770,6 +3791,13 @@ static void shmem_destroy_inodecache(void)
37703791
kmem_cache_destroy(shmem_inode_cachep);
37713792
}
37723793

3794+
/* Keep the page in page cache instead of truncating it */
3795+
static int shmem_error_remove_page(struct address_space *mapping,
3796+
struct page *page)
3797+
{
3798+
return 0;
3799+
}
3800+
37733801
const struct address_space_operations shmem_aops = {
37743802
.writepage = shmem_writepage,
37753803
.set_page_dirty = __set_page_dirty_no_writeback,
@@ -3780,7 +3808,7 @@ const struct address_space_operations shmem_aops = {
37803808
#ifdef CONFIG_MIGRATION
37813809
.migratepage = migrate_page,
37823810
#endif
3783-
.error_remove_page = generic_error_remove_page,
3811+
.error_remove_page = shmem_error_remove_page,
37843812
};
37853813
EXPORT_SYMBOL(shmem_aops);
37863814

@@ -4191,6 +4219,10 @@ struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
41914219
page = ERR_PTR(error);
41924220
else
41934221
unlock_page(page);
4222+
4223+
if (PageHWPoison(page))
4224+
page = ERR_PTR(-EIO);
4225+
41944226
return page;
41954227
#else
41964228
/*

mm/userfaultfd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ static int mcontinue_atomic_pte(struct mm_struct *dst_mm,
232232
goto out;
233233
}
234234

235+
if (PageHWPoison(page)) {
236+
ret = -EIO;
237+
goto out_release;
238+
}
239+
235240
ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
236241
page, false, wp_copy);
237242
if (ret)

0 commit comments

Comments
 (0)