Skip to content

cxl: add pkg/cxl/kernel to observe and change CXL kernel objects - #910

Draft
askervin wants to merge 5 commits into
stacked/askervin/cxl/02-cxl-apifrom
stacked/askervin/cxl/03-cxl-kernel
Draft

askervin wants to merge 5 commits into
stacked/askervin/cxl/02-cxl-apifrom
stacked/askervin/cxl/03-cxl-kernel

Conversation

@askervin

@askervin askervin commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

This PR adds pkg/cxl/kernel, the package that observes and changes the CXL kernel objects of a node. Code that manages CXL memory gets one place that reads sysfs and runs cxl and ndctl, so it does not repeat that work or define the derived facts (such as when a region is enabled) in several places. A fake kernel lets the unit tests run without CXL hardware, and a VM test checks the package against real devices.

  • pkg/cxl/kernel covers memdevs and the decoders of their ports, regions, dax regions and dax devices with their drivers, memory blocks and NUMA nodes, the persistent memory chain (nd region, namespaces, block devices), and the superblocks, mounts, binds and loop devices on it. It has a transition table for each object that has a state, and one lock that orders the dax bus operations. The package comment in doc.go lists the derived facts and the naming rules, and each object file starts with a comment about that object.
  • pkg/cxl/kernel/fake is the kernel's side of those objects for tests. It keeps a sysfs tree, a procfs and a /dev in temporary directories, and it implements the cxl and ndctl commands and the system calls of the package. Its package comment lists the kernel rules that it models and the ones that it leaves out.
  • scripts/cxl-dump prints what the package observes of a node as JSON.
  • memory-policy/n4-cxl/test01-pkg-cxl hot-plugs CXL memory devices in a VM and checks what cxl-dump reports for them. cxl.source.sh and e2e_json.py give e2e tests the cxl-dump view and the JSON assertions.
  • pkg/udev: a reader now binds its netlink socket with pid 0, so several readers can run in one process. A new test runs two readers at once.
  • The CXL unit-test job of the verify workflow now also runs pkg/udev and cxl-dump.

Start with pkg/cxl/kernel/doc.go, then read kernel.go for the Kernel type and its Sys interface, and then the object files such as region.go and dax.go.

Testing

  • go test -short -race ./pkg/cxl/... ./pkg/udev/... ./scripts/cxl-dump/... passes, two udev readers in one process included.
  • golangci-lint run reports nothing. GOOS=linux GOARCH=arm64 go vet ./pkg/cxl/... ./pkg/udev/... passes.
  • cxl-dump runs in the VMs of memory.test-suite/dra-driver-cxl, whose tests read its JSON. The suite passes.
  • Not run: memory-policy/n4-cxl/test01-pkg-cxl, which needs the n4-cxl VM.

@askervin
askervin added this pull request to stack #920 November 25, 2026 07:56
Comment thread pkg/cxl/kernel/port.go Fixed
Comment thread pkg/cxl/kernel/sys_linux.go Fixed
@askervin
askervin force-pushed the stacked/askervin/cxl/03-cxl-kernel branch from e9ed30d to b5fe67e Compare November 25, 2026 14:12
@askervin
askervin force-pushed the stacked/askervin/cxl/03-cxl-kernel branch from b5fe67e to b5e9660 Compare November 25, 2026 20:59
@askervin
askervin requested a balanced review from Copilot November 26, 2026 13:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical mount-scanning and DAX-validation gaps can allow device changes beneath live filesystems or read-write access to unintended devices, risking write failures and data loss.

5 open issues
What changed in this PR

This PR adds pkg/cxl/kernel as the node-level interface for observing and managing CXL memory, devices, and related filesystems.

Changes:

  • Adds CXL topology snapshots, device operations, state transitions, and a fake kernel for tests.
  • Adds cxl-dump, shared JSON helpers, and a VM topology test.
  • Allows multiple udev readers per process and expands CI test coverage.

Testing: The PR reports passing race tests, lint, arm64 vet, and the DRA VM suite. The new n4-cxl VM test has not been run.

File Description
test/​e2e/​memory.test-suite/​memory-policy/​n4-cxl/​test01-pkg-cxl/​code.var.sh Tests CXL hotplug, interleaving, and memory states.
test/​e2e/​memory.test-suite/​e2e_json.py Evaluates JSON assertions for VM tests.
test/​e2e/​memory.test-suite/​cxl.source.sh Adds CXL dump, polling, and assertion helpers.
scripts/​cxl-dump/​cxl-dump.go Prints the observed topology as JSON.
pkg/​udev/​reader.go Uses kernel-assigned netlink port IDs.
pkg/​udev/​reader_test.go Tests two readers in one process.
pkg/​cxl/​kernel/​wait.go Adds context-aware polling and waits.
pkg/​cxl/​kernel/​transitions.go Supports state names and transition documentation.
pkg/​cxl/​kernel/​sysfs.go Adds sysfs parsing and path helpers.
pkg/​cxl/​kernel/​sys_linux.go Implements Linux mount, device, and memory operations.
pkg/​cxl/​kernel/​region_test.go Tests region creation, removal, and safety checks.
pkg/​cxl/​kernel/​probe.go Adds address-aliasing probes and DAX zeroing.
pkg/​cxl/​kernel/​probe_test.go Tests temporary device-node cleanup.
pkg/​cxl/​kernel/​probe_other.go Defines unsupported cache-flush behavior.
pkg/​cxl/​kernel/​probe_amd64.s Implements cache flush and memory fence instructions.
pkg/​cxl/​kernel/​probe_amd64.go Declares amd64 probe primitives.
pkg/​cxl/​kernel/​port.go Observes decoder stacks and release-order blockers.
pkg/​cxl/​kernel/​port_test.go Tests decoder holes and ordered region removal.
pkg/​cxl/​kernel/​pmem_test.go Tests persistent-memory namespace lifecycles.
pkg/​cxl/​kernel/​observe.go Builds topology snapshots and lookup indexes.
pkg/​cxl/​kernel/​mount.go Inspects mounts and guards read-only device changes.
pkg/​cxl/​kernel/​memdev.go Observes and controls CXL memory devices.
pkg/​cxl/​kernel/​memdev_test.go Tests device discovery, states, and capacity.
pkg/​cxl/​kernel/​loop.go Identifies and configures loop devices.
pkg/​cxl/​kernel/​kernel.go Defines configuration and system-call interfaces.
pkg/​cxl/​kernel/​helpers_test.go Provides shared fixtures and assertions.
pkg/​cxl/​kernel/​fs.go Detects filesystem signatures and properties.
pkg/​cxl/​kernel/​fake/​pmem.go Simulates persistent-memory probing and namespaces.
pkg/​cxl/​kernel/​fake/​fake_test.go Tests fake hooks, reboot behavior, and kernel rules.
pkg/​cxl/​kernel/​events.go Broadcasts device changes and debounced notifications.
pkg/​cxl/​kernel/​errors.go Defines typed operation errors.
pkg/​cxl/​kernel/​doc.go Documents package scope and synchronization rules.
pkg/​cxl/​kernel/​devnode.go Opens device nodes with DAX identity checks.
pkg/​cxl/​kernel/​devnode_test.go Tests device opening and delayed node appearance.
pkg/​cxl/​kernel/​blocks.go Observes and controls memory blocks and NUMA nodes.
pkg/​cxl/​kernel/​blocks_test.go Tests ranges, block states, and online-memory checks.
.github/​workflows/​verify.yaml Adds udev and cxl-dump to race-test coverage.

Review effort: Balanced

Comment thread pkg/cxl/kernel/devnode.go Resolved
Comment thread pkg/cxl/kernel/devnode.go Resolved
Comment thread pkg/cxl/kernel/mount.go Resolved
Comment thread pkg/cxl/kernel/fake/pmem.go Resolved
Comment thread test/e2e/memory.test-suite/e2e_json.py Resolved
@askervin
askervin force-pushed the stacked/askervin/cxl/03-cxl-kernel branch from b5e9660 to 16a6515 Compare November 27, 2026 17:23
@askervin askervin changed the title cxl: add pkg/cxl/kernel for CXL regions, dax devices and memory blocks cxl: add pkg/cxl/kernel to observe and change CXL kernel objects Nov 27, 2026
A reader bound its netlink socket with the process id, so a second
reader in the same process failed to bind. With pid 0 the kernel
assigns the port id, so several readers can run in one process. A new
test runs two readers at once.

Signed-off-by: Antti Kervinen <antti.kervinen@intel.com>
Code that manages CXL memory needs to read and change many kernel
objects: memdevs, regions, dax devices, memory blocks, and the
persistent memory chain with its mounts. Package kernel gives that code
one place that observes them and one definition of each derived fact,
for example when a region counts as enabled or as having online memory.
It also holds the one lock that orders the dax bus operations that the
kernel could deadlock on.

Package fake is the kernel's side of the same objects, so unit tests
run without CXL hardware. It does not model the dax bus locks, so it
cannot show a deadlock there.

Signed-off-by: Antti Kervinen <antti.kervinen@intel.com>
cxl-dump prints what pkg/cxl/kernel observes of a node as JSON: memdevs,
ports, regions, dax devices, memory blocks and NUMA nodes.

Signed-off-by: Antti Kervinen <antti.kervinen@intel.com>
test01-pkg-cxl hot-plugs CXL memory devices in a VM and checks what
cxl-dump reports of their regions: interleaving, disabled and partly
online memory, and two host bridges. cxl.source.sh and e2e_json.py give
e2e tests the cxl-dump view and JSON assertions.

Signed-off-by: Antti Kervinen <antti.kervinen@intel.com>
Signed-off-by: Antti Kervinen <antti.kervinen@intel.com>
@askervin
askervin force-pushed the stacked/askervin/cxl/03-cxl-kernel branch from 16a6515 to 2cbaaf0 Compare November 30, 2026 13:58

This branch has not been deployed

No deployments
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.

3 participants