From patchwork Tue Sep 14 18:37:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12494425 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 1E2FCC433FE for ; Tue, 14 Sep 2021 18:37:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B279161107 for ; Tue, 14 Sep 2021 18:37:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B279161107 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 4FE4A900002; Tue, 14 Sep 2021 14:37:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 45F7D6B0075; Tue, 14 Sep 2021 14:37:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3005C900002; Tue, 14 Sep 2021 14:37:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0193.hostedemail.com [216.40.44.193]) by kanga.kvack.org (Postfix) with ESMTP id 178D46B0074 for ; Tue, 14 Sep 2021 14:37:39 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 918042DED9 for ; Tue, 14 Sep 2021 18:37:38 +0000 (UTC) X-FDA: 78587037396.18.E4B7475 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf26.hostedemail.com (Postfix) with ESMTP id 51DC120019DF for ; Tue, 14 Sep 2021 18:37:38 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id e7so63133pgk.2 for ; Tue, 14 Sep 2021 11:37:38 -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=9w1AJqNMCNIhFQPh+gH1tqRIw8tAS1H2alD3h2KH4Tg=; b=oRAhLcrLE1KArfHiZtQaKminXxHF840kk+E0maovq8zjG3QXSlhDRsv+IIvNS3f2IZ uh+mjaJolNp5iBSA2yQ02eKiMLlMLFGsXavyXNlZK2p6LAvoLs6KIYhlwVCfghAeaAbV +/kImpPmCqR95zWyPEvKBZVRBx8/VaTtgvZSTryBf4nr3/t/fL0j5DK2VIoQWyXMu3YZ OoBplmeZYginHwX2+luO+/ApvHdpmPCJuzGaUoRzov+C4blw3B6FCYVUV/8t43spurG5 8fUKSCJOMZ90yQpCxgCiMCEWM0jkumNjvJWyH3g1SIyzf5WTz0lyXs5JTXha8687MDtK /+0g== 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=9w1AJqNMCNIhFQPh+gH1tqRIw8tAS1H2alD3h2KH4Tg=; b=hoKjKTbKvsWXdoSB/nyha6ZK1hxp+Mmj98Q2Vu/JwQW8CCA7oy0rMJvkiLam5s5Ua4 RRU5/84SWVRwqNeNl7vrnhtlafhnKM/w5kLzJfpaLRYQ34Xz4N2XDqAQyFiPdzH1Gpmr CwSswWVnYOUdHMqeSRmu6dyzPZKVinzfBeQrIs3WSkG5aO7KMqAt66RpmcCr769AEJ6w D0WX0UwAlkx9rIQKoa2X3M60HCAFI03A2BtfpRgJFbsgn7xeI5itLImWjP9NyD4LZYhg xhjvWi9m0bjZhYUS+HKnbIivbpVsjMl0JYYACJexa4Fu6S0W9QQGdkFfS3A4Hd0oPmlh GUYw== X-Gm-Message-State: AOAM532oyFJhs8zk8Hhkru0fCgJK4tfwCP0fEcAK46bCnPKqlQ2MYkFS gRwT+7MGuOE3nJhzBYcvsGc= X-Google-Smtp-Source: ABdhPJzRie1QwXC8O0LrqqqozMmN656c59ZLcc2IjJ2ZadEETUI1oV5G1qGPgR0/qVKQhYJhTwjVtA== X-Received: by 2002:a63:62c7:: with SMTP id w190mr16518815pgb.105.1631644657522; Tue, 14 Sep 2021 11:37:37 -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 y3sm12003965pge.44.2021.09.14.11.37.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Sep 2021 11:37:36 -0700 (PDT) From: Yang Shi To: naoya.horiguchi@nec.com, hughd@google.com, kirill.shutemov@linux.intel.com, willy@infradead.org, 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: [PATCH 3/4] mm: shmem: don't truncate page if memory failure happens Date: Tue, 14 Sep 2021 11:37:17 -0700 Message-Id: <20210914183718.4236-4-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210914183718.4236-1-shy828301@gmail.com> References: <20210914183718.4236-1-shy828301@gmail.com> MIME-Version: 1.0 Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=oRAhLcrL; spf=pass (imf26.hostedemail.com: domain of shy828301@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=shy828301@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 51DC120019DF X-Stat-Signature: jgnfwrwzcab9g5acwk3fmbufijr6tx1m X-HE-Tag: 1631644658-417971 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 | 3 ++- mm/shmem.c | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index 54879c339024..3e06cb9d5121 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1101,7 +1101,8 @@ static int page_action(struct page_state *ps, struct page *p, result = ps->action(p, pfn); count = page_count(p) - 1; - if (ps->action == me_swapcache_dirty && result == MF_DELAYED) + if ((ps->action == me_swapcache_dirty && result == MF_DELAYED) || + (ps->action == me_pagecache_dirty && result == MF_FAILED)) count--; if (count > 0) { pr_err("Memory failure: %#lx: %s still referenced by %d users\n", diff --git a/mm/shmem.c b/mm/shmem.c index 88742953532c..ec33f4f7173d 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,19 @@ 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 (!ret) { + if (*pagep) { + if (PageHWPoison(*pagep)) { + unlock_page(*pagep); + put_page(*pagep); + ret = -EIO; + } + } + } + + return ret; } static int @@ -2555,6 +2568,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 @@ -3782,7 +3800,6 @@ const struct address_space_operations shmem_aops = { #ifdef CONFIG_MIGRATION .migratepage = migrate_page, #endif - .error_remove_page = generic_error_remove_page, }; EXPORT_SYMBOL(shmem_aops); @@ -4193,6 +4210,10 @@ struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, page = ERR_PTR(error); else unlock_page(page); + + if (PageHWPoison(page)) + page = NULL; + return page; #else /*