mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v7] timekeeping: Added a function to return tv_sec portion of ktime_get_ts64()
@ 2014-10-29  9:13 Heena Sirwani
  2014-10-29  9:21 ` Thomas Gleixner
  0 siblings, 1 reply; 4+ messages in thread
From: Heena Sirwani @ 2014-10-29  9:13 UTC (permalink / raw)
  To: linux-kernel; +Cc: john.stultz, tglx, arnd

The following patch replaces all instances of time_t with time64_t i.e.
change the type used for representing time from 32-bit to 64-bit. All
32-bit kernels to date use a signed 32-bit time_t type, which can only
represent time until January 2038. Since embedded systems running 32-bit
Linux are going to survive beyond that date, we have to change all
current uses, in a backwards compatible way.

The patch also changes the function get_seconds() that returns a 32-bit
integer to ktime_get_seconds() that returns seconds as 64-bit integer.

The patch changes the type of ticks from time_t to u32. We keep ticks as
32-bits as the function uses 32-bit arithmetic which would prove less
expensive than 64-bit arithmetic and the function is expected to be
called atleast once every 32 seconds.

Signed-off-by: Heena Sirwani <heenasirwani@gmail.com>
---
Changes in v7:
	- Reordered assignments in tk_update_ktime_data() to improve
	  readability.

 include/linux/timekeeper_internal.h |  2 ++
 include/linux/timekeeping.h         |  1 +
 kernel/time/timekeeping.c           | 31 ++++++++++++++++++++++++++++---
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h
index 95640dc..92e5e9d 100644
--- a/include/linux/timekeeper_internal.h
+++ b/include/linux/timekeeper_internal.h
@@ -42,6 +42,7 @@ struct tk_read_base {
  * struct timekeeper - Structure holding internal timekeeping values.
  * @tkr:		The readout base structure
  * @xtime_sec:		Current CLOCK_REALTIME time in seconds
+ * @ktime_sec:		Current CLOCK_MONOTONIC time in seconds
  * @wall_to_monotonic:	CLOCK_REALTIME to CLOCK_MONOTONIC offset
  * @offs_real:		Offset clock monotonic -> clock realtime
  * @offs_boot:		Offset clock monotonic -> clock boottime
@@ -77,6 +78,7 @@ struct tk_read_base {
 struct timekeeper {
 	struct tk_read_base	tkr;
 	u64			xtime_sec;
+	u64			ktime_sec;
 	struct timespec64	wall_to_monotonic;
 	ktime_t			offs_real;
 	ktime_t			offs_boot;
diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h
index 1caa6b0..115d55e 100644
--- a/include/linux/timekeeping.h
+++ b/include/linux/timekeeping.h
@@ -28,6 +28,7 @@ struct timespec __current_kernel_time(void);
 struct timespec get_monotonic_coarse(void);
 extern void getrawmonotonic(struct timespec *ts);
 extern void ktime_get_ts64(struct timespec64 *ts);
+extern time64_t ktime_get_seconds(void);
 
 extern int __getnstimeofday64(struct timespec64 *tv);
 extern void getnstimeofday64(struct timespec64 *tv);
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index ec1791f..7136a33 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -418,6 +418,8 @@ EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
 static inline void tk_update_ktime_data(struct timekeeper *tk)
 {
 	s64 nsec;
+	u64 seconds;
+	s64 nsec_offset;
 
 	/*
 	 * The xtime based monotonic readout is:
@@ -426,13 +428,18 @@ static inline void tk_update_ktime_data(struct timekeeper *tk)
 	 *	nsec = base_mono + now();
 	 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
 	 */
-	nsec = (s64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
-	nsec *= NSEC_PER_SEC;
-	nsec += tk->wall_to_monotonic.tv_nsec;
+	seconds = (s64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
+	nsec_offset = tk->wall_to_monotonic.tv_nsec;
+	nsec = seconds * NSEC_PER_SEC;
+	nsec += nsec_offset;
 	tk->tkr.base_mono = ns_to_ktime(nsec);
 
 	/* Update the monotonic raw base */
 	tk->base_raw = timespec64_to_ktime(tk->raw_time);
+
+	if (((long)(tk->tkr.xtime_nsec >> tk->tkr.shift) + nsec_offset) >= NSEC_PER_SEC)
+		seconds += 1;
+	tk->ktime_sec = seconds;
 }
 
 /* must hold timekeeper_lock */
@@ -648,6 +655,24 @@ void ktime_get_ts64(struct timespec64 *ts)
 }
 EXPORT_SYMBOL_GPL(ktime_get_ts64);
 
+time64_t ktime_get_seconds(void)
+{
+	time64_t seconds;
+	struct timekeeper *tk = &tk_core.timekeeper;
+	unsigned int seq;
+
+	WARN_ON(timekeeping_suspended);
+
+	do {
+		seq = read_seqcount_begin(&tk_core.seq);
+		seconds = tk->ktime_sec;
+
+	} while (read_seqcount_retry(&tk_core.seq, seq));
+
+	return seconds;
+}
+EXPORT_SYMBOL_GPL(ktime_get_seconds);
+
 #ifdef CONFIG_NTP_PPS
 
 /**
-- 
1.9.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v7] timekeeping: Added a function to return tv_sec portion of ktime_get_ts64()
  2014-10-29  9:13 [PATCH v7] timekeeping: Added a function to return tv_sec portion of ktime_get_ts64() Heena Sirwani
@ 2014-10-29  9:21 ` Thomas Gleixner
  2014-10-29  9:37   ` Arnd Bergmann
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Gleixner @ 2014-10-29  9:21 UTC (permalink / raw)
  To: Heena Sirwani; +Cc: linux-kernel, john.stultz, arnd

On Wed, 29 Oct 2014, Heena Sirwani wrote:
> +time64_t ktime_get_seconds(void)
> +{
> +	time64_t seconds;
> +	struct timekeeper *tk = &tk_core.timekeeper;
> +	unsigned int seq;
> +
> +	WARN_ON(timekeeping_suspended);

You want to have the same 64bit logic as you did for
ktime_get_real_seconds. So on 64bit it boils down to return
tk->ktime_sec.

> +
> +	do {
> +		seq = read_seqcount_begin(&tk_core.seq);
> +		seconds = tk->ktime_sec;
> +
> +	} while (read_seqcount_retry(&tk_core.seq, seq));
> +
> +	return seconds;
> +}

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v7] timekeeping: Added a function to return tv_sec portion of ktime_get_ts64()
  2014-10-29  9:21 ` Thomas Gleixner
@ 2014-10-29  9:37   ` Arnd Bergmann
  2014-10-29  9:40     ` Thomas Gleixner
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2014-10-29  9:37 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: Heena Sirwani, linux-kernel, john.stultz

On Wednesday 29 October 2014 10:21:18 Thomas Gleixner wrote:
> On Wed, 29 Oct 2014, Heena Sirwani wrote:
> > +time64_t ktime_get_seconds(void)
> > +{
> > +     time64_t seconds;
> > +     struct timekeeper *tk = &tk_core.timekeeper;
> > +     unsigned int seq;
> > +
> > +     WARN_ON(timekeeping_suspended);
> 
> You want to have the same 64bit logic as you did for
> ktime_get_real_seconds. So on 64bit it boils down to return
> tk->ktime_sec.
> 
> > +
> > +     do {
> > +             seq = read_seqcount_begin(&tk_core.seq);
> > +             seconds = tk->ktime_sec;
> > +
> > +     } while (read_seqcount_retry(&tk_core.seq, seq));
> 

I wonder if we should just make tk->ktime_sec 'unsigned long' and
avoid the lock for 32-bit as well. Are there any theoretical
cases where the monotonic time could overflow a 32-bit integer?

As a minor optimization 's64 nsec_offset' could also be 'long',
since that only stores a number that is known to be less than
1000000000.

	Arnd

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v7] timekeeping: Added a function to return tv_sec portion of ktime_get_ts64()
  2014-10-29  9:37   ` Arnd Bergmann
@ 2014-10-29  9:40     ` Thomas Gleixner
  0 siblings, 0 replies; 4+ messages in thread
From: Thomas Gleixner @ 2014-10-29  9:40 UTC (permalink / raw)
  To: Arnd Bergmann; +Cc: Heena Sirwani, linux-kernel, john.stultz

On Wed, 29 Oct 2014, Arnd Bergmann wrote:

> On Wednesday 29 October 2014 10:21:18 Thomas Gleixner wrote:
> > On Wed, 29 Oct 2014, Heena Sirwani wrote:
> > > +time64_t ktime_get_seconds(void)
> > > +{
> > > +     time64_t seconds;
> > > +     struct timekeeper *tk = &tk_core.timekeeper;
> > > +     unsigned int seq;
> > > +
> > > +     WARN_ON(timekeeping_suspended);
> > 
> > You want to have the same 64bit logic as you did for
> > ktime_get_real_seconds. So on 64bit it boils down to return
> > tk->ktime_sec.
> > 
> > > +
> > > +     do {
> > > +             seq = read_seqcount_begin(&tk_core.seq);
> > > +             seconds = tk->ktime_sec;
> > > +
> > > +     } while (read_seqcount_retry(&tk_core.seq, seq));
> > 
> 
> I wonder if we should just make tk->ktime_sec 'unsigned long' and
> avoid the lock for 32-bit as well. Are there any theoretical
> cases where the monotonic time could overflow a 32-bit integer?

136 years uptime :) I think we discussed that 32bit thing before, but
I forgot again.
 
> As a minor optimization 's64 nsec_offset' could also be 'long',
> since that only stores a number that is known to be less than
> 1000000000.

Indeed.

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-10-29  9:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-29  9:13 [PATCH v7] timekeeping: Added a function to return tv_sec portion of ktime_get_ts64() Heena Sirwani
2014-10-29  9:21 ` Thomas Gleixner
2014-10-29  9:37   ` Arnd Bergmann
2014-10-29  9:40     ` Thomas Gleixner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®