From nobody Tue Feb 10 22:15:25 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 0E5B11DB375 for ; Thu, 23 Oct 2025 21:26:06 +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=1761254770; cv=none; b=F0EUhsd2Py4x9BImf8IbfrP0XXtS5nIDhn8nj6T0OkaCqxYRn1GuBk5E3mlWFpUItSw61XGLpzrGFX50iX3pyNnCdDyM7E3X2sav5SJoYYBZ5rBJ4hp12zNpUzPiPnyZngtf/NlHa2mAtH9OPS9TV+KWvEuejOU0uZxxP2LhZxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761254770; c=relaxed/simple; bh=lwDclK5zyxp1awpvPaJAKW/PWA9pPm0C7jX9Z+AxEaU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UPxsmkryh4M6b+DoZkFf0d13ZTG0L8GLUJUuqgEYVS7nuw7XL81sQKrOxpZYRmaKVn1r1JT9hQ/rfnmnpKhcnzz1G+WZMYVLHBu5x/BOYprmdaOBBBcVjQl8InDOESfQyA32NPPCwWjHK1W05QG7A+sDJJdQeXceAUXA02LV5NU= 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=d5JtqhXR; 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="d5JtqhXR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1761254766; 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=tojreo2SCeXW/AFAiQR1a/N+EZ+cx5QocDZTELD04Hw=; b=d5JtqhXRcdiO0EwQXjzRGZU3Vc4mvpeXz75Q5ySlP/2V+w48JHRR7CaPRkvMBQy+3OYBNI WPIt1GXey3v5ACU6XXlUJfE3+9/o4jc6h12HTCuQkcauBxxgZnw4/xRZG/LAt0nDvDp2Iy G2rwpr0phC7mcOzJW4gYVrXHu8vGAac= 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-504-bgxhGK6HM7qRBtyz1apc3Q-1; Thu, 23 Oct 2025 17:26:03 -0400 X-MC-Unique: bgxhGK6HM7qRBtyz1apc3Q-1 X-Mimecast-MFC-AGG-ID: bgxhGK6HM7qRBtyz1apc3Q_1761254761 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 14FFF1800D89; Thu, 23 Oct 2025 21:26:01 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.235]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5975B30002E1; Thu, 23 Oct 2025 21:25:56 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Daniel Almeida , Danilo Krummrich , linux-kernel@vger.kernel.org Cc: David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Asahi Lina , Shankari Anand Subject: [PATCH v5 1/8] rust/drm: Add gem::impl_aref_for_gem_obj! Date: Thu, 23 Oct 2025 17:22:03 -0400 Message-ID: <20251023212540.1141999-2-lyude@redhat.com> In-Reply-To: <20251023212540.1141999-1-lyude@redhat.com> References: <20251023212540.1141999-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" 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 Reviewed-by: Daniel Almeida --- V5: * Move .as_raw() call to `let obj` in dec_ref, to ensure that the reference to object is not live by the time that we call drm_gem_object_put(). * Add missing #[macro_export] annotation rust/kernel/drm/gem/mod.rs | 54 +++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs index 20c2769a8c9d6..32bff2e8463f4 100644 --- a/rust/kernel/drm/gem/mod.rs +++ b/rust/kernel/drm/gem/mod.rs @@ -15,6 +15,44 @@ }; 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_export] +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::AlwaysRefCoun= ted 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() }.as_raw(); + + // SAFETY: The safety requirements guarantee that the refc= ount is non-zero. + unsafe { bindings::drm_gem_object_put(obj) }; + } + } + }; +} + +pub(crate) use impl_aref_for_gem_obj; + /// A type alias for retrieving a [`Driver`]s [`DriverFile`] implementatio= n from its /// [`DriverObject`] implementation. /// @@ -253,21 +291,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