From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384958; b=oztz9SpGckaYsdCCLTnWSI7sZt1aPHMlGtuHUymNFkHGvcjfY47ghMWPVpGYEwUYHjTa AG0hiJn7RfGHk9yY59eQ1eYy/UEy+OscnJU92bao+jJLdLMQFiwUv9KReuZrfC94156Jo /lHrU9h3ekK4oE3ojiHO18yC8avkia2qJSmb34dxfRJmAtL4NI3O6iSnxHjP6rQNY96mG LIHzsC1cXU/OGPfdHZvQFkgfMhOrMdyePdmK6Mwu0b90/nt3Ys5nZp9Jyr0Zi1IAjKGPz aUvbnxl1ZH3cJBrqkMGPL3LzC8cznuTzW2w5e9T/9IagbXqCi1bA2cRMbCMSc0C5+cg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384958; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=qleU6ZaeHuOLv8EzzyKhumqvN9ZdZUwsDL7QJ97SNkc=; b=LiZmsgZvBYGdcLgac227dmR92o93UAa9OQgiMMMKwRVRp40JOeeggzaweNxX824Kj/lc TtZ9ZScqOlMPnJgyFOvMw6oc8Dsb7djoH51w5v5AHjoHfpOUzqvWU/y21alCELHVkFrL8 fZ+8nHt30TUWOCAciBkimf7/GG8rR0p6uBw6qjr8DYjQCJV8sw8Xg7hs9KaLgg8k8/1Gy 63ymKymDDLUwouajWQupxg2PFrePJyYYGyFN1PxdApEe8tM0NDAwpVQvTilIw4QMjvSpf dTo5qa5pvFmoiTffK2nBZWkNxD/QpbTRs3Pgyvo8e+aAEOMdWp9ddXZLm+qar6pcghQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KPI9CO8n; arc=none smtp.remote-ip=209.85.208.71 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2551606A332 for ; Fri, 4 Dec 2026 11:49:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2551606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KPI9CO8n Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-6ba91bdc4abso699373a12.0 for ; Fri, 04 Dec 2026 03:49:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796384956; x=1796989756; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=qleU6ZaeHuOLv8EzzyKhumqvN9ZdZUwsDL7QJ97SNkc=; b=KPI9CO8ncyqewZKJPAoGAKwocQ9QYMo0xg3AEkuS/tziLi/NujfyfFv5MR/FUmqcnR a5/bC3x3LrDDR5FPTaLUp0ZYs/2+MIzQfDUlVbzPXYx3ayBtL1+rNEXDbp6YPFETU2++ bGzvgv+XSZyrmyh01Eca0JqsgmHP+nrEEkchrlW7mZsgTiwx28+yzG3LZGdftARQlKy0 mDh72PlhYsu7uxvCPO6KOP3tD75+/obmeM4ksrYzHuLFJxymMGZ9Dx+60rmjTeTnbnKN 54ghCJJzzPhbCMsGSyTMLqoDqMCVD7DrJr5NY8Yz7NZXTBVxmYg9blstjfFlQ94L2gp1 YdUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796384956; x=1796989756; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qleU6ZaeHuOLv8EzzyKhumqvN9ZdZUwsDL7QJ97SNkc=; b=Tg55Bw08j82lCwijCGcTSSC8z2cb7IXlxR3nDwgbE/SLNOYT6HtVnIf+8xCZaMH+IU yAeXhrelbZOUUorxqto3UpexnA++o1DNoNgBU8xiqKZk6tFArMJAV4Ljc8/srwAgfBAV YWmyfEgReifD1Qta3HPbOWMxeC0gyBR+aBVRrzcG7QDghflUG/lSB+E5QIOemSz29BZs oH3vgnci7vxb1fu5Uh6dqUfunCbBo2a+aw+Ib4gsBSJiiYdU9j/OkrR/oORnWtJxM+r8 Z26RxRtY15Awi5NKBoUORz4Mfmia4wrRDah8ryh6rSDldPDgs3hn7ZysoXg40JJRdzB9 ZiOA== X-Forwarded-Encrypted: i=1; AL+hErEje2KByjEMHk8Lmv58WG5/Zh24KT+EPjHrEqe57ytSvNs1OnJ5t51LkqMv3TNisfvpHr/vzT5y/L/9Dc0=@vger.kernel.org X-Gm-Message-State: APuU4Z/vw4KqLYdyp4vkhCpvQ90aFG1WeiBkzLI71NvIX3uavByctGvj vcBG9x5UzJyUaJIe0IBmo24Xaw5L0aSlnImtmDBHm/yvM8WhpSr9u3BHVg7ehYz2EzVsxuKjtWx a87mQdTVu0jkPJ3mPxA== X-Received: from ejvi3.prod.google.com ([2002:a17:906:4ec3:b0:c51:a9b5:4329]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:86aa:b0:c51:24a3:208 with SMTP id a640c23a62f3a-c5262473941mr116140966b.9.1796384956076; Fri, 04 Dec 2026 03:49:16 -0800 (PST) Date: Fri, 4 Dec 2026 11:49:14 +0000 In-Reply-To: <20261203-rust-drm-gem-shmem-imported-sgt-v3-5-8ca393e83e20@jannau.net> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261203-rust-drm-gem-shmem-imported-sgt-v3-0-8ca393e83e20@jannau.net> <20261203-rust-drm-gem-shmem-imported-sgt-v3-5-8ca393e83e20@jannau.net> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204114914.2151379-1-aliceryhl@google.com> Subject: Re: [PATCH v3 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects From: Alice Ryhl To: Janne Grunau Cc: Alice Ryhl , Miguel Ojeda , Boqun Feng , Gary Guo , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , "=?UTF-8?q?Onur=20=C3=96zkan?=" , David Airlie , Simona Vetter , Sami Tolvanen , Lyude Paul , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Deborah Brouwer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev, stable+noautosel@kernel.org Content-Type: text/plain; charset="UTF-8" On Thu, Dec 03, 2026 at 11:14:09AM +0100, Janne Grunau wrote: > GEM shmem objects imported through gem_prime_import_sg_table wrapping > drm_gem_shmem_prime_import_sg_table() do not own their SG table. > SGTableMap::drop() must not free it since it will be freed on exporter > side once the DMAbuf is not used anymore. > > Reviewed-by: Alice Ryhl > Fixes: 616c229ab010 ("rust: drm: gem: Introduce shmem::Object::sg_table()") > Cc: stable+noautosel@kernel.org # no in-tree users > Tested-by: Deborah Brouwer > Signed-off-by: Janne Grunau The code change itself looks good to me. Could we also update the `# Invariants` and doc comments on `Object` and `SGTableMap` to document imported objects? Something along the lines of: diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs --- a/rust/kernel/drm/gem/shmem.rs +++ b/rust/kernel/drm/gem/shmem.rs @@ -98,6 +98,11 @@ /// /// - `obj` contains a valid initialized `struct drm_gem_shmem_object` for the lifetime of this /// object. +/// - If `self.is_imported()` is true, `(*self.obj.get()).sgt` was initialized at import time to a +/// valid, pinned, DMA-mapped `struct sg_table` that remains valid and unmodified until +/// `drm_gem_shmem_release()` tears down the `dma_buf` attachment in `free_callback`. +/// - If `self.is_imported()` is false, `(*self.obj.get()).sgt` is owned by `sgt_res` (initialized +/// at most once by `SGTableMap::new()` and unmapped/freed when `SGTableMap` is dropped). #[repr(C)] #[pin_data] pub struct Object { @@ -105,7 +110,8 @@ obj: Opaque, /// Parent object that owns this object's DMA reservation object. parent_resv_obj: Option>>, - /// Devres object for unmapping any SGTable on driver-unbind. + /// Devres object revoking SGTable access on driver-unbind (and unmapping it for non-imported + /// objects). sgt_res: ManuallyDrop>>>, #[pin] /// Lock for protecting initialization of `sgt_res`. @@ -582,14 +588,18 @@ /// A reference to a GEM object that is known to have a mapped [`SGTable`]. /// -/// This is used by the Rust bindings with [`Devres`] in order to ensure that mappings for SGTables -/// on GEM shmem objects are revoked on driver-unbind. +/// This is used by the Rust bindings with [`Devres`] to revoke Rust access to a GEM shmem object's +/// [`SGTable`] on driver-unbind. For non-imported objects, dropping this value also unmaps and +/// frees the underlying `struct sg_table`; for PRIME-imported objects, the `dma_buf` attachment +/// mapping is torn down by `drm_prime_gem_destroy()` when the GEM object is freed. /// /// # Invariants /// -/// - `self.obj` always points to a valid GEM object. -/// - This object is proof that `self.obj.owner.sgt_res` has an initialized and valid pointer to an -/// [`SGTable`]. +/// - `self.obj` always points to a valid [`Object`] for the lifetime of `self`. +/// - `(*self.obj.as_ref().as_raw_shmem()).sgt` points to an initialized, valid `struct sg_table` +/// that is not modified or freed for the lifetime of `self`. +/// - If `!self.obj.as_ref().is_imported()`, `self` owns `(*self.obj.as_ref().as_raw_shmem()).sgt` +/// and is responsible for unmapping and freeing it on drop. /// /// [`SGTable`]: scatterlist::SGTable pub struct SGTableMap { @@ -616,21 +626,27 @@ } let _lock = DmaResvGuard::new(obj); - // SAFETY: We acquired the lock needed for calling this function above - // and the GEM object is not imported. + // SAFETY: + // - We acquired the `dma_resv` lock above. + // - Per our type invariants, since `!obj.is_imported()`, `self` owns `shmem->sgt` and it + // is valid and initialized. unsafe { bindings::__drm_gem_shmem_free_sgt_locked(obj.as_raw_shmem()) }; } } impl SGTableMap { fn new(obj: &Object) -> impl Init { - // INVARIANT: - // - We call drm_gem_shmem_get_pages_sgt below and check whether or not it succeeds, - // fulfilling the invariant of SGTableMap that the object's `sgt` field is initialized. // SAFETY: // - `obj` is fully initialized, making this function safe to call. from_err_ptr(unsafe { bindings::drm_gem_shmem_get_pages_sgt(obj.as_raw_shmem()) })?; + // INVARIANT: + // - `SGTableMap` is only stored in `obj.sgt_res` (initialized at most once under + // `obj.sgt_lock` and dropped in `free_callback` before `obj` is released), so + // `self.obj` remains valid for the lifetime of `self`. + // - `drm_gem_shmem_get_pages_sgt()` succeeded above: for imported objects this reuses + // the pre-populated `sgt` (valid until `drm_gem_shmem_release()`); for non-imported + // objects it allocates and DMA-maps `sgt` exclusively for `self`. Ok(Self { obj: obj.into() }) } } Alice