From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=209.85.128.52 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796394112; b=v4HicyHrWzK98klu+4Ga2HLFC8wND8z5UBT9EKRwwSWpC4ESinEMf4oH8t0R7wgpcFU8 cEqB92Don8NWp8zsyANvYMaEGX3tNgYMq59raXaCas4gWis5q9qeufhSV9rEkpb0K1ZSG SB6hNGB5bcAC39OsQQ+dPvNLm1ats9Z+k4YTjaWWya0dNwSO5UDucoAbj1cZ1E94wIHtL hU9ZBorEZCIjInuiBMKFWPXJxzyEX4X7Yk4jUS5jpy1zXU+vaFeXalK5I9Liam7gkdJkG xfsKINhBgWHZffwQWJESr7lHiR+EIN7/hmtsEivR7guEV29JqToN24aU66G32FfCCXQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796394112; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: MIME-Version:References:In-Reply-To:From:Date:X-Gm-Features: Message-ID:Subject:To:Cc:Content-Type:Content-Transfer-Encoding; bh=ZJnNEA8B7j3U7qRhGjylwa4C96/pUoSdPsDqc7fsOkI=; b=35xE3VbP0Somzg4IL0SXbXKRggdaLJ2iDKpZ2G1+vEJe/NCkZUKXY0YBh3nW+dX4MvSv VMuZY2KkdvC8qhtS8DiQHDZ4Ms6OkwTVBOPFNRBOgebrtLark3BmP5fJ5+SJPj0EDGlvJ TqKBrpChtcBMd6Dp8LpwNVlwhlFVX49YC8Zc8knOBS5I+masZgPW01o8K1CWjwziiWIBm MX3iD4pc20PAN2keiuh3fC3cfcR7cXbJvIEMretZJewxd+oZOtrR3Z1CWHtUhtW17YJNA inJPhy6aUFKY/c3BgaVQLvCSoEHZh/t94OcMreLjKq6No8f9u1UDTdLpJDMlAS61Vag== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=DACQTEHg; arc=pass header.oldest-pass=0 smtp.remote-ip=209.85.128.52 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 793F7606A322 for ; Fri, 4 Dec 2026 14:21:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 793F7606A322 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=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=DACQTEHg Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4b79502c27aso8941005e9.1 for ; Fri, 04 Dec 2026 06:21:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1796394111; cv=none; d=google.com; s=arc-20260327; b=gKaEJcJjheKnNCp0reERsoKUA+65DsnUsxz7ly/mxSJGA3fUacxEM47ZON07MsxmGc KOWqw3yojEat7VvwVhe08yuxbOGQBowYjVUAVvqDfH7k7IwylfYF3e9WaWA41e1/PGsb yAaOKuTBmUD3cqQdOmZeWmophLe8D9/eW8oqFTLXSBkBIb7KakOeWvSljDo16Sjk+HgC JL5xZec5poqg+rvRFXZ7ag0E80nh9iEFI9WFxGi83D4ohvDrKsklzeoktL9pVHr+iuUl aYhx2UB0RJTJFtFoTw/bEWbUXDWATWlHssQzc0D5uiscUqybgRYAec0bb3HlI5HX2ITx iBTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=ZJnNEA8B7j3U7qRhGjylwa4C96/pUoSdPsDqc7fsOkI=; fh=abjdXzN5uTwk7Spdo5m5EAZkKdpnbU35ZbiZbNAR7Io=; b=VBVgBzo6vDcg8lH5nMbJvfynj+ZxKfeXjGyYs0jxOZrDYu88in4v9cGqYfVjmeRmVZ 8Q/gmHyUU9V2DWhGcAl5+3K8kxEfTIH5XMsld58RPrLU9Kx1Sd70CnlufKd0ecstSQbV tjN9ysOz8QFOcRy+q3QrHdn6lfsTU52xtfiiS3BcGDc4XdB49mSkWVB7cFxq1YjXy9hT Qe3lquWwG7vX92/DHLnMiyrgUtqHEeAITZX17j3Sbfeqe9RnATb+yNjJOAS0NQuVu6oJ IHrHlIrHf7+XrVTmj+3PJl8bTwReBQk13jwff7j+gR5ChT+nEJMUztX5H8XIf4QriWXY pIeg==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796394111; x=1796998911; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ZJnNEA8B7j3U7qRhGjylwa4C96/pUoSdPsDqc7fsOkI=; b=DACQTEHgQWd7o98Gu7vC6WIWx72Q6QY+wVZojmIqN+ia7/LzfOx8pBOE/d/JxuOh7T yGoZ66vdZLeWWrr/bvZKScdxDZ5tF5DMHTJBdJwrjgXaCvM9XIeywlVhpvcmccIyt9WK j/vtp9RjHX9FJ+OSmhQAU177Bt8iozSd0gBgh9dUaU2NzqNTIvL8a3yLPJpurZ91jo1v UubVkPYEMEakSyYgwYguoHA/xK4FzXhf5ttVUBwS8u1yhZ3IFiTdyS6ys7+VSmP8cB0m YZ5F6WdlN7+socFRlZ4lCTWjCADzP3w+8+TWBxwcpmy2c97WIozbFax4GZmfCoROlwgx QbyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796394111; x=1796998911; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ZJnNEA8B7j3U7qRhGjylwa4C96/pUoSdPsDqc7fsOkI=; b=GTdCUNr7mqbnUjcM358NFekzlxQPQPmxHZL6sGH3a97Dgah3ouuSQ+eehDqE2t7FRb eqP9b6zfDnGQAMec3hjd/9Hn5yigYvm71/m8XwLteJNecYSjxCKx3QFFFid1K84PDZNQ IuKVWmMwfNsxOleUK/i9uC0ArobhGOfsSDKDkcpaB2eh037H98R9W1IERAgnlp3EthiP ZDn3DZlLrPfrO4I0HdhayjRVOCuyXk4VJaT0eFg+Thl3Hiw7N3TKbp3KYLp6F3bMmSEg kz6f8jw88Z9xbjiVq84Wr372DtpZEE7qy6GoWNK5zlBxEkUUCLXVY4YOI6BcSN3SuqNR OtyA== X-Forwarded-Encrypted: i=1; AL+hErGGwAZqNk38R2AoEkTUDnyo8r36xWgIUBM6DQuk+qczSyOceiGpXAr1z7dXpWD2upOA0hponWO9ux/w5eU=@vger.kernel.org X-Gm-Message-State: APuU4Z/paffxvvskX/poZuXlqG6Z0EB6lnDYKni5smP45RRL4kicnzQS Lk83jH0v1YO2fD6Q6jZ81/ak9mj5mZCjizBX1WCcJFk+7S6F4uAwGkbwb86vsYN0XDEgMwSm4HW C5maciy8ZVoBWRGnOTW8I41T3Zk/0VxkP4mz2DuBY X-Gm-Gg: AZFdTZd2KYekzZO1lxp0Ykp5d1AoKxDm3IHsqkbnXEb99KI7lJbmJTfnaQQyzYI7rnN KFk1MFK+JgQis40t/4ZEyF0XRDkURBdhlheGxeUJe800GRGKnN3PP/PkcrJ23ZDQdHEfu/KBIhQ zXsbBcWlclXlsSz04zRlEIzyyirf8RaZRfOL66F7hzBqWdFo5T1+7Dg+/xkIrVFxahKVmQN6Qli aLxQdyIjId0QbFw4lwtRlC0vTjTxO/i4soIeE1ena078+rAVB3PJNhyYLN21/EQ6YjB+hK84YB4 IpI7SZp/zFLHVDhi5ta/dKDB1t5NVqK+Jg9VWoQ9I9eyWYR4PcK49JxZGafTBT98dzORQbzqtoF IPkU/imrluGOlfhx+m3kuVmyjn6I+MKc37wFnWX+3556JK+SknO40bUzYSVpYEeZAXSAx2UKpm3 dy1kS9g5madfAeLL7eF/nJjmofCu880KfpSTLX3W/zBMI= X-Received: by 2002:a05:600c:800e:b0:4ba:6d8:dfad with SMTP id 5b1f17b1804b1-4bbeb8cb08emr39599935e9.14.1796394110211; Fri, 04 Dec 2026 06:21:50 -0800 (PST) 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> <20261204114914.2151379-1-aliceryhl@google.com> <20261204135108.GB6047@robin.jannau.net> In-Reply-To: <20261204135108.GB6047@robin.jannau.net> From: Alice Ryhl Date: Fri, 4 Dec 2026 15:21:36 +0100 X-Gm-Features: Aa3M54qfrxRl2xKeEciQPPGgdkLyfNfyb560x4_qLht08bDrXKd3X2DXzFA7a-U Message-ID: Subject: Re: [PATCH v3 5/5] rust: drm: gem: shmem: Do not free SG table of imported objects To: Janne Grunau Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?Q?Onur_=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" Content-Transfer-Encoding: quoted-printable On Fri, Dec 4, 2026 at 2:51=E2=80=AFPM Janne Grunau wrote: > > On Fri, Dec 04, 2026 at 11:49:14AM +0000, Alice Ryhl wrote: > > 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 exporte= r > > > side once the DMAbuf is not used anymore. > > > > > > Reviewed-by: Alice Ryhl > > > Fixes: 616c229ab010 ("rust: drm: gem: Introduce shmem::Object::sg_tab= le()") > > > 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: > > yes, thanks for the suggested changes. > > > diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.r= s > > --- 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 ini= tialized at import time to a > > +/// valid, pinned, DMA-mapped `struct sg_table` that remains valid a= nd 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 own= ed by `sgt_res` (initialized > > +/// at most once by `SGTableMap::new()` and unmapped/freed when `SGT= ableMap` 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 un= mapping 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 [`SGTab= le`]. > > /// > > -/// This is used by the Rust bindings with [`Devres`] in order to ensu= re 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 a= ccess to a GEM shmem object's > > +/// [`SGTable`] on driver-unbind. For non-imported objects, dropping t= his 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 obj= ect is freed. > > /// > > /// # Invariants > > /// > > -/// - `self.obj` always points to a valid GEM object. > > -/// - This object is proof that `self.obj.owner.sgt_res` has an initia= lized 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 initializ= ed, 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.a= s_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 =3D DmaResvGuard::new(obj); > > > > - // SAFETY: We acquired the lock needed for calling this functi= on above > > - // and the GEM object is not imported. > > + // SAFETY: > > + // - We acquired the `dma_resv` lock above. > > + // - Per our type invariants, since `!obj.is_imported()`, `sel= f` 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 wheth= er 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(ob= j.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 impo= rted objects this reuses > > + // the pre-populated `sgt` (valid until `drm_gem_shmem_relea= se()`); for non-imported > > + // objects it allocates and DMA-maps `sgt` exclusively for `= self`. > > Ok(Self { obj: obj.into() }) > > } > > } > > The changes look good. If you want to apply them as separate commit: > > Reviewed-by: Janne Grunau > > or I can fold them into the relevant changes and send a new version. I think it's a bit big of a change to make on apply, so I'd appreciate a new version. Alice