mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mark Brown <broonie@kernel.org>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: Alice Ryhl <aliceryhl@google.com>,
	lgirdwood@gmail.com, sebastian.reichel@collabora.com,
	sjoerd.simons@collabora.co.uk, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, a.hindborg@kernel.org,
	benno.lossin@proton.me, tmgross@umich.edu, dakr@kernel.org,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: regulator: add a bare minimum regulator abstraction
Date: Thu, 20 Feb 2025 14:14:41 +0000	[thread overview]
Message-ID: <Z7c40fpQVelj20gE@finisterre.sirena.org.uk> (raw)
In-Reply-To: <53A50677-CB35-441F-8E58-0FB1EA577C4E@collabora.com>

[-- Attachment #1: Type: text/plain, Size: 3663 bytes --]

On Thu, Feb 20, 2025 at 10:48:31AM -0300, Daniel Almeida wrote:
> > On 20 Feb 2025, at 09:09, Mark Brown <broonie@kernel.org> wrote:

> > Having an enabled regulator object depend on a regulator object seems
> > like a goal rather than a problem, surely it's common to need such
> > relationships and there's an idiomatic way to do it?  It seems to be how
> > Rust does mutexes…

> Not when you need to store both of them at the same time.

> For Mutex<T>, for example, you don’t store both Mutex<T> and MutexGuard<'_,T>
> at the same time. You store a Mutex<T>, and create MutexGuard<'_, T> on the
> stack by locking the Mutex when you need to access T. When MutexGuard goes away,
> the Mutex is automatically unlocked.

> The equivalent behavior for us here would be to enable a regulator on the
> stack, perform some work, and then have this regulator be automatically
> disabled. There would be no way to keep it enabled for a longer period.

Surely this need also exists with other lock types in Rust?  Holding a
semaphore for a long lived series of operations or similar.  It seems
surprising that this isn't a standard pattern.

> >> ```
> >> self.my_regulator.enable() // error, moves out of `&self`
> >> ```

> > Your second block of code doesn't look obviously painful there?

> It is painful in the sense that it won’t even compile.

> This example assumes a different API where EnabledRegulator consumes Regulator
> to exist, instead of keeping an internal reference:

As I mentioned that sounds like something users do want.

> But this is extremely unergonomic because Option<T> is meant for cases where we
> may or may not have something, but this is not the case here. We're simply
> abusing its API to make our own Regulator API work.

Are we?  We may or may not have enabled the regulator.

> >> I am sure we can find ways around that, but a simple `bool` here seems to fix this problem.
> > 
> >> Now you only have to store `Regulator`. If you need another part of your code to also keep
> >> the regulator enabled, you store a `Regulator` there and enable that as well. All calls to
> >> enable and disable will be automatically balanced for all instances of `Regulator` by
> >> virtue of the `enabled` bool as well.

> > What you're describing here with creating one Regulator object per
> > enable sounds more like you want it to be an error to do multiple
> > enables on a single regulator.  Instead of reference counting or
> > silently ignoring duplicate enables it should error out on a duplicate
> > enable.

> The problem here isn't necessarily forbiding multiple enables, but figuring out
> a way to make sure they are balanced in Rust. Let me think this through some
> more, because I do not have a solution in hand right now.

Right, what I'm saying is that when you talk above about each part of
the code getting another Regulator for the same underlying regulator and
doing it's enable there that sounds like a pattern where each Regulator
has a maximum enable count of one - if it's idiomatic to have different
Regulator objects for different uses then you can say that each use
can have only one enable and needs to resolve it's own concurrency
issues.  The C regulator API is perfectly happy with this (modulo
exclusive regulators which you've not covered here) so if it simplifies
things that should be fine.

Your formatting here is still odd BTW, line lengths are too long (though
less long than on the prior mail) and it looks like there's some '''
formatting rather than indents around code that looks like one of the
structured text formats?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2025-02-20 14:14 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-19 16:25 Daniel Almeida
2025-02-19 16:28 ` Alice Ryhl
2025-02-19 17:10   ` Daniel Almeida
2025-02-20 12:09     ` Mark Brown
2025-02-20 13:48       ` Daniel Almeida
2025-02-20 14:14         ` Mark Brown [this message]
2025-02-23  7:58     ` Boqun Feng
2025-03-04 15:36     ` Alice Ryhl
2025-02-19 23:05 ` Mark Brown
2025-02-19 23:35   ` Daniel Almeida
2025-02-20  1:37     ` Mark Brown
2025-02-20  9:42       ` Daniel Almeida
2025-02-20 11:58         ` Mark Brown

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=Z7c40fpQVelj20gE@finisterre.sirena.org.uk \
    --to=broonie@kernel.org \
    --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=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sebastian.reichel@collabora.com \
    --cc=sjoerd.simons@collabora.co.uk \
    --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

all inboxes | Powered by JetHome®