Skip to contentSkip to conversation

Use generic_file_read_iter() in hugetlbfs

PATCH Superseded Matthew Wilcox (Oracle) 5 participants 39 messages Jul 23 – Jul 25

Subsystems (14)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Matthew Wilcox (Oracle)

author maintainer
#0 lore raw

v4 cover Use generic_file_read_iter() in hugetlbfs

This set of patches are separated out from Jane's earlier series.
https://lore.kernel.org/linux-mm/[email protected]/

I've made a lot of changes over the last week; mostly with regard to hwpoison handling of hugetlb memory. Thanks to Miaohe Lin for suggesting using the hugetlb_lock to stablise the hugetlb folio.

v3 here:
https://lore.kernel.org/linux-mm/[email protected]/

Changes from v3:
- Fix unpoisoning a hugetlb folio
- Went back to hugetlb_hwpoison in the folio instead of having a global list of poisoned pages
- Rename CONFIG_FILEMAP_LARGE_FOLIO to CONFIG_LARGE_FOLIO
- Added missing call to mapping_set_authoritative()
- Fixed polarity of check in mapping_set_folio_order_range()
- Added is_ref_page_hwpoison() so we don't need to do the extra work of handling an unreferenced folio
- Use the has_hwpoisoned() flag for hugetlb as well as THP
- Added folio_set_huge_poison() so we can detect unreferenced hwpoisoned hugetlb folios reliably

Jane Chu (2):
filemap: Add hwpoison handling to filemap_read()
hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()

Matthew Wilcox (Oracle) (12):
memory-failure: Fix hardware poison check in unpoison_memory() again
memory-failure: Test the page is hwpoison before taking the mutex
mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()
hugetlb: Mark some function arguments as const
guest_memfd: Use folio_has_hwpoisoned_page()
memory-failure: Remove raw_hwp_list_head()
hugetlb: Use the has_hwpoisoned flag
mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()
mm: Check individual hugetlb pages for poison
filemap: Remove checks in mapping_set_folio_order_range()
hugetlb: Set mapping folio order
filemap: Add support for authoritative mappings

 fs/Kconfig                 |   2 +-
 fs/hugetlbfs/inode.c       | 123 ++++---------------------------
 include/linux/hugetlb.h    |  24 +++---
 include/linux/mm_types.h   |   4 +-
 include/linux/page-flags.h |  59 ++++++++++++---
 include/linux/pagemap.h    |  45 +++++++-----
 mm/Kconfig                 |   6 +-
 mm/filemap.c               |  51 ++++++++++++-
 mm/huge_memory.c           |   2 +-
 mm/hugetlb.c               |  16 ++--
 mm/memory-failure.c        | 146 +++++++++++++++++++++++--------------
 mm/memory_hotplug.c        |   2 +-
 mm/shmem.c                 |   2 +-
 mm/vmscan.c                |   2 +-
 virt/kvm/guest_memfd.c     |   2 +-
 15 files changed, 258 insertions(+), 228 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v4 1/14 memory-failure: Fix hardware poison check in unpoison_memory() again

The earlier patch (6c54312f9689) that attempted to fix unpoison_memory() was incorrect for hugetlb folios. Before a6fddef49eef, we checked the head page for poison (which was correct for hugetlb and incorrect for THP). We are currently incapable of unpoisoning pages (other than the first page) in a hugetlb folio.

Use is_page_hwpoison() which handles hugetlb pages sufficiently well for this purpose. This is racy as we don't have a reference on the folio at this point, but fixing that properly requires deeper surgery and this is a CAP_SYS_ADMIN path only so this will do for the moment and can be easily backported. It's no worse than the situation before a6fddef49eef.

The other bug in a6fddef49eef is that we currently clear the HWPoison flag on the precise page, which is wrong for hugetlb. Fix that too.

Fixes a6fddef49eef ("mm/memory-failure: convert unpoison_memory() to folios")
Cc Naoya Horiguchi <[email protected]>
Cc Miaohe Lin <[email protected]>
---
 mm/memory-failure.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/memory-failure.c +3 -2
index 51508a55c405..944e6e1d4971 100644
@@ -2695,8 +2695,8 @@ int unpoison_memory(unsigned long pfn)
 		goto unlock_mutex;
 	}
 
-	if (!PageHWPoison(p)) {
-		unpoison_pr_info("%#lx: page was already unpoisoned\n",
+	if (!is_page_hwpoison(p)) {
+		unpoison_pr_info("%#lx: page is not poisoned\n",
 				 pfn, &unpoison_rs);
 		goto unlock_mutex;
 	}
@@ -2748,6 +2748,7 @@ int unpoison_memory(unsigned long pfn)
 				folio_put(folio);
 				goto unlock_mutex;
 			}
+			p = &folio->page;
 		}
 
 		folio_put(folio);
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

Re: [PATCH v4 01/14] memory-failure: Fix hardware poison check in unpoison_memory() again

Sashiko feedback:

Quoted 42 lines

--- Patch [1]: [PATCH v4 01/14] memory-failure: Fix hardware poison check in unpoison_memory() again --- commit 5a43cce770d0c716bb5c78ee2c21e1cb711e5331
Author: Matthew Wilcox (Oracle) <[email protected]>

memory-failure: Fix hardware poison check in unpoison_memory() again

This commit addresses a bug in unpoison_memory() for hugetlb folios. It uses is_page_hwpoison() instead of checking the head page, and correctly targets the precise page for clearing the HWPoison flag.

Quoted 5 lines
mm/memory-failure.c +0 -0
index 51508a55c4055..944e6e1d49719 100644

mm/memory-failure.c int unpoison_memory(unsigned long pfn) · line 2702

Quoted 10 lines
mm/memory-failure.c +2 -2
@@ -2695,8 +2695,8 @@ int unpoison_memory(unsigned long pfn)
 		goto unlock_mutex;
 	}
 
-	if (!PageHWPoison(p)) {
-		unpoison_pr_info("%#lx: page was already unpoisoned\n",
+	if (!is_page_hwpoison(p)) {
+		unpoison_pr_info("%#lx: page is not poisoned\n",
 				 pfn, &unpoison_rs);
 		goto unlock_mutex;
 	}

This isn't a bug introduced by this patch, but can the speculative folio access without stabilization cause a use-after-free and reference leak?

Looking at unpoison_memory(), if the PFN belongs to a THP tail page and the THP is concurrently split, could the folio pointer refer to a stale head page?

unpoison_memory() {
        ...
        folio = page_folio(p);
        ...
        ghp = get_hwpoison_page(p, MF_UNPOISON);
        ...
        folio_put(folio);
}
Quoted 3 lines

Since get_hwpoison_page() takes a reference on the new order-0 page, does calling folio_put() on the stale head page drop the wrong reference and leak the reference on the order-0 page?

Uh. I don't know. If it does, I didn't make this problem _worse_, so I'm not going to address it.

Quoted 10 lines

This is a pre-existing issue, but does unpoison_memory() clear the PG_hwpoison flag on the wrong page for unsplit THPs?

For THP tail pages, if get_hwpoison_page() returns 0 because their _refcount is 0, does the code fall into the !ghp branch and call folio_test_clear_hwpoison(folio)?

Because PF_HEAD redirects to the head page, this clears the flag on the head page. Since THPs set PG_hwpoison on the exact corrupted tail page (PF_ANY), does the tail page remain poisoned and fail to restore?

Hm. Maybe? This code probably needs to be more careful in general.

Quoted 14 lines

[ ... ]

mm/memory-failure.c int unpoison_memory(unsigned long pfn) · line 2754

Quoted 8 lines
mm/memory-failure.c +1 -0
@@ -2748,6 +2748,7 @@ int unpoison_memory(unsigned long pfn)
 				folio_put(folio);
 				goto unlock_mutex;
 			}
+			p = &folio->page;
 		}
 
 		folio_put(folio);

This is a pre-existing issue, but does unpoison_memory() fail to clear the per-folio PG_has_hwpoisoned flag when unpoisoning THPs?

Quoted 7 lines

When successfully unpoisoning a THP head page (where get_hwpoison_page succeeds), TestClearPageHWPoison() clears the per-page PG_hwpoison flag.

However, since the per-folio fast-indicator PG_has_hwpoisoned is never cleared, will folio_contain_hwpoisoned_page() continue to return true, causing the kernel to erroneously treat the unpoisoned THP as poisoned in future operations?

Yes, it does fail to clear the has_hwpoisoned flag. We can have more than one HWPoison page in a folio, so we either have to leave it set, or scan all pages in the folio to know that it's safe to clear.

Again, I'm not making this worse for THP in this series, so I'm not addressing it. For hugetlb pages, later on I change to use the has_hwpoisoned flag, and that does get cleared because there's no scan of page bits involved, just a list of poisoned pages, and it's easy to check when that's empty.

It's mostly moot because we always try to split large folios that aren't THP, and that usually succeeds. So all the large folio has_hwpoisoned paths are very undertested.

author maintainer
#3 lore raw

PATCH v4 2/14 memory-failure: Test the page is hwpoison before taking the mutex

In reply to Matthew Wilcox (Oracle)

We don't need to synchronize with memory failure to filter out pages which are not hwpoisoned. It's inherently racy, so it doesn't matter.

---
 mm/memory-failure.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
mm/memory-failure.c +8 -7
index 944e6e1d4971..3615ced0a11b 100644
@@ -2677,7 +2677,12 @@ int unpoison_memory(unsigned long pfn)
 	p = pfn_to_online_page(pfn);
 	if (!p)
 		return -EIO;
-	folio = page_folio(p);
+
+	if (!is_page_hwpoison(p)) {
+		unpoison_pr_info("%#lx: page is not poisoned\n",
+				 pfn, &unpoison_rs);
+		return -EBUSY;
+	}
 
 	mutex_lock(&mf_mutex);
 
@@ -2688,6 +2693,8 @@ int unpoison_memory(unsigned long pfn)
 		goto unlock_mutex;
 	}
 
+	folio = page_folio(p);
+
 	if (is_huge_zero_folio(folio)) {
 		unpoison_pr_info("%#lx: huge zero page is not supported\n",
 				 pfn, &unpoison_rs);
@@ -2695,12 +2702,6 @@ int unpoison_memory(unsigned long pfn)
 		goto unlock_mutex;
 	}
 
-	if (!is_page_hwpoison(p)) {
-		unpoison_pr_info("%#lx: page is not poisoned\n",
-				 pfn, &unpoison_rs);
-		goto unlock_mutex;
-	}
-
 	if (folio_ref_count(folio) > 1) {
 		unpoison_pr_info("%#lx: someone grabs the hwpoison page\n",
 				 pfn, &unpoison_rs);
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

Re: [PATCH v4 02/14] memory-failure: Test the page is hwpoison before taking the mutex

Sashiko review:

Quoted 51 lines

--- Patch [2]: [PATCH v4 02/14] memory-failure: Test the page is hwpoison before taking the mutex --- commit 1cca344401408425fcfd4ca129165f85229f9227
Author: Matthew Wilcox (Oracle) <[email protected]>

memory-failure: Test the page is hwpoison before taking the mutex

This patch moves the is_page_hwpoison() check in unpoison_memory() outside of mf_mutex. This avoids synchronizing with memory failure just to filter out pages which are not hwpoisoned.

Quoted 28 lines
mm/memory-failure.c +8 -1
index 944e6e1d49719..3615ced0a11bc 100644
@@ -2677,7 +2677,12 @@ int unpoison_memory(unsigned long pfn)
 	p = pfn_to_online_page(pfn);
 	if (!p)
 		return -EIO;
-	folio = page_folio(p);
+
+	if (!is_page_hwpoison(p)) {
+		unpoison_pr_info("%#lx: page is not poisoned\n",
+				 pfn, &unpoison_rs);
+		return -EBUSY;
+	}
 
 	mutex_lock(&mf_mutex);
 
@@ -2688,6 +2693,8 @@ int unpoison_memory(unsigned long pfn)
 		goto unlock_mutex;
 	}
 
+	folio = page_folio(p);
+
 	if (is_huge_zero_folio(folio)) {
 		unpoison_pr_info("%#lx: huge zero page is not supported\n",
 				 pfn, &unpoison_rs);

mm/memory-failure.c int unpoison_memory(unsigned long pfn) · line 2703

Quoted 9 lines
mm/memory-failure.c +0 -6
@@ -2695,12 +2702,6 @@ int unpoison_memory(unsigned long pfn)
 		goto unlock_mutex;
 	}
 
-	if (!is_page_hwpoison(p)) {
-		unpoison_pr_info("%#lx: page is not poisoned\n",
-				 pfn, &unpoison_rs);
-		goto unlock_mutex;
-	}
-

Does removing this check from inside the mutex introduce a time-of-check to time-of-use race?

Quoted 12 lines

If two threads call unpoison_memory() concurrently on the same PFN, both could pass the lockless check. Thread A takes the lock, unpoisons the page, and the page is returned to the buddy allocator.

When Thread B acquires the lock, it will now proceed without re-checking is_page_hwpoison(). If the page was concurrently reallocated as a compound tail page, the speculative folio_ref_count() or folio_test_slab() checks below could trigger a VM_BUG_ON_PGFLAGS crash.

Quoted 3 lines
mm/memory-failure.c +0 -0
 	if (folio_ref_count(folio) > 1) {
 		unpoison_pr_info("%#lx: someone grabs the hwpoison page\n",
 				 pfn, &unpoison_rs);

Uh. Yeah. I wasn't thinking about two simultaneous calls to unpoison_memory(). I'll just drop this patch; it wasn't important.

author maintainer
#5 lore raw

PATCH v4 3/14 mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()

In reply to Matthew Wilcox (Oracle)

This is a better name for this predicate. Also make the argument const to allow other callers to be made const. No functional change.

---
 include/linux/page-flags.h | 4 ++--
 mm/huge_memory.c           | 2 +-
 mm/memory_hotplug.c        | 2 +-
 mm/shmem.c                 | 2 +-
 mm/vmscan.c                | 2 +-
 5 files changed, 6 insertions(+), 6 deletions(-)
include/linux/page-flags.h +2 -2
index 7223f6f4e2b4..4185a03a45cf 100644
@@ -1083,10 +1083,10 @@ static inline bool is_page_hwpoison(const struct page *page)
 	return folio_test_hugetlb(folio) && PageHWPoison(&folio->page);
 }
 
-static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
+static inline bool folio_has_hwpoisoned_page(const struct folio *folio)
 {
 	return folio_test_hwpoison(folio) ||
-	    (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
+	       (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
 }
 
 bool is_free_buddy_page(const struct page *page);
mm/huge_memory.c +1 -1
index 2bccb0a53a0a..353340309107 100644
@@ -4413,7 +4413,7 @@ static bool thp_underused(struct folio *folio)
 	if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
 		return false;
 
-	if (folio_contain_hwpoisoned_page(folio))
+	if (folio_has_hwpoisoned_page(folio))
 		return false;
 
 	for (i = 0; i < folio_nr_pages(folio); i++) {
mm/memory_hotplug.c +1 -1
index 7ac19fab2263..33007e8e546a 100644
@@ -1813,7 +1813,7 @@ static void do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
 		if (folio_test_large(folio))
 			pfn = folio_pfn(folio) + folio_nr_pages(folio) - 1;
 
-		if (folio_contain_hwpoisoned_page(folio)) {
+		if (folio_has_hwpoisoned_page(folio)) {
 			/*
 			 * unmap_poisoned_folio() cannot handle large folios
 			 * in all cases yet.
mm/shmem.c +1 -1
index b51f83c970bb..492c9bef3620 100644
@@ -3240,7 +3240,7 @@ shmem_write_begin(const struct kiocb *iocb, struct address_space *mapping,
 	if (ret)
 		return ret;
 
-	if (folio_contain_hwpoisoned_page(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return -EIO;
mm/vmscan.c +1 -1
index 35c3bb15ae96..70e54ace0e74 100644
@@ -1086,7 +1086,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 		if (!folio_trylock(folio))
 			goto keep;
 
-		if (folio_contain_hwpoisoned_page(folio)) {
+		if (folio_has_hwpoisoned_page(folio)) {
 			/*
 			 * unmap_poisoned_folio() can't handle large
 			 * folio, just skip it. memory_failure() will
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

Re: [PATCH v4 03/14] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()

Sashiko review:

Quoted 40 lines

--- Patch [3]: [PATCH v4 03/14] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page() --- commit 82722dc6d8a8b8030006cbc0fae2e5f36ec21fa1
Author: Matthew Wilcox (Oracle) <[email protected]>

mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()

This commit renames folio_contain_hwpoison_page() to folio_has_hwpoison_page() for better naming clarity. It also makes the argument const to allow other callers to be made const, with no functional change.

include/linux/page-flags.h static inline bool is_page_hwpoison(const struct page *page) · line 1084

Quoted 7 lines
include/linux/page-flags.h +0 -0
index 7223f6f4e2b40..4185a03a45cf7 100644
@@ -1083,10 +1083,10 @@ static inline bool is_page_hwpoison(const struct page *page)
 	return folio_test_hugetlb(folio) && PageHWPoison(&folio->page);
 }

This is a pre-existing issue, but while reviewing the surrounding context I noticed a speculative folio access here.

When a privileged process reads /proc/kcore, read_kcore_iter() in fs/proc/kcore.c iterates over PFNs and calls is_page_hwpoison() on raw page pointers without holding a reference:

fs/proc/kcore.c:read_kcore_iter() {
	...
	page = pfn_to_online_page(pfn);
	if (!page || PageOffline(page) ||
	    is_page_hwpoison(page) || !pfn_is_ram(pfn) ||
	...
}

Inside is_page_hwpoison(), the page is converted to a folio and its flags are accessed immediately:

	const struct folio *folio = page_folio(page);
	return folio_test_hugetlb(folio) && ...
Quoted 6 lines

Since the folio is unreferenced, could this cause a kernel panic via VM_BUG_ON_PGFLAGS(PageTail(page), page) in const_folio_flags() if the compound page is split or freed concurrently?

Should this path stabilize the folio with folio_try_get() and re-validate before checking folio_test_hugetlb()?

This is fixed later in the patch series. Bad bot, no cookie.

#7 lore raw

Re: [PATCH v4 03/14] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()

In reply to Matthew Wilcox (Oracle)
Quoted 19 lines · Matthew Wilcox (Oracle)

This is a better name for this predicate. Also make the argument const to allow other callers to be made const. No functional change.

---
 include/linux/page-flags.h | 4 ++--
 mm/huge_memory.c           | 2 +-
 mm/memory_hotplug.c        | 2 +-
 mm/shmem.c                 | 2 +-
 mm/vmscan.c                | 2 +-
 5 files changed, 6 insertions(+), 6 deletions(-)
include/linux/page-flags.h +0 -0
index 7223f6f4e2b4..4185a03a45cf 100644
@@ -1083,10 +1083,10 @@ static inline bool is_page_hwpoison(const struct page *page)
 	return folio_test_hugetlb(folio) && PageHWPoison(&folio->page);

include/linux/page-flags.h static inline bool is_page_hwpoison(const struct page *page) · line 1091

Quoted 10 lines
include/linux/page-flags.h +2 -2
 }
 
-static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
+static inline bool folio_has_hwpoisoned_page(const struct folio *folio)
 {
 	return folio_test_hwpoison(folio) ||
-	    (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
+	       (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
 }
 

It probably makes sense to document when this should be used in preference to folio_test_hwpoison, given a lot of people got this wrong?

Quoted 56 lines
include/linux/page-flags.h +0 -0
 bool is_free_buddy_page(const struct page *page);
mm/huge_memory.c +1 -1
index 2bccb0a53a0a..353340309107 100644
@@ -4413,7 +4413,7 @@ static bool thp_underused(struct folio *folio)
 	if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
 		return false;
 
-	if (folio_contain_hwpoisoned_page(folio))
+	if (folio_has_hwpoisoned_page(folio))
 		return false;
 
 	for (i = 0; i < folio_nr_pages(folio); i++) {
mm/memory_hotplug.c +1 -1
index 7ac19fab2263..33007e8e546a 100644
@@ -1813,7 +1813,7 @@ static void do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)
 		if (folio_test_large(folio))
 			pfn = folio_pfn(folio) + folio_nr_pages(folio) - 1;
 
-		if (folio_contain_hwpoisoned_page(folio)) {
+		if (folio_has_hwpoisoned_page(folio)) {
 			/*
 			 * unmap_poisoned_folio() cannot handle large folios
 			 * in all cases yet.
mm/shmem.c +1 -1
index b51f83c970bb..492c9bef3620 100644
@@ -3240,7 +3240,7 @@ shmem_write_begin(const struct kiocb *iocb, struct address_space *mapping,
 	if (ret)
 		return ret;
 
-	if (folio_contain_hwpoisoned_page(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return -EIO;
mm/vmscan.c +1 -1
index 35c3bb15ae96..70e54ace0e74 100644
@@ -1086,7 +1086,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 		if (!folio_trylock(folio))
 			goto keep;
 
-		if (folio_contain_hwpoisoned_page(folio)) {
+		if (folio_has_hwpoisoned_page(folio)) {
 			/*
 			 * unmap_poisoned_folio() can't handle large
 			 * folio, just skip it. memory_failure() will
Signature · 1 line
2.47.3
author maintainer
#8 lore raw

Re: [PATCH v4 03/14] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()

Quoted 4 lines · Michael S. Tsirkin
Quoted 4 lines
include/linux/page-flags.h +1 -1
-static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
+static inline bool folio_has_hwpoisoned_page(const struct folio *folio)
 {
 	return folio_test_hwpoison(folio) ||
Quoted 8 lines

include/linux/page-flags.h static inline bool is_page_hwpoison(const struct page *page) · line 1090

Quoted 4 lines
include/linux/page-flags.h +1 -1
-	    (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
+	       (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
 }

It probably makes sense to document when this should be used in preference to folio_test_hwpoison, given a lot of people got this wrong?

folio_test_hwpoison() / folio_set_hwpoison() / folio_clear_hwpoison() need to stop existing. I know how to do it, but it's a lot of churn to page-flags.h. Not something for this series, but it's on my radar.

author maintainer
#9 lore raw

PATCH v4 4/14 hugetlb: Mark some function arguments as const

In reply to Matthew Wilcox (Oracle)

These functions do not modify their folio argument, so we can mark them as being const which will allow for some minor optimisations and let us mark other function arguments as being const in the future.

---
 include/linux/hugetlb.h | 19 +++++++++----------
 mm/hugetlb.c            |  2 +-
 2 files changed, 10 insertions(+), 11 deletions(-)
include/linux/hugetlb.h +9 -10
index 2abaf99321e9..18661a7cdb37 100644
@@ -165,7 +165,7 @@ bool hugetlbfs_pagecache_present(struct hstate *h,
 				 struct vm_area_struct *vma,
 				 unsigned long address);
 
-struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio);
+struct address_space *hugetlb_folio_mapping_lock_write(const struct folio *folio);
 
 extern int movable_gigantic_pages __read_mostly;
 extern int sysctl_hugetlb_shm_group __read_mostly;
@@ -292,8 +292,7 @@ static inline unsigned long hugetlb_total_pages(void)
 	return 0;
 }
 
-static inline struct address_space *hugetlb_folio_mapping_lock_write(
-							struct folio *folio)
+static inline struct address_space *hugetlb_folio_mapping_lock_write(const struct folio *folio)
 {
 	return NULL;
 }
@@ -595,8 +594,8 @@ enum hugetlb_page_flags {
 #ifdef CONFIG_HUGETLB_PAGE
 #define TESTHPAGEFLAG(uname, flname)				\
 static __always_inline						\
-bool folio_test_hugetlb_##flname(struct folio *folio)		\
-	{	void *private = &folio->private;		\
+bool folio_test_hugetlb_##flname(const struct folio *folio)	\
+	{	const void *private = &folio->private;		\
 		return test_bit(HPG_##flname, private);		\
 	}
 
@@ -616,7 +615,7 @@ void folio_clear_hugetlb_##flname(struct folio *folio)		\
 #else
 #define TESTHPAGEFLAG(uname, flname)				\
 static inline bool						\
-folio_test_hugetlb_##flname(struct folio *folio)		\
+folio_test_hugetlb_##flname(const struct folio *folio)		\
 	{ return 0; }
 
 #define SETHPAGEFLAG(uname, flname)				\
@@ -727,7 +726,7 @@ static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
 	return HUGETLBFS_SB(inode->i_sb)->spool;
 }
 
-static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
+static inline struct hugepage_subpool *hugetlb_folio_subpool(const struct folio *folio)
 {
 	return folio->_hugetlb_subpool;
 }
@@ -857,7 +856,7 @@ static inline bool arch_has_huge_bootmem_alloc(void)
 }
 #endif
 
-static inline struct hstate *folio_hstate(struct folio *folio)
+static inline struct hstate *folio_hstate(const struct folio *folio)
 {
 	VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
 	return size_to_hstate(folio_size(folio));
@@ -1088,7 +1087,7 @@ static inline unsigned long huge_page_mask_align(struct file *file)
 	return 0;
 }
 
-static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
+static inline struct hugepage_subpool *hugetlb_folio_subpool(const struct folio *folio)
 {
 	return NULL;
 }
@@ -1157,7 +1156,7 @@ static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
 	return NULL;
 }
 
-static inline struct hstate *folio_hstate(struct folio *folio)
+static inline struct hstate *folio_hstate(const struct folio *folio)
 {
 	return NULL;
 }
mm/hugetlb.c +1 -1
index 571212b80835..cc8d98399913 100644
@@ -1775,7 +1775,7 @@ void init_new_hugetlb_folio(struct folio *folio)
  * stable.  Due to locking order, we can only trylock_write.  If we can
  * not get the lock, simply return NULL to caller.
  */
-struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio)
+struct address_space *hugetlb_folio_mapping_lock_write(const struct folio *folio)
 {
 	struct address_space *mapping = folio_mapping(folio);
 
Signature · 1 line
2.47.3
author maintainer
#10 lore raw

Re: [PATCH v4 04/14] hugetlb: Mark some function arguments as const

Quoted 6 lines · Matthew Wilcox (Oracle)

These functions do not modify their folio argument, so we can mark them as being const which will allow for some minor optimisations and let us mark other function arguments as being const in the future.

Sashiko finds no issues with this patch.

author maintainer
#11 lore raw

PATCH v4 5/14 guest_memfd: Use folio_has_hwpoisoned_page()

In reply to Matthew Wilcox (Oracle)

folio_test_hwpoison() only works on hugetlb folios today and will give the wrong answer for other large folios. folio_has_hwpoisoned_page() works on both.

---
 virt/kvm/guest_memfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/guest_memfd.c +1 -1
index db57c5766ab6..94eedcc7f1a9 100644
@@ -774,7 +774,7 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
 	if (IS_ERR(folio))
 		return folio;
 
-	if (folio_test_hwpoison(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return ERR_PTR(-EHWPOISON);
Signature · 1 line
2.47.3
author maintainer
#12 lore raw

Re: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()

Sashiko review:

Quoted 40 lines

--- Patch [5]: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page() --- commit b4db4098463261fdf87a2e92b28ce2b4c9445e74
Author: Matthew Wilcox (Oracle) <[email protected]>
guest_memfd: Use folio_has_hwpoisoned_page()

This commit changes guest_memfd to use folio_has_hwpoisoned_page() instead of folio_test_hwpoison() to correctly detect poisoned pages on all large folios, not just hugetlb folios.

virt/kvm/guest_memfd.c static struct folio *__kvm_gmem_get_pfn(struct file *file, · line 780

Quoted 13 lines
virt/kvm/guest_memfd.c +1 -1
index db57c5766ab60..94eedcc7f1a9e 100644
@@ -774,7 +774,7 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
 	if (IS_ERR(folio))
 		return folio;
 
-	if (folio_test_hwpoison(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return ERR_PTR(-EHWPOISON);

This is a pre-existing issue, but while looking at hwpoison checks in guest_memfd.c, I noticed a missing check in kvm_gmem_fault_user_mapping() that might trigger a host kernel MCE panic.

If a guest_memfd folio is allocated (e.g. via fallocate) but not zeroed, it remains in the page cache without the uptodate flag. If a memory failure occurs on this page, it is marked hwpoisoned.

Later, a fault could trigger kvm_gmem_fault_user_mapping() which executes:

    folio = kvm_gmem_get_folio(inode, vmf->pgoff);
    ...
    if (!folio_test_uptodate(folio)) {
        clear_highpage(folio_page(folio, 0));
        folio_mark_uptodate(folio);
    }
Quoted 6 lines

Because there is no folio_has_hwpoisoned_page() check before clearing the page, could the kernel-mode write in clear_highpage() to the uninitialized, poisoned page trigger an unrecoverable MCE panic?

Should kvm_gmem_fault_user_mapping() include the same HWPoison check as __kvm_gmem_get_pfn() and return VM_FAULT_HWPOISON if the page is poisoned?

I have no idea, but the purpose of this patch is to stop kvm from using the wrong API. This is definitely scope creep beyond my level of interest and the aims of this patch series. I've added the KVM people to the cc here, but I'm not going to do anything more with this review.

#13 lore raw

Re: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()

Quoted 45 lines · Matthew Wilcox

Sashiko review:

Quoted 44 lines

--- Patch [5]: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page() --- commit b4db4098463261fdf87a2e92b28ce2b4c9445e74
Author: Matthew Wilcox (Oracle) <[email protected]>
guest_memfd: Use folio_has_hwpoisoned_page()

This commit changes guest_memfd to use folio_has_hwpoisoned_page() instead of folio_test_hwpoison() to correctly detect poisoned pages on all large folios, not just hugetlb folios.

virt/kvm/guest_memfd.c static struct folio *__kvm_gmem_get_pfn(struct file *file, · line 780

Quoted 13 lines
virt/kvm/guest_memfd.c +1 -1
index db57c5766ab60..94eedcc7f1a9e 100644
@@ -774,7 +774,7 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
 	if (IS_ERR(folio))
 		return folio;
 
-	if (folio_test_hwpoison(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return ERR_PTR(-EHWPOISON);

This is a pre-existing issue, but while looking at hwpoison checks in guest_memfd.c, I noticed a missing check in kvm_gmem_fault_user_mapping() that might trigger a host kernel MCE panic.

If a guest_memfd folio is allocated (e.g. via fallocate) but not zeroed, it remains in the page cache without the uptodate flag. If a memory failure occurs on this page, it is marked hwpoisoned.

Later, a fault could trigger kvm_gmem_fault_user_mapping() which executes:

    folio = kvm_gmem_get_folio(inode, vmf->pgoff);
    ...
    if (!folio_test_uptodate(folio)) {
        clear_highpage(folio_page(folio, 0));
        folio_mark_uptodate(folio);
    }

Because there is no folio_has_hwpoisoned_page() check before clearing the page, could the kernel-mode write in clear_highpage() to the uninitialized, poisoned page trigger an unrecoverable MCE panic?

Quoted 7 lines
Quoted 2 lines

Should kvm_gmem_fault_user_mapping() include the same HWPoison check as __kvm_gmem_get_pfn() and return VM_FAULT_HWPOISON if the page is poisoned?

I have no idea, but the purpose of this patch is to stop kvm from using the wrong API. This is definitely scope creep beyond my level of interest and the aims of this patch series. I've added the KVM people to the cc here, but I'm not going to do anything more with this review.

Heh, you don't want to sign up for a week or two of onion peeling?

In the future, please Cc at least kvm@ on any guest_memfd changes, both to avoid unexpected conflicts (the code is under heavy development), and because us KVM people are still learning how the hell do filesystems stuff, and can use all the help and knowledge sharing we can get.

Thanks!

#14 lore raw

Re: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()

Quoted 49 lines · Sean Christopherson
Quoted 48 lines · Matthew Wilcox

Sashiko review:

Quoted 46 lines

--- Patch [5]: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page() --- commit b4db4098463261fdf87a2e92b28ce2b4c9445e74
Author: Matthew Wilcox (Oracle) <[email protected]>
guest_memfd: Use folio_has_hwpoisoned_page()

This commit changes guest_memfd to use folio_has_hwpoisoned_page() instead of folio_test_hwpoison() to correctly detect poisoned pages on all large folios, not just hugetlb folios.

virt/kvm/guest_memfd.c static struct folio *__kvm_gmem_get_pfn(struct file *file, · line 780

Quoted 13 lines
virt/kvm/guest_memfd.c +1 -1
index db57c5766ab60..94eedcc7f1a9e 100644
@@ -774,7 +774,7 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
 	if (IS_ERR(folio))
 		return folio;
 
-	if (folio_test_hwpoison(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return ERR_PTR(-EHWPOISON);

This is a pre-existing issue, but while looking at hwpoison checks in guest_memfd.c, I noticed a missing check in kvm_gmem_fault_user_mapping() that might trigger a host kernel MCE panic.

If a guest_memfd folio is allocated (e.g. via fallocate) but not zeroed, it remains in the page cache without the uptodate flag. If a memory failure occurs on this page, it is marked hwpoisoned.

Later, a fault could trigger kvm_gmem_fault_user_mapping() which executes:

    folio = kvm_gmem_get_folio(inode, vmf->pgoff);
    ...
    if (!folio_test_uptodate(folio)) {
        clear_highpage(folio_page(folio, 0));
        folio_mark_uptodate(folio);
    }

Because there is no folio_has_hwpoisoned_page() check before clearing the page, could the kernel-mode write in clear_highpage() to the uninitialized, poisoned page trigger an unrecoverable MCE panic?

Should kvm_gmem_fault_user_mapping() include the same HWPoison check as __kvm_gmem_get_pfn() and return VM_FAULT_HWPOISON if the page is poisoned?

Quoted 7 lines
Quoted 4 lines

I have no idea, but the purpose of this patch is to stop kvm from using the wrong API. This is definitely scope creep beyond my level of interest and the aims of this patch series. I've added the KVM people to the cc here, but I'm not going to do anything more with this review.

Heh, you don't want to sign up for a week or two of onion peeling?

Looks like Sashiko is right, we should fix this separately.

Quoted 3 lines

[...snip...]

author maintainer
#15 lore raw

Re: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()

In reply to Sean Christopherson
Quoted 1 line · Sean Christopherson

Heh, you don't want to sign up for a week or two of onion peeling?

Oh, you have no idea!

Here's my onion so far ...

1. I want to simplify filemap_fault()
2. That requires changing all the architecture fault handlers
3. So let's push some of that down to the MM with a new wrapper around handle_mm_fault()
4. This would be easier if we could hoist the creation of struct vm_fault from __handle_mm_fault() to handle_mm_fault()
5. Which would be possible if Jane's series to fix hugetlb use of pgoff had landed
6. Oh, hang on, handling hwpoison on hugetlb is actually broken in a few different ways (some pointed out by Sashiko, some I noticed myself)

I honestly thought I was just going to rearrange Jane's patch series a bit and resubmit it, but fixing the hugetlb hwpoison handling has consumed me for the past couple of weeks.

#16 lore raw

Re: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()

Quoted 19 lines · Matthew Wilcox
Quoted 1 line · Sean Christopherson

Heh, you don't want to sign up for a week or two of onion peeling?

Oh, you have no idea!

Here's my onion so far ...

1. I want to simplify filemap_fault()
2. That requires changing all the architecture fault handlers
3. So let's push some of that down to the MM with a new wrapper around handle_mm_fault()
4. This would be easier if we could hoist the creation of struct vm_fault from __handle_mm_fault() to handle_mm_fault()
5. Which would be possible if Jane's series to fix hugetlb use of pgoff had landed
6. Oh, hang on, handling hwpoison on hugetlb is actually broken in a few different ways (some pointed out by Sashiko, some I noticed myself)

Quoted 3 lines

I honestly thought I was just going to rearrange Jane's patch series a bit and resubmit it, but fixing the hugetlb hwpoison handling has consumed me for the past couple of weeks.

Last time I tried to fix hwpoison, i wasted untold hours on it, only to decide that it was always racy and does not seem to be worth fixing. At least yours isn't a race.

Signature · 1 line
MST
Reviewed-by on v4 5/14
#18 lore raw

Re: [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()

In reply to Matthew Wilcox (Oracle)
Quoted 23 lines · Matthew Wilcox (Oracle)

folio_test_hwpoison() only works on hugetlb folios today and will give the wrong answer for other large folios. folio_has_hwpoisoned_page() works on both.

---
 virt/kvm/guest_memfd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/guest_memfd.c +1 -2
index db57c5766ab6..94eedcc7f1a9 100644
@@ -774,7 +774,7 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
 	if (IS_ERR(folio))
 		return folio;
 
-	if (folio_test_hwpoison(folio)) {
+	if (folio_has_hwpoisoned_page(folio)) {
 		folio_unlock(folio);
 		folio_put(folio);
 		return ERR_PTR(-EHWPOISON);
--
Quoted 1 line

2.47.3

author maintainer
#19 lore raw

PATCH v4 6/14 memory-failure: Remove raw_hwp_list_head()

In reply to Matthew Wilcox (Oracle)

There's no need to hide the llist_head; we can just declare it directly in struct folio and do away with raw_hwp_list_head().

---
 include/linux/mm_types.h |  4 ++--
 mm/memory-failure.c      | 19 +++++--------------
 2 files changed, 7 insertions(+), 16 deletions(-)
include/linux/mm_types.h +2 -2
index b18c2b2e7d2c..d7a9b0d8bbdf 100644
@@ -387,7 +387,7 @@ typedef unsigned short mm_id_t;
  * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h.
  * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h.
  * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h.
- * @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head().
+ * @hugetlb_hwpoison: List of pages with hwpoison.
  * @_deferred_list: Folios to be split under memory pressure.
  * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab.
  *
@@ -499,7 +499,7 @@ struct folio {
 			void *_hugetlb_subpool;
 			void *_hugetlb_cgroup;
 			void *_hugetlb_cgroup_rsvd;
-			void *_hugetlb_hwpoison;
+			struct llist_head hugetlb_hwpoison;
 	/* private: the union with struct page is transitional */
 		};
 		struct page __page_3;
mm/memory-failure.c +5 -14
index 3615ced0a11b..18691c905ac9 100644
@@ -1810,21 +1810,15 @@ EXPORT_SYMBOL_GPL(mf_dax_kill_procs);
 
 /*
  * Struct raw_hwp_page represents information about "raw error page",
- * constructing singly linked list from ->_hugetlb_hwpoison field of folio.
+ * constructing singly linked list from folio->hugetlb_hwpoison field.
  */
 struct raw_hwp_page {
 	struct llist_node node;
 	struct page *page;
 };
 
-static inline struct llist_head *raw_hwp_list_head(struct folio *folio)
-{
-	return (struct llist_head *)&folio->_hugetlb_hwpoison;
-}
-
 bool is_raw_hwpoison_page_in_hugepage(struct page *page)
 {
-	struct llist_head *raw_hwp_head;
 	struct raw_hwp_page *p;
 	struct folio *folio = page_folio(page);
 	bool ret = false;
@@ -1844,8 +1838,7 @@ bool is_raw_hwpoison_page_in_hugepage(struct page *page)
 
 	mutex_lock(&mf_mutex);
 
-	raw_hwp_head = raw_hwp_list_head(folio);
-	llist_for_each_entry(p, raw_hwp_head->first, node) {
+	llist_for_each_entry(p, folio->hugetlb_hwpoison.first, node) {
 		if (page == p->page) {
 			ret = true;
 			break;
@@ -1863,7 +1856,7 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
 	struct raw_hwp_page *p, *next;
 	unsigned long count = 0;
 
-	head = llist_del_all(raw_hwp_list_head(folio));
+	head = llist_del_all(&folio->hugetlb_hwpoison);
 	llist_for_each_entry_safe(p, next, head, node) {
 		if (move_flag)
 			SetPageHWPoison(p->page);
@@ -1887,7 +1880,6 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
  */
 static int hugetlb_update_hwpoison(struct folio *folio, struct page *page)
 {
-	struct llist_head *head;
 	struct raw_hwp_page *raw_hwp;
 	struct raw_hwp_page *p;
 	int ret = folio_test_set_hwpoison(folio) ? MF_HUGETLB_FOLIO_PRE_POISONED : 0;
@@ -1899,8 +1891,7 @@ static int hugetlb_update_hwpoison(struct folio *folio, struct page *page)
 	 */
 	if (folio_test_hugetlb_raw_hwp_unreliable(folio))
 		return MF_HUGETLB_FOLIO_PRE_POISONED;
-	head = raw_hwp_list_head(folio);
-	llist_for_each_entry(p, head->first, node) {
+	llist_for_each_entry(p, folio->hugetlb_hwpoison.first, node) {
 		if (p->page == page)
 			return MF_HUGETLB_PAGE_PRE_POISONED;
 	}
@@ -1908,7 +1899,7 @@ static int hugetlb_update_hwpoison(struct folio *folio, struct page *page)
 	raw_hwp = kmalloc_obj(struct raw_hwp_page, GFP_ATOMIC);
 	if (raw_hwp) {
 		raw_hwp->page = page;
-		llist_add(&raw_hwp->node, head);
+		llist_add(&raw_hwp->node, &folio->hugetlb_hwpoison);
 	} else {
 		/*
 		 * Failed to save raw error info.  We no longer trace all
Signature · 1 line
2.47.3
author maintainer
#20 lore raw

Re: [PATCH v4 06/14] memory-failure: Remove raw_hwp_list_head()

Quoted 2 lines · Matthew Wilcox (Oracle)

There's no need to hide the llist_head; we can just declare it directly in struct folio and do away with raw_hwp_list_head().

Sashiko finds no issues with this patch.

author maintainer
#21 lore raw

PATCH v4 7/14 hugetlb: Use the has_hwpoisoned flag

In reply to Matthew Wilcox (Oracle)

Other large folios use the has_hwpoisoned flag. Convert hugetlb to match. This will help us use the per-page hwpoison flag in the future.

Also introduce a folio_test_huge_poison(). This has exactly the same meaning as folio_test_has_hwpoisoned() but can be used when we don't have a reference to the folio containing the page. folio_test_has_hwpoisoned() can race with folio splitting / reallocation and trip the assertions in const_folio_flags().

This closes a gap where a page in a previously-poisoned hugetlb folio could be observed to not have the hwpoison bit set.

---
 fs/Kconfig                 |  2 +-
 fs/hugetlbfs/inode.c       |  2 +-
 include/linux/page-flags.h | 42 +++++++++++++++++++++++------
 mm/Kconfig                 |  6 ++++-
 mm/hugetlb.c               | 14 +++++-----
 mm/memory-failure.c        | 55 +++++++++++++++++++++++++++-----------
 6 files changed, 87 insertions(+), 34 deletions(-)
fs/Kconfig +1 -1
index cf6ae64776e6..eddac4ed214b 100644
@@ -272,7 +272,7 @@ endif # HUGETLBFS
 
 config HUGETLB_PAGE
 	def_bool HUGETLBFS
-	select XARRAY_MULTI
+	select LARGE_FOLIO
 
 config HUGETLB_PAGE_OPTIMIZE_VMEMMAP
 	def_bool HUGETLB_PAGE
fs/hugetlbfs/inode.c +1 -1
index 216e1a0dd0b2..fbac554886c3 100644
@@ -258,7 +258,7 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		} else {
 			folio_unlock(folio);
 
-			if (!folio_test_hwpoison(folio))
+			if (!folio_test_has_hwpoisoned(folio))
 				want = nr;
 			else {
 				/*
include/linux/page-flags.h +34 -8

77 lines not on this page · raw message

mm/Kconfig +5 -1
index 9e0ca4824905..e666dd14ca0c 100644
@@ -843,11 +843,15 @@ config PERSISTENT_HUGE_ZERO_FOLIO
 config MM_ID
 	def_bool n
 
+config LARGE_FOLIO
+	def_bool n
+	select XARRAY_MULTI
+
 menuconfig TRANSPARENT_HUGEPAGE
 	bool "Transparent Hugepage Support"
 	depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT
 	select COMPACTION
-	select XARRAY_MULTI
+	select LARGE_FOLIO
 	select MM_ID
 	help
 	  Transparent Hugepages allows the kernel to use huge pages and
mm/hugetlb.c +7 -7
index cc8d98399913..aaac6e2af4c2 100644
@@ -1255,7 +1255,7 @@ static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,
 		if (pin && !folio_is_longterm_pinnable(folio))
 			continue;
 
-		if (folio_test_hwpoison(folio))
+		if (folio_test_has_hwpoisoned(folio))
 			continue;
 
 		if (is_migrate_isolate_page(&folio->page))
@@ -1498,10 +1498,10 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
 	}
 
 	/*
-	 * Move PageHWPoison flag from head page to the raw error pages,
-	 * which makes any healthy subpages reusable.
+	 * Move HWPoison flag to each error page
+	 * which makes any healthy pages reusable.
 	 */
-	if (unlikely(folio_test_hwpoison(folio)))
+	if (unlikely(folio_test_has_hwpoisoned(folio)))
 		folio_clear_hugetlb_hwpoison(folio);
 
 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
@@ -3998,7 +3998,7 @@ long demote_pool_huge_page(struct hstate *src, nodemask_t *nodes_allowed,
 		struct folio *folio, *next;
 
 		list_for_each_entry_safe(folio, next, &src->hugepage_freelists[node], lru) {
-			if (folio_test_hwpoison(folio))
+			if (folio_test_has_hwpoisoned(folio))
 				continue;
 
 			remove_hugetlb_folio(src, folio, false);
@@ -5809,7 +5809,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
 		 * don't have hwpoisoned swap entry for errored virtual address.
 		 * So we need to block hugepage fault by PG_hwpoison bit check.
 		 */
-		if (unlikely(folio_test_hwpoison(folio))) {
+		if (unlikely(folio_test_has_hwpoisoned(folio))) {
 			ret = VM_FAULT_HWPOISON_LARGE |
 				VM_FAULT_SET_HINDEX(hstate_index(h));
 			goto backout_unlocked;
@@ -6318,7 +6318,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
 	ptl = huge_pte_lock(h, dst_mm, dst_pte);
 
 	ret = -EIO;
-	if (folio_test_hwpoison(folio))
+	if (folio_test_has_hwpoisoned(folio))
 		goto out_release_unlock;
 
 	ret = -EEXIST;
mm/memory-failure.c +39 -16

103 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#22 lore raw

Re: [PATCH v4 07/14] hugetlb: Use the has_hwpoisoned flag

Sashiko review:

Quoted 51 lines

--- Patch [7]: [PATCH v4 07/14] hugetlb: Use the has_hwpoisoned flag --- commit f67cbd0b3ba7a67258490d423b9fc8352cb972aa
Author: Matthew Wilcox (Oracle) <[email protected]>

hugetlb: Use the has_hwpoisoned flag

Other large folios use the has_hwpoisoned flag. Convert hugetlb to match. This will help us use the per-page hwpoison flag in the future.

Quoted 4 lines
mm/memory-failure.c +0 -0
index 18691c905ac94..4200deab2c3c4 100644

[ ... ]

mm/memory-failure.c static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag) · line 1884

Quoted 12 lines
mm/memory-failure.c +8 -0
@@ -1874,6 +1874,23 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
 #define	MF_HUGETLB_FOLIO_PRE_POISONED	3	/* folio already poisoned */
 #define	MF_HUGETLB_PAGE_PRE_POISONED	4	/* exact page already poisoned */
 #define	MF_HUGETLB_RETRY		5	/* hugepage is busy, retry */
+
+static inline int hugetlb_set_poison(struct folio *folio)
+{
+	if (folio_test_set_has_hwpoisoned(folio))
+		return MF_HUGETLB_FOLIO_PRE_POISONED;
+	folio_set_huge_poison(folio);
+	return 0;
+}

By replacing folio_test_set_hwpoison(folio) and no longer propagating PG_hwpoison to the head page, does this break memory failure unmapping and silently bypass hwpoison swap entry creation?

If a hardware memory error occurs on a tail page of a hugetlb folio, memory_failure() calls TestSetPageHWPoison(p) on the exact tail page. It then calls hugetlb_set_poison(), which sets PG_has_hwpoisoned but no longer sets PG_hwpoison on the head page.

When unmapping the folio in try_to_unmap_one() in mm/rmap.c, the code extracts the base PFN from the hugetlb PTE, calculates subpage as the head page, and checks PageHWPoison(subpage):

mm/rmap.c:try_to_unmap_one() {
    ...
    VM_BUG_ON_PAGE(!PageHWPoison(subpage), subpage);
    ...
    if (PageHWPoison(subpage) && (flags & TTU_HWPOISON)) {
        pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
    ...
}
Quoted 4 lines

Since PG_hwpoison is no longer on the head page, this check fails, triggering VM_BUG_ON_PAGE. Without DEBUG_VM, the code skips inserting the hwpoison swap entry, simply clearing the PTE, which silently allocates a new zeroed page on subsequent accesses instead of delivering SIGBUS.

Oh; this is a good catch. I have now reworked try_to_unmap() to handle hugetlb pages a bit better and it'll be part of this patch in v5.

Quoted 23 lines

mm/memory-failure.c static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag) · line 1892

Quoted 8 lines
mm/memory-failure.c +8 -0
+
+static inline int hugetlb_clear_poison(struct folio *folio)
+{
+	if (!folio_test_clear_has_hwpoisoned(folio))
+		return -EBUSY;
+	folio_clear_huge_poison(folio);
+	return 0;
+}

Does this sequence open a race window with concurrent hugetlb freeing that results in memory corruption?

When unpoison_memory() is called on a free hugetlb folio, hugetlb_clear_poison() atomically clears PG_has_hwpoisoned first. This exposes the folio to concurrent hugetlb demotion (demote_pool_huge_page()), which can pick up the folio and call __update_and_free_hugetlb_folio(). This function sets folio->page.page_type = UINT_MAX to return it to the buddy allocator.

Concurrently, hugetlb_clear_poison() proceeds to execute folio_clear_huge_poison() (defined in include/linux/page-flags.h), which performs a non-atomic read-modify-write:

Quoted 7 lines
include/linux/page-flags.h:folio_clear_huge_poison() {
    folio->page.page_type &= ~(1 << 23);
}

This non-atomic write can overwrite the buddy allocator's UINT_MAX with 0xFF7FFFFF. When the buddy allocator receives the page, it expects page_type == UINT_MAX and will crash.

I'm not sure that it can be demoted while we're holding a reference to the folio. But I think it makes sense to change thusly:

+static inline int hugetlb_clear_poison(struct folio *folio) +{
+ if (!folio_test_has_hwpoisoned(folio))
+ return -EBUSY;
+ folio_clear_huge_poison(folio);
+ folio_clear_has_hwpoisoned(folio));
+ return 0;
+}

(looking at Jiaqi's patches, maybe we shouldn't be clearing has_hwpoisoned before freeing the folio?)

author maintainer
#23 lore raw

PATCH v4 8/14 mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()

In reply to Matthew Wilcox (Oracle)

We don't need the mutex to check the list, as long as we use READ_ONCE() to access the list head. Updates only add to the head of the list, so we can miss new entries, but not old ones. It's perfectly acceptable to miss new entries as callers cannot tell which order the add and check executed in anyway.

Rename is_raw_hwpoison_page_in_hugepage() to hugetlb_page_hwpoison() and make it take the folio (since the callers naturally have the folio). Also remove the handling of non-hugetlb folios and make the arguments const.

---
 fs/hugetlbfs/inode.c    |  4 ++--
 include/linux/hugetlb.h |  5 +----
 mm/memory-failure.c     | 31 ++++++++++++-------------------
 3 files changed, 15 insertions(+), 25 deletions(-)
fs/hugetlbfs/inode.c +2 -2
index fbac554886c3..dbca3f713bbf 100644
@@ -198,7 +198,7 @@ static size_t adjust_range_hwpoison(struct folio *folio, size_t offset,
 	struct page *page = folio_page(folio, offset / PAGE_SIZE);
 	size_t safe_bytes;
 
-	if (is_raw_hwpoison_page_in_hugepage(page))
+	if (hugetlb_page_hwpoison(folio, page))
 		return 0;
 	/* Safe to read the remaining bytes in this page. */
 	safe_bytes = PAGE_SIZE - (offset % PAGE_SIZE);
@@ -206,7 +206,7 @@ static size_t adjust_range_hwpoison(struct folio *folio, size_t offset,
 
 	/* Check each remaining page as long as we are not done yet. */
 	for (; safe_bytes < bytes; safe_bytes += PAGE_SIZE, page++)
-		if (is_raw_hwpoison_page_in_hugepage(page))
+		if (hugetlb_page_hwpoison(folio, page))
 			break;
 
 	return min(safe_bytes, bytes);
include/linux/hugetlb.h +1 -4
index 18661a7cdb37..325f0404a7eb 100644
@@ -1069,10 +1069,7 @@ void hugetlb_register_node(struct node *node);
 void hugetlb_unregister_node(struct node *node);
 #endif
 
-/*
- * Check if a given raw @page in a hugepage is HWPOISON.
- */
-bool is_raw_hwpoison_page_in_hugepage(struct page *page);
+bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page);
 
 static inline unsigned long huge_page_mask_align(struct file *file)
 {
mm/memory-failure.c +12 -19
index 4200deab2c3c..5076ee5d4788 100644
@@ -1817,37 +1817,30 @@ struct raw_hwp_page {
 	struct page *page;
 };
 
-bool is_raw_hwpoison_page_in_hugepage(struct page *page)
+/*
+ * Check if a given @page in a hugetlb folio is HWPOISON.
+ */
+bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page)
 {
-	struct raw_hwp_page *p;
-	struct folio *folio = page_folio(page);
-	bool ret = false;
+	const struct llist_head *list = &folio->hugetlb_hwpoison;
+	const struct raw_hwp_page *p;
 
 	if (!folio_test_has_hwpoisoned(folio))
 		return false;
 
-	if (!folio_test_hugetlb(folio))
-		return PageHWPoison(page);
-
 	/*
-	 * When RawHwpUnreliable is set, kernel lost track of which subpages
-	 * are HWPOISON. So return as if ALL subpages are HWPOISONed.
+	 * When RawHwpUnreliable is set, kernel lost track of which pages
+	 * are HWPOISON. So return as if ALL pages are HWPOISONed.
 	 */
 	if (folio_test_hugetlb_raw_hwp_unreliable(folio))
 		return true;
 
-	mutex_lock(&mf_mutex);
-
-	llist_for_each_entry(p, folio->hugetlb_hwpoison.first, node) {
-		if (page == p->page) {
-			ret = true;
-			break;
-		}
+	llist_for_each_entry(p, READ_ONCE(list->first), node) {
+		if (page == p->page)
+			return true;
 	}
 
-	mutex_unlock(&mf_mutex);
-
-	return ret;
+	return false;
 }
 
 static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
Signature · 1 line
2.47.3
author maintainer
#24 lore raw

Re: [PATCH v4 08/14] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()

Sashiko review:

Quoted 66 lines

--- Patch [8]: [PATCH v4 08/14] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage() --- commit c0f68557b399585852271ea0bc6a590caa47b3ad
Author: Matthew Wilcox (Oracle) <[email protected]>

mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()

This commit removes the mf_mutex protection from the raw hwpoison list iteration in hugetlb_page_hwpoison(), relying on lockless llist traversal using READ_ONCE(). It also renames the function and modifies it to take a folio.

Quoted 4 lines
mm/memory-failure.c +0 -0
index 4200deab2c3c4..5076ee5d4788d 100644

[ ... ]

Quoted 10 lines
mm/memory-failure.c +4 -1
@@ -1817,37 +1817,30 @@ struct raw_hwp_page {
 	struct page *page;
 };
 
-bool is_raw_hwpoison_page_in_hugepage(struct page *page)
+/*
+ * Check if a given @page in a hugetlb folio is HWPOISON.
+ */
+bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page)
 {

[ ... ]

mm/memory-failure.c struct raw_hwp_page { · line 1841

Quoted 11 lines
mm/memory-failure.c +3 -7
-	mutex_lock(&mf_mutex);
-
-	llist_for_each_entry(p, folio->hugetlb_hwpoison.first, node) {
-		if (page == p->page) {
-			ret = true;
-			break;
-		}
+	llist_for_each_entry(p, READ_ONCE(list->first), node) {
+		if (page == p->page)
+			return true;
 	}

Is the assumption in the commit message that updates only add to the head of the list accurate?

Looking at __folio_free_raw_hwp() in mm/memory-failure.c, the entire list can be concurrently detached and its nodes freed via kfree() without an RCU grace period:

    head = llist_del_all(&folio->hugetlb_hwpoison);
    llist_for_each_entry_safe(p, next, head, node) {
        ...
        kfree(p);
    }

This freeing can happen while the folio is still in active use. For example, if a subsequent memory failure on the same folio fails a GFP_ATOMIC allocation in hugetlb_update_hwpoison():

    } else {
        folio_set_hugetlb_raw_hwp_unreliable(folio);
        __folio_free_raw_hwp(folio, false);
    }

Since callers like hugetlbfs_read_iter() explicitly drop the folio lock before calling adjust_range_hwpoison() (which triggers this lockless traversal), could a concurrent memory failure free these list nodes while this loop is still accessing them, leading to a use-after-free?

Quoted 5 lines

mm/memory-failure.c struct raw_hwp_page { · line 1844

Quoted 5 lines
mm/memory-failure.c +1 -3
-	mutex_unlock(&mf_mutex);
-
-	return ret;
+	return false;
 }

argh. I give up on this cleverness. I'm going with the new spinlock approach (like I did in v3) but retaining the per-folio list of pages rather than having a global hash table.

author maintainer
#25 lore raw

PATCH v4 9/14 mm: Check individual hugetlb pages for poison

In reply to Matthew Wilcox (Oracle)

So far, is_page_hwpoison() has returned true for the entire poisoned hugetlb folio, but we can usually allow access to the pages which aren't poisoned. It's slightly tricky due to not having a reference to the folio, but the hugetlb lock can save us here.

---
 include/linux/page-flags.h |  4 +++-
 mm/memory-failure.c        | 43 +++++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 8 deletions(-)
include/linux/page-flags.h +3 -1
index 7c4652dd517a..9cde74320e48 100644
@@ -1090,6 +1090,8 @@ static inline bool PageHuge(const struct page *page)
 	return folio_test_hugetlb(page_folio(page));
 }
 
+bool hugetlb_unref_page_hwpoison(const struct page *page);
+
 /*
  * Check if a page is currently marked HWPoisoned.  This check is best
  * effort only and inherently racy: there is no way to synchronize with
@@ -1104,7 +1106,7 @@ static inline bool is_page_hwpoison(const struct page *page)
 		return true;
 	folio = page_folio(page);
 	if (folio_test_huge_poison(folio))
-		return true;
+		return hugetlb_unref_page_hwpoison(page);
 	/* In case we raced with hugetlb transferring flags */
 	return PageHWPoison(page);
 }
mm/memory-failure.c +36 -7

59 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#26 lore raw

Re: [PATCH v4 09/14] mm: Check individual hugetlb pages for poison

Sashiko review:

Quoted 30 lines

--- Patch [9]: [PATCH v4 09/14] mm: Check individual hugetlb pages for poison --- commit 5da9009498dbc7d3d1fea29d7065847731ac20dc
Author: Matthew Wilcox (Oracle) <[email protected]>
Subject: mm: Check individual hugetlb pages for poison

This commit modifies the hugetlb hardware poison checking logic to verify individual subpages instead of the entire folio. It introduces a new function to safely perform this check without holding a folio reference by relying on the hugetlb_lock.

Link N/A
Quoted 4 lines
include/linux/page-flags.h +0 -0
index 7c4652dd517a3..9cde74320e48a 100644

[ ... ]

include/linux/page-flags.h static inline bool PageHuge(const struct page *page) · line 1093

Quoted 5 lines
include/linux/page-flags.h +1 -0
@@ -1090,6 +1090,8 @@ static inline bool PageHuge(const struct page *page)
 	return folio_test_hugetlb(page_folio(page));
 }
 
+bool hugetlb_unref_page_hwpoison(const struct page *page);

Does this cause a build failure when CONFIG_MEMORY_FAILURE is not set?

When CONFIG_MEMORY_FAILURE=n, mm/memory-failure.c isn't compiled, meaning hugetlb_unref_page_hwpoison() is not defined.

Quoted 6 lines

Since is_page_hwpoison() is an inline function that evaluates a runtime condition when CONFIG_HUGETLB_PAGE=y, the compiler can't optimize the branch away.

Will this result in an undefined reference to hugetlb_unref_page_hwpoison() for callers like fs/proc/kcore.c?

Oh, right, need to define FOLIO_TEST_FLAG_FALSE(huge_poison) if CONFIG_MEMORY_FAILURE isn't defined.

Quoted 10 lines

include/linux/page-flags.h static inline bool is_page_hwpoison(const struct page *page) · line 1107

Quoted 3 lines
include/linux/page-flags.h +0 -0
@@ -1104,7 +1106,7 @@ static inline bool is_page_hwpoison(const struct page *page)
 		return true;
 	folio = page_folio(page);

This is a pre-existing issue, but can this speculative folio access cause a kernel panic?

In PFN-scanning contexts like /proc/kcore, is_page_hwpoison() is called without holding a stabilizing reference.

Quoted 6 lines

It calls page_folio() speculatively and reads flags via folio_test_huge_poison().

If the page is concurrently freed and reallocated to a non-compound user (like SLAB) that stores data in the compound_head field, page_folio() will return a garbage pointer.

... what? Slab is a good compound page user. You're drinking the bad bot juice again.

Quoted 12 lines

This isn't a bug introduced by this patch, but could the non-atomic modification of the poison flag corrupt the buddy allocator?

unpoison_memory() calls hugetlb_clear_poison(folio) which performs a non-atomic read-modify-write:

    folio->page.page_type &= ~(1 << 23);

When get_hwpoison_page() returns 0 for a free hugetlb folio, unpoison_memory() does not hold a reference. Concurrently, the folio can be freed to the buddy allocator.

Quoted 6 lines

If the non-atomic modification in unpoison_memory() races with __folio_clear_hugetlb() or the buddy allocator's __SetPageBuddy(), it can overwrite their updates.

For example, if it overwrites the buddy flag, PageBuddy() becomes false for a page on the buddy list, crashing the allocator when accessed.

I think it re-found the same problem it complained about earlier.

Quoted 8 lines
Quoted 8 lines
include/linux/page-flags.h +8 -0
+bool hugetlb_unref_page_hwpoison(const struct page *page)
+{
+	const struct folio *folio;
+	unsigned long flags;
+	bool ret;
+
+	spin_lock_irqsave(&hugetlb_lock, flags);
+	folio = page_folio(page);
Quoted 8 lines

include/linux/page-flags.h

Quoted 6 lines
include/linux/page-flags.h +6 -0
+	if (!folio_test_huge_poison(folio)) {
+		ret = PageHWPoison(page);
+		goto unlock;
+	}
+
+	ret = precise_page_poisoned(folio, page);

Can this list traversal result in a use-after-free?

As earlier, I'll protect this with a spinlock.

author maintainer
#27 lore raw

PATCH v4 10/14 filemap: Add hwpoison handling to filemap_read()

In reply to Matthew Wilcox (Oracle)

From: Jane Chu <[email protected]>

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.

---
 include/linux/hugetlb.h    |  2 --
 include/linux/page-flags.h | 11 +++++++++++
 mm/filemap.c               | 33 +++++++++++++++++++++++++++++++--
 3 files changed, 42 insertions(+), 4 deletions(-)
include/linux/hugetlb.h +0 -2
index 325f0404a7eb..1878642e75ec 100644
@@ -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));
include/linux/page-flags.h +11 -0
index 9cde74320e48..b146c5228741 100644
@@ -1090,6 +1090,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);
 
 /*
@@ -1111,6 +1112,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) ||
mm/filemap.c +31 -2
index 58eb9d240643..26a5f18121f9 100644
@@ -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;
 			}
 		}
Signature · 1 line
2.47.3
author maintainer
#28 lore raw

Re: [PATCH v4 10/14] filemap: Add hwpoison handling to filemap_read()

Sashiko review:

Quoted 37 lines

--- Patch [10]: [PATCH v4 10/14] filemap: Add hwpoison handling to filemap_read() --- commit c46fb3fcf6cb7055457888cfb186af5ae3a493c2
Author: Jane Chu <[email protected]>

filemap: Add hwpoison handling to filemap_read()

This commit adds hardware poison handling to filemap_read(). It allows the read iterator to make a best-effort copy of data from clean pages, preventing Machine Check Exceptions when the page cache contains HWpoison.

Quoted 5 lines
include/linux/page-flags.h +0 -0
index 9cde74320e48a..b146c5228741c 100644

include/linux/page-flags.h static inline bool is_page_hwpoison(const struct page *page) · line 1121

Quoted 10 lines
include/linux/page-flags.h +7 -0
@@ -1111,6 +1112,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);

Does this lockless traversal risk a Use-After-Free?

When this function calls into hugetlb_page_hwpoison(), it eventually reaches precise_page_poisoned() which performs a lockless iteration of the hugetlb_hwpoison llist:

precise_page_poisoned()

  llist_for_each_entry(p, READ_ONCE(list->first), node) {
    ...

}

Quoted 7 lines

However, a concurrent memory_failure() can free the list nodes while the lockless traversal is in progress, since __folio_free_raw_hwp() calls kfree(p) on the nodes without an RCU grace period.

While filemap_read() does not process hugetlb folios, hugetlbfs_read_iter() (added earlier in this series) does, making the Use-After-Free directly reachable.

Will be addressed by the spinlock I mentioned earlier.

Quoted 8 lines

[ ... ]

Quoted 6 lines
mm/filemap.c +0 -0
index 58eb9d2406434..26a5f18121f90 100644
@@ -2483,6 +2483,8 @@ static void filemap_get_read_batch(struct address_space *mapping,
 
Quoted 7 lines

mm/filemap.c static void filemap_get_read_batch(struct address_space *mapping, · line 2487

Quoted 4 lines
mm/filemap.c +2 -0
 		if (!folio_batch_add(fbatch, folio))
 			break;
+		if (folio_has_hwpoisoned_page(folio))
+			break;

This isn't a bug introduced by this patch, but does returning a poisoned folio here risk a Machine Check Exception if a disk read populates the folio?

Sure, but we remove hwpoisoned pages from the pagecache for storage-backed files. So this doesn't come up.

Quoted 36 lines

If a poisoned folio in the page cache is not uptodate, filemap_get_read_batch() will return it as the last folio in the batch.

Then filemap_get_pages() will see !folio_test_uptodate(folio) and call filemap_update_page(), which locks the folio and passes it to the filesystem's read_folio callback:

filemap_update_page()
error = filemap_read_folio(iocb->ki_filp, mapping->a_ops->read_folio,

                             folio);

If the filesystem populates the folio using CPU operations (which is common for inline data, fscrypt, squashfs, or block device emulation), the CPU will access the poisoned memory and trigger an unrecoverable Machine Check Exception. The patch guards the userspace copy path, but misses guarding the cache-miss path where the kernel itself reads into the poisoned folio.

Quoted 2 lines
mm/filemap.c +0 -0
 		if (!folio_test_uptodate(folio))
 			break;

[ ... ]

mm/filemap.c ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, · line 2893

Quoted 9 lines
mm/filemap.c +4 -1
@@ -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);

This isn't a bug introduced by this patch, but will filemap_splice_read() still trigger a Machine Check Exception on poisoned pages since it lacks similar protection?

Quoted 3 lines

While this patch successfully updates filemap_read() to use adjust_range_hwpoison() to prevent reading poisoned memory, filemap_splice_read() was left unmodified.

Hugetlb doesn't implement splice_read(), so this is irrelevant.

Quoted 10 lines

filemap_splice_read() uses the same filemap_get_pages() logic, loops over the returned batch, and unconditionally splices each folio into the pipe:

filemap_splice_read()
...
n = splice_folio_into_pipe(pipe, folio, *ppos, n);

When a userspace process subsequently reads from the pipe, the kernel will access the poisoned physical page (e.g., via copy_page_to_iter()), triggering an unrecoverable Machine Check Exception.

author maintainer
#29 lore raw

PATCH v4 11/14 filemap: Remove checks in mapping_set_folio_order_range()

In reply to Matthew Wilcox (Oracle)

These checks make it impossible to use mapping_set_folio_order_range() in hugetlb. They add very little value and can simply be removed.

---
 include/linux/pagemap.h | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)
include/linux/pagemap.h +14 -20
index 2c3718d592d6..436987f47aed 100644
@@ -373,7 +373,7 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
  * assumptions about maximum order if THP are disabled, but 8 seems like
  * a good order (that's 1MB if you're using 4kB pages)
  */
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+#ifdef CONFIG_FILEMAP_LARGE_FOLIO
 #define PREFERRED_MAX_PAGECACHE_ORDER	HPAGE_PMD_ORDER
 #else
 #define PREFERRED_MAX_PAGECACHE_ORDER	8
@@ -394,7 +394,7 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
  */
 static inline size_t mapping_max_folio_size_supported(void)
 {
-	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+	if (IS_ENABLED(CONFIG_FILEMAP_LARGE_FOLIO))
 		return 1U << (PAGE_SHIFT + MAX_PAGECACHE_ORDER);
 	return PAGE_SIZE;
 }
@@ -405,29 +405,23 @@ static inline size_t mapping_max_folio_size_supported(void)
  * @min: Minimum folio order (between 0-MAX_PAGECACHE_ORDER inclusive).
  * @max: Maximum folio order (between @min-MAX_PAGECACHE_ORDER inclusive).
  *
- * The filesystem should call this function in its inode constructor to
- * indicate which base size (min) and maximum size (max) of folio the VFS
- * can use to cache the contents of the file.  This should only be used
- * if the filesystem needs special handling of folio sizes (ie there is
- * something the core cannot know).
+ * The filesystem should call this function in its inode constructor
+ * to indicate which size folios can be used to cache the contents of
+ * the inode.  This should only be used if the filesystem needs special
+ * handling of folio sizes (ie there is something the core cannot know).
  * Do not tune it based on, eg, i_size.
  *
+ * hugetlb calls this with orders larger than MAX_PAGECACHE_ORDER.
+ * Normal filesystems should not do this.
+ *
  * Context: This should not be called while the inode is active as it
  * is non-atomic.
  */
 static inline void mapping_set_folio_order_range(struct address_space *mapping,
-						 unsigned int min,
-						 unsigned int max)
+		unsigned int min, unsigned int max)
 {
-	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+	if (!IS_ENABLED(CONFIG_FILEMAP_LARGE_FOLIO))
 		return;
-
-	if (min > MAX_PAGECACHE_ORDER)
-		min = MAX_PAGECACHE_ORDER;
-
-	if (max > MAX_PAGECACHE_ORDER)
-		max = MAX_PAGECACHE_ORDER;
-
 	if (max < min)
 		max = min;
 
@@ -460,7 +454,7 @@ static inline void mapping_set_large_folios(struct address_space *mapping)
 static inline unsigned int
 mapping_max_folio_order(const struct address_space *mapping)
 {
-	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+	if (!IS_ENABLED(CONFIG_FILEMAP_LARGE_FOLIO))
 		return 0;
 	return (mapping->flags & AS_FOLIO_ORDER_MAX_MASK) >> AS_FOLIO_ORDER_MAX;
 }
@@ -468,7 +462,7 @@ mapping_max_folio_order(const struct address_space *mapping)
 static inline unsigned int
 mapping_min_folio_order(const struct address_space *mapping)
 {
-	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+	if (!IS_ENABLED(CONFIG_FILEMAP_LARGE_FOLIO))
 		return 0;
 	return (mapping->flags & AS_FOLIO_ORDER_MIN_MASK) >> AS_FOLIO_ORDER_MIN;
 }
@@ -524,7 +518,7 @@ static inline bool mapping_large_folio_support(const struct address_space *mappi
  *
  * Return: True if PMD-sized folios are supported, otherwise false.
  */
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+#ifdef CONFIG_FILEMAP_LARGE_FOLIO
 static inline bool mapping_pmd_folio_support(const struct address_space *mapping)
 {
 	/* AS_FOLIO_ORDER is only reasonable for pagecache folios */
Signature · 1 line
2.47.3
author maintainer
#30 lore raw

Re: [PATCH v4 11/14] filemap: Remove checks in mapping_set_folio_order_range()

Sashiko review:

Quoted 16 lines

--- Patch [11]: [PATCH v4 11/14] filemap: Remove checks in mapping_set_folio_order_range() --- commit 795dd7139e11d246506bb93b2bb441409e4bbfbf
Author: Matthew Wilcox (Oracle) <[email protected]>

filemap: Remove checks in mapping_set_folio_order_range()

This patch removes max and min order bounds checking from mapping_set_folio_order_range() to allow its use with hugetlb. It also switches the configuration gate from CONFIG_TRANSPARENT_HUGEPAGE to CONFIG_FILEMAP_LARGE_FOLIO across several folio order functions.

Quoted 4 lines
include/linux/pagemap.h +0 -0
index 2c3718d592d6d..436987f47aed4 100644

[ ... ]

Quoted 10 lines

include/linux/pagemap.h static inline size_t mapping_max_folio_size_supported(void) · line 424

Quoted 8 lines

static inline void mapping_set_folio_order_range(struct address_space *mapping,

include/linux/pagemap.h +2 -3
-						 unsigned int min,
-						 unsigned int max)
+		unsigned int min, unsigned int max)
 {
-	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+	if (!IS_ENABLED(CONFIG_FILEMAP_LARGE_FOLIO))
 		return;

Is CONFIG_FILEMAP_LARGE_FOLIO defined anywhere in the codebase?

Oops. Forgot to continue the rename into this patch. Fixed.

author maintainer
#31 lore raw

PATCH v4 12/14 hugetlb: Set mapping folio order

In reply to Matthew Wilcox (Oracle)

hugetlbfs currently uses an fs-specific way to determine the size of the folios in its page cache. Thanks to the support of block sizes larger than page sizes, we now support this as part of the page cache. It's somewhat more efficient as we have this information directly in the mapping rather than going from inode->i_sb->s_fs_info. We can convert the rest of the hugetlb code to use this at our leisure; this is needed now so that we can use filemap_get_pages() unmodified.

---
 fs/hugetlbfs/inode.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +11 -2
index dbca3f713bbf..8d15413a67ee 100644
@@ -883,6 +883,16 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,
 	return inode;
 }
 
+static void hugetlbfs_init_regular_inode(struct inode *inode)
+{
+	struct hstate *hstate = hstate_inode(inode);
+	unsigned int order = huge_page_order(hstate);
+
+	inode->i_op = &hugetlbfs_inode_operations;
+	inode->i_fop = &hugetlbfs_file_operations;
+	mapping_set_folio_order_range(inode->i_mapping, order, order);
+}
+
 /*
  * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  * be taken from reclaim -- unlike regular filesystems. This needs an
@@ -926,8 +936,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb,
 			init_special_inode(inode, mode, dev);
 			break;
 		case S_IFREG:
-			inode->i_op = &hugetlbfs_inode_operations;
-			inode->i_fop = &hugetlbfs_file_operations;
+			hugetlbfs_init_regular_inode(inode);
 			break;
 		case S_IFDIR:
 			inode->i_op = &hugetlbfs_dir_inode_operations;
Signature · 1 line
2.47.3
author maintainer
#32 lore raw

Re: [PATCH v4 12/14] hugetlb: Set mapping folio order

Sashiko review indicates only the problem in the previous patch.

author maintainer
#33 lore raw

PATCH v4 13/14 filemap: Add support for authoritative mappings

In reply to Matthew Wilcox (Oracle)

An authoritative mapping knows about all the folios in the mapping. If read() finds a missing folio, there's no reason to allocate one and try to read it because we know it's a zero region of the file. We can just call iov_iter_zero() instead.

---
 include/linux/pagemap.h | 11 +++++++++++
 mm/filemap.c            | 18 ++++++++++++++++++
 2 files changed, 29 insertions(+)
include/linux/pagemap.h +11 -0
index 436987f47aed..6fdee5b03621 100644
@@ -210,6 +210,7 @@ enum mapping_flags {
 	AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM = 9,
 	AS_KERNEL_FILE = 10,	/* mapping for a fake kernel file that shouldn't
 				   account usage to user cgroups */
+	AS_AUTHORITATIVE = 11,	/* If we miss in the page cache, it's a hole */
 	/* Bits 16-25 are used for FOLIO_ORDER */
 	AS_FOLIO_ORDER_BITS = 5,
 	AS_FOLIO_ORDER_MIN = 16,
@@ -345,6 +346,16 @@ static inline bool mapping_writeback_may_deadlock_on_reclaim(const struct addres
 	return test_bit(AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM, &mapping->flags);
 }
 
+static inline void mapping_set_authoritative(struct address_space *mapping)
+{
+	set_bit(AS_AUTHORITATIVE, &mapping->flags);
+}
+
+static inline bool mapping_is_authoritative(const struct address_space *mapping)
+{
+	return test_bit(AS_AUTHORITATIVE, &mapping->flags);
+}
+
 static inline gfp_t mapping_gfp_mask(const struct address_space *mapping)
 {
 	return mapping->gfp_mask;
mm/filemap.c +18 -0
index 26a5f18121f9..5a8cc20e624e 100644
@@ -2697,6 +2697,8 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
 	if (!folio_batch_count(fbatch)) {
 		DEFINE_READAHEAD(ractl, filp, &filp->f_ra, mapping, index);
 
+		if (mapping_is_authoritative(mapping))
+			return 0;
 		if (iocb->ki_flags & IOCB_NOIO)
 			return -EAGAIN;
 		if (iocb->ki_flags & IOCB_NOWAIT)
@@ -2853,6 +2855,22 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
 			goto put_folios;
 		end_offset = min_t(loff_t, isize, iocb->ki_pos + iter->count);
 
+		if (!folio_batch_count(&fbatch)) {
+			size_t fsize = mapping_min_folio_nrbytes(mapping);
+			size_t offset = iocb->ki_pos & (fsize - 1);
+			size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
+					     fsize - offset);
+			size_t copied = iov_iter_zero(bytes, iter);
+
+			already_read += copied;
+			iocb->ki_pos += copied;
+			last_pos = iocb->ki_pos;
+
+			if (copied < bytes)
+				error = -EFAULT;
+			continue;
+		}
+
 		/*
 		 * Once we start copying data, we don't want to be touching any
 		 * cachelines that might be contended:
Signature · 1 line
2.47.3
author maintainer
#34 lore raw

Re: [PATCH v4 13/14] filemap: Add support for authoritative mappings

Sashiko review identifies only a problem identified in the previous patch.

author maintainer
#35 lore raw

Re: [PATCH v4 13/14] filemap: Add support for authoritative mappings

Quoted 1 line · Matthew Wilcox

Sashiko review identifies only a problem identified in the previous patch.

Er, this should have been the reply to patch 12, not 13.

Sashiko review:

Quoted 48 lines

--- Patch [13]: [PATCH v4 13/14] filemap: Add support for authoritative mappings --- commit f97ea9a8250c2693263cf2f409975e439e79ddd4
Author: Matthew Wilcox (Oracle) <[email protected]>

filemap: Add support for authoritative mappings

An authoritative mapping knows about all the folios in the mapping. If read() finds a missing folio, we know it's a zero region of the file and can call iov_iter_zero() instead of allocating and trying to read one.

mm/filemap.c static int filemap_get_pages(struct kiocb *iocb, size_t count, · line 2703

Quoted 12 lines
mm/filemap.c +2 -0
index 26a5f18121f90..5a8cc20e624e4 100644
@@ -2697,6 +2697,8 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
 	if (!folio_batch_count(fbatch)) {
 		DEFINE_READAHEAD(ractl, filp, &filp->f_ra, mapping, index);
 
+		if (mapping_is_authoritative(mapping))
+			return 0;
 		if (iocb->ki_flags & IOCB_NOIO)
 			return -EAGAIN;

Does this early return cause an infinite loop in filemap_splice_read() when reading a hole?

When an AS_AUTHORITATIVE mapping has a hole, filemap_get_pages() now returns 0 and leaves fbatch empty. filemap_read() was updated in this patch to handle an empty batch, but filemap_splice_read() appears to assume that a return value of 0 means progress was made.

If filemap_splice_read() receives an empty fbatch:

mm/filemap.c:filemap_splice_read() {
        ...
        error = filemap_get_pages(&iocb, len, &fbatch, true);
        if (error < 0)
                break;

        ...
        for (i = 0; i < folio_batch_count(&fbatch); i++) {
                struct folio *folio = fbatch.folios[i];
                // updates to *ppos and len
        }

        folio_batch_release(&fbatch);
    } while (len);
Quoted 2 lines

Since the for loop is skipped, len and *ppos remain unchanged, which could cause the outer do-while loop to spin indefinitely.

This is a latent footgun, I guess. When we come to it, we can move the code from shmem_file_splice_read() here.

author maintainer
#36 lore raw

PATCH v4 14/14 hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()

In reply to Matthew Wilcox (Oracle)

From: Jane Chu <[email protected]>

generic_file_read_iter() now handles hugetlb poison, hugetlb page size and missing folios correctly, so we no longer need a separate hugetlbfs_read_iter().

---
 fs/hugetlbfs/inode.c | 110 +------------------------------------------
 1 file changed, 2 insertions(+), 108 deletions(-)
fs/hugetlbfs/inode.c +2 -108
index 8d15413a67ee..cef7e61b827d 100644
@@ -187,113 +187,6 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	return mm_get_unmapped_area_vmflags(file, addr0, len, pgoff, flags, 0);
 }
 
-/*
- * Someone wants to read @bytes from a HWPOISON hugetlb @folio from @offset.
- * Returns the maximum number of bytes one can read without touching the 1st raw
- * HWPOISON page.
- */
-static size_t adjust_range_hwpoison(struct folio *folio, size_t offset,
-		size_t bytes)
-{
-	struct page *page = folio_page(folio, offset / PAGE_SIZE);
-	size_t safe_bytes;
-
-	if (hugetlb_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 (hugetlb_page_hwpoison(folio, page))
-			break;
-
-	return min(safe_bytes, bytes);
-}
-
-/*
- * Support for read() - Find the page attached to f_mapping and copy out the
- * data. This provides functionality similar to filemap_read().
- */
-static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
-{
-	struct file *file = iocb->ki_filp;
-	struct hstate *h = hstate_file(file);
-	struct address_space *mapping = file->f_mapping;
-	struct inode *inode = mapping->host;
-	unsigned long index = iocb->ki_pos >> huge_page_shift(h);
-	unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
-	unsigned long end_index;
-	loff_t isize;
-	ssize_t retval = 0;
-
-	while (iov_iter_count(to)) {
-		struct folio *folio;
-		size_t nr, copied, want;
-
-		/* nr is the maximum number of bytes to copy from this page */
-		nr = huge_page_size(h);
-		isize = i_size_read(inode);
-		if (!isize)
-			break;
-		end_index = (isize - 1) >> huge_page_shift(h);
-		if (index > end_index)
-			break;
-		if (index == end_index) {
-			nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
-			if (nr <= offset)
-				break;
-		}
-		nr = nr - offset;
-
-		/* Find the folio */
-		folio = filemap_lock_hugetlb_folio(h, mapping, index);
-		if (IS_ERR(folio)) {
-			/*
-			 * We have a HOLE, zero out the user-buffer for the
-			 * length of the hole or request.
-			 */
-			copied = iov_iter_zero(nr, to);
-		} else {
-			folio_unlock(folio);
-
-			if (!folio_test_has_hwpoisoned(folio))
-				want = nr;
-			else {
-				/*
-				 * Adjust how many bytes safe to read without
-				 * touching the 1st raw HWPOISON page after
-				 * offset.
-				 */
-				want = adjust_range_hwpoison(folio, offset, nr);
-				if (want == 0) {
-					folio_put(folio);
-					retval = -EIO;
-					break;
-				}
-			}
-
-			/*
-			 * We have the folio, copy it to user space buffer.
-			 */
-			copied = copy_folio_to_iter(folio, offset, want, to);
-			folio_put(folio);
-		}
-		offset += copied;
-		retval += copied;
-		if (copied != nr && iov_iter_count(to)) {
-			if (!retval)
-				retval = -EFAULT;
-			break;
-		}
-		index += offset >> huge_page_shift(h);
-		offset &= ~huge_page_mask(h);
-	}
-	iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
-	return retval;
-}
-
 static int hugetlbfs_write_begin(const struct kiocb *iocb,
 			struct address_space *mapping,
 			loff_t pos, unsigned len,
@@ -891,6 +784,7 @@ static void hugetlbfs_init_regular_inode(struct inode *inode)
 	inode->i_op = &hugetlbfs_inode_operations;
 	inode->i_fop = &hugetlbfs_file_operations;
 	mapping_set_folio_order_range(inode->i_mapping, order, order);
+	mapping_set_authoritative(inode->i_mapping);
 }
 
 /*
@@ -1215,7 +1109,7 @@ static void init_once(void *foo)
 }
 
 static const struct file_operations hugetlbfs_file_operations = {
-	.read_iter		= hugetlbfs_read_iter,
+	.read_iter		= generic_file_read_iter,
 	.mmap			= hugetlbfs_file_mmap,
 	.fsync			= noop_fsync,
 	.get_unmapped_area	= hugetlb_get_unmapped_area,
Signature · 1 line
2.47.3
author maintainer
#37 lore raw

Re: [PATCH v4 14/14] hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()

The Sashiko review finds two problems which are already addressed in earlier patches.

syzbot Tested-by [email protected] on the v4 series
#38 lore raw
In reply to Matthew Wilcox (Oracle)
syzbot ci has tested the following series

syzbot ci has tested the following series

[v4] Use generic_file_read_iter() in hugetlbfs
https://lore.kernel.org/all/[email protected]
* [PATCH v4 01/14] memory-failure: Fix hardware poison check in unpoison_memory() again
* [PATCH v4 02/14] memory-failure: Test the page is hwpoison before taking the mutex
* [PATCH v4 03/14] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()
* [PATCH v4 04/14] hugetlb: Mark some function arguments as const
* [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page()
* [PATCH v4 06/14] memory-failure: Remove raw_hwp_list_head()
* [PATCH v4 07/14] hugetlb: Use the has_hwpoisoned flag
* [PATCH v4 08/14] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()
* [PATCH v4 09/14] mm: Check individual hugetlb pages for poison
* [PATCH v4 10/14] filemap: Add hwpoison handling to filemap_read()
* [PATCH v4 11/14] filemap: Remove checks in mapping_set_folio_order_range()
* [PATCH v4 12/14] hugetlb: Set mapping folio order
* [PATCH v4 13/14] filemap: Add support for authoritative mappings
* [PATCH v4 14/14] hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()

and found the following issues:
* WARNING in bio_add_page
* general protection fault in create_empty_buffers

Full report is available here:
https://ci.syzbot.org/series/4b3a4f52-06f4-4a41-872f-dde0771be8ef

***

WARNING in bio_add_page

tree:      mm-new
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git
base:      1590cf0329716306e948a8fc29f1d3ee87d3989f
arch:      amd64
compiler:  Debian clang version 22.1.8 (++20260613092233+e80beda6e255-1~exp1~20260613092250.77), Debian LLD 22.1.8
config:    https://ci.syzbot.org/builds/bb071828-9749-428c-a78c-fc39b0b5caf2/config
syz repro: https://ci.syzbot.org/findings/38d53450-b153-4f47-8474-be250791ea91/syz_repro
------------[ cut here ]------------
len == 0
WARNING: block/bio.c:1052 at bio_add_page+0x516/0x700 block/bio.c:1052, CPU#0: udevd/5630
Modules linked in:
CPU: 0 UID: 0 PID: 5630 Comm: udevd Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
RIP: 0010:bio_add_page+0x516/0x700 block/bio.c:1052
Code: 31 ed 44 89 e8 48 83 c4 50 5b 41 5c 41 5d 41 5e 41 5f 5d c3 cc cc cc cc cc e8 86 5e 17 fd 90 0f 0b 90 eb db e8 7b 5e 17 fd 90 <0f> 0b 90 eb d0 e8 70 5e 17 fd 90 0f 0b 90 e9 76 fe ff ff e8 62 5e
RSP: 0018:ffffc9000487f0f8 EFLAGS: 00010293
RAX: ffffffff84af2445 RBX: 0000000000000000 RCX: ffff8881108d0000
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
RBP: 1ffff110228761e2 R08: ffff8881143b0f7f R09: 1ffff110228761ef
R10: dffffc0000000000 R11: ffffed10228761f0 R12: ffff8881143b0f14
R13: 0000000000000000 R14: dffffc0000000000 R15: 0000000000000000
FS:  00007faeac7ccc80(0000) GS:ffff88818d945000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000295e815ae000 CR3: 0000000114e0e000 CR4: 00000000000006f0
Call Trace:
 <TASK>
 bio_add_folio+0x64/0x90 block/bio.c:1107
 do_mpage_readpage+0xd8c/0x1b40 fs/mpage.c:294
 mpage_readahead+0x3e7/0x800 fs/mpage.c:370
 read_pages+0x193/0x5a0 mm/readahead.c:174
 page_cache_ra_unbounded+0x679/0x8f0 mm/readahead.c:315
 do_page_cache_ra mm/readahead.c:348 [inline]
 force_page_cache_ra+0x26c/0x2e0 mm/readahead.c:378
 filemap_get_pages+0x4d9/0x1ef0 mm/filemap.c:2708
 filemap_read+0x422/0x1630 mm/filemap.c:2841
 blkdev_read_iter+0x2f1/0x420 block/fops.c:830
 new_sync_read fs/read_write.c:493 [inline]
 vfs_read+0x595/0xa80 fs/read_write.c:574
 ksys_read+0x150/0x270 fs/read_write.c:716
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x174/0x580 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7faeac316b6a
Code: 00 3d 00 00 41 00 75 0d 50 48 8d 3d 2d 08 0a 00 e8 ea 7d 01 00 31 c0 e9 07 ff ff ff 64 8b 04 25 18 00 00 00 85 c0 75 1b 0f 05 <48> 3d 00 f0 ff ff 76 6c 48 8b 15 8f a2 0d 00 f7 d8 64 89 02 48 83
RSP: 002b:00007ffd8d811da8 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007faeac316b6a
RDX: 0000000000000400 RSI: 0000557e6b670e18 RDI: 0000000000000009
RBP: 0000000000000400 R08: 0000557e6b670df0 R09: 0000000000000008
R10: 0000000000000051 R11: 0000000000000246 R12: 0000557e6b670df0
R13: 0000557e6b670e08 R14: 0000557e6b66e128 R15: 0000557e6b66e0d0
 </TASK>

***

general protection fault in create_empty_buffers
tree:      mm-new
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git
base:      1590cf0329716306e948a8fc29f1d3ee87d3989f
arch:      amd64
compiler:  Debian clang version 22.1.8 (++20260613092233+e80beda6e255-1~exp1~20260613092250.77), Debian LLD 22.1.8
config:    https://ci.syzbot.org/builds/bb071828-9749-428c-a78c-fc39b0b5caf2/config
syz repro: https://ci.syzbot.org/findings/2406d176-ea76-40a8-b6bc-41d3e6326388/syz_repro
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN PTI
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
CPU: 1 UID: 0 PID: 5932 Comm: udevd Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
RIP: 0010:create_empty_buffers+0x48/0x520 fs/buffer.c:1672
Code: 00 00 00 00 fc ff df e8 96 dc 69 ff 48 89 df 4c 89 e6 ba 40 8c 40 00 e8 e6 bc ff ff 49 89 c4 48 89 04 24 4c 89 e0 48 c1 e8 03 <42> 80 3c 30 00 74 08 4c 89 e7 e8 09 bc d7 ff 4d 09 3c 24 49 83 c4
RSP: 0018:ffffc900034bf570 EFLAGS: 00010246
RAX: 0000000000000000 RBX: ffffea0005ceb100 RCX: 0000000000000046
RDX: 0000000000000002 RSI: ffffffff8e4b7f70 RDI: ffff888172cdbb80
RBP: ffffc900034bf6f0 R08: ffffffff825c6656 R09: 0000000000000000
R10: 0000000000000000 R11: ffffffff8eb5a120 R12: 0000000000000000
R13: 1ffffd4000b9d620 R14: dffffc0000000000 R15: 0000000000000000
FS:  00007fd3c0fc7c80(0000) GS:ffff8882a8f45000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00005567a5ebae48 CR3: 000000017395c000 CR4: 00000000000006f0
Call Trace:
 <TASK>
 block_read_full_folio+0x116/0x810 fs/buffer.c:2381
 filemap_read_folio+0x12c/0x3a0 mm/filemap.c:2512
 filemap_update_page mm/filemap.c:2599 [inline]
 filemap_get_pages+0x1742/0x1ef0 mm/filemap.c:2731
 filemap_read+0x422/0x1630 mm/filemap.c:2841
 blkdev_read_iter+0x2f1/0x420 block/fops.c:830
 new_sync_read fs/read_write.c:493 [inline]
 vfs_read+0x595/0xa80 fs/read_write.c:574
 ksys_read+0x150/0x270 fs/read_write.c:716
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x174/0x580 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fd3c0b16b6a
Code: 00 3d 00 00 41 00 75 0d 50 48 8d 3d 2d 08 0a 00 e8 ea 7d 01 00 31 c0 e9 07 ff ff ff 64 8b 04 25 18 00 00 00 85 c0 75 1b 0f 05 <48> 3d 00 f0 ff ff 76 6c 48 8b 15 8f a2 0d 00 f7 d8 64 89 02 48 83
RSP: 002b:00007ffcab174708 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007fd3c0b16b6a
RDX: 0000000000000400 RSI: 000055c883b6c638 RDI: 0000000000000009
RBP: 0000000000000400 R08: 000055c883b6c610 R09: 0000000000000008
R10: 0000000000000051 R11: 0000000000000246 R12: 000055c883b6c610
R13: 000055c883b6c628 R14: 000055c883b60b48 R15: 000055c883b60af0
 </TASK>
Modules linked in:
---[ end trace 0000000000000000 ]---
RIP: 0010:create_empty_buffers+0x48/0x520 fs/buffer.c:1672
Code: 00 00 00 00 fc ff df e8 96 dc 69 ff 48 89 df 4c 89 e6 ba 40 8c 40 00 e8 e6 bc ff ff 49 89 c4 48 89 04 24 4c 89 e0 48 c1 e8 03 <42> 80 3c 30 00 74 08 4c 89 e7 e8 09 bc d7 ff 4d 09 3c 24 49 83 c4
RSP: 0018:ffffc900034bf570 EFLAGS: 00010246
RAX: 0000000000000000 RBX: ffffea0005ceb100 RCX: 0000000000000046
RDX: 0000000000000002 RSI: ffffffff8e4b7f70 RDI: ffff888172cdbb80
RBP: ffffc900034bf6f0 R08: ffffffff825c6656 R09: 0000000000000000
R10: 0000000000000000 R11: ffffffff8eb5a120 R12: 0000000000000000
R13: 1ffffd4000b9d620 R14: dffffc0000000000 R15: 0000000000000000
FS:  00007fd3c0fc7c80(0000) GS:ffff8882a8f45000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000001b30763fff CR3: 000000017395c000 CR4: 00000000000006f0
----------------
Code disassembly (best guess), 6 bytes skipped:
   0:	df e8                	fucomip %st(0),%st
   2:	96                   	xchg   %eax,%esi
   3:	dc 69 ff             	fsubrl -0x1(%rcx)
   6:	48 89 df             	mov    %rbx,%rdi
   9:	4c 89 e6             	mov    %r12,%rsi
   c:	ba 40 8c 40 00       	mov    $0x408c40,%edx
  11:	e8 e6 bc ff ff       	call   0xffffbcfc
  16:	49 89 c4             	mov    %rax,%r12
  19:	48 89 04 24          	mov    %rax,(%rsp)
  1d:	4c 89 e0             	mov    %r12,%rax
  20:	48 c1 e8 03          	shr    $0x3,%rax
* 24:	42 80 3c 30 00       	cmpb   $0x0,(%rax,%r14,1) <-- trapping instruction
  29:	74 08                	je     0x33
  2b:	4c 89 e7             	mov    %r12,%rdi
  2e:	e8 09 bc d7 ff       	call   0xffd7bc3c
  33:	4d 09 3c 24          	or     %r15,(%r12)
  37:	49                   	rex.WB
  38:	83                   	.byte 0x83
  39:	c4                   	.byte 0xc4

***

If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].

To test a patch for this bug, please reply with #syz test (should be on a separate line).

The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.