xen/mm: Introduce NUMA-aware memory claim sets
v7 cover xen/mm: Introduce NUMA-aware memory claim sets
Xen's existing memory claim interface can account only for a host-wide amount of memory for a domain. That is not enough for toolstacks that need to claim memory from particular NUMA nodes before building the domain.
This series extends Xen's memory claim design to support claim sets with NUMA-node-specific entries. Roger Pau Monné summarized the core design requirement as:
Ideally, we would need to introduce a new hypercall that allows making claims from multiple nodes in a single locked region, as to ensure success or failure in an atomic way.
A claim set can contain multiple node-specific claims and a host-wide claim for memory that may come from any NUMA node. The new domctl installs the full claim set atomically, and the allocator is updated so that claim checks and claim consumption are NUMA-node aware.
The components are:
1. installing multi-node claim sets atomically,
2. protecting claimed pages from other claim requests and allocations, and
3. redeeming claims in a NUMA-aware manner when satisfying allocations for domains with claims.
Legacy XENMEM_claim_pages behaviour is preserved; the legacy interface is deprecated and superseded by XEN_DOMCTL_claim_memory.
This update addresses v6 review comments, refactors claim redemption, and adds a GET operation for inspecting a domain's claim set.
Only one preparatory patch remains in this version, followed by the main feature and finally the OCaml bindings.
The updated design document for this series was submitted on May 5:
https://lists.xen.org/archives/html/xen-devel/2026-05/msg00163.html
Rendered version: https://xen.kaindl.dev/claims-v7-design/designs/claims/
The core operational principles have been validated in the following ways:
1. A comprehensive functional system test suite running in Dom0, which will follow this submission.
2. A native integration test environment for the Xen page allocator, providing a configurable synthetic Xen heap for each test case, which will follow this submission as well.
3. A set of high-level functional integration tests and performance tests, executed as part of end-to-end product deployment validation.
4. Validation in a customer's application performance lab to confirm performance expectations. This was done on predecessor code sharing the same design; that earlier code only supported single-node claims.
Best regards,
Bernhard
Changes since v6:
- Incorporated v6 review feedback.
- Restored claim invariants as needed after offlining pages.
- Addressed Jan's point about the ambiguity of the term "global":
See https://lists.xen.org/archives/html/xen-devel/2026-05/msg00026.html
- Use "host-wide claims", with "host_claims" and "node_claims" as shorthands.
In addition, I adjusted helper names to make the domain scope explicit and to keep the "release", "set", and "get" operations named consistently:
* deduct_global_claims() -> domain_release_host_claims()
* deduct_node_claims() -> domain_release_node_claims()
* domain_set_outstanding_pages() -> domain_set_claim_entries()
* domain_get_claim_entries() was added.
- The new domain_set_claim_entries() helper also handles the legacy XENMEM_claim_pages interface for backward compatibility, replacing domain_set_outstanding_pages().
Bernhard Kaindl (3):
xen/mm: Introduce per-node free page counter xen/mm: Introduce NUMA-aware memory claim sets tools/ocaml: Add OCaml binding for NUMA claim sets
tools/include/xenctrl.h | 11 + tools/libs/ctrl/xc_domain.c | 28 ++ tools/ocaml/libs/xc/xenctrl.ml | 10 + tools/ocaml/libs/xc/xenctrl.mli | 10 + tools/ocaml/libs/xc/xenctrl_stubs.c | 50 ++++ xen/common/domain.c | 5 +- xen/common/domctl.c | 57 ++++ xen/common/memory.c | 5 +- xen/common/page_alloc.c | 416 +++++++++++++++++++++++----- xen/include/public/domctl.h | 38 +++ xen/include/public/memory.h | 2 + xen/include/xen/mm.h | 6 +- xen/include/xen/sched.h | 4 + xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 1 + 15 files changed, 570 insertions(+), 74 deletions(-)
Signature · 1 line
2.39.5
PATCH v7 1/3 xen/mm: Introduce per-node free page counter
From: Alejandro Vallejo <[email protected]>
Add node_avail_pages[], updated under heap_lock in sync with avail[node][zone] to cache the per-node sum of free pages.
Use it in avail_node_heap_pages() to avoid summing all zones on each call. Guard it with nodeid < MAX_NUMNODES and node_online(nodeid).
---
This patch was originally sent by Alejandro Vallejo:
https://lists.xenproject.org/archives/html/xen-devel/2025-03/msg01130.html
I use node_avail_pages[] in avail_node_heap_pages() as an optimisation.
Verification of the changes:
1. node_avail_pages[node] is updated whenever avail[node][zone] changes, so the two remain in sync.
2. avail_node_heap_pages() previously summed all zones of a node and now returns node_avail_pages[node], so the same free buddy pages are counted.
3. avail_node_heap_pages() returns 0 for offline nodes and for nodes
Quoted 1 line
=
MAX_NUMNODESas before.
4. avail_node_heap_pages(-1) returned the sum from all nodes, equal to total_avail_pages, but this is not used by current callers. avail_heap_pages(z, z, -1) is used by other callers for that instead. To avoid dead code, a check for -1 to implement this is not added.
Update locations:
- free_heap_pages() increments node_avail_pages[node] alongside avail[node][zone] when pages are freed, including during heap initialisation.
- alloc_heap_pages() decrements node_avail_pages[node] alongside avail[node][zone] when pages are allocated.
- reserve_offlined_page() decrements node_avail_pages[node] alongside avail[node][zone] when pages are offlined.
Colored pages do not go through the buddy allocator. Since they do not update avail[node][zone], they are not reflected in node_avail_pages[node] either.
N.B. Current callers already iterate over online nodes only.
Changes since v6:
- Preserved the 0 return for offline nodes and nodes >= MAX_NUMNODES.
--- xen/common/page_alloc.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
xen/common/page_alloc.c +9 -1
@@ -485,6 +485,7 @@ static unsigned long node_need_scrub[MAX_NUMNODES];static unsigned long *avail[MAX_NUMNODES ];static unsigned long total_avail_pages; +static unsigned long node_avail_pages[MAX_NUMNODES ];static DEFINE_SPINLOCK (heap_lock);/* Total outstanding claims by all domains */
@@ -1050,6 +1051,8 @@ static struct page_info *alloc_heap_pages( avail[node][zone] -= request;ASSERT (total_avail_pages >= request); total_avail_pages -= request; +ASSERT (node_avail_pages[node] >= request); + node_avail_pages[node] -= request;if ( d && d->outstanding_pages && !(memflags & MEMF_no_refcount) ) {
@@ -1243,6 +1246,8 @@ static int reserve_offlined_page(struct page_info *head) avail[node][zone]--;ASSERT (total_avail_pages >0 ); total_avail_pages--; +ASSERT (node_avail_pages[node] >0 ); + node_avail_pages[node]--; page_list_add_tail(cur_head, test_bit(_PGC_broken, &cur_head->count_info) ?
@@ -1566,6 +1571,7 @@ static void free_heap_pages( avail[node][zone] +=1 << order; total_avail_pages +=1 << order; + node_avail_pages[node] +=1 << order;if ( need_scrub ) { node_need_scrub[node] +=1 << order;
@@ -2831,7 +2837,9 @@ unsigned long avail_domheap_pages_region(unsigned long avail_node_heap_pages (unsigned int nodeid) { -return avail_heap_pages (MEMZONE_XEN ,NR_ZONES -1 , nodeid); +if ( nodeid <MAX_NUMNODES &&node_online (nodeid) ) +return node_avail_pages[nodeid]; +return 0 ; }
Signature · 1 line
2.39.5
PATCH v7 2/3 xen/mm: Introduce NUMA-aware memory claim sets
In reply to Bernhard KaindlThis commit extends Xen's memory claim design to support installing claim sets spanning multiple NUMA nodes atomically. As Roger Pau Monné suggested:
Ideally, we would need to introduce a new hypercall that allows making claims from multiple nodes in a single locked region, as to ensure success or failure in an atomic way.
A claim set can contain multiple node-specific claims and a host-wide claim for memory that may come from any NUMA node. The new domctl installs the full claim set atomically, and the allocator is updated so that claim checks and claim consumption follow the new semantics.
This adds:
1. installing multi-node claim sets atomically,
2. protecting claimed pages from other claim requests and allocations, and
3. redeeming held claims when satisfying allocations.
Legacy XENMEM_claim_pages behaviour is preserved; the interface is deprecated and superseded by XEN_DOMCTL_claim_memory.
---
The v7 design document submitted ahead of this series may help with review. It explains the background, design rationale, and implementation details.
Rendered version: https://xen.kaindl.dev/claims-v7-design/designs/claims
Many thanks to everyone who contributed to the earlier work and review:
especially Roger Pau Monné, Alejandro Vallejo, Jan Beulich, Andrew Cooper, Marcus Granado, and Edwin Török.
Thanks,
Bernhard
--- tools/include/xenctrl.h | 11 + tools/libs/ctrl/xc_domain.c | 28 ++ xen/common/domain.c | 5 +- xen/common/domctl.c | 57 ++++ xen/common/memory.c | 5 +- xen/common/page_alloc.c | 410 +++++++++++++++++++++++----- xen/include/public/domctl.h | 38 +++ xen/include/public/memory.h | 2 + xen/include/xen/mm.h | 6 +- xen/include/xen/sched.h | 4 + xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 1 + 12 files changed, 493 insertions(+), 75 deletions(-)
tools/include/xenctrl.h +11 -0
@@ -2659,6 +2659,17 @@ int xc_domain_set_llc_colors(xc_interface *xch, uint32_t domid,const uint32_t *llc_colors, uint32_t num_llc_colors); +/* +* Set or get memory claims for a domain. +* +* For XEN_DOMCTL_CLAIM_MEMORY_GET, callers may pass *nr_entries == 0 and +* claim_set == NULL to query the number of records needed. In that case the +* call fails with ERANGE and updates *nr_entries with the required count. +*/ +int xc_domain_claim_memory (xc_interface *xch,uint32_t domid,uint32_t mode, +uint32_t *nr_entries, +xen_memory_claim_t *claim_set); +#if defined(__arm__) || defined(__aarch64__)int xc_dt_overlay (xc_interface *xch,void *overlay_fdt,uint32_t overlay_fdt_size,uint8_t overlay_op);
tools/libs/ctrl/xc_domain.c +28 -0
@@ -1070,6 +1070,34 @@ int xc_domain_remove_from_physmap(xc_interface *xch,return xc_memory_op (xch, XENMEM_remove_from_physmap, &xrfp,sizeof (xrfp)); } +/* Set or get memory claims for a domain. */ +int xc_domain_claim_memory (xc_interface *xch,uint32_t domid,uint32_t mode, +uint32_t *nr_entries,xen_memory_claim_t *claim_set) +{ +struct xen_domctl domctl = {}; +DECLARE_HYPERCALL_BOUNCE (claim_set, *nr_entries *sizeof (*claim_set), +XC_HYPERCALL_BUFFER_BOUNCE_BOTH ); +int ret; + +if (xc_hypercall_bounce_pre (xch, claim_set) ) +return -1 ; + + domctl.cmd = XEN_DOMCTL_claim_memory; + domctl.domain = domid; + domctl.u.claim_memory.mode = mode; + domctl.u.claim_memory.nr_entries = *nr_entries; +set_xen_guest_handle (domctl.u.claim_memory.claim_set, claim_set); + + ret =do_domctl (xch, &domctl); + + *nr_entries = domctl.u.claim_memory.nr_entries; + +xc_hypercall_bounce_post (xch, claim_set); + +return ret; +} + +/* XENMEM_claim_pages is deprecated; use xc_domain_claim_memory() instead. */ int xc_domain_claim_pages (xc_interface *xch,uint32_t domid,unsigned long nr_pages)
xen/common/domain.c +4 -1
@@ -1319,7 +1319,10 @@ int domain_kill(struct domain *d)rspin_barrier (&d->domain_lock);argo_destroy (d);vnuma_destroy (d->vnuma); -domain_set_outstanding_pages (d,0 ); +/* Release all outstanding claims of the domain. */ +domain_set_claim_entries (d,1 , &(xen_memory_claim_t ){ + .target =XEN_DOMCTL_CLAIM_MEMORY_HOST , .pages =0 , + });/* fallthrough */ case DOMDYING_dying: rc =domain_teardown (d);
xen/common/domctl.c +57 -0
69 lines not on this page · raw message
xen/common/memory.c +4 -1
@@ -1805,7 +1805,10 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) rc = -EINVAL ;if ( !rc ) - rc =domain_set_outstanding_pages (d, reservation.nr_extents); + rc =domain_set_claim_entries (d,1 , &(xen_memory_claim_t ){ + .target =XEN_DOMCTL_CLAIM_MEMORY_LEGACY , + .pages = reservation.nr_extents, + });rcu_unlock_domain (d);
xen/common/page_alloc.c +338 -72
473 lines not on this page · raw message
xen/include/public/domctl.h +38 -0
@@ -1276,6 +1276,42 @@ struct xen_domctl_get_domain_state {uint64_t unique_id;/* Unique domain identifier. */ }; +struct xen_memory_claim { +uint64_aligned_t pages;/* Number of pages to claim. */ +uint32_t target;/* NUMA node or special target constant. */ +uint32_t cmd;/* Reserved, must be zero. */ +}; +typedef struct xen_memory_claim xen_memory_claim_t ; +DEFINE_XEN_GUEST_HANDLE (xen_memory_claim_t); + +/* Special claim targets for the target field of xen_memory_claim_t. */ +#define XEN_DOMCTL_CLAIM_MEMORY_HOST 0x80000000U/* Host-wide claims. */ +#define XEN_DOMCTL_CLAIM_MEMORY_LEGACY 0x40000000U/* Legacy semantics. */ + +/* +* XEN_DOMCTL_claim_memory +* +* Install or query a domain memory claim set. A SET operation replaces the +* existing claim set atomically. Claims are redeemed by later allocations to +* the domain. A SET request whose entries all have pages == 0 releases any +* existing claims. +* +* For GET, callers may pass nr_entries == 0 and claim_set == NULL to query the +* number of records needed. Xen returns -ERANGE and updates nr_entries. If the +* supplied array is too small, Xen returns -ERANGE and updates nr_entries +* without copying partial records. +*/ +struct xen_domctl_claim_memory { +/* IN/OUT: Array of struct xen_memory_claim. */ +XEN_GUEST_HANDLE_64 (xen_memory_claim_t) claim_set; +/* IN/OUT: Number of records in the claim_set array. */ +uint32_t nr_entries; +/* IN: Operation to perform on the claim set (GET or SET). */ +uint32_t mode; +#define XEN_DOMCTL_CLAIM_MEMORY_SET 0U/* Set the claim set for the domain. */ +#define XEN_DOMCTL_CLAIM_MEMORY_GET 1U/* Get the claim set of the domain. */ +}; +struct xen_domctl {/* Stable domctl ops: interface_version is required to be 0. */ uint32_t cmd;
@@ -1368,6 +1404,7 @@ struct xen_domctl {#define XEN_DOMCTL_gsi_permission 88#define XEN_DOMCTL_set_llc_colors 89#define XEN_DOMCTL_get_domain_state 90/* stable interface */ +#define XEN_DOMCTL_claim_memory 91#define XEN_DOMCTL_gdbsx_guestmemio 1000#define XEN_DOMCTL_gdbsx_pausevcpu 1001#define XEN_DOMCTL_gdbsx_unpausevcpu 1002
@@ -1436,6 +1473,7 @@ struct xen_domctl {#endif struct xen_domctl_set_llc_colors set_llc_colors;struct xen_domctl_get_domain_state get_domain_state; +struct xen_domctl_claim_memory claim_memory;uint8_t pad[128 ]; } u; };
xen/include/public/memory.h +2 -0
@@ -569,6 +569,8 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_sharing_op_t);* for 10, only 7 additional pages are claimed. * * Caller must be privileged or the hypercall fails. +* +* Note: This hypercall is superseded by XEN_DOMCTL_claim_memory. */ #define XENMEM_claim_pages 24
xen/include/xen/mm.h +5 -1
@@ -72,6 +72,7 @@#include <public/memory.h> struct page_info ; +struct xen_memory_claim ;extern bool using_static_heap;
@@ -131,7 +132,10 @@ int populate_pt_range(unsigned long virt, unsigned long nr_mfns);/* Claim handling */ unsigned long __must_check domain_adjust_tot_pages (struct domain *d,long pages); -int domain_set_outstanding_pages (struct domain *d,unsigned long pages); +int domain_set_claim_entries (struct domain *d,uint32_t nr_entries, +const struct xen_memory_claim *claim_set); +int domain_get_claim_entries (struct domain *d,uint32_t *nr_entries, +struct xen_memory_claim *claim_set);void get_outstanding_claims (uint64_t *free_pages,uint64_t *outstanding_pages);/* Domain suballocator. These functions are *not* interrupt-safe.*/
xen/include/xen/sched.h +4 -0
@@ -419,6 +419,10 @@ struct domainunsigned int xenheap_pages;/* pages allocated from Xen heap */ /* Pages claimed but not possessed, protected by global heap_lock. */ unsigned int outstanding_pages; +unsigned int node_claims;/* Sum of per-node claims. */ +/* Domain objects use dedicated pages, leaving room for per-node claims. */ +unsigned int claims[MAX_NUMNODES ];/* Per-NUMA-node claims. */ +unsigned int max_pages;/* maximum value for domain_tot_pages() */ unsigned int extra_pages;/* pages not included in domain_tot_pages() */
xen/xsm/flask/hooks.c +1 -0
@@ -651,6 +651,7 @@ static int cf_check flask_domctl(struct domain *d, unsigned int cmd,/* These have individual XSM hooks (common/domctl.c) */ case XEN_DOMCTL_getdomaininfo: +case XEN_DOMCTL_claim_memory:case XEN_DOMCTL_scheduler_op:case XEN_DOMCTL_irq_permission:case XEN_DOMCTL_iomem_permission:
xen/xsm/flask/policy/access_vectors +1 -0
@@ -216,6 +216,7 @@ class domain2 # XEN_DOMCTL_scheduler_op with XEN_DOMCTL_SCHEDOP_putinfo setscheduler # XENMEM_claim_pages +# XEN_DOMCTL_claim_memory setclaim # XEN_DOMCTL_cacheflush cacheflush
Signature · 1 line
2.39.5
PATCH v7 3/3 tools/ocaml: Add OCaml binding for NUMA claim sets
In reply to Bernhard KaindlAdd an OCaml binding for xc_domain_claim_memory():
- tools/ocaml/libs/xc/xenctrl.ml/mli:
Add claim record type and domain_claim_memory external.
- tools/ocaml/libs/xc/xenctrl_stubs.c:
Marshal the OCaml claim array into a xen_memory_claim_t array. Map nodes of -1 to XEN_DOMCTL_CLAIM_MEMORY_HOST for host-wide claims.
--- tools/ocaml/libs/xc/xenctrl.ml | 10 ++++++ tools/ocaml/libs/xc/xenctrl.mli | 10 ++++++ tools/ocaml/libs/xc/xenctrl_stubs.c | 50 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+)
tools/ocaml/libs/xc/xenctrl.ml +10 -0
@@ -370,6 +370,16 @@ external domain_deassign_device: handle -> domid -> (int * int * int * int) -> u external domain_test_assign_device: handle -> domid -> (int * int * int * int) -> bool = "stub_xc_domain_test_assign_device" +(* OCaml binding for xc_domain_claim_memory() to claim memory for a domain *) + +type claim = + { + pages: int64; (* Number of pages to claim *) + node: int32; (* NUMA node ID, or -1 for host-wide claims *) + } +external domain_claim_memory: handle -> domid -> claim array -> unit + = "stub_xc_domain_claim_memory" + external version: handle -> version = "stub_xc_version_version" external version_compile_info: handle -> compile_info = "stub_xc_version_compile_info"
tools/ocaml/libs/xc/xenctrl.mli +10 -0
@@ -297,6 +297,16 @@ external domain_deassign_device: handle -> domid -> (int * int * int * int) -> u external domain_test_assign_device: handle -> domid -> (int * int * int * int) -> bool = "stub_xc_domain_test_assign_device" +(* OCaml binding for xc_domain_claim_memory() to claim memory for a domain *) + +type claim = + { + pages: int64; (* Number of pages to claim *) + node: int32; (* NUMA node ID, or -1 for host-wide claims *) + } +external domain_claim_memory: handle -> domid -> claim array -> unit + = "stub_xc_domain_claim_memory" + external version : handle -> version = "stub_xc_version_version" external version_compile_info : handle -> compile_info = "stub_xc_version_compile_info"
tools/ocaml/libs/xc/xenctrl_stubs.c +50 -0
56 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH v7 3/3] tools/ocaml: Add OCaml binding for NUMA claim sets
Quoted 5 lines · Bernhard Kaindl
Add an OCaml binding for
xc_domain_claim_memory():-
tools/ocaml/libs/xc/xenctrl.ml/mli:
Add claim record type anddomain_claim_memoryexternal.
Quoted 7 lines
-
tools/ocaml/libs/xc/xenctrl_stubs.c:
Marshal the OCaml claim array into axen_memory_claim_tarray. Map nodes of -1 toXEN_DOMCTL_CLAIM_MEMORY_HOSTfor host-wide claims.Acked-by Christian Lindig <[email protected]>Requested-by Andrew Cooper <[email protected]>Signed-off-by Bernhard Kaindl <[email protected]>
Nit: Chronologically order tags, please: Christian can't have ack-ed this before you signed off on it.
Jan
RE: [PATCH v7 3/3] tools/ocaml: Add OCaml binding for NUMA claim sets
Quoted 9 lines
-----Original Message-----
Quoted 3 lines
Acked-by Christian Lindig <[email protected]>Requested-by Andrew Cooper <[email protected]>Signed-off-by Bernhard Kaindl <[email protected]>Nit: Chronologically order tags, please: Christian can't have ack-ed this before you signed off on it.
Jan
Hi Jan, as this was the last patch in this series,
I assume you found no reason which can't be fixed on merge for this series and you can ack it? (I think item could be fixed on merge)
If you could, please do so, otherwise could I you expedite your review of it?
At lot of further NUMA work depends on it, and it as it has been posted in this or very similar form before the deadlines for Xen 4.22, it would be nice if it could be considered for merge.
Thanks,
Bernhard
Re: [PATCH v7 3/3] tools/ocaml: Add OCaml binding for NUMA claim sets
Quoted 5 lines · Bernhard Kaindl
Quoted 5 lines
-----Original Message-----
Quoted 3 lines
Acked-by Christian Lindig <[email protected]>Requested-by Andrew Cooper <[email protected]>Signed-off-by Bernhard Kaindl <[email protected]>
Quoted 7 lines
Quoted 2 lines
Nit: Chronologically order tags, please: Christian can't have ack-ed this before you signed off on it.
Hi Jan, as this was the last patch in this series,
I assume you found no reason which can't be fixed on merge for this series and you can ack it? (I think item could be fixed on merge)
Why would I ack an OCaml patch?
As to fixing while committing - perhaps that's easily possible here, but
(a) I wouldn't commit this (tools) patch on its own and (b) the other two patches look to still be pending (with me just having returned from a week of PTO I still didn't get around to read xen-develq, though). However, edits upon committing is strictly an offer by the committer. It shouldn't normally be the submitter to be asking for such.
Quoted 2 lines
If you could, please do so, otherwise could I you expedite your review of it?
Expedite review of what? As this isn't the first time to try to apply pressure for me to review your patches: May I please ask that you don't? Your patches aren't any different from patches by others.
Quoted 3 lines
At lot of further NUMA work depends on it, and it as it has been posted in this or very similar form before the deadlines for Xen 4.22, it would be nice if it could be considered for merge.
Again, without having read xen-devel@ yet, it was my understanding (assumption) that the NUMA work has missed the 4.22 window.
Jan
Re: [PATCH v7 1/3] xen/mm: Introduce per-node free page counter
In reply to Bernhard KaindlQuoted 45 lines · Bernhard Kaindl
From: Alejandro Vallejo <[email protected]>
Add node_avail_pages[], updated under
heap_lockin sync with avail[node][zone] to cache the per-node sum of free pages.Use it in
avail_node_heap_pages()to avoid summing all zones on each call. Guard it with nodeid <MAX_NUMNODESandnode_online(nodeid).Signed-off-by Alejandro Vallejo <[email protected]>Signed-off-by Bernhard Kaindl <[email protected]>---
This patch was originally sent by Alejandro Vallejo:
https://lists.xenproject.org/archives/html/xen-devel/2025-03/msg01130.htmlI use node_avail_pages[] in
avail_node_heap_pages()as an optimisation.Verification of the changes:
1. node_avail_pages[node] is updated whenever avail[node][zone] changes, so the two remain in sync.
2.
avail_node_heap_pages()previously summed all zones of a node and now returns node_avail_pages[node], so the same free buddy pages are counted.3.
avail_node_heap_pages()returns 0 for offline nodes and for nodesQuoted 1 line
=
MAX_NUMNODESas before.4.
avail_node_heap_pages(-1)returned the sum from all nodes, equal tototal_avail_pages, but this is not used by current callers.avail_heap_pages(z, z, -1)is used by other callers for that instead. To avoid dead code, a check for -1 to implement this is not added.Update locations:
-
free_heap_pages()increments node_avail_pages[node] alongside avail[node][zone] when pages are freed, including during heap initialisation.-
alloc_heap_pages()decrements node_avail_pages[node] alongside avail[node][zone] when pages are allocated.-
reserve_offlined_page()decrements node_avail_pages[node] alongside avail[node][zone] when pages are offlined.
Quoted 8 lines
Colored pages do not go through the buddy allocator. Since they do not update avail[node][zone], they are not reflected in node_avail_pages[node] either.
N.B. Current callers already iterate over online nodes only.
Changes since v6:
- Preserved the 0 return for offline nodes and nodes >=MAX_NUMNODES.
Hard to identify what v6 was (and who, if anyone, asked for the change) when, afaict, the patch subject changed.
with ...
xen/common/page_alloc.c unsigned long avail_domheap_pages_region( · line 2843
Quoted 9 lines
xen/common/page_alloc.c +3
-1@@ -2831,7 +2837,9 @@ unsigned long avail_domheap_pages_region(unsigned long avail_node_heap_pages (unsigned int nodeid) { -return avail_heap_pages (MEMZONE_XEN ,NR_ZONES -1 , nodeid); +if ( nodeid <MAX_NUMNODES &&node_online (nodeid) ) +return node_avail_pages[nodeid]; +return 0 ; }
... a blank line inserted ahead of the "main" (not really here, just by its indentation) return.
One other remark: With the function called from just a sysctl and a keyhandler, the direct array access is likely fine. Generally it would want to be array_access_nospec() though, when accessible from guests.
Jan
Quoted 36 lines · Bernhard Kaindl
This commit extends Xen's memory claim design to support installing claim sets spanning multiple NUMA nodes atomically. As Roger Pau Monné suggested:
Ideally, we would need to introduce a new hypercall that allows making claims from multiple nodes in a single locked region, as to ensure success or failure in an atomic way.
A claim set can contain multiple node-specific claims and a host-wide claim for memory that may come from any NUMA node. The new domctl installs the full claim set atomically, and the allocator is updated so that claim checks and claim consumption follow the new semantics.
This adds:
1. installing multi-node claim sets atomically,
2. protecting claimed pages from other claim requests and allocations, and
3. redeeming held claims when satisfying allocations.Legacy
XENMEM_claim_pagesbehaviour is preserved; the interface is deprecated and superseded byXEN_DOMCTL_claim_memory.Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Bernhard Kaindl <[email protected]>---
The v7 design document submitted ahead of this series may help with review. It explains the background, design rationale, and implementation details.Rendered version: https://xen.kaindl.dev/claims-v7-design/designs/claims
Many thanks to everyone who contributed to the earlier work and review:
especially Roger Pau Monné, Alejandro Vallejo, Jan Beulich, Andrew Cooper, Marcus Granado, and Edwin Török.Thanks,
Bernhard
Quoted 14 lines
--- tools/include/xenctrl.h | 11 + tools/libs/ctrl/xc_domain.c | 28 ++ xen/common/domain.c | 5 +- xen/common/domctl.c | 57 ++++ xen/common/memory.c | 5 +- xen/common/page_alloc.c | 410 +++++++++++++++++++++++----- xen/include/public/domctl.h | 38 +++ xen/include/public/memory.h | 2 + xen/include/xen/mm.h | 6 +- xen/include/xen/sched.h | 4 + xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 1 + 12 files changed, 493 insertions(+), 75 deletions(-)
And there's no way to split this some? I'll only do some light review for now.
xen/common/domain.c int domain_kill(struct domain *d) · line 1325
Quoted 11 lines
xen/common/domain.c +4
-1@@ -1319,7 +1319,10 @@ int domain_kill(struct domain *d)rspin_barrier (&d->domain_lock);argo_destroy (d);vnuma_destroy (d->vnuma); -domain_set_outstanding_pages (d,0 ); +/* Release all outstanding claims of the domain. */ +domain_set_claim_entries (d,1 , &(xen_memory_claim_t ){ + .target =XEN_DOMCTL_CLAIM_MEMORY_HOST , .pages =0 , + });
This is pretty badly formatted. Maybe
domain_set_claim_entries(
d, 1,
&(xen_memory_claim_t){
.target = XEN_DOMCTL_CLAIM_MEMORY_HOST, .pages = 0,
});? (Applies elsewhere as well, obviously.)
Whether the .pages field needs explicitly mentioning is questionable.
Quoted 13 lines
xen/common/domctl.c +7
-0@@ -51,6 +51,57 @@ static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, MAX_NUMNODES); } +/* Set or get memory claims for a domain. */ +static int claim_memory (struct domain *d, +struct xen_domctl_claim_memory *uinfo,bool *copyback) +{ +xen_memory_claim_t *entries; +int rc = -EFAULT ; +
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 70
Quoted 10 lines
xen/common/domctl.c +10
-0+/* Reject LLC coloring; alloc_color_heap_page() does not handle claims. */ +if ( llc_coloring_enabled ) +return -EOPNOTSUPP ; + +switch ( uinfo->mode ) + { +case XEN_DOMCTL_CLAIM_MEMORY_SET : +if ( !uinfo->nr_entries ) +return -EINVAL ; +if ( uinfo->nr_entries >MAX_NUMNODES +1 )
Why's MAX_NUMNODES and MAX_NUMNODES + 1 as an input permitted?
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 79
Quoted 9 lines
xen/common/domctl.c +9
-0+return -E2BIG ; +break ; +case XEN_DOMCTL_CLAIM_MEMORY_GET : +if ( uinfo->nr_entries >MAX_NUMNODES +1 ) + uinfo->nr_entries =MAX_NUMNODES +1 ; +break ; +default : +return -EOPNOTSUPP ; + }
Here and elsewhere - blank lines please between non-fall-through case blocks.
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 83
Quoted 4 lines
xen/common/domctl.c +4
-0+if ( d->is_dying ) +return -ESRCH ; + + entries =xmalloc_array (xen_memory_claim_t, uinfo->nr_entries);
Better xzalloc_array() (really xvzalloc_array()) seeing ...
Quoted 4 lines
xen/common/domctl.c +4
-0+if ( entries ==NULL ) +return -ENOMEM ; + + switch ( uinfo->mode )
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 97
Quoted 10 lines
xen/common/domctl.c +10
-0+ { +case XEN_DOMCTL_CLAIM_MEMORY_SET : +if ( !copy_from_guest (entries, uinfo->claim_set, uinfo->nr_entries) ) + rc =domain_set_claim_entries (d, uinfo->nr_entries, entries); +break ; +case XEN_DOMCTL_CLAIM_MEMORY_GET : + rc =domain_get_claim_entries (d, &uinfo->nr_entries, entries); + *copyback = true; +if ( !rc &©_to_guest (uinfo->claim_set, entries, + uinfo->nr_entries) )
... this copy-out.
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 100
Quoted 3 lines
xen/common/domctl.c +3
-0+ rc = -EFAULT ; +break ; + }
Missing default:.
xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) · line 920
Quoted 6 lines
xen/common/domctl.c +2
-0@@ -865,6 +916,12 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) copyback = true;break ; +case XEN_DOMCTL_claim_memory: + ret =xsm_claim_pages (XSM_PRIV , d);
This and the xsm/flask/hooks.c change will need reworking, to fit the spirit of XSA-492.
xen/include/public/domctl.h struct xen_domctl_get_domain_state { · line 1282
Quoted 10 lines
xen/include/public/domctl.h +4
-0@@ -1276,6 +1276,42 @@ struct xen_domctl_get_domain_state {uint64_t unique_id;/* Unique domain identifier. */ }; +struct xen_memory_claim { +uint64_aligned_t pages;/* Number of pages to claim. */ +uint32_t target;/* NUMA node or special target constant. */ +uint32_t cmd;/* Reserved, must be zero. */
Why is this named "cmd" when it's reserved?
xen/include/public/domctl.h struct xen_domctl_get_domain_state { · line 1289
Quoted 7 lines
xen/include/public/domctl.h +7
-0+}; +typedef struct xen_memory_claim xen_memory_claim_t ; +DEFINE_XEN_GUEST_HANDLE (xen_memory_claim_t); + +/* Special claim targets for the target field of xen_memory_claim_t. */ +#define XEN_DOMCTL_CLAIM_MEMORY_HOST 0x80000000U/* Host-wide claims. */ +#define XEN_DOMCTL_CLAIM_MEMORY_LEGACY 0x40000000U/* Legacy semantics. */
I think the latter should be internal to Xen, and in particular not be permitted with this new domctl.
Quoted 8 lines
xen/include/public/domctl.h +8
-0+/* +* XEN_DOMCTL_claim_memory +* +* Install or query a domain memory claim set. A SET operation replaces the +* existing claim set atomically. Claims are redeemed by later allocations to +* the domain. A SET request whose entries all have pages == 0 releases any +* existing claims. +*
xen/include/public/domctl.h struct xen_domctl_get_domain_state { · line 1308
Quoted 10 lines
xen/include/public/domctl.h +10
-0+* For GET, callers may pass nr_entries == 0 and claim_set == NULL to query the +* number of records needed. Xen returns -ERANGE and updates nr_entries. If the +* supplied array is too small, Xen returns -ERANGE and updates nr_entries +* without copying partial records. +*/ +struct xen_domctl_claim_memory { +/* IN/OUT: Array of struct xen_memory_claim. */ +XEN_GUEST_HANDLE_64 (xen_memory_claim_t) claim_set; +/* IN/OUT: Number of records in the claim_set array. */ +uint32_t nr_entries;
What is IN and what is OUT looks to differ between GET and SET.
xen/include/public/domctl.h struct xen_domctl_get_domain_state { · line 1313
Quoted 5 lines
xen/include/public/domctl.h +5
-0+/* IN: Operation to perform on the claim set (GET or SET). */ +uint32_t mode; +#define XEN_DOMCTL_CLAIM_MEMORY_SET 0U/* Set the claim set for the domain. */ +#define XEN_DOMCTL_CLAIM_MEMORY_GET 1U/* Get the claim set of the domain. */ +};
And there's no way to fetch overall claims?
xen/include/public/domctl.h int populate_pt_range(unsigned long virt, unsigned long nr_mfns); · line 138
Quoted 9 lines
xen/include/public/domctl.h +4
-1@@ -131,7 +132,10 @@ int populate_pt_range(unsigned long virt, unsigned long nr_mfns);/* Claim handling */ unsigned long __must_check domain_adjust_tot_pages (struct domain *d,long pages); -int domain_set_outstanding_pages (struct domain *d,unsigned long pages); +int domain_set_claim_entries (struct domain *d,uint32_t nr_entries, +const struct xen_memory_claim *claim_set); +int domain_get_claim_entries (struct domain *d,uint32_t *nr_entries, +struct xen_memory_claim *claim_set);
Please see ./CODING_STYLE as to the use of fixed-width types. In the latter case I can see justification, but in the former I can't.
xen/include/xen/sched.h struct domain · line 424
Quoted 9 lines
xen/include/xen/sched.h +3
-0@@ -419,6 +419,10 @@ struct domainunsigned int xenheap_pages;/* pages allocated from Xen heap */ /* Pages claimed but not possessed, protected by global heap_lock. */ unsigned int outstanding_pages; +unsigned int node_claims;/* Sum of per-node claims. */ +/* Domain objects use dedicated pages, leaving room for per-node claims. */ +unsigned int claims[MAX_NUMNODES ];/* Per-NUMA-node claims. */
Potentially too big an array to embed directly here.
Jan
Hi Bernhard,
Thanks for the patch! At ~500 changed lines it is hard to review in one go. Could you split it into smaller patches, ideally under 200 lines each? For instance:
1. domain_{set,get}_claim_entries(), with XENMEM_claim_pages and domain_kill() converted to it,
2. per-node claim accounting in the allocator (claimed_pages[],
d->claims[], redeeming in alloc_heap_pages()/get_free_buddy()),
3. restoring the claim invariants on page offlining,
4. the new XEN_DOMCTL_claim_memory (public header, domctl, XSM),
5. the xc_domain_claim_memory() wrapper in libxenctrl.
Some comments below :)
Quoted 7 lines
This commit extends Xen's memory claim design to support installing claim sets spanning multiple NUMA nodes atomically. As Roger Pau Monné suggested:
Ideally, we would need to introduce a new hypercall that allows making claims from multiple nodes in a single locked region, as to ensure success or failure in an atomic way.
Quoting a mail in the commit message seems unusual in Xen. Credit should go in a tag as a Suggested-by trailer, and the motivation could simply be stated, e.g.:
Installing per-node claims one by one is not atomic: a request may succeed on some nodes and fail on others. Introduce a hypercall that installs a set of claims spanning multiple nodes under a single lock, so that it either fully succeeds or fully fails.
Quoted 10 lines
A claim set can contain multiple node-specific claims and a host-wide claim for memory that may come from any NUMA node. The new domctl installs the full claim set atomically, and the allocator is updated so that claim checks and claim consumption follow the new semantics.
This adds:
1. installing multi-node claim sets atomically,
2. protecting claimed pages from other claim requests and allocations, and
3. redeeming held claims when satisfying allocations.
"redeem" is used throughout the patch (here, in alloc_heap_pages() and in the public header) but never defined. Could you add a sentence saying what it means? E.g.:
Redeeming a claim means consuming it through allocation: each page allocated to the domain decreases its claim by one. This differs from releasing a claim, which drops it without any allocation.
Quoted 4 lines
Legacy
XENMEM_claim_pagesbehaviour is preserved; the interface is deprecated and superseded byXEN_DOMCTL_claim_memory.
Quoted 8 lines
Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Bernhard Kaindl <[email protected]>---
The v7 design document submitted ahead of this series may help with review. It explains the background, design rationale, and implementation details.Rendered version: https://xen.kaindl.dev/claims-v7-design/designs/claims
Very useful, thanks!
tools/libs/ctrl/xc_domain.c int xc_domain_remove_from_physmap(xc_interface *xch, · line 1073
Quoted 10 lines
[ ... 44 lines skipped ... ]
tools/libs/ctrl/xc_domain.c +1
-0@@ -1070,6 +1070,34 @@ int xc_domain_remove_from_physmap(xc_interface *xch,return xc_memory_op (xch, XENMEM_remove_from_physmap, &xrfp,sizeof (xrfp)); } +/* Set or get memory claims for a domain. */
This comment may not be needed: the function is already documented in the header file.
Quoted 2 lines
[ ... 26 lines skipped ... ]
+/*XENMEM_claim_pagesis deprecated; usexc_domain_claim_memory()instead. */
Shouldn't XENMEM_claim_pages then be formally deprecated, e.g. with a warning when it is used? I don't know whether Xen has such a practice, but it would notify users without breaking the hypercall ABI.
Quoted 9 lines
int xc_domain_claim_pages(xc_interface *xch,
uint32_tdomid, unsigned longnr_pages)xen/common/domain.c +0
-0
xen/common/domain.c int domain_kill(struct domain *d) · line 1325
Quoted 8 lines
xen/common/domain.c +4
-1@@ -1319,7 +1319,10 @@ int domain_kill(struct domain *d)rspin_barrier (&d->domain_lock);argo_destroy (d);vnuma_destroy (d->vnuma); -domain_set_outstanding_pages (d,0 ); +/* Release all outstanding claims of the domain. */ +domain_set_claim_entries (d,1 , &(xen_memory_claim_t ){ + .target =XEN_DOMCTL_CLAIM_MEMORY_HOST , .pages =0 , + });
Installing an empty host-wide claim to release everything works, but the reader has to go through domain_set_claim_entries() to see that it can't fail and ends up in release_existing. Could page_alloc.c instead export a small domain_release_claims(d) helper, taking the locks and calling domain_recall_node_claims() + domain_release_host_claims()? That keeps the release helpers static and makes this call self-explanatory.
Quoted 30 lines
[ ... 3 lines skipped ... ]
xen/common/domctl.c +21
-0@@ -51,6 +51,57 @@ static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, MAX_NUMNODES); } +/* Set or get memory claims for a domain. */ +static int claim_memory (struct domain *d, +struct xen_domctl_claim_memory *uinfo,bool *copyback) +{ +xen_memory_claim_t *entries; +int rc = -EFAULT ; + +/* Reject LLC coloring; alloc_color_heap_page() does not handle claims. */ +if ( llc_coloring_enabled ) +return -EOPNOTSUPP ; + +switch ( uinfo->mode ) + { +case XEN_DOMCTL_CLAIM_MEMORY_SET : +if ( !uinfo->nr_entries ) +return -EINVAL ; +if ( uinfo->nr_entries >MAX_NUMNODES +1 ) +return -E2BIG ; +break ; +case XEN_DOMCTL_CLAIM_MEMORY_GET : +if ( uinfo->nr_entries >MAX_NUMNODES +1 )
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 84
Quoted 10 lines
xen/common/domctl.c +10
-0+ uinfo->nr_entries =MAX_NUMNODES +1 ; +break ; +default : +return -EOPNOTSUPP ; + } +if ( d->is_dying ) +return -ESRCH ; + + entries =xmalloc_array (xen_memory_claim_t, uinfo->nr_entries); +if ( entries ==NULL )
Nit: !entries.
Quoted 8 lines
xen/common/domctl.c +8
-0+return -ENOMEM ; + +switch ( uinfo->mode ) + { +case XEN_DOMCTL_CLAIM_MEMORY_SET : +if ( !copy_from_guest (entries, uinfo->claim_set, uinfo->nr_entries) ) + rc =domain_set_claim_entries (d, uinfo->nr_entries, entries); +break ;
xen/common/domctl.c static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask, · line 102
Quoted 10 lines
xen/common/domctl.c +10
-0+case XEN_DOMCTL_CLAIM_MEMORY_GET : + rc =domain_get_claim_entries (d, &uinfo->nr_entries, entries); + *copyback = true; +if ( !rc &©_to_guest (uinfo->claim_set, entries, + uinfo->nr_entries) ) + rc = -EFAULT ; +break ; + } +xfree (entries); +return rc;
Blank line before the final return.
Quoted 17 lines
[ ... 128 lines skipped ... ]
xen/common/domctl.c +12
-0@@ -858,6 +805,253 @@ static void check_and_stop_scrub(struct page_info *head) } } +/* Release claims from the host-wide claims pool of the domain. */ +static unsigned long domain_release_host_claims (struct domain *d, +unsigned long release) +{ +unsigned long host_wide_claims; + +ASSERT (d->outstanding_pages >= d->node_claims); + host_wide_claims = d->outstanding_pages - d->node_claims; + +if ( release > host_wide_claims ) + release = host_wide_claims; +
xen/common/domctl.c static void check_and_stop_scrub(struct page_info *head) · line 829
Quoted 10 lines
xen/common/domctl.c +10
-0+ASSERT (release <= outstanding_claims); + + outstanding_claims -= release; + d->outstanding_pages -= release; + +return release; +} + +/* Release claims from the node-specific claims pool of the domain. */ +static unsigned long domain_release_node_claims(
I'm a bit confused by the naming of domain_release_node_claims() and domain_recall_node_claims(): both release node claims, the only difference being that the former targets one given node while the latter walks all nodes. I'd suggest naming them after that:
domain_release_node_claim(d, node, pages); /* one given node */
domain_release_any_node_claims(d, pages); /* any node */
xen/common/domctl.c static void check_and_stop_scrub(struct page_info *head) · line 830
Quoted 1 line
xen/common/domctl.c +1
-0+ struct domain *d, nodeid_t node, unsigned long release)
Nit: the parameters fit when aligned after the (, like domain_release_host_claims() above.
Quoted 27 lines
[ ... 42 lines skipped ... ]
xen/common/domctl.c +26
-0+/* Get claim entries for the outstanding claims of a domain. */ +int domain_get_claim_entries (struct domain *d,uint32_t *nr_entries, +xen_memory_claim_t *entries) +{ +unsigned int needed_entries =1 , max_entries = *nr_entries; +nodeid_t node; + +/* If max_entries is 0, only the number of needed entries is returned. */ +if ( max_entries ) + { +memset (entries,0 ,sizeof (*entries) * max_entries); + entries[0 ].target =XEN_DOMCTL_CLAIM_MEMORY_HOST ; + } + +spin_lock (&heap_lock); +if ( max_entries ) + { +ASSERT (d->outstanding_pages >= d->node_claims); + entries[0 ].pages = d->outstanding_pages - d->node_claims; + } + +for_each_online_node ( node ) + { +if ( !d->claims[node] ) +continue ; +
xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+if ( needed_entries < max_entries ) + { + entries[needed_entries].target = node; + entries[needed_entries].pages = d->claims[node]; + } + needed_entries++; + } +spin_unlock (&heap_lock); + *nr_entries = needed_entries; +return needed_entries <= max_entries ?0 : -ERANGE ;
Blank line before the final return.
xen/common/domctl.c
Quoted 7 lines
xen/common/domctl.c +7
-0+} + +/* Validate a set of claim records and install it if valid. */ +int domain_set_claim_entries (struct domain *d,uint32_t nr_entries, +const xen_memory_claim_t *claim_set) +{ +uint64_t avail_pages, request, node_requests =0 , host_requests =0 ;
I think native types would fit better for avail_pages and request. What about:
unsigned long avail_pages, request;
uint64_t node_requests = 0, host_requests = 0;The accumulators can stay uint64_t: node_requests + host_requests + domain_tot_pages(d) can exceed UINT_MAX, which would not fit in a 32-bit unsigned long.
With that, you would need to add this check just before request = claim_set[i].pages, since pages is a uint64_t, to make sure there is no narrowing conversion:
if ( claim_set[i].pages > d->max_pages )
goto out;As d->max_pages is an unsigned int, claim_set[i].pages is then guaranteed to fit in request.
xen/common/domctl.c
Quoted 2 lines
xen/common/domctl.c +2
-0+nodemask_t nodes; +uint32_t node;
And here:
unsigned int node;
xen/common/domctl.c
Quoted 3 lines
xen/common/domctl.c +3
-0+int ret = -EINVAL ; +bool host_request_seen = false, legacy_semantics = false; +
I think it would be worth stating nr_entries != 0 as a precondition, or adding a check:
if ( !nr_entries )
return ret;
I understand the only caller, claim_memory(), already checks it, but with nr_entries == 0 this function silently goes to release_existing and drops all existing claims, which a future caller may not expect.
Quoted 7 lines
xen/common/domctl.c +7
-0+/* +* Two locks are needed here: +* - d->page_alloc_lock: protects accesses to d->{tot,max,extra}_pages. +* - heap_lock: protects accesses to total_avail_pages, node_avail_pages, +* avail[], scrub state and all total and node-specific claim counters. +*/ +nrspin_lock (&d->page_alloc_lock);
xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+spin_lock (&heap_lock); + +/* +* Phase 1: Check and process the individual claim entries. Check +* the node-specific claim requests, and validate them against the +* available memory. Track legacy and host-wide claim requests and +* prepare validation of the combined claims in Phase 2. +*/ +nodes_clear (nodes); +for (uint32_t i =0 ; i < nr_entries; i++ )
Nit: unsigned int would fit better for i. Also, avail_pages, request and node are only used inside the loops, so their scope can be reduced.
Quoted 4 lines
xen/common/domctl.c +4
-0+ { + node = claim_set[i].target; + request = claim_set[i].pages; +
xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+if ( claim_set[i].cmd )/* Reserved for possible future use. */ + goto out; + +switch ( node ) + { +case XEN_DOMCTL_CLAIM_MEMORY_LEGACY : + legacy_semantics = true; + fallthrough; +case XEN_DOMCTL_CLAIM_MEMORY_HOST : +if ( host_request_seen )/* Only one host-wide request allowed. */
Nit: maybe a gdprintk() here, so the caller can tell why the request was rejected?
xen/common/domctl.c
Quoted 6 lines
xen/common/domctl.c +6
-0+ goto out; + + host_request_seen = true; + host_requests = request; +continue ; + }
Missing default label here (MISRA C:2012 Rule 16.4):
default:
break;Quoted 5 lines
xen/common/domctl.c +5
-0+ +/* Node-specific claim. */ +if ( node >=MAX_NUMNODES || !node_online (node) ) + { + ret = -ENOENT ;/* Node not valid or not online. */
xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+ goto out; + } + +if (nodemask_test (node, &nodes) )/* Only one request per node. */ + goto out; +node_set (node, nodes); + +ASSERT (node_avail_pages[node] >= claimed_pages[node]); + avail_pages = node_avail_pages[node] - claimed_pages[node]; +
Nit: could you add a short comment explaining why d->claims[node] is added back? Something like:
/* The new claim replaces the domain's current one on this node. */
xen/common/domctl.c
Quoted 5 lines
xen/common/domctl.c +5
-0+if ( request > avail_pages + d->claims[node] ) + { + ret = -ENOMEM ;/* Request exceeds available memory on node. */ + goto out; + }
I'm not sure a comment describing behaviour that happens later makes sense here.
xen/common/domctl.c
Quoted 12 lines
xen/common/domctl.c +12
-0+ node_requests += request;/* The combined total is checked below. */ + } + +/* +* Phase 2: Validate the combined claim set. +* Apply legacy semantics if needed, then check that the total fits +* within d->max_pages and within the available unclaimed heap memory. +* A zero new-claim request is a pure release; skip validation entirely. +*/ +if ( legacy_semantics ) + { +if ( node_requests )
Shouldn't there be a debug trace saying that node-specific requests are not supported with legacy semantics?
xen/common/domctl.c
Quoted 12 lines
xen/common/domctl.c +12
-0+ goto out; + +if ( !host_requests ) + goto release_existing; + +/* +* Legacy semantics require the request to exceed the current +* allocation; the request is then reduced by the current allocation. +* Existing outstanding claims are not allowed. +*/ +if ( d->outstanding_pages || + host_requests <=domain_tot_pages (d) )
This fits on one line.
xen/common/domctl.c
Quoted 12 lines
xen/common/domctl.c +12
-0+ goto out; + + host_requests -=domain_tot_pages (d); + } +else if ( !host_requests && !node_requests ) + goto release_existing; + +/* Ensure new claims plus domain_tot_pages() fit into d->max_pages. */ +if ( host_requests > d->max_pages || + node_requests > d->max_pages - host_requests || + node_requests + host_requests +domain_tot_pages (d) > d->max_pages ) + goto out;
With the per-entry check suggested above:
if ( claim_set[i].pages > d->max_pages )
goto out;each entry is at most UINT_MAX, so:
- host_requests <= UINT_MAX, as it comes from a single entry;
- domain_tot_pages(d) <= UINT_MAX;
- node_requests <= MAX_NUMNODES * UINT_MAX, as there is at most one entry per node. With MAX_NUMNODES = 64, that is below 2^38.
The sum is computed in uint64_t, since node_requests is a uint64_t, and stays below 2^39, so it can't overflow. The first two conditions only guard against that overflow and are implied by the third one, so this can be reduced to:
if ( node_requests + host_requests + domain_tot_pages(d) > d->max_pages )
goto out;xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+ +/* Ensure new claims fit in the total unclaimed heap memory. */ +ASSERT (total_avail_pages >= outstanding_claims); +if ( node_requests + host_requests > + (total_avail_pages - outstanding_claims) + d->outstanding_pages ) + { +/* New claims would exceed available unclaimed memory. */ + ret = -ENOMEM ; + goto out; + }
Nit: a local would make this easier to read, e.g.:
unsigned long unclaimed = total_avail_pages - outstanding_claims;
Quoted 4 lines
xen/common/domctl.c +4
-0+ + release_existing: +/* Phase 3: Remove the old claims before installing the new claims. */ +domain_recall_node_claims (d, d->node_claims);
xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+domain_release_host_claims (d, d->outstanding_pages); +ASSERT (!d->node_claims && !d->outstanding_pages); + +/* Phase 4: Install new node-specific and host-wide claims. */ + d->node_claims = node_requests; + d->outstanding_pages = host_requests + node_requests; + outstanding_claims += d->outstanding_pages; + +/* Phase 5: Install the individual node claims. */ +for (uint32_t i =0 ; i < nr_entries; i++ )
Nit: unsigned int here too.
Quoted 5 lines
xen/common/domctl.c +5
-0+ { + node = claim_set[i].target; + request = claim_set[i].pages; +if ( node >=MAX_NUMNODES )/* Skip non-node claim targets. */ +continue ;
xen/common/domctl.c
Quoted 10 lines
xen/common/domctl.c +10
-0+ +ASSERT (!d->claims[node]); + d->claims[node] = request; + claimed_pages[node] += request; + } + ret =0 ; + out: +spin_unlock (&heap_lock); +nrspin_unlock (&d->page_alloc_lock); +return ret;
Blank line before the label and before the final return.
Quoted 9 lines
xen/common/domctl.c +2
-0+} + static struct page_info *get_free_buddy(unsigned int zone_lo, unsigned int zone_hi, unsigned int order, unsigned int memflags,@@ -907,6 +1101,18 @@ static struct page_info *get_free_buddy(unsigned int zone_lo,*/ for ( ; ; ) {
xen/common/domctl.c static struct page_info *get_free_buddy(unsigned int zone_lo, · line 1113
Quoted 10 lines
xen/common/domctl.c +10
-0+unsigned long avail_pages; + +ASSERT (node_avail_pages[node] >= claimed_pages[node]); + avail_pages = node_avail_pages[node] - claimed_pages[node]; + +if ( d && !(memflags & MEMF_no_refcount) ) + avail_pages += d->claims[node]; + +/* Skip this node if memory and claims do not permit this request. */ +if ( avail_pages < (1UL << order) )
Nit: 1UL << order is now used twice per node iteration (here and in the avail[node][zone] check below). What about a local, as alloc_heap_pages() already does:
unsigned long request = 1UL << order;
Quoted 34 lines
[ ... 13 lines skipped ... ]
xen/common/domctl.c +13
-8@@ -1047,13 +1254,13 @@ static struct page_info *alloc_heap_pages( } } -ASSERT (avail[node][zone] >= request); - avail[node][zone] -= request; -ASSERT (total_avail_pages >= request); - total_avail_pages -= request; -ASSERT (node_avail_pages[node] >= request); - node_avail_pages[node] -= request; - +/* +* Ensure the domain's claims are fully redeemed before deducting +* the allocation from the page counters, otherwise the host or the +* node might be left with more claims than available pages. This would +* break the invariants claimed_pages[node] <= node_avail_pages[node] +* and outstanding_claims <= total_avail_pages. +*/ if ( d && d->outstanding_pages && !(memflags & MEMF_no_refcount) ) {/* @@ -1071,13 +1278,34 @@ static struct page_info *alloc_heap_pages(* the domain being destroyed before creation is finished. Losing part * of the claim makes no difference. */ -unsigned long outstanding =min (d->outstanding_pages +0UL , request); +unsigned long redeem =min (d->outstanding_pages +0UL , request); + +BUG_ON (redeem > outstanding_claims); + +/* Maintain invariant: claimed_pages[node] <= node_avail_pages[node] */ + redeem -=domain_release_node_claims (d, node, redeem);
xen/common/domctl.c static struct page_info *alloc_heap_pages( · line 1294
Quoted 10 lines
xen/common/domctl.c +7
-3-BUG_ON (outstanding > outstanding_claims); - outstanding_claims -= outstanding; - d->outstanding_pages -= outstanding; +if ( redeem ) + { +/* +* Maintain the invariant outstanding_claims <= total_avail_pages: +* Redeem from the host-wide claims pool and if needed from other +* nodes' claims pools to maintain that outstanding_claims do not +* exceed total_avail_pages after deducting the allocation.
Nit: this repeats the outstanding_claims <= total_avail_pages statement from the first line. I'd trim it to:
/*
* Redeem from the host-wide claims pool and, if needed, from
* other nodes' claims pools to keep outstanding_claims <=
* total_avail_pages after deducting the allocation. */
xen/common/domctl.c static int reserve_offlined_page(struct page_info *head) · line 1488
Quoted 6 lines
[ ... 25 lines skipped ... ]
xen/common/domctl.c +1
-0@@ -1256,6 +1485,43 @@ static int reserve_offlined_page(struct page_info *head) count++; } +/* Offlining pages may violate claim invariants; restore them if needed. */
Which invariants, and in which case does offlining break them? I can work it out because I'm reading the series in order, but someone reading this code once it's committed won't have that context. Please spell both out here, or in the commit message.
Quoted 10 lines
[ ... 39 lines skipped ... ]
xen/include/public/domctl.h +1
-0@@ -1276,6 +1276,42 @@ struct xen_domctl_get_domain_state {uint64_t unique_id;/* Unique domain identifier. */ }; +struct xen_memory_claim {
xen/include/public/domctl.h struct xen_domctl_get_domain_state { · line 1289
Quoted 10 lines
xen/include/public/domctl.h +10
-0+uint64_aligned_t pages;/* Number of pages to claim. */ +uint32_t target;/* NUMA node or special target constant. */ +uint32_t cmd;/* Reserved, must be zero. */ +}; +typedef struct xen_memory_claim xen_memory_claim_t ; +DEFINE_XEN_GUEST_HANDLE (xen_memory_claim_t); + +/* Special claim targets for the target field of xen_memory_claim_t. */ +#define XEN_DOMCTL_CLAIM_MEMORY_HOST 0x80000000U/* Host-wide claims. */ +#define XEN_DOMCTL_CLAIM_MEMORY_LEGACY 0x40000000U/* Legacy semantics. */
How did you choose these values? IMO the rationale belongs in the commit message.
xen/include/public/memory.h DEFINE_XEN_GUEST_HANDLE(xen_mem_sharing_op_t); · line 573
Quoted 11 lines
[ ... 44 lines skipped ... ]
xen/include/public/memory.h +2
-0@@ -569,6 +569,8 @@ DEFINE_XEN_GUEST_HANDLE(xen_mem_sharing_op_t);* for 10, only 7 additional pages are claimed. * * Caller must be privileged or the hypercall fails. +* +* Note: This hypercall is superseded by XEN_DOMCTL_claim_memory.
Same question as for xc_domain_claim_pages() above: should XENMEM_claim_pages emit a deprecation warning?
xen/include/xen/sched.h struct domain · line 425
Quoted 13 lines
[ ... 27 lines skipped ... ]
xen/include/xen/sched.h +4
-0@@ -419,6 +419,10 @@ struct domainunsigned int xenheap_pages;/* pages allocated from Xen heap */ /* Pages claimed but not possessed, protected by global heap_lock. */ unsigned int outstanding_pages; +unsigned int node_claims;/* Sum of per-node claims. */ +/* Domain objects use dedicated pages, leaving room for per-node claims. */ +unsigned int claims[MAX_NUMNODES ];/* Per-NUMA-node claims. */ +
Stray blank line added in the middle of the field group.
Signature · 1 line
Baptiste Le Duc <[email protected]>
Re: [PATCH v7 1/3] xen/mm: Introduce per-node free page counter
In reply to Bernhard KaindlQuoted 8 lines · Bernhard Kaindl
Add node_avail_pages[], updated under
heap_lockin sync with avail[node][zone] to cache the per-node sum of free pages.Use it in
avail_node_heap_pages()to avoid summing all zones on each call. Guard it with nodeid <MAX_NUMNODESandnode_online(nodeid).[...]
Signature · 1 line
Baptiste Le Duc <[email protected]>