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 BC3CB3D170C for ; Thu, 9 Jul 2026 18:48:03 +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=1783622886; cv=none; b=W5kJgFj8z0EMqQguys/T6wM6iLTz0PaPcHhuBqvbELA8kSL94P7Ivbbtm5c5JVlKXU7iWNVUh9024zpgRzSkiPTme8vDuakgVegI78bMfm17SVP6MZomg/r0YCDUVUbkAkCDvB5sMUlAN/hGE36ExJcO0QBoamsyJm/hnAr9hoo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783622886; c=relaxed/simple; bh=w6SF7HV4Ory9T204NNdSjlEW93vAthun5of5/F/oUq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MiREvi214IRpv0nk5ni6qVGngBzPuZYOxx8DTTbtnYSKV+DKGKXugCWLQOKqIJfMhpBzp30oT2ENR9ULodaAP/IlAt54YO2TCUl7oQw+O/BH6bGpRhuQ7T0B980AWJI1a5fpTOPKSKLVcOrdmQ6SKDUrAj5wJywOAXXydzbPbjo= 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=csWvlU2V; 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="csWvlU2V" 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=hnmCCPOw+FiLEmua87c8Dvtylciuo3AiyETpU9dKhJ4=; b=csWvlU2VVhkzDQpFoJPHNv39kP Wjnzqoe1nJ7ziaVKYukZRLzzqK8hMvv9dQ7EzlVFHsUapL7Sxw5VBr4bY6pKM7jpTxGvuai0eEuMp cwCI0XXkl3ievI+XhcR6+XUXf84RRJMklg98Co5mwIavcg99gLcDhgADTPwU1AM8ZdC+c8oduTMhp 0Xn82gfQeEnLKSJlgOhnCr6TRCaDLQkjzwGX8GkPBsY/DCde31TPBVgqMK9M1TTjIka2eSGXo0RK2 B0M1o7FeSvG3N1kCkgU9aQWqz8YcioNWoor4SopQ5FCFybZHsqdIwpkBpGdtv8hNunuvhpKHkEMJr wNbvcUIw==; Received: from willy by casper.infradead.org with local (Exim 4.99.1 #2 (Red Hat Linux)) id 1whtmh-00000005OhP-0TFr; Thu, 09 Jul 2026 18:47:43 +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 , Matthew Wilcox Subject: [PATCH v2 08/10] filemap: Add hwpoison handling to filemap_read() Date: Thu, 9 Jul 2026 19:47:36 +0100 Message-ID: <20260709184740.1286561-9-willy@infradead.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260709184740.1286561-1-willy@infradead.org> References: <20260709184740.1286561-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 --- mm/filemap.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/mm/filemap.c b/mm/filemap.c index 5af62e6abca5..c568d267c803 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_page_hwpoison(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_page_hwpoison(page)) + break; + + return min(safe_bytes, bytes); +} + static void filemap_end_dropbehind_read(struct folio *folio) { if (!folio_test_dropbehind(folio)) @@ -2862,7 +2887,8 @@ 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); + copied = copy_folio_to_iter(folio, offset, copied, iter); already_read += copied; iocb->ki_pos += copied; -- 2.47.3