From nobody Wed Dec 17 03:33:59 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 A00D52BEC58 for ; Wed, 21 May 2025 20:48:31 +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=1747860513; cv=none; b=QH+aFZv9eoA7xusB/rDHs9bwMCg9LS0A+V/FaOQvJACvBWy53t5BYl89ffVMgvcPzr4mtiDoXOVWhhkuQmVD4LlEk0r7L6sdHRKSRey0t7fj1ytqf0GfIPsX6urOHFRaj/KodKyvbsnP7Ti/Fc46O64WwdaxlUYfNBt4vhozAAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747860513; c=relaxed/simple; bh=5E4c3+cOe9MfMSRIXJV1QQOv9I46FpcTmGVomGEV9kI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OjsQ4WpqaRLWpiTfGAKku5dJEf7x4HU8mjLtPQl4iru/0gfviBL0MgKbvy2/dE1/lzyYE6Q7cLUE+u3yKObvUXv5V2zvOxTbFSpJ0+rStG1vEdlO9HceamW5RWTQiR659jrS7AIjl4KPUg0TCsbIDYZPn781JZBh9T7dJE0Kmc8= 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=DTlX0vSk; 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="DTlX0vSk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747860510; 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=B07NYKKXvANlIZfpwmKg8iAUTUr0H/iCuFhmRkbUJU8=; b=DTlX0vSk2/tLOZO56N/lrirB9r37fagCAR2O3kIlF9LlOAb4ahFT/u6q3FDZ38wjWrCkmb R9PRXGOGqqL5Vx4DaaiyecDvcF2CHIsr4aWSAtnR5qk53x4IioLcuYFhZerzTFNCHQRz8D L2WAt0dumeTvT0fSJSALYyJPIzitoeI= Received: from mx-prod-mc-04.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-68-YbFIHZ_dMxi94vPwO_OTsw-1; Wed, 21 May 2025 16:48:26 -0400 X-MC-Unique: YbFIHZ_dMxi94vPwO_OTsw-1 X-Mimecast-MFC-AGG-ID: YbFIHZ_dMxi94vPwO_OTsw_1747860503 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A841819560AA; Wed, 21 May 2025 20:48:23 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.80.100]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A2D601956066; Wed, 21 May 2025 20:48:19 +0000 (UTC) From: Lyude Paul To: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-kernel@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 , Asahi Lina , Daniel Almeida Subject: [PATCH v2 09/12] rust: drm: gem: Introduce OwnedSGTable Date: Wed, 21 May 2025 16:29:16 -0400 Message-ID: <20250521204654.1610607-10-lyude@redhat.com> In-Reply-To: <20250521204654.1610607-1-lyude@redhat.com> References: <20250521204654.1610607-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.40 Content-Type: text/plain; charset="utf-8" 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. So to support this, let's introduce OwnedSGTable - 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 OwnedSGTable. The type can be used identically to a normal SGTable. Signed-off-by: Lyude Paul --- rust/kernel/drm/gem/shmem.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs index c38fca715429e..bff038df93334 100644 --- a/rust/kernel/drm/gem/shmem.rs +++ b/rust/kernel/drm/gem/shmem.rs @@ -20,7 +20,7 @@ prelude::*, types::{ARef, Opaque}, container_of, - scatterlist, + scatterlist::SGTable, }; use core::{ mem::MaybeUninit, @@ -172,23 +172,36 @@ extern "C" fn free_callback(obj: *mut bindings::drm_g= em_object) { let _ =3D unsafe { KBox::from_raw(this) }; } =20 - /// Creates (if necessary) and returns a scatter-gather table of DMA p= ages for this object. + /// Creates (if necessary) and returns an immutable reference to a sca= tter-gather table of DMA + /// pages for this object. /// /// This will pin the object in memory. - pub fn sg_table(&self) -> Result> { + #[inline] + pub fn sg_table(&self) -> Result<&SGTable> { // SAFETY: // - drm_gem_shmem_get_pages_sgt is thread-safe. // - drm_gem_shmem_get_pages_sgt returns either a valid pointer to= a scatterlist, or an // error pointer. let sgt =3D from_err_ptr(unsafe { bindings::drm_gem_shmem_get_page= s_sgt(self.as_shmem()) })?; =20 - Ok(SGTable { - // SAFETY: We checked that `sgt` is not an error pointer, so i= t must be a valid pointer - // to a scatterlist. - sgt: NonNull::from(unsafe { scatterlist::SGTable::as_ref(sgt) = }), + // SAFETY: We checked above that `sgt` is not an error pointer, so= it must be a valid + // pointer to a scatterlist + Ok(unsafe { SGTable::as_ref(sgt) }) + } + + /// Creates (if necessary) and returns an owned scatter-gather table o= f DMA pages for this + /// object. + /// + /// This is the same as [`sg_table`](Self::sg_table), except that it i= nstead returns a + /// [`OwnedSGTable`] which holds a reference to the associated gem obj= ect. + /// + /// This will pin the object in memory. + pub fn owned_sg_table(&self) -> Result> { + Ok(OwnedSGTable { + sgt: self.sg_table()?.into(), // INVARIANT: We take an owned refcount to `self` here, ensuri= ng that `sgt` remains // valid for as long as this `OwnedSGTable`. - _owner: self.into() + _owner: self.into(), }) } =20 --=20 2.49.0