public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oleksij Rempel <o.rempel@pengutronix.de>
To: William Breathitt Gray <vilhelm.gray@gmail.com>
Cc: "David Jander" <david@protonic.nl>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"David Lechner" <david@lechnology.com>,
	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: Wed, 2 Feb 2022 13:32:41 +0100	[thread overview]
Message-ID: <Yfp56WznEMh7rp2O@pengutronix.de> (raw)
In-Reply-To: <YcaZEKbzRbX982YW@shinobu>

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
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

  parent reply	other threads:[~2022-02-02 12:32 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 [this message]
2022-02-02 15:17                       ` David Lechner
2022-02-03  7:24                         ` Oleksij Rempel
2022-02-03  7:50                           ` William Breathitt Gray
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=Yfp56WznEMh7rp2O@pengutronix.de \
    --to=o.rempel@pengutronix.de \
    --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=robin@protonic.nl \
    --cc=u.kleine-koenig@pengutronix.de \
    --cc=vilhelm.gray@gmail.com \
    /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