Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implement CPU time clocks for the POSIX clock interface. |
| 3 | */ |
| 4 | |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 5 | #include <linux/sched/signal.h> |
Ingo Molnar | 32ef551 | 2017-02-05 11:48:36 +0100 | [diff] [blame] | 6 | #include <linux/sched/cputime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/posix-timers.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/errno.h> |
Roman Zippel | f8bd225 | 2008-05-01 04:34:31 -0700 | [diff] [blame] | 9 | #include <linux/math64.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 10 | #include <linux/uaccess.h> |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 11 | #include <linux/kernel_stat.h> |
Xiao Guangrong | 3f0a525 | 2009-08-10 10:52:30 +0800 | [diff] [blame] | 12 | #include <trace/events/timer.h> |
Frederic Weisbecker | a857216 | 2013-04-18 01:31:13 +0200 | [diff] [blame] | 13 | #include <linux/tick.h> |
| 14 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Thomas Gleixner | bab0aae9 | 2017-05-30 23:15:41 +0200 | [diff] [blame] | 16 | #include "posix-timers.h" |
| 17 | |
Thomas Gleixner | f37fb0a | 2017-05-30 23:15:47 +0200 | [diff] [blame] | 18 | static void posix_cpu_timer_rearm(struct k_itimer *timer); |
| 19 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 20 | /* |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 21 | * Called after updating RLIMIT_CPU to run cpu timer and update |
| 22 | * tsk->signal->cputime_expires expiration cache if necessary. Needs |
| 23 | * siglock protection since other code may update expiration cache as |
| 24 | * well. |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 25 | */ |
Jiri Slaby | 5ab46b3 | 2009-08-28 14:05:12 +0200 | [diff] [blame] | 26 | void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 27 | { |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 28 | u64 nsecs = rlim_new * NSEC_PER_SEC; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 29 | |
Jiri Slaby | 5ab46b3 | 2009-08-28 14:05:12 +0200 | [diff] [blame] | 30 | spin_lock_irq(&task->sighand->siglock); |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 31 | set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL); |
Jiri Slaby | 5ab46b3 | 2009-08-28 14:05:12 +0200 | [diff] [blame] | 32 | spin_unlock_irq(&task->sighand->siglock); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 35 | static int check_clock(const clockid_t which_clock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | int error = 0; |
| 38 | struct task_struct *p; |
| 39 | const pid_t pid = CPUCLOCK_PID(which_clock); |
| 40 | |
| 41 | if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | if (pid == 0) |
| 45 | return 0; |
| 46 | |
Sergey Senozhatsky | c0deae8 | 2010-11-03 18:52:56 +0200 | [diff] [blame] | 47 | rcu_read_lock(); |
Pavel Emelyanov | 8dc86af | 2008-02-08 04:21:52 -0800 | [diff] [blame] | 48 | p = find_task_by_vpid(pid); |
Pavel Emelyanov | bac0abd6 | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 49 | if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ? |
Sergey Senozhatsky | c0deae8 | 2010-11-03 18:52:56 +0200 | [diff] [blame] | 50 | same_thread_group(p, current) : has_group_leader_pid(p))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | error = -EINVAL; |
| 52 | } |
Sergey Senozhatsky | c0deae8 | 2010-11-03 18:52:56 +0200 | [diff] [blame] | 53 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | return error; |
| 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Update expiry time from increment, and increase overrun count, |
| 60 | * given the current clock sample. |
| 61 | */ |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 62 | static void bump_cpu_timer(struct k_itimer *timer, u64 now) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | int i; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 65 | u64 delta, incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 67 | if (timer->it.cpu.incr == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return; |
| 69 | |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 70 | if (now < timer->it.cpu.expires) |
| 71 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 73 | incr = timer->it.cpu.incr; |
| 74 | delta = now + incr - timer->it.cpu.expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 76 | /* Don't use (incr*2 < delta), incr*2 might overflow. */ |
| 77 | for (i = 0; incr < delta - incr; i++) |
| 78 | incr = incr << 1; |
| 79 | |
| 80 | for (; i >= 0; incr >>= 1, i--) { |
| 81 | if (delta < incr) |
| 82 | continue; |
| 83 | |
| 84 | timer->it.cpu.expires += incr; |
| 85 | timer->it_overrun += 1 << i; |
| 86 | delta -= incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Frederic Weisbecker | 555347f | 2013-04-19 16:17:38 +0200 | [diff] [blame] | 90 | /** |
| 91 | * task_cputime_zero - Check a task_cputime struct for all zero fields. |
| 92 | * |
| 93 | * @cputime: The struct to compare. |
| 94 | * |
| 95 | * Checks @cputime to see if all fields are zero. Returns true if all fields |
| 96 | * are zero, false if any field is nonzero. |
| 97 | */ |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 98 | static inline int task_cputime_zero(const struct task_cputime *cputime) |
Frederic Weisbecker | 555347f | 2013-04-19 16:17:38 +0200 | [diff] [blame] | 99 | { |
| 100 | if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime) |
| 101 | return 1; |
| 102 | return 0; |
| 103 | } |
| 104 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 105 | static inline u64 prof_ticks(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 107 | u64 utime, stime; |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 108 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 109 | task_cputime(p, &utime, &stime); |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 110 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 111 | return utime + stime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 113 | static inline u64 virt_ticks(struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 115 | u64 utime, stime; |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 116 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 117 | task_cputime(p, &utime, &stime); |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 118 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 119 | return utime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Thomas Gleixner | bc2c8ea | 2011-02-01 13:52:12 +0000 | [diff] [blame] | 122 | static int |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 123 | posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | int error = check_clock(which_clock); |
| 126 | if (!error) { |
| 127 | tp->tv_sec = 0; |
| 128 | tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); |
| 129 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { |
| 130 | /* |
| 131 | * If sched_clock is using a cycle counter, we |
| 132 | * don't have any idea of its true resolution |
| 133 | * exported, but it is much more than 1s/HZ. |
| 134 | */ |
| 135 | tp->tv_nsec = 1; |
| 136 | } |
| 137 | } |
| 138 | return error; |
| 139 | } |
| 140 | |
Thomas Gleixner | bc2c8ea | 2011-02-01 13:52:12 +0000 | [diff] [blame] | 141 | static int |
Deepa Dinamani | 0fe6afe | 2017-03-26 12:04:16 -0700 | [diff] [blame] | 142 | posix_cpu_clock_set(const clockid_t which_clock, const struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | /* |
| 145 | * You can never reset a CPU clock, but we check for other errors |
| 146 | * in the call before failing with EPERM. |
| 147 | */ |
| 148 | int error = check_clock(which_clock); |
| 149 | if (error == 0) { |
| 150 | error = -EPERM; |
| 151 | } |
| 152 | return error; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /* |
| 157 | * Sample a per-thread clock for the given task. |
| 158 | */ |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 159 | static int cpu_clock_sample(const clockid_t which_clock, |
| 160 | struct task_struct *p, u64 *sample) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
| 162 | switch (CPUCLOCK_WHICH(which_clock)) { |
| 163 | default: |
| 164 | return -EINVAL; |
| 165 | case CPUCLOCK_PROF: |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 166 | *sample = prof_ticks(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | break; |
| 168 | case CPUCLOCK_VIRT: |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 169 | *sample = virt_ticks(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | break; |
| 171 | case CPUCLOCK_SCHED: |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 172 | *sample = task_sched_runtime(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 178 | /* |
| 179 | * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg |
| 180 | * to avoid race conditions with concurrent updates to cputime. |
| 181 | */ |
| 182 | static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime) |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 183 | { |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 184 | u64 curr_cputime; |
| 185 | retry: |
| 186 | curr_cputime = atomic64_read(cputime); |
| 187 | if (sum_cputime > curr_cputime) { |
| 188 | if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime) |
| 189 | goto retry; |
| 190 | } |
| 191 | } |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 192 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 193 | static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum) |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 194 | { |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 195 | __update_gt_cputime(&cputime_atomic->utime, sum->utime); |
| 196 | __update_gt_cputime(&cputime_atomic->stime, sum->stime); |
| 197 | __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime); |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 198 | } |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 199 | |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 200 | /* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */ |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 201 | static inline void sample_cputime_atomic(struct task_cputime *times, |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 202 | struct task_cputime_atomic *atomic_times) |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 203 | { |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 204 | times->utime = atomic64_read(&atomic_times->utime); |
| 205 | times->stime = atomic64_read(&atomic_times->stime); |
| 206 | times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime); |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 209 | void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times) |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 210 | { |
| 211 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 212 | struct task_cputime sum; |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 213 | |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 214 | /* Check if cputimer isn't running. This is accessed without locking. */ |
| 215 | if (!READ_ONCE(cputimer->running)) { |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 216 | /* |
| 217 | * The POSIX timer interface allows for absolute time expiry |
| 218 | * values through the TIMER_ABSTIME flag, therefore we have |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 219 | * to synchronize the timer to the clock every time we start it. |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 220 | */ |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 221 | thread_group_cputime(tsk, &sum); |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 222 | update_gt_cputime(&cputimer->cputime_atomic, &sum); |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * We're setting cputimer->running without a lock. Ensure |
| 226 | * this only gets written to in one operation. We set |
| 227 | * running after update_gt_cputime() as a small optimization, |
| 228 | * but barriers are not required because update_gt_cputime() |
| 229 | * can handle concurrent updates. |
| 230 | */ |
Jason Low | d5c373e | 2015-10-14 12:07:55 -0700 | [diff] [blame] | 231 | WRITE_ONCE(cputimer->running, true); |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 232 | } |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 233 | sample_cputime_atomic(times, &cputimer->cputime_atomic); |
Peter Zijlstra | 4da94d49 | 2009-02-11 11:30:27 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* |
| 237 | * Sample a process (thread group) clock for the given group_leader task. |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 238 | * Must be called with task sighand lock held for safe while_each_thread() |
| 239 | * traversal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 241 | static int cpu_clock_sample_group(const clockid_t which_clock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct task_struct *p, |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 243 | u64 *sample) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 245 | struct task_cputime cputime; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 246 | |
Petr Tesarik | eccdaea | 2008-11-24 15:46:31 +0100 | [diff] [blame] | 247 | switch (CPUCLOCK_WHICH(which_clock)) { |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 248 | default: |
| 249 | return -EINVAL; |
| 250 | case CPUCLOCK_PROF: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 251 | thread_group_cputime(p, &cputime); |
| 252 | *sample = cputime.utime + cputime.stime; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 253 | break; |
| 254 | case CPUCLOCK_VIRT: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 255 | thread_group_cputime(p, &cputime); |
| 256 | *sample = cputime.utime; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 257 | break; |
| 258 | case CPUCLOCK_SCHED: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 259 | thread_group_cputime(p, &cputime); |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 260 | *sample = cputime.sum_exec_runtime; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 261 | break; |
| 262 | } |
| 263 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 266 | static int posix_cpu_clock_get_task(struct task_struct *tsk, |
| 267 | const clockid_t which_clock, |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 268 | struct timespec64 *tp) |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 269 | { |
| 270 | int err = -EINVAL; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 271 | u64 rtn; |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 272 | |
| 273 | if (CPUCLOCK_PERTHREAD(which_clock)) { |
| 274 | if (same_thread_group(tsk, current)) |
| 275 | err = cpu_clock_sample(which_clock, tsk, &rtn); |
| 276 | } else { |
Frederic Weisbecker | 5087578 | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 277 | if (tsk == current || thread_group_leader(tsk)) |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 278 | err = cpu_clock_sample_group(which_clock, tsk, &rtn); |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (!err) |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 282 | *tp = ns_to_timespec64(rtn); |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 283 | |
| 284 | return err; |
| 285 | } |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 288 | static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | const pid_t pid = CPUCLOCK_PID(which_clock); |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 291 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | if (pid == 0) { |
| 294 | /* |
| 295 | * Special case constant value for our own clocks. |
| 296 | * We don't have to do any lookup to find ourselves. |
| 297 | */ |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 298 | err = posix_cpu_clock_get_task(current, which_clock, tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } else { |
| 300 | /* |
| 301 | * Find the given PID, and validate that the caller |
| 302 | * should be able to see it. |
| 303 | */ |
| 304 | struct task_struct *p; |
Paul E. McKenney | 1f2ea08 | 2007-02-16 01:28:22 -0800 | [diff] [blame] | 305 | rcu_read_lock(); |
Pavel Emelyanov | 8dc86af | 2008-02-08 04:21:52 -0800 | [diff] [blame] | 306 | p = find_task_by_vpid(pid); |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 307 | if (p) |
| 308 | err = posix_cpu_clock_get_task(p, which_clock, tp); |
Paul E. McKenney | 1f2ea08 | 2007-02-16 01:28:22 -0800 | [diff] [blame] | 309 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Frederic Weisbecker | 33ab0fe | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 312 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Validate the clockid_t for a new CPU-clock timer, and initialize the timer. |
Stanislaw Gruszka | ba5ea95 | 2009-11-17 14:14:13 -0800 | [diff] [blame] | 317 | * This is called from sys_timer_create() and do_cpu_nanosleep() with the |
| 318 | * new timer already all-zeros initialized. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | */ |
Thomas Gleixner | bc2c8ea | 2011-02-01 13:52:12 +0000 | [diff] [blame] | 320 | static int posix_cpu_timer_create(struct k_itimer *new_timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | int ret = 0; |
| 323 | const pid_t pid = CPUCLOCK_PID(new_timer->it_clock); |
| 324 | struct task_struct *p; |
| 325 | |
| 326 | if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX) |
| 327 | return -EINVAL; |
| 328 | |
Thomas Gleixner | d97bb75 | 2017-05-30 23:15:44 +0200 | [diff] [blame] | 329 | new_timer->kclock = &clock_posix_cpu; |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | INIT_LIST_HEAD(&new_timer->it.cpu.entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Sergey Senozhatsky | c0deae8 | 2010-11-03 18:52:56 +0200 | [diff] [blame] | 333 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) { |
| 335 | if (pid == 0) { |
| 336 | p = current; |
| 337 | } else { |
Pavel Emelyanov | 8dc86af | 2008-02-08 04:21:52 -0800 | [diff] [blame] | 338 | p = find_task_by_vpid(pid); |
Pavel Emelyanov | bac0abd6 | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 339 | if (p && !same_thread_group(p, current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | p = NULL; |
| 341 | } |
| 342 | } else { |
| 343 | if (pid == 0) { |
| 344 | p = current->group_leader; |
| 345 | } else { |
Pavel Emelyanov | 8dc86af | 2008-02-08 04:21:52 -0800 | [diff] [blame] | 346 | p = find_task_by_vpid(pid); |
Sergey Senozhatsky | c0deae8 | 2010-11-03 18:52:56 +0200 | [diff] [blame] | 347 | if (p && !has_group_leader_pid(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | p = NULL; |
| 349 | } |
| 350 | } |
| 351 | new_timer->it.cpu.task = p; |
| 352 | if (p) { |
| 353 | get_task_struct(p); |
| 354 | } else { |
| 355 | ret = -EINVAL; |
| 356 | } |
Sergey Senozhatsky | c0deae8 | 2010-11-03 18:52:56 +0200 | [diff] [blame] | 357 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Clean up a CPU-clock timer that is about to be destroyed. |
| 364 | * This is called from timer deletion with the timer already locked. |
| 365 | * If we return TIMER_RETRY, it's necessary to release the timer's lock |
| 366 | * and try again. (This happens when the timer is in the middle of firing.) |
| 367 | */ |
Thomas Gleixner | bc2c8ea | 2011-02-01 13:52:12 +0000 | [diff] [blame] | 368 | static int posix_cpu_timer_del(struct k_itimer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Oleg Nesterov | 108150e | 2005-10-23 20:25:39 +0400 | [diff] [blame] | 370 | int ret = 0; |
Frederic Weisbecker | 3d7a142 | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 371 | unsigned long flags; |
| 372 | struct sighand_struct *sighand; |
| 373 | struct task_struct *p = timer->it.cpu.task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 375 | WARN_ON_ONCE(p == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Frederic Weisbecker | 3d7a142 | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 377 | /* |
| 378 | * Protect against sighand release/switch in exit/exec and process/ |
| 379 | * thread timer list entry concurrent read/writes. |
| 380 | */ |
| 381 | sighand = lock_task_sighand(p, &flags); |
| 382 | if (unlikely(sighand == NULL)) { |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 383 | /* |
| 384 | * We raced with the reaping of the task. |
| 385 | * The deletion should have cleared us off the list. |
| 386 | */ |
Frederic Weisbecker | 531f64f | 2013-10-11 17:58:08 +0200 | [diff] [blame] | 387 | WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry)); |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 388 | } else { |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 389 | if (timer->it.cpu.firing) |
| 390 | ret = TIMER_RETRY; |
| 391 | else |
| 392 | list_del(&timer->it.cpu.entry); |
Frederic Weisbecker | 3d7a142 | 2013-10-11 17:41:11 +0200 | [diff] [blame] | 393 | |
| 394 | unlock_task_sighand(p, &flags); |
Oleg Nesterov | 108150e | 2005-10-23 20:25:39 +0400 | [diff] [blame] | 395 | } |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 396 | |
| 397 | if (!ret) |
| 398 | put_task_struct(p); |
Oleg Nesterov | 108150e | 2005-10-23 20:25:39 +0400 | [diff] [blame] | 399 | |
| 400 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Frederic Weisbecker | af82eb3 | 2013-10-11 16:11:43 +0200 | [diff] [blame] | 403 | static void cleanup_timers_list(struct list_head *head) |
Frederic Weisbecker | 1a7fa51 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 404 | { |
| 405 | struct cpu_timer_list *timer, *next; |
| 406 | |
Frederic Weisbecker | a0b2062 | 2013-06-28 00:06:43 +0000 | [diff] [blame] | 407 | list_for_each_entry_safe(timer, next, head, entry) |
Frederic Weisbecker | 1a7fa51 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 408 | list_del_init(&timer->entry); |
Frederic Weisbecker | 1a7fa51 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Clean out CPU timers still ticking when a thread exited. The task |
| 413 | * pointer is cleared, and the expiry time is replaced with the residual |
| 414 | * time for later timer_gettime calls to return. |
| 415 | * This must be called with the siglock held. |
| 416 | */ |
Frederic Weisbecker | af82eb3 | 2013-10-11 16:11:43 +0200 | [diff] [blame] | 417 | static void cleanup_timers(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
Frederic Weisbecker | af82eb3 | 2013-10-11 16:11:43 +0200 | [diff] [blame] | 419 | cleanup_timers_list(head); |
| 420 | cleanup_timers_list(++head); |
| 421 | cleanup_timers_list(++head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* |
| 425 | * These are both called with the siglock held, when the current thread |
| 426 | * is being reaped. When the final (leader) thread in the group is reaped, |
| 427 | * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit. |
| 428 | */ |
| 429 | void posix_cpu_timers_exit(struct task_struct *tsk) |
| 430 | { |
Frederic Weisbecker | af82eb3 | 2013-10-11 16:11:43 +0200 | [diff] [blame] | 431 | cleanup_timers(tsk->cpu_timers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | void posix_cpu_timers_exit_group(struct task_struct *tsk) |
| 434 | { |
Frederic Weisbecker | af82eb3 | 2013-10-11 16:11:43 +0200 | [diff] [blame] | 435 | cleanup_timers(tsk->signal->cpu_timers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 438 | static inline int expires_gt(u64 expires, u64 new_exp) |
Stanislaw Gruszka | d1e3b6d | 2009-07-29 12:15:28 +0200 | [diff] [blame] | 439 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 440 | return expires == 0 || expires > new_exp; |
Stanislaw Gruszka | d1e3b6d | 2009-07-29 12:15:28 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /* |
| 444 | * Insert the timer on the appropriate list before any timers that |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 445 | * expire later. This must be called with the sighand lock held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | */ |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 447 | static void arm_timer(struct k_itimer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
| 449 | struct task_struct *p = timer->it.cpu.task; |
| 450 | struct list_head *head, *listpos; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 451 | struct task_cputime *cputime_expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | struct cpu_timer_list *const nt = &timer->it.cpu; |
| 453 | struct cpu_timer_list *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 455 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
| 456 | head = p->cpu_timers; |
| 457 | cputime_expires = &p->cputime_expires; |
| 458 | } else { |
| 459 | head = p->signal->cpu_timers; |
| 460 | cputime_expires = &p->signal->cputime_expires; |
| 461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | head += CPUCLOCK_WHICH(timer->it_clock); |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | listpos = head; |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 465 | list_for_each_entry(next, head, entry) { |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 466 | if (nt->expires < next->expires) |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 467 | break; |
| 468 | listpos = &next->entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | list_add(&nt->entry, listpos); |
| 471 | |
| 472 | if (listpos == head) { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 473 | u64 exp = nt->expires; |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | /* |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 476 | * We are the new earliest-expiring POSIX 1.b timer, hence |
| 477 | * need to update expiration cache. Take into account that |
| 478 | * for process timers we share expiration cache with itimers |
| 479 | * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | */ |
| 481 | |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 482 | switch (CPUCLOCK_WHICH(timer->it_clock)) { |
| 483 | case CPUCLOCK_PROF: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 484 | if (expires_gt(cputime_expires->prof_exp, exp)) |
| 485 | cputime_expires->prof_exp = exp; |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 486 | break; |
| 487 | case CPUCLOCK_VIRT: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 488 | if (expires_gt(cputime_expires->virt_exp, exp)) |
| 489 | cputime_expires->virt_exp = exp; |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 490 | break; |
| 491 | case CPUCLOCK_SCHED: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 492 | if (expires_gt(cputime_expires->sched_exp, exp)) |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 493 | cputime_expires->sched_exp = exp; |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 494 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
Frederic Weisbecker | b787830 | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 496 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
| 497 | tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER); |
| 498 | else |
| 499 | tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* |
| 504 | * The timer is locked, fire it and arrange for its reload. |
| 505 | */ |
| 506 | static void cpu_timer_fire(struct k_itimer *timer) |
| 507 | { |
Stanislaw Gruszka | 1f169f8 | 2010-03-11 14:04:41 -0800 | [diff] [blame] | 508 | if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { |
| 509 | /* |
| 510 | * User don't want any signal. |
| 511 | */ |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 512 | timer->it.cpu.expires = 0; |
Stanislaw Gruszka | 1f169f8 | 2010-03-11 14:04:41 -0800 | [diff] [blame] | 513 | } else if (unlikely(timer->sigq == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | /* |
| 515 | * This a special case for clock_nanosleep, |
| 516 | * not a normal timer from sys_timer_create. |
| 517 | */ |
| 518 | wake_up_process(timer->it_process); |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 519 | timer->it.cpu.expires = 0; |
| 520 | } else if (timer->it.cpu.incr == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* |
| 522 | * One-shot timer. Clear it as soon as it's fired. |
| 523 | */ |
| 524 | posix_timer_event(timer, 0); |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 525 | timer->it.cpu.expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { |
| 527 | /* |
| 528 | * The signal did not get queued because the signal |
| 529 | * was ignored, so we won't get any callback to |
| 530 | * reload the timer. But we need to keep it |
| 531 | * ticking in case the signal is deliverable next time. |
| 532 | */ |
Thomas Gleixner | f37fb0a | 2017-05-30 23:15:47 +0200 | [diff] [blame] | 533 | posix_cpu_timer_rearm(timer); |
Thomas Gleixner | af888d6 | 2017-05-30 23:15:42 +0200 | [diff] [blame] | 534 | ++timer->it_requeue_pending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | /* |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 539 | * Sample a process (thread group) timer for the given group_leader task. |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 540 | * Must be called with task sighand lock held for safe while_each_thread() |
| 541 | * traversal. |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 542 | */ |
| 543 | static int cpu_timer_sample_group(const clockid_t which_clock, |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 544 | struct task_struct *p, u64 *sample) |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 545 | { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 546 | struct task_cputime cputime; |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 547 | |
| 548 | thread_group_cputimer(p, &cputime); |
| 549 | switch (CPUCLOCK_WHICH(which_clock)) { |
| 550 | default: |
| 551 | return -EINVAL; |
| 552 | case CPUCLOCK_PROF: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 553 | *sample = cputime.utime + cputime.stime; |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 554 | break; |
| 555 | case CPUCLOCK_VIRT: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 556 | *sample = cputime.utime; |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 557 | break; |
| 558 | case CPUCLOCK_SCHED: |
Peter Zijlstra | 23cfa36 | 2014-11-12 12:37:37 +0100 | [diff] [blame] | 559 | *sample = cputime.sum_exec_runtime; |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | * Guts of sys_timer_settime for CPU timers. |
| 567 | * This is called with the timer locked and interrupts disabled. |
| 568 | * If we return TIMER_RETRY, it's necessary to release the timer's lock |
| 569 | * and try again. (This happens when the timer is in the middle of firing.) |
| 570 | */ |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 571 | static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 572 | struct itimerspec64 *new, struct itimerspec64 *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | { |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 574 | unsigned long flags; |
| 575 | struct sighand_struct *sighand; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | struct task_struct *p = timer->it.cpu.task; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 577 | u64 old_expires, new_expires, old_incr, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | int ret; |
| 579 | |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 580 | WARN_ON_ONCE(p == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 582 | new_expires = timespec64_to_ns(&new->it_value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /* |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 585 | * Protect against sighand release/switch in exit/exec and p->cpu_timers |
| 586 | * and p->signal->cpu_timers read/write in arm_timer() |
| 587 | */ |
| 588 | sighand = lock_task_sighand(p, &flags); |
| 589 | /* |
| 590 | * If p has just been reaped, we can no |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | * longer get any information about it at all. |
| 592 | */ |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 593 | if (unlikely(sighand == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return -ESRCH; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Disarm any old timer after extracting its expiry time. |
| 599 | */ |
Frederic Weisbecker | 531f64f | 2013-10-11 17:58:08 +0200 | [diff] [blame] | 600 | WARN_ON_ONCE(!irqs_disabled()); |
Oleg Nesterov | a69ac4a | 2005-10-24 18:29:58 +0400 | [diff] [blame] | 601 | |
| 602 | ret = 0; |
Stanislaw Gruszka | ae1a78e | 2010-03-11 14:04:39 -0800 | [diff] [blame] | 603 | old_incr = timer->it.cpu.incr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | old_expires = timer->it.cpu.expires; |
Oleg Nesterov | a69ac4a | 2005-10-24 18:29:58 +0400 | [diff] [blame] | 605 | if (unlikely(timer->it.cpu.firing)) { |
| 606 | timer->it.cpu.firing = -1; |
| 607 | ret = TIMER_RETRY; |
| 608 | } else |
| 609 | list_del_init(&timer->it.cpu.entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | * We need to sample the current value to convert the new |
| 613 | * value from to relative and absolute, and to convert the |
| 614 | * old value from absolute to relative. To set a process |
| 615 | * timer, we need a sample to balance the thread expiry |
| 616 | * times (in arm_timer). With an absolute time, we must |
| 617 | * check if it's already passed. In short, we need a sample. |
| 618 | */ |
| 619 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
| 620 | cpu_clock_sample(timer->it_clock, p, &val); |
| 621 | } else { |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 622 | cpu_timer_sample_group(timer->it_clock, p, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | if (old) { |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 626 | if (old_expires == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | old->it_value.tv_sec = 0; |
| 628 | old->it_value.tv_nsec = 0; |
| 629 | } else { |
| 630 | /* |
| 631 | * Update the timer in case it has |
| 632 | * overrun already. If it has, |
| 633 | * we'll report it as having overrun |
| 634 | * and with the next reloaded timer |
| 635 | * already ticking, though we are |
| 636 | * swallowing that pending |
| 637 | * notification here to install the |
| 638 | * new setting. |
| 639 | */ |
| 640 | bump_cpu_timer(timer, val); |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 641 | if (val < timer->it.cpu.expires) { |
| 642 | old_expires = timer->it.cpu.expires - val; |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 643 | old->it_value = ns_to_timespec64(old_expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } else { |
| 645 | old->it_value.tv_nsec = 1; |
| 646 | old->it_value.tv_sec = 0; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Oleg Nesterov | a69ac4a | 2005-10-24 18:29:58 +0400 | [diff] [blame] | 651 | if (unlikely(ret)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | /* |
| 653 | * We are colliding with the timer actually firing. |
| 654 | * Punt after filling in the timer's old value, and |
| 655 | * disable this firing since we are already reporting |
| 656 | * it as an overrun (thanks to bump_cpu_timer above). |
| 657 | */ |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 658 | unlock_task_sighand(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | goto out; |
| 660 | } |
| 661 | |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 662 | if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) { |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 663 | new_expires += val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Install the new expiry time (or zero). |
| 668 | * For a timer with no notification action, we don't actually |
| 669 | * arm the timer (we'll just fake it for timer_gettime). |
| 670 | */ |
| 671 | timer->it.cpu.expires = new_expires; |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 672 | if (new_expires != 0 && val < new_expires) { |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 673 | arm_timer(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 676 | unlock_task_sighand(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /* |
| 678 | * Install the new reload setting, and |
| 679 | * set up the signal and overrun bookkeeping. |
| 680 | */ |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 681 | timer->it.cpu.incr = timespec64_to_ns(&new->it_interval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * This acts as a modification timestamp for the timer, |
| 685 | * so any automatic reload attempt will punt on seeing |
| 686 | * that we have reset the timer manually. |
| 687 | */ |
| 688 | timer->it_requeue_pending = (timer->it_requeue_pending + 2) & |
| 689 | ~REQUEUE_PENDING; |
| 690 | timer->it_overrun_last = 0; |
| 691 | timer->it_overrun = -1; |
| 692 | |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 693 | if (new_expires != 0 && !(val < new_expires)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | /* |
| 695 | * The designated time already passed, so we notify |
| 696 | * immediately, even if the thread never runs to |
| 697 | * accumulate more time on this clock. |
| 698 | */ |
| 699 | cpu_timer_fire(timer); |
| 700 | } |
| 701 | |
| 702 | ret = 0; |
| 703 | out: |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 704 | if (old) |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 705 | old->it_interval = ns_to_timespec64(old_incr); |
Frederic Weisbecker | b787830 | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return ret; |
| 708 | } |
| 709 | |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 710 | static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 712 | u64 now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | struct task_struct *p = timer->it.cpu.task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 715 | WARN_ON_ONCE(p == NULL); |
| 716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | /* |
| 718 | * Easy part: convert the reload time. |
| 719 | */ |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 720 | itp->it_interval = ns_to_timespec64(timer->it.cpu.incr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Thomas Gleixner | eabdec0 | 2017-05-30 23:15:51 +0200 | [diff] [blame^] | 722 | if (!timer->it.cpu.expires) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * Sample the clock to take the difference with the expiry time. |
| 727 | */ |
| 728 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
| 729 | cpu_clock_sample(timer->it_clock, p, &now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } else { |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 731 | struct sighand_struct *sighand; |
| 732 | unsigned long flags; |
| 733 | |
| 734 | /* |
| 735 | * Protect against sighand release/switch in exit/exec and |
| 736 | * also make timer sampling safe if it ends up calling |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 737 | * thread_group_cputime(). |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 738 | */ |
| 739 | sighand = lock_task_sighand(p, &flags); |
| 740 | if (unlikely(sighand == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | /* |
| 742 | * The process has been reaped. |
| 743 | * We can't even collect a sample any more. |
| 744 | * Call the timer disarmed, nothing else to do. |
| 745 | */ |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 746 | timer->it.cpu.expires = 0; |
Alexey Dobriyan | 2c13ce8 | 2016-07-08 01:39:11 +0300 | [diff] [blame] | 747 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } else { |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 749 | cpu_timer_sample_group(timer->it_clock, p, &now); |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 750 | unlock_task_sighand(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 754 | if (now < timer->it.cpu.expires) { |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 755 | itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } else { |
| 757 | /* |
| 758 | * The timer should have expired already, but the firing |
| 759 | * hasn't taken place yet. Say it's just about to expire. |
| 760 | */ |
| 761 | itp->it_value.tv_nsec = 1; |
| 762 | itp->it_value.tv_sec = 0; |
| 763 | } |
| 764 | } |
| 765 | |
Frederic Weisbecker | 2473f3e | 2013-06-28 00:06:43 +0000 | [diff] [blame] | 766 | static unsigned long long |
| 767 | check_timers_list(struct list_head *timers, |
| 768 | struct list_head *firing, |
| 769 | unsigned long long curr) |
| 770 | { |
| 771 | int maxfire = 20; |
| 772 | |
| 773 | while (!list_empty(timers)) { |
| 774 | struct cpu_timer_list *t; |
| 775 | |
| 776 | t = list_first_entry(timers, struct cpu_timer_list, entry); |
| 777 | |
| 778 | if (!--maxfire || curr < t->expires) |
| 779 | return t->expires; |
| 780 | |
| 781 | t->firing = 1; |
| 782 | list_move_tail(&t->entry, firing); |
| 783 | } |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | /* |
| 789 | * Check for any per-thread CPU timers that have fired and move them off |
| 790 | * the tsk->cpu_timers[N] list onto the firing list. Here we update the |
| 791 | * tsk->it_*_expires values to reflect the remaining thread CPU timers. |
| 792 | */ |
| 793 | static void check_thread_timers(struct task_struct *tsk, |
| 794 | struct list_head *firing) |
| 795 | { |
| 796 | struct list_head *timers = tsk->cpu_timers; |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 797 | struct signal_struct *const sig = tsk->signal; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 798 | struct task_cputime *tsk_expires = &tsk->cputime_expires; |
| 799 | u64 expires; |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 800 | unsigned long soft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
Jason Low | 934715a | 2015-10-14 12:07:54 -0700 | [diff] [blame] | 802 | /* |
| 803 | * If cputime_expires is zero, then there are no active |
| 804 | * per thread CPU timers. |
| 805 | */ |
| 806 | if (task_cputime_zero(&tsk->cputime_expires)) |
| 807 | return; |
| 808 | |
Frederic Weisbecker | 2473f3e | 2013-06-28 00:06:43 +0000 | [diff] [blame] | 809 | expires = check_timers_list(timers, firing, prof_ticks(tsk)); |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 810 | tsk_expires->prof_exp = expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Frederic Weisbecker | 2473f3e | 2013-06-28 00:06:43 +0000 | [diff] [blame] | 812 | expires = check_timers_list(++timers, firing, virt_ticks(tsk)); |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 813 | tsk_expires->virt_exp = expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Frederic Weisbecker | 2473f3e | 2013-06-28 00:06:43 +0000 | [diff] [blame] | 815 | tsk_expires->sched_exp = check_timers_list(++timers, firing, |
| 816 | tsk->se.sum_exec_runtime); |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 817 | |
| 818 | /* |
| 819 | * Check for the special case thread timers. |
| 820 | */ |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 821 | soft = READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur); |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 822 | if (soft != RLIM_INFINITY) { |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 823 | unsigned long hard = |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 824 | READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max); |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 825 | |
Peter Zijlstra | 5a52dd5 | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 826 | if (hard != RLIM_INFINITY && |
| 827 | tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) { |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 828 | /* |
| 829 | * At the hard limit, we just die. |
| 830 | * No need to calculate anything else now. |
| 831 | */ |
Thomas Gleixner | 43fe8b8 | 2017-05-23 23:27:38 +0200 | [diff] [blame] | 832 | if (print_fatal_signals) { |
| 833 | pr_info("CPU Watchdog Timeout (hard): %s[%d]\n", |
| 834 | tsk->comm, task_pid_nr(tsk)); |
| 835 | } |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 836 | __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); |
| 837 | return; |
| 838 | } |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 839 | if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) { |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 840 | /* |
| 841 | * At the soft limit, send a SIGXCPU every second. |
| 842 | */ |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 843 | if (soft < hard) { |
| 844 | soft += USEC_PER_SEC; |
| 845 | sig->rlim[RLIMIT_RTTIME].rlim_cur = soft; |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 846 | } |
Thomas Gleixner | 43fe8b8 | 2017-05-23 23:27:38 +0200 | [diff] [blame] | 847 | if (print_fatal_signals) { |
| 848 | pr_info("RT Watchdog Timeout (soft): %s[%d]\n", |
| 849 | tsk->comm, task_pid_nr(tsk)); |
| 850 | } |
Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 851 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
| 852 | } |
| 853 | } |
Frederic Weisbecker | b787830 | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 854 | if (task_cputime_zero(tsk_expires)) |
| 855 | tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 858 | static inline void stop_process_timers(struct signal_struct *sig) |
Peter Zijlstra | 3fccfd6 | 2009-02-10 16:37:31 +0100 | [diff] [blame] | 859 | { |
Stanislaw Gruszka | 15365c1 | 2010-03-11 14:04:31 -0800 | [diff] [blame] | 860 | struct thread_group_cputimer *cputimer = &sig->cputimer; |
Peter Zijlstra | 3fccfd6 | 2009-02-10 16:37:31 +0100 | [diff] [blame] | 861 | |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 862 | /* Turn off cputimer->running. This is done without locking. */ |
Jason Low | d5c373e | 2015-10-14 12:07:55 -0700 | [diff] [blame] | 863 | WRITE_ONCE(cputimer->running, false); |
Frederic Weisbecker | b787830 | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 864 | tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER); |
Peter Zijlstra | 3fccfd6 | 2009-02-10 16:37:31 +0100 | [diff] [blame] | 865 | } |
| 866 | |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 867 | static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 868 | u64 *expires, u64 cur_time, int signo) |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 869 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 870 | if (!it->expires) |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 871 | return; |
| 872 | |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 873 | if (cur_time >= it->expires) { |
| 874 | if (it->incr) |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 875 | it->expires += it->incr; |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 876 | else |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 877 | it->expires = 0; |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 878 | |
Xiao Guangrong | 3f0a525 | 2009-08-10 10:52:30 +0800 | [diff] [blame] | 879 | trace_itimer_expire(signo == SIGPROF ? |
| 880 | ITIMER_PROF : ITIMER_VIRTUAL, |
| 881 | tsk->signal->leader_pid, cur_time); |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 882 | __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); |
| 883 | } |
| 884 | |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 885 | if (it->expires && (!*expires || it->expires < *expires)) |
| 886 | *expires = it->expires; |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | /* |
| 890 | * Check for any per-thread CPU timers that have fired and move them |
| 891 | * off the tsk->*_timers list onto the firing list. Per-thread timers |
| 892 | * have already been taken off. |
| 893 | */ |
| 894 | static void check_process_timers(struct task_struct *tsk, |
| 895 | struct list_head *firing) |
| 896 | { |
| 897 | struct signal_struct *const sig = tsk->signal; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 898 | u64 utime, ptime, virt_expires, prof_expires; |
| 899 | u64 sum_sched_runtime, sched_expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | struct list_head *timers = sig->cpu_timers; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 901 | struct task_cputime cputime; |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 902 | unsigned long soft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* |
Jason Low | 934715a | 2015-10-14 12:07:54 -0700 | [diff] [blame] | 905 | * If cputimer is not running, then there are no active |
| 906 | * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU). |
| 907 | */ |
| 908 | if (!READ_ONCE(tsk->signal->cputimer.running)) |
| 909 | return; |
| 910 | |
Jason Low | c8d75aa | 2015-10-14 12:07:56 -0700 | [diff] [blame] | 911 | /* |
| 912 | * Signify that a thread is checking for process timers. |
| 913 | * Write access to this field is protected by the sighand lock. |
| 914 | */ |
| 915 | sig->cputimer.checking_timer = true; |
| 916 | |
Jason Low | 934715a | 2015-10-14 12:07:54 -0700 | [diff] [blame] | 917 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * Collect the current process totals. |
| 919 | */ |
Peter Zijlstra | 4cd4c1b | 2009-02-05 12:24:16 +0100 | [diff] [blame] | 920 | thread_group_cputimer(tsk, &cputime); |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 921 | utime = cputime.utime; |
| 922 | ptime = utime + cputime.stime; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 923 | sum_sched_runtime = cputime.sum_exec_runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Frederic Weisbecker | 2473f3e | 2013-06-28 00:06:43 +0000 | [diff] [blame] | 925 | prof_expires = check_timers_list(timers, firing, ptime); |
| 926 | virt_expires = check_timers_list(++timers, firing, utime); |
| 927 | sched_expires = check_timers_list(++timers, firing, sum_sched_runtime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * Check for the special case process timers. |
| 931 | */ |
Stanislaw Gruszka | 42c4ab4 | 2009-07-29 12:15:26 +0200 | [diff] [blame] | 932 | check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime, |
| 933 | SIGPROF); |
| 934 | check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, |
| 935 | SIGVTALRM); |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 936 | soft = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 937 | if (soft != RLIM_INFINITY) { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 938 | unsigned long psecs = div_u64(ptime, NSEC_PER_SEC); |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 939 | unsigned long hard = |
Jason Low | 316c1608d | 2015-04-28 13:00:20 -0700 | [diff] [blame] | 940 | READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_max); |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 941 | u64 x; |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 942 | if (psecs >= hard) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | /* |
| 944 | * At the hard limit, we just die. |
| 945 | * No need to calculate anything else now. |
| 946 | */ |
Thomas Gleixner | 43fe8b8 | 2017-05-23 23:27:38 +0200 | [diff] [blame] | 947 | if (print_fatal_signals) { |
| 948 | pr_info("RT Watchdog Timeout (hard): %s[%d]\n", |
| 949 | tsk->comm, task_pid_nr(tsk)); |
| 950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); |
| 952 | return; |
| 953 | } |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 954 | if (psecs >= soft) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | /* |
| 956 | * At the soft limit, send a SIGXCPU every second. |
| 957 | */ |
Thomas Gleixner | 43fe8b8 | 2017-05-23 23:27:38 +0200 | [diff] [blame] | 958 | if (print_fatal_signals) { |
| 959 | pr_info("CPU Watchdog Timeout (soft): %s[%d]\n", |
| 960 | tsk->comm, task_pid_nr(tsk)); |
| 961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
Jiri Slaby | d4bb5274 | 2010-03-05 13:42:53 -0800 | [diff] [blame] | 963 | if (soft < hard) { |
| 964 | soft++; |
| 965 | sig->rlim[RLIMIT_CPU].rlim_cur = soft; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 968 | x = soft * NSEC_PER_SEC; |
| 969 | if (!prof_expires || x < prof_expires) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | prof_expires = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 973 | sig->cputime_expires.prof_exp = prof_expires; |
| 974 | sig->cputime_expires.virt_exp = virt_expires; |
Stanislaw Gruszka | 29f87b7 | 2010-04-27 14:12:15 -0700 | [diff] [blame] | 975 | sig->cputime_expires.sched_exp = sched_expires; |
| 976 | if (task_cputime_zero(&sig->cputime_expires)) |
| 977 | stop_process_timers(sig); |
Jason Low | c8d75aa | 2015-10-14 12:07:56 -0700 | [diff] [blame] | 978 | |
| 979 | sig->cputimer.checking_timer = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /* |
Thomas Gleixner | 96fe3b0 | 2017-05-30 23:15:46 +0200 | [diff] [blame] | 983 | * This is called from the signal code (via posixtimer_rearm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | * when the last timer signal was delivered and we have to reload the timer. |
| 985 | */ |
Thomas Gleixner | f37fb0a | 2017-05-30 23:15:47 +0200 | [diff] [blame] | 986 | static void posix_cpu_timer_rearm(struct k_itimer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 988 | struct sighand_struct *sighand; |
| 989 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | struct task_struct *p = timer->it.cpu.task; |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 991 | u64 now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
Frederic Weisbecker | a3222f8 | 2013-10-11 00:37:39 +0200 | [diff] [blame] | 993 | WARN_ON_ONCE(p == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * Fetch the current sample and update the timer's expiry time. |
| 997 | */ |
| 998 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
| 999 | cpu_clock_sample(timer->it_clock, p, &now); |
| 1000 | bump_cpu_timer(timer, now); |
Frederic Weisbecker | 724a3713 | 2013-10-10 16:55:57 +0200 | [diff] [blame] | 1001 | if (unlikely(p->exit_state)) |
Thomas Gleixner | af888d6 | 2017-05-30 23:15:42 +0200 | [diff] [blame] | 1002 | return; |
Frederic Weisbecker | 724a3713 | 2013-10-10 16:55:57 +0200 | [diff] [blame] | 1003 | |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 1004 | /* Protect timer list r/w in arm_timer() */ |
| 1005 | sighand = lock_task_sighand(p, &flags); |
| 1006 | if (!sighand) |
Thomas Gleixner | af888d6 | 2017-05-30 23:15:42 +0200 | [diff] [blame] | 1007 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } else { |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * Protect arm_timer() and timer sampling in case of call to |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 1011 | * thread_group_cputime(). |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 1012 | */ |
| 1013 | sighand = lock_task_sighand(p, &flags); |
| 1014 | if (unlikely(sighand == NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | /* |
| 1016 | * The process has been reaped. |
| 1017 | * We can't even collect a sample any more. |
| 1018 | */ |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 1019 | timer->it.cpu.expires = 0; |
Thomas Gleixner | af888d6 | 2017-05-30 23:15:42 +0200 | [diff] [blame] | 1020 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } else if (unlikely(p->exit_state) && thread_group_empty(p)) { |
Thomas Gleixner | af888d6 | 2017-05-30 23:15:42 +0200 | [diff] [blame] | 1022 | /* If the process is dying, no need to rearm */ |
| 1023 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
Peter Zijlstra | 3997ad3 | 2009-02-12 15:00:52 +0100 | [diff] [blame] | 1025 | cpu_timer_sample_group(timer->it_clock, p, &now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | bump_cpu_timer(timer, now); |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 1027 | /* Leave the sighand locked for the call below. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * Now re-arm for the new expiry time. |
| 1032 | */ |
Frederic Weisbecker | 531f64f | 2013-10-11 17:58:08 +0200 | [diff] [blame] | 1033 | WARN_ON_ONCE(!irqs_disabled()); |
Stanislaw Gruszka | 5eb9aa6 | 2010-03-11 14:04:38 -0800 | [diff] [blame] | 1034 | arm_timer(timer); |
Thomas Gleixner | af888d6 | 2017-05-30 23:15:42 +0200 | [diff] [blame] | 1035 | unlock: |
Frederic Weisbecker | e73d84e | 2013-10-11 18:56:49 +0200 | [diff] [blame] | 1036 | unlock_task_sighand(p, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1039 | /** |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1040 | * task_cputime_expired - Compare two task_cputime entities. |
| 1041 | * |
| 1042 | * @sample: The task_cputime structure to be checked for expiration. |
| 1043 | * @expires: Expiration times, against which @sample will be checked. |
| 1044 | * |
| 1045 | * Checks @sample against @expires to see if any field of @sample has expired. |
| 1046 | * Returns true if any field of the former is greater than the corresponding |
| 1047 | * field of the latter if the latter field is set. Otherwise returns false. |
| 1048 | */ |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 1049 | static inline int task_cputime_expired(const struct task_cputime *sample, |
| 1050 | const struct task_cputime *expires) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1051 | { |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 1052 | if (expires->utime && sample->utime >= expires->utime) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1053 | return 1; |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 1054 | if (expires->stime && sample->utime + sample->stime >= expires->stime) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1055 | return 1; |
| 1056 | if (expires->sum_exec_runtime != 0 && |
| 1057 | sample->sum_exec_runtime >= expires->sum_exec_runtime) |
| 1058 | return 1; |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * fastpath_timer_check - POSIX CPU timers fast path. |
| 1064 | * |
| 1065 | * @tsk: The task (thread) being checked. |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1066 | * |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1067 | * Check the task and thread group timers. If both are zero (there are no |
| 1068 | * timers set) return false. Otherwise snapshot the task and thread group |
| 1069 | * timers and compare them with the corresponding expiration times. Return |
| 1070 | * true if a timer has expired, else return false. |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1071 | */ |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1072 | static inline int fastpath_timer_check(struct task_struct *tsk) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1073 | { |
Oleg Nesterov | ad133ba | 2008-11-17 15:39:47 +0100 | [diff] [blame] | 1074 | struct signal_struct *sig; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1075 | |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1076 | if (!task_cputime_zero(&tsk->cputime_expires)) { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 1077 | struct task_cputime task_sample; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1078 | |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 1079 | task_cputime(tsk, &task_sample.utime, &task_sample.stime); |
Jason Low | 7c177d9 | 2015-10-14 12:07:53 -0700 | [diff] [blame] | 1080 | task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1081 | if (task_cputime_expired(&task_sample, &tsk->cputime_expires)) |
| 1082 | return 1; |
| 1083 | } |
Oleg Nesterov | ad133ba | 2008-11-17 15:39:47 +0100 | [diff] [blame] | 1084 | |
| 1085 | sig = tsk->signal; |
Jason Low | c8d75aa | 2015-10-14 12:07:56 -0700 | [diff] [blame] | 1086 | /* |
| 1087 | * Check if thread group timers expired when the cputimer is |
| 1088 | * running and no other thread in the group is already checking |
| 1089 | * for thread group cputimers. These fields are read without the |
| 1090 | * sighand lock. However, this is fine because this is meant to |
| 1091 | * be a fastpath heuristic to determine whether we should try to |
| 1092 | * acquire the sighand lock to check/handle timers. |
| 1093 | * |
| 1094 | * In the worst case scenario, if 'running' or 'checking_timer' gets |
| 1095 | * set but the current thread doesn't see the change yet, we'll wait |
| 1096 | * until the next thread in the group gets a scheduler interrupt to |
| 1097 | * handle the timer. This isn't an issue in practice because these |
| 1098 | * types of delays with signals actually getting sent are expected. |
| 1099 | */ |
| 1100 | if (READ_ONCE(sig->cputimer.running) && |
| 1101 | !READ_ONCE(sig->cputimer.checking_timer)) { |
Frederic Weisbecker | ebd7e7f | 2017-01-31 04:09:34 +0100 | [diff] [blame] | 1102 | struct task_cputime group_sample; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1103 | |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 1104 | sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic); |
Oleg Nesterov | 8d1f431 | 2010-06-11 20:04:46 +0200 | [diff] [blame] | 1105 | |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1106 | if (task_cputime_expired(&group_sample, &sig->cputime_expires)) |
| 1107 | return 1; |
| 1108 | } |
Oleg Nesterov | 37bebc7 | 2009-03-23 20:34:11 +0100 | [diff] [blame] | 1109 | |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1110 | return 0; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * This is called from the timer interrupt handler. The irq handler has |
| 1115 | * already updated our counts. We need to check if any timers fire now. |
| 1116 | * Interrupts are disabled. |
| 1117 | */ |
| 1118 | void run_posix_cpu_timers(struct task_struct *tsk) |
| 1119 | { |
| 1120 | LIST_HEAD(firing); |
| 1121 | struct k_itimer *timer, *next; |
Oleg Nesterov | 0bdd2ed | 2010-06-11 01:10:18 +0200 | [diff] [blame] | 1122 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
Frederic Weisbecker | 531f64f | 2013-10-11 17:58:08 +0200 | [diff] [blame] | 1124 | WARN_ON_ONCE(!irqs_disabled()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | /* |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1127 | * The fast path checks that there are no expired thread or thread |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1128 | * group timers. If that's so, just return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | */ |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1130 | if (!fastpath_timer_check(tsk)) |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1131 | return; |
Ingo Molnar | 5ce73a4 | 2008-09-14 17:11:46 +0200 | [diff] [blame] | 1132 | |
Oleg Nesterov | 0bdd2ed | 2010-06-11 01:10:18 +0200 | [diff] [blame] | 1133 | if (!lock_task_sighand(tsk, &flags)) |
| 1134 | return; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1135 | /* |
| 1136 | * Here we take off tsk->signal->cpu_timers[N] and |
| 1137 | * tsk->cpu_timers[N] all the timers that are firing, and |
| 1138 | * put them on the firing list. |
| 1139 | */ |
| 1140 | check_thread_timers(tsk, &firing); |
Jason Low | 934715a | 2015-10-14 12:07:54 -0700 | [diff] [blame] | 1141 | |
| 1142 | check_process_timers(tsk, &firing); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1144 | /* |
| 1145 | * We must release these locks before taking any timer's lock. |
| 1146 | * There is a potential race with timer deletion here, as the |
| 1147 | * siglock now protects our private firing list. We have set |
| 1148 | * the firing flag in each timer, so that a deletion attempt |
| 1149 | * that gets the timer lock before we do will give it up and |
| 1150 | * spin until we've taken care of that timer below. |
| 1151 | */ |
Oleg Nesterov | 0bdd2ed | 2010-06-11 01:10:18 +0200 | [diff] [blame] | 1152 | unlock_task_sighand(tsk, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
| 1154 | /* |
| 1155 | * Now that all the timers on our list have the firing flag, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1156 | * no one will touch their list entries but us. We'll take |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | * each timer's lock before clearing its firing flag, so no |
| 1158 | * timer call will interfere. |
| 1159 | */ |
| 1160 | list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) { |
H Hartley Sweeten | 6e85c5b | 2009-04-29 19:14:32 -0400 | [diff] [blame] | 1161 | int cpu_firing; |
| 1162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | spin_lock(&timer->it_lock); |
| 1164 | list_del_init(&timer->it.cpu.entry); |
H Hartley Sweeten | 6e85c5b | 2009-04-29 19:14:32 -0400 | [diff] [blame] | 1165 | cpu_firing = timer->it.cpu.firing; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | timer->it.cpu.firing = 0; |
| 1167 | /* |
| 1168 | * The firing flag is -1 if we collided with a reset |
| 1169 | * of the timer, which already reported this |
| 1170 | * almost-firing as an overrun. So don't generate an event. |
| 1171 | */ |
H Hartley Sweeten | 6e85c5b | 2009-04-29 19:14:32 -0400 | [diff] [blame] | 1172 | if (likely(cpu_firing >= 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | cpu_timer_fire(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | spin_unlock(&timer->it_lock); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | /* |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1179 | * Set one of the process-wide special case CPU timers or RLIMIT_CPU. |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1180 | * The tsk->sighand->siglock must be held by the caller. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | */ |
| 1182 | void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1183 | u64 *newval, u64 *oldval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1185 | u64 now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Frederic Weisbecker | 531f64f | 2013-10-11 17:58:08 +0200 | [diff] [blame] | 1187 | WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED); |
Peter Zijlstra | 4cd4c1b | 2009-02-05 12:24:16 +0100 | [diff] [blame] | 1188 | cpu_timer_sample_group(clock_idx, tsk, &now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
| 1190 | if (oldval) { |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1191 | /* |
| 1192 | * We are setting itimer. The *oldval is absolute and we update |
| 1193 | * it to be relative, *newval argument is relative and we update |
| 1194 | * it to be absolute. |
| 1195 | */ |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 1196 | if (*oldval) { |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1197 | if (*oldval <= now) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | /* Just about to fire. */ |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1199 | *oldval = TICK_NSEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } else { |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1201 | *oldval -= now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 1205 | if (!*newval) |
Frederic Weisbecker | b787830 | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1206 | return; |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1207 | *newval += now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | /* |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1211 | * Update expiration cache if we are the earliest timer, or eventually |
| 1212 | * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | */ |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1214 | switch (clock_idx) { |
| 1215 | case CPUCLOCK_PROF: |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1216 | if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval)) |
| 1217 | tsk->signal->cputime_expires.prof_exp = *newval; |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1218 | break; |
| 1219 | case CPUCLOCK_VIRT: |
Frederic Weisbecker | 858cf3a | 2017-01-31 04:09:35 +0100 | [diff] [blame] | 1220 | if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval)) |
| 1221 | tsk->signal->cputime_expires.virt_exp = *newval; |
Stanislaw Gruszka | f55db60 | 2010-03-11 14:04:37 -0800 | [diff] [blame] | 1222 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
Frederic Weisbecker | b787830 | 2015-07-17 22:25:49 +0200 | [diff] [blame] | 1224 | |
| 1225 | tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1228 | static int do_cpu_nanosleep(const clockid_t which_clock, int flags, |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1229 | struct timespec64 *rqtp, struct itimerspec64 *it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | struct k_itimer timer; |
| 1232 | int error; |
| 1233 | |
| 1234 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | * Set up a temporary timer and then wait for it to go off. |
| 1236 | */ |
| 1237 | memset(&timer, 0, sizeof timer); |
| 1238 | spin_lock_init(&timer.it_lock); |
| 1239 | timer.it_clock = which_clock; |
| 1240 | timer.it_overrun = -1; |
| 1241 | error = posix_cpu_timer_create(&timer); |
| 1242 | timer.it_process = current; |
| 1243 | if (!error) { |
Deepa Dinamani | 5f252b3 | 2017-03-26 12:04:17 -0700 | [diff] [blame] | 1244 | static struct itimerspec64 zero_it; |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1245 | |
| 1246 | memset(it, 0, sizeof *it); |
| 1247 | it->it_value = *rqtp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | spin_lock_irq(&timer.it_lock); |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1250 | error = posix_cpu_timer_set(&timer, flags, it, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | if (error) { |
| 1252 | spin_unlock_irq(&timer.it_lock); |
| 1253 | return error; |
| 1254 | } |
| 1255 | |
| 1256 | while (!signal_pending(current)) { |
Frederic Weisbecker | 55ccb61 | 2013-06-28 00:06:42 +0000 | [diff] [blame] | 1257 | if (timer.it.cpu.expires == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | /* |
Stanislaw Gruszka | e6c42c2 | 2013-02-15 11:08:11 +0100 | [diff] [blame] | 1259 | * Our timer fired and was reset, below |
| 1260 | * deletion can not fail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | */ |
Stanislaw Gruszka | e6c42c2 | 2013-02-15 11:08:11 +0100 | [diff] [blame] | 1262 | posix_cpu_timer_del(&timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | spin_unlock_irq(&timer.it_lock); |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Block until cpu_timer_fire (or a signal) wakes us. |
| 1269 | */ |
| 1270 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1271 | spin_unlock_irq(&timer.it_lock); |
| 1272 | schedule(); |
| 1273 | spin_lock_irq(&timer.it_lock); |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * We were interrupted by a signal. |
| 1278 | */ |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1279 | *rqtp = ns_to_timespec64(timer.it.cpu.expires); |
| 1280 | error = posix_cpu_timer_set(&timer, 0, &zero_it, it); |
Stanislaw Gruszka | e6c42c2 | 2013-02-15 11:08:11 +0100 | [diff] [blame] | 1281 | if (!error) { |
| 1282 | /* |
| 1283 | * Timer is now unarmed, deletion can not fail. |
| 1284 | */ |
| 1285 | posix_cpu_timer_del(&timer); |
| 1286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | spin_unlock_irq(&timer.it_lock); |
| 1288 | |
Stanislaw Gruszka | e6c42c2 | 2013-02-15 11:08:11 +0100 | [diff] [blame] | 1289 | while (error == TIMER_RETRY) { |
| 1290 | /* |
| 1291 | * We need to handle case when timer was or is in the |
| 1292 | * middle of firing. In other cases we already freed |
| 1293 | * resources. |
| 1294 | */ |
| 1295 | spin_lock_irq(&timer.it_lock); |
| 1296 | error = posix_cpu_timer_del(&timer); |
| 1297 | spin_unlock_irq(&timer.it_lock); |
| 1298 | } |
| 1299 | |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1300 | if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | /* |
| 1302 | * It actually did fire already. |
| 1303 | */ |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1307 | error = -ERESTART_RESTARTBLOCK; |
| 1308 | } |
| 1309 | |
| 1310 | return error; |
| 1311 | } |
| 1312 | |
Thomas Gleixner | bc2c8ea | 2011-02-01 13:52:12 +0000 | [diff] [blame] | 1313 | static long posix_cpu_nsleep_restart(struct restart_block *restart_block); |
| 1314 | |
| 1315 | static int posix_cpu_nsleep(const clockid_t which_clock, int flags, |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1316 | struct timespec64 *rqtp, struct timespec __user *rmtp) |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1317 | { |
Andy Lutomirski | f56141e | 2015-02-12 15:01:14 -0800 | [diff] [blame] | 1318 | struct restart_block *restart_block = ¤t->restart_block; |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1319 | struct itimerspec64 it; |
| 1320 | struct timespec ts; |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1321 | int error; |
| 1322 | |
| 1323 | /* |
| 1324 | * Diagnose required errors first. |
| 1325 | */ |
| 1326 | if (CPUCLOCK_PERTHREAD(which_clock) && |
| 1327 | (CPUCLOCK_PID(which_clock) == 0 || |
Eric W. Biederman | 01a2197 | 2017-04-13 10:32:16 -0500 | [diff] [blame] | 1328 | CPUCLOCK_PID(which_clock) == task_pid_vnr(current))) |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1329 | return -EINVAL; |
| 1330 | |
| 1331 | error = do_cpu_nanosleep(which_clock, flags, rqtp, &it); |
| 1332 | |
| 1333 | if (error == -ERESTART_RESTARTBLOCK) { |
| 1334 | |
Thomas Gleixner | 3751f9f | 2011-02-01 13:51:20 +0000 | [diff] [blame] | 1335 | if (flags & TIMER_ABSTIME) |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1336 | return -ERESTARTNOHAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | /* |
Thomas Gleixner | 3751f9f | 2011-02-01 13:51:20 +0000 | [diff] [blame] | 1338 | * Report back to the user the time still remaining. |
| 1339 | */ |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1340 | ts = timespec64_to_timespec(it.it_value); |
| 1341 | if (rmtp && copy_to_user(rmtp, &ts, sizeof(*rmtp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | return -EFAULT; |
| 1343 | |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1344 | restart_block->fn = posix_cpu_nsleep_restart; |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1345 | restart_block->nanosleep.clockid = which_clock; |
Thomas Gleixner | 3751f9f | 2011-02-01 13:51:20 +0000 | [diff] [blame] | 1346 | restart_block->nanosleep.rmtp = rmtp; |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1347 | restart_block->nanosleep.expires = timespec64_to_ns(rqtp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | return error; |
| 1350 | } |
| 1351 | |
Thomas Gleixner | bc2c8ea | 2011-02-01 13:52:12 +0000 | [diff] [blame] | 1352 | static long posix_cpu_nsleep_restart(struct restart_block *restart_block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | { |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1354 | clockid_t which_clock = restart_block->nanosleep.clockid; |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1355 | struct itimerspec64 it; |
| 1356 | struct timespec64 t; |
| 1357 | struct timespec tmp; |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1358 | int error; |
Thomas Gleixner | 97735f2 | 2006-01-09 20:52:37 -0800 | [diff] [blame] | 1359 | |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1360 | t = ns_to_timespec64(restart_block->nanosleep.expires); |
Thomas Gleixner | 97735f2 | 2006-01-09 20:52:37 -0800 | [diff] [blame] | 1361 | |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1362 | error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it); |
| 1363 | |
| 1364 | if (error == -ERESTART_RESTARTBLOCK) { |
Thomas Gleixner | 3751f9f | 2011-02-01 13:51:20 +0000 | [diff] [blame] | 1365 | struct timespec __user *rmtp = restart_block->nanosleep.rmtp; |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1366 | /* |
Thomas Gleixner | 3751f9f | 2011-02-01 13:51:20 +0000 | [diff] [blame] | 1367 | * Report back to the user the time still remaining. |
| 1368 | */ |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1369 | tmp = timespec64_to_timespec(it.it_value); |
| 1370 | if (rmtp && copy_to_user(rmtp, &tmp, sizeof(*rmtp))) |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1371 | return -EFAULT; |
| 1372 | |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1373 | restart_block->nanosleep.expires = timespec64_to_ns(&t); |
Toyo Abe | e4b7655 | 2006-09-29 02:00:29 -0700 | [diff] [blame] | 1374 | } |
| 1375 | return error; |
| 1376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | #define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED) |
| 1380 | #define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED) |
| 1381 | |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 1382 | static int process_cpu_clock_getres(const clockid_t which_clock, |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 1383 | struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | { |
| 1385 | return posix_cpu_clock_getres(PROCESS_CLOCK, tp); |
| 1386 | } |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 1387 | static int process_cpu_clock_get(const clockid_t which_clock, |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 1388 | struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | { |
| 1390 | return posix_cpu_clock_get(PROCESS_CLOCK, tp); |
| 1391 | } |
| 1392 | static int process_cpu_timer_create(struct k_itimer *timer) |
| 1393 | { |
| 1394 | timer->it_clock = PROCESS_CLOCK; |
| 1395 | return posix_cpu_timer_create(timer); |
| 1396 | } |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 1397 | static int process_cpu_nsleep(const clockid_t which_clock, int flags, |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1398 | struct timespec64 *rqtp, |
Thomas Gleixner | 97735f2 | 2006-01-09 20:52:37 -0800 | [diff] [blame] | 1399 | struct timespec __user *rmtp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | { |
Thomas Gleixner | 97735f2 | 2006-01-09 20:52:37 -0800 | [diff] [blame] | 1401 | return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1403 | static long process_cpu_nsleep_restart(struct restart_block *restart_block) |
| 1404 | { |
| 1405 | return -EINVAL; |
| 1406 | } |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 1407 | static int thread_cpu_clock_getres(const clockid_t which_clock, |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 1408 | struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | { |
| 1410 | return posix_cpu_clock_getres(THREAD_CLOCK, tp); |
| 1411 | } |
Thomas Gleixner | a924b04 | 2006-01-09 20:52:27 -0800 | [diff] [blame] | 1412 | static int thread_cpu_clock_get(const clockid_t which_clock, |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 1413 | struct timespec64 *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
| 1415 | return posix_cpu_clock_get(THREAD_CLOCK, tp); |
| 1416 | } |
| 1417 | static int thread_cpu_timer_create(struct k_itimer *timer) |
| 1418 | { |
| 1419 | timer->it_clock = THREAD_CLOCK; |
| 1420 | return posix_cpu_timer_create(timer); |
| 1421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
Christoph Hellwig | d3ba5a9 | 2017-05-26 12:03:11 +0300 | [diff] [blame] | 1423 | const struct k_clock clock_posix_cpu = { |
Thomas Gleixner | 1976945 | 2011-02-01 13:51:06 +0000 | [diff] [blame] | 1424 | .clock_getres = posix_cpu_clock_getres, |
| 1425 | .clock_set = posix_cpu_clock_set, |
| 1426 | .clock_get = posix_cpu_clock_get, |
| 1427 | .timer_create = posix_cpu_timer_create, |
| 1428 | .nsleep = posix_cpu_nsleep, |
| 1429 | .nsleep_restart = posix_cpu_nsleep_restart, |
| 1430 | .timer_set = posix_cpu_timer_set, |
| 1431 | .timer_del = posix_cpu_timer_del, |
| 1432 | .timer_get = posix_cpu_timer_get, |
Thomas Gleixner | f37fb0a | 2017-05-30 23:15:47 +0200 | [diff] [blame] | 1433 | .timer_rearm = posix_cpu_timer_rearm, |
Thomas Gleixner | 1976945 | 2011-02-01 13:51:06 +0000 | [diff] [blame] | 1434 | }; |
| 1435 | |
Christoph Hellwig | d3ba5a9 | 2017-05-26 12:03:11 +0300 | [diff] [blame] | 1436 | const struct k_clock clock_process = { |
| 1437 | .clock_getres = process_cpu_clock_getres, |
| 1438 | .clock_get = process_cpu_clock_get, |
| 1439 | .timer_create = process_cpu_timer_create, |
| 1440 | .nsleep = process_cpu_nsleep, |
| 1441 | .nsleep_restart = process_cpu_nsleep_restart, |
| 1442 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Christoph Hellwig | d3ba5a9 | 2017-05-26 12:03:11 +0300 | [diff] [blame] | 1444 | const struct k_clock clock_thread = { |
| 1445 | .clock_getres = thread_cpu_clock_getres, |
| 1446 | .clock_get = thread_cpu_clock_get, |
| 1447 | .timer_create = thread_cpu_timer_create, |
| 1448 | }; |