From: "Danilo Krummrich" <dakr@kernel.org>
To: "Ke Sun" <sk.alvin.x@gmail.com>
Cc: "Ke Sun" <sunke@kylinos.cn>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"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>,
linux-rtc@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [RFC PATCH v1 4/4] rust: add PL031 RTC driver
Date: Tue, 06 Jan 2026 14:32:56 +0100 [thread overview]
Message-ID: <DFHJM2HAG7Q3.1HGZ3P7H55FD2@kernel.org> (raw)
In-Reply-To: <88b1a3dd-e646-4583-bc41-07ff7e9422a7@gmail.com>
(Cc: Greg, Rafael)
On Tue Jan 6, 2026 at 3:51 AM CET, Ke Sun wrote:
> Following the platform driver implementation, the AMBA driver stores its
> drvdata in amba_device->dev. However,
> the RTC driver also stores its drvdata in the parent device (which is
> also amba_device->dev), causing a conflict.
A simple driver usually has two devices to deal with:
(1) The bus device, which represents the actual physical device sitting on
some bus, e.g. PCI or platform.
(2) A class device, which represents the higher level functionality - i.e. the
class the device belongs to - to the upper layers, e.g. RTC, DRM, PWM,
etc.
The bus device does not belong to the driver per se, it only gets bound to a
driver. This relationship remains until the driver itself is unregistered, the
physical device falls off the bus or they are manually unbound from each other.
The driver's bus device private data only lives as long as the two are bound and
the lifetime is managed by the bus abstraction, e.g. platform or AMBA.
The class device is created by the driver to represent its functionality to the
upper layers; its lifetime is defined by the driver.
Other than the bus device private data, the class device private data typically
lives as long as the class device itself.
In the relationship between the two, the bus device becomes the parent device of
the class device.
If the class device implementation guarantees that it is unregistered when the
parent (bus) device is unbound (which is the most common case), i.e. no more
class device callbacks happen afterwards, the abstraction can treat the parent
device as &Device<Bound>, which allows drivers to directly access device
resources from the parent device without further synchronization.
The short answer for your question is, store the class device private data
within the class device itself, not within the parent device.
Alternatively, if the class device implementation does guarantee that in all its
callbacks the parent device is bound, i.e. you have a &Device<Bound>, you can
also access the bus device private data with Device<Bound>::drvdata().
In this case you can technically also omit having separate private data for the
class device at all.
However, while this is actually been done in quite some C drivers, I do not
recommend this:
- At least in C this can become pretty error prone, given that bus device
resources are only valid to access as long as the device is bound to the
driver; in Rust we do protect against device resource accesses after driver
unbind though.
- Separating the private data properly encourages driver authors to actually
think about ownership and lifetime of objects, eventually leading to better
driver design.
A common pattern you will see in drivers is that the data relevant for the class
device goes into the class device private data and the class device itself is
stored within the bus device private data.
I hope this helps!
- Danilo
next prev parent reply other threads:[~2026-01-06 13:33 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-04 6:06 [RFC PATCH v1 0/4] rust: Add RTC driver support Ke Sun
2026-01-04 6:06 ` [RFC PATCH v1 1/4] rust: add AMBA bus abstractions Ke Sun
2026-01-04 11:37 ` Miguel Ojeda
2026-01-04 12:37 ` Danilo Krummrich
2026-01-04 6:06 ` [RFC PATCH v1 2/4] rust: add device wakeup support Ke Sun
2026-01-04 13:31 ` Danilo Krummrich
2026-01-04 6:06 ` [RFC PATCH v1 3/4] rust: add RTC core abstractions and data structures Ke Sun
2026-01-04 13:00 ` Danilo Krummrich
2026-01-04 6:06 ` [RFC PATCH v1 4/4] rust: add PL031 RTC driver Ke Sun
2026-01-04 9:02 ` Dirk Behme
2026-01-07 10:15 ` Danilo Krummrich
2026-01-04 11:40 ` Miguel Ojeda
2026-01-04 13:10 ` Danilo Krummrich
2026-01-06 2:51 ` Ke Sun
2026-01-06 13:32 ` Danilo Krummrich [this message]
2026-01-06 14:44 ` Greg Kroah-Hartman
2026-01-06 15:04 ` Danilo Krummrich
2026-01-06 15:12 ` Greg Kroah-Hartman
2026-01-04 13:36 ` [RFC PATCH v1 0/4] rust: Add RTC driver support Danilo Krummrich
2026-01-04 14:11 ` Ke Sun
2026-01-06 7:41 ` Kari Argillander
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=DFHJM2HAG7Q3.1HGZ3P7H55FD2@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=alexandre.belloni@bootlin.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-rtc@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sk.alvin.x@gmail.com \
--cc=sunke@kylinos.cn \
--cc=tmgross@umich.edu \
/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