2021-04-10 07:26:11

by Miaohe Lin

[permalink] [raw]
Subject: [PATCH v2 0/5] 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(-)

--
2.19.1


2021-04-10 07:26:45

by Miaohe Lin

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

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 <[email protected]>
---
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

2021-04-10 07:27:43

by Miaohe Lin

[permalink] [raw]
Subject: [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.

Reviewed-by: Mike Kravetz <[email protected]>
Signed-off-by: Miaohe Lin <[email protected]>
---
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

2021-04-10 07:27:46

by Miaohe Lin

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

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 <[email protected]>
---
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

2021-04-10 07:28:13

by Miaohe Lin

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

The local variable pseudo_vma is not used anymore.

Signed-off-by: Miaohe Lin <[email protected]>
---
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

2021-04-13 06:32:17

by Mike Kravetz

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

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 <[email protected]>

Thanks,

Reviewed-by: Mike Kravetz <[email protected]>
--
Mike Kravetz

2021-04-13 06:47:06

by Mike Kravetz

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

On 4/10/21 12:23 AM, Miaohe Lin wrote:
> The local variable pseudo_vma is not used anymore.
>
> Signed-off-by: Miaohe Lin <[email protected]>

Thanks,

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

Reviewed-by: Mike Kravetz <[email protected]>
--
Mike Kravetz

2021-04-13 10:45:55

by Mike Kravetz

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

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 <[email protected]>

Thanks,

Reviewed-by: Mike Kravetz <[email protected]>
--
Mike Kravetz

2021-04-13 13:10:26

by Miaohe Lin

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

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 <[email protected]>
>
> 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 <[email protected]>
>