Skip to contentSkip to conversation

Cleanup and fixup for hugetlb

PATCH Applied Miaohe Lin 2 participants 11 messages Apr 10, 2021 – Apr 13, 2021

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

v2 cover Cleanup and fixup for hugetlb

Hi all,
This series contains cleanups to remove redundant VM_BUG_ON() and simplify the return code. Also this handles the error case in hugetlb_fix_reserve_counts() correctly. More details can be found in the respective changelogs. Thanks!

v1->v2:
collect Reviewed-by tag
remove the HPAGE_RESV_OWNER check per Mike
add a comment to hugetlb_unreserve_pages per Mike expand warning message a bit for hugetlb_fix_reserve_counts Add a new patch to remove unused variable
Many thanks for Mike's review and suggestion!

Miaohe Lin (5):
mm/hugeltb: remove redundant VM_BUG_ON() in region_add() mm/hugeltb: simplify the return code of __vma_reservation_common() mm/hugeltb: clarify (chg - freed) won't go negative in
hugetlb_unreserve_pages()
mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts() mm/hugetlb: remove unused variable pseudo_vma in
remove_inode_hugepages()

 fs/hugetlbfs/inode.c |  3 ---
 mm/hugetlb.c         | 57 +++++++++++++++++++++++++-------------------
 2 files changed, 33 insertions(+), 27 deletions(-)
Signature · 1 line
2.19.1
author
#1 lore raw

PATCH v2 1/5 mm/hugeltb: remove redundant VM_BUG_ON() in region_add()

The same VM_BUG_ON() check is already done in the callee. Remove this extra one to simplify the code slightly.

---
 mm/hugetlb.c | 1 -
 1 file changed, 1 deletion(-)
mm/hugetlb.c +0 -1
index c22111f3da20..a03a50b7c410 100644
@@ -556,7 +556,6 @@ static long region_add(struct resv_map *resv, long f, long t,
 	resv->adds_in_progress -= in_regions_needed;
 
 	spin_unlock(&resv->lock);
-	VM_BUG_ON(add < 0);
 	return add;
 }
 
Signature · 1 line
2.19.1
author
#2 lore raw

PATCH v2 2/5 mm/hugeltb: simplify the return code of __vma_reservation_common()

In reply to Miaohe Lin

It's guaranteed that the vma is associated with a resv_map, i.e. either VM_MAYSHARE or HPAGE_RESV_OWNER, when the code reaches here or we would have returned via !resv check above. So it's unneeded to check whether HPAGE_RESV_OWNER is set here. Simplify the return code to make it more clear.

---
 mm/hugetlb.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)
mm/hugetlb.c +20 -21
index a03a50b7c410..9b4c05699a90 100644
@@ -2163,27 +2163,26 @@ static long __vma_reservation_common(struct hstate *h,
 
 	if (vma->vm_flags & VM_MAYSHARE)
 		return ret;
-	else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) && ret >= 0) {
-		/*
-		 * In most cases, reserves always exist for private mappings.
-		 * However, a file associated with mapping could have been
-		 * hole punched or truncated after reserves were consumed.
-		 * As subsequent fault on such a range will not use reserves.
-		 * Subtle - The reserve map for private mappings has the
-		 * opposite meaning than that of shared mappings.  If NO
-		 * entry is in the reserve map, it means a reservation exists.
-		 * If an entry exists in the reserve map, it means the
-		 * reservation has already been consumed.  As a result, the
-		 * return value of this routine is the opposite of the
-		 * value returned from reserve map manipulation routines above.
-		 */
-		if (ret)
-			return 0;
-		else
-			return 1;
-	}
-	else
-		return ret < 0 ? ret : 0;
+	/*
+	 * We know private mapping must have HPAGE_RESV_OWNER set.
+	 *
+	 * In most cases, reserves always exist for private mappings.
+	 * However, a file associated with mapping could have been
+	 * hole punched or truncated after reserves were consumed.
+	 * As subsequent fault on such a range will not use reserves.
+	 * Subtle - The reserve map for private mappings has the
+	 * opposite meaning than that of shared mappings.  If NO
+	 * entry is in the reserve map, it means a reservation exists.
+	 * If an entry exists in the reserve map, it means the
+	 * reservation has already been consumed.  As a result, the
+	 * return value of this routine is the opposite of the
+	 * value returned from reserve map manipulation routines above.
+	 */
+	if (ret > 0)
+		return 0;
+	if (ret == 0)
+		return 1;
+	return ret;
 }
 
 static long vma_needs_reservation(struct hstate *h,
Signature · 1 line
2.19.1
Reviewed-by on v2 2/5
#3 lore raw

Re: [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common()

Quoted 7 lines · Miaohe Lin

It's guaranteed that the vma is associated with a resv_map, i.e. either VM_MAYSHARE or HPAGE_RESV_OWNER, when the code reaches here or we would have returned via !resv check above. So it's unneeded to check whether HPAGE_RESV_OWNER is set here. Simplify the return code to make it more clear.

Thanks,

Signature · 1 line
Mike Kravetz
author
#4 lore raw

PATCH v2 3/5 mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages()

In reply to Miaohe Lin

The resv_map could be NULL since this routine can be called in the evict inode path for all hugetlbfs inodes and we will have chg = 0 in this case. But (chg - freed) won't go negative as Mike pointed out:

"If resv_map is NULL, then no hugetlb pages can be allocated/associated
with the file. As a result, remove_inode_hugepages will never find any huge pages associated with the inode and the passed value 'freed' will always be zero."

Add a comment clarifying this to make it clear and also avoid confusion.

---
 mm/hugetlb.c | 3 +++
 1 file changed, 3 insertions(+)
mm/hugetlb.c +3 -0
index 9b4c05699a90..387c9a62615e 100644
@@ -5435,6 +5435,9 @@ long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
 	/*
 	 * If the subpool has a minimum size, the number of global
 	 * reservations to be released may be adjusted.
+	 *
+	 * Note that !resv_map implies freed == 0. So (chg - freed)
+	 * won't go negative.
 	 */
 	gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
 	hugetlb_acct_memory(h, -gbl_reserve);
Signature · 1 line
2.19.1
Reviewed-by on v2 3/5
#5 lore raw

Re: [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages()

Quoted 4 lines · Miaohe Lin

The resv_map could be NULL since this routine can be called in the evict inode path for all hugetlbfs inodes and we will have chg = 0 in this case. But (chg - freed) won't go negative as Mike pointed out:

Quoted 8 lines

"If resv_map is NULL, then no hugetlb pages can be allocated/associated
with the file. As a result, remove_inode_hugepages will never find any huge pages associated with the inode and the passed value 'freed' will always be zero."

Add a comment clarifying this to make it clear and also avoid confusion.

Thanks,

Signature · 1 line
Mike Kravetz
author
#6 lore raw

PATCH v2 4/5 mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts()

In reply to Miaohe Lin

A rare out of memory error would prevent removal of the reserve map region for a page. hugetlb_fix_reserve_counts() handles this rare case to avoid dangling with incorrect counts. Unfortunately, hugepage_subpool_get_pages and hugetlb_acct_memory could possibly fail too. We should correctly handle these cases.

Fixes b5cec28d36f5 ("hugetlbfs: truncate_hugepages() takes a range of pages")
---
 mm/hugetlb.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/hugetlb.c +9 -2
index 387c9a62615e..a14bb1a03ee4 100644
@@ -745,13 +745,20 @@ void hugetlb_fix_reserve_counts(struct inode *inode)
 {
 	struct hugepage_subpool *spool = subpool_inode(inode);
 	long rsv_adjust;
+	bool reserved = false;
 
 	rsv_adjust = hugepage_subpool_get_pages(spool, 1);
-	if (rsv_adjust) {
+	if (rsv_adjust > 0) {
 		struct hstate *h = hstate_inode(inode);
 
-		hugetlb_acct_memory(h, 1);
+		if (!hugetlb_acct_memory(h, 1))
+			reserved = true;
+	} else if (!rsv_adjust) {
+		reserved = true;
 	}
+
+	if (!reserved)
+		pr_warn("hugetlb: Huge Page Reserved count may go negative.\n");
 }
 
 /*
Signature · 1 line
2.19.1
Reviewed-by on v2 4/5
#7 lore raw

Re: [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts()

Quoted 8 lines · Miaohe Lin

A rare out of memory error would prevent removal of the reserve map region for a page. hugetlb_fix_reserve_counts() handles this rare case to avoid dangling with incorrect counts. Unfortunately, hugepage_subpool_get_pages and hugetlb_acct_memory could possibly fail too. We should correctly handle these cases.

Fixes b5cec28d36f5 ("hugetlbfs: truncate_hugepages() takes a range of pages")

Thanks,

Signature · 1 line
Mike Kravetz
author
#8 lore raw

PATCH v2 5/5 mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages()

In reply to Miaohe Lin

The local variable pseudo_vma is not used anymore.

---
 fs/hugetlbfs/inode.c | 3 ---
 1 file changed, 3 deletions(-)
fs/hugetlbfs/inode.c +0 -3
index d81f52b87bd7..a2a42335e8fd 100644
@@ -463,14 +463,11 @@ static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
 	struct address_space *mapping = &inode->i_data;
 	const pgoff_t start = lstart >> huge_page_shift(h);
 	const pgoff_t end = lend >> huge_page_shift(h);
-	struct vm_area_struct pseudo_vma;
 	struct pagevec pvec;
 	pgoff_t next, index;
 	int i, freed = 0;
 	bool truncate_op = (lend == LLONG_MAX);
 
-	vma_init(&pseudo_vma, current->mm);
-	pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
 	pagevec_init(&pvec);
 	next = start;
 	while (next < end) {
Signature · 1 line
2.19.1
Reviewed-by on v2 5/5
#9 lore raw

Re: [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages()

Quoted 3 lines · Miaohe Lin

The local variable pseudo_vma is not used anymore.

Thanks,

That should have been removed with 1b426bac66e6 ("hugetlb: use same fault hash key for shared and private mappings").

Signature · 1 line
Mike Kravetz
author
#10 lore raw

Re: [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages()

Quoted 10 lines · Mike Kravetz
Quoted 3 lines · Miaohe Lin

The local variable pseudo_vma is not used anymore.

Thanks,

That should have been removed with 1b426bac66e6 ("hugetlb: use same fault hash key for shared and private mappings").

Yep. Many thanks for Reviewed-by tag! :)

Quoted 2 lines