From nobody Mon Feb 9 06:24:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6507311C33 for ; Thu, 22 Jan 2026 22:51:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769122300; cv=none; b=lH21OO6S3qVPXP/rkUd+8KyNmdvpPEC9N7MRGOBVEL/hCAwL1uRV4FUM/jaE6DCqRGAWni8Bk2n0odSHrr7dE75JQrYsBNldkO7ENL5NRaTjiMBsJ8JzFSUBbe5vmbGRm2kz+VXOmMmjyDixFGOtiackCzepejcd2no/Ygj2NXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769122300; c=relaxed/simple; bh=6bdd0gxxIUoT7vkza/x9EvzhD+mOJtGCp26GKzkFTsI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lXqb/yhiAIUGlrOY0AqNOjCBC1CgyqS8C/rVZkBqLGoQSEFMJS7YxVNpDGrosBI+5kijpQusSAcaTCiI6lmnpef0lnB+OTA0C5FwnSwmUuhCXQPCvJZhBBSqZgmbrdLRZkZCfZhMIA+E3unxrd/X8gd6fCUbptzGETzPERuBPdg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WM3xYd7e; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WM3xYd7e" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769122293; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O5w1C5A1b7/iDoWWklIxjrD42HoB0GOu7XofvrCfsI8=; b=WM3xYd7eLQvPio7NXe+WUrm7ku8785nqG+gUI0lzLxqqutddDlLKOHs+uiCDmtEXu81IL9 LqB/LrVVbulBUAUUuF7jRFqo5yqU0xt3nlTGG+zaWaaLhzh/fV+i7A2fBW1mv7xvKe55Km tyXTbstL9r3nqRplY9uN6d42L6D2iEA= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-679-TTC83R5rPseRm-Vi29HBwg-1; Thu, 22 Jan 2026 17:51:31 -0500 X-MC-Unique: TTC83R5rPseRm-Vi29HBwg-1 X-Mimecast-MFC-AGG-ID: TTC83R5rPseRm-Vi29HBwg_1769122290 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 01747180044D; Thu, 22 Jan 2026 22:51:30 +0000 (UTC) Received: from GoldenWind.redhat.com (unknown [10.22.89.232]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 090C130002D8; Thu, 22 Jan 2026 22:51:27 +0000 (UTC) From: Lyude Paul To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Cc: nouveau@lists.freedesktop.org, "Miguel Ojeda" , "Simona Vetter" , "Alice Ryhl" , "Shankari Anand" , "David Airlie" , "Benno Lossin" , "Asahi Lina" , "Daniel Almeida" , "Lyude Paul" Subject: [PATCH v3 2/3] rust/drm: Don't setup private driver data until registration Date: Thu, 22 Jan 2026 17:46:37 -0500 Message-ID: <20260122225057.3589500-3-lyude@redhat.com> In-Reply-To: <20260122225057.3589500-1-lyude@redhat.com> References: <20260122225057.3589500-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Now that we have a DeviceContext that we can use to represent whether a Device is known to have been registered, we can make it so that drivers can create their Devices but wait until the registration phase to assign their private data to the Device. This is desirable as some drivers need to make use of the DRM device early on before finalizing their private driver data. As such, this change makes it so that the driver's private data can currently only be accessed through Device types and not Device. Signed-off-by: Lyude Paul --- drivers/gpu/drm/nova/driver.rs | 4 +-- drivers/gpu/drm/tyr/driver.rs | 4 +-- rust/kernel/drm/device.rs | 63 ++++++++++++++++++++-------------- rust/kernel/drm/driver.rs | 19 ++++++++-- 4 files changed, 58 insertions(+), 32 deletions(-) diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs index 99d6841b69cbc..8cea5f68c3b04 100644 --- a/drivers/gpu/drm/nova/driver.rs +++ b/drivers/gpu/drm/nova/driver.rs @@ -56,8 +56,8 @@ impl auxiliary::Driver for NovaDriver { fn probe(adev: &auxiliary::Device, _info: &Self::IdInfo) -> impl= PinInit { let data =3D try_pin_init!(NovaData { adev: adev.into() }); =20 - let drm =3D drm::UnregisteredDevice::::new(adev.as_ref(), da= ta)?; - let drm =3D drm::Registration::new_foreign_owned(drm, adev.as_ref(= ), 0)?; + let drm =3D drm::UnregisteredDevice::::new(adev.as_ref())?; + let drm =3D drm::Registration::new_foreign_owned(drm, adev.as_ref(= ), data, 0)?; =20 Ok(Self { drm: drm.into() }) } diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs index ac265a60f5667..e73c56659ea75 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -133,8 +133,8 @@ fn probe( gpu_info, }); =20 - let tdev =3D drm::UnregisteredDevice::::new(pdev.as_ref(), d= ata)?; - let tdev =3D drm::driver::Registration::new_foreign_owned(tdev, pd= ev.as_ref(), 0)?; + let tdev =3D drm::UnregisteredDevice::::new(pdev.as_ref())?; + let tdev =3D drm::driver::Registration::new_foreign_owned(tdev, pd= ev.as_ref(), data, 0)?; =20 let driver =3D TyrDriver { device: tdev.into(), diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index f529bc7fc4032..0e81957cf8c28 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -26,13 +26,15 @@ }; use core::{ alloc::Layout, + cell::UnsafeCell, marker::PhantomData, - mem, - ops::Deref, - ptr::{ + mem::{ self, - NonNull, // + MaybeUninit, // }, + ops::Deref, + ptr::NonNull, + sync::atomic::*, }; =20 #[cfg(CONFIG_DRM_LEGACY)] @@ -141,7 +143,7 @@ impl DeviceContext for Uninit {} /// /// The device in `self.0` is guaranteed to be a newly created [`Device`] = that has not yet been /// registered with userspace until this type is dropped. -pub struct UnregisteredDevice(ARef>, Not= ThreadSafe); +pub struct UnregisteredDevice(pub(crate) ARef>, NotThreadSafe); =20 impl Deref for UnregisteredDevice { type Target =3D Device; @@ -188,7 +190,7 @@ impl UnregisteredDevice { /// Create a new `UnregisteredDevice` for a `drm::Driver`. /// /// This can be used to create a [`Registration`](kernel::drm::Registr= ation). - pub fn new(dev: &device::Device, data: impl PinInit) -= > Result { + pub fn new(dev: &device::Device) -> Result { // `__drm_dev_alloc` uses `kmalloc()` to allocate memory, hence en= sure a `kmalloc()` // compatible `Layout`. let layout =3D Kmalloc::aligned_layout(Layout::new::()); @@ -207,22 +209,6 @@ pub fn new(dev: &device::Device, data: impl PinInit) -> Result) -> Result { dev: Opaque, - data: T::Data, + + /// Keeps track of whether we've initialized the device data yet. + pub(crate) data_is_init: AtomicBool, + + /// The Driver's private data. + /// + /// This must only be written to from [`drm::Registration::new`]. + pub(crate) data: UnsafeCell>, + _ctx: PhantomData, } =20 @@ -305,6 +299,21 @@ extern "C" fn release(ptr: *mut bindings::drm_device) { // SAFETY: `ptr` is a valid pointer to a `struct drm_device` and e= mbedded in `Self`. let this =3D unsafe { Self::from_drm_device(ptr) }; =20 + { + // SAFETY: + // - Since we are in release(), we are guaranteed that no one = else has access to `this`. + // - We confirmed above that `this` is a valid pointer to an i= nitialized `Self`. + let this =3D unsafe { &mut *this }; + if this.data_is_init.load(Ordering::Relaxed) { + // SAFETY: + // - Since we are in release(), we are guaranteed that no = one else has access to + // `this`. + // - We checked that the data is initialized above. + // - We do not use `data` any point after calling this fun= ction. + unsafe { (&mut *this.data.get()).assume_init_drop() }; + } + } + // SAFETY: // - When `release` runs it is guaranteed that there is no further= access to `this`. // - `this` is valid for dropping. @@ -323,11 +332,15 @@ pub(crate) unsafe fn assume_ctx(&self) -> &Device Deref for Device { +impl Deref for Device { type Target =3D T::Data; =20 fn deref(&self) -> &Self::Target { - &self.data + // SAFETY: + // - `data` is initialized before any `Device`s with the `Register= ed` context are available + // to the user. + // - `data` is only written to once in `Registration::new()`, so t= his read will never race. + unsafe { (&*self.data.get()).assume_init_ref() } } } =20 diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index da9f1bfef1f14..a16605b407159 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -15,7 +15,8 @@ }; use core::{ mem, - ptr::NonNull, // + ptr::NonNull, + sync::atomic::*, // }; =20 /// Driver use the GEM memory manager. This should be set for all modern d= rivers. @@ -127,7 +128,18 @@ pub trait Driver { pub struct Registration(ARef>); =20 impl Registration { - fn new(drm: drm::UnregisteredDevice, flags: usize) -> Result { + fn new( + drm: drm::UnregisteredDevice, + data: impl PinInit, + flags: usize, + ) -> Result { + // SAFETY: + // - `raw_data` is a valid pointer to uninitialized memory. + // - `raw_data` will not move until it is dropped. + unsafe { data.__pinned_init(drm.0.data.get().cast()) }?; + + drm.data_is_init.store(true, Ordering::Relaxed); + // SAFETY: `drm.as_raw()` is valid by the invariants of `drm::Devi= ce`. to_result(unsafe { bindings::drm_dev_register(drm.as_raw(), flags)= })?; =20 @@ -150,6 +162,7 @@ fn new(drm: drm::UnregisteredDevice, flags: usize) -= > Result { pub fn new_foreign_owned<'a>( drm: drm::UnregisteredDevice, dev: &'a device::Device, + data: impl PinInit, flags: usize, ) -> Result<&'a drm::Device> where @@ -160,7 +173,7 @@ pub fn new_foreign_owned<'a>( return Err(EINVAL); } =20 - let reg =3D Registration::::new(drm, flags)?; + let reg =3D Registration::::new(drm, data, flags)?; let drm =3D NonNull::from(reg.device()); =20 devres::register(dev, reg, GFP_KERNEL)?; --=20 2.52.0