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

[PATCH v8 7/8] xen/domctl: add XEN_DOMCTL_get_memory_claims


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx, Baptiste Le Duc <baptiste.le-duc@xxxxxxxxxx>
  • From: Bernhard Kaindl <bernhard.kaindl@xxxxxxxxxx>
  • Date: Tue, 01 Dec 2026 18:29:47 +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=pAtQ0EXE9ray5EuAKRWp8ryB02yuergXDor0CZGGg1E=; b=VQ2R0pJdlnALrh+GeTn0dONzzii+t5f0JG+xHzQcOoZOhiBlXeP9wpdTbFfyAVNQTI2Tj0rWFy25DioyiATzQFf/MqpZsMwh6oTVlai96guPT+eeXZOnSLCzcDxoPQYm5o9ogB7JnaO8pj1TF5gtTRdcYiyr/jDMiUsGaOpldiljYboreW/0bSJc/Vbmzgi/q8LpIO0djO8htbbWASS7srl09wey/hn5hsdvEjbthz+foJCXAVdAgNQKNumU9kIpnxNG09CsIS5ohgqgR2vqnBm9Bp5EEYUpHpXS0BHD+Nx+WYPSAJkd1uEi7wFrdfi+agXMOo/+xTF2mXp5SB9YkA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=CzCC/V/sKZOOu05u4fI+m1o+9xChyIv777h702rm6+sAZ0HWj9hT0OPFSTjBglStn/gcNrL1mkC35hnZvzrCHAyqqVU3H2Y5sV0OYyyKIwypLvBi/CKQzDSw4B6x0UWGmS4dIpEgriVmk6hRwXbX7jJr4urHLeX5tmWbjhKRqBqyqruXBqbLMMyxz5lMlhly9deFOEUgpHiIJoLmZfAcdBBX48ht3VdhxvferI6Xll54thh/GxblrrbdXdZJhcwYBkkY3jJovG6NxUl7Qw/z0UUBTO9QQLFW7pQESnAfkSTGiFiDT9IBiw02GjhJjjnfy+7gozqx++CCwbnYC+sg0A==
  • 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>, Bernhard Kaindl <bernhard.kaindl@xxxxxxxxxx>
  • Delivery-date: Tue, 01 Dec 2026 17:30:37 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

XEN_DOMCTL_getdomaininfo reports a domain's aggregate claim
count in outstanding_pages, without a per-node breakdown.

An allocation can fail even when free pages remain, because claims can
make those pages unavailable.  The aggregate count does not show which
nodes a domain's claims cover, limiting diagnosis of such failures.

Add XEN_DOMCTL_get_memory_claims to report nonzero claims by target.
Under FLASK, reuse the `getdomaininfo` permission.

- Take a private snapshot under heap_lock and copy it to the
  caller after unlocking. RCU protects the domain's lifetime;
  heap_lock serializes the snapshot with claim updates, so
  the query does not need the domctl lock.

- Use nr_claims as the array capacity on input and return the
  required entry count. If capacity is insufficient, return
  -ENOBUFS with that count without copying any entries.

- The caller can query the required count with zero capacity and then
  allocate an array. Claims may change between calls, so the next call
  can still return -ENOBUFS with a different required count.

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:
- Refactor the submission into smaller topic-focused patches.
- Split claim queries into XEN_DOMCTL_get_memory_claims.
- Flatten control flow in domain_get_claims().
- Handle it without the domctl lock; RCU and heap_lock are sufficient.
- Use xvzalloc_array() for zero-initialized output storage;
  the buffer need not be physically contiguous.
- Omit the host-wide entry when its claim is zero, so every returned
  entry is a nonzero claim.
- Like XEN_DOMCTL_get_vcpu_msrs and XENMEM_get_vnumainfo do,
  return -ENOBUFS instead of -ERANGE when the buffer is too small.
---
 xen/include/public/domctl.h         | 33 +++++++++++++++++++++++--
 xen/xsm/flask/policy/access_vectors |  3 ++-
 xen/xsm/flask/hooks.c               |  3 +++
 xen/include/xen/mm.h                |  5 ++++
 xen/common/domctl.c                 | 36 +++++++++++++++++++++++++++
 xen/common/page_alloc.c             | 49 +++++++++++++++++++++++++++++++++++++
 6 files changed, 126 insertions(+), 3 deletions(-)

diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h
index 76d6b81687..8e0772eea8 100644
--- a/xen/include/public/domctl.h
+++ b/xen/include/public/domctl.h
@@ -1280,6 +1280,7 @@ struct xen_domctl_get_domain_state {
 };
 
 /* XEN_DOMCTL_set_memory_claims */
+/* XEN_DOMCTL_get_memory_claims */
 /*
  * Special target for a host-wide claim, not associated with a NUMA node.
  * Other values with bit 31 set are reserved for future special targets.
@@ -1331,10 +1332,37 @@ DEFINE_XEN_GUEST_HANDLE(xen_domctl_memory_claim_t);
  *  ENOMEM     - allocation failure or not enough unclaimed memory,
  *               in total or on a node
  */
+
+/*
+ * XEN_DOMCTL_get_memory_claims
+ *
+ * Read the outstanding memory claims of a domain: the host-wide claim,
+ * if any, followed by the per-node claims in ascending node order.
+ *
+ * nr_claims is the capacity of the claims array on input.  On output, it is
+ * the number of claims of the domain, which is 0 for a domain without
+ * claims.  If that exceeds the capacity, no claims are written and ENOBUFS
+ * is returned, so a capacity of 0 queries the number of claims.  A capacity
+ * above the maximum number of nodes plus one for the host-wide claim is
+ * clamped.
+ *
+ * XEN_DOMCTL_get_memory_claims fails with:
+ *  ESRCH       - the domain does not exist
+ *  EINVAL      - pad not zero
+ *  ENOMEM      - allocation failure
+ *  ENOBUFS     - the capacity is below the number of claims
+ *  EFAULT      - the claims array or domctl cannot be copied to the guest
+ */
+
+/* The argument of XEN_DOMCTL_{set,get}_memory_claims. */
 struct xen_domctl_memory_claims {
-    /* IN: array of claims. */
+    /* For set, IN: claims to install.  For get, OUT: claims of the domain. */
     XEN_GUEST_HANDLE_64(xen_domctl_memory_claim_t) claims;
-    uint32_t nr_claims;     /* IN: number of claims. */
+    /*
+     * For set, IN: number of claims.
+     * For get, IN: capacity of claims, OUT: number of claims of the domain.
+     */
+    uint32_t nr_claims;
     uint32_t pad;           /* IN: Must be zero. */
 };
 
@@ -1431,6 +1459,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_set_llc_colors                89
 #define XEN_DOMCTL_get_domain_state              90 /* stable interface */
 #define XEN_DOMCTL_set_memory_claims             91
+#define XEN_DOMCTL_get_memory_claims             92
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
diff --git a/xen/xsm/flask/policy/access_vectors 
b/xen/xsm/flask/policy/access_vectors
index 0658853f23..25a56010c4 100644
--- a/xen/xsm/flask/policy/access_vectors
+++ b/xen/xsm/flask/policy/access_vectors
@@ -136,7 +136,8 @@ class domain
     getaffinity
 # XEN_DOMCTL_scheduler_op with XEN_DOMCTL_SCHEDOP_getinfo
     getscheduler
-# XEN_DOMCTL_getdomaininfo, XEN_SYSCTL_getdomaininfolist
+# XEN_DOMCTL_getdomaininfo, XEN_SYSCTL_getdomaininfolist,
+# XEN_DOMCTL_get_memory_claims
     getdomaininfo
 # XEN_DOMCTL_getvcpuinfo
     getvcpuinfo
diff --git a/xen/xsm/flask/hooks.c b/xen/xsm/flask/hooks.c
index f9b6eab109..06fb73c357 100644
--- a/xen/xsm/flask/hooks.c
+++ b/xen/xsm/flask/hooks.c
@@ -884,6 +884,9 @@ static int cf_check flask_domctl(struct domain *d, struct 
xen_domctl *op)
     case XEN_DOMCTL_set_memory_claims:
         return flask_claim_pages(d);
 
+    case XEN_DOMCTL_get_memory_claims:
+        return flask_getdomaininfo(d);
+
     default:
         return avc_unknown_permission("domctl", op->cmd);
     }
diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h
index 4402b6e14e..8374c4ee0b 100644
--- a/xen/include/xen/mm.h
+++ b/xen/include/xen/mm.h
@@ -132,6 +132,10 @@ int populate_pt_range(unsigned long virt, unsigned long 
nr_mfns);
 /* Claim handling */
 struct xen_domctl_memory_claim;
 struct claim_request {
+    /*
+     * Set: number of entries.
+     * Get: capacity of entries[] on input, number provided/needed on output.
+     */
     unsigned int nr_entries;
     struct xen_domctl_memory_claim *entries;
     /* Sums of pages, computed by domain_set_claims(). */
@@ -140,6 +144,7 @@ struct claim_request {
 };
 
 int domain_set_claims(struct domain *d, struct claim_request *request);
+int domain_get_claims(const struct domain *d, struct claim_request *request);
 
 unsigned long __must_check domain_adjust_tot_pages(struct domain *d,
     long pages);
diff --git a/xen/common/domctl.c b/xen/common/domctl.c
index 55906bd301..5784bfa51c 100644
--- a/xen/common/domctl.c
+++ b/xen/common/domctl.c
@@ -51,6 +51,36 @@ static int xenctl_bitmap_to_nodemask(nodemask_t *nodemask,
                                    MAX_NUMNODES);
 }
 
+/* Take a private snapshot so guest copies do not hold heap_lock. */
+static int get_memory_claims(const struct domain *d,
+                             struct xen_domctl_memory_claims *op)
+{
+    /* A domain has at most one claim per node plus a host-wide one. */
+    struct claim_request response = {
+        .nr_entries = min(op->nr_claims, MAX_NUMNODES + 1U),
+    };
+    struct xen_domctl_memory_claim *entries;
+    int ret;
+
+    if ( op->pad )
+        return -EINVAL;
+
+    entries = xvzalloc_array(struct xen_domctl_memory_claim,
+                             response.nr_entries);
+    if ( !entries )
+        return -ENOMEM;
+
+    response.entries = entries;
+    ret = domain_get_claims(d, &response);
+
+    op->nr_claims = response.nr_entries;
+    if ( !ret && copy_to_guest(op->claims, entries, response.nr_entries) )
+        ret = -EFAULT;
+
+    xvfree(entries);
+    return ret;
+}
+
 static int set_memory_claims(struct domain *d,
                              const struct xen_domctl_memory_claims *op)
 {
@@ -617,6 +647,12 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) 
u_domctl)
         goto domctl_out_unlock_rcuonly;
     }
 
+    case XEN_DOMCTL_get_memory_claims:
+        ret = get_memory_claims(d, &op->u.memory_claims);
+        if ( !ret || ret == -ENOBUFS )
+            copyback = true;
+        goto domctl_out_unlock_rcuonly;
+
     case XEN_DOMCTL_shadow_op:
         if ( op->u.shadow_op.op == XEN_DOMCTL_SHADOW_OP_CLEAN ||
              op->u.shadow_op.op == XEN_DOMCTL_SHADOW_OP_PEEK )
diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index 19a7d2f089..17c51a8b0c 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -762,6 +762,55 @@ int domain_set_claims(struct domain *d, struct 
claim_request *request)
     return ret;
 }
 
+/* Snapshot the nonzero claims, with the host-wide claim first. */
+int domain_get_claims(const struct domain *d,
+                      struct claim_request *request)
+{
+    /* nr_entries is also the return value for the number of claim entries */
+    const unsigned int capacity = request->nr_entries;
+    unsigned int node, count = 0;
+    unsigned int host_claim;
+
+    /* heap_lock guards d->outstanding_pages, d->node_claims, and d->claims. */
+    spin_lock(&heap_lock);
+    ASSERT(d->outstanding_pages >= d->node_claims);
+    host_claim = d->outstanding_pages - d->node_claims;
+
+    if ( host_claim )
+    {
+        if ( count < capacity )
+            request->entries[count] = (struct xen_domctl_memory_claim){
+                .target = XEN_DOMCTL_MEMORY_CLAIM_TARGET_HOST,
+                .pages = host_claim,
+            };
+
+        count++;
+    }
+
+    if ( !d->claims )
+        goto out;
+
+    for_each_online_node ( node )
+    {
+        if ( !d->claims[node] )
+            continue;
+
+        if ( count < capacity )
+            request->entries[count] = (struct xen_domctl_memory_claim){
+                .target = node,
+                .pages = d->claims[node],
+            };
+
+        count++;
+    }
+
+ out:
+    spin_unlock(&heap_lock);
+    request->nr_entries = count;
+
+    return count <= capacity ? 0 : -ENOBUFS;
+}
+
 int domain_set_outstanding_pages(struct domain *d, unsigned long pages)
 {
     int ret = -ENOMEM;

-- 
2.43.0




 


Rackspace

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