diff --git a/core/device/directio.cpp b/core/device/directio.cpp index ae936828..d164c02d 100644 --- a/core/device/directio.cpp +++ b/core/device/directio.cpp @@ -15,8 +15,13 @@ namespace rad { namespace { -/* Bounce buffer size; allocated on first misaligned use only. */ -constexpr int64_t kBounceBytes = 1 << 20; +/* Bounce buffer size; allocated on first use only. Large enough that a bounced extent (see + * EFAULT below) streams at close to the drive's rate. */ +constexpr int64_t kBounceBytes = 8 << 20; + +/* errno of the last failed transfer on this thread, so a caller can tell EFAULT from other errors + * after the RAD_E_IO the helpers return. */ +thread_local int t_errno = 0; int64_t align_down(int64_t v, int64_t a) { return v / a * a; } @@ -27,6 +32,7 @@ int64_t pread_some(int fd, void* dst, int64_t bytes, int64_t off) { ssize_t r = ::pread(fd, (uint8_t*)dst + done, (size_t)(bytes - done), (off_t)(off + done)); if (r < 0) { if (errno == EINTR) continue; + t_errno = errno; return RAD_E_IO; } if (r == 0) break; @@ -52,6 +58,7 @@ int pwrite_exact(int fd, const void* src, int64_t bytes, int64_t off) { (off_t)(off + done)); if (w <= 0) { if (w < 0 && errno == EINTR) continue; + t_errno = w < 0 ? errno : EIO; return RAD_E_IO; } done += w; @@ -137,7 +144,15 @@ int DirectFile::read_at(void* dst, int64_t off, int64_t bytes) { /* Fast path: one pread straight into the caller's pinned buffer. Every .rad extent lands here * (movement units and rad_dev_alloc pools are both RAD_ALIGN_UNIT-aligned). */ - if (aligned) return pread_exact(fd_, dst, bytes, off); + if (aligned) { + t_errno = 0; + const int rc = pread_exact(fd_, dst, bytes, off); + /* EFAULT: memory the kernel cannot pin for the transfer, such as pinned memory a GPU + * driver maps itself (SYCL malloc_host, core/device/sycl.cpp). Read it through the bounce, + * whole chunks at a time. */ + if (rc == RAD_E_IO && t_errno == EFAULT) return bounce_read(dst, off, bytes); + return rc; + } /* O_DIRECT rejects unaligned transfers with EINVAL, so read an aligned superset through the * bounce and copy out. This serves file tails and header/footer reads, never the mover; a @@ -161,6 +176,17 @@ int DirectFile::read_at(void* dst, int64_t off, int64_t bytes) { return RAD_OK; } +/* An aligned extent read through the bounce: the destination is memory O_DIRECT cannot target. */ +int DirectFile::bounce_read(void* dst, int64_t off, int64_t bytes) { + RAD_TRY(bounce_ready()); + for (int64_t done = 0; done < bytes; done += bounce_bytes_) { + const int64_t span = std::min(bounce_bytes_, bytes - done); + RAD_TRY(pread_exact(fd_, bounce_, span, off + done)); + std::memcpy((uint8_t*)dst + done, bounce_, (size_t)span); + } + return RAD_OK; +} + /* ------------------------------------------------------------------ write */ /* Aligned writes go straight through; anything else is a read-modify-write of whole blocks. */ @@ -172,7 +198,20 @@ int DirectFile::write_at(const void* src, int64_t off, int64_t bytes) { const int64_t A = alignment(); const bool aligned = (off % A) == 0 && (bytes % A) == 0 && ((uintptr_t)src % (uintptr_t)A) == 0; - if (aligned) return pwrite_exact(fd_, src, bytes, off); + if (aligned) { + t_errno = 0; + const int rc = pwrite_exact(fd_, src, bytes, off); + if (rc == RAD_E_IO && t_errno == EFAULT) { /* as in read_at */ + RAD_TRY(bounce_ready()); + for (int64_t done = 0; done < bytes; done += bounce_bytes_) { + const int64_t span = std::min(bounce_bytes_, bytes - done); + std::memcpy(bounce_, (const uint8_t*)src + done, (size_t)span); + RAD_TRY(pwrite_exact(fd_, bounce_, span, off + done)); + } + return RAD_OK; + } + return rc; + } RAD_TRY(bounce_ready()); const int64_t old_size = size(); diff --git a/core/device/directio.h b/core/device/directio.h index 5b363e9e..e419ceb7 100644 --- a/core/device/directio.h +++ b/core/device/directio.h @@ -8,7 +8,9 @@ * * O_DIRECT requires the file offset, transfer length and buffer address to be block-aligned. * rad_dev_alloc memory is (host.cpp) and .rad pads movement units to RAD_ALIGN_UNIT - * (rad_format.h), so every mover read takes the unbounced path. The bounce is for other callers. + * (rad_format.h), so every mover read takes the unbounced path. The bounce is for other callers, + * and for pinned memory O_DIRECT cannot target (EFAULT: a GPU driver's own mapping, such as the + * SYCL backend's malloc_host), which is bounced whole. */ #pragma once #include @@ -63,6 +65,7 @@ public: private: int bounce_ready(); + int bounce_read(void* dst, int64_t off, int64_t bytes); int fd_ = -1; bool direct_ = false; diff --git a/core/device/sycl.cpp b/core/device/sycl.cpp index 9b8ae615..1c227aa6 100644 --- a/core/device/sycl.cpp +++ b/core/device/sycl.cpp @@ -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 g_pinned; /* base -> bytes */ - -bool in_pinned(const void* p) { - const uintptr_t a = (uintptr_t)p; - std::lock_guard 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 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 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 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 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 +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(lo, ranges[i].src); hi = std::max(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> g_gate_graveyard; - -void gate_reap() { - std::lock_guard 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 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(*g->flag).store(1, std::memory_order_release); - --g->s->gates; - { - std::lock_guard 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 */ diff --git a/libxe2/xe2_elementwise.cpp b/libxe2/xe2_elementwise.cpp index 7b28543a..a98f01ce 100644 --- a/libxe2/xe2_elementwise.cpp +++ b/libxe2/xe2_elementwise.cpp @@ -146,7 +146,9 @@ int scatter_rows(const RadArgs* a, RadStream s) { if (!q) return RAD_E_DEVICE; /* libref refuses an index past x's rows before writing anything; the indices live on the card, * so a device flag records one and the call reports it after the copy is skipped. */ - int* bad = sycl::malloc_shared(1, *q); + /* USM host memory, not shared: shared USM may live in the driver's shared virtual memory, + * whose page migration the backend avoids (core/device/sycl.cpp, memory). */ + int* bad = sycl::malloc_host(1, *q); if (!bad) return refuse("scatter_rows: no flag memory"); *bad = 0; int rc = submit([&] { diff --git a/tests/device_test.cpp b/tests/device_test.cpp index 78ea0216..38d499eb 100644 --- a/tests/device_test.cpp +++ b/tests/device_test.cpp @@ -1414,6 +1414,7 @@ TEST(a_host_sync_on_an_event_not_made_for_it_is_refused) { * copy land well inside 20 ms. */ TEST(nothing_behind_a_closed_gate_runs_until_it_opens) { Stream st; + if (rad_gate_room(st.s) <= 0) SKIP("no gates on this backend"); DevBuf src(4096, RAD_MEM_HOST_PINNED); DevBuf dst(4096, RAD_MEM_HOST_PINNED); REQUIRE(src && dst); @@ -1494,6 +1495,7 @@ TEST(a_dma_lists_mark_stays_out_of_the_pool_until_the_engine_has_read_it) { * is refused with RAD_E_STATE instead of hanging, and works again once the gate is open. */ TEST(a_host_wait_on_a_gated_stream_is_refused_not_a_deadlock) { Stream st; + if (rad_gate_room(st.s) <= 0) SKIP("no gates on this backend"); void* gate = nullptr; REQUIRE_EQ(rad_stream_gate(st.s, &gate), RAD_OK); CHECK_EQ(rad_stream_sync(st.s), RAD_E_STATE); @@ -1538,6 +1540,7 @@ TEST(a_full_gated_queue_refuses_the_next_submission_instead_of_waiting) { * with one it is not. Timing is the behaviour here, and the margins are an order of magnitude. */ TEST(events_behind_a_gate_leave_the_hosts_submission_time_out) { Stream st; + if (rad_gate_room(st.s) <= 0) SKIP("no gates on this backend"); DevBuf a(4096, RAD_MEM_DEVICE); REQUIRE(a); Event e0, e1;