summaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/compat.c526
-rw-r--r--kernel/signal.c3
-rw-r--r--kernel/time/alarmtimer.c381
-rw-r--r--kernel/time/hrtimer.c106
-rw-r--r--kernel/time/itimer.c46
-rw-r--r--kernel/time/posix-clock.c117
-rw-r--r--kernel/time/posix-cpu-timers.c147
-rw-r--r--kernel/time/posix-stubs.c112
-rw-r--r--kernel/time/posix-timers.c759
-rw-r--r--kernel/time/posix-timers.h40
-rw-r--r--kernel/time/time.c106
-rw-r--r--kernel/time/timekeeping.c46
-rw-r--r--kernel/time/timer.c50
13 files changed, 1198 insertions, 1241 deletions
diff --git a/kernel/compat.c b/kernel/compat.c
index 933bcb31ae10..ebd8bdc3fd68 100644
--- a/kernel/compat.c
+++ b/kernel/compat.c
@@ -30,100 +30,66 @@
30 30
31#include <linux/uaccess.h> 31#include <linux/uaccess.h>
32 32
33static int compat_get_timex(struct timex *txc, struct compat_timex __user *utp) 33int compat_get_timex(struct timex *txc, const struct compat_timex __user *utp)
34{ 34{
35 memset(txc, 0, sizeof(struct timex)); 35 struct compat_timex tx32;
36
37 if (!access_ok(VERIFY_READ, utp, sizeof(struct compat_timex)) ||
38 __get_user(txc->modes, &utp->modes) ||
39 __get_user(txc->offset, &utp->offset) ||
40 __get_user(txc->freq, &utp->freq) ||
41 __get_user(txc->maxerror, &utp->maxerror) ||
42 __get_user(txc->esterror, &utp->esterror) ||
43 __get_user(txc->status, &utp->status) ||
44 __get_user(txc->constant, &utp->constant) ||
45 __get_user(txc->precision, &utp->precision) ||
46 __get_user(txc->tolerance, &utp->tolerance) ||
47 __get_user(txc->time.tv_sec, &utp->time.tv_sec) ||
48 __get_user(txc->time.tv_usec, &utp->time.tv_usec) ||
49 __get_user(txc->tick, &utp->tick) ||
50 __get_user(txc->ppsfreq, &utp->ppsfreq) ||
51 __get_user(txc->jitter, &utp->jitter) ||
52 __get_user(txc->shift, &utp->shift) ||
53 __get_user(txc->stabil, &utp->stabil) ||
54 __get_user(txc->jitcnt, &utp->jitcnt) ||
55 __get_user(txc->calcnt, &utp->calcnt) ||
56 __get_user(txc->errcnt, &utp->errcnt) ||
57 __get_user(txc->stbcnt, &utp->stbcnt))
58 return -EFAULT;
59 36
60 return 0; 37 if (copy_from_user(&tx32, utp, sizeof(struct compat_timex)))
61}
62
63static int compat_put_timex(struct compat_timex __user *utp, struct timex *txc)
64{
65 if (!access_ok(VERIFY_WRITE, utp, sizeof(struct compat_timex)) ||
66 __put_user(txc->modes, &utp->modes) ||
67 __put_user(txc->offset, &utp->offset) ||
68 __put_user(txc->freq, &utp->freq) ||
69 __put_user(txc->maxerror, &utp->maxerror) ||
70 __put_user(txc->esterror, &utp->esterror) ||
71 __put_user(txc->status, &utp->status) ||
72 __put_user(txc->constant, &utp->constant) ||
73 __put_user(txc->precision, &utp->precision) ||
74 __put_user(txc->tolerance, &utp->tolerance) ||
75 __put_user(txc->time.tv_sec, &utp->time.tv_sec) ||
76 __put_user(txc->time.tv_usec, &utp->time.tv_usec) ||
77 __put_user(txc->tick, &utp->tick) ||
78 __put_user(txc->ppsfreq, &utp->ppsfreq) ||
79 __put_user(txc->jitter, &utp->jitter) ||
80 __put_user(txc->shift, &utp->shift) ||
81 __put_user(txc->stabil, &utp->stabil) ||
82 __put_user(txc->jitcnt, &utp->jitcnt) ||
83 __put_user(txc->calcnt, &utp->calcnt) ||
84 __put_user(txc->errcnt, &utp->errcnt) ||
85 __put_user(txc->stbcnt, &utp->stbcnt) ||
86 __put_user(txc->tai, &utp->tai))
87 return -EFAULT; 38 return -EFAULT;
88 return 0;
89}
90 39
91COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv, 40 txc->modes = tx32.modes;
92 struct timezone __user *, tz) 41 txc->offset = tx32.offset;
93{ 42 txc->freq = tx32.freq;
94 if (tv) { 43 txc->maxerror = tx32.maxerror;
95 struct timeval ktv; 44 txc->esterror = tx32.esterror;
96 do_gettimeofday(&ktv); 45 txc->status = tx32.status;
97 if (compat_put_timeval(&ktv, tv)) 46 txc->constant = tx32.constant;
98 return -EFAULT; 47 txc->precision = tx32.precision;
99 } 48 txc->tolerance = tx32.tolerance;
100 if (tz) { 49 txc->time.tv_sec = tx32.time.tv_sec;
101 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) 50 txc->time.tv_usec = tx32.time.tv_usec;
102 return -EFAULT; 51 txc->tick = tx32.tick;
103 } 52 txc->ppsfreq = tx32.ppsfreq;
53 txc->jitter = tx32.jitter;
54 txc->shift = tx32.shift;
55 txc->stabil = tx32.stabil;
56 txc->jitcnt = tx32.jitcnt;
57 txc->calcnt = tx32.calcnt;
58 txc->errcnt = tx32.errcnt;
59 txc->stbcnt = tx32.stbcnt;
104 60
105 return 0; 61 return 0;
106} 62}
107 63
108COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv, 64int compat_put_timex(struct compat_timex __user *utp, const struct timex *txc)
109 struct timezone __user *, tz) 65{
110{ 66 struct compat_timex tx32;
111 struct timespec64 new_ts; 67
112 struct timeval user_tv; 68 memset(&tx32, 0, sizeof(struct compat_timex));
113 struct timezone new_tz; 69 tx32.modes = txc->modes;
114 70 tx32.offset = txc->offset;
115 if (tv) { 71 tx32.freq = txc->freq;
116 if (compat_get_timeval(&user_tv, tv)) 72 tx32.maxerror = txc->maxerror;
117 return -EFAULT; 73 tx32.esterror = txc->esterror;
118 new_ts.tv_sec = user_tv.tv_sec; 74 tx32.status = txc->status;
119 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; 75 tx32.constant = txc->constant;
120 } 76 tx32.precision = txc->precision;
121 if (tz) { 77 tx32.tolerance = txc->tolerance;
122 if (copy_from_user(&new_tz, tz, sizeof(*tz))) 78 tx32.time.tv_sec = txc->time.tv_sec;
123 return -EFAULT; 79 tx32.time.tv_usec = txc->time.tv_usec;
124 } 80 tx32.tick = txc->tick;
125 81 tx32.ppsfreq = txc->ppsfreq;
126 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 82 tx32.jitter = txc->jitter;
83 tx32.shift = txc->shift;
84 tx32.stabil = txc->stabil;
85 tx32.jitcnt = txc->jitcnt;
86 tx32.calcnt = txc->calcnt;
87 tx32.errcnt = txc->errcnt;
88 tx32.stbcnt = txc->stbcnt;
89 tx32.tai = txc->tai;
90 if (copy_to_user(utp, &tx32, sizeof(struct compat_timex)))
91 return -EFAULT;
92 return 0;
127} 93}
128 94
129static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv) 95static int __compat_get_timeval(struct timeval *tv, const struct compat_timeval __user *ctv)
@@ -213,141 +179,28 @@ int compat_convert_timespec(struct timespec __user **kts,
213 return 0; 179 return 0;
214} 180}
215 181
216static long compat_nanosleep_restart(struct restart_block *restart) 182int get_compat_itimerval(struct itimerval *o, const struct compat_itimerval __user *i)
217{
218 struct compat_timespec __user *rmtp;
219 struct timespec rmt;
220 mm_segment_t oldfs;
221 long ret;
222
223 restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
224 oldfs = get_fs();
225 set_fs(KERNEL_DS);
226 ret = hrtimer_nanosleep_restart(restart);
227 set_fs(oldfs);
228
229 if (ret == -ERESTART_RESTARTBLOCK) {
230 rmtp = restart->nanosleep.compat_rmtp;
231
232 if (rmtp && compat_put_timespec(&rmt, rmtp))
233 return -EFAULT;
234 }
235
236 return ret;
237}
238
239COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
240 struct compat_timespec __user *, rmtp)
241{ 183{
242 struct timespec tu, rmt; 184 struct compat_itimerval v32;
243 struct timespec64 tu64;
244 mm_segment_t oldfs;
245 long ret;
246 185
247 if (compat_get_timespec(&tu, rqtp)) 186 if (copy_from_user(&v32, i, sizeof(struct compat_itimerval)))
248 return -EFAULT; 187 return -EFAULT;
249 188 o->it_interval.tv_sec = v32.it_interval.tv_sec;
250 tu64 = timespec_to_timespec64(tu); 189 o->it_interval.tv_usec = v32.it_interval.tv_usec;
251 if (!timespec64_valid(&tu64)) 190 o->it_value.tv_sec = v32.it_value.tv_sec;
252 return -EINVAL; 191 o->it_value.tv_usec = v32.it_value.tv_usec;
253 192 return 0;
254 oldfs = get_fs();
255 set_fs(KERNEL_DS);
256 ret = hrtimer_nanosleep(&tu64,
257 rmtp ? (struct timespec __user *)&rmt : NULL,
258 HRTIMER_MODE_REL, CLOCK_MONOTONIC);
259 set_fs(oldfs);
260
261 /*
262 * hrtimer_nanosleep() can only return 0 or
263 * -ERESTART_RESTARTBLOCK here because:
264 *
265 * - we call it with HRTIMER_MODE_REL and therefor exclude the
266 * -ERESTARTNOHAND return path.
267 *
268 * - we supply the rmtp argument from the task stack (due to
269 * the necessary compat conversion. So the update cannot
270 * fail, which excludes the -EFAULT return path as well. If
271 * it fails nevertheless we have a bigger problem and wont
272 * reach this place anymore.
273 *
274 * - if the return value is 0, we do not have to update rmtp
275 * because there is no remaining time.
276 *
277 * We check for -ERESTART_RESTARTBLOCK nevertheless if the
278 * core implementation decides to return random nonsense.
279 */
280 if (ret == -ERESTART_RESTARTBLOCK) {
281 struct restart_block *restart = &current->restart_block;
282
283 restart->fn = compat_nanosleep_restart;
284 restart->nanosleep.compat_rmtp = rmtp;
285
286 if (rmtp && compat_put_timespec(&rmt, rmtp))
287 return -EFAULT;
288 }
289 return ret;
290}
291
292static inline long get_compat_itimerval(struct itimerval *o,
293 struct compat_itimerval __user *i)
294{
295 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
296 (__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
297 __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
298 __get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
299 __get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
300}
301
302static inline long put_compat_itimerval(struct compat_itimerval __user *o,
303 struct itimerval *i)
304{
305 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
306 (__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
307 __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
308 __put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
309 __put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
310}
311
312asmlinkage long sys_ni_posix_timers(void);
313
314COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
315 struct compat_itimerval __user *, it)
316{
317 struct itimerval kit;
318 int error;
319
320 if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
321 return sys_ni_posix_timers();
322
323 error = do_getitimer(which, &kit);
324 if (!error && put_compat_itimerval(it, &kit))
325 error = -EFAULT;
326 return error;
327} 193}
328 194
329COMPAT_SYSCALL_DEFINE3(setitimer, int, which, 195int put_compat_itimerval(struct compat_itimerval __user *o, const struct itimerval *i)
330 struct compat_itimerval __user *, in,
331 struct compat_itimerval __user *, out)
332{ 196{
333 struct itimerval kin, kout; 197 struct compat_itimerval v32;
334 int error;
335
336 if (!IS_ENABLED(CONFIG_POSIX_TIMERS))
337 return sys_ni_posix_timers();
338 198
339 if (in) { 199 v32.it_interval.tv_sec = i->it_interval.tv_sec;
340 if (get_compat_itimerval(&kin, in)) 200 v32.it_interval.tv_usec = i->it_interval.tv_usec;
341 return -EFAULT; 201 v32.it_value.tv_sec = i->it_value.tv_sec;
342 } else 202 v32.it_value.tv_usec = i->it_value.tv_usec;
343 memset(&kin, 0, sizeof(kin)); 203 return copy_to_user(o, &v32, sizeof(struct compat_itimerval)) ? -EFAULT : 0;
344
345 error = do_setitimer(which, &kin, out ? &kout : NULL);
346 if (error || !out)
347 return error;
348 if (put_compat_itimerval(out, &kout))
349 return -EFAULT;
350 return 0;
351} 204}
352 205
353static compat_clock_t clock_t_to_compat_clock_t(clock_t x) 206static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
@@ -689,193 +542,6 @@ int put_compat_itimerspec(struct compat_itimerspec __user *dst,
689 return 0; 542 return 0;
690} 543}
691 544
692COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
693 struct compat_sigevent __user *, timer_event_spec,
694 timer_t __user *, created_timer_id)
695{
696 struct sigevent __user *event = NULL;
697
698 if (timer_event_spec) {
699 struct sigevent kevent;
700
701 event = compat_alloc_user_space(sizeof(*event));
702 if (get_compat_sigevent(&kevent, timer_event_spec) ||
703 copy_to_user(event, &kevent, sizeof(*event)))
704 return -EFAULT;
705 }
706
707 return sys_timer_create(which_clock, event, created_timer_id);
708}
709
710COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
711 struct compat_itimerspec __user *, new,
712 struct compat_itimerspec __user *, old)
713{
714 long err;
715 mm_segment_t oldfs;
716 struct itimerspec newts, oldts;
717
718 if (!new)
719 return -EINVAL;
720 if (get_compat_itimerspec(&newts, new))
721 return -EFAULT;
722 oldfs = get_fs();
723 set_fs(KERNEL_DS);
724 err = sys_timer_settime(timer_id, flags,
725 (struct itimerspec __user *) &newts,
726 (struct itimerspec __user *) &oldts);
727 set_fs(oldfs);
728 if (!err && old && put_compat_itimerspec(old, &oldts))
729 return -EFAULT;
730 return err;
731}
732
733COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
734 struct compat_itimerspec __user *, setting)
735{
736 long err;
737 mm_segment_t oldfs;
738 struct itimerspec ts;
739
740 oldfs = get_fs();
741 set_fs(KERNEL_DS);
742 err = sys_timer_gettime(timer_id,
743 (struct itimerspec __user *) &ts);
744 set_fs(oldfs);
745 if (!err && put_compat_itimerspec(setting, &ts))
746 return -EFAULT;
747 return err;
748}
749
750COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
751 struct compat_timespec __user *, tp)
752{
753 long err;
754 mm_segment_t oldfs;
755 struct timespec ts;
756
757 if (compat_get_timespec(&ts, tp))
758 return -EFAULT;
759 oldfs = get_fs();
760 set_fs(KERNEL_DS);
761 err = sys_clock_settime(which_clock,
762 (struct timespec __user *) &ts);
763 set_fs(oldfs);
764 return err;
765}
766
767COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
768 struct compat_timespec __user *, tp)
769{
770 long err;
771 mm_segment_t oldfs;
772 struct timespec ts;
773
774 oldfs = get_fs();
775 set_fs(KERNEL_DS);
776 err = sys_clock_gettime(which_clock,
777 (struct timespec __user *) &ts);
778 set_fs(oldfs);
779 if (!err && compat_put_timespec(&ts, tp))
780 return -EFAULT;
781 return err;
782}
783
784COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
785 struct compat_timex __user *, utp)
786{
787 struct timex txc;
788 mm_segment_t oldfs;
789 int err, ret;
790
791 err = compat_get_timex(&txc, utp);
792 if (err)
793 return err;
794
795 oldfs = get_fs();
796 set_fs(KERNEL_DS);
797 ret = sys_clock_adjtime(which_clock, (struct timex __user *) &txc);
798 set_fs(oldfs);
799
800 err = compat_put_timex(utp, &txc);
801 if (err)
802 return err;
803
804 return ret;
805}
806
807COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
808 struct compat_timespec __user *, tp)
809{
810 long err;
811 mm_segment_t oldfs;
812 struct timespec ts;
813
814 oldfs = get_fs();
815 set_fs(KERNEL_DS);
816 err = sys_clock_getres(which_clock,
817 (struct timespec __user *) &ts);
818 set_fs(oldfs);
819 if (!err && tp && compat_put_timespec(&ts, tp))
820 return -EFAULT;
821 return err;
822}
823
824static long compat_clock_nanosleep_restart(struct restart_block *restart)
825{
826 long err;
827 mm_segment_t oldfs;
828 struct timespec tu;
829 struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
830
831 restart->nanosleep.rmtp = (struct timespec __user *) &tu;
832 oldfs = get_fs();
833 set_fs(KERNEL_DS);
834 err = clock_nanosleep_restart(restart);
835 set_fs(oldfs);
836
837 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
838 compat_put_timespec(&tu, rmtp))
839 return -EFAULT;
840
841 if (err == -ERESTART_RESTARTBLOCK) {
842 restart->fn = compat_clock_nanosleep_restart;
843 restart->nanosleep.compat_rmtp = rmtp;
844 }
845 return err;
846}
847
848COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
849 struct compat_timespec __user *, rqtp,
850 struct compat_timespec __user *, rmtp)
851{
852 long err;
853 mm_segment_t oldfs;
854 struct timespec in, out;
855 struct restart_block *restart;
856
857 if (compat_get_timespec(&in, rqtp))
858 return -EFAULT;
859
860 oldfs = get_fs();
861 set_fs(KERNEL_DS);
862 err = sys_clock_nanosleep(which_clock, flags,
863 (struct timespec __user *) &in,
864 (struct timespec __user *) &out);
865 set_fs(oldfs);
866
867 if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
868 compat_put_timespec(&out, rmtp))
869 return -EFAULT;
870
871 if (err == -ERESTART_RESTARTBLOCK) {
872 restart = &current->restart_block;
873 restart->fn = compat_clock_nanosleep_restart;
874 restart->nanosleep.compat_rmtp = rmtp;
875 }
876 return err;
877}
878
879/* 545/*
880 * We currently only need the following fields from the sigevent 546 * We currently only need the following fields from the sigevent
881 * structure: sigev_value, sigev_signo, sig_notify and (sometimes 547 * structure: sigev_value, sigev_signo, sig_notify and (sometimes
@@ -1035,64 +701,6 @@ COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
1035 return ret; 701 return ret;
1036} 702}
1037 703
1038#ifdef __ARCH_WANT_COMPAT_SYS_TIME
1039
1040/* compat_time_t is a 32 bit "long" and needs to get converted. */
1041
1042COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
1043{
1044 compat_time_t i;
1045 struct timeval tv;
1046
1047 do_gettimeofday(&tv);
1048 i = tv.tv_sec;
1049
1050 if (tloc) {
1051 if (put_user(i,tloc))
1052 return -EFAULT;
1053 }
1054 force_successful_syscall_return();
1055 return i;
1056}
1057
1058COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
1059{
1060 struct timespec tv;
1061 int err;
1062
1063 if (get_user(tv.tv_sec, tptr))
1064 return -EFAULT;
1065
1066 tv.tv_nsec = 0;
1067
1068 err = security_settime(&tv, NULL);
1069 if (err)
1070 return err;
1071
1072 do_settimeofday(&tv);
1073 return 0;
1074}
1075
1076#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
1077
1078COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
1079{
1080 struct timex txc;
1081 int err, ret;
1082
1083 err = compat_get_timex(&txc, utp);
1084 if (err)
1085 return err;
1086
1087 ret = do_adjtimex(&txc);
1088
1089 err = compat_put_timex(utp, &txc);
1090 if (err)
1091 return err;
1092
1093 return ret;
1094}
1095
1096#ifdef CONFIG_NUMA 704#ifdef CONFIG_NUMA
1097COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages, 705COMPAT_SYSCALL_DEFINE6(move_pages, pid_t, pid, compat_ulong_t, nr_pages,
1098 compat_uptr_t __user *, pages32, 706 compat_uptr_t __user *, pages32,
diff --git a/kernel/signal.c b/kernel/signal.c
index 9b28954e058f..35a570f71f07 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -39,6 +39,7 @@
39#include <linux/compat.h> 39#include <linux/compat.h>
40#include <linux/cn_proc.h> 40#include <linux/cn_proc.h>
41#include <linux/compiler.h> 41#include <linux/compiler.h>
42#include <linux/posix-timers.h>
42 43
43#define CREATE_TRACE_POINTS 44#define CREATE_TRACE_POINTS
44#include <trace/events/signal.h> 45#include <trace/events/signal.h>
@@ -637,7 +638,7 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
637 * about to disable them again anyway. 638 * about to disable them again anyway.
638 */ 639 */
639 spin_unlock(&tsk->sighand->siglock); 640 spin_unlock(&tsk->sighand->siglock);
640 do_schedule_next_timer(info); 641 posixtimer_rearm(info);
641 spin_lock(&tsk->sighand->siglock); 642 spin_lock(&tsk->sighand->siglock);
642 } 643 }
643#endif 644#endif
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index ee2f4202d82a..c991cf212c6d 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -27,6 +27,9 @@
27#include <linux/posix-timers.h> 27#include <linux/posix-timers.h>
28#include <linux/workqueue.h> 28#include <linux/workqueue.h>
29#include <linux/freezer.h> 29#include <linux/freezer.h>
30#include <linux/compat.h>
31
32#include "posix-timers.h"
30 33
31#define CREATE_TRACE_POINTS 34#define CREATE_TRACE_POINTS
32#include <trace/events/alarmtimer.h> 35#include <trace/events/alarmtimer.h>
@@ -45,11 +48,13 @@ static struct alarm_base {
45 clockid_t base_clockid; 48 clockid_t base_clockid;
46} alarm_bases[ALARM_NUMTYPE]; 49} alarm_bases[ALARM_NUMTYPE];
47 50
51#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
48/* freezer information to handle clock_nanosleep triggered wakeups */ 52/* freezer information to handle clock_nanosleep triggered wakeups */
49static enum alarmtimer_type freezer_alarmtype; 53static enum alarmtimer_type freezer_alarmtype;
50static ktime_t freezer_expires; 54static ktime_t freezer_expires;
51static ktime_t freezer_delta; 55static ktime_t freezer_delta;
52static DEFINE_SPINLOCK(freezer_delta_lock); 56static DEFINE_SPINLOCK(freezer_delta_lock);
57#endif
53 58
54static struct wakeup_source *ws; 59static struct wakeup_source *ws;
55 60
@@ -307,38 +312,6 @@ static int alarmtimer_resume(struct device *dev)
307} 312}
308#endif 313#endif
309 314
310static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
311{
312 struct alarm_base *base;
313 unsigned long flags;
314 ktime_t delta;
315
316 switch(type) {
317 case ALARM_REALTIME:
318 base = &alarm_bases[ALARM_REALTIME];
319 type = ALARM_REALTIME_FREEZER;
320 break;
321 case ALARM_BOOTTIME:
322 base = &alarm_bases[ALARM_BOOTTIME];
323 type = ALARM_BOOTTIME_FREEZER;
324 break;
325 default:
326 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
327 return;
328 }
329
330 delta = ktime_sub(absexp, base->gettime());
331
332 spin_lock_irqsave(&freezer_delta_lock, flags);
333 if (!freezer_delta || (delta < freezer_delta)) {
334 freezer_delta = delta;
335 freezer_expires = absexp;
336 freezer_alarmtype = type;
337 }
338 spin_unlock_irqrestore(&freezer_delta_lock, flags);
339}
340
341
342/** 315/**
343 * alarm_init - Initialize an alarm structure 316 * alarm_init - Initialize an alarm structure
344 * @alarm: ptr to alarm to be initialized 317 * @alarm: ptr to alarm to be initialized
@@ -488,6 +461,38 @@ u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
488} 461}
489EXPORT_SYMBOL_GPL(alarm_forward_now); 462EXPORT_SYMBOL_GPL(alarm_forward_now);
490 463
464#ifdef CONFIG_POSIX_TIMERS
465
466static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
467{
468 struct alarm_base *base;
469 unsigned long flags;
470 ktime_t delta;
471
472 switch(type) {
473 case ALARM_REALTIME:
474 base = &alarm_bases[ALARM_REALTIME];
475 type = ALARM_REALTIME_FREEZER;
476 break;
477 case ALARM_BOOTTIME:
478 base = &alarm_bases[ALARM_BOOTTIME];
479 type = ALARM_BOOTTIME_FREEZER;
480 break;
481 default:
482 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
483 return;
484 }
485
486 delta = ktime_sub(absexp, base->gettime());
487
488 spin_lock_irqsave(&freezer_delta_lock, flags);
489 if (!freezer_delta || (delta < freezer_delta)) {
490 freezer_delta = delta;
491 freezer_expires = absexp;
492 freezer_alarmtype = type;
493 }
494 spin_unlock_irqrestore(&freezer_delta_lock, flags);
495}
491 496
492/** 497/**
493 * clock2alarm - helper that converts from clockid to alarmtypes 498 * clock2alarm - helper that converts from clockid to alarmtypes
@@ -511,22 +516,26 @@ static enum alarmtimer_type clock2alarm(clockid_t clockid)
511static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, 516static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
512 ktime_t now) 517 ktime_t now)
513{ 518{
514 unsigned long flags;
515 struct k_itimer *ptr = container_of(alarm, struct k_itimer, 519 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
516 it.alarm.alarmtimer); 520 it.alarm.alarmtimer);
517 enum alarmtimer_restart result = ALARMTIMER_NORESTART; 521 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
522 unsigned long flags;
523 int si_private = 0;
518 524
519 spin_lock_irqsave(&ptr->it_lock, flags); 525 spin_lock_irqsave(&ptr->it_lock, flags);
520 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
521 if (IS_ENABLED(CONFIG_POSIX_TIMERS) &&
522 posix_timer_event(ptr, 0) != 0)
523 ptr->it_overrun++;
524 }
525 526
526 /* Re-add periodic timers */ 527 ptr->it_active = 0;
527 if (ptr->it.alarm.interval) { 528 if (ptr->it_interval)
528 ptr->it_overrun += alarm_forward(alarm, now, 529 si_private = ++ptr->it_requeue_pending;
529 ptr->it.alarm.interval); 530
531 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
532 /*
533 * Handle ignored signals and rearm the timer. This will go
534 * away once we handle ignored signals proper.
535 */
536 ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
537 ++ptr->it_requeue_pending;
538 ptr->it_active = 1;
530 result = ALARMTIMER_RESTART; 539 result = ALARMTIMER_RESTART;
531 } 540 }
532 spin_unlock_irqrestore(&ptr->it_lock, flags); 541 spin_unlock_irqrestore(&ptr->it_lock, flags);
@@ -535,6 +544,72 @@ static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
535} 544}
536 545
537/** 546/**
547 * alarm_timer_rearm - Posix timer callback for rearming timer
548 * @timr: Pointer to the posixtimer data struct
549 */
550static void alarm_timer_rearm(struct k_itimer *timr)
551{
552 struct alarm *alarm = &timr->it.alarm.alarmtimer;
553
554 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
555 alarm_start(alarm, alarm->node.expires);
556}
557
558/**
559 * alarm_timer_forward - Posix timer callback for forwarding timer
560 * @timr: Pointer to the posixtimer data struct
561 * @now: Current time to forward the timer against
562 */
563static int alarm_timer_forward(struct k_itimer *timr, ktime_t now)
564{
565 struct alarm *alarm = &timr->it.alarm.alarmtimer;
566
567 return (int) alarm_forward(alarm, timr->it_interval, now);
568}
569
570/**
571 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
572 * @timr: Pointer to the posixtimer data struct
573 * @now: Current time to calculate against
574 */
575static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
576{
577 struct alarm *alarm = &timr->it.alarm.alarmtimer;
578
579 return ktime_sub(now, alarm->node.expires);
580}
581
582/**
583 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
584 * @timr: Pointer to the posixtimer data struct
585 */
586static int alarm_timer_try_to_cancel(struct k_itimer *timr)
587{
588 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
589}
590
591/**
592 * alarm_timer_arm - Posix timer callback to arm a timer
593 * @timr: Pointer to the posixtimer data struct
594 * @expires: The new expiry time
595 * @absolute: Expiry value is absolute time
596 * @sigev_none: Posix timer does not deliver signals
597 */
598static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
599 bool absolute, bool sigev_none)
600{
601 struct alarm *alarm = &timr->it.alarm.alarmtimer;
602 struct alarm_base *base = &alarm_bases[alarm->type];
603
604 if (!absolute)
605 expires = ktime_add_safe(expires, base->gettime());
606 if (sigev_none)
607 alarm->node.expires = expires;
608 else
609 alarm_start(&timr->it.alarm.alarmtimer, expires);
610}
611
612/**
538 * alarm_clock_getres - posix getres interface 613 * alarm_clock_getres - posix getres interface
539 * @which_clock: clockid 614 * @which_clock: clockid
540 * @tp: timespec to fill 615 * @tp: timespec to fill
@@ -591,97 +666,6 @@ static int alarm_timer_create(struct k_itimer *new_timer)
591} 666}
592 667
593/** 668/**
594 * alarm_timer_get - posix timer_get interface
595 * @new_timer: k_itimer pointer
596 * @cur_setting: itimerspec data to fill
597 *
598 * Copies out the current itimerspec data
599 */
600static void alarm_timer_get(struct k_itimer *timr,
601 struct itimerspec64 *cur_setting)
602{
603 ktime_t relative_expiry_time =
604 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
605
606 if (ktime_to_ns(relative_expiry_time) > 0) {
607 cur_setting->it_value = ktime_to_timespec64(relative_expiry_time);
608 } else {
609 cur_setting->it_value.tv_sec = 0;
610 cur_setting->it_value.tv_nsec = 0;
611 }
612
613 cur_setting->it_interval = ktime_to_timespec64(timr->it.alarm.interval);
614}
615
616/**
617 * alarm_timer_del - posix timer_del interface
618 * @timr: k_itimer pointer to be deleted
619 *
620 * Cancels any programmed alarms for the given timer.
621 */
622static int alarm_timer_del(struct k_itimer *timr)
623{
624 if (!rtcdev)
625 return -ENOTSUPP;
626
627 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
628 return TIMER_RETRY;
629
630 return 0;
631}
632
633/**
634 * alarm_timer_set - posix timer_set interface
635 * @timr: k_itimer pointer to be deleted
636 * @flags: timer flags
637 * @new_setting: itimerspec to be used
638 * @old_setting: itimerspec being replaced
639 *
640 * Sets the timer to new_setting, and starts the timer.
641 */
642static int alarm_timer_set(struct k_itimer *timr, int flags,
643 struct itimerspec64 *new_setting,
644 struct itimerspec64 *old_setting)
645{
646 ktime_t exp;
647
648 if (!rtcdev)
649 return -ENOTSUPP;
650
651 if (flags & ~TIMER_ABSTIME)
652 return -EINVAL;
653
654 if (old_setting)
655 alarm_timer_get(timr, old_setting);
656
657 /* If the timer was already set, cancel it */
658 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
659 return TIMER_RETRY;
660
661 /* start the timer */
662 timr->it.alarm.interval = timespec64_to_ktime(new_setting->it_interval);
663
664 /*
665 * Rate limit to the tick as a hot fix to prevent DOS. Will be
666 * mopped up later.
667 */
668 if (timr->it.alarm.interval < TICK_NSEC)
669 timr->it.alarm.interval = TICK_NSEC;
670
671 exp = timespec64_to_ktime(new_setting->it_value);
672 /* Convert (if necessary) to absolute time */
673 if (flags != TIMER_ABSTIME) {
674 ktime_t now;
675
676 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
677 exp = ktime_add_safe(now, exp);
678 }
679
680 alarm_start(&timr->it.alarm.alarmtimer, exp);
681 return 0;
682}
683
684/**
685 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep 669 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
686 * @alarm: ptr to alarm that fired 670 * @alarm: ptr to alarm that fired
687 * 671 *
@@ -705,8 +689,10 @@ static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
705 * 689 *
706 * Sets the alarm timer and sleeps until it is fired or interrupted. 690 * Sets the alarm timer and sleeps until it is fired or interrupted.
707 */ 691 */
708static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp) 692static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
693 enum alarmtimer_type type)
709{ 694{
695 struct restart_block *restart;
710 alarm->data = (void *)current; 696 alarm->data = (void *)current;
711 do { 697 do {
712 set_current_state(TASK_INTERRUPTIBLE); 698 set_current_state(TASK_INTERRUPTIBLE);
@@ -719,36 +705,25 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
719 705
720 __set_current_state(TASK_RUNNING); 706 __set_current_state(TASK_RUNNING);
721 707
722 return (alarm->data == NULL); 708 if (!alarm->data)
723}
724
725
726/**
727 * update_rmtp - Update remaining timespec value
728 * @exp: expiration time
729 * @type: timer type
730 * @rmtp: user pointer to remaining timepsec value
731 *
732 * Helper function that fills in rmtp value with time between
733 * now and the exp value
734 */
735static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
736 struct timespec __user *rmtp)
737{
738 struct timespec rmt;
739 ktime_t rem;
740
741 rem = ktime_sub(exp, alarm_bases[type].gettime());
742
743 if (rem <= 0)
744 return 0; 709 return 0;
745 rmt = ktime_to_timespec(rem);
746 710
747 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) 711 if (freezing(current))
748 return -EFAULT; 712 alarmtimer_freezerset(absexp, type);
713 restart = &current->restart_block;
714 if (restart->nanosleep.type != TT_NONE) {
715 struct timespec rmt;
716 ktime_t rem;
717
718 rem = ktime_sub(absexp, alarm_bases[type].gettime());
749 719
750 return 1; 720 if (rem <= 0)
721 return 0;
722 rmt = ktime_to_timespec(rem);
751 723
724 return nanosleep_copyout(restart, &rmt);
725 }
726 return -ERESTART_RESTARTBLOCK;
752} 727}
753 728
754/** 729/**
@@ -760,32 +735,12 @@ static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
760static long __sched alarm_timer_nsleep_restart(struct restart_block *restart) 735static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
761{ 736{
762 enum alarmtimer_type type = restart->nanosleep.clockid; 737 enum alarmtimer_type type = restart->nanosleep.clockid;
763 ktime_t exp; 738 ktime_t exp = restart->nanosleep.expires;
764 struct timespec __user *rmtp;
765 struct alarm alarm; 739 struct alarm alarm;
766 int ret = 0;
767 740
768 exp = restart->nanosleep.expires;
769 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup); 741 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
770 742
771 if (alarmtimer_do_nsleep(&alarm, exp)) 743 return alarmtimer_do_nsleep(&alarm, exp, type);
772 goto out;
773
774 if (freezing(current))
775 alarmtimer_freezerset(exp, type);
776
777 rmtp = restart->nanosleep.rmtp;
778 if (rmtp) {
779 ret = update_rmtp(exp, type, rmtp);
780 if (ret <= 0)
781 goto out;
782 }
783
784
785 /* The other values in restart are already filled in */
786 ret = -ERESTART_RESTARTBLOCK;
787out:
788 return ret;
789} 744}
790 745
791/** 746/**
@@ -798,11 +753,10 @@ out:
798 * Handles clock_nanosleep calls against _ALARM clockids 753 * Handles clock_nanosleep calls against _ALARM clockids
799 */ 754 */
800static int alarm_timer_nsleep(const clockid_t which_clock, int flags, 755static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
801 struct timespec64 *tsreq, 756 const struct timespec64 *tsreq)
802 struct timespec __user *rmtp)
803{ 757{
804 enum alarmtimer_type type = clock2alarm(which_clock); 758 enum alarmtimer_type type = clock2alarm(which_clock);
805 struct restart_block *restart; 759 struct restart_block *restart = &current->restart_block;
806 struct alarm alarm; 760 struct alarm alarm;
807 ktime_t exp; 761 ktime_t exp;
808 int ret = 0; 762 int ret = 0;
@@ -825,35 +779,36 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
825 exp = ktime_add(now, exp); 779 exp = ktime_add(now, exp);
826 } 780 }
827 781
828 if (alarmtimer_do_nsleep(&alarm, exp)) 782 ret = alarmtimer_do_nsleep(&alarm, exp, type);
829 goto out; 783 if (ret != -ERESTART_RESTARTBLOCK)
830 784 return ret;
831 if (freezing(current))
832 alarmtimer_freezerset(exp, type);
833 785
834 /* abs timers don't set remaining time or restart */ 786 /* abs timers don't set remaining time or restart */
835 if (flags == TIMER_ABSTIME) { 787 if (flags == TIMER_ABSTIME)
836 ret = -ERESTARTNOHAND; 788 return -ERESTARTNOHAND;
837 goto out;
838 }
839 789
840 if (rmtp) {
841 ret = update_rmtp(exp, type, rmtp);
842 if (ret <= 0)
843 goto out;
844 }
845
846 restart = &current->restart_block;
847 restart->fn = alarm_timer_nsleep_restart; 790 restart->fn = alarm_timer_nsleep_restart;
848 restart->nanosleep.clockid = type; 791 restart->nanosleep.clockid = type;
849 restart->nanosleep.expires = exp; 792 restart->nanosleep.expires = exp;
850 restart->nanosleep.rmtp = rmtp;
851 ret = -ERESTART_RESTARTBLOCK;
852
853out:
854 return ret; 793 return ret;
855} 794}
856 795
796const struct k_clock alarm_clock = {
797 .clock_getres = alarm_clock_getres,
798 .clock_get = alarm_clock_get,
799 .timer_create = alarm_timer_create,
800 .timer_set = common_timer_set,
801 .timer_del = common_timer_del,
802 .timer_get = common_timer_get,
803 .timer_arm = alarm_timer_arm,
804 .timer_rearm = alarm_timer_rearm,
805 .timer_forward = alarm_timer_forward,
806 .timer_remaining = alarm_timer_remaining,
807 .timer_try_to_cancel = alarm_timer_try_to_cancel,
808 .nsleep = alarm_timer_nsleep,
809};
810#endif /* CONFIG_POSIX_TIMERS */
811
857 812
858/* Suspend hook structures */ 813/* Suspend hook structures */
859static const struct dev_pm_ops alarmtimer_pm_ops = { 814static const struct dev_pm_ops alarmtimer_pm_ops = {
@@ -879,23 +834,9 @@ static int __init alarmtimer_init(void)
879 struct platform_device *pdev; 834 struct platform_device *pdev;
880 int error = 0; 835 int error = 0;
881 int i; 836 int i;
882 struct k_clock alarm_clock = {
883 .clock_getres = alarm_clock_getres,
884 .clock_get = alarm_clock_get,
885 .timer_create = alarm_timer_create,
886 .timer_set = alarm_timer_set,
887 .timer_del = alarm_timer_del,
888 .timer_get = alarm_timer_get,
889 .nsleep = alarm_timer_nsleep,
890 };
891 837
892 alarmtimer_rtc_timer_init(); 838 alarmtimer_rtc_timer_init();
893 839
894 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
895 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
896 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
897 }
898
899 /* Initialize alarm bases */ 840 /* Initialize alarm bases */
900 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME; 841 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
901 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real; 842 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index ac053bb5296e..81da124f1115 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -51,6 +51,7 @@
51#include <linux/sched/debug.h> 51#include <linux/sched/debug.h>
52#include <linux/timer.h> 52#include <linux/timer.h>
53#include <linux/freezer.h> 53#include <linux/freezer.h>
54#include <linux/compat.h>
54 55
55#include <linux/uaccess.h> 56#include <linux/uaccess.h>
56 57
@@ -1439,8 +1440,29 @@ void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1439} 1440}
1440EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); 1441EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1441 1442
1443int nanosleep_copyout(struct restart_block *restart, struct timespec *ts)
1444{
1445 switch(restart->nanosleep.type) {
1446#ifdef CONFIG_COMPAT
1447 case TT_COMPAT:
1448 if (compat_put_timespec(ts, restart->nanosleep.compat_rmtp))
1449 return -EFAULT;
1450 break;
1451#endif
1452 case TT_NATIVE:
1453 if (copy_to_user(restart->nanosleep.rmtp, ts, sizeof(struct timespec)))
1454 return -EFAULT;
1455 break;
1456 default:
1457 BUG();
1458 }
1459 return -ERESTART_RESTARTBLOCK;
1460}
1461
1442static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) 1462static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1443{ 1463{
1464 struct restart_block *restart;
1465
1444 hrtimer_init_sleeper(t, current); 1466 hrtimer_init_sleeper(t, current);
1445 1467
1446 do { 1468 do {
@@ -1457,53 +1479,38 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
1457 1479
1458 __set_current_state(TASK_RUNNING); 1480 __set_current_state(TASK_RUNNING);
1459 1481
1460 return t->task == NULL; 1482 if (!t->task)
1461}
1462
1463static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1464{
1465 struct timespec rmt;
1466 ktime_t rem;
1467
1468 rem = hrtimer_expires_remaining(timer);
1469 if (rem <= 0)
1470 return 0; 1483 return 0;
1471 rmt = ktime_to_timespec(rem);
1472 1484
1473 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) 1485 restart = &current->restart_block;
1474 return -EFAULT; 1486 if (restart->nanosleep.type != TT_NONE) {
1487 ktime_t rem = hrtimer_expires_remaining(&t->timer);
1488 struct timespec rmt;
1475 1489
1476 return 1; 1490 if (rem <= 0)
1491 return 0;
1492 rmt = ktime_to_timespec(rem);
1493
1494 return nanosleep_copyout(restart, &rmt);
1495 }
1496 return -ERESTART_RESTARTBLOCK;
1477} 1497}
1478 1498
1479long __sched hrtimer_nanosleep_restart(struct restart_block *restart) 1499static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1480{ 1500{
1481 struct hrtimer_sleeper t; 1501 struct hrtimer_sleeper t;
1482 struct timespec __user *rmtp; 1502 int ret;
1483 int ret = 0;
1484 1503
1485 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid, 1504 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1486 HRTIMER_MODE_ABS); 1505 HRTIMER_MODE_ABS);
1487 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); 1506 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1488 1507
1489 if (do_nanosleep(&t, HRTIMER_MODE_ABS)) 1508 ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
1490 goto out;
1491
1492 rmtp = restart->nanosleep.rmtp;
1493 if (rmtp) {
1494 ret = update_rmtp(&t.timer, rmtp);
1495 if (ret <= 0)
1496 goto out;
1497 }
1498
1499 /* The other values in restart are already filled in */
1500 ret = -ERESTART_RESTARTBLOCK;
1501out:
1502 destroy_hrtimer_on_stack(&t.timer); 1509 destroy_hrtimer_on_stack(&t.timer);
1503 return ret; 1510 return ret;
1504} 1511}
1505 1512
1506long hrtimer_nanosleep(struct timespec64 *rqtp, struct timespec __user *rmtp, 1513long hrtimer_nanosleep(const struct timespec64 *rqtp,
1507 const enum hrtimer_mode mode, const clockid_t clockid) 1514 const enum hrtimer_mode mode, const clockid_t clockid)
1508{ 1515{
1509 struct restart_block *restart; 1516 struct restart_block *restart;
@@ -1517,7 +1524,8 @@ long hrtimer_nanosleep(struct timespec64 *rqtp, struct timespec __user *rmtp,
1517 1524
1518 hrtimer_init_on_stack(&t.timer, clockid, mode); 1525 hrtimer_init_on_stack(&t.timer, clockid, mode);
1519 hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); 1526 hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
1520 if (do_nanosleep(&t, mode)) 1527 ret = do_nanosleep(&t, mode);
1528 if (ret != -ERESTART_RESTARTBLOCK)
1521 goto out; 1529 goto out;
1522 1530
1523 /* Absolute timers do not update the rmtp value and restart: */ 1531 /* Absolute timers do not update the rmtp value and restart: */
@@ -1526,19 +1534,10 @@ long hrtimer_nanosleep(struct timespec64 *rqtp, struct timespec __user *rmtp,
1526 goto out; 1534 goto out;
1527 } 1535 }
1528 1536
1529 if (rmtp) {
1530 ret = update_rmtp(&t.timer, rmtp);
1531 if (ret <= 0)
1532 goto out;
1533 }
1534
1535 restart = &current->restart_block; 1537 restart = &current->restart_block;
1536 restart->fn = hrtimer_nanosleep_restart; 1538 restart->fn = hrtimer_nanosleep_restart;
1537 restart->nanosleep.clockid = t.timer.base->clockid; 1539 restart->nanosleep.clockid = t.timer.base->clockid;
1538 restart->nanosleep.rmtp = rmtp;
1539 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); 1540 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1540
1541 ret = -ERESTART_RESTARTBLOCK;
1542out: 1541out:
1543 destroy_hrtimer_on_stack(&t.timer); 1542 destroy_hrtimer_on_stack(&t.timer);
1544 return ret; 1543 return ret;
@@ -1557,8 +1556,31 @@ SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1557 if (!timespec64_valid(&tu64)) 1556 if (!timespec64_valid(&tu64))
1558 return -EINVAL; 1557 return -EINVAL;
1559 1558
1560 return hrtimer_nanosleep(&tu64, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC); 1559 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1560 current->restart_block.nanosleep.rmtp = rmtp;
1561 return hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1562}
1563
1564#ifdef CONFIG_COMPAT
1565
1566COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
1567 struct compat_timespec __user *, rmtp)
1568{
1569 struct timespec64 tu64;
1570 struct timespec tu;
1571
1572 if (compat_get_timespec(&tu, rqtp))
1573 return -EFAULT;
1574
1575 tu64 = timespec_to_timespec64(tu);
1576 if (!timespec64_valid(&tu64))
1577 return -EINVAL;
1578
1579 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1580 current->restart_block.nanosleep.compat_rmtp = rmtp;
1581 return hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1561} 1582}
1583#endif
1562 1584
1563/* 1585/*
1564 * Functions related to boot-time initialization: 1586 * Functions related to boot-time initialization:
diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
index 087d6a1279b8..2ef98a02376a 100644
--- a/kernel/time/itimer.c
+++ b/kernel/time/itimer.c
@@ -15,6 +15,7 @@
15#include <linux/posix-timers.h> 15#include <linux/posix-timers.h>
16#include <linux/hrtimer.h> 16#include <linux/hrtimer.h>
17#include <trace/events/timer.h> 17#include <trace/events/timer.h>
18#include <linux/compat.h>
18 19
19#include <linux/uaccess.h> 20#include <linux/uaccess.h>
20 21
@@ -116,6 +117,19 @@ SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
116 return error; 117 return error;
117} 118}
118 119
120#ifdef CONFIG_COMPAT
121COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
122 struct compat_itimerval __user *, it)
123{
124 struct itimerval kit;
125 int error = do_getitimer(which, &kit);
126
127 if (!error && put_compat_itimerval(it, &kit))
128 error = -EFAULT;
129 return error;
130}
131#endif
132
119 133
120/* 134/*
121 * The timer is automagically restarted, when interval != 0 135 * The timer is automagically restarted, when interval != 0
@@ -138,8 +152,12 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
138 u64 oval, nval, ointerval, ninterval; 152 u64 oval, nval, ointerval, ninterval;
139 struct cpu_itimer *it = &tsk->signal->it[clock_id]; 153 struct cpu_itimer *it = &tsk->signal->it[clock_id];
140 154
141 nval = timeval_to_ns(&value->it_value); 155 /*
142 ninterval = timeval_to_ns(&value->it_interval); 156 * Use the to_ktime conversion because that clamps the maximum
157 * value to KTIME_MAX and avoid multiplication overflows.
158 */
159 nval = ktime_to_ns(timeval_to_ktime(value->it_value));
160 ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
143 161
144 spin_lock_irq(&tsk->sighand->siglock); 162 spin_lock_irq(&tsk->sighand->siglock);
145 163
@@ -294,3 +312,27 @@ SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
294 return -EFAULT; 312 return -EFAULT;
295 return 0; 313 return 0;
296} 314}
315
316#ifdef CONFIG_COMPAT
317COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
318 struct compat_itimerval __user *, in,
319 struct compat_itimerval __user *, out)
320{
321 struct itimerval kin, kout;
322 int error;
323
324 if (in) {
325 if (get_compat_itimerval(&kin, in))
326 return -EFAULT;
327 } else {
328 memset(&kin, 0, sizeof(kin));
329 }
330
331 error = do_setitimer(which, &kin, out ? &kout : NULL);
332 if (error || !out)
333 return error;
334 if (put_compat_itimerval(out, &kout))
335 return -EFAULT;
336 return 0;
337}
338#endif
diff --git a/kernel/time/posix-clock.c b/kernel/time/posix-clock.c
index 31d588d37a17..17cdc554c9fe 100644
--- a/kernel/time/posix-clock.c
+++ b/kernel/time/posix-clock.c
@@ -25,6 +25,8 @@
25#include <linux/syscalls.h> 25#include <linux/syscalls.h>
26#include <linux/uaccess.h> 26#include <linux/uaccess.h>
27 27
28#include "posix-timers.h"
29
28static void delete_clock(struct kref *kref); 30static void delete_clock(struct kref *kref);
29 31
30/* 32/*
@@ -82,38 +84,6 @@ static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
82 return result; 84 return result;
83} 85}
84 86
85static int posix_clock_fasync(int fd, struct file *fp, int on)
86{
87 struct posix_clock *clk = get_posix_clock(fp);
88 int err = 0;
89
90 if (!clk)
91 return -ENODEV;
92
93 if (clk->ops.fasync)
94 err = clk->ops.fasync(clk, fd, fp, on);
95
96 put_posix_clock(clk);
97
98 return err;
99}
100
101static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
102{
103 struct posix_clock *clk = get_posix_clock(fp);
104 int err = -ENODEV;
105
106 if (!clk)
107 return -ENODEV;
108
109 if (clk->ops.mmap)
110 err = clk->ops.mmap(clk, vma);
111
112 put_posix_clock(clk);
113
114 return err;
115}
116
117static long posix_clock_ioctl(struct file *fp, 87static long posix_clock_ioctl(struct file *fp,
118 unsigned int cmd, unsigned long arg) 88 unsigned int cmd, unsigned long arg)
119{ 89{
@@ -199,8 +169,6 @@ static const struct file_operations posix_clock_file_operations = {
199 .unlocked_ioctl = posix_clock_ioctl, 169 .unlocked_ioctl = posix_clock_ioctl,
200 .open = posix_clock_open, 170 .open = posix_clock_open,
201 .release = posix_clock_release, 171 .release = posix_clock_release,
202 .fasync = posix_clock_fasync,
203 .mmap = posix_clock_mmap,
204#ifdef CONFIG_COMPAT 172#ifdef CONFIG_COMPAT
205 .compat_ioctl = posix_clock_compat_ioctl, 173 .compat_ioctl = posix_clock_compat_ioctl,
206#endif 174#endif
@@ -359,88 +327,9 @@ out:
359 return err; 327 return err;
360} 328}
361 329
362static int pc_timer_create(struct k_itimer *kit) 330const struct k_clock clock_posix_dynamic = {
363{
364 clockid_t id = kit->it_clock;
365 struct posix_clock_desc cd;
366 int err;
367
368 err = get_clock_desc(id, &cd);
369 if (err)
370 return err;
371
372 if (cd.clk->ops.timer_create)
373 err = cd.clk->ops.timer_create(cd.clk, kit);
374 else
375 err = -EOPNOTSUPP;
376
377 put_clock_desc(&cd);
378
379 return err;
380}
381
382static int pc_timer_delete(struct k_itimer *kit)
383{
384 clockid_t id = kit->it_clock;
385 struct posix_clock_desc cd;
386 int err;
387
388 err = get_clock_desc(id, &cd);
389 if (err)
390 return err;
391
392 if (cd.clk->ops.timer_delete)
393 err = cd.clk->ops.timer_delete(cd.clk, kit);
394 else
395 err = -EOPNOTSUPP;
396
397 put_clock_desc(&cd);
398
399 return err;
400}
401
402static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec64 *ts)
403{
404 clockid_t id = kit->it_clock;
405 struct posix_clock_desc cd;
406
407 if (get_clock_desc(id, &cd))
408 return;
409
410 if (cd.clk->ops.timer_gettime)
411 cd.clk->ops.timer_gettime(cd.clk, kit, ts);
412
413 put_clock_desc(&cd);
414}
415
416static int pc_timer_settime(struct k_itimer *kit, int flags,
417 struct itimerspec64 *ts, struct itimerspec64 *old)
418{
419 clockid_t id = kit->it_clock;
420 struct posix_clock_desc cd;
421 int err;
422
423 err = get_clock_desc(id, &cd);
424 if (err)
425 return err;
426
427 if (cd.clk->ops.timer_settime)
428 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
429 else
430 err = -EOPNOTSUPP;
431
432 put_clock_desc(&cd);
433
434 return err;
435}
436
437struct k_clock clock_posix_dynamic = {
438 .clock_getres = pc_clock_getres, 331 .clock_getres = pc_clock_getres,
439 .clock_set = pc_clock_settime, 332 .clock_set = pc_clock_settime,
440 .clock_get = pc_clock_gettime, 333 .clock_get = pc_clock_gettime,
441 .clock_adj = pc_clock_adjtime, 334 .clock_adj = pc_clock_adjtime,
442 .timer_create = pc_timer_create,
443 .timer_set = pc_timer_settime,
444 .timer_del = pc_timer_delete,
445 .timer_get = pc_timer_gettime,
446}; 335};
diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index d2a1e6dd0291..60cb24ac9ebc 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -12,6 +12,11 @@
12#include <trace/events/timer.h> 12#include <trace/events/timer.h>
13#include <linux/tick.h> 13#include <linux/tick.h>
14#include <linux/workqueue.h> 14#include <linux/workqueue.h>
15#include <linux/compat.h>
16
17#include "posix-timers.h"
18
19static void posix_cpu_timer_rearm(struct k_itimer *timer);
15 20
16/* 21/*
17 * Called after updating RLIMIT_CPU to run cpu timer and update 22 * Called after updating RLIMIT_CPU to run cpu timer and update
@@ -322,6 +327,8 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)
322 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX) 327 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
323 return -EINVAL; 328 return -EINVAL;
324 329
330 new_timer->kclock = &clock_posix_cpu;
331
325 INIT_LIST_HEAD(&new_timer->it.cpu.entry); 332 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
326 333
327 rcu_read_lock(); 334 rcu_read_lock();
@@ -524,7 +531,8 @@ static void cpu_timer_fire(struct k_itimer *timer)
524 * reload the timer. But we need to keep it 531 * reload the timer. But we need to keep it
525 * ticking in case the signal is deliverable next time. 532 * ticking in case the signal is deliverable next time.
526 */ 533 */
527 posix_cpu_timer_schedule(timer); 534 posix_cpu_timer_rearm(timer);
535 ++timer->it_requeue_pending;
528 } 536 }
529} 537}
530 538
@@ -572,7 +580,11 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
572 580
573 WARN_ON_ONCE(p == NULL); 581 WARN_ON_ONCE(p == NULL);
574 582
575 new_expires = timespec64_to_ns(&new->it_value); 583 /*
584 * Use the to_ktime conversion because that clamps the maximum
585 * value to KTIME_MAX and avoid multiplication overflows.
586 */
587 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
576 588
577 /* 589 /*
578 * Protect against sighand release/switch in exit/exec and p->cpu_timers 590 * Protect against sighand release/switch in exit/exec and p->cpu_timers
@@ -712,10 +724,8 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp
712 */ 724 */
713 itp->it_interval = ns_to_timespec64(timer->it.cpu.incr); 725 itp->it_interval = ns_to_timespec64(timer->it.cpu.incr);
714 726
715 if (timer->it.cpu.expires == 0) { /* Timer not armed at all. */ 727 if (!timer->it.cpu.expires)
716 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
717 return; 728 return;
718 }
719 729
720 /* 730 /*
721 * Sample the clock to take the difference with the expiry time. 731 * Sample the clock to take the difference with the expiry time.
@@ -739,7 +749,6 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp
739 * Call the timer disarmed, nothing else to do. 749 * Call the timer disarmed, nothing else to do.
740 */ 750 */
741 timer->it.cpu.expires = 0; 751 timer->it.cpu.expires = 0;
742 itp->it_value = ns_to_timespec64(timer->it.cpu.expires);
743 return; 752 return;
744 } else { 753 } else {
745 cpu_timer_sample_group(timer->it_clock, p, &now); 754 cpu_timer_sample_group(timer->it_clock, p, &now);
@@ -976,10 +985,10 @@ static void check_process_timers(struct task_struct *tsk,
976} 985}
977 986
978/* 987/*
979 * This is called from the signal code (via do_schedule_next_timer) 988 * This is called from the signal code (via posixtimer_rearm)
980 * when the last timer signal was delivered and we have to reload the timer. 989 * when the last timer signal was delivered and we have to reload the timer.
981 */ 990 */
982void posix_cpu_timer_schedule(struct k_itimer *timer) 991static void posix_cpu_timer_rearm(struct k_itimer *timer)
983{ 992{
984 struct sighand_struct *sighand; 993 struct sighand_struct *sighand;
985 unsigned long flags; 994 unsigned long flags;
@@ -995,12 +1004,12 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
995 cpu_clock_sample(timer->it_clock, p, &now); 1004 cpu_clock_sample(timer->it_clock, p, &now);
996 bump_cpu_timer(timer, now); 1005 bump_cpu_timer(timer, now);
997 if (unlikely(p->exit_state)) 1006 if (unlikely(p->exit_state))
998 goto out; 1007 return;
999 1008
1000 /* Protect timer list r/w in arm_timer() */ 1009 /* Protect timer list r/w in arm_timer() */
1001 sighand = lock_task_sighand(p, &flags); 1010 sighand = lock_task_sighand(p, &flags);
1002 if (!sighand) 1011 if (!sighand)
1003 goto out; 1012 return;
1004 } else { 1013 } else {
1005 /* 1014 /*
1006 * Protect arm_timer() and timer sampling in case of call to 1015 * Protect arm_timer() and timer sampling in case of call to
@@ -1013,11 +1022,10 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
1013 * We can't even collect a sample any more. 1022 * We can't even collect a sample any more.
1014 */ 1023 */
1015 timer->it.cpu.expires = 0; 1024 timer->it.cpu.expires = 0;
1016 goto out; 1025 return;
1017 } else if (unlikely(p->exit_state) && thread_group_empty(p)) { 1026 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1018 unlock_task_sighand(p, &flags); 1027 /* If the process is dying, no need to rearm */
1019 /* Optimizations: if the process is dying, no need to rearm */ 1028 goto unlock;
1020 goto out;
1021 } 1029 }
1022 cpu_timer_sample_group(timer->it_clock, p, &now); 1030 cpu_timer_sample_group(timer->it_clock, p, &now);
1023 bump_cpu_timer(timer, now); 1031 bump_cpu_timer(timer, now);
@@ -1029,12 +1037,8 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
1029 */ 1037 */
1030 WARN_ON_ONCE(!irqs_disabled()); 1038 WARN_ON_ONCE(!irqs_disabled());
1031 arm_timer(timer); 1039 arm_timer(timer);
1040unlock:
1032 unlock_task_sighand(p, &flags); 1041 unlock_task_sighand(p, &flags);
1033
1034out:
1035 timer->it_overrun_last = timer->it_overrun;
1036 timer->it_overrun = -1;
1037 ++timer->it_requeue_pending;
1038} 1042}
1039 1043
1040/** 1044/**
@@ -1227,9 +1231,11 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1227} 1231}
1228 1232
1229static int do_cpu_nanosleep(const clockid_t which_clock, int flags, 1233static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1230 struct timespec64 *rqtp, struct itimerspec64 *it) 1234 const struct timespec64 *rqtp)
1231{ 1235{
1236 struct itimerspec64 it;
1232 struct k_itimer timer; 1237 struct k_itimer timer;
1238 u64 expires;
1233 int error; 1239 int error;
1234 1240
1235 /* 1241 /*
@@ -1243,12 +1249,13 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1243 timer.it_process = current; 1249 timer.it_process = current;
1244 if (!error) { 1250 if (!error) {
1245 static struct itimerspec64 zero_it; 1251 static struct itimerspec64 zero_it;
1252 struct restart_block *restart;
1246 1253
1247 memset(it, 0, sizeof *it); 1254 memset(&it, 0, sizeof(it));
1248 it->it_value = *rqtp; 1255 it.it_value = *rqtp;
1249 1256
1250 spin_lock_irq(&timer.it_lock); 1257 spin_lock_irq(&timer.it_lock);
1251 error = posix_cpu_timer_set(&timer, flags, it, NULL); 1258 error = posix_cpu_timer_set(&timer, flags, &it, NULL);
1252 if (error) { 1259 if (error) {
1253 spin_unlock_irq(&timer.it_lock); 1260 spin_unlock_irq(&timer.it_lock);
1254 return error; 1261 return error;
@@ -1277,8 +1284,8 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1277 /* 1284 /*
1278 * We were interrupted by a signal. 1285 * We were interrupted by a signal.
1279 */ 1286 */
1280 *rqtp = ns_to_timespec64(timer.it.cpu.expires); 1287 expires = timer.it.cpu.expires;
1281 error = posix_cpu_timer_set(&timer, 0, &zero_it, it); 1288 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
1282 if (!error) { 1289 if (!error) {
1283 /* 1290 /*
1284 * Timer is now unarmed, deletion can not fail. 1291 * Timer is now unarmed, deletion can not fail.
@@ -1298,7 +1305,7 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1298 spin_unlock_irq(&timer.it_lock); 1305 spin_unlock_irq(&timer.it_lock);
1299 } 1306 }
1300 1307
1301 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) { 1308 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
1302 /* 1309 /*
1303 * It actually did fire already. 1310 * It actually did fire already.
1304 */ 1311 */
@@ -1306,6 +1313,17 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1306 } 1313 }
1307 1314
1308 error = -ERESTART_RESTARTBLOCK; 1315 error = -ERESTART_RESTARTBLOCK;
1316 /*
1317 * Report back to the user the time still remaining.
1318 */
1319 restart = &current->restart_block;
1320 restart->nanosleep.expires = expires;
1321 if (restart->nanosleep.type != TT_NONE) {
1322 struct timespec ts;
1323
1324 ts = timespec64_to_timespec(it.it_value);
1325 error = nanosleep_copyout(restart, &ts);
1326 }
1309 } 1327 }
1310 1328
1311 return error; 1329 return error;
@@ -1314,11 +1332,9 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1314static long posix_cpu_nsleep_restart(struct restart_block *restart_block); 1332static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1315 1333
1316static int posix_cpu_nsleep(const clockid_t which_clock, int flags, 1334static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1317 struct timespec64 *rqtp, struct timespec __user *rmtp) 1335 const struct timespec64 *rqtp)
1318{ 1336{
1319 struct restart_block *restart_block = &current->restart_block; 1337 struct restart_block *restart_block = &current->restart_block;
1320 struct itimerspec64 it;
1321 struct timespec ts;
1322 int error; 1338 int error;
1323 1339
1324 /* 1340 /*
@@ -1329,23 +1345,15 @@ static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1329 CPUCLOCK_PID(which_clock) == task_pid_vnr(current))) 1345 CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
1330 return -EINVAL; 1346 return -EINVAL;
1331 1347
1332 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it); 1348 error = do_cpu_nanosleep(which_clock, flags, rqtp);
1333 1349
1334 if (error == -ERESTART_RESTARTBLOCK) { 1350 if (error == -ERESTART_RESTARTBLOCK) {
1335 1351
1336 if (flags & TIMER_ABSTIME) 1352 if (flags & TIMER_ABSTIME)
1337 return -ERESTARTNOHAND; 1353 return -ERESTARTNOHAND;
1338 /*
1339 * Report back to the user the time still remaining.
1340 */
1341 ts = timespec64_to_timespec(it.it_value);
1342 if (rmtp && copy_to_user(rmtp, &ts, sizeof(*rmtp)))
1343 return -EFAULT;
1344 1354
1345 restart_block->fn = posix_cpu_nsleep_restart; 1355 restart_block->fn = posix_cpu_nsleep_restart;
1346 restart_block->nanosleep.clockid = which_clock; 1356 restart_block->nanosleep.clockid = which_clock;
1347 restart_block->nanosleep.rmtp = rmtp;
1348 restart_block->nanosleep.expires = timespec64_to_ns(rqtp);
1349 } 1357 }
1350 return error; 1358 return error;
1351} 1359}
@@ -1353,28 +1361,11 @@ static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1353static long posix_cpu_nsleep_restart(struct restart_block *restart_block) 1361static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1354{ 1362{
1355 clockid_t which_clock = restart_block->nanosleep.clockid; 1363 clockid_t which_clock = restart_block->nanosleep.clockid;
1356 struct itimerspec64 it;
1357 struct timespec64 t; 1364 struct timespec64 t;
1358 struct timespec tmp;
1359 int error;
1360 1365
1361 t = ns_to_timespec64(restart_block->nanosleep.expires); 1366 t = ns_to_timespec64(restart_block->nanosleep.expires);
1362 1367
1363 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it); 1368 return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
1364
1365 if (error == -ERESTART_RESTARTBLOCK) {
1366 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
1367 /*
1368 * Report back to the user the time still remaining.
1369 */
1370 tmp = timespec64_to_timespec(it.it_value);
1371 if (rmtp && copy_to_user(rmtp, &tmp, sizeof(*rmtp)))
1372 return -EFAULT;
1373
1374 restart_block->nanosleep.expires = timespec64_to_ns(&t);
1375 }
1376 return error;
1377
1378} 1369}
1379 1370
1380#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED) 1371#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
@@ -1396,14 +1387,9 @@ static int process_cpu_timer_create(struct k_itimer *timer)
1396 return posix_cpu_timer_create(timer); 1387 return posix_cpu_timer_create(timer);
1397} 1388}
1398static int process_cpu_nsleep(const clockid_t which_clock, int flags, 1389static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1399 struct timespec64 *rqtp, 1390 const struct timespec64 *rqtp)
1400 struct timespec __user *rmtp)
1401{
1402 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1403}
1404static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1405{ 1391{
1406 return -EINVAL; 1392 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
1407} 1393}
1408static int thread_cpu_clock_getres(const clockid_t which_clock, 1394static int thread_cpu_clock_getres(const clockid_t which_clock,
1409 struct timespec64 *tp) 1395 struct timespec64 *tp)
@@ -1421,36 +1407,27 @@ static int thread_cpu_timer_create(struct k_itimer *timer)
1421 return posix_cpu_timer_create(timer); 1407 return posix_cpu_timer_create(timer);
1422} 1408}
1423 1409
1424struct k_clock clock_posix_cpu = { 1410const struct k_clock clock_posix_cpu = {
1425 .clock_getres = posix_cpu_clock_getres, 1411 .clock_getres = posix_cpu_clock_getres,
1426 .clock_set = posix_cpu_clock_set, 1412 .clock_set = posix_cpu_clock_set,
1427 .clock_get = posix_cpu_clock_get, 1413 .clock_get = posix_cpu_clock_get,
1428 .timer_create = posix_cpu_timer_create, 1414 .timer_create = posix_cpu_timer_create,
1429 .nsleep = posix_cpu_nsleep, 1415 .nsleep = posix_cpu_nsleep,
1430 .nsleep_restart = posix_cpu_nsleep_restart,
1431 .timer_set = posix_cpu_timer_set, 1416 .timer_set = posix_cpu_timer_set,
1432 .timer_del = posix_cpu_timer_del, 1417 .timer_del = posix_cpu_timer_del,
1433 .timer_get = posix_cpu_timer_get, 1418 .timer_get = posix_cpu_timer_get,
1419 .timer_rearm = posix_cpu_timer_rearm,
1434}; 1420};
1435 1421
1436static __init int init_posix_cpu_timers(void) 1422const struct k_clock clock_process = {
1437{ 1423 .clock_getres = process_cpu_clock_getres,
1438 struct k_clock process = { 1424 .clock_get = process_cpu_clock_get,
1439 .clock_getres = process_cpu_clock_getres, 1425 .timer_create = process_cpu_timer_create,
1440 .clock_get = process_cpu_clock_get, 1426 .nsleep = process_cpu_nsleep,
1441 .timer_create = process_cpu_timer_create, 1427};
1442 .nsleep = process_cpu_nsleep,
1443 .nsleep_restart = process_cpu_nsleep_restart,
1444 };
1445 struct k_clock thread = {
1446 .clock_getres = thread_cpu_clock_getres,
1447 .clock_get = thread_cpu_clock_get,
1448 .timer_create = thread_cpu_timer_create,
1449 };
1450
1451 posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1452 posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1453 1428
1454 return 0; 1429const struct k_clock clock_thread = {
1455} 1430 .clock_getres = thread_cpu_clock_getres,
1456__initcall(init_posix_cpu_timers); 1431 .clock_get = thread_cpu_clock_get,
1432 .timer_create = thread_cpu_timer_create,
1433};
diff --git a/kernel/time/posix-stubs.c b/kernel/time/posix-stubs.c
index c0cd53eb018a..38f3b20efa29 100644
--- a/kernel/time/posix-stubs.c
+++ b/kernel/time/posix-stubs.c
@@ -17,6 +17,7 @@
17#include <linux/ktime.h> 17#include <linux/ktime.h>
18#include <linux/timekeeping.h> 18#include <linux/timekeeping.h>
19#include <linux/posix-timers.h> 19#include <linux/posix-timers.h>
20#include <linux/compat.h>
20 21
21asmlinkage long sys_ni_posix_timers(void) 22asmlinkage long sys_ni_posix_timers(void)
22{ 23{
@@ -27,6 +28,7 @@ asmlinkage long sys_ni_posix_timers(void)
27} 28}
28 29
29#define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers) 30#define SYS_NI(name) SYSCALL_ALIAS(sys_##name, sys_ni_posix_timers)
31#define COMPAT_SYS_NI(name) SYSCALL_ALIAS(compat_sys_##name, sys_ni_posix_timers)
30 32
31SYS_NI(timer_create); 33SYS_NI(timer_create);
32SYS_NI(timer_gettime); 34SYS_NI(timer_gettime);
@@ -39,6 +41,12 @@ SYS_NI(setitimer);
39#ifdef __ARCH_WANT_SYS_ALARM 41#ifdef __ARCH_WANT_SYS_ALARM
40SYS_NI(alarm); 42SYS_NI(alarm);
41#endif 43#endif
44COMPAT_SYS_NI(timer_create);
45COMPAT_SYS_NI(clock_adjtime);
46COMPAT_SYS_NI(timer_settime);
47COMPAT_SYS_NI(timer_gettime);
48COMPAT_SYS_NI(getitimer);
49COMPAT_SYS_NI(setitimer);
42 50
43/* 51/*
44 * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC 52 * We preserve minimal support for CLOCK_REALTIME and CLOCK_MONOTONIC
@@ -110,22 +118,106 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
110 case CLOCK_REALTIME: 118 case CLOCK_REALTIME:
111 case CLOCK_MONOTONIC: 119 case CLOCK_MONOTONIC:
112 case CLOCK_BOOTTIME: 120 case CLOCK_BOOTTIME:
113 if (copy_from_user(&t, rqtp, sizeof (struct timespec))) 121 break;
114 return -EFAULT;
115 t64 = timespec_to_timespec64(t);
116 if (!timespec64_valid(&t64))
117 return -EINVAL;
118 return hrtimer_nanosleep(&t64, rmtp, flags & TIMER_ABSTIME ?
119 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
120 which_clock);
121 default: 122 default:
122 return -EINVAL; 123 return -EINVAL;
123 } 124 }
125
126 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
127 return -EFAULT;
128 t64 = timespec_to_timespec64(t);
129 if (!timespec64_valid(&t64))
130 return -EINVAL;
131 if (flags & TIMER_ABSTIME)
132 rmtp = NULL;
133 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
134 current->restart_block.nanosleep.rmtp = rmtp;
135 return hrtimer_nanosleep(&t64, flags & TIMER_ABSTIME ?
136 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
137 which_clock);
124} 138}
125 139
126#ifdef CONFIG_COMPAT 140#ifdef CONFIG_COMPAT
127long clock_nanosleep_restart(struct restart_block *restart_block) 141COMPAT_SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
142 struct compat_timespec __user *, tp)
143{
144 struct timespec64 new_tp64;
145 struct timespec new_tp;
146
147 if (which_clock != CLOCK_REALTIME)
148 return -EINVAL;
149 if (compat_get_timespec(&new_tp, tp))
150 return -EFAULT;
151
152 new_tp64 = timespec_to_timespec64(new_tp);
153 return do_sys_settimeofday64(&new_tp64, NULL);
154}
155
156COMPAT_SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
157 struct compat_timespec __user *,tp)
128{ 158{
129 return hrtimer_nanosleep_restart(restart_block); 159 struct timespec64 kernel_tp64;
160 struct timespec kernel_tp;
161
162 switch (which_clock) {
163 case CLOCK_REALTIME: ktime_get_real_ts64(&kernel_tp64); break;
164 case CLOCK_MONOTONIC: ktime_get_ts64(&kernel_tp64); break;
165 case CLOCK_BOOTTIME: get_monotonic_boottime64(&kernel_tp64); break;
166 default: return -EINVAL;
167 }
168
169 kernel_tp = timespec64_to_timespec(kernel_tp64);
170 if (compat_put_timespec(&kernel_tp, tp))
171 return -EFAULT;
172 return 0;
173}
174
175COMPAT_SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
176 struct compat_timespec __user *, tp)
177{
178 struct timespec rtn_tp = {
179 .tv_sec = 0,
180 .tv_nsec = hrtimer_resolution,
181 };
182
183 switch (which_clock) {
184 case CLOCK_REALTIME:
185 case CLOCK_MONOTONIC:
186 case CLOCK_BOOTTIME:
187 if (compat_put_timespec(&rtn_tp, tp))
188 return -EFAULT;
189 return 0;
190 default:
191 return -EINVAL;
192 }
193}
194COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
195 struct compat_timespec __user *, rqtp,
196 struct compat_timespec __user *, rmtp)
197{
198 struct timespec64 t64;
199 struct timespec t;
200
201 switch (which_clock) {
202 case CLOCK_REALTIME:
203 case CLOCK_MONOTONIC:
204 case CLOCK_BOOTTIME:
205 break;
206 default:
207 return -EINVAL;
208 }
209
210 if (compat_get_timespec(&t, rqtp))
211 return -EFAULT;
212 t64 = timespec_to_timespec64(t);
213 if (!timespec64_valid(&t64))
214 return -EINVAL;
215 if (flags & TIMER_ABSTIME)
216 rmtp = NULL;
217 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
218 current->restart_block.nanosleep.compat_rmtp = rmtp;
219 return hrtimer_nanosleep(&t64, flags & TIMER_ABSTIME ?
220 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
221 which_clock);
130} 222}
131#endif 223#endif
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 4d7b2ce09c27..82d67be7d9d1 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -49,8 +49,10 @@
49#include <linux/workqueue.h> 49#include <linux/workqueue.h>
50#include <linux/export.h> 50#include <linux/export.h>
51#include <linux/hashtable.h> 51#include <linux/hashtable.h>
52#include <linux/compat.h>
52 53
53#include "timekeeping.h" 54#include "timekeeping.h"
55#include "posix-timers.h"
54 56
55/* 57/*
56 * Management arrays for POSIX timers. Timers are now kept in static hash table 58 * Management arrays for POSIX timers. Timers are now kept in static hash table
@@ -69,6 +71,10 @@ static struct kmem_cache *posix_timers_cache;
69static DEFINE_HASHTABLE(posix_timers_hashtable, 9); 71static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
70static DEFINE_SPINLOCK(hash_lock); 72static DEFINE_SPINLOCK(hash_lock);
71 73
74static const struct k_clock * const posix_clocks[];
75static const struct k_clock *clockid_to_kclock(const clockid_t id);
76static const struct k_clock clock_realtime, clock_monotonic;
77
72/* 78/*
73 * we assume that the new SIGEV_THREAD_ID shares no bits with the other 79 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
74 * SIGEV values. Here we put out an error if this assumption fails. 80 * SIGEV values. Here we put out an error if this assumption fails.
@@ -124,22 +130,6 @@ static DEFINE_SPINLOCK(hash_lock);
124 * have is CLOCK_REALTIME and its high res counter part, both of 130 * have is CLOCK_REALTIME and its high res counter part, both of
125 * which we beg off on and pass to do_sys_settimeofday(). 131 * which we beg off on and pass to do_sys_settimeofday().
126 */ 132 */
127
128static struct k_clock posix_clocks[MAX_CLOCKS];
129
130/*
131 * These ones are defined below.
132 */
133static int common_nsleep(const clockid_t, int flags, struct timespec64 *t,
134 struct timespec __user *rmtp);
135static int common_timer_create(struct k_itimer *new_timer);
136static void common_timer_get(struct k_itimer *, struct itimerspec64 *);
137static int common_timer_set(struct k_itimer *, int,
138 struct itimerspec64 *, struct itimerspec64 *);
139static int common_timer_del(struct k_itimer *timer);
140
141static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
142
143static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); 133static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
144 134
145#define lock_timer(tid, flags) \ 135#define lock_timer(tid, flags) \
@@ -285,91 +275,23 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
285 */ 275 */
286static __init int init_posix_timers(void) 276static __init int init_posix_timers(void)
287{ 277{
288 struct k_clock clock_realtime = {
289 .clock_getres = posix_get_hrtimer_res,
290 .clock_get = posix_clock_realtime_get,
291 .clock_set = posix_clock_realtime_set,
292 .clock_adj = posix_clock_realtime_adj,
293 .nsleep = common_nsleep,
294 .nsleep_restart = hrtimer_nanosleep_restart,
295 .timer_create = common_timer_create,
296 .timer_set = common_timer_set,
297 .timer_get = common_timer_get,
298 .timer_del = common_timer_del,
299 };
300 struct k_clock clock_monotonic = {
301 .clock_getres = posix_get_hrtimer_res,
302 .clock_get = posix_ktime_get_ts,
303 .nsleep = common_nsleep,
304 .nsleep_restart = hrtimer_nanosleep_restart,
305 .timer_create = common_timer_create,
306 .timer_set = common_timer_set,
307 .timer_get = common_timer_get,
308 .timer_del = common_timer_del,
309 };
310 struct k_clock clock_monotonic_raw = {
311 .clock_getres = posix_get_hrtimer_res,
312 .clock_get = posix_get_monotonic_raw,
313 };
314 struct k_clock clock_realtime_coarse = {
315 .clock_getres = posix_get_coarse_res,
316 .clock_get = posix_get_realtime_coarse,
317 };
318 struct k_clock clock_monotonic_coarse = {
319 .clock_getres = posix_get_coarse_res,
320 .clock_get = posix_get_monotonic_coarse,
321 };
322 struct k_clock clock_tai = {
323 .clock_getres = posix_get_hrtimer_res,
324 .clock_get = posix_get_tai,
325 .nsleep = common_nsleep,
326 .nsleep_restart = hrtimer_nanosleep_restart,
327 .timer_create = common_timer_create,
328 .timer_set = common_timer_set,
329 .timer_get = common_timer_get,
330 .timer_del = common_timer_del,
331 };
332 struct k_clock clock_boottime = {
333 .clock_getres = posix_get_hrtimer_res,
334 .clock_get = posix_get_boottime,
335 .nsleep = common_nsleep,
336 .nsleep_restart = hrtimer_nanosleep_restart,
337 .timer_create = common_timer_create,
338 .timer_set = common_timer_set,
339 .timer_get = common_timer_get,
340 .timer_del = common_timer_del,
341 };
342
343 posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime);
344 posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic);
345 posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
346 posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
347 posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
348 posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
349 posix_timers_register_clock(CLOCK_TAI, &clock_tai);
350
351 posix_timers_cache = kmem_cache_create("posix_timers_cache", 278 posix_timers_cache = kmem_cache_create("posix_timers_cache",
352 sizeof (struct k_itimer), 0, SLAB_PANIC, 279 sizeof (struct k_itimer), 0, SLAB_PANIC,
353 NULL); 280 NULL);
354 return 0; 281 return 0;
355} 282}
356
357__initcall(init_posix_timers); 283__initcall(init_posix_timers);
358 284
359static void schedule_next_timer(struct k_itimer *timr) 285static void common_hrtimer_rearm(struct k_itimer *timr)
360{ 286{
361 struct hrtimer *timer = &timr->it.real.timer; 287 struct hrtimer *timer = &timr->it.real.timer;
362 288
363 if (timr->it.real.interval == 0) 289 if (!timr->it_interval)
364 return; 290 return;
365 291
366 timr->it_overrun += (unsigned int) hrtimer_forward(timer, 292 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
367 timer->base->get_time(), 293 timer->base->get_time(),
368 timr->it.real.interval); 294 timr->it_interval);
369
370 timr->it_overrun_last = timr->it_overrun;
371 timr->it_overrun = -1;
372 ++timr->it_requeue_pending;
373 hrtimer_restart(timer); 295 hrtimer_restart(timer);
374} 296}
375 297
@@ -384,24 +306,27 @@ static void schedule_next_timer(struct k_itimer *timr)
384 * To protect against the timer going away while the interrupt is queued, 306 * To protect against the timer going away while the interrupt is queued,
385 * we require that the it_requeue_pending flag be set. 307 * we require that the it_requeue_pending flag be set.
386 */ 308 */
387void do_schedule_next_timer(struct siginfo *info) 309void posixtimer_rearm(struct siginfo *info)
388{ 310{
389 struct k_itimer *timr; 311 struct k_itimer *timr;
390 unsigned long flags; 312 unsigned long flags;
391 313
392 timr = lock_timer(info->si_tid, &flags); 314 timr = lock_timer(info->si_tid, &flags);
315 if (!timr)
316 return;
393 317
394 if (timr && timr->it_requeue_pending == info->si_sys_private) { 318 if (timr->it_requeue_pending == info->si_sys_private) {
395 if (timr->it_clock < 0) 319 timr->kclock->timer_rearm(timr);
396 posix_cpu_timer_schedule(timr); 320
397 else 321 timr->it_active = 1;
398 schedule_next_timer(timr); 322 timr->it_overrun_last = timr->it_overrun;
323 timr->it_overrun = -1;
324 ++timr->it_requeue_pending;
399 325
400 info->si_overrun += timr->it_overrun_last; 326 info->si_overrun += timr->it_overrun_last;
401 } 327 }
402 328
403 if (timr) 329 unlock_timer(timr, flags);
404 unlock_timer(timr, flags);
405} 330}
406 331
407int posix_timer_event(struct k_itimer *timr, int si_private) 332int posix_timer_event(struct k_itimer *timr, int si_private)
@@ -410,12 +335,12 @@ int posix_timer_event(struct k_itimer *timr, int si_private)
410 int shared, ret = -1; 335 int shared, ret = -1;
411 /* 336 /*
412 * FIXME: if ->sigq is queued we can race with 337 * FIXME: if ->sigq is queued we can race with
413 * dequeue_signal()->do_schedule_next_timer(). 338 * dequeue_signal()->posixtimer_rearm().
414 * 339 *
415 * If dequeue_signal() sees the "right" value of 340 * If dequeue_signal() sees the "right" value of
416 * si_sys_private it calls do_schedule_next_timer(). 341 * si_sys_private it calls posixtimer_rearm().
417 * We re-queue ->sigq and drop ->it_lock(). 342 * We re-queue ->sigq and drop ->it_lock().
418 * do_schedule_next_timer() locks the timer 343 * posixtimer_rearm() locks the timer
419 * and re-schedules it while ->sigq is pending. 344 * and re-schedules it while ->sigq is pending.
420 * Not really bad, but not that we want. 345 * Not really bad, but not that we want.
421 */ 346 */
@@ -431,7 +356,6 @@ int posix_timer_event(struct k_itimer *timr, int si_private)
431 /* If we failed to send the signal the timer stops. */ 356 /* If we failed to send the signal the timer stops. */
432 return ret > 0; 357 return ret > 0;
433} 358}
434EXPORT_SYMBOL_GPL(posix_timer_event);
435 359
436/* 360/*
437 * This function gets called when a POSIX.1b interval timer expires. It 361 * This function gets called when a POSIX.1b interval timer expires. It
@@ -450,7 +374,8 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
450 timr = container_of(timer, struct k_itimer, it.real.timer); 374 timr = container_of(timer, struct k_itimer, it.real.timer);
451 spin_lock_irqsave(&timr->it_lock, flags); 375 spin_lock_irqsave(&timr->it_lock, flags);
452 376
453 if (timr->it.real.interval != 0) 377 timr->it_active = 0;
378 if (timr->it_interval != 0)
454 si_private = ++timr->it_requeue_pending; 379 si_private = ++timr->it_requeue_pending;
455 380
456 if (posix_timer_event(timr, si_private)) { 381 if (posix_timer_event(timr, si_private)) {
@@ -459,7 +384,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
459 * we will not get a call back to restart it AND 384 * we will not get a call back to restart it AND
460 * it should be restarted. 385 * it should be restarted.
461 */ 386 */
462 if (timr->it.real.interval != 0) { 387 if (timr->it_interval != 0) {
463 ktime_t now = hrtimer_cb_get_time(timer); 388 ktime_t now = hrtimer_cb_get_time(timer);
464 389
465 /* 390 /*
@@ -488,15 +413,16 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
488 { 413 {
489 ktime_t kj = NSEC_PER_SEC / HZ; 414 ktime_t kj = NSEC_PER_SEC / HZ;
490 415
491 if (timr->it.real.interval < kj) 416 if (timr->it_interval < kj)
492 now = ktime_add(now, kj); 417 now = ktime_add(now, kj);
493 } 418 }
494#endif 419#endif
495 timr->it_overrun += (unsigned int) 420 timr->it_overrun += (unsigned int)
496 hrtimer_forward(timer, now, 421 hrtimer_forward(timer, now,
497 timr->it.real.interval); 422 timr->it_interval);
498 ret = HRTIMER_RESTART; 423 ret = HRTIMER_RESTART;
499 ++timr->it_requeue_pending; 424 ++timr->it_requeue_pending;
425 timr->it_active = 1;
500 } 426 }
501 } 427 }
502 428
@@ -521,30 +447,6 @@ static struct pid *good_sigevent(sigevent_t * event)
521 return task_pid(rtn); 447 return task_pid(rtn);
522} 448}
523 449
524void posix_timers_register_clock(const clockid_t clock_id,
525 struct k_clock *new_clock)
526{
527 if ((unsigned) clock_id >= MAX_CLOCKS) {
528 printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
529 clock_id);
530 return;
531 }
532
533 if (!new_clock->clock_get) {
534 printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
535 clock_id);
536 return;
537 }
538 if (!new_clock->clock_getres) {
539 printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
540 clock_id);
541 return;
542 }
543
544 posix_clocks[clock_id] = *new_clock;
545}
546EXPORT_SYMBOL_GPL(posix_timers_register_clock);
547
548static struct k_itimer * alloc_posix_timer(void) 450static struct k_itimer * alloc_posix_timer(void)
549{ 451{
550 struct k_itimer *tmr; 452 struct k_itimer *tmr;
@@ -581,17 +483,6 @@ static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
581 call_rcu(&tmr->it.rcu, k_itimer_rcu_free); 483 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
582} 484}
583 485
584static struct k_clock *clockid_to_kclock(const clockid_t id)
585{
586 if (id < 0)
587 return (id & CLOCKFD_MASK) == CLOCKFD ?
588 &clock_posix_dynamic : &clock_posix_cpu;
589
590 if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
591 return NULL;
592 return &posix_clocks[id];
593}
594
595static int common_timer_create(struct k_itimer *new_timer) 486static int common_timer_create(struct k_itimer *new_timer)
596{ 487{
597 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); 488 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
@@ -599,15 +490,12 @@ static int common_timer_create(struct k_itimer *new_timer)
599} 490}
600 491
601/* Create a POSIX.1b interval timer. */ 492/* Create a POSIX.1b interval timer. */
602 493static int do_timer_create(clockid_t which_clock, struct sigevent *event,
603SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, 494 timer_t __user *created_timer_id)
604 struct sigevent __user *, timer_event_spec,
605 timer_t __user *, created_timer_id)
606{ 495{
607 struct k_clock *kc = clockid_to_kclock(which_clock); 496 const struct k_clock *kc = clockid_to_kclock(which_clock);
608 struct k_itimer *new_timer; 497 struct k_itimer *new_timer;
609 int error, new_timer_id; 498 int error, new_timer_id;
610 sigevent_t event;
611 int it_id_set = IT_ID_NOT_SET; 499 int it_id_set = IT_ID_NOT_SET;
612 500
613 if (!kc) 501 if (!kc)
@@ -629,31 +517,28 @@ SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
629 it_id_set = IT_ID_SET; 517 it_id_set = IT_ID_SET;
630 new_timer->it_id = (timer_t) new_timer_id; 518 new_timer->it_id = (timer_t) new_timer_id;
631 new_timer->it_clock = which_clock; 519 new_timer->it_clock = which_clock;
520 new_timer->kclock = kc;
632 new_timer->it_overrun = -1; 521 new_timer->it_overrun = -1;
633 522
634 if (timer_event_spec) { 523 if (event) {
635 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
636 error = -EFAULT;
637 goto out;
638 }
639 rcu_read_lock(); 524 rcu_read_lock();
640 new_timer->it_pid = get_pid(good_sigevent(&event)); 525 new_timer->it_pid = get_pid(good_sigevent(event));
641 rcu_read_unlock(); 526 rcu_read_unlock();
642 if (!new_timer->it_pid) { 527 if (!new_timer->it_pid) {
643 error = -EINVAL; 528 error = -EINVAL;
644 goto out; 529 goto out;
645 } 530 }
531 new_timer->it_sigev_notify = event->sigev_notify;
532 new_timer->sigq->info.si_signo = event->sigev_signo;
533 new_timer->sigq->info.si_value = event->sigev_value;
646 } else { 534 } else {
647 memset(&event.sigev_value, 0, sizeof(event.sigev_value)); 535 new_timer->it_sigev_notify = SIGEV_SIGNAL;
648 event.sigev_notify = SIGEV_SIGNAL; 536 new_timer->sigq->info.si_signo = SIGALRM;
649 event.sigev_signo = SIGALRM; 537 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
650 event.sigev_value.sival_int = new_timer->it_id; 538 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
651 new_timer->it_pid = get_pid(task_tgid(current)); 539 new_timer->it_pid = get_pid(task_tgid(current));
652 } 540 }
653 541
654 new_timer->it_sigev_notify = event.sigev_notify;
655 new_timer->sigq->info.si_signo = event.sigev_signo;
656 new_timer->sigq->info.si_value = event.sigev_value;
657 new_timer->sigq->info.si_tid = new_timer->it_id; 542 new_timer->sigq->info.si_tid = new_timer->it_id;
658 new_timer->sigq->info.si_code = SI_TIMER; 543 new_timer->sigq->info.si_code = SI_TIMER;
659 544
@@ -684,6 +569,36 @@ out:
684 return error; 569 return error;
685} 570}
686 571
572SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
573 struct sigevent __user *, timer_event_spec,
574 timer_t __user *, created_timer_id)
575{
576 if (timer_event_spec) {
577 sigevent_t event;
578
579 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
580 return -EFAULT;
581 return do_timer_create(which_clock, &event, created_timer_id);
582 }
583 return do_timer_create(which_clock, NULL, created_timer_id);
584}
585
586#ifdef CONFIG_COMPAT
587COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
588 struct compat_sigevent __user *, timer_event_spec,
589 timer_t __user *, created_timer_id)
590{
591 if (timer_event_spec) {
592 sigevent_t event;
593
594 if (get_compat_sigevent(&event, timer_event_spec))
595 return -EFAULT;
596 return do_timer_create(which_clock, &event, created_timer_id);
597 }
598 return do_timer_create(which_clock, NULL, created_timer_id);
599}
600#endif
601
687/* 602/*
688 * Locking issues: We need to protect the result of the id look up until 603 * Locking issues: We need to protect the result of the id look up until
689 * we get the timer locked down so it is not deleted under us. The 604 * we get the timer locked down so it is not deleted under us. The
@@ -717,6 +632,20 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
717 return NULL; 632 return NULL;
718} 633}
719 634
635static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
636{
637 struct hrtimer *timer = &timr->it.real.timer;
638
639 return __hrtimer_expires_remaining_adjusted(timer, now);
640}
641
642static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
643{
644 struct hrtimer *timer = &timr->it.real.timer;
645
646 return (int)hrtimer_forward(timer, now, timr->it_interval);
647}
648
720/* 649/*
721 * Get the time remaining on a POSIX.1b interval timer. This function 650 * Get the time remaining on a POSIX.1b interval timer. This function
722 * is ALWAYS called with spin_lock_irq on the timer, thus it must not 651 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
@@ -733,55 +662,61 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
733 * it is the same as a requeue pending timer WRT to what we should 662 * it is the same as a requeue pending timer WRT to what we should
734 * report. 663 * report.
735 */ 664 */
736static void 665void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
737common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
738{ 666{
667 const struct k_clock *kc = timr->kclock;
739 ktime_t now, remaining, iv; 668 ktime_t now, remaining, iv;
740 struct hrtimer *timer = &timr->it.real.timer; 669 struct timespec64 ts64;
670 bool sig_none;
741 671
742 memset(cur_setting, 0, sizeof(*cur_setting)); 672 sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
743 673 iv = timr->it_interval;
744 iv = timr->it.real.interval;
745 674
746 /* interval timer ? */ 675 /* interval timer ? */
747 if (iv) 676 if (iv) {
748 cur_setting->it_interval = ktime_to_timespec64(iv); 677 cur_setting->it_interval = ktime_to_timespec64(iv);
749 else if (!hrtimer_active(timer) && 678 } else if (!timr->it_active) {
750 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) 679 /*
751 return; 680 * SIGEV_NONE oneshot timers are never queued. Check them
681 * below.
682 */
683 if (!sig_none)
684 return;
685 }
752 686
753 now = timer->base->get_time(); 687 /*
688 * The timespec64 based conversion is suboptimal, but it's not
689 * worth to implement yet another callback.
690 */
691 kc->clock_get(timr->it_clock, &ts64);
692 now = timespec64_to_ktime(ts64);
754 693
755 /* 694 /*
756 * When a requeue is pending or this is a SIGEV_NONE 695 * When a requeue is pending or this is a SIGEV_NONE timer move the
757 * timer move the expiry time forward by intervals, so 696 * expiry time forward by intervals, so expiry is > now.
758 * expiry is > now.
759 */ 697 */
760 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || 698 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
761 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) 699 timr->it_overrun += kc->timer_forward(timr, now);
762 timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
763 700
764 remaining = __hrtimer_expires_remaining_adjusted(timer, now); 701 remaining = kc->timer_remaining(timr, now);
765 /* Return 0 only, when the timer is expired and not pending */ 702 /* Return 0 only, when the timer is expired and not pending */
766 if (remaining <= 0) { 703 if (remaining <= 0) {
767 /* 704 /*
768 * A single shot SIGEV_NONE timer must return 0, when 705 * A single shot SIGEV_NONE timer must return 0, when
769 * it is expired ! 706 * it is expired !
770 */ 707 */
771 if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) 708 if (!sig_none)
772 cur_setting->it_value.tv_nsec = 1; 709 cur_setting->it_value.tv_nsec = 1;
773 } else 710 } else {
774 cur_setting->it_value = ktime_to_timespec64(remaining); 711 cur_setting->it_value = ktime_to_timespec64(remaining);
712 }
775} 713}
776 714
777/* Get the time remaining on a POSIX.1b interval timer. */ 715/* Get the time remaining on a POSIX.1b interval timer. */
778SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, 716static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
779 struct itimerspec __user *, setting)
780{ 717{
781 struct itimerspec64 cur_setting64;
782 struct itimerspec cur_setting;
783 struct k_itimer *timr; 718 struct k_itimer *timr;
784 struct k_clock *kc; 719 const struct k_clock *kc;
785 unsigned long flags; 720 unsigned long flags;
786 int ret = 0; 721 int ret = 0;
787 722
@@ -789,20 +724,49 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
789 if (!timr) 724 if (!timr)
790 return -EINVAL; 725 return -EINVAL;
791 726
792 kc = clockid_to_kclock(timr->it_clock); 727 memset(setting, 0, sizeof(*setting));
728 kc = timr->kclock;
793 if (WARN_ON_ONCE(!kc || !kc->timer_get)) 729 if (WARN_ON_ONCE(!kc || !kc->timer_get))
794 ret = -EINVAL; 730 ret = -EINVAL;
795 else 731 else
796 kc->timer_get(timr, &cur_setting64); 732 kc->timer_get(timr, setting);
797 733
798 unlock_timer(timr, flags); 734 unlock_timer(timr, flags);
735 return ret;
736}
799 737
800 cur_setting = itimerspec64_to_itimerspec(&cur_setting64); 738/* Get the time remaining on a POSIX.1b interval timer. */
801 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) 739SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
802 return -EFAULT; 740 struct itimerspec __user *, setting)
741{
742 struct itimerspec64 cur_setting64;
803 743
744 int ret = do_timer_gettime(timer_id, &cur_setting64);
745 if (!ret) {
746 struct itimerspec cur_setting;
747 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
748 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
749 ret = -EFAULT;
750 }
751 return ret;
752}
753
754#ifdef CONFIG_COMPAT
755COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
756 struct compat_itimerspec __user *, setting)
757{
758 struct itimerspec64 cur_setting64;
759
760 int ret = do_timer_gettime(timer_id, &cur_setting64);
761 if (!ret) {
762 struct itimerspec cur_setting;
763 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
764 if (put_compat_itimerspec(setting, &cur_setting))
765 ret = -EFAULT;
766 }
804 return ret; 767 return ret;
805} 768}
769#endif
806 770
807/* 771/*
808 * Get the number of overruns of a POSIX.1b interval timer. This is to 772 * Get the number of overruns of a POSIX.1b interval timer. This is to
@@ -810,7 +774,7 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
810 * accumulating overruns on the next timer. The overrun is frozen when 774 * accumulating overruns on the next timer. The overrun is frozen when
811 * the signal is delivered, either at the notify time (if the info block 775 * the signal is delivered, either at the notify time (if the info block
812 * is not queued) or at the actual delivery time (as we are informed by 776 * is not queued) or at the actual delivery time (as we are informed by
813 * the call back to do_schedule_next_timer(). So all we need to do is 777 * the call back to posixtimer_rearm(). So all we need to do is
814 * to pick up the frozen overrun. 778 * to pick up the frozen overrun.
815 */ 779 */
816SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) 780SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
@@ -829,117 +793,183 @@ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
829 return overrun; 793 return overrun;
830} 794}
831 795
832/* Set a POSIX.1b interval timer. */ 796static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
833/* timr->it_lock is taken. */ 797 bool absolute, bool sigev_none)
834static int
835common_timer_set(struct k_itimer *timr, int flags,
836 struct itimerspec64 *new_setting, struct itimerspec64 *old_setting)
837{ 798{
838 struct hrtimer *timer = &timr->it.real.timer; 799 struct hrtimer *timer = &timr->it.real.timer;
839 enum hrtimer_mode mode; 800 enum hrtimer_mode mode;
840 801
802 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
803 /*
804 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
805 * clock modifications, so they become CLOCK_MONOTONIC based under the
806 * hood. See hrtimer_init(). Update timr->kclock, so the generic
807 * functions which use timr->kclock->clock_get() work.
808 *
809 * Note: it_clock stays unmodified, because the next timer_set() might
810 * use ABSTIME, so it needs to switch back.
811 */
812 if (timr->it_clock == CLOCK_REALTIME)
813 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
814
815 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
816 timr->it.real.timer.function = posix_timer_fn;
817
818 if (!absolute)
819 expires = ktime_add_safe(expires, timer->base->get_time());
820 hrtimer_set_expires(timer, expires);
821
822 if (!sigev_none)
823 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
824}
825
826static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
827{
828 return hrtimer_try_to_cancel(&timr->it.real.timer);
829}
830
831/* Set a POSIX.1b interval timer. */
832int common_timer_set(struct k_itimer *timr, int flags,
833 struct itimerspec64 *new_setting,
834 struct itimerspec64 *old_setting)
835{
836 const struct k_clock *kc = timr->kclock;
837 bool sigev_none;
838 ktime_t expires;
839
841 if (old_setting) 840 if (old_setting)
842 common_timer_get(timr, old_setting); 841 common_timer_get(timr, old_setting);
843 842
844 /* disable the timer */ 843 /* Prevent rearming by clearing the interval */
845 timr->it.real.interval = 0; 844 timr->it_interval = 0;
846 /* 845 /*
847 * careful here. If smp we could be in the "fire" routine which will 846 * Careful here. On SMP systems the timer expiry function could be
848 * be spinning as we hold the lock. But this is ONLY an SMP issue. 847 * active and spinning on timr->it_lock.
849 */ 848 */
850 if (hrtimer_try_to_cancel(timer) < 0) 849 if (kc->timer_try_to_cancel(timr) < 0)
851 return TIMER_RETRY; 850 return TIMER_RETRY;
852 851
853 timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 852 timr->it_active = 0;
853 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
854 ~REQUEUE_PENDING; 854 ~REQUEUE_PENDING;
855 timr->it_overrun_last = 0; 855 timr->it_overrun_last = 0;
856 856
857 /* switch off the timer when it_value is zero */ 857 /* Switch off the timer when it_value is zero */
858 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) 858 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
859 return 0; 859 return 0;
860 860
861 mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; 861 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
862 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); 862 expires = timespec64_to_ktime(new_setting->it_value);
863 timr->it.real.timer.function = posix_timer_fn; 863 sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
864
865 hrtimer_set_expires(timer, timespec64_to_ktime(new_setting->it_value));
866
867 /* Convert interval */
868 timr->it.real.interval = timespec64_to_ktime(new_setting->it_interval);
869
870 /* SIGEV_NONE timers are not queued ! See common_timer_get */
871 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
872 /* Setup correct expiry time for relative timers */
873 if (mode == HRTIMER_MODE_REL) {
874 hrtimer_add_expires(timer, timer->base->get_time());
875 }
876 return 0;
877 }
878 864
879 hrtimer_start_expires(timer, mode); 865 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
866 timr->it_active = !sigev_none;
880 return 0; 867 return 0;
881} 868}
882 869
883/* Set a POSIX.1b interval timer */ 870static int do_timer_settime(timer_t timer_id, int flags,
884SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, 871 struct itimerspec64 *new_spec64,
885 const struct itimerspec __user *, new_setting, 872 struct itimerspec64 *old_spec64)
886 struct itimerspec __user *, old_setting)
887{ 873{
888 struct itimerspec64 new_spec64, old_spec64; 874 const struct k_clock *kc;
889 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
890 struct itimerspec new_spec, old_spec;
891 struct k_itimer *timr; 875 struct k_itimer *timr;
892 unsigned long flag; 876 unsigned long flag;
893 struct k_clock *kc;
894 int error = 0; 877 int error = 0;
895 878
896 if (!new_setting) 879 if (!timespec64_valid(&new_spec64->it_interval) ||
880 !timespec64_valid(&new_spec64->it_value))
897 return -EINVAL; 881 return -EINVAL;
898 882
899 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) 883 if (old_spec64)
900 return -EFAULT; 884 memset(old_spec64, 0, sizeof(*old_spec64));
901 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
902
903 if (!timespec64_valid(&new_spec64.it_interval) ||
904 !timespec64_valid(&new_spec64.it_value))
905 return -EINVAL;
906retry: 885retry:
907 timr = lock_timer(timer_id, &flag); 886 timr = lock_timer(timer_id, &flag);
908 if (!timr) 887 if (!timr)
909 return -EINVAL; 888 return -EINVAL;
910 889
911 kc = clockid_to_kclock(timr->it_clock); 890 kc = timr->kclock;
912 if (WARN_ON_ONCE(!kc || !kc->timer_set)) 891 if (WARN_ON_ONCE(!kc || !kc->timer_set))
913 error = -EINVAL; 892 error = -EINVAL;
914 else 893 else
915 error = kc->timer_set(timr, flags, &new_spec64, rtn); 894 error = kc->timer_set(timr, flags, new_spec64, old_spec64);
916 895
917 unlock_timer(timr, flag); 896 unlock_timer(timr, flag);
918 if (error == TIMER_RETRY) { 897 if (error == TIMER_RETRY) {
919 rtn = NULL; // We already got the old time... 898 old_spec64 = NULL; // We already got the old time...
920 goto retry; 899 goto retry;
921 } 900 }
922 901
923 old_spec = itimerspec64_to_itimerspec(&old_spec64); 902 return error;
924 if (old_setting && !error && 903}
925 copy_to_user(old_setting, &old_spec, sizeof (old_spec))) 904
926 error = -EFAULT; 905/* Set a POSIX.1b interval timer */
906SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
907 const struct itimerspec __user *, new_setting,
908 struct itimerspec __user *, old_setting)
909{
910 struct itimerspec64 new_spec64, old_spec64;
911 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
912 struct itimerspec new_spec;
913 int error = 0;
914
915 if (!new_setting)
916 return -EINVAL;
917
918 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
919 return -EFAULT;
920 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
921
922 error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
923 if (!error && old_setting) {
924 struct itimerspec old_spec;
925 old_spec = itimerspec64_to_itimerspec(&old_spec64);
926 if (copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
927 error = -EFAULT;
928 }
929 return error;
930}
931
932#ifdef CONFIG_COMPAT
933COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
934 struct compat_itimerspec __user *, new,
935 struct compat_itimerspec __user *, old)
936{
937 struct itimerspec64 new_spec64, old_spec64;
938 struct itimerspec64 *rtn = old ? &old_spec64 : NULL;
939 struct itimerspec new_spec;
940 int error = 0;
941
942 if (!new)
943 return -EINVAL;
944 if (get_compat_itimerspec(&new_spec, new))
945 return -EFAULT;
927 946
947 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
948 error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
949 if (!error && old) {
950 struct itimerspec old_spec;
951 old_spec = itimerspec64_to_itimerspec(&old_spec64);
952 if (put_compat_itimerspec(old, &old_spec))
953 error = -EFAULT;
954 }
928 return error; 955 return error;
929} 956}
957#endif
930 958
931static int common_timer_del(struct k_itimer *timer) 959int common_timer_del(struct k_itimer *timer)
932{ 960{
933 timer->it.real.interval = 0; 961 const struct k_clock *kc = timer->kclock;
934 962
935 if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0) 963 timer->it_interval = 0;
964 if (kc->timer_try_to_cancel(timer) < 0)
936 return TIMER_RETRY; 965 return TIMER_RETRY;
966 timer->it_active = 0;
937 return 0; 967 return 0;
938} 968}
939 969
940static inline int timer_delete_hook(struct k_itimer *timer) 970static inline int timer_delete_hook(struct k_itimer *timer)
941{ 971{
942 struct k_clock *kc = clockid_to_kclock(timer->it_clock); 972 const struct k_clock *kc = timer->kclock;
943 973
944 if (WARN_ON_ONCE(!kc || !kc->timer_del)) 974 if (WARN_ON_ONCE(!kc || !kc->timer_del))
945 return -EINVAL; 975 return -EINVAL;
@@ -1018,7 +1048,7 @@ void exit_itimers(struct signal_struct *sig)
1018SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, 1048SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1019 const struct timespec __user *, tp) 1049 const struct timespec __user *, tp)
1020{ 1050{
1021 struct k_clock *kc = clockid_to_kclock(which_clock); 1051 const struct k_clock *kc = clockid_to_kclock(which_clock);
1022 struct timespec64 new_tp64; 1052 struct timespec64 new_tp64;
1023 struct timespec new_tp; 1053 struct timespec new_tp;
1024 1054
@@ -1035,7 +1065,7 @@ SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1035SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, 1065SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1036 struct timespec __user *,tp) 1066 struct timespec __user *,tp)
1037{ 1067{
1038 struct k_clock *kc = clockid_to_kclock(which_clock); 1068 const struct k_clock *kc = clockid_to_kclock(which_clock);
1039 struct timespec64 kernel_tp64; 1069 struct timespec64 kernel_tp64;
1040 struct timespec kernel_tp; 1070 struct timespec kernel_tp;
1041 int error; 1071 int error;
@@ -1055,7 +1085,7 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1055SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, 1085SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1056 struct timex __user *, utx) 1086 struct timex __user *, utx)
1057{ 1087{
1058 struct k_clock *kc = clockid_to_kclock(which_clock); 1088 const struct k_clock *kc = clockid_to_kclock(which_clock);
1059 struct timex ktx; 1089 struct timex ktx;
1060 int err; 1090 int err;
1061 1091
@@ -1078,7 +1108,7 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1078SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, 1108SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1079 struct timespec __user *, tp) 1109 struct timespec __user *, tp)
1080{ 1110{
1081 struct k_clock *kc = clockid_to_kclock(which_clock); 1111 const struct k_clock *kc = clockid_to_kclock(which_clock);
1082 struct timespec64 rtn_tp64; 1112 struct timespec64 rtn_tp64;
1083 struct timespec rtn_tp; 1113 struct timespec rtn_tp;
1084 int error; 1114 int error;
@@ -1095,13 +1125,98 @@ SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1095 return error; 1125 return error;
1096} 1126}
1097 1127
1128#ifdef CONFIG_COMPAT
1129
1130COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1131 struct compat_timespec __user *, tp)
1132{
1133 const struct k_clock *kc = clockid_to_kclock(which_clock);
1134 struct timespec64 new_tp64;
1135 struct timespec new_tp;
1136
1137 if (!kc || !kc->clock_set)
1138 return -EINVAL;
1139
1140 if (compat_get_timespec(&new_tp, tp))
1141 return -EFAULT;
1142
1143 new_tp64 = timespec_to_timespec64(new_tp);
1144
1145 return kc->clock_set(which_clock, &new_tp64);
1146}
1147
1148COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1149 struct compat_timespec __user *, tp)
1150{
1151 const struct k_clock *kc = clockid_to_kclock(which_clock);
1152 struct timespec64 kernel_tp64;
1153 struct timespec kernel_tp;
1154 int error;
1155
1156 if (!kc)
1157 return -EINVAL;
1158
1159 error = kc->clock_get(which_clock, &kernel_tp64);
1160 kernel_tp = timespec64_to_timespec(kernel_tp64);
1161
1162 if (!error && compat_put_timespec(&kernel_tp, tp))
1163 error = -EFAULT;
1164
1165 return error;
1166}
1167
1168COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1169 struct compat_timex __user *, utp)
1170{
1171 const struct k_clock *kc = clockid_to_kclock(which_clock);
1172 struct timex ktx;
1173 int err;
1174
1175 if (!kc)
1176 return -EINVAL;
1177 if (!kc->clock_adj)
1178 return -EOPNOTSUPP;
1179
1180 err = compat_get_timex(&ktx, utp);
1181 if (err)
1182 return err;
1183
1184 err = kc->clock_adj(which_clock, &ktx);
1185
1186 if (err >= 0)
1187 err = compat_put_timex(utp, &ktx);
1188
1189 return err;
1190}
1191
1192COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1193 struct compat_timespec __user *, tp)
1194{
1195 const struct k_clock *kc = clockid_to_kclock(which_clock);
1196 struct timespec64 rtn_tp64;
1197 struct timespec rtn_tp;
1198 int error;
1199
1200 if (!kc)
1201 return -EINVAL;
1202
1203 error = kc->clock_getres(which_clock, &rtn_tp64);
1204 rtn_tp = timespec64_to_timespec(rtn_tp64);
1205
1206 if (!error && tp && compat_put_timespec(&rtn_tp, tp))
1207 error = -EFAULT;
1208
1209 return error;
1210}
1211#endif
1212
1098/* 1213/*
1099 * nanosleep for monotonic and realtime clocks 1214 * nanosleep for monotonic and realtime clocks
1100 */ 1215 */
1101static int common_nsleep(const clockid_t which_clock, int flags, 1216static int common_nsleep(const clockid_t which_clock, int flags,
1102 struct timespec64 *tsave, struct timespec __user *rmtp) 1217 const struct timespec64 *rqtp)
1103{ 1218{
1104 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ? 1219 return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
1105 HRTIMER_MODE_ABS : HRTIMER_MODE_REL, 1220 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1106 which_clock); 1221 which_clock);
1107} 1222}
@@ -1110,7 +1225,7 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1110 const struct timespec __user *, rqtp, 1225 const struct timespec __user *, rqtp,
1111 struct timespec __user *, rmtp) 1226 struct timespec __user *, rmtp)
1112{ 1227{
1113 struct k_clock *kc = clockid_to_kclock(which_clock); 1228 const struct k_clock *kc = clockid_to_kclock(which_clock);
1114 struct timespec64 t64; 1229 struct timespec64 t64;
1115 struct timespec t; 1230 struct timespec t;
1116 1231
@@ -1125,21 +1240,141 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1125 t64 = timespec_to_timespec64(t); 1240 t64 = timespec_to_timespec64(t);
1126 if (!timespec64_valid(&t64)) 1241 if (!timespec64_valid(&t64))
1127 return -EINVAL; 1242 return -EINVAL;
1243 if (flags & TIMER_ABSTIME)
1244 rmtp = NULL;
1245 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1246 current->restart_block.nanosleep.rmtp = rmtp;
1128 1247
1129 return kc->nsleep(which_clock, flags, &t64, rmtp); 1248 return kc->nsleep(which_clock, flags, &t64);
1130} 1249}
1131 1250
1132/* 1251#ifdef CONFIG_COMPAT
1133 * This will restart clock_nanosleep. This is required only by 1252COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1134 * compat_clock_nanosleep_restart for now. 1253 struct compat_timespec __user *, rqtp,
1135 */ 1254 struct compat_timespec __user *, rmtp)
1136long clock_nanosleep_restart(struct restart_block *restart_block)
1137{ 1255{
1138 clockid_t which_clock = restart_block->nanosleep.clockid; 1256 const struct k_clock *kc = clockid_to_kclock(which_clock);
1139 struct k_clock *kc = clockid_to_kclock(which_clock); 1257 struct timespec64 t64;
1258 struct timespec t;
1140 1259
1141 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart)) 1260 if (!kc)
1142 return -EINVAL; 1261 return -EINVAL;
1262 if (!kc->nsleep)
1263 return -ENANOSLEEP_NOTSUP;
1143 1264
1144 return kc->nsleep_restart(restart_block); 1265 if (compat_get_timespec(&t, rqtp))
1266 return -EFAULT;
1267
1268 t64 = timespec_to_timespec64(t);
1269 if (!timespec64_valid(&t64))
1270 return -EINVAL;
1271 if (flags & TIMER_ABSTIME)
1272 rmtp = NULL;
1273 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1274 current->restart_block.nanosleep.compat_rmtp = rmtp;
1275
1276 return kc->nsleep(which_clock, flags, &t64);
1277}
1278#endif
1279
1280static const struct k_clock clock_realtime = {
1281 .clock_getres = posix_get_hrtimer_res,
1282 .clock_get = posix_clock_realtime_get,
1283 .clock_set = posix_clock_realtime_set,
1284 .clock_adj = posix_clock_realtime_adj,
1285 .nsleep = common_nsleep,
1286 .timer_create = common_timer_create,
1287 .timer_set = common_timer_set,
1288 .timer_get = common_timer_get,
1289 .timer_del = common_timer_del,
1290 .timer_rearm = common_hrtimer_rearm,
1291 .timer_forward = common_hrtimer_forward,
1292 .timer_remaining = common_hrtimer_remaining,
1293 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1294 .timer_arm = common_hrtimer_arm,
1295};
1296
1297static const struct k_clock clock_monotonic = {
1298 .clock_getres = posix_get_hrtimer_res,
1299 .clock_get = posix_ktime_get_ts,
1300 .nsleep = common_nsleep,
1301 .timer_create = common_timer_create,
1302 .timer_set = common_timer_set,
1303 .timer_get = common_timer_get,
1304 .timer_del = common_timer_del,
1305 .timer_rearm = common_hrtimer_rearm,
1306 .timer_forward = common_hrtimer_forward,
1307 .timer_remaining = common_hrtimer_remaining,
1308 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1309 .timer_arm = common_hrtimer_arm,
1310};
1311
1312static const struct k_clock clock_monotonic_raw = {
1313 .clock_getres = posix_get_hrtimer_res,
1314 .clock_get = posix_get_monotonic_raw,
1315};
1316
1317static const struct k_clock clock_realtime_coarse = {
1318 .clock_getres = posix_get_coarse_res,
1319 .clock_get = posix_get_realtime_coarse,
1320};
1321
1322static const struct k_clock clock_monotonic_coarse = {
1323 .clock_getres = posix_get_coarse_res,
1324 .clock_get = posix_get_monotonic_coarse,
1325};
1326
1327static const struct k_clock clock_tai = {
1328 .clock_getres = posix_get_hrtimer_res,
1329 .clock_get = posix_get_tai,
1330 .nsleep = common_nsleep,
1331 .timer_create = common_timer_create,
1332 .timer_set = common_timer_set,
1333 .timer_get = common_timer_get,
1334 .timer_del = common_timer_del,
1335 .timer_rearm = common_hrtimer_rearm,
1336 .timer_forward = common_hrtimer_forward,
1337 .timer_remaining = common_hrtimer_remaining,
1338 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1339 .timer_arm = common_hrtimer_arm,
1340};
1341
1342static const struct k_clock clock_boottime = {
1343 .clock_getres = posix_get_hrtimer_res,
1344 .clock_get = posix_get_boottime,
1345 .nsleep = common_nsleep,
1346 .timer_create = common_timer_create,
1347 .timer_set = common_timer_set,
1348 .timer_get = common_timer_get,
1349 .timer_del = common_timer_del,
1350 .timer_rearm = common_hrtimer_rearm,
1351 .timer_forward = common_hrtimer_forward,
1352 .timer_remaining = common_hrtimer_remaining,
1353 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1354 .timer_arm = common_hrtimer_arm,
1355};
1356
1357static const struct k_clock * const posix_clocks[] = {
1358 [CLOCK_REALTIME] = &clock_realtime,
1359 [CLOCK_MONOTONIC] = &clock_monotonic,
1360 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1361 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1362 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1363 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1364 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1365 [CLOCK_BOOTTIME] = &clock_boottime,
1366 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1367 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1368 [CLOCK_TAI] = &clock_tai,
1369};
1370
1371static const struct k_clock *clockid_to_kclock(const clockid_t id)
1372{
1373 if (id < 0)
1374 return (id & CLOCKFD_MASK) == CLOCKFD ?
1375 &clock_posix_dynamic : &clock_posix_cpu;
1376
1377 if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
1378 return NULL;
1379 return posix_clocks[id];
1145} 1380}
diff --git a/kernel/time/posix-timers.h b/kernel/time/posix-timers.h
new file mode 100644
index 000000000000..fb303c3be4d3
--- /dev/null
+++ b/kernel/time/posix-timers.h
@@ -0,0 +1,40 @@
1#define TIMER_RETRY 1
2
3struct k_clock {
4 int (*clock_getres)(const clockid_t which_clock,
5 struct timespec64 *tp);
6 int (*clock_set)(const clockid_t which_clock,
7 const struct timespec64 *tp);
8 int (*clock_get)(const clockid_t which_clock,
9 struct timespec64 *tp);
10 int (*clock_adj)(const clockid_t which_clock, struct timex *tx);
11 int (*timer_create)(struct k_itimer *timer);
12 int (*nsleep)(const clockid_t which_clock, int flags,
13 const struct timespec64 *);
14 int (*timer_set)(struct k_itimer *timr, int flags,
15 struct itimerspec64 *new_setting,
16 struct itimerspec64 *old_setting);
17 int (*timer_del)(struct k_itimer *timr);
18 void (*timer_get)(struct k_itimer *timr,
19 struct itimerspec64 *cur_setting);
20 void (*timer_rearm)(struct k_itimer *timr);
21 int (*timer_forward)(struct k_itimer *timr, ktime_t now);
22 ktime_t (*timer_remaining)(struct k_itimer *timr, ktime_t now);
23 int (*timer_try_to_cancel)(struct k_itimer *timr);
24 void (*timer_arm)(struct k_itimer *timr, ktime_t expires,
25 bool absolute, bool sigev_none);
26};
27
28extern const struct k_clock clock_posix_cpu;
29extern const struct k_clock clock_posix_dynamic;
30extern const struct k_clock clock_process;
31extern const struct k_clock clock_thread;
32extern const struct k_clock alarm_clock;
33
34int posix_timer_event(struct k_itimer *timr, int si_private);
35
36void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting);
37int common_timer_set(struct k_itimer *timr, int flags,
38 struct itimerspec64 *new_setting,
39 struct itimerspec64 *old_setting);
40int common_timer_del(struct k_itimer *timer);
diff --git a/kernel/time/time.c b/kernel/time/time.c
index 49c73c6ed648..7c89e437c4d7 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -39,6 +39,7 @@
39#include <linux/ptrace.h> 39#include <linux/ptrace.h>
40 40
41#include <linux/uaccess.h> 41#include <linux/uaccess.h>
42#include <linux/compat.h>
42#include <asm/unistd.h> 43#include <asm/unistd.h>
43 44
44#include <generated/timeconst.h> 45#include <generated/timeconst.h>
@@ -99,6 +100,47 @@ SYSCALL_DEFINE1(stime, time_t __user *, tptr)
99 100
100#endif /* __ARCH_WANT_SYS_TIME */ 101#endif /* __ARCH_WANT_SYS_TIME */
101 102
103#ifdef CONFIG_COMPAT
104#ifdef __ARCH_WANT_COMPAT_SYS_TIME
105
106/* compat_time_t is a 32 bit "long" and needs to get converted. */
107COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
108{
109 struct timeval tv;
110 compat_time_t i;
111
112 do_gettimeofday(&tv);
113 i = tv.tv_sec;
114
115 if (tloc) {
116 if (put_user(i,tloc))
117 return -EFAULT;
118 }
119 force_successful_syscall_return();
120 return i;
121}
122
123COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
124{
125 struct timespec tv;
126 int err;
127
128 if (get_user(tv.tv_sec, tptr))
129 return -EFAULT;
130
131 tv.tv_nsec = 0;
132
133 err = security_settime(&tv, NULL);
134 if (err)
135 return err;
136
137 do_settimeofday(&tv);
138 return 0;
139}
140
141#endif /* __ARCH_WANT_COMPAT_SYS_TIME */
142#endif
143
102SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv, 144SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv,
103 struct timezone __user *, tz) 145 struct timezone __user *, tz)
104{ 146{
@@ -215,6 +257,47 @@ SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
215 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 257 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
216} 258}
217 259
260#ifdef CONFIG_COMPAT
261COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv,
262 struct timezone __user *, tz)
263{
264 if (tv) {
265 struct timeval ktv;
266
267 do_gettimeofday(&ktv);
268 if (compat_put_timeval(&ktv, tv))
269 return -EFAULT;
270 }
271 if (tz) {
272 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
273 return -EFAULT;
274 }
275
276 return 0;
277}
278
279COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv,
280 struct timezone __user *, tz)
281{
282 struct timespec64 new_ts;
283 struct timeval user_tv;
284 struct timezone new_tz;
285
286 if (tv) {
287 if (compat_get_timeval(&user_tv, tv))
288 return -EFAULT;
289 new_ts.tv_sec = user_tv.tv_sec;
290 new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
291 }
292 if (tz) {
293 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
294 return -EFAULT;
295 }
296
297 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
298}
299#endif
300
218SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p) 301SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
219{ 302{
220 struct timex txc; /* Local copy of parameter */ 303 struct timex txc; /* Local copy of parameter */
@@ -224,12 +307,33 @@ SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
224 * structure. But bear in mind that the structures 307 * structure. But bear in mind that the structures
225 * may change 308 * may change
226 */ 309 */
227 if(copy_from_user(&txc, txc_p, sizeof(struct timex))) 310 if (copy_from_user(&txc, txc_p, sizeof(struct timex)))
228 return -EFAULT; 311 return -EFAULT;
229 ret = do_adjtimex(&txc); 312 ret = do_adjtimex(&txc);
230 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret; 313 return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
231} 314}
232 315
316#ifdef CONFIG_COMPAT
317
318COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
319{
320 struct timex txc;
321 int err, ret;
322
323 err = compat_get_timex(&txc, utp);
324 if (err)
325 return err;
326
327 ret = do_adjtimex(&txc);
328
329 err = compat_put_timex(utp, &txc);
330 if (err)
331 return err;
332
333 return ret;
334}
335#endif
336
233/* 337/*
234 * Convert jiffies to milliseconds and back. 338 * Convert jiffies to milliseconds and back.
235 * 339 *
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index b602c48cb841..cedafa008de5 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -72,6 +72,10 @@ static inline void tk_normalize_xtime(struct timekeeper *tk)
72 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift; 72 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
73 tk->xtime_sec++; 73 tk->xtime_sec++;
74 } 74 }
75 while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
76 tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
77 tk->raw_sec++;
78 }
75} 79}
76 80
77static inline struct timespec64 tk_xtime(struct timekeeper *tk) 81static inline struct timespec64 tk_xtime(struct timekeeper *tk)
@@ -285,12 +289,14 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
285 /* if changing clocks, convert xtime_nsec shift units */ 289 /* if changing clocks, convert xtime_nsec shift units */
286 if (old_clock) { 290 if (old_clock) {
287 int shift_change = clock->shift - old_clock->shift; 291 int shift_change = clock->shift - old_clock->shift;
288 if (shift_change < 0) 292 if (shift_change < 0) {
289 tk->tkr_mono.xtime_nsec >>= -shift_change; 293 tk->tkr_mono.xtime_nsec >>= -shift_change;
290 else 294 tk->tkr_raw.xtime_nsec >>= -shift_change;
295 } else {
291 tk->tkr_mono.xtime_nsec <<= shift_change; 296 tk->tkr_mono.xtime_nsec <<= shift_change;
297 tk->tkr_raw.xtime_nsec <<= shift_change;
298 }
292 } 299 }
293 tk->tkr_raw.xtime_nsec = 0;
294 300
295 tk->tkr_mono.shift = clock->shift; 301 tk->tkr_mono.shift = clock->shift;
296 tk->tkr_raw.shift = clock->shift; 302 tk->tkr_raw.shift = clock->shift;
@@ -510,6 +516,7 @@ static void halt_fast_timekeeper(struct timekeeper *tk)
510} 516}
511 517
512#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD 518#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
519#warning Please contact your maintainers, as GENERIC_TIME_VSYSCALL_OLD compatibity will disappear soon.
513 520
514static inline void update_vsyscall(struct timekeeper *tk) 521static inline void update_vsyscall(struct timekeeper *tk)
515{ 522{
@@ -619,9 +626,6 @@ static inline void tk_update_ktime_data(struct timekeeper *tk)
619 nsec = (u32) tk->wall_to_monotonic.tv_nsec; 626 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
620 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec); 627 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
621 628
622 /* Update the monotonic raw base */
623 tk->tkr_raw.base = timespec64_to_ktime(tk->raw_time);
624
625 /* 629 /*
626 * The sum of the nanoseconds portions of xtime and 630 * The sum of the nanoseconds portions of xtime and
627 * wall_to_monotonic can be greater/equal one second. Take 631 * wall_to_monotonic can be greater/equal one second. Take
@@ -631,6 +635,11 @@ static inline void tk_update_ktime_data(struct timekeeper *tk)
631 if (nsec >= NSEC_PER_SEC) 635 if (nsec >= NSEC_PER_SEC)
632 seconds++; 636 seconds++;
633 tk->ktime_sec = seconds; 637 tk->ktime_sec = seconds;
638
639 /* Update the monotonic raw base */
640 seconds = tk->raw_sec;
641 nsec = (u32)(tk->tkr_raw.xtime_nsec >> tk->tkr_raw.shift);
642 tk->tkr_raw.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
634} 643}
635 644
636/* must hold timekeeper_lock */ 645/* must hold timekeeper_lock */
@@ -672,7 +681,6 @@ static void timekeeping_update(struct timekeeper *tk, unsigned int action)
672static void timekeeping_forward_now(struct timekeeper *tk) 681static void timekeeping_forward_now(struct timekeeper *tk)
673{ 682{
674 u64 cycle_now, delta; 683 u64 cycle_now, delta;
675 u64 nsec;
676 684
677 cycle_now = tk_clock_read(&tk->tkr_mono); 685 cycle_now = tk_clock_read(&tk->tkr_mono);
678 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask); 686 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
@@ -684,10 +692,13 @@ static void timekeeping_forward_now(struct timekeeper *tk)
684 /* If arch requires, add in get_arch_timeoffset() */ 692 /* If arch requires, add in get_arch_timeoffset() */
685 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift; 693 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
686 694
687 tk_normalize_xtime(tk);
688 695
689 nsec = clocksource_cyc2ns(delta, tk->tkr_raw.mult, tk->tkr_raw.shift); 696 tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult;
690 timespec64_add_ns(&tk->raw_time, nsec); 697
698 /* If arch requires, add in get_arch_timeoffset() */
699 tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift;
700
701 tk_normalize_xtime(tk);
691} 702}
692 703
693/** 704/**
@@ -1373,19 +1384,18 @@ int timekeeping_notify(struct clocksource *clock)
1373void getrawmonotonic64(struct timespec64 *ts) 1384void getrawmonotonic64(struct timespec64 *ts)
1374{ 1385{
1375 struct timekeeper *tk = &tk_core.timekeeper; 1386 struct timekeeper *tk = &tk_core.timekeeper;
1376 struct timespec64 ts64;
1377 unsigned long seq; 1387 unsigned long seq;
1378 u64 nsecs; 1388 u64 nsecs;
1379 1389
1380 do { 1390 do {
1381 seq = read_seqcount_begin(&tk_core.seq); 1391 seq = read_seqcount_begin(&tk_core.seq);
1392 ts->tv_sec = tk->raw_sec;
1382 nsecs = timekeeping_get_ns(&tk->tkr_raw); 1393 nsecs = timekeeping_get_ns(&tk->tkr_raw);
1383 ts64 = tk->raw_time;
1384 1394
1385 } while (read_seqcount_retry(&tk_core.seq, seq)); 1395 } while (read_seqcount_retry(&tk_core.seq, seq));
1386 1396
1387 timespec64_add_ns(&ts64, nsecs); 1397 ts->tv_nsec = 0;
1388 *ts = ts64; 1398 timespec64_add_ns(ts, nsecs);
1389} 1399}
1390EXPORT_SYMBOL(getrawmonotonic64); 1400EXPORT_SYMBOL(getrawmonotonic64);
1391 1401
@@ -1509,8 +1519,7 @@ void __init timekeeping_init(void)
1509 tk_setup_internals(tk, clock); 1519 tk_setup_internals(tk, clock);
1510 1520
1511 tk_set_xtime(tk, &now); 1521 tk_set_xtime(tk, &now);
1512 tk->raw_time.tv_sec = 0; 1522 tk->raw_sec = 0;
1513 tk->raw_time.tv_nsec = 0;
1514 if (boot.tv_sec == 0 && boot.tv_nsec == 0) 1523 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
1515 boot = tk_xtime(tk); 1524 boot = tk_xtime(tk);
1516 1525
@@ -2011,15 +2020,12 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
2011 *clock_set |= accumulate_nsecs_to_secs(tk); 2020 *clock_set |= accumulate_nsecs_to_secs(tk);
2012 2021
2013 /* Accumulate raw time */ 2022 /* Accumulate raw time */
2014 tk->tkr_raw.xtime_nsec += (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift;
2015 tk->tkr_raw.xtime_nsec += tk->raw_interval << shift; 2023 tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2016 snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift; 2024 snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2017 while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) { 2025 while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2018 tk->tkr_raw.xtime_nsec -= snsec_per_sec; 2026 tk->tkr_raw.xtime_nsec -= snsec_per_sec;
2019 tk->raw_time.tv_sec++; 2027 tk->raw_sec++;
2020 } 2028 }
2021 tk->raw_time.tv_nsec = tk->tkr_raw.xtime_nsec >> tk->tkr_raw.shift;
2022 tk->tkr_raw.xtime_nsec -= (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift;
2023 2029
2024 /* Accumulate error between NTP and clock interval */ 2030 /* Accumulate error between NTP and clock interval */
2025 tk->ntp_error += tk->ntp_tick << shift; 2031 tk->ntp_error += tk->ntp_tick << shift;
diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 152a706ef8b8..71ce3f4eead3 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -195,7 +195,7 @@ EXPORT_SYMBOL(jiffies_64);
195#endif 195#endif
196 196
197struct timer_base { 197struct timer_base {
198 spinlock_t lock; 198 raw_spinlock_t lock;
199 struct timer_list *running_timer; 199 struct timer_list *running_timer;
200 unsigned long clk; 200 unsigned long clk;
201 unsigned long next_expiry; 201 unsigned long next_expiry;
@@ -913,10 +913,10 @@ static struct timer_base *lock_timer_base(struct timer_list *timer,
913 913
914 if (!(tf & TIMER_MIGRATING)) { 914 if (!(tf & TIMER_MIGRATING)) {
915 base = get_timer_base(tf); 915 base = get_timer_base(tf);
916 spin_lock_irqsave(&base->lock, *flags); 916 raw_spin_lock_irqsave(&base->lock, *flags);
917 if (timer->flags == tf) 917 if (timer->flags == tf)
918 return base; 918 return base;
919 spin_unlock_irqrestore(&base->lock, *flags); 919 raw_spin_unlock_irqrestore(&base->lock, *flags);
920 } 920 }
921 cpu_relax(); 921 cpu_relax();
922 } 922 }
@@ -986,9 +986,9 @@ __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
986 /* See the comment in lock_timer_base() */ 986 /* See the comment in lock_timer_base() */
987 timer->flags |= TIMER_MIGRATING; 987 timer->flags |= TIMER_MIGRATING;
988 988
989 spin_unlock(&base->lock); 989 raw_spin_unlock(&base->lock);
990 base = new_base; 990 base = new_base;
991 spin_lock(&base->lock); 991 raw_spin_lock(&base->lock);
992 WRITE_ONCE(timer->flags, 992 WRITE_ONCE(timer->flags,
993 (timer->flags & ~TIMER_BASEMASK) | base->cpu); 993 (timer->flags & ~TIMER_BASEMASK) | base->cpu);
994 } 994 }
@@ -1013,7 +1013,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
1013 } 1013 }
1014 1014
1015out_unlock: 1015out_unlock:
1016 spin_unlock_irqrestore(&base->lock, flags); 1016 raw_spin_unlock_irqrestore(&base->lock, flags);
1017 1017
1018 return ret; 1018 return ret;
1019} 1019}
@@ -1106,16 +1106,16 @@ void add_timer_on(struct timer_list *timer, int cpu)
1106 if (base != new_base) { 1106 if (base != new_base) {
1107 timer->flags |= TIMER_MIGRATING; 1107 timer->flags |= TIMER_MIGRATING;
1108 1108
1109 spin_unlock(&base->lock); 1109 raw_spin_unlock(&base->lock);
1110 base = new_base; 1110 base = new_base;
1111 spin_lock(&base->lock); 1111 raw_spin_lock(&base->lock);
1112 WRITE_ONCE(timer->flags, 1112 WRITE_ONCE(timer->flags,
1113 (timer->flags & ~TIMER_BASEMASK) | cpu); 1113 (timer->flags & ~TIMER_BASEMASK) | cpu);
1114 } 1114 }
1115 1115
1116 debug_activate(timer, timer->expires); 1116 debug_activate(timer, timer->expires);
1117 internal_add_timer(base, timer); 1117 internal_add_timer(base, timer);
1118 spin_unlock_irqrestore(&base->lock, flags); 1118 raw_spin_unlock_irqrestore(&base->lock, flags);
1119} 1119}
1120EXPORT_SYMBOL_GPL(add_timer_on); 1120EXPORT_SYMBOL_GPL(add_timer_on);
1121 1121
@@ -1141,7 +1141,7 @@ int del_timer(struct timer_list *timer)
1141 if (timer_pending(timer)) { 1141 if (timer_pending(timer)) {
1142 base = lock_timer_base(timer, &flags); 1142 base = lock_timer_base(timer, &flags);
1143 ret = detach_if_pending(timer, base, true); 1143 ret = detach_if_pending(timer, base, true);
1144 spin_unlock_irqrestore(&base->lock, flags); 1144 raw_spin_unlock_irqrestore(&base->lock, flags);
1145 } 1145 }
1146 1146
1147 return ret; 1147 return ret;
@@ -1150,7 +1150,7 @@ EXPORT_SYMBOL(del_timer);
1150 1150
1151/** 1151/**
1152 * try_to_del_timer_sync - Try to deactivate a timer 1152 * try_to_del_timer_sync - Try to deactivate a timer
1153 * @timer: timer do del 1153 * @timer: timer to delete
1154 * 1154 *
1155 * This function tries to deactivate a timer. Upon successful (ret >= 0) 1155 * This function tries to deactivate a timer. Upon successful (ret >= 0)
1156 * exit the timer is not queued and the handler is not running on any CPU. 1156 * exit the timer is not queued and the handler is not running on any CPU.
@@ -1168,7 +1168,7 @@ int try_to_del_timer_sync(struct timer_list *timer)
1168 if (base->running_timer != timer) 1168 if (base->running_timer != timer)
1169 ret = detach_if_pending(timer, base, true); 1169 ret = detach_if_pending(timer, base, true);
1170 1170
1171 spin_unlock_irqrestore(&base->lock, flags); 1171 raw_spin_unlock_irqrestore(&base->lock, flags);
1172 1172
1173 return ret; 1173 return ret;
1174} 1174}
@@ -1299,13 +1299,13 @@ static void expire_timers(struct timer_base *base, struct hlist_head *head)
1299 data = timer->data; 1299 data = timer->data;
1300 1300
1301 if (timer->flags & TIMER_IRQSAFE) { 1301 if (timer->flags & TIMER_IRQSAFE) {
1302 spin_unlock(&base->lock); 1302 raw_spin_unlock(&base->lock);
1303 call_timer_fn(timer, fn, data); 1303 call_timer_fn(timer, fn, data);
1304 spin_lock(&base->lock); 1304 raw_spin_lock(&base->lock);
1305 } else { 1305 } else {
1306 spin_unlock_irq(&base->lock); 1306 raw_spin_unlock_irq(&base->lock);
1307 call_timer_fn(timer, fn, data); 1307 call_timer_fn(timer, fn, data);
1308 spin_lock_irq(&base->lock); 1308 raw_spin_lock_irq(&base->lock);
1309 } 1309 }
1310 } 1310 }
1311} 1311}
@@ -1474,7 +1474,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
1474 if (cpu_is_offline(smp_processor_id())) 1474 if (cpu_is_offline(smp_processor_id()))
1475 return expires; 1475 return expires;
1476 1476
1477 spin_lock(&base->lock); 1477 raw_spin_lock(&base->lock);
1478 nextevt = __next_timer_interrupt(base); 1478 nextevt = __next_timer_interrupt(base);
1479 is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA); 1479 is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
1480 base->next_expiry = nextevt; 1480 base->next_expiry = nextevt;
@@ -1502,7 +1502,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
1502 if ((expires - basem) > TICK_NSEC) 1502 if ((expires - basem) > TICK_NSEC)
1503 base->is_idle = true; 1503 base->is_idle = true;
1504 } 1504 }
1505 spin_unlock(&base->lock); 1505 raw_spin_unlock(&base->lock);
1506 1506
1507 return cmp_next_hrtimer_event(basem, expires); 1507 return cmp_next_hrtimer_event(basem, expires);
1508} 1508}
@@ -1590,7 +1590,7 @@ static inline void __run_timers(struct timer_base *base)
1590 if (!time_after_eq(jiffies, base->clk)) 1590 if (!time_after_eq(jiffies, base->clk))
1591 return; 1591 return;
1592 1592
1593 spin_lock_irq(&base->lock); 1593 raw_spin_lock_irq(&base->lock);
1594 1594
1595 while (time_after_eq(jiffies, base->clk)) { 1595 while (time_after_eq(jiffies, base->clk)) {
1596 1596
@@ -1601,7 +1601,7 @@ static inline void __run_timers(struct timer_base *base)
1601 expire_timers(base, heads + levels); 1601 expire_timers(base, heads + levels);
1602 } 1602 }
1603 base->running_timer = NULL; 1603 base->running_timer = NULL;
1604 spin_unlock_irq(&base->lock); 1604 raw_spin_unlock_irq(&base->lock);
1605} 1605}
1606 1606
1607/* 1607/*
@@ -1786,16 +1786,16 @@ int timers_dead_cpu(unsigned int cpu)
1786 * The caller is globally serialized and nobody else 1786 * The caller is globally serialized and nobody else
1787 * takes two locks at once, deadlock is not possible. 1787 * takes two locks at once, deadlock is not possible.
1788 */ 1788 */
1789 spin_lock_irq(&new_base->lock); 1789 raw_spin_lock_irq(&new_base->lock);
1790 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); 1790 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1791 1791
1792 BUG_ON(old_base->running_timer); 1792 BUG_ON(old_base->running_timer);
1793 1793
1794 for (i = 0; i < WHEEL_SIZE; i++) 1794 for (i = 0; i < WHEEL_SIZE; i++)
1795 migrate_timer_list(new_base, old_base->vectors + i); 1795 migrate_timer_list(new_base, old_base->vectors + i);
1796 1796
1797 spin_unlock(&old_base->lock); 1797 raw_spin_unlock(&old_base->lock);
1798 spin_unlock_irq(&new_base->lock); 1798 raw_spin_unlock_irq(&new_base->lock);
1799 put_cpu_ptr(&timer_bases); 1799 put_cpu_ptr(&timer_bases);
1800 } 1800 }
1801 return 0; 1801 return 0;
@@ -1811,7 +1811,7 @@ static void __init init_timer_cpu(int cpu)
1811 for (i = 0; i < NR_BASES; i++) { 1811 for (i = 0; i < NR_BASES; i++) {
1812 base = per_cpu_ptr(&timer_bases[i], cpu); 1812 base = per_cpu_ptr(&timer_bases[i], cpu);
1813 base->cpu = cpu; 1813 base->cpu = cpu;
1814 spin_lock_init(&base->lock); 1814 raw_spin_lock_init(&base->lock);
1815 base->clk = jiffies; 1815 base->clk = jiffies;
1816 } 1816 }
1817} 1817}