Skip to contentSkip to conversation

mm/memory_failure: reject unsupported non-folio compound page

PATCH In review Zi Yan 3 participants 6 messages Feb 5

Subsystems (3)
lore
Share & tools
lore

Zi Yan

author
#0 lore raw

PATCH 1/1 mm/memory_failure: reject unsupported non-folio compound page

When !CONFIG_TRANSPARENT_HUGEPAGE, a non-folio compound page can appear in a userspace mapping via either vm_insert_*() functions or vm_operatios_struct->fault(). They are not folios, thus should not be considered for folio operations like split. Change memory_failure() and soft_offline_page() to reject these non-folio compound pages as EOPNOTSUPP.

Add PageNonFolioCompound() helper function. This function is functionally equivalent to folio_test_large() && !folio_test_large_rmappable(), but it is supposed to be used on struct page. So open code it instead.

---
 include/linux/page-flags.h | 16 ++++++++++++++++
 mm/memory-failure.c        |  9 ++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/page-flags.h +16 -0
index f7a0e4af0c73..2fe8047f42a3 100644
@@ -1102,6 +1102,22 @@ static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
 
 bool is_free_buddy_page(const struct page *page);
 
+static inline bool PageNonFolioCompound(const struct page *page)
+{
+	if (PageCompound(page)) {
+		const struct page *head = compound_head(page);
+
+		/*
+		 * Without CONFIG_TRANSPARENT_HUGEPAGE, PG_large_rmappable
+		 * should not be set/used.
+		 */
+		return !IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) ||
+		       !test_bit(PG_large_rmappable, &head[1].flags.f);
+	}
+
+	return false;
+}
+
 #ifdef CONFIG_MIGRATION
 /*
  * This page is migratable through movable_ops (for selected typed pages
mm/memory-failure.c +6 -3
index cf0d526e6d41..8b6b5950bb66 100644
@@ -2440,9 +2440,12 @@ int memory_failure(unsigned long pfn, int flags)
 
 	folio = page_folio(p);
 
-	/* filter pages that are protected from hwpoison test by users */
+	/*
+	 * filter pages that are protected from hwpoison test by users or
+	 * unsupported non folio compound ones
+	 */
 	folio_lock(folio);
-	if (hwpoison_filter(p)) {
+	if (hwpoison_filter(p) || PageNonFolioCompound(p)) {
 		ClearPageHWPoison(p);
 		folio_unlock(folio);
 		folio_put(folio);
@@ -2945,7 +2948,7 @@ int soft_offline_page(unsigned long pfn, int flags)
 	ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
 	put_online_mems();
 
-	if (hwpoison_filter(page)) {
+	if (hwpoison_filter(page) || PageNonFolioCompound(page)) {
 		if (ret > 0)
 			put_page(page);
 
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 67 lines · Zi Yan

When !CONFIG_TRANSPARENT_HUGEPAGE, a non-folio compound page can appear in a userspace mapping via either vm_insert_*() functions or vm_operatios_struct->fault(). They are not folios, thus should not be considered for folio operations like split. Change memory_failure() and soft_offline_page() to reject these non-folio compound pages as EOPNOTSUPP.

Add PageNonFolioCompound() helper function. This function is functionally equivalent to folio_test_large() && !folio_test_large_rmappable(), but it is supposed to be used on struct page. So open code it instead.

---
 include/linux/page-flags.h | 16 ++++++++++++++++
 mm/memory-failure.c        |  9 ++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/page-flags.h +16 -0
index f7a0e4af0c73..2fe8047f42a3 100644
@@ -1102,6 +1102,22 @@ static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
 
 bool is_free_buddy_page(const struct page *page);
 
+static inline bool PageNonFolioCompound(const struct page *page)
+{
+	if (PageCompound(page)) {
+		const struct page *head = compound_head(page);
+
+		/*
+		 * Without CONFIG_TRANSPARENT_HUGEPAGE, PG_large_rmappable
+		 * should not be set/used.
+		 */
+		return !IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) ||
+		       !test_bit(PG_large_rmappable, &head[1].flags.f);
+	}
+
+	return false;
+}
+
 #ifdef CONFIG_MIGRATION
 /*
  * This page is migratable through movable_ops (for selected typed pages
mm/memory-failure.c +5 -2
index cf0d526e6d41..8b6b5950bb66 100644
@@ -2440,9 +2440,12 @@ int memory_failure(unsigned long pfn, int flags)
 
 	folio = page_folio(p);
 
-	/* filter pages that are protected from hwpoison test by users */
+	/*
+	 * filter pages that are protected from hwpoison test by users or
+	 * unsupported non folio compound ones
+	 */
 	folio_lock(folio);
-	if (hwpoison_filter(p)) {
+	if (hwpoison_filter(p) || PageNonFolioCompound(p)) {
 		ClearPageHWPoison(p);
 		folio_unlock(folio);
 		folio_put(folio);

mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2951

Quoted 5 lines
mm/memory-failure.c +1 -1
@@ -2945,7 +2948,7 @@ int soft_offline_page(unsigned long pfn, int flags)
 	ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
 	put_online_mems();
 
-	if (hwpoison_filter(page)) {
+	if (hwpoison_filter(page) || PageNonFolioCompound(page)) {

There should be no problem in soft_offline_page(). HWPoisonHandlable() check will be used by get_hwpoison_page() to reject PageNonFolioCompound folios. Or am I miss something?

Thanks.
.

author
#2 lore raw
Quoted 71 lines · Miaohe Lin
Quoted 70 lines · Zi Yan

When !CONFIG_TRANSPARENT_HUGEPAGE, a non-folio compound page can appear in a userspace mapping via either vm_insert_*() functions or vm_operatios_struct->fault(). They are not folios, thus should not be considered for folio operations like split. Change memory_failure() and soft_offline_page() to reject these non-folio compound pages as EOPNOTSUPP.

Add PageNonFolioCompound() helper function. This function is functionally equivalent to folio_test_large() && !folio_test_large_rmappable(), but it is supposed to be used on struct page. So open code it instead.

---
 include/linux/page-flags.h | 16 ++++++++++++++++
 mm/memory-failure.c        |  9 ++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/page-flags.h +16 -0
index f7a0e4af0c73..2fe8047f42a3 100644
@@ -1102,6 +1102,22 @@ static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
 
 bool is_free_buddy_page(const struct page *page);
 
+static inline bool PageNonFolioCompound(const struct page *page)
+{
+	if (PageCompound(page)) {
+		const struct page *head = compound_head(page);
+
+		/*
+		 * Without CONFIG_TRANSPARENT_HUGEPAGE, PG_large_rmappable
+		 * should not be set/used.
+		 */
+		return !IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) ||
+		       !test_bit(PG_large_rmappable, &head[1].flags.f);
+	}
+
+	return false;
+}
+
 #ifdef CONFIG_MIGRATION
 /*
  * This page is migratable through movable_ops (for selected typed pages
mm/memory-failure.c +5 -2
index cf0d526e6d41..8b6b5950bb66 100644
@@ -2440,9 +2440,12 @@ int memory_failure(unsigned long pfn, int flags)
 
 	folio = page_folio(p);
 
-	/* filter pages that are protected from hwpoison test by users */
+	/*
+	 * filter pages that are protected from hwpoison test by users or
+	 * unsupported non folio compound ones
+	 */
 	folio_lock(folio);
-	if (hwpoison_filter(p)) {
+	if (hwpoison_filter(p) || PageNonFolioCompound(p)) {
 		ClearPageHWPoison(p);
 		folio_unlock(folio);
 		folio_put(folio);
@@ -2945,7 +2948,7 @@ int soft_offline_page(unsigned long pfn, int flags)
 	ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
 	put_online_mems();
 
Quoted 5 lines

mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2951

Quoted 2 lines
mm/memory-failure.c +1 -1
-	if (hwpoison_filter(page)) {
+	if (hwpoison_filter(page) || PageNonFolioCompound(page)) {

There should be no problem in soft_offline_page(). HWPoisonHandlable() check will be used by get_hwpoison_page() to reject PageNonFolioCompound folios. Or am I miss something?

I did not know that. Why does memory_failure() not call HWPosonHandlable() to check the input page? It looks to me that HWPosonHandlable() is more appropriate than PageNonFolioCompound() here.

--
Best Regards,
Yan, Zi

#3 lore raw
Quoted 73 lines · Zi Yan
Quoted 71 lines · Miaohe Lin
Quoted 70 lines · Zi Yan

When !CONFIG_TRANSPARENT_HUGEPAGE, a non-folio compound page can appear in a userspace mapping via either vm_insert_*() functions or vm_operatios_struct->fault(). They are not folios, thus should not be considered for folio operations like split. Change memory_failure() and soft_offline_page() to reject these non-folio compound pages as EOPNOTSUPP.

Add PageNonFolioCompound() helper function. This function is functionally equivalent to folio_test_large() && !folio_test_large_rmappable(), but it is supposed to be used on struct page. So open code it instead.

---
 include/linux/page-flags.h | 16 ++++++++++++++++
 mm/memory-failure.c        |  9 ++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/page-flags.h +16 -0
index f7a0e4af0c73..2fe8047f42a3 100644
@@ -1102,6 +1102,22 @@ static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
 
 bool is_free_buddy_page(const struct page *page);
 
+static inline bool PageNonFolioCompound(const struct page *page)
+{
+	if (PageCompound(page)) {
+		const struct page *head = compound_head(page);
+
+		/*
+		 * Without CONFIG_TRANSPARENT_HUGEPAGE, PG_large_rmappable
+		 * should not be set/used.
+		 */
+		return !IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) ||
+		       !test_bit(PG_large_rmappable, &head[1].flags.f);
+	}
+
+	return false;
+}
+
 #ifdef CONFIG_MIGRATION
 /*
  * This page is migratable through movable_ops (for selected typed pages
mm/memory-failure.c +5 -2
index cf0d526e6d41..8b6b5950bb66 100644
@@ -2440,9 +2440,12 @@ int memory_failure(unsigned long pfn, int flags)
 
 	folio = page_folio(p);
 
-	/* filter pages that are protected from hwpoison test by users */
+	/*
+	 * filter pages that are protected from hwpoison test by users or
+	 * unsupported non folio compound ones
+	 */
 	folio_lock(folio);
-	if (hwpoison_filter(p)) {
+	if (hwpoison_filter(p) || PageNonFolioCompound(p)) {
 		ClearPageHWPoison(p);
 		folio_unlock(folio);
 		folio_put(folio);
@@ -2945,7 +2948,7 @@ int soft_offline_page(unsigned long pfn, int flags)
 	ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
 	put_online_mems();
 
Quoted 5 lines
Quoted 5 lines

mm/memory-failure.c int soft_offline_page(unsigned long pfn, int flags) · line 2951

Quoted 2 lines
mm/memory-failure.c +1 -1
-	if (hwpoison_filter(page)) {
+	if (hwpoison_filter(page) || PageNonFolioCompound(page)) {

There should be no problem in soft_offline_page(). HWPoisonHandlable() check will be used by get_hwpoison_page() to reject PageNonFolioCompound folios. Or am I miss something?

Oops, I missed Miohe's email.
So it's rejected because it's not on LRU.

Quoted 3 lines

I did not know that. Why does memory_failure() not call HWPosonHandlable() to check the input page? It looks to me that HWPosonHandlable() is more appropriate than PageNonFolioCompound() here.

soft_offline_page() unconditionally calls get_hwpoison_page(), but memory_failure() doesn't call it if MF_COUNT_INCREASED is set.

(MF_COUNT_INCREASED set by madvise_inject_error(), in this case)

Signature · 2 lines
Cheers,
Harry / Hyeonggon
Acked-by on v1 1/1
#4 lore raw
In reply to Zi Yan
Quoted 11 lines · Zi Yan

When !CONFIG_TRANSPARENT_HUGEPAGE, a non-folio compound page can appear in a userspace mapping via either vm_insert_*() functions or vm_operatios_struct->fault(). They are not folios, thus should not be considered for folio operations like split. Change memory_failure() and soft_offline_page() to reject these non-folio compound pages as EOPNOTSUPP.

Add PageNonFolioCompound() helper function. This function is functionally equivalent to folio_test_large() && !folio_test_large_rmappable(), but it is supposed to be used on struct page. So open code it instead.

Quoted 5 lines

---

Looks good to me,

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#5 lore raw
In reply to Zi Yan

Hi Andrew,

Miaohe has a more appropriate solution[1]. I think it is better to drop this patch and wait for Miaohe's proper patch fix.

Thanks.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 77 lines · Zi Yan

When !CONFIG_TRANSPARENT_HUGEPAGE, a non-folio compound page can appear in a userspace mapping via either vm_insert_*() functions or vm_operatios_struct->fault(). They are not folios, thus should not be considered for folio operations like split. Change memory_failure() and soft_offline_page() to reject these non-folio compound pages as EOPNOTSUPP.

Add PageNonFolioCompound() helper function. This function is functionally equivalent to folio_test_large() && !folio_test_large_rmappable(), but it is supposed to be used on struct page. So open code it instead.

---
 include/linux/page-flags.h | 16 ++++++++++++++++
 mm/memory-failure.c        |  9 ++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/page-flags.h +16 -0
index f7a0e4af0c73..2fe8047f42a3 100644
@@ -1102,6 +1102,22 @@ static inline bool folio_contain_hwpoisoned_page(struct folio *folio)
 
 bool is_free_buddy_page(const struct page *page);
 
+static inline bool PageNonFolioCompound(const struct page *page)
+{
+	if (PageCompound(page)) {
+		const struct page *head = compound_head(page);
+
+		/*
+		 * Without CONFIG_TRANSPARENT_HUGEPAGE, PG_large_rmappable
+		 * should not be set/used.
+		 */
+		return !IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) ||
+		       !test_bit(PG_large_rmappable, &head[1].flags.f);
+	}
+
+	return false;
+}
+
 #ifdef CONFIG_MIGRATION
 /*
  * This page is migratable through movable_ops (for selected typed pages
mm/memory-failure.c +6 -3
index cf0d526e6d41..8b6b5950bb66 100644
@@ -2440,9 +2440,12 @@ int memory_failure(unsigned long pfn, int flags)
 
 	folio = page_folio(p);
 
-	/* filter pages that are protected from hwpoison test by users */
+	/*
+	 * filter pages that are protected from hwpoison test by users or
+	 * unsupported non folio compound ones
+	 */
 	folio_lock(folio);
-	if (hwpoison_filter(p)) {
+	if (hwpoison_filter(p) || PageNonFolioCompound(p)) {
 		ClearPageHWPoison(p);
 		folio_unlock(folio);
 		folio_put(folio);
@@ -2945,7 +2948,7 @@ int soft_offline_page(unsigned long pfn, int flags)
 	ret = get_hwpoison_page(page, flags | MF_SOFT_OFFLINE);
 	put_online_mems();
 
-	if (hwpoison_filter(page)) {
+	if (hwpoison_filter(page) || PageNonFolioCompound(page)) {
 		if (ret > 0)
 			put_page(page);
 
Signature · 1 line
2.51.0

--
Best Regards,
Yan, Zi