From: Claude Code Review Bot <claude-review@example.com>
To: dri-devel-reviews@example.com
Subject: Claude review: rust: devres: return reference in `devres::register`
Date: Mon, 16 Mar 2026 12:17:35 +1000 [thread overview]
Message-ID: <review-patch1-20260313-rust_serdev-v3-1-c9a3af214f7f@posteo.de> (raw)
In-Reply-To: <20260313-rust_serdev-v3-1-c9a3af214f7f@posteo.de>
Patch Review
This patch changes `devres::register` to return `Result<&'a T>` instead of `Result`, tying the returned reference's lifetime to the `Device<Bound>` reference.
**Critical safety concern with the lifetime reasoning:**
```rust
+pub fn register<'a, T, E>(
+ dev: &'a Device<Bound>,
+ data: impl PinInit<T, E>,
+ flags: Flags,
+) -> Result<&'a T>
```
The SAFETY comment states:
```rust
+ // SAFETY: `dev` is valid for the lifetime of 'a. As long as there is a reference to
+ // `Device<Bound>`, it is guaranteed that the device is not unbound and data has not been
+ // dropped. Thus `data_ptr` is also valid for the lifetime of 'a.
```
The issue is that `data` is moved into `register_foreign` via `KBox::pin_init` and then consumed by `into_foreign()` inside `register_foreign`. The raw pointer `data_ptr` is taken *before* the data is registered with devres. If `register_foreign` fails (returns an error), `data` is dropped, but we never reach the `.map()` so that's OK. However, the lifetime argument is subtle: the devres-managed data could in theory be freed by devres callbacks (e.g., if the device is removed concurrently) while a `&'a T` reference is still held. The safety argument relies on `Device<Bound>` guaranteeing the device won't unbind while the reference exists. This needs careful scrutiny from the Rust-for-Linux maintainers (Danilo, Benno, Alice) to verify the invariant actually holds.
**The callers now discard the return value:**
```rust
- devres::register(dev, Self::new()?, GFP_KERNEL)
+ devres::register(dev, Self::new()?, GFP_KERNEL)?;
+ Ok(())
```
This works but changes the API for all existing callers to require `?;` + `Ok(())` where they previously just returned the `Result`. This is somewhat unfortunate ergonomically. An alternative approach might be a separate `devres::register_ref` function to avoid forcing this pattern on callers that don't need the reference.
---
Generated by Claude Code Patch Reviewer
next prev parent reply other threads:[~2026-03-16 2:17 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-13 18:12 [PATCH v3 0/4] rust: add basic serial device bus abstractions Markus Probst
2026-03-13 18:12 ` [PATCH v3 1/4] rust: devres: return reference in `devres::register` Markus Probst
2026-03-16 2:17 ` Claude Code Review Bot [this message]
2026-03-13 18:12 ` [PATCH v3 2/4] serdev: add rust private data to serdev_device Markus Probst
2026-03-14 8:07 ` Greg Kroah-Hartman
2026-03-14 11:42 ` Markus Probst
2026-03-14 11:52 ` Greg Kroah-Hartman
2026-03-14 12:08 ` Markus Probst
2026-03-14 13:24 ` Alice Ryhl
2026-03-14 13:31 ` Greg Kroah-Hartman
2026-03-14 13:42 ` Danilo Krummrich
2026-03-14 13:49 ` Markus Probst
2026-03-14 13:54 ` Danilo Krummrich
2026-03-14 14:58 ` Markus Probst
2026-03-16 2:17 ` Claude review: " Claude Code Review Bot
2026-03-13 18:12 ` [PATCH v3 3/4] rust: add basic serial device bus abstractions Markus Probst
2026-03-16 2:17 ` Claude review: " Claude Code Review Bot
2026-03-13 18:12 ` [PATCH v3 4/4] samples: rust: add Rust serial device bus sample device driver Markus Probst
2026-03-16 2:17 ` Claude review: " Claude Code Review Bot
2026-03-16 2:17 ` Claude review: rust: add basic serial device bus abstractions Claude Code Review Bot
-- strict thread matches above, loose matches on Subject: below --
2026-03-06 19:35 [PATCH v2 0/4] " Markus Probst
2026-03-06 19:35 ` [PATCH v2 1/4] rust: devres: return reference in `devres::register` Markus Probst
2026-03-08 22:21 ` Claude review: " 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-patch1-20260313-rust_serdev-v3-1-c9a3af214f7f@posteo.de \
--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