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