All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Janne Grunau" <j@jannau.net>, "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Sami Tolvanen" <samitolvanen@google.com>,
	"Lyude Paul" <lyude@redhat.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Philipp Stanner" <phasta@kernel.org>
Cc: dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev,
	stable+noautosel@kernel.org,
	"Deborah Brouwer" <deborah.brouwer@collabora.com>,
	media-ci@linuxtv.org
Subject: Re: [PATCH v4 5/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback
Date: Sat, 05 Dec 2026 13:33:46 +0000	[thread overview]
Message-ID: <sashiko-outbox-251579@kernel.org> (raw)
In-Reply-To: <20261205-rust-drm-gem-shmem-imported-sgt-v4-5-f6ccbdafbb76@jannau.net>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] rust: drm: shmem: narrowed provenance in
  prime_import_sg_table_callback()
- [Low] rust: drm: shmem: no INVARIANT comment in
  prime_import_sg_table_callback()
--

commit 93fbf9fffcbe36057059749efd20c7a4b1a389f5
Author: Janne Grunau <j@jannau.net>

rust: drm: gem: shmem: Implement prime_import_sg_table callback

This commit implements a Rust gem_prime_import_sg_table callback for
shmem GEM objects to avoid C-side object allocation and teardown, and
adds a create_imported() constructor to DriverObject for Nova, Tyr, and
the KUnit test driver.

Link: https://lore.kernel.org/rust-for-linux/20261118114102.490384-1-aliceryhl@google.com/

> diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
> index 41f31adb23717..e0af54eb36564 100644
> --- a/rust/kernel/drm/gem/shmem.rs
> +++ b/rust/kernel/drm/gem/shmem.rs

[ ... ]

> @@ -346,6 +347,67 @@ pub fn new(
>          Ok(obj)
>      }
>  
> +    /// Create a new shmem GEM object from another driver's scatter/gather table of pinned pages.
> +    ///
> +    /// This is a Rust re-implemetation of `drm_gem_shmem_prime_import_sg_table()` to avoid
> +    /// creating and destroying `Self` in C code.
> +    /// This function imports a scatter/gather table exported via DMA-BUF by another driver.
> +    /// It should be only called via the &drm_driver.gem_prime_import_sg_table callback.
> +    extern "C" fn prime_import_sg_table_callback(
> +        raw_dev: *mut bindings::drm_device,
> +        raw_attach: *mut bindings::dma_buf_attachment,
> +        raw_sgt: *mut bindings::sg_table,
> +    ) -> *mut bindings::drm_gem_object {

[ ... ]

> +        let new: Pin<KBox<Self>> = match KBox::try_pin_init(
> +            try_pin_init!(Self {
> +                obj <- Opaque::init_zeroed(),
> +                parent_resv_obj: None,
> +                sgt_res: ManuallyDrop::new(SetOnce::new()),
> +                sgt_lock <- new_mutex!(()),
> +                inner <- T::create_imported(dev, size),
> +            }),
> +            GFP_KERNEL,
> +        ) {
> +            Ok(new) => new,
> +            Err(e) => return e.to_ptr(),
> +        };

[Severity: Low]
Object<T> documents an "# Invariants" section covering the initialized
drm_gem_shmem_object and the imported sgt field.  Per the kernel Rust
coding guidelines, should constructing Self here carry an // INVARIANT:
comment explaining how those invariants are satisfied?

[ ... ]

> +        // SAFETY: We never move out of `self`. Ownership of the allocation is transferred to the
> +        // C side.
> +        let new = KBox::into_raw(unsafe { Pin::into_inner_unchecked(new) });
> +
> +        // SAFETY: `new` was just produced by `KBox::into_raw` and so is a valid pointer to an
> +        // initialized `Self`. `obj.base` is the embedded `drm_gem_object` the caller expects.
> +        unsafe { &raw mut (*(*new).obj.get()).base }
> +    }

[Severity: Medium]
Does (*new).obj.get() narrow pointer provenance to the obj field here?

Opaque::get() takes &self, so (*new).obj.get() creates a temporary
shared reference restricted to the obj subobject.  When C later passes
the returned pointer to Object::free_callback(), container_of! accesses
sibling fields and frees the full Object<T> via KBox::from_raw().

Under Rust's aliasing models that is undefined behavior.  Using
&raw mut (*Opaque::cast_into(&raw const (*new).obj)).base avoids the
intermediate reference and preserves the full allocation provenance.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-rust-drm-gem-shmem-imported-sgt-v4-0-f6ccbdafbb76@jannau.net?part=5

      reply	other threads:[~2026-12-05 13:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:17 [PATCH v4 0/5] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
2026-12-05 13:17 ` [PATCH v4 1/5] rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() Janne Grunau
2026-12-05 13:17 ` [PATCH v4 2/5] rust: drm: gem: add an is_imported() accessor Janne Grunau
2026-12-05 13:17 ` [PATCH v4 3/5] rust: drm: gem: shmem: Do not free SG table of imported objects Janne Grunau
2026-12-05 13:17 ` [PATCH v4 4/5] drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too Janne Grunau
2026-12-05 13:17 ` [PATCH v4 5/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback Janne Grunau
2026-12-05 13:33   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251579@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=christian.koenig@amd.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=deborah.brouwer@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=j@jannau.net \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=media-ci@linuxtv.org \
    --cc=mripard@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=samitolvanen@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=simona@ffwll.ch \
    --cc=stable+noautosel@kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=tzimmermann@suse.de \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.