xen: Memory claims fixes and testing
cover xen: Memory claims fixes and testing
Two bugfixes, and a test for some basic behaviour.
https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2218133231
Andrew Cooper (4):
xen/domain: Disallow XENMEM_claim_pages on dying domains tools/libxc: Delete ENOSYS squashing in xc_domain_claim_pages() tools/tests: Sort Makefile
tools/tests: Memory Claims
tools/libs/ctrl/xc_domain.c | 9 +- tools/tests/Makefile | 12 +- tools/tests/mem-claim/.gitignore | 1 + tools/tests/mem-claim/Makefile | 38 +++++ tools/tests/mem-claim/test-mem-claim.c | 190 +++++++++++++++++++++++++ xen/common/memory.c | 3 + 6 files changed, 240 insertions(+), 13 deletions(-) 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
base-commit: 46f3ed0d53db6daf38c230dff0d2910bcb61fbe2
Signature · 1 line
2.39.5
PATCH 1/4 xen/domain: Disallow XENMEM_claim_pages on dying domains
XENMEM_claim_pages can be issued on a domain in the later stages of domain_kill(). In that case, the claimed memory is leaked, as there's no way to subsequently release the claim.
Claims are intended for use during domain construction; to reserve sufficient RAM to construct the domain fully. There's no legitimate reason to be calling it on a dying domain.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- xen/common/memory.c | 3 +++ 1 file changed, 3 insertions(+)
xen/common/memory.c +3 -0
@@ -1681,6 +1681,9 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) rc =xsm_claim_pages (XSM_PRIV , d); +if ( !rc && d->is_dying ) + rc = -EINVAL ; +if ( !rc ) rc =domain_set_outstanding_pages (d, reservation.nr_extents);
Signature · 1 line
2.39.5
Re: [PATCH 1/4] xen/domain: Disallow XENMEM_claim_pages on dying domains
Quoted 10 lines · Andrew Cooper
XENMEM_claim_pagescan be issued on a domain in the later stages ofdomain_kill(). In that case, the claimed memory is leaked, as there's no way to subsequently release the claim.Claims are intended for use during domain construction; to reserve sufficient RAM to construct the domain fully. There's no legitimate reason to be calling it on a dying domain.
Fixes 65c9792df600 ("mmu: Introduce XENMEM_claim_pages (subop of memory ops)")Signed-off-by Andrew Cooper <[email protected]>
PATCH 2/4 tools/libxc: Delete ENOSYS squashing in xc_domain_claim_pages()
In reply to Andrew CooperIt's not acceptable to hide this from the caller; the effect of doing so is to break an atomicity expectation.
Only the caller can know what the appropriate safety action is in the case that the claim hypercall isn't available.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- tools/libs/ctrl/xc_domain.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
tools/libs/ctrl/xc_domain.c +1 -8
@@ -1074,7 +1074,6 @@ int xc_domain_claim_pages(xc_interface *xch, uint32_t domid, unsigned long nr_pages) { - int err;struct xen_memory_reservation reservation = { .nr_extents = nr_pages, .extent_order =0 ,
@@ -1082,13 +1081,7 @@ int xc_domain_claim_pages(xc_interface *xch, .domid = domid }; -set_xen_guest_handle (reservation.extent_start,HYPERCALL_BUFFER_NULL ); - - err =xc_memory_op (xch, XENMEM_claim_pages, &reservation,sizeof (reservation)); -/* Ignore it if the hypervisor does not support the call. */ -if (err ==-1 && errno ==ENOSYS ) - err = errno =0 ; -return err; +return xc_memory_op (xch, XENMEM_claim_pages, &reservation,sizeof (reservation)); } int xc_domain_populate_physmap(xc_interface *xch,
Signature · 1 line
2.39.5
Re: [PATCH 2/4] tools/libxc: Delete ENOSYS squashing in xc_domain_claim_pages()
Quoted 8 lines · Andrew Cooper
It's not acceptable to hide this from the caller; the effect of doing so is to break an atomicity expectation.
Only the caller can know what the appropriate safety action is in the case that the claim hypercall isn't available.
Fixes fc67e9dc0c1f ("xc: use XENMEM_claim_pages hypercall during guest creation.")Signed-off-by Andrew Cooper <[email protected]>
Thanks, Roger.
PATCH 3/4 tools/tests: Sort Makefile
In reply to Andrew Cooper... and split the x86 group away from the common group.
No functional change.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- tools/tests/Makefile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
tools/tests/Makefile +6 -5
@@ -3,17 +3,18 @@ include $(XEN_ROOT)/tools/Rules.mk SUBDIRS-y := SUBDIRS-y += domid +SUBDIRS-y += paging-mempool +SUBDIRS-y += pdx +SUBDIRS-y += rangeset SUBDIRS-y += resource +SUBDIRS-y += vpci +SUBDIRS-y += xenstore + SUBDIRS-$(CONFIG_X86) += cpu-policy SUBDIRS-$(CONFIG_X86) += tsx ifneq ($(clang),y) SUBDIRS-$(CONFIG_X86) += x86_emulator endif -SUBDIRS-y += xenstore -SUBDIRS-y += pdx -SUBDIRS-y += rangeset -SUBDIRS-y += vpci -SUBDIRS-y += paging-mempool .PHONY: all clean install distclean uninstall all clean distclean install uninstall: %: subdirs-%
Signature · 1 line
2.39.5
Re: [PATCH 3/4] tools/tests: Sort Makefile
Quoted 5 lines · Andrew Cooper
... and split the x86 group away from the common group.
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
Thanks, Roger.
PATCH 4/4 tools/tests: Memory Claims
In reply to Andrew CooperAdd some basic testing of the memory claims mechainsm.
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>
--- 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
tools/tests/Makefile +1 -0
@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk SUBDIRS-y := SUBDIRS-y += domid +SUBDIRS-y += mem-claim SUBDIRS-y += paging-mempool SUBDIRS-y += pdx SUBDIRS-y += rangeset
tools/tests/mem-claim/.gitignore new file +1 -0
@@ -0,0 +1 @@ +test-mem-claim
tools/tests/mem-claim/Makefile new file +38 -0
@@ -0,0 +1,38 @@ +XEN_ROOT = $(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TARGET := 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 += $(CFLAGS_xeninclude) +CFLAGS += $(CFLAGS_libxenctrl) +CFLAGS += $(APPEND_CFLAGS) + +LDFLAGS += $(LDLIBS_libxenctrl) +LDFLAGS += $(APPEND_LDFLAGS) + +%.o: Makefile + +$(TARGET): test-mem-claim.o + $(CC) -o $@ $< $(LDFLAGS) + +-include $(DEPS_INCLUDE)
tools/tests/mem-claim/test-mem-claim.c new file +190 -0
190 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH 4/4] tools/tests: Memory Claims
Quoted 3 lines · Andrew Cooper
Add some basic testing of the memory claims mechainsm.
Signed-off-by Andrew Cooper <[email protected]>
Some nits below.
tools/tests/mem-claim/test-mem-claim.c
Quoted 86 lines
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>--- 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.ctools/tests/Makefile +1
-0@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk SUBDIRS-y := SUBDIRS-y += domid +SUBDIRS-y += mem-claim SUBDIRS-y += paging-mempool SUBDIRS-y += pdx SUBDIRS-y += rangesettools/tests/mem-claim/.gitignore new file +1
-0@@ -0,0 +1 @@ +test-mem-claimtools/tests/mem-claim/Makefile new file +38
-0@@ -0,0 +1,38 @@ +XEN_ROOT = $(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TARGET := 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 += $(CFLAGS_xeninclude) +CFLAGS += $(CFLAGS_libxenctrl) +CFLAGS += $(APPEND_CFLAGS) + +LDFLAGS += $(LDLIBS_libxenctrl) +LDFLAGS += $(APPEND_LDFLAGS) + +%.o: Makefile + +$(TARGET): test-mem-claim.o + $(CC) -o $@ $< $(LDFLAGS) + +-include $(DEPS_INCLUDE)tools/tests/mem-claim/test-mem-claim.c new file +0
-0@@ -0,0 +1,190 @@
SPDX header comment?
Quoted 11 lines
tools/tests/mem-claim/test-mem-claim.c new file +11
-0+#include <err.h> +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> + +#include <xenctrl.h> +#include <xenforeignmemory.h> +#include <xengnttab.h> +#include <xen-tools/common-macros.h>
tools/tests/mem-claim/test-mem-claim.c line 21
Quoted 10 lines
tools/tests/mem-claim/test-mem-claim.c new file +10
-0+ +static unsigned int nr_failures; +#define fail (fmt, ...) \ +({ \ + nr_failures++; \ + (void)printf(fmt, ##__VA_ARGS__); \ +}) + +static xc_interface *xch; +static uint32_t domid =-1 ;
I think you could use domid_t and DOMID_INVALID as the default value?
And then you could avoid the casting and just use domid < DOMID_FIRST_RESERVED as the check for whether the cleanup is needed?
Quoted 71 lines
tools/tests/mem-claim/test-mem-claim.c new file +71
-0+ +static xc_physinfo_t physinfo; + +static struct xen_domctl_createdomain create = { + .flags = XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap, + .max_vcpus =1 , + .max_grant_frames =1 , + .grant_opts =XEN_DOMCTL_GRANT_version (1 ), + + .arch = { +#if defined(__x86_64__) || defined(__i386__) + .emulation_flags =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 =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 &= ~XEN_DOMCTL_CDF_hap; + +if ( !(physinfo.capabilities & (XEN_SYSCTL_PHYSCAP_hap|XEN_SYSCTL_PHYSCAP_shadow)) || + !(physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hvm) ) + { + create.flags &= ~XEN_DOMCTL_CDF_hvm; + create.arch.emulation_flags =0 ; + } +#endif + + rc =xc_domain_create (xch, &domid, &create); +if ( rc ) +return fail (" Domain create failure: %d - %s\n" , + errno,strerror (errno)); + + rc =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 =xc_physinfo (xch, &physinfo); +if ( rc ) +return fail (" Failed to obtain physinfo: %d - %s\n" , + errno,strerror (errno));
tools/tests/mem-claim/test-mem-claim.c line 102
Quoted 10 lines
tools/tests/mem-claim/test-mem-claim.c new file +10
-0+ +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, and +* show up globally. +*/ + rc =xc_domain_claim_pages (xch, domid,4 *1024 *1024 /4096 );
You can use MB(4) (macro is in tools/common-macros.h) I think for clarity? Same below with MB(2).
Thanks, Roger.
Re: [PATCH 4/4] tools/tests: Memory Claims
Quoted 5 lines · Roger Pau Monné
Quoted 3 lines · Andrew Cooper
Add some basic testing of the memory claims mechainsm.
Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>
Thanks.
Quoted 81 lines
Some nits below.
Quoted 79 lines
---
CC: Anthony PERARD <[email protected]>
CC: Michal Orzel <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Stefano Stabellini <[email protected]>--- 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.ctools/tests/Makefile +1
-0@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk SUBDIRS-y := SUBDIRS-y += domid +SUBDIRS-y += mem-claim SUBDIRS-y += paging-mempool SUBDIRS-y += pdx SUBDIRS-y += rangesettools/tests/mem-claim/.gitignore new file +1
-0@@ -0,0 +1 @@ +test-mem-claimtools/tests/mem-claim/Makefile new file +37
-0@@ -0,0 +1,38 @@ +XEN_ROOT = $(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TARGET := 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 += $(CFLAGS_xeninclude) +CFLAGS += $(CFLAGS_libxenctrl) +CFLAGS += $(APPEND_CFLAGS) + +LDFLAGS += $(LDLIBS_libxenctrl) +LDFLAGS += $(APPEND_LDFLAGS) + +%.o: Makefile + +$(TARGET): test-mem-claim.o + $(CC) -o $@ $< $(LDFLAGS) +
Quoted 8 lines
tools/tests/mem-claim/test-mem-claim.c
Quoted 7 lines
tools/tests/mem-claim/Makefile new file +1
-0+-include $(DEPS_INCLUDE)tools/tests/mem-claim/test-mem-claim.c new file +0
-0@@ -0,0 +1,190 @@SPDX header comment?
Will do.
Quoted 15 lines
Quoted 15 lines
tools/tests/mem-claim/test-mem-claim.c new file +14
-0+#include <err.h> +#include <errno.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> + +#include <xenctrl.h> +#include <xenforeignmemory.h> +#include <xengnttab.h> +#include <xen-tools/common-macros.h> + +static unsigned int nr_failures; +#define fail (fmt, ...) \
Quoted 8 lines
tools/tests/mem-claim/test-mem-claim.c line 21
Quoted 7 lines
tools/tests/mem-claim/test-mem-claim.c new file +7
-0+({ \ + nr_failures++; \ + (void)printf(fmt, ##__VA_ARGS__); \ +}) + +static xc_interface *xch; +static uint32_t domid =-1 ;I think you could use
domid_tandDOMID_INVALIDas the default value?
Sadly can't. Domid's are strictly uint32_t due to xc_domain_create() taking it by pointer.
Quoted 2 lines
And then you could avoid the casting and just use domid <
DOMID_FIRST_RESERVEDas the check for whether the cleanup is needed?
Hmm. The DOMID_INVALID part of that is nice-ish, but having the boundary check be a different constant isn't.
Maybe I can just do != DOMID_INVALID for the cleanup.
Quoted 76 lines
Quoted 76 lines
tools/tests/mem-claim/test-mem-claim.c new file +75
-0+ +static xc_physinfo_t physinfo; + +static struct xen_domctl_createdomain create = { + .flags = XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap, + .max_vcpus =1 , + .max_grant_frames =1 , + .grant_opts =XEN_DOMCTL_GRANT_version (1 ), + + .arch = { +#if defined(__x86_64__) || defined(__i386__) + .emulation_flags =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 =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 &= ~XEN_DOMCTL_CDF_hap; + +if ( !(physinfo.capabilities & (XEN_SYSCTL_PHYSCAP_hap|XEN_SYSCTL_PHYSCAP_shadow)) || + !(physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hvm) ) + { + create.flags &= ~XEN_DOMCTL_CDF_hvm; + create.arch.emulation_flags =0 ; + } +#endif + + rc =xc_domain_create (xch, &domid, &create); +if ( rc ) +return fail (" Domain create failure: %d - %s\n" , + errno,strerror (errno)); + + rc =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 =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);
Quoted 8 lines
tools/tests/mem-claim/test-mem-claim.c line 102
Quoted 6 lines
tools/tests/mem-claim/test-mem-claim.c new file +6
-0+ +/* +* Set a claim for 4M. This should be the only claim in the system, and +* show up globally. +*/ + rc =xc_domain_claim_pages (xch, domid,4 *1024 *1024 /4096 );You can use MB(4) (macro is in
tools/common-macros.h) I think for clarity? Same below with MB(2).
I'll wrap it as MB_TO_PAGES() to hide the divide by 4k.
~Andrew