From nobody Sat Feb 7 17:42:17 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 A266D2F25E0 for ; Sat, 31 Jan 2026 00:16:37 +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=1769818599; cv=none; b=ZASrdcpj//lwa5OF+P18tDJquDJk2ylfa0vukB7o/zeUTO9aZW5+o1IPKLTX/m1/PgOtM/dS2a8PEDQtwsYyknBFc0FhidCzcrXnQodj/NS3Vyc15p8YczKmiHUTX8/3xNscZziyEmDJNHzhT4FFJp87Y88YlaV1qNa+qrybwGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769818599; c=relaxed/simple; bh=x6BkJw6QGJ2oa0Vs3ckvYkRN78d3lMcvw/PideCkfcE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WujAnq07RDcZZEiZyGrj4h7b80jm1Mfzb6t7HtaZGZWrfk5dXdF+Q6193E8iIwubCfMdvgSGsrtlAMUI57NiTk0n0OkcfPxCMriHRq3mhzz2rfpO2u9XroyMp/imAwBbRejHZ/4zjbV/8qAiPCEU7JAeYvrNlpPjLgIwYMlIRTE= 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=htkokAhn; 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="htkokAhn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769818596; 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=I4xaYhUpLXOBwZoEHLADcBcWiGEAuhBq9dyW2GYPd4M=; b=htkokAhn64TR8LmBuYDSVVRkjOcSAka2fJ9bzgw/INf/LQ8nV8WlKOihrY7AXy4zLKbFD3 ShNqClM3BOCt/2JZ5vTN1yDcAmgYVoMEgjhfQKQxbYgirNd1/biSTg6UDOkHx5IBFcDPoC 61OzQSX8w5Ow2Y3I3zghPA2YphTqauM= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-155-SPkFFwgKPVKNflvsfufnaA-1; Fri, 30 Jan 2026 19:16:31 -0500 X-MC-Unique: SPkFFwgKPVKNflvsfufnaA-1 X-Mimecast-MFC-AGG-ID: SPkFFwgKPVKNflvsfufnaA_1769818589 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C4AA41954B06; Sat, 31 Jan 2026 00:16:28 +0000 (UTC) Received: from GoldenWind.lan (unknown [10.22.81.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CA693180097D; Sat, 31 Jan 2026 00:16:26 +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 v5 2/4] rust/drm: Don't setup private driver data until registration Date: Fri, 30 Jan 2026 19:13:31 -0500 Message-ID: <20260131001602.2095470-3-lyude@redhat.com> In-Reply-To: <20260131001602.2095470-1-lyude@redhat.com> References: <20260131001602.2095470-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.111 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 Reviewed-by: Daniel Almeida --- V4: * Remove accidental double-aliasing &mut in Device::release() --- drivers/gpu/drm/nova/driver.rs | 4 +-- drivers/gpu/drm/tyr/driver.rs | 4 +-- rust/kernel/drm/device.rs | 64 +++++++++++++++++++++------------- rust/kernel/drm/driver.rs | 19 ++++++++-- 4 files changed, 59 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 0d479b5d7bd62..b574ee1c24587 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 8f1780b8f0196..a1d9b0e92f3fd 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 @@ -304,6 +298,22 @@ 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 initi= alized `Self`. + let is_init =3D unsafe { &*this }.data_is_init.load(Ordering::Rela= xed); + if is_init { + // SAFETY: + // - We confirmed we have unique access to this above. + // - We confirmed that `data` is initialized above. + let data_ptr =3D unsafe { &mut (*this).data }; + + // SAFETY: + // - We checked that the data is initialized above. + // - We do not use `data` any point after calling this functio= n. + unsafe { data_ptr.get_mut().assume_init_drop() }; + } + // SAFETY: // - When `release` runs it is guaranteed that there is no further= access to `this`. // - `this` is valid for dropping. @@ -322,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 55b01ee088548..cfb8884ece700 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 @@ -159,7 +172,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