From 3ed11a43663eb0f110901c4773e6234104aaae24 Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Mon, 2 Nov 2026 05:13:36 +0000 Subject: [PATCH] Wait for copies and fills before freeing a UsmBox A UsmBox could be freed while a copy or fill that uses it was still running, which breaks the precondition of sycl::free that no enqueued or in-progress command uses the memory. Safe code could do this by dropping an allocation before its zero fill finished, or by dropping the source or destination of a copy before the copy finished. Have each UsmBox record the events of the copies and fills that use it, and wait for them before freeing the memory. If waiting fails, the memory is leaked instead, since a command may still be using it. Signed-off-by: Zhiwei Liang --- CHANGELOG.md | 2 ++ sycl/sycl-rs/src/lib.rs | 5 +++-- sycl/sycl-rs/src/queue.rs | 14 ++++++++++---- sycl/sycl-rs/src/usmbox.rs | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 446be5c..d53b4d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ can be found at: - Initial Rust API for discovering SYCL platforms and devices. - Queues, events, contexts, USM allocations, and host/device memory operations. +- Dropping a `UsmBox` waits for the copies and fills that use it, so its + memory is not freed while a device may still use it. - Runtime kernel bundle compilation and one-, two-, and three-dimensional kernel launch support. - Derive support for typed kernel argument lists. diff --git a/sycl/sycl-rs/src/lib.rs b/sycl/sycl-rs/src/lib.rs index 0073685..bffcd59 100644 --- a/sycl/sycl-rs/src/lib.rs +++ b/sycl/sycl-rs/src/lib.rs @@ -90,9 +90,10 @@ //! ``` //! //! # Safety model -//! - USM allocations are represented by a zero-cost [`UsmBox`](crate::usmbox::UsmBox) type managed -//! through RAII. +//! - USM allocations are represented by a [`UsmBox`](crate::usmbox::UsmBox) type managed through +//! RAII. //! - Note: `UsmBox` arrays do not rely on accessors, unlike SYCL buffers. +//! - Dropping a `UsmBox` waits for the copies and fills that use it to complete. //! - `UsmBox`es are zero-initialized by default. //! - `UsmBox`es can only store types that implement [`bytemuck::Pod`]. //! - Kernel launch is inherently unsafe. In particular, the caller must ensure that every argument diff --git a/sycl/sycl-rs/src/queue.rs b/sycl/sycl-rs/src/queue.rs index d0814e4..2ff6f07 100644 --- a/sycl/sycl-rs/src/queue.rs +++ b/sycl/sycl-rs/src/queue.rs @@ -140,7 +140,10 @@ impl Queue { ptr: (*e).clone().0, }) .collect::>(); - unsafe { ffi::memset(&mut self.0, ptr, value, num_bytes, dep_events) }.map(Into::into) + let event: Event = + unsafe { ffi::memset(&mut self.0, ptr, value, num_bytes, dep_events) }?.into(); + array.add_pending(&event); + Ok(event) } /// Submits a barrier to the queue. @@ -228,7 +231,7 @@ impl Queue { .collect::>(); let num_bytes = src.get_len() * size_of::(); - unsafe { + let event: Event = unsafe { ffi::memcpy( &mut self.0, dst.get_byte_ptr(), @@ -236,8 +239,11 @@ impl Queue { num_bytes, dep_events, ) - } - .map(Into::into) + }? + .into(); + src.add_pending(&event); + dst.add_pending(&event); + Ok(event) } } diff --git a/sycl/sycl-rs/src/usmbox.rs b/sycl/sycl-rs/src/usmbox.rs index b9ba1ab..b41a792 100644 --- a/sycl/sycl-rs/src/usmbox.rs +++ b/sycl/sycl-rs/src/usmbox.rs @@ -8,6 +8,7 @@ use std::{ alloc::{Layout, handle_alloc_error}, + cell::RefCell, ops::{Deref, DerefMut}, pin::Pin, ptr::NonNull, @@ -21,6 +22,7 @@ use pin_project::pin_project; use crate::{ Result, event::{Event, EventFuture}, + info::{self, InfoTarget}, kernel::KernelArgument, usm::{ DeviceAllocator, HostAccessible, HostAllocator, SharedAllocator, UsmAlloc, UsmAllocator, @@ -39,11 +41,16 @@ use crate::{ /// The `UsmBox` struct takes a generic parameter [`UsmAlloc`](`crate::usm::UsmAlloc`) for /// specifying an allocator which is used by the SYCL runtime when allocating temporary memory on /// the host. +/// +/// A `UsmBox` keeps track of the copies and fills that use it, and dropping it waits for them to +/// complete, so its memory is not freed while a device may still use it. pub struct UsmBox { data: NonNull, len: usize, layout: Layout, allocator: A, + /// Events of the enqueued commands that may still use the memory. + pending: RefCell>, } impl UsmBox { @@ -61,7 +68,25 @@ impl UsmBox { len, layout, allocator, + pending: RefCell::default(), + } + } + + /// Records an enqueued command that uses the memory, so that dropping the box waits for it. + pub(crate) fn add_pending(&self, event: &Event) { + let mut pending = self.pending.borrow_mut(); + // Forget the completed commands only once the list is full, and leave room for as many + // new ones as are still pending, so that recording a command takes amortized constant + // time even when many are in flight. + if pending.len() == pending.capacity() { + pending.retain(|command| { + command.get_info::() + != info::EventCommandStatus::Complete + }); + let still_pending = pending.len(); + pending.reserve(still_pending); } + pending.push(event.clone()); } pub(crate) fn get_byte_ptr(&self) -> *mut u8 { @@ -96,10 +121,15 @@ impl DerefMut for UsmBox { } } +/// Waits for the copies and fills that use the `UsmBox`, then frees its memory. If waiting fails, +/// the memory is leaked, since a command may still be using it. impl Drop for UsmBox { fn drop(&mut self) { - unsafe { - self.allocator.deallocate(self.data.cast(), self.layout); + let waited = self.pending.get_mut().iter_mut().try_for_each(Event::wait); + if waited.is_ok() { + unsafe { + self.allocator.deallocate(self.data.cast(), self.layout); + } } } } @@ -124,7 +154,7 @@ impl EnqueuedUsmBox { /// Performs a blocking wait for the [`UsmBox`] initialization to complete. Returns an error if /// a synchronous SYCL exception occurs. /// - /// Dropping an enqueued array does not wait for its completion. + /// Dropping an enqueued array waits for its initialization to complete, then frees it. pub fn wait(mut self) -> Result> { self.event.wait().map(|_| self.array) }