Skip to content
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
565fcfa
fix(rocr): unwind failed HSA initialization safely
ihhethan Aug 25, 2026
e4f1e6f
fix(libhsakmt): free cached blocks before the last close consumes the…
ihhethan Aug 31, 2026
5d007f4
test(rocr): prune a subsumed lifecycle case and wire the ops by name
ihhethan Aug 31, 2026
8eeeaec
docs(libhsakmt): correct two comments describing deleted tests
ihhethan Aug 31, 2026
d11f584
fix(rocr): narrow KfdDriver::Close() to the open reference
ihhethan Aug 31, 2026
b3d9b74
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Aug 31, 2026
1b46f51
docs(rocr): explain why an already-open thunk counts as success
ihhethan Aug 31, 2026
1c653a5
fix(rocr): leave the parent's thunk references alone after a fork
ihhethan Sep 2, 2026
8e46794
docs(rocr): name the in-process thunk consumer that exists today
ihhethan Sep 2, 2026
aa1507d
fix(rocr): guard the optional sigbus-delay thunk before calling it
ihhethan Sep 2, 2026
4fc34b0
fix(rocr): name the failing stage when ShutDown() gives up a claim
ihhethan Sep 2, 2026
53d41a0
fix(rocr): guard the loader's dlerror string before printing it
ihhethan Sep 2, 2026
1107662
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Sep 3, 2026
1a16ca1
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Sep 9, 2026
652cd37
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Sep 15, 2026
d3d4fae
fix(rocprofiler-sdk): remove unlocked m_pool read from pool::release()
ihhethan Sep 14, 2026
cf22cb7
fix(rocprofiler-sdk): retire pool storage in clear() instead of freei…
ihhethan Sep 15, 2026
609f7c1
fix(rocprofiler-sdk): stop leaking an hsa_signal_t on every pooled ac…
ihhethan Sep 15, 2026
31df173
refactor(rocprofiler-sdk): lock pool::release() and drop the size mirror
ihhethan Sep 15, 2026
c3342e9
docs(rocprofiler-sdk): name an owner on the pool acquire() TODO
ihhethan Sep 15, 2026
cccfbdf
refactor(rocr): fold KfdLifecycle into KfdDriver
ihhethan Sep 15, 2026
1b0c25a
test(rocr): drop the now-unused unit-test harness header
ihhethan Sep 15, 2026
ccd1f4f
fix(rocprofiler-sdk): close the pool acquire/release data race
ihhethan Sep 16, 2026
4819dd5
fix(rocr): give the XDNA devnode back from ShutDown()
ihhethan Sep 16, 2026
af86cd1
fix(rocprofiler-sdk): reset a pooled signal's value when it is reused
ihhethan Sep 16, 2026
3316b79
style(rocprofiler-sdk): reflow two log statements per clang-format 11
ihhethan Sep 16, 2026
0278e95
refactor(rocprofiler-sdk): restore the construct_hsa_signal name
ihhethan Sep 16, 2026
f654d94
test(rocprofiler-sdk): trim identity and duplicate pool assertions
ihhethan Sep 16, 2026
ad9b08e
fix(rocr): stub unresolved optional thunk entry points
ihhethan Sep 17, 2026
ae395b3
fix(rocr): let a forked child run all three teardown stages
ihhethan Sep 17, 2026
c4a2d0b
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Sep 18, 2026
a7f7be1
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Sep 20, 2026
6fa79a1
fix(rocr): make a failed hsa_init() permanent rather than retryable
ihhethan Sep 21, 2026
611e8df
fix(rocr): stop KfdDriver from guarding against a second open
ihhethan Sep 21, 2026
ba5d5cb
refactor(rocprofiler-sdk): encapsulate pooled signal identity
ihhethan Sep 23, 2026
9a05151
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Sep 24, 2026
74c359a
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Oct 1, 2026
0217362
fix(libhsakmt): drop the cached /dev/dxg fd when hsaKmtOpenKFD() fails
ihhethan Oct 2, 2026
e473115
fix(libhsakmt): serialize DXG fork recovery between concurrent first …
ihhethan Oct 2, 2026
68f63cc
fix(libhsakmt): give back fork-inherited DXG references before live ones
ihhethan Oct 2, 2026
3705bef
refactor(rocr): leave fork ownership of KFD claims to the thunk
ihhethan Oct 2, 2026
1a0c3a7
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Oct 7, 2026
7c23a32
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Oct 28, 2026
0ef1e4b
test(libhsakmt): drop the DXG lifecycle tests
ihhethan Oct 28, 2026
08a7a64
Merge branch 'develop' into users/ihhuang/rocr-hsa-init-lifecycle
ihhethan Nov 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/rocr-runtime-wsl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,19 @@ jobs:
exit 1
fi

# Build the library
# Build the library, and with it the DXG unit tests: they are off by
# default so that a packaging build is unaffected, which leaves this
# job as the only place they ever get built and run.
mkdir -p build
cd build
cmake .. -G Ninja -DWIN_SDK="${win_sdk}/shared"
cmake .. -G Ninja -DWIN_SDK="${win_sdk}/shared" -DBUILD_ROCDXG_TESTS=ON
ninja

echo "libhsakmt build completed successfully!"

- name: Run DXG unit tests
shell: wsl-bash {0}
run: |
set -euo pipefail
cd ~/rocm-systems/projects/rocr-runtime/libhsakmt/build
ctest --output-on-failure
10 changes: 10 additions & 0 deletions projects/rocr-runtime/libhsakmt/CMakeLists_wsl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ install ( TARGETS ${ROCDXG_TARGET} EXPORT ${ROCDXG_TARGET}Targets
# separate header installation is needed here (and duplicating it breaks
# TheRock artifact uniqueness checks — see ROCm/TheRock#7005).

# Pure-logic unit tests. One drives the KFD lifecycle state machine with
# injected thunk calls; the other reads the built library's dynamic symbol
# table. Neither needs a GPU or a DXCore adapter, so `ctest` here works on a
# machine with neither. Off by default so a packaging build is unaffected.
option ( BUILD_ROCDXG_TESTS "Build the librocdxg unit tests" OFF )
if ( BUILD_ROCDXG_TESTS )
enable_testing()
add_subdirectory ( tests/dxg )
endif()

# Record our usage data for clients find_package calls.
install ( EXPORT ${ROCDXG_TARGET}Targets
FILE ${ROCDXG_TARGET}Targets.cmake
Expand Down
8 changes: 7 additions & 1 deletion projects/rocr-runtime/libhsakmt/include/hsakmt/hsakmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
extern "C" {
#endif

/* Forward declaration for debug trap ioctl arguments */
/* Forward declarations for the KFD ioctl arguments the entry points below take
* by pointer. linux/kfd_ioctl.h defines them and the dev package excludes it,
* so without these a consumer's C compiler invents a type scoped to the
* prototype and the pointer it passes is a different one.
*/
struct kfd_ioctl_dbg_trap_args;
struct kfd_runtime_info;
struct kfd_dbg_device_info_entry;

/**
"Opens" the HSA kernel driver for user-kernel mode communication.
Expand Down
23 changes: 19 additions & 4 deletions projects/rocr-runtime/libhsakmt/src/dxg/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtDbgAddressWatch(
HSAKMT_STATUS HSAKMTAPI hsaKmtCheckRuntimeDebugSupport(void) {
CHECK_DXG_OPEN();

// Make sure the WDDMDevices are created.
HsaSystemProperties props;
hsaKmtAcquireSystemProperties(&props);
[](auto&&...) {}(props);
// The caller owns the topology snapshot the WDDMDevices belong to; this probe
// deliberately takes no reference of its own. Acquiring here would enumerate
// the adapters only to destroy them again on return, leaving the caller to
// build the very same list a second time.
//
// An empty device list therefore means no snapshot is held (or the machine
// has no adapter). Say so, rather than letting the loop below fall through
// and report support for devices nobody has looked at.
if (dxg_topology->wdevices_.empty()) {
pr_warn_once(
"DXG system properties must be acquired before checking runtime debug support\n");
return HSAKMT_STATUS_NOT_SUPPORTED;
}

for (auto&& device : dxg_topology->wdevices_) {
if (Wkmi::KmdDbgVersion version;
Expand All @@ -77,6 +86,12 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtCheckRuntimeDebugSupport(void) {
}

HSAKMT_STATUS HSAKMTAPI hsaKmtRuntimeEnable(void *rDebug, bool setupTtmp) {
/* No acquire here either: the caller holds the snapshot across this call.
* ROCr's KfdDriver takes it in Init() before enabling, precisely so the
* device list this walks is the one BuildTopology() goes on to use. The
* probe reports NOT_SUPPORTED when that list is empty, so a caller that has
* not acquired never reaches the loop below.
*/
HSAKMT_STATUS result = hsaKmtCheckRuntimeDebugSupport();

if (result)
Expand Down
4 changes: 4 additions & 0 deletions projects/rocr-runtime/libhsakmt/src/dxg/librocdxg.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ uint32_t get_direct_link_cpu(uint32_t gpu_node);
HSAKMT_STATUS topology_sysfs_get_system_props(HsaSystemProperties& props);
HSAKMT_STATUS topology_get_node_props(HSAuint32 NodeId,
HsaNodeProperties *NodeProperties);
void topology_clear_snapshot_refs(void);
void topology_abandon_after_fork(void);
bool topology_snapshot_is_live(void);
void topology_drop_snapshot_at_last_close(void);
HSAKMT_STATUS topology_get_iolink_props(HSAuint32 NodeId,
HSAuint32 NumIoLinks,
HsaIoLinkProperties *IoLinkProperties);
Expand Down
55 changes: 49 additions & 6 deletions projects/rocr-runtime/libhsakmt/src/dxg/openclose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,17 @@ static void parent_fork_handler(void) { dxg_runtime->hsakmt_mutex.unlock(); }
static void child_fork_handler(void) {
dxg_runtime->is_forked = true;

/* Sever the references the child inherited but never took, before any public
* entry point can act on them. CHECK_DXG_OPEN() already rejects calls while
* is_forked is set, but zeroing the counts means that even a path that
* bypasses it cannot decrement the parent's bookkeeping. The inherited open
* count is cleared with a plain store and the snapshot count with a relaxed
* store on a lock-free atomic, so neither allocates nor takes a lock; heavier
* snapshot and object teardown waits for clear_after_fork().
*/
dxg_runtime->dxg_open_count = 0;
topology_clear_snapshot_refs();

/* prepare_fork_handler() locked hsakmt_mutex right before fork() so that
* no other thread would be mid-operation during the fork snapshot. In the
* child only this one thread survives, and its TID differs from whichever
Expand All @@ -555,6 +566,11 @@ static void child_fork_handler(void) {
static void clear_after_fork(void) {
reset_suballocator();
clear_allocation_map();
/* The snapshot's WDDMDevices belong to the parent's DXCore session, which
* the runtime reset below discards. Let go of them rather than reusing or
* destroying them.
*/
topology_abandon_after_fork();

if (dxg_runtime->dxg_fd >= 0) {
#if defined(__linux__)
Expand Down Expand Up @@ -669,15 +685,22 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) {
HsaSystemProperties sys_props;
char *error;

std::lock_guard<std::recursive_mutex> lck(dxg_runtime->hsakmt_mutex);

/* If the process has forked, the child process must re-initialize
* it's connection to DXG. Any references tracked by dxg_open_count
* belong to the parent
* belong to the parent.
*
* This runs before the lock is taken, and must: clear_after_fork() replaces
* *dxg_runtime, so a lock_guard constructed on the old object would unlock
* freed memory when it went out of scope. Only the forking thread survives
* into the child and child_fork_handler() has already reinitialized the
* mutex, so there is nothing here to exclude. is_forked is only ever written
* in a single-threaded child, so reading it unlocked races with nothing.
*/
if (is_forked_child())
clear_after_fork();

std::lock_guard<std::recursive_mutex> lck(dxg_runtime->hsakmt_mutex);

if (dxg_runtime->dxg_open_count == 0) {
static bool atfork_installed = false;

Expand Down Expand Up @@ -711,7 +734,16 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) {



dxg_runtime->dxg_open_count = 1;
dxg_runtime->dxg_open_count++;

/* Only the 0->1 transition owns the suballocator. A later open comes from
* a second in-process consumer (on WSL, rocprofiler-sdk reading the KMT
* topology alongside the HSA runtime) and the first one's allocations are
* still live - resetting the fragment allocator here would throw away the
* bookkeeping that tracks them. clear_after_fork() above resets it on the
* one path where the inherited bookkeeping really is meaningless.
*/
reset_suballocator();

if (!atfork_installed) {
/* Atfork handlers cannot be uninstalled and
Expand All @@ -730,7 +762,6 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtOpenKFD(void) {
result = HSAKMT_STATUS_KERNEL_ALREADY_OPENED;
}

reset_suballocator();
return result;
dxcore_loader_failed:
#if defined(__linux__)
Expand All @@ -746,7 +777,18 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtCloseKFD(void) {
std::lock_guard<std::recursive_mutex> lck(dxg_runtime->hsakmt_mutex);

if (dxg_runtime->dxg_open_count > 0) {
if (--dxg_runtime->dxg_open_count == 0) {
/* Recognize the last reference without consuming it yet. The teardown below
* releases GPU memory through hsaKmtFreeMemoryInternal(), whose
* CHECK_DXG_OPEN() rejects every call once the count reads zero. Decrementing
* first would therefore make each of those frees fail and leak the very
* allocations the teardown runs to release, so the decrement follows it.
*/
if (dxg_runtime->dxg_open_count == 1) {
/* Before DXCore goes, and so before the WDDMDevice objects a snapshot
* names become pointers into a dead session.
*/
topology_drop_snapshot_at_last_close();

dxg_runtime->HeapFini();
#if defined(__linux__)
close(dxg_runtime->dxg_fd);
Expand All @@ -755,6 +797,7 @@ HSAKMT_STATUS HSAKMTAPI hsaKmtCloseKFD(void) {
wsl::thunk::dxcore::DxcoreLoader::Instance().Shutdown();
}

--dxg_runtime->dxg_open_count;
result = HSAKMT_STATUS_SUCCESS;
} else
result = HSAKMT_STATUS_KERNEL_IO_CHANNEL_NOT_OPENED;
Expand Down
Loading
Loading