mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Matti Vaittinen <mazziesaccount@gmail.com>,
	David Lechner <dlechner@baylibre.com>,
	Stepan Ionichev <sozdayvek@gmail.com>,
	nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] iio: pressure: rohm-bm1390: notify trigger on all error paths
Date: Mon, 18 May 2026 21:31:48 +0300	[thread overview]
Message-ID: <agtbFDbw2m9ptEua@ashevche-desk.local> (raw)
In-Reply-To: <20260518155521.6c61504d@jic23-huawei>

On Mon, May 18, 2026 at 03:55:21PM +0100, Jonathan Cameron wrote:
> On Mon, 18 May 2026 09:59:24 +0300
> Andy Shevchenko <andriy.shevchenko@intel.com> wrote:
> > On Mon, May 18, 2026 at 08:21:17AM +0300, Matti Vaittinen wrote:
> > > On 17/05/2026 20:12, David Lechner wrote:  
> > > > On 5/17/26 11:08 AM, Stepan Ionichev wrote:  

...

> > > Maybe it would be better to do something like:
> > > 
> > > void iio_trigger_poll_nested(struct iio_trigger *trig)
> > > {
> > >         int i;
> > > 
> > >         if (!atomic_read(&trig->use_count)) {
> > >                 atomic_set(&trig->use_count,
> > > CONFIG_IIO_CONSUMERS_PER_TRIGGER);  
> > 
> > Just in case somebody is going to do that, avoid doing atomic_read() followed
> > by atomic_set(). This is typical TOCTOU issue. This should be something like
> > atomic_xchg() or atomic_add_return() or something like this in a single atomic
> > operation.
> 
> Just to clarify - the current code is fine.  This got reported a few years
> back and I did the analysis to prove it. From what I recall the key is
> that the state space isn't as complex as it immediately looks.
> That counter is either non 0 at the start (we don't use it here and we
> skip an interrupt - that's actually the desired behaviour if the trigger is running
> too fast - triggers must survive that - reenable() callback is there to make that
> all work).
> 
> Otherwise there is a single path that sets it and we know any decrement until after
> that happens would have undeflowed (and hence was a bug). The rest are decrement
> only and it can never go to less than 0.
> 
> Hence it is fine.
> 
> Agreed things get messy if we make this alg any more complex though!

Perhaps we need a good comment just on top of this atomic_read()/atomic_set()
pair. Because it's really the code no one should take as an example how to do
atomics :-) Logical question, why do we even have atomics there? Shouldn't
be that READ_ONCE()/WRITE_ONCE() to have an integrity in place? (This I believe
even mentioned in the documentation for atomics.)

> > >                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
> > >                         if (trig->subirqs[i].enabled)
> > >                                 handle_nested_irq(trig->subirq_base + i);
> > >                         else
> > >                                 iio_trigger_notify_done(trig);
> > >                 }
> > > 		atomic_set(&trig->use_count, 0); /* Clear the use_count if drivers didn't
> > > */
> > >         }
> > > }
> > > 
> > > to prevent this class of problems once and for all. But yeah, wiser minds
> > > have designed this - so let's hear some other opinions as well :)  

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-05-18 18:31 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-17 16:08 Stepan Ionichev
2026-05-17 17:12 ` David Lechner
2026-05-17 17:18   ` Stepan Ionichev
2026-05-18  5:21   ` Matti Vaittinen
2026-05-18  6:59     ` Andy Shevchenko
2026-05-18  7:35       ` Matti Vaittinen
2026-05-18 14:55       ` Jonathan Cameron
2026-05-18 18:31         ` Andy Shevchenko [this message]
2026-05-20 10:39           ` Jonathan Cameron
2026-05-18 14:50     ` Jonathan Cameron
2026-05-18  6:54 ` Andy Shevchenko
2026-05-18  9:42 ` [PATCH v2] " Stepan Ionichev
2026-05-18 10:42   ` Andy Shevchenko
2026-05-18 13:06   ` Matti Vaittinen
2026-05-18 15:15   ` Jonathan Cameron
2026-05-19  5:48     ` Matti Vaittinen
2026-05-20 11:08       ` Jonathan Cameron
2026-05-22 12:38         ` Matti Vaittinen
2026-05-29  8:21           ` Matti Vaittinen
2026-06-01 18:36             ` Andy Shevchenko
2026-06-04  6:10               ` Matti Vaittinen
2026-06-03 17:26             ` Jonathan Cameron
2026-06-04  6:05               ` Matti Vaittinen

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=agtbFDbw2m9ptEua@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazziesaccount@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=sozdayvek@gmail.com \
    --cc=stable@vger.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®