Skip to contentSkip to conversation

Use generic_file_read_iter() in hugetlbfs

PATCH In review Matthew Wilcox (Oracle) 7 participants 54 messages Aug 5 – Sep 24

Subsystems (16)
lore
Share & tools
lore

Matthew Wilcox (Oracle)

author maintainer
#0 lore raw

v9 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]/

We need three things to use filemap_read_iter() instead of a hugetlbfs specific read_iter: hwpoison handling, treating gaps in the pagecache as holes, and having hugetlb set the folio size in the mapping. hwopison handling turned out to need a lot of prep work; the other two things are fairly straightforward.

Sashiko complains that the implementation of AS_AUTHORITATIVE is incomplete, but hugetlbfs doesn't use any of that functionality. We'll add it when we need it, either when hugetlbfs uses it, or when we convert shmem to use filemap.

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

Changes from v7:
- Fix the KPF_HWPOISON blooper
- Expand HVO in a comment
- Collect R-b tags from Gregory & Jane

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

Matthew Wilcox (Oracle) (13):
memory-failure: Fix hardware poison check in unpoison_memory() again
memory-failure: Prevent hugetlb freeing during unpoisoning
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()
kpageflags: Use is_page_hwpoison() to set KPF_HWPOISON
hugetlb: Move poison to pages before clearing hugetlb page type
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 +++--------------------
 fs/proc/page.c             |   8 +-
 include/linux/hugetlb.h    |  43 +++++---
 include/linux/mm_types.h   |   4 +-
 include/linux/page-flags.h |  64 ++++++++++--
 include/linux/pagemap.h    |  45 +++++----
 mm/Kconfig                 |   6 +-
 mm/filemap.c               |  51 +++++++++-
 mm/huge_memory.c           |   2 +-
 mm/hugetlb.c               |  41 ++++----
 mm/memory-failure.c        | 197 +++++++++++++++++++++++++------------
 mm/memory_hotplug.c        |   2 +-
 mm/rmap.c                  |  43 +++++---
 mm/shmem.c                 |   2 +-
 mm/vmscan.c                |   2 +-
 virt/kvm/guest_memfd.c     |   2 +-
 17 files changed, 368 insertions(+), 269 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v9 1/15 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")
---
 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

PATCH v9 2/15 memory-failure: Prevent hugetlb freeing during unpoisoning

In reply to Matthew Wilcox (Oracle)

If we fail to get a reference on the hugetlb folio, then it might be freed as we operate on it. Prevent the freeing and the attendant races around manipulation of the raw_hwp list by holding the hugetlb_lock, which is also held by the hugetlb code when freeing hugetlb folios.

Fixes ac5fcde0a96a ("mm, hwpoison: make unpoison aware of raw error info in hwpoisoned hugepage")
---
 include/linux/hugetlb.h | 19 +++++++++++++++++++
 mm/memory-failure.c     |  6 +++++-
 2 files changed, 24 insertions(+), 1 deletion(-)
include/linux/hugetlb.h +19 -0
index 2abaf99321e9..50eab2c23299 100644
@@ -110,6 +110,17 @@ extern struct resv_map *resv_map_alloc(void);
 void resv_map_release(struct kref *ref);
 
 extern spinlock_t hugetlb_lock;
+
+static inline void hugetlb_lock_irq(void)
+{
+	spin_lock_irq(&hugetlb_lock);
+}
+
+static inline void hugetlb_unlock_irq(void)
+{
+	spin_unlock_irq(&hugetlb_lock);
+}
+
 extern int hugetlb_max_hstate __read_mostly;
 #define for_each_hstate(h) \
 	for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++)
@@ -279,6 +290,14 @@ unsigned int arch_hugetlb_cma_order(void);
 
 #else /* !CONFIG_HUGETLB_PAGE */
 
+static inline void hugetlb_lock_irq(void)
+{
+}
+
+static inline void hugetlb_unlock_irq(void)
+{
+}
+
 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
 {
 }
mm/memory-failure.c +5 -1
index 944e6e1d4971..1dd0e7b99bb1 100644
@@ -2725,13 +2725,17 @@ int unpoison_memory(unsigned long pfn)
 
 	ghp = get_hwpoison_page(p, MF_UNPOISON);
 	if (!ghp) {
+		hugetlb_lock_irq();
 		if (folio_test_hugetlb(folio)) {
 			huge = true;
 			count = folio_free_raw_hwp(folio, false);
-			if (count == 0)
+			if (count == 0) {
+				hugetlb_unlock_irq();
 				goto unlock_mutex;
+			}
 		}
 		ret = folio_test_clear_hwpoison(folio) ? 0 : -EBUSY;
+		hugetlb_unlock_irq();
 	} else if (ghp < 0) {
 		if (ghp == -EHWPOISON) {
 			ret = put_page_back_buddy(p) ? 0 : -EBUSY;
Signature · 1 line
2.47.3
maintainer Reviewed-by on v9 2/15
#3 lore raw

Re: [PATCH v9 02/15] memory-failure: Prevent hugetlb freeing during unpoisoning

Quoted 5 lines · Matthew Wilcox (Oracle)

If we fail to get a reference on the hugetlb folio, then it might be freed as we operate on it. Prevent the freeing and the attendant races around manipulation of the raw_hwp list by holding the hugetlb_lock, which is also held by the hugetlb code when freeing hugetlb folios.

Quoted 8 lines
Fixes ac5fcde0a96a ("mm, hwpoison: make unpoison aware of raw error info in hwpoisoned hugepage")
---
 include/linux/hugetlb.h | 19 +++++++++++++++++++
 mm/memory-failure.c     |  6 +++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

Thanks.
.

author maintainer
#4 lore raw

PATCH v9 3/15 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
maintainer Acked-by on v9 3/15
#5 lore raw

Re: [PATCH v9 03/15] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()

Quoted 9 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.

---

Signature · 3 lines
Cheers,

David
author maintainer
#6 lore raw

PATCH v9 4/15 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 50eab2c23299..e200c93088bc 100644
@@ -176,7 +176,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;
@@ -311,8 +311,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;
 }
@@ -614,8 +613,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);		\
 	}
 
@@ -635,7 +634,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)				\
@@ -746,7 +745,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;
 }
@@ -876,7 +875,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));
@@ -1107,7 +1106,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;
 }
@@ -1176,7 +1175,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
#7 lore raw

PATCH v9 5/15 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
maintainer Acked-by on v9 5/15
#8 lore raw

Re: [PATCH v9 05/15] guest_memfd: Use folio_has_hwpoisoned_page()

Quoted 18 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 +0 -0
index db57c5766ab6..94eedcc7f1a9 100644

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

Quoted 8 lines
virt/kvm/guest_memfd.c +1 -1
@@ -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);

guest_memfd only uses small folios, so it doesn't really matter.

Signature · 3 lines
Cheers,

David
author maintainer
#9 lore raw

Re: [PATCH v9 05/15] guest_memfd: Use folio_has_hwpoisoned_page()

Quoted 6 lines · David Hildenbrand (Arm)
Quoted 3 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.

guest_memfd only uses small folios, so it doesn't really matter.

Well ...

virt/kvm/guest_memfd.c: /* TODO: Support huge pages. */

and one of the followup patches I have actually removes folio_test/set/clear_hwpoison() (it's a bit churny so I didn't want to include it as part of this series)

Quoted 1 line

Thanks!

#10 lore raw

Re: [PATCH v9 05/15] guest_memfd: Use folio_has_hwpoisoned_page()

Quoted 11 lines · Matthew Wilcox
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 3 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.

guest_memfd only uses small folios, so it doesn't really matter.

Well ...

virt/kvm/guest_memfd.c: /* TODO: Support huge pages. */

Yeah, that was wishful thinking :)

Signature · 3 lines
Cheers,

David
author maintainer
#11 lore raw

PATCH v9 6/15 kpageflags: Use is_page_hwpoison() to set KPF_HWPOISON

In reply to Matthew Wilcox (Oracle)

Instead of knowing how hugetlb handles hwpoison, just ask is_page_hwpoison(). This gives us the flexibility to change how hwpoison is handled without updating this function in the future.

---
 fs/proc/page.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
fs/proc/page.c +2 -6
index 7d9387143435..323e4c049f69 100644
@@ -233,12 +233,8 @@ u64 stable_page_flags(const struct page *page)
 	u |= kpf_copy_bit(k, KPF_UNEVICTABLE,	PG_unevictable);
 	u |= kpf_copy_bit(k, KPF_MLOCKED,	PG_mlocked);
 
-#ifdef CONFIG_MEMORY_FAILURE
-	if (u & (1 << KPF_HUGE))
-		u |= kpf_copy_bit(k, KPF_HWPOISON,	PG_hwpoison);
-	else
-		u |= kpf_copy_bit(ps.page_snapshot.flags.f, KPF_HWPOISON, PG_hwpoison);
-#endif
+	if (is_page_hwpoison(page))
+		u |= 1 << KPF_HWPOISON;
 
 	u |= kpf_copy_bit(k, KPF_RESERVED,	PG_reserved);
 	u |= kpf_copy_bit(k, KPF_OWNER_2,	PG_owner_2);
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH v9 06/15] kpageflags: Use is_page_hwpoison() to set KPF_HWPOISON

Quoted 17 lines · Matthew Wilcox (Oracle)

Instead of knowing how hugetlb handles hwpoison, just ask is_page_hwpoison(). This gives us the flexibility to change how hwpoison is handled without updating this function in the future.

---
 fs/proc/page.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
fs/proc/page.c +0 -0
index 7d9387143435..323e4c049f69 100644
@@ -233,12 +233,8 @@ u64 stable_page_flags(const struct page *page)
 	u |= kpf_copy_bit(k, KPF_UNEVICTABLE,	PG_unevictable);

fs/proc/page.c u64 stable_page_flags(const struct page *page) · line 237

Quoted 10 lines
fs/proc/page.c +2 -6
 	u |= kpf_copy_bit(k, KPF_MLOCKED,	PG_mlocked);
 
-#ifdef CONFIG_MEMORY_FAILURE
-	if (u & (1 << KPF_HUGE))
-		u |= kpf_copy_bit(k, KPF_HWPOISON,	PG_hwpoison);
-	else
-		u |= kpf_copy_bit(ps.page_snapshot.flags.f, KPF_HWPOISON, PG_hwpoison);
-#endif
+	if (is_page_hwpoison(page))
+		u |= 1 << KPF_HWPOISON;

snapshot_page() takes a snapshot of the folio and the page. If you look at "page", you start going to the live version again.

Signature · 3 lines
Cheers,

David
#13 lore raw

Re: [PATCH v9 06/15] kpageflags: Use is_page_hwpoison() to set KPF_HWPOISON

Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Matthew Wilcox (Oracle)

Instead of knowing how hugetlb handles hwpoison, just ask is_page_hwpoison(). This gives us the flexibility to change how hwpoison is handled without updating this function in the future.

---
 fs/proc/page.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
fs/proc/page.c +0 -0
index 7d9387143435..323e4c049f69 100644
@@ -233,12 +233,8 @@ u64 stable_page_flags(const struct page *page)
 	u |= kpf_copy_bit(k, KPF_UNEVICTABLE,	PG_unevictable);
 	u |= kpf_copy_bit(k, KPF_MLOCKED,	PG_mlocked);
 
Quoted 8 lines

fs/proc/page.c u64 stable_page_flags(const struct page *page) · line 237

Quoted 8 lines
fs/proc/page.c +2 -6
-#ifdef CONFIG_MEMORY_FAILURE
-	if (u & (1 << KPF_HUGE))
-		u |= kpf_copy_bit(k, KPF_HWPOISON,	PG_hwpoison);
-	else
-		u |= kpf_copy_bit(ps.page_snapshot.flags.f, KPF_HWPOISON, PG_hwpoison);
-#endif
+	if (is_page_hwpoison(page))
+		u |= 1 << KPF_HWPOISON;

I also just realized, that other users in this function now use BIT_ULL.

Quoted 4 lines

snapshot_page() takes a snapshot of the folio and the page. If you look at "page", you start going to the live version again.

If we really cannot use the snapshot_page, one alternative to avoid using "page" after actually snapshotting it would be:

fs/proc/page.c +2 -6
index 260772b20bd99..f2fc0917f0b6c 100644
@@ -223,12 +223,8 @@ u64 stable_page_flags(const struct page *page)
        u |= kpf_copy_bit(k, KPF_UNEVICTABLE,   PG_unevictable);
        u |= kpf_copy_bit(k, KPF_MLOCKED,       PG_mlocked);
 
-#ifdef CONFIG_MEMORY_FAILURE
-       if (u & BIT_ULL(KPF_HUGE))
-               u |= kpf_copy_bit(k, KPF_HWPOISON,      PG_hwpoison);
-       else
-               u |= kpf_copy_bit(ps.page_snapshot.flags.f, KPF_HWPOISON, PG_hwpoison);
-#endif
+       if (ps.flags & PAGE_SNAPSHOT_HWPOISON)
+               u |= BIT_ULL(KPF_HWPOISON);
 
        u |= kpf_copy_bit(k, KPF_RESERVED,      PG_reserved);
        u |= kpf_copy_bit(k, KPF_OWNER_2,       PG_owner_2);
include/linux/mm.h +1 -0
index 29f13cc6b52a2..7e14e58b98c38 100644
@@ -5496,6 +5496,7 @@ static inline bool page_pool_page_is_pp(const struct page *page)
 #define PAGE_SNAPSHOT_FAITHFUL (1 << 0)
 #define PAGE_SNAPSHOT_PG_BUDDY (1 << 1)
 #define PAGE_SNAPSHOT_PG_IDLE  (1 << 2)
+#define PAGE_SNAPSHOT_HWPOISON (1 << 3)
 
 struct page_snapshot {
        struct folio folio_snapshot;
mm/util.c +2 -0
index bf0513d1d3d08..06195d0801130 100644
@@ -1294,6 +1294,8 @@ static void set_ps_flags(struct page_snapshot *ps, const struct folio *folio,
                ps->flags |= PAGE_SNAPSHOT_PG_BUDDY;
        else if (page_count(page) == 0 && is_free_buddy_page(page))
                ps->flags |= PAGE_SNAPSHOT_PG_BUDDY;
+       if (is_page_hwpoison(page))
+               ps->flags |= PAGE_SNAPSHOT_HWPOISON;
 
        if (folio_test_idle(folio))
                ps->flags |= PAGE_SNAPSHOT_PG_IDLE;

(I'm starting to hate all these different hwpoison helpers)

Signature · 3 lines
Cheers,

David
author maintainer
#14 lore raw

Re: [PATCH v9 06/15] kpageflags: Use is_page_hwpoison() to set KPF_HWPOISON

Quoted 8 lines · David Hildenbrand (Arm)
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (is_page_hwpoison(page))
+		u |= 1 << KPF_HWPOISON;

I also just realized, that other users in this function now use BIT_ULL.

Quoted 3 lines

snapshot_page() takes a snapshot of the folio and the page. If you look at "page", you start going to the live version again.

This is now:

+ if (is_page_hwpoison(&ps.page_snapshot))
+ u |= BIT_ULL(KPF_HWPOISON);

author maintainer
#15 lore raw

PATCH v9 7/15 hugetlb: Move poison to pages before clearing hugetlb page type

In reply to Matthew Wilcox (Oracle)

For is_page_hwpoison() to be reliable, we need to set the hwpoison flag on each page before clearing the hugetlb flag. Introduce folio_clear_hugetlb() to make this easier. This does not by itself make is_page_hwpoison() reliable; see subsequent patches.

---
 mm/hugetlb.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
mm/hugetlb.c +18 -13
index cc8d98399913..47403d02be88 100644
@@ -1381,6 +1381,18 @@ static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask, int
 }
 #endif
 
+static void folio_clear_hugetlb(struct folio *folio)
+{
+	/*
+	 * Move HWPoison flag to each error page
+	 * which makes any healthy pages reusable.
+	 */
+	if (unlikely(folio_test_hwpoison(folio)))
+		folio_clear_hugetlb_hwpoison(folio);
+
+	__folio_clear_hugetlb(folio);
+}
+
 /*
  * Remove hugetlb folio from lists.
  * If vmemmap exists for the folio, clear the hugetlb flag so that the
@@ -1419,7 +1431,7 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio,
 	 * to tail struct pages.
 	 */
 	if (!folio_test_hugetlb_vmemmap_optimized(folio))
-		__folio_clear_hugetlb(folio);
+		folio_clear_hugetlb(folio);
 
 	h->nr_huge_pages--;
 	h->nr_huge_pages_node[nid]--;
@@ -1493,17 +1505,10 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
 	 */
 	if (folio_test_hugetlb(folio)) {
 		spin_lock_irq(&hugetlb_lock);
-		__folio_clear_hugetlb(folio);
+		folio_clear_hugetlb(folio);
 		spin_unlock_irq(&hugetlb_lock);
 	}
 
-	/*
-	 * Move PageHWPoison flag from head page to the raw error pages,
-	 * which makes any healthy subpages reusable.
-	 */
-	if (unlikely(folio_test_hwpoison(folio)))
-		folio_clear_hugetlb_hwpoison(folio);
-
 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
 	if (folio_test_hugetlb_cma(folio))
 		hugetlb_cma_free_frozen_folio(folio);
@@ -1595,7 +1600,7 @@ static void bulk_vmemmap_restore_error(struct hstate *h,
 		list_for_each_entry_safe(folio, t_folio, non_hvo_folios, lru) {
 			list_del(&folio->lru);
 			spin_lock_irq(&hugetlb_lock);
-			__folio_clear_hugetlb(folio);
+			folio_clear_hugetlb(folio);
 			spin_unlock_irq(&hugetlb_lock);
 			update_and_free_hugetlb_folio(h, folio, false);
 			cond_resched();
@@ -1620,7 +1625,7 @@ static void bulk_vmemmap_restore_error(struct hstate *h,
 			} else {
 				list_del(&folio->lru);
 				spin_lock_irq(&hugetlb_lock);
-				__folio_clear_hugetlb(folio);
+				folio_clear_hugetlb(folio);
 				spin_unlock_irq(&hugetlb_lock);
 				update_and_free_hugetlb_folio(h, folio, false);
 				cond_resched();
@@ -1653,14 +1658,14 @@ static void update_and_free_pages_bulk(struct hstate *h,
 	 * should only be pages on the non_hvo_folios list.
 	 * Do note that the non_hvo_folios list could be empty.
 	 * Without HVO enabled, ret will be 0 and there is no need to call
-	 * __folio_clear_hugetlb as this was done previously.
+	 * folio_clear_hugetlb as this was done previously.
 	 */
 	VM_WARN_ON(!list_empty(folio_list));
 	VM_WARN_ON(ret < 0);
 	if (!list_empty(&non_hvo_folios) && ret) {
 		spin_lock_irq(&hugetlb_lock);
 		list_for_each_entry(folio, &non_hvo_folios, lru)
-			__folio_clear_hugetlb(folio);
+			folio_clear_hugetlb(folio);
 		spin_unlock_irq(&hugetlb_lock);
 	}
 
Signature · 1 line
2.47.3
author maintainer
#16 lore raw

PATCH v9 8/15 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 | 47 ++++++++++++++++++++++++------
 mm/Kconfig                 |  6 +++-
 mm/hugetlb.c               | 10 +++----
 mm/memory-failure.c        | 58 +++++++++++++++++++++++++++-----------
 mm/rmap.c                  | 43 +++++++++++++++++-----------
 7 files changed, 120 insertions(+), 48 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 +39 -8

82 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 +5 -5
index 47403d02be88..40ae967b9ecc 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))
@@ -1387,7 +1387,7 @@ static void folio_clear_hugetlb(struct folio *folio)
 	 * 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);
 
 	__folio_clear_hugetlb(folio);
@@ -4003,7 +4003,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);
@@ -5814,7 +5814,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;
@@ -6323,7 +6323,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 +42 -16

106 lines not on this page · raw message

mm/rmap.c +27 -16

119 lines not on this page · raw message

Signature · 1 line
2.47.3
#17 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 2 lines · 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.

Currently, the first thing we do in hugetlb_update_hwpoison() is:

	int ret = folio_test_set_hwpoison(folio);

So a hugetlb folio *always* has folio_test_hwpoison() set.

So we can locklessly check whether a hugetlb folios has the flag set simply by ... checking whether it's a hugetlb folio and has the flag set.

Then you write:

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

When does the race you describe become relevant? When freeing the hugetlb folio or when demoting it? in both cases, we should be holding the hugetlb lock that nasty hwpoison code could sync with (if it doesn't already do that).

I can understand that we want something that covers both scenarios: an indication whether any large folio has any hwpoisoned page. Such a unification would be good.

But then, special-casing hugetlb *again* through folio_test_huge_poison(), that's rather odd, no?

(what on earth is "huge_poison" is this supposed to be "hugetlb_poison" ? Why "poison" and not "hwpoison"? Really odd)

I'd expect that we actually get rid of folio_test_hwpoison entirely and exclusively work on per-page state and has_hwpoison. But IIUC, now it's some mixture of folio checks, page checks, folio_has, mixed with some hugetlb oddity.

I am not quite clear whether the change you propose here is actually required for the remainder of this series?

[PATCH v9 00/15] Use generic_file_read_iter() in hugetlbfs

IOW, do we really need all this hugetlb hwpoison handling just to accomplish that, or could some of that (bigger hwpoison rework) be done separately?

[...]

Quoted 7 lines
mm/rmap.c +1 -0
@@ -1978,6 +1978,22 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio,
 				     FPB_RESPECT_WRITE | FPB_RESPECT_SOFT_DIRTY);
 }
 
+/*

mm/rmap.c static inline unsigned int folio_unmap_pte_batch(struct folio *folio, · line 1991

Quoted 10 lines
mm/rmap.c +10 -0
+ * Since we cannot split a hugetlb folio, we want to insert a poison
+ * entry into the page table for the whole folio even if only one page
+ * is poisoned.  Otherwise, we've split down to the PTE level and we only
+ * want to poison the precise page
+ */
+static bool ttu_create_hwpoison(const struct folio *folio,
+		const struct page *page, enum ttu_flags flags)
+{
+	if (!(flags & TTU_HWPOISON))
+		return false;

Why not keep the flag in the caller? Really looks weird given that we have two users and one of them doesn't even need the check and must fake it.

I also don't quite enjoy the function name, and it's likely the reason why we need 4 lines of doc for a simple internal helper that checks whether the thing (page table entry) we are unmapping/migarting is hwpoisoned. This function is not involved at all in any kind of splitting.

Some of that code is getting reworked by Dev to better separate out all the hugetlb oddities. At least for the try_to_unmap() path that helper will not be required. Staring at the abomination we call try_to_migrate_one(), I assume we'll do something similar.

I'd just call this "ttu_page_is_hwpoisoned()", and add a short

/*
* hugetlb folios are always entirely mapped, so any
* hwpoisoned page is problematic.
*/

Quoted 12 lines
mm/rmap.c +5 -0
+	if (folio_test_hugetlb(folio))
+		return folio_test_has_hwpoisoned(folio);
+	return PageHWPoison(page);
+}
+
 /*
  * @arg: enum ttu_flags will be passed to this argument
  */
@@ -1993,7 +2009,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 	unsigned long nr_pages = 1, end_addr;
 	unsigned long pfn;

[...]

mm/rmap.c static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, · line 2426

Quoted 7 lines
mm/rmap.c +0 -1
 				dec_mm_counter(mm, mm_counter(folio));
 				set_pte_at(mm, address, pvmw.pte, pteval);
@@ -2423,7 +2436,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 	struct mmu_notifier_range range;
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 	unsigned long pfn;
-	unsigned long hsz = 0;

While these size changes are reasonable, they make this patch harder to review and are not even described in the patch desription.

Signature · 3 lines
Cheers,

David
author maintainer
#18 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 4 lines · David Hildenbrand (Arm)
Quoted 2 lines · 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.

Quoted 8 lines

Currently, the first thing we do in hugetlb_update_hwpoison() is:

	int ret = folio_test_set_hwpoison(folio);

So a hugetlb folio *always* has folio_test_hwpoison() set.

So we can locklessly check whether a hugetlb folios has the flag set simply by ... checking whether it's a hugetlb folio and has the flag set.

You make it sound so easy ;-)

If we don't hold a reference on the folio, then the folio (whether it's hugetlb or not) can be split. And then folio_test_hwpoison() can hit the assertion that it's now a tail page.

Quoted 8 lines

Then you write:

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

When does the race you describe become relevant? When freeing the hugetlb folio or when demoting it? in both cases, we should be holding the hugetlb lock that nasty hwpoison code could sync with (if it doesn't already do that).

Before this patch series, we don't always hold the hugetlb lock when splitting a hugetlb folio that contains hwpoison. And we don't want to have to grab the hugetlb lock if we can avoid it -- unprivileged userspace can hammer these paths hard, and I don't want to see this lock be contended.

Quoted 3 lines

I can understand that we want something that covers both scenarios: an indication whether any large folio has any hwpoisoned page. Such a unification would be good.

Right, and after this patch, we now have that. Any unknowing part of the kernel can test folio_has_hwpoisoned_page() (assuming it has a refcount on the folio).

Quoted 2 lines

But then, special-casing hugetlb *again* through folio_test_huge_poison(), that's rather odd, no?

Oh, I'm not happy about it. But we need an atomic way to determine if a page belongs to a hugetlb folio with hwpoison detected. Short of a complete rearchitecture of how we handle hwpoison, this is the best I've come up with.

Quoted 2 lines

(what on earth is "huge_poison" is this supposed to be "hugetlb_poison" ? Why "poison" and not "hwpoison"? Really odd)

We're inconsistent in our naming on both of these things. It doesn't help that somebody decided to reuse the term "poison" to mean "uninitialised struct page".

Quoted 3 lines

I'd expect that we actually get rid of folio_test_hwpoison entirely and exclusively work on per-page state and has_hwpoison. But IIUC, now it's some mixture of folio checks, page checks, folio_has, mixed with some hugetlb oddity.

If we could get rid of HVO, we could do it entirely on struct page. Or, as above, entirely rearchitecture hwpoison handling to not be based around pages or folios any more.

Quoted 7 lines

I am not quite clear whether the change you propose here is actually required for the remainder of this series?

[PATCH v9 00/15] Use generic_file_read_iter() in hugetlbfs

IOW, do we really need all this hugetlb hwpoison handling just to accomplish that, or could some of that (bigger hwpoison rework) be done separately?

Blame Sashiko! I'm going to ignore it in future, but it's really good at nerd-sniping "hey all of this is already broken and you could fix it as part of this series".

Quoted 15 lines

mm/rmap.c static inline unsigned int folio_unmap_pte_batch(struct folio *folio, · line 1991

Quoted 12 lines
mm/rmap.c +11 -0
+/*
+ * Since we cannot split a hugetlb folio, we want to insert a poison
+ * entry into the page table for the whole folio even if only one page
+ * is poisoned.  Otherwise, we've split down to the PTE level and we only
+ * want to poison the precise page
+ */
+static bool ttu_create_hwpoison(const struct folio *folio,
+		const struct page *page, enum ttu_flags flags)
+{
+	if (!(flags & TTU_HWPOISON))
+		return false;

Why not keep the flag in the caller? Really looks weird given that we have two users and one of them doesn't even need the check and must fake it.

try_to_unmap_one() is already 200 lines and contains some tricky code flow. I'm trying to at least make the problem not-worse. I think we should pull out even more code into helpers, but not in this patch series.

Quoted 4 lines

I also don't quite enjoy the function name, and it's likely the reason why we need 4 lines of doc for a simple internal helper that checks whether the thing (page table entry) we are unmapping/migarting is hwpoisoned. This function is not involved at all in any kind of splitting.

I think I was trying to pull out even more code into this function, which was when I named it, and then I found it tricky to do so. And I didn't go back and change the function name.

Quoted 11 lines

Some of that code is getting reworked by Dev to better separate out all the hugetlb oddities. At least for the try_to_unmap() path that helper will not be required. Staring at the abomination we call try_to_migrate_one(), I assume we'll do something similar.

I'd just call this "ttu_page_is_hwpoisoned()", and add a short

/*
* hugetlb folios are always entirely mapped, so any
* hwpoisoned page is problematic.
*/

I can rename it, no problem. I think the more verbose description is justified though; we need to explain why we care about any page in the folio for hugetlb, but only about the precise page for non-hugetlb.

Quoted 8 lines

mm/rmap.c static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, · line 2426

Quoted 5 lines
mm/rmap.c +0 -1
@@ -2423,7 +2436,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 	struct mmu_notifier_range range;
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 	unsigned long pfn;
-	unsigned long hsz = 0;

While these size changes are reasonable, they make this patch harder to review and are not even described in the patch desription.

I suppose. Do you want me to take them out?

#19 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

I'm a terrible person and it took me way too long to get back to this :(

Quoted 2 lines

You make it sound so easy ;-)

Heh, as long as you hold a folio reference it really is :)

Quoted 4 lines

If we don't hold a reference on the folio, then the folio (whether it's hugetlb or not) can be split. And then folio_test_hwpoison() can hit the assertion that it's now a tail page.

Right. I am/was missing the connection to "generic_file_read_iter() in hugetlbfs".

Quoted 10 lines
Quoted 9 lines

Then you write:

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

When does the race you describe become relevant? When freeing the hugetlb folio or when demoting it? in both cases, we should be holding the hugetlb lock that nasty hwpoison code could sync with (if it doesn't already do that).

Quoted 5 lines

Before this patch series, we don't always hold the hugetlb lock when splitting a hugetlb folio that contains hwpoison. And we don't want to have to grab the hugetlb lock if we can avoid it -- unprivileged userspace can hammer these paths hard, and I don't want to see this lock be contended.

Right. But doesn't at least the pagecache always hold a folio reference when testing for hwpoison?

I mean, there is other code that might need care (PFN walkers like memory offlining), but I was surprised to see pagecache code require a rework of lockless hwpoison checking.

I'm sure I am missing something.

Quoted 9 lines
Quoted 4 lines

I can understand that we want something that covers both scenarios: an indication whether any large folio has any hwpoisoned page. Such a unification would be good.

Right, and after this patch, we now have that. Any unknowing part of the kernel can test folio_has_hwpoisoned_page() (assuming it has a refcount on the folio).

Quoted 7 lines
Quoted 2 lines

But then, special-casing hugetlb *again* through folio_test_huge_poison(), that's rather odd, no?

Oh, I'm not happy about it. But we need an atomic way to determine if a page belongs to a hugetlb folio with hwpoison detected. Short of a complete rearchitecture of how we handle hwpoison, this is the best I've come up with.

(did I express how much I hate the hwpoison infrastructure and how it's racy left and right? :) )

Quoted 7 lines
Quoted 3 lines

(what on earth is "huge_poison" is this supposed to be "hugetlb_poison" ? Why "poison" and not "hwpoison"? Really odd)

We're inconsistent in our naming on both of these things. It doesn't help that somebody decided to reuse the term "poison" to mean "uninitialised struct page".

Right, but let's be consistent with hugetlb and with hwpoison. ;)

Quoted 6 lines
Quoted 4 lines

I'd expect that we actually get rid of folio_test_hwpoison entirely and exclusively work on per-page state and has_hwpoison. But IIUC, now it's some mixture of folio checks, page checks, folio_has, mixed with some hugetlb oddity.

If we could get rid of HVO, we could do it entirely on struct page.

That's what I hope we will achieve at some point.

Quoted 6 lines

Or, as above, entirely rearchitecture hwpoison handling to not be based around pages or folios any more.

Quoted 3 lines

I am not quite clear whether the change you propose here is actually required for the remainder of this series?

Quoted 8 lines
Quoted 4 lines

[PATCH v9 00/15] Use generic_file_read_iter() in hugetlbfs

IOW, do we really need all this hugetlb hwpoison handling just to accomplish that, or could some of that (bigger hwpoison rework) be done separately?

Blame Sashiko! I'm going to ignore it in future, but it's really good at nerd-sniping "hey all of this is already broken and you could fix it as part of this series".

Right, and this is only the tip of the iceberg, because the entire hwpoison infrastructure is a complete hacked-on racy piece of ... engineering excellence.

To summarize my question: is it possible to separate for this series the pagecache part (Use generic_file_read_iter() in hugetlbfs) from all the hugetlb hwpoison rework?

OTOH, if it's really not avoidable, please let me know.

Quoted 17 lines
Quoted 16 lines

mm/rmap.c static inline unsigned int folio_unmap_pte_batch(struct folio *folio, · line 1991

Quoted 13 lines
mm/rmap.c +11 -0
+/*
+ * Since we cannot split a hugetlb folio, we want to insert a poison
+ * entry into the page table for the whole folio even if only one page
+ * is poisoned.  Otherwise, we've split down to the PTE level and we only
+ * want to poison the precise page
+ */
+static bool ttu_create_hwpoison(const struct folio *folio,
+		const struct page *page, enum ttu_flags flags)
+{
+	if (!(flags & TTU_HWPOISON))
+		return false;

Why not keep the flag in the caller? Really looks weird given that we have two users and one of them doesn't even need the check and must fake it.

Quoted 3 lines

try_to_unmap_one() is already 200 lines and contains some tricky code flow. I'm trying to at least make the problem not-worse. I think we should pull out even more code into helpers, but not in this patch series.

Note that this code now was partly reworked such that this helper will soon no longer be required.

[...]

Quoted 10 lines
Quoted 8 lines

mm/rmap.c static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, · line 2426

Quoted 5 lines
mm/rmap.c +0 -1
@@ -2423,7 +2436,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 	struct mmu_notifier_range range;
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 	unsigned long pfn;
-	unsigned long hsz = 0;

While these size changes are reasonable, they make this patch harder to review and are not even described in the patch desription.

I suppose. Do you want me to take them out?

Cleaner, yes.

Again, sorry for being so terribly slow.

Signature · 3 lines
Cheers,

David
author maintainer
#20 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 5 lines · David Hildenbrand (Arm)

I'm a terrible person and it took me way too long to get back to this :(

Quoted 2 lines

You make it sound so easy ;-)

Quoted 8 lines

Heh, as long as you hold a folio reference it really is :)

Quoted 4 lines

If we don't hold a reference on the folio, then the folio (whether it's hugetlb or not) can be split. And then folio_test_hwpoison() can hit the assertion that it's now a tail page.

Right. I am/was missing the connection to "generic_file_read_iter() in hugetlbfs".

We might be able to split this into two series at this point. Is that worth me spending time on doing? Originally I was going to use is_page_hwpoison() in filemap, then it became obvious that this was inefficient when we had a reference to the folio, and so we ended up with is_ref_page_hwpoison().

Quoted 6 lines
Quoted 5 lines

Before this patch series, we don't always hold the hugetlb lock when splitting a hugetlb folio that contains hwpoison. And we don't want to have to grab the hugetlb lock if we can avoid it -- unprivileged userspace can hammer these paths hard, and I don't want to see this lock be contended.

Quoted 8 lines

Right. But doesn't at least the pagecache always hold a folio reference when testing for hwpoison?

I mean, there is other code that might need care (PFN walkers like memory offlining), but I was surprised to see pagecache code require a rework of lockless hwpoison checking.

I'm sure I am missing something.

It's just how everything evolved.

Quoted 7 lines
Quoted 4 lines

Oh, I'm not happy about it. But we need an atomic way to determine if a page belongs to a hugetlb folio with hwpoison detected. Short of a complete rearchitecture of how we handle hwpoison, this is the best I've come up with.

(did I express how much I hate the hwpoison infrastructure and how it's racy left and right? :) )

Yes, and you'll have the chance to do it again on Wednesday ;-)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

(what on earth is "huge_poison" is this supposed to be "hugetlb_poison" ? Why "poison" and not "hwpoison"? Really odd)

We're inconsistent in our naming on both of these things. It doesn't help that somebody decided to reuse the term "poison" to mean "uninitialised struct page".

Right, but let's be consistent with hugetlb and with hwpoison. ;)

I'll take another look ...

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I'd expect that we actually get rid of folio_test_hwpoison entirely and exclusively work on per-page state and has_hwpoison. But IIUC, now it's some mixture of folio checks, page checks, folio_has, mixed with some hugetlb oddity.

If we could get rid of HVO, we could do it entirely on struct page.

That's what I hope we will achieve at some point.

I'm not sure that's a realistic hope in the next few years. Even if we get struct page down to 8 bytes, that's 2MB of memory per 1GiB allocation (assuming 4KiB pages). Always a tempting target for someone looking for memory savings.

I would prefer an out-of-line tree that doesn't rely on a bit in struct page. Maybe a bit in struct folio wuld be fine (which tells you whether it's OK to skip the tree search).

Quoted 15 lines
Quoted 6 lines

Or, as above, entirely rearchitecture hwpoison handling to not be based around pages or folios any more.

Quoted 3 lines

I am not quite clear whether the change you propose here is actually required for the remainder of this series?

Quoted 8 lines
Quoted 4 lines

[PATCH v9 00/15] Use generic_file_read_iter() in hugetlbfs

IOW, do we really need all this hugetlb hwpoison handling just to accomplish that, or could some of that (bigger hwpoison rework) be done separately?

Blame Sashiko! I'm going to ignore it in future, but it's really good at nerd-sniping "hey all of this is already broken and you could fix it as part of this series".

Quoted 8 lines

Right, and this is only the tip of the iceberg, because the entire hwpoison infrastructure is a complete hacked-on racy piece of ... engineering excellence.

To summarize my question: is it possible to separate for this series the pagecache part (Use generic_file_read_iter() in hugetlbfs) from all the hugetlb hwpoison rework?

OTOH, if it's really not avoidable, please let me know.

I think Sashiko will whine and complain incessantly if we drop the race elimination patches off the front. I can try it, but is it worth doing? Eliminating the races seems like good engineering anyway.

Quoted 6 lines
Quoted 3 lines

try_to_unmap_one() is already 200 lines and contains some tricky code flow. I'm trying to at least make the problem not-worse. I think we should pull out even more code into helpers, but not in this patch series.

Note that this code now was partly reworked such that this helper will soon no longer be required.

Rebasing it on current Linus simplified this a lot.

#21 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 9 lines · Matthew Wilcox
Quoted 8 lines · David Hildenbrand (Arm)

I'm a terrible person and it took me way too long to get back to this :(

Quoted 2 lines

You make it sound so easy ;-)

Heh, as long as you hold a folio reference it really is :)

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

If we don't hold a reference on the folio, then the folio (whether it's hugetlb or not) can be split. And then folio_test_hwpoison() can hit the assertion that it's now a tail page.

Right. I am/was missing the connection to "generic_file_read_iter() in hugetlbfs".

We might be able to split this into two series at this point.

That would be good, because I suspect the "generic_file_read_iter() in hugetlbfs" part is less controversial than the hugetlb cleanup and we can just merge that easily without touching too much other code.

[...]

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

If we could get rid of HVO, we could do it entirely on struct page.

That's what I hope we will achieve at some point.

I'm not sure that's a realistic hope in the next few years.

:(

Quoted 4 lines

Even if
we get struct page down to 8 bytes, that's 2MB of memory per 1GiB allocation (assuming 4KiB pages). Always a tempting target for someone looking for memory savings.

Ack

Quoted 4 lines

I would prefer an out-of-line tree that doesn't rely on a bit in struct page. Maybe a bit in struct folio wuld be fine (which tells you whether it's OK to skip the tree search).

Yes, that would likely be better. A single bit would likely just remove any overhead of checking in the common case (no hwpoison).

Not sure what to do with non-folio things.

Quoted 16 lines
Quoted 16 lines
Quoted 8 lines

Or, as above, entirely rearchitecture hwpoison handling to not be based around pages or folios any more.

Blame Sashiko! I'm going to ignore it in future, but it's really good at nerd-sniping "hey all of this is already broken and you could fix it as part of this series".

Right, and this is only the tip of the iceberg, because the entire hwpoison infrastructure is a complete hacked-on racy piece of ... engineering excellence.

To summarize my question: is it possible to separate for this series the pagecache part (Use generic_file_read_iter() in hugetlbfs) from all the hugetlb hwpoison rework?

Quoted 5 lines
Quoted 1 line

OTOH, if it's really not avoidable, please let me know.

I think Sashiko will whine and complain incessantly if we drop the race elimination patches off the front. I can try it, but is it worth doing?

Note that Sashiko will apparently no longer complain about preexisting stuff. I've been told that these reports are now collected elsewhere.

So I wouldn't worry about Sashiko crying about hwpoison stuff that was broken for ever. I'd rather have fixes for that handled separately (after we properly discuss a better long-term solution to handle most these races and where to store hwpoison information long-term).

Signature · 3 lines
Cheers,

David
author maintainer
#22 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 14 lines · David Hildenbrand (Arm)
Quoted 13 lines · Matthew Wilcox
Quoted 12 lines · David Hildenbrand (Arm)

I'm a terrible person and it took me way too long to get back to this :(

Quoted 2 lines

You make it sound so easy ;-)

Heh, as long as you hold a folio reference it really is :)

Quoted 4 lines

If we don't hold a reference on the folio, then the folio (whether it's hugetlb or not) can be split. And then folio_test_hwpoison() can hit the assertion that it's now a tail page.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Right. I am/was missing the connection to "generic_file_read_iter() in hugetlbfs".

We might be able to split this into two series at this point.

That would be good, because I suspect the "generic_file_read_iter() in hugetlbfs" part is less controversial than the hugetlb cleanup and we can just merge that easily without touching too much other code.

Oh, no, it's awful and complicated. We could strip out the two bugfixes which are at the start of the series, but why would we want to do that?

It has to do with hugetlb not using the has_hwpoisoned() flag today, and to do that we need to do all the other cleanup first. You don't need to read + understand those earlier patches; they have enough review from people familiar with this area. And sashiko ...

#23 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 11 lines · Matthew Wilcox
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 2 lines · Matthew Wilcox

We might be able to split this into two series at this point.

That would be good, because I suspect the "generic_file_read_iter() in hugetlbfs" part is less controversial than the hugetlb cleanup and we can just merge that easily without touching too much other code.

Oh, no, it's awful and complicated. We could strip out the two bugfixes which are at the start of the series, but why would we want to do that?

Andrew usually wants us to separate bugfixes (especially stable ones) from other work. Unless we all agree that they are not urgent and can go in in the next cycle. I assume that's the case here.

Quoted 5 lines

It has to do with hugetlb not using the has_hwpoisoned() flag today, and to do that we need to do all the other cleanup first. You don't need to read + understand those earlier patches; they have enough review from people familiar with this area. And sashiko ...

I think I reviewed most of them? Also, I like to understand code that touches files I maintain. Independent of who (or what) else reviewed it.

Taking another look, most of the hwpoison patches are fine and I fully agree that using has_hwpoisoned is much better. And if it helps to implement support for the generic iterator, good.

Figuring out the actual hwpoisoned page for hugetlb looks like the right thing to do.

I am not convinced about the huge_poison thing though, that originally caught my attention.

So trying to understand the race we are trying to solve (that is independent of the original idea of the patch set), I realize that these races are only on some slow paths we don't particularly care about for performance.

I'd just do the following to avoid that huge_poison thing and keep it simple:

From 9f175684aea7e1cebb1d4070c217a20c9e847294 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Thu, 24 Sep 2026 12:12:17 +0200
Subject: [PATCH] tmp

---
 include/linux/page-flags.h | 40 ++++++++++++++++----------------------
 mm/memory-failure.c        | 37 ++++++-----------------------------
 2 files changed, 23 insertions(+), 54 deletions(-)
include/linux/page-flags.h +17 -23
index f75d66c425095..c7f6ce68c6d15 100644
@@ -909,6 +909,7 @@ FOLIO_TEST_SET_FLAG(has_hwpoisoned, FOLIO_SECOND_PAGE)
 FOLIO_TEST_CLEAR_FLAG(has_hwpoisoned, FOLIO_SECOND_PAGE)
 #else
 FOLIO_FLAG_FALSE(has_hwpoisoned)
+FOLIO_TEST_CLEAR_FLAG_FALSE(has_hwpoisoned)
 #endif
 
 /*
@@ -1047,29 +1048,8 @@ PAGE_TYPE_OPS(Slab, slab, slab)
 
 #ifdef CONFIG_HUGETLB_PAGE
 FOLIO_TYPE_OPS(hugetlb, hugetlb)
-
-#ifdef CONFIG_MEMORY_FAILURE
-static inline bool folio_test_huge_poison(const struct folio *folio)
-{
-	return (READ_ONCE(folio->page.page_type) >> 23) ==
-		((PGTY_hugetlb << 1) | 1);
-}
-
-static inline void folio_set_huge_poison(struct folio *folio)
-{
-	folio->page.page_type |= (1 << 23);
-}
-
-static inline void folio_clear_huge_poison(struct folio *folio)
-{
-	folio->page.page_type &= ~(1 << 23);
-}
-#else
-FOLIO_TEST_FLAG_FALSE(huge_poison)
-#endif
 #else
 FOLIO_TEST_FLAG_FALSE(hugetlb)
-FOLIO_TEST_FLAG_FALSE(huge_poison)
 #endif
 
 PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
@@ -1096,7 +1076,14 @@ static inline bool PageHuge(const struct page *page)
 }
 
 bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page);
+#ifdef CONFIG_MEMORY_FAILURE
 bool hugetlb_unref_page_hwpoison(const struct page *page);
+#else
+static inline bool hugetlb_unref_page_hwpoison(const struct page *page)
+{
+	return false;
+}
+#endif
 
 /*
  * Check if a page is currently marked HWPoisoned.  This check is best
@@ -1111,8 +1098,15 @@ static inline bool is_page_hwpoison(const struct page *page)
 	if (PageHWPoison(page))
 		return true;
 	folio = page_folio(page);
-	if (folio_test_huge_poison(folio))
-		return hugetlb_unref_page_hwpoison(page);
+
+	/*
+	 * It's not safe to query folio_test_has_hwpoisoned() due to concurrent
+	 * splitting.  As we don't expect to be called on any hot paths, simply
+	 * always take the slow path for hugetlb folios, whose per-page poison
+	 * state may be tracked out-of-line.
+	 */
+	if (folio_test_hugetlb(folio) && hugetlb_unref_page_hwpoison(page))
+		return true;
 	/* In case we raced with hugetlb transferring flags */
 	return PageHWPoison(page);
 }
mm/memory-failure.c +6 -31
index c0a402522c216..d713eddf47640 100644
@@ -1869,13 +1869,8 @@ bool hugetlb_unref_page_hwpoison(const struct page *page)
 
 	spin_lock_irqsave(&hugetlb_lock, flags);
 	folio = page_folio(page);
-	if (!folio_test_huge_poison(folio)) {
-		ret = PageHWPoison(page);
-		goto unlock;
-	}
-
-	ret = precise_page_poisoned(folio, page);
-unlock:
+	ret = folio_test_hugetlb(folio) &&
+	      hugetlb_page_hwpoison(folio, page);
 	spin_unlock_irqrestore(&hugetlb_lock, flags);
 	return ret;
 }
@@ -1908,23 +1903,6 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
 #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;
-}
-
-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;
-}
-
 /*
  * Set hugetlb folio as hwpoisoned, update folio private raw hwpoison list
  * to keep track of the poisoned pages.
@@ -1933,7 +1911,8 @@ static int hugetlb_update_hwpoison(struct folio *folio, struct page *page)
 {
 	struct hwp_page *p;
 	unsigned long flags;
-	int ret = hugetlb_set_poison(folio);
+	int ret = folio_test_set_has_hwpoisoned(folio) ?
+		  MF_HUGETLB_FOLIO_PRE_POISONED : 0;
 
 	/*
 	 * Once the hwpoison hugepage has lost reliable raw error info,
@@ -2163,10 +2142,6 @@ static inline unsigned long folio_free_raw_hwp(struct folio *folio, bool flag)
 	return 0;
 }
 
-static inline int hugetlb_clear_poison(struct folio *folio)
-{
-	return 0;
-}
 #endif	/* CONFIG_HUGETLB_PAGE */
 
 /* Drop the extra refcount in case we come from madvise() */
@@ -2796,7 +2771,7 @@ int unpoison_memory(unsigned long pfn)
 				hugetlb_unlock_irq();
 				goto unlock_mutex;
 			}
-			ret = hugetlb_clear_poison(folio);
+			ret = folio_test_clear_has_hwpoisoned(folio) ? 0 : -EBUSY;
 		} else {
 			ret = TestClearPageHWPoison(p) ? 0 : -EBUSY;
 		}
@@ -2816,7 +2791,7 @@ int unpoison_memory(unsigned long pfn)
 			folio_put(folio);
 			goto unlock_mutex;
 		}
-		ret = hugetlb_clear_poison(folio);
+		ret = folio_test_clear_has_hwpoisoned(folio) ? 0 : -EBUSY;
 		folio_put(folio);
 		if (!ret)
 			folio_put(folio);
Signature · 13 lines
2.43.0



Makes /proc/kpageflags scanning slower with hugetlb folios, I don't think
sure anybody cares, really. We could add a way to safely check in a racy
way the has_hwpoison flag, but I don't really see the need to right now for some
debug infrastructure that does these racy things.

-- 
Cheers,

David
author maintainer
#24 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 6 lines · David Hildenbrand (Arm)
Quoted 2 lines

Oh, no, it's awful and complicated. We could strip out the two bugfixes which are at the start of the series, but why would we want to do that?

Andrew usually wants us to separate bugfixes (especially stable ones) from other work. Unless we all agree that they are not urgent and can go in in the next cycle. I assume that's the case here.

I don't understand that preference, to be honest. As long as they're first in the patch series, they're easy to pick out and don't depend on the other patches in the series.

Quoted 6 lines

I am not convinced about the huge_poison thing though, that originally caught my attention.

So trying to understand the race we are trying to solve (that is independent of the original idea of the patch set), I realize that these races are only on some slow paths we don't particularly care about for performance.

They are not hot paths, I agree, but they are user-triggerable paths. So I'm not happy about the ability to spin up thousands of threads and delay other accesses to hugetlb_lock indefinitely.

#25 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 11 lines · Matthew Wilcox
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 2 lines

Oh, no, it's awful and complicated. We could strip out the two bugfixes which are at the start of the series, but why would we want to do that?

Andrew usually wants us to separate bugfixes (especially stable ones) from other work. Unless we all agree that they are not urgent and can go in in the next cycle. I assume that's the case here.

I don't understand that preference, to be honest. As long as they're first in the patch series, they're easy to pick out and don't depend on the other patches in the series.

I mean, I agree. However, Andrew tends to fault in the cover letter into the first patch and tracks them in one unit.

Quoted 9 lines
Quoted 7 lines

I am not convinced about the huge_poison thing though, that originally caught my attention.

So trying to understand the race we are trying to solve (that is independent of the original idea of the patch set), I realize that these races are only on some slow paths we don't particularly care about for performance.

They are not hot paths, I agree, but they are user-triggerable paths.

You seem to assume that any of the users would be triggerable by unprivileged users?

Remember: you need to operate on arbitrary PFNs without taking proper references.

/proc/kcore? Certainly not.

/proc/kpageflags? Certainly not.

/sys/kernel/debug/hwpoison/unpoison-pfn or any other software poison injection? Certainly not.

Signature · 3 lines
Cheers,

David
maintainer
#26 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

In reply to Matthew Wilcox
Quoted 11 lines · On Thu, 24 Sep 2026 19:53:09 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 2 lines

Oh, no, it's awful and complicated. We could strip out the two bugfixes which are at the start of the series, but why would we want to do that?

Andrew usually wants us to separate bugfixes (especially stable ones) from other work. Unless we all agree that they are not urgent and can go in in the next cycle. I assume that's the case here.

I don't understand that preference, to be honest. As long as they're first in the patch series, they're easy to pick out and don't depend on the other patches in the series.

It's a timing thing.

Backportable fixes go into mm-hotfixes and get upstreamed in this -rc cycle.

Less urgent material goes into mm-[un]stable and get upstreamed in the next merge window.

I'm OK with merging cc:stable material in the next merge window, in which case there's no need to split the series. But the decision "we don't need to present this to -stable maintainers for another month or two" should be carefully made.

#27 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

In reply to Matthew Wilcox (Oracle)
Quoted 21 lines · 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.

---

mm/Kconfig +0 -0
index 9e0ca4824905..e666dd14ca0c 100644

mm/Kconfig config PERSISTENT_HUGE_ZERO_FOLIO · line 847

Quoted 5 lines
mm/Kconfig +2 -0
@@ -843,11 +843,15 @@ config PERSISTENT_HUGE_ZERO_FOLIO
 config MM_ID
 	def_bool n
 
+config LARGE_FOLIO
+	def_bool n

random nit: def_bool n means the same as just bool

I can't comment on hwpoison stuff, sorry :)

Signature · 1 line
Pedro
maintainer Reviewed-by on v9 8/15
#28 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

In reply to Matthew Wilcox (Oracle)
Quoted 266 lines · 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 | 47 ++++++++++++++++++++++++------
 mm/Kconfig                 |  6 +++-
 mm/hugetlb.c               | 10 +++----
 mm/memory-failure.c        | 58 +++++++++++++++++++++++++++-----------
 mm/rmap.c                  | 43 +++++++++++++++++-----------
 7 files changed, 120 insertions(+), 48 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 +39 -8
index 4185a03a45cf..7e8784e852fb 100644
@@ -893,14 +893,20 @@ static inline int PageTransCompound(const struct page *page)
 TESTPAGEFLAG_FALSE(TransCompound, transcompound)
 #endif
 
-#if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
+#if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_LARGE_FOLIO)
 /*
- * PageHasHWPoisoned indicates that at least one subpage is hwpoisoned in the
- * compound page.
+ * The has_hwpoisoned flag indicates that at least one page is hwpoisoned
+ * in the folio.  That page will usually also have the HWPoison flag
+ * set, but this is not possible for folios which have HugeTLB vmemmap
+ * optimization (see hugetlb_update_hwpoison() for the scheme used
+ * in that case).  You probably don't want to call this directly; use
+ * folio_has_hwpoisoned_page() instead.
  *
  * This flag is set by hwpoison handler.  Cleared by THP split or free page.
  */
 FOLIO_FLAG(has_hwpoisoned, FOLIO_SECOND_PAGE)
+FOLIO_TEST_SET_FLAG(has_hwpoisoned, FOLIO_SECOND_PAGE)
+FOLIO_TEST_CLEAR_FLAG(has_hwpoisoned, FOLIO_SECOND_PAGE)
 #else
 FOLIO_FLAG_FALSE(has_hwpoisoned)
 #endif
@@ -1041,8 +1047,29 @@ PAGE_TYPE_OPS(Slab, slab, slab)
 
 #ifdef CONFIG_HUGETLB_PAGE
 FOLIO_TYPE_OPS(hugetlb, hugetlb)
+
+#ifdef CONFIG_MEMORY_FAILURE
+static inline bool folio_test_huge_poison(const struct folio *folio)
+{
+	return (READ_ONCE(folio->page.page_type) >> 23) ==
+		((PGTY_hugetlb << 1) | 1);
+}
+
+static inline void folio_set_huge_poison(struct folio *folio)
+{
+	folio->page.page_type |= (1 << 23);
+}
+
+static inline void folio_clear_huge_poison(struct folio *folio)
+{
+	folio->page.page_type &= ~(1 << 23);
+}
+#else
+FOLIO_TEST_FLAG_FALSE(huge_poison)
+#endif
 #else
 FOLIO_TEST_FLAG_FALSE(hugetlb)
+FOLIO_TEST_FLAG_FALSE(huge_poison)
 #endif
 
 PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc)
@@ -1069,9 +1096,10 @@ static inline bool PageHuge(const struct page *page)
 }
 
 /*
- * Check if a page is currently marked HWPoisoned. Note that this check is
- * best effort only and inherently racy: there is no way to synchronize with
- * failing hardware.
+ * Check if a page is currently marked HWPoisoned.  This check is best
+ * effort only and inherently racy: there is no way to synchronize with
+ * failing hardware.  The caller may not have a refcount on the folio
+ * containing the page, so we must be careful to not trip any assertions.
  */
 static inline bool is_page_hwpoison(const struct page *page)
 {
@@ -1080,12 +1108,15 @@ static inline bool is_page_hwpoison(const struct page *page)
 	if (PageHWPoison(page))
 		return true;
 	folio = page_folio(page);
-	return folio_test_hugetlb(folio) && PageHWPoison(&folio->page);
+	if (folio_test_huge_poison(folio))
+		return true;
+	/* In case we raced with hugetlb transferring flags */
+	return PageHWPoison(page);
 }
 
 static inline bool folio_has_hwpoisoned_page(const struct folio *folio)
 {
-	return folio_test_hwpoison(folio) ||
+	return PageHWPoison(&folio->page) ||
 	       (folio_test_large(folio) && folio_test_has_hwpoisoned(folio));
 }
 
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 +5 -5
index 47403d02be88..40ae967b9ecc 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))
@@ -1387,7 +1387,7 @@ static void folio_clear_hugetlb(struct folio *folio)
 	 * 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);
 
 	__folio_clear_hugetlb(folio);
@@ -4003,7 +4003,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);
@@ -5814,7 +5814,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;
@@ -6323,7 +6323,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 +22 -4
index 1dd0e7b99bb1..714e1b398f2c 100644
@@ -1829,7 +1829,7 @@ bool is_raw_hwpoison_page_in_hugepage(struct page *page)
 	struct folio *folio = page_folio(page);
 	bool ret = false;
 
-	if (!folio_test_hwpoison(folio))
+	if (!folio_test_has_hwpoisoned(folio))
 		return false;
 
 	if (!folio_test_hugetlb(folio))
@@ -1881,6 +1881,24 @@ 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;
+}
+
+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;
+}
+
 /*
  * Set hugetlb folio as hwpoisoned, update folio private raw hwpoison list
  * to keep track of the poisoned pages.
@@ -1890,12 +1908,12 @@ 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;
+	int ret = hugetlb_set_poison(folio);
 
 	/*
 	 * Once the hwpoison hugepage has lost reliable raw error info,
-	 * there is little meaning to keep additional error info precisely,
-	 * so skip to add additional raw error info.
+	 * there is no point in keeping additional error info precisely,
+	 * so skip adding additional raw error info.
 	 */
 	if (folio_test_hugetlb_raw_hwp_unreliable(folio))
 		return MF_HUGETLB_FOLIO_PRE_POISONED;

mm/memory-failure.c void folio_clear_hugetlb_hwpoison(struct folio *folio) · line 1972

Quoted 6 lines
mm/memory-failure.c +1 -1
@@ -1950,8 +1968,8 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)
 		return;
 	if (folio_test_hugetlb_vmemmap_optimized(folio))
 		return;
-	folio_clear_hwpoison(folio);
 	folio_free_raw_hwp(folio, true);
+	folio_clear_has_hwpoisoned(folio);

Should hugetlb_clear_poison be used here to clear huge_poison flag now that hwpoison flag is transferred to the raw error pages? Apart from this, this patch looks good to me.

Thanks.
.

author maintainer
#29 lore raw

Re: [PATCH v9 08/15] hugetlb: Use the has_hwpoisoned flag

Quoted 10 lines · Miaohe Lin

mm/memory-failure.c void folio_clear_hugetlb_hwpoison(struct folio *folio) · line 1972

Quoted 7 lines
mm/memory-failure.c +1 -1
@@ -1950,8 +1968,8 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)
 		return;
 	if (folio_test_hugetlb_vmemmap_optimized(folio))
 		return;
-	folio_clear_hwpoison(folio);
 	folio_free_raw_hwp(folio, true);
+	folio_clear_has_hwpoisoned(folio);

Should hugetlb_clear_poison be used here to clear huge_poison flag now that hwpoison flag is transferred to the raw error pages?

I think that should be safe. is_page_hwpoison() looks robust against that to me.

Quoted 3 lines

Apart from this, this patch looks good to me.

Thanks!

author maintainer
#30 lore raw

PATCH v9 9/15 mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()

In reply to Matthew Wilcox (Oracle)

Sleeping in this kind of predicate is unexpected. Add a new spinlock to protect access to the list, and turn it into a normal singly linked list now that it doesn't need to be a lockless list.

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 +-
 include/linux/mm_types.h |   4 +-
 mm/memory-failure.c      | 101 ++++++++++++++++++++++-----------------
 4 files changed, 61 insertions(+), 53 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 e200c93088bc..ec604cee8d22 100644
@@ -1088,10 +1088,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)
 {
include/linux/mm_types.h +2 -2
index b18c2b2e7d2c..86a2d0fee557 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 hwp_page *hugetlb_hwpoison;
 	/* private: the union with struct page is transitional */
 		};
 		struct page __page_3;
mm/memory-failure.c +56 -45

167 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer Reviewed-by on v9 9/15
#31 lore raw

Re: [PATCH v9 09/15] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()

Quoted 71 lines · Matthew Wilcox (Oracle)

Sleeping in this kind of predicate is unexpected. Add a new spinlock to protect access to the list, and turn it into a normal singly linked list now that it doesn't need to be a lockless list.

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 +-
 include/linux/mm_types.h |   4 +-
 mm/memory-failure.c      | 101 ++++++++++++++++++++++-----------------
 4 files changed, 61 insertions(+), 53 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 e200c93088bc..ec604cee8d22 100644
@@ -1088,10 +1088,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)
 {
include/linux/mm_types.h +1 -1
index b18c2b2e7d2c..86a2d0fee557 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.
  *

include/linux/mm_types.h struct folio { · line 502

Quoted 5 lines
include/linux/mm_types.h +1 -1
@@ -499,7 +499,7 @@ struct folio {
 			void *_hugetlb_subpool;
 			void *_hugetlb_cgroup;
 			void *_hugetlb_cgroup_rsvd;
-			void *_hugetlb_hwpoison;
+			struct hwp_page *hugetlb_hwpoison;

It might be better to rename it as _hugetlb_hwpoison, i.e. with "_" prefix, to keep the naming style consistant. Anyway:

Thanks.
.

author maintainer
#32 lore raw

Re: [PATCH v9 09/15] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()

Quoted 9 lines · Miaohe Lin

include/linux/mm_types.h struct folio { · line 502

Quoted 6 lines
include/linux/mm_types.h +1 -1
@@ -499,7 +499,7 @@ struct folio {
 			void *_hugetlb_subpool;
 			void *_hugetlb_cgroup;
 			void *_hugetlb_cgroup_rsvd;
-			void *_hugetlb_hwpoison;
+			struct hwp_page *hugetlb_hwpoison;

It might be better to rename it as _hugetlb_hwpoison, i.e. with "_" prefix, to keep the naming style consistant. Anyway:

The '_' prefix means "don't use directly", but there's no reason not to use it directly.

Quoted 4 lines

Thanks.
.

author maintainer
#33 lore raw

PATCH v9 10/15 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 7e8784e852fb..5d01e5b28d0f 100644
@@ -1095,6 +1095,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
@@ -1109,7 +1111,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
maintainer Reviewed-by on v9 10/15
#34 lore raw

Re: [PATCH v9 10/15] mm: Check individual hugetlb pages for poison

Quoted 5 lines · 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.

Quoted 7 lines
---
 include/linux/page-flags.h |  4 +++-
 mm/memory-failure.c        | 43 +++++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 8 deletions(-)

Thanks.
.

author maintainer
#35 lore raw

PATCH v9 11/15 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 ec604cee8d22..639c0a772856 100644
@@ -1088,8 +1088,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 5d01e5b28d0f..f75d66c42509 100644
@@ -1095,6 +1095,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);
 
 /*
@@ -1116,6 +1117,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
#36 lore raw

Re: [PATCH v9 11/15] filemap: Add hwpoison handling to filemap_read()

Quoted 71 lines · 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 ec604cee8d22..639c0a772856 100644
@@ -1088,8 +1088,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 5d01e5b28d0f..f75d66c42509 100644
@@ -1095,6 +1095,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);
 
 /*
@@ -1116,6 +1117,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 +2 -0
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))

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2758

Quoted 8 lines
mm/filemap.c +5 -0
@@ -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;

offset >> PAGE_SHIFT would probably be more idiomatic

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2766

Quoted 8 lines
mm/filemap.c +8 -0
+
+	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);

similarly, offset_in_page(offset)

(yes, no codegen difference in both cases, but there could be a difference if the ARM dynamic page size stuff ever moves forward)

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2775

Quoted 9 lines
mm/filemap.c +9 -0
+	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);
+}

Wouldn't this whole logic fit better in copy_folio_to_iter? shmem for instance also handrolls its own hwpoison read_iter logic with per-page-copies, etc; not amazing.

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

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

I don't love reusing copied here, but I guess there isn't much better (in order to detect hwpoison-derived truncation)

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

Quoted 10 lines
mm/filemap.c +3 -1
+			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;

Logic looks good though.

Signature · 1 line
Pedro
author maintainer
#37 lore raw

Re: [PATCH v9 11/15] filemap: Add hwpoison handling to filemap_read()

Quoted 6 lines · Pedro Falcato

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2758

Quoted 5 lines
mm/filemap.c +5 -0
+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;

offset >> PAGE_SHIFT would probably be more idiomatic

$ git grep '/ PAGE_SIZE' mm fs |wc -l
116
$ git grep '>> PAGE_SHIFT' mm fs |wc -l
656

it seems relatively common. Personally I find / easier to understand because I sometimes get confused between left and right shifts. I don't think I've ever been confused between multiply and divide ;-)

Quoted 3 lines

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2766

Quoted 2 lines
mm/filemap.c +2 -0
+	/* Safe to read the remaining bytes in this page. */
+	safe_bytes = PAGE_SIZE - (offset % PAGE_SIZE);

similarly, offset_in_page(offset)

yes, that's easier to read.

Quoted 2 lines

(yes, no codegen difference in both cases, but there could be a difference if the ARM dynamic page size stuff ever moves forward)

I rather hope it doesn't ... but I do hope it manages to strength-reduce if it does.

Quoted 5 lines
Quoted 5 lines
mm/filemap.c +5 -0
+	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))
Quoted 8 lines

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2775

Quoted 4 lines
mm/filemap.c +4 -0
+			break;
+
+	return min(safe_bytes, bytes);
+}

Wouldn't this whole logic fit better in copy_folio_to_iter? shmem for instance also handrolls its own hwpoison read_iter logic with per-page-copies, etc; not amazing.

I did wonder wbout doing that. We don't currently have the folio in copy_page_to_iter(), and copy_folio_to_iter() decays to copy_page_to_iter() immediately. So it'd be a larger change.

Quoted 8 lines

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

Quoted 5 lines
mm/filemap.c +3 -1
-			copied = copy_folio_to_iter(folio, offset, bytes, iter);
+			copied = adjust_range_hwpoison(folio, offset, bytes);
+			if (copied < bytes)
+				error = -EIO;

I don't love reusing copied here, but I guess there isn't much better (in order to detect hwpoison-derived truncation)

I thought this was neater than using a separate variable.

#38 lore raw

Re: [PATCH v9 11/15] filemap: Add hwpoison handling to filemap_read()

Quoted 8 lines · Matthew Wilcox
Quoted 6 lines · Pedro Falcato

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2758

Quoted 5 lines
mm/filemap.c +5 -0
+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;

offset >> PAGE_SHIFT would probably be more idiomatic

Quoted 8 lines
$ git grep '/ PAGE_SIZE' mm fs |wc -l
116
$ git grep '>> PAGE_SHIFT' mm fs |wc -l
656

it seems relatively common. Personally I find / easier to understand because I sometimes get confused between left and right shifts. I don't think I've ever been confused between multiply and divide ;-)

Linus once explained why ">> PAGE_SHIFT" is preferable when the type of the function might be signed, as the shift generates better code.

I personally find "offset / PAGE_SIZE" more intuitive, but I also have sympathy for PHYS_PFN() to abstract the operation.

(SIZE_TO_PAGES would be a bit mouthful so I wouldn't ever dare suggesting to introduce a new helper)

Signature · 3 lines
Cheers,

David
author maintainer
#39 lore raw

Re: [PATCH v9 11/15] filemap: Add hwpoison handling to filemap_read()

Quoted 18 lines · David Hildenbrand (Arm)
Quoted 8 lines · Matthew Wilcox
Quoted 6 lines · Pedro Falcato

mm/filemap.c static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio) · line 2758

Quoted 5 lines
mm/filemap.c +5 -0
+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;

offset >> PAGE_SHIFT would probably be more idiomatic

Quoted 8 lines
$ git grep '/ PAGE_SIZE' mm fs |wc -l
116
$ git grep '>> PAGE_SHIFT' mm fs |wc -l
656

it seems relatively common. Personally I find / easier to understand because I sometimes get confused between left and right shifts. I don't think I've ever been confused between multiply and divide ;-)

Quoted 5 lines

Linus once explained why ">> PAGE_SHIFT" is preferable when the type of the function might be signed, as the shift generates better code.

I personally find "offset / PAGE_SIZE" more intuitive, but I also have sympathy for PHYS_PFN() to abstract the operation.

Oof. I appreciate PHYS_PFN() is literally 'shift by PAGE_SHIFT' but it implies that the parameter is a physical address, which this isn't (it's a byte offset within a folio).

Quoted 2 lines

(SIZE_TO_PAGES would be a bit mouthful so I wouldn't ever dare suggesting to introduce a new helper)

I wouldn't mind a new helper, but it'd have to be a pithy name, and I don't have one in mind.

author maintainer
#40 lore raw

PATCH v9 12/15 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..968b791cfd14 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_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_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_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_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_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_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
Reviewed-by on v9 12/15
#41 lore raw

Re: [PATCH v9 12/15] filemap: Remove checks in mapping_set_folio_order_range()

Quoted 6 lines · 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.

Signature · 1 line
Pedro
author maintainer
#42 lore raw

PATCH v9 13/15 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
Reviewed-by on v9 13/15
#43 lore raw

Re: [PATCH v9 13/15] hugetlb: Set mapping folio order

Quoted 11 lines · 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.

Signature · 1 line
Pedro
maintainer Reviewed-by on v9 13/15
#44 lore raw

Re: [PATCH v9 13/15] hugetlb: Set mapping folio order

In reply to Matthew Wilcox (Oracle)
Quoted 37 lines · 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 +10 -0
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

fs/hugetlbfs/inode.c static struct inode *hugetlbfs_get_inode(struct super_block *sb, · line 942

Quoted 9 lines
fs/hugetlbfs/inode.c +1 -2
@@ -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;

Yeah, that's very nice.

Signature · 3 lines
Cheers,

David
author maintainer
#45 lore raw

PATCH v9 14/15 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 968b791cfd14..22d48935ffda 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
Reviewed-by on v9 14/15
#46 lore raw

Re: [PATCH v9 14/15] filemap: Add support for authoritative mappings

Quoted 64 lines · 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 968b791cfd14..22d48935ffda 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 +8 -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);

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

Quoted 10 lines
mm/filemap.c +10 -0
+
+			already_read += copied;
+			iocb->ki_pos += copied;
+			last_pos = iocb->ki_pos;
+
+			if (copied < bytes)
+				error = -EFAULT;
+			continue;
+		}
+

I was wondering whether this could interact weirdly with EOF, but it doesn't seem so.

Signature · 1 line
Pedro
#47 lore raw

Re: [PATCH v9 14/15] filemap: Add support for authoritative mappings

In reply to Matthew Wilcox (Oracle)
Quoted 17 lines · 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 +0 -0
index 968b791cfd14..22d48935ffda 100644

include/linux/pagemap.h enum mapping_flags { · line 213

Quoted 4 lines
include/linux/pagemap.h +1 -0
@@ -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 */

Just curious if "in-memory file system" would be a different way of looking at it. There, a hole in the page cache always expresses a hole in the file.

AS_IN_MEMORY_FILE

Just an idea, because that's why I always had in mind regarding that (all state in memory).

Signature · 3 lines
Cheers,

David
author maintainer
#48 lore raw

Re: [PATCH v9 14/15] filemap: Add support for authoritative mappings

Quoted 11 lines · David Hildenbrand (Arm)

include/linux/pagemap.h enum mapping_flags { · line 213

Quoted 6 lines · Matthew Wilcox (Oracle)

+++ b/include/linux/pagemap.h

include/linux/pagemap.h +1 -0
@@ -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 */

Just curious if "in-memory file system" would be a different way of looking at it. There, a hole in the page cache always expresses a hole in the file.

Quoted 4 lines

AS_IN_MEMORY_FILE

Just an idea, because that's why I always had in mind regarding that (all state in memory).

I suspect that's always going to be the case, but I don't want to be quite so prescriptive. I'd rather describe the property than the user.

#49 lore raw

Re: [PATCH v9 14/15] filemap: Add support for authoritative mappings

Quoted 12 lines · Matthew Wilcox
Quoted 11 lines · David Hildenbrand (Arm)

include/linux/pagemap.h enum mapping_flags { · line 213

Quoted 6 lines · Matthew Wilcox (Oracle)

+++ b/include/linux/pagemap.h

include/linux/pagemap.h +1 -0
@@ -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 */

Just curious if "in-memory file system" would be a different way of looking at it. There, a hole in the page cache always expresses a hole in the file.

Quoted 7 lines
Quoted 4 lines

AS_IN_MEMORY_FILE

Just an idea, because that's why I always had in mind regarding that (all state in memory).

I suspect that's always going to be the case, but I don't want to be quite so prescriptive. I'd rather describe the property than the user.

Hard to imagine other users but I guess nothing is impossible :)

Signature · 3 lines
Cheers,

David
author maintainer
#50 lore raw

PATCH v9 15/15 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
Reviewed-by on v9 15/15
#51 lore raw

Re: [PATCH v9 15/15] hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()

Quoted 10 lines · 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().

Awesome!

Signature · 1 line
Pedro
#52 lore raw
In reply to Matthew Wilcox (Oracle)

Hi, Matthew,

Quoted 9 lines · Matthew Wilcox (Oracle)

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

We need three things to use filemap_read_iter() instead of a hugetlbfs specific read_iter: hwpoison handling, treating gaps in the pagecache as holes, and having hugetlb set the folio size in the mapping. hwopison handling turned out to need a lot of prep work; the other two things are fairly straightforward.

Quoted 4 lines

Sashiko complains that the implementation of AS_AUTHORITATIVE is incomplete, but hugetlbfs doesn't use any of that functionality. We'll add it when we need it, either when hugetlbfs uses it, or when we convert shmem to use filemap.

I've run some tests (read over hole, hwpoison etc) for the series, the results look good.

-thanks,
-jane

#53 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 41 lines · Matthew Wilcox (Oracle)

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

We need three things to use filemap_read_iter() instead of a hugetlbfs specific read_iter: hwpoison handling, treating gaps in the pagecache as holes, and having hugetlb set the folio size in the mapping. hwopison handling turned out to need a lot of prep work; the other two things are fairly straightforward.

Sashiko complains that the implementation of AS_AUTHORITATIVE is incomplete, but hugetlbfs doesn't use any of that functionality. We'll add it when we need it, either when hugetlbfs uses it, or when we convert shmem to use filemap.

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

Changes from v7:
- Fix the KPF_HWPOISON blooper
- Expand HVO in a comment
- Collect R-b tags from Gregory & Jane

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

Matthew Wilcox (Oracle) (13):
memory-failure: Fix hardware poison check in unpoison_memory() again
memory-failure: Prevent hugetlb freeing during unpoisoning
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()
kpageflags: Use is_page_hwpoison() to set KPF_HWPOISON
hugetlb: Move poison to pages before clearing hugetlb page type
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

Quoted 14 lines
 fs/Kconfig                 |   2 +-
 fs/hugetlbfs/inode.c       | 123 +++--------------------
 fs/proc/page.c             |   8 +-
 include/linux/hugetlb.h    |  43 +++++---
 include/linux/mm_types.h   |   4 +-
 include/linux/page-flags.h |  64 ++++++++++--
 include/linux/pagemap.h    |  45 +++++----
 mm/Kconfig                 |   6 +-
 mm/filemap.c               |  51 +++++++++-
 mm/huge_memory.c           |   2 +-
 mm/hugetlb.c               |  41 ++++----
 mm/memory-failure.c        | 197 +++++++++++++++++++++++++------------
 mm/memory_hotplug.c        |   2 +-
 mm/rmap.c                  |  43 +++++---

Much as I'd like to not look at this, nor think about it, nor even admit that any of this actually exists, I do co-maintain the rmap with David so please do cc- me on future revisions :)

Quoted 9 lines
 mm/shmem.c                 |   2 +-
 mm/vmscan.c                |   2 +-
 virt/kvm/guest_memfd.c     |   2 +-
 17 files changed, 368 insertions(+), 269 deletions(-)

--
2.47.3

--
Cheers, Lorenzo