Skip to content

Commit d822cdc

Browse files
Al ViroKAGA-KOKO
authored andcommitted
posix-timers: Move compat versions of clock_gettime/settime/getres
Move them to the native implementations and get rid of the set_fs() hackery. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: John Stultz <john.stultz@linaro.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20170607084241.28657-13-viro@ZenIV.linux.org.uk
1 parent 54ad9c4 commit d822cdc

3 files changed

Lines changed: 115 additions & 55 deletions

File tree

‎kernel/compat.c‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -598,57 +598,6 @@ COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
598598
return sys_timer_create(which_clock, event, created_timer_id);
599599
}
600600

601-
COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
602-
struct compat_timespec __user *, tp)
603-
{
604-
long err;
605-
mm_segment_t oldfs;
606-
struct timespec ts;
607-
608-
if (compat_get_timespec(&ts, tp))
609-
return -EFAULT;
610-
oldfs = get_fs();
611-
set_fs(KERNEL_DS);
612-
err = sys_clock_settime(which_clock,
613-
(struct timespec __user *) &ts);
614-
set_fs(oldfs);
615-
return err;
616-
}
617-
618-
COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
619-
struct compat_timespec __user *, tp)
620-
{
621-
long err;
622-
mm_segment_t oldfs;
623-
struct timespec ts;
624-
625-
oldfs = get_fs();
626-
set_fs(KERNEL_DS);
627-
err = sys_clock_gettime(which_clock,
628-
(struct timespec __user *) &ts);
629-
set_fs(oldfs);
630-
if (!err && compat_put_timespec(&ts, tp))
631-
return -EFAULT;
632-
return err;
633-
}
634-
635-
COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
636-
struct compat_timespec __user *, tp)
637-
{
638-
long err;
639-
mm_segment_t oldfs;
640-
struct timespec ts;
641-
642-
oldfs = get_fs();
643-
set_fs(KERNEL_DS);
644-
err = sys_clock_getres(which_clock,
645-
(struct timespec __user *) &ts);
646-
set_fs(oldfs);
647-
if (!err && tp && compat_put_timespec(&ts, tp))
648-
return -EFAULT;
649-
return err;
650-
}
651-
652601
/*
653602
* We currently only need the following fields from the sigevent
654603
* structure: sigev_value, sigev_signo, sig_notify and (sometimes

‎kernel/time/posix-stubs.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,59 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
137137
}
138138

139139
#ifdef CONFIG_COMPAT
140+
COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
141+
struct compat_timespec __user *, tp)
142+
{
143+
struct timespec64 new_tp64;
144+
struct timespec new_tp;
145+
146+
if (which_clock != CLOCK_REALTIME)
147+
return -EINVAL;
148+
if (compat_get_timespec(&new_tp, tp))
149+
return -EFAULT;
150+
151+
new_tp64 = timespec_to_timespec64(new_tp);
152+
return do_sys_settimeofday64(&new_tp64, NULL);
153+
}
154+
155+
COMPAT_SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
156+
struct compat_timespec __user *,tp)
157+
{
158+
struct timespec64 kernel_tp64;
159+
struct timespec kernel_tp;
160+
161+
switch (which_clock) {
162+
case CLOCK_REALTIME: ktime_get_real_ts64(&kernel_tp64); break;
163+
case CLOCK_MONOTONIC: ktime_get_ts64(&kernel_tp64); break;
164+
case CLOCK_BOOTTIME: get_monotonic_boottime64(&kernel_tp64); break;
165+
default: return -EINVAL;
166+
}
167+
168+
kernel_tp = timespec64_to_timespec(kernel_tp64);
169+
if (compat_put_timespec(&kernel_tp, tp))
170+
return -EFAULT;
171+
return 0;
172+
}
173+
174+
COMPAT_SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
175+
struct compat_timespec __user *, tp)
176+
{
177+
struct timespec rtn_tp = {
178+
.tv_sec = 0,
179+
.tv_nsec = hrtimer_resolution,
180+
};
181+
182+
switch (which_clock) {
183+
case CLOCK_REALTIME:
184+
case CLOCK_MONOTONIC:
185+
case CLOCK_BOOTTIME:
186+
if (compat_put_timespec(&rtn_tp, tp))
187+
return -EFAULT;
188+
return 0;
189+
default:
190+
return -EINVAL;
191+
}
192+
}
140193
COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
141194
struct compat_timespec __user *, rqtp,
142195
struct compat_timespec __user *, rmtp)

‎kernel/time/posix-timers.c‎

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,66 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
10821082
return err;
10831083
}
10841084

1085+
SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1086+
struct timespec __user *, tp)
1087+
{
1088+
const struct k_clock *kc = clockid_to_kclock(which_clock);
1089+
struct timespec64 rtn_tp64;
1090+
struct timespec rtn_tp;
1091+
int error;
1092+
1093+
if (!kc)
1094+
return -EINVAL;
1095+
1096+
error = kc->clock_getres(which_clock, &rtn_tp64);
1097+
rtn_tp = timespec64_to_timespec(rtn_tp64);
1098+
1099+
if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1100+
error = -EFAULT;
1101+
1102+
return error;
1103+
}
1104+
10851105
#ifdef CONFIG_COMPAT
10861106

1107+
COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1108+
struct compat_timespec __user *, tp)
1109+
{
1110+
const struct k_clock *kc = clockid_to_kclock(which_clock);
1111+
struct timespec64 new_tp64;
1112+
struct timespec new_tp;
1113+
1114+
if (!kc || !kc->clock_set)
1115+
return -EINVAL;
1116+
1117+
if (compat_get_timespec(&new_tp, tp))
1118+
return -EFAULT;
1119+
1120+
new_tp64 = timespec_to_timespec64(new_tp);
1121+
1122+
return kc->clock_set(which_clock, &new_tp64);
1123+
}
1124+
1125+
COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1126+
struct compat_timespec __user *, tp)
1127+
{
1128+
const struct k_clock *kc = clockid_to_kclock(which_clock);
1129+
struct timespec64 kernel_tp64;
1130+
struct timespec kernel_tp;
1131+
int error;
1132+
1133+
if (!kc)
1134+
return -EINVAL;
1135+
1136+
error = kc->clock_get(which_clock, &kernel_tp64);
1137+
kernel_tp = timespec64_to_timespec(kernel_tp64);
1138+
1139+
if (!error && compat_put_timespec(&kernel_tp, tp))
1140+
error = -EFAULT;
1141+
1142+
return error;
1143+
}
1144+
10871145
COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
10881146
struct compat_timex __user *, utp)
10891147
{
@@ -1107,10 +1165,9 @@ COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
11071165

11081166
return err;
11091167
}
1110-
#endif
11111168

1112-
SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1113-
struct timespec __user *, tp)
1169+
COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1170+
struct compat_timespec __user *, tp)
11141171
{
11151172
const struct k_clock *kc = clockid_to_kclock(which_clock);
11161173
struct timespec64 rtn_tp64;
@@ -1123,11 +1180,12 @@ SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
11231180
error = kc->clock_getres(which_clock, &rtn_tp64);
11241181
rtn_tp = timespec64_to_timespec(rtn_tp64);
11251182

1126-
if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1183+
if (!error && tp && compat_put_timespec(&rtn_tp, tp))
11271184
error = -EFAULT;
11281185

11291186
return error;
11301187
}
1188+
#endif
11311189

11321190
/*
11331191
* nanosleep for monotonic and realtime clocks

0 commit comments

Comments
 (0)