|
[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
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
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |