timerfd -- implement missing parts to checkpoint and restore timerfd state, v3
Subsystems (4)
cover timerfd -- implement missing parts to checkpoint and restore timerfd state, v3
Hello! This is (I hope) the final version. Most changes from v2 are sitting in documentation, where I tried to reflect that the time we're printing out is remaining until the timer expiration. Please take a look.
PATCH 1/3 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 handle it. Thus implement show_fdinfo method which provides enough information for timer re-creation.
One of significant changes I think is addition of timerfd_ctx::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 as well.
v2 (by avagin@, vdavydov@ and tglx@):
- Add it_value/it_interval fields
- Save flags being used in timerfd_setup in context
--- fs/timerfd.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
fs/timerfd.c +29 -1
@@ -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,36 @@ static ssize_t timerfd_read(struct filereturn res; } +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); +} +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)
Re: [patch 1/3] timerfd: Implement show_fdinfo method
Quoted 14 lines · Cyrill Gorcunov
fs/timerfd.c +13
-0+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"
fs/timerfd.c static ssize_t timerfd_read(struct file · line 312
Quoted 10 lines
fs/timerfd.c +10
-0+"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); +}
Shouldn't this depend on CONFIG_PROCFS?
Quoted 11 lines
static const struct
file_operationstimerfd_fops= {fs/timerfd.c +1
-0.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)
Re: [patch 1/3] timerfd: Implement show_fdinfo method
Quoted 11 lines · Thomas Gleixner
Shouldn't this depend on
CONFIG_PROCFS?Quoted 8 lines
static const struct
file_operationstimerfd_fops= {(quoted diff) +1
-0.release = timerfd_release, .poll = timerfd_poll, .read = timerfd_read, .llseek = noop_llseek, + .show_fdinfo = timerfd_show, };
Quoted 1 line
Quoted 1 line
static int
timerfd_fget(int fd, struct fd *p)
yeah, good point, thanks! Updated.
From: Cyrill Gorcunov <[email protected]>
Subject: 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 handle it. Thus implement show_fdinfo method which provides enough information for timer re-creation.
One of significant changes I think is addition of timerfd_ctx::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 as well.
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
--- fs/timerfd.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
fs/timerfd.c +33 -1
@@ -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 filereturn 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); +} +#endif +static const struct file_operations timerfd_fops = { .release = timerfd_release, .poll = timerfd_poll, .read = timerfd_read, .llseek = noop_llseek, +#ifdef CONFIG_PROC_FS + .show_fdinfo = timerfd_show, +#endif };static int timerfd_fget (int fd,struct fd *p)
PATCH 2/3 docs: procfs -- Document timerfd output
In reply to Cyrill Gorcunov--- Documentation/filesystems/proc.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
Documentation/filesystems/proc.txt +19 -0
@@ -1741,6 +1741,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 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 TFD_TIMER_ABSTIME option which will be shown in 'settime flags', + but 'it_value' still exhibit timer's remaining time. ------------------------------------------------------------------------------ Configuring procfs
PATCH 3/3 timerfd: Implement write method
In reply to Cyrill GorcunovThe 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 state as it was at checkpoint moment we need a way to setup ticks back. So as a counterpart of read() the write() takes ticks number from the userspace and updates internal timer ticks accordingly.
--- fs/timerfd.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/timerfd.c +21 -0
@@ -311,10 +311,31 @@ static int timerfd_show(struct seq_file (unsigned long long)t.it_interval.tv_nsec); } + +static ssize_t timerfd_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct timerfd_ctx *ctx = file->private_data; +u64 ticks =0 ; + +if (count <sizeof (ticks)) +return -EINVAL ; + +if (get_user (ticks, (u64 __user *) buf)) +return -EFAULT ; + +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks; +spin_unlock_irq (&ctx->wqh.lock); + +return sizeof (ticks); +} +static const struct file_operations timerfd_fops = { .release = timerfd_release, .poll = timerfd_poll, .read = timerfd_read, + .write = timerfd_write, .llseek = noop_llseek, .show_fdinfo = timerfd_show, };
Re: [patch 3/3] timerfd: Implement write method
Quoted 5 lines · Cyrill Gorcunov
fs/timerfd.c +5
-0+ +static ssize_t timerfd_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct timerfd_ctx *ctx = file->private_data;
fs/timerfd.c static int timerfd_show(struct seq_file · line 328
Quoted 10 lines
fs/timerfd.c +10
-0+u64 ticks =0 ; + +if (count <sizeof (ticks)) +return -EINVAL ; + +if (get_user (ticks, (u64 __user *) buf)) +return -EFAULT ; + +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks;
So what wakes a potential waiter in read/poll?
Thanks,
tglx
Re: [patch 3/3] timerfd: Implement write method
Quoted 17 lines · Thomas Gleixner
Quoted 4 lines · Cyrill Gorcunov
fs/timerfd.c +4
-0+static ssize_t timerfd_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct timerfd_ctx *ctx = file->private_data;fs/timerfd.c static int timerfd_show(struct seq_file · line 328
Quoted 10 lines
fs/timerfd.c +10
-0+u64 ticks =0 ; + +if (count <sizeof (ticks)) +return -EINVAL ; + +if (get_user (ticks, (u64 __user *) buf)) +return -EFAULT ; + +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks;So what wakes a potential waiter in read/poll?
Why should it? You mean the scenario when timer is armed then someone writes nonzero @ticks and we should wake waiters? The idea was to setup this ticks on timer restore without waking anyone. If it breaks the logic of timerfd in general, then sure I need to rework. Hm?
Re: [patch 3/3] timerfd: Implement write method
Quoted 19 lines · Cyrill Gorcunov
Quoted 17 lines · Thomas Gleixner
Quoted 4 lines · Cyrill Gorcunov
fs/timerfd.c +4
-0+static ssize_t timerfd_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct timerfd_ctx *ctx = file->private_data;fs/timerfd.c static int timerfd_show(struct seq_file · line 328
Quoted 10 lines
fs/timerfd.c +10
-0+u64 ticks =0 ; + +if (count <sizeof (ticks)) +return -EINVAL ; + +if (get_user (ticks, (u64 __user *) buf)) +return -EFAULT ; + +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks;So what wakes a potential waiter in read/poll?
Quoted 5 lines
Why should it? You mean the scenario when timer is armed then someone writes nonzero @ticks and we should wake waiters? The idea was to setup this ticks on timer restore without waking anyone. If it breaks the logic of timerfd in general, then sure I need to rework. Hm?
There is a world outside of checkpoint/restore, really.
So what's the semantics of that write function? We really want to have that agreed on and documented in the man page.
Right now the write will just update the ticks and nothing else. So what if there is a waiter already? What if there is a timer armed?
Can you please describe how checkpoint/restore is going to use all of this. How is the timer restored and how/when is the reader which was waiting in read/poll at the time of suspend reattached to it.
Thanks,
tglx
Re: [patch 3/3] timerfd: Implement write method
Quoted 2 lines · Thomas Gleixner
There is a world outside of checkpoint/restore, really.
Yes, I simply don't know who else might use this write() functionality for other purpose, I mean i don't see a point to use it for anything else.
Quoted 2 lines
So what's the semantics of that write function? We really want to have that agreed on and documented in the man page.
The idea was to provide a way to setup @ticks into (nonzero) value which we get from show_fdinfo output. Then when we restore it we setup the timer and set @ticks to the value it had at dump moment.
Quoted 6 lines
Right now the write will just update the ticks and nothing else. So what if there is a waiter already? What if there is a timer armed?
Can you please describe how checkpoint/restore is going to use all of this. How is the timer restored and how/when is the reader which was waiting in read/poll at the time of suspend reattached to it.
Thomas, I see what you mean. Need to think (I must admit I forgot about polling of timerfds :( I were to restore timerfds like this
- fetch data from fdinfo
- use timer_create/settime to arm it
- write @ticks then
but i didn't try restore polling waiters, my bad. Letme rework this trying addressing your comments.
Re: [patch 3/3] timerfd: Implement write method
Quoted 8 lines · Cyrill Gorcunov
Quoted 2 lines · Thomas Gleixner
There is a world outside of checkpoint/restore, really.
Yes, I simply don't know who else might use this
write()functionality for other purpose, I mean i don't see a point to use it for anything else.
Quoted 7 lines
Quoted 2 lines
So what's the semantics of that write function? We really want to have that agreed on and documented in the man page.
The idea was to provide a way to setup @ticks into (nonzero) value which we get from
show_fdinfooutput. Then when we restore it we setup the timer and set @ticks to the value it had at dump moment.
That's not describing the semantics. It's describing what you use it for.
Quoted 7 lines
Quoted 6 lines
Right now the write will just update the ticks and nothing else. So what if there is a waiter already? What if there is a timer armed?
Can you please describe how checkpoint/restore is going to use all of this. How is the timer restored and how/when is the reader which was waiting in read/poll at the time of suspend reattached to it.
Quoted 6 lines
Thomas, I see what you mean. Need to think (I must admit I forgot about polling of timerfds :( I were to restore timerfds like this
- fetch data from fdinfo
- use timer_create/settime to arm it
- write @ticks then
That's clear to me.
So again you have to answer the questions:
Do we just allow the write unconditionally?
Do we care about waking readers/pollers?
Whatever the answer is, it needs to be documented coherently in the changelog, in the code and in the man page.
Thanks,
tglx
Re: [patch 3/3] timerfd: Implement write method
Quoted 11 lines · Thomas Gleixner
Quoted 8 lines
Quoted 3 lines
So what's the semantics of that write function? We really want to have that agreed on and documented in the man page.
The idea was to provide a way to setup @ticks into (nonzero) value which we get from
show_fdinfooutput. Then when we restore it we setup the timer and set @ticks to the value it had at dump moment.That's not describing the semantics. It's describing what you use it for.
That's I've been intending to use it for and as result the semantic was to write unconditionally. But because I missed polling in first place now I think such semantic is wrong and write() should be a complete counterpart of read() method and wake up waiters.
Quoted 16 lines
Quoted 7 lines
Quoted 6 lines
Right now the write will just update the ticks and nothing else. So what if there is a waiter already? What if there is a timer armed?
Can you please describe how checkpoint/restore is going to use all of this. How is the timer restored and how/when is the reader which was waiting in read/poll at the time of suspend reattached to it.
Quoted 6 lines
Thomas, I see what you mean. Need to think (I must admit I forgot about polling of timerfds :( I were to restore timerfds like this
- fetch data from fdinfo
- use timer_create/settime to arm it
- write @ticks thenThat's clear to me.
Quoted 7 lines
So again you have to answer the questions:
Do we just allow the write unconditionally?
Do we care about waking readers/pollers?Whatever the answer is, it needs to be documented coherently in the changelog, in the code and in the man page.
"Yes" to both questions I think. Thomas I'll return with a new patchset, testcase and man update.
Re: [patch 3/3] timerfd: Implement write method
In reply to Cyrill Gorcunov[Thomas, thanks for pinging me on this.]
Hi Cyril
Please CC linux-api on changes that affect kernel-user-space ABI/API.
Quoted 7 lines · Cyrill Gorcunov
Quoted 2 lines · Thomas Gleixner
There is a world outside of checkpoint/restore, really.
Yes, I simply don't know who else might use this
write()functionality for other purpose, I mean i don't see a point to use it for anything else.
Nevertheless, when making API changes like this, we should be thinking as generally as possible, rather than looking from the perspective of a single use case.
Quoted 18 lines
Quoted 2 lines
So what's the semantics of that write function? We really want to have that agreed on and documented in the man page.
The idea was to provide a way to setup @ticks into (nonzero) value which we get from
show_fdinfooutput. Then when we restore it we setup the timer and set @ticks to the value it had at dump moment.Quoted 6 lines
Right now the write will just update the ticks and nothing else. So what if there is a waiter already? What if there is a timer armed?
Can you please describe how checkpoint/restore is going to use all of this. How is the timer restored and how/when is the reader which was waiting in read/poll at the time of suspend reattached to it.
Thomas, I see what you mean. Need to think (I must admit I forgot about polling of timerfds :( I were to restore timerfds like this
Quoted 6 lines
- fetch data from fdinfo
- use timer_create/settime to arm it
- write @ticks thenbut i didn't try restore polling waiters, my bad. Letme rework this trying addressing your comments.
Great. Please CC me and linux-api@ on the next round.
Cheers,
Michael
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Author of "The Linux Programming Interface", http://blog.man7.org/
Re: [patch 3/3] timerfd: Implement write method
Quoted 5 lines · Michael Kerrisk
[Thomas, thanks for pinging me on this.]
Hi Cyril
Please CC linux-api on changes that affect kernel-user-space ABI/API.
Sure!
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [patch 3/3] timerfd: Implement write method
Quoted 10 lines · Thomas Gleixner
Quoted 9 lines · Cyrill Gorcunov
fs/timerfd.c +9
-0+ +static ssize_t timerfd_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +struct timerfd_ctx *ctx = file->private_data; +u64 ticks =0 ; + +if (count <sizeof (ticks)) +return -EINVAL ;
Quoted 8 lines
fs/timerfd.c static int timerfd_show(struct seq_file · line 328
Quoted 6 lines
fs/timerfd.c +6
-0+ +if (get_user (ticks, (u64 __user *) buf)) +return -EFAULT ; + +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks;So what wakes a potential waiter in read/poll?
And who is updating timerfd_create(2) ?
Thanks,
tglx
Re: [patch 3/3] timerfd: Implement write method
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines
So what wakes a potential waiter in read/poll?
And who is updating
timerfd_create(2)?
Thomas, could you please take a look if the approach below is acceptable? If it will be fine I update manpage then.
---
From: Cyrill Gorcunov <[email protected]>
Subject: 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 requires CAP_SYS_RESOURCE capability to be able to set @ticks into arbitrary value. Note this command doesn't wake up readers/waiters and its purpose only to serve C/R needs (for same sake I wrapped code with CONFIG_CHECKPOINT_RESTORE). Still if needed the ioctl may be extended for new commands and CONFIG_CHECKPOINT_RESTORE dropped off.
--- fs/timerfd.c | 31 +++++++++++++++++++++++++++++++ include/linux/timerfd.h | 5 +++++ 2 files changed, 36 insertions(+)
fs/timerfd.c +31 -0
@@ -313,11 +313,42 @@ 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 (!capable (CAP_SYS_RESOURCE )) +return -EPERM ; +if (get_user (ticks, (u64 __user *)arg)) +return -EFAULT ; +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks; +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
include/linux/timerfd.h +5 -0
@@ -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 */
Re: [patch 3/3] timerfd: Implement write method
[CC += linux-api@]
Quoted 106 lines · Cyrill Gorcunov
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines
So what wakes a potential waiter in read/poll?
And who is updating
timerfd_create(2)?Thomas, could you please take a look if the approach below is acceptable? If it will be fine I update manpage then.
---
From: Cyrill Gorcunov <[email protected]>
Subject: timerfd: Implementtimerfd_ioctlmethod to restoretimerfd_ctx::ticksThe
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 withTFD_IOC_SET_TICKScommand which requiresCAP_SYS_RESOURCEcapability to be able to set @ticks into arbitrary value. Note this command doesn't wake up readers/waiters and its purpose only to serve C/R needs (for same sake I wrapped code withCONFIG_CHECKPOINT_RESTORE). Still if needed the ioctl may be extended for new commands andCONFIG_CHECKPOINT_RESTOREdropped off.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]>Signed-off-by Cyrill Gorcunov <[email protected]>--- fs/timerfd.c | 31 +++++++++++++++++++++++++++++++ include/linux/timerfd.h | 5 +++++ 2 files changed, 36 insertions(+)Index:
linux-2.6.git/fs/timerfd.c
===================================================================fs/timerfd.c +31
-0@@ -313,11 +313,42 @@ 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 (!capable (CAP_SYS_RESOURCE )) +return -EPERM ; +if (get_user (ticks, (u64 __user *)arg)) +return -EFAULT ; +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks; +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, #endifIndex:
linux-2.6.git/include/linux/timerfd.h
===================================================================include/linux/timerfd.h +5
-0@@ -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 */
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/
Re: [patch 3/3] timerfd: Implement write method
Quoted 29 lines · Michael Kerrisk (man-pages)
[CC += linux-api@]
Quoted 26 lines · Cyrill Gorcunov
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines
So what wakes a potential waiter in read/poll?
And who is updating
timerfd_create(2)?Thomas, could you please take a look if the approach below is acceptable? If it will be fine I update manpage then.
---
From: Cyrill Gorcunov <[email protected]>
Subject: timerfd: Implementtimerfd_ioctlmethod to restoretimerfd_ctx::ticksThe
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 withTFD_IOC_SET_TICKScommand which requiresCAP_SYS_RESOURCEcapability to be able to set @ticks into arbitrary value. Note this command doesn't wake up readers/waiters and its purpose only to serve C/R needs (for same sake I wrapped code withCONFIG_CHECKPOINT_RESTORE). Still if needed the ioctl may be extended for new commands andCONFIG_CHECKPOINT_RESTOREdropped off.
Why does this need CAP_SYS_RESOURCE?
--Andy
Re: [patch 3/3] timerfd: Implement write method
Quoted 3 lines · Andy Lutomirski
Quoted 1 line · Michael Kerrisk (man-pages)
[CC += linux-api@]
Thanks Michael!
Quoted 28 lines
Quoted 20 lines · Cyrill Gorcunov
Quoted 19 lines · Thomas Gleixner
Quoted 4 lines
Quoted 2 lines
So what wakes a potential waiter in read/poll?
And who is updating
timerfd_create(2)?Thomas, could you please take a look if the approach below is acceptable? If it will be fine I update manpage then.
---
From: Cyrill Gorcunov <[email protected]>
Subject: timerfd: Implementtimerfd_ioctlmethod to restoretimerfd_ctx::ticksThe
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.Quoted 7 lines
Quoted 7 lines
Instead implement
timerfd_ioctl()method withTFD_IOC_SET_TICKScommand which requiresCAP_SYS_RESOURCEcapability to be able to set @ticks into arbitrary value. Note this command doesn't wake up readers/waiters and its purpose only to serve C/R needs (for same sake I wrapped code withCONFIG_CHECKPOINT_RESTORE). Still if needed the ioctl may be extended for new commands andCONFIG_CHECKPOINT_RESTOREdropped off.
Quoted 1 line
Why does this need
CAP_SYS_RESOURCE?
Because I think this interface should not be used by a regular
applications, the only purpose is to restore the @ticks after checkpoint. Requiring CAP_SYS_RESOURCE means that at least program which use it knows what it's doing.
Still if someone has a scenarion where we might need this
intarface out of this cap requirement -- we always can drop it of without breaking existing users, but not the reverse.
P.S. I remember Thomas' words about existence of the other word out of c/r, still I treat this ioctl as exception (as in prctl codes we use for c/r).
Re: [patch 3/3] timerfd: Implement write method
In reply to Cyrill GorcunovQuoted 48 lines · Cyrill Gorcunov
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines
So what wakes a potential waiter in read/poll?
And who is updating
timerfd_create(2)?Thomas, could you please take a look if the approach below is acceptable? If it will be fine I update manpage then.
---
From: Cyrill Gorcunov <[email protected]>
Subject: timerfd: Implementtimerfd_ioctlmethod to restoretimerfd_ctx::ticksThe
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 withTFD_IOC_SET_TICKScommand which requiresCAP_SYS_RESOURCEcapability to be able to set @ticks into arbitrary value. Note this command doesn't wake up readers/waiters and its purpose only to serve C/R needs (for same sake I wrapped code withCONFIG_CHECKPOINT_RESTORE). Still if needed the ioctl may be extended for new commands andCONFIG_CHECKPOINT_RESTOREdropped off.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 | 31 +++++++++++++++++++++++++++++++ include/linux/timerfd.h | 5 +++++ 2 files changed, 36 insertions(+)Index:
linux-2.6.git/fs/timerfd.c
===================================================================fs/timerfd.c +2
-0@@ -313,11 +313,42 @@ 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)
fs/timerfd.c static int timerfd_show(struct seq_file · line 327
Quoted 10 lines
fs/timerfd.c +10
-0+{ +struct timerfd_ctx *ctx = file->private_data; +int ret =0 ; + +switch (cmd) { +case TFD_IOC_SET_TICKS : { +u64 ticks; + +if (!capable (CAP_SYS_RESOURCE )) +return -EPERM ;
I think it is too strong. It will not work in userns.
Why do we need to check CAP_SYS_RESOURCE here?
Can we replace capable on ns_capable?
fs/timerfd.c static int timerfd_show(struct seq_file · line 331
Quoted 4 lines
fs/timerfd.c +4
-0+if (get_user (ticks, (u64 __user *)arg)) +return -EFAULT ; +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks;
I think we need to wakt up readers here if ctx->ticks isn't zero.
Quoted 44 lines
fs/timerfd.c +15
-0+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, #endifIndex:
linux-2.6.git/include/linux/timerfd.h
===================================================================include/linux/timerfd.h +5
-0@@ -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 */
Re: [patch 3/3] timerfd: Implement write method
Quoted 11 lines · Andrew Vagin
Review
Quoted 5 lines
(quoted diff) +5
-0+ case TFD_IOC_SET_TICKS: { + u64 ticks; + + if (!capable(CAP_SYS_RESOURCE)) + return -EPERM;I think it is too strong. It will not work in userns.
Why do we need to check
CAP_SYS_RESOURCEhere?
Can we replace capable onns_capable?
Quoted 6 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ if (get_user(ticks, (u64 __user *)arg)) + return -EFAULT; + spin_lock_irq(&ctx->wqh.lock); + ctx->ticks = ticks;I think we need to wakt up readers here if
ctx->ticksisn't zero.
I used this caps to prevent arbitrary changing the ticks from a
regular programs because i didn't wake up readers. But after thinking more i think indeed this case (write ticks but not wake up anyone) leads to strange situation -- currently (without the patch) there can't be scenario when we have a waiter not woken up with nonzero @ticks, but with this patch applied the timerfd logic become vague. One can set up @ticks to nonzero value and still have waiter looping around.
Thanks Andrew! How about the patch below? We allow to modify @ticks
and wake up waiter on non-zero @ticks assignment. I think this should fit current timerfd logic.
(once such or anything else approach get approved -- I'll resend new
series with man updated).
---
From: Cyrill Gorcunov <[email protected]>
Subject: 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 arbitrary value. In case if new value is non-zero we are waking up the waiters. I wrapped code with CONFIG_CHECKPOINT_RESTORE which can be dropped off if there users except c/r camp appear.
--- fs/timerfd.c | 31 +++++++++++++++++++++++++++++++ include/linux/timerfd.h | 5 +++++ 2 files changed, 36 insertions(+)
fs/timerfd.c +31 -0
@@ -313,11 +313,42 @@ 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 ; +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks; +if (ticks) +wake_up_locked (&ctx->wqh); +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
include/linux/timerfd.h +5 -0
@@ -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 */
Re: [patch 3/3] timerfd: Implement write method
Quoted 1 line · Cyrill Gorcunov
On Wed, Jun 11, 2014 at 11:27:43AM +0400, Andrew Vagin wrote:
...
Quoted 6 lines
fs/timerfd.c +6
-0+#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 ; +
fs/timerfd.c static int timerfd_show(struct seq_file · line 331
Quoted 10 lines
fs/timerfd.c +10
-0+switch (cmd) { +case TFD_IOC_SET_TICKS : { +u64 ticks; + +if (get_user (ticks, (u64 __user *)arg)) +return -EFAULT ; +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks; +if (ticks) +wake_up_locked (&ctx->wqh);
Setting ticks to zero is equivalent to timerfd_read(), isn't it? So do we need to re-arme the timer, if it's periodic?
Quoted 11 lines
fs/timerfd.c +11
-0+spin_unlock_irq (&ctx->wqh.lock); + break; + } +default : + ret = -ENOTTY ; +break ; + } + +V return ret; +} +#endif
Re: [patch 3/3] timerfd: Implement write method
Quoted 16 lines · Andrew Vagin
Quoted 1 line · Cyrill Gorcunov
On Wed, Jun 11, 2014 at 11:27:43AM +0400, Andrew Vagin wrote:
...
Quoted 11 lines
fs/timerfd.c +11
-0+#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))
Quoted 8 lines
fs/timerfd.c static int timerfd_show(struct seq_file · line 331
Quoted 5 lines
fs/timerfd.c +5
-0+return -EFAULT ; +spin_lock_irq (&ctx->wqh.lock); + ctx->ticks = ticks; +if (ticks) +wake_up_locked (&ctx->wqh);Setting ticks to zero is equivalent to
timerfd_read(), isn't it? So do we need to re-arme the timer, if it's periodic?
I must admit I'm not really sure if we should rearm it in such case. In general @ticks are zeroified in case of timer-setup/cancel/read.
- lets consider someone armed the timer it triggered but no read done yet, instead ioctl called and @ticks are set to zero, then call for read() and it returns zero to caller not rearming the timer (in current patch approach and non-block read)
- in turn if we rearm timer on @ticks = 0 in ioctl this makes it close to behaviour of read() function (which in turn look to me as a duplication of read() interface).
That said, I'm not sure yet...
Re: [patch 3/3] timerfd: Implement write method
Quoted 15 lines · Cyrill Gorcunov
Quoted 5 lines · Andrew Vagin
Quoted 1 line · Cyrill Gorcunov
On Wed, Jun 11, 2014 at 11:27:43AM +0400, Andrew Vagin wrote:
Setting ticks to zero is equivalent to
timerfd_read(), isn't it? So do we need to re-arme the timer, if it's periodic?I must admit I'm not really sure if we should rearm it in such case. In general @ticks are zeroified in case of timer-setup/cancel/read.
- lets consider someone armed the timer it triggered but no read done yet, instead ioctl called and @ticks are set to zero, then call for
read()and it returns zero to caller not rearming the timer (in current patch approach and non-block read)
Quoted 5 lines
- in turn if we rearm timer on @ticks = 0 in ioctl this makes it close to behaviour of
read()function (which in turn look to me as a duplication ofread()interface).That said, I'm not sure yet...
What if we prohibit setting non-zero values here? @ticks are set to zero on timerfd_setup thus there is always a way to create a timer with fields zeroified. Something like
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
} else
ret = -ECANCELED;
spin_unlock_irq(&ctx->wqh.lock);
break;
}?
Quoted 4 lines · Cyrill Gorcunov
Hello! This is (I hope) the final version. Most changes from v2 are sitting in documentation, where I tried to reflect that the time we're printing out is remaining until the timer expiration. Please take a look.
Thomas, ping?