Skip to contentSkip to conversation

timerfd c/r support, v5

PATCH In review Cyrill Gorcunov 4 participants 10 messages Jul 15, 2014 – Apr 13, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover timerfd c/r support, v5

Hi, sorry for spamming, but in v4 I simply messed up in the series, doing reply-to-reply with updated variants. So here are all patches up to date on top of -rc5. Sorry again for resend.

#1 lore raw

PATCH 1/4 timerfd: Implement show_fdinfo method

For checkpoint/restore of timerfd files we need to know how exactly the timer were armed, to be able to recreate it on restore stage. Thus implement show_fdinfo method which provides enough information for that.

One of significant changes I think is the addition of @settime_flags member. Currently there are two flags TFD_TIMER_ABSTIME and TFD_TIMER_CANCEL_ON_SET, and the second can be found from @might_cancel variable but in case if the flags will be extended in future we most probably will have to somehow remember them explicitly anyway so I guss doing that right now won't hurt.

To not bloat the timerfd_ctx structure I've converted @expired to short integer and defined @settime_flags as short too.

v2 (by avagin@, vdavydov@ and tglx@):

- Add it_value/it_interval fields
- Save flags being used in timerfd_setup in context

v3 (by tglx@):
- don't forget to use CONFIG_PROC_FS

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

CC Thomas Gleixner <[email protected]>
CC Andrew Morton <akpm-de/[email protected]>
CC Andrey Vagin <[email protected]>
CC Pavel Emelyanov <[email protected]>
CC Vladimir Davydov <[email protected]>
---
 fs/timerfd.c |   34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
fs/timerfd.c +33 -1
Index: linux-2.6.git/fs/timerfd.c
===================================================================
@@ -35,8 +35,9 @@ struct timerfd_ctx {
 	ktime_t moffs;
 	wait_queue_head_t wqh;
 	u64 ticks;
-	int expired;
 	int clockid;
+	short unsigned expired;
+	short unsigned settime_flags;	/* to show in fdinfo */
 	struct rcu_head rcu;
 	struct list_head clist;
 	bool might_cancel;
@@ -196,6 +197,8 @@ static int timerfd_setup(struct timerfd_
 		if (timerfd_canceled(ctx))
 			return -ECANCELED;
 	}
+
+	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
 	return 0;
 }
 
@@ -284,11 +287,40 @@ static ssize_t timerfd_read(struct file
 	return res;
 }
 
+#ifdef CONFIG_PROC_FS
+static int timerfd_show(struct seq_file *m, struct file *file)
+{
+	struct timerfd_ctx *ctx = file->private_data;
+	struct itimerspec t;
+
+	spin_lock_irq(&ctx->wqh.lock);
+	t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
+	t.it_interval = ktime_to_timespec(ctx->tintv);
+	spin_unlock_irq(&ctx->wqh.lock);
+
+	return seq_printf(m,
+			  "clockid: %d\n"
+			  "ticks: %llu\n"
+			  "settime flags: 0%o\n"
+			  "it_value: (%llu, %llu)\n"
+			  "it_interval: (%llu, %llu)\n",
+			  ctx->clockid, (unsigned long long)ctx->ticks,
+			  ctx->settime_flags,
+			  (unsigned long long)t.it_value.tv_sec,
+			  (unsigned long long)t.it_value.tv_nsec,
+			  (unsigned long long)t.it_interval.tv_sec,
+			  (unsigned long long)t.it_interval.tv_nsec);
+}
+#else
+#define timerfd_show 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,
 };
 
 static int timerfd_fget(int fd, struct fd *p)
#2 lore raw

[tip:timers/core] timerfd: Implement show_fdinfo method

timerfd: Implement show_fdinfo method
Commit-ID:  af9c4957cf212ad9cf0bee34c95cb11de5426e85
Gitweb:     http://git.kernel.org/tip/af9c4957cf212ad9cf0bee34c95cb11de5426e85
Author:     Cyrill Gorcunov <[email protected]>
AuthorDate: Wed, 16 Jul 2014 01:54:52 +0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 18 Jul 2014 11:49:57 +0200

timerfd: Implement show_fdinfo method

For checkpoint/restore of timerfd files we need to know how exactly the timer were armed, to be able to recreate it on restore stage. Thus implement show_fdinfo method which provides enough information for that.

One of significant changes I think is the addition of @settime_flags member. Currently there are two flags TFD_TIMER_ABSTIME and TFD_TIMER_CANCEL_ON_SET, and the second can be found from @might_cancel variable but in case if the flags will be extended in future we most probably will have to somehow remember them explicitly anyway so I guss doing that right now won't hurt.

To not bloat the timerfd_ctx structure I've converted @expired to short integer and defined @settime_flags as short too.

v2 (by avagin@, vdavydov@ and tglx@):

- Add it_value/it_interval fields
- Save flags being used in timerfd_setup in context

v3 (by tglx@):
- don't forget to use CONFIG_PROC_FS

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

---
 fs/timerfd.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
fs/timerfd.c +33 -1
index 0013142..77183f0 100644
@@ -35,8 +35,9 @@ struct timerfd_ctx {
 	ktime_t moffs;
 	wait_queue_head_t wqh;
 	u64 ticks;
-	int expired;
 	int clockid;
+	short unsigned expired;
+	short unsigned settime_flags;	/* to show in fdinfo */
 	struct rcu_head rcu;
 	struct list_head clist;
 	bool might_cancel;
@@ -196,6 +197,8 @@ static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
 		if (timerfd_canceled(ctx))
 			return -ECANCELED;
 	}
+
+	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
 	return 0;
 }
 
@@ -284,11 +287,40 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
 	return res;
 }
 
+#ifdef CONFIG_PROC_FS
+static int timerfd_show(struct seq_file *m, struct file *file)
+{
+	struct timerfd_ctx *ctx = file->private_data;
+	struct itimerspec t;
+
+	spin_lock_irq(&ctx->wqh.lock);
+	t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
+	t.it_interval = ktime_to_timespec(ctx->tintv);
+	spin_unlock_irq(&ctx->wqh.lock);
+
+	return seq_printf(m,
+			  "clockid: %d\n"
+			  "ticks: %llu\n"
+			  "settime flags: 0%o\n"
+			  "it_value: (%llu, %llu)\n"
+			  "it_interval: (%llu, %llu)\n",
+			  ctx->clockid, (unsigned long long)ctx->ticks,
+			  ctx->settime_flags,
+			  (unsigned long long)t.it_value.tv_sec,
+			  (unsigned long long)t.it_value.tv_nsec,
+			  (unsigned long long)t.it_interval.tv_sec,
+			  (unsigned long long)t.it_interval.tv_nsec);
+}
+#else
+#define timerfd_show 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,
 };
 
 static int timerfd_fget(int fd, struct fd *p)
#3 lore raw

PATCH 2/4 docs: procfs -- Document timerfd output

In reply to Cyrill Gorcunov
CC Thomas Gleixner <[email protected]>
CC Andrew Morton <akpm-de/[email protected]>
CC Andrey Vagin <[email protected]>
CC Pavel Emelyanov <[email protected]>
CC Vladimir Davydov <[email protected]>
---
 Documentation/filesystems/proc.txt |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
Documentation/filesystems/proc.txt +19 -0
Index: linux-2.6.git/Documentation/filesystems/proc.txt
===================================================================
@@ -1743,6 +1743,25 @@ pair provide additional information part
 	While the first three lines are mandatory and always printed, the rest is
 	optional and may be omitted if no marks created yet.
 
+	Timerfd files
+	~~~~~~~~~~~~~
+
+	pos:	0
+	flags:	02
+	mnt_id:	9
+	clockid: 0
+	ticks: 0
+	settime flags: 01
+	it_value: (0, 49406829)
+	it_interval: (1, 0)
+
+	where 'clockid' is the clock type and 'ticks' is the number of the timer expirations
+	that have occurred [see timerfd_create(2) for details]. 'settime flags' are
+	flags in octal form been used to setup the timer [see timerfd_settime(2) for
+	details]. 'it_value' is remaining time until the timer exiration.
+	'it_interval' is the interval for the timer. Note the timer might be set up
+	with TIMER_ABSTIME option which will be shown in 'settime flags', but 'it_value'
+	still exhibits timer's remaining time.
 
 ------------------------------------------------------------------------------
 Configuring procfs
#4 lore raw

[tip:timers/core] docs: Procfs -- Document timerfd output

docs: Procfs -- Document timerfd output
Commit-ID:  854d06d9fc350130617debdd9e9f49d1c4ba5206
Gitweb:     http://git.kernel.org/tip/854d06d9fc350130617debdd9e9f49d1c4ba5206
Author:     Cyrill Gorcunov <[email protected]>
AuthorDate: Wed, 16 Jul 2014 01:54:53 +0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 18 Jul 2014 11:49:57 +0200

docs: Procfs -- Document timerfd output

---
 Documentation/filesystems/proc.txt | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
Documentation/filesystems/proc.txt +19 -0
index ddc531a..eb8a10e 100644
@@ -1743,6 +1743,25 @@ pair provide additional information particular to the objects they represent.
 	While the first three lines are mandatory and always printed, the rest is
 	optional and may be omitted if no marks created yet.
 
+	Timerfd files
+	~~~~~~~~~~~~~
+
+	pos:	0
+	flags:	02
+	mnt_id:	9
+	clockid: 0
+	ticks: 0
+	settime flags: 01
+	it_value: (0, 49406829)
+	it_interval: (1, 0)
+
+	where 'clockid' is the clock type and 'ticks' is the number of the timer expirations
+	that have occurred [see timerfd_create(2) for details]. 'settime flags' are
+	flags in octal form been used to setup the timer [see timerfd_settime(2) for
+	details]. 'it_value' is remaining time until the timer exiration.
+	'it_interval' is the interval for the timer. Note the timer might be set up
+	with TIMER_ABSTIME option which will be shown in 'settime flags', but 'it_value'
+	still exhibits timer's remaining time.
 
 ------------------------------------------------------------------------------
 Configuring procfs
#5 lore raw

PATCH 3/4 timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks, v3

In reply to Cyrill Gorcunov

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 <akpm-de/[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]>
---
 fs/timerfd.c            |   37 +++++++++++++++++++++++++++++++++++++
 include/linux/timerfd.h |    5 +++++
 2 files changed, 42 insertions(+)
fs/timerfd.c +37 -0
Index: 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)
include/linux/timerfd.h +5 -0
Index: 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 */
#6 lore raw

[tip:timers/core] timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks, v3

timerfd: Implement timerfd_ioctl method to restore timerfd_ctx::ticks, v3
Commit-ID:  5442e9fbd7c23172a1c9bc736629cd123a9923f0
Gitweb:     http://git.kernel.org/tip/5442e9fbd7c23172a1c9bc736629cd123a9923f0
Author:     Cyrill Gorcunov <[email protected]>
AuthorDate: Wed, 16 Jul 2014 01:54:54 +0400
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 18 Jul 2014 11:49:57 +0200

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 Andrew Morton <[email protected]>
Cc Michael Kerrisk <[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]>
---
 fs/timerfd.c            | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/timerfd.h |  5 +++++
 2 files changed, 42 insertions(+)
fs/timerfd.c +37 -0
index 77183f0..709603c 100644
@@ -315,12 +315,49 @@ static int timerfd_show(struct seq_file *m, struct file *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)
include/linux/timerfd.h +5 -0
index d3b57fa..bd36ce4 100644
@@ -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 */
#7 lore raw

PATCH 4/4 timerfd.2: Add ioctl method description

In reply to Cyrill Gorcunov

ioctl(2)
The following commands are supported: TFD_IOC_SET_TICKS to adjust the number of the timer expirations that have occurred. It take a pointer to nonzero 8-byte integer (uint64_t*) containing new number of expirations. Once the number is set any waiter on the timer is woken up. The only purpose of this command is to restore the expirations in a sake of checkpoint/restore procedure. It requires the kernel to be built with CONFIG_CHECKPOINT_RESTORE support.

CC Michael Kerrisk <[email protected]>
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]>
---
 man2/timerfd_create.2 |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
man2/timerfd_create.2 +14 -0
Index: man-pages/man2/timerfd_create.2
===================================================================
@@ -260,6 +260,20 @@ multiplexing APIs:
 and
 .BR epoll (7).
 .TP
+.BR ioctl "(2)"
+The following commands are supported:
+.B TFD_IOC_SET_TICKS
+to adjust the number of the timer expirations that have occurred.
+It take a pointer to nonzero 8-byte integer
+.RI ( uint64_t *)
+containing the new number of expirations.
+Once the number is set any waiter on the timer is woken up.
+The only purpose of this command is to restore the expirations
+in a sake of checkpoint/restore procedure.
+It requires the kernel to be built with
+.BR CONFIG_CHECKPOINT_RESTORE
+support.
+.TP
 .BR close (2)
 When the file descriptor is no longer required it should be closed.
 When all file descriptors associated with the same timer object
#8 lore raw

Re: [patch 4/4] timerfd.2: Add ioctl method description

Hello Cyrill,

Quoted 9 lines · Cyrill Gorcunov

ioctl(2)
The following commands are supported: TFD_IOC_SET_TICKS to adjust the number of the timer expirations that have occurred. It take a pointer to nonzero 8-byte integer (uint64_t*) containing new number of expirations. Once the number is set any waiter on the timer is woken up. The only purpose of this command is to restore the expirations in a sake of checkpoint/restore procedure. It requires the kernel to be built with CONFIG_CHECKPOINT_RESTORE support.

Late... but, patch applied. Thank you.

Cheers,

Michael

Quoted 39 lines
CC Michael Kerrisk <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Andrew Morton <akpm-de/[email protected]>
CC Andrey Vagin <[email protected]>
CC Pavel Emelyanov <[email protected]>
CC Vladimir Davydov <[email protected]>
---
 man2/timerfd_create.2 |   14 ++++++++++++++
 1 file changed, 14 insertions(+)

Index: man-pages/man2/timerfd_create.2
===================================================================

man2/timerfd_create.2 +14 -0
@@ -260,6 +260,20 @@ multiplexing APIs:
 and
 .BR epoll (7).
 .TP
+.BR ioctl "(2)"
+The following commands are supported:
+.B TFD_IOC_SET_TICKS
+to adjust the number of the timer expirations that have occurred.
+It take a pointer to nonzero 8-byte integer
+.RI ( uint64_t *)
+containing the new number of expirations.
+Once the number is set any waiter on the timer is woken up.
+The only purpose of this command is to restore the expirations
+in a sake of checkpoint/restore procedure.
+It requires the kernel to be built with
+.BR CONFIG_CHECKPOINT_RESTORE
+support.
+.TP
 .BR close (2)
 When the file descriptor is no longer required it should be closed.
 When all file descriptors associated with the same timer object
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#9 lore raw

Re: [patch 4/4] timerfd.2: Add ioctl method description

Quoted 13 lines · Michael Kerrisk (man-pages)

Hello Cyrill,

Quoted 9 lines · Cyrill Gorcunov

ioctl(2)
The following commands are supported: TFD_IOC_SET_TICKS to adjust the number of the timer expirations that have occurred. It take a pointer to nonzero 8-byte integer (uint64_t*) containing new number of expirations. Once the number is set any waiter on the timer is woken up. The only purpose of this command is to restore the expirations in a sake of checkpoint/restore procedure. It requires the kernel to be built with CONFIG_CHECKPOINT_RESTORE support.

Quoted 1 line

Late... but, patch applied. Thank you.

Sure thing! Ping me if anything else should be added.