Skip to content

kexec: refuse to unload a multikernel instance that is running - #57

Merged
congwang-mk merged 1 commit into
multikernel:masterfrom
pro-utkarshM:fix/mk-unload-running
Nov 25, 2026
Merged

congwang-mk merged 1 commit into
multikernel:masterfrom
pro-utkarshM:fix/mk-unload-running

Conversation

@pro-utkarshM

@pro-utkarshM pro-utkarshM commented Nov 24, 2026 •

Copy link
Copy Markdown

KEXEC_FILE_UNLOAD frees a multikernel image without looking at its instance. For a running instance, kimage_free_multikernel() hands the segments that hold the running kernel back to the instance pool, drops its IPI ring and moves it to READY. The next load then copies a new image over the kernel still executing there, and the host can no longer halt it.

  • Unload returns -EBUSY while the instance is ACTIVE.
  • The multikernel load path refuses while any CPU of the instance's previous run is not confirmed parked (mk_instance_confirm_parked(), the check a reload already makes). It sits before the new image is allocated from the instance pool, so an instance with a CPU that never parked can still be unloaded, and its teardown reaches the leak path in mk_instance_free_memory().

Tested: RISC-V QEMU virt (aia=aplic-imsic, OpenSBI v1.8.1) on master 2017469, with a test-only knob making mk_instance_confirm_parked() report a CPU as not parked:

unload, spawn running                  rc=-1 errno=16  state active  "Multikernel instance 1 is running, halt it before unloading"
force halt, knob on, unload            rc=0            state ready
load, knob on                          rc=-1 errno=16  state ready   "Multikernel instance 1 still has running CPUs, refusing to load"
knob off, load, spawn, halt, teardown  all pass

Spawn, graceful and forced halt, and re-spawn also pass; dmesg clean with lockdep. Builds: x86_64 clang W=1 with MULTIKERNEL=y and =n, no new warnings; checkpatch --strict clean.

@congwang-mk

Copy link
Copy Markdown

Thanks, the bug is real: the unload path has never looked at the instance state (only kerf unload does, in user space), so the Fixes: tag is right.

The MK_STATE_ACTIVE refusal is good as is. I would like the mk_instance_confirm_parked() half moved, though:

  • It blocks teardown after a lost CPU. If a CPU never parks after a force halt, unload now returns -EBUSY forever, the instance stays LOADED, and mk_instance_destroy() refuses LOADED, so the instance can never be removed. Today unload followed by rmdir reaches the deliberate "leak its pool" path in mk_instance_free_memory(), which exists for exactly this case.
  • Returning the segments to the instance pool is harmless by itself. The damage is the next load copying into that pool, and the load path has no parked check. Putting the check there closes the same window and keeps teardown working.
  • mk_park_wait() polls up to 1 s per CPU, serially, so an unload racing a halt on a large instance can hold the kexec lock for a long time.

Could you respin with the ACTIVE check on unload and the parked check on the multikernel load path instead? Also, the second sentence of the code comment is hard to follow without the commit message.

KEXEC_FILE_UNLOAD frees a multikernel image without looking at its
instance. For a running instance, kimage_free_multikernel() returns
the image's segments, which hold the running kernel, to the instance
pool, drops its IPI ring and moves it back to READY. The next load
into that instance then copies a new image over the kernel that is
still executing there, and the host has no way left to halt it.

Return -EBUSY from unload while the instance is ACTIVE.

A halt settles the instance to LOADED before its CPUs reach the park
loop, so a CPU can still run in the segments after a halt too. Handing
them back to the pool is harmless by itself; the damage is the next
load copying into them. Check that the instance's CPUs are parked on
the multikernel load path, as a reload of the instance already does,
rather than on unload, so an instance with a CPU that never parked can
still be unloaded and its teardown reaches the leak path in
mk_instance_free_memory().

Fixes: b6e539f ("kexec: Integrate multikernel support with kexec_file_load()")
Signed-off-by: Utkarsh Maurya <projects.utkarshMaurya@gmail.com>
@pro-utkarshM

Copy link
Copy Markdown
Author

Respun on current master: the ACTIVE check stays on unload, the parked check moved to the multikernel load path, before the new image is allocated from the instance pool, and the comment is rewritten.

The load check still runs under the kexec lock, but mk_instance_confirm_parked() only waits when a CPU has really not parked; normally it returns at once, as the existing check on reload does.

Tested on RISC-V QEMU with a test-only knob making mk_instance_confirm_parked() report a CPU as not parked: unload after a force halt now succeeds, the following load gets -EBUSY ("still has running CPUs, refusing to load"), and with the knob off load, spawn, halt and teardown all work.

@congwang-mk
congwang-mk merged commit 125746c into multikernel:master Nov 25, 2026
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.

2 participants