2014-06-23 19:04:01

by Cyrill Gorcunov

[permalink] [raw]
Subject: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

The read() of timerfd files allows to fetch the number of timer ticks
while there is no way to set it back from userspace.

To restore the timer's state as it was at checkpoint moment we need
a path to bring @ticks back. Initially I thought about writing ticks
back via write() interface but it seems such API is somehow obscure.

Instead implement timerfd_ioctl() method with TFD_IOC_SET_TICKS
command which allows to adjust @ticks into non-zero value waking
up the waiters. The reason to forbid from

I wrapped code with CONFIG_CHECKPOINT_RESTORE which can be
dropped off if there users except c/r camp appear.

CC: Thomas Gleixner <[email protected]>
CC: Andrew Morton <[email protected]>
CC: Andrey Vagin <[email protected]>
CC: Pavel Emelyanov <[email protected]>
CC: Vladimir Davydov <[email protected]>
Signed-off-by: Cyrill Gorcunov <[email protected]>
---
fs/timerfd.c | 37 +++++++++++++++++++++++++++++++++++++
include/linux/timerfd.h | 5 +++++
2 files changed, 42 insertions(+)

Index: linux-2.6.git/fs/timerfd.c
===================================================================
--- linux-2.6.git.orig/fs/timerfd.c
+++ linux-2.6.git/fs/timerfd.c
@@ -313,11 +313,48 @@ static int timerfd_show(struct seq_file
}
#endif

+#ifdef CONFIG_CHECKPOINT_RESTORE
+static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct timerfd_ctx *ctx = file->private_data;
+ int ret = 0;
+
+ switch (cmd) {
+ case TFD_IOC_SET_TICKS: {
+ u64 ticks;
+
+ if (get_user(ticks, (u64 __user *)arg))
+ return -EFAULT;
+ if (!ticks)
+ return -EINVAL;
+
+ spin_lock_irq(&ctx->wqh.lock);
+ if (!timerfd_canceled(ctx)) {
+ ctx->ticks = ticks;
+ if (ticks)
+ wake_up_locked(&ctx->wqh);
+ } else
+ ret = -ECANCELED;
+ spin_unlock_irq(&ctx->wqh.lock);
+ break;
+ }
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ return ret;
+}
+#endif
+
static const struct file_operations timerfd_fops = {
.release = timerfd_release,
.poll = timerfd_poll,
.read = timerfd_read,
.llseek = noop_llseek,
+#ifdef CONFIG_CHECKPOINT_RESTORE
+ .unlocked_ioctl = timerfd_ioctl,
+#endif
#ifdef CONFIG_PROC_FS
.show_fdinfo = timerfd_show,
#endif
Index: linux-2.6.git/include/linux/timerfd.h
===================================================================
--- linux-2.6.git.orig/include/linux/timerfd.h
+++ linux-2.6.git/include/linux/timerfd.h
@@ -11,6 +11,9 @@
/* For O_CLOEXEC and O_NONBLOCK */
#include <linux/fcntl.h>

+/* For _IO helpers */
+#include <linux/ioctl.h>
+
/*
* CAREFUL: Check include/asm-generic/fcntl.h when defining
* new flags, since they might collide with O_* ones. We want
@@ -29,4 +32,6 @@
/* Flags for timerfd_settime. */
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)

+#define TFD_IOC_SET_TICKS _IOW('T', 0, u64)
+
#endif /* _LINUX_TIMERFD_H */


2014-06-24 22:03:56

by Cyrill Gorcunov

[permalink] [raw]
Subject: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

The read() of timerfd files allows to fetch the number of timer ticks
while there is no way to set it back from userspace.

To restore the timer's state as it was at checkpoint moment we need
a path to bring @ticks back. Initially I thought about writing ticks
back via write() interface but it seems such API is somehow obscure.

Instead implement timerfd_ioctl() method with TFD_IOC_SET_TICKS
command which allows to adjust @ticks into non-zero value waking
up the waiters.

I wrapped code with CONFIG_CHECKPOINT_RESTORE which can be
dropped off if there users except c/r camp appear.

v2 (by akpm@):
-Use define timerfd_ioctl NULL for non c/r config

CC: Thomas Gleixner <[email protected]>
CC: Andrew Morton <[email protected]>
CC: Andrey Vagin <[email protected]>
CC: Pavel Emelyanov <[email protected]>
CC: Vladimir Davydov <[email protected]>
Signed-off-by: Cyrill Gorcunov <[email protected]>
---

A nit fixed for for non c/r config in timerfd_ioctl declaration

fs/timerfd.c | 37 +++++++++++++++++++++++++++++++++++++
include/linux/timerfd.h | 5 +++++
2 files changed, 42 insertions(+)

Index: linux-2.6.git/fs/timerfd.c
===================================================================
--- linux-2.6.git.orig/fs/timerfd.c
+++ linux-2.6.git/fs/timerfd.c
@@ -315,12 +315,49 @@ static int timerfd_show(struct seq_file
#define timerfd_show NULL
#endif

+#ifdef CONFIG_CHECKPOINT_RESTORE
+static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct timerfd_ctx *ctx = file->private_data;
+ int ret = 0;
+
+ switch (cmd) {
+ case TFD_IOC_SET_TICKS: {
+ u64 ticks;
+
+ if (get_user(ticks, (u64 __user *)arg))
+ return -EFAULT;
+ if (!ticks)
+ return -EINVAL;
+
+ spin_lock_irq(&ctx->wqh.lock);
+ if (!timerfd_canceled(ctx)) {
+ ctx->ticks = ticks;
+ if (ticks)
+ wake_up_locked(&ctx->wqh);
+ } else
+ ret = -ECANCELED;
+ spin_unlock_irq(&ctx->wqh.lock);
+ break;
+ }
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ return ret;
+}
+#else
+#define timerfd_ioctl NULL
+#endif
+
static const struct file_operations timerfd_fops = {
.release = timerfd_release,
.poll = timerfd_poll,
.read = timerfd_read,
.llseek = noop_llseek,
.show_fdinfo = timerfd_show,
+ .unlocked_ioctl = timerfd_ioctl,
};

static int timerfd_fget(int fd, struct fd *p)
Index: linux-2.6.git/include/linux/timerfd.h
===================================================================
--- linux-2.6.git.orig/include/linux/timerfd.h
+++ linux-2.6.git/include/linux/timerfd.h
@@ -11,6 +11,9 @@
/* For O_CLOEXEC and O_NONBLOCK */
#include <linux/fcntl.h>

+/* For _IO helpers */
+#include <linux/ioctl.h>
+
/*
* CAREFUL: Check include/asm-generic/fcntl.h when defining
* new flags, since they might collide with O_* ones. We want
@@ -29,4 +32,6 @@
/* Flags for timerfd_settime. */
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)

+#define TFD_IOC_SET_TICKS _IOW('T', 0, u64)
+
#endif /* _LINUX_TIMERFD_H */

2014-07-02 16:49:58

by Christopher Covington

[permalink] [raw]
Subject: Re: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

Hi Cyrill,

On 06/24/2014 06:03 PM, Cyrill Gorcunov wrote:
> The read() of timerfd files allows to fetch the number of timer ticks
> while there is no way to set it back from userspace.
>
> To restore the timer's state as it was at checkpoint moment we need
> a path to bring @ticks back. Initially I thought about writing ticks
> back via write() interface but it seems such API is somehow obscure.
>
> Instead implement timerfd_ioctl() method with TFD_IOC_SET_TICKS
> command which allows to adjust @ticks into non-zero value waking
> up the waiters.
>
> I wrapped code with CONFIG_CHECKPOINT_RESTORE which can be
> dropped off if there users except c/r camp appear.
>
> v2 (by akpm@):
> -Use define timerfd_ioctl NULL for non c/r config
>
> CC: Thomas Gleixner <[email protected]>
> CC: Andrew Morton <[email protected]>
> CC: Andrey Vagin <[email protected]>
> CC: Pavel Emelyanov <[email protected]>
> CC: Vladimir Davydov <[email protected]>
> Signed-off-by: Cyrill Gorcunov <[email protected]>
> ---
>
> A nit fixed for for non c/r config in timerfd_ioctl declaration
>
> fs/timerfd.c | 37 +++++++++++++++++++++++++++++++++++++
> include/linux/timerfd.h | 5 +++++
> 2 files changed, 42 insertions(+)
>
> Index: linux-2.6.git/fs/timerfd.c
> ===================================================================
> --- linux-2.6.git.orig/fs/timerfd.c
> +++ linux-2.6.git/fs/timerfd.c
> @@ -315,12 +315,49 @@ static int timerfd_show(struct seq_file
> #define timerfd_show NULL
> #endif
>
> +#ifdef CONFIG_CHECKPOINT_RESTORE
> +static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> +{
> + struct timerfd_ctx *ctx = file->private_data;
> + int ret = 0;
> +
> + switch (cmd) {
> + case TFD_IOC_SET_TICKS: {
> + u64 ticks;
> +
> + if (get_user(ticks, (u64 __user *)arg))

64-bit get_user is currently unsupported on ARM, although it appears work is
ongoing [1].

1. https://lkml.org/lkml/2014/6/17/260

Regards,
Christopher

--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by the Linux Foundation.

2014-07-02 17:04:22

by Cyrill Gorcunov

[permalink] [raw]
Subject: Re: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

On Wed, Jul 02, 2014 at 12:49:51PM -0400, Christopher Covington wrote:
> >
> > +#ifdef CONFIG_CHECKPOINT_RESTORE
> > +static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> > +{
> > + struct timerfd_ctx *ctx = file->private_data;
> > + int ret = 0;
> > +
> > + switch (cmd) {
> > + case TFD_IOC_SET_TICKS: {
> > + u64 ticks;
> > +
> > + if (get_user(ticks, (u64 __user *)arg))
>
> 64-bit get_user is currently unsupported on ARM, although it appears work is
> ongoing [1].
>
> 1. https://lkml.org/lkml/2014/6/17/260

Thanks for info, Christopher! What arm camp is using then, copy-from-user?

2014-07-02 19:01:26

by Arnd Bergmann

[permalink] [raw]
Subject: Re: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

On Wednesday 02 July 2014 21:04:16 Cyrill Gorcunov wrote:
> On Wed, Jul 02, 2014 at 12:49:51PM -0400, Christopher Covington wrote:
> > >
> > > +#ifdef CONFIG_CHECKPOINT_RESTORE
> > > +static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> > > +{
> > > + struct timerfd_ctx *ctx = file->private_data;
> > > + int ret = 0;
> > > +
> > > + switch (cmd) {
> > > + case TFD_IOC_SET_TICKS: {
> > > + u64 ticks;
> > > +
> > > + if (get_user(ticks, (u64 __user *)arg))
> >
> > 64-bit get_user is currently unsupported on ARM, although it appears work is
> > ongoing [1].
> >
> > 1. https://lkml.org/lkml/2014/6/17/260
>
> Thanks for info, Christopher! What arm camp is using then, copy-from-user?
>

copy_from_user should work on all architectures. I believe a 64-bit get_user
is currently unsupported on most 32-bit architectures, x86-32 being a notable
exception.

Arnd

2014-07-02 19:07:11

by Cyrill Gorcunov

[permalink] [raw]
Subject: Re: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

On Wed, Jul 02, 2014 at 09:01:02PM +0200, Arnd Bergmann wrote:
> > >
> > > 64-bit get_user is currently unsupported on ARM, although it appears work is
> > > ongoing [1].
> > >
> > > 1. https://lkml.org/lkml/2014/6/17/260
> >
> > Thanks for info, Christopher! What arm camp is using then, copy-from-user?
> >
>
> copy_from_user should work on all architectures. I believe a 64-bit get_user
> is currently unsupported on most 32-bit architectures, x86-32 being a notable
> exception.

Thanks, I'll update.

2014-07-02 19:37:06

by Cyrill Gorcunov

[permalink] [raw]
Subject: Re: [patch 3/4] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks

Updated variant, thanks a lot for feedback!

---
From: Cyrill Gorcunov <[email protected]>
Subject: timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks, v3

The read() of timerfd files allows to fetch the number of timer ticks
while there is no way to set it back from userspace.

To restore the timer's state as it was at checkpoint moment we need
a path to bring @ticks back. Initially I thought about writing ticks
back via write() interface but it seems such API is somehow obscure.

Instead implement timerfd_ioctl() method with TFD_IOC_SET_TICKS
command which allows to adjust @ticks into non-zero value waking
up the waiters.

I wrapped code with CONFIG_CHECKPOINT_RESTORE which can be
dropped off if there users except c/r camp appear.

v2 (by akpm@):
- Use define timerfd_ioctl NULL for non c/r config

v3:
- Use copy_from_user for @ticks fetching since
not all arch support get_user for 8 byte argument

CC: Thomas Gleixner <[email protected]>
CC: Andrew Morton <[email protected]>
CC: Andrey Vagin <[email protected]>
CC: Arnd Bergmann <[email protected]>
CC: Christopher Covington <[email protected]>
CC: Pavel Emelyanov <[email protected]>
CC: Vladimir Davydov <[email protected]>
Signed-off-by: Cyrill Gorcunov <[email protected]>
---
fs/timerfd.c | 37 +++++++++++++++++++++++++++++++++++++
include/linux/timerfd.h | 5 +++++
2 files changed, 42 insertions(+)

Index: linux-2.6.git/fs/timerfd.c
===================================================================
--- linux-2.6.git.orig/fs/timerfd.c
+++ linux-2.6.git/fs/timerfd.c
@@ -315,12 +315,49 @@ static int timerfd_show(struct seq_file
#define timerfd_show NULL
#endif

+#ifdef CONFIG_CHECKPOINT_RESTORE
+static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct timerfd_ctx *ctx = file->private_data;
+ int ret = 0;
+
+ switch (cmd) {
+ case TFD_IOC_SET_TICKS: {
+ u64 ticks;
+
+ if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
+ return -EFAULT;
+ if (!ticks)
+ return -EINVAL;
+
+ spin_lock_irq(&ctx->wqh.lock);
+ if (!timerfd_canceled(ctx)) {
+ ctx->ticks = ticks;
+ if (ticks)
+ wake_up_locked(&ctx->wqh);
+ } else
+ ret = -ECANCELED;
+ spin_unlock_irq(&ctx->wqh.lock);
+ break;
+ }
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ return ret;
+}
+#else
+#define timerfd_ioctl NULL
+#endif
+
static const struct file_operations timerfd_fops = {
.release = timerfd_release,
.poll = timerfd_poll,
.read = timerfd_read,
.llseek = noop_llseek,
.show_fdinfo = timerfd_show,
+ .unlocked_ioctl = timerfd_ioctl,
};

static int timerfd_fget(int fd, struct fd *p)
Index: linux-2.6.git/include/linux/timerfd.h
===================================================================
--- linux-2.6.git.orig/include/linux/timerfd.h
+++ linux-2.6.git/include/linux/timerfd.h
@@ -11,6 +11,9 @@
/* For O_CLOEXEC and O_NONBLOCK */
#include <linux/fcntl.h>

+/* For _IO helpers */
+#include <linux/ioctl.h>
+
/*
* CAREFUL: Check include/asm-generic/fcntl.h when defining
* new flags, since they might collide with O_* ones. We want
@@ -29,4 +32,6 @@
/* Flags for timerfd_settime. */
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)

+#define TFD_IOC_SET_TICKS _IOW('T', 0, u64)
+
#endif /* _LINUX_TIMERFD_H */