Linux-mm Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] mm/hugetlb: Fix calculation of adjust_range_if_pmd_sharing_possible
@ 2020-07-30 19:50 Peter Xu
  2020-07-30 19:57 ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Xu @ 2020-07-30 19:50 UTC (permalink / raw)
  To: linux-kernel, linux-mm
  Cc: peterx, Andrew Morton, Andrea Arcangeli, Mike Kravetz

This is found by code observation only.

Firstly, the worst case scenario should assume the whole range was covered by
pmd sharing.  The old algorithm might not work as expected for ranges
like (1g-2m, 1g+2m), where the adjusted range should be (0, 1g+2m) but the
expected range should be (0, 2g).

Since at it, remove the loop since it should not be required.  With that, the
new code should be faster too when the invalidating range is huge.

CC: Andrea Arcangeli <aarcange@redhat.com>
CC: Mike Kravetz <mike.kravetz@oracle.com>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: linux-mm@kvack.org
CC: linux-kernel@vger.kernel.org
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 mm/hugetlb.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 4645f1441d32..0e5a0512c13c 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -43,6 +43,9 @@
 #include <linux/page_owner.h>
 #include "internal.h"
 
+#define MAX(a,b)  (((a)>(b))?(a):(b))
+#define MIN(a,b)  (((a)<(b))?(a):(b))
+
 int hugetlb_max_hstate __read_mostly;
 unsigned int default_hstate_idx;
 struct hstate hstates[HUGE_MAX_HSTATE];
@@ -5321,25 +5324,21 @@ 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 check_addr;
+	unsigned long a_start, a_end;
 
 	if (!(vma->vm_flags & VM_MAYSHARE))
 		return;
 
-	for (check_addr = *start; check_addr < *end; check_addr += PUD_SIZE) {
-		unsigned long a_start = check_addr & PUD_MASK;
-		unsigned long a_end = a_start + PUD_SIZE;
+	/* 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 sharing is possible, adjust start/end if necessary.
-		 */
-		if (range_in_vma(vma, a_start, a_end)) {
-			if (a_start < *start)
-				*start = a_start;
-			if (a_end > *end)
-				*end = a_end;
-		}
-	}
+	/*
+	 * 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);
 }
 
 /*
-- 
2.26.2



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

* Re: [PATCH] mm/hugetlb: Fix calculation of adjust_range_if_pmd_sharing_possible
  2020-07-30 19:50 [PATCH] mm/hugetlb: Fix calculation of adjust_range_if_pmd_sharing_possible Peter Xu
@ 2020-07-30 19:57 ` Matthew Wilcox
  2020-07-30 20:10   ` Peter Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Wilcox @ 2020-07-30 19:57 UTC (permalink / raw)
  To: Peter Xu
  Cc: linux-kernel, linux-mm, Andrew Morton, Andrea Arcangeli, Mike Kravetz

On Thu, Jul 30, 2020 at 03:50:30PM -0400, Peter Xu wrote:
> +#define MAX(a,b)  (((a)>(b))?(a):(b))
> +#define MIN(a,b)  (((a)<(b))?(a):(b))

What's wrong with max() and min() defined in kernel.h?



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

* Re: [PATCH] mm/hugetlb: Fix calculation of adjust_range_if_pmd_sharing_possible
  2020-07-30 19:57 ` Matthew Wilcox
@ 2020-07-30 20:10   ` Peter Xu
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Xu @ 2020-07-30 20:10 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: linux-kernel, linux-mm, Andrew Morton, Andrea Arcangeli, Mike Kravetz

On Thu, Jul 30, 2020 at 08:57:22PM +0100, Matthew Wilcox wrote:
> On Thu, Jul 30, 2020 at 03:50:30PM -0400, Peter Xu wrote:
> > +#define MAX(a,b)  (((a)>(b))?(a):(b))
> > +#define MIN(a,b)  (((a)<(b))?(a):(b))
> 
> What's wrong with max() and min() defined in kernel.h?

Oops.. Posting another one... Thanks.

-- 
Peter Xu



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

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-30 19:50 [PATCH] mm/hugetlb: Fix calculation of adjust_range_if_pmd_sharing_possible Peter Xu
2020-07-30 19:57 ` Matthew Wilcox
2020-07-30 20:10   ` Peter Xu

Linux-mm Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-mm/0 linux-mm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-mm linux-mm/ https://lore.kernel.org/linux-mm \
		linux-mm@kvack.org
	public-inbox-index linux-mm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kvack.linux-mm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git