2023-01-12 21:35:02

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH mm-unstable 0/8] convert hugepage memory failure functions to folios

============== OVERVIEW ===========================
This series contains a 1:1 straightforward page to folio conversion for
memory failure functions which deal with huge pages. I renamed a few
functions to fit with how other folio operating functions are named.
These include:

hugetlb_clear_page_hwpoison -> folio_clear_hugetlb_hwpoison
free_raw_hwp_pages -> folio_free_raw_hwp
__free_raw_hwp_pages -> __folio_free_raw_hwp
hugetlb_set_page_hwpoison -> folio_set_hugetlb_hwpoison

The goal of this series was to reduce users of the hugetlb specific
page flag macros which take in a page so users are protected by
the compiler to make sure they are operating on a head page.

Sidhartha Kumar (8):
mm/memory-failure: convert __get_huge_page_for_hwpoison() to folios
mm/memory-failure: convert try_memory_failure_hugetlb() to folios
mm/memory-failure: convert hugetlb_clear_page_hwpoison to folios
mm/memory-failure: convert free_raw_hwp_pages() to folios
mm/memory-failure: convert raw_hwp_list_head() to folios
mm/memory-failure: convert __free_raw_hwp_pages() to folios
mm/memory-failure: convert hugetlb_set_page_hwpoison() to folios
mm/memory-failure: convert unpoison_memory() to folios

include/linux/hugetlb.h | 4 +-
mm/hugetlb.c | 2 +-
mm/memory-failure.c | 116 ++++++++++++++++++++--------------------
3 files changed, 61 insertions(+), 61 deletions(-)

--
2.39.0


2023-01-12 21:36:03

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH mm-unstable 8/8] mm/memory-failure: convert unpoison_memory() to folios

Use a folio inside unpoison_memory which replaces a compound_head() call
with a call to page_folio().

Signed-off-by: Sidhartha Kumar <[email protected]>
---
mm/memory-failure.c | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index fb98652b211a..c53588ea5993 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -2335,7 +2335,6 @@ core_initcall(memory_failure_init);
*/
int unpoison_memory(unsigned long pfn)
{
- struct page *page;
struct folio *folio;
struct page *p;
int ret = -EBUSY;
@@ -2348,7 +2347,6 @@ int unpoison_memory(unsigned long pfn)
return -ENXIO;

p = pfn_to_page(pfn);
- page = compound_head(p);
folio = page_folio(p);

mutex_lock(&mf_mutex);
@@ -2360,31 +2358,31 @@ int unpoison_memory(unsigned long pfn)
goto unlock_mutex;
}

- if (!PageHWPoison(p)) {
+ if (!folio_test_hwpoison(folio)) {
unpoison_pr_info("Unpoison: Page was already unpoisoned %#lx\n",
pfn, &unpoison_rs);
goto unlock_mutex;
}

- if (page_count(page) > 1) {
+ if (folio_ref_count(folio) > 1) {
unpoison_pr_info("Unpoison: Someone grabs the hwpoison page %#lx\n",
pfn, &unpoison_rs);
goto unlock_mutex;
}

- if (page_mapped(page)) {
+ if (folio_mapped(folio)) {
unpoison_pr_info("Unpoison: Someone maps the hwpoison page %#lx\n",
pfn, &unpoison_rs);
goto unlock_mutex;
}

- if (page_mapping(page)) {
+ if (folio_mapping(folio)) {
unpoison_pr_info("Unpoison: the hwpoison page has non-NULL mapping %#lx\n",
pfn, &unpoison_rs);
goto unlock_mutex;
}

- if (PageSlab(page) || PageTable(page) || PageReserved(page))
+ if (folio_test_slab(folio) || PageTable(&folio->page) || folio_test_reserved(folio))
goto unlock_mutex;

ret = get_hwpoison_page(p, MF_UNPOISON);
@@ -2397,7 +2395,7 @@ int unpoison_memory(unsigned long pfn)
goto unlock_mutex;
}
}
- ret = TestClearPageHWPoison(page) ? 0 : -EBUSY;
+ ret = folio_test_clear_hwpoison(folio) ? 0 : -EBUSY;
} else if (ret < 0) {
if (ret == -EHWPOISON) {
ret = put_page_back_buddy(p) ? 0 : -EBUSY;
@@ -2410,14 +2408,14 @@ int unpoison_memory(unsigned long pfn)
count = folio_free_raw_hwp(folio, false);
if (count == 0) {
ret = -EBUSY;
- put_page(page);
+ folio_put(folio);
goto unlock_mutex;
}
}

- put_page(page);
+ folio_put(folio);
if (TestClearPageHWPoison(p)) {
- put_page(page);
+ folio_put(folio);
ret = 0;
}
}
--
2.39.0

2023-01-12 21:37:16

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH mm-unstable 3/8] mm/memory-failure: convert hugetlb_clear_page_hwpoison to folios

Change hugetlb_clear_page_hwpoison() to folio_clear_hugetlb_hwpoison() by
changing the function to take in a folio. This converts one use of
ClearPageHWPoison and HPageRawHwpUnreliable to their folio equivalents.

Signed-off-by: Sidhartha Kumar <[email protected]>
---
include/linux/hugetlb.h | 4 ++--
mm/hugetlb.c | 2 +-
mm/memory-failure.c | 10 +++++-----
3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index befe64cb40d1..c33e36f5a1dd 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -823,9 +823,9 @@ extern int dissolve_free_huge_pages(unsigned long start_pfn,
unsigned long end_pfn);

#ifdef CONFIG_MEMORY_FAILURE
-extern void hugetlb_clear_page_hwpoison(struct page *hpage);
+extern void folio_clear_hugetlb_hwpoison(struct folio *folio);
#else
-static inline void hugetlb_clear_page_hwpoison(struct page *hpage)
+static inline void folio_clear_hugetlb_hwpoison(struct folio *folio)
{
}
#endif
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index ae9c6ba07a8a..a5d410c16cbe 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1592,7 +1592,7 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
* which makes any healthy subpages reusable.
*/
if (unlikely(folio_test_hwpoison(folio)))
- hugetlb_clear_page_hwpoison(&folio->page);
+ folio_clear_hugetlb_hwpoison(folio);

for (i = 0; i < pages_per_huge_page(h); i++) {
subpage = folio_page(folio, i);
diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index ca84b27489d0..205272c64f37 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1785,12 +1785,12 @@ static unsigned long free_raw_hwp_pages(struct page *hpage, bool move_flag)
return __free_raw_hwp_pages(hpage, move_flag);
}

-void hugetlb_clear_page_hwpoison(struct page *hpage)
+void folio_clear_hugetlb_hwpoison(struct folio *folio)
{
- if (HPageRawHwpUnreliable(hpage))
+ if (folio_test_hugetlb_raw_hwp_unreliable(folio))
return;
- ClearPageHWPoison(hpage);
- free_raw_hwp_pages(hpage, true);
+ folio_clear_hwpoison(folio);
+ free_raw_hwp_pages(&folio->page, true);
}

/*
@@ -1889,7 +1889,7 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb
folio_lock(folio);

if (hwpoison_filter(p)) {
- hugetlb_clear_page_hwpoison(&folio->page);
+ folio_clear_hugetlb_hwpoison(folio);
if (migratable_cleared)
folio_set_hugetlb_migratable(folio);
folio_unlock(folio);
--
2.39.0

2023-01-12 21:50:33

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH mm-unstable 1/8] mm/memory-failure: convert __get_huge_page_for_hwpoison() to folios

Use a folio throughout the function rather than using a head page. This
also reduces the users of the page version of hugetlb specific page flags.

Signed-off-by: Sidhartha Kumar <[email protected]>
---
mm/memory-failure.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 9a80d7830b40..ee7e79ec6eaf 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1807,20 +1807,20 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
bool *migratable_cleared)
{
struct page *page = pfn_to_page(pfn);
- struct page *head = compound_head(page);
+ struct folio *folio = page_folio(page);
int ret = 2; /* fallback to normal page handling */
bool count_increased = false;

- if (!PageHeadHuge(head))
+ if (!folio_test_hugetlb(folio))
goto out;

if (flags & MF_COUNT_INCREASED) {
ret = 1;
count_increased = true;
- } else if (HPageFreed(head)) {
+ } else if (folio_test_hugetlb_freed(folio)) {
ret = 0;
- } else if (HPageMigratable(head)) {
- ret = get_page_unless_zero(head);
+ } else if (folio_test_hugetlb_migratable(folio)) {
+ ret = folio_try_get(folio);
if (ret)
count_increased = true;
} else {
@@ -1829,24 +1829,24 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags,
goto out;
}

- if (hugetlb_set_page_hwpoison(head, page)) {
+ if (hugetlb_set_page_hwpoison(&folio->page, page)) {
ret = -EHWPOISON;
goto out;
}

/*
- * Clearing HPageMigratable for hwpoisoned hugepages to prevent them
+ * Clearing hugetlb_migratable for hwpoisoned hugepages to prevent them
* from being migrated by memory hotremove.
*/
- if (count_increased && HPageMigratable(head)) {
- ClearHPageMigratable(head);
+ if (count_increased && folio_test_hugetlb_migratable(folio)) {
+ folio_clear_hugetlb_migratable(folio);
*migratable_cleared = true;
}

return ret;
out:
if (count_increased)
- put_page(head);
+ folio_put(folio);
return ret;
}

--
2.39.0

2023-01-12 21:59:47

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH mm-unstable 5/8] mm/memory-failure: convert raw_hwp_list_head() to folios

Change raw_hwp_list_head() to take in a folio and modify its callers to
pass in a folio. Also converts two users of hugetlb specific page macro
users to their folio equivalents.

Signed-off-by: Sidhartha Kumar <[email protected]>
---
mm/memory-failure.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index d5c828328e32..55f34be8ea39 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1695,9 +1695,9 @@ struct raw_hwp_page {
struct page *page;
};

-static inline struct llist_head *raw_hwp_list_head(struct page *hpage)
+static inline struct llist_head *raw_hwp_list_head(struct folio *folio)
{
- return (struct llist_head *)&page_folio(hpage)->_hugetlb_hwpoison;
+ return (struct llist_head *)&folio->_hugetlb_hwpoison;
}

static unsigned long __free_raw_hwp_pages(struct page *hpage, bool move_flag)
@@ -1705,8 +1705,9 @@ static unsigned long __free_raw_hwp_pages(struct page *hpage, bool move_flag)
struct llist_head *head;
struct llist_node *t, *tnode;
unsigned long count = 0;
+ struct folio *folio = page_folio(hpage);

- head = raw_hwp_list_head(hpage);
+ head = raw_hwp_list_head(folio);
llist_for_each_safe(tnode, t, head->first) {
struct raw_hwp_page *p = container_of(tnode, struct raw_hwp_page, node);

@@ -1727,15 +1728,16 @@ static int hugetlb_set_page_hwpoison(struct page *hpage, struct page *page)
struct raw_hwp_page *raw_hwp;
struct llist_node *t, *tnode;
int ret = TestSetPageHWPoison(hpage) ? -EHWPOISON : 0;
+ struct folio *folio = page_folio(hpage);

/*
* 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.
*/
- if (HPageRawHwpUnreliable(hpage))
+ if (folio_test_hugetlb_raw_hwp_unreliable(folio))
return -EHWPOISON;
- head = raw_hwp_list_head(hpage);
+ head = raw_hwp_list_head(folio);
llist_for_each_safe(tnode, t, head->first) {
struct raw_hwp_page *p = container_of(tnode, struct raw_hwp_page, node);

@@ -1756,9 +1758,9 @@ static int hugetlb_set_page_hwpoison(struct page *hpage, struct page *page)
* hwpoisoned subpages, and we need refuse to free/dissolve
* this hwpoisoned hugepage.
*/
- SetHPageRawHwpUnreliable(hpage);
+ folio_set_hugetlb_raw_hwp_unreliable(folio);
/*
- * Once HPageRawHwpUnreliable is set, raw_hwp_page is not
+ * Once hugetlb_raw_hwp_unreliable is set, raw_hwp_page is not
* used any more, so free it.
*/
__free_raw_hwp_pages(hpage, false);
--
2.39.0

2023-01-12 22:03:42

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH mm-unstable 6/8] mm/memory-failure: convert __free_raw_hwp_pages() to folios

Change __free_raw_hwp_pages() to __folio_free_raw_hwp() and modify its
callers to pass in a folio.

Signed-off-by: Sidhartha Kumar <[email protected]>
---
mm/memory-failure.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/mm/memory-failure.c b/mm/memory-failure.c
index 55f34be8ea39..3fff073da89b 100644
--- a/mm/memory-failure.c
+++ b/mm/memory-failure.c
@@ -1700,12 +1700,11 @@ static inline struct llist_head *raw_hwp_list_head(struct folio *folio)
return (struct llist_head *)&folio->_hugetlb_hwpoison;
}

-static unsigned long __free_raw_hwp_pages(struct page *hpage, bool move_flag)
+static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
{
struct llist_head *head;
struct llist_node *t, *tnode;
unsigned long count = 0;
- struct folio *folio = page_folio(hpage);

head = raw_hwp_list_head(folio);
llist_for_each_safe(tnode, t, head->first) {
@@ -1763,7 +1762,7 @@ static int hugetlb_set_page_hwpoison(struct page *hpage, struct page *page)
* Once hugetlb_raw_hwp_unreliable is set, raw_hwp_page is not
* used any more, so free it.
*/
- __free_raw_hwp_pages(hpage, false);
+ __folio_free_raw_hwp(folio, false);
}
return ret;
}
@@ -1784,7 +1783,7 @@ static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag)
if (folio_test_hugetlb_raw_hwp_unreliable(folio))
return 0;

- return __free_raw_hwp_pages(&folio->page, move_flag);
+ return __folio_free_raw_hwp(folio, move_flag);
}

void folio_clear_hugetlb_hwpoison(struct folio *folio)
--
2.39.0

Subject: Re: [PATCH mm-unstable 0/8] convert hugepage memory failure functions to folios

On Thu, Jan 12, 2023 at 02:46:00PM -0600, Sidhartha Kumar wrote:
> ============== OVERVIEW ===========================
> This series contains a 1:1 straightforward page to folio conversion for
> memory failure functions which deal with huge pages. I renamed a few
> functions to fit with how other folio operating functions are named.
> These include:
>
> hugetlb_clear_page_hwpoison -> folio_clear_hugetlb_hwpoison
> free_raw_hwp_pages -> folio_free_raw_hwp
> __free_raw_hwp_pages -> __folio_free_raw_hwp
> hugetlb_set_page_hwpoison -> folio_set_hugetlb_hwpoison
>
> The goal of this series was to reduce users of the hugetlb specific
> page flag macros which take in a page so users are protected by
> the compiler to make sure they are operating on a head page.
>
> Sidhartha Kumar (8):
> mm/memory-failure: convert __get_huge_page_for_hwpoison() to folios
> mm/memory-failure: convert try_memory_failure_hugetlb() to folios
> mm/memory-failure: convert hugetlb_clear_page_hwpoison to folios
> mm/memory-failure: convert free_raw_hwp_pages() to folios
> mm/memory-failure: convert raw_hwp_list_head() to folios
> mm/memory-failure: convert __free_raw_hwp_pages() to folios
> mm/memory-failure: convert hugetlb_set_page_hwpoison() to folios
> mm/memory-failure: convert unpoison_memory() to folios

Hi Sidhartha,

I looked through the patchset and all look fine to me. And I tested the
patchset with v6.2-rc3 and no new issue is detected (I failed to boot with
latest mm-unstable, but maybe that should not be caused by your patches).

Thank you very much,

Acked-by: Naoya Horiguchi <[email protected]>

for the whole series.

- Naoya Horiguchi

2023-01-13 16:25:29

by Sidhartha Kumar

[permalink] [raw]
Subject: Re: [PATCH mm-unstable 0/8] convert hugepage memory failure functions to folios

On 1/13/23 1:03 AM, HORIGUCHI NAOYA(堀口 直也) wrote:
> On Thu, Jan 12, 2023 at 02:46:00PM -0600, Sidhartha Kumar wrote:
>> ============== OVERVIEW ===========================
>> This series contains a 1:1 straightforward page to folio conversion for
>> memory failure functions which deal with huge pages. I renamed a few
>> functions to fit with how other folio operating functions are named.
>> These include:
>>
>> hugetlb_clear_page_hwpoison -> folio_clear_hugetlb_hwpoison
>> free_raw_hwp_pages -> folio_free_raw_hwp
>> __free_raw_hwp_pages -> __folio_free_raw_hwp
>> hugetlb_set_page_hwpoison -> folio_set_hugetlb_hwpoison
>>
>> The goal of this series was to reduce users of the hugetlb specific
>> page flag macros which take in a page so users are protected by
>> the compiler to make sure they are operating on a head page.
>>
>> Sidhartha Kumar (8):
>> mm/memory-failure: convert __get_huge_page_for_hwpoison() to folios
>> mm/memory-failure: convert try_memory_failure_hugetlb() to folios
>> mm/memory-failure: convert hugetlb_clear_page_hwpoison to folios
>> mm/memory-failure: convert free_raw_hwp_pages() to folios
>> mm/memory-failure: convert raw_hwp_list_head() to folios
>> mm/memory-failure: convert __free_raw_hwp_pages() to folios
>> mm/memory-failure: convert hugetlb_set_page_hwpoison() to folios
>> mm/memory-failure: convert unpoison_memory() to folios
>
> Hi Sidhartha,
>
> I looked through the patchset and all look fine to me. And I tested the
> patchset with v6.2-rc3 and no new issue is detected (I failed to boot with
> latest mm-unstable, but maybe that should not be caused by your patches).

Thanks for your review and testing.

It looks like the boot failure has been described here:
https://lore.kernel.org/linux-mm/[email protected]/T/#u
and is not caused by this patch series.

Thanks,
Sidhartha Kumar
>
> Thank you very much,
>
> Acked-by: Naoya Horiguchi <[email protected]>
>
> for the whole series.
>
> - Naoya Horiguchi

2023-07-15 05:15:06

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH mm-unstable 8/8] mm/memory-failure: convert unpoison_memory() to folios

On Thu, Jan 12, 2023 at 02:46:08PM -0600, Sidhartha Kumar wrote:
> @@ -2348,7 +2347,6 @@ int unpoison_memory(unsigned long pfn)
> return -ENXIO;
>
> p = pfn_to_page(pfn);
> - page = compound_head(p);
> folio = page_folio(p);
>
> mutex_lock(&mf_mutex);
> @@ -2360,31 +2358,31 @@ int unpoison_memory(unsigned long pfn)
> goto unlock_mutex;
> }
>
> - if (!PageHWPoison(p)) {
> + if (!folio_test_hwpoison(folio)) {

This is wrong. You need to test the individual page for PageHWPoison()
instead of testing the folio. I understand that this will work for
hugetlbfs pages, but for THP, it's the individual pages that get poisoned,
and folio_test_hwpoison() on a THP actually only tests the head page.

Ideas for preventing this mistake in the future gratefully received.