linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] Refactor of vma_merge and new merge call
@ 2022-06-03 14:57 Jakub Matěna
  2022-06-03 14:57 ` [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge() Jakub Matěna
  2022-06-03 14:57 ` [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize Jakub Matěna
  0 siblings, 2 replies; 5+ messages in thread
From: Jakub Matěna @ 2022-06-03 14:57 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, patches, linux-kernel, vbabka, mhocko, mgorman, willy,
	liam.howlett, hughd, kirill, riel, rostedt, peterz,
	Jakub Matěna

I am currently working on my master's thesis trying to increase
number of merges of VMAs currently failing because of page offset
incompatibility and difference in their anon_vmas. The following
refactor and added merge call included in this series is just two
smaller upgrades I created along the way.

The rest of the work is still being worked on but was send to mailing
list as a RFC:
https://lore.kernel.org/all/20220516125405.1675-1-matenajakub@gmail.com/

This patch series is based on mm-unstable branch (specifically commit
1bdf44dbc966e31c635199d413ee064a7d320844). This is fourth version,
I added a mremap expand merge test, added a comment and new variables to
the merge call and based the series on the mm-unstable kernel branch.

Jakub Matěna (2):
  mm: refactor of vma_merge()
  mm: add merging after mremap resize

 mm/mmap.c   | 87 +++++++++++++++++++++++------------------------------
 mm/mremap.c |  8 +++--
 2 files changed, 43 insertions(+), 52 deletions(-)

-- 
2.35.1



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

* [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge()
  2022-06-03 14:57 [PATCH v4 0/2] Refactor of vma_merge and new merge call Jakub Matěna
@ 2022-06-03 14:57 ` Jakub Matěna
  2022-06-16  8:17   ` Vlastimil Babka
  2022-06-03 14:57 ` [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize Jakub Matěna
  1 sibling, 1 reply; 5+ messages in thread
From: Jakub Matěna @ 2022-06-03 14:57 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, patches, linux-kernel, vbabka, mhocko, mgorman, willy,
	liam.howlett, hughd, kirill, riel, rostedt, peterz,
	Jakub Matěna

Refactor vma_merge() to make it shorter and more understandable.
Main change is the elimination of code duplicity in the case of
merge next check. This is done by first doing checks and caching
the results before executing the merge itself. The variable 'area' is
divided into 'mid' and 'res' as previously it was used for two purposes,
as the middle VMA between prev and next and also as the result of the
merge itself. Exit paths are also unified.

Signed-off-by: Jakub Matěna <matenajakub@gmail.com>
---
 mm/mmap.c | 87 +++++++++++++++++++++++--------------------------------
 1 file changed, 37 insertions(+), 50 deletions(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index 313b57d55a63..91100fdc400a 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1170,8 +1170,10 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 			struct anon_vma_name *anon_name)
 {
 	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
-	struct vm_area_struct *area, *next;
-	int err;
+	struct vm_area_struct *mid, *next, *res;
+	int err = -1;
+	bool merge_prev = false;
+	bool merge_next = false;
 
 	/*
 	 * We later require that vma->vm_flags == vm_flags,
@@ -1181,75 +1183,60 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 		return NULL;
 
 	next = find_vma(mm, prev ? prev->vm_end : 0);
-	area = next;
-	if (area && area->vm_end == end)		/* cases 6, 7, 8 */
+	mid = next;
+	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
 		next = find_vma(mm, next->vm_end);
 
 	/* verify some invariant that must be enforced by the caller */
 	VM_WARN_ON(prev && addr <= prev->vm_start);
-	VM_WARN_ON(area && end > area->vm_end);
+	VM_WARN_ON(mid && end > mid->vm_end);
 	VM_WARN_ON(addr >= end);
 
-	/*
-	 * Can it merge with the predecessor?
-	 */
+	/* Can we merge the predecessor? */
 	if (prev && prev->vm_end == addr &&
 			mpol_equal(vma_policy(prev), policy) &&
 			can_vma_merge_after(prev, vm_flags,
 					    anon_vma, file, pgoff,
 					    vm_userfaultfd_ctx, anon_name)) {
-		/*
-		 * OK, it can.  Can we now merge in the successor as well?
-		 */
-		if (next && end == next->vm_start &&
-				mpol_equal(policy, vma_policy(next)) &&
-				can_vma_merge_before(next, vm_flags,
-						     anon_vma, file,
-						     pgoff+pglen,
-						     vm_userfaultfd_ctx, anon_name) &&
-				is_mergeable_anon_vma(prev->anon_vma,
-						      next->anon_vma, NULL)) {
-							/* cases 1, 6 */
-			err = __vma_adjust(prev, prev->vm_start,
-					 next->vm_end, prev->vm_pgoff, NULL,
-					 prev);
-		} else					/* cases 2, 5, 7 */
-			err = __vma_adjust(prev, prev->vm_start,
-					 end, prev->vm_pgoff, NULL, prev);
-		if (err)
-			return NULL;
-		khugepaged_enter_vma(prev, vm_flags);
-		return prev;
+		merge_prev = true;
 	}
-
-	/*
-	 * Can this new request be merged in front of next?
-	 */
+	/* Can we merge the successor? */
 	if (next && end == next->vm_start &&
 			mpol_equal(policy, vma_policy(next)) &&
 			can_vma_merge_before(next, vm_flags,
 					     anon_vma, file, pgoff+pglen,
 					     vm_userfaultfd_ctx, anon_name)) {
+		merge_next = true;
+	}
+	/* Can we merge both the predecessor and the successor? */
+	if (merge_prev && merge_next &&
+			is_mergeable_anon_vma(prev->anon_vma,
+				next->anon_vma, NULL)) {	 /* cases 1, 6 */
+		err = __vma_adjust(prev, prev->vm_start,
+					next->vm_end, prev->vm_pgoff, NULL,
+					prev);
+		res = prev;
+	} else if (merge_prev) {			/* cases 2, 5, 7 */
+		err = __vma_adjust(prev, prev->vm_start,
+					end, prev->vm_pgoff, NULL, prev);
+		res = prev;
+	} else if (merge_next) {
 		if (prev && addr < prev->vm_end)	/* case 4 */
 			err = __vma_adjust(prev, prev->vm_start,
-					 addr, prev->vm_pgoff, NULL, next);
-		else {					/* cases 3, 8 */
-			err = __vma_adjust(area, addr, next->vm_end,
-					 next->vm_pgoff - pglen, NULL, next);
-			/*
-			 * In case 3 area is already equal to next and
-			 * this is a noop, but in case 8 "area" has
-			 * been removed and next was expanded over it.
-			 */
-			area = next;
-		}
-		if (err)
-			return NULL;
-		khugepaged_enter_vma(area, vm_flags);
-		return area;
+					addr, prev->vm_pgoff, NULL, next);
+		else					/* cases 3, 8 */
+			err = __vma_adjust(mid, addr, next->vm_end,
+					next->vm_pgoff - pglen, NULL, next);
+		res = next;
 	}
 
-	return NULL;
+	/*
+	 * Cannot merge with predecessor or successor or error in __vma_adjust?
+	 */
+	if (err)
+		return NULL;
+	khugepaged_enter_vma(res, vm_flags);
+	return res;
 }
 
 /*
-- 
2.35.1



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

* [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize
  2022-06-03 14:57 [PATCH v4 0/2] Refactor of vma_merge and new merge call Jakub Matěna
  2022-06-03 14:57 ` [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge() Jakub Matěna
@ 2022-06-03 14:57 ` Jakub Matěna
  2022-06-16  8:23   ` Vlastimil Babka
  1 sibling, 1 reply; 5+ messages in thread
From: Jakub Matěna @ 2022-06-03 14:57 UTC (permalink / raw)
  To: akpm
  Cc: linux-mm, patches, linux-kernel, vbabka, mhocko, mgorman, willy,
	liam.howlett, hughd, kirill, riel, rostedt, peterz,
	Jakub Matěna

When mremap call results in expansion, it might be possible to merge the
VMA with the next VMA which might become adjacent. This patch adds
vma_merge call after the expansion is done to try and merge.

Signed-off-by: Jakub Matěna <matenajakub@gmail.com>
---
 mm/mremap.c                              | 19 +++++++++-
 tools/testing/selftests/vm/mremap_test.c | 47 +++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/mm/mremap.c b/mm/mremap.c
index 0b93fac76851..66970dcd636a 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -9,6 +9,7 @@
  */
 
 #include <linux/mm.h>
+#include <linux/mm_inline.h>
 #include <linux/hugetlb.h>
 #include <linux/shm.h>
 #include <linux/ksm.h>
@@ -23,6 +24,7 @@
 #include <linux/mmu_notifier.h>
 #include <linux/uaccess.h>
 #include <linux/userfaultfd_k.h>
+#include <linux/mempolicy.h>
 
 #include <asm/cacheflush.h>
 #include <asm/tlb.h>
@@ -1014,6 +1016,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
 		/* can we just expand the current mapping? */
 		if (vma_expandable(vma, new_len - old_len)) {
 			long pages = (new_len - old_len) >> PAGE_SHIFT;
+			unsigned long extension_start = addr + old_len;
+			unsigned long extension_end = addr + new_len;
+			pgoff_t extension_pgoff = vma->vm_pgoff + (old_len >> PAGE_SHIFT);
 
 			if (vma->vm_flags & VM_ACCOUNT) {
 				if (security_vm_enough_memory_mm(mm, pages)) {
@@ -1022,8 +1027,18 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
 				}
 			}
 
-			if (vma_adjust(vma, vma->vm_start, addr + new_len,
-				       vma->vm_pgoff, NULL)) {
+			/*
+			 * Function vma_merge() is called on the extension we are adding to
+			 * the already existing vma, vma_merge() will merge this extension with
+			 * the already existing vma (expand operation itself) and possibly also
+			 * with the next vma if it becomes adjacent to the expanded vma and
+			 * otherwise compatible.
+			 */
+			vma = vma_merge(mm, vma, extension_start, extension_end,
+					vma->vm_flags, vma->anon_vma, vma->vm_file,
+					extension_pgoff, vma_policy(vma),
+					vma->vm_userfaultfd_ctx, anon_vma_name(vma));
+			if (!vma) {
 				vm_unacct_memory(pages);
 				ret = -ENOMEM;
 				goto out;
diff --git a/tools/testing/selftests/vm/mremap_test.c b/tools/testing/selftests/vm/mremap_test.c
index db0270127aeb..0865a6cb5bdb 100644
--- a/tools/testing/selftests/vm/mremap_test.c
+++ b/tools/testing/selftests/vm/mremap_test.c
@@ -118,6 +118,48 @@ static unsigned long long get_mmap_min_addr(void)
 	return addr;
 }
 
+/*
+ * This test validates that merge is called when expanding a mapping.
+ * Mapping containing three pages is created, middle page is unmapped
+ * and then the mapping containing the first page is expanded so that
+ * it fills the created hole. The two parts should merge creating
+ * single mapping with three pages.
+ */
+static void mremap_expand_merge(unsigned long page_size)
+{
+	char *test_name = "mremap expand merge";
+	FILE *fp;
+	char *line = NULL;
+	size_t len = 0;
+	bool success = false;
+
+	char *start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	munmap(start + page_size, page_size);
+	mremap(start, page_size, 2 * page_size, 0);
+
+	fp = fopen("/proc/self/maps", "r");
+	if (fp == NULL) {
+		ksft_test_result_fail("%s\n", test_name);
+		return;
+	}
+
+	while(getline(&line, &len, fp) != -1) {
+		char *first = strtok(line,"- ");
+		void *first_val = (void *) strtol(first, NULL, 16);
+		char *second = strtok(NULL,"- ");
+		void *second_val = (void *) strtol(second, NULL, 16);
+		if (first_val == start && second_val == start + 3 * page_size) {
+			success = true;
+			break;
+		}
+	}
+	if (success)
+		ksft_test_result_pass("%s\n", test_name);
+	else
+		ksft_test_result_fail("%s\n", test_name);
+	fclose(fp);
+}
+
 /*
  * Returns the start address of the mapping on success, else returns
  * NULL on failure.
@@ -336,6 +378,7 @@ int main(int argc, char **argv)
 	int i, run_perf_tests;
 	unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD;
 	unsigned int pattern_seed;
+	int num_expand_tests = 1;
 	struct test test_cases[MAX_TEST];
 	struct test perf_test_cases[MAX_PERF_TEST];
 	int page_size;
@@ -407,12 +450,14 @@ int main(int argc, char **argv)
 				(threshold_mb * _1MB >= _1GB);
 
 	ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ?
-		      ARRAY_SIZE(perf_test_cases) : 0));
+		      ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests);
 
 	for (i = 0; i < ARRAY_SIZE(test_cases); i++)
 		run_mremap_test_case(test_cases[i], &failures, threshold_mb,
 				     pattern_seed);
 
+	mremap_expand_merge(page_size);
+
 	if (run_perf_tests) {
 		ksft_print_msg("\n%s\n",
 		 "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");
-- 
2.35.1



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

* Re: [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge()
  2022-06-03 14:57 ` [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge() Jakub Matěna
@ 2022-06-16  8:17   ` Vlastimil Babka
  0 siblings, 0 replies; 5+ messages in thread
From: Vlastimil Babka @ 2022-06-16  8:17 UTC (permalink / raw)
  To: Jakub Matěna, akpm
  Cc: linux-mm, patches, linux-kernel, mhocko, mgorman, willy,
	liam.howlett, hughd, kirill, riel, rostedt, peterz

On 6/3/22 16:57, Jakub Matěna wrote:
> Refactor vma_merge() to make it shorter and more understandable.
> Main change is the elimination of code duplicity in the case of
> merge next check. This is done by first doing checks and caching
> the results before executing the merge itself. The variable 'area' is
> divided into 'mid' and 'res' as previously it was used for two purposes,
> as the middle VMA between prev and next and also as the result of the
> merge itself. Exit paths are also unified.
> 
> Signed-off-by: Jakub Matěna <matenajakub@gmail.com>

Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/mmap.c | 87 +++++++++++++++++++++++--------------------------------
>  1 file changed, 37 insertions(+), 50 deletions(-)
> 
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 313b57d55a63..91100fdc400a 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1170,8 +1170,10 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
>  			struct anon_vma_name *anon_name)
>  {
>  	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
> -	struct vm_area_struct *area, *next;
> -	int err;
> +	struct vm_area_struct *mid, *next, *res;
> +	int err = -1;
> +	bool merge_prev = false;
> +	bool merge_next = false;
>  
>  	/*
>  	 * We later require that vma->vm_flags == vm_flags,
> @@ -1181,75 +1183,60 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
>  		return NULL;
>  
>  	next = find_vma(mm, prev ? prev->vm_end : 0);
> -	area = next;
> -	if (area && area->vm_end == end)		/* cases 6, 7, 8 */
> +	mid = next;
> +	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
>  		next = find_vma(mm, next->vm_end);
>  
>  	/* verify some invariant that must be enforced by the caller */
>  	VM_WARN_ON(prev && addr <= prev->vm_start);
> -	VM_WARN_ON(area && end > area->vm_end);
> +	VM_WARN_ON(mid && end > mid->vm_end);
>  	VM_WARN_ON(addr >= end);
>  
> -	/*
> -	 * Can it merge with the predecessor?
> -	 */
> +	/* Can we merge the predecessor? */
>  	if (prev && prev->vm_end == addr &&
>  			mpol_equal(vma_policy(prev), policy) &&
>  			can_vma_merge_after(prev, vm_flags,
>  					    anon_vma, file, pgoff,
>  					    vm_userfaultfd_ctx, anon_name)) {
> -		/*
> -		 * OK, it can.  Can we now merge in the successor as well?
> -		 */
> -		if (next && end == next->vm_start &&
> -				mpol_equal(policy, vma_policy(next)) &&
> -				can_vma_merge_before(next, vm_flags,
> -						     anon_vma, file,
> -						     pgoff+pglen,
> -						     vm_userfaultfd_ctx, anon_name) &&
> -				is_mergeable_anon_vma(prev->anon_vma,
> -						      next->anon_vma, NULL)) {
> -							/* cases 1, 6 */
> -			err = __vma_adjust(prev, prev->vm_start,
> -					 next->vm_end, prev->vm_pgoff, NULL,
> -					 prev);
> -		} else					/* cases 2, 5, 7 */
> -			err = __vma_adjust(prev, prev->vm_start,
> -					 end, prev->vm_pgoff, NULL, prev);
> -		if (err)
> -			return NULL;
> -		khugepaged_enter_vma(prev, vm_flags);
> -		return prev;
> +		merge_prev = true;
>  	}
> -
> -	/*
> -	 * Can this new request be merged in front of next?
> -	 */
> +	/* Can we merge the successor? */
>  	if (next && end == next->vm_start &&
>  			mpol_equal(policy, vma_policy(next)) &&
>  			can_vma_merge_before(next, vm_flags,
>  					     anon_vma, file, pgoff+pglen,
>  					     vm_userfaultfd_ctx, anon_name)) {
> +		merge_next = true;
> +	}
> +	/* Can we merge both the predecessor and the successor? */
> +	if (merge_prev && merge_next &&
> +			is_mergeable_anon_vma(prev->anon_vma,
> +				next->anon_vma, NULL)) {	 /* cases 1, 6 */
> +		err = __vma_adjust(prev, prev->vm_start,
> +					next->vm_end, prev->vm_pgoff, NULL,
> +					prev);
> +		res = prev;
> +	} else if (merge_prev) {			/* cases 2, 5, 7 */
> +		err = __vma_adjust(prev, prev->vm_start,
> +					end, prev->vm_pgoff, NULL, prev);
> +		res = prev;
> +	} else if (merge_next) {
>  		if (prev && addr < prev->vm_end)	/* case 4 */
>  			err = __vma_adjust(prev, prev->vm_start,
> -					 addr, prev->vm_pgoff, NULL, next);
> -		else {					/* cases 3, 8 */
> -			err = __vma_adjust(area, addr, next->vm_end,
> -					 next->vm_pgoff - pglen, NULL, next);
> -			/*
> -			 * In case 3 area is already equal to next and
> -			 * this is a noop, but in case 8 "area" has
> -			 * been removed and next was expanded over it.
> -			 */
> -			area = next;
> -		}
> -		if (err)
> -			return NULL;
> -		khugepaged_enter_vma(area, vm_flags);
> -		return area;
> +					addr, prev->vm_pgoff, NULL, next);
> +		else					/* cases 3, 8 */
> +			err = __vma_adjust(mid, addr, next->vm_end,
> +					next->vm_pgoff - pglen, NULL, next);
> +		res = next;
>  	}
>  
> -	return NULL;
> +	/*
> +	 * Cannot merge with predecessor or successor or error in __vma_adjust?
> +	 */
> +	if (err)
> +		return NULL;
> +	khugepaged_enter_vma(res, vm_flags);
> +	return res;
>  }
>  
>  /*



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

* Re: [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize
  2022-06-03 14:57 ` [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize Jakub Matěna
@ 2022-06-16  8:23   ` Vlastimil Babka
  0 siblings, 0 replies; 5+ messages in thread
From: Vlastimil Babka @ 2022-06-16  8:23 UTC (permalink / raw)
  To: Jakub Matěna, akpm
  Cc: linux-mm, patches, linux-kernel, mhocko, mgorman, willy,
	liam.howlett, hughd, kirill, riel, rostedt, peterz

On 6/3/22 16:57, Jakub Matěna wrote:
> When mremap call results in expansion, it might be possible to merge the
> VMA with the next VMA which might become adjacent. This patch adds
> vma_merge call after the expansion is done to try and merge.
> 
> Signed-off-by: Jakub Matěna <matenajakub@gmail.com>

Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/mremap.c                              | 19 +++++++++-
>  tools/testing/selftests/vm/mremap_test.c | 47 +++++++++++++++++++++++-
>  2 files changed, 63 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/mremap.c b/mm/mremap.c
> index 0b93fac76851..66970dcd636a 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -9,6 +9,7 @@
>   */
>  
>  #include <linux/mm.h>
> +#include <linux/mm_inline.h>
>  #include <linux/hugetlb.h>
>  #include <linux/shm.h>
>  #include <linux/ksm.h>
> @@ -23,6 +24,7 @@
>  #include <linux/mmu_notifier.h>
>  #include <linux/uaccess.h>
>  #include <linux/userfaultfd_k.h>
> +#include <linux/mempolicy.h>
>  
>  #include <asm/cacheflush.h>
>  #include <asm/tlb.h>
> @@ -1014,6 +1016,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
>  		/* can we just expand the current mapping? */
>  		if (vma_expandable(vma, new_len - old_len)) {
>  			long pages = (new_len - old_len) >> PAGE_SHIFT;
> +			unsigned long extension_start = addr + old_len;
> +			unsigned long extension_end = addr + new_len;
> +			pgoff_t extension_pgoff = vma->vm_pgoff + (old_len >> PAGE_SHIFT);
>  
>  			if (vma->vm_flags & VM_ACCOUNT) {
>  				if (security_vm_enough_memory_mm(mm, pages)) {
> @@ -1022,8 +1027,18 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
>  				}
>  			}
>  
> -			if (vma_adjust(vma, vma->vm_start, addr + new_len,
> -				       vma->vm_pgoff, NULL)) {
> +			/*
> +			 * Function vma_merge() is called on the extension we are adding to
> +			 * the already existing vma, vma_merge() will merge this extension with
> +			 * the already existing vma (expand operation itself) and possibly also
> +			 * with the next vma if it becomes adjacent to the expanded vma and
> +			 * otherwise compatible.
> +			 */
> +			vma = vma_merge(mm, vma, extension_start, extension_end,
> +					vma->vm_flags, vma->anon_vma, vma->vm_file,
> +					extension_pgoff, vma_policy(vma),
> +					vma->vm_userfaultfd_ctx, anon_vma_name(vma));
> +			if (!vma) {
>  				vm_unacct_memory(pages);
>  				ret = -ENOMEM;
>  				goto out;
> diff --git a/tools/testing/selftests/vm/mremap_test.c b/tools/testing/selftests/vm/mremap_test.c
> index db0270127aeb..0865a6cb5bdb 100644
> --- a/tools/testing/selftests/vm/mremap_test.c
> +++ b/tools/testing/selftests/vm/mremap_test.c
> @@ -118,6 +118,48 @@ static unsigned long long get_mmap_min_addr(void)
>  	return addr;
>  }
>  
> +/*
> + * This test validates that merge is called when expanding a mapping.
> + * Mapping containing three pages is created, middle page is unmapped
> + * and then the mapping containing the first page is expanded so that
> + * it fills the created hole. The two parts should merge creating
> + * single mapping with three pages.
> + */
> +static void mremap_expand_merge(unsigned long page_size)
> +{
> +	char *test_name = "mremap expand merge";
> +	FILE *fp;
> +	char *line = NULL;
> +	size_t len = 0;
> +	bool success = false;
> +
> +	char *start = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +	munmap(start + page_size, page_size);
> +	mremap(start, page_size, 2 * page_size, 0);
> +
> +	fp = fopen("/proc/self/maps", "r");
> +	if (fp == NULL) {
> +		ksft_test_result_fail("%s\n", test_name);
> +		return;
> +	}
> +
> +	while(getline(&line, &len, fp) != -1) {
> +		char *first = strtok(line,"- ");
> +		void *first_val = (void *) strtol(first, NULL, 16);
> +		char *second = strtok(NULL,"- ");
> +		void *second_val = (void *) strtol(second, NULL, 16);
> +		if (first_val == start && second_val == start + 3 * page_size) {
> +			success = true;
> +			break;
> +		}
> +	}
> +	if (success)
> +		ksft_test_result_pass("%s\n", test_name);
> +	else
> +		ksft_test_result_fail("%s\n", test_name);
> +	fclose(fp);
> +}
> +
>  /*
>   * Returns the start address of the mapping on success, else returns
>   * NULL on failure.
> @@ -336,6 +378,7 @@ int main(int argc, char **argv)
>  	int i, run_perf_tests;
>  	unsigned int threshold_mb = VALIDATION_DEFAULT_THRESHOLD;
>  	unsigned int pattern_seed;
> +	int num_expand_tests = 1;
>  	struct test test_cases[MAX_TEST];
>  	struct test perf_test_cases[MAX_PERF_TEST];
>  	int page_size;
> @@ -407,12 +450,14 @@ int main(int argc, char **argv)
>  				(threshold_mb * _1MB >= _1GB);
>  
>  	ksft_set_plan(ARRAY_SIZE(test_cases) + (run_perf_tests ?
> -		      ARRAY_SIZE(perf_test_cases) : 0));
> +		      ARRAY_SIZE(perf_test_cases) : 0) + num_expand_tests);
>  
>  	for (i = 0; i < ARRAY_SIZE(test_cases); i++)
>  		run_mremap_test_case(test_cases[i], &failures, threshold_mb,
>  				     pattern_seed);
>  
> +	mremap_expand_merge(page_size);
> +
>  	if (run_perf_tests) {
>  		ksft_print_msg("\n%s\n",
>  		 "mremap HAVE_MOVE_PMD/PUD optimization time comparison for 1GB region:");



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

end of thread, other threads:[~2022-06-16  8:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-03 14:57 [PATCH v4 0/2] Refactor of vma_merge and new merge call Jakub Matěna
2022-06-03 14:57 ` [PATCH v4 1/2] [PATCH 1/2] mm: refactor of vma_merge() Jakub Matěna
2022-06-16  8:17   ` Vlastimil Babka
2022-06-03 14:57 ` [PATCH v4 2/2] [PATCH 2/2] mm: add merging after mremap resize Jakub Matěna
2022-06-16  8:23   ` Vlastimil Babka

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).