linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rjw@sisk.pl>
To: Matt Helsley <matthltc@us.ibm.com>
Cc: "Linux PM list" <linux-pm@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	"Magnus Damm" <magnus.damm@gmail.com>,
	markgross@thegnar.org, "Matthew Garrett" <mjg@redhat.com>,
	"Greg KH" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"John Stultz" <john.stultz@linaro.org>,
	"Brian Swetland" <swetland@google.com>,
	"Neil Brown" <neilb@suse.de>,
	"Alan Stern" <stern@rowland.harvard.edu>,
	"Dmitry Torokhov" <dmitry.torokhov@gmail.com>
Subject: Re: [RFC][PATCH 4/7] Input / PM: Add ioctl to block suspend while event queue is not empty
Date: Sun, 26 Feb 2012 21:57:18 +0100	[thread overview]
Message-ID: <201202262157.18496.rjw@sisk.pl> (raw)
In-Reply-To: <20120224051649.GB7666@count0.beaverton.ibm.com>

On Friday, February 24, 2012, Matt Helsley wrote:
> On Wed, Feb 22, 2012 at 12:34:58AM +0100, Rafael J. Wysocki wrote:
> > From: Arve Hjønnevåg <arve@android.com>
> > 
> > Add a new ioctl, EVIOCSWAKEUPSRC, to attach a wakeup source object to
> > an evdev client event queue, such that it will be active whenever the
> > queue is not empty.  Then, all events in the queue will be regarded
> > as wakeup events in progress and pm_get_wakeup_count() will block (or
> > return false if woken up by a signal) until they are removed from the
> > queue.  In consequence, if the checking of wakeup events is enabled
> > (e.g. throught the /sys/power/wakeup_count interface), the system
> > won't be able to go into a sleep state until the queue is empty.
> > 
> > This allows user space processes to handle situations in which they
> > want to do a select() on an evdev descriptor, so they go to sleep
> > until there are some events to read from the device's queue, and then
> > they don't want the system to go into a sleep state until all the
> > events are read (presumably for further processing).  Of course, if
> > they don't want the system to go into a sleep state _after_ all the
> > events have been read from the queue, they have to use a separate
> > mechanism that will prevent the system from doing that and it has
> > to be activated before reading the first event (that also may be the
> > last one).
> 
> I haven't seen this idea mentioned before but I must admit I haven't
> been following this thread too closely so apologies (and don't bother
> rehashing) if it has:
> 
> Could you just add this to epoll so that any fd userspace chooses would be
> capable of doing this without introducing potentially ecclectic ioctl
> interfaces?
> 
> struct epoll_event ev;
> 
> epfd = epoll_create1(EPOLL_STAY_AWAKE_SET);
> ev.data.ptr = foo;
> epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
> 
> Which could be useful because you can put one epollfd in another's epoll
> set. Or maybe as an EPOLLKEEPAWAKE flag in the event struct sort of like
> EPOLLET:
> 
> epfd = epoll_create1(0);
> ev.events = EPOLLIN|EPOLLKEEPAWAKE;
> epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);

Do you mean something like the patch below, or something different?

Rafael

---
 drivers/input/evdev.c     |   55 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/eventpoll.c            |   15 +++++++++++-
 include/linux/eventpoll.h |    6 +++++
 include/linux/fs.h        |    1 
 4 files changed, 76 insertions(+), 1 deletion(-)

Index: linux/include/linux/fs.h
===================================================================
--- linux.orig/include/linux/fs.h
+++ linux/include/linux/fs.h
@@ -1604,6 +1604,7 @@ struct file_operations {
 	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
 	int (*readdir) (struct file *, void *, filldir_t);
 	unsigned int (*poll) (struct file *, struct poll_table_struct *);
+	void (*epoll_ctl) (struct file *, int, unsigned int);
 	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
 	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
 	int (*mmap) (struct file *, struct vm_area_struct *);
Index: linux/fs/eventpoll.c
===================================================================
--- linux.orig/fs/eventpoll.c
+++ linux/fs/eventpoll.c
@@ -609,6 +609,10 @@ static int ep_remove(struct eventpoll *e
 	unsigned long flags;
 	struct file *file = epi->ffd.file;
 
+	/* Notify the underlying driver that the polling has completed */
+	if (file->f_op->epoll_ctl)
+		file->f_op->epoll_ctl(file, EPOLL_CTL_DEL, epi->event.events);
+
 	/*
 	 * Removes poll wait queue hooks. We _have_ to do this without holding
 	 * the "ep->lock" otherwise a deadlock might occur. This because of the
@@ -1094,6 +1098,10 @@ static int ep_insert(struct eventpoll *e
 	epq.epi = epi;
 	init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
 
+	/* Notify the underlying driver that we want to poll it */
+	if (tfile->f_op->epoll_ctl)
+		tfile->f_op->epoll_ctl(tfile, EPOLL_CTL_ADD, event->events);
+
 	/*
 	 * Attach the item to the poll hooks and get current event bits.
 	 * We can safely use the file* here because its usage count has
@@ -1185,6 +1193,7 @@ error_unregister:
  */
 static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
 {
+	struct file *file = epi->ffd.file;
 	int pwake = 0;
 	unsigned int revents;
 
@@ -1196,11 +1205,15 @@ static int ep_modify(struct eventpoll *e
 	epi->event.events = event->events;
 	epi->event.data = event->data; /* protected by mtx */
 
+	/* Notify the underlying driver of the change */
+	if (file->f_op->epoll_ctl)
+		file->f_op->epoll_ctl(file, EPOLL_CTL_MOD, event->events);
+
 	/*
 	 * Get current event bits. We can safely use the file* here because
 	 * its usage count has been increased by the caller of this function.
 	 */
-	revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
+	revents = file->f_op->poll(file, NULL);
 
 	/*
 	 * If the item is "hot" and it is not registered inside the ready
Index: linux/drivers/input/evdev.c
===================================================================
--- linux.orig/drivers/input/evdev.c
+++ linux/drivers/input/evdev.c
@@ -16,6 +16,7 @@
 #define EVDEV_BUF_PACKETS	8
 
 #include <linux/poll.h>
+#include <linux/eventpoll.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/module.h>
@@ -43,6 +44,7 @@ struct evdev_client {
 	unsigned int tail;
 	unsigned int packet_head; /* [future] position of the first element of next packet */
 	spinlock_t buffer_lock; /* protects access to buffer, head and tail */
+	struct wakeup_source *wakeup_source;
 	struct fasync_struct *fasync;
 	struct evdev *evdev;
 	struct list_head node;
@@ -75,10 +77,12 @@ static void evdev_pass_event(struct evde
 		client->buffer[client->tail].value = 0;
 
 		client->packet_head = client->tail;
+		__pm_relax(client->wakeup_source);
 	}
 
 	if (event->type == EV_SYN && event->code == SYN_REPORT) {
 		client->packet_head = client->head;
+		__pm_stay_awake(client->wakeup_source);
 		kill_fasync(&client->fasync, SIGIO, POLL_IN);
 	}
 
@@ -255,6 +259,8 @@ static int evdev_release(struct inode *i
 	mutex_unlock(&evdev->mutex);
 
 	evdev_detach_client(evdev, client);
+	wakeup_source_unregister(client->wakeup_source);
+
 	kfree(client);
 
 	evdev_close_device(evdev);
@@ -373,6 +379,8 @@ static int evdev_fetch_next_event(struct
 	if (have_event) {
 		*event = client->buffer[client->tail++];
 		client->tail &= client->bufsize - 1;
+		if (client->packet_head == client->tail)
+			__pm_relax(client->wakeup_source);
 	}
 
 	spin_unlock_irq(&client->buffer_lock);
@@ -433,6 +441,52 @@ static unsigned int evdev_poll(struct fi
 	return mask;
 }
 
+static void evdev_client_attach_wakeup_source(struct evdev_client *client)
+{
+	struct wakeup_source *ws;
+
+	ws = wakeup_source_register(dev_name(&client->evdev->dev));
+	spin_lock_irq(&client->buffer_lock);
+	client->wakeup_source = ws;
+	if (client->packet_head != client->tail)
+		__pm_stay_awake(client->wakeup_source);
+	spin_unlock_irq(&client->buffer_lock);
+}
+
+static void evdev_client_detach_wakeup_source(struct evdev_client *client)
+{
+	struct wakeup_source *ws;
+
+	spin_lock_irq(&client->buffer_lock);
+	ws = client->wakeup_source;
+	client->wakeup_source = NULL;
+	spin_unlock_irq(&client->buffer_lock);
+	wakeup_source_unregister(ws);
+}
+
+static void evdev_epoll_ctl(struct file *file, int op,
+				    unsigned int events)
+{
+	struct evdev_client *client = file->private_data;
+
+	switch (op) {
+	case EPOLL_CTL_ADD:
+		if ((events & EPOLLWAKEUP) && !client->wakeup_source)
+			evdev_client_attach_wakeup_source(client);
+		break;
+	case EPOLL_CTL_DEL:
+		if (events & EPOLLWAKEUP)
+			evdev_client_detach_wakeup_source(client);
+		break;
+	case EPOLL_CTL_MOD:
+		/* 'events' is the new events mask (after the change) */
+		if ((events & EPOLLWAKEUP) && !client->wakeup_source)
+			evdev_client_attach_wakeup_source(client);
+		else if (!(events & EPOLLWAKEUP))
+			evdev_client_detach_wakeup_source(client);
+	}
+}
+
 #ifdef CONFIG_COMPAT
 
 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
@@ -845,6 +899,7 @@ static const struct file_operations evde
 	.read		= evdev_read,
 	.write		= evdev_write,
 	.poll		= evdev_poll,
+	.epoll_ctl	= evdev_epoll_ctl,
 	.open		= evdev_open,
 	.release	= evdev_release,
 	.unlocked_ioctl	= evdev_ioctl,
Index: linux/include/linux/eventpoll.h
===================================================================
--- linux.orig/include/linux/eventpoll.h
+++ linux/include/linux/eventpoll.h
@@ -26,6 +26,12 @@
 #define EPOLL_CTL_DEL 2
 #define EPOLL_CTL_MOD 3
 
+/*
+ * Request the handling of system wakeup events so as to prevent automatic
+ * system suspends from happening while those events are being processed.
+ */
+#define EPOLLWAKEUP (1 << 29)
+
 /* Set the One Shot behaviour for the target file descriptor */
 #define EPOLLONESHOT (1 << 30)
 

  parent reply	other threads:[~2012-02-26 20:53 UTC|newest]

Thread overview: 129+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-07  1:00 [RFC][PATCH 0/8] PM: Implement autosleep and "wake locks" Rafael J. Wysocki
2012-02-07  1:01 ` [PATCH 1/8] PM / Sleep: Initialize wakeup source locks in wakeup_source_add() Rafael J. Wysocki
2012-02-07 22:29   ` John Stultz
2012-02-07 22:41     ` Rafael J. Wysocki
2012-02-07  1:03 ` [PATCH 2/8] PM / Sleep: Do not check wakeup too often in try_to_freeze_tasks() Rafael J. Wysocki
2012-02-07  1:03 ` [PATCH 3/8] PM / Sleep: Look for wakeup events in later stages of device suspend Rafael J. Wysocki
2012-02-07  1:04 ` [PATCH 4/8] PM / Sleep: Use wait queue to signal "no wakeup events in progress" Rafael J. Wysocki
2012-02-08 23:10   ` NeilBrown
2012-02-09  0:05     ` Rafael J. Wysocki
2012-02-12  1:27   ` mark gross
2012-02-07  1:05 ` [RFC][PATCH 5/8] PM / Sleep: Change wakeup statistics Rafael J. Wysocki
2012-02-15  6:15   ` Arve Hjønnevåg
2012-02-15 22:37     ` Rafael J. Wysocki
2012-02-17  2:11       ` Arve Hjønnevåg
2012-02-07  1:06 ` [RFC][PATCH 6/8] PM / Sleep: Implement opportunistic sleep Rafael J. Wysocki
2012-02-07 22:49   ` [Update][RFC][PATCH " Rafael J. Wysocki
2012-02-07  1:06 ` [RFC][PATCH 7/8] PM / Sleep: Add "prevent autosleep time" statistics to wakeup sources Rafael J. Wysocki
2012-02-07  1:07 ` [RFC][PATCH 8/8] PM / Sleep: Add user space interface for manipulating " Rafael J. Wysocki
2012-02-07  1:13 ` [RFC][PATCH 0/8] PM: Implement autosleep and "wake locks" Rafael J. Wysocki
2012-02-08 23:57 ` NeilBrown
2012-02-10  0:44   ` Rafael J. Wysocki
2012-02-12  2:05     ` mark gross
2012-02-12 21:32       ` Rafael J. Wysocki
2012-02-14  0:11         ` Arve Hjønnevåg
2012-02-15 15:28           ` mark gross
2012-02-12  1:54   ` mark gross
2012-02-12  1:19 ` mark gross
2012-02-14  2:07 ` Arve Hjønnevåg
2012-02-14 23:22   ` Rafael J. Wysocki
2012-02-15  5:57     ` Arve Hjønnevåg
2012-02-15 23:07       ` Rafael J. Wysocki
2012-02-16 22:22         ` Rafael J. Wysocki
2012-02-17  3:56           ` Arve Hjønnevåg
2012-02-17 23:02             ` [PATCH] PM / Sleep: Add more wakeup source initialization routines Rafael J. Wysocki
2012-02-18 23:50               ` [Update][PATCH] " Rafael J. Wysocki
2012-02-20 23:04                 ` [Update 2x][PATCH] " Rafael J. Wysocki
2012-02-17  3:55         ` [RFC][PATCH 0/8] PM: Implement autosleep and "wake locks" Arve Hjønnevåg
2012-02-17 20:57           ` Rafael J. Wysocki
2012-02-21 23:31 ` [RFC][PATCH 0/7] PM: Implement autosleep and "wake locks", take 2 Rafael J. Wysocki
2012-02-21 23:32   ` [RFC][PATCH 1/7] PM / Sleep: Look for wakeup events in later stages of device suspend Rafael J. Wysocki
2012-02-21 23:33   ` [RFC][PATCH 2/7] PM / Sleep: Use wait queue to signal "no wakeup events in progress" Rafael J. Wysocki
2012-02-21 23:34   ` [RFC][PATCH 3/7] PM / Sleep: Change wakeup source statistics to follow Android Rafael J. Wysocki
2012-02-21 23:34   ` [RFC][PATCH 4/7] Input / PM: Add ioctl to block suspend while event queue is not empty Rafael J. Wysocki
2012-02-24  5:16     ` Matt Helsley
2012-02-25  4:25       ` Arve Hjønnevåg
2012-02-25 23:33         ` Rafael J. Wysocki
2012-02-28  0:19         ` Matt Helsley
2012-02-26 20:57       ` Rafael J. Wysocki [this message]
2012-02-27 22:18         ` Matt Helsley
2012-02-28  1:17           ` Rafael J. Wysocki
2012-02-28  5:58         ` Arve Hjønnevåg
2012-03-04 22:56           ` Rafael J. Wysocki
2012-03-06  1:04             ` [PATCH 1/2] epoll: Add a flag, EPOLLWAKEUP, to prevent suspend while epoll events are ready Arve Hjønnevåg
2012-03-06  1:04               ` [PATCH 2/2] PM / Sleep: Add wakeup_source_activate and wakeup_source_deactivate tracepoints Arve Hjønnevåg
2012-02-21 23:35   ` [RFC][PATCH 5/7] PM / Sleep: Implement opportunistic sleep Rafael J. Wysocki
2012-02-22  8:45     ` Srivatsa S. Bhat
2012-02-22 22:10       ` Rafael J. Wysocki
2012-02-23  5:35         ` Srivatsa S. Bhat
2012-02-21 23:36   ` [RFC][PATCH 6/7] PM / Sleep: Add "prevent autosleep time" statistics to wakeup sources Rafael J. Wysocki
2012-02-21 23:37   ` [RFC][PATCH 7/7] PM / Sleep: Add user space interface for manipulating " Rafael J. Wysocki
2012-02-22  4:49   ` [RFC][PATCH 0/7] PM: Implement autosleep and "wake locks", take 2 John Stultz
2012-02-22  8:44     ` Srivatsa S. Bhat
2012-02-22 22:10       ` [RFC][PATCH 0/7] PM: Implement autosleep and "wake locks", take2 Rafael J. Wysocki
2012-02-23  6:25         ` Srivatsa S. Bhat
2012-02-23 21:26           ` Rafael J. Wysocki
2012-02-23 21:32             ` Rafael J. Wysocki
2012-02-24  4:44               ` Srivatsa S. Bhat
2012-02-24 23:21                 ` Rafael J. Wysocki
2012-02-25  4:43                   ` Arve Hjønnevåg
2012-02-25 20:43                     ` Rafael J. Wysocki
2012-02-25 19:20                   ` Srivatsa S. Bhat
2012-02-25 21:01                     ` Rafael J. Wysocki
2012-02-28 10:24                       ` Srivatsa S. Bhat
2012-04-22 21:19   ` [RFC][PATCH 0/8] PM: Implement autosleep and "wake locks", take 3 Rafael J. Wysocki
2012-04-22 21:19     ` [PATCH 1/8] PM / Sleep: Look for wakeup events in later stages of device suspend Rafael J. Wysocki
2012-04-22 21:20     ` [PATCH 2/8] PM / Sleep: Use wait queue to signal "no wakeup events in progress" Rafael J. Wysocki
2012-04-23  4:01       ` mark gross
2012-04-22 21:21     ` [PATCH 3/8] PM / Sleep: Change wakeup source statistics to follow Android Rafael J. Wysocki
2012-04-22 21:21     ` [PATCH 4/8] PM / Sleep: Add wakeup_source_activate and wakeup_source_deactivate tracepoints Rafael J. Wysocki
2012-04-22 21:22     ` [RFC][PATCH 5/8] epoll: Add a flag, EPOLLWAKEUP, to prevent suspend while epoll events are ready Rafael J. Wysocki
2012-04-26  4:03       ` NeilBrown
2012-04-26 20:40         ` Rafael J. Wysocki
2012-04-27  3:49           ` Arve Hjønnevåg
2012-04-27 21:18             ` Rafael J. Wysocki
2012-04-27 23:26               ` [PATCH] " Arve Hjønnevåg
2012-04-30  1:58             ` [RFC][PATCH 5/8] " NeilBrown
2012-05-01  0:52               ` Arve Hjønnevåg
2012-05-01  2:18                 ` NeilBrown
2012-05-01  5:33                 ` [PATCH] " Arve Hjønnevåg
2012-05-01  6:28                   ` NeilBrown
2012-05-01 13:51                     ` Rafael J. Wysocki
2012-07-16  6:38                   ` Michael Kerrisk
2012-07-16 11:00                     ` Rafael J. Wysocki
2012-07-16 22:04                       ` Arve Hjønnevåg
2012-07-17  5:14                         ` Michael Kerrisk
2012-07-17 19:22                           ` Rafael J. Wysocki
2012-07-17 19:36                             ` Greg KH
2012-07-17 19:55                               ` Rafael J. Wysocki
2012-07-18  6:41                             ` Michael Kerrisk (man-pages)
2012-04-22 21:23     ` [RFC][PATCH 6/8] PM / Sleep: Implement opportunistic sleep Rafael J. Wysocki
2012-04-26  3:05       ` NeilBrown
2012-04-26 21:52         ` Rafael J. Wysocki
2012-04-27  0:39           ` NeilBrown
2012-04-27 21:22             ` Rafael J. Wysocki
2012-05-03  0:23           ` Arve Hjønnevåg
2012-05-03 13:28             ` Rafael J. Wysocki
2012-05-03 21:27               ` Arve Hjønnevåg
2012-05-03 22:20                 ` Rafael J. Wysocki
2012-05-03 22:16                   ` Arve Hjønnevåg
2012-05-03 22:24                     ` Rafael J. Wysocki
2012-04-22 21:24     ` [RFC][PATCH 7/8] PM / Sleep: Add "prevent autosleep time" statistics to wakeup sources Rafael J. Wysocki
2012-04-22 21:24     ` [RFC][PATCH 8/8] PM / Sleep: Add user space interface for manipulating " Rafael J. Wysocki
2012-04-24  1:35       ` John Stultz
2012-04-24 21:27         ` Rafael J. Wysocki
2012-04-26  6:31           ` NeilBrown
2012-04-26 22:04             ` Rafael J. Wysocki
2012-04-27  0:07               ` NeilBrown
2012-04-27 21:15                 ` Rafael J. Wysocki
2012-04-27  3:57               ` Arve Hjønnevåg
2012-04-27 21:14                 ` Rafael J. Wysocki
2012-04-27 21:17                   ` Arve Hjønnevåg
2012-04-27 21:34                     ` Rafael J. Wysocki
2012-05-03 19:29                       ` [PATCH 0/2]: Kconfig options for wakelocks limit and gc (was: Re: [RFC][PATCH 8/8] PM / Sleep: Add user space ...) Rafael J. Wysocki
2012-05-03 19:30                         ` [PATCH 1/2] PM / Sleep: Make the limit of user space wakeup sources configurable Rafael J. Wysocki
2012-05-03 19:34                         ` [PATCH 2/2] PM / Sleep: User space wakeup sources garbage collector Kconfig option Rafael J. Wysocki
2012-05-03 22:14                         ` [PATCH 0/2]: Kconfig options for wakelocks limit and gc (was: Re: [RFC][PATCH 8/8] PM / Sleep: Add user space ...) Arve Hjønnevåg
2012-05-03 22:20                           ` Rafael J. Wysocki
2012-04-23 16:49     ` [RFC][PATCH 0/8] PM: Implement autosleep and "wake locks", take 3 Greg KH
2012-04-23 19:51       ` Rafael J. Wysocki

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=201202262157.18496.rjw@sisk.pl \
    --to=rjw@sisk.pl \
    --cc=arve@android.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=magnus.damm@gmail.com \
    --cc=markgross@thegnar.org \
    --cc=matthltc@us.ibm.com \
    --cc=mjg@redhat.com \
    --cc=neilb@suse.de \
    --cc=stern@rowland.harvard.edu \
    --cc=swetland@google.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;
as well as URLs for NNTP newsgroup(s).