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