diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b37058b..c0351b00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.21) # VERSION is what /version reports and what a client checks compatibility against. It is NOT the # ABI version -- that lives in abi/rad_abi.h and is read out of the header below, because # a plugin contract and a server release do not move together. -project(radiance VERSION 1.1.0 LANGUAGES C CXX) +project(radiance VERSION 1.1.1 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/core/device/aql.cpp b/core/device/aql.cpp index 507e59db..cd5c70f2 100644 --- a/core/device/aql.cpp +++ b/core/device/aql.cpp @@ -1023,10 +1023,15 @@ void loc_keep(const void* host_base, size_t bytes, intptr_t shift, uint64_t owne h.gen = gen; } -/* The host ranges this backend pinned itself (see "pinned host memory, in pieces"): base -> - * length, ordered so an inner address finds its range. */ +/* The host ranges this backend pinned itself (see "pinned host memory, in pieces"), by base, + * ordered so an inner address finds its range. `pool` is the host-pool allocation the range sits + * in when it was not locked in pieces, and null when it was. */ +struct Pinned { + int64_t len = 0; + void* pool = nullptr; +}; std::mutex g_pinned_mu; -std::map g_pinned; +std::map g_pinned; bool pinned_range(const void* p, uintptr_t* lo, int64_t* len) { const uintptr_t a = (uintptr_t)p; @@ -1034,9 +1039,9 @@ bool pinned_range(const void* p, uintptr_t* lo, int64_t* len) { auto it = g_pinned.upper_bound(a); if (it == g_pinned.begin()) return false; --it; - if (a - it->first >= (uintptr_t)it->second) return false; + if (a - it->first >= (uintptr_t)it->second.len) return false; if (lo) *lo = it->first; - if (len) *len = it->second; + if (len) *len = it->second.len; return true; } @@ -2040,6 +2045,20 @@ int a_vmem_decommit(void* addr, int64_t bytes) { * of one reservation are consecutive on the card too. Each piece is checked rather than assumed, * because the pools, the mover and the activation arena all hand a kernel base + offset. * + * THAT TAKES THE KERNEL DRIVER'S SVM API, AND WITHOUT IT THE RANGE IS ONE ALLOCATION OF THE HOST + * POOL. A lock lands at its host address because ROCr's thunk registers the range through the + * KFD's SVM API, which maps a range where it is. A driver that does not offer it -- amdgpu could + * not register the card's memory with HMM, seen in a KVM guest with both cards passed through -- + * gets a userptr buffer object instead, at an address the thunk's own aperture hands out: below + * the host address, and each piece a guard page away from the one before, so the pieces are + * neither where the host has them nor next to each other on the card. hsa_amd_vmem cannot join + * them (ROCm 7.2 refuses a vmem handle on the host pool). The host pool's own allocation can: the + * thunk maps it for the CPU at the address the aperture gave the cards. It is one buffer object, + * as hipHostMalloc made before the pieces, so a host whose IOMMU refuses a large pin AND whose + * driver has no SVM API has no large pinned range at all; the refusal names both. Which path a + * process takes is the runtime's HSA_AMD_SYSTEM_INFO_SVM_SUPPORTED, the same capability the thunk + * decides by, and the log says it once. + * * What hipHostMalloc does besides the pin is kept, so the pages are the same pages: shared * anonymous memory, huge pages advised from 2 MiB up, a preferred NUMA node that is the one * nearest the current card (ROCr allocates from that node's pool), fine-grained coherence (the @@ -2093,7 +2112,56 @@ const char* hsa_why(hsa_status_t st) { return hsa_status_string(st, &m) == HSA_STATUS_SUCCESS && m ? m : "unknown HSA status"; } +/* Whether every card's driver offers the SVM API, so that a locked piece is mapped in place. */ +bool svm_in_place() { + static const bool on = [] { + bool svm = false; + if (hsa_system_get_info((hsa_system_info_t)HSA_AMD_SYSTEM_INFO_SVM_SUPPORTED, &svm) != + HSA_STATUS_SUCCESS) + svm = false; + if (!svm) + RAD_INFO("device: the kernel driver offers no SVM API, so a pinned host range is one " + "allocation of the host pool rather than locked in pieces of %s", + humanb(kPinPiece).c_str()); + return svm; + }(); + return on; +} + +/* The range as one allocation of the host pool, for a driver without the SVM API. The pool + * aligns only a buffer's end to 2 MiB, so one alignment more is asked for and the base rounded + * up inside it. */ +void* pin_whole(int64_t bytes, const std::vector& cards) { + const int64_t align = RAD_ALIGN_POOL; + const int64_t len = align_up(bytes, 4096); + const int64_t ask = align_up(len, align) + align; + void* q = nullptr; + hsa_status_t st = hsa_amd_memory_pool_allocate(G().host_fine, (size_t)ask, 0, &q); + if (st != HSA_STATUS_SUCCESS) { + device_set_error("rad_dev_alloc(%s): the kernel driver offers no SVM API, so a pinned " + "range has to be one allocation of the host pool (locked pieces would " + "land apart on the cards), and the pool refused %s: %s", + humanb(bytes).c_str(), humanb(ask).c_str(), hsa_why(st)); + return nullptr; + } + st = hsa_amd_agents_allow_access((uint32_t)cards.size(), cards.data(), nullptr, q); + if (st != HSA_STATUS_SUCCESS) { + device_set_error("rad_dev_alloc(%s): giving the cards access to the host-pool range: %s", + humanb(bytes).c_str(), hsa_why(st)); + (void)hsa_amd_memory_pool_free(q); + return nullptr; + } + char* const p = (char*)(((uintptr_t)q + (uintptr_t)align - 1) & ~((uintptr_t)align - 1)); + std::lock_guard lk(g_pinned_mu); + g_pinned.emplace((uintptr_t)p, Pinned{len, q}); + return p; +} + void* pin_pieces(int64_t bytes) { + std::vector cards; + for (const Dev& d : G().dev) cards.push_back(d.agent); + if (!svm_in_place()) return pin_whole(bytes, cards); + const int64_t align = RAD_ALIGN_POOL; const int64_t len = align_up(bytes, 4096); /* One alignment over, trimmed at both ends, so the range starts on a 2 MiB boundary: a pool's @@ -2113,8 +2181,6 @@ void* pin_pieces(int64_t bytes) { prefer_numa_node(p, len, current_card_numa_node()); if (len >= ((int64_t)2 << 20)) ::madvise(p, (size_t)len, MADV_HUGEPAGE); - std::vector cards; - for (const Dev& d : G().dev) cards.push_back(d.agent); const int pieces = (int)((len + kPinPiece - 1) / kPinPiece); int done = 0; for (; done < pieces; ++done) { @@ -2133,7 +2199,9 @@ void* pin_pieces(int64_t bytes) { if (on_card != p + off) { ++done; device_set_error("rad_dev_alloc(%s): piece %d of %d is mapped on the cards at %p, not " - "at its host address %p, so the pieces are not one range there", + "at its host address %p, so the pieces are not one range there, " + "although the kernel driver reports the SVM API that maps a lock in " + "place", humanb(bytes).c_str(), done, pieces, on_card, (void*)(p + off)); break; } @@ -2144,12 +2212,12 @@ void* pin_pieces(int64_t bytes) { return nullptr; } std::lock_guard lk(g_pinned_mu); - g_pinned.emplace((uintptr_t)p, len); + g_pinned.emplace((uintptr_t)p, Pinned{len, nullptr}); return p; } void unpin_pieces(void* p) { - int64_t len = 0; + Pinned r; { std::lock_guard lk(g_pinned_mu); auto it = g_pinned.find((uintptr_t)p); @@ -2158,9 +2226,16 @@ void unpin_pieces(void* p) { "leaked rather than unpinned", p); return; } - len = it->second; + r = it->second; g_pinned.erase(it); } + if (r.pool) { + const hsa_status_t st = hsa_amd_memory_pool_free(r.pool); + if (st != HSA_STATUS_SUCCESS) + RAD_WARN("device: freeing pinned host-pool range %p: %s", r.pool, hsa_why(st)); + return; + } + const int64_t len = r.len; for (int64_t off = 0; off < len; off += kPinPiece) { const hsa_status_t st = hsa_amd_memory_unlock((char*)p + off); if (st != HSA_STATUS_SUCCESS) diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 08f7896e..6a5b81c5 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -231,15 +231,22 @@ static void report_pool_refusal(int rank, int device, const Pools& pools, const } } } + /* A refusal with room under both is not a capacity one, and advice to shrink the pool + * would send the reader after the wrong cause: the device's reason above is the answer. */ + const int64_t want = (int64_t)cfg.tp * cfg.host_pool_mib * 1024 * 1024 + pinned + mapped; + const bool fits = cap > 0 && avail >= 0 && want <= cap && want <= avail; RAD_ERR("host: --host-pool-mib %lld is pinned once per rank, so %d rank(s) pin %s; this " "process had taken %s of pinned host memory before rank %d's pool. The amdgpu " "driver caps pinned memory system-wide at %s (ttm.pages_limit, half of RAM unless " - "raised), and %s of RAM is available. Lower --host-pool-mib, or raise the cap", + "raised), and %s of RAM is available. %s", (long long)cfg.host_pool_mib, cfg.tp, humanb((int64_t)cfg.tp * cfg.host_pool_mib * 1024 * 1024).c_str(), humanb(pinned + mapped).c_str(), rank, cap > 0 ? humanb(cap).c_str() : "an unreadable limit", - avail >= 0 ? humanb(avail).c_str() : "an unknown amount"); + avail >= 0 ? humanb(avail).c_str() : "an unknown amount", + fits ? "The pools fit under both, so the refusal is not one of size; the reason " + "the device gave is above" + : "Lower --host-pool-mib, or raise the cap"); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4b438547..308cd96c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -152,6 +152,21 @@ if(RAD_HIP_FOUND AND TARGET r4d_selftest) ENVIRONMENT "RADIANCE_HOME=${CMAKE_BINARY_DIR}/radiance_home") endif() +# ================================================================== the device layer without SVM +# +# A KERNEL DRIVER WITHOUT THE SVM API PINS HOST MEMORY ANOTHER WAY, and a machine with it never +# takes that way. ROCr's thunk maps a locked range in place only through the KFD's SVM API; without +# it (a KVM guest whose amdgpu registered no device memory with HMM) the backend allocates a pinned +# range whole from the host pool (core/device/aql.cpp, "pinned host memory, in pieces"). HSA_USE_SVM=0 +# is the thunk's own switch for that driver, so device_test runs a second time under it and the +# path is held on every machine with a card. On the host backend both runs are the same run. +if(RAD_HIP_FOUND AND TARGET device_test) + add_test(NAME device_test_nosvm COMMAND device_test) + set_tests_properties(device_test_nosvm PROPERTIES + SKIP_RETURN_CODE 77 + ENVIRONMENT "RADIANCE_HOME=${CMAKE_BINARY_DIR}/radiance_home;HSA_USE_SVM=0") +endif() + # ================================================================== the host kernels # # rad-avx-check holds libavx to libref, op for op and at every ISA level the machine executes, and diff --git a/tests/device_test.cpp b/tests/device_test.cpp index 37d4ce3a..d2f24018 100644 --- a/tests/device_test.cpp +++ b/tests/device_test.cpp @@ -164,7 +164,8 @@ TEST(alloc_free_roundtrip) { * address on the card, copies and fills that cross a piece boundary landing whole, and none of * them taken for pageable memory -- a fill of pageable memory is refused, so the fill is the * check. 2.5 GiB is the single-pin size an Intel VT-d host refused; the boundaries crossed are - * the whole GiBs. */ + * the whole GiBs. Under HSA_USE_SVM=0 (ctest's device_test_nosvm) the same range is one host-pool + * allocation instead, and the same checks hold it. */ TEST(a_pinned_range_larger_than_one_pin_is_one_range_on_the_card) { const int64_t gib = (int64_t)1 << 30; const int64_t n = 5 * (gib / 2) + 4096 + 17;