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