All of lore.kernel.org
 help / color / mirror / Atom feed
From: Deborah Brouwer <deborah.brouwer@collabora.com>
To: Alvin Sun <alvin.sun@linux.dev>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Lyude Paul" <lyude@redhat.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Johan Hovold" <johan@kernel.org>,
	"Sami Tolvanen" <samitolvanen@google.com>,
	rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org,
	driver-core@lists.linux.dev, "Ke Sun" <sunke@kylinos.cn>
Subject: Re: [PATCH v7 07/10] drm/tyr: add VM-related ioctls
Date: Tue, 24 Nov 2026 16:40:55 -0700	[thread overview]
Message-ID: <awYgh-A8JtN899eB@um790> (raw)
In-Reply-To: <20261122-tyr-ioctls-v7-7-3a2af624b992@linux.dev>

On Sun, Nov 22, 2026 at 08:50:26PM +0800, Alvin Sun wrote:
> Implement VM_CREATE, VM_DESTROY, VM_BIND and VM_GET_STATE. VM_CREATE
> hands the new VM to the per-file pool and, on insertion failure, the
> caller tears it down, so an aborted creation never leaks a VM.
> VM_DESTROY removes the VM from the pool and kills it, even if in-flight
> jobs still hold references (matching panthor: such jobs are expected
> to fault). VM_BIND runs synchronously for now.
> 
> Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
> ---
>  drivers/gpu/drm/tyr/driver.rs |  10 +-
>  drivers/gpu/drm/tyr/file.rs   | 295 ++++++++++++++++++++++++++++++++++++++++--
>  drivers/gpu/drm/tyr/gem.rs    |   1 -
>  drivers/gpu/drm/tyr/vm.rs     |  90 ++++++++++---
>  4 files changed, 369 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs
> index 46caf7b336d93..ba03cc4c52539 100644
> --- a/drivers/gpu/drm/tyr/driver.rs
> +++ b/drivers/gpu/drm/tyr/driver.rs
> @@ -73,6 +73,9 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
>      /// Firmware sections.
>      pub(crate) fw: Firmware<'drm>,
>  
> +    /// Memory management unit for address space slots.
> +    pub(crate) mmu: Arc<Mmu<'drm>>,
> +
>      #[pin]
>      clks: Mutex<Clocks>,
>  
> @@ -171,6 +174,7 @@ fn probe<'bound>(
>                      pin_init!(TyrDrmRegistrationData {
>                          pdev,
>                          fw: firmware,
> +                        mmu,
>                          clks <- new_mutex!(Clocks {
>                              core: core_clk,
>                              stacks: stacks_clk,
> @@ -209,7 +213,7 @@ fn drop(self: Pin<&mut Self>) {}
>  impl drm::Driver for TyrDrmDriver {
>      type Data = ();
>      type RegistrationData<'drm> = TyrDrmRegistrationData<'drm>;
> -    type File<'a> = TyrDrmFileData;
> +    type File<'a> = TyrDrmFileData<'a>;
>      type Object = Bo;
>      type ParentDevice<Ctx: DeviceContext> = platform::Device<Ctx>;
>  
> @@ -218,6 +222,10 @@ impl drm::Driver for TyrDrmDriver {
>  
>      kernel::declare_drm_ioctls! {
>          (PANTHOR_DEV_QUERY, drm_panthor_dev_query, ioctl::RENDER_ALLOW, TyrDrmFileData::dev_query),
> +        (PANTHOR_VM_CREATE, drm_panthor_vm_create, ioctl::RENDER_ALLOW, TyrDrmFileData::vm_create),
> +        (PANTHOR_VM_DESTROY, drm_panthor_vm_destroy, ioctl::RENDER_ALLOW, TyrDrmFileData::vm_destroy),
> +        (PANTHOR_VM_BIND, drm_panthor_vm_bind, ioctl::RENDER_ALLOW, TyrDrmFileData::vm_bind),
> +        (PANTHOR_VM_GET_STATE, drm_panthor_vm_get_state, ioctl::RENDER_ALLOW, TyrDrmFileData::vm_get_state),
>      }
>  }
>  
> diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs
> index 933a365cb016e..901c0fb951adf 100644
> --- a/drivers/gpu/drm/tyr/file.rs
> +++ b/drivers/gpu/drm/tyr/file.rs
> @@ -3,37 +3,58 @@
>  use kernel::{
>      drm::{
>          self,
> +        gem::BaseObject,
>          Registered, //
>      },
> +    num::casts::FromSafeCast,
>      prelude::*,
> +    sizes::SizeConstants,
> +    transmute::FromBytes,
>      uaccess::UserSlice,
>      uapi, //
>  };
>  
> -use crate::driver::{
> -    TyrDrmDevice,
> -    TyrDrmDriver,
> -    TyrDrmRegistrationData, //
> +use crate::{
> +    driver::{
> +        TyrDrmDevice,
> +        TyrDrmDriver,
> +        TyrDrmRegistrationData, //
> +    },
> +    vm::{
> +        UserVaRequest,
> +        Vm,
> +        VmBindOpType,
> +        VmMapFlags,
> +        VmPool, //
> +    }, //
>  };
>  
>  #[pin_data]
> -pub(crate) struct TyrDrmFileData {}
> +pub(crate) struct TyrDrmFileData<'a> {
> +    reg: &'a TyrDrmRegistrationData<'a>,
> +
> +    #[pin]
> +    vm_pool: VmPool<'a>,
> +}
>  
>  /// Convenience type alias for our DRM `File` type.
>  pub(crate) type TyrDrmFile = drm::file::File<TyrDrmDriver>;
>  
> -impl drm::file::DriverFile<'_> for TyrDrmFileData {
> +impl<'a> drm::file::DriverFile<'a> for TyrDrmFileData<'a> {
>      type Driver = TyrDrmDriver;
>  
>      fn open(
>          _device: &TyrDrmDevice<Registered>,
> -        _reg_data: &TyrDrmRegistrationData<'_>,
> +        reg_data: &'a TyrDrmRegistrationData<'a>,
>      ) -> impl PinInit<Self, Error> {
> -        Ok(Self {})
> +        try_pin_init!(Self {
> +            reg: reg_data,
> +            vm_pool <- VmPool::new(reg_data.pdev.as_ref())?,
> +        })
>      }
>  }
>  
> -impl TyrDrmFileData {
> +impl TyrDrmFileData<'_> {
>      pub(crate) fn dev_query(
>          _ddev: &TyrDrmDevice<Registered>,
>          reg_data: &TyrDrmRegistrationData<'_>,
> @@ -65,4 +86,260 @@ pub(crate) fn dev_query(
>              }
>          }
>      }
> +
> +    pub(crate) fn vm_create(
> +        ddev: &TyrDrmDevice<Registered>,
> +        _reg_data: &TyrDrmRegistrationData<'_>,
> +        vmcreate: &mut uapi::drm_panthor_vm_create,
> +        file: &TyrDrmFile,
> +    ) -> Result<u32> {
> +        if vmcreate.flags != 0 {
> +            dev_dbg!(
> +                ddev.as_ref(),
> +                "Invalid VM create flags: {:#x}\n",
> +                vmcreate.flags
> +            );
> +            return Err(EINVAL);
> +        }
> +
> +        let ret: Result<u32, Error> = file.inner_with(|pfile| {
> +            let vm = Vm::new_for_user(
> +                pfile.reg.pdev.as_ref(),
> +                ddev,
> +                pfile.reg.mmu.as_arc_borrow(),
> +                &pfile.reg.gpu_info,
> +                UserVaRequest::from_uapi(vmcreate.user_va_range),
> +            )?;
> +            let user_va_range = vm.layout.user.end;
> +            let id = pfile.vm_pool.add(vm).map_err(|e| {
> +                e.vm.kill();
> +                e.error
> +            })?;
> +            vmcreate.user_va_range = user_va_range;
> +            vmcreate.id = id;
> +
> +            Ok(0)
> +        });
> +        ret
> +    }
> +
> +    pub(crate) fn vm_destroy(
> +        ddev: &TyrDrmDevice<Registered>,
> +        _reg_data: &TyrDrmRegistrationData<'_>,
> +        vmdestroy: &mut uapi::drm_panthor_vm_destroy,
> +        file: &TyrDrmFile,
> +    ) -> Result<u32> {
> +        if vmdestroy.pad != 0 {
> +            dev_dbg!(
> +                ddev.as_ref(),
> +                "Invalid VM destroy pad: {:#x}\n",
> +                vmdestroy.pad
> +            );
> +            return Err(EINVAL);
> +        }
> +
> +        let ret: Result<u32, Error> = file.inner_with(|pfile| {
> +            pfile.vm_pool.remove(vmdestroy.id)?;
> +            Ok(0)
> +        });
> +        ret
> +    }
> +
> +    pub(crate) fn vm_bind(
> +        ddev: &TyrDrmDevice<Registered>,
> +        _reg_data: &TyrDrmRegistrationData<'_>,
> +        vmbind: &mut uapi::drm_panthor_vm_bind,
> +        file: &TyrDrmFile,
> +    ) -> Result<u32> {
> +        let async_flag = uapi::drm_panthor_vm_bind_flags_DRM_PANTHOR_VM_BIND_ASYNC;
> +
> +        if vmbind.flags & !async_flag != 0 {
> +            dev_dbg!(
> +                ddev.as_ref(),
> +                "Invalid VM_BIND flags: {:#x}\n",
> +                vmbind.flags
> +            );
> +            return Err(EINVAL);
> +        }
> +
> +        if vmbind.flags & async_flag != 0 {
> +            dev_dbg!(ddev.as_ref(), "Async VM_BIND not supported\n");
> +            return Err(EOPNOTSUPP);
> +        }
> +
> +        let count = usize::from_safe_cast(vmbind.ops.count);
> +        if count == 0 {
> +            return file.inner_with(|pfile| {
> +                pfile.vm_pool.get(vmbind.vm_id).ok_or(EINVAL)?;
> +                Ok(0)
> +            });
> +        }
> +
> +        // Panthor allows larger strides; UserSliceReader::read needs an exact one.
> +        if vmbind.ops.stride as usize != size_of::<VmBindOp>() {
> +            dev_dbg!(
> +                ddev.as_ref(),
> +                "Invalid VM_BIND op stride {} (expected {})\n",
> +                vmbind.ops.stride,
> +                size_of::<VmBindOp>()
> +            );
> +            return Err(EINVAL);
> +        }
> +        // TODO: support variable strides with copy_struct_from_user()
> +        // semantics: zero-pad shorter structs, reject non-zero trailing bytes.
> +
> +        let total_len = size_of::<VmBindOp>().checked_mul(count).ok_or_else(|| {
> +            dev_dbg!(ddev.as_ref(), "VM_BIND ops length overflow\n");
> +            EINVAL
> +        })?;
> +        let mut reader =
> +            UserSlice::new(UserPtr::from_addr(vmbind.ops.array as usize), total_len).reader();
> +        let mut ops = KVec::new();

Hi - fyi I was able to reproduce sashiko's concern that if we have a
vmbind.ops.count >= 65537 then this does trigger a warning for
___kmalloc_large_node for exceeding KMALLOC_MAX_SIZE which for me at
least is configured to 4MiB by default.

Using KVmalloc does solve the problem and it's also what panthor does:
  KVVec::with_capacity(count, GFP_KERNEL)?;

> +        for _ in 0..count {
> +            ops.push(reader.read::<VmBindOp>()?, GFP_KERNEL)?;
> +        }
> +
> +        let ret: Result<u32, Error> = file.inner_with(|pfile| {
> +            let vm = pfile.vm_pool.get(vmbind.vm_id).ok_or_else(|| {
> +                dev_dbg!(ddev.as_ref(), "Invalid VM_BIND vm_id: {}\n", vmbind.vm_id);
> +                EINVAL
> +            })?;
> +
> +            for (i, op) in ops.iter().enumerate() {
> +                if let Err(e) = vm_bind_exec_op(&vm, file, op) {
> +                    dev_dbg!(ddev.as_ref(), "VM_BIND op {} failed: {:?}\n", i, e);
> +                    vmbind.ops.count = i as u32;
> +                    return Err(e);
> +                }
> +            }
> +
> +            Ok(0)
> +        });
> +        ret
> +    }
> +
> +    pub(crate) fn vm_get_state(
> +        ddev: &TyrDrmDevice<Registered>,
> +        _reg_data: &TyrDrmRegistrationData<'_>,
> +        vmgetstate: &mut uapi::drm_panthor_vm_get_state,
> +        file: &TyrDrmFile,
> +    ) -> Result<u32> {
> +        file.inner_with(|pfile| {
> +            let vm = pfile.vm_pool.get(vmgetstate.vm_id).ok_or_else(|| {
> +                dev_dbg!(
> +                    ddev.as_ref(),
> +                    "Invalid VM_GET_STATE vm_id: {}\n",
> +                    vmgetstate.vm_id
> +                );
> +                EINVAL
> +            })?;
> +            vmgetstate.state = if vm.is_unusable() {
> +                uapi::drm_panthor_vm_state_DRM_PANTHOR_VM_STATE_UNUSABLE
> +            } else {
> +                uapi::drm_panthor_vm_state_DRM_PANTHOR_VM_STATE_USABLE
> +            };
> +            Ok(0)
> +        })
> +    }
> +}
> +
> +fn vm_bind_exec_op(vm: &Vm<'_>, file: &TyrDrmFile, op: &VmBindOp) -> Result {
> +    if op.syncs.count != 0 {
> +        dev_dbg!(vm.dev(), "VM_BIND op syncs not supported\n");
> +        return Err(EINVAL);
> +    }
> +
> +    if op.size == 0 {
> +        return Ok(());
> +    }
> +
> +    let end = match op.va.checked_add(op.size) {
> +        Some(end) => end,
> +        None => {
> +            dev_dbg!(vm.dev(), "VM_BIND op VA range overflow\n");
> +            return Err(EINVAL);
> +        }
> +    };
> +    if op.va < vm.layout.user.start || end > vm.layout.user.end {
> +        dev_dbg!(
> +            vm.dev(),
> +            "VM_BIND op VA range {:#x}..{:#x} outside user range\n",
> +            op.va,
> +            end
> +        );
> +        return Err(EINVAL);
> +    }
> +
> +    if (op.va | op.size | op.bo_offset) & (u64::SZ_4K - 1) != 0 {
> +        dev_dbg!(vm.dev(), "VM_BIND op not GPU-page-aligned\n");
> +        return Err(EINVAL);
> +    }
> +
> +    match VmBindOpType::try_from(op.flags) {
> +        Ok(VmBindOpType::Map) => {
> +            // Once the VM is unusable only MAP ops are rejected; UNMAP
> +            // stays available for cleanup (see the UAPI docs).
> +            if vm.is_unusable() {
> +                dev_dbg!(vm.dev(), "VM_BIND map op on unusable VM\n");
> +                return Err(EINVAL);
> +            }
> +
> +            let map_flags = match VmMapFlags::try_from(op.flags & !VmBindOpType::MASK) {
> +                Ok(flags) => flags,
> +                Err(_) => {
> +                    dev_dbg!(vm.dev(), "VM_BIND op invalid map flags {:#x}\n", op.flags);
> +                    return Err(EINVAL);
> +                }
> +            };
> +            let bo = crate::gem::lookup_handle(file, op.bo_handle).map_err(|_| {
> +                dev_dbg!(vm.dev(), "VM_BIND op invalid BO handle {}\n", op.bo_handle);
> +                EINVAL
> +            })?;
> +            // Validate the BO window before mapping.
> +            let bo_size = u64::from_safe_cast(bo.size());
> +            if op.size > bo_size || op.bo_offset > bo_size - op.size {
> +                dev_dbg!(vm.dev(), "VM_BIND op BO range out of bounds\n");
> +                return Err(EINVAL);
> +            }
> +            vm.map_bo_range(&bo, op.bo_offset, op.size, op.va, map_flags)
> +        }
> +        Ok(VmBindOpType::Unmap) => {
> +            // Unmap must not carry map-specific flags or BO references.
> +            if op.flags & !VmBindOpType::MASK != 0 || op.bo_handle != 0 || op.bo_offset != 0 {
> +                dev_dbg!(
> +                    vm.dev(),
> +                    "VM_BIND UNMAP carries flags/BO refs: flags={:#x} bo_handle={} bo_offset={}\n",
> +                    op.flags,
> +                    op.bo_handle,
> +                    op.bo_offset
> +                );
> +                return Err(EINVAL);
> +            }
> +            vm.unmap_range(op.va, op.size)
> +        }
> +        Err(_) => {
> +            dev_dbg!(
> +                vm.dev(),
> +                "VM_BIND op type {:#x} not supported\n",
> +                op.flags & VmBindOpType::MASK
> +            );
> +            Err(EINVAL)
> +        }
> +    }
> +}
> +
> +#[repr(transparent)]
> +struct VmBindOp(uapi::drm_panthor_vm_bind_op);
> +
> +impl core::ops::Deref for VmBindOp {
> +    type Target = uapi::drm_panthor_vm_bind_op;
> +
> +    fn deref(&self) -> &Self::Target {
> +        &self.0
> +    }
>  }
> +
> +// TODO: use zerocopy FromBytes when these traits are derived for bindings.
> +// SAFETY: `VmBindOp` contains only integers, so any bit pattern is valid;
> +// the `#[repr(transparent)]` wrapper has the same layout as the UAPI struct.
> +unsafe impl FromBytes for VmBindOp {}
> diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs
> index 0e0989a48678f..60f6363995a5a 100644
> --- a/drivers/gpu/drm/tyr/gem.rs
> +++ b/drivers/gpu/drm/tyr/gem.rs
> @@ -77,7 +77,6 @@ pub(crate) fn new_object(ddev: &TyrDrmDevice, size: usize, flags: u32) -> Result
>  }
>  
>  /// Look up a GEM object by handle for a DRM file.
> -#[expect(dead_code)]
>  pub(crate) fn lookup_handle(file: &TyrDrmFile, handle: u32) -> Result<ARef<Bo>> {
>      Bo::lookup_handle(file, handle)
>  }
> diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
> index 029a08051a87e..45e42e210de29 100644
> --- a/drivers/gpu/drm/tyr/vm.rs
> +++ b/drivers/gpu/drm/tyr/vm.rs
> @@ -55,6 +55,11 @@
>      },
>      sync::{
>          aref::ARef,
> +        atomic::{
> +            Acquire,
> +            Atomic,
> +            Release, //
> +        },
>          Arc,
>          ArcBorrow,
>          Mutex, //
> @@ -174,7 +179,6 @@ pub(crate) enum UserVaRequest {
>  
>  impl UserVaRequest {
>      /// UAPI boundary normalization: `0` -> [`Auto`](Self::Auto).
> -    #[expect(dead_code)]
>      pub(crate) fn from_uapi(v: u64) -> Self {
>          match NonZeroU64::new(v) {
>              Some(size) => Self::Fixed(size),
> @@ -183,6 +187,38 @@ pub(crate) fn from_uapi(v: u64) -> Self {
>      }
>  }
>  
> +/// Operation type, packed into the top nibble of
> +/// `drm_panthor_vm_bind_op::flags`.
> +#[derive(Debug, Clone, Copy, PartialEq, Eq)]
> +pub(crate) enum VmBindOpType {
> +    /// Map a BO range into the VM.
> +    Map,
> +    /// Unmap a VA range.
> +    Unmap,
> +}
> +
> +impl VmBindOpType {
> +    /// Bits occupied by the op type in `drm_panthor_vm_bind_op::flags`.
> +    pub(crate) const MASK: u32 =
> +        uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_TYPE_MASK as u32;
> +}
> +
> +impl TryFrom<u32> for VmBindOpType {
> +    type Error = Error;
> +
> +    fn try_from(flags: u32) -> Result<Self, Self::Error> {
> +        const MAP: u32 = uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_TYPE_MAP as u32;
> +        const UNMAP: u32 =
> +            uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP as u32;
> +
> +        match flags & Self::MASK {
> +            MAP => Ok(Self::Map),
> +            UNMAP => Ok(Self::Unmap),
> +            _ => Err(EINVAL),
> +        }
> +    }
> +}
> +
>  /// Final user/kernel VA layout for a VM.
>  pub(crate) struct VmLayout {
>      /// Full GPU VA range covered by this VM.
> @@ -425,6 +461,8 @@ pub(crate) struct Vm<'drm> {
>      gpuvm: ARef<GpuVm<GpuVmData<'drm>>>,
>      /// VA layout for this VM.
>      pub(crate) layout: VmLayout,
> +    /// Whether the VM is unusable.
> +    unusable: Atomic<bool>,
>  }
>  
>  impl<'drm> Vm<'drm> {
> @@ -446,7 +484,6 @@ pub(crate) fn new_for_fw(
>      }
>  
>      /// Creates a user VM, splitting the GPU VA range per `user_va`.
> -    #[expect(dead_code)]
>      pub(crate) fn new_for_user(
>          dev: &'drm Device<Bound>,
>          ddev: &TyrDrmDevice,
> @@ -513,6 +550,7 @@ fn new_internal(
>                  gpuvm,
>                  gpuvm_unique <- new_mutex!(gpuvm_unique),
>                  layout,
> +                unusable: Atomic::new(false),
>              }),
>              GFP_KERNEL,
>          )?;
> @@ -543,6 +581,7 @@ fn deactivate(&self) -> Result {
>  
>      /// Kills the VM by deactivating it and unmapping all regions.
>      pub(crate) fn kill(&self) {
> +        self.mark_unusable();
>          let _ = self.deactivate();
>          let _ = self
>              .unmap_range(
> @@ -554,6 +593,15 @@ pub(crate) fn kill(&self) {
>              });
>      }
>  
> +    /// Marks the VM unusable.
> +    fn mark_unusable(&self) {
> +        self.unusable.store(true, Release);
> +    }
> +
> +    pub(crate) fn is_unusable(&self) -> bool {
> +        self.unusable.load(Acquire)
> +    }
> +
>      /// Executes a virtual memory operation.
>      ///
>      /// This handles both map and unmap operations by coordinating between the
> @@ -648,6 +696,14 @@ pub(crate) fn map_bo_range(
>  
>          let va_end: u64 = va.checked_add(map_size).ok_or(EINVAL)?;
>  
> +        let mut resources = VmOpResources {
> +            preallocated_gpuvas: [
> +                Some(GpuVaAlloc::<GpuVmData<'drm>>::new(GFP_KERNEL)?),
> +                Some(GpuVaAlloc::<GpuVmData<'drm>>::new(GFP_KERNEL)?),
> +                Some(GpuVaAlloc::<GpuVmData<'drm>>::new(GFP_KERNEL)?),
> +            ],
> +        };
> +
>          let req = VmOpRequest {
>              op_type: VmOpType::Map(VmMapArgs {
>                  vm_bo: self.gpuvm.obtain(bo, ())?,
> @@ -656,16 +712,24 @@ pub(crate) fn map_bo_range(
>              }),
>              region: va..va_end,
>          };
> -        let mut resources = VmOpResources {
> -            preallocated_gpuvas: [
> -                Some(GpuVaAlloc::<GpuVmData<'drm>>::new(GFP_KERNEL)?),
> -                Some(GpuVaAlloc::<GpuVmData<'drm>>::new(GFP_KERNEL)?),
> -                Some(GpuVaAlloc::<GpuVmData<'drm>>::new(GFP_KERNEL)?),
> -            ],
> -        };
>          let result = {
>              let mut gpuvm_unique = self.gpuvm_unique.lock();
> -            self.exec_op(gpuvm_unique.as_mut().get_mut(), req, &mut resources)
> +            // Check under the GPUVM lock, which the kill path also takes to
> +            // unmap, so this serializes against a concurrent teardown.
> +            if self.is_unusable() {
> +                dev_err!(
> +                    self.dev,
> +                    "Failed to map VA {:#x}..{:#x}: VM is unusable\n",
> +                    req.region.start,
> +                    req.region.end
> +                );
> +                // Drop the vm_bo reference before the deferred cleanup
> +                // below runs; otherwise the vm_bo would leak.
> +                drop(req);
> +                Err(EINVAL)
> +            } else {
> +                self.exec_op(gpuvm_unique.as_mut().get_mut(), req, &mut resources)
> +            }
>          };
>          // We flush the defer cleanup list now. Things will be different in
>          // the asynchronous VM_BIND path, where we want the cleanup to
> @@ -1100,9 +1164,7 @@ pub(crate) struct VmPool<'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>>,
>  }
>  
> @@ -1112,7 +1174,6 @@ impl<'drm> VmPool<'drm> {
>      /// 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 {
> @@ -1128,7 +1189,6 @@ pub(crate) fn new(dev: &'drm Device<Bound>) -> Result<impl PinInit<Self>, AllocE
>      /// 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();
> @@ -1178,7 +1238,6 @@ pub(crate) fn add(&self, vm: Arc<Vm<'drm>>) -> Result<u32, AddError<'drm>> {
>      }
>  
>      /// Removes and kills the VM with the given ID.
> -    #[expect(dead_code)]
>      pub(crate) fn remove(&self, id: u32) -> Result<()> {
>          let mut vms = self.vms.lock();
>          let vm = match vms.remove(usize::from_safe_cast(id)) {
> @@ -1195,7 +1254,6 @@ pub(crate) fn remove(&self, id: u32) -> Result<()> {
>      }
>  
>      /// 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))?;
> 
> -- 
> 2.43.0
> 
> 

  parent reply	other threads:[~2026-11-24 23:41 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 12:50 [PATCH v7 00/10] drm/tyr: add VM and BO ioctl support Alvin Sun
2026-11-22 12:50 ` [PATCH v7 01/10] rust: sizes: add SZ_4G constant Alvin Sun
2026-11-22 12:50 ` [PATCH v7 02/10] rust: task: add task_size() Alvin Sun
2026-11-25 14:45   ` Daniel Almeida
2026-11-22 12:50 ` [PATCH v7 03/10] rust: sync: arc: relax `ForeignOwnable` for `Arc<T>` Alvin Sun
2026-11-22 12:50 ` [PATCH v7 04/10] drm/tyr: add per-file VM pool Alvin Sun
2026-11-24 23:43   ` Deborah Brouwer
2026-11-25 15:14   ` Daniel Almeida
2026-11-22 12:50 ` [PATCH v7 05/10] drm/tyr: add user and MCU VM specifications Alvin Sun
2026-11-25 16:13   ` Daniel Almeida
2026-11-22 12:50 ` [PATCH v7 06/10] drm/tyr: add BO creation and lookup helpers Alvin Sun
2026-11-22 12:50 ` [PATCH v7 07/10] drm/tyr: add VM-related ioctls Alvin Sun
2026-11-22 13:19   ` sashiko-bot
2026-11-24 23:40   ` Deborah Brouwer [this message]
2026-11-25 18:49   ` Daniel Almeida
2026-11-22 12:50 ` [PATCH v7 08/10] rust: device: expose the device DMA attribute Alvin Sun
2026-11-25 18:52   ` Daniel Almeida
2026-11-22 12:50 ` [PATCH v7 09/10] drm/tyr: support write-combine BO mappings Alvin Sun
2026-11-22 13:16   ` sashiko-bot
2026-11-30 23:47     ` Daniel Almeida
2026-11-25 19:59   ` Daniel Almeida
2026-11-30 20:09     ` Deborah Brouwer
2026-11-30 23:11       ` Daniel Almeida
2026-11-22 12:50 ` [PATCH v7 10/10] drm/tyr: add BO-related ioctls Alvin Sun
2026-11-22 13:15   ` sashiko-bot
2026-11-25 21:02   ` Daniel Almeida
2026-11-23 22:25 ` [PATCH v7 00/10] drm/tyr: add VM and BO ioctl support Daniel Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awYgh-A8JtN899eB@um790 \
    --to=deborah.brouwer@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=alvin.sun@linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=samitolvanen@google.com \
    --cc=simona@ffwll.ch \
    --cc=sunke@kylinos.cn \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.