pstore/ram: no timekeeping calls when unavailable
PATCH v2 1/1 pstore/ram: no timekeeping calls when unavailable
We must not call timekeeping functions unless they are available. If we dump before they have resumed, avoid a WARN_ON by setting the timestamp to 0.
Since the "ram" pstore driver can be a module, we must have timekeeping_suspended exported.
---
v2:
- export needed for timekeeping_suspended (thanks to Fengguang Wu).
--- fs/pstore/ram.c | 8 +++++++- kernel/time/timekeeping.c | 1 + 2 files changed, 8 insertions(+), 1 deletion(-)
fs/pstore/ram.c +7 -1
@@ -171,7 +171,13 @@ static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)struct timeval timestamp;size_t len; -do_gettimeofday (×tamp); +/* Handle dumping before timekeeping has resumed. */ +if (unlikely (timekeeping_suspended)) { + timestamp.tv_sec =0 ; + timestamp.tv_usec =0 ; + }else +do_gettimeofday (×tamp); + hdr =kasprintf (GFP_ATOMIC ,RAMOOPS_KERNMSG_HDR "%lu.%lu\n" , (long )timestamp.tv_sec, (long )timestamp.tv_usec);WARN_ON_ONCE (!hdr);
kernel/time/timekeeping.c +1 -0
@@ -33,6 +33,7 @@ __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);/* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; +EXPORT_SYMBOL_GPL (timekeeping_suspended);static inline void tk_normalize_xtime (struct timekeeper *tk) {
Signature · 6 lines
1.7.9.5 -- Kees Cook Chrome OS Security
Quoted 24 lines · Kees Cook
We must not call timekeeping functions unless they are available. If we dump before they have resumed, avoid a
WARN_ONby setting the timestamp to 0.Since the "ram" pstore driver can be a module, we must have
timekeeping_suspendedexported.Reported-by Doug Anderson <[email protected]>Cc Anton Vorontsov <[email protected]>Cc John Stultz <[email protected]>Signed-off-by Kees Cook <[email protected]>---
v2:
- export needed fortimekeeping_suspended(thanks to Fengguang Wu).--- fs/pstore/ram.c | 8 +++++++- kernel/time/timekeeping.c | 1 + 2 files changed, 8 insertions(+), 1 deletion(-)fs/pstore/ram.c +0
-0@@ -171,7 +171,13 @@ static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)struct timeval timestamp;
fs/pstore/ram.c static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz) · line 180
Quoted 10 lines
fs/pstore/ram.c +7
-1size_t len; -do_gettimeofday (×tamp); +/* Handle dumping before timekeeping has resumed. */ +if (unlikely (timekeeping_suspended)) { + timestamp.tv_sec =0 ; + timestamp.tv_usec =0 ; + }else +do_gettimeofday (×tamp); +
Would nulling out the timestamp be better done in do_gettimeofday()? That way we don't have to export timekeeping internals and users would get something more sane for this corner case.
thanks
-john
Quoted 30 lines · John Stultz
Quoted 29 lines · Kees Cook
We must not call timekeeping functions unless they are available. If we dump
before they have resumed, avoid aWARN_ONby setting the timestamp to 0.Since the "ram" pstore driver can be a module, we must have
timekeeping_suspendedexported.Reported-by Doug Anderson <[email protected]>Cc Anton Vorontsov <[email protected]>Cc John Stultz <[email protected]>Signed-off-by Kees Cook <[email protected]>---
v2:
- export needed fortimekeeping_suspended(thanks to Fengguang Wu).--- fs/pstore/ram.c | 8 +++++++- kernel/time/timekeeping.c | 1 + 2 files changed, 8 insertions(+), 1 deletion(-)fs/pstore/ram.c +0
-0@@ -171,7 +171,13 @@ static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)struct timeval timestamp;size_t len;
Quoted 12 lines
fs/pstore/ram.c static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz) · line 180
Quoted 8 lines
fs/pstore/ram.c +7
-1-do_gettimeofday (×tamp); +/* Handle dumping before timekeeping has resumed. */ +if (unlikely (timekeeping_suspended)) { + timestamp.tv_sec =0 ; + timestamp.tv_usec =0 ; + }else +do_gettimeofday (×tamp); +Would nulling out the timestamp be better done in
do_gettimeofday()? That way we don't have to export timekeeping internals and users would get something more sane for this corner case.
Well... I'm not sure. If we don't want to expose the timekeeping_suspended variable, maybe we need a function to check this? I think it's probably better to find the users of timekeeping that could call it when suspended. That's why I figured the BUG was there. Very very few things should be attempting to call gettimeofday in a place where it might be suspended. As such, it seems like those things should be able to determine how to handle it. Maybe not everything would be sensible to get back 0s.
In this particular case, I'm fine with removing the BUG and returning 0 instead, since that's fine for ramoops. :)
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
On Fri, Nov 09, 2012 at 05:26:53PM -0800, Kees Cook wrote:
[....]
Quoted 27 lines
Quoted 5 lines
Quoted 5 lines
fs/pstore/ram.c +0
-0@@ -171,7 +171,13 @@ static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)struct timeval timestamp;size_t len;Quoted 12 lines
fs/pstore/ram.c static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz) · line 180
Quoted 8 lines
fs/pstore/ram.c +7
-1-do_gettimeofday (×tamp); +/* Handle dumping before timekeeping has resumed. */ +if (unlikely (timekeeping_suspended)) { + timestamp.tv_sec =0 ; + timestamp.tv_usec =0 ; + }else +do_gettimeofday (×tamp); +Would nulling out the timestamp be better done in
do_gettimeofday()? That way we don't have to export timekeeping internals and users would get something more sane for this corner case.Well... I'm not sure. If we don't want to expose the
timekeeping_suspendedvariable, maybe we need a function to check this? I think it's probably better to find the users of timekeeping that could call it when suspended. That's why I figured the BUG was there. Very very few things should be attempting to call gettimeofday in a place where it might be suspended. As such, it seems like those things should be able to determine how to handle it. Maybe not everything would be sensible to get back 0s.
Quoted 2 lines
In this particular case, I'm fine with removing the BUG and returning 0 instead, since that's fine for ramoops. :)
In the lack of agreement on kernel/time/timekeeping.c change, I can't apply the patch. And personally I tend to agree that doing this workaround in the pstore code is odd. How about introducing ___do_gettimeofday() that is safe to call when suspened, and the func would have good kernel doc comments explaining the purpose of it?
Thanks,
Anton.
Quoted 27 lines · Anton Vorontsov
On Fri, Nov 09, 2012 at 05:26:53PM -0800, Kees Cook wrote:
[....]Quoted 25 lines
Quoted 5 lines
Quoted 5 lines
fs/pstore/ram.c +0
-0@@ -171,7 +171,13 @@ static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)struct timeval timestamp;size_t len;Quoted 11 lines
fs/pstore/ram.c static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz) · line 180
Quoted 8 lines
fs/pstore/ram.c +7
-1-do_gettimeofday (×tamp); +/* Handle dumping before timekeeping has resumed. */ +if (unlikely (timekeeping_suspended)) { + timestamp.tv_sec =0 ; + timestamp.tv_usec =0 ; + }else +do_gettimeofday (×tamp); +Would nulling out the timestamp be better done in
do_gettimeofday()? That way we don't have to export timekeeping internals and users would get something more sane for this corner case.Well... I'm not sure. If we don't want to expose the
timekeeping_suspendedvariable, maybe we need a function to check this? I think it's probably better to find the users of timekeeping that could call it when suspended. That's why I figured the BUG was there. Very very few things should be attempting to call gettimeofday in a place where it might be suspended. As such, it seems like those things should be able to determine how to handle it. Maybe not everything would be sensible to get back 0s.
Quoted 7 lines
Quoted 2 lines
In this particular case, I'm fine with removing the BUG and returning 0 instead, since that's fine for ramoops. :)
In the lack of agreement on
kernel/time/timekeeping.cchange, I can't apply the patch. And personally I tend to agree that doing this workaround in the pstore code is odd. How about introducing___do_gettimeofday()that is safe to call when suspened, and the func would have good kernel doc comments explaining the purpose of it?
Yea, I wanted to revisit this, because it is an odd case.
We don't want to call getnstimeofday() while the timekeeping code is suspended, since the clocksource cycle_last value may be invalid if the hardware was reset during suspend. Kees is correct, the WARN_ONs were there to make sure no one tries to use the timekeeping core before its resumed, so removing them is problematic.
Your sugggestion of having the __do_gettimeofday() internal accessor that maybe returns an error if timekeeping has been suspended could work.
The other possibility is depending on the needs for accuracy with the timestamp, current_kernel_time() might be a better interface to use, since it will return the time at the last tick, and doesn't require accessing the clocksource hardware. Might that be a simpler solution? Or is sub-tick granularity necessary?
thanks
-john
Quoted 8 lines · John Stultz
Yea, I wanted to revisit this, because it is an odd case.
We don't want to call
getnstimeofday()while the timekeeping code is suspended, since the clocksourcecycle_lastvalue may be invalid if the hardware was reset during suspend. Kees is correct, theWARN_ONswere there to make sure no one tries to use the timekeeping core before its resumed, so removing them is problematic.
Quoted 8 lines
Your sugggestion of having the
__do_gettimeofday()internal accessor that maybe returns an error if timekeeping has been suspended could work.The other possibility is depending on the needs for accuracy with the timestamp,
current_kernel_time()might be a better interface to use, since it will return the time at the last tick, and doesn't require accessing the clocksource hardware. Might that be a simpler solution? Or is sub-tick granularity necessary?
I think it's only useful to have this to the same granularity as sched_clock(), so things can be correlated to dmesg output. If it's the same, I'd be fine to switch to using current_kernel_time().
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Quoted 12 lines · Kees Cook
Quoted 11 lines · John Stultz
Yea, I wanted to revisit this, because it is an odd case.
We don't want to call
getnstimeofday()while the timekeeping code is suspended, since the clocksourcecycle_lastvalue may be invalid if the hardware was reset during suspend. Kees is correct, theWARN_ONswere there to make sure no one tries to use the timekeeping core before its resumed, so removing them is problematic.Your sugggestion of having the
__do_gettimeofday()internal accessor that maybe returns an error if timekeeping has been suspended could work.
Quoted 8 lines
Quoted 5 lines
The other possibility is depending on the needs for accuracy with the timestamp,
current_kernel_time()might be a better interface to use, since it will return the time at the last tick, and doesn't require accessing the clocksource hardware. Might that be a simpler solution? Or is sub-tick granularity necessary?I think it's only useful to have this to the same granularity as
sched_clock(), so things can be correlated to dmesg output. If it's the same, I'd be fine to switch to usingcurrent_kernel_time().
Oof. That's another can of worms, sched_clock() resolution isn't tied to getnstimeofday(), since you may have cases where the TSC is invalid for timekeeping (so we use something slow like the ACPI PM) but ok for scheduling, etc.
Even so, its current_kernel_time() is just tick granularity, so that probably won't work for you.
So I'm leaning towards Anton's suggestion of adding a new internal accessor that returns an error if we're suspended.
Thomas, what do you think of something like what's below?
thanks
-john
include/linux/time.h +1 -0
@@ -158,6 +158,7 @@ extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);extern unsigned int alarm_setitimer (unsigned int seconds);extern int do_getitimer (int which,struct itimerval *value); +extern int __getnstimeofday (struct timespec *tv);extern void getnstimeofday (struct timespec *tv);extern void getrawmonotonic (struct timespec *ts); extern void getnstime_raw_and_real(struct timespec *ts_raw,
kernel/time/timekeeping.c +19 -5
@@ -220,19 +220,20 @@ static void timekeeping_forward_now(struct timekeeper *tk) }/** -* getnstimeofday - Returns the time of day in a timespec +* __getnstimeofday - Returns the time of day in a timespec * @ts: pointer to the timespec to be set * -* Returns the time of day in a timespec. +* Returns -1 if timekeeping is suspended. +* Returns 0 on success. */ -void getnstimeofday (struct timespec *ts) +int __getnstimeofday (struct timespec *ts) {struct timekeeper *tk = &timekeeper;unsigned long seq;s64 nsecs =0 ; -WARN_ON (timekeeping_suspended); - +if (unlikely (timekeeping_suspended)); +return -1 ; do { seq = read_seqbegin(&tk->lock);
@@ -243,6 +244,19 @@ void getnstimeofday(struct timespec *ts) ts->tv_nsec =0 ;timespec_add_ns (ts, nsecs); +return 0 ; +} +EXPORT_SYMBOL (__getnstimeofday); + +/** +* getnstimeofday - Returns the time of day in a timespec +* @ts: pointer to the timespec to be set +* +* Returns the time of day in a timespec. +*/ +void getnstimeofday (struct timespec *ts) +{ +WARN_ON (__getnstimeofday (ts)); }EXPORT_SYMBOL (getnstimeofday);
Quoted 73 lines · John Stultz
Quoted 22 lines · Kees Cook
Quoted 12 lines · John Stultz
Yea, I wanted to revisit this, because it is an odd case.
We don't want to call
getnstimeofday()while the timekeeping code is suspended, since the clocksourcecycle_lastvalue may be invalid if the hardware was reset during suspend. Kees is correct, theWARN_ONswere there to make sure no one tries to use the timekeeping core before its resumed, so removing them is problematic.Your sugggestion of having the
__do_gettimeofday()internal accessor that maybe returns an error if timekeeping has been suspended could work.Quoted 7 lines
The other possibility is depending on the needs for accuracy with the timestamp,
current_kernel_time()might be a better interface to use, since
it will return the time at the last tick, and doesn't require accessing the
clocksource hardware. Might that be a simpler solution? Or is sub-tick granularity necessary?Quoted 3 lines
I think it's only useful to have this to the same granularity as
sched_clock(), so things can be correlated to dmesg output. If it's the same, I'd be fine to switch to usingcurrent_kernel_time().Oof. That's another can of worms,
sched_clock()resolution isn't tied togetnstimeofday(), since you may have cases where the TSC is invalid for timekeeping (so we use something slow like the ACPI PM) but ok for scheduling, etc.Even so, its
current_kernel_time()is just tick granularity, so that probably won't work for you.So I'm leaning towards Anton's suggestion of adding a new internal accessor that returns an error if we're suspended.
Thomas, what do you think of something like what's below?
thanks
(quoted diff) +0
-1-johninclude/linux/time.h +1
-0@@ -158,6 +158,7 @@ extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);extern unsigned int alarm_setitimer (unsigned int seconds);extern int do_getitimer (int which,struct itimerval *value); +extern int __getnstimeofday (struct timespec *tv);extern void getnstimeofday (struct timespec *tv);extern void getrawmonotonic (struct timespec *ts);extern void getnstime_raw_and_real(struct timespec
*ts_raw,kernel/time/timekeeping.c +3
-2@@ -220,19 +220,20 @@ static void timekeeping_forward_now(struct timekeeper *tk) }/** -* getnstimeofday - Returns the time of day in a timespec +* __getnstimeofday - Returns the time of day in a timespec * @ts: pointer to the timespec to be set * -* Returns the time of day in a timespec. +* Returns -1 if timekeeping is suspended. +* Returns 0 on success. */
kernel/time/timekeeping.c static void timekeeping_forward_now(struct timekeeper · line 236
Quoted 10 lines
kernel/time/timekeeping.c +3
-2-void getnstimeofday (struct timespec *ts) +int __getnstimeofday (struct timespec *ts) {struct timekeeper *tk = &timekeeper;unsigned long seq;s64 nsecs =0 ; -WARN_ON (timekeeping_suspended); - +if (unlikely (timekeeping_suspended)); +return -1 ;
Is it useful to make this -EAGAIN or something, just for clarity? Also, this technically changes the semantics of callers that were hitting WARN (there should be none) in that the timespec isn't updated at all. In the prior code, a WARN would be emitted, but it would still fill out the timespec and return.
When I looked at implementing this error path, I actually moved the return -EAGAIN to the end of the function to keep the results the same. All that said, this is much cleaner and more correct if not touching the timespec on error is tolerable.
Quoted 11 lines
kernel/time/timekeeping.c +5
-0do { seq = read_seqbegin(&tk->lock); @@ -243,6 +244,19 @@ void getnstimeofday(struct timespec *ts) ts->tv_nsec = 0; timespec_add_ns(ts, nsecs); + return 0; +} +EXPORT_SYMBOL(__getnstimeofday); + +/**
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 261
Quoted 10 lines
kernel/time/timekeeping.c +8
-0+* getnstimeofday - Returns the time of day in a timespec +* @ts: pointer to the timespec to be set +* +* Returns the time of day in a timespec. +*/ +void getnstimeofday (struct timespec *ts) +{ +WARN_ON (__getnstimeofday (ts)); }EXPORT_SYMBOL (getnstimeofday);
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Quoted 86 lines · Kees Cook
Quoted 69 lines · John Stultz
Quoted 19 lines · Kees Cook
Quoted 11 lines · John Stultz
Yea, I wanted to revisit this, because it is an odd case.
We don't want to call
getnstimeofday()while the timekeeping code is suspended, since the clocksourcecycle_lastvalue may be invalid if the hardware was reset during suspend. Kees is correct, theWARN_ONswere there to make sure no one tries to use the timekeeping core before its resumed, so removing them is problematic.Your sugggestion of having the
__do_gettimeofday()internal accessor that maybe returns an error if timekeeping has been suspended could work.Quoted 7 lines
The other possibility is depending on the needs for accuracy with the timestamp,
current_kernel_time()might be a better interface to use, since
it will return the time at the last tick, and doesn't require accessing the
clocksource hardware. Might that be a simpler solution? Or is sub-tick granularity necessary?Quoted 3 lines
I think it's only useful to have this to the same granularity as
sched_clock(), so things can be correlated to dmesg output. If it's the same, I'd be fine to switch to usingcurrent_kernel_time().Oof. That's another can of worms,
sched_clock()resolution isn't tied togetnstimeofday(), since you may have cases where the TSC is invalid for timekeeping (so we use something slow like the ACPI PM) but ok for scheduling, etc.Even so, its
current_kernel_time()is just tick granularity, so that probably won't work for you.So I'm leaning towards Anton's suggestion of adding a new internal accessor that returns an error if we're suspended.
Thomas, what do you think of something like what's below?
thanks
(quoted diff) +0
-1-johninclude/linux/time.h +1
-0@@ -158,6 +158,7 @@ extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);extern unsigned int alarm_setitimer (unsigned int seconds);extern int do_getitimer (int which,struct itimerval *value); +extern int __getnstimeofday (struct timespec *tv);extern void getnstimeofday (struct timespec *tv);extern void getrawmonotonic (struct timespec *ts);extern void getnstime_raw_and_real(struct timespec
*ts_raw,kernel/time/timekeeping.c +3
-2@@ -220,19 +220,20 @@ static void timekeeping_forward_now(struct timekeeper *tk) }/** -* getnstimeofday - Returns the time of day in a timespec +* __getnstimeofday - Returns the time of day in a timespec * @ts: pointer to the timespec to be set * -* Returns the time of day in a timespec. +* Returns -1 if timekeeping is suspended. +* Returns 0 on success. */ kernel/time/timekeeping.c static void timekeeping_forward_now(struct timekeeper · line 236
Quoted 10 lines
kernel/time/timekeeping.c +3
-2-void getnstimeofday (struct timespec *ts) +int __getnstimeofday (struct timespec *ts) {struct timekeeper *tk = &timekeeper;unsigned long seq;s64 nsecs =0 ; -WARN_ON (timekeeping_suspended); - +if (unlikely (timekeeping_suspended)); +return -1 ;Is it useful to make this
-EAGAINor something, just for clarity? Also, this technically changes the semantics of callers that were hitting WARN (there should be none) in that the timespec isn't updated at all. In the prior code, a WARN would be emitted, but it would still fill out the timespec and return.
Quoted 4 lines
When I looked at implementing this error path, I actually moved the return
-EAGAINto the end of the function to keep the results the same. All that said, this is much cleaner and more correct if not touching the timespec on error is tolerable.
Yea, although really, the value returned if the WARN_ON is emitted is really junk. If the clocksource was reset under us, you could see really crazy time values.
Thinking some more, a better solution could be to simply return the time when timekeeping was suspended, which would be sane.
This wouldn't catch cases where folks were accessing the time while timekeeping is suspended, but they would get as valid a value as we can provide.
The only downside, is that accessors could see a flat-spot in time where they just get the same value over and over while timekeeping is suspended. So could be possible for bad behavior if something was doing something dumb like spinning in a loop waiting for some time to pass and was preventing suspend from completing.
Untested patch below. Any thoughts?
thanks
-john
kernel/time/timekeeping.c +6 -10
@@ -146,6 +146,9 @@ static inline s64 timekeeping_get_ns(struct timekeeper *tk)struct clocksource *clock;s64 nsec; +if (unlikely (timekeeping_suspended)) +return tk->xtime_nsec >> tk->shift; +/* read clocksource: */ clock = tk->clock; cycle_now = clock->read (clock);
@@ -166,6 +169,9 @@ static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)struct clocksource *clock;s64 nsec; +if (unlikely (timekeeping_suspended)) +return 0 ; +/* read clocksource: */ clock = tk->clock; cycle_now = clock->read (clock);
@@ -231,8 +237,6 @@ void getnstimeofday(struct timespec *ts)unsigned long seq;s64 nsecs =0 ; -WARN_ON (timekeeping_suspended); - do { seq = read_seqbegin(&tk->lock);
@@ -252,8 +256,6 @@ ktime_t ktime_get(void)unsigned int seq;s64 secs, nsecs; -WARN_ON (timekeeping_suspended); - do { seq = read_seqbegin(&tk->lock); secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
@@ -283,8 +285,6 @@ void ktime_get_ts(struct timespec *ts)s64 nsec;unsigned int seq; -WARN_ON (timekeeping_suspended); - do { seq = read_seqbegin(&tk->lock); ts->tv_sec = tk->xtime_sec;
@@ -316,8 +316,6 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)unsigned long seq;s64 nsecs_raw, nsecs_real; -WARN_ON_ONCE (timekeeping_suspended); - do { seq = read_seqbegin(&tk->lock);
@@ -1203,8 +1201,6 @@ void get_monotonic_boottime(struct timespec *ts)s64 nsec;unsigned int seq; -WARN_ON (timekeeping_suspended); - do { seq = read_seqbegin(&tk->lock); ts->tv_sec = tk->xtime_sec;
Quoted 116 lines · John Stultz
Quoted 98 lines · Kees Cook
Quoted 79 lines · John Stultz
Quoted 24 lines · Kees Cook
Quoted 13 lines · John Stultz
Yea, I wanted to revisit this, because it is an odd case.
We don't want to call
getnstimeofday()while the timekeeping code is suspended, since the clocksourcecycle_lastvalue may be invalid if the hardware was reset during suspend. Kees is correct, theWARN_ONswere there to make sure no one tries to use the timekeeping core before its resumed, so removing them is problematic.Your sugggestion of having the
__do_gettimeofday()internal accessor that
maybe returns an error if timekeeping has been suspended could work.Quoted 7 lines
The other possibility is depending on the needs for accuracy with the timestamp,
current_kernel_time()might be a better interface to use, since
it will return the time at the last tick, and doesn't require accessing the
clocksource hardware. Might that be a simpler solution? Or is sub-tick granularity necessary?Quoted 3 lines
I think it's only useful to have this to the same granularity as
sched_clock(), so things can be correlated to dmesg output. If it's the same, I'd be fine to switch to usingcurrent_kernel_time().Oof. That's another can of worms,
sched_clock()resolution isn't tied
togetnstimeofday(), since you may have cases where the TSC is invalid for timekeeping (so we use something slow like the ACPI PM) but ok for scheduling, etc.Even so, its
current_kernel_time()is just tick granularity, so that probably won't work for you.So I'm leaning towards Anton's suggestion of adding a new internal accessor
that returns an error if we're suspended.Thomas, what do you think of something like what's below?
thanks
(quoted diff) +0
-1-johninclude/linux/time.h +1
-0@@ -158,6 +158,7 @@ extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue);extern unsigned int alarm_setitimer (unsigned int seconds);extern int do_getitimer (int which,struct itimerval *value); +extern int __getnstimeofday (struct timespec *tv);extern void getnstimeofday (struct timespec *tv);extern void getrawmonotonic (struct timespec *ts);extern void getnstime_raw_and_real(struct timespec
*ts_raw,kernel/time/timekeeping.c +0
-0@@ -220,19 +220,20 @@ static void timekeeping_forward_now(structtimekeeper
*tk)
}
/**
- * getnstimeofday - Returns the time of day in a timespec
+ *__getnstimeofday- Returns the time of day in a timespec
* @ts: pointer to the timespec to be set
*
- * Returns the time of day in a timespec.
+ * Returns -1 if timekeeping is suspended.
+ * Returns 0 on success.
*/kernel/time/timekeeping.c
Quoted 10 lines
kernel/time/timekeeping.c +3
-2-void getnstimeofday (struct timespec *ts) +int __getnstimeofday (struct timespec *ts) {struct timekeeper *tk = &timekeeper;unsigned long seq;s64 nsecs =0 ; -WARN_ON (timekeeping_suspended); - +if (unlikely (timekeeping_suspended)); +return -1 ;Is it useful to make this
-EAGAINor something, just for clarity? Also, this technically changes the semantics of callers that were hitting WARN (there should be none) in that the timespec isn't updated at all. In the prior code, a WARN would be emitted, but it would still fill out the timespec and return.Quoted 4 lines
When I looked at implementing this error path, I actually moved the return
-EAGAINto the end of the function to keep the results the same. All that said, this is much cleaner and more correct if not touching the timespec on error is tolerable.Yea, although really, the value returned if the
WARN_ONis emitted is really junk. If the clocksource was reset under us, you could see really crazy time values.Thinking some more, a better solution could be to simply return the time when timekeeping was suspended, which would be sane.
This wouldn't catch cases where folks were accessing the time while timekeeping is suspended, but they would get as valid a value as we can provide.
Quoted 7 lines
The only downside, is that accessors could see a flat-spot in time where they just get the same value over and over while timekeeping is suspended. So could be possible for bad behavior if something was doing something dumb like spinning in a loop waiting for some time to pass and was preventing suspend from completing.
Untested patch below. Any thoughts?
Hrm, yeah, the dead-lock condition is a bit worrisome. It seems like it'd be better to keep the WARN_ONs and still define __getnstimeofday? Then callers that don't care about a flat-spot can report "most recent" time, and callers not expecting to run when suspended can still be detected.
Quoted 19 lines
thanks
(quoted diff) +0
-1-johnkernel/time/timekeeping.c +2
-0@@ -146,6 +146,9 @@ static inline s64 timekeeping_get_ns(struct timekeeper *tk)struct clocksource *clock;s64 nsec; + if (unlikely(timekeeping_suspended)) +return tk->xtime_nsec >> tk->shift; +/* read clocksource: */ clock = tk->clock; cycle_now = clock->read (clock);
kernel/time/timekeeping.c static inline s64 timekeeping_get_ns_raw(struct · line 177
Quoted 9 lines
kernel/time/timekeeping.c +2
-0@@ -166,6 +169,9 @@ static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)struct clocksource *clock;s64 nsec; + if (unlikely(timekeeping_suspended)) +return 0 ; +/* read clocksource: */ clock = tk->clock; cycle_now = clock->read (clock);
Then fs/pstore/ram.c wouldn't have to set things to zero; it would just report the flat spot, which is better than reporting 0.
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security