Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60B3835C6B8 for ; Sat, 25 Jul 2026 16:01:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784995279; cv=none; b=LlFp0bttmFccyCvXpTBIBKJe2VJknWobPwWW9l5puuUbjBxVUQZBj/eXCDyYE/uY7moDckdsLuTjQq1PNlNAbke+r6Ui/oODFLOU8+8ZjA24LxzMwGMLduVVQKeJB1cOqiVSz9fYpMRpHXOSsa9rh83/A/bHwtr5OMUjUQX0um0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784995279; c=relaxed/simple; bh=AyQWgSo8XWTUphonEAuyYBrdrjkAJEvZbg2IOcYNkbs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MEIglgWsL2BSFXSXndRBD3agZaz1HAO8JDRwjq4WdLL+SmsJV1a3TdkXw2u+IEofnuUnPocITY7MXCt6gguMs6QPsO1PRKHzHjXiKLuK28Nq5PA3PkXuGWA8ceMWplc6lPrXg26MedQp28ZiHVI2rIHoDsJTy0R8u/rTyxHj4EE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=pass smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=r/gDQrZr; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="r/gDQrZr" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=xPkMEYO9mjH/Ia6rRtWcuKRSGQrQUxUN5Ze4piyRhiI=; b=r/gDQrZreIDyytDBd3FdRh/3yp p2ZpEovIwzfbFdOpy/79/s6Jque7baGRO8LSJkEw+mRoDlyCc2wNVacuL26Nb1mts+i0mJ5YwkEfN PHN6+Q4wufjwsZjXxqILPbvgrlZRqAYFP1nCs8oZCo+8PM3kDeDqzeRvolqC4hvBlQRUkDxMSNL57 x7rys1AVlpIKsG/1B5ZQtqZNQ1x/rMqO5Xx52B6Nw9CyunydfeH8vPyLAC2J/cRtaMnnAnd+js2h1 ySST+Muii3q81M1A16Fw6JbwSCTjKVqvVTbn84YJPxEjLAIWJEqc5GJp04MfygcQUXQvWSop1Oc1X WkMDl5Sg==; Received: from willy by casper.infradead.org with local (Exim 4.99.1 #2 (Red Hat Linux)) id 1wnens-00000006X7e-11Fr; Sat, 25 Jul 2026 16:00:44 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton , Jane Chu , linux-mm@kvack.org Cc: Muchun Song , Oscar Salvador , David Hildenbrand , Miaohe Lin , Naoya Horiguchi , Jan Kara , linux-fsdevel@vger.kernel.org, Christian Brauner , Jiaqi Yan , Matthew Wilcox Subject: [PATCH v5 08/12] filemap: Add hwpoison handling to filemap_read() Date: Sat, 25 Jul 2026 17:00:36 +0100 Message-ID: <20260725160042.1557264-9-willy@infradead.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260725160042.1557264-1-willy@infradead.org> References: <20260725160042.1557264-1-willy@infradead.org> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jane Chu Add hwpoison handling to filemap_read() such that .read_iter() could make best effort copying data out of clean pages without risking MCE in case page cache contains HWpoison. Signed-off-by: Jane Chu Co-developed-by: Matthew Wilcox Signed-off-by: Matthew Wilcox --- include/linux/hugetlb.h | 2 -- include/linux/page-flags.h | 11 +++++++++++ mm/filemap.c | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 325f0404a7eb..1878642e75ec 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -1069,8 +1069,6 @@ void hugetlb_register_node(struct node *node); void hugetlb_unregister_node(struct node *node); #endif -bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page); - static inline unsigned long huge_page_mask_align(struct file *file) { return PAGE_MASK & ~huge_page_mask(hstate_file(file)); diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index ee026ba9b0b5..07cc7854b2de 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -1094,6 +1094,7 @@ static inline bool PageHuge(const struct page *page) return folio_test_hugetlb(page_folio(page)); } +bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page); bool hugetlb_unref_page_hwpoison(const struct page *page); /* @@ -1115,6 +1116,16 @@ static inline bool is_page_hwpoison(const struct page *page) return PageHWPoison(page); } +static inline bool is_ref_page_hwpoison(const struct folio *folio, + const struct page *page) +{ + if (PageHWPoison(page)) + return true; + if (folio_test_hugetlb(folio)) + return hugetlb_page_hwpoison(folio, page); + return false; +} + static inline bool folio_has_hwpoisoned_page(const struct folio *folio) { return PageHWPoison(&folio->page) || diff --git a/mm/filemap.c b/mm/filemap.c index 58eb9d240643..26a5f18121f9 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2483,6 +2483,8 @@ static void filemap_get_read_batch(struct address_space *mapping, if (!folio_batch_add(fbatch, folio)) break; + if (folio_has_hwpoisoned_page(folio)) + break; if (!folio_test_uptodate(folio)) break; if (folio_test_readahead(folio)) @@ -2749,6 +2751,29 @@ static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) return (pos1 >> shift == pos2 >> shift); } +static size_t adjust_range_hwpoison(const struct folio *folio, size_t offset, + size_t bytes) +{ + const struct page *page = folio_page(folio, offset / PAGE_SIZE); + size_t safe_bytes; + + if (!folio_has_hwpoisoned_page(folio)) + return bytes; + if (is_ref_page_hwpoison(folio, page)) + return 0; + + /* Safe to read the remaining bytes in this page. */ + safe_bytes = PAGE_SIZE - (offset % PAGE_SIZE); + page++; + + /* Check each remaining page as long as we are not done yet. */ + for (; safe_bytes < bytes; safe_bytes += PAGE_SIZE, page++) + if (is_ref_page_hwpoison(folio, page)) + break; + + return min(safe_bytes, bytes); +} + static void filemap_end_dropbehind_read(struct folio *folio) { if (!folio_test_dropbehind(folio)) @@ -2862,14 +2887,18 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, if (writably_mapped) flush_dcache_folio(folio); - copied = copy_folio_to_iter(folio, offset, bytes, iter); + copied = adjust_range_hwpoison(folio, offset, bytes); + if (copied < bytes) + error = -EIO; + copied = copy_folio_to_iter(folio, offset, copied, iter); already_read += copied; iocb->ki_pos += copied; last_pos = iocb->ki_pos; if (copied < bytes) { - error = -EFAULT; + if (!error) + error = -EFAULT; break; } } -- 2.47.3