Skip to contentSkip to conversation

mm/thp: support mTHP in thp_underused()

PATCH In review Ye Liu 3 participants 4 messages Jul 15

Subsystems (2)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH 1/1 mm/thp: support mTHP in thp_underused()

From: Ye Liu <[email protected]>

When khugepaged_max_ptes_none is configured to a non-default value, scale it for non-PMD-mappable folios to match the logic in collapse_max_ptes_none(), so mTHP can be classified as underused instead of being silently skipped due to unscaled PMD-sized thresholds.

---
 mm/huge_memory.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
mm/huge_memory.c +13 -8
index 25612af72dca..c642bec967fa 100644
@@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
 static bool thp_underused(struct folio *folio)
 {
 	int num_zero_pages = 0, num_filled_pages = 0;
+	unsigned int max_ptes_none = khugepaged_max_ptes_none;
+	unsigned int folio_nr = folio_nr_pages(folio);
 	int i;
 
-	if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
+	if (!folio_test_pmd_mappable(folio)) {
+		if (max_ptes_none == HPAGE_PMD_NR - 1)
+			max_ptes_none = folio_nr - 1;
+		else if (max_ptes_none)
+			return false;
+	}
+
+	if (max_ptes_none == folio_nr - 1)
 		return false;
 
 	if (folio_contain_hwpoisoned_page(folio))
 		return false;
 
-	for (i = 0; i < folio_nr_pages(folio); i++) {
+	for (i = 0; i < folio_nr; i++) {
 		if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
-			if (++num_zero_pages > khugepaged_max_ptes_none)
+			if (++num_zero_pages > max_ptes_none)
 				return true;
 		} else {
-			/*
-			 * Another path for early exit once the number
-			 * of non-zero filled pages exceeds threshold.
-			 */
-			if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
+			if (++num_filled_pages >= folio_nr - max_ptes_none)
 				return false;
 		}
 	}
Signature · 1 line
2.43.0
#1 lore raw
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

When khugepaged_max_ptes_none is configured to a non-default value, scale it for non-PMD-mappable folios to match the logic in collapse_max_ptes_none(), so mTHP can be classified as underused instead of being silently skipped due to unscaled PMD-sized thresholds.

IIUC we don't actually add non-PMD folios to the deferred split list and only operate on fully mapped folios, so consequently, we never try to shrink them with the underused shrinker.

Let me know if im missing something!

Cheers,
-- Nico

Quoted 51 lines
---
 mm/huge_memory.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
mm/huge_memory.c +13 -9
index 25612af72dca..c642bec967fa 100644
@@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
 static bool thp_underused(struct folio *folio)
 {
        int num_zero_pages = 0, num_filled_pages = 0;
+       unsigned int max_ptes_none = khugepaged_max_ptes_none;
+       unsigned int folio_nr = folio_nr_pages(folio);
        int i;
 
-       if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
+       if (!folio_test_pmd_mappable(folio)) {
+               if (max_ptes_none == HPAGE_PMD_NR - 1)
+                       max_ptes_none = folio_nr - 1;
+               else if (max_ptes_none)
+                       return false;
+       }
+
+       if (max_ptes_none == folio_nr - 1)
                return false;
 
        if (folio_contain_hwpoisoned_page(folio))
                return false;
 
-       for (i = 0; i < folio_nr_pages(folio); i++) {
+       for (i = 0; i < folio_nr; i++) {
                if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
-                       if (++num_zero_pages > khugepaged_max_ptes_none)
+                       if (++num_zero_pages > max_ptes_none)
                                return true;
                } else {
-                       /*
-                        * Another path for early exit once the number
-                        * of non-zero filled pages exceeds threshold.
-                        */
-                       if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
+                       if (++num_filled_pages >= folio_nr - max_ptes_none)
                                return false;
                }
        }
--

2.43.0

reviewer
#2 lore raw

CC Joanne.

Quoted 4 lines · Nico Pache
Quoted 3 lines · Ye Liu

From: Ye Liu <[email protected]>

Quoted 8 lines
Quoted 4 lines

When khugepaged_max_ptes_none is configured to a non-default value, scale it for non-PMD-mappable folios to match the logic in collapse_max_ptes_none(), so mTHP can be classified as underused instead of being silently skipped due to unscaled PMD-sized thresholds.

IIUC we don't actually add non-PMD folios to the deferred split list and only operate on fully mapped folios, so consequently, we never try to shrink them with the underused shrinker.

Right. Joanne sent a similar patch set[1] to split underused mTHP folios, but David said "needs more proper thought".

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

Quoted 50 lines
Quoted 50 lines
---
  mm/huge_memory.c | 21 +++++++++++++--------
  1 file changed, 13 insertions(+), 8 deletions(-)
mm/huge_memory.c +13 -9
index 25612af72dca..c642bec967fa 100644
@@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
  static bool thp_underused(struct folio *folio)
  {
         int num_zero_pages = 0, num_filled_pages = 0;
+       unsigned int max_ptes_none = khugepaged_max_ptes_none;
+       unsigned int folio_nr = folio_nr_pages(folio);
         int i;
 
-       if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
+       if (!folio_test_pmd_mappable(folio)) {
+               if (max_ptes_none == HPAGE_PMD_NR - 1)
+                       max_ptes_none = folio_nr - 1;
+               else if (max_ptes_none)
+                       return false;
+       }
+
+       if (max_ptes_none == folio_nr - 1)
                 return false;
 
         if (folio_contain_hwpoisoned_page(folio))
                 return false;
 
-       for (i = 0; i < folio_nr_pages(folio); i++) {
+       for (i = 0; i < folio_nr; i++) {
                 if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
-                       if (++num_zero_pages > khugepaged_max_ptes_none)
+                       if (++num_zero_pages > max_ptes_none)
                                 return true;
                 } else {
-                       /*
-                        * Another path for early exit once the number
-                        * of non-zero filled pages exceeds threshold.
-                        */
-                       if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
+                       if (++num_filled_pages >= folio_nr - max_ptes_none)
                                 return false;
                 }
         }
--

2.43.0

author
#3 lore raw
Quoted 12 lines · Baolin Wang

CC Joanne.

Quoted 9 lines · Nico Pache
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

When khugepaged_max_ptes_none is configured to a non-default value, scale it for non-PMD-mappable folios to match the logic in collapse_max_ptes_none(), so mTHP can be classified as underused instead of being silently skipped due to unscaled PMD-sized thresholds.

Quoted 6 lines
Quoted 3 lines

IIUC we don't actually add non-PMD folios to the deferred split list and only operate on fully mapped folios, so consequently, we never try to shrink them with the underused shrinker.

Right. Joanne sent a similar patch set[1] to split underused mTHP folios, but David said "needs more proper thought".

Hi Nico,Baolin

Thanks for the review and to Baolin for providing the relevant information. You're right – non‑PMD folios aren't on the deferred split list, so this patch alone is pointless.
I'll drop it and continue to follow the discussions surrounding the Joanne patch series instead. Sorry for the noise.

Quoted 53 lines

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

Quoted 50 lines
Quoted 50 lines

---
  mm/huge_memory.c | 21 +++++++++++++--------

  1 file changed, 13 insertions(+), 8 deletions(-)
mm/huge_memory.c +13 -8
index 25612af72dca..c642bec967fa 100644
@@ -4489,24 +4489,29 @@ static unsigned long deferred_split_count(struct shrinker *shrink,
   static bool thp_underused(struct folio *folio)
   {
          int num_zero_pages = 0, num_filled_pages = 0;
+       unsigned int max_ptes_none = khugepaged_max_ptes_none;
+       unsigned int folio_nr = folio_nr_pages(folio);
          int i;
 
-       if (khugepaged_max_ptes_none == HPAGE_PMD_NR - 1)
+       if (!folio_test_pmd_mappable(folio)) {
+               if (max_ptes_none == HPAGE_PMD_NR - 1)
+                       max_ptes_none = folio_nr - 1;
+               else if (max_ptes_none)
+                       return false;
+       }
+
+       if (max_ptes_none == folio_nr - 1)
                  return false;
 
          if (folio_contain_hwpoisoned_page(folio))
                  return false;
 
-       for (i = 0; i < folio_nr_pages(folio); i++) {
+       for (i = 0; i < folio_nr; i++) {
                  if (pages_identical(folio_page(folio, i), ZERO_PAGE(0))) {
-                       if (++num_zero_pages > khugepaged_max_ptes_none)
+                       if (++num_zero_pages > max_ptes_none)
                                  return true;
                  } else {
-                       /*
-                        * Another path for early exit once the number
-                        * of non-zero filled pages exceeds threshold.
-                        */
-                       if (++num_filled_pages >= HPAGE_PMD_NR - khugepaged_max_ptes_none)
+                       if (++num_filled_pages >= folio_nr - max_ptes_none)
                                  return false;
                  }
          }
Signature · 1 line
2.43.0
Signature · 2 lines
Thanks,
Ye Liu