From nobody Thu Dec 18 09:45:16 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1765902781; cv=none; d=zohomail.com; s=zohoarc; b=hzc8dG65/rPXp+Sq3LmerVjXCStWmiwLAVTSiz34M7/Yr8rpurSA2odY8rslb9ENB45zRCghSkz5XAeBZoDHEuGlZ0DuLX8syCyBC6khpL/05+Nyldqp+5EPY+r3G5c/Dqlf+LjuAuYRml5Cnpp8GM6V9c4Wnj7VVl9ofTpwx5w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1765902781; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=5gXaR4CNMM1JwOw0eyl/FJQAxgRZwy+cxNH9Xft08So=; b=QFAD8uoFR2HTbTFj6JJ8ZKD0uP4AnZJdYPdooYQOhZoV9bHuxw2Nlq+aZUJicHKNfv4PqChVdHW5PGSqx8FEu8xi4PAdMld7XNST5j+Kn3QdkeW94WKonzp16Iu8Fzm8S8ku1/mngr4AftvJrGZYPI0TxHOMC7z3FdUvrCOwzo8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1765902781215248.33128483271514; Tue, 16 Dec 2025 08:33:01 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1188085.1509367 (Exim 4.92) (envelope-from ) id 1vVXyd-0002KB-Go; Tue, 16 Dec 2025 16:32:43 +0000 Received: by outflank-mailman (output) from mailman id 1188085.1509367; Tue, 16 Dec 2025 16:32:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vVXyd-0002K4-Dr; Tue, 16 Dec 2025 16:32:43 +0000 Received: by outflank-mailman (input) for mailman id 1188085; Tue, 16 Dec 2025 16:32:41 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vVXyb-0001QJ-UE for xen-devel@lists.xenproject.org; Tue, 16 Dec 2025 16:32:41 +0000 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [2a00:1450:4864:20::32f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id d6a46efb-da9c-11f0-9cce-f158ae23cfc8; Tue, 16 Dec 2025 17:32:40 +0100 (CET) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-47bdbc90dcaso633435e9.1 for ; Tue, 16 Dec 2025 08:32:40 -0800 (PST) Received: from localhost.localdomain (host-92-26-102-188.as13285.net. [92.26.102.188]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47bd994a221sm12994765e9.7.2025.12.16.08.32.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Dec 2025 08:32:38 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d6a46efb-da9c-11f0-9cce-f158ae23cfc8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1765902759; x=1766507559; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5gXaR4CNMM1JwOw0eyl/FJQAxgRZwy+cxNH9Xft08So=; b=ACFzqlCgx0ER4aD7erwshX4ntmHMalZjwi/sS91C+cpNqVNoB3LjFkWXSUsYd2WMH/ +FeyG08KWecYL545rouMkAoYIt7So6do9l70zv4D6w667atP1ReerjpBy2/5qiBdpWhO Zy2GwahPt+n78WHSlS7lGeW0Ok5LiWWbcZyac= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765902759; x=1766507559; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5gXaR4CNMM1JwOw0eyl/FJQAxgRZwy+cxNH9Xft08So=; b=MyLQbXlMU3/5UViUlLm9iOF86SW2FblYNEHZ/8aYT2FWlc0yD24GxEazoBZ7X/0uAa FkXYlOHsx2d8P0xg6a9oPZ8mXfsnCluZZsLyK6t7iiipXmTan6NtfH6ryca6LW+pOmsw +UdEtETg8bFcnR7M1iFNIMsbpB9Y1HkPmVNsJx9tg21axX/Z4PqPhsHE2VOOBHlGO9yy 0DRSKMU7gnJO4HiAkcICSXj+ecNbQcqf6RMbp8OYNW9ePBnxmuqk4xN4NVjmlv6rLUyf 49L7GNjZ7Q5In6bFwx38XfASq87fpUdHdS9yXhAmoeU0+keYz/95EXZdV7RPdfmQlX0t p9ZQ== X-Gm-Message-State: AOJu0YxN9DsDPJDb2FM3oddVe3AU46XKgumvkuMTlpft2gO4TZTQALCm 9BulJt1qPnWTnJxCNSrbRgnmKMSaWuGEUOyz5FaNBAJPCIC4+PnNM0gGHkt9Y8VBKzWN8ZjuWxw 73N4w X-Gm-Gg: AY/fxX5x94Bbh0G2kPbUGyOAob+6AiiKgthee5oXkNsYwhLZt8kxflOJdFCZ/OldHEt SUZUcC3A/KFpmhdCXQhKhEN1wS9XC9NGJL5JLNZJtOYr3jSv3JtsX/w5LItt0WTvgIrrMyZ1FIT Ul4qVZ+CzAAdMbSxp802A154vmWyjd4AOuTUT+wYx47yrc3UkqaY0fVDmvgiYanNE4xNFpAlA4X X3/0e0xl1cRaipklWloz0sji4gKv5TvUYPAEZeETYhJSJglMz4MT6HTJApoWWp2y5yzd09I1Vlk 9YLQH3gHZSG8EB3sZ9Ak5cZ1XkUq4HhgvEaLTibCPKTrcxBtKtSQf/5bz4j8qi2+hKENL7puuRj NqrAzDlr1CzuD3bT5qTwRzfddnvduU5TFyFI9WRioqqnEVKqNgWeRpPBUkgO563M2uwbyFjwOvr MrLuxtVg1eRfUN46DSXoAQXVOvRitsu8jTotu0I/y1MmzhY2/o+pp7gnvPlDXgzQ== X-Google-Smtp-Source: AGHT+IHs5rK2CgHB0hILBdJRVro1dQCu5BQib4zZRP6eGKEUwzYltfCTDPzhWnlri2qpScjrghDDVA== X-Received: by 2002:a05:600c:5252:b0:477:76cb:4812 with SMTP id 5b1f17b1804b1-47a8f708ebamr151546015e9.0.1765902759242; Tue, 16 Dec 2025 08:32:39 -0800 (PST) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH 4/4] tools/tests: Memory Claims Date: Tue, 16 Dec 2025 16:32:30 +0000 Message-Id: <20251216163230.2768186-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251216163230.2768186-1-andrew.cooper3@citrix.com> References: <20251216163230.2768186-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1765902783342154100 Add some basic testing of the memory claims mechainsm. Signed-off-by: Andrew Cooper Reviewed-by: Roger Pau Monn=C3=A9 --- CC: Anthony PERARD CC: Michal Orzel CC: Jan Beulich CC: Julien Grall CC: Roger Pau Monn=C3=A9 CC: Stefano Stabellini --- tools/tests/Makefile | 1 + tools/tests/mem-claim/.gitignore | 1 + tools/tests/mem-claim/Makefile | 38 +++++ tools/tests/mem-claim/test-mem-claim.c | 190 +++++++++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 tools/tests/mem-claim/.gitignore create mode 100644 tools/tests/mem-claim/Makefile create mode 100644 tools/tests/mem-claim/test-mem-claim.c diff --git a/tools/tests/Makefile b/tools/tests/Makefile index e566bd169952..6477a4386dda 100644 --- a/tools/tests/Makefile +++ b/tools/tests/Makefile @@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk =20 SUBDIRS-y :=3D SUBDIRS-y +=3D domid +SUBDIRS-y +=3D mem-claim SUBDIRS-y +=3D paging-mempool SUBDIRS-y +=3D pdx SUBDIRS-y +=3D rangeset diff --git a/tools/tests/mem-claim/.gitignore b/tools/tests/mem-claim/.giti= gnore new file mode 100644 index 000000000000..cfcee00b819b --- /dev/null +++ b/tools/tests/mem-claim/.gitignore @@ -0,0 +1 @@ +test-mem-claim diff --git a/tools/tests/mem-claim/Makefile b/tools/tests/mem-claim/Makefile new file mode 100644 index 000000000000..76ba3e3c8bef --- /dev/null +++ b/tools/tests/mem-claim/Makefile @@ -0,0 +1,38 @@ +XEN_ROOT =3D $(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TARGET :=3D test-mem-claim + +.PHONY: all +all: $(TARGET) + +.PHONY: clean +clean: + $(RM) -- *.o $(TARGET) $(DEPS_RM) + +.PHONY: distclean +distclean: clean + $(RM) -- *~ + +.PHONY: install +install: all + $(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests + $(INSTALL_PROG) $(TARGET) $(DESTDIR)$(LIBEXEC)/tests + +.PHONY: uninstall +uninstall: + $(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$(TARGET) + +CFLAGS +=3D $(CFLAGS_xeninclude) +CFLAGS +=3D $(CFLAGS_libxenctrl) +CFLAGS +=3D $(APPEND_CFLAGS) + +LDFLAGS +=3D $(LDLIBS_libxenctrl) +LDFLAGS +=3D $(APPEND_LDFLAGS) + +%.o: Makefile + +$(TARGET): test-mem-claim.o + $(CC) -o $@ $< $(LDFLAGS) + +-include $(DEPS_INCLUDE) diff --git a/tools/tests/mem-claim/test-mem-claim.c b/tools/tests/mem-claim= /test-mem-claim.c new file mode 100644 index 000000000000..78eae9091f52 --- /dev/null +++ b/tools/tests/mem-claim/test-mem-claim.c @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static unsigned int nr_failures; +#define fail(fmt, ...) \ +({ \ + nr_failures++; \ + (void)printf(fmt, ##__VA_ARGS__); \ +}) + +static xc_interface *xch; +static uint32_t domid =3D -1; + +static xc_physinfo_t physinfo; + +static struct xen_domctl_createdomain create =3D { + .flags =3D XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap, + .max_vcpus =3D 1, + .max_grant_frames =3D 1, + .grant_opts =3D XEN_DOMCTL_GRANT_version(1), + + .arch =3D { +#if defined(__x86_64__) || defined(__i386__) + .emulation_flags =3D XEN_X86_EMU_LAPIC, +#endif + }, +}; + +static void run_tests(void) +{ + int rc; + + /* + * Check that the system is quiescent. Outstanding claims is a global + * field. + */ + rc =3D xc_physinfo(xch, &physinfo); + if ( rc ) + return fail("Failed to obtain physinfo: %d - %s\n", + errno, strerror(errno)); + + printf("Free pages: %"PRIu64", Oustanding claims: %"PRIu64"\n", + physinfo.free_pages, physinfo.outstanding_pages); + + if ( physinfo.outstanding_pages ) + return fail(" Test needs running on a quiescent system\n"); + + /* + * We want any arbitrary domain. Start with HVM/HAP, falling back to + * HVM/Shadow and then to PV. The dom0 running this test case is one = of + * these modes. + */ +#if defined(__x86_64__) || defined(__i386__) + if ( !(physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hap) ) + create.flags &=3D ~XEN_DOMCTL_CDF_hap; + + if ( !(physinfo.capabilities & (XEN_SYSCTL_PHYSCAP_hap|XEN_SYSCTL_PHYS= CAP_shadow)) || + !(physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hvm) ) + { + create.flags &=3D ~XEN_DOMCTL_CDF_hvm; + create.arch.emulation_flags =3D 0; + } +#endif + + rc =3D xc_domain_create(xch, &domid, &create); + if ( rc ) + return fail(" Domain create failure: %d - %s\n", + errno, strerror(errno)); + + rc =3D xc_domain_setmaxmem(xch, domid, -1); + if ( rc ) + return fail(" Failed to set maxmem: %d - %s\n", + errno, strerror(errno)); + + printf(" Created d%u\n", domid); + + /* + * Creating a domain shouldn't change the claim. Check it's still 0. + */ + rc =3D xc_physinfo(xch, &physinfo); + if ( rc ) + return fail(" Failed to obtain physinfo: %d - %s\n", + errno, strerror(errno)); + + if ( physinfo.outstanding_pages ) + return fail(" Unexpected outstanding claim of %"PRIu64" pages\n", + physinfo.outstanding_pages); + + /* + * Set a claim for 4M. This should be the only claim in the system, a= nd + * show up globally. + */ + rc =3D xc_domain_claim_pages(xch, domid, 4*1024*1024/4096); + if ( rc ) + return fail(" Failed to claim 4M of RAM: %d - %s\n", + errno, strerror(errno)); + + rc =3D xc_physinfo(xch, &physinfo); + if ( rc ) + return fail(" Failed to obtain physinfo: %d - %s\n", + errno, strerror(errno)); + + if ( physinfo.outstanding_pages !=3D 4*1024*1024/4096 ) + return fail(" Expected claim to be 4M, got %"PRIu64" pages\n", + physinfo.outstanding_pages); + + /* + * Allocate 2M of RAM to the domain. This should be deducted from glo= bal + * claim. + */ + xen_pfn_t ram[] =3D { 0 }; + rc =3D xc_domain_populate_physmap_exact( + xch, domid, ARRAY_SIZE(ram), 9, 0, ram); + if ( rc ) + return fail(" Failed to populate physmap domain: %d - %s\n", + errno, strerror(errno)); + + rc =3D xc_physinfo(xch, &physinfo); + if ( rc ) + return fail(" Failed to obtain physinfo: %d - %s\n", + errno, strerror(errno)); + + if ( physinfo.outstanding_pages !=3D 2*1024*1024/4096 ) + return fail(" Expected claim to be 2M, got %"PRIu64" pages\n", + physinfo.outstanding_pages); + + /* + * Destroying the domain should release the outstanding 2M claim. + */ + rc =3D xc_domain_destroy(xch, domid); + + /* Cancel the cleanup path, even in the case of an error. */ + domid =3D -1; + + if ( rc ) + return fail(" Failed to destroy domain: %d - %s\n", + errno, strerror(errno)); + + rc =3D xc_physinfo(xch, &physinfo); + if ( rc ) + return fail(" Failed to obtain physinfo: %d - %s\n", + errno, strerror(errno)); + + if ( physinfo.outstanding_pages ) + return fail(" Expected no outstanding claim, got %"PRIu64" pages\= n", + physinfo.outstanding_pages); +} + +int main(int argc, char **argv) +{ + int rc; + + printf("Memory claims tests\n"); + + xch =3D xc_interface_open(NULL, NULL, 0); + + if ( !xch ) + err(1, "xc_interface_open"); + + run_tests(); + + if ( (int)domid >=3D 0 ) + { + rc =3D xc_domain_destroy(xch, domid); + if ( rc ) + fail(" Failed to destroy domain: %d - %s\n", + errno, strerror(errno)); + } + + return !!nr_failures; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.39.5