Skip to contentSkip to conversation

numa: add unit testing plus fix regression

PATCH In review Roger Pau Monne 3 participants 14 messages Jun 3 – Jun 5

lore
Share & tools
lore
#0 lore raw

v2 cover numa: add unit testing plus fix regression

Hello,

The first 4 patches add some basic unit testing for the NUMA setup logic. The last patch expands the test cases and fixes an issue the new test-cases would otherwise trigger.

Only patch 4 is missing an Ack or Reviewed by.

Thanks, Roger.

Roger Pau Monne (5):
tools/bitops: adjust bitmap_or() interface to match hypervisor tools/macros: adjust ROUNDUP() interface to match hypervisor xen/numa: prepare NUMA setup code for unit testing tests/numa: add unit tests for NUMA setup logic xen/numa: fix setup of non-aligned memory affinity ranges

 tools/console/daemon/io.c                 |   2 +-
 tools/include/xen-tools/bitops.h          |   7 +-
 tools/include/xen-tools/common-macros.h   |   5 +-
 tools/libs/call/buffer.c                  |   3 +-
 tools/libs/foreignmemory/linux.c          |   2 +-
 tools/libs/gnttab/freebsd.c               |   2 +-
 tools/libs/gnttab/linux.c                 |   2 +-
 tools/libs/guest/xg_core.c                |   2 +-
 tools/libs/guest/xg_dom_arm.c             |   6 +-
 tools/libs/guest/xg_dom_x86.c             |   2 +-
 tools/libs/guest/xg_private.h             |   4 +-
 tools/libs/guest/xg_sr_common.c           |   6 +-
 tools/libs/guest/xg_sr_save.c             |   3 +-
 tools/libs/guest/xg_sr_stream_format.h    |   2 +-
 tools/libs/light/libxl_arm_acpi.c         |  24 +-
 tools/libs/light/libxl_create.c           |   2 +-
 tools/libs/light/libxl_sr_stream_format.h |   2 +-
 tools/libs/light/libxl_stream_read.c      |   2 +-
 tools/libs/light/libxl_stream_write.c     |   4 +-
 tools/misc/xen-mfndump.c                  |   2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c       |   2 +-
 tools/tests/Makefile                      |   1 +
 tools/tests/numa/.gitignore               |   2 +
 tools/tests/numa/Makefile                 |  47 ++++
 tools/tests/numa/harness.h                | 184 +++++++++++++++
 tools/tests/numa/test-numa.c              | 267 ++++++++++++++++++++++
 tools/xenstored/core.c                    |   4 +-
 tools/xenstored/domain.c                  |   9 +-
 tools/xenstored/watch.c                   |   2 +-
 xen/common/numa.c                         |  20 +-
 30 files changed, 568 insertions(+), 54 deletions(-)
 create mode 100644 tools/tests/numa/.gitignore
 create mode 100644 tools/tests/numa/Makefile
 create mode 100644 tools/tests/numa/harness.h
 create mode 100644 tools/tests/numa/test-numa.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/5 tools/bitops: adjust bitmap_or() interface to match hypervisor

Adjust the only toolstack caller to use the new interface. No functional change intended.

---
 tools/include/xen-tools/bitops.h | 7 ++++---
 tools/libs/guest/xg_sr_save.c    | 3 ++-
 2 files changed, 6 insertions(+), 4 deletions(-)
tools/include/xen-tools/bitops.h +4 -3
index 3b98fba6d74c..29587e89fa6d 100644
@@ -81,14 +81,15 @@ static inline int test_and_set_bit(unsigned long nr, void *addr)
     return oldbit;
 }
 
-static inline void bitmap_or(void *_dst, const void *_other,
+static inline void bitmap_or(void *_dst, const void *_src1, const void *_src2,
                              unsigned long nr_bits)
 {
     char *dst = _dst;
-    const char *other = _other;
+    const char *src1 = _src1, *src2 = _src2;
     unsigned long i;
+
     for ( i = 0; i < bitmap_size(nr_bits); ++i )
-        dst[i] |= other[i];
+        dst[i] = src1[i] | src2[i];
 }
 
 #endif  /* __XEN_TOOLS_BITOPS_H__ */
tools/libs/guest/xg_sr_save.c +2 -1
index 3b2c5222e429..fdbceab52e46 100644
@@ -668,7 +668,8 @@ static int suspend_and_send_dirty(struct xc_sr_context *ctx)
     else
         xc_set_progress_prefix(xch, "Checkpointed save");
 
-    bitmap_or(dirty_bitmap, ctx->save.deferred_pages, ctx->save.p2m_size);
+    bitmap_or(dirty_bitmap, dirty_bitmap, ctx->save.deferred_pages,
+              ctx->save.p2m_size);
 
     if ( !ctx->save.live && ctx->stream_type == XC_STREAM_COLO )
     {
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/5 tools/macros: adjust ROUNDUP() interface to match hypervisor

In reply to Roger Pau Monne

Adjust user-space callers to use the new interface. No functional change intended.

---
 tools/console/daemon/io.c                 |  2 +-
 tools/include/xen-tools/common-macros.h   |  4 +---
 tools/libs/call/buffer.c                  |  3 ++-
 tools/libs/foreignmemory/linux.c          |  2 +-
 tools/libs/gnttab/freebsd.c               |  2 +-
 tools/libs/gnttab/linux.c                 |  2 +-
 tools/libs/guest/xg_core.c                |  2 +-
 tools/libs/guest/xg_dom_arm.c             |  6 +++---
 tools/libs/guest/xg_dom_x86.c             |  2 +-
 tools/libs/guest/xg_private.h             |  4 ++--
 tools/libs/guest/xg_sr_common.c           |  6 +++---
 tools/libs/guest/xg_sr_stream_format.h    |  2 +-
 tools/libs/light/libxl_arm_acpi.c         | 24 +++++++++++------------
 tools/libs/light/libxl_create.c           |  2 +-
 tools/libs/light/libxl_sr_stream_format.h |  2 +-
 tools/libs/light/libxl_stream_read.c      |  2 +-
 tools/libs/light/libxl_stream_write.c     |  4 ++--
 tools/misc/xen-mfndump.c                  |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c       |  2 +-
 tools/xenstored/core.c                    |  4 ++--
 tools/xenstored/domain.c                  |  9 +++++----
 tools/xenstored/watch.c                   |  2 +-
 22 files changed, 45 insertions(+), 45 deletions(-)
tools/console/daemon/io.c +1 -1
index 43d4973c247c..b6c46d11dea7 100644
@@ -1233,7 +1233,7 @@ static int set_fds(int fd, short events)
 		/* Round up to 2^8 boundary, in practice this just
 		 * make newsize larger than current_array_size.
 		 */
-		newsize = ROUNDUP(nr_fds + 1, 8);
+		newsize = ROUNDUP(nr_fds + 1, 1U << 8);
 
 		new_fds = realloc(fds, sizeof(struct pollfd)*newsize);
 		if (!new_fds)
tools/include/xen-tools/common-macros.h +1 -3
index 9838a108aaea..9e2799178235 100644
@@ -68,9 +68,7 @@
     })
 #endif
 
-#ifndef ROUNDUP
-#define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
-#endif
+#define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))
 
 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
tools/libs/call/buffer.c +2 -1
index 2579b8c71986..155e4f9d43eb 100644
@@ -155,7 +155,8 @@ struct allocation_header {
 
 void *xencall_alloc_buffer(xencall_handle *xcall, size_t size)
 {
-    size_t actual_size = ROUNDUP(size + sizeof(struct allocation_header), PAGE_SHIFT);
+    size_t actual_size = ROUNDUP(size + sizeof(struct allocation_header),
+                                 PAGE_SIZE);
     int nr_pages = actual_size >> PAGE_SHIFT;
     struct allocation_header *hdr;
 
tools/libs/foreignmemory/linux.c +1 -1
index 12f959765abd..6d2f30cdf1b6 100644
@@ -198,7 +198,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
          */
         privcmd_mmapbatch_t ioctlx;
         xen_pfn_t *pfn;
-        unsigned int pfn_arr_size = ROUNDUP((num * sizeof(*pfn)), XC_PAGE_SHIFT);
+        unsigned int pfn_arr_size = ROUNDUP(num * sizeof(*pfn), XC_PAGE_SIZE);
         int os_page_size = sysconf(_SC_PAGESIZE);
 
         if ( pfn_arr_size <= os_page_size )
tools/libs/gnttab/freebsd.c +1 -1
index d69d928a165e..801274478217 100644
@@ -74,7 +74,7 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
     int domids_stride;
     unsigned int refs_size = ROUNDUP(count *
                                      sizeof(struct ioctl_gntdev_grant_ref),
-                                     XC_PAGE_SHIFT);
+                                     XC_PAGE_SIZE);
     int os_page_size = getpagesize();
 
     domids_stride = (flags & XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN) ? 0 : 1;
tools/libs/gnttab/linux.c +1 -1
index 7286c1d4fe83..829877e64baa 100644
@@ -101,7 +101,7 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
         map = alloca(map_size);
     else
     {
-        map_size = ROUNDUP(map_size, XC_PAGE_SHIFT);
+        map_size = ROUNDUP(map_size, XC_PAGE_SIZE);
         map = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
         if ( map == MAP_FAILED )
tools/libs/guest/xg_core.c +1 -1
index f83436d6cbf8..d3640f0ef891 100644
@@ -696,7 +696,7 @@ xc_domain_dumpcore_via_callback(xc_interface *xch,
     for ( i = 1; i < sheaders->num; i++ )
         sheaders->shdrs[i].sh_offset += fixup;
     offset += fixup;
-    dummy_len = ROUNDUP(offset, PAGE_SHIFT) - offset; /* padding length */
+    dummy_len = ROUNDUP(offset, PAGE_SIZE) - offset; /* padding length */
     offset += dummy_len;
 
     /* pages */
tools/libs/guest/xg_dom_arm.c +3 -3
index c8d091850622..739ec1c33837 100644
@@ -364,12 +364,12 @@ static int meminit(struct xc_dom_image *dom)
 
     /* Convenient */
     const uint64_t kernbase = dom->kernel_seg.vstart;
-    const uint64_t kernend = ROUNDUP(dom->kernel_seg.vend, 21/*2MB*/);
+    const uint64_t kernend = ROUNDUP(dom->kernel_seg.vend, MB(2));
     const uint64_t kernsize = kernend - kernbase;
     const uint64_t dtb_size = dom->devicetree_blob ?
-        ROUNDUP(dom->devicetree_size, XC_PAGE_SHIFT) : 0;
+        ROUNDUP(dom->devicetree_size, XC_PAGE_SIZE) : 0;
     const uint64_t ramdisk_size = dom->modules[0].blob ?
-        ROUNDUP(dom->modules[0].size, XC_PAGE_SHIFT) : 0;
+        ROUNDUP(dom->modules[0].size, XC_PAGE_SIZE) : 0;
     const uint64_t modsize = dtb_size + ramdisk_size;
     const uint64_t ram128mb = bankbase[0] + (128<<20);
 
tools/libs/guest/xg_dom_x86.c +1 -1
index 268936efe25b..9af83535944a 100644
@@ -678,7 +678,7 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
     {
         if ( dom->cmdline )
         {
-            dom->cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 3);
+            dom->cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 1U << 3);
             start_info_size += dom->cmdline_size;
         }
     }
tools/libs/guest/xg_private.h +2 -2
index 285229cf82a3..31a79bccf75e 100644
@@ -135,7 +135,7 @@ typedef uint64_t x86_pgentry_t;
 #define PAGE_SIZE_X86           (1UL << PAGE_SHIFT_X86)
 #define PAGE_MASK_X86           (~(PAGE_SIZE_X86-1))
 
-#define NRPAGES(x) (ROUNDUP(x, PAGE_SHIFT) >> PAGE_SHIFT)
+#define NRPAGES(x) (ROUNDUP(x, PAGE_SIZE) >> PAGE_SHIFT)
 
 static inline xen_pfn_t xc_pfn_to_mfn(xen_pfn_t pfn, xen_pfn_t *p2m,
                                       unsigned gwidth)
@@ -167,7 +167,7 @@ int pin_table(xc_interface *xch, unsigned int type, unsigned long mfn,
  */
 #define M2P_SHIFT       L2_PAGETABLE_SHIFT_PAE
 #define M2P_CHUNK_SIZE  (1 << M2P_SHIFT)
-#define M2P_SIZE(_m)    ROUNDUP(((_m) * sizeof(xen_pfn_t)), M2P_SHIFT)
+#define M2P_SIZE(_m)    ROUNDUP(((_m) * sizeof(xen_pfn_t)), M2P_CHUNK_SIZE)
 #define M2P_CHUNKS(_m)  (M2P_SIZE((_m)) >> M2P_SHIFT)
 
 #if defined(__x86_64__) || defined(__i386__)
tools/libs/guest/xg_sr_common.c +3 -3
index 7ccdc3b1f6aa..54dde924a7c0 100644
@@ -56,11 +56,11 @@ const char *rec_type_to_str(uint32_t type)
 int write_split_record(struct xc_sr_context *ctx, struct xc_sr_record *rec,
                        void *buf, size_t sz)
 {
-    static const char zeroes[(1u << REC_ALIGN_ORDER) - 1] = { 0 };
+    static const char zeroes[REC_ALIGN - 1] = { 0 };
 
     xc_interface *xch = ctx->xch;
     typeof(rec->length) combined_length = rec->length + sz;
-    size_t record_length = ROUNDUP(combined_length, REC_ALIGN_ORDER);
+    size_t record_length = ROUNDUP(combined_length, REC_ALIGN);
     struct iovec parts[] = {
         { &rec->type,       sizeof(rec->type) },
         { &combined_length, sizeof(combined_length) },
@@ -110,7 +110,7 @@ int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec)
         return -1;
     }
 
-    datasz = ROUNDUP(rhdr.length, REC_ALIGN_ORDER);
+    datasz = ROUNDUP(rhdr.length, REC_ALIGN);
 
     if ( datasz )
     {
tools/libs/guest/xg_sr_stream_format.h +1 -1
index 8a0da26f7543..4310f4311e65 100644
@@ -53,7 +53,7 @@ struct xc_sr_rhdr
 };
 
 /* All records must be aligned up to an 8 octet boundary */
-#define REC_ALIGN_ORDER               (3U)
+#define REC_ALIGN                     (1U << 3)
 /* Somewhat arbitrary - 128MB */
 #define REC_LENGTH_MAX                (128U << 20)
 
tools/libs/light/libxl_arm_acpi.c +12 -12
index ba874c3d3224..ac8165de15b6 100644
@@ -107,12 +107,12 @@ int libxl__get_acpi_size(libxl__gc *gc,
     if (rc < 0)
         goto out;
 
-    *out = ROUNDUP(size, 3) +
-           ROUNDUP(sizeof(struct acpi_table_rsdp), 3) +
-           ROUNDUP(sizeof(struct acpi_table_xsdt), 3) +
-           ROUNDUP(sizeof(struct acpi_table_gtdt), 3) +
-           ROUNDUP(sizeof(struct acpi_table_fadt), 3) +
-           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 3);
+    *out = ROUNDUP(size, 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_rsdp), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_xsdt), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_gtdt), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_fadt), 1U << 3) +
+           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 1U << 3);
 
 out:
     return rc;
@@ -128,7 +128,7 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
 
     acpitables[RSDP].addr = GUEST_ACPI_BASE;
     acpitables[RSDP].size = sizeof(struct acpi_table_rsdp);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 1U << 3);
 
     acpitables[XSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     /*
@@ -137,11 +137,11 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
      */
     acpitables[XSDT].size = sizeof(struct acpi_table_xsdt) +
                             sizeof(uint64_t) * 2;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 1U << 3);
 
     acpitables[GTDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[GTDT].size = sizeof(struct acpi_table_gtdt);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 1U << 3);
 
     acpitables[MADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
 
@@ -150,15 +150,15 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
         goto out;
 
     acpitables[MADT].size = size;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 1U << 3);
 
     acpitables[FADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[FADT].size = sizeof(struct acpi_table_fadt);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 1U << 3);
 
     acpitables[DSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[DSDT].size = dsdt_anycpu_arm_len;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 1U << 3);
 
     assert(dom->acpi_modules[0].length <= GUEST_ACPI_SIZE);
     dom->acpi_modules[0].data = libxl__zalloc(gc, dom->acpi_modules[0].length);
tools/libs/light/libxl_create.c +1 -1
index 6fd62d140389..a8b0c8c50070 100644
@@ -600,7 +600,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config,
                 .opts = 0, /* .opts will be set below */
                 .nr = b_info->altp2m_count,
             },
-            .vmtrace_size = ROUNDUP(b_info->vmtrace_buf_kb << 10, XC_PAGE_SHIFT),
+            .vmtrace_size = ROUNDUP(b_info->vmtrace_buf_kb << 10, XC_PAGE_SIZE),
             .cpupool_id = info->poolid,
         };
 
tools/libs/light/libxl_sr_stream_format.h +1 -1
index f8f4723c2e91..b02c954a388e 100644
@@ -29,7 +29,7 @@ typedef struct libxl__sr_rec_hdr
 } libxl__sr_rec_hdr;
 
 /* All records must be aligned up to an 8 octet boundary */
-#define REC_ALIGN_ORDER              3U
+#define REC_ALIGN                    (1U << 3)
 
 #define REC_TYPE_END                    0x00000000U
 #define REC_TYPE_LIBXC_CONTEXT          0x00000001U
tools/libs/light/libxl_stream_read.c +1 -1
index e64e8f0eadec..99c7607b6c54 100644
@@ -511,7 +511,7 @@ static void record_header_done(libxl__egc *egc,
         return;
     }
 
-    size_t bytes_to_read = ROUNDUP(rec->hdr.length, REC_ALIGN_ORDER);
+    size_t bytes_to_read = ROUNDUP(rec->hdr.length, REC_ALIGN);
     rec->body = libxl__malloc(NOGC, bytes_to_read);
 
     rc = setup_read(stream, "record body",
tools/libs/light/libxl_stream_write.c +2 -2
index 98d44597a732..9ea64369352f 100644
@@ -119,7 +119,7 @@ static void setup_generic_write(libxl__egc *egc,
                                 void *body,
                                 sws_record_done_cb cb)
 {
-    static const uint8_t zero_padding[1U << REC_ALIGN_ORDER] = { 0 };
+    static const uint8_t zero_padding[REC_ALIGN] = { 0 };
 
     libxl__datacopier_state *dc = &stream->dc;
     int rc;
@@ -136,7 +136,7 @@ static void setup_generic_write(libxl__egc *egc,
         return;
     }
 
-    size_t padsz = ROUNDUP(hdr->length, REC_ALIGN_ORDER) - hdr->length;
+    size_t padsz = ROUNDUP(hdr->length, REC_ALIGN) - hdr->length;
     uint32_t length = hdr->length;
 
     /* Insert header */
tools/misc/xen-mfndump.c +1 -1
index 28687afbf01a..99a0b1d3b5c0 100644
@@ -10,7 +10,7 @@
 
 #include <xen-tools/common-macros.h>
 
-#define M2P_SIZE(_m)    ROUNDUP(((_m) * sizeof(xen_pfn_t)), 21)
+#define M2P_SIZE(_m)    ROUNDUP(((_m) * sizeof(xen_pfn_t)), MB(2))
 #define is_mapped(pfn_type) (!((pfn_type) & 0x80000000UL))
 
 #define ERROR(msg, args...) fprintf(stderr, msg, ## args)
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index c55f73b265b2..7f6381cdd2fe 100644
@@ -221,7 +221,7 @@ CAMLprim value stub_xc_domain_create(value xch_val, value wanted_domid, value co
 	if ( altp2m_nr != (uint16_t)altp2m_nr )
 		caml_invalid_argument("altp2m_count");
 
-	vmtrace_size = ROUNDUP(vmtrace_size << 10, XC_PAGE_SHIFT);
+	vmtrace_size = ROUNDUP(vmtrace_size << 10, XC_PAGE_SIZE);
 	if ( vmtrace_size != (uint32_t)vmtrace_size )
 		caml_invalid_argument("vmtrace_buf_kb");
 
tools/xenstored/core.c +2 -2
index d6d462b7bc82..86c86b3e9a77 100644
@@ -466,7 +466,7 @@ int set_fd(int fd, short events)
 		/* Round up to 2^8 boundary, in practice this just
 		 * make newsize larger than current_array_size.
 		 */
-		newsize = ROUNDUP(nr_fds + 1, 8);
+		newsize = ROUNDUP(nr_fds + 1, 1U << 8);
 
 		new_fds = realloc(poll_fds, sizeof(struct pollfd)*newsize);
 		if (!new_fds)
@@ -3067,7 +3067,7 @@ static int dump_state_node(const void *ctx, struct connection *conn,
 	head.length += node->hdr.num_perms * sizeof(*sn.perms);
 	head.length += pathlen;
 	head.length += node->hdr.datalen;
-	head.length = ROUNDUP(head.length, 3);
+	head.length = ROUNDUP(head.length, 1U << 3);
 
 	if (fwrite(&head, sizeof(head), 1, fp) != 1)
 		return dump_state_node_err(data, "Dump node head error");
tools/xenstored/domain.c +5 -4
index 2db452144dd4..a880ff678ef9 100644
@@ -2159,7 +2159,7 @@ const char *dump_state_connections(FILE *fp)
 		if (ret)
 			return ret;
 		head.length += sc.data_in_len + sc.data_out_len;
-		head.length = ROUNDUP(head.length, 3);
+		head.length = ROUNDUP(head.length, 1U << 3);
 		if (c->domain) {
 			sc.fields |= XS_STATE_CONN_FIELDS_UNIQ_ID;
 			head.length += sizeof(uint64_t);
@@ -2232,7 +2232,8 @@ void read_state_connection(const void *ctx, const void *state)
 		unsigned long off;
 
 		off = sizeof(*sc) + sc->data_in_len + sc->data_out_len;
-		domain->unique_id = *(uint64_t *)(state + ROUNDUP(off, 3));
+		domain->unique_id =
+		    *(uint64_t *)(state + ROUNDUP(off, 1U << 3));
 	}
 }
 
@@ -2308,7 +2309,7 @@ static int dump_state_domain(const void *k, void *v, void *arg)
 	n_quota = get_quota_size(domain->acc, &rec_len);
 	rec_len += n_quota * sizeof(sd->quota_val[0]);
 	rec_len += sizeof(*sd);
-	rec_len = ROUNDUP(rec_len, 3);
+	rec_len = ROUNDUP(rec_len, 1U << 3);
 
 	record = talloc_size(NULL, rec_len + sizeof(*head));
 	if (!record)
@@ -2372,7 +2373,7 @@ const char *dump_state_glb_quota(FILE *fp)
 	n_quota = get_quota_size(quotas, &rec_len);
 	rec_len += n_quota * sizeof(glb->quota_val[0]);
 	rec_len += sizeof(*glb);
-	rec_len = ROUNDUP(rec_len, 3);
+	rec_len = ROUNDUP(rec_len, 1U << 3);
 
 	record = talloc_size(NULL, rec_len + sizeof(*head));
 	if (!record)
tools/xenstored/watch.c +1 -1
index a9a06e9e4816..309c5bb66bef 100644
@@ -349,7 +349,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		}
 
 		head.length += path_len + token_len;
-		head.length = ROUNDUP(head.length, 3);
+		head.length = ROUNDUP(head.length, 1U << 3);
 		if (fwrite(&head, sizeof(head), 1, fp) != 1)
 			return "Dump watch state error";
 
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH for-4.22 v2 2/5] tools/macros: adjust ROUNDUP() interface to match hypervisor

Quoted 5 lines · Roger Pau Monne

Adjust user-space callers to use the new interface. No functional change intended.

Yeah, libxl's choice to use order was always bizarre...  I'm glad we're getting rid of this.

tools/libs/guest/xg_dom_x86.c static int alloc_magic_pages_hvm(struct xc_dom_image *dom) · line 684

Quoted 13 lines
tools/libs/guest/xg_dom_x86.c +1 -1
index 268936efe25b..9af83535944a 100644
@@ -678,7 +678,7 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
     {
         if ( dom->cmdline )
         {
-            dom->cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 3);
+            dom->cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 1U << 3);
             start_info_size += dom->cmdline_size;
         }
     }

I think this would be better as a literal 8.

tools/libs/guest/xg_sr_common.c const char *rec_type_to_str(uint32_t type) · line 59

Quoted 10 lines
tools/libs/guest/xg_sr_common.c +1 -1
index 7ccdc3b1f6aa..54dde924a7c0 100644
@@ -56,11 +56,11 @@ const char *rec_type_to_str(uint32_t type)
 int write_split_record(struct xc_sr_context *ctx, struct xc_sr_record *rec,
                        void *buf, size_t sz)
 {
-    static const char zeroes[(1u << REC_ALIGN_ORDER) - 1] = { 0 };
+    static const char zeroes[REC_ALIGN - 1] = { 0 };

= {} as you're editing the line.

I have no idea why this is 7 in libxg, but 8 in libxl, but dropping the -1 here is probably wise.

tools/libs/guest/xg_sr_stream_format.h struct xc_sr_rhdr · line 56

Quoted 10 lines
tools/libs/guest/xg_sr_stream_format.h +1 -1
index 8a0da26f7543..4310f4311e65 100644
@@ -53,7 +53,7 @@ struct xc_sr_rhdr
 };
 
 /* All records must be aligned up to an 8 octet boundary */
-#define REC_ALIGN_ORDER               (3U)
+#define REC_ALIGN                     (1U << 3)

This really does want to be 8 rather than a shift.

Quoted 55 lines
tools/libs/guest/xg_sr_stream_format.h +0 -0
 /* Somewhat arbitrary - 128MB */
 #define REC_LENGTH_MAX                (128U << 20)
 
tools/libs/light/libxl_arm_acpi.c +10 -10
index ba874c3d3224..ac8165de15b6 100644
@@ -107,12 +107,12 @@ int libxl__get_acpi_size(libxl__gc *gc,
     if (rc < 0)
         goto out;
 
-    *out = ROUNDUP(size, 3) +
-           ROUNDUP(sizeof(struct acpi_table_rsdp), 3) +
-           ROUNDUP(sizeof(struct acpi_table_xsdt), 3) +
-           ROUNDUP(sizeof(struct acpi_table_gtdt), 3) +
-           ROUNDUP(sizeof(struct acpi_table_fadt), 3) +
-           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 3);
+    *out = ROUNDUP(size, 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_rsdp), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_xsdt), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_gtdt), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_fadt), 1U << 3) +
+           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 1U << 3);
 
 out:
     return rc;
@@ -128,7 +128,7 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
 
     acpitables[RSDP].addr = GUEST_ACPI_BASE;
     acpitables[RSDP].size = sizeof(struct acpi_table_rsdp);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 1U << 3);
 
     acpitables[XSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     /*
@@ -137,11 +137,11 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
      */
     acpitables[XSDT].size = sizeof(struct acpi_table_xsdt) +
                             sizeof(uint64_t) * 2;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 1U << 3);
 
     acpitables[GTDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[GTDT].size = sizeof(struct acpi_table_gtdt);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 1U << 3);
 
     acpitables[MADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
 
@@ -150,15 +150,15 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
         goto out;
 
     acpitables[MADT].size = size;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 1U << 3);

tools/libs/light/libxl_arm_acpi.c static int libxl__allocate_acpi_tables(libxl__gc *gc, · line 161

Quoted 10 lines
tools/libs/light/libxl_arm_acpi.c +2 -2
 
     acpitables[FADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[FADT].size = sizeof(struct acpi_table_fadt);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 1U << 3);
 
     acpitables[DSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[DSDT].size = dsdt_anycpu_arm_len;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 1U << 3);

And all of these.

Quoted 18 lines
tools/libs/light/libxl_sr_stream_format.h +1 -1
index f8f4723c2e91..b02c954a388e 100644
@@ -29,7 +29,7 @@ typedef struct libxl__sr_rec_hdr
 } libxl__sr_rec_hdr;
 
 /* All records must be aligned up to an 8 octet boundary */
-#define REC_ALIGN_ORDER              3U
+#define REC_ALIGN                    (1U << 3)
 
 #define REC_TYPE_END                    0x00000000U
 #define REC_TYPE_LIBXC_CONTEXT          0x00000001U
tools/libs/light/libxl_stream_write.c +0 -0
index 98d44597a732..9ea64369352f 100644

tools/libs/light/libxl_stream_write.c static void setup_generic_write(libxl__egc *egc, · line 122

Quoted 5 lines
tools/libs/light/libxl_stream_write.c +1 -1
@@ -119,7 +119,7 @@ static void setup_generic_write(libxl__egc *egc,
                                 void *body,
                                 sws_record_done_cb cb)
 {
-    static const uint8_t zero_padding[1U << REC_ALIGN_ORDER] = { 0 };
+    static const uint8_t zero_padding[REC_ALIGN] = { 0 };

These want the same adjustments as the libxg side.

Quoted 59 lines
tools/xenstored/core.c +1 -1
index d6d462b7bc82..86c86b3e9a77 100644
@@ -3067,7 +3067,7 @@ static int dump_state_node(const void *ctx, struct connection *conn,
 	head.length += node->hdr.num_perms * sizeof(*sn.perms);
 	head.length += pathlen;
 	head.length += node->hdr.datalen;
-	head.length = ROUNDUP(head.length, 3);
+	head.length = ROUNDUP(head.length, 1U << 3);
 
 	if (fwrite(&head, sizeof(head), 1, fp) != 1)
 		return dump_state_node_err(data, "Dump node head error");
tools/xenstored/domain.c +5 -4
index 2db452144dd4..a880ff678ef9 100644
@@ -2159,7 +2159,7 @@ const char *dump_state_connections(FILE *fp)
 		if (ret)
 			return ret;
 		head.length += sc.data_in_len + sc.data_out_len;
-		head.length = ROUNDUP(head.length, 3);
+		head.length = ROUNDUP(head.length, 1U << 3);
 		if (c->domain) {
 			sc.fields |= XS_STATE_CONN_FIELDS_UNIQ_ID;
 			head.length += sizeof(uint64_t);
@@ -2232,7 +2232,8 @@ void read_state_connection(const void *ctx, const void *state)
 		unsigned long off;
 
 		off = sizeof(*sc) + sc->data_in_len + sc->data_out_len;
-		domain->unique_id = *(uint64_t *)(state + ROUNDUP(off, 3));
+		domain->unique_id =
+		    *(uint64_t *)(state + ROUNDUP(off, 1U << 3));
 	}
 }
 
@@ -2308,7 +2309,7 @@ static int dump_state_domain(const void *k, void *v, void *arg)
 	n_quota = get_quota_size(domain->acc, &rec_len);
 	rec_len += n_quota * sizeof(sd->quota_val[0]);
 	rec_len += sizeof(*sd);
-	rec_len = ROUNDUP(rec_len, 3);
+	rec_len = ROUNDUP(rec_len, 1U << 3);
 
 	record = talloc_size(NULL, rec_len + sizeof(*head));
 	if (!record)
@@ -2372,7 +2373,7 @@ const char *dump_state_glb_quota(FILE *fp)
 	n_quota = get_quota_size(quotas, &rec_len);
 	rec_len += n_quota * sizeof(glb->quota_val[0]);
 	rec_len += sizeof(*glb);
-	rec_len = ROUNDUP(rec_len, 3);
+	rec_len = ROUNDUP(rec_len, 1U << 3);
 
 	record = talloc_size(NULL, rec_len + sizeof(*head));
 	if (!record)
tools/xenstored/watch.c +0 -0
index a9a06e9e4816..309c5bb66bef 100644

tools/xenstored/watch.c const char *dump_state_watches(FILE *fp, struct connection *conn, · line 355

Quoted 8 lines
tools/xenstored/watch.c +1 -1
@@ -349,7 +349,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		}
 
 		head.length += path_len + token_len;
-		head.length = ROUNDUP(head.length, 3);
+		head.length = ROUNDUP(head.length, 1U << 3);
 		if (fwrite(&head, sizeof(head), 1, fp) != 1)
 			return "Dump watch state error";
 

And these want to be 8 as well I think.

Definitely with the migration formation adjustments, and preferably with the others too, Reviewed-by: Andrew Cooper <[email protected]>

~Andrew

#4 lore raw

Re: [PATCH for-4.22 v2 2/5] tools/macros: adjust ROUNDUP() interface to match hypervisor

Quoted 39 lines · Andrew Cooper
Quoted 5 lines · Roger Pau Monne

Adjust user-space callers to use the new interface. No functional change intended.

Yeah, libxl's choice to use order was always bizarre...  I'm glad we're getting rid of this.

tools/libs/guest/xg_dom_x86.c static int alloc_magic_pages_hvm(struct xc_dom_image *dom) · line 684

Quoted 13 lines
tools/libs/guest/xg_dom_x86.c +1 -1
index 268936efe25b..9af83535944a 100644
@@ -678,7 +678,7 @@ static int alloc_magic_pages_hvm(struct xc_dom_image *dom)
     {
         if ( dom->cmdline )
         {
-            dom->cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 3);
+            dom->cmdline_size = ROUNDUP(strlen(dom->cmdline) + 1, 1U << 3);
             start_info_size += dom->cmdline_size;
         }
     }

I think this would be better as a literal 8.

tools/libs/guest/xg_sr_common.c const char *rec_type_to_str(uint32_t type) · line 59

Quoted 10 lines
tools/libs/guest/xg_sr_common.c +1 -1
index 7ccdc3b1f6aa..54dde924a7c0 100644
@@ -56,11 +56,11 @@ const char *rec_type_to_str(uint32_t type)
 int write_split_record(struct xc_sr_context *ctx, struct xc_sr_record *rec,
                        void *buf, size_t sz)
 {
-    static const char zeroes[(1u << REC_ALIGN_ORDER) - 1] = { 0 };
+    static const char zeroes[REC_ALIGN - 1] = { 0 };

= {} as you're editing the line.

Quoted 2 lines

I have no idea why this is 7 in libxg, but 8 in libxl, but dropping the -1 here is probably wise.

I've noticed this oddity, but was cautious with doing any change there, given the code freeze status. I can do the adjustment.

Quoted 177 lines

tools/libs/guest/xg_sr_stream_format.h struct xc_sr_rhdr · line 56

Quoted 10 lines
tools/libs/guest/xg_sr_stream_format.h +1 -1
index 8a0da26f7543..4310f4311e65 100644
@@ -53,7 +53,7 @@ struct xc_sr_rhdr
 };
 
 /* All records must be aligned up to an 8 octet boundary */
-#define REC_ALIGN_ORDER               (3U)
+#define REC_ALIGN                     (1U << 3)

This really does want to be 8 rather than a shift.

Quoted 55 lines
tools/libs/guest/xg_sr_stream_format.h +0 -0
 /* Somewhat arbitrary - 128MB */
 #define REC_LENGTH_MAX                (128U << 20)
 
tools/libs/light/libxl_arm_acpi.c +10 -10
index ba874c3d3224..ac8165de15b6 100644
@@ -107,12 +107,12 @@ int libxl__get_acpi_size(libxl__gc *gc,
     if (rc < 0)
         goto out;
 
-    *out = ROUNDUP(size, 3) +
-           ROUNDUP(sizeof(struct acpi_table_rsdp), 3) +
-           ROUNDUP(sizeof(struct acpi_table_xsdt), 3) +
-           ROUNDUP(sizeof(struct acpi_table_gtdt), 3) +
-           ROUNDUP(sizeof(struct acpi_table_fadt), 3) +
-           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 3);
+    *out = ROUNDUP(size, 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_rsdp), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_xsdt), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_gtdt), 1U << 3) +
+           ROUNDUP(sizeof(struct acpi_table_fadt), 1U << 3) +
+           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 1U << 3);
 
 out:
     return rc;
@@ -128,7 +128,7 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
 
     acpitables[RSDP].addr = GUEST_ACPI_BASE;
     acpitables[RSDP].size = sizeof(struct acpi_table_rsdp);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 1U << 3);
 
     acpitables[XSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     /*
@@ -137,11 +137,11 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
      */
     acpitables[XSDT].size = sizeof(struct acpi_table_xsdt) +
                             sizeof(uint64_t) * 2;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 1U << 3);
 
     acpitables[GTDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[GTDT].size = sizeof(struct acpi_table_gtdt);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 1U << 3);
 
     acpitables[MADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
 
@@ -150,15 +150,15 @@ static int libxl__allocate_acpi_tables(libxl__gc *gc,
         goto out;
 
     acpitables[MADT].size = size;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 1U << 3);

tools/libs/light/libxl_arm_acpi.c static int libxl__allocate_acpi_tables(libxl__gc *gc, · line 161

Quoted 10 lines
tools/libs/light/libxl_arm_acpi.c +2 -2
 
     acpitables[FADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[FADT].size = sizeof(struct acpi_table_fadt);
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 1U << 3);
 
     acpitables[DSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
     acpitables[DSDT].size = dsdt_anycpu_arm_len;
-    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 3);
+    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 1U << 3);

And all of these.

Quoted 18 lines
tools/libs/light/libxl_sr_stream_format.h +1 -1
index f8f4723c2e91..b02c954a388e 100644
@@ -29,7 +29,7 @@ typedef struct libxl__sr_rec_hdr
 } libxl__sr_rec_hdr;
 
 /* All records must be aligned up to an 8 octet boundary */
-#define REC_ALIGN_ORDER              3U
+#define REC_ALIGN                    (1U << 3)
 
 #define REC_TYPE_END                    0x00000000U
 #define REC_TYPE_LIBXC_CONTEXT          0x00000001U
tools/libs/light/libxl_stream_write.c +0 -0
index 98d44597a732..9ea64369352f 100644

tools/libs/light/libxl_stream_write.c static void setup_generic_write(libxl__egc *egc, · line 122

Quoted 5 lines
tools/libs/light/libxl_stream_write.c +1 -1
@@ -119,7 +119,7 @@ static void setup_generic_write(libxl__egc *egc,
                                 void *body,
                                 sws_record_done_cb cb)
 {
-    static const uint8_t zero_padding[1U << REC_ALIGN_ORDER] = { 0 };
+    static const uint8_t zero_padding[REC_ALIGN] = { 0 };

These want the same adjustments as the libxg side.

Quoted 59 lines
tools/xenstored/core.c +1 -1
index d6d462b7bc82..86c86b3e9a77 100644
@@ -3067,7 +3067,7 @@ static int dump_state_node(const void *ctx, struct connection *conn,
 	head.length += node->hdr.num_perms * sizeof(*sn.perms);
 	head.length += pathlen;
 	head.length += node->hdr.datalen;
-	head.length = ROUNDUP(head.length, 3);
+	head.length = ROUNDUP(head.length, 1U << 3);
 
 	if (fwrite(&head, sizeof(head), 1, fp) != 1)
 		return dump_state_node_err(data, "Dump node head error");
tools/xenstored/domain.c +5 -4
index 2db452144dd4..a880ff678ef9 100644
@@ -2159,7 +2159,7 @@ const char *dump_state_connections(FILE *fp)
 		if (ret)
 			return ret;
 		head.length += sc.data_in_len + sc.data_out_len;
-		head.length = ROUNDUP(head.length, 3);
+		head.length = ROUNDUP(head.length, 1U << 3);
 		if (c->domain) {
 			sc.fields |= XS_STATE_CONN_FIELDS_UNIQ_ID;
 			head.length += sizeof(uint64_t);
@@ -2232,7 +2232,8 @@ void read_state_connection(const void *ctx, const void *state)
 		unsigned long off;
 
 		off = sizeof(*sc) + sc->data_in_len + sc->data_out_len;
-		domain->unique_id = *(uint64_t *)(state + ROUNDUP(off, 3));
+		domain->unique_id =
+		    *(uint64_t *)(state + ROUNDUP(off, 1U << 3));
 	}
 }
 
@@ -2308,7 +2309,7 @@ static int dump_state_domain(const void *k, void *v, void *arg)
 	n_quota = get_quota_size(domain->acc, &rec_len);
 	rec_len += n_quota * sizeof(sd->quota_val[0]);
 	rec_len += sizeof(*sd);
-	rec_len = ROUNDUP(rec_len, 3);
+	rec_len = ROUNDUP(rec_len, 1U << 3);
 
 	record = talloc_size(NULL, rec_len + sizeof(*head));
 	if (!record)
@@ -2372,7 +2373,7 @@ const char *dump_state_glb_quota(FILE *fp)
 	n_quota = get_quota_size(quotas, &rec_len);
 	rec_len += n_quota * sizeof(glb->quota_val[0]);
 	rec_len += sizeof(*glb);
-	rec_len = ROUNDUP(rec_len, 3);
+	rec_len = ROUNDUP(rec_len, 1U << 3);
 
 	record = talloc_size(NULL, rec_len + sizeof(*head));
 	if (!record)
tools/xenstored/watch.c +0 -0
index a9a06e9e4816..309c5bb66bef 100644

tools/xenstored/watch.c const char *dump_state_watches(FILE *fp, struct connection *conn, · line 355

Quoted 8 lines
tools/xenstored/watch.c +1 -1
@@ -349,7 +349,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		}
 
 		head.length += path_len + token_len;
-		head.length = ROUNDUP(head.length, 3);
+		head.length = ROUNDUP(head.length, 1U << 3);
 		if (fwrite(&head, sizeof(head), 1, fp) != 1)
 			return "Dump watch state error";
 

And these want to be 8 as well I think.

Quoted 2 lines

Definitely with the migration formation adjustments, and preferably with the others too, Reviewed-by: Andrew Cooper <[email protected]>

Thanks, I've done all the adjustments.

Roger.

#5 lore raw

PATCH v2 3/5 xen/numa: prepare NUMA setup code for unit testing

In reply to Roger Pau Monne

Introduce __XEN__ guards to differentiate between hypervisor vs unit test builds. Also move numa_set_node() so it's outside the __XEN__ guards. No functional change intended.

---
 xen/common/numa.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
xen/common/numa.c +9 -5
index ad75955a1622..8544a1598218 100644
@@ -4,6 +4,7 @@
  * Adapted for Xen: Ryan Harper <[email protected]>
  */
 
+#ifdef __XEN__
 #include <xen/init.h>
 #include <xen/keyhandler.h>
 #include <xen/mm.h>
@@ -13,6 +14,7 @@
 #include <xen/pfn.h>
 #include <xen/sched.h>
 #include <xen/softirq.h>
+#endif /* __XEN__ */
 
 static nodemask_t __initdata processor_nodes_parsed;
 static nodemask_t __initdata memory_nodes_parsed;
@@ -561,6 +563,12 @@ void __init numa_init_array(void)
     }
 }
 
+void numa_set_node(unsigned int cpu, nodeid_t node)
+{
+    cpu_to_node[cpu] = node;
+}
+
+#ifdef __XEN__
 #ifdef CONFIG_NUMA_EMU
 static unsigned int __initdata numa_fake;
 
@@ -661,11 +669,6 @@ void numa_add_cpu(unsigned int cpu)
     cpumask_set_cpu(cpu, &node_to_cpumask[cpu_to_node(cpu)]);
 }
 
-void numa_set_node(unsigned int cpu, nodeid_t node)
-{
-    cpu_to_node[cpu] = node;
-}
-
 /* [numa=off] */
 static int __init cf_check numa_setup(const char *opt)
 {
@@ -830,3 +833,4 @@ static int __init cf_check register_numa_trigger(void)
     return 0;
 }
 __initcall(register_numa_trigger);
+#endif /* __XEN__ */
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 4/5 tests/numa: add unit tests for NUMA setup logic

In reply to Roger Pau Monne

NUMA setup, like PDX, requires certain amount of logic to configure the internal structures and parameters for NUMA operation. Introduce some very basic testing that allows building and testing NUMA setup logic in as a user-space unit test. This allows feeding synthetic memory affinity and map to the logic, allowing to reproduce bugs that would otherwise need access to real systems with such a configuration.

For the time being introduce a single test case, based on a known working NUMA setup for an AMD Turin system. Also the testing after setup is currently limited to ensuring the start and end RAM region addresses fall into a correctly setup memory block.

---
Changes since v1:
- Remove unneeded !!.
- Remove redundant parentheses around macro parameters.

---
 tools/tests/Makefile         |   1 +
 tools/tests/numa/.gitignore  |   2 +
 tools/tests/numa/Makefile    |  47 ++++++++
 tools/tests/numa/harness.h   | 184 +++++++++++++++++++++++++++++
 tools/tests/numa/test-numa.c | 222 +++++++++++++++++++++++++++++++++++
 5 files changed, 456 insertions(+)
 create mode 100644 tools/tests/numa/.gitignore
 create mode 100644 tools/tests/numa/Makefile
 create mode 100644 tools/tests/numa/harness.h
 create mode 100644 tools/tests/numa/test-numa.c
tools/tests/Makefile +1 -0
index 6477a4386dda..fc0ed8091510 100644
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += domid
 SUBDIRS-y += mem-claim
+SUBDIRS-y += numa
 SUBDIRS-y += paging-mempool
 SUBDIRS-y += pdx
 SUBDIRS-y += rangeset
tools/tests/numa/.gitignore new file +2 -0
new file mode 100644
index 000000000000..0710a767f400
@@ -0,0 +1,2 @@
+/numa.h
+/test-numa
tools/tests/numa/Makefile new file +47 -0

47 lines not on this page · raw message

tools/tests/numa/harness.h new file +184 -0

184 lines not on this page · raw message

tools/tests/numa/test-numa.c new file +222 -0

222 lines not on this page · raw message

Signature · 1 line
2.53.0
Reviewed-by on v2 4/5
#7 lore raw

Re: [PATCH for-4.22 v2 4/5] tests/numa: add unit tests for NUMA setup logic

tools/tests/numa/Makefile line 35

Quoted 9 lines · Roger Pau Monne
tools/tests/numa/Makefile new file +4 -0
new file mode 100644
index 000000000000..5235f9d6297f
+
+.PHONY: uninstall
+uninstall:
+	$(RM) -- $(patsubst %,$(DESTDIR)$(LIBEXEC)/tests/%,$(TARGETS))

There's a simpler way to write this, with

    $(addprefix $(DESTDIR)$(LIBEXEC)/tests/,$(TARGETS))

But that's ok to.

tools/tests/numa/Makefile line 45

Quoted 10 lines
tools/tests/numa/Makefile new file +10 -0
+
+numa.h: $(XEN_ROOT)/xen/include/xen/numa.h
+	sed -e '/^#[[:space:]]*include/d' <$< >$@
+
+CFLAGS += -D__XEN_TOOLS__
+CFLAGS += $(APPEND_CFLAGS)
+CFLAGS += $(CFLAGS_xeninclude)
+
+test-numa: test-numa.c numa.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_$*.o) -o $@ $< $(APPEND_CFLAGS)

$* should be undefined here.
So we have $(CFLAGS_.o), but that variable doesn't exit either. You could remove $(CFLAGS_$*.o), it's not use here.

Also, $(APPEND_CFLAGS) is added twice, once via $(CFLAGS) and a second time on the command line. I think the one added to $(CFLAGS) should be removed.

tools/tests/numa/test-numa.c line 177

Quoted 8 lines
tools/tests/numa/test-numa.c new file +3 -0
new file mode 100644
index 000000000000..bced68d4d7f1
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].affinity) && tests[i].affinity[j].end;

Why do you test the value .end ? ARRAY_SIZE is likely enough as the test array is static. Same thing later, with the ram array.

Anyway, it's all look good enough to me:

Thanks,

--
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#8 lore raw

Re: [PATCH for-4.22 v2 4/5] tests/numa: add unit tests for NUMA setup logic

Quoted 12 lines · Anthony PERARD

tools/tests/numa/Makefile line 35

Quoted 9 lines · Roger Pau Monne
tools/tests/numa/Makefile new file +4 -0
new file mode 100644
index 000000000000..5235f9d6297f
+
+.PHONY: uninstall
+uninstall:
+	$(RM) -- $(patsubst %,$(DESTDIR)$(LIBEXEC)/tests/%,$(TARGETS))

There's a simpler way to write this, with

Quoted 2 lines
    $(addprefix $(DESTDIR)$(LIBEXEC)/tests/,$(TARGETS))

But that's ok to.

I've merely c&p what we had in other Makefiles, and didn't think much about the specific runes. I will leave that one alone if you don't mind, we could always do a sweep change of this pattern in tools/tests.

Quoted 6 lines
Quoted 6 lines
tools/tests/numa/Makefile new file +6 -0
+
+numa.h: $(XEN_ROOT)/xen/include/xen/numa.h
+	sed -e '/^#[[:space:]]*include/d' <$< >$@
+
+CFLAGS += -D__XEN_TOOLS__
+CFLAGS += $(APPEND_CFLAGS)
Quoted 8 lines

tools/tests/numa/Makefile line 45

Quoted 4 lines
tools/tests/numa/Makefile new file +4 -0
+CFLAGS += $(CFLAGS_xeninclude)
+
+test-numa: test-numa.c numa.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_$*.o) -o $@ $< $(APPEND_CFLAGS)

$* should be undefined here.
So we have $(CFLAGS_.o), but that variable doesn't exit either. You could remove $(CFLAGS_$*.o), it's not use here.

Ack.

Quoted 3 lines

Also, $(APPEND_CFLAGS) is added twice, once via $(CFLAGS) and a second time on the command line. I think the one added to $(CFLAGS) should be removed.

Done, will remove that from CLFAGS then.

Quoted 11 lines

tools/tests/numa/test-numa.c line 177

Quoted 8 lines
tools/tests/numa/test-numa.c new file +3 -0
new file mode 100644
index 000000000000..bced68d4d7f1
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].affinity) && tests[i].affinity[j].end;

Why do you test the value .end ? ARRAY_SIZE is likely enough as the test array is static. Same thing later, with the ram array.

To skip processing empty/uninitialized entries (ie: ones with start == end == 0).

Quoted 4 lines

Anyway, it's all look good enough to me:

Thanks, will do the above changes and do one last pre-push CI run.

Roger.

#9 lore raw

Re: [PATCH for-4.22 v2 4/5] tests/numa: add unit tests for NUMA setup logic

In reply to Roger Pau Monne
Quoted 6 lines · Roger Pau Monne
tools/tests/numa/harness.h new file +0 -0
new file mode 100644
index 000000000000..9eec77f31402

tools/tests/numa/harness.h line 9

Quoted 9 lines
tools/tests/numa/harness.h new file +9 -0
@@ -0,0 +1,184 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Unit tests for NUMA setup.
+ *
+ * Copyright (C) 2026 Cloud Software Group
+ */
+
+#ifndef _TEST_HARNESS_
+#define _TEST_HARNESS_

This is overly generic, and liable to break if anyone copies it.  Maybe NUMA_HARNESS, or WRAP_XEN_NUMA because ...

Looking below, how about naming it wrapped-xen-numa.h, so ...

tools/tests/numa/harness.h line 173

Quoted 9 lines

<snip>

tools/tests/numa/harness.h new file +7 -0
+
+static inline bool arch_numa_unavailable(void)
+{
+    return false;
+}
+
+#include "numa.h"

I presume this is the real xen/numa.h ?

Quoted 20 lines
tools/tests/numa/harness.h new file +11 -0
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/tests/numa/test-numa.c new file +3 -0
new file mode 100644
index 000000000000..bced68d4d7f1
@@ -0,0 +1,222 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Unit tests for NUMA setup.

tools/tests/numa/test-numa.c line 13

Quoted 10 lines
tools/tests/numa/test-numa.c new file +10 -0
+ *
+ * Copyright (C) 2026 Cloud Software Group
+ */
+
+#include "harness.h"
+
+static paddr_t mem_hotplug;
+unsigned int __read_mostly nr_cpu_ids = NR_CPUS;
+
+#include "../../xen/common/numa.c"

... this has

#include "wrapped-xen-numa.h"
#include "../../xen/common/numa.c"

which I think is clearer to follow.

tools/tests/numa/test-numa.c line 140

Quoted 9 lines

<snip>

tools/tests/numa/test-numa.c new file +7 -0
+int main(int argc, char **argv)
+{
+    static const struct {
+        struct mem_affinity affinity[MAX_RANGES];
+        struct mem_range ram[MAX_RANGES];
+    } tests[] = {
+        /* AMD Turin system. */

I'd suggest /* From an arbitrary AMD Turin system */

Just "AMD Turin system" feels a little as if all systems are like this, which is absolutely not the case.

Quoted 51 lines
tools/tests/numa/test-numa.c new file +51 -0
+        {
+            .affinity = {
+                { .nid = 0, .start = 0x00000000000ULL, .end = 0x0000009ffffULL },
+                { .nid = 0, .start = 0x000000c0000ULL, .end = 0x000afffffffULL },
+                { .nid = 0, .start = 0x00100000000ULL, .end = 0x0c04fffffffULL },
+                { .nid = 1, .start = 0x0c050000000ULL, .end = 0x0fc4fffffffULL },
+                { .nid = 1, .start = 0x10000000000ULL, .end = 0x183ffffffffULL },
+            },
+            .ram = {
+                { .start = 0x00000000000ULL, .end = 0x0000009ffffULL },
+                { .start = 0x00000100000ULL, .end = 0x0007590ffffULL },
+                { .start = 0x000759d1000ULL, .end = 0x00075a0ffffULL },
+                { .start = 0x00076000000ULL, .end = 0x00094c73fffULL },
+                { .start = 0x0009b5ff000ULL, .end = 0x0009fff9fffULL },
+                { .start = 0x0009ffff000ULL, .end = 0x0009fffffffULL },
+                { .start = 0x00100010000ULL, .end = 0x0fc4fffffffULL },
+                { .start = 0x10000000000ULL, .end = 0x183f7ffffffULL },
+                { .start = 0x183f8800000ULL, .end = 0x183faabffffULL },
+            },
+        },
+    };
+    int ret_code = EXIT_SUCCESS;
+
+    /* Dummy firmware interface provider name, use TST for TEST. */
+    numa_fw_nid_name = "TST";
+
+    for ( unsigned int i = 0 ; i < ARRAY_SIZE(tests); i++ )
+    {
+        paddr_t min = ~(paddr_t)0, max = 0;
+        unsigned int j;
+
+        numa_reset_state();
+
+        ram = tests[i].ram;
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].affinity) && tests[i].affinity[j].end;
+              j++ )
+        {
+            const struct mem_affinity *affinity = &tests[i].affinity[j];
+            paddr_t length = affinity->end - affinity->start + 1;
+
+            if ( !numa_update_node_memblks(affinity->nid, affinity->nid,
+                                           affinity->start, length, false) )
+            {
+                printf("Fail to add NID %u [%" PRIpaddr ", %" PRIpaddr "]\n",
+                        affinity->nid, affinity->start, affinity->end);
+                ret_code = EXIT_FAILURE;
+                continue;
+            }
+

tools/tests/numa/test-numa.c line 201

Quoted 10 lines
tools/tests/numa/test-numa.c new file +10 -0
+            min = min(min, affinity->start);
+            max = max(max, affinity->end);
+        }
+
+        if ( !numa_process_nodes(min, max + 1) )
+        {
+                printf("Unable to process nodes\n");
+                print_ranges(tests[i].affinity);
+                ret_code = EXIT_FAILURE;
+                continue;

This is mis-indented.  Best double check the whole file.

tools/tests/numa/test-numa.c line 212

Quoted 11 lines
tools/tests/numa/test-numa.c new file +11 -0
+        }
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].ram) && tests[i].ram[j].end;
+              j++ )
+            if ( !test_paddr(tests[i].ram[j].start) ||
+                 !test_paddr(tests[i].ram[j].end) )
+                ret_code = EXIT_FAILURE;
+    }
+
+    return ret_code;

This is fine for now, but we're going to have to consolidate the patterns eventually.

Do you have a Gitlab CI run with this passing?

~Andrew

#10 lore raw

Re: [PATCH for-4.22 v2 4/5] tests/numa: add unit tests for NUMA setup logic

Quoted 166 lines · Andrew Cooper
Quoted 6 lines · Roger Pau Monne
tools/tests/numa/harness.h new file +0 -0
new file mode 100644
index 000000000000..9eec77f31402

tools/tests/numa/harness.h line 9

Quoted 9 lines
tools/tests/numa/harness.h new file +9 -0
@@ -0,0 +1,184 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Unit tests for NUMA setup.
+ *
+ * Copyright (C) 2026 Cloud Software Group
+ */
+
+#ifndef _TEST_HARNESS_
+#define _TEST_HARNESS_

This is overly generic, and liable to break if anyone copies it.  Maybe NUMA_HARNESS, or WRAP_XEN_NUMA because ...

Looking below, how about naming it wrapped-xen-numa.h, so ...

tools/tests/numa/harness.h line 173

Quoted 9 lines

<snip>

tools/tests/numa/harness.h new file +7 -0
+
+static inline bool arch_numa_unavailable(void)
+{
+    return false;
+}
+
+#include "numa.h"

I presume this is the real xen/numa.h ?

Quoted 20 lines
tools/tests/numa/harness.h new file +11 -0
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/tests/numa/test-numa.c new file +3 -0
new file mode 100644
index 000000000000..bced68d4d7f1
@@ -0,0 +1,222 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Unit tests for NUMA setup.

tools/tests/numa/test-numa.c line 13

Quoted 10 lines
tools/tests/numa/test-numa.c new file +10 -0
+ *
+ * Copyright (C) 2026 Cloud Software Group
+ */
+
+#include "harness.h"
+
+static paddr_t mem_hotplug;
+unsigned int __read_mostly nr_cpu_ids = NR_CPUS;
+
+#include "../../xen/common/numa.c"

... this has

#include "wrapped-xen-numa.h"
#include "../../xen/common/numa.c"

which I think is clearer to follow.

tools/tests/numa/test-numa.c line 140

Quoted 9 lines

<snip>

tools/tests/numa/test-numa.c new file +7 -0
+int main(int argc, char **argv)
+{
+    static const struct {
+        struct mem_affinity affinity[MAX_RANGES];
+        struct mem_range ram[MAX_RANGES];
+    } tests[] = {
+        /* AMD Turin system. */

I'd suggest /* From an arbitrary AMD Turin system */

Just "AMD Turin system" feels a little as if all systems are like this, which is absolutely not the case.

Quoted 51 lines
tools/tests/numa/test-numa.c new file +51 -0
+        {
+            .affinity = {
+                { .nid = 0, .start = 0x00000000000ULL, .end = 0x0000009ffffULL },
+                { .nid = 0, .start = 0x000000c0000ULL, .end = 0x000afffffffULL },
+                { .nid = 0, .start = 0x00100000000ULL, .end = 0x0c04fffffffULL },
+                { .nid = 1, .start = 0x0c050000000ULL, .end = 0x0fc4fffffffULL },
+                { .nid = 1, .start = 0x10000000000ULL, .end = 0x183ffffffffULL },
+            },
+            .ram = {
+                { .start = 0x00000000000ULL, .end = 0x0000009ffffULL },
+                { .start = 0x00000100000ULL, .end = 0x0007590ffffULL },
+                { .start = 0x000759d1000ULL, .end = 0x00075a0ffffULL },
+                { .start = 0x00076000000ULL, .end = 0x00094c73fffULL },
+                { .start = 0x0009b5ff000ULL, .end = 0x0009fff9fffULL },
+                { .start = 0x0009ffff000ULL, .end = 0x0009fffffffULL },
+                { .start = 0x00100010000ULL, .end = 0x0fc4fffffffULL },
+                { .start = 0x10000000000ULL, .end = 0x183f7ffffffULL },
+                { .start = 0x183f8800000ULL, .end = 0x183faabffffULL },
+            },
+        },
+    };
+    int ret_code = EXIT_SUCCESS;
+
+    /* Dummy firmware interface provider name, use TST for TEST. */
+    numa_fw_nid_name = "TST";
+
+    for ( unsigned int i = 0 ; i < ARRAY_SIZE(tests); i++ )
+    {
+        paddr_t min = ~(paddr_t)0, max = 0;
+        unsigned int j;
+
+        numa_reset_state();
+
+        ram = tests[i].ram;
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].affinity) && tests[i].affinity[j].end;
+              j++ )
+        {
+            const struct mem_affinity *affinity = &tests[i].affinity[j];
+            paddr_t length = affinity->end - affinity->start + 1;
+
+            if ( !numa_update_node_memblks(affinity->nid, affinity->nid,
+                                           affinity->start, length, false) )
+            {
+                printf("Fail to add NID %u [%" PRIpaddr ", %" PRIpaddr "]\n",
+                        affinity->nid, affinity->start, affinity->end);
+                ret_code = EXIT_FAILURE;
+                continue;
+            }
+

tools/tests/numa/test-numa.c line 201

Quoted 10 lines
tools/tests/numa/test-numa.c new file +10 -0
+            min = min(min, affinity->start);
+            max = max(max, affinity->end);
+        }
+
+        if ( !numa_process_nodes(min, max + 1) )
+        {
+                printf("Unable to process nodes\n");
+                print_ranges(tests[i].affinity);
+                ret_code = EXIT_FAILURE;
+                continue;

This is mis-indented.  Best double check the whole file.

tools/tests/numa/test-numa.c line 212

Quoted 11 lines
tools/tests/numa/test-numa.c new file +11 -0
+        }
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].ram) && tests[i].ram[j].end;
+              j++ )
+            if ( !test_paddr(tests[i].ram[j].start) ||
+                 !test_paddr(tests[i].ram[j].end) )
+                ret_code = EXIT_FAILURE;
+    }
+
+    return ret_code;

This is fine for now, but we're going to have to consolidate the patterns eventually.

Quoted 1 line

Do you have a Gitlab CI run with this passing?

Yes, this for example:

https://gitlab.com/xen-project/hardware/xen-staging/-/jobs/14693648817

Thanks, Roger.

#11 lore raw

Re: [PATCH for-4.22 v2 4/5] tests/numa: add unit tests for NUMA setup logic

Quoted 161 lines · Roger Pau Monné
Quoted 160 lines · Andrew Cooper
Quoted 6 lines · Roger Pau Monne
tools/tests/numa/harness.h new file +0 -0
new file mode 100644
index 000000000000..9eec77f31402

tools/tests/numa/harness.h line 9

Quoted 9 lines
tools/tests/numa/harness.h new file +9 -0
@@ -0,0 +1,184 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Unit tests for NUMA setup.
+ *
+ * Copyright (C) 2026 Cloud Software Group
+ */
+
+#ifndef _TEST_HARNESS_
+#define _TEST_HARNESS_

This is overly generic, and liable to break if anyone copies it.  Maybe NUMA_HARNESS, or WRAP_XEN_NUMA because ...

Looking below, how about naming it wrapped-xen-numa.h, so ...

tools/tests/numa/harness.h line 173

Quoted 9 lines

<snip>

tools/tests/numa/harness.h new file +7 -0
+
+static inline bool arch_numa_unavailable(void)
+{
+    return false;
+}
+
+#include "numa.h"

I presume this is the real xen/numa.h ?

Quoted 20 lines
tools/tests/numa/harness.h new file +11 -0
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
tools/tests/numa/test-numa.c new file +3 -0
new file mode 100644
index 000000000000..bced68d4d7f1
@@ -0,0 +1,222 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Unit tests for NUMA setup.

tools/tests/numa/test-numa.c line 13

Quoted 10 lines
tools/tests/numa/test-numa.c new file +10 -0
+ *
+ * Copyright (C) 2026 Cloud Software Group
+ */
+
+#include "harness.h"
+
+static paddr_t mem_hotplug;
+unsigned int __read_mostly nr_cpu_ids = NR_CPUS;
+
+#include "../../xen/common/numa.c"

... this has

#include "wrapped-xen-numa.h"
#include "../../xen/common/numa.c"

which I think is clearer to follow.

tools/tests/numa/test-numa.c line 140

Quoted 9 lines

<snip>

tools/tests/numa/test-numa.c new file +7 -0
+int main(int argc, char **argv)
+{
+    static const struct {
+        struct mem_affinity affinity[MAX_RANGES];
+        struct mem_range ram[MAX_RANGES];
+    } tests[] = {
+        /* AMD Turin system. */

I'd suggest /* From an arbitrary AMD Turin system */

Just "AMD Turin system" feels a little as if all systems are like this, which is absolutely not the case.

Quoted 51 lines
tools/tests/numa/test-numa.c new file +51 -0
+        {
+            .affinity = {
+                { .nid = 0, .start = 0x00000000000ULL, .end = 0x0000009ffffULL },
+                { .nid = 0, .start = 0x000000c0000ULL, .end = 0x000afffffffULL },
+                { .nid = 0, .start = 0x00100000000ULL, .end = 0x0c04fffffffULL },
+                { .nid = 1, .start = 0x0c050000000ULL, .end = 0x0fc4fffffffULL },
+                { .nid = 1, .start = 0x10000000000ULL, .end = 0x183ffffffffULL },
+            },
+            .ram = {
+                { .start = 0x00000000000ULL, .end = 0x0000009ffffULL },
+                { .start = 0x00000100000ULL, .end = 0x0007590ffffULL },
+                { .start = 0x000759d1000ULL, .end = 0x00075a0ffffULL },
+                { .start = 0x00076000000ULL, .end = 0x00094c73fffULL },
+                { .start = 0x0009b5ff000ULL, .end = 0x0009fff9fffULL },
+                { .start = 0x0009ffff000ULL, .end = 0x0009fffffffULL },
+                { .start = 0x00100010000ULL, .end = 0x0fc4fffffffULL },
+                { .start = 0x10000000000ULL, .end = 0x183f7ffffffULL },
+                { .start = 0x183f8800000ULL, .end = 0x183faabffffULL },
+            },
+        },
+    };
+    int ret_code = EXIT_SUCCESS;
+
+    /* Dummy firmware interface provider name, use TST for TEST. */
+    numa_fw_nid_name = "TST";
+
+    for ( unsigned int i = 0 ; i < ARRAY_SIZE(tests); i++ )
+    {
+        paddr_t min = ~(paddr_t)0, max = 0;
+        unsigned int j;
+
+        numa_reset_state();
+
+        ram = tests[i].ram;
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].affinity) && tests[i].affinity[j].end;
+              j++ )
+        {
+            const struct mem_affinity *affinity = &tests[i].affinity[j];
+            paddr_t length = affinity->end - affinity->start + 1;
+
+            if ( !numa_update_node_memblks(affinity->nid, affinity->nid,
+                                           affinity->start, length, false) )
+            {
+                printf("Fail to add NID %u [%" PRIpaddr ", %" PRIpaddr "]\n",
+                        affinity->nid, affinity->start, affinity->end);
+                ret_code = EXIT_FAILURE;
+                continue;
+            }
+

tools/tests/numa/test-numa.c line 201

Quoted 10 lines
tools/tests/numa/test-numa.c new file +10 -0
+            min = min(min, affinity->start);
+            max = max(max, affinity->end);
+        }
+
+        if ( !numa_process_nodes(min, max + 1) )
+        {
+                printf("Unable to process nodes\n");
+                print_ranges(tests[i].affinity);
+                ret_code = EXIT_FAILURE;
+                continue;

This is mis-indented.  Best double check the whole file.

tools/tests/numa/test-numa.c line 212

Quoted 11 lines
tools/tests/numa/test-numa.c new file +11 -0
+        }
+
+        for ( j = 0;
+              j < ARRAY_SIZE(tests[i].ram) && tests[i].ram[j].end;
+              j++ )
+            if ( !test_paddr(tests[i].ram[j].start) ||
+                 !test_paddr(tests[i].ram[j].end) )
+                ret_code = EXIT_FAILURE;
+    }
+
+    return ret_code;

This is fine for now, but we're going to have to consolidate the patterns eventually.

Quoted 4 lines
Quoted 1 line

Do you have a Gitlab CI run with this passing?

Yes, this for example:

https://gitlab.com/xen-project/hardware/xen-staging/-/jobs/14693648817

Ok.  Acked-by: Andrew Cooper <[email protected]> preferably with the suggested adjustments.

#12 lore raw

PATCH v2 5/5 xen/numa: fix setup of non-aligned memory affinity ranges

In reply to Roger Pau Monne

The logic to populate memnodemap in populate_memnodemap() assumes that all ranges are aligned to the hash shift, this however is only true for the first address in a memory affinity node. Any subsequent ranges belonging to the same node might not be aligned to the hash shift value.

Such lack of alignment causes issues to the logic in populate_memnodemap(), as then the tail of the range might not be properly accounted for and setup in memnodemap. Fix this by forcing the start address of all regions to be aligned to the hash shift; if such alignment causes a region overlap it would always be between regions on the same node, and hence will never cause setup issues of the memnodemap array.

Introduce two additional test cases to the user-space NUMA setup unit testing, first test case is the native memory affinity and memory map of the system where this issue was found, second test case is a simplification to demonstrate the original problem more clearly.

Fixes 1666086b0044 ("x86/NUMA: improve memnode_shift calculation for multi node system")

---
Changes since v1:
- Do the rounding after the checking.

---
 tools/include/xen-tools/common-macros.h |  1 +
 tools/tests/numa/test-numa.c            | 45 +++++++++++++++++++++++++
 xen/common/numa.c                       |  6 ++++
 3 files changed, 52 insertions(+)
tools/include/xen-tools/common-macros.h +1 -0
index 9e2799178235..88b4a0e5a693 100644
@@ -69,6 +69,7 @@
 #endif
 
 #define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))
+#define ROUNDDOWN(x, a) ((x) & ~((a) - 1))
 
 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
tools/tests/numa/test-numa.c +45 -0

51 lines not on this page · raw message

xen/common/numa.c +6 -0
index 8544a1598218..92f8f1cedce1 100644
@@ -405,6 +405,12 @@ static int __init populate_memnodemap(const struct node *nodes,
         if ( (epdx >> shift) >= memnodemapsize )
             return 0;
 
+        /*
+         * Round down start address: if start is not aligned to the memnodemap
+         * chunk size the tail remainder might not be added.  Overlaps created
+         * by rounding will fall into the same NUMA region.
+         */
+        spdx = ROUNDDOWN(spdx, 1UL << shift);
         do {
             if ( memnodemap[spdx >> shift] != NUMA_NO_NODE &&
                  (!nodeids || memnodemap[spdx >> shift] != nodeids[i]) )
Signature · 1 line
2.53.0
#13 lore raw

Re: [PATCH for-4.22 v2 5/5] xen/numa: fix setup of non-aligned memory affinity ranges

tools/tests/numa/test-numa.c int main(int argc, char **argv) · line 161

Quoted 9 lines · Roger Pau Monne
tools/tests/numa/test-numa.c +1 -0
index bced68d4d7f1..8122e63a88ed 100644
@@ -158,6 +158,51 @@ int main(int argc, char **argv)
                 { .start = 0x183f8800000ULL, .end = 0x183faabffffULL },
             },
         },
+        /* System with unaligned affinity memblock. */

This probably wants to say "Found on a pre-production system", rather than just something we invented.  The reader can make the implication given the Zen5 above.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>