[PATCH v16 5/9] mm: hugetlb: set the PageHWPoison to the raw error page

From: Muchun Song
Date: Fri Feb 19 2021 - 05:55:45 EST


Because we reuse the first tail vmemmap page frame and remap it
with read-only, we cannot set the PageHWPosion on some tail pages.
So we can use the head[4].private (There are at least 128 struct
page structures associated with the optimized HugeTLB page, so
using head[4].private is safe) to record the real error page index
and set the raw error page PageHWPoison later.

Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
Reviewed-by: Oscar Salvador <osalvador@xxxxxxx>
Acked-by: David Rientjes <rientjes@xxxxxxxxxx>
---
mm/hugetlb.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++------
mm/hugetlb_vmemmap.c | 12 --------
mm/hugetlb_vmemmap.h | 17 +++++++++++
3 files changed, 91 insertions(+), 21 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index bcf856974c48..f0877411f790 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1305,6 +1305,74 @@ static inline void destroy_compound_gigantic_page(struct page *page,
unsigned int order) { }
#endif

+#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP
+static inline void hwpoison_subpage_deliver(struct hstate *h, struct page *head)
+{
+ struct page *page;
+
+ if (!PageHWPoison(head) || !free_vmemmap_pages_per_hpage(h))
+ return;
+
+ page = head + page_private(head + 4);
+
+ /*
+ * Move PageHWPoison flag from head page to the raw error page,
+ * which makes any subpages rather than the error page reusable.
+ */
+ if (page != head) {
+ SetPageHWPoison(page);
+ ClearPageHWPoison(head);
+ }
+}
+
+static inline void hwpoison_subpage_set(struct hstate *h, struct page *head,
+ struct page *page)
+{
+ if (!PageHWPoison(head))
+ return;
+
+ if (free_vmemmap_pages_per_hpage(h)) {
+ set_page_private(head + 4, page - head);
+ } else if (page != head) {
+ /*
+ * Move PageHWPoison flag from head page to the raw error page,
+ * which makes any subpages rather than the error page reusable.
+ */
+ SetPageHWPoison(page);
+ ClearPageHWPoison(head);
+ }
+}
+
+static inline void hwpoison_subpage_clear(struct hstate *h, struct page *head)
+{
+ if (!PageHWPoison(head) || !free_vmemmap_pages_per_hpage(h))
+ return;
+
+ set_page_private(head + 4, 0);
+}
+#else
+static inline void hwpoison_subpage_deliver(struct hstate *h, struct page *head)
+{
+}
+
+static inline void hwpoison_subpage_set(struct hstate *h, struct page *head,
+ struct page *page)
+{
+ if (PageHWPoison(head) && page != head) {
+ /*
+ * Move PageHWPoison flag from head page to the raw error page,
+ * which makes any subpages rather than the error page reusable.
+ */
+ SetPageHWPoison(page);
+ ClearPageHWPoison(head);
+ }
+}
+
+static inline void hwpoison_subpage_clear(struct hstate *h, struct page *head)
+{
+}
+#endif
+
static int update_and_free_page(struct hstate *h, struct page *page)
__releases(&hugetlb_lock) __acquires(&hugetlb_lock)
{
@@ -1351,6 +1419,8 @@ static int update_and_free_page(struct hstate *h, struct page *page)
return -ENOMEM;
}

+ hwpoison_subpage_deliver(h, page);
+
for (i = 0; i < pages_per_huge_page(h); i++) {
page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
1 << PG_referenced | 1 << PG_dirty |
@@ -1795,22 +1865,17 @@ int dissolve_free_huge_page(struct page *page)
goto retry;
}

- /*
- * Move PageHWPoison flag from head page to the raw error page,
- * which makes any subpages rather than the error page reusable.
- */
- if (PageHWPoison(head) && page != head) {
- SetPageHWPoison(page);
- ClearPageHWPoison(head);
- }
+ hwpoison_subpage_set(h, head, page);
ClearHPageFreed(page);
list_del(&head->lru);
h->free_huge_pages--;
h->free_huge_pages_node[nid]--;
h->max_huge_pages--;
rc = update_and_free_page(h, head);
- if (rc)
+ if (rc) {
h->max_huge_pages++;
+ hwpoison_subpage_clear(h, head);
+ }
}
out:
spin_unlock(&hugetlb_lock);
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 29a3380f3b20..f7ab3d99250a 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -181,18 +181,6 @@
#define RESERVE_VMEMMAP_NR 2U
#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT)

-/*
- * How many vmemmap pages associated with a HugeTLB page that can be freed
- * to the buddy allocator.
- *
- * Todo: Returns zero for now, which means the feature is disabled. We will
- * enable it once all the infrastructure is there.
- */
-static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h)
-{
- return 0;
-}
-
static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
{
return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h
index e5547d53b9f5..a37771b0b82a 100644
--- a/mm/hugetlb_vmemmap.h
+++ b/mm/hugetlb_vmemmap.h
@@ -13,6 +13,18 @@
#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP
int alloc_huge_page_vmemmap(struct hstate *h, struct page *head);
void free_huge_page_vmemmap(struct hstate *h, struct page *head);
+
+/*
+ * How many vmemmap pages associated with a HugeTLB page that can be freed
+ * to the buddy allocator.
+ *
+ * Todo: Returns zero for now, which means the feature is disabled. We will
+ * enable it once all the infrastructure is there.
+ */
+static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h)
+{
+ return 0;
+}
#else
static inline int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
{
@@ -22,5 +34,10 @@ static inline int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head)
{
}
+
+static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h)
+{
+ return 0;
+}
#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */
#endif /* _LINUX_HUGETLB_VMEMMAP_H */
--
2.11.0