Skip to content

hcsoci,hcs,shim: honor CPU affinity for Argon containers#2764

Open
zylxjtu wants to merge 1 commit into
microsoft:mainfrom
zylxjtu:main
Open

hcsoci,hcs,shim: honor CPU affinity for Argon containers#2764
zylxjtu wants to merge 1 commit into
microsoft:mainfrom
zylxjtu:main

Conversation

@zylxjtu
Copy link
Copy Markdown

@zylxjtu zylxjtu commented Jun 2, 2026

Process-isolated (Argon) WCOW containers run inside a server silo, which is a job object owned by HCS. HCS does not expose a CPU-affinity field on the container Processor schema (Count/Maximum/Weight), so the OCI spec.Windows.Resources.CPU.Affinity field was silently ignored for Argon containers at both create and update time.

Honor it the same way HostProcess containers do: open the silo's job object (by its well-known \Container_ name, the same handle queryInProc already opens) and apply the affinity with SetInformationJobObject(JobObjectGroupInformationEx) via the existing SetCPUGroupAffinities helper.

Create path:

  • Add (*hcs.System).SetSiloCPUGroupAffinities, which opens the silo job and sets the group affinities on it.
  • After CreateComputeSystem but before the container is started, apply affinity for Argon (HostingSystem == nil) via applyArgonCPUAffinity. This is race-free: the kernel records the property on the job, then applies it to the init process (and every descendant that later joins the silo) at the moment HCS calls AssignProcessToJobObject during Start. No container instruction ever runs on a forbidden processor, and the property lives on the kernel object so no per-PID walking or watchdog is needed.

Update path (runtime update / UpdateContainerResources):

  • Loosen isValidWindowsCPUResources to accept an affinity-only update (no Count/Shares/Maximum), so affinity can be changed after the container starts.
  • updateWCOWContainerCPU now only sends the HCS Processor modify request when a rate control is set (avoiding an empty no-op request) and additionally applies affinity out of band when present.
  • New updateWCOWContainerCPUAffinity re-pins the silo for Argon via the same SetSiloCPUGroupAffinities mechanism (the kernel re-applies the mask to current and future silo members). Hypervisor-isolated (Xenon) containers return ErrNotImplemented rather than silently dropping the request, since they require a UVM-level CPU-group swap.

Shared helpers:

  • Collect the container-kind-agnostic CPU affinity validators (ValidateCPUAffinity / ValidateCPUAffinityEntries), their sentinel errors, and the OCI -> jobobject.GroupAffinity converter (ToJobObjectAffinities) into a single kind-neutral cpuaffinity.go, shared by the HostProcess (internal/jobcontainers) and Argon paths. jobcontainers now reuses ToJobObjectAffinities instead of a duplicated conversion loop.

Xenon (UVM-backed) containers are out of scope here and are skipped; they require UVM-level CPU groups and are handled separately.

Tests: unit coverage for the converter, the affinity validators, the loosened isValidWindowsCPUResources (including affinity-only), and the affinity-update dispatch (no-op and Xenon not-implemented branches). End-to-end silo pinning still requires the Windows functional suite.

Authored-By: zylxjtu (zhang.yuanliang@hotmail.com)

Process-isolated (Argon) WCOW containers run inside a server silo, which is a
job object owned by HCS. HCS does not expose a CPU-affinity field on the
container Processor schema (Count/Maximum/Weight), so the OCI
spec.Windows.Resources.CPU.Affinity field was silently ignored for Argon
containers at both create and update time.

Honor it the same way HostProcess containers do: open the silo's job object (by
its well-known \Container_<id> name, the same handle queryInProc already opens)
and apply the affinity with SetInformationJobObject(JobObjectGroupInformationEx)
via the existing SetCPUGroupAffinities helper.

Create path:
- Add (*hcs.System).SetSiloCPUGroupAffinities, which opens the silo job and sets
  the group affinities on it.
- After CreateComputeSystem but before the container is started, apply affinity
  for Argon (HostingSystem == nil) via applyArgonCPUAffinity. This is race-free:
  the kernel records the property on the job, then applies it to the init
  process (and every descendant that later joins the silo) at the moment HCS
  calls AssignProcessToJobObject during Start. No container instruction ever runs
  on a forbidden processor, and the property lives on the kernel object so no
  per-PID walking or watchdog is needed.

Update path (runtime update / UpdateContainerResources):
- Loosen isValidWindowsCPUResources to accept an affinity-only update (no
  Count/Shares/Maximum), so affinity can be changed after the container starts.
- updateWCOWContainerCPU now only sends the HCS Processor modify request when a
  rate control is set (avoiding an empty no-op request) and additionally applies
  affinity out of band when present.
- New updateWCOWContainerCPUAffinity re-pins the silo for Argon via the same
  SetSiloCPUGroupAffinities mechanism (the kernel re-applies the mask to current
  and future silo members). Hypervisor-isolated (Xenon) containers return
  ErrNotImplemented rather than silently dropping the request, since they require
  a UVM-level CPU-group swap.

Shared helpers:
- Collect the container-kind-agnostic CPU affinity validators
  (ValidateCPUAffinity / ValidateCPUAffinityEntries), their sentinel errors, and
  the OCI -> jobobject.GroupAffinity converter (ToJobObjectAffinities) into a
  single kind-neutral cpuaffinity.go, shared by the HostProcess
  (internal/jobcontainers) and Argon paths. jobcontainers now reuses
  ToJobObjectAffinities instead of a duplicated conversion loop.

Xenon (UVM-backed) containers are out of scope here and are skipped; they require
UVM-level CPU groups and are handled separately.

Tests: unit coverage for the converter, the affinity validators, the loosened
isValidWindowsCPUResources (including affinity-only), and the affinity-update
dispatch (no-op and Xenon not-implemented branches). End-to-end silo pinning
still requires the Windows functional suite.

Authored-By: zylxjtu (zhang.yuanliang@hotmail.com)
@zylxjtu zylxjtu requested a review from a team as a code owner June 2, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant