mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Christian Marangi <ansuelsmth@gmail.com>
Cc: "Uwe Kleine-König" <ukleinek@kernel.org>,
	linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org,
	"Benjamin Larsson" <benjamin.larsson@genexis.eu>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Lorenzo Bianconi" <lorenzo@kernel.org>
Subject: Re: [PATCH v18] pwm: airoha: Add support for EN7581 SoC
Date: Mon, 30 Jun 2025 13:29:54 +0300	[thread overview]
Message-ID: <aGJnItQ3hjQ80rlz@smile.fi.intel.com> (raw)
In-Reply-To: <686264ac.df0a0220.feace.3044@mx.google.com>

On Mon, Jun 30, 2025 at 12:19:22PM +0200, Christian Marangi wrote:
> On Fri, Jun 27, 2025 at 01:56:34PM +0300, Andy Shevchenko wrote:
> > On Fri, Jun 27, 2025 at 12:34:49PM +0200, Christian Marangi wrote:
> > > On Fri, Jun 27, 2025 at 01:25:48PM +0300, Andy Shevchenko wrote:
> > > > On Fri, Jun 27, 2025 at 11:32:46AM +0200, Christian Marangi wrote:
> > > > > On Fri, Jun 27, 2025 at 11:58:04AM +0300, Andy Shevchenko wrote:
> > > > > > On Fri, Jun 27, 2025 at 12:47:53AM +0200, Christian Marangi wrote:

...

> > > > > > > +	/* Global mutex to protect bucket used refcount_t */
> > > > > > > +	struct mutex mutex;
> > > > > > 
> > > > > > This makes a little sense. Either you use refcount_t (which is atomic) or
> > > > > > use mutex + regular variable.
> > > > > 
> > > > > Using a regular variable I lose all the benefits of refcount_t with
> > > > > underflow and other checks.
> > > > 
> > > > Then drop the mutex, atomic operations do not need an additional
> > > > synchronisation. Btw, have you looked at kref APIs? Maybe that
> > > > would make the intention clearer?
> > > 
> > > It's needed for
> > > 
> > > +       mutex_lock(&pc->mutex);
> > > +       if (refcount_read(&pc->buckets[bucket].used) == 0) {
> > > +               config_bucket = true;
> > > +               refcount_set(&pc->buckets[bucket].used, 1);
> > > +       } else {
> > > +               refcount_inc(&pc->buckets[bucket].used);
> > > +       }
> > > +       mutex_unlock(&pc->mutex);
> > > 
> > > the refcount_read + refcount_set.
> > 
> > Which is simply wrong. Nobody should use atomics in such a way.
> > Imagine if somebody wants to copy something like this in their
> > code (in case of no mutex is there), they most likely won't notice
> > this subtle bug.
> >
> 
> Yes I understand that someone might think the additional mutex can be
> ""optional""
> 
> > > As you explained there might be case where refcount_read is zero but nother
> > > PWM channel is setting the value so one refcount gets lost.
> > 
> > Right, because you should use refcount_inc_and_test() and initialise it
> > to -MAX instead of 0. Or something like this.
> 
> Mhhh I think API for _inc_and_test doesn't currently exist and I don't
> feel too confident implementing them currently.

Ther is refcount_inc_not_zero(), but the main point here that refcount_t seems
not fit the case. It doesn't work with negative values, and 0 is special.

> > > kref I checked but not useful for the task.
> > 
> > Okay.
> > 
> > > The logic here is
> > > 
> > > - refcount init as 0 (bucket unused)
> > > - refcount set to 1 on first bucket use (bucket get configured)
> > > - refcount increased if already used
> > > - refcount decreased when PWM channel released
> > > - bucket gets flagged as unused when refcount goes to 0 again
> 
> Do you think I should bite the bullet and just drop using refcount and
> implement a simple int variable protected by a mutex?

Yes.

-- 
With Best Regards,
Andy Shevchenko



      reply	other threads:[~2025-06-30 10:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-26 22:47 Christian Marangi
2025-06-27  8:58 ` Andy Shevchenko
2025-06-27  9:32   ` Christian Marangi
2025-06-27 10:25     ` Andy Shevchenko
2025-06-27 10:34       ` Christian Marangi
2025-06-27 10:56         ` Andy Shevchenko
2025-06-30 10:19           ` Christian Marangi
2025-06-30 10:29             ` Andy Shevchenko [this message]

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=aGJnItQ3hjQ80rlz@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=ansuelsmth@gmail.com \
    --cc=benjamin.larsson@genexis.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=lorenzo@kernel.org \
    --cc=ukleinek@kernel.org \
    /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®