diff mbox

[v5,13/24] posix-timers: Implement y2038 safe clock_get64() callback

Message ID 31f264e19c66a513889a1d3511da1db7e7592df2.1434079263.git.baolin.wang@linaro.org
State New
Headers show

Commit Message

(Exiting) Baolin Wang June 12, 2015, 7:48 a.m. UTC
The clock_get() callback in struct k_clock is not year 2038 safe on
32bit systems.

To address this implement a new callback clock_get64() which uses
struct timespec64 along with a default implementation which is a
wrapper for the existing clock_get() callback. The default callback
is installed at registration time for all posix clocks which are not
yet converted to clock_get64() and will be removed once this is
completed.

Use the new callback in __clock_gettime().

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
 include/linux/posix-timers.h |    1 +
 kernel/time/posix-timers.c   |   28 ++++++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)
diff mbox

Patch

diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h
index 2b19ec8..be2123d 100644
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -103,6 +103,7 @@  struct k_clock {
 	int (*clock_set64) (const clockid_t which_clock,
 			    const struct timespec64 *tp);
 	int (*clock_get) (const clockid_t which_clock, struct timespec * tp);
+	int (*clock_get64) (const clockid_t which_clock, struct timespec64 *tp);
 	int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
 	int (*timer_create) (struct k_itimer *timer);
 	int (*nsleep) (const clockid_t which_clock, int flags,
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index ca63626..021cd8f 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -589,6 +589,20 @@  static int default_clock_set64(const clockid_t which_clock,
 	return ret;
 }
 
+static int default_clock_get64(const clockid_t which_clock,
+			       struct timespec64 *tp64)
+{
+	struct k_clock *kc = clockid_to_kclock(which_clock);
+	struct timespec tp;
+	int ret;
+
+	ret = kc->clock_get(which_clock, &tp);
+	if (!ret)
+		*tp64 = timespec_to_timespec64(tp);
+
+	return ret;
+}
+
 void posix_timers_register_clock(const clockid_t clock_id,
 				 struct k_clock *new_clock)
 {
@@ -598,8 +612,8 @@  void posix_timers_register_clock(const clockid_t clock_id,
 		return;
 	}
 
-	if (!new_clock->clock_get) {
-		printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
+	if (!new_clock->clock_get && !new_clock->clock_get64) {
+		printk(KERN_WARNING "POSIX clock id %d lacks clock_get() and clock_get64()\n",
 		       clock_id);
 		return;
 	}
@@ -615,6 +629,8 @@  void posix_timers_register_clock(const clockid_t clock_id,
 		new_clock->timer_set64 = default_timer_set64;
 	if (new_clock->clock_set && !new_clock->clock_set64)
 		new_clock->clock_set64 = default_clock_set64;
+	if (new_clock->clock_get && !new_clock->clock_get64)
+		new_clock->clock_get64 = default_clock_get64;
 
 	posix_clocks[clock_id] = *new_clock;
 }
@@ -1124,25 +1140,25 @@  SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
 	return __clock_settime(which_clock, &new_tp);
 }
 
-static int __clock_gettime(clockid_t which_clock, struct timespec *ts)
+static int __clock_gettime(clockid_t which_clock, struct timespec64 *ts)
 {
 	struct k_clock *kc = clockid_to_kclock(which_clock);
 
 	if (!kc)
 		return -EINVAL;
 
-	return kc->clock_get(which_clock, ts);
+	return kc->clock_get64(which_clock, ts);
 }
 
 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
 		struct timespec __user *,tp)
 {
-	struct timespec kernel_tp;
+	struct timespec64 kernel_tp;
 	int error;
 
 	error = __clock_gettime(which_clock, &kernel_tp);
 
-	if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
+	if (!error && put_timespec(&kernel_tp, tp))
 		error = -EFAULT;
 
 	return error;