From patchwork Thu Sep 23 03:28:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12511951 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 874D9C433EF for ; Thu, 23 Sep 2021 03:28:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 38CF960F48 for ; Thu, 23 Sep 2021 03:28:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 38CF960F48 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 D63E194000A; Wed, 22 Sep 2021 23:28:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC60C940007; Wed, 22 Sep 2021 23:28:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BA4D94000A; Wed, 22 Sep 2021 23:28:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0190.hostedemail.com [216.40.44.190]) by kanga.kvack.org (Postfix) with ESMTP id 7FA84940007 for ; Wed, 22 Sep 2021 23:28:56 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 31D318249980 for ; Thu, 23 Sep 2021 03:28:56 +0000 (UTC) X-FDA: 78617406672.18.15C1A9E Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by imf19.hostedemail.com (Postfix) with ESMTP id F0980B0000A5 for ; Thu, 23 Sep 2021 03:28:55 +0000 (UTC) Received: by mail-pj1-f50.google.com with SMTP id t20so3477506pju.5 for ; Wed, 22 Sep 2021 20:28:55 -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=yW3mImt34w9s0m+L7odHrGN0UQd73DqRlDM0Drbnbq0=; b=jxB3fQZNu+GeaHBv00hPLH4J/m8FuWBxq/v04Bod2S2KuKkUeYinzZKA33aJn4nV/0 wSt+t1hRMs/h4kDb+9mdTwyuRRAD+ygz6UwNDb2YVRNaVIX0xYPauFfjupDOR7BkF1ft KKUrkPnL5zraJ2V0rKFf66I7fpV3KmP0u1ugczPuK70DnBTch04TH8idfgu0H0KM4QtX OOo/60N9FmBIlyxtlRRCOoNk5Mb9CdO8Hypzjw5z4OtRuWxW325Z+o6MMw8GJlHUgbLo WGpkDum8DJo8Pq7ihgq3wrNBy8ecrS7b27IQt5VTbTA9SI+437QpdIbSoeNSoa9DPOjG paWg== 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=yW3mImt34w9s0m+L7odHrGN0UQd73DqRlDM0Drbnbq0=; b=tlYnPTNzFdHlDtuIPDH3R7XZrcT57RVQe2KZhn09YLf3HPFJUcAsZdZtJT3ssDAMJC e59atLU5dSXdDccL469EjIeMnn7lg7Q7+Tx6ev6Ib6Is5mVccV42ELUAVqriwR1GJFrD AiEi6lOxfSHUxU3Rm5jGSGh2mPWjBShPAwTJtyCAYj8sJpBDtz6Jh9RZiy2W66VlPBRS zkrCCn5+tRQmRr0RebHPAW7QRHOaF8jr2BW+wFuQyX+x3foAU6Dx0o/hjJK+1kNc183x H41BJJgVgtc6Z/jXf0NNFLm2WS+Kfe4Q2QhUHk+iW/+VO136lWSOmXbULsUwJfXPSNwb hNzA== X-Gm-Message-State: AOAM530KHWuIlyVGEKXjS/snVR6IdOwOyb7612d6VghfCO0GDGgPk3Lp f/EUNudk04bFIM2l/PUXRMs= X-Google-Smtp-Source: ABdhPJw+bMrYlauFtFJ2kCM+HSaD4khflzoqdmQXG7VkN29NFSCdbk7y1zN5PWRYVxXfxUjEZApT5A== X-Received: by 2002:a17:90a:1a19:: with SMTP id 25mr15242818pjk.34.1632367735079; Wed, 22 Sep 2021 20:28:55 -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 x8sm3699696pfq.131.2021.09.22.20.28.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 20:28:54 -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: [v2 PATCH 4/5] mm: shmem: don't truncate page if memory failure happens Date: Wed, 22 Sep 2021 20:28:29 -0700 Message-Id: <20210923032830.314328-5-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210923032830.314328-1-shy828301@gmail.com> References: <20210923032830.314328-1-shy828301@gmail.com> MIME-Version: 1.0 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=jxB3fQZN; spf=pass (imf19.hostedemail.com: domain of shy828301@gmail.com designates 209.85.216.50 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: iat78ge9fcyhq6ppp8twrxgp1ijj6c9r X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: F0980B0000A5 X-HE-Tag: 1632367735-393111 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 | 31 +++++++++++++++++++++++++++++-- mm/userfaultfd.c | 5 +++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 5c7f1c2aabd9..3824bc708e55 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" @@ -866,6 +867,7 @@ static int me_pagecache_clean(struct page_state *ps, struct page *p) { int ret; struct address_space *mapping; + bool dec; delete_from_lru_cache(p); @@ -894,6 +896,12 @@ static int me_pagecache_clean(struct page_state *ps, struct page *p) goto out; } + /* + * The shmem page is kept in page cache instead of truncating + * so need decrement the refcount from page cache. + */ + dec = 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); - if (has_extra_refcount(ps, p, false)) + if (has_extra_refcount(ps, p, dec)) ret = MF_FAILED; return ret; diff --git a/mm/shmem.c b/mm/shmem.c index 88742953532c..75c36b6a405a 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 = mapping->host; struct shmem_inode_info *info = SHMEM_I(inode); pgoff_t index = pos >> PAGE_SHIFT; + int ret = 0; /* i_rwsem is held by caller */ if (unlikely(info->seals & (F_SEAL_GROW | @@ -2466,7 +2467,17 @@ shmem_write_begin(struct file *file, struct address_space *mapping, return -EPERM; } - return shmem_getpage(inode, index, pagep, SGP_WRITE); + ret = shmem_getpage(inode, index, pagep, SGP_WRITE); + + if (*pagep) { + if (PageHWPoison(*pagep)) { + unlock_page(*pagep); + put_page(*pagep); + ret = -EIO; + } + } + + return ret; } static int @@ -2555,6 +2566,11 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) unlock_page(page); } + if (page && PageHWPoison(page)) { + error = -EIO; + break; + } + /* * We must evaluate after, since reads (unlike writes) * are called without i_rwsem protection against truncate @@ -3772,6 +3788,13 @@ static void shmem_destroy_inodecache(void) kmem_cache_destroy(shmem_inode_cachep); } +/* 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 = { .writepage = shmem_writepage, .set_page_dirty = __set_page_dirty_no_writeback, @@ -3782,7 +3805,7 @@ const struct address_space_operations shmem_aops = { #ifdef CONFIG_MIGRATION .migratepage = migrate_page, #endif - .error_remove_page = generic_error_remove_page, + .error_remove_page = shmem_error_remove_page, }; EXPORT_SYMBOL(shmem_aops); @@ -4193,6 +4216,10 @@ struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, page = ERR_PTR(error); else unlock_page(page); + + if (PageHWPoison(page)) + page = 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 *dst_mm, goto out; } + if (PageHWPoison(page)) { + ret = -EIO; + goto out_release; + } + ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, page, false, wp_copy); if (ret)