linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: Sean Anderson <sean.anderson@linux.dev>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"Nuno Sá" <noname.nuno@gmail.com>,
	"Jean Delvare" <jdelvare@suse.com>,
	"Guenter Roeck" <linux@roeck-us.net>,
	linux-iio@vger.kernel.org, linux-hwmon@vger.kernel.org,
	"Andy Shevchenko" <andy@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>,
	linux-kernel@vger.kernel.org,
	"David Lechner" <dlechner@baylibre.com>
Subject: Re: [PATCH 3/7] iio: Add in-kernel API for events
Date: Thu, 31 Jul 2025 13:59:25 +0100	[thread overview]
Message-ID: <20250731135925.00007e5d@huawei.com> (raw)
In-Reply-To: <edb59657-ced2-4557-afe5-07bd83af848e@linux.dev>

On Tue, 29 Jul 2025 16:09:20 -0400
Sean Anderson <sean.anderson@linux.dev> wrote:

> On 7/29/25 14:33, Jonathan Cameron wrote:
> > On Mon, 28 Jul 2025 18:44:30 -0400
> > Sean Anderson <sean.anderson@linux.dev> wrote:
> >   
> >> On 7/27/25 12:21, Jonathan Cameron wrote:  
> >> > On Tue, 15 Jul 2025 12:52:19 -0400
> >> > Sean Anderson <sean.anderson@linux.dev> wrote:
> >> >     
> >> >> On 7/15/25 07:09, Nuno Sá wrote:    
> >> >> > On Mon, 2025-07-14 at 21:20 -0400, Sean Anderson wrote:      
> >> >> >> Add an API to notify consumers about events. Events still need to be
> >> >> >> enabled using the iio_read_event/iio_write_event functions. Of course,
> >> >> >> userspace can also manipulate the enabled events. I don't think this is
> >> >> >> too much of an issue, since userspace can also manipulate the event
> >> >> >> thresholds. But enabling events may cause existing programs to be
> >> >> >> surprised when they get something unexpected. Maybe we should set the
> >> >> >> interface as busy when there are any in-kernel listeners?
> >> >> >>       
> >> >> > 
> >> >> > Sensible question. I'm not that familiar with events but I suspect is not
> >> >> > trivial (if doable) to do a similar approach as with buffers? With buffers, an
> >> >> > inkernal consumer get's it's own buffer object (that goes into a list of active
> >> >> > buffers in the iio device) with all channels enabled and then we demux the
> >> >> > appropriate channels for each consumer.      
> >> >> 
> >> >> For in-kernel consumers I think it's reasonable to expect them to handle
> >> >> events they didn't explicitly enable. I'm not sure about userspace
> >> >> consumers.    
> >> > 
> >> > This already happens because we don't have a demux equivalent (what we do
> >> > for buffered data flow) so if a device only has a single enable bit that covers
> >> > multiple events (annoyingly common for accelerometers for example) then
> >> > userspace will get events it didn't ask for.   We 'could' fix that,
> >> > but it's never really been worth the effort.
> >> > 
> >> > Events tend to be low data rate so an occasionally extra is rather different
> >> > to having to have much larger data buffers to handle a range of channels you
> >> > never asked for.
> >> > 
> >> > Lets be careful to document this behaviour as 'may enable extra events'
> >> > as then if we decide later to do demux type stuff we won't be breaking ABI.
> >> > No one will mind getting fewer spurious events due to a core improvement.    
> >> 
> >> Where would this get documented?  
> > 
> > Starting point will be in the docs for the ABI that asks for any events at all.
> > 
> > Also useful to add some thing to Documentation/IIO though there are lots of
> > other things those docs don't yet cover :(  
> 
> Notably the whole events API :l
> 
> >>   
> >> >>     
> >> >> > Independent of the above, we can argue that having both inkernel and userspace
> >> >> > changing thresholds is ok (I mean, there's nothing stopping two userspace apps
> >> >> > doing that) but we should likely be careful with enabling/disabling. If multiple
> >> >> > consumers enable the same event, one of them disabling it should not disable it
> >> >> > for all the consumers, right?      
> >> >> 
> >> >> Right now the HWMON consumer never permanently disable events to avoid this
> >> >> issue. It does toggle the enable to determine if an alarm should stay
> >> >> enabled:
> >> >>              ________
> >> >> condition __/        \________
> >> >>           _____    ____    ___
> >> >> enable         \__/    \__/
> >> >> 
> >> >> event       |     |
> >> >>              __    ____
> >> >> alarm     __/  \__/    \_____
> >> >> 
> >> >> read           1       1    0
> >> >> 
> >> >> I suppose this could also be done by comparing the raw threshold to the
> >> >> channel.    
> >> > 
> >> > I wonder if we should add the option to do a 'get_exclusive' or similar
> >> > to block the IIO user interfaces if something critical is using the device.
> >> > 
> >> > If we were for instance to use this to block the IOCTL to get the events
> >> > fd then any built in driver etc will almost certainly load before anyone
> >> > can call the ioctl so it will fairly cleanly block things.    
> >> 
> >> This is how it currently works for userspace. Only one process can create
> >> the event fd, and everyone else gets -EBUSY.
> >> 
> >> Of course, it would be pretty surprising to have an IIO device where
> >> some channels were used by userspace and others were used by hwmon and
> >> then have your daemon stop working after you update your kernel because
> >> now the hwmon driver takes exclusive event access.  
> > 
> > True.  I wonder how many boards we don't know about are using the iio-hwmon
> > bridge. We can check the ones in kernel for whether they grab all the
> > channels (which would rule this out).
> >
> > Another things we could do is have an opt in from the IIO driver.
> > That way only 'new' drivers would have this behaviour.  Not nice though.  
> 
> I would really like for this to "just work" if at all possible, so an
> opt-out would be preferable. Maybe a hwmon module parameter.
> 
> But I think we can do better:
> 
> - Both kernel/userspace can/should handle unexpected events
>   - This includes extra (synthetic) events.
> - Both kernel/userspace mostly just want to enable events
> - Disabling events is not as important because of the previous bullet.
> - But losing events is probably bad so we want to ensure we trigger
>   events at the same places they would have been triggered before.
> 
> So maybe we have an implementation where
> 
> - Enabling an event disables the backing event before re-enabling it if
>   there are any existing users
> - Disabling an event only disables the backing event if all users are
>   gone
> 
> It could look something like
> 
> iio_sysfs_event_set(event, val):
>     if val:
>         if !event.user_enable
>             disable(event)
>         enable(event)
>     else if !event.kernel_enables
>         disable(event)
>     event.user_enable = val
> 
> iio_inkern_event_set(event, val):
>     if val:
>         if event.kernel_enables++ || event.user_enable
>             disable(event)
>         enable(event)
>     else if !--event.kernel_enables && !event.user_enable:
>         disable(event)

Something like that should work.  We'll need to be careful
to gate any push towards userspace on it waiting for something.

Given we only send them when IIO_BUSY_BIT_POS is set on the
event interface (which happens on requesting the fd) I think
we may be fine already.

Jonathan

> 
> --Sean
> 
> >> 
> >> I originally had kernel users read from the kfifo just like userspace,
> >> but I was concerned about the above scenario.
> >>   
> > 
> > yeah, always a problem to retrofit policy.
> >   
> >> --Sean
> >>   
> >   
> 
> 


  reply	other threads:[~2025-07-31 12:59 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-15  1:20 [PATCH 0/7] hwmon: iio: Add alarm support Sean Anderson
2025-07-15  1:20 ` [PATCH 1/7] math64: Add div64_s64_rem Sean Anderson
2025-07-15  8:03   ` Andy Shevchenko
2025-07-15 17:36     ` Sean Anderson
2025-07-16 10:15       ` Andy Shevchenko
2025-07-15  1:20 ` [PATCH 2/7] iio: inkern: Add API for reading/writing events Sean Anderson
2025-07-15  8:18   ` Andy Shevchenko
2025-07-15 15:42     ` Sean Anderson
2025-07-16  9:28       ` Andy Shevchenko
2025-07-17 16:42         ` Sean Anderson
2025-07-27 15:55           ` Jonathan Cameron
2025-07-15 10:35   ` Nuno Sá
2025-07-15 15:43     ` Sean Anderson
2025-07-16  6:23       ` Nuno Sá
2025-07-27 16:13   ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 3/7] iio: Add in-kernel API for events Sean Anderson
2025-07-15  8:20   ` Andy Shevchenko
2025-07-15 15:47     ` Sean Anderson
2025-07-16  9:47       ` Andy Shevchenko
2025-07-15 11:09   ` Nuno Sá
2025-07-15 16:52     ` Sean Anderson
2025-07-27 16:21       ` Jonathan Cameron
2025-07-28 22:44         ` Sean Anderson
2025-07-29 18:33           ` Jonathan Cameron
2025-07-29 20:09             ` Sean Anderson
2025-07-31 12:59               ` Jonathan Cameron [this message]
2025-07-27 16:24   ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 4/7] hwmon: iio: Refactor scale calculation into helper Sean Anderson
2025-07-15  8:35   ` Andy Shevchenko
2025-07-15  1:20 ` [PATCH 5/7] hwmon: iio: Add helper function for creating attributes Sean Anderson
2025-07-15  8:38   ` Andy Shevchenko
2025-07-15 15:55     ` Sean Anderson
2025-07-16 10:00       ` Andy Shevchenko
2025-07-27 16:31   ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 6/7] hwmon: iio: Add min/max support Sean Anderson
2025-07-15  8:41   ` Andy Shevchenko
2025-07-15 16:05     ` Sean Anderson
2025-07-16 10:01       ` Andy Shevchenko
2025-07-17 16:11         ` Sean Anderson
2025-07-27 16:35   ` Jonathan Cameron
2025-07-28 22:32     ` Sean Anderson
2025-07-29 18:37       ` Jonathan Cameron
2025-07-15  1:20 ` [PATCH 7/7] hwmon: iio: Add alarm support Sean Anderson
2025-07-15  8:50   ` Andy Shevchenko
2025-07-15 16:20     ` Sean Anderson
2025-07-16 10:08       ` Andy Shevchenko
2025-07-17 16:23         ` Sean Anderson
2025-07-21  7:42           ` Andy Shevchenko
2025-07-21 14:24             ` Sean Anderson
2025-07-15 11:28   ` Nuno Sá
2025-07-15 17:02     ` Sean Anderson
2025-07-15 19:26       ` Guenter Roeck
2025-07-15 19:40         ` Sean Anderson
2025-07-16  6:37       ` Nuno Sá
2025-07-17 16:00         ` Sean Anderson
2025-07-31 10:52           ` Nuno Sá
2025-08-02 10:53             ` Jonathan Cameron
2025-07-15 16:13   ` kernel test robot
2025-07-15 19:34   ` Guenter Roeck
2025-07-15 20:08     ` Sean Anderson
2025-07-16  7:44   ` kernel test robot
2025-07-27 16:50   ` Jonathan Cameron

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=20250731135925.00007e5d@huawei.com \
    --to=jonathan.cameron@huawei.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jdelvare@suse.com \
    --cc=jic23@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=noname.nuno@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=sean.anderson@linux.dev \
    /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;
as well as URLs for NNTP newsgroup(s).