linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: William Breathitt Gray <vilhelm.gray@gmail.com>
To: Oleksij Rempel <o.rempel@pengutronix.de>
Cc: "David Lechner" <david@lechnology.com>,
	"David Jander" <david@protonic.nl>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	linux-iio@vger.kernel.org,
	"Robin van der Gracht" <robin@protonic.nl>,
	linux-kernel@vger.kernel.org,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Jonathan Cameron" <jic23@kernel.org>
Subject: Re: [PATCH v1] counter: interrupt-cnt: add counter_push_event()
Date: Thu, 3 Feb 2022 16:50:54 +0900	[thread overview]
Message-ID: <YfuJXrxpas1ufzp2@shinobu> (raw)
In-Reply-To: <20220203072411.GA12695@pengutronix.de>

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

On Thu, Feb 03, 2022 at 08:24:11AM +0100, Oleksij Rempel wrote:
> On Wed, Feb 02, 2022 at 09:17:57AM -0600, David Lechner wrote:
> > On 2/2/22 6:32 AM, Oleksij Rempel wrote:
> > > Hi William,
> > > 
> > > On Sat, Dec 25, 2021 at 01:07:44PM +0900, William Breathitt Gray wrote:
> > > ...
> > > > So the counter_push_event() function interacts with two spinlocks:
> > > > events_list_lock and events_in_lock. The events_list_lock spinlock is
> > > > necessary because userspace can modify the events_list list via the
> > > > counter_enable_events() and counter_disable_events() functions. The
> > > > events_in_lock spinlock is necessary because userspace can modify the
> > > > events kfifo via the counter_events_queue_size_write() function.
> > > > 
> > > > A lockless solution for this might be possible if the driver maintains
> > > > its own circular buffer as you suggest. The driver's IRQ handler can
> > > > write to this circular buffer without calling the counter_push_event()
> > > > function, and then flush the buffer to the Counter character device via
> > > > a userspace write to a "flush_events" sysfs attribute or similar; this
> > > > eliminates the need for the events_in_lock spinlock. The state of the
> > > > events_list list can be captured in the driver's events_configure()
> > > > callback and stored locally in the driver for reference, thus
> > > > eliminating the need for the events_list_lock; interrupts can be
> > > > disabled before the driver's local copy of events_list is modified.
> > > > 
> > > > With only one reader and one writer operating on the driver's buffer,
> > > > you can use the normal kfifo_in and kfifo_out calls for lockless
> > > > operations. Perhaps that is a way forward for this problem.
> > > 
> > > As proof of concept, I implemented the double buffered version with the
> > > sysfs flush_events interface. Currently it feels kind of wired, I use
> > > poll and wait until it timeouts to run the sysfs_flush_counter() to
> > > trigger new data.
> > > 
> > > Here is example:
> > > int main(void)
> > > {
> > > 	ret = sysfs_enable_counter();
> > > 	...
> > > 
> > > 	fd = open("/dev/counter0", O_RDWR);
> > > 	...
> > > 
> > > 	ret = ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches);
> > > 	...
> > > 
> > > 	ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
> > > 	...
> > > 
> > > 	for (;;) {
> > > 		struct pollfd fds[] = {
> > > 			{
> > > 				.fd = fd,
> > > 				.events = POLLIN,
> > > 			},
> > > 		};
> > > 		ssize_t i;
> > > 
> > > 		/* wait for 10 sec */
> > > 		ret = poll(fds, ARRAY_SIZE(fds), DEFAULT_TIMEOUT_MS);
> > > 		if (ret == -EINTR)
> > > 			continue;
> > > 		else if (ret < 0)
> > > 			return -errno;
> > > 		else if (ret == 0) {
> > > 			sysfs_flush_counter(); <---- request to flush queued events from the driver
> > > 			continue;
> > > 		}
> > > 
> > > 		ret = read(fd, event_data, sizeof(event_data));
> > > 		...
> > > 
> > > 		for (i = 0; i < ret / (ssize_t)sizeof(event_data[0]); i++)
> > > 			/* process event */
> > > 			....
> > > 		}
> > > 	}
> > > 
> > > 	return ret;
> > > }
> > > 
> > > If it is still the only way to go, I'll send kernel patches.
> > > 
> > > Regards,
> > > Oleksij
> > > 
> > 
> > Couldn't the flush be implicit in the `read()` implementation
> > instead of requiring a separate sysfs attribute to trigger it?
> 
> Hm...
> 
> To detect pulse frequency, I need a burst of sequential time-stamps
> without drops. In case the pulse frequency is higher then the use space
> is able to get it out of FIFO, we will get high number of drops. 
> So, we do not need all time stamps. Only bunch of them without drops in
> the middle.
> 
> I know, at some frequency we wont be able to collect all pulses any way.
> Internal FIFO is just increasing the max detectable frequency. So, it is
> sort of optimization.
> 
> My current driver version has own FIFO which is filled directly by the
> IRQ handler and user space trigger flush_cb to push all collected
> time stamps. The main question is: how the flush procedure should be
> controlled. We have following options:
> 
> - Attach it to the read(). The disadvantage: at high frequencies, we
>   wont be able to get a burst with time stamps without drops in the
>   middle
> - Trigger flush from user space. In this case, we make user space a bit
>   more complicated and cant really get all advantages of poll().
> - kernel driver is using own timer to trigger flush. The timer can be
>   configured from user space. The advantage of it, the user space is
>   simple and has full advantage of using poll()
> 
> Regards,
> Oleksij

Hi Oleksij,

Earlier in this thread, Jonathan Cameron suggested using the RCU macros
to protect access to the events. Taking an RCU approach would eliminate
the need for spinlocks because the memory barriers are built-in to the
macros, so I assume flushing would no longer be necessary. Would RCU be
a viable solution for your needs?

William Breathitt Gray

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

  reply	other threads:[~2022-02-03  7:51 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-23 13:45 [PATCH v1] counter: interrupt-cnt: add counter_push_event() Oleksij Rempel
2021-11-24  6:09 ` William Breathitt Gray
2021-11-24  7:27   ` Oleksij Rempel
2021-11-25  1:58     ` William Breathitt Gray
2021-11-25  7:27       ` David Jander
2021-12-06 19:24       ` David Lechner
2021-12-07  7:16         ` David Jander
2021-12-08 13:59           ` Uwe Kleine-König
2021-12-08 16:10             ` David Jander
2021-12-15  8:48               ` William Breathitt Gray
2021-12-15  9:08                 ` David Jander
2021-12-25  4:07                   ` William Breathitt Gray
2021-12-27 15:16                     ` David Lechner
2021-12-29  9:26                       ` William Breathitt Gray
2021-12-29 16:45                         ` Jonathan Cameron
2022-02-02 12:32                     ` Oleksij Rempel
2022-02-02 15:17                       ` David Lechner
2022-02-03  7:24                         ` Oleksij Rempel
2022-02-03  7:50                           ` William Breathitt Gray [this message]
2022-02-03 10:40                             ` Oleksij Rempel

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=YfuJXrxpas1ufzp2@shinobu \
    --to=vilhelm.gray@gmail.com \
    --cc=david@lechnology.com \
    --cc=david@protonic.nl \
    --cc=jic23@kernel.org \
    --cc=kernel@pengutronix.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=robin@protonic.nl \
    --cc=u.kleine-koenig@pengutronix.de \
    /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).