Skip to contentSkip to conversation

mm/ksm: catch tail page abnormal in page_stable_node

PATCH In review [email protected] 4 participants 6 messages Mar 18, 2024 – Mar 20, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/12 mm/ksm: catch tail page abnormal in page_stable_node

From: Alex Shi <[email protected]>

commit 19138349ed59 ("mm/migrate: Add folio_migrate_flags()") change the meaning of func page_stable_node() to check the compound head for tail 'page' instead of tail page self.
But seems both semantics are same at results, the func always return NULL
for tail page. So adding a bug monitor here in case of abnormal.

Cc Izik Eidus <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Chris Wright <[email protected]>

To: [email protected]
To: [email protected]
To: Andrew Morton <[email protected]>

---
 mm/ksm.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/ksm.c +6 -1
index 8c001819cf10..3ff469961927 100644
@@ -1091,7 +1091,12 @@ static inline struct ksm_stable_node *folio_stable_node(struct folio *folio)
 
 static inline struct ksm_stable_node *page_stable_node(struct page *page)
 {
-	return folio_stable_node(page_folio(page));
+	struct ksm_stable_node *node;
+
+	node = folio_stable_node(page_folio(page));
+	VM_BUG_ON_PAGE(PageTail(page) && node, page);
+
+	return node;
 }
 
 static inline void set_page_stable_node(struct page *page,
Signature · 1 line
2.43.0
#1 lore raw
Quoted 26 lines · On 18.03.24 13:14, [email protected] wrote:

From: Alex Shi <[email protected]>

commit 19138349ed59 ("mm/migrate: Add folio_migrate_flags()") change the meaning of func page_stable_node() to check the compound head for tail 'page' instead of tail page self.
But seems both semantics are same at results, the func always return NULL
for tail page. So adding a bug monitor here in case of abnormal.

Cc Izik Eidus <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Chris Wright <[email protected]>

To: [email protected]
To: [email protected]
To: Andrew Morton <[email protected]>

---
  mm/ksm.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
mm/ksm.c +0 -0
index 8c001819cf10..3ff469961927 100644

mm/ksm.c static inline struct ksm_stable_node *folio_stable_node(struct folio *folio) · line 1097

Quoted 8 lines
mm/ksm.c +4 -1
@@ -1091,7 +1091,12 @@ static inline struct ksm_stable_node *folio_stable_node(struct folio *folio)
 
  static inline struct ksm_stable_node *page_stable_node(struct page *page)
  {
-	return folio_stable_node(page_folio(page));
+	struct ksm_stable_node *node;
+
+	node = folio_stable_node(page_folio(page));
+	VM_BUG_ON_PAGE(PageTail(page) && node, page);

I don't really understand why we would want this.

Only KSM folios can have a node in the stable tree. KSM folios cannot be large folios. At that is precisely what folio_stable_node() checks.

If we'd have a large folio identify as a KSM folio we'd be in much bigger trouble.

Besides, I'm sure you read "22) Do not crash the kernel" in Documentation/process/coding-style.rst

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 37 lines · David Hildenbrand
Quoted 26 lines · On 18.03.24 13:14, [email protected] wrote:

From: Alex Shi <[email protected]>

commit 19138349ed59 ("mm/migrate: Add folio_migrate_flags()") change the meaning of func page_stable_node() to check the compound head for tail 'page' instead of tail page self.
But seems both semantics are same at results, the func always return NULL for tail page. So adding a bug monitor here in case of abnormal.

Cc Izik Eidus <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Chris Wright <[email protected]>

To: [email protected]
To: [email protected]
To: Andrew Morton <[email protected]>
---
  mm/ksm.c | 7 ++++++-

  1 file changed, 6 insertions(+), 1 deletion(-)
mm/ksm.c +0 -0
index 8c001819cf10..3ff469961927 100644

mm/ksm.c static inline struct ksm_stable_node *folio_stable_node(struct folio *folio) · line 1096

Quoted 7 lines
mm/ksm.c +4 -1
@@ -1091,7 +1091,12 @@ static inline struct ksm_stable_node *folio_stable_node(struct folio *folio)
     static inline struct ksm_stable_node *page_stable_node(struct page *page)
   {
-    return folio_stable_node(page_folio(page));
+    struct ksm_stable_node *node;
+
+    node = folio_stable_node(page_folio(page));
+    VM_BUG_ON_PAGE(PageTail(page) && node, page);

I don't really understand why we would want this.

Quoted 7 lines

Only KSM folios can have a node in the stable tree. KSM folios cannot be large folios. At that is precisely what folio_stable_node() checks.

If we'd have a large folio identify as a KSM folio we'd be in much bigger trouble.

Besides, I'm sure you read "22) Do not crash the kernel" in Documentation/process/coding-style.rst

Hi David,

Thanks for comments!
Forgive my stupidity, I understand KSM stable tree has no compound pages, but when searching a tail page in ksm_do_scan(), why we couldn't be in a race, that another VM doing THP collapse on the same contents pages, while the 3rd vm is doing hugepage spliting?

Best regards!
Alex

#3 lore raw
Quoted 45 lines · Alex Shi
Quoted 42 lines · David Hildenbrand
Quoted 26 lines · On 18.03.24 13:14, [email protected] wrote:

From: Alex Shi <[email protected]>

commit 19138349ed59 ("mm/migrate: Add folio_migrate_flags()") change the meaning of func page_stable_node() to check the compound head for tail 'page' instead of tail page self.
But seems both semantics are same at results, the func always return NULL
  for tail page. So adding a bug monitor here in case of abnormal.

Cc Izik Eidus <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Chris Wright <[email protected]>

To: [email protected]
To: [email protected]
To: Andrew Morton <[email protected]>

---
   mm/ksm.c | 7 ++++++-
   1 file changed, 6 insertions(+), 1 deletion(-)
mm/ksm.c +0 -0
index 8c001819cf10..3ff469961927 100644

mm/ksm.c static inline struct ksm_stable_node *folio_stable_node(struct folio *folio) · line 1096

Quoted 7 lines
mm/ksm.c +4 -1
@@ -1091,7 +1091,12 @@ static inline struct ksm_stable_node *folio_stable_node(struct folio *folio)
     static inline struct ksm_stable_node *page_stable_node(struct page *page)
   {
-    return folio_stable_node(page_folio(page));
+    struct ksm_stable_node *node;
+
+    node = folio_stable_node(page_folio(page));
+    VM_BUG_ON_PAGE(PageTail(page) && node, page);

I don't really understand why we would want this.

Only KSM folios can have a node in the stable tree. KSM folios cannot be large folios. At that is precisely what folio_stable_node() checks.

If we'd have a large folio identify as a KSM folio we'd be in much bigger trouble.

Quoted 7 lines
Quoted 2 lines

Besides, I'm sure you read "22) Do not crash the kernel" in Documentation/process/coding-style.rst

Hi David,

Thanks for comments!
Forgive my stupidity, I understand KSM stable tree has no compound pages, but when searching a tail page in ksm_do_scan(), why we couldn't be in a race, that another VM doing THP collapse on the same contents pages, while the 3rd vm is doing hugepage spliting?

We always call cmp_and_merge_page() while holding a reference on the page.

There, we call page_stable_node() directly and via stable_tree_search()->page_stable_node() on that page.

When stable_tree_search() returns a kpage, we also hold a reference to that kpage. So calling page_stable_node() on the kpage behaves the same.

As we are holding page references, pages cannot be split/merged and we should not see any races in page_stable_node().

Am I missing something?

Note that your change would also not help here: if it would be racy, you'd also not reliably catch any tail pages.

But it should not be racy unless I am missing something.

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
Quoted 60 lines · David Hildenbrand
Quoted 45 lines · Alex Shi
Quoted 42 lines · David Hildenbrand
Quoted 26 lines · On 18.03.24 13:14, [email protected] wrote:

From: Alex Shi <[email protected]>

commit 19138349ed59 ("mm/migrate: Add folio_migrate_flags()") change the meaning of func page_stable_node() to check the compound head for tail 'page' instead of tail page self.
But seems both semantics are same at results, the func always return NULL for tail page. So adding a bug monitor here in case of abnormal.

Cc Izik Eidus <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Chris Wright <[email protected]>

To: [email protected]
To: [email protected]
To: Andrew Morton <[email protected]>
---
   mm/ksm.c | 7 ++++++-

   1 file changed, 6 insertions(+), 1 deletion(-)
mm/ksm.c +0 -0
index 8c001819cf10..3ff469961927 100644

mm/ksm.c static inline struct ksm_stable_node *folio_stable_node(struct folio *folio) · line 1096

Quoted 7 lines
mm/ksm.c +4 -1
@@ -1091,7 +1091,12 @@ static inline struct ksm_stable_node *folio_stable_node(struct folio *folio)
      static inline struct ksm_stable_node *page_stable_node(struct page *page)
    {
-    return folio_stable_node(page_folio(page));
+    struct ksm_stable_node *node;
+
+    node = folio_stable_node(page_folio(page));
+    VM_BUG_ON_PAGE(PageTail(page) && node, page);

I don't really understand why we would want this.

Only KSM folios can have a node in the stable tree. KSM folios cannot be large folios. At that is precisely what folio_stable_node() checks.

If we'd have a large folio identify as a KSM folio we'd be in much bigger trouble.

Quoted 7 lines
Quoted 2 lines

Besides, I'm sure you read "22) Do not crash the kernel" in Documentation/process/coding-style.rst

Hi David,

Thanks for comments!
Forgive my stupidity, I understand KSM stable tree has no compound pages, but when searching a tail page in ksm_do_scan(), why we couldn't be in a race, that another VM doing THP collapse on the same contents pages, while the 3rd vm is doing hugepage spliting?

We always call cmp_and_merge_page() while holding a reference on the page.

There, we call page_stable_node() directly and via stable_tree_search()->page_stable_node() on that page.

When stable_tree_search() returns a kpage, we also hold a reference to that kpage. So calling page_stable_node() on the kpage behaves the same.

Quoted 8 lines

As we are holding page references, pages cannot be split/merged and we should not see any races in page_stable_node().

Am I missing something?

Note that your change would also not help here: if it would be racy, you'd also not reliably catch any tail pages.

But it should not be racy unless I am missing something.

Hi David,

Thanks for the info, I see.

BTW, I should cc you for my KSM folio patchset review: https://lore.kernel.org/linux-mm/[email protected]/

Best regards!
Alex

#5 lore raw
In reply to [email protected]
Quoted 7 lines · On Mon, Mar 18, 2024 at 08:14:30PM +0800, [email protected] wrote:

From: Alex Shi <[email protected]>

commit 19138349ed59 ("mm/migrate: Add folio_migrate_flags()") change the meaning of func page_stable_node() to check the compound head for tail 'page' instead of tail page self.
But seems both semantics are same at results, the func always return NULL
for tail page. So adding a bug monitor here in case of abnormal.

No. I didn't object to you doing this to convince yourself, but there's no way we should have this upstream.

If we did have large KSM folios (and I don't think we should!), you'd crash the kernel instead of silently doing the right thing.

Quoted 10 lines

static inline struct ksm_stable_node *page_stable_node(struct page *page) {

mm/ksm.c +6 -1
-	return folio_stable_node(page_folio(page));
+	struct ksm_stable_node *node;
+
+	node = folio_stable_node(page_folio(page));
+	VM_BUG_ON_PAGE(PageTail(page) && node, page);
+
+	return node;
 }