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

[PATCH v8 4/8] xen/mm: add domain_set_claims() for new claim requests


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx, Baptiste Le Duc <baptiste.le-duc@xxxxxxxxxx>
  • From: Bernhard Kaindl <bernhard.kaindl@xxxxxxxxxx>
  • Date: Tue, 01 Dec 2026 18:29:44 +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=MNA/clTQV8yx4qvHJw+qv5wdeJFNJi8XLQE+5XdMFHI=; b=P7KX3WATS+Ft3rVE5Yakqpp3YxIARjEqM1WXFYfvio9Xi66yUya4FPUkPzK5nVzwDu3bqJfn01oh55V6jIQXc1BkRfNPM2xMpartQOOS96T7u1zvFoECSfgwXQO9zUIqUVGCRMZDKkPOLN1ODybyLi8UPggk2rKxxvDcrsH3N3b22EljAvmdVgxSo1r2CRN7sP1oCcXtfxT+I24nrFzhTpgNnbuZ9WSoQGOBe8wQWZeKzShAkFDqCdjgWPwod6A8zbWhmbO5hJRwUQWk6MI8KwVpG7WkwSFKA6v1c3JkhkNm1E3y2qBF82pKmsPiXHdqmywGjTMEO7SW7pS4CZaSnA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pcEq9uPFny+WpJaKZcaAkvooBgvzHErHoKyRDireRIr4P+cTOhj+evhBcnsMSuMB9NmjlzyVP/GHhDEMmGDiiGEvb+RwIQOWyR5s05bgOw+a7RhmCuJSu2/b75XeispjZly/F7B59ecFW2G+MEDvQnMlCC8hQZJgUovPBOPS+0p+hp+Enh7xpLWnGqBiWWLv2mBxCiOPMeaaRrLoPTmr+A8eWu2apeTOhh+A+4Yf0yTJOmIJrvT3WLHKNrqMSQoETvDcG94OWp2hOF6k5VSt0H4IxjpRQbhWoGa47elBOd1FUtmZ3Agh6Gpn3FdU/ge5yQyB9onpqSjYne/Dz8d2aw==
  • 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:25 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

A claim request needs validation and capacity checks before any claims
are installed. Take d->page_alloc_lock and:

1. Validate page counts, padding and targets, rejecting duplicates and
   keeping domain_tot_pages(d) plus requested claims within d->max_pages.

2. Take heap_lock, reject requests when the domain has claims,
   and check unclaimed capacity on the host and each requested node.

3. Update all claim counters before releasing heap_lock.

domain_set_claims() has no callers in this commit.
Existing claim handling code is not changed.

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.
- Rename domain_set_claim_entries() to domain_set_claims()
  and separate validation, admission and installation.
- Remove support for replacing claims; no use case requires
  atomic replacement of existing outstanding claims.
- Add struct claim_request to pass the request between the functions.
- Allocate the claims array only for requests with per-node
  claims.
- The new claim path reserves the MSB of the claim target for
  special targets.
- Rename XEN_DOMCTL_CLAIM_MEMORY_HOST (host-wide claim target) to
  XEN_DOMCTL_MEMORY_CLAIM_TARGET_HOST to also match the structure
  xen_domctl_memory_claim, so the domctl names share the same prefix.
---
 xen/include/public/domctl.h |  13 ++++
 xen/include/xen/mm.h        |  12 ++++
 xen/common/page_alloc.c     | 170 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)

diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h
index 4ca8a2d7ca..163e246ff4 100644
--- a/xen/include/public/domctl.h
+++ b/xen/include/public/domctl.h
@@ -1279,6 +1279,19 @@ struct xen_domctl_get_domain_state {
     uint64_t unique_id;      /* Unique domain identifier. */
 };
 
+/*
+ * 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.
+ */
+#define XEN_DOMCTL_MEMORY_CLAIM_TARGET_HOST 0x80000000U
+
+/* Memory claim structure */
+typedef struct xen_domctl_memory_claim {
+    uint64_aligned_t pages; /* Number of claimed pages. */
+    uint32_t target;        /* NUMA node or special target constant. */
+    uint32_t pad;           /* Must be zero. */
+} xen_domctl_memory_claim_t;
+
 struct xen_domctl {
 /* Stable domctl ops: interface_version is required to be 0.  */
     uint32_t cmd;
diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h
index 8f4bba9491..4402b6e14e 100644
--- a/xen/include/xen/mm.h
+++ b/xen/include/xen/mm.h
@@ -128,7 +128,19 @@ mfn_t xen_map_to_mfn(unsigned long va);
  * page range in Xen virtual address space.
  */
 int populate_pt_range(unsigned long virt, unsigned long nr_mfns);
+
 /* Claim handling */
+struct xen_domctl_memory_claim;
+struct claim_request {
+    unsigned int nr_entries;
+    struct xen_domctl_memory_claim *entries;
+    /* Sums of pages, computed by domain_set_claims(). */
+    unsigned long total_pages; /* Of all entries. */
+    unsigned long node_pages;  /* Of the per-node entries. */
+};
+
+int domain_set_claims(struct domain *d, struct claim_request *request);
+
 unsigned long __must_check domain_adjust_tot_pages(struct domain *d,
     long pages);
 int domain_set_outstanding_pages(struct domain *d, unsigned long pages);
diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index df23c31d85..19a7d2f089 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -592,6 +592,176 @@ static void domain_redeem_other_node_claims(struct domain 
*d,
     ASSERT(!pages);
 }
 
+/* Validate a claim request and compute its ->total_pages and ->node_pages. */
+static int domain_validate_claim_request(const struct domain *d,
+                                         struct claim_request *request)
+{
+    nodemask_t seen = NODE_MASK_NONE;
+    bool host_seen = false;
+
+    request->total_pages = request->node_pages = 0;
+    /* d->page_alloc_lock guards d->max_pages and domain_tot_pages(d) */
+    ASSERT(rspin_is_locked(&d->page_alloc_lock));
+
+    for ( unsigned int i = 0; i < request->nr_entries; ++i )
+    {
+        const unsigned int target = request->entries[i].target;
+        const uint64_t target_pages = request->entries[i].pages;
+
+        /* The sum request->total_pages is bounded by max_pages. */
+        if ( !target_pages || request->entries[i].pad ||
+             target_pages > d->max_pages - request->total_pages )
+            return -EINVAL;
+
+        request->total_pages += target_pages;
+
+        if ( target == XEN_DOMCTL_MEMORY_CLAIM_TARGET_HOST )
+        {
+            if ( host_seen )
+                return -EINVAL;
+
+            host_seen = true;
+            continue;
+        }
+
+        /* Bit 31 marks special targets. */
+        if ( target & 0x80000000U )
+            return -EINVAL;
+
+        if ( target >= MAX_NUMNODES || !node_online(target) )
+            return -ENOENT;
+
+        /* Reject duplicated targets. */
+        if ( node_test_and_set(target, seen) )
+            return -EINVAL;
+
+        request->node_pages += target_pages;
+    }
+
+    /*
+     * Pages awaiting assignment are not included in domain_tot_pages(d).
+     * By design, d->page_alloc_lock does not guard the allocation itself.
+     * Builders should allocate pages only after requests to claim memory.
+     */
+    if ( domain_tot_pages(d) > d->max_pages ||
+         request->total_pages > d->max_pages - domain_tot_pages(d) )
+        return -EINVAL;
+
+    return 0;
+}
+
+/* Check if the request's claims can be satisfied using unclaimed memory. */
+static int check_memory_for_claim_request(const struct claim_request *request)
+{
+    /*
+     * heap_lock guards total_avail_pages, outstanding_claims,
+     * node_avail_pages and node_claimed_pages.
+     */
+    ASSERT(spin_is_locked(&heap_lock));
+
+    /* Check if host has enough unclaimed pages for the request. */
+    if ( request->total_pages > total_avail_pages ||
+         outstanding_claims > total_avail_pages - request->total_pages )
+        return -ENOMEM;
+
+    /* Check if each node has enough unclaimed pages for the request. */
+    for ( unsigned int i = 0; i < request->nr_entries; ++i )
+    {
+        const unsigned int target = request->entries[i].target;
+
+        if ( target == XEN_DOMCTL_MEMORY_CLAIM_TARGET_HOST )
+            continue;
+
+        /* The host-wide check bounds this sum by total_avail_pages. */
+        if ( request->entries[i].pages + node_claimed_pages[target] >
+             node_avail_pages[target] )
+            return -ENOMEM;
+    }
+
+    return 0;
+}
+
+/*
+ * Install the claims of the request, using *claims as the per-node array.
+ * On return, *claims is the array for the caller to free: the unused new
+ * one on failure, else the one that was replaced, if any.
+ */
+static int domain_install_claims(struct domain *d,
+                                 const struct claim_request *request,
+                                 unsigned int **claims)
+{
+    unsigned int *new_claims = *claims;
+    int ret;
+
+    ASSERT(rspin_is_locked(&d->page_alloc_lock));
+    spin_lock(&heap_lock);
+    if ( d->outstanding_pages )
+    {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = check_memory_for_claim_request(request);
+    if ( ret )
+        goto out;
+
+    /* Validation bounded the sums by max_pages, so they fit the fields. */
+    d->node_claims = request->node_pages;
+    d->outstanding_pages = request->total_pages;
+    outstanding_claims += d->outstanding_pages;
+    /* Without outstanding claims, a previous array only holds zeros. */
+    *claims = d->claims;
+    d->claims = new_claims;
+
+    for ( unsigned int i = 0; i < request->nr_entries; ++i )
+    {
+        const unsigned int target = request->entries[i].target;
+
+        if ( target == XEN_DOMCTL_MEMORY_CLAIM_TARGET_HOST )
+            continue;
+
+        new_claims[target] = request->entries[i].pages;
+        node_claimed_pages[target] += new_claims[target];
+    }
+
+ out:
+    spin_unlock(&heap_lock);
+    return ret;
+}
+
+/* Atomically set the claims of a domain that has no outstanding claims. */
+int domain_set_claims(struct domain *d, struct claim_request *request)
+{
+    unsigned int *new_claims = NULL;
+    int ret;
+
+    ASSERT(request->nr_entries);
+
+    /* Acquire d->page_alloc_lock for domain_validate_claim_request() */
+    nrspin_lock(&d->page_alloc_lock);
+    ret = domain_validate_claim_request(d, request);
+    if ( ret )
+        goto out;
+
+    if ( request->node_pages )
+    {
+        new_claims = xvzalloc_array(unsigned int, MAX_NUMNODES);
+        if ( !new_claims )
+        {
+            ret = -ENOMEM;
+            goto out;
+        }
+    }
+
+    ret = domain_install_claims(d, request, &new_claims);
+
+ out:
+    nrspin_unlock(&d->page_alloc_lock);
+    xvfree(new_claims);
+
+    return ret;
+}
+
 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®.