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