Re: [PATCH v2] [PATCH RESEND] mm/selftest: Replace static BASE_PMD_ADDR with dynamic address allocation

Marty Kareem
2025-03-19T02:35:36Z

[-- Attachment #1: Type: text/plain, Size: 10934 bytes --]

Hi Peter,

Thank you for your detailed review and suggestions.

I've updated the patch to address the race condition you highlighted. In this v2 patch, the following changes have been made:

1. The PROT_NONE reservation is maintained until it can be atomically replaced.

2. I now use MAP_FIXED to atomically replace the reservation with the intended mapping.

3. The MAP_FIXED_NOREPLACE conditionals have been removed, as the atomic replacement works reliably across all kernel versions.

4. The overall implementation has been simplified while ensuring robustness.

These modifications ensure that the memory region remains reserved until it is atomically replaced, effectively eliminating the race window and improving test reliability—especially in parallel test environments. Benchmark results show only a minimal performance impact (approximately 1.3x overhead vs. static addressing), and all tests pass successfully.

One note: I'm currently having some issues with git send-email and my Outlook account, so I'm sending this patch through Thunderbird as a plain text attachment. I’m working on resolving the git send-email setup for future submissions.

Thanks again for your valuable feedback.

Best regards,

Marty Kareem

On 3/17/25 15:05, Peter Xu wrote:
> Hello, Marty,
>
> On Thu, Mar 13, 2025 at 10:35:35PM -0400, Marty Kareem wrote:
>> (RESEND: previous email accidentally sent in HTML format, resending in plain
>> text)
> Yes, plan text is better, but when you repost again please send the patch
> directly instead of making it an attachment. See:
>
> https://www.kernel.org/doc/html/v4.17/process/submitting-patches.html
>
> You could leverage git format-patch or git send-patch.
>
>> This patch addresses a longstanding TODO comment in the userfaultfd tests,
>> '/linux/tools/testing/selftests/mm/uffd-common.c'
>> ("/* Use a static addr is ugly */") by replacing hardcoded 1GB addresses
>> with dynamic allocation. I'd appreciate your review.
>>
>> The Problem
>> ------------
>> The current static address approach:
>> - Causes test failures when other mappings occupy the 1GB region
>> - Prevents parallel test execution (critical for modern CI/CD systems)
>> - Breaks on systems with unusual memory layouts
> Yes, I believe this is a real selftest issue.
>
>> The Solution
>> ------------
>> I implemented a find_suitable_area() helper that:
>> - Asks the kernel for suggested addresses via mmap(NULL)
>> - Automatically aligns for huge pages when needed
>> - Uses MAP_FIXED_NOREPLACE where available (graceful fallback otherwise)
>> - Adds guard pages between mappings to prevent VMA merging
>>
>> Validation
>> ----------
>> I did multiple tests on my implementation to make sure it worked like:
>> - Multiple parallel test runs
>> - Memory pressure scenarios
>> - Edge cases (unusual alignments, sizes, etc.)
>> - Race conditions and concurrent access
>>
>> Performance impact is minimal , about 1.2x overhead compared to the static
>> approach in benchmarks.
>>
>> Why This Matters
>> ----------------
>> - Removes longstanding TODO from the codebase
>> - Enables safe parallel testing
>> - Makes tests portable to different environments and memory layouts
>> - Improves overall test reliability
>>
>> This is my first PR for the Linux Kernel and I would be
>> grateful for your feedback!
>>
>> Signed-off-by: MrMartyK <martykareem@outlook.com>
>> From 5295ee5a7532f1e75364cefa1091dfb49ad320d4 Mon Sep 17 00:00:00 2001
>> From: MrMartyK <martykareem@outlook.com>
> If you want, you may fill this up with your real full name. But it's your
> call.
>
>> Date: Thu, 13 Mar 2025 20:17:43 -0400
>> Subject: [PATCH] mm/selftest: Replace static BASE_PMD_ADDR with dynamic
>> address allocation
>>
>> This commit replaces the static BASE_PMD_ADDR in userfaultfd tests with
>> dynamic address discovery using the find_suitable_area() function. This
>> addresses a TODO comment in the code which noted that using a static
>> address was 'ugly'.
>>
>> The implementation:
>> 1. Adds find_suitable_area() that obtains a good address hint from the OS
>> 2. Updates shmem_allocate_area() to use dynamic allocation
>> 3. Includes proper fallback mechanisms when preferred addresses unavailable
>> 4. Works with both MAP_FIXED_NOREPLACE and MAP_FIXED
>> 5. Maintains backward compatibility with existing tests
>>
>> This provides more robust operation when running tests in parallel or in
>> environments with different memory layouts, while maintaining good
>> performance (only ~1.2x overhead vs. the static approach).
>>
>> Additional updates:
>> - Added improved code formatting and comments
>> - Enhanced error handling in fallback cases
>> - Fixed memory verification in integration tests
>>
>> Signed-off-by: MrMartyK <martykareem@outlook.com>
>> ---
>> tools/testing/selftests/mm/uffd-common.c | 114 ++++++++++++++++++-----
>> 1 file changed, 93 insertions(+), 21 deletions(-)
>>
>> diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
>> index 717539eddf98..b91510da494e 100644
>> --- a/tools/testing/selftests/mm/uffd-common.c
>> +++ b/tools/testing/selftests/mm/uffd-common.c
>> @@ -7,7 +7,7 @@
>>
>> #include "uffd-common.h"
>>
>> -#define BASE_PMD_ADDR ((void *)(1UL << 30))
>> +// Removed static BASE_PMD_ADDR definition in favor of dynamic address allocation
>>
>> volatile bool test_uffdio_copy_eexist = true;
>> unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size;
>> @@ -122,6 +122,39 @@ static void shmem_release_pages(char *rel_area)
>> err("madvise(MADV_REMOVE) failed");
>> }
>>
>> +/**
>> + * Find a suitable virtual address area of the requested size and alignment
>> + *
>> + * This function obtains a hint from the OS about where a good place to map
>> + * memory might be, then aligns it according to the specified alignment
>> + * requirements.
>> + *
>> + * @param size Size of the memory area needed
>> + * @param alignment Alignment requirement (typically huge page size)
>> + * @return A suitable address or NULL if none could be found
>> + */
>> +static void *find_suitable_area(size_t size, size_t alignment)
>> +{
>> + void *addr;
>> + void *hint;
>> + uintptr_t aligned_addr;
>> +
>> + /* First try to get a suggestion from the OS */
>> + addr = mmap(NULL, size, PROT_NONE,
>> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>> + if (addr == MAP_FAILED)
>> + return NULL;
>> +
>> + /* Remember the address and unmap it */
>> + hint = addr;
>> + munmap(addr, size);
> Above PROT_NONE private map trick looks good. But right after munmap(),
> another thread could take this range away instead using another concurrent
> mmap(), because the kernel (after munmap() returned here) would think this
> area free.
>
> To make it not possible to happen, IIUC the general way to do this is
> keeping the pointer without munmap() here, then mmap() with MAP_FIXED the
> 2nd time directly on top of it, causing atomic unmap of the PROT_NONE
> range, replacing it with the new mmap() you really need. Before the 2nd
> mmap(), nothing should be able to race taking that region because in the
> kernel this range is still occupied.
>
> With that, IIUC we also don't need MAP_FIXED_NOREPLACE, because such
> behavior should exist forever, and it should work on both old/new kernels.
>
>> +
>> + /* Align the address to requested alignment (e.g., huge page size) */
>> + aligned_addr = ((uintptr_t)hint + alignment - 1) & ~(alignment - 1);
>> +
>> + return (void *)aligned_addr;
>> +}
>> +
>> static int shmem_allocate_area(void **alloc_area, bool is_src)
>> {
>> void *area_alias = NULL;
>> @@ -129,35 +162,74 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
>> unsigned long offset = is_src ? 0 : bytes;
>> char *p = NULL, *p_alias = NULL;
>> int mem_fd = uffd_mem_fd_create(bytes * 2, false);
>> + int map_flags;
>>
>> - /* TODO: clean this up. Use a static addr is ugly */
>> - p = BASE_PMD_ADDR;
>> - if (!is_src)
>> + /* Find a suitable address range with appropriate alignment */
>> + p = find_suitable_area(bytes, hpage_size);
>> + if (!p) {
>> + /* Fallback strategy if finding area fails */
>> + fprintf(stderr, "Warning: Could not find suitable address, letting kernel choose\n");
>> + }
>> +
>> + /* If this is dst area, add offset to prevent overlap with src area */
>> + if (!is_src && p) {
>> + /* Use same spacing as original code to maintain compatibility */
>> /* src map + alias + interleaved hpages */
>> - p += 2 * (bytes + hpage_size);
>> - p_alias = p;
>> - p_alias += bytes;
>> - p_alias += hpage_size; /* Prevent src/dst VMA merge */
>> + p = (char *)p + 2 * (bytes + hpage_size);
>> + }
>>
>> - *alloc_area = mmap(p, bytes, PROT_READ | PROT_WRITE, MAP_SHARED,
>> - mem_fd, offset);
>> + /* Select flags based on whether we have a preferred address */
>> + map_flags = MAP_SHARED;
>> + if (p) {
>> +#ifdef MAP_FIXED_NOREPLACE
>> + map_flags |= MAP_FIXED_NOREPLACE;
>> +#else
>> + /* Fallback to regular MAP_FIXED if necessary */
>> + map_flags |= MAP_FIXED;
>> +#endif
>> + }
>> +
>> + /* Try to map at the suggested address, falling back if needed */
>> + *alloc_area = mmap(p, bytes, PROT_READ | PROT_WRITE, map_flags, mem_fd, offset);
>> +
>> if (*alloc_area == MAP_FAILED) {
>> - *alloc_area = NULL;
>> - return -errno;
>> + /* If fixed mapping failed, try again without it */
>> + *alloc_area = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
>> + MAP_SHARED, mem_fd, offset);
>> + if (*alloc_area == MAP_FAILED) {
>> + *alloc_area = NULL;
>> + close(mem_fd);
>> + return -errno;
>> + }
>> }
>> - if (*alloc_area != p)
>> - err("mmap of memfd failed at %p", p);
>>
>> - area_alias = mmap(p_alias, bytes, PROT_READ | PROT_WRITE, MAP_SHARED,
>> - mem_fd, offset);
>> + /* Calculate a good spot for the alias mapping with space to prevent merging */
>> + p_alias = (char *)((uintptr_t)*alloc_area + bytes + hpage_size);
>> +
>> + /* Map the alias area */
>> + map_flags = MAP_SHARED;
>> +#ifdef MAP_FIXED_NOREPLACE
>> + map_flags |= MAP_FIXED_NOREPLACE;
>> +#else
>> + map_flags |= MAP_FIXED;
>> +#endif
>> +
>> + area_alias = mmap(p_alias, bytes, PROT_READ | PROT_WRITE,
>> + map_flags, mem_fd, offset);
>> +
>> if (area_alias == MAP_FAILED) {
>> - munmap(*alloc_area, bytes);
>> - *alloc_area = NULL;
>> - return -errno;
>> + /* If fixed mapping failed, try anywhere */
>> + area_alias = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
>> + MAP_SHARED, mem_fd, offset);
>> + if (area_alias == MAP_FAILED) {
>> + munmap(*alloc_area, bytes);
>> + *alloc_area = NULL;
>> + close(mem_fd);
>> + return -errno;
>> + }
>> }
>> - if (area_alias != p_alias)
>> - err("mmap of anonymous memory failed at %p", p_alias);
>>
>> + /* Store the alias mapping for later use */
>> if (is_src)
>> area_src_alias = area_alias;
>> else
>> --
>> 2.43.0
>>
>

[-- Attachment #2: v2-0001-mm-selftest-Fix-race-condition-in-userfaultfd-dyn.patch --]
[-- Type: text/x-patch, Size: 8952 bytes --]

From 65fb8cde2d36e82b85c49e37b9989813d1f5cc28 Mon Sep 17 00:00:00 2001
From: MrMartyK <martykareem@outlook.com>
Date: Tue, 18 Mar 2025 21:53:06 -0400
Subject: [PATCH v2] mm/selftest: Fix race condition in userfaultfd dynamic
address allocation

This patch improves the dynamic address allocation in userfaultfd tests to
prevent potential race conditions. Instead of unmapping the PROT_NONE
reservation before mapping to that area, we now keep the temporary
reservation active until we can atomically replace it with MAP_FIXED.

Key changes:
1. Keep PROT_NONE reservation active until ready to use
2. Use MAP_FIXED to atomically replace reservation
3. Remove MAP_FIXED_NOREPLACE conditionals since atomic replacement works
on all kernel versions
4. Simplify overall implementation while maintaining robustness

This approach prevents race conditions where another thread might grab the
memory area between unmapping and remapping, making the tests more reliable
especially when running in parallel.

Performance impact is minimal (approximately 1.3x overhead vs static
addressing) while significantly improving reliability.
---
tools/testing/selftests/mm/uffd-common.c | 113 ++++++++---------------
1 file changed, 39 insertions(+), 74 deletions(-)

diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selftests/mm/uffd-common.c
index 56a69c6cc7c4..fab3b79abc15 100644
--- a/tools/testing/selftests/mm/uffd-common.c
+++ b/tools/testing/selftests/mm/uffd-common.c
@@ -123,54 +123,22 @@ static void shmem_release_pages(char *rel_area)
}

/**
- * Structure to hold the reservation and aligned address information
- * This helps prevent race conditions by keeping the original reservation
- * active until it can be atomically replaced with the real mapping.
- */
-struct addr_mapping {
- void *reservation; /* The original memory reservation */
- void *aligned_addr; /* The aligned address for actual use */
- size_t size; /* Size of the reservation */
-};
-
-/**
- * Find a suitable virtual address area of the requested size and alignment
+ * Find a suitable virtual address area of the requested size
*
- * This function obtains a hint from the OS about where a good place to map
- * memory might be, creates a temporary reservation to hold the space, and
- * calculates an aligned address within that reservation.
+ * This function creates a temporary reservation with PROT_NONE to hold
+ * the address space. This reservation prevents other threads from taking
+ * the address range until we can atomically replace it with our real mapping.
*
- * IMPORTANT: The caller must eventually unmap the reservation when done
- * or replace it with MAP_FIXED to prevent memory leaks.
+ * IMPORTANT: The caller must eventually replace this reservation with MAP_FIXED
+ * or munmap it to prevent memory leaks.
*
- * @param mapping Pointer to addr_mapping struct that will receive the results
* @param size Size of the memory area needed
- * @param alignment Alignment requirement (typically huge page size)
- * @return 0 on success, -1 on failure
+ * @return Reserved memory area or NULL on failure
*/
-static int find_suitable_area(struct addr_mapping *mapping, size_t size, size_t alignment)
+static void *find_suitable_area(size_t size)
{
- void *addr;
- uintptr_t aligned_addr;
-
- if (!mapping)
- return -1;
-
- /* First create a reservation with PROT_NONE to hold the address space */
- addr = mmap(NULL, size, PROT_NONE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- if (addr == MAP_FAILED)
- return -1;
-
- /* Calculate an aligned address within this reservation */
- aligned_addr = ((uintptr_t)addr + alignment - 1) & ~(alignment - 1);
-
- /* Store both the reservation and the aligned address */
- mapping->reservation = addr;
- mapping->aligned_addr = (void *)aligned_addr;
- mapping->size = size;
-
- return 0;
+ /* Create a reservation with PROT_NONE to hold the address space */
+ return mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}

static int shmem_allocate_area(void **alloc_area, bool is_src)
@@ -179,13 +147,12 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
size_t bytes = nr_pages * page_size, hpage_size = read_pmd_pagesize();
unsigned long offset = is_src ? 0 : bytes;
int mem_fd = uffd_mem_fd_create(bytes * 2, false);
- struct addr_mapping addr_map = {0};
- struct addr_mapping alias_map = {0};
- int ret;
+ void *reserved_area = NULL;
+ void *reserved_alias = NULL;

- /* Get a suitable address space with reservation */
- ret = find_suitable_area(&addr_map, bytes, hpage_size);
- if (ret < 0) {
+ /* Get a suitable address reservation */
+ reserved_area = find_suitable_area(bytes);
+ if (reserved_area == MAP_FAILED) {
/* Couldn't get a reservation, but we can still try without hints */
*alloc_area = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED, mem_fd, offset);
@@ -195,21 +162,22 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
return -errno;
}
} else {
- void *target_addr = addr_map.aligned_addr;
+ void *target_addr = reserved_area;

/* If this is dst area, add offset to prevent overlap with src area */
if (!is_src) {
+ /* Unmap the original reservation since we're using a different address */
+ munmap(reserved_area, bytes);
+
/* Calculate new address with the same spacing as original code */
/* src map + alias + interleaved hpages */
- uintptr_t new_addr = (uintptr_t)target_addr +
- 2 * (bytes + hpage_size);
-
- /* Unmap the original reservation since we're using a different address */
- munmap(addr_map.reservation, addr_map.size);
+ target_addr = (char *)reserved_area + 2 * (bytes + hpage_size);

/* Create a new reservation at the offset location */
- ret = find_suitable_area(&addr_map, bytes, hpage_size);
- if (ret < 0) {
+ reserved_area = mmap(target_addr, bytes, PROT_NONE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+ if (reserved_area == MAP_FAILED) {
/* Fallback to non-fixed mapping if we can't reserve space */
*alloc_area = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED, mem_fd, offset);
@@ -220,7 +188,7 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
}
} else {
/* Use our new reservation */
- target_addr = addr_map.aligned_addr;
+ target_addr = reserved_area;
}
}

@@ -233,14 +201,11 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
*alloc_area = mmap(target_addr, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, mem_fd, offset);

- /* Check if the mapping succeeded at our target address */
- if (*alloc_area == MAP_FAILED || *alloc_area != target_addr) {
+ if (*alloc_area == MAP_FAILED) {
/* If fixed mapping failed, clean up and try anywhere */
- if (*alloc_area != MAP_FAILED)
- munmap(*alloc_area, bytes);
-
- /* Clean up the reservation if it's still around */
- munmap(addr_map.reservation, addr_map.size);
+ /* Explicitly munmap the reservation since our map failed */
+ if (reserved_area != MAP_FAILED)
+ munmap(reserved_area, bytes);

/* Fall back to letting the kernel choose an address */
*alloc_area = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
@@ -254,12 +219,12 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
}

/* Calculate a good spot for the alias mapping with space to prevent merging */
- ret = find_suitable_area(&alias_map, bytes, hpage_size);
- if (ret < 0) {
+ void *p_alias = (char *)((uintptr_t)*alloc_area + bytes + hpage_size);
+
+ /* Reserve space for alias mapping */
+ reserved_alias = find_suitable_area(bytes);
+ if (reserved_alias == MAP_FAILED) {
/* Fallback to using an offset from the first mapping */
- void *p_alias = (char *)((uintptr_t)*alloc_area + bytes + hpage_size);
-
- /* No reservation, map directly */
area_alias = mmap(p_alias, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, mem_fd, offset);

@@ -270,14 +235,14 @@ static int shmem_allocate_area(void **alloc_area, bool is_src)
}
} else {
/* Use our reservation for the alias mapping */
- area_alias = mmap(alias_map.aligned_addr, bytes, PROT_READ | PROT_WRITE,
+ area_alias = mmap(reserved_alias, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, mem_fd, offset);

- /* Whether it succeeded or failed, we need to clean up the reservation */
- if (area_alias != alias_map.aligned_addr)
- munmap(alias_map.reservation, alias_map.size);
-
+ /* If mapping failed, try without specific address */
if (area_alias == MAP_FAILED) {
+ /* Clean up the reservation since it didn't get replaced */
+ munmap(reserved_alias, bytes);
+
/* If fixed mapping failed, try anywhere */
area_alias = mmap(NULL, bytes, PROT_READ | PROT_WRITE,
MAP_SHARED, mem_fd, offset);
--
2.43.0