From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18F04C433EF for ; Thu, 14 Oct 2021 19:16:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B5CC661156 for ; Thu, 14 Oct 2021 19:16:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B5CC661156 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5676794000B; Thu, 14 Oct 2021 15:16:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 517F0940008; Thu, 14 Oct 2021 15:16:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B83694000B; Thu, 14 Oct 2021 15:16:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0137.hostedemail.com [216.40.44.137]) by kanga.kvack.org (Postfix) with ESMTP id 255B8940008 for ; Thu, 14 Oct 2021 15:16:34 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id CD368267D7 for ; Thu, 14 Oct 2021 19:16:33 +0000 (UTC) X-FDA: 78695999466.08.8FE7250 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf04.hostedemail.com (Postfix) with ESMTP id DD36E50000BB for ; Thu, 14 Oct 2021 19:16:32 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id qe4-20020a17090b4f8400b0019f663cfcd1so7659027pjb.1 for ; Thu, 14 Oct 2021 12:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=i3w2W8bf8fjDmVl/a2MTya23w6Ml3a37fmUhHSxCWSw=; b=RN8XOpaKozLpSRy62DfljKxSu4xgRL4YHek5rUXyRHn42zeCBPjwrsm9JhrNHhl7x9 FtFHvs1XnE+gZB/WLoBaq/AOu5FDm2KMfT1vJ1fnLZ+07bO3MtuHGVaeeo0tYkKvJ+xu H2Jmq7qQwbwev/P3Zlyk4jHi6hWqxHz2VDjbbn6s/RsCL0m6IGlzTcYeR/VUKKn7mTK/ iDeA/Zs8AryWYJgGDdSMeG9wnqdAAkHvToH0f1k2+fR7MlT11ogBj2MNxmPRAfYBSjWc whyUyi8YgRvW2hWzE0lDx+GQ9qqjrjY/OiIkBvWftAAn2XgYhsstEez3gpw8uoUzyih1 B2Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=i3w2W8bf8fjDmVl/a2MTya23w6Ml3a37fmUhHSxCWSw=; b=KyryVsikd6FUFz1uDAb3Arb72wMtwihafQOd8U7cAVeeiSS06JOr1BpwQVBcIqbly3 32gq6neRSp9mR/wku6aeSnxZZPCNQyuTbb1z4HxqyJmRjdXUfYpe8suGPa365NU4t9bG UhOTd+47RzhVvuhD8MWvhWSFCUbL4JrK02QVjyNC5QpONwaps8NxzI0Tg+wwYGmI2jRI BmcXQ5NgoveQuSd0COpIGPb6ZF2xInB/irGGjBzjTp8vyAXQkUufkpaBPEo5W/J7HEFK +JU8ckZUihbnpsfIMWKjffAs2bF2xJBBdQFISWYBIaV4bc3D9og0XZTQ8lnIS72vAGtY Kk4g== X-Gm-Message-State: AOAM5320xn3vo1+pyufF3X8fq9IT2tpCmkewIswC5CLfwyZwpN9T3nes eAmb+umCnDkW8igVTzEZXRY= X-Google-Smtp-Source: ABdhPJzyYUg+gMPtvrV9ydY5GePka1BMotVN1zD+rEHkSnivupp6VjU8VlqL5uvi+6a727k3NtNjhQ== X-Received: by 2002:a17:90b:3ecc:: with SMTP id rm12mr8233928pjb.48.1634238992599; Thu, 14 Oct 2021 12:16:32 -0700 (PDT) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id x129sm3253922pfc.140.2021.10.14.12.16.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 12:16:31 -0700 (PDT) From: Yang Shi To: naoya.horiguchi@nec.com, hughd@google.com, kirill.shutemov@linux.intel.com, willy@infradead.org, peterx@redhat.com, osalvador@suse.de, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [v4 PATCH 5/6] mm: shmem: don't truncate page if memory failure happens Date: Thu, 14 Oct 2021 12:16:14 -0700 Message-Id: <20211014191615.6674-6-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20211014191615.6674-1-shy828301@gmail.com> References: <20211014191615.6674-1-shy828301@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: DD36E50000BB X-Stat-Signature: x1qkqt6axha6p5nkyf7dckuc5hayf7r3 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=RN8XOpaK; spf=pass (imf04.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1634238992-625345 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Signed-off-by: Yang Shi --- mm/memory-failure.c | 10 +++++++++- mm/shmem.c | 37 ++++++++++++++++++++++++++++++++++--- mm/userfaultfd.c | 5 +++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index cdf8ccd0865f..f5eab593b2a7 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -57,6 +57,7 @@ #include #include #include +#include #include "internal.h" #include "ras/ras_event.h" =20 @@ -866,6 +867,7 @@ static int me_pagecache_clean(struct page_state *ps, = struct page *p) { int ret; struct address_space *mapping; + bool extra_pins; =20 delete_from_lru_cache(p); =20 @@ -894,6 +896,12 @@ static int me_pagecache_clean(struct page_state *ps,= struct page *p) goto out; } =20 + /* + * The shmem page is kept in page cache instead of truncating + * so is expected to have an extra refcount after error-handling. + */ + extra_pins =3D shmem_mapping(mapping); + /* * Truncation is a bit tricky. Enable it per file system for now. * @@ -903,7 +911,7 @@ static int me_pagecache_clean(struct page_state *ps, = struct page *p) out: unlock_page(p); =20 - if (has_extra_refcount(ps, p, false)) + if (has_extra_refcount(ps, p, extra_pins)) ret =3D MF_FAILED; =20 return ret; diff --git a/mm/shmem.c b/mm/shmem.c index b5860f4a2738..69eaf65409e6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2456,6 +2456,7 @@ shmem_write_begin(struct file *file, struct address= _space *mapping, struct inode *inode =3D mapping->host; struct shmem_inode_info *info =3D SHMEM_I(inode); pgoff_t index =3D pos >> PAGE_SHIFT; + int ret =3D 0; =20 /* i_rwsem is held by caller */ if (unlikely(info->seals & (F_SEAL_GROW | @@ -2466,7 +2467,15 @@ shmem_write_begin(struct file *file, struct addres= s_space *mapping, return -EPERM; } =20 - return shmem_getpage(inode, index, pagep, SGP_WRITE); + ret =3D shmem_getpage(inode, index, pagep, SGP_WRITE); + + if (*pagep && PageHWPoison(*pagep)) { + unlock_page(*pagep); + put_page(*pagep); + ret =3D -EIO; + } + + return ret; } =20 static int @@ -2555,6 +2564,11 @@ static ssize_t shmem_file_read_iter(struct kiocb *= iocb, struct iov_iter *to) unlock_page(page); } =20 + if (page && PageHWPoison(page)) { + error =3D -EIO; + break; + } + /* * We must evaluate after, since reads (unlike writes) * are called without i_rwsem protection against truncate @@ -3114,7 +3128,8 @@ static const char *shmem_get_link(struct dentry *de= ntry, page =3D find_get_page(inode->i_mapping, 0); if (!page) return ERR_PTR(-ECHILD); - if (!PageUptodate(page)) { + if (PageHWPoison(page) || + !PageUptodate(page)) { put_page(page); return ERR_PTR(-ECHILD); } @@ -3122,6 +3137,11 @@ static const char *shmem_get_link(struct dentry *d= entry, error =3D shmem_getpage(inode, 0, &page, SGP_READ); if (error) return ERR_PTR(error); + if (page && PageHWPoison(page)) { + unlock_page(page); + put_page(page); + return ERR_PTR(-ECHILD); + } unlock_page(page); } set_delayed_call(done, shmem_put_link, page); @@ -3772,6 +3792,13 @@ static void shmem_destroy_inodecache(void) kmem_cache_destroy(shmem_inode_cachep); } =20 +/* Keep the page in page cache instead of truncating it */ +static int shmem_error_remove_page(struct address_space *mapping, + struct page *page) +{ + return 0; +} + const struct address_space_operations shmem_aops =3D { .writepage =3D shmem_writepage, .set_page_dirty =3D __set_page_dirty_no_writeback, @@ -3782,7 +3809,7 @@ const struct address_space_operations shmem_aops =3D= { #ifdef CONFIG_MIGRATION .migratepage =3D migrate_page, #endif - .error_remove_page =3D generic_error_remove_page, + .error_remove_page =3D shmem_error_remove_page, }; EXPORT_SYMBOL(shmem_aops); =20 @@ -4193,6 +4220,10 @@ struct page *shmem_read_mapping_page_gfp(struct ad= dress_space *mapping, page =3D ERR_PTR(error); else unlock_page(page); + + if (PageHWPoison(page)) + page =3D ERR_PTR(-EIO); + return page; #else /* diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 7a9008415534..b688d5327177 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -233,6 +233,11 @@ static int mcontinue_atomic_pte(struct mm_struct *ds= t_mm, goto out; } =20 + if (PageHWPoison(page)) { + ret =3D -EIO; + goto out_release; + } + ret =3D mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, page, false, wp_copy); if (ret) --=20 2.26.2