Skip to contentSkip to conversation

mm: don't convert the page to folio before splitting in split_huge_page()

PATCH In review Pankaj Raghav (Samsung) 4 participants 8 messages Sep 2, 2024 – Sep 3, 2024

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

PATCH 1/1 mm: don't convert the page to folio before splitting in split_huge_page()

From: Pankaj Raghav <[email protected]>

Sven reported that a commit from bs > ps series was breaking the ksm ltp test[1].

split_huge_page() takes precisely a page that is locked, and it also expects the folio that contains that page to be locked after that huge page has been split. The changes introduced converted the page to folio, and passed the head page to be split, which might not be locked, resulting in a kernel panic.

This commit fixes it by always passing the correct page to be split from split_huge_page() with the appropriate minimum order for splitting.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

Fixes fd031210c9ce ("mm: split a folio in minimum folio order chunks")

---
This applies to the vfs.blocksize branch on the vfs tree.

@Christian, Stephen already sent a mail saying that there is a conflict with these changes and mm-unstable. For now, I have based these patches out of your tree. Let me know if you need the same patch based on linux-next.

 include/linux/huge_mm.h | 16 +++++++++++++++-
 mm/huge_memory.c        | 21 +++++++++++++--------
 2 files changed, 28 insertions(+), 9 deletions(-)
include/linux/huge_mm.h +15 -1
index 7c50aeed0522..7a570e0437c9 100644
@@ -319,10 +319,24 @@ unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long add
 bool can_split_folio(struct folio *folio, int *pextra_pins);
 int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
 		unsigned int new_order);
+int min_order_for_split(struct folio *folio);
 int split_folio_to_list(struct folio *folio, struct list_head *list);
 static inline int split_huge_page(struct page *page)
 {
-	return split_folio(page_folio(page));
+	struct folio *folio = page_folio(page);
+	int ret = min_order_for_split(folio);
+
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * split_huge_page() locks the page before splitting and
+	 * expects the same page that has been split to be locked when
+	 * returned. split_folio(page_folio(page)) cannot be used here
+	 * because it converts the page to folio and passes the head
+	 * page to be split.
+	 */
+	return split_huge_page_to_list_to_order(page, NULL, ret);
 }
 void deferred_split_folio(struct folio *folio);
 
mm/huge_memory.c +13 -8
index c29af9451d92..9931ff1d9a9d 100644
@@ -3297,12 +3297,10 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
 	return ret;
 }
 
-int split_folio_to_list(struct folio *folio, struct list_head *list)
+int min_order_for_split(struct folio *folio)
 {
-	unsigned int min_order = 0;
-
 	if (folio_test_anon(folio))
-		goto out;
+		return 0;
 
 	if (!folio->mapping) {
 		if (folio_test_pmd_mappable(folio))
@@ -3310,10 +3308,17 @@ int split_folio_to_list(struct folio *folio, struct list_head *list)
 		return -EBUSY;
 	}
 
-	min_order = mapping_min_folio_order(folio->mapping);
-out:
-	return split_huge_page_to_list_to_order(&folio->page, list,
-							min_order);
+	return mapping_min_folio_order(folio->mapping);
+}
+
+int split_folio_to_list(struct folio *folio, struct list_head *list)
+{
+	int ret = min_order_for_split(folio);
+
+	if (ret < 0)
+		return ret;
+
+	return split_huge_page_to_list_to_order(&folio->page, list, ret);
 }
 
 void __folio_undo_large_rmappable(struct folio *folio)
Signature · 1 line
2.44.1
#1 lore raw
Quoted 5 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

Sven reported that a commit from bs > ps series was breaking the ksm ltp test[1].

Quoted 8 lines

split_huge_page() takes precisely a page that is locked, and it also expects the folio that contains that page to be locked after that huge page has been split. The changes introduced converted the page to folio, and passed the head page to be split, which might not be locked, resulting in a kernel panic.

This commit fixes it by always passing the correct page to be split from split_huge_page() with the appropriate minimum order for splitting.

Looks reasonable.

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
In reply to Pankaj Raghav (Samsung)
Quoted 5 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

Sven reported that a commit from bs > ps series was breaking the ksm ltp test[1].

Quoted 8 lines

split_huge_page() takes precisely a page that is locked, and it also expects the folio that contains that page to be locked after that huge page has been split. The changes introduced converted the page to folio, and passed the head page to be split, which might not be locked, resulting in a kernel panic.

This commit fixes it by always passing the correct page to be split from split_huge_page() with the appropriate minimum order for splitting.

This should be folded into the patch that is broken, not be a separate fix commit, otherwise it introduces a bisection hazard which are to be avoided when possible.

Quoted 91 lines

[1] https://lore.kernel.org/linux-xfs/[email protected]/

Fixes fd031210c9ce ("mm: split a folio in minimum folio order chunks")

---
This applies to the vfs.blocksize branch on the vfs tree.

@Christian, Stephen already sent a mail saying that there is a conflict with these changes and mm-unstable. For now, I have based these patches out of your tree. Let me know if you need the same patch based on linux-next.

 include/linux/huge_mm.h | 16 +++++++++++++++-
 mm/huge_memory.c        | 21 +++++++++++++--------
 2 files changed, 28 insertions(+), 9 deletions(-)
include/linux/huge_mm.h +15 -1
index 7c50aeed0522..7a570e0437c9 100644
@@ -319,10 +319,24 @@ unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long add
 bool can_split_folio(struct folio *folio, int *pextra_pins);
 int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
 		unsigned int new_order);
+int min_order_for_split(struct folio *folio);
 int split_folio_to_list(struct folio *folio, struct list_head *list);
 static inline int split_huge_page(struct page *page)
 {
-	return split_folio(page_folio(page));
+	struct folio *folio = page_folio(page);
+	int ret = min_order_for_split(folio);
+
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * split_huge_page() locks the page before splitting and
+	 * expects the same page that has been split to be locked when
+	 * returned. split_folio(page_folio(page)) cannot be used here
+	 * because it converts the page to folio and passes the head
+	 * page to be split.
+	 */
+	return split_huge_page_to_list_to_order(page, NULL, ret);
 }
 void deferred_split_folio(struct folio *folio);
 
mm/huge_memory.c +13 -8
index c29af9451d92..9931ff1d9a9d 100644
@@ -3297,12 +3297,10 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
 	return ret;
 }
 
-int split_folio_to_list(struct folio *folio, struct list_head *list)
+int min_order_for_split(struct folio *folio)
 {
-	unsigned int min_order = 0;
-
 	if (folio_test_anon(folio))
-		goto out;
+		return 0;
 
 	if (!folio->mapping) {
 		if (folio_test_pmd_mappable(folio))
@@ -3310,10 +3308,17 @@ int split_folio_to_list(struct folio *folio, struct list_head *list)
 		return -EBUSY;
 	}
 
-	min_order = mapping_min_folio_order(folio->mapping);
-out:
-	return split_huge_page_to_list_to_order(&folio->page, list,
-							min_order);
+	return mapping_min_folio_order(folio->mapping);
+}
+
+int split_folio_to_list(struct folio *folio, struct list_head *list)
+{
+	int ret = min_order_for_split(folio);
+
+	if (ret < 0)
+		return ret;
+
+	return split_huge_page_to_list_to_order(&folio->page, list, ret);
 }
 
 void __folio_undo_large_rmappable(struct folio *folio)
Signature · 1 line
2.44.1
#3 lore raw
Quoted 12 lines · Matthew Wilcox
Quoted 11 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

Sven reported that a commit from bs > ps series was breaking the ksm ltp test[1].

split_huge_page() takes precisely a page that is locked, and it also expects the folio that contains that page to be locked after that huge page has been split. The changes introduced converted the page to folio, and passed the head page to be split, which might not be locked, resulting in a kernel panic.

Quoted 6 lines
Quoted 2 lines

This commit fixes it by always passing the correct page to be split from split_huge_page() with the appropriate minimum order for splitting.

This should be folded into the patch that is broken, not be a separate fix commit, otherwise it introduces a bisection hazard which are to be avoided when possible.

Patch folded into "mm: split a folio in minimum folio order chunks" with the Link to this patch. Please double-check.

#4 lore raw
Quoted 16 lines · Christian Brauner
Quoted 15 lines · Matthew Wilcox
Quoted 13 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

Sven reported that a commit from bs > ps series was breaking the ksm ltp test[1].

split_huge_page() takes precisely a page that is locked, and it also expects the folio that contains that page to be locked after that huge page has been split. The changes introduced converted the page to folio, and passed the head page to be split, which might not be locked, resulting in a kernel panic.

This commit fixes it by always passing the correct page to be split from split_huge_page() with the appropriate minimum order for splitting.

Quoted 6 lines
Quoted 3 lines

This should be folded into the patch that is broken, not be a separate fix commit, otherwise it introduces a bisection hazard which are to be avoided when possible.

Patch folded into "mm: split a folio in minimum folio order chunks" with the Link to this patch. Please double-check.

Thanks a lot!

I still don't see it upstream[1]. Maybe it is yet to be pushed?

[1] https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/commit/?h=vfs.blocksize&id=fd031210c9ceb399db1dea001c6a5e98f3b4e2e7

#5 lore raw
Quoted 21 lines · Pankaj Raghav (Samsung)
Quoted 20 lines · Christian Brauner
Quoted 18 lines · Matthew Wilcox
Quoted 5 lines · Pankaj Raghav (Samsung)

From: Pankaj Raghav <[email protected]>

Sven reported that a commit from bs > ps series was breaking the ksm ltp test[1].

Quoted 8 lines

split_huge_page() takes precisely a page that is locked, and it also expects the folio that contains that page to be locked after that huge page has been split. The changes introduced converted the page to folio, and passed the head page to be split, which might not be locked, resulting in a kernel panic.

This commit fixes it by always passing the correct page to be split from split_huge_page() with the appropriate minimum order for splitting.

This should be folded into the patch that is broken, not be a separate fix commit, otherwise it introduces a bisection hazard which are to be avoided when possible.

Quoted 7 lines
Quoted 2 lines

Patch folded into "mm: split a folio in minimum folio order chunks" with the Link to this patch. Please double-check.

Thanks a lot!

I still don't see it upstream[1]. Maybe it is yet to be pushed?

[1] https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/commit/?h=vfs.blocksize&id=fd031210c9ceb399db1dea001c6a5e98f3b4e2e7

Pushed now.

#6 lore raw
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

This should be folded into the patch that is broken, not be a separate fix commit, otherwise it introduces a bisection hazard which are to be avoided when possible.

Patch folded into "mm: split a folio in minimum folio order chunks" with the Link to this patch. Please double-check.

Quoted 7 lines
Quoted 5 lines

Thanks a lot!

I still don't see it upstream[1]. Maybe it is yet to be pushed?

[1] https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/commit/?h=vfs.blocksize&id=fd031210c9ceb399db1dea001c6a5e98f3b4e2e7

Pushed now.

I can see it now. Thanks Christian. :)

This patch has a merge conflict in linux-next. It should be a trivial merge but let me know if you want me to send a patch for it.

#7 lore raw
Quoted 10 lines · Pankaj Raghav (Samsung)
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

This should be folded into the patch that is broken, not be a separate fix commit, otherwise it introduces a bisection hazard which are to be avoided when possible.

Patch folded into "mm: split a folio in minimum folio order chunks" with the Link to this patch. Please double-check.

Thanks a lot!

I still don't see it upstream[1]. Maybe it is yet to be pushed?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

[1] https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git/commit/?h=vfs.blocksize&id=fd031210c9ceb399db1dea001c6a5e98f3b4e2e7

Pushed now.

I can see it now. Thanks Christian. :)

This patch has a merge conflict in linux-next. It should be a trivial merge but let me know if you want me to send a patch for it.

No need, Linus will usually just sort those out.