From nobody Sun Feb 8 05:35:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 C207A2EB1D for ; Thu, 1 May 2025 18:37:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124664; cv=none; b=fWbdLg2YWaZATPdHa6un/+AIw7phPZSd5sho93KaCMZ4pwQ69St913UGp2nD+BaslYu/Z6oIVqXaTcvftJ5DkL+nuMvTaIFsFclz0fIkPm+hodGUv07NPWVkwb/qhk71bO7LWhgJARcr9PtJgYwvGwArA6esMT1GmD+rMtI2em4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124664; c=relaxed/simple; bh=QKJ4KMIR2LHo4QbyjHq6CKsc6K7OQm8a8lXBoN9U8yo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CAv4eCKs1ULmntfSxqWcsIVFY7cMfrQww8+24Wu5sTalgL9/Shkw4pNmksjxbFnKWTnBZxixVBlV2RgC+eXKcz32sRWh6vQ6IkgNe57H3RKv4iHfQdBOk0G1TcDr4iBBtOMQnPWstEqkopPhFoOCzkVE2JUmN9yDnYkrmRQRQcM= 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=bP4uz6cz; arc=none smtp.client-ip=170.10.133.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="bP4uz6cz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746124660; 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=yk7M5D8H8aC/JgAaxCS1maVk5f6A5cbefflNr0lOROM=; b=bP4uz6cz06ZrGNxqsHWjLVFZYWMvfLoybvmZlfCW535Zad0Fl48RjcbP/RUqzI5KgpT+I1 TE8CRzGFqIbX5+sKvUDXpRjxKPeoaPS6HeA3uO7VXAKPFS1fRRakm263OQKln/XnskGjhP wOu1AOHScTWnhNbOJtJKzWel/APMcJA= Received: from mx-prod-mc-06.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-402-U4in9u2YObyfp8QPw3IqWA-1; Thu, 01 May 2025 14:37:37 -0400 X-MC-Unique: U4in9u2YObyfp8QPw3IqWA-1 X-Mimecast-MFC-AGG-ID: U4in9u2YObyfp8QPw3IqWA_1746124655 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B899F1800980; Thu, 1 May 2025 18:37:34 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0D2E41800115; Thu, 1 May 2025 18:37:29 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Asahi Lina , Alyssa Rosenzweig Subject: [PATCH 1/4] rust: drm: gem: Use NonNull for Object::dev Date: Thu, 1 May 2025 14:33:16 -0400 Message-ID: <20250501183717.2058109-2-lyude@redhat.com> In-Reply-To: <20250501183717.2058109-1-lyude@redhat.com> References: <20250501183717.2058109-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.93 Content-Type: text/plain; charset="utf-8" There is usually not much of a reason to use a raw pointer in a data struct, so move this to NonNull instead. Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida Reviewed-by: Danilo Krummrich --- rust/kernel/drm/gem/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index 0cafa4a424206..df8f9fdae5c22 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -177,7 +177,7 @@ impl BaseObject for T where Self: crate::types::Alwa= ysRefCounted + IntoGEMObj #[pin_data] pub struct Object { obj: Opaque, - dev: *const drm::Device, + dev: NonNull>, #[pin] data: T, } @@ -212,7 +212,7 @@ pub fn new(dev: &drm::Device, size: usize) -= > Result> { data <- T::new(dev, size), // INVARIANT: The drm subsystem guarantees that the `struc= t drm_device` will live // as long as the GEM object lives. - dev, + dev: dev.into(), }), GFP_KERNEL, )?; @@ -237,7 +237,7 @@ pub fn new(dev: &drm::Device, size: usize) -= > Result> { pub fn dev(&self) -> &drm::Device { // SAFETY: The DRM subsystem guarantees that the `struct drm_devic= e` will live as long as // the GEM object lives, hence the pointer must be valid. - unsafe { &*self.dev } + unsafe { self.dev.as_ref() } } =20 fn as_raw(&self) -> *mut bindings::drm_gem_object { --=20 2.48.1 From nobody Sun Feb 8 05:35:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 573B1223DED for ; Thu, 1 May 2025 18:37:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124672; cv=none; b=hLGbfj7YD+qfGw3gfh4NwL9fyJO9FqTLZSwi42yXX74pvr5D8Qjn9JdKFF6h2DouPirYvAJEICqqS9N30j1O/mX6KkQmoi5H5LGql/yepVwpleBl+5ioe7CAJ1HDaMStgl/YNYECcDUp67t7WWBbLdQRa/4myuzjjqj1+eaBuTE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124672; c=relaxed/simple; bh=SrzFAyg30d6oGKEyLW8zbG9Y5AMJTECOWzAs9Z0xqlQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kNLc+ar/Kdz0zt5tg+WRKa6JtVY16nNCD54URklUXOzdCc2/cF7fmWs/N3/KIPAhv4Yx+dMtMPXMQh33opygo85+usen59v795hQwa7x9h6akF6ferD9puL62NMsXJmyY36trvl98Diba5fpPUaerpZx8A47P5RXMEQ2JTwVZH4= 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=JFYajEIl; arc=none smtp.client-ip=170.10.133.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="JFYajEIl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746124668; 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=GRNBnPmAgdVGuEdAa7ItVzU96gbWj7IMk8GXzUCuJj8=; b=JFYajEIlcQeoe10bwaukanJMNFVxWCJ6pF5XdmCzjNd1Qem9GFDv3WhEcDBLKRNFDY0jUn Dq9XP8wDnvF0s1V53yBz/t/fW5uAPs0i74CJd1Z9QzTDc1TZLpt3hBmP/NSG9IwvzRUzLC jvLwQfjyz4iaIO7x5koje3DqYZx64m0= 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-18-gnVgooa7MYmArRUfhUZFwg-1; Thu, 01 May 2025 14:37:45 -0400 X-MC-Unique: gnVgooa7MYmArRUfhUZFwg-1 X-Mimecast-MFC-AGG-ID: gnVgooa7MYmArRUfhUZFwg_1746124662 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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 D64771801A0F; Thu, 1 May 2025 18:37:41 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 033881800115; Thu, 1 May 2025 18:37:37 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Asahi Lina , Alyssa Rosenzweig Subject: [PATCH 2/4] rust: drm: gem: Refactor IntoGEMObject::from_gem_obj() to as_ref() Date: Thu, 1 May 2025 14:33:17 -0400 Message-ID: <20250501183717.2058109-3-lyude@redhat.com> In-Reply-To: <20250501183717.2058109-1-lyude@redhat.com> References: <20250501183717.2058109-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.93 Content-Type: text/plain; charset="utf-8" There's a few issues with this function, mainly: * This function -probably- should have been unsafe from the start. Pointers are not always necessarily valid, but you want a function that does field-projection for a pointer that can travel outside of the original struct to be unsafe, at least if I understand properly. * *mut Self is not terribly useful in this context, the majority of uses of from_gem_obj() grab a *mut Self and then immediately convert it into a &'a Self. It also goes against the ffi conventions we've set in the rest of the kernel thus far. * from_gem_obj() also doesn't follow the naming conventions in the rest of the DRM bindings at the moment, as_ref() would be a better name. So, let's: * Make from_gem_obj() unsafe * Convert it to return &'a Self * Rename it to as_ref() * Update all call locations Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida --- rust/kernel/drm/gem/mod.rs | 67 ++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index df8f9fdae5c22..f70531889c21f 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -45,8 +45,12 @@ pub trait IntoGEMObject: Sized + super::private::Sealed { #[allow(clippy::wrong_self_convention)] fn into_gem_obj(&self) -> &Opaque; =20 - /// Converts a pointer to a `struct drm_gem_object` into a pointer to = `Self`. - fn from_gem_obj(obj: *mut bindings::drm_gem_object) -> *mut Self; + /// Converts a pointer to a `struct drm_gem_object` into a reference t= o `Self`. + /// + /// # Safety + /// + /// `self_ptr` must be a valid pointer to `Self`. + unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a S= elf; } =20 /// Trait which must be implemented by drivers using base GEM objects. @@ -63,14 +67,13 @@ extern "C" fn open_callback, U: = BaseObject>( let file =3D unsafe { drm::File::<<::Driver as drm::Driver>::File>::= as_ref(raw_file) }; - let obj =3D - <<::Driver as drm::Driver>::Object as IntoGEMO= bject>::from_gem_obj( - raw_obj, - ); - - // SAFETY: `from_gem_obj()` returns a valid pointer as long as the typ= e is correct and the - // `raw_obj` we got is valid. - match T::open(unsafe { &*obj }, file) { + // SAFETY: `open_callback` is specified in the AllocOps structure for = `Object`, ensuring that + // `raw_obj` is indeed contained within a `Object`. + let obj =3D unsafe { + <<::Driver as drm::Driver>::Object as IntoGEMO= bject>::as_ref(raw_obj) + }; + + match T::open(obj, file) { Err(e) =3D> e.to_errno(), Ok(()) =3D> 0, } @@ -84,14 +87,13 @@ extern "C" fn close_callback, U:= BaseObject>( let file =3D unsafe { drm::File::<<::Driver as drm::Driver>::File>::= as_ref(raw_file) }; - let obj =3D - <<::Driver as drm::Driver>::Object as IntoGEMO= bject>::from_gem_obj( - raw_obj, - ); - - // SAFETY: `from_gem_obj()` returns a valid pointer as long as the typ= e is correct and the - // `raw_obj` we got is valid. - T::close(unsafe { &*obj }, file); + // SAFETY: `close_callback` is specified in the AllocOps structure for= `Object`, ensuring + // that `raw_obj` is indeed contained within a `Object`. + let obj =3D unsafe { + <<::Driver as drm::Driver>::Object as IntoGEMO= bject>::as_ref(raw_obj) + }; + + T::close(obj, file); } =20 impl IntoGEMObject for Object { @@ -101,9 +103,10 @@ fn into_gem_obj(&self) -> &Opaque { &self.obj } =20 - fn from_gem_obj(obj: *mut bindings::drm_gem_object) -> *mut Self { - // SAFETY: All of our objects are Object. - unsafe { crate::container_of!(obj, Object, obj).cast_mut() } + unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a S= elf { + // SAFETY: `obj` is guaranteed to be in an `Object` via the saf= ety contract of this + // function + unsafe { &*crate::container_of!(self_ptr, Object, obj) } } } =20 @@ -144,11 +147,25 @@ fn lookup_handle( ) -> Result> { // SAFETY: The arguments are all valid per the type invariants. let ptr =3D unsafe { bindings::drm_gem_object_lookup(file.as_raw()= .cast(), handle) }; - let ptr =3D ::from_gem_obj(ptr); - let ptr =3D NonNull::new(ptr).ok_or(ENOENT)?; =20 - // SAFETY: We take ownership of the reference of `drm_gem_object_l= ookup()`. - Ok(unsafe { ARef::from_raw(ptr) }) + // SAFETY: + // - A `drm::Driver` can only have a single `File` implementation. + // - `file` uses the same `drm::Driver` as `Self`. + // - Therefore, we're guaranteed that `ptr` must be a gem object e= mbedded within `Self`. + // - And we check if the pointer is null befoe calling as_ref(), e= nsuring that `ptr` is a + // valid pointer to an initialized `Self`. + // XXX: The expect lint here is to workaround + // https://github.com/rust-lang/rust-clippy/issues/13024 + #[expect(clippy::undocumented_unsafe_blocks)] + let obj =3D (!ptr.is_null()) + .then(|| unsafe { Self::as_ref(ptr) }) + .ok_or(ENOENT)?; + + // SAFETY: + // - We take ownership of the reference of `drm_gem_object_lookup(= )`. + // - Our `NonNull` comes from an immutable reference, thus ensurin= g it is a valid pointer to + // `Self`. + Ok(unsafe { ARef::from_raw(obj.into()) }) } =20 /// Creates an mmap offset to map the object from userspace. --=20 2.48.1 From nobody Sun Feb 8 05:35:15 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 B2919223DE3 for ; Thu, 1 May 2025 18:37:55 +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=1746124677; cv=none; b=mpqne1O6A0cmTGrD2Xh60yMbKqmHRKKimRt6QVzlQuf1XROJr/WK0Uhnhzk7NNssJbbB/ERVNYGLF0xLAVULXB942U0MIuiTJNA+JC0GSo3XCREeLzW+nHbeS5HBRIfk/CqgqLIvYOhlYUZNfuL/8laU684eDu60pQgyYlnPejo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124677; c=relaxed/simple; bh=0xjLkMFUFx06scABoQ/zs3kafFJnRYg9F5/jrAqUGVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RKA4sErlX5oCTv0dMII9e72l0CeUoaj5/vDs82UNA58bE72fpEqTVGrdUijaDbfC6GLeLUniqjjcD+WPLqq+KofCbOLz4EjfvMogLY6DzUXhvgKxukkL9MC8ZUd6D2cj21pkFeQtoBYmHYvO/Uwr7QUkL3Af1u1u79ikD0shxbA= 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=DHwp4bZs; 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="DHwp4bZs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746124674; 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=LKTzchovYe9btT6kqsT42uzZZYhEcMTVxwqNJvjbEbI=; b=DHwp4bZsovAsCLP8idHSzgxDDKc4NyCxwOlDNUx1Wm/JFWNCjgJgDbRdSurw0zd3T6Yakv XR4J1rtK1UqdRzqgUdpVarf0iJfFPI++rvKqWaol+2nurT9x/GZQzchQ1GVwsbil9ibARQ lJ1lQuLVMB3okVDmunMAe+BlHThtM5s= Received: from mx-prod-mc-06.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-269-XILNJ3fROF2y79f7i2Bsog-1; Thu, 01 May 2025 14:37:51 -0400 X-MC-Unique: XILNJ3fROF2y79f7i2Bsog-1 X-Mimecast-MFC-AGG-ID: XILNJ3fROF2y79f7i2Bsog_1746124669 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E0C7C1800985; Thu, 1 May 2025 18:37:48 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 22FC31800115; Thu, 1 May 2025 18:37:44 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Alyssa Rosenzweig , Asahi Lina Subject: [PATCH 3/4] rust: drm: gem: s/into_gem_obj()/as_gem_obj()/ Date: Thu, 1 May 2025 14:33:18 -0400 Message-ID: <20250501183717.2058109-4-lyude@redhat.com> In-Reply-To: <20250501183717.2058109-1-lyude@redhat.com> References: <20250501183717.2058109-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.93 Content-Type: text/plain; charset="utf-8" There's a few changes here: * The rename, of course (this should also let us drop the clippy annotation here) * Return *mut bindings::drm_gem_object instead of &Opaque - the latter doesn't really have any benefit and just results in conversion from the rust type to the C type having to be more verbose than necessary. Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida --- rust/kernel/drm/gem/mod.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index f70531889c21f..55b2f1d056c39 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -42,8 +42,7 @@ pub trait IntoGEMObject: Sized + super::private::Sealed { =20 /// Returns a reference to the raw `drm_gem_object` structure, which m= ust be valid as long as /// this owning object is valid. - #[allow(clippy::wrong_self_convention)] - fn into_gem_obj(&self) -> &Opaque; + fn as_gem_obj(&self) -> *mut bindings::drm_gem_object; =20 /// Converts a pointer to a `struct drm_gem_object` into a reference t= o `Self`. /// @@ -99,8 +98,8 @@ extern "C" fn close_callback, U: B= aseObject>( impl IntoGEMObject for Object { type Driver =3D T::Driver; =20 - fn into_gem_obj(&self) -> &Opaque { - &self.obj + fn as_gem_obj(&self) -> *mut bindings::drm_gem_object { + self.obj.get() } =20 unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a S= elf { @@ -119,7 +118,7 @@ pub trait BaseObject fn size(&self) -> usize { // SAFETY: `self.into_gem_obj()` is guaranteed to be a pointer to = a valid `struct // drm_gem_object`. - unsafe { (*self.into_gem_obj().get()).size } + unsafe { (*self.as_gem_obj()).size } } =20 /// Creates a new handle for the object associated with a given `File` @@ -131,11 +130,7 @@ fn create_handle( let mut handle: u32 =3D 0; // SAFETY: The arguments are all valid per the type invariants. to_result(unsafe { - bindings::drm_gem_handle_create( - file.as_raw().cast(), - self.into_gem_obj().get(), - &mut handle, - ) + bindings::drm_gem_handle_create(file.as_raw().cast(), self.as_= gem_obj(), &mut handle) })?; Ok(handle) } @@ -171,13 +166,11 @@ fn lookup_handle( /// Creates an mmap offset to map the object from userspace. fn create_mmap_offset(&self) -> Result { // SAFETY: The arguments are valid per the type invariant. - to_result(unsafe { bindings::drm_gem_create_mmap_offset(self.into_= gem_obj().get()) })?; + to_result(unsafe { bindings::drm_gem_create_mmap_offset(self.as_ge= m_obj()) })?; =20 // SAFETY: The arguments are valid per the type invariant. Ok(unsafe { - bindings::drm_vma_node_offset_addr(ptr::addr_of_mut!( - (*self.into_gem_obj().get()).vma_node - )) + bindings::drm_vma_node_offset_addr(ptr::addr_of_mut!((*self.as= _gem_obj()).vma_node)) }) } } --=20 2.48.1 From nobody Sun Feb 8 05:35:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 1C15122371F for ; Thu, 1 May 2025 18:38:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124688; cv=none; b=so3NYubDD/vxKkuft68yGXZ0OoJk+cQMGE/klCCLFBZEWNKqwjP696YobIUnEr4vl/XpwWQhKfZzQ/20fUdI+UaB5x5CLseCC5ZQJb/dwsbtuaT7Q3Znx3I7smhp5FcNe1iL2THvVImvc7gnZKZwv94I3bBo/Vc4S888f8r+Cik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746124688; c=relaxed/simple; bh=DxnFwbyrPKOcqAtYcepj6/QO3dHzr/O5vFh2+K3OJCQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W5ELBmuh9KAAH8rhbAHfFuH2zCPTbFnVWD/YNC3Uywt3dytOReBP882JM1vuJYfbynwoyMYZlTPPht23GTMxPjI4rYgVAvY4ThnJHRBRLa/rB9bicxVV0e+TGDsrG5vmy9axcQZMTmKXr2lilfbE3GfHyohaP58KI7C2wM7iEaQ= 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=SUiPDmGf; arc=none smtp.client-ip=170.10.133.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="SUiPDmGf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746124685; 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=1DK3REbEj1TGDhbQ2B+tFzyd7w0GXeJQ4DMYyVW608g=; b=SUiPDmGfjhrt9/NTwPd6SitTgNGq4IoK0tgAMLYoGr9al7NIVSy9zYskVCS8MMOvTbBHbs jusIAVlt+g6pzP1oyHCaf9SNCGDQNCO8O8oTJlyMxwX6FdOc5e673LtBMZAgdbV+AIxBu2 Qwp3rNoT/BSHNSrQco1/AEb2GjKr/lE= Received: from mx-prod-mc-03.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-116-JMktBwGQPSmXujas70seOQ-1; Thu, 01 May 2025 14:38:01 -0400 X-MC-Unique: JMktBwGQPSmXujas70seOQ-1 X-Mimecast-MFC-AGG-ID: JMktBwGQPSmXujas70seOQ_1746124675 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7C1B219560AF; Thu, 1 May 2025 18:37:55 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1E60C1800365; Thu, 1 May 2025 18:37:51 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Alyssa Rosenzweig , Asahi Lina Subject: [PATCH 4/4] rust: drm: gem: Implement AlwaysRefCounted for all gem objects automatically Date: Thu, 1 May 2025 14:33:19 -0400 Message-ID: <20250501183717.2058109-5-lyude@redhat.com> In-Reply-To: <20250501183717.2058109-1-lyude@redhat.com> References: <20250501183717.2058109-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.93 Content-Type: text/plain; charset="utf-8" Currently we are requiring AlwaysRefCounted in most trait bounds for gem objects, and implementing it by hand for our only current type of gem object. However, all gem objects use the same functions for reference counting - and all gem objects support reference counting. We're planning on adding support for shmem gem objects, let's move this around a bit by instead making IntoGEMObject require AlwaysRefCounted as a trait bound, and then provide a blanket AlwaysRefCounted implementation for any object that implements IntoGEMObject so all gem object types can use the same AlwaysRefCounted implementation. This also makes things less verbose by making the AlwaysRefCounted trait bound implicit for any IntoGEMObject bound. Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida --- rust/kernel/drm/gem/mod.rs | 47 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index 55b2f1d056c39..929f6c9718362 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -10,7 +10,7 @@ drm::driver::{AllocImpl, AllocOps}, error::{to_result, Result}, prelude::*, - types::{ARef, Opaque}, + types::{ARef, AlwaysRefCounted, Opaque}, }; use core::{mem, ops::Deref, ptr, ptr::NonNull}; =20 @@ -36,7 +36,7 @@ fn close( } =20 /// Trait that represents a GEM object subtype -pub trait IntoGEMObject: Sized + super::private::Sealed { +pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted= { /// Owning driver for this type type Driver: drm::Driver; =20 @@ -52,6 +52,26 @@ pub trait IntoGEMObject: Sized + super::private::Sealed { unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a S= elf; } =20 +// SAFETY: All gem objects are refcounted. +unsafe impl AlwaysRefCounted for T { + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference guarantees that the= refcount is non-zero. + unsafe { bindings::drm_gem_object_get(self.as_gem_obj()) }; + } + + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: We either hold the only refcount on `obj`, or one of ma= ny - meaning that no one + // else could possibly hold a mutable reference to `obj` and thus = this immutable reference + // is safe. + let obj =3D unsafe { obj.as_ref() }.as_gem_obj(); + + // SAFETY: + // - The safety requirements guarantee that the refcount is non-ze= ro. + // - We hold no references to `obj` now, making it safe for us to = potentially deallocate it. + unsafe { bindings::drm_gem_object_put(obj) }; + } +} + /// Trait which must be implemented by drivers using base GEM objects. pub trait DriverObject: BaseDriverObject> { /// Parent `Driver` for this object. @@ -110,10 +130,7 @@ unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_= object) -> &'a Self { } =20 /// Base operations shared by all GEM object classes -pub trait BaseObject -where - Self: crate::types::AlwaysRefCounted + IntoGEMObject, -{ +pub trait BaseObject: IntoGEMObject { /// Returns the size of the object in bytes. fn size(&self) -> usize { // SAFETY: `self.into_gem_obj()` is guaranteed to be a pointer to = a valid `struct @@ -175,7 +192,7 @@ fn create_mmap_offset(&self) -> Result { } } =20 -impl BaseObject for T where Self: crate::types::AlwaysRefCounted + Into= GEMObject {} +impl BaseObject for T {} =20 /// A base GEM object. /// @@ -269,22 +286,6 @@ extern "C" fn free_callback(obj: *mut bindings::drm_ge= m_object) { } } =20 -// SAFETY: Instances of `Object` are always reference-counted. -unsafe impl crate::types::AlwaysRefCounted for Object { - fn inc_ref(&self) { - // SAFETY: The existence of a shared reference guarantees that the= refcount is non-zero. - unsafe { bindings::drm_gem_object_get(self.as_raw()) }; - } - - unsafe fn dec_ref(obj: NonNull) { - // SAFETY: `obj` is a valid pointer to an `Object`. - let obj =3D unsafe { obj.as_ref() }; - - // SAFETY: The safety requirements guarantee that the refcount is = non-zero. - unsafe { bindings::drm_gem_object_put(obj.as_raw()) } - } -} - impl super::private::Sealed for Object {} =20 impl Deref for Object { --=20 2.48.1