public inbox for drm-ai-reviews@public-inbox.freedesktop.org
 help / color / mirror / Atom feed
From: Claude Code Review Bot <claude-review@example.com>
To: dri-devel-reviews@example.com
Subject: Claude review: rust: drm: gem: Introduce shmem::SGTable
Date: Sun, 12 Apr 2026 12:01:18 +1000	[thread overview]
Message-ID: <review-patch4-20260409001559.622026-5-lyude@redhat.com> (raw)
In-Reply-To: <20260409001559.622026-5-lyude@redhat.com>

Patch Review

This is the core Rust abstraction patch. The design — `SGTableMap` as a Devres-managed resource, `SGTable` as an owned handle — is well thought out. However:

**Object destruction ordering (potential use-after-free):** This is the main concern. Consider the normal buffer teardown path where a GEM object is freed while the driver is still bound:

1. `free_callback` calls `drm_gem_shmem_release(shmem)` → frees sgt (sets `shmem->sgt = NULL`), calls `drm_gem_object_release()` (destroys the `dma_resv`)
2. `KBox::from_raw(this)` drops the `Object<T>`, which drops `sgt_res`
3. `Devres<SGTableMap>` is dropped, deregistering the devres action
4. If the `SGTableMap` inside the `Revocable` hasn't been revoked, its `Drop` runs:

```rust
impl<T: DriverObject> Drop for SGTableMap<T> {
    fn drop(&mut self) {
        let obj = unsafe { self.obj.as_ref() };
        unsafe { bindings::dma_resv_lock(obj.raw_dma_resv(), ptr::null_mut()) };
        unsafe { bindings::__drm_gem_shmem_free_sgt_locked(obj.as_raw_shmem()) };
        unsafe { bindings::dma_resv_unlock(obj.raw_dma_resv()) };
    }
}
```

At step 4, `dma_resv` has already been destroyed by `drm_gem_object_release()`, so `dma_resv_lock` is UB. And `shmem->sgt` is NULL, so `__drm_gem_shmem_free_sgt_locked` would dereference NULL.

The driver-unbind case (Case 1) is fine: Devres revokes `SGTableMap` before the object is freed. But the normal-teardown case (Case 2) needs to be addressed. Possible fixes: clear/revoke `sgt_res` in `free_callback` before calling `drm_gem_shmem_release`, or add a NULL guard to `__drm_gem_shmem_free_sgt_locked`.

**`dma_resv_lock` return value ignored:** In `create_sg_table`:

```rust
unsafe { bindings::dma_resv_lock(self.raw_dma_resv(), ptr::null_mut()) };
```

With `ctx=NULL` this effectively can't fail (it's a plain mutex lock), but the return value is silently discarded. A `// SAFETY` comment should note that with ctx=NULL, dma_resv_lock cannot return EDEADLK and thus cannot fail, or use `to_result()` to be explicit.

**`NonNull<Object<T>>` in SGTableMap is a raw pointer without refcounting:**

```rust
pub struct SGTableMap<T: DriverObject> {
    obj: NonNull<Object<T>>,
}
```

The soundness of this depends entirely on the Devres lifecycle guaranteeing that `SGTableMap` is revoked before the Object is freed. Per the destruction-ordering issue above, this invariant can be violated.

**C-style comment syntax in safety comment:**

```rust
// SAFETY: We grabbed the lock required for calling this function above */
```

There's a trailing `*/` from a C-style comment.

---

---
Generated by Claude Code Patch Reviewer

  parent reply	other threads:[~2026-04-12  2:01 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-09  0:12 [PATCH v10 0/5] Rust bindings for gem shmem Lyude Paul
2026-04-09  0:12 ` [PATCH v10 1/5] rust: drm: gem: s/device::Device/Device/ for shmem.rs Lyude Paul
2026-04-10  7:54   ` Alexandre Courbot
2026-04-12  2:01   ` Claude review: " Claude Code Review Bot
2026-04-09  0:12 ` [PATCH v10 2/5] drm/gem/shmem: Introduce __drm_gem_shmem_free_sgt_locked() Lyude Paul
2026-04-10  7:54   ` Alexandre Courbot
2026-04-12  2:01   ` Claude review: " Claude Code Review Bot
2026-04-09  0:12 ` [PATCH v10 3/5] drm/gem/shmem: Export drm_gem_shmem_get_pages_sgt_locked() Lyude Paul
2026-04-10  7:55   ` Alexandre Courbot
2026-04-12  2:01   ` Claude review: " Claude Code Review Bot
2026-04-09  0:12 ` [PATCH v10 4/5] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
2026-04-09 22:57   ` Deborah Brouwer
2026-04-10  7:55   ` Alexandre Courbot
2026-04-12  2:01   ` Claude Code Review Bot [this message]
2026-04-09  0:12 ` [PATCH v10 5/5] rust: drm: gem: Add vmap functions to shmem bindings Lyude Paul
2026-04-11 13:32   ` Alexandre Courbot
2026-04-12  2:01   ` Claude review: " Claude Code Review Bot
2026-04-12  2:01 ` Claude review: Rust bindings for gem shmem Claude Code Review Bot

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=review-patch4-20260409001559.622026-5-lyude@redhat.com \
    --to=claude-review@example.com \
    --cc=dri-devel-reviews@example.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox