[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aF544lt-9YJq8r0y@smile.fi.intel.com>
Date: Fri, 27 Jun 2025 13:56:34 +0300
From: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
To: Christian Marangi <ansuelsmth@...il.com>
Cc: Uwe Kleine-König <ukleinek@...nel.org>,
linux-kernel@...r.kernel.org, linux-pwm@...r.kernel.org,
Benjamin Larsson <benjamin.larsson@...exis.eu>,
AngeloGioacchino Del Regno <angelogioacchino.delregno@...labora.com>,
Lorenzo Bianconi <lorenzo@...nel.org>
Subject: Re: [PATCH v18] pwm: airoha: Add support for EN7581 SoC
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.
> 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.
> 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
--
With Best Regards,
Andy Shevchenko
Powered by blists - more mailing lists