> On 22 Nov 2026, at 09:50, Alvin Sun <[email protected]> wrote:
>
> Userspace needs multiple independent GPU address spaces per file,
> addressed by ID through the VM ioctls as in panthor. Store them in an
Can you please capitalize Panthor?
> IdPool (capped at 32 for panthor parity) plus an XArray. On failure
> the caller receives the VM for teardown through the returned error,
> whether the un-stored clone or the original on an ID conflict, so an
Perhaps “not yet stored”? Although a native English speaker is
probably in a better position to judge this.
> aborted creation never leaks a VM. The pool kills every VM still
> stored when the file closes.
>
> Signed-off-by: Alvin Sun <[email protected]>
> ---
> drivers/gpu/drm/tyr/vm.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 144 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
> index 1dd11b2b12532..e67a6efcf451b 100644
> --- a/drivers/gpu/drm/tyr/vm.rs
> +++ b/drivers/gpu/drm/tyr/vm.rs
> @@ -11,6 +11,7 @@
> use core::ops::Range;
>
> use kernel::{
> + alloc::AllocError,
> device::{
> Bound,
> Device, //
> @@ -33,6 +34,7 @@
> }, //
> },
> fmt,
> + id_pool::IdPool,
> impl_flags,
> io::PhysAddr,
> iommu::pgtable::{
> @@ -41,6 +43,7 @@
> ARM64LPAES1, //
> },
> new_mutex,
> + num::casts::FromSafeCast,
> prelude::*,
> sizes::{
> SZ_1G,
> @@ -53,7 +56,11 @@
> ArcBorrow,
> Mutex, //
> },
> - uapi, //
> + uapi,
> + xarray::{
> + AllocKind,
> + XArray, //
> + }, //
> };
>
> use crate::{
> @@ -947,3 +954,139 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_,
> ARM64LPAES1>, range: Range<u64>)
>
> Ok(())
> }
> +
> +/// Maximum number of VMs a single file may hold, matching panthor's
> +/// `PANTHOR_MAX_VMS_PER_FILE`.
> +const MAX_VMS_PER_FILE: usize = 32;
> +
> +/// Per-open-file pool of VMs.
> +#[pin_data(PinnedDrop)]
> +pub(crate) struct VmPool<'drm> {
> + /// Parent device, for error reporting.
> + dev: &'drm Device<Bound>,
> + #[pin]
> + ids: Mutex<IdPool>,
> + #[pin]
> + vms: XArray<Arc<Vm<'drm>>>,
> +}
> +
> +/// Error returned when a VM cannot be added to the pool. The caller
> +/// owns the teardown of the VM in [`AddError::vm`].
> +pub(crate) struct AddError<'drm> {
> + #[expect(dead_code)]
> + pub(crate) error: Error,
> + #[expect(dead_code)]
> + pub(crate) vm: Arc<Vm<'drm>>,
> +}
> +
> +impl<'drm> VmPool<'drm> {
> + /// Creates a new [`VmPool`].
> + ///
> + /// The pool is sized for [`MAX_VMS_PER_FILE`] IDs on all architectures,
> + /// including 32-bit builds where `IdPool::new()` would only hold 31 IDs
> + /// because `find_unused_id(1)` skips bit 0 of an inline bitmap.
> + #[expect(dead_code)]
> + pub(crate) fn new(dev: &'drm Device<Bound>) -> Result<impl
> PinInit<Self>, AllocError> {
> + let ids = IdPool::with_capacity(MAX_VMS_PER_FILE + 1, GFP_KERNEL)?;
> + Ok(pin_init!(Self {
> + dev,
> + ids <- new_mutex!(ids),
> + vms <- XArray::new(AllocKind::Alloc),
> + }))
> + }
> +
> + /// Stores the VM and returns the allocated ID.
> + ///
> + /// On failure - ID space exhausted or a store failure - the VM is
> + /// returned in an [`AddError`] so the caller owns the teardown.
> + // TODO: allocate IDs with the XArray directly (once it grows range
> + // allocation, the equivalent of C's `XA_LIMIT`) and drop the IdPool.
> + #[expect(dead_code)]
> + pub(crate) fn add(&self, vm: Arc<Vm<'drm>>) -> Result<u32,
> AddError<'drm>> {
> + let id = (|| {
> + let mut ids = self.ids.lock();
> + let unused = ids.find_unused_id(1).ok_or(ENOSPC)?;
> + if unused.as_usize() > MAX_VMS_PER_FILE {
> + return Err(ENOSPC);
> + }
> + Ok(unused.acquire())
> + })();
> + let id = match id {
> + Ok(id) => id,
> + Err(error) => return Err(AddError { error, vm }),
> + };
> +
> + let mut vms = self.vms.lock();
> + match vms.store(id, vm.clone(), GFP_KERNEL) {
> + Ok(prev_vm) => {
> + if let Some(prev_vm) = prev_vm {
> + // An existing VM at the freshly allocated slot means the
> + // IdPool and the XArray are out of sync: the stored VM
> is
> + // a pool entry, so the pool kills it and the caller's VM
> + // is returned for teardown.
> + drop(vms.remove(id));
> + drop(vms);
> + prev_vm.kill();
> + self.ids.lock().release_id(id);
> + dev_err!(
> + self.dev,
VMs already have a dev() accessor. For the time being
I don’t think we need to store dev here too.
> + "IdPool/XArray out of sync at VM pool id {id}
> (stored VM conflict)"
Missing newline here. Also, could you please improve this message? Someone
seeing this on dmesg has no idea what is going on. How about:
"VM id {id} marked free but still in use\n"
?
> + );
> + return Err(AddError { error: EBUSY, vm });
> + }
> + Ok(id as u32)
Please remove this cast. Use as_u32() instead.
> + }
> + Err(err) => {
> + // Drop the XArray spinlock before acquiring the `ids` mutex.
> + drop(vms);
> + // Release the pooled id; the VM never entered the pool, so
> it
> + // is returned to the caller for teardown.
> + self.ids.lock().release_id(id);
> + Err(AddError {
> + error: err.error,
> + vm: err.value,
> + })
> + }
> + }
> + }
> +
> + /// Removes and kills the VM with the given ID.
> + #[expect(dead_code)]
> + pub(crate) fn remove(&self, id: u32) -> Result<()> {
Result<()> -> Result
> + let mut vms = self.vms.lock();
> + let vm = match vms.remove(usize::from_safe_cast(id)) {
> + Some(vm) => vm,
> + None => return Err(EINVAL),
> + };
> + // Drop the XArray spinlock before killing, since `kill()` may sleep.
> + drop(vms);
> + // Kill the VM before handing the id back to the pool, so a VM is
> + // never reusable while still alive.
> + vm.kill();
> + self.ids.lock().release_id(usize::from_safe_cast(id));
> + Ok(())
> + }
> +
> + /// Gets a shared reference to the VM with the given ID.
> + #[expect(dead_code)]
> + pub(crate) fn get(&self, id: u32) -> Option<Arc<Vm<'drm>>> {
> + let vms = self.vms.lock();
> + let borrow = vms.get(usize::from_safe_cast(id))?;
> + Some(Arc::from(borrow))
> + }
> +}
> +
> +#[pinned_drop]
> +impl PinnedDrop for VmPool<'_> {
> + fn drop(self: Pin<&mut Self>) {
> + // Kill every VM left in the pool. The ID range is bounded by
> + // `MAX_VMS_PER_FILE`, so this loop is cheap and runs at file close.
> + for id in 1..=MAX_VMS_PER_FILE {
> + // Release the XArray lock guard before killing: `kill()` may
> sleep.
> + let vm = self.vms.lock().remove(id);
> + if let Some(vm) = vm {
> + vm.kill();
> + }
> + }
> + }
> +}
>
> --
> 2.43.0
>
>
With the fixes above + the fixes Deborah asked for:
Reviewed-by: Daniel Almeida <[email protected]>