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