diff mbox series

[6/7] mm: truncate: split thp to a non-zero order if possible.

Message ID 20201119160605.1272425-7-zi.yan@sent.com (mailing list archive)
State New
Headers show
Series Split huge pages to any lower order pages and selftests. | expand

Commit Message

Zi Yan Nov. 19, 2020, 4:06 p.m. UTC
From: Zi Yan <ziy@nvidia.com>

To minimize the number of pages after a truncation, when truncating a
THP, we do not need to split it all the way down to order-0. The THP has
at most three parts, the part before offset, the part to be truncated,
the part left at the end. Use the non-zero minimum of them to decide
what order we split the THP to.

Signed-off-by: Zi Yan <ziy@nvidia.com>
---
 mm/truncate.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/mm/truncate.c b/mm/truncate.c
index 20bd17538ec2..2e93d702f2c6 100644
--- a/mm/truncate.c
+++ b/mm/truncate.c
@@ -237,7 +237,9 @@  int truncate_inode_page(struct address_space *mapping, struct page *page)
 bool truncate_inode_partial_page(struct page *page, loff_t start, loff_t end)
 {
 	loff_t pos = page_offset(page);
-	unsigned int offset, length;
+	unsigned int offset, length, remaining, min_subpage_size = PAGE_SIZE;
+	unsigned int new_order;
+
 
 	if (pos < start)
 		offset = start - pos;
@@ -248,6 +250,7 @@  bool truncate_inode_partial_page(struct page *page, loff_t start, loff_t end)
 		length = length - offset;
 	else
 		length = end + 1 - pos - offset;
+	remaining = thp_size(page) - offset - length;
 
 	wait_on_page_writeback(page);
 	if (length == thp_size(page)) {
@@ -267,7 +270,29 @@  bool truncate_inode_partial_page(struct page *page, loff_t start, loff_t end)
 		do_invalidatepage(page, offset, length);
 	if (!PageTransHuge(page))
 		return true;
-	return split_huge_page(page) == 0;
+
+	/*
+	 * find the non-zero minimum of offset, length, and remaining and use it
+	 * to decide the new order of the page after split
+	 */
+	if (offset && remaining)
+		min_subpage_size = min_t(unsigned int,
+					 min_t(unsigned int, offset, length),
+					 remaining);
+	else if (!offset)
+		min_subpage_size = min_t(unsigned int, length, remaining);
+	else /* remaining == 0 */
+		min_subpage_size = min_t(unsigned int, length, offset);
+
+	min_subpage_size = max_t(unsigned int, PAGE_SIZE, min_subpage_size);
+
+	new_order = ilog2(min_subpage_size/PAGE_SIZE);
+
+	/* order-1 THP not supported, downgrade to order-0 */
+	if (new_order == 1)
+		new_order = 0;
+
+	return split_huge_page_to_list_to_order(page, NULL, new_order) == 0;
 }
 
 /*