From: "Liam R. Howlett" <Liam.Howlett@oracle.com>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Jarkko Sakkinen <jarkko@kernel.org>,
Dave Hansen <dave.hansen@linux.intel.com>,
Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
Arnd Bergmann <arnd@arndb.de>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Dan Williams <dan.j.williams@intel.com>,
Vishal Verma <vishal.l.verma@intel.com>,
Dave Jiang <dave.jiang@intel.com>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Jani Nikula <jani.nikula@linux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@intel.com>,
Tvrtko Ursulin <tursulin@ursulin.net>,
Christian Koenig <christian.koenig@amd.com>,
Huang Rui <ray.huang@amd.com>,
Matthew Auld <matthew.auld@intel.com>,
Matthew Brost <matthew.brost@intel.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Benjamin LaHaise <bcrl@kvack.org>, Gao Xiang <xiang@kernel.org>,
Chao Yu <chao@kernel.org>, Yue Hu <zbestahu@gmail.com>,
Jeffle Xu <jefflexu@linux.alibaba.com>,
Sandeep Dhavale <dhavale@google.com>,
Hongbo Li <lihongbo22@huawei.com>,
Chunhai Guo <guochunhai@vivo.com>, Theodore Ts'o <tytso@mit.edu>,
Andreas Dilger <adilger.kernel@dilger.ca>,
Muchun Song <muchun.song@linux.dev>,
Oscar Salvador <osalvador@suse.de>,
David Hildenbrand <david@kernel.org>,
Konstantin Komarov <almaz.alexandrovich@paragon-software.com>,
Mike Marshall <hubcap@omnibond.com>,
Martin Brandenburg <martin@omnibond.com>,
Tony Luck <tony.luck@intel.com>,
Reinette Chatre <reinette.chatre@intel.com>,
Dave Martin <Dave.Martin@arm.com>,
James Morse <james.morse@arm.com>,
Babu Moger <babu.moger@amd.com>, Carlos Maiolino <cem@kernel.org>,
Damien Le Moal <dlemoal@kernel.org>,
Naohiro Aota <naohiro.aota@wdc.com>,
Johannes Thumshirn <jth@kernel.org>,
Matthew Wilcox <willy@infradead.org>,
Vlastimil Babka <vbabka@suse.cz>, Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Hugh Dickins <hughd@google.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Zi Yan <ziy@nvidia.com>, Nico Pache <npache@redhat.com>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Jann Horn <jannh@google.com>, Pedro Falcato <pfalcato@suse.de>,
David Howells <dhowells@redhat.com>,
Paul Moore <paul@paul-moore.com>,
James Morris <jmorris@namei.org>,
"Serge E . Hallyn" <serge@hallyn.com>,
Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org,
nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org,
dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org,
linux-fsdevel@vger.kernel.org, linux-aio@kvack.org,
linux-erofs@lists.ozlabs.org, linux-ext4@vger.kernel.org,
linux-mm@kvack.org, ntfs3@lists.linux.dev,
devel@lists.orangefs.org, linux-xfs@vger.kernel.org,
keyrings@vger.kernel.org, linux-security-module@vger.kernel.org,
Jason Gunthorpe <jgg@nvidia.com>
Subject: Re: [PATCH v2 11/13] tools/testing/vma: separate VMA userland tests into separate files
Date: Mon, 9 Feb 2026 19:58:22 +0000 [thread overview]
Message-ID: <opopo4rlyxz7upi2bzrx7e6cyji2nmaynvlhvr2nzvtvb5pfxu@4fekgb5faybz> (raw)
In-Reply-To: <a0455ccfe4fdcd1c962c64f76304f612e5662a4e.1769097829.git.lorenzo.stoakes@oracle.com>
* Lorenzo Stoakes <lorenzo.stoakes@oracle.com> [260122 16:06]:
> So far the userland VMA tests have been established as a rough expression
> of what's been possible.
>
> qAdapt it into a more usable form by separating out tests and shared helper
^^^^ Typo
> functions.
>
> Since we test functions that are declared statically in mm/vma.c, we make
> use of the trick of #include'ing kernel C files directly.
>
> In order for the tests to continue to function, we must therefore also
> this way into the tests/ directory.
>
> We try to keep as much shared logic actually modularised into a separate
> compilation unit in shared.c, however the merge_existing() and attach_vma()
> helpers rely on statically declared mm/vma.c functions so these must be
> declared in main.c.
>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Besides that typo, it looks good.
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> ---
> tools/testing/vma/Makefile | 4 +-
> tools/testing/vma/main.c | 55 ++++
> tools/testing/vma/shared.c | 131 ++++++++
> tools/testing/vma/shared.h | 114 +++++++
> tools/testing/vma/{vma.c => tests/merge.c} | 332 +--------------------
> tools/testing/vma/tests/mmap.c | 57 ++++
> tools/testing/vma/tests/vma.c | 39 +++
> tools/testing/vma/vma_internal.h | 9 -
> 8 files changed, 406 insertions(+), 335 deletions(-)
> create mode 100644 tools/testing/vma/main.c
> create mode 100644 tools/testing/vma/shared.c
> create mode 100644 tools/testing/vma/shared.h
> rename tools/testing/vma/{vma.c => tests/merge.c} (82%)
> create mode 100644 tools/testing/vma/tests/mmap.c
> create mode 100644 tools/testing/vma/tests/vma.c
>
> diff --git a/tools/testing/vma/Makefile b/tools/testing/vma/Makefile
> index 66f3831a668f..94133d9d3955 100644
> --- a/tools/testing/vma/Makefile
> +++ b/tools/testing/vma/Makefile
> @@ -6,10 +6,10 @@ default: vma
>
> include ../shared/shared.mk
>
> -OFILES = $(SHARED_OFILES) vma.o maple-shim.o
> +OFILES = $(SHARED_OFILES) main.o shared.o maple-shim.o
> TARGETS = vma
>
> -vma.o: vma.c vma_internal.h ../../../mm/vma.c ../../../mm/vma_init.c ../../../mm/vma_exec.c ../../../mm/vma.h
> +main.o: main.c shared.c shared.h vma_internal.h tests/merge.c tests/mmap.c tests/vma.c ../../../mm/vma.c ../../../mm/vma_init.c ../../../mm/vma_exec.c ../../../mm/vma.h
>
> vma: $(OFILES)
> $(CC) $(CFLAGS) -o $@ $(OFILES) $(LDLIBS)
> diff --git a/tools/testing/vma/main.c b/tools/testing/vma/main.c
> new file mode 100644
> index 000000000000..49b09e97a51f
> --- /dev/null
> +++ b/tools/testing/vma/main.c
> @@ -0,0 +1,55 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#include "shared.h"
> +/*
> + * Directly import the VMA implementation here. Our vma_internal.h wrapper
> + * provides userland-equivalent functionality for everything vma.c uses.
> + */
> +#include "../../../mm/vma_init.c"
> +#include "../../../mm/vma_exec.c"
> +#include "../../../mm/vma.c"
> +
> +/* Tests are included directly so they can test static functions in mm/vma.c. */
> +#include "tests/merge.c"
> +#include "tests/mmap.c"
> +#include "tests/vma.c"
> +
> +/* Helper functions which utilise static kernel functions. */
> +
> +struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg)
> +{
> + struct vm_area_struct *vma;
> +
> + vma = vma_merge_existing_range(vmg);
> + if (vma)
> + vma_assert_attached(vma);
> + return vma;
> +}
> +
> +int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma)
> +{
> + int res;
> +
> + res = vma_link(mm, vma);
> + if (!res)
> + vma_assert_attached(vma);
> + return res;
> +}
> +
> +/* Main test running which invokes tests/ *.c runners. */
> +int main(void)
> +{
> + int num_tests = 0, num_fail = 0;
> +
> + maple_tree_init();
> + vma_state_init();
> +
> + run_merge_tests(&num_tests, &num_fail);
> + run_mmap_tests(&num_tests, &num_fail);
> + run_vma_tests(&num_tests, &num_fail);
> +
> + printf("%d tests run, %d passed, %d failed.\n",
> + num_tests, num_tests - num_fail, num_fail);
> +
> + return num_fail == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
> +}
> diff --git a/tools/testing/vma/shared.c b/tools/testing/vma/shared.c
> new file mode 100644
> index 000000000000..bda578cc3304
> --- /dev/null
> +++ b/tools/testing/vma/shared.c
> @@ -0,0 +1,131 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#include "shared.h"
> +
> +
> +bool fail_prealloc;
> +unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
> +unsigned long dac_mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
> +unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
> +
> +const struct vm_operations_struct vma_dummy_vm_ops;
> +struct anon_vma dummy_anon_vma;
> +struct task_struct __current;
> +
> +struct vm_area_struct *alloc_vma(struct mm_struct *mm,
> + unsigned long start, unsigned long end,
> + pgoff_t pgoff, vm_flags_t vm_flags)
> +{
> + struct vm_area_struct *vma = vm_area_alloc(mm);
> +
> + if (vma == NULL)
> + return NULL;
> +
> + vma->vm_start = start;
> + vma->vm_end = end;
> + vma->vm_pgoff = pgoff;
> + vm_flags_reset(vma, vm_flags);
> + vma_assert_detached(vma);
> +
> + return vma;
> +}
> +
> +void detach_free_vma(struct vm_area_struct *vma)
> +{
> + vma_mark_detached(vma);
> + vm_area_free(vma);
> +}
> +
> +struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
> + unsigned long start, unsigned long end,
> + pgoff_t pgoff, vm_flags_t vm_flags)
> +{
> + struct vm_area_struct *vma = alloc_vma(mm, start, end, pgoff, vm_flags);
> +
> + if (vma == NULL)
> + return NULL;
> +
> + if (attach_vma(mm, vma)) {
> + detach_free_vma(vma);
> + return NULL;
> + }
> +
> + /*
> + * Reset this counter which we use to track whether writes have
> + * begun. Linking to the tree will have caused this to be incremented,
> + * which means we will get a false positive otherwise.
> + */
> + vma->vm_lock_seq = UINT_MAX;
> +
> + return vma;
> +}
> +
> +void reset_dummy_anon_vma(void)
> +{
> + dummy_anon_vma.was_cloned = false;
> + dummy_anon_vma.was_unlinked = false;
> +}
> +
> +int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi)
> +{
> + struct vm_area_struct *vma;
> + int count = 0;
> +
> + fail_prealloc = false;
> + reset_dummy_anon_vma();
> +
> + vma_iter_set(vmi, 0);
> + for_each_vma(*vmi, vma) {
> + detach_free_vma(vma);
> + count++;
> + }
> +
> + mtree_destroy(&mm->mm_mt);
> + mm->map_count = 0;
> + return count;
> +}
> +
> +bool vma_write_started(struct vm_area_struct *vma)
> +{
> + int seq = vma->vm_lock_seq;
> +
> + /* We reset after each check. */
> + vma->vm_lock_seq = UINT_MAX;
> +
> + /* The vma_start_write() stub simply increments this value. */
> + return seq > -1;
> +}
> +
> +void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
> + struct anon_vma_chain *avc, struct anon_vma *anon_vma)
> +{
> + vma->anon_vma = anon_vma;
> + INIT_LIST_HEAD(&vma->anon_vma_chain);
> + list_add(&avc->same_vma, &vma->anon_vma_chain);
> + avc->anon_vma = vma->anon_vma;
> +}
> +
> +void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
> + struct anon_vma_chain *avc)
> +{
> + __vma_set_dummy_anon_vma(vma, avc, &dummy_anon_vma);
> +}
> +
> +struct task_struct *get_current(void)
> +{
> + return &__current;
> +}
> +
> +unsigned long rlimit(unsigned int limit)
> +{
> + return (unsigned long)-1;
> +}
> +
> +void vma_set_range(struct vm_area_struct *vma,
> + unsigned long start, unsigned long end,
> + pgoff_t pgoff)
> +{
> + vma->vm_start = start;
> + vma->vm_end = end;
> + vma->vm_pgoff = pgoff;
> +}
> diff --git a/tools/testing/vma/shared.h b/tools/testing/vma/shared.h
> new file mode 100644
> index 000000000000..6c64211cfa22
> --- /dev/null
> +++ b/tools/testing/vma/shared.h
> @@ -0,0 +1,114 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#pragma once
> +
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +
> +#include "generated/bit-length.h"
> +#include "maple-shared.h"
> +#include "vma_internal.h"
> +#include "../../../mm/vma.h"
> +
> +/* Simple test runner. Assumes local num_[fail, tests] counters. */
> +#define TEST(name) \
> + do { \
> + (*num_tests)++; \
> + if (!test_##name()) { \
> + (*num_fail)++; \
> + fprintf(stderr, "Test " #name " FAILED\n"); \
> + } \
> + } while (0)
> +
> +#define ASSERT_TRUE(_expr) \
> + do { \
> + if (!(_expr)) { \
> + fprintf(stderr, \
> + "Assert FAILED at %s:%d:%s(): %s is FALSE.\n", \
> + __FILE__, __LINE__, __FUNCTION__, #_expr); \
> + return false; \
> + } \
> + } while (0)
> +
> +#define ASSERT_FALSE(_expr) ASSERT_TRUE(!(_expr))
> +#define ASSERT_EQ(_val1, _val2) ASSERT_TRUE((_val1) == (_val2))
> +#define ASSERT_NE(_val1, _val2) ASSERT_TRUE((_val1) != (_val2))
> +
> +#define IS_SET(_val, _flags) ((_val & _flags) == _flags)
> +
> +extern bool fail_prealloc;
> +
> +/* Override vma_iter_prealloc() so we can choose to fail it. */
> +#define vma_iter_prealloc(vmi, vma) \
> + (fail_prealloc ? -ENOMEM : mas_preallocate(&(vmi)->mas, (vma), GFP_KERNEL))
> +
> +#define CONFIG_DEFAULT_MMAP_MIN_ADDR 65536
> +
> +extern unsigned long mmap_min_addr;
> +extern unsigned long dac_mmap_min_addr;
> +extern unsigned long stack_guard_gap;
> +
> +extern const struct vm_operations_struct vma_dummy_vm_ops;
> +extern struct anon_vma dummy_anon_vma;
> +extern struct task_struct __current;
> +
> +/*
> + * Helper function which provides a wrapper around a merge existing VMA
> + * operation.
> + *
> + * Declared in main.c as uses static VMA function.
> + */
> +struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg);
> +
> +/*
> + * Helper function to allocate a VMA and link it to the tree.
> + *
> + * Declared in main.c as uses static VMA function.
> + */
> +int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma);
> +
> +/* Helper function providing a dummy vm_ops->close() method.*/
> +static inline void dummy_close(struct vm_area_struct *)
> +{
> +}
> +
> +/* Helper function to simply allocate a VMA. */
> +struct vm_area_struct *alloc_vma(struct mm_struct *mm,
> + unsigned long start, unsigned long end,
> + pgoff_t pgoff, vm_flags_t vm_flags);
> +
> +/* Helper function to detach and free a VMA. */
> +void detach_free_vma(struct vm_area_struct *vma);
> +
> +/* Helper function to allocate a VMA and link it to the tree. */
> +struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
> + unsigned long start, unsigned long end,
> + pgoff_t pgoff, vm_flags_t vm_flags);
> +
> +/*
> + * Helper function to reset the dummy anon_vma to indicate it has not been
> + * duplicated.
> + */
> +void reset_dummy_anon_vma(void);
> +
> +/*
> + * Helper function to remove all VMAs and destroy the maple tree associated with
> + * a virtual address space. Returns a count of VMAs in the tree.
> + */
> +int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi);
> +
> +/* Helper function to determine if VMA has had vma_start_write() performed. */
> +bool vma_write_started(struct vm_area_struct *vma);
> +
> +void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
> + struct anon_vma_chain *avc, struct anon_vma *anon_vma);
> +
> +/* Provide a simple dummy VMA/anon_vma dummy setup for testing. */
> +void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
> + struct anon_vma_chain *avc);
> +
> +/* Helper function to specify a VMA's range. */
> +void vma_set_range(struct vm_area_struct *vma,
> + unsigned long start, unsigned long end,
> + pgoff_t pgoff);
> diff --git a/tools/testing/vma/vma.c b/tools/testing/vma/tests/merge.c
> similarity index 82%
> rename from tools/testing/vma/vma.c
> rename to tools/testing/vma/tests/merge.c
> index 93d21bc7e112..3708dc6945b0 100644
> --- a/tools/testing/vma/vma.c
> +++ b/tools/testing/vma/tests/merge.c
> @@ -1,132 +1,5 @@
> // SPDX-License-Identifier: GPL-2.0-or-later
>
> -#include <stdbool.h>
> -#include <stdio.h>
> -#include <stdlib.h>
> -
> -#include "generated/bit-length.h"
> -
> -#include "maple-shared.h"
> -#include "vma_internal.h"
> -
> -/* Include so header guard set. */
> -#include "../../../mm/vma.h"
> -
> -static bool fail_prealloc;
> -
> -/* Then override vma_iter_prealloc() so we can choose to fail it. */
> -#define vma_iter_prealloc(vmi, vma) \
> - (fail_prealloc ? -ENOMEM : mas_preallocate(&(vmi)->mas, (vma), GFP_KERNEL))
> -
> -#define CONFIG_DEFAULT_MMAP_MIN_ADDR 65536
> -
> -unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
> -unsigned long dac_mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
> -unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
> -
> -/*
> - * Directly import the VMA implementation here. Our vma_internal.h wrapper
> - * provides userland-equivalent functionality for everything vma.c uses.
> - */
> -#include "../../../mm/vma_init.c"
> -#include "../../../mm/vma_exec.c"
> -#include "../../../mm/vma.c"
> -
> -const struct vm_operations_struct vma_dummy_vm_ops;
> -static struct anon_vma dummy_anon_vma;
> -
> -#define ASSERT_TRUE(_expr) \
> - do { \
> - if (!(_expr)) { \
> - fprintf(stderr, \
> - "Assert FAILED at %s:%d:%s(): %s is FALSE.\n", \
> - __FILE__, __LINE__, __FUNCTION__, #_expr); \
> - return false; \
> - } \
> - } while (0)
> -#define ASSERT_FALSE(_expr) ASSERT_TRUE(!(_expr))
> -#define ASSERT_EQ(_val1, _val2) ASSERT_TRUE((_val1) == (_val2))
> -#define ASSERT_NE(_val1, _val2) ASSERT_TRUE((_val1) != (_val2))
> -
> -#define IS_SET(_val, _flags) ((_val & _flags) == _flags)
> -
> -static struct task_struct __current;
> -
> -struct task_struct *get_current(void)
> -{
> - return &__current;
> -}
> -
> -unsigned long rlimit(unsigned int limit)
> -{
> - return (unsigned long)-1;
> -}
> -
> -/* Helper function to simply allocate a VMA. */
> -static struct vm_area_struct *alloc_vma(struct mm_struct *mm,
> - unsigned long start,
> - unsigned long end,
> - pgoff_t pgoff,
> - vm_flags_t vm_flags)
> -{
> - struct vm_area_struct *vma = vm_area_alloc(mm);
> -
> - if (vma == NULL)
> - return NULL;
> -
> - vma->vm_start = start;
> - vma->vm_end = end;
> - vma->vm_pgoff = pgoff;
> - vm_flags_reset(vma, vm_flags);
> - vma_assert_detached(vma);
> -
> - return vma;
> -}
> -
> -/* Helper function to allocate a VMA and link it to the tree. */
> -static int attach_vma(struct mm_struct *mm, struct vm_area_struct *vma)
> -{
> - int res;
> -
> - res = vma_link(mm, vma);
> - if (!res)
> - vma_assert_attached(vma);
> - return res;
> -}
> -
> -static void detach_free_vma(struct vm_area_struct *vma)
> -{
> - vma_mark_detached(vma);
> - vm_area_free(vma);
> -}
> -
> -/* Helper function to allocate a VMA and link it to the tree. */
> -static struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
> - unsigned long start,
> - unsigned long end,
> - pgoff_t pgoff,
> - vm_flags_t vm_flags)
> -{
> - struct vm_area_struct *vma = alloc_vma(mm, start, end, pgoff, vm_flags);
> -
> - if (vma == NULL)
> - return NULL;
> -
> - if (attach_vma(mm, vma)) {
> - detach_free_vma(vma);
> - return NULL;
> - }
> -
> - /*
> - * Reset this counter which we use to track whether writes have
> - * begun. Linking to the tree will have caused this to be incremented,
> - * which means we will get a false positive otherwise.
> - */
> - vma->vm_lock_seq = UINT_MAX;
> -
> - return vma;
> -}
> -
> /* Helper function which provides a wrapper around a merge new VMA operation. */
> static struct vm_area_struct *merge_new(struct vma_merge_struct *vmg)
> {
> @@ -146,20 +19,6 @@ static struct vm_area_struct *merge_new(struct vma_merge_struct *vmg)
> return vma;
> }
>
> -/*
> - * Helper function which provides a wrapper around a merge existing VMA
> - * operation.
> - */
> -static struct vm_area_struct *merge_existing(struct vma_merge_struct *vmg)
> -{
> - struct vm_area_struct *vma;
> -
> - vma = vma_merge_existing_range(vmg);
> - if (vma)
> - vma_assert_attached(vma);
> - return vma;
> -}
> -
> /*
> * Helper function which provides a wrapper around the expansion of an existing
> * VMA.
> @@ -173,8 +32,8 @@ static int expand_existing(struct vma_merge_struct *vmg)
> * Helper function to reset merge state the associated VMA iterator to a
> * specified new range.
> */
> -static void vmg_set_range(struct vma_merge_struct *vmg, unsigned long start,
> - unsigned long end, pgoff_t pgoff, vm_flags_t vm_flags)
> +void vmg_set_range(struct vma_merge_struct *vmg, unsigned long start,
> + unsigned long end, pgoff_t pgoff, vm_flags_t vm_flags)
> {
> vma_iter_set(vmg->vmi, start);
>
> @@ -197,8 +56,8 @@ static void vmg_set_range(struct vma_merge_struct *vmg, unsigned long start,
>
> /* Helper function to set both the VMG range and its anon_vma. */
> static void vmg_set_range_anon_vma(struct vma_merge_struct *vmg, unsigned long start,
> - unsigned long end, pgoff_t pgoff, vm_flags_t vm_flags,
> - struct anon_vma *anon_vma)
> + unsigned long end, pgoff_t pgoff, vm_flags_t vm_flags,
> + struct anon_vma *anon_vma)
> {
> vmg_set_range(vmg, start, end, pgoff, vm_flags);
> vmg->anon_vma = anon_vma;
> @@ -211,10 +70,9 @@ static void vmg_set_range_anon_vma(struct vma_merge_struct *vmg, unsigned long s
> * VMA, link it to the maple tree and return it.
> */
> static struct vm_area_struct *try_merge_new_vma(struct mm_struct *mm,
> - struct vma_merge_struct *vmg,
> - unsigned long start, unsigned long end,
> - pgoff_t pgoff, vm_flags_t vm_flags,
> - bool *was_merged)
> + struct vma_merge_struct *vmg, unsigned long start,
> + unsigned long end, pgoff_t pgoff, vm_flags_t vm_flags,
> + bool *was_merged)
> {
> struct vm_area_struct *merged;
>
> @@ -234,72 +92,6 @@ static struct vm_area_struct *try_merge_new_vma(struct mm_struct *mm,
> return alloc_and_link_vma(mm, start, end, pgoff, vm_flags);
> }
>
> -/*
> - * Helper function to reset the dummy anon_vma to indicate it has not been
> - * duplicated.
> - */
> -static void reset_dummy_anon_vma(void)
> -{
> - dummy_anon_vma.was_cloned = false;
> - dummy_anon_vma.was_unlinked = false;
> -}
> -
> -/*
> - * Helper function to remove all VMAs and destroy the maple tree associated with
> - * a virtual address space. Returns a count of VMAs in the tree.
> - */
> -static int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi)
> -{
> - struct vm_area_struct *vma;
> - int count = 0;
> -
> - fail_prealloc = false;
> - reset_dummy_anon_vma();
> -
> - vma_iter_set(vmi, 0);
> - for_each_vma(*vmi, vma) {
> - detach_free_vma(vma);
> - count++;
> - }
> -
> - mtree_destroy(&mm->mm_mt);
> - mm->map_count = 0;
> - return count;
> -}
> -
> -/* Helper function to determine if VMA has had vma_start_write() performed. */
> -static bool vma_write_started(struct vm_area_struct *vma)
> -{
> - int seq = vma->vm_lock_seq;
> -
> - /* We reset after each check. */
> - vma->vm_lock_seq = UINT_MAX;
> -
> - /* The vma_start_write() stub simply increments this value. */
> - return seq > -1;
> -}
> -
> -/* Helper function providing a dummy vm_ops->close() method.*/
> -static void dummy_close(struct vm_area_struct *)
> -{
> -}
> -
> -static void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
> - struct anon_vma_chain *avc,
> - struct anon_vma *anon_vma)
> -{
> - vma->anon_vma = anon_vma;
> - INIT_LIST_HEAD(&vma->anon_vma_chain);
> - list_add(&avc->same_vma, &vma->anon_vma_chain);
> - avc->anon_vma = vma->anon_vma;
> -}
> -
> -static void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
> - struct anon_vma_chain *avc)
> -{
> - __vma_set_dummy_anon_vma(vma, avc, &dummy_anon_vma);
> -}
> -
> static bool test_simple_merge(void)
> {
> struct vm_area_struct *vma;
> @@ -1616,39 +1408,6 @@ static bool test_merge_extend(void)
> return true;
> }
>
> -static bool test_copy_vma(void)
> -{
> - vm_flags_t vm_flags = VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE;
> - struct mm_struct mm = {};
> - bool need_locks = false;
> - VMA_ITERATOR(vmi, &mm, 0);
> - struct vm_area_struct *vma, *vma_new, *vma_next;
> -
> - /* Move backwards and do not merge. */
> -
> - vma = alloc_and_link_vma(&mm, 0x3000, 0x5000, 3, vm_flags);
> - vma_new = copy_vma(&vma, 0, 0x2000, 0, &need_locks);
> - ASSERT_NE(vma_new, vma);
> - ASSERT_EQ(vma_new->vm_start, 0);
> - ASSERT_EQ(vma_new->vm_end, 0x2000);
> - ASSERT_EQ(vma_new->vm_pgoff, 0);
> - vma_assert_attached(vma_new);
> -
> - cleanup_mm(&mm, &vmi);
> -
> - /* Move a VMA into position next to another and merge the two. */
> -
> - vma = alloc_and_link_vma(&mm, 0, 0x2000, 0, vm_flags);
> - vma_next = alloc_and_link_vma(&mm, 0x6000, 0x8000, 6, vm_flags);
> - vma_new = copy_vma(&vma, 0x4000, 0x2000, 4, &need_locks);
> - vma_assert_attached(vma_new);
> -
> - ASSERT_EQ(vma_new, vma_next);
> -
> - cleanup_mm(&mm, &vmi);
> - return true;
> -}
> -
> static bool test_expand_only_mode(void)
> {
> vm_flags_t vm_flags = VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE;
> @@ -1689,73 +1448,8 @@ static bool test_expand_only_mode(void)
> return true;
> }
>
> -static bool test_mmap_region_basic(void)
> -{
> - struct mm_struct mm = {};
> - unsigned long addr;
> - struct vm_area_struct *vma;
> - VMA_ITERATOR(vmi, &mm, 0);
> -
> - current->mm = &mm;
> -
> - /* Map at 0x300000, length 0x3000. */
> - addr = __mmap_region(NULL, 0x300000, 0x3000,
> - VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> - 0x300, NULL);
> - ASSERT_EQ(addr, 0x300000);
> -
> - /* Map at 0x250000, length 0x3000. */
> - addr = __mmap_region(NULL, 0x250000, 0x3000,
> - VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> - 0x250, NULL);
> - ASSERT_EQ(addr, 0x250000);
> -
> - /* Map at 0x303000, merging to 0x300000 of length 0x6000. */
> - addr = __mmap_region(NULL, 0x303000, 0x3000,
> - VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> - 0x303, NULL);
> - ASSERT_EQ(addr, 0x303000);
> -
> - /* Map at 0x24d000, merging to 0x250000 of length 0x6000. */
> - addr = __mmap_region(NULL, 0x24d000, 0x3000,
> - VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> - 0x24d, NULL);
> - ASSERT_EQ(addr, 0x24d000);
> -
> - ASSERT_EQ(mm.map_count, 2);
> -
> - for_each_vma(vmi, vma) {
> - if (vma->vm_start == 0x300000) {
> - ASSERT_EQ(vma->vm_end, 0x306000);
> - ASSERT_EQ(vma->vm_pgoff, 0x300);
> - } else if (vma->vm_start == 0x24d000) {
> - ASSERT_EQ(vma->vm_end, 0x253000);
> - ASSERT_EQ(vma->vm_pgoff, 0x24d);
> - } else {
> - ASSERT_FALSE(true);
> - }
> - }
> -
> - cleanup_mm(&mm, &vmi);
> - return true;
> -}
> -
> -int main(void)
> +static void run_merge_tests(int *num_tests, int *num_fail)
> {
> - int num_tests = 0, num_fail = 0;
> -
> - maple_tree_init();
> - vma_state_init();
> -
> -#define TEST(name) \
> - do { \
> - num_tests++; \
> - if (!test_##name()) { \
> - num_fail++; \
> - fprintf(stderr, "Test " #name " FAILED\n"); \
> - } \
> - } while (0)
> -
> /* Very simple tests to kick the tyres. */
> TEST(simple_merge);
> TEST(simple_modify);
> @@ -1771,15 +1465,5 @@ int main(void)
> TEST(dup_anon_vma);
> TEST(vmi_prealloc_fail);
> TEST(merge_extend);
> - TEST(copy_vma);
> TEST(expand_only_mode);
> -
> - TEST(mmap_region_basic);
> -
> -#undef TEST
> -
> - printf("%d tests run, %d passed, %d failed.\n",
> - num_tests, num_tests - num_fail, num_fail);
> -
> - return num_fail == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
> }
> diff --git a/tools/testing/vma/tests/mmap.c b/tools/testing/vma/tests/mmap.c
> new file mode 100644
> index 000000000000..bded4ecbe5db
> --- /dev/null
> +++ b/tools/testing/vma/tests/mmap.c
> @@ -0,0 +1,57 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +static bool test_mmap_region_basic(void)
> +{
> + struct mm_struct mm = {};
> + unsigned long addr;
> + struct vm_area_struct *vma;
> + VMA_ITERATOR(vmi, &mm, 0);
> +
> + current->mm = &mm;
> +
> + /* Map at 0x300000, length 0x3000. */
> + addr = __mmap_region(NULL, 0x300000, 0x3000,
> + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> + 0x300, NULL);
> + ASSERT_EQ(addr, 0x300000);
> +
> + /* Map at 0x250000, length 0x3000. */
> + addr = __mmap_region(NULL, 0x250000, 0x3000,
> + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> + 0x250, NULL);
> + ASSERT_EQ(addr, 0x250000);
> +
> + /* Map at 0x303000, merging to 0x300000 of length 0x6000. */
> + addr = __mmap_region(NULL, 0x303000, 0x3000,
> + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> + 0x303, NULL);
> + ASSERT_EQ(addr, 0x303000);
> +
> + /* Map at 0x24d000, merging to 0x250000 of length 0x6000. */
> + addr = __mmap_region(NULL, 0x24d000, 0x3000,
> + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
> + 0x24d, NULL);
> + ASSERT_EQ(addr, 0x24d000);
> +
> + ASSERT_EQ(mm.map_count, 2);
> +
> + for_each_vma(vmi, vma) {
> + if (vma->vm_start == 0x300000) {
> + ASSERT_EQ(vma->vm_end, 0x306000);
> + ASSERT_EQ(vma->vm_pgoff, 0x300);
> + } else if (vma->vm_start == 0x24d000) {
> + ASSERT_EQ(vma->vm_end, 0x253000);
> + ASSERT_EQ(vma->vm_pgoff, 0x24d);
> + } else {
> + ASSERT_FALSE(true);
> + }
> + }
> +
> + cleanup_mm(&mm, &vmi);
> + return true;
> +}
> +
> +static void run_mmap_tests(int *num_tests, int *num_fail)
> +{
> + TEST(mmap_region_basic);
> +}
> diff --git a/tools/testing/vma/tests/vma.c b/tools/testing/vma/tests/vma.c
> new file mode 100644
> index 000000000000..6d9775aee243
> --- /dev/null
> +++ b/tools/testing/vma/tests/vma.c
> @@ -0,0 +1,39 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +static bool test_copy_vma(void)
> +{
> + vm_flags_t vm_flags = VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE;
> + struct mm_struct mm = {};
> + bool need_locks = false;
> + VMA_ITERATOR(vmi, &mm, 0);
> + struct vm_area_struct *vma, *vma_new, *vma_next;
> +
> + /* Move backwards and do not merge. */
> +
> + vma = alloc_and_link_vma(&mm, 0x3000, 0x5000, 3, vm_flags);
> + vma_new = copy_vma(&vma, 0, 0x2000, 0, &need_locks);
> + ASSERT_NE(vma_new, vma);
> + ASSERT_EQ(vma_new->vm_start, 0);
> + ASSERT_EQ(vma_new->vm_end, 0x2000);
> + ASSERT_EQ(vma_new->vm_pgoff, 0);
> + vma_assert_attached(vma_new);
> +
> + cleanup_mm(&mm, &vmi);
> +
> + /* Move a VMA into position next to another and merge the two. */
> +
> + vma = alloc_and_link_vma(&mm, 0, 0x2000, 0, vm_flags);
> + vma_next = alloc_and_link_vma(&mm, 0x6000, 0x8000, 6, vm_flags);
> + vma_new = copy_vma(&vma, 0x4000, 0x2000, 4, &need_locks);
> + vma_assert_attached(vma_new);
> +
> + ASSERT_EQ(vma_new, vma_next);
> +
> + cleanup_mm(&mm, &vmi);
> + return true;
> +}
> +
> +static void run_vma_tests(int *num_tests, int *num_fail)
> +{
> + TEST(copy_vma);
> +}
> diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h
> index 2743f12ecf32..b48ebae3927d 100644
> --- a/tools/testing/vma/vma_internal.h
> +++ b/tools/testing/vma/vma_internal.h
> @@ -1127,15 +1127,6 @@ static inline void mapping_allow_writable(struct address_space *mapping)
> atomic_inc(&mapping->i_mmap_writable);
> }
>
> -static inline void vma_set_range(struct vm_area_struct *vma,
> - unsigned long start, unsigned long end,
> - pgoff_t pgoff)
> -{
> - vma->vm_start = start;
> - vma->vm_end = end;
> - vma->vm_pgoff = pgoff;
> -}
> -
> static inline
> struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
> {
> --
> 2.52.0
>
next prev parent reply other threads:[~2026-02-09 19:59 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-22 16:06 [PATCH v2 00/13] mm: add bitmap VMA flag helpers and convert all mmap_prepare to use them Lorenzo Stoakes
2026-01-22 16:06 ` [PATCH v2 01/13] mm/vma: remove __private sparse decoration from vma_flags_t Lorenzo Stoakes
2026-02-06 17:02 ` Pedro Falcato
2026-02-09 18:38 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 02/13] mm: rename vma_flag_test/set_atomic() to vma_test/set_atomic_flag() Lorenzo Stoakes
2026-02-06 17:04 ` Pedro Falcato
2026-02-09 18:40 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 03/13] mm: add mk_vma_flags() bitmap flag macro helper Lorenzo Stoakes
2026-02-06 17:14 ` Pedro Falcato
2026-02-09 14:02 ` Lorenzo Stoakes
2026-02-09 18:44 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 04/13] tools: bitmap: add missing bitmap_[subset(), andnot()] Lorenzo Stoakes
2026-02-09 18:45 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 05/13] mm: add basic VMA flag operation helper functions Lorenzo Stoakes
2026-02-06 17:35 ` Pedro Falcato
2026-02-09 14:04 ` Lorenzo Stoakes
2026-02-09 18:53 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 06/13] mm: update hugetlbfs to use VMA flags on mmap_prepare Lorenzo Stoakes
2026-02-09 19:03 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 07/13] mm: update secretmem " Lorenzo Stoakes
2026-01-28 12:08 ` Chris Mason
2026-01-28 16:04 ` Lorenzo Stoakes
2026-01-28 16:44 ` Lorenzo Stoakes
2026-02-09 19:06 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 08/13] mm: update shmem_[kernel]_file_*() functions to use vma_flags_t Lorenzo Stoakes
2026-01-23 6:20 ` Baolin Wang
2026-01-23 12:30 ` Lorenzo Stoakes
2026-01-23 7:46 ` Darrick J. Wong
2026-01-23 12:26 ` Lorenzo Stoakes
2026-01-23 12:33 ` Lorenzo Stoakes
2026-01-25 14:50 ` Jarkko Sakkinen
2026-02-09 19:13 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 09/13] mm: update all remaining mmap_prepare users " Lorenzo Stoakes
2026-01-22 23:31 ` Damien Le Moal
2026-01-23 7:47 ` Darrick J. Wong
2026-02-06 17:46 ` Pedro Falcato
2026-02-06 19:31 ` Andrew Morton
2026-02-06 20:01 ` Lorenzo Stoakes
2026-02-09 19:27 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 10/13] mm: make vm_area_desc utilise vma_flags_t only Lorenzo Stoakes
2026-02-06 17:49 ` Pedro Falcato
2026-02-09 19:32 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 11/13] tools/testing/vma: separate VMA userland tests into separate files Lorenzo Stoakes
2026-02-09 19:58 ` Liam R. Howlett [this message]
2026-02-10 17:44 ` Lorenzo Stoakes
2026-01-22 16:06 ` [PATCH v2 12/13] tools/testing/vma: separate out vma_internal.h into logical headers Lorenzo Stoakes
2026-01-27 10:03 ` Lorenzo Stoakes
2026-02-09 20:18 ` Liam R. Howlett
2026-01-22 16:06 ` [PATCH v2 13/13] tools/testing/vma: add VMA userland tests for VMA flag functions Lorenzo Stoakes
2026-02-09 20:23 ` Liam R. Howlett
2026-01-22 16:56 ` [PATCH v2 00/13] mm: add bitmap VMA flag helpers and convert all mmap_prepare to use them Andrew Morton
2026-01-27 13:53 ` Yury Norov
2026-01-27 14:40 ` Lorenzo Stoakes
2026-01-27 21:36 ` Yury Norov
2026-01-28 9:33 ` Lorenzo Stoakes
2026-01-28 15:50 ` Pedro Falcato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=opopo4rlyxz7upi2bzrx7e6cyji2nmaynvlhvr2nzvtvb5pfxu@4fekgb5faybz \
--to=liam.howlett@oracle.com \
--cc=Dave.Martin@arm.com \
--cc=adilger.kernel@dilger.ca \
--cc=airlied@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=almaz.alexandrovich@paragon-software.com \
--cc=arnd@arndb.de \
--cc=babu.moger@amd.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=bcrl@kvack.org \
--cc=bp@alien8.de \
--cc=brauner@kernel.org \
--cc=cem@kernel.org \
--cc=chao@kernel.org \
--cc=christian.koenig@amd.com \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=dave.jiang@intel.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=devel@lists.orangefs.org \
--cc=dhavale@google.com \
--cc=dhowells@redhat.com \
--cc=dlemoal@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gregkh@linuxfoundation.org \
--cc=guochunhai@vivo.com \
--cc=hpa@zytor.com \
--cc=hubcap@omnibond.com \
--cc=hughd@google.com \
--cc=intel-gfx@lists.freedesktop.org \
--cc=jack@suse.cz \
--cc=james.morse@arm.com \
--cc=jani.nikula@linux.intel.com \
--cc=jannh@google.com \
--cc=jarkko@kernel.org \
--cc=jefflexu@linux.alibaba.com \
--cc=jgg@nvidia.com \
--cc=jmorris@namei.org \
--cc=joonas.lahtinen@linux.intel.com \
--cc=jth@kernel.org \
--cc=keyrings@vger.kernel.org \
--cc=lance.yang@linux.dev \
--cc=lihongbo22@huawei.com \
--cc=linux-aio@kvack.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-erofs@lists.ozlabs.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linux-sgx@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=lorenzo.stoakes@oracle.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=martin@omnibond.com \
--cc=matthew.auld@intel.com \
--cc=matthew.brost@intel.com \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=mripard@kernel.org \
--cc=muchun.song@linux.dev \
--cc=naohiro.aota@wdc.com \
--cc=npache@redhat.com \
--cc=ntfs3@lists.linux.dev \
--cc=nvdimm@lists.linux.dev \
--cc=osalvador@suse.de \
--cc=paul@paul-moore.com \
--cc=pfalcato@suse.de \
--cc=ray.huang@amd.com \
--cc=reinette.chatre@intel.com \
--cc=rodrigo.vivi@intel.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=serge@hallyn.com \
--cc=simona@ffwll.ch \
--cc=surenb@google.com \
--cc=tglx@kernel.org \
--cc=tony.luck@intel.com \
--cc=tursulin@ursulin.net \
--cc=tytso@mit.edu \
--cc=tzimmermann@suse.de \
--cc=vbabka@suse.cz \
--cc=viro@zeniv.linux.org.uk \
--cc=vishal.l.verma@intel.com \
--cc=willy@infradead.org \
--cc=x86@kernel.org \
--cc=xiang@kernel.org \
--cc=yury.norov@gmail.com \
--cc=zbestahu@gmail.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox