All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V2] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing
@ 2021-01-04  8:16 Li Xinhai
  2021-01-05 18:23 ` Mike Kravetz
  0 siblings, 1 reply; 3+ messages in thread
From: Li Xinhai @ 2021-01-04  8:16 UTC (permalink / raw)
  To: linux-mm; +Cc: akpm, Mike Kravetz, Peter Xu

The current code would unnecessarily expand the address range. Consider
one example, (start, end) = (1G-2M, 3G+2M), and (vm_start, vm_end) =
(1G-4M, 3G+4M), the expected adjustment should be keep (1G-2M, 3G+2M)
without expand. But the current result will be (1G-4M, 3G+4M). Actually,
the range (1G-4M, 1G) and (3G, 3G+4M) would never been involved in pmd
sharing.

After this patch, we will check that the vma span at least one PUD
aligned size and the start,end range overlap the aligned range of vma.

With above example, the aligned vma range is (1G, 3G), so if (start, end)
range is within (1G-4M, 1G), or within (3G, 3G+4M), then no adjustment
to both start and end. Otherwise, we will have chance to adjust start
downwards or end upwards without exceeding (vm_start, vm_end).

Fixes: 75802ca66354 ("mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible")
Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: Li Xinhai <lixinhai.lxh@gmail.com>
---

v2<-v1:
Mike found that previous patch didn't fix all possible cases, and
proposed new patch to fully fix the existing issue, and also suggested
to consider checking the vma's size at the begining of the function.

Yes, we can simplify the code a bit wiht less comparison by aligning
vma's size first and then do the checking with start,end.
Hope this new one is also easier to understand.

 mm/hugetlb.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index cbf32d2824fd..a41ce2b22275 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5239,21 +5239,23 @@ static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
 				unsigned long *start, unsigned long *end)
 {
-	unsigned long a_start, a_end;
+	unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE),
+		v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
 
-	if (!(vma->vm_flags & VM_MAYSHARE))
+	/*
+	 * vma need span at least one aligned PUD size and the start,end range
+	 * must at least partialy within it.
+	 */
+	if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) ||
+		(*end <= v_start) || (*start >= v_end))
 		return;
 
 	/* Extend the range to be PUD aligned for a worst case scenario */
-	a_start = ALIGN_DOWN(*start, PUD_SIZE);
-	a_end = ALIGN(*end, PUD_SIZE);
+	if (*start > v_start)
+		*start = ALIGN_DOWN(*start, PUD_SIZE);
 
-	/*
-	 * Intersect the range with the vma range, since pmd sharing won't be
-	 * across vma after all
-	 */
-	*start = max(vma->vm_start, a_start);
-	*end = min(vma->vm_end, a_end);
+	if (*end < v_end)
+		*end = ALIGN(*end, PUD_SIZE);
 }
 
 /*
-- 
2.18.4



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing
  2021-01-04  8:16 [PATCH V2] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing Li Xinhai
@ 2021-01-05 18:23 ` Mike Kravetz
  2021-01-06  0:49   ` Li Xinhai
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Kravetz @ 2021-01-05 18:23 UTC (permalink / raw)
  To: Li Xinhai, linux-mm; +Cc: akpm, Peter Xu

On 1/4/21 12:16 AM, Li Xinhai wrote:
> The current code would unnecessarily expand the address range. Consider
> one example, (start, end) = (1G-2M, 3G+2M), and (vm_start, vm_end) =
> (1G-4M, 3G+4M), the expected adjustment should be keep (1G-2M, 3G+2M)
> without expand. But the current result will be (1G-4M, 3G+4M). Actually,
> the range (1G-4M, 1G) and (3G, 3G+4M) would never been involved in pmd
> sharing.
> 
> After this patch, we will check that the vma span at least one PUD
> aligned size and the start,end range overlap the aligned range of vma.
> 
> With above example, the aligned vma range is (1G, 3G), so if (start, end)
> range is within (1G-4M, 1G), or within (3G, 3G+4M), then no adjustment
> to both start and end. Otherwise, we will have chance to adjust start
> downwards or end upwards without exceeding (vm_start, vm_end).
> 
> Fixes: 75802ca66354 ("mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible")
> Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
> Cc: Mike Kravetz <mike.kravetz@oracle.com>
> Cc: Peter Xu <peterx@redhat.com>
> Signed-off-by: Li Xinhai <lixinhai.lxh@gmail.com>
> ---

Thanks again.  Comments and discussion in previous thread.

Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>

-- 
Mike Kravetz


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH V2] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing
  2021-01-05 18:23 ` Mike Kravetz
@ 2021-01-06  0:49   ` Li Xinhai
  0 siblings, 0 replies; 3+ messages in thread
From: Li Xinhai @ 2021-01-06  0:49 UTC (permalink / raw)
  To: Mike Kravetz, linux-mm; +Cc: akpm, Peter Xu



On 1/6/21 2:23 AM, Mike Kravetz wrote:
> On 1/4/21 12:16 AM, Li Xinhai wrote:
>> The current code would unnecessarily expand the address range. Consider
>> one example, (start, end) = (1G-2M, 3G+2M), and (vm_start, vm_end) =
>> (1G-4M, 3G+4M), the expected adjustment should be keep (1G-2M, 3G+2M)
>> without expand. But the current result will be (1G-4M, 3G+4M). Actually,
>> the range (1G-4M, 1G) and (3G, 3G+4M) would never been involved in pmd
>> sharing.
>>
>> After this patch, we will check that the vma span at least one PUD
>> aligned size and the start,end range overlap the aligned range of vma.
>>
>> With above example, the aligned vma range is (1G, 3G), so if (start, end)
>> range is within (1G-4M, 1G), or within (3G, 3G+4M), then no adjustment
>> to both start and end. Otherwise, we will have chance to adjust start
>> downwards or end upwards without exceeding (vm_start, vm_end).
>>
>> Fixes: 75802ca66354 ("mm/hugetlb: fix calculation of adjust_range_if_pmd_sharing_possible")
>> Suggested-by: Mike Kravetz <mike.kravetz@oracle.com>
>> Cc: Mike Kravetz <mike.kravetz@oracle.com>
>> Cc: Peter Xu <peterx@redhat.com>
>> Signed-off-by: Li Xinhai <lixinhai.lxh@gmail.com>
>> ---
> 
> Thanks again.  Comments and discussion in previous thread.
> 
> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
> 

Thanks for review.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-06  0:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-04  8:16 [PATCH V2] mm/hugetlb.c: fix unnecessary address expansion of pmd sharing Li Xinhai
2021-01-05 18:23 ` Mike Kravetz
2021-01-06  0:49   ` Li Xinhai

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.