Files
mac_linuxgpu/hsa/tests/test_ipc_lifetime.cpp
geramyloveless 5a437c81cc HSA runtime: no abort at exit, and a clear status during a session close
Every LemonSeed Engine run on 246 aborted in exit() with "mutex lock
failed: Invalid argument". The runtime's static executable registry
outlived the transport's static mutex (added in 243 with the
session-call protocol check, ff324ff). Its destructor, run by
__cxa_finalize, freed each loaded executable's GPU buffer through the
connection, and the RPC locked the already-destroyed mutex. 242 had no
such mutex on that path. The runtime's process-wide state (registries,
agents, pools, allocations, signals, queues and their mutexes) is now
never destroyed: a client that exits without hsa_shut_down leaves the
GPU memory for the driver to reclaim when its connection closes with the
process. hsa_shut_down still tears everything down.

hsa_init while the driver closes a session failed with
HSA_STATUS_ERROR_INVALID_AGENT, because NewUserClient refuses session
clients then (kIOReturnNotAttached). The runtime now asks an observer
for the session state and:
- waits for a close in progress, bounded by MAC_HSA_SESSION_CLOSE_WAIT_MS
  (60 s by default), saying so on stderr;
- returns HSA_STATUS_ERROR_RESOURCE_BUSY for a close still running at the
  bound, or a retiring driver (an upgrade);
- returns HSA_STATUS_ERROR_FATAL at once for a session that will not
  close (quarantined, restart required, GPU wedged or removed).
hsa_status_string names HSA_STATUS_ERROR_RESOURCE_BUSY.

test-client-transports:
- runs the HSA runtime's IOKit transport, as a process of its own, with
  an executable and a queue loaded and no hsa_shut_down. Before this
  change it aborts in exit() with the same "mutex lock failed".
- runs hsa_init during a close that finishes (it waits), past the bound
  (busy), during a retire (busy) and with a quarantined session (lost).
2026-10-05 11:54:50 -07:00

94 lines
4.0 KiB
C++

#include "runtime_state.h"
#include <cassert>
#include <condition_variable>
#include <cstdio>
#include <set>
#include <thread>
#include <unistd.h>
namespace mac_hsa::detail {
std::mutex &runtimeMutex = *new std::mutex; // runtime_state.h: process-lifetime state
uint32_t references = 1;
std::vector<Agent> &agents = *new std::vector<Agent>;
std::map<uintptr_t, std::shared_ptr<Allocation>> &allocations = *new std::map<uintptr_t, std::shared_ptr<Allocation>>;
Agent *findAgent(hsa_agent_t handle) {
for (auto &agent : agents) if (agent.handle.handle == handle.handle) return &agent;
return nullptr;
}
std::shared_ptr<Allocation> findAllocation(const void *pointer) {
auto it = allocations.upper_bound(uintptr_t(pointer));
if (it == allocations.begin()) return {};
--it;
return uintptr_t(pointer) - it->first < it->second->size ? it->second : nullptr;
}
}
using namespace mac_hsa;
using namespace mac_hsa::detail;
class MockConnection final : public Connection {
std::mutex mutex;
std::condition_variable changed;
std::set<uint64_t> live;
public:
unsigned imports = 0, releases = 0;
bool synchronize = true;
hsa_status_t read(DeviceSnapshot &) override { return HSA_STATUS_SUCCESS; }
hsa_status_t importBuffer(const BufferToken &token, DeviceBuffer &buffer) override {
std::unique_lock lock(mutex);
const auto handle = ++imports;
live.insert(handle);
changed.notify_all();
if (synchronize) changed.wait(lock, [&] { return imports == 2; });
buffer = {handle, 0x800000000ull, token.size};
return HSA_STATUS_SUCCESS;
}
hsa_status_t freeBuffer(const DeviceBuffer &buffer) override {
// A last-reference release may call arbitrary transport code. Holding
// the registry mutex here would deadlock a transport reentry.
std::lock_guard registry(runtimeMutex);
std::lock_guard lock(mutex);
assert(live.erase(buffer.handle) == 1);
++releases;
return HSA_STATUS_SUCCESS;
}
};
int main() {
alarm(15);
const hsa_agent_t gpu{17};
auto connection = std::make_shared<MockConnection>();
agents.push_back({gpu, connection});
BufferToken token{5, {23, 29}, 16384};
hsa_amd_ipc_memory_t handle{};
std::memcpy(&handle, &token, sizeof(handle));
void *pointers[2]{};
hsa_status_t statuses[2]{};
std::thread first([&] { statuses[0] = hsa_amd_ipc_memory_attach(&handle, token.size, 1, &gpu, &pointers[0]); });
std::thread second([&] { statuses[1] = hsa_amd_ipc_memory_attach(&handle, token.size, 1, &gpu, &pointers[1]); });
first.join(); second.join();
assert(statuses[0] == HSA_STATUS_SUCCESS && statuses[1] == HSA_STATUS_SUCCESS);
assert(pointers[0] && pointers[0] == pointers[1]);
assert(connection->imports == 2 && connection->releases == 1);
assert(allocations.size() == 1 && allocations.begin()->second->ipcReferences == 2);
void *again = nullptr;
assert(hsa_amd_ipc_memory_attach(&handle, token.size, 1, &gpu, &again) == HSA_STATUS_SUCCESS);
assert(again == pointers[0] && connection->imports == 2);
assert(allocations.begin()->second->ipcReferences == 3);
connection->synchronize = false;
++token.token[0];
std::memcpy(&handle, &token, sizeof(handle));
assert(hsa_amd_ipc_memory_attach(&handle, token.size, 1, &gpu, &again) == HSA_STATUS_ERROR_OUT_OF_RESOURCES);
assert(!again && connection->imports == 3 && connection->releases == 2);
assert(allocations.begin()->second->ipcReferences == 3);
assert(hsa_amd_ipc_memory_detach(pointers[0]) == HSA_STATUS_SUCCESS);
assert(hsa_amd_ipc_memory_detach(pointers[0]) == HSA_STATUS_SUCCESS);
assert(connection->releases == 2);
assert(hsa_amd_ipc_memory_detach(pointers[0]) == HSA_STATUS_SUCCESS);
assert(allocations.empty() && connection->releases == 3);
assert(hsa_amd_ipc_memory_detach(pointers[0]) == HSA_STATUS_ERROR_INVALID_ARGUMENT);
agents.clear();
std::puts("PASS HSA IPC concurrent duplicate imports, balanced detach, alias rejection and unlocked BO release");
}