[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v8 1/8] xen/mm: protect per-node claims in get_free_buddy()


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx, Baptiste Le Duc <baptiste.le-duc@xxxxxxxxxx>
  • From: Bernhard Kaindl <bernhard.kaindl@xxxxxxxxxx>
  • Date: Tue, 01 Dec 2026 18:29:41 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Ar6qZNnJUFUfSWhi9kVn62ysAa1HW+/4xsHEnFTL298=; b=OI6WYWMI1FjgsZgpJknBjLMIAOUZm9pK3cI6IPF9QS7+x074yC4JXPsf3zlNMRClY/pt/tkDkwEVRRv0uc6pYM0FETYdgCPgIw+hzv6A3mtvy4MdKAXE/5ZHpFFnBKdPaN7MPeTV9srU7YASDvbJL5ztrQK1upz7Pvyv776NFYaDoc8Xj2z+0cQy2Bd8bbvrDJJcuvLThUvjsMlzitxRjWvb58XQriQuVgT7jkZKJpyE1a0BGjuya84sguljBSVVvDn8B/b3bmU9lrrfT19oJ8VrOegay3SCHKuelger2nSvxEMn6Hr+MCPMMwdR+QNNi63ZSk0BSglyj2GkHtAgDA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=sz1KZiArTRVxy+1+Eu6n7vwqrbqO+FnxT9/ArwBc2wCPr3BFKkBwYXISoQ9bezgtvZ7HSZdauGwJv4zG33c3TPaVHsNAe+Jz3fgw9M2/tRq2RW8Xdjzqs49Msoax5Rz5Z3nVqygJ68nPc0yA9nRMJ9qgKIERO001nCR77YRSoEjkQbgI7vDbBo/0nE5wySngP+8E8fyyvcMHM5mTFVglvEplzMQ7+hRSQxpauhT6wkp87aXUmjGdbZqvKrGSnsNszVV3JsdtTd+XbiFYKrCwTurlV9lRT72gtS/mYkQJI8GtEsXakM/M46torMbHasnVfHhMT+0DrYp4k31ntqC3XQ==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, "Daniel P. Smith" <dpsmith@xxxxxxxxxxxxxxxxxxxx>, Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>, Community Manager <community.manager@xxxxxxxxxxxxxx>, Juergen Gross <jgross@xxxxxxxx>, Bernhard Kaindl <contact@xxxxxxxxxxxxxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, Bernhard Kaindl <bernhard.kaindl@xxxxxxxxxx>
  • Delivery-date: Tue, 01 Dec 2026 17:30:13 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Before calling get_free_buddy(), alloc_heap_pages() checks
the outstanding claims of a domain. This check is unchanged.

However, allow an allocation only if the node has enough unclaimed
memory after crediting any eligible claim on that node:

- Before searching a node's zones, subtract its outstanding claims
  from its free page count.

- Clamp the result at zero, as memory errors detected during Xen
  or platform scrubbing can trigger machine-check recovery and
  sysctl(XEN_SYSCTL_page_offline_op) may offline free pages as well:
  Claims protect against competing allocations, but can't protect
  against physical memory loss.

- For ref-counted allocations, credit the domain's claim on that node
  to the available capacity. Allocations with MEMF_no_refcount receive
  no credit, matching the existing host-wide check, since they do not
  redeem claims. If the capacity is below the request, skip this node.

No functional change without per-node claims: the added check skips
nodes whose capacity is below the request. The existing per-zone loop
would reject such nodes anyway, so allocation outcomes are unchanged.

Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
Assisted-by: Vibe:Mistral-Medium-3.5, Copilot:claude-opus-5-5 # review
Assisted-by: Copilot:gpt-6.2-astra, Copilot:gpt-6.2-sol # review
Signed-off-by: Bernhard Kaindl <bernhard.kaindl@xxxxxxxxxx>
---
v8:
- Make d->claims a pointer to a separately allocated array,
  or NULL when no per-node claims are installed. In v5-v7,
  the array was embedded in struct domain. Allocate it only
  when needed during domain build, leaving space in struct
  domain for permanent state.
- Replace the assertion that free memory covers outstanding
  claims with a subtraction clamped at zero. Free pages can
  be offlined by XEN_SYSCTL_page_offline_op or machine-check
  recovery, including for errors detected during scrubbing.
- Refactor the submission into smaller topic-focused patches.
- As suggested by Baptiste Le Duc, add request = 1UL << order
  and use it in both locations that need to check the request.
- While at it, fix the incorrect comment above the zone check:

  /* Check if target node can support the allocation. */
  if ( !avail[node] || (avail[node][zone] < request) )
       continue;

  to:

  /* Skip zones on the node that can't support the allocation. */
  if ( !avail[node] || (avail[node][zone] < request) )
       continue;

v5:
- Introduce the per-node claims array to track outstanding claims
  on individual NUMA nodes.
- The previous, functionally equivalent per-node claims protection
  method is deployed on all Citrix XenServer 9 installations.
---
 xen/include/xen/sched.h |  2 ++
 xen/common/page_alloc.c | 22 ++++++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index 644cbf70d0..2ee5354bae 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -429,6 +429,8 @@ struct domain
     unsigned int     xenheap_pages;     /* pages allocated from Xen heap */
     /* Pages claimed but not possessed, protected by global heap_lock. */
     unsigned int     outstanding_pages;
+    /* Per-node claims array or NULL, protected by global heap_lock. */
+    unsigned int    *claims;
     unsigned int     max_pages;         /* maximum value for 
domain_tot_pages() */
     unsigned int     extra_pages;       /* pages not included in 
domain_tot_pages() */
 
diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index 56df17ea6c..fba5a907e3 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -491,6 +491,9 @@ static DEFINE_SPINLOCK(heap_lock);
 /* Total outstanding claims by all domains */
 static unsigned long outstanding_claims;
 
+/* Outstanding claims of all domains on each NUMA node. */
+static unsigned long node_claimed_pages[MAX_NUMNODES];
+
 static unsigned long avail_heap_pages(
     unsigned int zone_lo, unsigned int zone_hi, unsigned int node)
 {
@@ -863,6 +866,7 @@ static struct page_info *get_free_buddy(unsigned int 
zone_lo,
                                         unsigned int order, unsigned int 
memflags,
                                         const struct domain *d)
 {
+    const unsigned long request = 1UL << order;
     nodeid_t first, node = MEMF_get_node(memflags), req_node = node;
     nodemask_t nodemask = node_online_map;
     unsigned int j, zone, nodemask_retry = 0;
@@ -907,10 +911,23 @@ static struct page_info *get_free_buddy(unsigned int 
zone_lo,
      */
     for ( ; ; )
     {
+        /* Offlining can reduce free pages below outstanding claims. */
+        unsigned long available =
+            node_avail_pages[node] > node_claimed_pages[node]
+            ? node_avail_pages[node] - node_claimed_pages[node]
+            : 0;
+
+        /* Ref-counted allocations may use the domain's per-node claim. */
+        if ( d && !(memflags & MEMF_no_refcount) && d->claims )
+            available += d->claims[node];
+
+        if ( available < request )
+            goto next_node;
+
         zone = zone_hi;
         do {
-            /* Check if target node can support the allocation. */
-            if ( !avail[node] || (avail[node][zone] < (1UL << order)) )
+            /* Skip zones on the node that can't support the allocation. */
+            if ( !avail[node] || (avail[node][zone] < request) )
                 continue;
 
             /* Find smallest order which can satisfy the request. */
@@ -936,6 +953,7 @@ static struct page_info *get_free_buddy(unsigned int 
zone_lo,
             }
         } while ( zone-- > zone_lo ); /* careful: unsigned zone may wrap */
 
+ next_node:
         if ( (memflags & MEMF_exact_node) && req_node != NUMA_NO_NODE )
             return NULL;
 

-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.