Commit b2a79f62 authored by Thomas Weißschuh's avatar Thomas Weißschuh Committed by Andrew Morton
Browse files

selftests/mm: virtual_address_range: unmap chunks after validation

For each accessed chunk a PTE is created.  More than 1GiB of PTEs is used
in this way.  Remove each PTE after validating a chunk to reduce peak
memory usage.

It is important to only unmap memory that previously mmap()ed, as
unmapping other mappings like the stack, heap or executable mappings will
crash the process.

The mappings read from /proc/self/maps and the return values from mmap()
don't allow a simple correlation due to merging and no guaranteed order. 
To correlate the pointers and mappings use prctl(PR_SET_VMA_ANON_NAME). 
While it introduces a test dependency, other alternatives would introduce
runtime or development overhead.

Link: https://lkml.kernel.org/r/20250114-virtual_address_range-tests-v4-2-6fd7269934a5@linutronix.de


Fixes: 01040964 ("selftests/mm: confirm VA exhaustion without reliance on correctness of mmap()")
Signed-off-by: default avatarThomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
Cc: Anshuman Khandual <khandual@linux.vnet.ibm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: kernel test robot <oliver.sang@intel.com>
Cc: Shuah Khan (Samsung OSG) <shuah@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent a005145b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,3 +7,4 @@ CONFIG_TEST_HMM=m
CONFIG_GUP_TEST=y
CONFIG_TRANSPARENT_HUGEPAGE=y
CONFIG_MEM_SOFT_DIRTY=y
CONFIG_ANON_VMA_NAME=y
+31 −2
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/prctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <fcntl.h>
@@ -82,6 +83,24 @@ static void validate_addr(char *ptr, int high_addr)
		ksft_exit_fail_msg("Bad address %lx\n", addr);
}

static void mark_range(char *ptr, size_t size)
{
	if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "virtual_address_range") == -1) {
		if (errno == EINVAL) {
			/* Depends on CONFIG_ANON_VMA_NAME */
			ksft_test_result_skip("prctl(PR_SET_VMA_ANON_NAME) not supported\n");
			ksft_finished();
		} else {
			ksft_exit_fail_perror("prctl(PR_SET_VMA_ANON_NAME) failed\n");
		}
	}
}

static int is_marked_vma(const char *vma_name)
{
	return vma_name && !strcmp(vma_name, "[anon:virtual_address_range]\n");
}

static int validate_lower_address_hint(void)
{
	char *ptr;
@@ -116,12 +135,17 @@ static int validate_complete_va_space(void)

	prev_end_addr = 0;
	while (fgets(line, sizeof(line), file)) {
		const char *vma_name = NULL;
		int vma_name_start = 0;
		unsigned long hop;

		if (sscanf(line, "%lx-%lx %s[rwxp-]",
			   &start_addr, &end_addr, prot) != 3)
		if (sscanf(line, "%lx-%lx %4s %*s %*s %*s %n",
			   &start_addr, &end_addr, prot, &vma_name_start) != 3)
			ksft_exit_fail_msg("cannot parse /proc/self/maps\n");

		if (vma_name_start)
			vma_name = line + vma_name_start;

		/* end of userspace mappings; ignore vsyscall mapping */
		if (start_addr & (1UL << 63))
			return 0;
@@ -149,6 +173,9 @@ static int validate_complete_va_space(void)
				return 1;
			lseek(fd, 0, SEEK_SET);

			if (is_marked_vma(vma_name))
				munmap((char *)(start_addr + hop), MAP_CHUNK_SIZE);

			hop += MAP_CHUNK_SIZE;
		}
	}
@@ -175,6 +202,7 @@ int main(int argc, char *argv[])
			break;
		}

		mark_range(ptr[i], MAP_CHUNK_SIZE);
		validate_addr(ptr[i], 0);
	}
	lchunks = i;
@@ -192,6 +220,7 @@ int main(int argc, char *argv[])
		if (hptr[i] == MAP_FAILED)
			break;

		mark_range(ptr[i], MAP_CHUNK_SIZE);
		validate_addr(hptr[i], 1);
	}
	hchunks = i;