diff mbox

[RFC,v2,04/11] time: Add rtc_time_to_tm64() safe version(using time64_t)

Message ID 1414667745-7703-5-git-send-email-pang.xunlei@linaro.org
State New
Headers show

Commit Message

pang.xunlei Oct. 30, 2014, 11:15 a.m. UTC
As part of addressing 2038 saftey for in-kernel uses, this patch
adds safe rtc_time_to_tm64() using time64_t. After this patch,
rtc_time_to_tm() should be replaced by rtc_time_to_tm64() one by
one. Eventually, rtc_time_to_tm() will be removed from the kernel
when it has no users.

Signed-off-by: pang.xunlei <pang.xunlei@linaro.org>
---
 drivers/rtc/rtc-lib.c |   20 ++++++++++++++++----
 include/linux/rtc.h   |    1 +
 2 files changed, 17 insertions(+), 4 deletions(-)

Comments

Thomas Gleixner Oct. 30, 2014, 2:07 p.m. UTC | #1
On Thu, 30 Oct 2014, pang.xunlei wrote:

As you can guess already: $subject sucks.

> +
>  /*
>   * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
> + * Safe version for 2038 safety.

See previous replies.

>   */
> -void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
> +void rtc_time_to_tm64(time64_t time, struct rtc_time *tm)

What's tm64? 

This function is converting time64 to rtc_time. So the proper function
name is: rtc_time64_to_tm

Can you see the difference?

>  {
>  	unsigned int month, year;
>  	int days;
>  
> -	days = time / 86400;
> +	days = div_s64(time, 86400);
>  	time -= (unsigned int) days * 86400;
>  
>  	/* day of the week, 1970-01-01 was a Thursday */
> @@ -81,13 +83,23 @@ void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
>  	tm->tm_mon = month;
>  	tm->tm_mday = days + 1;
>  
> -	tm->tm_hour = time / 3600;
> +	tm->tm_hour = div_s64(time, 3600);

We already adjusted time to a value which is less than 24 * 3600,
i.e. it fits nicely into a long on 32bit. So we can avoid the whole
div_s64 business and simply do:

	unsigned int month, year;
+	unsigned long secs;
	int days;

-	days = time / 86400;
- 	time -= (unsigned int) days * 86400;
+	days = div_s64(time, 86400);
+	secs = time - (unsigned int) days * 86400;

And change the rest which uses time to:

-	tm->tm_hour = time / 3600;
+	tm->tm_hour = secs / 3600;

Sigh.

	tglx
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
diff mbox

Patch

diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c
index 6948cbd..e84e3a0 100644
--- a/drivers/rtc/rtc-lib.c
+++ b/drivers/rtc/rtc-lib.c
@@ -45,15 +45,17 @@  int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
 }
 EXPORT_SYMBOL(rtc_year_days);
 
+
 /*
  * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
+ * Safe version for 2038 safety.
  */
-void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
+void rtc_time_to_tm64(time64_t time, struct rtc_time *tm)
 {
 	unsigned int month, year;
 	int days;
 
-	days = time / 86400;
+	days = div_s64(time, 86400);
 	time -= (unsigned int) days * 86400;
 
 	/* day of the week, 1970-01-01 was a Thursday */
@@ -81,13 +83,23 @@  void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
 	tm->tm_mon = month;
 	tm->tm_mday = days + 1;
 
-	tm->tm_hour = time / 3600;
+	tm->tm_hour = div_s64(time, 3600);
 	time -= tm->tm_hour * 3600;
-	tm->tm_min = time / 60;
+	tm->tm_min = div_s64(time, 60);
 	tm->tm_sec = time - tm->tm_min * 60;
 
 	tm->tm_isdst = 0;
 }
+EXPORT_SYMBOL(rtc_time_to_tm64);
+
+/* TODO: [2038 safety] should be replaced by rtc_time_to_tm64() */
+void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
+{
+	time64_t time64;
+
+	time64 = (time64_t)time;
+	rtc_time_to_tm64(time64, tm);
+}
 EXPORT_SYMBOL(rtc_time_to_tm);
 
 /*
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index 2a6a9ce..12bacba 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -21,6 +21,7 @@  extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year
 extern int rtc_valid_tm(struct rtc_time *tm);
 extern int rtc_tm_to_time64(struct rtc_time *tm, time64_t *time);
 extern int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time);
+extern void rtc_time_to_tm64(time64_t time, struct rtc_time *tm);
 extern void rtc_time_to_tm(unsigned long time, struct rtc_time *tm);
 ktime_t rtc_tm_to_ktime(struct rtc_time tm);
 struct rtc_time rtc_ktime_to_tm(ktime_t kt);