Archived
sycl: keep the device out of plain system memory
On kernel 7.0 the xe driver can oops (drm_pagemap_zdd_destroy from __drm_pagemap_migrate_to_ram) when the CPU faults on a page its shared virtual memory had migrated into VRAM. The backend reached plain system memory in several ways; each now uses USM the runtime knows: - RAD_MEM_HOST_PINNED/_MAPPED are sycl::aligned_alloc_host, not posix_memalign + prepare_for_device_copy. O_DIRECT cannot target those pages (EFAULT), so DirectFile bounces such transfers through an 8 MiB buffer. The 27B load is not slower (10.8 s read, disk-bound). - Copies with a pageable side are staged through a host-USM bounce, synchronously (rare: startup and tests). The ranged copy stages a host range list even when its source is on the card, and refuses a pageable destination, which its kernel would write. - scatter_rows' bad-index flag is host USM, not shared. - Gates are gone (the documented state): a spinning kernel never sees a host store to host USM, and a device flag opened by a copy from another queue deadlocks behind copies queued past the gate. rad-kbench times ungated; the gate tests skip on backends without gates. Checked: device_test (sycl) 70 cases, 0 failed; rad-kbench fixture 1989/1989; 27B int4 chat output byte-identical (c4c5ba5e727c) at 30.6 tok/s; no xe messages in the kernel log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4b70a96378
commit
8931aae8b2
5 files changed
+171
-143
No files matched your search
+118
-137
@@ -272,24 +272,15 @@ int d_vram_used(int device, int64_t* out) {
|
||||
|
||||
/* ------------------------------------------------------------------ memory
|
||||
*
|
||||
* Pinned host memory (RAD_MEM_HOST_PINNED and _MAPPED) is ordinary page-aligned memory registered
|
||||
* with the driver (prepare_for_device_copy), not SYCL's malloc_host. The driver maps malloc_host
|
||||
* pages itself, and O_DIRECT cannot read into them (EFAULT), while the mover reads the file tier
|
||||
* straight into pinned buffers (directio.h). Registered memory copies at the same rate (28.4 GB/s
|
||||
* either way on the Arc Pro B65) and kernels reach it through system USM, which the Intel driver
|
||||
* provides (aspect usm_system_allocations). The runtime does not know such a pointer, so the
|
||||
* ranges are kept here for host_ptr, device_ptr and the memset check. */
|
||||
std::mutex g_pinned_m;
|
||||
std::map<uintptr_t, size_t> g_pinned; /* base -> bytes */
|
||||
|
||||
bool in_pinned(const void* p) {
|
||||
const uintptr_t a = (uintptr_t)p;
|
||||
std::lock_guard<std::mutex> lk(g_pinned_m);
|
||||
auto it = g_pinned.upper_bound(a);
|
||||
if (it == g_pinned.begin()) return false;
|
||||
--it;
|
||||
return a < it->first + it->second;
|
||||
}
|
||||
* Pinned host memory (RAD_MEM_HOST_PINNED and _MAPPED) is SYCL's malloc_host: USM host memory
|
||||
* the runtime knows, which kernels and the copy engines reach through the driver's own mapping.
|
||||
* Nothing the device touches is plain system memory. Ordinary pages registered with
|
||||
* prepare_for_device_copy were used before, so that O_DIRECT could read into them; the device then
|
||||
* reaches them through the xe driver's shared virtual memory, which may migrate the pages into
|
||||
* VRAM, and on kernel 7.0 the CPU fault that moves such a page back can oops the kernel
|
||||
* (drm_pagemap_zdd_destroy from __drm_pagemap_migrate_to_ram). O_DIRECT cannot read into
|
||||
* malloc_host pages (EFAULT, the driver maps them itself), so DirectFile bounces those transfers
|
||||
* (directio.cpp). */
|
||||
|
||||
/* Device bytes this process holds through rad_dev_alloc, for the room check's message. */
|
||||
std::atomic<int64_t> g_device_bytes{0};
|
||||
@@ -341,19 +332,11 @@ void* d_alloc(int64_t bytes, int kind) {
|
||||
break;
|
||||
case RAD_MEM_HOST_PINNED:
|
||||
case RAD_MEM_HOST_MAPPED: {
|
||||
/* Registered page-aligned memory (see above): one address on both sides, so
|
||||
* host_ptr and device_ptr are the pointer itself. */
|
||||
/* USM host memory (see above): one address on both sides, so host_ptr and
|
||||
* device_ptr are the pointer itself. Page-aligned and whole pages, as the
|
||||
* RAD_ALIGN_UNIT pools and the file tier's extents expect. */
|
||||
const size_t n = (size_t)((bytes + 4095) / 4096 * 4096);
|
||||
if (posix_memalign(&p, 4096, n) != 0) p = nullptr;
|
||||
if (!p) break;
|
||||
try {
|
||||
syclex::prepare_for_device_copy(p, n, *rt().ctx);
|
||||
} catch (...) {
|
||||
std::free(p);
|
||||
throw;
|
||||
}
|
||||
std::lock_guard<std::mutex> lk(g_pinned_m);
|
||||
g_pinned[(uintptr_t)p] = n;
|
||||
p = sycl::aligned_alloc_host(4096, n, *rt().ctx);
|
||||
break;
|
||||
}
|
||||
case RAD_MEM_HOST:
|
||||
@@ -392,20 +375,21 @@ void d_free(void* p, int kind) {
|
||||
break;
|
||||
case RAD_MEM_HOST_PINNED:
|
||||
case RAD_MEM_HOST_MAPPED: {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_pinned_m);
|
||||
if (!g_pinned.erase((uintptr_t)p)) {
|
||||
RAD_WARN("rad_dev_free(%p): not pinned memory this backend gave out; leaked "
|
||||
"rather than freed with the wrong allocator", p);
|
||||
break;
|
||||
}
|
||||
sycl::usm::alloc k = sycl::usm::alloc::unknown;
|
||||
try {
|
||||
k = sycl::get_pointer_type(p, *rt().ctx);
|
||||
} catch (const sycl::exception&) {
|
||||
}
|
||||
if (k != sycl::usm::alloc::host) {
|
||||
RAD_WARN("rad_dev_free(%p): not pinned memory this backend gave out; leaked "
|
||||
"rather than freed with the wrong allocator", p);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
syclex::release_from_device_copy(p, *rt().ctx);
|
||||
sycl::free(p, *rt().ctx);
|
||||
} catch (const sycl::exception& e) {
|
||||
RAD_WARN("device: rad_dev_free(%p): %s", p, e.what());
|
||||
}
|
||||
std::free(p);
|
||||
break;
|
||||
}
|
||||
case RAD_MEM_HOST:
|
||||
@@ -418,10 +402,8 @@ void d_free(void* p, int kind) {
|
||||
}
|
||||
}
|
||||
|
||||
/* What `p` is to the device: USM device memory, host memory it reaches (SYCL's or this backend's
|
||||
* registered pinned ranges), or unknown (pageable). */
|
||||
/* What `p` is to the device: USM device, host or shared memory, or unknown (pageable). */
|
||||
sycl::usm::alloc ptr_kind(const void* p) {
|
||||
if (in_pinned(p)) return sycl::usm::alloc::host;
|
||||
try {
|
||||
return sycl::get_pointer_type(p, *rt().ctx);
|
||||
} catch (const sycl::exception&) {
|
||||
@@ -446,7 +428,6 @@ void* d_device_ptr(void* p) {
|
||||
struct SyclStream {
|
||||
RadSyclStream pub;
|
||||
sycl::queue q;
|
||||
std::atomic<int> gates{0}; /* closed gates on this stream (rad_stream_gate) */
|
||||
char* stage = nullptr; /* rad_memcpy_ranges_async's device copy of host sources */
|
||||
size_t stage_n = 0;
|
||||
SyclStream(const sycl::context& c, const sycl::device& d, const sycl::property_list& pl)
|
||||
@@ -516,11 +497,6 @@ int d_stream_sync(RadStream st) {
|
||||
device_set_error("rad_stream_sync: null stream");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (as_stream(st)->gates.load() > 0) {
|
||||
device_set_error("rad_stream_sync: the stream waits at a closed gate, which only "
|
||||
"rad_gate_open releases");
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
SYCL_TRY("rad_stream_sync", as_stream(st)->q.wait_and_throw());
|
||||
return async_status("rad_stream_sync");
|
||||
}
|
||||
@@ -646,6 +622,40 @@ void d_host_wrote(void) {}
|
||||
|
||||
/* ------------------------------------------------------------------ copies and fills */
|
||||
|
||||
/* A copy with a side in pageable host memory (a std::vector, the stack) goes through a bounce of
|
||||
* USM host memory, and synchronously: handed such a pointer, the runtime may reach it through the
|
||||
* driver's shared virtual memory, which the backend keeps out of (see memory, above). Rare -- the
|
||||
* engine copies through pinned buffers -- so the wait costs nothing on the step path. `span` is
|
||||
* the bytes from each base the copy can touch; `run` makes the copy with the bounce standing in
|
||||
* for the pageable side(s). */
|
||||
bool is_pageable(const void* p) { return ptr_kind(p) == sycl::usm::alloc::unknown; }
|
||||
|
||||
template <class Run>
|
||||
void bounced(sycl::queue& q, void* dst, const void* src, size_t span, Run run) {
|
||||
const bool pd = is_pageable(dst), ps = is_pageable(src);
|
||||
char* bd = pd ? (char*)sycl::malloc_host(span, q) : nullptr;
|
||||
char* bs = ps ? (char*)sycl::malloc_host(span, q) : nullptr;
|
||||
if ((pd && !bd) || (ps && !bs)) {
|
||||
if (bd) sycl::free(bd, q);
|
||||
if (bs) sycl::free(bs, q);
|
||||
throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation),
|
||||
"bounce for pageable host memory");
|
||||
}
|
||||
if (bs) std::memcpy(bs, src, span);
|
||||
if (bd) std::memcpy(bd, dst, span); /* bytes between rows of a 2-D copy stay as they were */
|
||||
try {
|
||||
run(bd ? (void*)bd : dst, bs ? (const void*)bs : src);
|
||||
q.wait_and_throw();
|
||||
} catch (...) {
|
||||
if (bd) sycl::free(bd, q);
|
||||
if (bs) sycl::free(bs, q);
|
||||
throw;
|
||||
}
|
||||
if (bd) std::memcpy(dst, bd, span);
|
||||
if (bd) sycl::free(bd, q);
|
||||
if (bs) sycl::free(bs, q);
|
||||
}
|
||||
|
||||
int d_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream st) {
|
||||
RAD_TRY(check_stream(st, "rad_memcpy_async"));
|
||||
if (bytes == 0) return RAD_OK;
|
||||
@@ -654,7 +664,14 @@ int d_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream st) {
|
||||
(long long)bytes);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
SYCL_TRY("rad_memcpy_async", as_stream(st)->q.memcpy(dst, src, (size_t)bytes));
|
||||
sycl::queue& q = as_stream(st)->q;
|
||||
if (is_pageable(dst) || is_pageable(src)) {
|
||||
SYCL_TRY("rad_memcpy_async", bounced(q, dst, src, (size_t)bytes, [&](void* d, const void* s) {
|
||||
q.memcpy(d, s, (size_t)bytes);
|
||||
}));
|
||||
return async_status("rad_memcpy_async");
|
||||
}
|
||||
SYCL_TRY("rad_memcpy_async", q.memcpy(dst, src, (size_t)bytes));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -668,9 +685,18 @@ int d_memcpy_2d_async(void* dst, int64_t dpitch, const void* src, int64_t spitch
|
||||
(long long)height);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
sycl::queue& q = as_stream(st)->q;
|
||||
if (is_pageable(dst) || is_pageable(src)) {
|
||||
/* One span for both sides, the larger pitch's. */
|
||||
const size_t span = (size_t)(std::max(dpitch, spitch) * (height - 1) + width);
|
||||
SYCL_TRY("rad_memcpy_2d_async", bounced(q, dst, src, span, [&](void* d, const void* s) {
|
||||
q.ext_oneapi_memcpy2d(d, (size_t)dpitch, s, (size_t)spitch, (size_t)width, (size_t)height);
|
||||
}));
|
||||
return async_status("rad_memcpy_2d_async");
|
||||
}
|
||||
SYCL_TRY("rad_memcpy_2d_async",
|
||||
as_stream(st)->q.ext_oneapi_memcpy2d(dst, (size_t)dpitch, src, (size_t)spitch,
|
||||
(size_t)width, (size_t)height));
|
||||
q.ext_oneapi_memcpy2d(dst, (size_t)dpitch, src, (size_t)spitch, (size_t)width,
|
||||
(size_t)height));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -716,24 +742,42 @@ int d_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadC
|
||||
dst, src);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (n > 0 && is_pageable(dst)) {
|
||||
/* The ranges are written by a kernel, which pageable memory is not open to (as for
|
||||
* rad_memset_async, device.h note 1). */
|
||||
device_set_error("rad_memcpy_ranges_async(%p): the destination is not device-accessible "
|
||||
"(pageable host memory)", dst);
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
SyclStream* ss = as_stream(st);
|
||||
sycl::queue& q = ss->q;
|
||||
char* d = (char*)dst;
|
||||
const char* s = (const char*)src;
|
||||
SYCL_TRY("rad_memcpy_ranges_async", {
|
||||
if (prefix > 0) q.memcpy(d, s, (size_t)prefix);
|
||||
if (prefix > 0) {
|
||||
if (is_pageable(d) || is_pageable(s))
|
||||
bounced(q, d, s, (size_t)prefix, [&](void* dd, const void* ff) { q.memcpy(dd, ff, (size_t)prefix); });
|
||||
else
|
||||
q.memcpy(d, s, (size_t)prefix);
|
||||
}
|
||||
/* Sources and ranges in host memory (the step upload's pinned staging): read by the copy
|
||||
* kernel they cost a link round trip a load -- 650 us a call over a PCIe x1 link -- so
|
||||
* their span and the range list cross first in one transfer each, into a device buffer
|
||||
* of the stream's (in order, so the next call's copy waits for this kernel). */
|
||||
if (n > 0 && ptr_kind(src) != sycl::usm::alloc::device && ptr_kind(ranges) != sycl::usm::alloc::device) {
|
||||
/* Either in host memory: the source span, the range list or both cross into the stage.
|
||||
* A pageable one goes through the bounce (see bounced), so the kernel only ever reads
|
||||
* device memory. */
|
||||
const bool host_src = n > 0 && ptr_kind(src) != sycl::usm::alloc::device;
|
||||
const bool host_rng = n > 0 && ptr_kind(ranges) != sycl::usm::alloc::device;
|
||||
if (host_src || host_rng) {
|
||||
int64_t lo = INT64_MAX, hi = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
lo = std::min<int64_t>(lo, ranges[i].src);
|
||||
hi = std::max<int64_t>(hi, ranges[i].src + ranges[i].bytes);
|
||||
}
|
||||
lo &= ~(int64_t)15;
|
||||
const size_t span = (size_t)(hi - lo + 15) / 16 * 16, rb = (size_t)n * sizeof(RadCopyRange);
|
||||
const size_t span = host_src ? (size_t)(hi - lo + 15) / 16 * 16 : 0;
|
||||
const size_t rb = host_rng ? (size_t)n * sizeof(RadCopyRange) : 0;
|
||||
if (ss->stage_n < span + rb) {
|
||||
q.wait();
|
||||
if (ss->stage) sycl::free(ss->stage, q);
|
||||
@@ -741,10 +785,20 @@ int d_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadC
|
||||
ss->stage = (char*)sycl::malloc_device(ss->stage_n, q);
|
||||
if (!ss->stage) { ss->stage_n = 0; throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation), "range staging"); }
|
||||
}
|
||||
q.memcpy(ss->stage, s + lo, (size_t)(hi - lo));
|
||||
q.memcpy(ss->stage + span, ranges, rb);
|
||||
s = ss->stage - lo;
|
||||
ranges = (const RadCopyRange*)(ss->stage + span);
|
||||
auto up = [&](void* d, const void* from, size_t bytes) {
|
||||
if (is_pageable(from))
|
||||
bounced(q, d, from, bytes, [&](void* dd, const void* ff) { q.memcpy(dd, ff, bytes); });
|
||||
else
|
||||
q.memcpy(d, from, bytes);
|
||||
};
|
||||
if (host_src) {
|
||||
up(ss->stage, s + lo, (size_t)(hi - lo));
|
||||
s = ss->stage - lo;
|
||||
}
|
||||
if (host_rng) {
|
||||
up(ss->stage + span, ranges, rb);
|
||||
ranges = (const RadCopyRange*)(ss->stage + span);
|
||||
}
|
||||
}
|
||||
if (n > 0) {
|
||||
q.parallel_for(sycl::nd_range<1>{(size_t)n * kRangeWg, kRangeWg}, [=](sycl::nd_item<1> it) {
|
||||
@@ -772,85 +826,13 @@ int d_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadC
|
||||
|
||||
/* ------------------------------------------------------------------ gates
|
||||
*
|
||||
* A gate is a one-work-item kernel that spins until a flag in pinned host memory turns 1; the
|
||||
* in-order queue holds everything behind it. rad_gate_open sets the flag. The flag's memory is
|
||||
* freed only once the kernel has finished (reaped on later gate calls), since the kernel may still
|
||||
* be reading it when the host opens. A gate held for seconds can trip the driver's job timeout;
|
||||
* rad-kbench holds one for a batch. */
|
||||
struct SyclGate {
|
||||
uint32_t* flag = nullptr;
|
||||
SyclStream* s = nullptr;
|
||||
sycl::event spin;
|
||||
};
|
||||
* None (rad_stream_gate reports the backend has none; rad-kbench then times ungated). A gate's
|
||||
* spinning kernel needs a flag the host can flip while it runs: on the Arc Pro B65 it never sees a
|
||||
* store to host USM, and plain system memory, which it does see, goes through the driver's shared
|
||||
* virtual memory (see memory, above). A device flag written by a copy from another queue deadlocks
|
||||
* as soon as a copy is queued behind the gate (both share the copy engine), and a kernel on another
|
||||
* queue does not run beside the spinning one. */
|
||||
|
||||
std::mutex g_gate_m;
|
||||
std::vector<std::pair<sycl::event, uint32_t*>> g_gate_graveyard;
|
||||
|
||||
void gate_reap() {
|
||||
std::lock_guard<std::mutex> lk(g_gate_m);
|
||||
for (size_t i = 0; i < g_gate_graveyard.size();) {
|
||||
bool done = true;
|
||||
try {
|
||||
done = event_done(g_gate_graveyard[i].first);
|
||||
} catch (const sycl::exception&) {
|
||||
}
|
||||
if (!done) { ++i; continue; }
|
||||
d_free(g_gate_graveyard[i].second, RAD_MEM_HOST_PINNED);
|
||||
g_gate_graveyard[i] = g_gate_graveyard.back();
|
||||
g_gate_graveyard.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
int d_stream_gate(RadStream st, void** out) {
|
||||
RAD_TRY(check_stream(st, "rad_stream_gate"));
|
||||
if (!out) return RAD_E_INVAL;
|
||||
gate_reap();
|
||||
SyclStream* s = as_stream(st);
|
||||
auto* g = new (std::nothrow) SyclGate();
|
||||
uint32_t* flag = (uint32_t*)d_alloc(64, RAD_MEM_HOST_PINNED);
|
||||
if (!g || !flag) {
|
||||
delete g;
|
||||
if (flag) d_free(flag, RAD_MEM_HOST_PINNED);
|
||||
device_set_error("rad_stream_gate: out of memory");
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
*flag = 0;
|
||||
try {
|
||||
g->spin = s->q.single_task([=] {
|
||||
sycl::atomic_ref<uint32_t, sycl::memory_order::relaxed, sycl::memory_scope::system,
|
||||
sycl::access::address_space::global_space> f(*flag);
|
||||
while (f.load() == 0) {
|
||||
}
|
||||
sycl::atomic_fence(sycl::memory_order::acquire, sycl::memory_scope::system);
|
||||
});
|
||||
} catch (const sycl::exception& e) {
|
||||
delete g;
|
||||
d_free(flag, RAD_MEM_HOST_PINNED);
|
||||
return sycl_fail(e, "rad_stream_gate");
|
||||
}
|
||||
g->flag = flag;
|
||||
g->s = s;
|
||||
++s->gates;
|
||||
*out = g;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int d_gate_open(void* p) {
|
||||
auto* g = (SyclGate*)p;
|
||||
if (!g || !g->flag) return RAD_E_INVAL;
|
||||
std::atomic_ref<uint32_t>(*g->flag).store(1, std::memory_order_release);
|
||||
--g->s->gates;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_gate_m);
|
||||
g_gate_graveyard.emplace_back(g->spin, g->flag);
|
||||
}
|
||||
delete g;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* A Level Zero queue has no fixed depth, so nothing behind a gate waits for room; the figure only
|
||||
* bounds how far a caller runs ahead. */
|
||||
int64_t d_gate_room(RadStream st) { return st ? 4096 : 0; }
|
||||
|
||||
const DeviceBackend g_sycl = {
|
||||
.name = "sycl",
|
||||
@@ -874,7 +856,6 @@ const DeviceBackend g_sycl = {
|
||||
.pci_address = d_pci_address,
|
||||
.runtime_version = d_runtime_version,
|
||||
/* Expert prefetch: null. */
|
||||
.stream_gate = d_stream_gate, .gate_open = d_gate_open, .gate_room = d_gate_room,
|
||||
};
|
||||
|
||||
} /* namespace */
|
||||
|
||||
Reference in new issue
Block a user