mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "pang.xunlei" <pang.xunlei@linaro.org>
To: linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com
Cc: John Stultz <john.stultz@linaro.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Alessandro Zummo <a.zummo@towertech.it>,
	"pang.xunlei" <pang.xunlei@linaro.org>
Subject: [PATCH RFC 02/12] time: Add mktime() safe version(using time64_t)
Date: Mon, 27 Oct 2014 13:46:32 +0800	[thread overview]
Message-ID: <1414388802-5866-1-git-send-email-pang.xunlei@linaro.org> (raw)

The kernel uses 32-bit signed value(time_t) for seconds since 1970-01-01:00:00:00, thus it
will overflow at 2038-01-19 03:14:08 on 32-bit systems. We call this "2038 safety" issue.

As part of addressing 2038 saftey for in-kernel uses, this patch adds the safe mktime() using
time64_t. After this patch, mktime_unsafe() should be replaced by mktime() one by one.

Eventually, mktime_unsafe() will be removed from the kernel when it has no users.

Signed-off-by: pang.xunlei <pang.xunlei@linaro.org>
---
 include/linux/time.h |    8 ++++++--
 kernel/time/time.c   |   39 ++++++++++++++++++++++++++-------------
 2 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/include/linux/time.h b/include/linux/time.h
index e49d421..08f413c 100644
--- a/include/linux/time.h
+++ b/include/linux/time.h
@@ -39,9 +39,13 @@ static inline int timeval_compare(const struct timeval *lhs, const struct timeva
 	return lhs->tv_usec - rhs->tv_usec;
 }
 
+extern time64_t mktime(const unsigned int year, const unsigned int mon,
+					const unsigned int day, const unsigned int hour,
+					const unsigned int min, const unsigned int sec);
+
 extern unsigned long mktime_unsafe(const unsigned int year, const unsigned int mon,
-                            const unsigned int day, const unsigned int hour,
-                            const unsigned int min, const unsigned int sec);
+							const unsigned int day, const unsigned int hour,
+							const unsigned int min, const unsigned int sec);
 
 extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec);
 
diff --git a/kernel/time/time.c b/kernel/time/time.c
index d6a4464..f9521fa 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -304,7 +304,8 @@ struct timespec timespec_trunc(struct timespec t, unsigned gran)
 }
 EXPORT_SYMBOL(timespec_trunc);
 
-/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
+/*
+ * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  *
@@ -314,16 +315,13 @@ EXPORT_SYMBOL(timespec_trunc);
  * -year/100+year/400 terms, and add 10.]
  *
  * This algorithm was first published by Gauss (I think).
- *
- * WARNING: this function will overflow on 2106-02-07 06:28:16 on
- * machines where long is 32-bit! (However, as time_t is signed, we
- * will already get problems at other places on 2038-01-19 03:14:08)
  */
-unsigned long
-mktime_unsafe(const unsigned int year0, const unsigned int mon0,
+time64_t
+mktime(const unsigned int year0, const unsigned int mon0,
        const unsigned int day, const unsigned int hour,
        const unsigned int min, const unsigned int sec)
 {
+	time64_t ret;
 	unsigned int mon = mon0, year = year0;
 
 	/* 1..12 -> 11,12,1..10 */
@@ -332,12 +330,27 @@ mktime_unsafe(const unsigned int year0, const unsigned int mon0,
 		year -= 1;
 	}
 
-	return ((((unsigned long)
-		  (year/4 - year/100 + year/400 + 367*mon/12 + day) +
-		  year*365 - 719499
-	    )*24 + hour /* now have hours */
-	  )*60 + min /* now have minutes */
-	)*60 + sec; /* finally seconds */
+	ret = (year/4 - year/100 + year/400 + 367*mon/12 + day) + year*365 - 719499;
+	ret = ret*24 + hour; /* now have hours */
+	ret = ret*60 + min; /* now have minutes */
+	ret = ret*60 + sec; /* finally seconds */
+
+	return ret;
+}
+
+EXPORT_SYMBOL(mktime);
+
+/*
+ * TODO: [2038 safety] 2038 unsafe for legacy, replace it by mktime().
+ * WARNING: mktime_unsafe() will overflow on 2106-02-07 06:28:16 on
+ * machines where long is 32-bit! (However, as time_t is signed, we
+ * will already get problems at other places on 2038-01-19 03:14:08)
+ */
+unsigned long mktime_unsafe(const unsigned int year0, const unsigned int mon0,
+       const unsigned int day, const unsigned int hour,
+       const unsigned int min, const unsigned int sec)
+{
+	return (unsigned long) mktime(year0, mon0, day, hour, min, sec);
 }
 
 EXPORT_SYMBOL(mktime_unsafe);
-- 
1.7.9.5


             reply	other threads:[~2014-10-27  5:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-27  5:46 pang.xunlei [this message]
2014-10-27  5:46 ` [PATCH RFC 03/12] time: Rename rtc_tm_to_time() to rtc_tm_to_time_unsafe() pang.xunlei
2014-10-27  5:46 ` [PATCH RFC 04/12] time: Add rtc_tm_to_time() safe version(using time64_t) pang.xunlei
2014-10-27  5:46 ` [PATCH RFC 05/12] time: Convert rtc_tm_to_time_unsafe() to rtc_tm_to_time() in rtc_hctosys() pang.xunlei
2014-10-27 22:03   ` Thomas Gleixner
2014-10-27 22:13     ` Thomas Gleixner
2014-10-27  5:46 ` [PATCH RFC 06/12] time: Fix build warnings for time64_t to __kernel_time_t warning on 64-bit systems pang.xunlei
2014-10-27 21:41   ` Thomas Gleixner
2014-10-27  5:46 ` [PATCH RFC 07/12] time: Rename rtc_time_to_tm() to rtc_time_to_tm_unsafe() pang.xunlei
2014-10-27  5:46 ` [PATCH RFC 08/12] time: Add rtc_time_to_tm() safe version(using time64_t) pang.xunlei
2014-10-27  5:46 ` [PATCH RFC 09/12] time: Convert rtc_time_to_tm_unsafe() to rtc_time_to_tm() in alarm_set_rtc() pang.xunlei
2014-10-27  5:46 ` [PATCH RFC 10/12] time: Convert do_settimeofday() to use timespec64 pang.xunlei
2014-10-27  5:46 ` [PATCH RFC 11/12] time: Convert xen_read_wallclock() " pang.xunlei
     [not found]   ` <alpine.DEB.2.02.1410271525560.22875@kaball.uk.xensource.com>
2014-10-29 13:04     ` Xunlei Pang
2014-10-27  5:46 ` [PATCH RFC 12/12] time: Convert pvclock_read_wallclock() " pang.xunlei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1414388802-5866-1-git-send-email-pang.xunlei@linaro.org \
    --to=pang.xunlei@linaro.org \
    --cc=a.zummo@towertech.it \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rtc-linux@googlegroups.com \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®