* [PATCH v2 0/5] Cleanup and fixup for hugetlb
@ 2021-04-10 7:23 Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 1/5] mm/hugeltb: remove redundant VM_BUG_ON() in region_add() Miaohe Lin
` (4 more replies)
0 siblings, 5 replies; 11+ messages in thread
From: Miaohe Lin @ 2021-04-10 7:23 UTC (permalink / raw)
To: akpm, mike.kravetz; +Cc: linux-kernel, linux-mm, linmiaohe, linfeilong
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(-)
--
2.19.1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 1/5] mm/hugeltb: remove redundant VM_BUG_ON() in region_add()
2021-04-10 7:23 [PATCH v2 0/5] Cleanup and fixup for hugetlb Miaohe Lin
@ 2021-04-10 7:23 ` Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common() Miaohe Lin
` (3 subsequent siblings)
4 siblings, 0 replies; 11+ messages in thread
From: Miaohe Lin @ 2021-04-10 7:23 UTC (permalink / raw)
To: akpm, mike.kravetz; +Cc: linux-kernel, linux-mm, linmiaohe, linfeilong
The same VM_BUG_ON() check is already done in the callee. Remove this extra
one to simplify the code slightly.
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
mm/hugetlb.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index c22111f3da20..a03a50b7c410 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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;
}
--
2.19.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common()
2021-04-10 7:23 [PATCH v2 0/5] Cleanup and fixup for hugetlb Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 1/5] mm/hugeltb: remove redundant VM_BUG_ON() in region_add() Miaohe Lin
@ 2021-04-10 7:23 ` Miaohe Lin
2021-04-12 18:26 ` Mike Kravetz
2021-04-10 7:23 ` [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages() Miaohe Lin
` (2 subsequent siblings)
4 siblings, 1 reply; 11+ messages in thread
From: Miaohe Lin @ 2021-04-10 7:23 UTC (permalink / raw)
To: akpm, mike.kravetz; +Cc: linux-kernel, linux-mm, linmiaohe, linfeilong
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.
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
mm/hugetlb.c | 41 ++++++++++++++++++++---------------------
1 file changed, 20 insertions(+), 21 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index a03a50b7c410..9b4c05699a90 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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,
--
2.19.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages()
2021-04-10 7:23 [PATCH v2 0/5] Cleanup and fixup for hugetlb Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 1/5] mm/hugeltb: remove redundant VM_BUG_ON() in region_add() Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common() Miaohe Lin
@ 2021-04-10 7:23 ` Miaohe Lin
2021-04-12 18:28 ` Mike Kravetz
2021-04-10 7:23 ` [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts() Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages() Miaohe Lin
4 siblings, 1 reply; 11+ messages in thread
From: Miaohe Lin @ 2021-04-10 7:23 UTC (permalink / raw)
To: akpm, mike.kravetz; +Cc: linux-kernel, linux-mm, linmiaohe, linfeilong
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.
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
mm/hugetlb.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 9b4c05699a90..387c9a62615e 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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);
--
2.19.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts()
2021-04-10 7:23 [PATCH v2 0/5] Cleanup and fixup for hugetlb Miaohe Lin
` (2 preceding siblings ...)
2021-04-10 7:23 ` [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages() Miaohe Lin
@ 2021-04-10 7:23 ` Miaohe Lin
2021-04-12 18:30 ` Mike Kravetz
2021-04-10 7:23 ` [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages() Miaohe Lin
4 siblings, 1 reply; 11+ messages in thread
From: Miaohe Lin @ 2021-04-10 7:23 UTC (permalink / raw)
To: akpm, mike.kravetz; +Cc: linux-kernel, linux-mm, linmiaohe, linfeilong
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")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
mm/hugetlb.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 387c9a62615e..a14bb1a03ee4 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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");
}
/*
--
2.19.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages()
2021-04-10 7:23 [PATCH v2 0/5] Cleanup and fixup for hugetlb Miaohe Lin
` (3 preceding siblings ...)
2021-04-10 7:23 ` [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts() Miaohe Lin
@ 2021-04-10 7:23 ` Miaohe Lin
2021-04-12 18:51 ` Mike Kravetz
4 siblings, 1 reply; 11+ messages in thread
From: Miaohe Lin @ 2021-04-10 7:23 UTC (permalink / raw)
To: akpm, mike.kravetz; +Cc: linux-kernel, linux-mm, linmiaohe, linfeilong
The local variable pseudo_vma is not used anymore.
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
fs/hugetlbfs/inode.c | 3 ---
1 file changed, 3 deletions(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index d81f52b87bd7..a2a42335e8fd 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -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) {
--
2.19.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common()
2021-04-10 7:23 ` [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common() Miaohe Lin
@ 2021-04-12 18:26 ` Mike Kravetz
0 siblings, 0 replies; 11+ messages in thread
From: Mike Kravetz @ 2021-04-12 18:26 UTC (permalink / raw)
To: Miaohe Lin, akpm; +Cc: linux-kernel, linux-mm, linfeilong
On 4/10/21 12:23 AM, Miaohe Lin wrote:
> 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.
>
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Thanks,
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
--
Mike Kravetz
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages()
2021-04-10 7:23 ` [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages() Miaohe Lin
@ 2021-04-12 18:28 ` Mike Kravetz
0 siblings, 0 replies; 11+ messages in thread
From: Mike Kravetz @ 2021-04-12 18:28 UTC (permalink / raw)
To: Miaohe Lin, akpm; +Cc: linux-kernel, linux-mm, linfeilong
On 4/10/21 12:23 AM, Miaohe Lin wrote:
> 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.
>
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Thanks,
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
--
Mike Kravetz
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts()
2021-04-10 7:23 ` [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts() Miaohe Lin
@ 2021-04-12 18:30 ` Mike Kravetz
0 siblings, 0 replies; 11+ messages in thread
From: Mike Kravetz @ 2021-04-12 18:30 UTC (permalink / raw)
To: Miaohe Lin, akpm; +Cc: linux-kernel, linux-mm, linfeilong
On 4/10/21 12:23 AM, Miaohe Lin wrote:
> 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")
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Thanks,
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
--
Mike Kravetz
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages()
2021-04-10 7:23 ` [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages() Miaohe Lin
@ 2021-04-12 18:51 ` Mike Kravetz
2021-04-13 2:22 ` Miaohe Lin
0 siblings, 1 reply; 11+ messages in thread
From: Mike Kravetz @ 2021-04-12 18:51 UTC (permalink / raw)
To: Miaohe Lin, akpm; +Cc: linux-kernel, linux-mm, linfeilong
On 4/10/21 12:23 AM, Miaohe Lin wrote:
> The local variable pseudo_vma is not used anymore.
>
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Thanks,
That should have been removed with 1b426bac66e6 ("hugetlb: use same fault
hash key for shared and private mappings").
Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
--
Mike Kravetz
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages()
2021-04-12 18:51 ` Mike Kravetz
@ 2021-04-13 2:22 ` Miaohe Lin
0 siblings, 0 replies; 11+ messages in thread
From: Miaohe Lin @ 2021-04-13 2:22 UTC (permalink / raw)
To: Mike Kravetz, akpm; +Cc: linux-kernel, linux-mm
On 2021/4/13 2:51, Mike Kravetz wrote:
> On 4/10/21 12:23 AM, Miaohe Lin wrote:
>> The local variable pseudo_vma is not used anymore.
>>
>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>
> 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! :)
> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2021-04-13 2:24 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-10 7:23 [PATCH v2 0/5] Cleanup and fixup for hugetlb Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 1/5] mm/hugeltb: remove redundant VM_BUG_ON() in region_add() Miaohe Lin
2021-04-10 7:23 ` [PATCH v2 2/5] mm/hugeltb: simplify the return code of __vma_reservation_common() Miaohe Lin
2021-04-12 18:26 ` Mike Kravetz
2021-04-10 7:23 ` [PATCH v2 3/5] mm/hugeltb: clarify (chg - freed) won't go negative in hugetlb_unreserve_pages() Miaohe Lin
2021-04-12 18:28 ` Mike Kravetz
2021-04-10 7:23 ` [PATCH v2 4/5] mm/hugeltb: handle the error case in hugetlb_fix_reserve_counts() Miaohe Lin
2021-04-12 18:30 ` Mike Kravetz
2021-04-10 7:23 ` [PATCH v2 5/5] mm/hugetlb: remove unused variable pseudo_vma in remove_inode_hugepages() Miaohe Lin
2021-04-12 18:51 ` Mike Kravetz
2021-04-13 2:22 ` Miaohe Lin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).