Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(avf): add explicit vCPU-count topology and harden engine lifecycle

ExTvd7f651fbf152
diff --git a/app/src/main/java/com/excp/podroid/PodroidApplication.kt b/app/src/main/java/com/excp/podroid/PodroidApplication.kt
index c285f41..cd739ea 100644
--- a/app/src/main/java/com/excp/podroid/PodroidApplication.kt
+++ b/app/src/main/java/com/excp/podroid/PodroidApplication.kt
@@ -50,8 +50,11 @@ class PodroidApplication : Application() {
suspend fun awaitAssetsReady() = assetsReady.await()
// Android 14+ hides @SystemApi reflection lookups (returning NoSuchMethod
- // even via getDeclared*). Two prefixes need exempting:
+ // even via getDeclared*). Prefixes needing exemption:
// - Landroid/system/virtualmachine/ — AVF framework (AvfDiagnostics + AvfEngine)
+ // - Landroid/system/virtualizationservice/ — AVF AIDL parcelables
+ // (CpuOptions, VirtualMachineRawConfig, IVirtualizationService) used by
+ // AvfReflect's explicit-vCPU-count hook (issue #29).
// - Ljava/net/UnixDomainSocketAddress — ConsoleFanout needs UDS.of(String)
// which Android marks BLOCKED for untrusted_app even though the class
// itself is on the bootclasspath.
@@ -61,6 +64,7 @@ class PodroidApplication : Application() {
runCatching {
HiddenApiBypass.addHiddenApiExemptions(
"Landroid/system/virtualmachine/",
+ "Landroid/system/virtualizationservice/",
"Landroid/system/UnixSocketAddress",
"Ljava/net/UnixDomainSocketAddress",
)
@@ -102,6 +106,7 @@ class PodroidApplication : Application() {
{ copyAssetIfNeeded("alpine-rootfs.squashfs", filesDir, forceCopy) },
)
val pool = Executors.newFixedThreadPool(tasks.size.coerceAtMost(4))
+ var allSucceeded = true
try {
// invokeAll blocks until every Callable finishes (or times out).
// Each Callable wraps the task so a thrown exception is captured
@@ -114,19 +119,29 @@ class PodroidApplication : Application() {
// copyAssetIfNeeded / copyAssetFileIfNeeded already log
// their own failures; this catches anything that escaped.
Log.w(TAG, "Asset extraction task failed", e)
+ allSucceeded = false
}
}
} finally {
pool.shutdown()
if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {
pool.shutdownNow()
+ allSucceeded = false
}
}
- // Commit the new stamp only after extraction so a half-finished copy
- // (process killed mid-extract) doesn't leave us thinking we're done.
- runCatching { stampFile.writeText(currentStamp) }
- .onFailure { Log.w(TAG, "Failed to write assets stamp", it) }
+ // Commit the new stamp ONLY if every extraction task succeeded.
+ // Writing it after a failed copy (e.g. squashfs copy failed on an
+ // upgrade: disk full, killed mid-copy) would mark the OLD file as
+ // current — and because mksquashfs is deterministic the size check
+ // can't catch it either, so a stale rootfs would boot forever. On
+ // failure we leave the stamp stale so the next launch re-extracts.
+ if (allSucceeded) {
+ runCatching { stampFile.writeText(currentStamp) }
+ .onFailure { Log.w(TAG, "Failed to write assets stamp", it) }
+ } else {
+ Log.w(TAG, "asset extraction incomplete — leaving stamp stale to force re-extract next launch")
+ }
} finally {
// Always release waiters — a failed/partial extract is detected by
// the per-file size-check on the next read, not by hanging here.
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfCpuPolicy.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfCpuPolicy.kt
new file mode 100644
index 0000000..db97db4
--- /dev/null
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfCpuPolicy.kt
@@ -0,0 +1,57 @@
+/*
+ * Podroid - Rootless Podman for Android
+ * Copyright (C) 2024-2026 Podroid contributors
+ *
+ * Pure logic for the AVF multi-vCPU boot policy. AVF's public Builder API only
+ * exposes ONE_CPU (1 vCPU) or MATCH_HOST (all host cores) - see
+ * AvfReflect.setNumCpus. MATCH_HOST clones the host's heterogeneous big.LITTLE
+ * topology into the guest device tree, which makes the guest reset in early
+ * boot on some SoCs (Tensor G3/G4: STOP_REASON_REBOOT, no panic - issue #29).
+ * 1 vCPU always boots because none of that topology is emitted.
+ *
+ * Two tiers request multi-core (see AvfEngine.buildConfig):
+ * - Android 16+: an exact homogeneous vCPU count via the raw AIDL
+ * (AvfReflect.installExplicitCpuCount) - no heterogeneous topology at all.
+ * - Otherwise: MATCH_HOST bounded by `nr_cpus=N` on the kernel cmdline
+ * (all vCPUs still exist; the kernel onlines only N).
+ *
+ * This object decides the effective core count and the descending fallback
+ * ladder shared by both tiers, adaptively and without any per-device model
+ * hardcoding: a normal launch tries the user's requested count; an early-boot
+ * reset steps the count down and the result is persisted per device so later
+ * launches go straight to the value that boots.
+ *
+ * Pure (no Android deps) so it unit-tests on the JVM.
+ */
+package com.excp.podroid.engine.avf
+
+object AvfCpuPolicy {
+
+ /** Sentinel for "no cap recorded yet" (persisted default). */
+ const val NO_CAP: Int = 0
+
+ /**
+ * The vCPU count to actually launch with, given the user's [requested] count
+ * and the per-device [cap] discovered by prior boots ([NO_CAP] = none yet).
+ * Never returns below 1.
+ */
+ fun effectiveCpus(requested: Int, cap: Int): Int {
+ val r = requested.coerceAtLeast(1)
+ return if (cap > NO_CAP) minOf(r, cap).coerceAtLeast(1) else r
+ }
+
+ /**
+ * After an early-boot reset while attempting [current] vCPUs, the next lower
+ * count to try, or null to stop laddering (a single-vCPU boot that still
+ * resets is a genuine failure, not a topology cap).
+ *
+ * Ladder: anything above 2 collapses straight to 2 (the highest value proven
+ * to boot on an affected SoC), then 2 -> 1 (always boots). Monotonic and
+ * finite, so the retry path can never loop.
+ */
+ fun nextRungDown(current: Int): Int? = when {
+ current > 2 -> 2
+ current == 2 -> 1
+ else -> null
+ }
+}
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
index 57dd558..79dc7be 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
@@ -31,10 +31,13 @@ import com.excp.podroid.util.LogProxy
import com.termux.terminal.TerminalSession
import com.termux.terminal.TerminalSessionClient
import dagger.hilt.android.qualifiers.ApplicationContext
+import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
+import kotlinx.coroutines.sync.Mutex
+import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -51,7 +54,7 @@ import javax.inject.Singleton
@Singleton
class AvfEngine @Inject constructor(
@ApplicationContext private val context: Context,
- @Suppress("UnusedPrivateMember") private val settingsRepository: SettingsRepository,
+ private val settingsRepository: SettingsRepository,
) : VmEngine {
companion object {
@@ -60,15 +63,23 @@ class AvfEngine @Inject constructor(
// "podroid-avf-smoke" — a different name — so there is no config
// conflict between the two paths.
private const val VM_NAME = "podroid"
- // Mirrors QemuEngine's 60s boot-timeout fallback so a guest that never
- // emits "Ready!" (a console quirk, a lost marker, no onStopped/onDied)
- // doesn't strand the engine in Starting forever — EngineHolder.trySwap
- // waits on a terminal state, so a stuck Starting blocks backend switch.
- private const val BOOT_TIMEOUT_MS = 60_000L
+ // Safety net for a guest that never emits "Ready!" (a console quirk, a
+ // lost marker, no onStopped/onDied) so it doesn't strand the engine in
+ // Starting forever — EngineHolder.trySwap waits on a terminal state, so
+ // a stuck Starting blocks backend switch. Sized to match QemuEngine's
+ // BOOT_READY_SAFETY_MS: a first boot does dropbear host-key generation
+ // (~56s worst case) which AVF performs too. A premature forced-Running
+ // is worse on AVF than QEMU — the console.log privacy gate (onVmBytes)
+ // closes on leaving Starting, so the tail of a genuinely slow boot
+ // (including a late panic) would be lost.
+ private const val BOOT_TIMEOUT_MS = 120_000L
// Upper bound on the best-effort guest flush before stop. Bounded so a
// hung guest can never block the stop path; the caller proceeds to kill
// the VM if no SYNCED ack arrives.
private const val SYNC_TIMEOUT_MS = 8_000L
+ // VirtualMachineCallback.STOP_REASON_REBOOT — a guest-requested reboot,
+ // which during early boot is the MATCH_HOST topology crash (issue #29).
+ private const val STOP_REASON_REBOOT = 5
}
private val _state = MutableStateFlow<VmState>(VmState.Idle)
@@ -120,6 +131,12 @@ class AvfEngine @Inject constructor(
}
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
+ // Serializes the check-and-claim in start(). Two near-simultaneous
+ // ACTION_STARTs can both pass PodroidService's main-thread alreadyActive
+ // check before either coroutine flips _state to Starting; without this lock
+ // both would run launchAttempt against the same VM_NAME (mirrors
+ // QemuEngine.startMutex).
+ private val startMutex = Mutex()
// @Volatile: written on the start()/cleanup() thread, read from stop() and
// addPortForward() on other threads. Every other shared lifecycle field here
// (control, stopRequested, vmGeneration) is already @Volatile for the same
@@ -169,6 +186,19 @@ class AvfEngine @Inject constructor(
@Volatile private var lastSentRows = -1
@Volatile private var lastSentCols = -1
+ // Adaptive multi-vCPU fallback (issue #29). The launch args are remembered so
+ // an early-boot reset can re-launch with fewer cores WITHOUT going through a
+ // terminal state (which would tear the foreground service down). attemptedCpus
+ // is the effective count the current boot was started with, read by the
+ // onStopped funnel to decide the next rung. See AvfCpuPolicy.
+ @Volatile private var lastConfig: VmConfig? = null
+ private val lastPortForwards = mutableListOf<com.excp.podroid.data.repository.PortForwardRule>()
+ @Volatile private var attemptedCpus = 1
+ // True when buildConfig chose the explicit-cpuCount topology (Android 16+
+ // raw AIDL, the #29 fix proper) — launchAttempt must then arm the createVm
+ // hook; the cmdline carries no nr_cpus on that path.
+ @Volatile private var useExplicitCpuCount = false
+
/** Backend-specific diagnostics for the export log (observational only). */
@Volatile private var lastLifecycleEvent: String = "(no lifecycle callback yet)"
@Volatile private var launchConfigSummary: String = "(VM not started this session)"
@@ -177,7 +207,14 @@ class AvfEngine @Inject constructor(
val terminalSockPath: String get() = "${context.filesDir.absolutePath}/avf-terminal.sock"
val ctrlSockPath: String get() = "${context.filesDir.absolutePath}/avf-ctrl.sock"
- private val detector = BootStageDetector(_bootStage, _state) {
+ // Per-run detector (re-created each launchAttempt, not reset-and-reused).
+ // A fresh instance means a prior run's still-draining ConsoleFanout pump
+ // feeds the OLD detector (already ready=true, ignored) instead of flipping
+ // the NEW boot to Running on a late "Ready!" — the one state-transition
+ // path the generation token didn't otherwise cover.
+ @Volatile private var detector = newDetector()
+
+ private fun newDetector() = BootStageDetector(_bootStage, _state) {
// Detector already flipped _state to Running; stamp the uptime origin.
_runningSinceMs = System.currentTimeMillis()
Log.i(TAG, "boot Ready! detected — bridge connects via ConsoleFanout")
@@ -218,12 +255,46 @@ class AvfEngine @Inject constructor(
control = ctl
ctl.open()
scope.launch {
- for (rule in initialRules) addPortForward(rule)
+ // addPortForward now rethrows on failure (for EngineHolder's retry).
+ // Guard each replay so one failing rule doesn't abort the rest;
+ // EngineHolder's diff still retries genuinely-failed DataStore rules.
+ for (rule in initialRules) {
+ try {
+ addPortForward(rule)
+ } catch (c: CancellationException) {
+ throw c
+ } catch (e: Throwable) {
+ Log.w(TAG, "initial forward replay failed for $rule (continuing)", e)
+ }
+ }
}
}
override suspend fun start(portForwards: List<PortForwardRule>, config: VmConfig) {
- if (_state.value is VmState.Running || _state.value is VmState.Starting) return
+ startMutex.withLock {
+ if (_state.value is VmState.Running || _state.value is VmState.Starting) return
+ // Remember the launch args so the adaptive multi-vCPU fallback (issue
+ // #29) can re-launch with fewer cores after an early-boot reset WITHOUT
+ // routing through a terminal state (Error/Stopped tears the foreground
+ // service down — see PodroidService.observeStateForShutdown). Claim
+ // Starting under the lock so a re-entrant start() is rejected.
+ lastConfig = config
+ synchronized(lastPortForwards) { lastPortForwards.clear(); lastPortForwards.addAll(portForwards) }
+ _state.value = VmState.Starting
+ }
+ launchAttempt(portForwards, config, bootMsg = "Initializing AVF...")
+ }
+
+ /**
+ * One VM boot attempt. Split out of start() so the adaptive vCPU fallback
+ * (relaunchWithFewerCpus) can re-enter it after an early-boot reset while the
+ * state stays Starting. Assumes _state is already Starting.
+ */
+ private suspend fun launchAttempt(
+ portForwards: List<PortForwardRule>,
+ config: VmConfig,
+ bootMsg: String,
+ ) {
// New VM lifetime: bump the generation so any in-flight callback from a
// prior VM is ignored, clear the user-stop flag, and arm cleanup() to run
// once on this run's first terminal transition.
@@ -237,12 +308,11 @@ class AvfEngine @Inject constructor(
_consoleText.value = ""
initialRules.clear()
initialRules.addAll(portForwards)
- _state.value = VmState.Starting
- _bootStage.value = "Initializing AVF..."
- // Re-arm the one-shot detector so a Stop → Start cycle's second boot
- // can fire onReady again. Without this the new boot's "Ready!" marker
- // is silently ignored and state stays Starting forever.
- detector.reset()
+ _bootStage.value = bootMsg
+ // Fresh detector per run so a Stop → Start cycle's second boot can fire
+ // onReady again AND a prior run's late console bytes can't drive this
+ // boot's state (see the field comment).
+ detector = newDetector()
try {
val mgr = AvfReflect.manager(context)
@@ -265,6 +335,18 @@ class AvfEngine @Inject constructor(
}
vmHandle = vm
+ // Explicit-cpuCount topology (issue #29): arm the createVm rewrite
+ // before run(). A failed install poisons the hook process-wide, so
+ // the immediate relaunch rebuilds the cmdline on the nr_cpus
+ // fallback path — this cannot recurse a second time.
+ if (useExplicitCpuCount && !AvfReflect.installExplicitCpuCount(vm, attemptedCpus)) {
+ Log.w(TAG, "explicit vCPU hook unavailable at install time; " +
+ "relaunching on the nr_cpus fallback path")
+ cleanup()
+ launchAttempt(portForwards, config, bootMsg)
+ return
+ }
+
val cb = AvfReflect.newVmCallback(
onError = { code, msg ->
Log.e(TAG, "VM onError ${AvfReasonCodes.errorCode(code)} msg=$msg")
@@ -278,15 +360,23 @@ class AvfEngine @Inject constructor(
onStopped = { reason ->
Log.i(TAG, "VM onStopped ${AvfReasonCodes.stopReason(reason)}")
lastLifecycleEvent = "onStopped(${AvfReasonCodes.stopReason(reason)})"
- // A stop while still Starting means the VM exited during boot;
- // otherwise it's a clean stop. The funnel maps a user-stop to
- // Stopped regardless and ignores stale/terminal transitions.
- val target = if (_state.value is VmState.Starting && !stopRequested) {
- VmState.Error("VM exited during boot (${AvfReasonCodes.stopReason(reason)})")
- } else {
- VmState.Stopped
+ // Adaptive multi-vCPU fallback (issue #29): an early-boot reset
+ // (STOP_REASON_REBOOT while still Starting) with more than one
+ // core attempted is the MATCH_HOST heterogeneous-topology crash
+ // on Tensor G3/G4. Step the core count down and relaunch rather
+ // than surfacing an error; only a reset at 1 core (or a clean
+ // stop) falls through to the normal terminal handling.
+ if (!maybeRelaunchWithFewerCpus(generation, reason)) {
+ // A stop while still Starting means the VM exited during boot;
+ // otherwise it's a clean stop. The funnel maps a user-stop to
+ // Stopped regardless and ignores stale/terminal transitions.
+ val target = if (_state.value is VmState.Starting && !stopRequested) {
+ VmState.Error("VM exited during boot (${AvfReasonCodes.stopReason(reason)})")
+ } else {
+ VmState.Stopped
+ }
+ onVmTerminal(generation, target)
}
- onVmTerminal(generation, target)
},
onDied = { reason ->
// onDied fires on backend-level death (virtmgr/crosvm), often
@@ -366,6 +456,16 @@ class AvfEngine @Inject constructor(
fanout = fo
fo.start()
+ // Final stop-vs-start interleave guard: a user Stop landing during
+ // config build / VM create set stopRequested (and took the
+ // null-handle terminal path). Don't run() a VM the user already
+ // asked to stop — clean up and bail before it boots headless.
+ if (stopRequested || cleanedUp.get()) {
+ Log.i(TAG, "AVF launch aborted before run() — stop requested mid-start")
+ cleanup()
+ return
+ }
+
AvfReflect.run(vm)
val status = runCatching { AvfReflect.getStatus(vm) }.getOrDefault(-1)
Log.i(TAG, "vm.run() returned — VM booting (status=$status)")
@@ -389,6 +489,16 @@ class AvfEngine @Inject constructor(
bringUpControlChannel()
}
}
+ } catch (e: CancellationException) {
+ // The start coroutine runs in PodroidService.serviceScope, cancelled
+ // on every stop/teardown; buildConfig has real suspension points (the
+ // DataStore cpu-cap read). A cancel landing mid-start is NOT an engine
+ // error — letting it fall into the generic catch below would set a
+ // sticky Error that poisons the next start (QemuEngine guards the same
+ // way). Clean up and let the cancellation propagate.
+ Log.i(TAG, "AVF start cancelled (teardown) — not an error")
+ cleanup()
+ throw e
} catch (e: Throwable) {
val cause = e.cause ?: e
Log.e(TAG, "AVF start failed", cause)
@@ -397,6 +507,47 @@ class AvfEngine @Inject constructor(
}
}
+ /**
+ * Adaptive multi-vCPU fallback for the Tensor G3/G4 MATCH_HOST early-boot
+ * reset (issue #29). Returns true if it consumed the stop event by scheduling
+ * a relaunch with fewer cores; false to let normal terminal handling proceed.
+ *
+ * Fires only on a STOP_REASON_REBOOT for the live generation, while still
+ * Starting, not user-initiated, with more than one core attempted. The next
+ * lower count (AvfCpuPolicy.nextRungDown) is persisted so later launches skip
+ * straight to a value that boots; the relaunch keeps _state in Starting so the
+ * foreground service stays up (a terminal state would tear it down). The
+ * ladder is finite and monotonic (e.g. 8 -> 2 -> 1), so this can't loop.
+ */
+ private fun maybeRelaunchWithFewerCpus(generation: Long, reason: Int): Boolean {
+ if (reason != STOP_REASON_REBOOT) return false
+ if (generation != vmGeneration) return false
+ if (stopRequested || _state.value !is VmState.Starting) return false
+ val next = AvfCpuPolicy.nextRungDown(attemptedCpus) ?: return false
+ val config = lastConfig ?: return false
+ val forwards = synchronized(lastPortForwards) { lastPortForwards.toList() }
+ val mode = if (useExplicitCpuCount) "explicit-count" else "MATCH_HOST+nr_cpus"
+ Log.w(TAG, "AVF early-boot reset at $attemptedCpus vCPU(s) ($mode topology, #29); " +
+ "capping to $next and relaunching")
+ scope.launch {
+ runCatching { settingsRepository.setAvfCpuCap(next) }
+ .onFailure { Log.w(TAG, "persisting AVF cpu cap failed (continuing)", it) }
+ // Free the dead VM's resources without a terminal transition: cleanup()
+ // never touches _state, and launchAttempt re-arms cleanedUp.
+ cleanup()
+ // A user Stop landing between the callback-thread check above and here
+ // must win: cleanup() ran, so honour the stop instead of relaunching.
+ if (stopRequested) {
+ Log.i(TAG, "AVF relaunch aborted — stop requested during cleanup")
+ onVmTerminal(vmGeneration, VmState.Stopped)
+ return@launch
+ }
+ val plural = if (next == 1) "" else "s"
+ launchAttempt(forwards, config, bootMsg = "Retrying with $next core$plural...")
+ }
+ return true
+ }
+
/**
* Spawn libpodroid-bridge.so unconditionally at VM start, NOT on demand.
* The fanout's accept() loops until the bridge connects; without an early
@@ -428,16 +579,21 @@ class AvfEngine @Inject constructor(
}
override fun stop() {
+ // Mark the stop as user-initiated FIRST so any in-flight launchAttempt
+ // (which checks stopRequested before AvfReflect.run) and any resulting
+ // onStopped/onDied/onError is mapped to Stopped, never downgraded to
+ // Error. Set on the null-handle path too: a stop landing in the window
+ // between _state=Starting and vmHandle=vm must not let launchAttempt
+ // proceed to run() a VM the user already asked to stop.
+ stopRequested = true
val vm = vmHandle
if (vm == null) {
- // Nothing running (or already torn down). Make sure state is terminal
- // so EngineHolder.trySwap can release, then run the idempotent cleanup.
+ // Nothing running (or already torn down, or start hasn't reached
+ // vmHandle assignment). Make sure state is terminal so
+ // EngineHolder.trySwap can release, then run the idempotent cleanup.
onVmTerminal(vmGeneration, VmState.Stopped)
return
}
- // Mark the stop as user-initiated so a resulting onStopped/onDied/onError
- // is mapped to Stopped (never downgraded to Error) by the funnel.
- stopRequested = true
val generation = vmGeneration
scope.launch {
// Best-effort guest flush so unwritten ext4 data hits storage before
@@ -449,10 +605,21 @@ class AvfEngine @Inject constructor(
val requested = runCatching { AvfReflect.stop(vm) }
.onFailure { Log.w(TAG, "AVF stop() request failed; VM may still be running", it) }
.isSuccess
+ if (!requested) {
+ // The framework rejected stop(): the VM is still live. Forcing
+ // Stopped + cleanup() here would null vmHandle, stranding an
+ // unkillable crosvm (see AvfReflect.stop contract). Surface an
+ // error and KEEP the handle so a later real callback, or a retry,
+ // can still reach the VM. Don't run the watchdog on this path.
+ if (generation == vmGeneration && !cleanedUp.get()) {
+ _state.value = VmState.Error("AVF stop request rejected; VM may still be running")
+ }
+ return@launch
+ }
// Drive the final Stopped + cleanup from onStopped/onDied. If no
- // terminal callback arrives (or stop() was rejected), a bounded
- // watchdog forces the transition so we never strand in Running.
- kotlinx.coroutines.delay(if (requested) 5_000L else 1_000L)
+ // terminal callback arrives, a bounded watchdog forces the transition
+ // so we never strand in Running.
+ kotlinx.coroutines.delay(5_000L)
if (generation == vmGeneration && !cleanedUp.get()) {
Log.w(TAG, "AVF stop: no terminal callback within timeout — forcing Stopped")
onVmTerminal(generation, VmState.Stopped)
@@ -471,11 +638,14 @@ class AvfEngine @Inject constructor(
// TCP keeps vport == hostPort (unchanged); UDP is offset so a TCP and a
// UDP rule on the same host port don't collide on the vsock port space.
val vport = AvfVport.forRule(rule)
+ // Implicit VNC/audio forwards bind loopback so an authless X session +
+ // raw PCM aren't exposed to the LAN; user rules stay 0.0.0.0.
+ val bindAddr = if (rule.loopbackOnly) "127.0.0.1" else "0.0.0.0"
try {
val fw: Forwarder = if (rule.protocol == "udp") {
- VsockUdpForwarder(hostPort = rule.hostPort, guestVsockPort = vport, vm = vm, scope = scope)
+ VsockUdpForwarder(hostPort = rule.hostPort, guestVsockPort = vport, vm = vm, scope = scope, bindAddress = bindAddr)
} else {
- VsockPortForwarder(hostPort = rule.hostPort, guestVsockPort = vport, vm = vm, scope = scope)
+ VsockPortForwarder(hostPort = rule.hostPort, guestVsockPort = vport, vm = vm, scope = scope, bindAddress = bindAddr)
}
// putIfAbsent races safely with a concurrent addPortForward for the
// same vport — only one forwarder wins; the loser is closed here. fw
@@ -501,10 +671,18 @@ class AvfEngine @Inject constructor(
Log.w(TAG, "addPortForward(${rule.hostPort}/${rule.protocol}): control channel not up yet; " +
"guest ADD skipped (host listener is bound)")
}
- Log.i(TAG, "live forward up: 0.0.0.0:${rule.hostPort}/${rule.protocol} → vsock:$vport → 127.0.0.1:${rule.guestPort}")
+ Log.i(TAG, "live forward up: ${rule.hostPort}/${rule.protocol} → vsock:$vport → 127.0.0.1:${rule.guestPort}")
+ } catch (c: CancellationException) {
+ forwarders.remove(vport)
+ throw c
} catch (e: Throwable) {
forwarders.remove(vport)
Log.w(TAG, "addPortForward($rule) failed", e)
+ // Rethrow so EngineHolder's diff records this rule as not-applied and
+ // retries on the next diff, instead of permanently treating a
+ // transient failure as live. The initialRules replay loop guards each
+ // call so one failure there doesn't abort the rest.
+ throw e
}
}
@@ -644,10 +822,27 @@ class AvfEngine @Inject constructor(
private fun ensureStorageImage(storageSizeGb: Int): File {
val storageFile = File(context.filesDir, "storage.img")
val desiredBytes = storageSizeGb.toLong() * 1024L * 1024L * 1024L
- if (storageFile.exists() && storageFile.length() == desiredBytes) return storageFile
if (storageFile.exists()) {
- Log.d(TAG, "storage.img size mismatch — recreating")
- storageFile.delete()
+ val current = storageFile.length()
+ when {
+ current == desiredBytes -> return storageFile
+ // NEVER delete: storage.img is the persistent ext4 overlay. Grow
+ // in place when larger (guest resize2fs claims it on next boot);
+ // keep as-is if smaller (shrinking would corrupt the filesystem).
+ // Deleting on mismatch could wipe all guest data. See QemuEngine.
+ desiredBytes > current -> {
+ runCatching {
+ java.io.RandomAccessFile(storageFile, "rw").use { it.setLength(desiredBytes) }
+ }.onSuccess {
+ Log.i(TAG, "storage.img grown ${current / (1024 * 1024)}MB → ${storageSizeGb}GB (guest resize2fs on next boot)")
+ }.onFailure { Log.e(TAG, "Failed to grow storage.img", it) }
+ return storageFile
+ }
+ else -> {
+ Log.w(TAG, "storage.img (${current / (1024 * 1024)}MB) larger than requested ${storageSizeGb}GB; keeping existing image (shrink would corrupt the filesystem)")
+ return storageFile
+ }
+ }
}
try {
java.io.RandomAccessFile(storageFile, "rw").use { it.setLength(desiredBytes) }
@@ -688,7 +883,28 @@ class AvfEngine @Inject constructor(
appendLine("last VM lifecycle callback: $lastLifecycleEvent")
}
- private fun buildConfig(mgr: Any, config: VmConfig): Any {
+ private suspend fun buildConfig(mgr: Any, config: VmConfig): Any {
+ // Resolve the effective vCPU count: the user's request, clamped by any
+ // per-device cap discovered by a prior early-boot reset (issue #29).
+ // attemptedCpus feeds the onStopped fallback ladder.
+ val cpuCap = runCatching { settingsRepository.getAvfCpuCapSnapshot() }
+ .getOrDefault(AvfCpuPolicy.NO_CAP)
+ val effectiveCpus = AvfCpuPolicy.effectiveCpus(config.cpus, cpuCap)
+ attemptedCpus = effectiveCpus
+ // Multi-core topology mode (issue #29). Preferred: an exact homogeneous
+ // vCPU count via the Android 16+ raw AIDL (no heterogeneous host
+ // topology cloned into the guest — the confirmed reset trigger on
+ // Tensor G3/G4). Fallback: MATCH_HOST bounded by nr_cpus on the kernel
+ // cmdline. At 1 core the builder's ONE_CPU already means a clean
+ // single-vCPU topology, so neither mechanism is needed.
+ useExplicitCpuCount = effectiveCpus > 1 && AvfReflect.supportsExplicitCpuCount()
+ // Clear any explicit-count arming left on the process-shared binder proxy
+ // from a prior launch. launchAttempt re-arms via installExplicitCpuCount
+ // when useExplicitCpuCount is true; on every other path (ONE_CPU, the
+ // nr_cpus fallback tier, the ladder's 1-core rescue) a stale count must
+ // NOT rewrite this launch's topology back up (issue #29).
+ if (!useExplicitCpuCount) AvfReflect.disarmExplicitCpuCount()
+
val kernelSrc = File(context.filesDir, "vmlinuz-virt").also {
require(it.exists()) { "kernel missing at ${it.absolutePath}" }
}
@@ -729,23 +945,28 @@ class AvfEngine @Inject constructor(
// finally leaves a panic instead of an empty console.log. keep_bootcon
// keeps it printing after the real console registers.
val earlycon = "earlycon keep_bootcon"
- // DEBUG BRANCH (AVF multi-vCPU diagnosis): force ignore_loglevel on every
- // AVF boot, not just when the verbose toggle is on. The default extra
- // cmdline is `loglevel=1 quiet`, which suppresses the early-boot panic we
- // need to capture for the Tensor G3/G4 MATCH_HOST reboot (#29). With
- // ignore_loglevel, earlycon prints the full early boot into console.log
- // so one reporter run yields the panic without any settings to toggle.
- // Do NOT carry this unconditional flag to main — gate it back behind
- // config.verboseLogging once the root cause is pinned.
- val verboseFlags = " ignore_loglevel"
+ // ignore_loglevel makes earlycon print the full early boot (incl. an
+ // early-boot panic like the #29 MATCH_HOST reset) into console.log. The
+ // default extra cmdline is `loglevel=1 quiet`, which suppresses it, so
+ // gate this behind the verbose-logging toggle — on for reporters who need
+ // a capture, off for everyone else.
+ val verboseFlags = if (config.verboseLogging) " ignore_loglevel" else ""
+ // Fallback tier only (devices without the explicit-count AIDL): bound
+ // the cores the GUEST KERNEL brings up under MATCH_HOST. The full
+ // heterogeneous topology is still emitted; nr_cpus just limits how many
+ // vCPUs the kernel onlines (#29). On the explicit-count path the guest
+ // gets a clean N-vCPU topology and must online all of them. See
+ // AvfCpuPolicy.
+ val nrCpusFlag =
+ if (effectiveCpus > 1 && !useExplicitCpuCount) " nr_cpus=$effectiveCpus" else ""
val resolvedCmdline = ("console=hvc0 $earlycon root=/dev/ram0 mitigations=off " +
"elevator=mq-deadline podroid.tty=hvc0 podroid.backend=avf podroid.epoch=$epoch " +
- "podroid.x11.dpi=${config.x11Dpi}$verboseFlags " +
+ "podroid.x11.dpi=${config.x11Dpi}$verboseFlags$nrCpusFlag " +
config.kernelExtraCmdline).trim()
AvfReflect.addParams(cb, resolvedCmdline)
if (config.verboseLogging) {
Log.i(TAG, "verbose: resolved cmdline = $resolvedCmdline")
- Log.i(TAG, "verbose: ramMb=${config.ramMb} cpus=${config.cpus} " +
+ Log.i(TAG, "verbose: ramMb=${config.ramMb} cpus=$effectiveCpus (requested ${config.cpus}) " +
"storageAccess=${config.storageAccessEnabled}")
}
AvfReflect.addDisk(cb, storage.absolutePath, writable = true)
@@ -808,14 +1029,21 @@ class AvfEngine @Inject constructor(
}
}
AvfReflect.setMemoryBytes(vb, config.ramMb.toLong() * 1024 * 1024)
- AvfReflect.setNumCpus(vb, config.cpus)
+ AvfReflect.setNumCpus(vb, effectiveCpus)
AvfReflect.setDebugLevel(vb, AvfReflect.DEBUG_LEVEL_FULL)
AvfReflect.setConsoleInputDevice(vb, "hvc0") // virtio-console (line-rate), NOT PL011
AvfReflect.setConnectVmConsole(vb, false) // false: avoid inheriting Activity FDs (SurfaceFlinger sync fences → SELinux denial)
AvfReflect.setVmOutputCaptured(vb, true)
AvfReflect.setVmConsoleInputSupported(vb, true)
AvfReflect.setCustomImageConfig(vb, customCfg)
- launchConfigSummary = "vCPUs=${config.cpus}, memory=${config.ramMb}MB, console=hvc0, " +
+ val capStr = if (cpuCap > AvfCpuPolicy.NO_CAP) ", cpuCap=$cpuCap" else ""
+ val cpuMode = when {
+ effectiveCpus <= 1 -> "one-cpu"
+ useExplicitCpuCount -> "explicit-count"
+ else -> "match-host+nr_cpus"
+ }
+ launchConfigSummary = "vCPUs=$effectiveCpus ($cpuMode, requested ${config.cpus}$capStr), " +
+ "memory=${config.ramMb}MB, console=hvc0, " +
"protectedVm=$protectedStr, downloadsShare=$shareSummary, verboseLogging=${config.verboseLogging}"
return AvfReflect.build(vb)
}
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt
index 3efe0e3..2fa13af 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt
@@ -112,17 +112,159 @@ object AvfReflect {
fun setNumCpus(b: Any, n: Int) {
// AVF's setCpuTopology takes a CPU_TOPOLOGY_* constant — only 0 (one
- // CPU) or 1 (all host cores) are accepted. There is no fine-grained
- // count setter on the public AVF API. Map the user's requested count:
- // 1 → ONE_CPU; anything > 1 → MATCH_HOST (= all host cores).
+ // CPU) or 1 (all host cores) are accepted; there is no fine-grained
+ // count setter on the public Builder API. Map the user's requested
+ // count: 1 → ONE_CPU; anything > 1 → MATCH_HOST. Where the device
+ // supports it, installExplicitCpuCount later rewrites the topology to
+ // an exact homogeneous count at the createVm boundary (issue #29).
val topology = if (n <= 1) CPU_TOPOLOGY_ONE_CPU else CPU_TOPOLOGY_MATCH_HOST
if (n > 1) android.util.Log.d(
"AvfReflect",
- "AVF can't allocate a specific count of vCPUs; user requested $n → MATCH_HOST (all host cores)",
+ "builder topology for $n vCPUs → MATCH_HOST (exact count applied via createVm hook when supported)",
)
invokeDecl(b, "setCpuTopology", Int::class.javaPrimitiveType!! to topology)
}
+ // ---- Explicit vCPU count (issue #29) -----------------------------------
+ //
+ // MATCH_HOST makes virtmgr pass crosvm `--host-cpu-topology`, cloning the
+ // host's heterogeneous big.LITTLE topology (real multi-cluster MPIDRs,
+ // cpu-map, capacity-dmips-mhz, OPP tables) into the guest device tree.
+ // Onlining those cross-cluster vCPUs resets some SoCs' guests in early
+ // boot (Tensor G3/G4 — STOP_REASON_REBOOT before the console flushes).
+ //
+ // The virtualizationservice AIDL (Android 16+ com.android.virt) accepts
+ // what the public Builder doesn't: CpuOptions.CpuTopology.cpuCount(N),
+ // which virtmgr maps to crosvm `--cpus N` WITHOUT `--host-cpu-topology` —
+ // a clean homogeneous topology (contiguous MPIDRs 0..N-1, none of the
+ // heterogeneous DT). The framework's own toVsRawConfig only ever emits
+ // cpuCount(1) or matchHost(true), so we rewrite the parcelable in flight:
+ // VirtualMachine.run() fetches the IVirtualizationService AIDL interface
+ // from VirtualizationService.mBinder and hands the fully-built raw config
+ // to createVm(). Swapping a java.lang.reflect.Proxy into that field lets
+ // every call delegate untouched except createVm, which first replaces
+ // rawConfig.cpuOptions — all of run()'s fd/console/instance-id glue runs
+ // unmodified.
+
+ private const val VS_PKG = "android.system.virtualizationservice"
+
+ /** Sticky poison: a runtime install/rewrite failure disables the hook so
+ * the relaunch path falls back to the nr_cpus ladder instead of looping. */
+ @Volatile private var explicitCpuCountBroken = false
+
+ /** Count the proxy applies at the next createVm (set per launch attempt). */
+ @Volatile private var pendingCpuCount = 0
+
+ private val explicitCpuCountProbe: Boolean by lazy {
+ runCatching {
+ Class.forName("$VS_PKG.CpuOptions\$CpuTopology")
+ .getDeclaredMethod("cpuCount", Int::class.javaPrimitiveType)
+ Class.forName("$VS_PKG.CpuOptions").getDeclaredField("cpuTopology")
+ Class.forName("$VS_PKG.VirtualMachineRawConfig").getDeclaredField("cpuOptions")
+ Class.forName("$VS_PKG.IVirtualizationService")
+ Class.forName("$PKG.VirtualMachine").getDeclaredField("mVirtualizationService")
+ Class.forName("$PKG.VirtualizationService").getDeclaredField("mBinder")
+ true
+ }.getOrElse {
+ android.util.Log.i("AvfReflect",
+ "explicit vCPU count unavailable on this AVF revision: ${it.message}")
+ false
+ }
+ }
+
+ /**
+ * Whether this device's AVF stack accepts an exact vCPU count via the raw
+ * AIDL (Android 16+). The probe exercises every reflective lookup the hook
+ * needs, so a true here means installExplicitCpuCount is expected to work;
+ * a runtime failure still poisons the hook for the rest of the process.
+ */
+ fun supportsExplicitCpuCount(): Boolean = !explicitCpuCountBroken && explicitCpuCountProbe
+
+ /**
+ * Arms the createVm rewrite for [vm] with an exact count of [n] vCPUs.
+ * Call after the VirtualMachine is created and before run(). Returns false
+ * (and poisons the hook) on failure — the caller should relaunch so the
+ * cmdline-based nr_cpus fallback applies instead.
+ */
+ fun installExplicitCpuCount(vm: Any, n: Int): Boolean {
+ if (!supportsExplicitCpuCount() || n < 1) return false
+ return runCatching {
+ val vsField = Class.forName("$PKG.VirtualMachine")
+ .getDeclaredField("mVirtualizationService").apply { isAccessible = true }
+ val vs = vsField.get(vm) ?: error("mVirtualizationService is null")
+ val binderField = Class.forName("$PKG.VirtualizationService")
+ .getDeclaredField("mBinder").apply { isAccessible = true }
+ val real = binderField.get(vs) ?: error("mBinder is null")
+ pendingCpuCount = n
+ // A fresh VirtualizationService carries the raw AIDL stub; if this
+ // one already holds our proxy (same instance is cached per process),
+ // updating pendingCpuCount above is all that's needed.
+ if (!Proxy.isProxyClass(real.javaClass)) {
+ val ivs = Class.forName("$VS_PKG.IVirtualizationService")
+ val proxy = Proxy.newProxyInstance(ivs.classLoader, arrayOf(ivs)) { _, method, args ->
+ if (method.name == "createVm") rewriteCpuOptions(args?.getOrNull(0))
+ try {
+ if (args == null) method.invoke(real) else method.invoke(real, *args)
+ } catch (e: java.lang.reflect.InvocationTargetException) {
+ // Re-throw the binder's real exception (e.g.
+ // ServiceSpecificException) so run()'s catches see the
+ // original type, not our reflective wrapper.
+ throw e.cause ?: e
+ }
+ }
+ binderField.set(vs, proxy)
+ }
+ android.util.Log.i("AvfReflect", "explicit vCPU hook armed: cpuCount=$n")
+ true
+ }.getOrElse {
+ explicitCpuCountBroken = true
+ android.util.Log.e("AvfReflect",
+ "explicit vCPU hook install failed; falling back to nr_cpus ladder", it)
+ false
+ }
+ }
+
+ /**
+ * Disarms the createVm rewrite without removing the installed proxy. The
+ * proxy is cached on the process-shared VirtualizationService binder, so it
+ * survives across launches; a launch that does NOT want an explicit count
+ * (ONE_CPU, or the MATCH_HOST+nr_cpus fallback tier, or the ladder's 1-core
+ * rescue rung) MUST clear pendingCpuCount, otherwise the still-installed
+ * proxy would rewrite this launch's cpuOptions back to the previous count
+ * and silently re-run a failing config. rewriteCpuOptions no-ops at count 0.
+ */
+ fun disarmExplicitCpuCount() {
+ pendingCpuCount = 0
+ }
+
+ /**
+ * Replaces rawConfig.cpuOptions with an explicit cpuCount topology on the
+ * in-flight createVm parcelable (parceling happens after this returns, at
+ * binder transact time). On any failure the VM still launches with the
+ * builder's MATCH_HOST topology — poison the hook so AvfEngine's adaptive
+ * nr_cpus ladder rescues the likely early-boot reset on relaunch.
+ */
+ private fun rewriteCpuOptions(cfgUnion: Any?) {
+ val n = pendingCpuCount
+ if (cfgUnion == null || n < 1) return
+ runCatching {
+ val raw = cfgUnion.javaClass.getDeclaredMethod("getRawConfig")
+ .apply { isAccessible = true }.invoke(cfgUnion) ?: return
+ val topo = Class.forName("$VS_PKG.CpuOptions\$CpuTopology")
+ .getDeclaredMethod("cpuCount", Int::class.javaPrimitiveType)
+ .apply { isAccessible = true }.invoke(null, n)
+ val optsCls = Class.forName("$VS_PKG.CpuOptions")
+ val opts = optsCls.getDeclaredConstructor().apply { isAccessible = true }.newInstance()
+ optsCls.getDeclaredField("cpuTopology").apply { isAccessible = true }.set(opts, topo)
+ raw.javaClass.getDeclaredField("cpuOptions").apply { isAccessible = true }.set(raw, opts)
+ android.util.Log.i("AvfReflect", "createVm: cpuOptions rewritten to explicit cpuCount=$n")
+ }.onFailure {
+ explicitCpuCountBroken = true
+ android.util.Log.e("AvfReflect",
+ "createVm cpuOptions rewrite failed (VM launches with MATCH_HOST)", it)
+ }
+ }
+
fun setConsoleInputDevice(b: Any, device: String) {
invokeDecl(b, "setConsoleInputDevice", String::class.java to device)
}
diff --git a/app/src/test/java/com/excp/podroid/engine/avf/AvfCpuPolicyTest.kt b/app/src/test/java/com/excp/podroid/engine/avf/AvfCpuPolicyTest.kt
new file mode 100644
index 0000000..4cda4d4
--- /dev/null
+++ b/app/src/test/java/com/excp/podroid/engine/avf/AvfCpuPolicyTest.kt
@@ -0,0 +1,60 @@
+package com.excp.podroid.engine.avf
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Test
+
+class AvfCpuPolicyTest {
+
+ // --- effectiveCpus ---
+
+ @Test fun `no cap uses the requested count`() {
+ assertEquals(8, AvfCpuPolicy.effectiveCpus(requested = 8, cap = AvfCpuPolicy.NO_CAP))
+ assertEquals(4, AvfCpuPolicy.effectiveCpus(requested = 4, cap = 0))
+ }
+
+ @Test fun `cap clamps the requested count`() {
+ assertEquals(2, AvfCpuPolicy.effectiveCpus(requested = 8, cap = 2))
+ assertEquals(1, AvfCpuPolicy.effectiveCpus(requested = 8, cap = 1))
+ }
+
+ @Test fun `cap above request does not raise the count`() {
+ assertEquals(2, AvfCpuPolicy.effectiveCpus(requested = 2, cap = 8))
+ }
+
+ @Test fun `result is never below one`() {
+ assertEquals(1, AvfCpuPolicy.effectiveCpus(requested = 0, cap = AvfCpuPolicy.NO_CAP))
+ assertEquals(1, AvfCpuPolicy.effectiveCpus(requested = -1, cap = 1))
+ }
+
+ @Test fun `a non-positive cap is treated as no cap`() {
+ assertEquals(4, AvfCpuPolicy.effectiveCpus(requested = 4, cap = -3))
+ assertEquals(4, AvfCpuPolicy.effectiveCpus(requested = 4, cap = 0))
+ }
+
+ // --- nextRungDown ---
+
+ @Test fun `above two collapses to two`() {
+ assertEquals(2, AvfCpuPolicy.nextRungDown(8))
+ assertEquals(2, AvfCpuPolicy.nextRungDown(4))
+ assertEquals(2, AvfCpuPolicy.nextRungDown(3))
+ }
+
+ @Test fun `two steps to one`() {
+ assertEquals(1, AvfCpuPolicy.nextRungDown(2))
+ }
+
+ @Test fun `one or below stops laddering`() {
+ assertNull(AvfCpuPolicy.nextRungDown(1))
+ assertNull(AvfCpuPolicy.nextRungDown(0))
+ }
+
+ @Test fun `ladder is finite and monotonic from a high count`() {
+ // 8 -> 2 -> 1 -> stop, never loops.
+ val first = AvfCpuPolicy.nextRungDown(8)!!
+ assertEquals(2, first)
+ val second = AvfCpuPolicy.nextRungDown(first)!!
+ assertEquals(1, second)
+ assertNull(AvfCpuPolicy.nextRungDown(second))
+ }
+}