All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Shishkin <virtuoso@slind.org>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: Alexander Shishkin <virtuoso@slind.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	John Stultz <johnstul@us.ibm.com>,
	Chris Friesen <chris.friesen@genband.com>,
	Kay Sievers <kay.sievers@vrfy.org>,
	"Kirill A. Shutemov" <kirill@shutemov.name>,
	linux-kernel@vger.kernel.org
Subject: [RFC][PATCH 4/4] timerfd: add cancellation
Date: Wed, 27 Apr 2011 13:43:43 +0300	[thread overview]
Message-ID: <1303901023-11568-4-git-send-email-virtuoso@slind.org> (raw)
In-Reply-To: <1303901023-11568-1-git-send-email-virtuoso@slind.org>

This patch implements conditional cancellation for timerfd timers.
Similarly to clock_nanosleep, users who want to be woken up when
system time changes have to use TFD_CANCEL_ON_CLOCK_SET flag and
monotonic vs realtime offset, obtained from clock_rtoffset system
call, in otmr.it_interval to timerfd_settime() call. This is only
supported for absolute timers (TFD_TIMER_ABSTIME).

If the provided monotonic offset is still effective, poll on the
timerfd will sleep until either the requested time comes or somebody
changes the system time, in which case the system call will return
POLLHUP. If the monotonic offset has changed by the time of calling
timerfd_settime(), it will return ECANCELLED straight away. This
seems more straightforward than returning POLLIN from poll and only
returning error from read.

Signed-off-by: Alexander Shishkin <virtuoso@slind.org>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: John Stultz <johnstul@us.ibm.com>
CC: Chris Friesen <chris.friesen@genband.com>
CC: Kay Sievers <kay.sievers@vrfy.org>
CC: Kirill A. Shutemov <kirill@shutemov.name>
CC: linux-kernel@vger.kernel.org
---
 fs/timerfd.c            |   40 +++++++++++++++++++++++++++++++++++++++-
 include/linux/timerfd.h |    3 ++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/fs/timerfd.c b/fs/timerfd.c
index 8c4fc14..1955552 100644
--- a/fs/timerfd.c
+++ b/fs/timerfd.c
@@ -59,6 +59,16 @@ static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
 	return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
 }
 
+static void timerfd_canceller(struct hrtimer *timer)
+{
+	struct timerfd_ctx *ctx = container_of(timer, struct timerfd_ctx, tmr);
+	unsigned long flags;
+
+	spin_lock_irqsave(&ctx->wqh.lock, flags);
+	wake_up_locked(&ctx->wqh);
+	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
+}
+
 static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
 			  const struct itimerspec *ktmr)
 {
@@ -99,6 +109,8 @@ static unsigned int timerfd_poll(struct file *file, poll_table *wait)
 	spin_lock_irqsave(&ctx->wqh.lock, flags);
 	if (ctx->ticks)
 		events |= POLLIN;
+	if (ctx->tmr.cancel.cancelled)
+		events |= POLLHUP;
 	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
 
 	return events;
@@ -117,7 +129,15 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
 	if (file->f_flags & O_NONBLOCK)
 		res = -EAGAIN;
 	else
-		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
+		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks
+						 || ctx->tmr.cancel.cancelled);
+
+	/* if the timer is being cancelled, don't rearm it */
+	if (ctx->tmr.cancel.cancelled) {
+		spin_unlock_irq(&ctx->wqh.lock);
+		return -ECANCELED;
+	}
+
 	if (ctx->ticks) {
 		ticks = ctx->ticks;
 		if (ctx->expired && ctx->tintv.tv64) {
@@ -197,6 +217,7 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
 		struct itimerspec __user *, otmr)
 {
 	struct file *file;
+	struct timespec offset;
 	struct timerfd_ctx *ctx;
 	struct itimerspec ktmr, kotmr;
 
@@ -208,6 +229,14 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
 	    !timespec_valid(&ktmr.it_interval))
 		return -EINVAL;
 
+	if (flags & TFD_CANCEL_ON_CLOCK_SET) {
+		if (!otmr || !(flags & TFD_TIMER_ABSTIME))
+			return -EINVAL;
+		if (copy_from_user(&kotmr, otmr, sizeof(kotmr)))
+			return -EFAULT;
+		offset = kotmr.it_interval;
+	}
+
 	file = timerfd_fget(ufd);
 	if (IS_ERR(file))
 		return PTR_ERR(file);
@@ -242,6 +271,15 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
 	 */
 	timerfd_setup(ctx, flags, &ktmr);
 
+	if ((flags & TFD_CANCEL_ON_CLOCK_SET) &&
+	    hrtimer_set_cancel_on_clock_set(&ctx->tmr, &offset,
+					    timerfd_canceller)) {
+		hrtimer_cancel(&ctx->tmr);
+		spin_unlock_irq(&ctx->wqh.lock);
+		fput(file);
+		return -ECANCELED;
+	}
+
 	spin_unlock_irq(&ctx->wqh.lock);
 	fput(file);
 	if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
diff --git a/include/linux/timerfd.h b/include/linux/timerfd.h
index 2d07929..f4cd638 100644
--- a/include/linux/timerfd.h
+++ b/include/linux/timerfd.h
@@ -19,6 +19,7 @@
  * shared O_* flags.
  */
 #define TFD_TIMER_ABSTIME (1 << 0)
+#define TFD_CANCEL_ON_CLOCK_SET (1 << 1)
 #define TFD_CLOEXEC O_CLOEXEC
 #define TFD_NONBLOCK O_NONBLOCK
 
@@ -26,6 +27,6 @@
 /* Flags for timerfd_create.  */
 #define TFD_CREATE_FLAGS TFD_SHARED_FCNTL_FLAGS
 /* Flags for timerfd_settime.  */
-#define TFD_SETTIME_FLAGS TFD_TIMER_ABSTIME
+#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_CANCEL_ON_CLOCK_SET)
 
 #endif /* _LINUX_TIMERFD_H */
-- 
1.7.4.1


  parent reply	other threads:[~2011-04-27 10:45 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-09 14:36 [RFCv4] timerfd: add TFD_NOTIFY_CLOCK_SET to watch for clock changes Alexander Shishkin
2011-03-10  0:25 ` Andrew Morton
2011-03-10  0:25   ` Andrew Morton
2011-03-10  0:36   ` Kay Sievers
2011-03-10  0:36     ` Kay Sievers
2011-03-10  8:19     ` Alexander Shishkin
2011-03-10  8:19       ` Alexander Shishkin
2011-03-10  9:08     ` Thomas Gleixner
2011-03-10 11:16       ` Jamie Lokier
2011-03-10 11:16         ` Jamie Lokier
2011-03-10 11:41         ` Thomas Gleixner
2011-03-10 11:41           ` Thomas Gleixner
2011-03-10  2:01   ` Scott James Remnant
2011-03-10  2:01     ` Scott James Remnant
2011-03-10  8:25     ` Andrew Morton
2011-03-10  8:25       ` Andrew Morton
2011-03-11 19:51       ` Scott James Remnant
2011-03-11 19:51         ` Scott James Remnant
2011-03-11 19:56         ` Thomas Gleixner
2011-03-11 19:56           ` Thomas Gleixner
2011-03-15  1:53           ` Scott James Remnant
2011-03-15  1:53             ` Scott James Remnant
2011-03-10  8:10   ` Alexander Shishkin
2011-03-10  8:02 ` Kirill A. Shutemov
2011-03-10  8:15   ` Alexander Shishkin
2011-03-10  8:48 ` Arnd Bergmann
2011-03-10 14:19   ` Alexander Shishkin
2011-03-10  9:52 ` Thomas Gleixner
2011-03-10 14:12   ` Alexander Shishkin
2011-03-10 14:55     ` Thomas Gleixner
2011-03-10 15:43       ` Alexander Shishkin
2011-03-10 16:40         ` Thomas Gleixner
2011-03-10 21:57     ` Thomas Gleixner
2011-04-27 10:43       ` [RFC][PATCH 1/4] clock_rtoffset: new syscall Alexander Shishkin
2011-04-27 10:43         ` [RFC][PATCH 2/4] hrtimer: add cancellation when clock is set Alexander Shishkin
2011-04-27 10:43         ` [RFC][PATCH 3/4] hrtimer: add nanosleep cancellation Alexander Shishkin
2011-04-27 10:43         ` Alexander Shishkin [this message]
2011-04-27 14:02         ` [RFC][PATCH 1/4] clock_rtoffset: new syscall Thomas Gleixner
2011-04-27 19:11           ` john stultz
2011-04-27 22:19             ` Thomas Gleixner
2011-04-27 20:55           ` Kay Sievers
2011-04-29 17:32             ` Thomas Gleixner
2011-05-02  8:10               ` Alexander Shishkin
2011-04-28  7:15           ` Alexander Shishkin

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=1303901023-11568-4-git-send-email-virtuoso@slind.org \
    --to=virtuoso@slind.org \
    --cc=akpm@linux-foundation.org \
    --cc=chris.friesen@genband.com \
    --cc=johnstul@us.ibm.com \
    --cc=kay.sievers@vrfy.org \
    --cc=kirill@shutemov.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.