Files
mac_linuxgpu/linuxu/tests/test_session_shutdown.cpp
T
geramyloveless 331692b9ca display: a GPU that left the bus has no display to drive
With the device removed (rt/removal.h), every display op returns
-ENODEV, and turning a pattern or output off commits nothing: the
recorded configuration is dropped and the buffers and client go as on a
Linux unplug. Surface imports, copies and checks return -ENODEV;
releasing still works and the client's mapping is returned once the
removal has completed the BO's fences.

The dext releases what the display ops hold in one place
(release_display_owners): at a session close unless a quarantine keeps
every owner, or the device is gone; and when a removal releases a
session that was quarantined, before compute stop and upstream removal.

Tests: test-session-shutdown removes the device under a display agent
with an output on screen and two imports, in a plain session, one that
quarantines during the close, and one quarantined before it (released
only by the removal); test-surface-import removes the device with an
import held; test-dm-offline removes it with the pattern on screen and
turns it off without a register write.
2026-10-03 15:57:17 -07:00

973 lines
49 KiB
C++

/* Execute extracted production shutdown functions with no DriverKit calls. */
#include "observer_gate.h"
#include "raw_bar_lease.h"
#include "session_state.h"
#include <Block.h>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <string>
#include <algorithm>
#include <vector>
#include <unistd.h>
#include "retained_log.h"
using kern_return_t = int;
enum {
kIOReturnSuccess, kIOReturnBadArgument, kIOReturnBusy, kIOReturnError,
kIOReturnNotAttached, kIOReturnNotReady, kIOReturnNotOpen,
kIOReturnNoResources, kIOReturnNotPermitted,
};
enum { DEXT_COMPUTE_STAGE_NONE, kMacAMDGPUMethodShutdownGPU = 42 };
enum { DEXT_PCI_FAULT_NONE = 0, DEXT_PCI_FAULT_CONFIG = 1, DEXT_PCI_FAULT_QUARANTINE = 4 };
#define SUPERDISPATCH 0
#define IMPL(cls, method) cls::method(IOService *provider)
static std::string platformLog;
static void platformLogSink(const char *text) {
// A platform sink may query the cached log: do not retain its lock here.
uint64_t cursor = UINT64_MAX, end = 0;
assert(klog_read(&cursor, nullptr, 0, &end) == 0 && cursor == end);
platformLog += text;
}
#define MACLINUXGPU_LOG(...) maclinuxgpu::RetainedLog(platformLogSink, __VA_ARGS__)
static std::string retainedLog() {
char bytes[LINUXU_KLOG_CAPACITY];
uint64_t cursor = 0;
return std::string(bytes, klog_read(&cursor, bytes, sizeof(bytes), nullptr));
}
static void expectLog(const char *message) {
const auto text = retainedLog();
assert(text == platformLog);
assert(text.find(message) != std::string::npos);
}
static void checkLogFormat() {
unsigned evaluations = 0;
MACLINUXGPU_LOG("failure=%d flags=%#x percent=%% text=%s once=%u", -38, 0xab,
"%{private}s", ++evaluations);
assert(evaluations == 1);
expectLog("mac.linuxgpu: failure=-38 flags=0xab percent=% text=%{private}s once=1\n");
const std::string large(8192, 'X');
const size_t before = platformLog.size();
MACLINUXGPU_LOG("%s", large.c_str());
assert(platformLog.size() - before == LINUXU_KLOG_MESSAGE_CAPACITY - 1);
expectLog("... [truncated]\n");
}
static std::vector<std::string> events;
static bool irqDrained, endpointReset, dmaCompleted;
static unsigned driverStops, clientStops;
static int holdError, computeError, irqError, resetError, dmaFiniError, isolationError;
static unsigned resetFailures = UINT32_MAX; // resets that fail with resetError
static int transportFault = DEXT_PCI_FAULT_NONE;
static bool probeCleanupRetained, computeQuiescent = true;
// The visible-VRAM aperture upstream leaves ioremapped after drm_dev_unplug().
static int bar0Aliases;
static unsigned pciOpens;
static void (*irqCompletion)(void *);
static void *irqContext;
struct IOService {
unsigned references = 1;
void retain() { ++references; }
void release() { assert(references > 1); --references; }
};
struct IOPCIDevice : IOService {};
struct IODispatchQueue {
using Block = void (^)(void);
std::deque<Block> pending;
void DispatchAsync(Block block) {
assert(irqDrained);
events.push_back("enqueue_finish");
pending.push_back(Block_copy(block));
}
void drain() {
while (!pending.empty()) {
auto block = pending.front(); pending.pop_front();
block(); Block_release(block);
}
}
};
struct IOUserClientMethodArguments {
uint32_t scalarInputCount = 0;
uint64_t *scalarOutput = nullptr;
uint32_t scalarOutputCount = 2;
};
struct MacLinuxGPUUserClient_IVars;
struct MacLinuxGPU : IOService {
void FinishSession();
void FinishStop(IOService *provider);
};
struct MacLinuxGPUUserClient : IOService {
MacLinuxGPUUserClient_IVars *ivars = nullptr;
unsigned superStops = 0;
void FinishStop(IOService *provider);
kern_return_t Stop(IOService *provider);
kern_return_t Stop(IOService *, int) { ++superStops; return kIOReturnSuccess; }
kern_return_t shutdown(IOUserClientMethodArguments *arguments);
kern_return_t failedProbe();
};
static uint64_t linuxu_dart_budget();
static int dext_dma_begin_shutdown(uint64_t);
static int dext_compute_stop();
static void linuxu_driver_shutdown();
static void dext_dma_quarantine();
static int dext_pci_quarantine();
static int dext_irq_fini_async(void (*)(void *), void *);
static void rt_device_free(void *);
static int dext_pci_shutdown_reset();
static void dext_compute_set_pci_open(bool);
static void dext_compute_set_stage(int);
static int dext_dma_fini();
static void dext_close();
static void rt_gart_reset();
static void *rt_device_get_pdev(void *);
static int rt_pci_probe_cleanup_retained(void *);
static int dext_pci_transport_fault();
static int dext_compute_quiescent();
static int dext_dma_quarantine_releasable();
static int dext_pci_quarantine_releasable();
static int dext_dma_lift_quarantine(int);
static int dext_pci_release_quarantine();
static int dext_bar0_cpu_release_orphaned();
static int dext_set_pci(void *, void *);
static int dext_open(uint32_t *);
static int dext_compute_query_info(uint64_t, uint64_t *, int);
static int dext_compute_release_client(uint64_t);
// Surprise removal (rt/removal.h, dext_pci_* and dext_dma_device_removed).
struct pci_dev;
static bool devicePresent = true, dmaRemoved;
static int dext_pci_device_present() { return devicePresent; }
static void dext_pci_mark_removed() { events.push_back("pci_mark_removed"); }
static int dext_pci_close_removed();
static int rt_removal_begin(struct pci_dev *) { events.push_back("removal_begin"); return 0; }
static void rt_removal_end() { events.push_back("removal_end"); }
static void dext_compute_device_removed() { events.push_back("compute_removed"); }
static int dext_dma_device_removed() {
events.push_back("dma_removed");
// A device off the bus uses no mapping: retired descriptors complete.
dmaRemoved = dmaCompleted = true;
return 0;
}
struct rt_drm_info { int closes; };
static void IOSleep(uint64_t);
static void rt_drm_info_close(rt_drm_info *);
// Linux-file processes and CS self-tests (lx_files, cs_selftest): a session
// close tears them down first; a self-test whose GPU work never completed
// leaves the session quarantined, compute uncertain.
struct MacLinuxGPUUserClient;
static unsigned lxTeardowns, lxParked;
static void lx_gate_close() {}
static bool lx_teardown_all() {
++lxTeardowns;
if (lxParked) events.push_back("lx_teardown_parked");
return lxParked != 0;
}
static unsigned rt_cs_selftest_parked() { return lxParked; }
static void lx_client_stop(MacLinuxGPUUserClient *, IOService *) { assert(false); }
// The display test (rt/display.h): a showing pattern is turned off while
// the driver runs, after observer admission drained, never in quarantine.
static bool displayShowing;
static int rt_display_showing() { return displayShowing; }
static void rt_display_stop();
// A display agent's imported surfaces are released with the display.
static unsigned surfacesImported;
static unsigned rt_surface_count() { return surfacesImported; }
static unsigned rt_surface_remove_all();
// An observer's Stop releases what its display agent imported.
static unsigned observerDisplayStops;
static void observer_display_client_stop(uint64_t) { ++observerDisplayStops; }
// No KFD suspend is held in these scenarios (power_state.h): the hook that
// hands one back before upstream removal has nothing to do.
static unsigned powerRemovalHooks;
static void power_before_removal() { ++powerRemovalHooks; }
#include "session_shutdown_production.inc"
// An observer read in flight when the session closes: the close waits, and
// the read finishes (leaves) while the close sleeps.
static bool observerReadInFlight;
static void IOSleep(uint64_t ms) {
assert(ms == 1 && !s_observerReads.admitting() && observerReadInFlight);
events.push_back("observer_wait");
observerReadInFlight = false;
s_observerReads.leave();
}
static void rt_drm_info_close(rt_drm_info *drm) {
assert(!s_observerReads.admitting() && s_observerReads.drained());
assert(s_modulesRunning && !s_dmaQuarantined && !s_observerDrm);
++drm->closes;
events.push_back("observer_drm_close");
}
static void rt_display_stop() {
assert(displayShowing && !s_observerReads.admitting() && s_observerReads.drained());
// Before Linux-file teardown in a close; for a removed device also from
// the release of a session that was quarantined first.
assert(s_modulesRunning && !s_dmaQuarantined && (lxTeardowns == 0 || s_deviceRemoved));
events.push_back(s_deviceRemoved ? "display_off_removed" : "display_off");
displayShowing = false;
}
static unsigned rt_surface_remove_all() {
assert(surfacesImported && !s_observerReads.admitting() && s_observerReads.drained());
assert(s_modulesRunning && !s_dmaQuarantined && !displayShowing &&
(lxTeardowns == 0 || s_deviceRemoved));
events.push_back("surfaces_release");
const unsigned n = surfacesImported;
surfacesImported = 0;
return n;
}
static void *rt_device_get_pdev(void *device) {
assert(device && device == s_rtDevice);
return device;
}
static int rt_pci_probe_cleanup_retained(void *device) {
assert(device && device == s_rtDevice);
return probeCleanupRetained;
}
static uint64_t linuxu_dart_budget() { return 1ull << 30; }
static int dext_dma_begin_shutdown(uint64_t budget) {
assert(budget == linuxu_dart_budget() && s_irqDeliver && s_irqReady);
events.push_back("hold_dma");
return holdError;
}
static int dext_compute_stop() {
assert(s_deviceRemoved || (s_irqDeliver && s_irqReady && s_dmaShutdownPrepared));
events.push_back("compute_stop");
if (!computeError) computeQuiescent = true;
else computeQuiescent = false;
return computeError;
}
static void linuxu_driver_shutdown() {
assert(s_modulesRunning);
assert(s_deviceRemoved || (s_irqDeliver && s_irqReady && s_dmaShutdownPrepared));
events.push_back("upstream_shutdown");
// amdgpu_pci_remove() unplugs the DRM device first, so the aperture
// mapping survives the removal.
}
static void dext_dma_quarantine() { events.push_back("dma_quarantine"); }
static int dext_pci_quarantine() {
events.push_back("pci_quarantine");
if (transportFault == DEXT_PCI_FAULT_NONE) transportFault = DEXT_PCI_FAULT_QUARANTINE;
return isolationError;
}
static int dext_irq_fini_async(void (*callback)(void *), void *context) {
assert(!s_irqDeliver && !s_irqReady && !irqCompletion);
events.push_back("cancel_irqs");
irqCompletion = callback; irqContext = context;
return irqError;
}
static void rt_device_free(void *device) {
assert(device && device == s_rtDevice && irqDrained);
assert(!s_modulesRunning && !s_irqDeliver);
if (!s_deviceRemoved) assert(s_dmaShutdownPrepared && !endpointReset && !dmaCompleted);
events.push_back("device_free");
}
static int dext_bar0_cpu_release_orphaned() {
// Only after every Linux owner is gone; the real seam also requires the
// shutdown hold and no other DMA owner or CPU alias.
assert(irqDrained && !s_rtDevice && !s_modulesRunning && !endpointReset);
const int released = bar0Aliases;
if (released) events.push_back("release_bar0");
bar0Aliases = 0;
return released;
}
static int dext_pci_shutdown_reset() {
assert(irqDrained && !s_irqDeliver && !s_rtDevice && !s_modulesRunning);
assert(s_dmaShutdownPrepared && !dmaCompleted);
events.push_back("endpoint_reset");
// The real DMA seam refuses the reset while a BAR0 CPU mapping exists.
if (bar0Aliases) return -16;
if (resetError && resetFailures) {
if (resetFailures != UINT32_MAX) --resetFailures;
return resetError;
}
endpointReset = true;
// This seam releases held DMA descriptors only after endpoint isolation.
events.push_back("complete_dma");
dmaCompleted = true;
return 0;
}
static bool pciOpenExpected;
static void dext_compute_set_pci_open(bool open) { assert(open == pciOpenExpected); }
static void dext_compute_set_stage(int stage) { assert(stage == DEXT_COMPUTE_STAGE_NONE); }
static int dext_dma_fini() {
assert(irqDrained && dmaCompleted && !s_rtDevice);
// A removed device is never reset; its descriptors completed on removal.
assert(s_deviceRemoved ? dmaRemoved && !endpointReset : endpointReset);
events.push_back("dma_fini");
return dmaFiniError;
}
static void dext_close() {
assert(dmaCompleted && !dmaFiniError && !s_dmaQuarantined);
events.push_back("pci_close");
}
static void rt_gart_reset() {
assert(dmaCompleted && !s_dmaQuarantined);
events.push_back("gart_reset");
}
static int dext_pci_transport_fault() { return transportFault; }
static int dext_compute_quiescent() { return computeQuiescent; }
static int dext_dma_quarantine_releasable() { return !dmaFiniError || endpointReset; }
static int dext_pci_quarantine_releasable() { return transportFault == DEXT_PCI_FAULT_QUARANTINE; }
static int dext_dma_lift_quarantine(int keep) {
assert(keep && s_dmaQuarantined && s_finalCleanup);
events.push_back("lift_dma_quarantine");
// A retried release resets and completes the retained descriptors again.
endpointReset = dmaCompleted = false;
dmaFiniError = 0;
return 0;
}
static int dext_pci_release_quarantine() {
events.push_back("reopen_pci");
if (transportFault != DEXT_PCI_FAULT_QUARANTINE) return -5;
transportFault = DEXT_PCI_FAULT_NONE;
return 0;
}
static int dext_set_pci(void *pci, void *client) {
assert(pci == s_retainedPCI && client == s_driver && !s_pciOpen);
events.push_back("pci_open");
return 0;
}
static int dext_open(uint32_t *token) { ++pciOpens; *token = 9; return 0; }
static int dext_compute_query_info(uint64_t tag, uint64_t *out, int) {
assert(tag == 4); out[0] = 2; return 1;
}
static int dext_compute_release_client(uint64_t) { assert(false); return 0; }
static int dext_pci_close_removed() {
// Nothing left that could touch the device: interrupts drained,
// upstream removed, runtime device freed, DMA released.
assert(irqDrained && !s_modulesRunning && !s_rtDevice && dmaRemoved && !endpointReset);
events.push_back("pci_close_removed");
return 0;
}
static void power_device_removed() { events.push_back("power_lost"); }
void MacLinuxGPU::FinishStop(IOService *provider) {
assert(!s_dmaQuarantined && !s_sessionClosing && dmaCompleted);
events.push_back("super_driver_stop"); ++driverStops;
provider->release(); release();
}
void MacLinuxGPUUserClient::FinishStop(IOService *provider) {
if (!ivars->observer)
assert(!s_dmaQuarantined && !s_sessionClosing && dmaCompleted);
events.push_back(ivars->observer ? "observer_stop" : "super_client_stop");
if (!ivars->observer) ++clientStops;
auto owner = ivars->ownerDriver;
s_rawBARLease.release(ivars->clientID);
ivars = nullptr;
Stop(provider, SUPERDISPATCH);
provider->release(); owner->release(); release();
}
struct Fixture {
MacLinuxGPU driver;
IOPCIDevice provider;
IODispatchQueue queue;
MacLinuxGPUUserClient clients[2], observer;
MacLinuxGPUUserClient_IVars ivars[3]{};
unsigned retainedDriver, retainedProvider;
int device;
Fixture() {
s_driver = &driver; s_retainedPCI = &provider; s_bringupQueue = &queue;
s_rtDevice = &device; s_modulesRunning = true;
s_irqReady = s_irqDeliver = s_pciOpen = true;
s_token = 7; s_stopProvider = &provider;
bar0Aliases = 1;
// Stop has retained the driver/provider; each stopping client also
// retains its provider, original owning driver, and itself.
driver.retain(); provider.retain();
for (unsigned i = 0; i < 2; i++) {
clients[i].ivars = &ivars[i];
ivars[i].ownerDriver = &driver; ivars[i].stopProvider = &driver;
ivars[i].clientID = i + 1; ivars[i].stopping = true;
ivars[i].nextStopping = i ? nullptr : &clients[1];
driver.retain(); driver.retain(); clients[i].retain();
}
s_stoppingClients = &clients[0];
observer.ivars = &ivars[2];
ivars[2].ownerDriver = &driver; ivars[2].clientID = 3;
retainedDriver = driver.references; retainedProvider = provider.references;
}
void deliverIRQDrain() {
assert(irqCompletion && queue.pending.empty());
auto callback = irqCompletion; auto context = irqContext;
irqCompletion = nullptr; irqContext = nullptr;
irqDrained = true; events.push_back("irq_drained");
callback(context);
assert(queue.pending.size() == 1);
queue.drain();
}
void assertRetained(bool missingCallback) {
assert(s_dmaQuarantined && s_sessionClosing && s_quarantineRetained);
assert(!clientStops && !driverStops && s_stopProvider == &provider);
assert(s_stoppingClients == &clients[0] && s_retainedPCI == &provider);
assert(clients[0].ivars == &ivars[0] && clients[1].ivars == &ivars[1]);
assert(clients[0].references == 2 && clients[1].references == 2);
assert(provider.references == retainedProvider);
assert(driver.references == retainedDriver + 1 + unsigned(missingCallback));
assert(s_pciOpen && s_token == 7 && s_sessionGeneration == 1);
}
void assertReleased() {
assert(clientStops == 2 && driverStops == 1);
assert(!s_pciOpen && !s_sessionClosing && !s_dmaShutdownPrepared && !s_token);
assert(!s_dmaQuarantined && !s_quarantineRetained);
assert(!s_stoppingClients && !s_stopProvider && s_sessionGeneration == 2);
assert(driver.references == 1 && provider.references == 1);
}
};
static bool saw(const char *event) {
for (const auto &entry : events) if (entry == event) return true;
return false;
}
static std::vector<uint64_t> state() {
std::vector<uint64_t> out(MLG_SESSION_STATE_WORDS);
session_state(out.data());
assert(out[0] == MLG_SESSION_STATE_VERSION);
return out;
}
static void checkSelectorGuards(Fixture &fixture) {
uint64_t output[2]{};
IOUserClientMethodArguments arguments{0, output, 2};
fixture.observer.ivars->sessionGeneration = s_sessionGeneration;
s_participants = 2;
assert(fixture.observer.shutdown(&arguments) == kIOReturnBusy && events.empty());
s_participants = 1;
assert(s_rawBARLease.claim(3, true, 1) && s_rawBARLease.markMapped(3));
assert(fixture.observer.shutdown(&arguments) == kIOReturnBusy && events.empty());
s_rawBARLease.release(3);
assert(fixture.observer.shutdown(&arguments) == kIOReturnSuccess);
assert(output[0] == kIOReturnBusy && output[1] == 2);
auto count = events.size();
assert(fixture.observer.shutdown(&arguments) == kIOReturnSuccess);
assert(output[0] == kIOReturnBusy && output[1] == 2 && events.size() == count);
fixture.deliverIRQDrain();
assert(fixture.observer.shutdown(&arguments) == kIOReturnSuccess);
assert(output[0] == kIOReturnSuccess && output[1] == 6);
}
// The observer policy reads cached state only and never opens a session.
static void checkObserverPolicy() {
const uint64_t probe[] = {MLG_QUERY_PROBE_STATUS};
const uint64_t session[] = {MLG_QUERY_SESSION_STATE};
const uint64_t log[] = {MLG_QUERY_KERNEL_LOG, 0};
const uint64_t topology[] = {10};
assert(mlg_observer_selector_allowed(MLG_SELECTOR_PING, nullptr, 0));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_RUNTIME_BUILD, nullptr, 0));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_RELEASE_QUARANTINE, nullptr, 0));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, probe, 1));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, session, 1));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, log, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, log, 1));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, probe, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, topology, 1));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, nullptr, 0));
// The power state, and the power selector's ops (PREPARE and RESUME
// check the release entitlement in the handler).
const uint64_t power[] = {MLG_QUERY_POWER_STATE};
const uint64_t query[] = {MLG_POWER_OP_QUERY}, wait[] = {MLG_POWER_OP_WAIT, 3};
const uint64_t prepare[] = {MLG_POWER_OP_PREPARE}, badOp[] = {MLG_POWER_OP_WAIT + 1};
assert(mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, power, 1));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_QUERY_INFO, power, 2));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_POWER, query, 1));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_POWER, wait, 2));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_POWER, prepare, 1));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_POWER, badOp, 1));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_POWER, nullptr, 0));
const uint64_t read[] = {MLG_SYSFS_OP_READ, 0}, list[] = {MLG_SYSFS_OP_LIST, 4096};
const uint64_t write[] = {2, 0}, info[] = {0x1d, 4}, huge[] = {0x1d, MLG_SYSFS_CHUNK_MAX + 1};
assert(mlg_observer_selector_allowed(MLG_SELECTOR_SYSFS_READ, read, 2));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_SYSFS_READ, list, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_SYSFS_READ, write, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_SYSFS_READ, read, 1));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DRM_INFO, info, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DRM_INFO, huge, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DRM_INFO, info, 1));
// Display: an explicit confirmation word, a known op, a pattern only
// for SHOW.
const uint64_t displayProbe[] = {MLG_DISPLAY_OP_PROBE, 0, MLG_DISPLAY_CONFIRM};
const uint64_t displayShow[] = {MLG_DISPLAY_OP_SHOW, MLG_DISPLAY_PATTERN_GRADIENT, MLG_DISPLAY_CONFIRM};
const uint64_t displayOff[] = {MLG_DISPLAY_OP_OFF, 0, MLG_DISPLAY_CONFIRM};
const uint64_t displayNoConfirm[] = {MLG_DISPLAY_OP_SHOW, 0, 0};
const uint64_t displayBadPattern[] = {MLG_DISPLAY_OP_SHOW, MLG_DISPLAY_PATTERNS, MLG_DISPLAY_CONFIRM};
const uint64_t displayOffPattern[] = {MLG_DISPLAY_OP_OFF, 1, MLG_DISPLAY_CONFIRM};
const uint64_t displayStatus[] = {MLG_DISPLAY_OP_STATUS, 0, MLG_DISPLAY_CONFIRM};
const uint64_t displayModes[] = {MLG_DISPLAY_OP_MODES, 0, MLG_DISPLAY_CONFIRM};
const uint64_t displayModesPattern[] = {MLG_DISPLAY_OP_MODES, 1, MLG_DISPLAY_CONFIRM};
const uint64_t displayBadOp[] = {10, 0, MLG_DISPLAY_CONFIRM};
const uint64_t displayImport[] = {MLG_DISPLAY_OP_IMPORT, (2560ULL << 48) | (1440ULL << 32) | 10240,
MLG_DISPLAY_CONFIRM};
const uint64_t displayImportBad[] = {MLG_DISPLAY_OP_IMPORT, (1440ULL << 32) | 10240, MLG_DISPLAY_CONFIRM};
const uint64_t displayVerify[] = {MLG_DISPLAY_OP_VERIFY, (3ULL << 32) | 7, MLG_DISPLAY_CONFIRM};
const uint64_t displayVerifyBad[] = {MLG_DISPLAY_OP_VERIFY, 7, MLG_DISPLAY_CONFIRM};
const uint64_t displayPresent[] = {MLG_DISPLAY_OP_PRESENT, 3, MLG_DISPLAY_CONFIRM};
const uint64_t displayPresentBad[] = {MLG_DISPLAY_OP_PRESENT, 1ULL << 32, MLG_DISPLAY_CONFIRM};
const uint64_t displayOutput[] = {MLG_DISPLAY_OP_OUTPUT, 59951, MLG_DISPLAY_CONFIRM};
const uint64_t displayOutputBad[] = {MLG_DISPLAY_OP_OUTPUT, 0, MLG_DISPLAY_CONFIRM};
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayImport, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayImportBad, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayVerify, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayVerifyBad, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayPresent, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayPresentBad, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayOutput, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayOutputBad, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayStatus, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayModes, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayModesPattern, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayProbe, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayShow, 3));
assert(mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayOff, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayShow, 2));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayNoConfirm, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayBadPattern, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayOffPattern, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, displayBadOp, 3));
assert(!mlg_observer_selector_allowed(MLG_SELECTOR_DISPLAY, nullptr, 0));
char path[MLG_SYSFS_PATH_MAX + 1];
assert(mlg_sysfs_path_copy(path, "gpu_metrics", 11, false) && !std::strcmp(path, "gpu_metrics"));
assert(mlg_sysfs_path_copy(path, "hwmon/hwmon0/temp1_input", 25, false) &&
!std::strcmp(path, "hwmon/hwmon0/temp1_input"));
for (const char *bad : {"", "/gpu_metrics", "hwmon/", "a//b", "..", "../x", "a/../b", "./a", "a/."})
assert(!mlg_sysfs_path_copy(path, bad, std::strlen(bad), false));
assert(!mlg_sysfs_path_copy(path, "a\0b", 3, false) && !mlg_sysfs_path_copy(path, nullptr, 0, false));
// Only a listing may name the device directory itself, with no path.
assert(mlg_sysfs_path_copy(path, nullptr, 0, true) && !path[0]);
assert(mlg_sysfs_path_copy(path, "", 1, true) && !path[0]);
assert(!mlg_sysfs_path_copy(path, "..", 2, true) && !mlg_sysfs_path_copy(path, "/", 1, true));
const std::string longest(MLG_SYSFS_PATH_MAX, 'a'), tooLong(MLG_SYSFS_PATH_MAX + 1, 'a');
assert(mlg_sysfs_path_copy(path, longest.c_str(), longest.size() + 1, false));
assert(!mlg_sysfs_path_copy(path, tooLong.c_str(), tooLong.size(), false));
for (uint64_t selector = 1; selector < 128; ++selector) {
if (selector == MLG_SELECTOR_QUERY_INFO || selector == MLG_SELECTOR_RUNTIME_BUILD ||
selector == MLG_SELECTOR_RELEASE_QUARANTINE) continue;
assert(!mlg_observer_selector_allowed(selector, probe, 1));
}
}
// Successful probe and compute start, then the only client exits without
// destroying anything: no quarantine, and a new client opens a new session.
static void clientExitReopen(bool queueExhausted) {
MacLinuxGPU driver;
IOPCIDevice provider;
IODispatchQueue queue;
MacLinuxGPUUserClient client, next;
MacLinuxGPUUserClient_IVars clientIvars{}, nextIvars{};
int device = 0;
s_driver = &driver; s_retainedPCI = &provider; s_bringupQueue = &queue;
s_rtDevice = &device; s_modulesRunning = true; s_probeAttempted = true;
s_irqReady = s_irqDeliver = s_pciOpen = true; s_token = 7;
bar0Aliases = 1;
client.ivars = &clientIvars;
clientIvars = {&driver, nullptr, nullptr, s_sessionGeneration, 1, false, false, false, nullptr, false,
false, nullptr, nullptr};
driver.retain(); s_participants = 1;
// A second queue found every slot held. The runtime refused it before
// any allocation (or the driver did, with -ENOSPC before reserving), so
// compute stop sees only the first queue and its BOs.
if (queueExhausted) computeError = 0;
assert(client.Stop(&driver) == kIOReturnSuccess);
assert(s_sessionClosing && s_participants == 0 && !clientStops);
{
assert(irqCompletion);
auto callback = irqCompletion; auto context = irqContext;
irqCompletion = nullptr; irqContext = nullptr;
irqDrained = true; events.push_back("irq_drained");
callback(context);
queue.drain();
}
const std::vector<std::string> expected{
"hold_dma", "compute_stop", "upstream_shutdown", "cancel_irqs",
"irq_drained", "enqueue_finish", "device_free", "release_bar0",
"endpoint_reset", "complete_dma", "dma_fini", "pci_close", "gart_reset",
"super_client_stop"};
assert(events == expected);
// The power hook ran once, ahead of upstream removal.
assert(powerRemovalHooks == 1);
assert(!s_dmaQuarantined && !s_sessionClosing && !s_quarantineRetained);
assert(s_sessionGeneration == 2 && clientStops == 1 && client.superStops == 1);
assert(driver.references == 1 && provider.references == 1);
const auto snapshot = state();
assert(!(snapshot[1] & (MLG_SESSION_FLAG_QUARANTINED | MLG_SESSION_FLAG_CLOSING)));
assert(snapshot[2] == MLG_QUARANTINE_NONE && snapshot[6] == MLG_RELEASE_NOT_QUARANTINED);
expectLog("released 1 orphaned BAR0 CPU mapping reference(s) after upstream removal");
expectLog("session closed after upstream removal, interrupt drain and endpoint isolation");
// The dext is reusable: a new client opens a new PCI session.
next.ivars = &nextIvars;
nextIvars = {&driver, nullptr, nullptr, 0, 2, false, false, false, nullptr, false,
false, nullptr, nullptr};
pciOpenExpected = true;
assert(ensure_open(&next) == kIOReturnSuccess);
assert(s_pciOpen && s_participants == 1 && pciOpens == 1 && saw("pci_open"));
assert(nextIvars.sessionGeneration == s_sessionGeneration);
std::printf("PASS production session shutdown: %s\n",
queueExhausted ? "queue-exhaustion-exit" : "client-exit-reopen");
}
// Surprise removal: the GPU leaves the bus with a client's session open.
// The client is stopped first (IOKit terminates the clients, then the
// provider). With "quarantined", the session was already quarantined (its
// compute stop failed while the device was still there) when the provider
// stop sees the device gone. Either way no reset, isolation or quarantine
// follows: everything is released, the provider closes, every Stop
// finishes, and the next client opens a new session.
static void surpriseRemoval(bool quarantined, bool held = false) {
MacLinuxGPU driver;
IOPCIDevice provider;
IODispatchQueue queue;
MacLinuxGPUUserClient client, next;
MacLinuxGPUUserClient_IVars clientIvars{}, nextIvars{};
int device = 0;
s_driver = &driver; s_retainedPCI = &provider; s_bringupQueue = &queue;
s_rtDevice = &device; s_modulesRunning = true; s_probeAttempted = true;
s_irqReady = s_irqDeliver = s_pciOpen = true; s_token = 7;
bar0Aliases = 1;
client.ivars = &clientIvars;
clientIvars = {&driver, nullptr, nullptr, s_sessionGeneration, 1, false, false, false, nullptr, false,
false, nullptr, nullptr};
driver.retain(); s_participants = 1;
// The KFD close cannot confirm anything once MES is gone.
computeError = -11006;
// A display agent was mirroring onto the GPU: an output on screen and
// two imported capture surfaces.
displayShowing = true;
surfacesImported = 2;
// "held": the session was quarantined before the close began, so the
// close keeps the display's owners until the removal releases them.
if (held) { s_dmaQuarantined = true; note_quarantine(MLG_QUARANTINE_COMPUTE_UNCERTAIN, -5); }
if (!quarantined) devicePresent = false;
assert(client.Stop(&driver) == kIOReturnSuccess);
assert(s_sessionClosing && s_participants == 0 && !clientStops);
if (quarantined) {
assert(s_dmaQuarantined && !s_deviceRemoved);
assert(saw("pci_quarantine") && saw("dma_quarantine"));
if (held) {
// Retained with every other owner while quarantined.
assert(displayShowing && surfacesImported == 2 && !saw("display_off"));
} else {
// The close began before anything quarantined: the display went
// off and its imports were released first, with the device there.
assert(!displayShowing && !surfacesImported && saw("display_off") && saw("surfaces_release"));
}
} else {
assert(!s_dmaQuarantined && s_deviceRemoved);
}
{
assert(irqCompletion);
auto callback = irqCompletion; auto context = irqContext;
irqCompletion = nullptr; irqContext = nullptr;
irqDrained = true; events.push_back("irq_drained");
callback(context);
queue.drain();
}
if (quarantined) {
// Retained until the provider stops and finds the device gone,
// which releases it (what MacLinuxGPU::Stop does).
assert(s_dmaQuarantined && !clientStops);
devicePresent = false;
assert(device_removed("provider stop"));
release_removed(&driver);
}
assert(!s_dmaQuarantined && !s_sessionClosing && !s_quarantineRetained && !s_deviceRemoved);
assert(clientStops == 1 && client.superStops == 1);
assert(driver.references == 1 && provider.references == 1 && s_sessionGeneration == 2);
assert(saw("pci_mark_removed") && saw("removal_begin") && saw("compute_removed"));
assert(saw("dma_removed") && saw("power_lost") && saw("removal_end"));
assert(saw("upstream_shutdown") && saw("device_free") && saw("pci_close_removed"));
assert(!saw("endpoint_reset") && !saw("pci_close"));
// The display's owners are gone before upstream removal; for a device
// already known removed nothing was committed.
assert(!displayShowing && !surfacesImported);
{
auto at = [](const char *e) { return std::find(events.begin(), events.end(), e) - events.begin(); };
const char *off = quarantined && !held ? "display_off" : "display_off_removed";
assert(at(off) < at("surfaces_release") && at("surfaces_release") < at("upstream_shutdown"));
if (!quarantined || held) assert(at("removal_begin") < at(off) && !saw("display_off"));
}
expectLog("released 2 imported surface(s)");
if (!quarantined) {
const std::vector<std::string> expected{
"pci_mark_removed", "removal_begin", "compute_removed", "dma_removed", "power_lost",
"display_off_removed", "surfaces_release", "compute_stop", "removal_end", "upstream_shutdown", "cancel_irqs",
"irq_drained", "enqueue_finish", "device_free", "release_bar0", "dma_removed",
"dma_fini", "pci_close_removed", "gart_reset", "super_client_stop"};
assert(events == expected);
assert(!saw("pci_quarantine") && !saw("hold_dma"));
}
const auto snapshot = state();
assert(!(snapshot[1] & (MLG_SESSION_FLAG_QUARANTINED | MLG_SESSION_FLAG_CLOSING |
MLG_SESSION_FLAG_DEVICE_REMOVED)));
expectLog("device removed from the bus");
// The replugged device: a new client opens a new session.
devicePresent = true;
next.ivars = &nextIvars;
nextIvars = {&driver, nullptr, nullptr, 0, 2, false, false, false, nullptr, false,
false, nullptr, nullptr};
pciOpenExpected = true;
assert(ensure_open(&next) == kIOReturnSuccess);
assert(s_pciOpen && s_participants == 1 && saw("pci_open"));
std::printf("PASS production session shutdown: %s\n",
held ? "surprise-removal-held" : quarantined ? "surprise-removal-quarantined" : "surprise-removal");
}
static rt_drm_info observerDrm;
int main(int argc, char **argv) {
alarm(15);
assert(argc == 2);
const std::string scenario = argv[1];
if (scenario == "client-exit-reopen" || scenario == "queue-exhaustion-exit") {
clientExitReopen(scenario == "queue-exhaustion-exit");
return 0;
}
if (scenario == "surprise-removal" || scenario == "surprise-removal-quarantined" ||
scenario == "surprise-removal-held") {
surpriseRemoval(scenario != "surprise-removal", scenario == "surprise-removal-held");
return 0;
}
Fixture fixture;
if (scenario == "log-format") {
checkLogFormat();
checkObserverPolicy();
std::puts("PASS lifecycle log: single printf evaluation, bounded text, retained/platform equality and sink reentry; observer selector policy");
return 0;
}
if (scenario == "shutdown-selector") {
checkSelectorGuards(fixture);
std::puts("PASS production shutdown selector: peer/raw-BAR rejection, busy and completed phases");
return 0;
}
if (scenario == "hold-failure") holdError = 1;
else if (scenario == "compute-failure") computeError = 1;
else if (scenario == "irq-failure" || scenario == "irq-failure-late") irqError = 1;
else if (scenario == "reset-failure") resetError = 1;
else if (scenario == "dma-fini-failure") dmaFiniError = 1;
else if (scenario == "pre-quarantined") s_dmaQuarantined = true;
else if (scenario == "isolation-failure") { s_dmaQuarantined = true; isolationError = 1; }
else if (scenario == "raw-mapped") {
assert(s_rawBARLease.claim(1, true, 1) && s_rawBARLease.markMapped(1));
}
else if (scenario == "probe-retained") {
probeCleanupRetained = true;
s_probeResult = -38;
s_probeAttempted = true;
}
else if (scenario == "observer-quarantined" || scenario == "release-after-isolation-failure") {
resetError = -5;
}
else if (scenario == "release-refused-upstream") computeError = -11006;
else if (scenario == "release-reset-failed") resetError = -110;
else if (scenario == "stop-release") { resetError = -5; resetFailures = 1; s_stopping = true; }
else if (scenario == "pci-fault-cause") { transportFault = DEXT_PCI_FAULT_CONFIG; holdError = -1; }
else if (scenario == "selftest-parked") lxParked = 1;
else if (scenario == "display-showing") { displayShowing = true; surfacesImported = 2; }
else if (scenario == "display-quarantined") {
displayShowing = true; surfacesImported = 1; s_dmaQuarantined = true;
}
else if (scenario == "observer-reads") {
// A running session admits observer reads; one is in flight and the
// observers' render file is open.
s_observerReads.open();
assert(s_observerReads.enter());
observerReadInFlight = true;
s_observerDrm = &observerDrm;
}
else assert(scenario == "success");
if (scenario == "probe-retained")
assert(fixture.observer.failedProbe() == kIOReturnError);
else close_session(&fixture.driver);
assert(!clientStops && !driverStops && s_sessionClosing && s_rtDevice);
assert(!s_irqDeliver && !s_irqReady && !irqDrained);
const auto eventCount = events.size();
const auto references = fixture.driver.references;
close_session(&fixture.driver);
assert(events.size() == eventCount && fixture.driver.references == references);
if (scenario != "irq-failure") {
// Before the drain completes nothing can be released.
if (s_dmaQuarantined && !s_irqDrainFailed && transportFault == DEXT_PCI_FAULT_QUARANTINE)
assert(state()[6] == MLG_RELEASE_IRQ_PENDING);
fixture.deliverIRQDrain();
}
if (scenario == "observer-reads") {
// Admission closes and drains before anything is torn down, and the
// render file closes before the upstream driver is removed.
const std::vector<std::string> expected{
"observer_wait", "hold_dma", "compute_stop", "observer_drm_close",
"upstream_shutdown", "cancel_irqs",
"irq_drained", "enqueue_finish", "device_free", "release_bar0",
"endpoint_reset", "complete_dma", "dma_fini", "pci_close", "gart_reset",
"super_client_stop", "super_client_stop", "super_driver_stop"};
assert(events == expected);
assert(observerDrm.closes == 1 && !s_observerDrm && !s_observerReads.enter());
fixture.assertReleased();
} else if (scenario == "display-showing") {
// The pattern goes first, before Linux-file teardown and removal.
const std::vector<std::string> expected{
"display_off", "surfaces_release", "hold_dma", "compute_stop", "upstream_shutdown", "cancel_irqs",
"irq_drained", "enqueue_finish", "device_free", "release_bar0",
"endpoint_reset", "complete_dma", "dma_fini", "pci_close", "gart_reset",
"super_client_stop", "super_client_stop", "super_driver_stop"};
assert(events == expected && !displayShowing);
fixture.assertReleased();
expectLog("session close: turning the display pattern or output off");
expectLog("session close: released 2 imported surface(s)");
} else if (scenario == "success") {
const std::vector<std::string> expected{
"hold_dma", "compute_stop", "upstream_shutdown", "cancel_irqs",
"irq_drained", "enqueue_finish", "device_free", "release_bar0",
"endpoint_reset", "complete_dma", "dma_fini", "pci_close", "gart_reset",
"super_client_stop", "super_client_stop", "super_driver_stop"};
assert(events == expected);
assert(lxTeardowns == 1);
fixture.assertReleased();
expectLog("session closed after upstream removal, interrupt drain and endpoint isolation");
} else if (scenario == "stop-release") {
// Deactivation must not stall on a quiescent quarantine: the stopping
// driver retries the reset at once and finishes every Stop.
fixture.assertReleased();
assert(saw("lift_dma_quarantine") && saw("reopen_pci") && saw("pci_close"));
expectLog("quarantine cause: step 5 code -5");
expectLog("quarantine released after endpoint reset; provider closed");
} else {
fixture.assertRetained(scenario == "irq-failure");
assert(!saw("pci_close") && !saw("gart_reset"));
if (holdError || computeError || scenario == "pre-quarantined" || scenario == "display-quarantined" ||
scenario == "raw-mapped" || scenario == "isolation-failure" ||
scenario == "probe-retained" || lxParked) {
assert(s_rtDevice && s_modulesRunning);
assert(!saw("device_free") && !saw("endpoint_reset") && !saw("dma_fini"));
}
if (lxParked) {
// The self-test's work is still on the GPU: nothing upstream is
// stopped or removed under it.
assert(events.front() == "lx_teardown_parked");
assert(!saw("hold_dma") && !saw("compute_stop") && !saw("upstream_shutdown"));
}
if (scenario == "raw-mapped") {
assert(!saw("hold_dma") && !saw("compute_stop") && !saw("upstream_shutdown"));
assert(s_rawBARLease.hasMappings() && !s_rawBARLease.allowsJoin(2));
}
if (scenario == "probe-retained") {
assert(!saw("hold_dma") && !saw("compute_stop") && !saw("upstream_shutdown"));
assert(s_modulesRunning && s_rtDevice == &fixture.device);
assert(s_probeAttempted && s_probeResult == -38);
expectLog("upstream PCI probe failed: -38");
expectLog("upstream failed-probe ownership retained; preserving modules, device and DMA backing");
}
if (resetError) assert(!saw("complete_dma") && !saw("dma_fini"));
// Repeated quarantine must not acquire an unbounded service retain.
const auto retained = fixture.driver.references;
quarantine_session(&fixture.driver);
assert(fixture.driver.references == retained);
uint64_t output[2]{};
IOUserClientMethodArguments arguments{0, output, 2};
if (scenario == "raw-mapped")
assert(fixture.observer.shutdown(&arguments) == kIOReturnBusy);
else {
assert(fixture.observer.shutdown(&arguments) == kIOReturnSuccess);
assert(output[0] == kIOReturnError && output[1] == 5);
}
if (holdError == 1) expectLog("cannot reserve DMA backing for shutdown (1)");
if (computeError == 1) expectLog("GPU completion uncertain (1)");
if (irqError) expectLog("interrupt cancellation failed (1)");
if (resetError == 1) expectLog("endpoint isolation failed (1)");
if (dmaFiniError) expectLog("live DMA backing retained (1)");
if (isolationError) expectLog("PCI isolation unconfirmed (1)");
if (scenario == "raw-mapped") expectLog("raw BAR mapping lifetime uncertain");
if (scenario != "irq-failure")
expectLog("session quarantined: retaining clients, provider and runtime owners");
// Every quarantine names its step through the cached session state.
const auto snapshot = state();
assert(snapshot[1] & MLG_SESSION_FLAG_QUARANTINED);
assert(snapshot[1] & MLG_SESSION_FLAG_CLOSING);
uint32_t cause = MLG_QUARANTINE_NONE;
if (scenario == "hold-failure") cause = MLG_QUARANTINE_SHUTDOWN_HOLD;
else if (computeError) cause = MLG_QUARANTINE_COMPUTE_UNCERTAIN;
else if (irqError) cause = MLG_QUARANTINE_IRQ_CANCEL;
else if (resetError) cause = MLG_QUARANTINE_ENDPOINT_ISOLATION;
else if (dmaFiniError) cause = MLG_QUARANTINE_DMA_RETAINED;
else if (scenario == "raw-mapped") cause = MLG_QUARANTINE_RAW_BAR_MAPPING;
else if (scenario == "probe-retained") cause = MLG_QUARANTINE_PROBE_RETAINED;
else if (scenario == "pci-fault-cause") cause = MLG_QUARANTINE_PCI_FAULT;
else if (lxParked) cause = MLG_QUARANTINE_COMPUTE_UNCERTAIN;
assert(snapshot[2] == cause);
if (lxParked) assert(snapshot[6] == MLG_RELEASE_UPSTREAM_RETAINED);
if (scenario == "pci-fault-cause") {
assert(snapshot[3] == DEXT_PCI_FAULT_CONFIG);
assert(snapshot[4] == MLG_QUARANTINE_SHUTDOWN_HOLD);
assert(snapshot[6] == MLG_RELEASE_PCI_FAULT);
}
if (resetError || dmaFiniError) assert(snapshot[3] == (uint64_t)(int64_t)(resetError ? resetError : dmaFiniError));
if (computeError || holdError || scenario == "pre-quarantined" || scenario == "display-quarantined" ||
scenario == "isolation-failure" || scenario == "raw-mapped" ||
scenario == "probe-retained" || irqError || lxParked) {
// Owners that were never removed (or live callbacks) cannot be
// released in this process: the readers say restart, never kill.
assert(snapshot[1] & MLG_SESSION_FLAG_RESTART_REQUIRED);
assert(mlg_release_blocker_permanent((uint32_t)snapshot[6]));
const auto before = events.size();
assert(release_quarantine(&fixture.driver) == snapshot[6]);
assert(events.size() == before);
fixture.assertRetained(scenario == "irq-failure");
expectLog("quarantine release refused");
if (scenario == "release-refused-upstream") {
assert(snapshot[6] == MLG_RELEASE_UPSTREAM_RETAINED);
expectLog("restart required, do not kill the driver");
}
}
if (scenario == "observer-quarantined") {
// An observer is never a participant: stopping it while
// quarantined finishes at once and changes no session state.
const auto before = events.size();
const auto participants = s_participants;
fixture.driver.retain(); // the observer's Start retained its owner
fixture.observer.ivars->sessionGeneration = 0;
fixture.observer.ivars->observer = true;
assert(ensure_open(&fixture.observer) == kIOReturnNotPermitted);
assert(fixture.observer.Stop(&fixture.driver) == kIOReturnSuccess);
assert(events.size() == before + 1 && events.back() == "observer_stop");
assert(s_participants == participants && s_stoppingClients == &fixture.clients[0]);
assert(!fixture.observer.ivars && fixture.observer.superStops == 1);
fixture.assertRetained(false);
assert(state()[6] == MLG_RELEASE_READY);
}
if (scenario == "release-after-isolation-failure" || scenario == "dma-fini-failure" ||
scenario == "reset-failure") {
assert(snapshot[1] & MLG_SESSION_FLAG_RELEASABLE);
assert(!(snapshot[1] & MLG_SESSION_FLAG_RESTART_REQUIRED));
resetError = 0;
assert(release_quarantine(&fixture.driver) == MLG_RELEASE_READY);
fixture.assertReleased();
expectLog("quarantine released after endpoint reset; provider closed");
}
if (scenario == "release-reset-failed") {
assert(snapshot[6] == MLG_RELEASE_READY);
assert(release_quarantine(&fixture.driver) == MLG_RELEASE_RESET_FAILED);
fixture.assertRetained(false);
const auto after = state();
assert(after[6] == MLG_RELEASE_RESET_FAILED);
assert(after[1] & MLG_SESSION_FLAG_RESTART_REQUIRED);
assert(after[2] == MLG_QUARANTINE_ENDPOINT_ISOLATION);
expectLog("quarantine release failed (-110); restart required, do not kill the driver");
}
}
if (scenario == "probe-retained")
expectLog("session close begin: probe=1 result=-38 modules=1 pci=1 quarantine=1");
else expectLog("session close begin: probe=0 result=0 modules=1 pci=1 quarantine=");
expectLog("session close: requesting interrupt drain");
if (scenario != "irq-failure") {
expectLog("interrupt drain completed; scheduling final cleanup");
expectLog("session close: final cleanup entered");
}
std::printf("PASS production session shutdown: %s\n", argv[1]);
}