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: Thu, 04 Jun 2026 15:18:43 +1000 [thread overview]
Message-ID: <review-patch1-20260530-rust_serdev-v10-1-65d1d5db876c@posteo.de> (raw)
In-Reply-To: <20260530-rust_serdev-v10-1-65d1d5db876c@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 `&'a Device<Bound>`.
The core change:
```rust
+pub fn register<'a, T, E>(
+ dev: &'a Device<Bound>,
+ data: impl PinInit<T, E>,
+ flags: Flags,
+) -> Result<&'a T>
...
+ let data_ptr = &raw const *data;
+
register_foreign(dev, data)
+ // 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.
+ .map(|()| unsafe { &*data_ptr })
```
**Concern: Safety of the returned reference.** The safety comment says "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." This is the critical invariant. After `register_foreign` moves ownership of `data` (the `KBox`) to the devres system, dereferencing `data_ptr` is only valid if the devres entry outlives `'a`. The argument is that `'a` is tied to `&Device<Bound>`, and `Bound` state guarantees the device won't be unbound (and hence devres won't fire) during that lifetime. This seems sound given the Rust driver model's `Bound` guarantees, but this is a subtle invariant that deserves scrutiny from the Rust-for-Linux maintainers, particularly regarding whether any path could trigger devres cleanup while a `&Device<Bound>` still exists.
The callers (`cpufreq.rs`, `drm/driver.rs`) are updated to discard the new return value with `?; Ok(())`, which is straightforward and correct.
**Nit:** The raw pointer `data_ptr` is taken before `register_foreign` moves `data`. Since `register_foreign` takes `data: P` by value (a `Pin<KBox<T>>`), the pointer remains valid only because register_foreign stores it in devres. The ordering is correct, but worth calling out in the safety comment that `data_ptr` points into the `KBox`-allocated memory which `register_foreign` takes ownership of.
---
Generated by Claude Code Patch Reviewer
next prev parent reply other threads:[~2026-06-04 5:18 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-30 19:25 [PATCH v10 0/5] rust: add basic serial device bus abstractions Markus Probst
2026-05-30 19:25 ` [PATCH v10 1/5] rust: devres: return reference in `devres::register` Markus Probst
2026-06-04 5:18 ` Claude Code Review Bot [this message]
2026-05-30 19:25 ` [PATCH v10 2/5] serdev: add rust private data to serdev_device Markus Probst
2026-06-04 5:18 ` Claude review: " Claude Code Review Bot
2026-05-30 19:25 ` [PATCH v10 3/5] rust: add basic serial device bus abstractions Markus Probst
2026-06-04 5:18 ` Claude review: " Claude Code Review Bot
2026-05-30 19:25 ` [PATCH v10 4/5] samples: rust: add Rust serial device bus sample device driver Markus Probst
2026-06-04 5:18 ` Claude review: " Claude Code Review Bot
2026-05-30 19:25 ` [PATCH v10 5/5] MAINTAINERS: serdev: Add self for serdev Markus Probst
2026-06-04 5:18 ` Claude review: " Claude Code Review Bot
2026-06-04 5:18 ` Claude review: rust: add basic serial device bus abstractions Claude Code Review Bot
-- strict thread matches above, loose matches on Subject: below --
2026-05-30 1:13 [PATCH v8 0/5] " Markus Probst via B4 Relay
2026-05-30 1:13 ` [PATCH v8 1/5] rust: devres: return reference in `devres::register` Markus Probst via B4 Relay
2026-06-04 5:57 ` Claude review: " Claude Code Review Bot
2026-04-29 18:21 [PATCH v7 0/4] rust: add basic serial device bus abstractions Markus Probst via B4 Relay
2026-04-29 18:21 ` [PATCH v7 1/4] rust: devres: return reference in `devres::register` Markus Probst via B4 Relay
2026-05-05 1:18 ` Claude review: " Claude Code Review Bot
2026-04-27 18:05 [PATCH v6 0/4] rust: add basic serial device bus abstractions Markus Probst via B4 Relay
2026-04-27 18:05 ` [PATCH v6 1/4] rust: devres: return reference in `devres::register` Markus Probst via B4 Relay
2026-04-28 4:14 ` Claude review: " Claude Code Review Bot
2026-04-20 20:07 [PATCH v5 0/4] rust: add basic serial device bus abstractions Markus Probst
2026-04-20 20:07 ` [PATCH v5 1/4] rust: devres: return reference in `devres::register` Markus Probst
2026-04-22 23:21 ` Claude review: " Claude Code Review Bot
2026-04-11 15:10 [PATCH v4 0/4] rust: add basic serial device bus abstractions Markus Probst via B4 Relay
2026-04-11 15:10 ` [PATCH v4 1/4] rust: devres: return reference in `devres::register` Markus Probst via B4 Relay
2026-04-11 22:55 ` Claude review: " Claude Code Review Bot
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 review: " Claude Code Review Bot
2026-03-06 19:35 [PATCH v2 0/4] rust: add basic serial device bus abstractions 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-20260530-rust_serdev-v10-1-65d1d5db876c@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