tools/tests/native: Native test environment for NUMA claim sets
v3 cover tools/tests/native: Native test environment for NUMA claim sets
Dear reviewers,
This patch series introduces a native test environment for Xen, currently focused on the hypercall API and exercising the page allocator APIs in native test programs. The available APIs are:
- The DOMCTL APIs in xc_domain.c, e.g. xc_domain_claim_memory().
- The XENMEM APIs in xc_domain.c, e.g. xc_domain_claim_pages().
- The SYSCTL APIs in xc_domain.c, e.g. xc_availheap().
Where needed, test cases can call Xen functions directly, and can check the heap state e.g. to check consistency. Any issues can be investigated directly without rebooting a Xen instance.
The second patch adds regression tests for the NUMA claim sets v7 series, so this series depends on it.
Please help to review/ack it. It should be eligible for Xen 4.22:
[PATCH v7 0/7] xen/mm: Introduce NUMA-aware claim sets for domains
https://lists.xen.org/archives/html/xen-devel/2026-05/msg00363.html
https://patchwork.kernel.org/project/xen-devel/list/?series=1091810
For local review and test, you can pull and the claims-v7 code and the tests:
$ git pull [email protected]:bernhardkaindl/xen.git claims-native-tests-v3 $ make -C tools/tests/native test cross=y # see tools/tests/native/README.rst
Thanks,
Bernhard Kaindl
Changes since v2:
- Replaced shims that mimicked sched.h and mm.h with the real Xen headers
- Updated and extended the tests for the NUMA claims v7 series
- Changed test calls to use DOMCTL, XENMEM and SYSCTL APIs in xc_domain.c
Bernhard Kaindl (2):
tests/native: Add native Xen test environment tests/native: Add native tests for NUMA claim sets
tools/tests/Makefile | 1 + tools/tests/native/.gitignore | 4 + tools/tests/native/Makefile | 206 +++++++++++ tools/tests/native/README.rst | 166 +++++++++ tools/tests/native/harness/common.h | 280 +++++++++++++++ tools/tests/native/harness/domctl-shim.h | 228 ++++++++++++ tools/tests/native/harness/domctl-wrapper.h | 28 ++ tools/tests/native/harness/memory-shim.h | 201 +++++++++++ tools/tests/native/harness/memory-wrapper.h | 42 +++ tools/tests/native/harness/mm-wrapper.h | 118 +++++++ tools/tests/native/harness/native.h | 81 +++++ tools/tests/native/harness/page-alloc-env.h | 254 ++++++++++++++ tools/tests/native/harness/page-alloc-shim.h | 328 ++++++++++++++++++ tools/tests/native/harness/sysctl-shim.h | 155 +++++++++ tools/tests/native/harness/sysctl-wrapper.h | 38 ++ tools/tests/native/harness/testcase-asserts.h | 310 +++++++++++++++++ tools/tests/native/harness/xc-domain-env.h | 77 ++++ tools/tests/native/harness/xen-macros.h | 118 +++++++ tools/tests/native/harness/xenctrl-shim.h | 170 +++++++++ tools/tests/native/host-claims.c | 248 +++++++++++++ tools/tests/native/node-claims.c | 230 ++++++++++++ 21 files changed, 3283 insertions(+) create mode 100644 tools/tests/native/.gitignore create mode 100644 tools/tests/native/Makefile create mode 100644 tools/tests/native/README.rst create mode 100644 tools/tests/native/harness/common.h create mode 100644 tools/tests/native/harness/domctl-shim.h create mode 100644 tools/tests/native/harness/domctl-wrapper.h create mode 100644 tools/tests/native/harness/memory-shim.h create mode 100644 tools/tests/native/harness/memory-wrapper.h create mode 100644 tools/tests/native/harness/mm-wrapper.h create mode 100644 tools/tests/native/harness/native.h create mode 100644 tools/tests/native/harness/page-alloc-env.h create mode 100644 tools/tests/native/harness/page-alloc-shim.h create mode 100644 tools/tests/native/harness/sysctl-shim.h create mode 100644 tools/tests/native/harness/sysctl-wrapper.h create mode 100644 tools/tests/native/harness/testcase-asserts.h create mode 100644 tools/tests/native/harness/xc-domain-env.h create mode 100644 tools/tests/native/harness/xen-macros.h create mode 100644 tools/tests/native/harness/xenctrl-shim.h create mode 100644 tools/tests/native/host-claims.c create mode 100644 tools/tests/native/node-claims.c
Signature · 1 line
2.39.5
PATCH v3 1/2 tests/native: Add native Xen test environment
Add a native test environment for hypercall testing that runs without a Xen instance.
The initial focus is the page allocator (page_alloc.c) and its hypercalls. In general, APIs testable with this environment include:
- DOMCTL APIs via xc_domain.c, e.g. xc_domain_claim_memory().
- XENMEM APIs via xc_domain.c, e.g. xc_domain_claim_pages().
- SYSCTL APIs via xc_domain.c, e.g. xc_availheap().
The test programs build the Xen code under test into the same compilation unit as the test case source. This gives the tests full control over the environment and allows selected functions to be stubbed, mocked, or wrapped as required by the scenario under test.
For the runtime buddy allocator in page_alloc.c, the environment creates a synthetic Xen heap. Each test program can define a Xen configuration, for example whether CONFIG_NUMA and CONFIG_SYSCTL are enabled. The test cases populate the free lists individually for each test scenario as needed and can check the heap state.
For testing new code across multiple target architectures, the harness supports cross-compilation of both the tests and the code under test for x86_64, arm64, arm32, and riscv64 by passing cross=y to the build.
If binfmt-misc and qemu-user-static are configured, the cross-compiled test programs can also be run. Where supported, clang-based builds are automatically added to the test matrix.
--- tools/tests/Makefile | 1 + tools/tests/native/.gitignore | 4 + tools/tests/native/Makefile | 206 +++++++++++ tools/tests/native/README.rst | 166 +++++++++ tools/tests/native/harness/common.h | 280 +++++++++++++++ tools/tests/native/harness/domctl-shim.h | 228 ++++++++++++ tools/tests/native/harness/domctl-wrapper.h | 28 ++ tools/tests/native/harness/memory-shim.h | 201 +++++++++++ tools/tests/native/harness/memory-wrapper.h | 42 +++ tools/tests/native/harness/mm-wrapper.h | 118 +++++++ tools/tests/native/harness/native.h | 81 +++++ tools/tests/native/harness/page-alloc-env.h | 254 ++++++++++++++ tools/tests/native/harness/page-alloc-shim.h | 328 ++++++++++++++++++ tools/tests/native/harness/sysctl-shim.h | 155 +++++++++ tools/tests/native/harness/sysctl-wrapper.h | 38 ++ tools/tests/native/harness/testcase-asserts.h | 310 +++++++++++++++++ tools/tests/native/harness/xc-domain-env.h | 77 ++++ tools/tests/native/harness/xen-macros.h | 118 +++++++ tools/tests/native/harness/xenctrl-shim.h | 170 +++++++++ 19 files changed, 2805 insertions(+) create mode 100644 tools/tests/native/.gitignore create mode 100644 tools/tests/native/Makefile create mode 100644 tools/tests/native/README.rst create mode 100644 tools/tests/native/harness/common.h create mode 100644 tools/tests/native/harness/domctl-shim.h create mode 100644 tools/tests/native/harness/domctl-wrapper.h create mode 100644 tools/tests/native/harness/memory-shim.h create mode 100644 tools/tests/native/harness/memory-wrapper.h create mode 100644 tools/tests/native/harness/mm-wrapper.h create mode 100644 tools/tests/native/harness/native.h create mode 100644 tools/tests/native/harness/page-alloc-env.h create mode 100644 tools/tests/native/harness/page-alloc-shim.h create mode 100644 tools/tests/native/harness/sysctl-shim.h create mode 100644 tools/tests/native/harness/sysctl-wrapper.h create mode 100644 tools/tests/native/harness/testcase-asserts.h create mode 100644 tools/tests/native/harness/xc-domain-env.h create mode 100644 tools/tests/native/harness/xen-macros.h create mode 100644 tools/tests/native/harness/xenctrl-shim.h
tools/tests/Makefile +1 -0
@@ -16,6 +16,7 @@ SUBDIRS-$(CONFIG_X86) += tsx ifneq ($(clang),y) SUBDIRS-$(CONFIG_X86) += x86_emulator endif +SUBDIRS-$(CONFIG_X86) += native .PHONY: all clean install distclean uninstall all clean distclean install uninstall: %: subdirs-%
tools/tests/native/.gitignore new file +4 -0
@@ -0,0 +1,4 @@ +*.x86_64* +*.arm32* +*.arm64* +*.riscv64*
tools/tests/native/Makefile new file +206 -0
206 lines not on this page · raw message
tools/tests/native/README.rst new file +166 -0
166 lines not on this page · raw message
tools/tests/native/harness/common.h new file +280 -0
280 lines not on this page · raw message
tools/tests/native/harness/domctl-shim.h new file +228 -0
228 lines not on this page · raw message
tools/tests/native/harness/domctl-wrapper.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Wrapper around xen/common/domctl.c for the allocator test framework. +* +* This file includes the real domctl.c directly in its translation unit. +* +* It allows to pass hypercalls from xc_domain to the do_domctl() +* hypercall handler bin xen/common/domctl.c, which is the entry +* point for domctl commands in the Xen hypervisor. +* +* Copyright (C) 2026 Cloud Software Group +*/ +#ifndef TOOLS_TESTS_NATIVE_HARNESS_DOMCTL_WRAPPER_H +#define TOOLS_TESTS_NATIVE_HARNESS_DOMCTL_WRAPPER_H +#ifdef TEST_WRAP_XEN_COMMON_DOMCTL_C +#include "domctl-shim.h" +#pragma GCC diagnostic push +/* Header guards for xen/common/domctl.c inclusion. */ +#define _ASM_HW_IRQ_H +#define __XEN_HYPERCALL_H__ +#pragma GCC diagnostic ignored "-Wunused-parameter" +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wfor-loop-analysis" +#endif +#include "../../xen/common/domctl.c" +#pragma GCC diagnostic pop +#endif +#endif
tools/tests/native/harness/memory-shim.h new file +201 -0
201 lines not on this page · raw message
tools/tests/native/harness/memory-wrapper.h new file +42 -0
42 lines not on this page · raw message
tools/tests/native/harness/mm-wrapper.h new file +118 -0
118 lines not on this page · raw message
tools/tests/native/harness/native.h new file +81 -0
81 lines not on this page · raw message
tools/tests/native/harness/page-alloc-env.h new file +254 -0
254 lines not on this page · raw message
tools/tests/native/harness/page-alloc-shim.h new file +328 -0
328 lines not on this page · raw message
tools/tests/native/harness/sysctl-shim.h new file +155 -0
155 lines not on this page · raw message
tools/tests/native/harness/sysctl-wrapper.h new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Wrapper around xen/common/sysctl.c for the allocator test framework. +* +* Includes the real sysctl.c directly in its translation unit. +* +* Copyright (C) 2026 Cloud Software Group +*/ +#ifndef TOOLS_TESTS_NATIVE_HARNESS_SYSCTL_WRAPPER_H +#define TOOLS_TESTS_NATIVE_HARNESS_SYSCTL_WRAPPER_H +#ifdef TEST_WRAP_XEN_COMMON_SYSCTL_C + +#include "sysctl-shim.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wunused-variable" +#pragma GCC diagnostic ignored "-Wunused-function" +#include "../../xen/common/sysctl.c" +#pragma GCC diagnostic pop + +#define TEST_WRAP_TOOLS_INCLUDE_XENCTRL_H +#include "xenctrl-shim.h" + +int xc_sysctl (xc_interface *xch,struct xen_sysctl *sysctl) +{ +/* Convert from sysctl pointer to xen_sysctl_t handle */ +union { +XEN_GUEST_HANDLE_PARAM (xen_sysctl_t) handle; +struct xen_sysctl *ptr; + } u = { .ptr = sysctl }; + + sysctl->interface_version =XEN_SYSCTL_INTERFACE_VERSION ; +ASSERT (xch == &test_xc_handle); +return do_sysctl (u.handle);/* Call xen/common/sysctl.c's do_sysctl() */ +} + +#endif +#endif
tools/tests/native/harness/testcase-asserts.h new file +310 -0
310 lines not on this page · raw message
tools/tests/native/harness/xc-domain-env.h new file +77 -0
77 lines not on this page · raw message
tools/tests/native/harness/xen-macros.h new file +118 -0
118 lines not on this page · raw message
tools/tests/native/harness/xenctrl-shim.h new file +170 -0
171 lines not on this page · raw message
Signature · 1 line
2.39.5
PATCH v3 2/2 tests/native: Add native tests for NUMA claim sets
In reply to Bernhard KaindlTest the page allocator's claim behaviour using:
- the legacy xc_domain_claim_pages() call, and
- the new xc_domain_claim_memory() call of the claim-set API,
exercising both host-wide and NUMA node-specific multi-node claims.
--- tools/tests/native/host-claims.c | 248 +++++++++++++++++++++++++++++++ tools/tests/native/node-claims.c | 230 ++++++++++++++++++++++++++++ 2 files changed, 478 insertions(+) create mode 100644 tools/tests/native/host-claims.c create mode 100644 tools/tests/native/node-claims.c
tools/tests/native/host-claims.c new file +248 -0
@@ -0,0 +1,248 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Test basic host-wide functionality of memory claims, including +* installing and redeeming claims, and that claims are respected +* by allocations and protected against other allocations. +* +* Copyright (C) 2026 Cloud Software Group +*/ + +#define TEST_ENABLE_XC_DOMAIN_C /* Enable xc_domain.c APIs */ +#include "harness/native.h" + +typedef int (*set_global_claims )(struct domain *d,unsigned long pages); +set_global_claims install_host_claims; + +/* Install a host-wide claim using the legacy xc_domain_claim_pages() call */ +int install_host_claims_legacy (struct domain *d,unsigned long pages) +{ +if (pages ==0 ) +return xc_domain_claim_pages (xch, d->domain_id,0 ); + +/* The legacy call need resetting claims before claims can be set again */ +xc_domain_claim_pages (xch, d->domain_id,0 ); + +/* The argument of the legacy call includes the domain's existing pages */ + pages +=domain_tot_pages (d); + +return xc_domain_claim_pages (xch, d->domain_id, pages); +} + +/* Install a host-wide claim set using the xc_domain.c hypercall API */ +int xc_domain_claim_memory_host (struct domain *d,unsigned long pages) +{ +xen_memory_claim_t claim_set[] = { + { .target =XEN_DOMCTL_CLAIM_MEMORY_HOST , .pages = pages }, + }; +uint32_t nr_entries =ARRAY_SIZE (claim_set); + +return xc_domain_claim_memory (xch, d->domain_id, +XEN_DOMCTL_CLAIM_MEMORY_SET , &nr_entries, claim_set); +} + +static void test_alloc_domheap_redeems_claims (int start_mfn) +{ +int ret; +struct page_info *pages = frame_table + start_mfn, *pg; + +test_page_list_add_buddy (pages, order2); +ASSERT (!install_host_claims (dom1,3 )); +ASSERT (alloc_domheap_pages (dom1, order1,0 ) == pages +2 ); +ASSERT (alloc_domheap_pages (dom1, order0,0 ) == pages +1 ); +CHECK (TOTAL_CLAIMS ==0 ,"Expect all claims consumed after allocations" ); +CHECK (FREE_PAGES ==1 ,"Expect one free page after allocations" ); + +ASSERT (!install_host_claims (dom2,FREE_PAGES )); + +/* Claim more than dom1 already has fails with ENOMEM (claimed by dom2) */ + ret =install_host_claims (dom1,domain_tot_pages (dom1) +1 ); +CHECK (ret == -ENOMEM ,"dom 1 claim +1 fails due to insufficient pages" ); + +/* Claim more than dom1's d->max_pages fails with EINVAL */ + ret =install_host_claims (dom1, dom1->max_pages +1 ); +CHECK (ret == -EINVAL ,"dom 1 claim fails due to exceeding max_pages" ); + +/* Attempt to allocate an order-0 page with a foreign claim present */ + pg =alloc_domheap_pages (dom1, order0,0 ); +CHECK (pg ==NULL ,"dom 1 allocation fails because of domain 2's claim" ); +CHECK (TOTAL_CLAIMS ==1 ,"Expect domain 2's claim to be still present" ); +CHECK (FREE_PAGES ==1 ,"Expect one free page after failed alloc" ); +} + +/* +* Test that memory claims can be cancelled by setting the claim count to 0, +* and that cancelled claims are freed up for other domains to claim. +* +* This is important for domain_kill() to be able to cancel claims of a dying +* domain and free up the pages for other domains to claim and free, otherwise +* the host might run out of free pages due to claims that are not released. +* +* - Test that after claiming pages for a domain, allocations redeem a portion +* of those claims. +* +* - Test that other domains cannot claim more pages than the unclaimed free +* pages, and that cancelled claims are no longer present after cancellation. +* +* - Test that after cancelling claims for a domain, other domains can claim +* and allocate all remaining free pages. +*/ +static void test_claim_alloc_cancel (int start_mfn) +{ +struct page_info *expected, *page = frame_table + start_mfn; +unsigned long heap_pages, claims; +unsigned int alloc_order; + +/* Create a buddy of order 2 (4 pages) and add it to the heap. */ +test_page_list_add_buddy (page, order3); + heap_pages =FREE_PAGES ; + claims = heap_pages /2 ; + +/* Claim half of the free pages for domain 1 */ +ASSERT (install_host_claims (dom1, claims) ==0 ); +ASSERT (TOTAL_CLAIMS == claims); + +/* Allocate an order 1 page for domain 1 */ + alloc_order = order1; +/* Expect the highest available page to be allocated */ + expected = page +FREE_PAGES - (1UL << alloc_order); +ASSERT (alloc_domheap_pages (dom1, alloc_order,0 ) == expected); +ASSERT (TOTAL_CLAIMS == (claims -=1UL << alloc_order, claims)); + +/* Allocate an order 0 page for domain 1 */ + alloc_order = order0; +/* Expect the highest available page to be allocated */ + expected = page +FREE_PAGES - (1UL << alloc_order); +ASSERT (alloc_domheap_pages (dom1, alloc_order,0 ) == expected); +ASSERT (TOTAL_CLAIMS == (claims -=1UL << alloc_order, claims)); + +/* Claiming more than unclaimed for domain 2 should fail */ +ASSERT (install_host_claims (dom2, heap_pages - claims +1 ) == -ENOMEM ); +/* Claiming all free pages for domain 2 should fail (dom1 has a claim) */ +ASSERT (install_host_claims (dom2,FREE_PAGES ) == -ENOMEM ); +ASSERT (TOTAL_CLAIMS == claims); + +/* +* Cancelling claims needs to always work, the checks in place for +* installing claims should not prevent cancelling claims, which is +* important for domain_kill() to be able to cancel claims of a dying +* domain regardless of the state of the domain's configuration. +* +* An important check that cancelling claims needs to bypass is the +* max_pages check, as a domain's max_pages can be set to a low value +* due to a toolstack process (Xapi's "squeezed" squeezing the domain +* can set its max_pages to a lower value than domain_tot_pages() by +* invoking do_domctl(XEN_DOMCTL_max_mem). +* +* This should not prevent the claims from being cancelled as required. +*/ + dom1->max_pages =domain_tot_pages (dom1) -1 ; + +/* Cancel all remaining claims for domain 1 */ +ASSERT (install_host_claims (dom1,0 ) ==0 ); +ASSERT (TOTAL_CLAIMS ==0 ); + +/* Claim all free pages for domain 2, should work */ + claims =FREE_PAGES ; +ASSERT (install_host_claims (dom2, claims) ==0 ); +ASSERT (TOTAL_CLAIMS == claims); + +/* Claiming for domain 1 should fail with EINVAL due to max_pages = 0 */ +ASSERT (install_host_claims (dom1,1 ) == -EINVAL ); + +/* With d->max_pages > domain_tot_pages(), dom1 claims fails with -ENOMEM */ + dom1->max_pages = heap_pages; +ASSERT (install_host_claims (dom1,1 ) == -ENOMEM ); + +/* Attempting to allocate a page for domain 1 should likewise fail now */ +ASSERT (alloc_domheap_pages (dom1, order0,0 ) ==NULL ); + +/* Allocating a page for domain 2 still work as it has the claims */ + alloc_order = order0; +/* Expect the highest available page to be allocated */ + expected = page +FREE_PAGES - (1UL << alloc_order); +ASSERT (alloc_domheap_pages (dom2, alloc_order,0 ) == expected); +ASSERT (TOTAL_CLAIMS == (claims -=1UL << alloc_order, claims)); + +/* Even allocating the remaining order 2 buddy for domain 2 works */ + alloc_order = order2; +/* Expect the highest available page to be allocated */ + expected = page +FREE_PAGES - (1UL << alloc_order); +ASSERT (alloc_domheap_pages (dom2, alloc_order,0 ) == expected); +ASSERT (TOTAL_CLAIMS == (claims -=1UL << alloc_order, claims)); +} + +/* Test offlining free pages outside and inside the claimed pages pool */ +static void test_offlining_host_claims (int start_mfn) +{ +struct page_info *pages = frame_table + start_mfn; +unsigned long heap_size, claims; + +test_page_list_add_buddy (pages, order2); + + heap_size =FREE_PAGES ; + claims = heap_size -1 ;/* Claim all but one page */ +ASSERT (!install_host_claims (dom1, claims)); + +/* Mark a first page as offline */ + +mark_page_offline (pages +3 ,0 ); +ASSERT (page_state_is (pages +3 , offlined)); + +/* Due to the single unclaimed page, the claims should remain unchanged */ +ASSERT (FREE_PAGES == heap_size); +ASSERT (TOTAL_CLAIMS == heap_size -1 ); +ASSERT (reserve_offlined_page (pages) ==1 ); +ASSERT (FREE_PAGES == heap_size -1 );/* One free page is offlined */ +ASSERT (TOTAL_CLAIMS == heap_size -1 ); + +/* Offline a second page. Offlines a portion of the claimed pages pool. */ + +mark_page_offline (pages +1 ,0 ); +ASSERT (page_state_is (pages +1 , offlined)); + +/* Assert the effect of offlining a portion of the claimed pages pool */ +ASSERT (FREE_PAGES == heap_size -1 ); +ASSERT (TOTAL_CLAIMS == heap_size -1 ); +ASSERT (reserve_offlined_page (pages) ==1 ); +ASSERT (FREE_PAGES == heap_size -2 );/* Two pages are offlined */ +ASSERT (TOTAL_CLAIMS == heap_size -2 );/* One claim is be released */ +} + +int main (int argc,char *argv[]) +{ +const char *topic ="Test host-wide claims with old and new interfaces" ; + +if ( !parse_args (argc, argv, topic) ) +return EXIT_FAILURE ; + +init_page_alloc_tests (); + +/* +* Run the tests on all levels of the claims interface: +* +* 1. Direct page_alloc function call used by other code in the hypervisor, +* which needs to support claim cancellation, needed by domain_kill(). +* +* 2. The domctl helper for the hypercall, which is used by the hypercall +* handler itself to parse the hypercall arguments before calling the +* page_allocator functions. +* +* 3. The real DOMCTL handler, do_domctl(), which is the actual handler +* function called when invoking the real hypercall. +*/ + +/* Test using the direct claim function call used inside the hypervisor */ + install_host_claims = install_host_claims_legacy; +RUN_TESTCASE ("TCCD" , test_claim_alloc_cancel,8 ); +RUN_TESTCASE ("DCGD" , test_alloc_domheap_redeems_claims,4 ); + +/* Test claims setup using the actual DOMCTL handler itself, do_domctl() */ + install_host_claims = xc_domain_claim_memory_host; +RUN_TESTCASE ("TCCH" , test_claim_alloc_cancel,8 ); +RUN_TESTCASE ("DCGH" , test_alloc_domheap_redeems_claims,4 ); + +/* Test offlining free pages outside and inside the claimed pages pool */ +RUN_TESTCASE ("OHCH" , test_offlining_host_claims,4 ); + +return test_complete (); +}
tools/tests/native/node-claims.c new file +230 -0
230 lines not on this page · raw message
Signature · 1 line
2.39.5