From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender3-pp-f112.zoho.com (sender3-pp-f112.zoho.com [136.143.184.112]) (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 4A31D311C1F; Mon, 24 Nov 2025 15:43:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.184.112 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763999006; cv=pass; b=pN+eHWjPTVllfQrO7YTWnm8OXJhFRbr0ltxCWqvEUyYs3aGqLgpc0fMn5jlrR5keXJXNoWnaEg52jRdyeLA0salAAp14J9JnobwBVZVvjWVCVSynFoGoN0ks5Fdo7Jm4BChTodeAxvT3ysFLWyJbgxBWPDypDWvVda2ZCOxZUTY= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763999006; c=relaxed/simple; bh=RqpqOfdIxSw+yL50a8nj6xDKDz0/SyaGKHJpcsRfjhU=; h=Content-Type:Mime-Version:Subject:From:In-Reply-To:Date:Cc: Message-Id:References:To; b=uyYiW1VMY2/4EchUNONhvS+uYtr5tMsCB4p3c3mk5dru2i+tWUHQeIFgsWEzcvBqdVlj4um0WC7XxCWxbCZZtwNlLI7G0UNW5UpmpoKi3oHneGljqz+ap6ZyykEF2+oDRJJjiQ7OnME6wxq1++ArdQ/wLgcDzidBonw73WISsTA= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=RaVtnid6; arc=pass smtp.client-ip=136.143.184.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="RaVtnid6" ARC-Seal: i=1; a=rsa-sha256; t=1763998989; cv=none; d=zohomail.com; s=zohoarc; b=eFmxcoIMxnmeVY5DbhoRsZVhavz51fEOUjrKp6iqCJTQPVCNz+fqBL5LT2yH1jQiJ/xx4bbIIjhMRETBYC73VaMWmAxn/99iQqf/LIpHzuc5uYAVVoxPJdMcjV91ysMJXZ58qHA+DtbLQm6XYMYSMBde2MeLm9jHmfALpvqIsBg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763998989; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=GugiLL3/rtrH8HTV3+2ct9rwL7Gy7JIiT78O4d3h9ho=; b=TiQVB7RkisNUkGrwUgEWuLA6/tbleSZY48e3ChY17EV7I0H15RU+S/Np/DPD7h8+qFgpqVRnYrGIOrpYU85Sf7ug1YVr7/Zd0RSAwCCpC0ZcELdX+k+hVKtVlPKzkStViiEo5dAr9nyr2gVNRjYTGZcaa2pays4jT4MwgRbkOXQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1763998989; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=Content-Type:Mime-Version:Subject:Subject:From:From:In-Reply-To:Date:Date:Cc:Cc:Content-Transfer-Encoding:Message-Id:Message-Id:References:To:To:Reply-To; bh=GugiLL3/rtrH8HTV3+2ct9rwL7Gy7JIiT78O4d3h9ho=; b=RaVtnid6oxmG72uhwNHv7Zof6O9j4whb+6tzMvFioXTsTB9FY5JIiWTcyKmncmZn 9ON0DHNiyOls/Bn4DFQQc9ru2vdoR0uiPIrd1QPeWRKFZYJNpzw4WvfaNnP8YJk1VBc 53N8Nux2H6wNE5qw26p96LIobJsZB7kuyI20zsWw= Received: by mx.zohomail.com with SMTPS id 1763998987629172.66116131321337; Mon, 24 Nov 2025 07:43:07 -0800 (PST) Content-Type: text/plain; charset=utf-8 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: [PATCH v5 6/8] rust: drm: gem: Introduce shmem::SGTable From: Daniel Almeida In-Reply-To: <20251023212540.1141999-7-lyude@redhat.com> Date: Mon, 24 Nov 2025 12:42:55 -0300 Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Danilo Krummrich , linux-kernel@vger.kernel.org, David Airlie , Simona Vetter , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Asahi Lina Content-Transfer-Encoding: quoted-printable Message-Id: References: <20251023212540.1141999-1-lyude@redhat.com> <20251023212540.1141999-7-lyude@redhat.com> To: Lyude Paul X-Mailer: Apple Mail (2.3826.700.81) X-ZohoMailClient: External > On 23 Oct 2025, at 18:22, Lyude Paul wrote: >=20 > Currently we expose the ability to retrieve an SGTable for an shmem = gem > object using gem::shmem::Object::::sg_table(). However, this only = gives > us a borrowed reference. This being said - retrieving an SGTable is a > fallible operation, and as such it's reasonable that a driver may want = to > hold onto an SGTable for longer then a reference would allow in order = to > avoid having to deal with fallibility every time they want to access = the > SGTable. One such driver with this usecase is the Asahi driver. >=20 > So to support this, let's introduce shmem::SGTable - which both holds = a > pointer to the SGTable and a reference to its respective GEM object in > order to keep the GEM object alive for as long as the shmem::SGTable. = The > type can be used identically to a normal SGTable. >=20 > Signed-off-by: Lyude Paul >=20 > --- > V3: > * Rename OwnedSGTable to shmem::SGTable. Since the current version of = the > SGTable abstractions now has a `Owned` and `Borrowed` variant, I = think > renaming this to shmem::SGTable makes things less confusing. > We do however, keep the name of owned_sg_table() as-is. > V4: > * Clarify safety comments for SGTable to explain why the object is > thread-safe. > * Rename from SGTableRef to SGTable >=20 > rust/kernel/drm/gem/shmem.rs | 50 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 50 insertions(+) >=20 > diff --git a/rust/kernel/drm/gem/shmem.rs = b/rust/kernel/drm/gem/shmem.rs > index 45b95d60a3ec7..21ccb6c1824be 100644 > --- a/rust/kernel/drm/gem/shmem.rs > +++ b/rust/kernel/drm/gem/shmem.rs > @@ -173,6 +173,25 @@ pub fn sg_table(&self) -> = Result<&scatterlist::SGTable> { > // pointer to a scatterlist > Ok(unsafe { scatterlist::SGTable::from_raw(sgt) }) > } > + > + /// Creates (if necessary) and returns an owned reference to a = scatter-gather table of DMA pages > + /// for this object. > + /// > + /// This is the same as [`sg_table`](Self::sg_table), except that = it instead returns an > + /// [`shmem::SGTable`] which holds a reference to the associated = gem object, instead of a > + /// reference to an [`scatterlist::SGTable`]. > + /// > + /// This will pin the object in memory. > + /// > + /// [`shmem::SGTable`]: SGTable > + pub fn owned_sg_table(&self) -> Result> { > + Ok(SGTable { > + sgt: self.sg_table()?.into(), > + // INVARIANT: We take an owned refcount to `self` here, = ensuring that `sgt` remains > + // valid for as long as this `SGTable`. > + _owner: self.into(), > + }) > + } > } >=20 > impl Deref for Object { > @@ -223,3 +242,34 @@ impl driver::AllocImpl for = Object { > dumb_map_offset: None, > }; > } > + > +/// An owned reference to a scatter-gather table of DMA address spans = for a GEM shmem object. > +/// > +/// This object holds an owned reference to the underlying GEM shmem = object, ensuring that the > +/// [`scatterlist::SGTable`] referenced by this type remains valid = for the lifetime of this object. > +/// > +/// # Invariants > +/// > +/// - `sgt` is kept alive by `_owner`, ensuring it remains valid for = as long as `Self`. > +/// - `sgt` corresponds to the owned object in `_owner`. > +/// - This object is only exposed in situations where we know the = underlying `SGTable` will not be > +/// modified for the lifetime of this object. Thus, it is safe to = send/access this type across > +/// threads. > +pub struct SGTable { > + sgt: NonNull, > + _owner: ARef>, > +} > + > +// SAFETY: This object is thread-safe via our type invariants. > +unsafe impl Send for SGTable {} > +// SAFETY: This object is thread-safe via our type invariants. > +unsafe impl Sync for SGTable {} > + > +impl Deref for SGTable { > + type Target =3D scatterlist::SGTable; > + > + fn deref(&self) -> &Self::Target { > + // SAFETY: Creating an immutable reference to this is safe = via our type invariants. > + unsafe { self.sgt.as_ref() } > + } > +} > --=20 > 2.51.0 >=20 Same comment as last patch. =E2=80=94 Daniel