From nobody Mon Feb 9 08:55:03 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 2FF3A2DE6F8 for ; Fri, 16 Jan 2026 20:48:48 +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=1768596530; cv=none; b=Iry5ascqm2jgLh/jxAXIjIgGOInEJ9p2+cZSAPeSCn6CZa1BwmqfeoCctwP8oaLa8NnLhNtK+KjBHf4axiXD5CG8rpF99Hgq5lp8FTLI7jUPrZ6Rua7I3YVwkT9H6henQuYukOBprStuSHc15WaS0ofyak6c3I30a/bMaU9aRKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768596530; c=relaxed/simple; bh=dM7a0WypQmASWwxBmj9+G9CCNno8Ose+awQHp0TZJMg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jLSvXZLBBECM0nB5Uz+DZvyc9LffhISYx0d68B5ypYuc+jiD3SxylYTAc46ni6LpC3vQ+5lS/Pw0IF4nrUNSf0b0zDgbGtad9BbafJiArT/cUYTLbT+9Nt7rG+i42vz3H0bIL4/lzHKNA3HgYGucHHN9vmvUXHbi6hFpr6Ii35c= 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=TemqPS/B; 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="TemqPS/B" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768596528; 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=aVghHh/6Tvx1nZadAtowBMJ57wxygN81p6bj4elge0k=; b=TemqPS/BkqzbQdQGDyG3D6p0LzcNGGQDzRCDbPqFwxuDr/rdjEF2CH8w9x6ZbPCG5qK2T+ YhdHwvltEuPMsH8L+wWBM5eGi+2ZPjUTEJuIo39kVtAug9hc2oaIIbeNfmNr7xf2E20NHp 529F97W8oA5X0fh3wisosFDzGeB2VXg= 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-49-RIXfw7V9ODu69zKM8Jr40g-1; Fri, 16 Jan 2026 15:48:44 -0500 X-MC-Unique: RIXfw7V9ODu69zKM8Jr40g-1 X-Mimecast-MFC-AGG-ID: RIXfw7V9ODu69zKM8Jr40g_1768596522 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 892901800447; Fri, 16 Jan 2026 20:48:42 +0000 (UTC) Received: from GoldenWind.redhat.com (unknown [10.22.88.63]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8282319560A7; Fri, 16 Jan 2026 20:48:40 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, "Miguel Ojeda" , "Simona Vetter" , "Alice Ryhl" , "Shankari Anand" , "David Airlie" , "Benno Lossin" , "Danilo Krummrich" , "Asahi Lina" , "Atharv Dubey" , "Daniel Almeida" , "Lyude Paul" Subject: [PATCH v2 2/3] rust/drm: Don't setup private driver data until registration Date: Fri, 16 Jan 2026 15:41:42 -0500 Message-ID: <20260116204728.725579-3-lyude@redhat.com> In-Reply-To: <20260116204728.725579-1-lyude@redhat.com> References: <20260116204728.725579-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.0 on 10.30.177.12 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 | 59 +++++++++++++++++++++------------- rust/kernel/drm/driver.rs | 23 +++++++++++-- 4 files changed, 60 insertions(+), 30 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 09c3157531072..768d50bde929e 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -139,8 +139,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 53ca71daf2f86..441ed7c94fcf5 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -16,10 +16,12 @@ }; use core::{ alloc::Layout, + cell::UnsafeCell, marker::PhantomData, - mem::{self}, + mem::{self, MaybeUninit}, ops::Deref, - ptr::{self, NonNull}, + ptr::NonNull, + sync::atomic::*, }; =20 #[cfg(CONFIG_DRM_LEGACY)] @@ -118,7 +120,7 @@ pub trait DeviceContext: Sealed + Send + Sync {} /// /// 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; @@ -165,7 +167,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::()); @@ -184,22 +186,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 @@ -282,6 +276,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. @@ -300,11 +309,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 fb4f215320bd9..108d789e43759 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -10,7 +10,12 @@ prelude::*, sync::aref::ARef, }; -use core::{mem, ptr::NonNull}; +use core::{ + mem, + ptr::NonNull, + sync::atomic::*, + // +}; use macros::vtable; =20 /// Driver use the GEM memory manager. This should be set for all modern d= rivers. @@ -123,7 +128,18 @@ pub trait Driver { =20 impl Registration { /// Creates a new [`Registration`] and registers it. - 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 @@ -145,6 +161,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 @@ -155,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