DRA: pass memory policy settings in claim configs #857
allocations.clone() in topology-aware-policy.go:764 reconstructs each claim with only Grant and results, dropping this new field. Reconfigure clones allocations before restoring them, so a successful reload loses claim memory policies: new containers receive no policy, and preparing an existing claim again fails the consistency check. Copy mpols in the claim-cloning path (for example, with slices.Clone(c.mpols)) and add a reconfiguration regression test.
When memory resources are preserved, applyGrant leaves cpuset.mems unchanged but passes the grant's memory zone here (or zero if pinning is disabled). An omitted or allowed-mems selector therefore uses the wrong mask. For example, preserved node 2 with a grant on node 0 produces an interleave policy targeting a disallowed node, which can fail container startup. Read the preserved cpuset.mems before resolving the policy.
A claim with mode: interleave and omitted nodes has the same policy as one explicitly using nodes: allowed-mems. However, conversion preserves the different strings, so DeepEqual treats them as conflicting and prevents a container from holding both claims. Compare these documented equivalent node defaults consistently.
With cpu.preserve.resource-policy.nri.io: "true", the enclosing guard skips claim memory-policy lookup and conflict validation as well as CPU handling. Containers silently lose their claim's memory policy and can hold conflicting policies, although the annotation only preserves CPU resources. Fetch and validate claim policies independently of cpuPreserve, keep CPU pinning unchanged, and resolve CPU-based node selectors against the preserved CPU set.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Reloads lose claim policies, resource preservation mishandles them, and equivalent policies can incorrectly block container creation.
<details open>
<summary><strong>4 open issues</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<details>
<summary><strong>What changed in this PR</strong></summary>
Adds claim-configured memory policies to topology-aware DRA, reusing the memory-policy plugin’s configuration and node resolution.
Changes:
| File | Description |
| ---- | ----------- |
| test/e2e/policies.test-suite/topology-aware/n4c16/test45-dra-memory-policy/mempolicy-pod.yaml.in | Defines request-selective test containers. |
| test/e2e/policies.test-suite/topology-aware/n4c16/test45-dra-memory-policy/mempolicy-claim.yaml.in | Defines configurable test claims. |
| test/e2e/policies.test-suite/topology-aware/n4c16/test45-dra-memory-policy/deviceclass.yaml.in | Selects topology-aware devices. |
| test/e2e/policies.test-suite/topology-aware/n4c16/test45-dra-memory-policy/code.var.sh | Checks applied policies and invalid configs. |
| pkg/resmgr/policy/policy.go | Documents claim-config handling. |
| pkg/resmgr/lib/memory/policy.go | Shares memory-node resolution. |
| pkg/resmgr/lib/memory/policy_test.go | Tests node resolution. |
| pkg/resmgr/dra/claim.go | Adds driver-specific config extraction. |
| pkg/resmgr/dra/claim_test.go | Tests config extraction. |
| pkg/resmgr/cache/container.go | Records memory-policy adjustments. |
| pkg/resmgr/cache/container_test.go | Tests creation-only adjustments. |
| pkg/resmgr/cache/cache.go | Exposes the memory-policy setter. |
| pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go | Adds memory-policy deep copying. |
| pkg/apis/config/v1alpha1/resmgr/policy/topologyaware/config.go | Aliases the shared policy type. |
| pkg/apis/config/v1alpha1/resmgr/policy/topologyaware/claimconfig.go | Defines claim config and short-name conversion. |
| pkg/apis/config/v1alpha1/resmgr/policy/topologyaware/claimconfig_test.go | Tests claim policy conversion. |
| pkg/apis/config/v1alpha1/resmgr/policy/memorypolicy.go | Defines shared policy and NRI conversion. |
| cmd/plugins/topology-aware/policy/resources.go | Carries claim policies through resource grants. |
| cmd/plugins/topology-aware/policy/pools.go | Applies policies to new containers. |
| cmd/plugins/topology-aware/policy/mocks_test.go | Extends the container mock. |
| cmd/plugins/topology-aware/policy/dra.go | Validates and resolves claim policies. |
| cmd/plugins/topology-aware/policy/dra_test.go | Tests claim-policy behavior and persistence. |
| cmd/plugins/topology-aware/policy/cache.go | Serializes claim memory policies. |
| cmd/plugins/template/policy/dra.go | Rejects unsupported claim configs. |
| cmd/plugins/template/policy/dra_test.go | Tests config rejection. |
| cmd/plugins/memory-policy/main.go | Uses shared policy conversion and resolution. |
| cmd/plugins/memory-policy/main_test.go | Adapts existing resolution tests. |
</details>
<sub>Review effort: Balanced</sub>
Fixed. clone() now copies the memory policies of the claims. TestReconfigureKeepsClaimMemoryPolicies checks that a reconfiguration keeps them and that the claim can be prepared again.
Fixed. When the memory of the container is preserved, applyGrant now reads its cpuset.mems before resolving the nodes of the policy. The same goes for preserved CPUs and cpuset.cpus. The preserved memory and preserved CPUs cases of TestApplyClaimMemoryPolicy cover this.
Fixed. claimMemoryPolicies stores allowed-mems as unset once the value has been checked, so the two forms compare equal. TestConflictingClaimMemoryPolicies now holds one claim of each form.
Fixed. newRequest looks up the claims of every container now, and only drops the claimed CPUs when the CPUs are preserved, so CPU pinning does not change. Such a container gets the memory policy of its claims, with the nodes resolved for its own cpuset.
Flags retain their input order, so otherwise identical bind policies with [static-nodes, numa-balancing] and the reversed list fail containerClaims' reflect.DeepEqual check. A container holding both claims is refused even though they request the same kernel flags. Sort and deduplicate flags before storing the policy, and add a conflict-test case for equivalent flag lists.
With pinCPU: false, the pinning block above is skipped, but CPU-based memory selectors still use the allocated and claimed CPUs. For example, a claim on node 0 resolves cpu-nodes to node 0 even when the container's unchanged cpuset spans nodes 0–1. When pinning is disabled, resolve from container.GetCpusetCpus() without adding claimed CPUs; the existing fallback handles an unset cpuset. Add a regression case with CPU pinning disabled.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Disabled CPU pinning resolves incorrect memory nodes, and equivalent flag lists can falsely block container creation.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<details>
<summary><strong>4 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Fixed in 33356499. claimMemoryPolicies now sorts the flags and drops duplicates, next to the allowed-mems handling. TestConflictingClaimMemoryPolicies has a new case with the same flags in a different order.
Fixed in 33356499. With CPU pinning off, the nodes now come from the container's cpuset, without the claimed CPUs, as for preserved CPUs. TestApplyClaimMemoryPolicy now also runs with CPU pinning off, with a new case for it.
A claim with mode: bind, nodes: "0", and flags [static-nodes, relative-nodes] passes preparation because both flag names are valid. Linux rejects this mutually exclusive combination with EINVAL, so the prepared claim cannot be used to start its containers. Reject the combination before allocating the claim, and add a preparation-rejection test so the configuration error is reported at prepare time.
Two otherwise identical bind claims with nodes: "0-1" and nodes: "0,1" resolve to the same mask, but retain different strings. containerClaims then treats them as conflicting and refuses to create a container holding both. Canonicalize explicit numeric node lists using the validated mask before storing them, and add a regression case for these equivalent lists.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Policy normalization and flag validation leave container-start failures unresolved.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<details>
<summary><strong>2 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
mode: interleave with flags: [numa-balancing] passes this validation and claim preparation, but Linux rejects MPOL_INTERLEAVE | MPOL_F_NUMA_BALANCING with EINVAL. The claim therefore reserves CPUs successfully while consuming containers fail to start. Validate mode/flag compatibility here so preparation rejects this combination. Update the positive “interleave with flags” case in claimconfig_test.go to expect an error, and cover the rejection in the claim-preparation tests.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
An invalid mode/flag combination passes claim preparation but prevents consuming containers from starting.
<details open>
<summary><strong>1 open issue</strong></summary>
</details>
<details>
<summary><strong>2 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Fixed, thanks. The kernel (sanitize_mpol_flags() in mm/mempolicy.c) allows MPOL_F_NUMA_BALANCING only with MPOL_BIND and MPOL_PREFERRED_MANY. ToMemoryPolicy() now refuses numa-balancing with other modes. It also refuses local with static-nodes or relative-nodes, because mpol_new() refuses that too.
The "interleave with flags" test case no longer uses numa-balancing. There are new cases for the accepted and refused flags, and TestClaimConfigRefused now checks that an interleave claim with numa-balancing is refused and gets no CPUs.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Cross-component claim lifecycle changes and kernel-dependent runtime behavior require final human integration validation.
No open issues 🎉
<details>
<summary><strong>1 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
This checks node-list syntax, but not whether an explicit absolute mask contains usable memory nodes. For example, mode: bind, nodes: "63", and no flags passes preparation on a host with only nodes 0–3. CPUs are then reserved, but consuming containers cannot start because the kernel rejects that memory policy. Reject unusable absolute masks during preparation and add a refusal regression test. Treat relative-nodes separately because its values are indices, not host node IDs.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Preparation accepts unusable absolute node masks, reserving claim resources while consuming containers fail to start.
<details open>
<summary><strong>1 open issue</strong></summary>
</details>
<details>
<summary><strong>1 file not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
Could we avoid reflect with autogenerated DeepEqual(). Perhaps with something like this: https://github.com/containers/nri-plugins/pull/860 ?
Fixed. A node list with no node of the host is now refused when the claim is prepared, unless the relative-nodes flag is set, since then the values are indices. Added a test for both cases.
Would you prefer to merge #860 first or I can cherry-pick from it to this PR's branch?
Done. Cherry-picked the generator commit from #860 as the first commit of this PR and dropped DeepEqual usage, PTAL.
The wrapper ignores the Makefile's configurable LOCALBIN and DEEPEQUAL_GEN paths. For example, make generate-deepequals LOCALBIN=/tmp/nri-tools installs the generator there but then looks under build-aux/bin, failing if that binary is absent or using a stale binary if present. Pass $(DEEPEQUAL_GEN) from the recipe and use that supplied path in the wrapper.
NodeIDs() includes memoryless NUMA nodes. If node 1 has CPUs but no memory, mode: bind with nodes: "1" passes this check and reserves CPUs, but Linux rejects the policy when a consuming container starts. Validate absolute masks against the allocator's memory-bearing nodes, retaining the exception for relative indices, and add a memoryless-node refusal test.
Symbolic selectors resolve to absolute node IDs, but relative-nodes makes Linux interpret those numbers as indices into the allowed memory nodes. With cpuset.mems=1-2 and CPUs on node 1, a bind policy using nodes: cpu-nodes resolves to 1 and then targets node 2 instead. Convert symbolic results to relative indices, or reject symbolic selectors with this flag during preparation. Explicit numeric lists should remain indices. Cover a nonzero, restricted allowed-node set in a regression test.
strconv.Atoi accepts negative distances, so mode: interleave with nodes: max-dist:-1 passes claim preparation and reserves CPUs. Resolving this selector for a container produces an empty mask, which Linux rejects for interleave with EINVAL. Reject negative distances here so preparation reports the invalid config instead of leaving consuming containers unable to start. Add a refusal regression test.
<!-- ccr-overview-v2 -->
🟡 Changes recommended
Unresolved node-validation, relative-node mapping, and generator-path issues affect correctness and reliable generation.
<details open>
<summary><strong>4 open issues</strong></summary>
</details>
<details>
<summary><strong>1 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>2 files not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/generated.deepequal.go</code></strong>: Generated file</li>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
It is easier if you just cherry-pick I think.
@bart0sh This seems like a real bug wrt. relative indices.
@bart0sh This also seems like something we should flag as an error.
Fixed. This PR now carries the updated commit from #860. The Makefile passes DEEPEQUAL_GEN to the script, and the script uses it.
Fixed. The check now only counts nodes that have memory. A new test removes the memory from one node and checks that a bind to that node is refused.
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Unresolved configuration correctness issues and kernel-facing policy behavior require fixes and final human validation.
<details open>
<summary><strong>2 open issues</strong></summary>
</details>
<details>
<summary><strong>4 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>2 files not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/generated.deepequal.go</code></strong>: Generated file</li>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Persisted claim state and create-only kernel memory-policy application warrant final human integration review.
No open issues 🎉
<details>
<summary><strong>2 resolved since last review</strong></summary>
</details>
<details>
<summary><strong>2 files not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/generated.deepequal.go</code></strong>: Generated file</li>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
<!-- ccr-overview-v2 -->
🔵 Needs a closer look
Claim recovery and create-time NUMA policies span runtime-sensitive components that warrant final human validation.
No open issues 🎉
<details>
<summary><strong>2 files not reviewed</strong></summary>
<ul>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/generated.deepequal.go</code></strong>: Generated file</li>
<li><strong><code>pkg/apis/config/v1alpha1/resmgr/policy/zz_generated.deepcopy.go</code></strong>: Generated file</li>
</ul>
</details>
<sub>Review effort: Balanced</sub>
bart0sh
Passing memory policy settings in claim configs
A claim can now set the memory policy of the containers that hold it, in an opaque config for the topology-aware driver:
memoryPolicyhas the same fields as thepolicy.memory-policy.nri.ioannotation of the memory-policy plugin, with short mode and flag names such asinterleaveandstatic-nodes. The policy spec and the node resolution of the plugin move to the policy config API and libmem, so that both use the same code. The plugin works as before.The policy decodes and checks the configs when the claim is prepared. A bad config fails the prepare, and the pod's events show the error. The config can also be in the
DeviceClass. Class configs come first, and a later config overrides an earlier one. A config which names some requests applies only to the containers holding those requests.A container holding the claim gets the memory policy in its NRI adjustment when it is created, with the nodes resolved for its CPUs and memory nodes. Creating a container holding claims with different memory policies fails. Template refuses a claim with a config, and balloons does not support claims yet.
Tested with new unit tests and test45, which runs a claim with one CPU on node 0 and one on node 1 with an interleave policy. A container holding both requests gets
interleave:0-1, one holding only the first request getsinterleave:0, and a claim with an unknown mode fails to prepare.