These are new helper macros that convert between a user timespec/ itimerspec and a kernel timespec64/itimerspec64 structure.
When converting syscall functions, it need to deal with these conversions: user timespec (32 bit), kernel timespec (32 bit) user timespec (64 bit), kernel timespec (64 bit) user timespec (32 bit), kernel timespec64 (64 bit) user timespec (64 bit), kernel timespec64 (64 bit) user itimerspec (32 bit), kernel itimerspec (32 bit) user itimerspec (64 bit), kernel itimerspec (64 bit) user itimerspec (32 bit), kernel itimerspec64 (64 bit) user itimerspec (64 bit), kernel itimerspec64 (64 bit)
In order to handle all these conversions, and ensure that it can change over all callers at the same time with the structure type above, and introduce the get/put macros based on __put_user() and __get_user().
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- kernel/time/posix-timers.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 31d11ac..5ddd912 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -147,6 +147,35 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); __timr; \ })
+#define __get_timespec(kts, uts) \ + (__get_user((kts)->tv_sec, &(uts)->tv_sec) || \ + __get_user((kts)->tv_nsec, &(uts)->tv_nsec)) + +#define __put_timespec(kts, uts) \ + (__put_user((kts)->tv_sec, &(uts)->tv_sec) || \ + __put_user((kts)->tv_nsec, &(uts)->tv_nsec)) + +#define get_timespec(kts, uts) \ + ((access_ok(VERIFY_READ, (uts), sizeof(*(uts))) || \ + __get_timespec((kts), (uts))) ? \ + -EFAULT : 0) + +#define put_timespec(kts, uts) \ + ((access_ok(VERIFY_WRITE, (uts), sizeof(*(uts))) || \ + __put_timespec((kts), (uts))) ? \ + -EFAULT : 0) + +#define get_itimerspec(kit, uit) \ + ((access_ok(VERIFY_READ, (uit), sizeof(*(uit))) || \ + __get_timespec(&(kit)->it_interval, &(uit)->it_interval) || \ + __get_timespec(&(kit)->it_value, &(uit)->it_value))) + +#define put_itimerspec(kit, uit) \ + ((access_ok(VERIFY_WRITE, (uit), sizeof(*(uit))) || \ + __put_timespec(&(kit)->it_interval, &(uit)->it_interval) || \ + __put_timespec(&(kit)->it_value, &(uit)->it_value)) ? \ + -EFAULT : 0) + static int hash(struct signal_struct *sig, unsigned int nr) { return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
In order to reuse the very same logic for the year 2038 safe syscalls which we need to introduce for 32bit systems, factor out the guts of the 'timer_gettime' syscall.
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- kernel/time/posix-timers.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 5ddd912..7d1973a 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -802,11 +802,8 @@ common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting) cur_setting->it_value = ktime_to_timespec(remaining); }
-/* Get the time remaining on a POSIX.1b interval timer. */ -SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, - struct itimerspec __user *, setting) +static int __timer_gettime(timer_t timer_id, struct itimerspec *cur_setting) { - struct itimerspec cur_setting; struct k_itimer *timr; struct k_clock *kc; unsigned long flags; @@ -820,9 +817,18 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, if (WARN_ON_ONCE(!kc || !kc->timer_get)) ret = -EINVAL; else - kc->timer_get(timr, &cur_setting); + kc->timer_get(timr, cur_setting);
unlock_timer(timr, flags); + return ret; +} + +/* Get the time remaining on a POSIX.1b interval timer. */ +SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, + struct itimerspec __user *, setting) +{ + struct itimerspec cur_setting; + int ret = __timer_gettime(timer_id, &cur_setting);
if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) return -EFAULT;
The timer_get() callback in struct k_clock is not year 2038 safe on 32bit systems.
To address this implement a new callback timer_get64() which uses struct itimerspec64 along with a default implementation which is a wrapper for the existing timer_get() callback. The default callback is installed at registration time for all posix clocks which are not yet converted to timer_get64() and will be removed once this is completed.
Use the new callback in __timer_gettime().
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- include/linux/posix-timers.h | 2 ++ kernel/time/posix-timers.c | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-)
diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 907f3fd..e84436b 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -113,6 +113,8 @@ struct k_clock { #define TIMER_RETRY 1 void (*timer_get) (struct k_itimer * timr, struct itimerspec * cur_setting); + void (*timer_get64) (struct k_itimer *timr, + struct itimerspec64 *cur_setting); };
extern struct k_clock clock_posix_cpu; diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 7d1973a..70b5550 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -140,6 +140,7 @@ static int common_timer_del(struct k_itimer *timer); static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); +static struct k_clock *clockid_to_kclock(const clockid_t id);
#define lock_timer(tid, flags) \ ({ struct k_itimer *__timr; \ @@ -549,6 +550,16 @@ static struct pid *good_sigevent(sigevent_t * event) return task_pid(rtn); }
+static void default_timer_get64(struct k_itimer *timr, + struct itimerspec64 *cur_setting64) +{ + struct itimerspec cur_setting; + struct k_clock *kc = clockid_to_kclock(timr->it_clock); + + kc->timer_get(timr, &cur_setting); + *cur_setting64 = itimerspec_to_itimerspec64(&cur_setting); +} + void posix_timers_register_clock(const clockid_t clock_id, struct k_clock *new_clock) { @@ -569,6 +580,9 @@ void posix_timers_register_clock(const clockid_t clock_id, return; }
+ if (new_clock->timer_get && !new_clock->timer_get64) + new_clock->timer_get64 = default_timer_get64; + posix_clocks[clock_id] = *new_clock; } EXPORT_SYMBOL_GPL(posix_timers_register_clock); @@ -802,7 +816,7 @@ common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting) cur_setting->it_value = ktime_to_timespec(remaining); }
-static int __timer_gettime(timer_t timer_id, struct itimerspec *cur_setting) +static int __timer_gettime(timer_t timer_id, struct itimerspec64 *cur_setting) { struct k_itimer *timr; struct k_clock *kc; @@ -814,10 +828,10 @@ static int __timer_gettime(timer_t timer_id, struct itimerspec *cur_setting) return -EINVAL;
kc = clockid_to_kclock(timr->it_clock); - if (WARN_ON_ONCE(!kc || !kc->timer_get)) + if (WARN_ON_ONCE(!kc || !kc->timer_get64)) ret = -EINVAL; else - kc->timer_get(timr, cur_setting); + kc->timer_get64(timr, cur_setting);
unlock_timer(timr, flags); return ret; @@ -827,10 +841,11 @@ static int __timer_gettime(timer_t timer_id, struct itimerspec *cur_setting) SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, struct itimerspec __user *, setting) { - struct itimerspec cur_setting; + struct itimerspec64 cur_setting; + int ret = __timer_gettime(timer_id, &cur_setting);
- if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) + if (!ret && put_itimerspec(&cur_setting, setting)) return -EFAULT;
return ret;
In order to reuse the very same logic for the year 2038 safe syscalls which we need to introduce for 32bit system, factor out the guts of the 'timer_settime' syscall.
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- kernel/time/posix-timers.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-)
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 70b5550..2f1c26f 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -927,27 +927,18 @@ common_timer_set(struct k_itimer *timr, int flags, return 0; }
-/* Set a POSIX.1b interval timer */ -SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, - const struct itimerspec __user *, new_setting, - struct itimerspec __user *, old_setting) +static int __timer_settime(timer_t timer_id, int flags, struct itimerspec *new_spec, + struct itimerspec *old_spec) { struct k_itimer *timr; - struct itimerspec new_spec, old_spec; int error = 0; unsigned long flag; - struct itimerspec *rtn = old_setting ? &old_spec : NULL; struct k_clock *kc;
- if (!new_setting) + if (!timespec_valid(&new_spec->it_interval) || + !timespec_valid(&new_spec->it_value)) return -EINVAL;
- if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) - return -EFAULT; - - if (!timespec_valid(&new_spec.it_interval) || - !timespec_valid(&new_spec.it_value)) - return -EINVAL; retry: timr = lock_timer(timer_id, &flag); if (!timr) @@ -957,14 +948,34 @@ retry: if (WARN_ON_ONCE(!kc || !kc->timer_set)) error = -EINVAL; else - error = kc->timer_set(timr, flags, &new_spec, rtn); + error = kc->timer_set(timr, flags, new_spec, old_spec);
unlock_timer(timr, flag); if (error == TIMER_RETRY) { - rtn = NULL; // We already got the old time... + old_spec = NULL; // We already got the old time... goto retry; }
+ return error; +} + +/* Set a POSIX.1b interval timer */ +SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, + const struct itimerspec __user *, new_setting, + struct itimerspec __user *, old_setting) +{ + struct itimerspec new_spec, old_spec; + int error = 0; + struct itimerspec *rtn = old_setting ? &old_spec : NULL; + + if (!new_setting) + return -EINVAL; + + if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) + return -EFAULT; + + error = __timer_settime(timer_id, flags, &new_spec, rtn); + if (old_setting && !error && copy_to_user(old_setting, &old_spec, sizeof (old_spec))) error = -EFAULT;
The timer_set() callback in struct k_clock is not year 2038 safe on 32bit systems.
To address this implement a new callback timer_set64() which uses struct itimerspec64 along with a default implementation which is a wrapper for the existing timer_set() callback. The default callback is installed at registration time for all posix clocks which are not yet converted to timer_set64() and will be removed once this is completed.
Use the new callback in __timer_settime().
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- include/linux/posix-timers.h | 3 +++ kernel/time/posix-timers.c | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index e84436b..16c3364 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -109,6 +109,9 @@ struct k_clock { int (*timer_set) (struct k_itimer * timr, int flags, struct itimerspec * new_setting, struct itimerspec * old_setting); + int (*timer_set64) (struct k_itimer *timr, int flags, + struct itimerspec64 *new_setting, + struct itimerspec64 *old_setting); int (*timer_del) (struct k_itimer * timr); #define TIMER_RETRY 1 void (*timer_get) (struct k_itimer * timr, diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 2f1c26f..778610c 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -560,6 +560,23 @@ static void default_timer_get64(struct k_itimer *timr, *cur_setting64 = itimerspec_to_itimerspec64(&cur_setting); }
+static int default_timer_set64(struct k_itimer *timr, int flags, + struct itimerspec64 *new_setting64, + struct itimerspec64 *old_setting64) +{ + struct k_clock *kc = clockid_to_kclock(timr->it_clock); + struct itimerspec new_setting, old_setting; + struct itimerspec *rtn = old_setting64 ? &old_setting : NULL; + int ret; + + new_setting = itimerspec64_to_itimerspec(new_setting64); + ret = kc->timer_set(timr, flags, &new_setting, rtn); + if (!ret && old_setting64) + *old_setting64 = itimerspec_to_itimerspec64(&old_setting); + + return ret; +} + void posix_timers_register_clock(const clockid_t clock_id, struct k_clock *new_clock) { @@ -582,6 +599,8 @@ void posix_timers_register_clock(const clockid_t clock_id,
if (new_clock->timer_get && !new_clock->timer_get64) new_clock->timer_get64 = default_timer_get64; + if (new_clock->timer_set && !new_clock->timer_set64) + new_clock->timer_set64 = default_timer_set64;
posix_clocks[clock_id] = *new_clock; } @@ -927,16 +946,16 @@ common_timer_set(struct k_itimer *timr, int flags, return 0; }
-static int __timer_settime(timer_t timer_id, int flags, struct itimerspec *new_spec, - struct itimerspec *old_spec) +static int __timer_settime(timer_t timer_id, int flags, struct itimerspec64 *new_spec, + struct itimerspec64 *old_spec) { struct k_itimer *timr; int error = 0; unsigned long flag; struct k_clock *kc;
- if (!timespec_valid(&new_spec->it_interval) || - !timespec_valid(&new_spec->it_value)) + if (!timespec64_valid(&new_spec->it_interval) || + !timespec64_valid(&new_spec->it_value)) return -EINVAL;
retry: @@ -945,10 +964,10 @@ retry: return -EINVAL;
kc = clockid_to_kclock(timr->it_clock); - if (WARN_ON_ONCE(!kc || !kc->timer_set)) + if (WARN_ON_ONCE(!kc || !kc->timer_set64)) error = -EINVAL; else - error = kc->timer_set(timr, flags, new_spec, old_spec); + error = kc->timer_set64(timr, flags, new_spec, old_spec);
unlock_timer(timr, flag); if (error == TIMER_RETRY) { @@ -964,20 +983,20 @@ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, const struct itimerspec __user *, new_setting, struct itimerspec __user *, old_setting) { - struct itimerspec new_spec, old_spec; + struct itimerspec64 new_spec, old_spec; int error = 0; - struct itimerspec *rtn = old_setting ? &old_spec : NULL; + struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
if (!new_setting) return -EINVAL;
- if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) + if (get_itimerspec(&new_spec, new_setting)) return -EFAULT;
error = __timer_settime(timer_id, flags, &new_spec, rtn);
if (old_setting && !error && - copy_to_user(old_setting, &old_spec, sizeof (old_spec))) + put_itimerspec(&old_spec, old_setting)) error = -EFAULT;
return error;
In order to reuse the very same logic for the year 2038 safe syscalls which we need to introduce for 32bit system, factor out the guts of the 'clock_settime' syscall.
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- kernel/time/posix-timers.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 778610c..65610ac 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1089,19 +1089,25 @@ void exit_itimers(struct signal_struct *sig) } }
-SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, - const struct timespec __user *, tp) +static int __clock_settime(clockid_t which_clock, struct timespec *ts) { struct k_clock *kc = clockid_to_kclock(which_clock); - struct timespec new_tp;
if (!kc || !kc->clock_set) return -EINVAL;
+ return kc->clock_set(which_clock, ts); +} + +SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, + const struct timespec __user *, tp) +{ + struct timespec new_tp; + if (copy_from_user(&new_tp, tp, sizeof (*tp))) return -EFAULT;
- return kc->clock_set(which_clock, &new_tp); + return __clock_settime(which_clock, &new_tp); }
SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
The clock_set() callback in struct k_clock is not year 2038 safe on 32bit systems.
To address this implement a new callback clock_set64() which uses struct timespec64 along with a default implementation which is a wrapper for the existing clock_set() callback. The default callback is installed at registration time for all posix clocks which are not yet converted to clock_set64() and will be removed once this is completed.
Use the new callback in __clock_settime().
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- include/linux/posix-timers.h | 2 ++ kernel/time/posix-timers.c | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index 16c3364..2b19ec8 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -100,6 +100,8 @@ struct k_clock { int (*clock_getres) (const clockid_t which_clock, struct timespec *tp); int (*clock_set) (const clockid_t which_clock, const struct timespec *tp); + 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_adj) (const clockid_t which_clock, struct timex *tx); int (*timer_create) (struct k_itimer *timer); diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 65610ac..dcc632c 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -577,6 +577,18 @@ static int default_timer_set64(struct k_itimer *timr, int flags, return ret; }
+static int default_clock_set64(const clockid_t which_clock, + const struct timespec64 *tp64) +{ + struct k_clock *kc = clockid_to_kclock(which_clock); + struct timespec tp; + int ret; + + tp = timespec64_to_timespec(*tp64); + ret = kc->clock_set(which_clock, &tp); + return ret; +} + void posix_timers_register_clock(const clockid_t clock_id, struct k_clock *new_clock) { @@ -601,6 +613,8 @@ void posix_timers_register_clock(const clockid_t clock_id, new_clock->timer_get64 = default_timer_get64; if (new_clock->timer_set && !new_clock->timer_set64) new_clock->timer_set64 = default_timer_set64; + if (new_clock->clock_set && !new_clock->clock_set64) + new_clock->clock_set64 = default_clock_set64;
posix_clocks[clock_id] = *new_clock; } @@ -1089,22 +1103,22 @@ void exit_itimers(struct signal_struct *sig) } }
-static int __clock_settime(clockid_t which_clock, struct timespec *ts) +static int __clock_settime(clockid_t which_clock, struct timespec64 *ts) { struct k_clock *kc = clockid_to_kclock(which_clock);
- if (!kc || !kc->clock_set) + if (!kc || !kc->clock_set64) return -EINVAL;
- return kc->clock_set(which_clock, ts); + return kc->clock_set64(which_clock, ts); }
SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, const struct timespec __user *, tp) { - struct timespec new_tp; + struct timespec64 new_tp;
- if (copy_from_user(&new_tp, tp, sizeof (*tp))) + if (get_timespec(&new_tp, tp)) return -EFAULT;
return __clock_settime(which_clock, &new_tp);
In order to reuse the very same logic for the year 2038 safe syscalls which we need to introduce for 32bit system, factor out the guts of the 'clock_gettime' syscall.
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- kernel/time/posix-timers.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index dcc632c..ca63626 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1124,17 +1124,23 @@ SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, return __clock_settime(which_clock, &new_tp); }
-SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, - struct timespec __user *,tp) +static int __clock_gettime(clockid_t which_clock, struct timespec *ts) { struct k_clock *kc = clockid_to_kclock(which_clock); - struct timespec kernel_tp; - int error;
if (!kc) return -EINVAL;
- error = kc->clock_get(which_clock, &kernel_tp); + return kc->clock_get(which_clock, ts); +} + +SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, + struct timespec __user *,tp) +{ + struct timespec kernel_tp; + int error; + + error = __clock_gettime(which_clock, &kernel_tp);
if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp))) error = -EFAULT;
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 --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;
In order to reuse the very same logic for the year 2038 safe syscalls which we need to introduce for 32bit system, factor out the guts of the 'clock_getres' syscall.
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- kernel/time/posix-timers.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 021cd8f..a5207f0 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1187,17 +1187,23 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, return err; }
-SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, - struct timespec __user *, tp) +static int __clock_getres(clockid_t which_clock, struct timespec *ts) { struct k_clock *kc = clockid_to_kclock(which_clock); - struct timespec rtn_tp; - int error;
if (!kc) return -EINVAL;
- error = kc->clock_getres(which_clock, &rtn_tp); + return kc->clock_getres(which_clock, ts); +} + +SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, + struct timespec __user *, tp) +{ + struct timespec rtn_tp; + int error; + + error = __clock_getres(which_clock, &rtn_tp);
if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) error = -EFAULT;
The clock_getres() callback in struct k_clock is not year 2038 safe on 32bit systems.
To address this implement a new callback clock_getres64() which uses struct timespec64 along with a default implementation which is a wrapper for the existing clock_getres() callback. The default callback is installed at registration time for all posix clocks which are not yet converted to clock_getres64() and will be removed once this is completed.
Use the new callback in __clock_getres().
Signed-off-by: Baolin Wang baolin.wang@linaro.org --- include/linux/posix-timers.h | 1 + kernel/time/posix-timers.c | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h index be2123d..35786c5 100644 --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -98,6 +98,7 @@ struct k_itimer {
struct k_clock { int (*clock_getres) (const clockid_t which_clock, struct timespec *tp); + int (*clock_getres64) (const clockid_t which_clock, struct timespec64 *tp); int (*clock_set) (const clockid_t which_clock, const struct timespec *tp); int (*clock_set64) (const clockid_t which_clock, diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index a5207f0..0b0663a 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -603,6 +603,20 @@ static int default_clock_get64(const clockid_t which_clock, return ret; }
+static int default_clock_getres64(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_getres(which_clock, &tp); + if (!ret) + *tp64 = timespec_to_timespec64(tp); + + return 0; +} + void posix_timers_register_clock(const clockid_t clock_id, struct k_clock *new_clock) { @@ -617,8 +631,8 @@ void posix_timers_register_clock(const clockid_t clock_id, clock_id); return; } - if (!new_clock->clock_getres) { - printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n", + if (!new_clock->clock_getres && !new_clock->clock_getres64) { + printk(KERN_WARNING "POSIX clock id %d lacks clock_getres() and clock_getres64()\n", clock_id); return; } @@ -631,6 +645,8 @@ void posix_timers_register_clock(const clockid_t clock_id, new_clock->clock_set64 = default_clock_set64; if (new_clock->clock_get && !new_clock->clock_get64) new_clock->clock_get64 = default_clock_get64; + if (new_clock->clock_getres && !new_clock->clock_getres64) + new_clock->clock_getres64 = default_clock_getres64;
posix_clocks[clock_id] = *new_clock; } @@ -678,7 +694,8 @@ static struct k_clock *clockid_to_kclock(const clockid_t id) return (id & CLOCKFD_MASK) == CLOCKFD ? &clock_posix_dynamic : &clock_posix_cpu;
- if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres) + if (id >= MAX_CLOCKS || (!posix_clocks[id].clock_getres + && !posix_clocks[id].clock_getres64)) return NULL; return &posix_clocks[id]; } @@ -1187,25 +1204,25 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, return err; }
-static int __clock_getres(clockid_t which_clock, struct timespec *ts) +static int __clock_getres(clockid_t which_clock, struct timespec64 *ts) { struct k_clock *kc = clockid_to_kclock(which_clock);
if (!kc) return -EINVAL;
- return kc->clock_getres(which_clock, ts); + return kc->clock_getres64(which_clock, ts); }
SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, struct timespec __user *, tp) { - struct timespec rtn_tp; + struct timespec64 rtn_tp; int error;
error = __clock_getres(which_clock, &rtn_tp);
- if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) + if (!error && tp && put_timespec(&rtn_tp, tp)) error = -EFAULT;
return error;