From nobody Wed Sep 10 01:53:14 2025 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 B11892EA166 for ; Mon, 8 Sep 2025 22:07:16 +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=1757369238; cv=none; b=GN/e3YCp1xoRdUTyfwnPJO6gjn2IG3LErRRuVlVIhFdgf6o07LuGAGUEN2o/iYDAYurpdh3CmMW3DSCA+3RI8c8wYxVb0/iCKC3dx7rTLaWvXq27vAMp38ipLopfoaqzWVOjprAwpBbeP1swnO7WSbgjyQLwddBnbMyX16mQMUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757369238; c=relaxed/simple; bh=VTyyObHR88FDo90+g9Ql1CNb2rsAg3EyIbgbO6VqvY8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ChuDHG2bgg1k3x4A+JiPoT9P7Axf8U6kGwe3sbpzP2nnfq1rZzldse5OAB449O+N1+EyBlvDE3HOJqSLQ6amLU/5Jo5ZzTOharCXZHCh2zDtp/YaDpQupQ99+ef2d/Anhsr2pDubAMUv5B+z2a0858mr5MaHse7yZlzRepQGyzA= 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=UYa6ViDL; 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="UYa6ViDL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757369235; 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=dKR3M+u2UbhgI9ZzLhPjRhOHXsTKC9RJaF9aymRCG+M=; b=UYa6ViDLN9opQoETBZUPeiohX5HbkXUDAPqoywOOnhU1Ji6Rh0U7KbIsOrDqocmWFWTsBX uUltcWFFDuZSo5XQqh37bWb94xrQb8QVoSghU1zY+/lA6e3W5UWb2ro6R2h3b0/or1UUgS 9BT0q4jNW8+x+KhpZEYEZgWlXvSLK+o= 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-144-3ydI1IEgPEucyGfw7RBkRQ-1; Mon, 08 Sep 2025 18:07:13 -0400 X-MC-Unique: 3ydI1IEgPEucyGfw7RBkRQ-1 X-Mimecast-MFC-AGG-ID: 3ydI1IEgPEucyGfw7RBkRQ_1757369230 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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E507C1800365; Mon, 8 Sep 2025 22:07:09 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.64.41]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BBBC41800452; Mon, 8 Sep 2025 22:07:05 +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 , Daniel Almeida , Asahi Lina Subject: [PATCH 1/2] Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all gem objects automatically" Date: Mon, 8 Sep 2025 18:04:44 -0400 Message-ID: <20250908220657.165715-2-lyude@redhat.com> In-Reply-To: <20250908220657.165715-1-lyude@redhat.com> References: <20250908220657.165715-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" I made a very silly mistake with this commit that managed to slip by because I forgot to mzke sure rvkms was rebased before testing my work last - we can't do blanket implementations like this due to rust's orphan rule. The code -does- build just fine right now, but it doesn't with the ongoing bindings for gem shmem. So, just revert this and we'll introduce a macro for implementing AlwaysRefCounted individually for each type of gem implementation. Note that we leave the IntoGEMObject since it is true that all gem objects are refcounted, so any implementations that are added should be implementing AlwaysRefCounted anyhow. This reverts commit 38cb08c3fcd3f3b1d0225dcec8ae50fab5751549. Signed-off-by: Lyude Paul --- rust/kernel/drm/gem/mod.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index fd872de3b6695..af92f2d46d8d8 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -54,26 +54,6 @@ pub trait IntoGEMObject: Sized + super::private::Sealed = + AlwaysRefCounted { unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a= Self; } =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_raw()) }; - } - - 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_raw(); - - // 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) }; - } -} - extern "C" fn open_callback( raw_obj: *mut bindings::drm_gem_object, raw_file: *mut bindings::drm_file, @@ -272,6 +252,22 @@ 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.51.0 From nobody Wed Sep 10 01:53:14 2025 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 D5A0C2E2DD4 for ; Mon, 8 Sep 2025 22:07:25 +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=1757369247; cv=none; b=tZscaj/5jnrqJ+cWvOG/NmJR10RQmhzXwN3CXu3MnUwUtSw0dhZ1hBv9mWtQH9RrnvJUAzq316gqEvqQpOzAG2Ssu6slDofeFhcQqmAYz2SuT2jI5HpB/P4/avqjsnx5tAYJC6HQ/45MozG3+xYtXiVXfSJJhKWuCTbr2SKsuyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757369247; c=relaxed/simple; bh=hc/nevHocOJ876jD2SRmqY5GYnp5OLlh9aqg2Gbf+DA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ai6cKh76qpGfzM0R9K1GKuM8EPfN7/3Mk2mU7/+NcWzp6upIGxiQiRV/TCHqPHQczXvb0eDqZqZJkQ1+WdbXesETC6uHEGepsFxuunvoPvWiAwrFDMVSwlGfmkJ+OQF82k3JQS9VTM9zs9fi0Go2XPdn7gSTnYS3rLWequKKufo= 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=fgHh5ogG; 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="fgHh5ogG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757369244; 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=b2wPtiNmJME90sAyrYUjZU76jbZvcVUh0oGD0p8TgMU=; b=fgHh5ogGmstX1whwcgzwkDM/Va+ksXT365Tk5McD7VaNTebw0wcVHnIRW2ygOSIfgrW9PQ nsm4uiky0HArR4npVrIb7sIuCew1IER0yvWCOz6MOX1mgy9AyFJs+bhQ+Sq/gfvqd0YUWd Ed9vBnrdh4QU0nQJX/0pqvPuW7olfqI= 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-614-nYeROWUjPWeC8K3Twj_F2Q-1; Mon, 08 Sep 2025 18:07:20 -0400 X-MC-Unique: nYeROWUjPWeC8K3Twj_F2Q-1 X-Mimecast-MFC-AGG-ID: nYeROWUjPWeC8K3Twj_F2Q_1757369236 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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8F6821800292; Mon, 8 Sep 2025 22:07:16 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.64.41]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C57331800452; Mon, 8 Sep 2025 22:07:12 +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 , Daniel Almeida , Asahi Lina Subject: [PATCH 2/2] rust/drm: Add gem::impl_aref_for_gem_obj! Date: Mon, 8 Sep 2025 18:04:45 -0400 Message-ID: <20250908220657.165715-3-lyude@redhat.com> In-Reply-To: <20250908220657.165715-1-lyude@redhat.com> References: <20250908220657.165715-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" In the future we're going to be introducing more GEM object types in rust then just gem::Object. Since all types of GEM objects have refcounting, let's introduce a macro that we can use in the gem crate in order to copy this boilerplate implementation for each type: impl_aref_for_gem_obj!(). Signed-off-by: Lyude Paul --- rust/kernel/drm/gem/mod.rs | 53 +++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index af92f2d46d8d8..317edd455cc1c 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -14,6 +14,43 @@ }; use core::{ops::Deref, ptr::NonNull}; =20 +/// A macro for implementing [`AlwaysRefCounted`] for any GEM object type. +/// +/// Since all GEM objects use the same refcounting scheme. +macro_rules! impl_aref_for_gem_obj { + ( + impl $( <$( $tparam_id:ident ),+> )? for $type:ty + $( + where + $( $bind_param:path : $bind_trait:path ),+ + )? + ) =3D> { + // SAFETY: All gem objects are refcounted + unsafe impl $( <$( $tparam_id ),+> )? crate::types::AlwaysRefCount= ed for $type + $( + where + $( $bind_param : $bind_trait ),+ + )? + { + 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: core::ptr::NonNull) { + // SAFETY: `obj` is a valid pointer to an `Object`. + let obj =3D unsafe { obj.as_ref() }; + + // SAFETY: The safety requirements guarantee that the refc= ount is non-zero. + unsafe { bindings::drm_gem_object_put(obj.as_raw()) }; + } + } + }; +} + +pub(crate) use impl_aref_for_gem_obj; + /// A type alias for retrieving a [`Driver`]s [`DriverFile`] implementatio= n from its /// [`DriverObject`] implementation. /// @@ -252,21 +289,7 @@ 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_aref_for_gem_obj!(impl for Object where T: DriverObject); =20 impl super::private::Sealed for Object {} =20 --=20 2.51.0