From: Dirk Behme <dirk.behme@gmail.com>
To: Danilo Krummrich <dakr@kernel.org>, Remo Senekowitsch <remo@buenzli.dev>
Cc: "Rob Herring" <robh@kernel.org>,
"Saravana Kannan" <saravanak@google.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Dirk Behme" <dirk.behme@de.bosch.com>,
linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v3 3/7] rust: property: Introduce PropertyGuard
Date: Sat, 26 Apr 2025 16:35:07 +0200 [thread overview]
Message-ID: <39798ebd-35a8-4a67-9df4-f12a6f20ef11@gmail.com> (raw)
In-Reply-To: <aAzrg31NB2g0X4qL@cassiopeiae>
On 26.04.25 16:19, Danilo Krummrich wrote:
> On Sat, Apr 26, 2025 at 01:08:39PM +0200, Remo Senekowitsch wrote:
>> On Sat Apr 26, 2025 at 12:15 PM CEST, Danilo Krummrich wrote:
>>> On Sat, Apr 26, 2025 at 08:19:09AM +0200, Dirk Behme wrote:
>>>> On 25.04.25 17:35, Danilo Krummrich wrote:
>>>>> On Fri, Apr 25, 2025 at 05:01:26PM +0200, Remo Senekowitsch wrote:
>>>>>> This abstraction is a way to force users to specify whether a property
>>>>>> is supposed to be required or not. This allows us to move error
>>>>>> logging of missing required properties into core, preventing a lot of
>>>>>> boilerplate in drivers.
>>>>>>
>>>>>> It will be used by upcoming methods for reading device properties.
>>>>>>
>>>>>> Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
>>>>>> ---
>>>>>> rust/kernel/device/property.rs | 57 ++++++++++++++++++++++++++++++++++
>>>>>> 1 file changed, 57 insertions(+)
>>>>>>
>>>>>> diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
>>>>>> index 28850aa3b..de31a1f56 100644
>>>>>> --- a/rust/kernel/device/property.rs
>>>>>> +++ b/rust/kernel/device/property.rs
>>>>>> @@ -146,3 +146,60 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
>>>>>> unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
>>>>>> }
>>>>>> }
>>>>>> +
>>>>>> +/// A helper for reading device properties.
>>>>>> +///
>>>>>> +/// Use [`Self::required`] if a missing property is considered a bug and
>>>>>> +/// [`Self::optional`] otherwise.
>>>>>> +///
>>>>>> +/// For convenience, [`Self::or`] and [`Self::or_default`] are provided.
>>>>>> +pub struct PropertyGuard<'fwnode, 'name, T> {
>>>>>> + /// The result of reading the property.
>>>>>> + inner: Result<T>,
>>>>>> + /// The fwnode of the property, used for logging in the "required" case.
>>>>>> + fwnode: &'fwnode FwNode,
>>>>>> + /// The name of the property, used for logging in the "required" case.
>>>>>> + name: &'name CStr,
>>>>>> +}
>>>>>> +
>>>>>> +impl<T> PropertyGuard<'_, '_, T> {
>>>>>> + /// Access the property, indicating it is required.
>>>>>> + ///
>>>>>> + /// If the property is not present, the error is automatically logged. If a
>>>>>> + /// missing property is not an error, use [`Self::optional`] instead.
>>>>>> + pub fn required(self) -> Result<T> {
>>>>>> + if self.inner.is_err() {
>>>>>> + pr_err!(
>>>>>> + "{}: property '{}' is missing\n",
>>>>>> + self.fwnode.display_path(),
>>>>>> + self.name
>>>>>> + );
>>>>>
>>>>> Hm, we can't use the device pointer of the fwnode_handle, since it is not
>>>>> guaranteed to be valid, hence the pr_*() print...
>>>>>
>>>>> Anyways, I'm not sure we need to print here at all. If a driver wants to print
>>>>> that it is unhappy about a missing required property it can do so by itself, I
>>>>> think.
>>>>
>>>> Hmm, the driver said by using 'required' that it *is* required. So a
>>>> missing property is definitely an error here. Else it would have used
>>>> 'optional'. Which doesn't print in case the property is missing.
>>>>
>>>> If I remember correctly having 'required' and 'optional' is the result
>>>> of some discussion on Zulip. And one conclusion of that discussion was
>>>> to move checking & printing the error out of the individual drivers
>>>> into a central place to avoid this error checking & printing in each
>>>> and every driver. I think the idea is that the drivers just have to do
>>>> ...required()?; and that's it, then.
>>>
>>> Yes, I get the idea.
>>>
>>> If it'd be possible to use dev_err!() instead I wouldn't object in this specific
>>> case. But this code is used by drivers from probe(), hence printing the error
>>> without saying for which device it did occur is a bit pointless.
>>>
>>> Drivers can still decide to properly print the error if the returned Result
>>> indicates one.
>>
>> One alternative would be to store a reference count to the device in
>> `FwNode`. At that point we'd be guaranteed to have a valid reference
>> whenever we want to log something.
>
> Yes, that would work. However, I'm not convinced that it's worth to store an
> ARef<Device> (i.e. take a device reference) in each FwNode structure *only* to
> be able to force an error print if a required device property isn't available.
>
> Why do you think it is important to force this error print by having it in
> PropertyGuard::required() and even take an additional device reference for this
> purpose, rather than leaving it to the driver when to print a message for an
> error condition that makes it fail to probe()?
To my understanding doing the error print in "core" was proposed by
Rob [1]:
"If the property is missing and required, then we may want to print an
error msg (in the core, not every caller)"
Dirk
[1]
https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/DS90UB954.20driver.20done.2C.20ready.20to.20upstream.3F/near/496884813
next prev parent reply other threads:[~2025-04-26 14:35 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-25 15:01 [PATCH v3 0/7] More Rust bindings for device property reads Remo Senekowitsch
2025-04-25 15:01 ` [PATCH v3 1/7] rust: property: Move property_present to separate file Remo Senekowitsch
2025-04-25 15:25 ` Danilo Krummrich
2025-04-30 6:14 ` Dirk Behme
2025-04-25 15:01 ` [PATCH v3 2/7] rust: property: Enable printing fwnode name and path Remo Senekowitsch
2025-04-25 15:48 ` Danilo Krummrich
2025-04-30 7:44 ` Dirk Behme
2025-04-25 15:01 ` [PATCH v3 3/7] rust: property: Introduce PropertyGuard Remo Senekowitsch
2025-04-25 15:35 ` Danilo Krummrich
2025-04-26 6:19 ` Dirk Behme
2025-04-26 10:15 ` Danilo Krummrich
2025-04-26 11:08 ` Remo Senekowitsch
2025-04-26 14:19 ` Danilo Krummrich
2025-04-26 14:35 ` Dirk Behme [this message]
2025-04-26 15:02 ` Danilo Krummrich
2025-04-26 21:50 ` Remo Senekowitsch
2025-04-27 22:12 ` John Hubbard
2025-04-28 20:18 ` Rob Herring
2025-04-28 20:25 ` John Hubbard
2025-04-28 21:10 ` Rob Herring
2025-04-27 6:11 ` Dirk Behme
2025-04-27 12:23 ` Danilo Krummrich
2025-04-28 5:03 ` Dirk Behme
2025-04-28 16:09 ` Danilo Krummrich
2025-04-28 20:48 ` Rob Herring
2025-04-28 21:21 ` Danilo Krummrich
2025-04-28 21:50 ` Remo Senekowitsch
2025-04-29 8:50 ` Danilo Krummrich
2025-04-25 15:01 ` [PATCH v3 4/7] rust: property: Add bindings for reading device properties Remo Senekowitsch
2025-04-25 15:01 ` [PATCH v3 5/7] rust: property: Add child accessor and iterator Remo Senekowitsch
2025-04-30 6:26 ` Dirk Behme
2025-04-25 15:01 ` [PATCH v3 6/7] rust: property: Add property_get_reference_args Remo Senekowitsch
2025-04-25 15:01 ` [PATCH v3 7/7] samples: rust: platform: Add property read examples Remo Senekowitsch
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=39798ebd-35a8-4a67-9df4-f12a6f20ef11@gmail.com \
--to=dirk.behme@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dirk.behme@de.bosch.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=remo@buenzli.dev \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=saravanak@google.com \
--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