1
2
3
4
5#include <linux/sched.h>
6#include <linux/posix-timers.h>
7#include <asm/uaccess.h>
8#include <linux/errno.h>
9
10static int check_clock(const 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 same_thread_group(p, current) : thread_group_leader(p))) {
26 error = -EINVAL;
27 }
28 read_unlock(&tasklist_lock);
29
30 return error;
31}
32
33static inline union cpu_time_count
34timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
35{
36 union cpu_time_count ret;
37 ret.sched = 0;
38 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
39 ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
40 } else {
41 ret.cpu = timespec_to_cputime(tp);
42 }
43 return ret;
44}
45
46static void sample_to_timespec(const 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
58static inline int cpu_time_before(const 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}
68static inline void cpu_time_add(const 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}
78static inline union cpu_time_count cpu_time_sub(const 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
92
93
94
95
96static inline cputime_t cputime_div_non_zero(cputime_t time, unsigned long div)
97{
98 cputime_t res = cputime_div(time, div);
99
100 return max_t(cputime_t, res, 1);
101}
102
103
104
105
106
107static void bump_cpu_timer(struct k_itimer *timer,
108 union cpu_time_count now)
109{
110 int i;
111
112 if (timer->it.cpu.incr.sched == 0)
113 return;
114
115 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
116 unsigned long long delta, incr;
117
118 if (now.sched < timer->it.cpu.expires.sched)
119 return;
120 incr = timer->it.cpu.incr.sched;
121 delta = now.sched + incr - timer->it.cpu.expires.sched;
122
123 for (i = 0; incr < delta - incr; i++)
124 incr = incr << 1;
125 for (; i >= 0; incr >>= 1, i--) {
126 if (delta < incr)
127 continue;
128 timer->it.cpu.expires.sched += incr;
129 timer->it_overrun += 1 << i;
130 delta -= incr;
131 }
132 } else {
133 cputime_t delta, incr;
134
135 if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
136 return;
137 incr = timer->it.cpu.incr.cpu;
138 delta = cputime_sub(cputime_add(now.cpu, incr),
139 timer->it.cpu.expires.cpu);
140
141 for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
142 incr = cputime_add(incr, incr);
143 for (; i >= 0; incr = cputime_halve(incr), i--) {
144 if (cputime_lt(delta, incr))
145 continue;
146 timer->it.cpu.expires.cpu =
147 cputime_add(timer->it.cpu.expires.cpu, incr);
148 timer->it_overrun += 1 << i;
149 delta = cputime_sub(delta, incr);
150 }
151 }
152}
153
154static inline cputime_t prof_ticks(struct task_struct *p)
155{
156 return cputime_add(p->utime, p->stime);
157}
158static inline cputime_t virt_ticks(struct task_struct *p)
159{
160 return p->utime;
161}
162static inline unsigned long long sched_ns(struct task_struct *p)
163{
164 return task_sched_runtime(p);
165}
166
167int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
168{
169 int error = check_clock(which_clock);
170 if (!error) {
171 tp->tv_sec = 0;
172 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
173 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
174
175
176
177
178
179 tp->tv_nsec = 1;
180 }
181 }
182 return error;
183}
184
185int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
186{
187
188
189
190
191 int error = check_clock(which_clock);
192 if (error == 0) {
193 error = -EPERM;
194 }
195 return error;
196}
197
198
199
200
201
202static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
203 union cpu_time_count *cpu)
204{
205 switch (CPUCLOCK_WHICH(which_clock)) {
206 default:
207 return -EINVAL;
208 case CPUCLOCK_PROF:
209 cpu->cpu = prof_ticks(p);
210 break;
211 case CPUCLOCK_VIRT:
212 cpu->cpu = virt_ticks(p);
213 break;
214 case CPUCLOCK_SCHED:
215 cpu->sched = sched_ns(p);
216 break;
217 }
218 return 0;
219}
220
221
222
223
224
225
226static int cpu_clock_sample_group_locked(unsigned int clock_idx,
227 struct task_struct *p,
228 union cpu_time_count *cpu)
229{
230 struct task_struct *t = p;
231 switch (clock_idx) {
232 default:
233 return -EINVAL;
234 case CPUCLOCK_PROF:
235 cpu->cpu = cputime_add(p->signal->utime, p->signal->stime);
236 do {
237 cpu->cpu = cputime_add(cpu->cpu, prof_ticks(t));
238 t = next_thread(t);
239 } while (t != p);
240 break;
241 case CPUCLOCK_VIRT:
242 cpu->cpu = p->signal->utime;
243 do {
244 cpu->cpu = cputime_add(cpu->cpu, virt_ticks(t));
245 t = next_thread(t);
246 } while (t != p);
247 break;
248 case CPUCLOCK_SCHED:
249 cpu->sched = p->signal->sum_sched_runtime;
250
251 while ((t = next_thread(t)) != p) {
252 cpu->sched += t->se.sum_exec_runtime;
253 }
254 cpu->sched += sched_ns(p);
255 break;
256 }
257 return 0;
258}
259
260
261
262
263
264static int cpu_clock_sample_group(const clockid_t which_clock,
265 struct task_struct *p,
266 union cpu_time_count *cpu)
267{
268 int ret;
269 unsigned long flags;
270 spin_lock_irqsave(&p->sighand->siglock, flags);
271 ret = cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock), p,
272 cpu);
273 spin_unlock_irqrestore(&p->sighand->siglock, flags);
274 return ret;
275}
276
277
278int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
279{
280 const pid_t pid = CPUCLOCK_PID(which_clock);
281 int error = -EINVAL;
282 union cpu_time_count rtn;
283
284 if (pid == 0) {
285
286
287
288
289 if (CPUCLOCK_PERTHREAD(which_clock)) {
290
291
292
293 error = cpu_clock_sample(which_clock,
294 current, &rtn);
295 } else {
296 read_lock(&tasklist_lock);
297 error = cpu_clock_sample_group(which_clock,
298 current, &rtn);
299 read_unlock(&tasklist_lock);
300 }
301 } else {
302
303
304
305
306 struct task_struct *p;
307 rcu_read_lock();
308 p = find_task_by_pid(pid);
309 if (p) {
310 if (CPUCLOCK_PERTHREAD(which_clock)) {
311 if (same_thread_group(p, current)) {
312 error = cpu_clock_sample(which_clock,
313 p, &rtn);
314 }
315 } else {
316 read_lock(&tasklist_lock);
317 if (thread_group_leader(p) && p->signal) {
318 error =
319 cpu_clock_sample_group(which_clock,
320 p, &rtn);
321 }
322 read_unlock(&tasklist_lock);
323 }
324 }
325 rcu_read_unlock();
326 }
327
328 if (error)
329 return error;
330 sample_to_timespec(which_clock, rtn, tp);
331 return 0;
332}
333
334
335
336
337
338
339int posix_cpu_timer_create(struct k_itimer *new_timer)
340{
341 int ret = 0;
342 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
343 struct task_struct *p;
344
345 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
346 return -EINVAL;
347
348 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
349 new_timer->it.cpu.incr.sched = 0;
350 new_timer->it.cpu.expires.sched = 0;
351
352 read_lock(&tasklist_lock);
353 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
354 if (pid == 0) {
355 p = current;
356 } else {
357 p = find_task_by_pid(pid);
358 if (p && !same_thread_group(p, current))
359 p = NULL;
360 }
361 } else {
362 if (pid == 0) {
363 p = current->group_leader;
364 } else {
365 p = find_task_by_pid(pid);
366 if (p && !thread_group_leader(p))
367 p = NULL;
368 }
369 }
370 new_timer->it.cpu.task = p;
371 if (p) {
372 get_task_struct(p);
373 } else {
374 ret = -EINVAL;
375 }
376 read_unlock(&tasklist_lock);
377
378 return ret;
379}
380
381
382
383
384
385
386
387int posix_cpu_timer_del(struct k_itimer *timer)
388{
389 struct task_struct *p = timer->it.cpu.task;
390 int ret = 0;
391
392 if (likely(p != NULL)) {
393 read_lock(&tasklist_lock);
394 if (unlikely(p->signal == NULL)) {
395
396
397
398
399 BUG_ON(!list_empty(&timer->it.cpu.entry));
400 } else {
401 spin_lock(&p->sighand->siglock);
402 if (timer->it.cpu.firing)
403 ret = TIMER_RETRY;
404 else
405 list_del(&timer->it.cpu.entry);
406 spin_unlock(&p->sighand->siglock);
407 }
408 read_unlock(&tasklist_lock);
409
410 if (!ret)
411 put_task_struct(p);
412 }
413
414 return ret;
415}
416
417
418
419
420
421
422
423static void cleanup_timers(struct list_head *head,
424 cputime_t utime, cputime_t stime,
425 unsigned long long sum_exec_runtime)
426{
427 struct cpu_timer_list *timer, *next;
428 cputime_t ptime = cputime_add(utime, stime);
429
430 list_for_each_entry_safe(timer, next, head, entry) {
431 list_del_init(&timer->entry);
432 if (cputime_lt(timer->expires.cpu, ptime)) {
433 timer->expires.cpu = cputime_zero;
434 } else {
435 timer->expires.cpu = cputime_sub(timer->expires.cpu,
436 ptime);
437 }
438 }
439
440 ++head;
441 list_for_each_entry_safe(timer, next, head, entry) {
442 list_del_init(&timer->entry);
443 if (cputime_lt(timer->expires.cpu, utime)) {
444 timer->expires.cpu = cputime_zero;
445 } else {
446 timer->expires.cpu = cputime_sub(timer->expires.cpu,
447 utime);
448 }
449 }
450
451 ++head;
452 list_for_each_entry_safe(timer, next, head, entry) {
453 list_del_init(&timer->entry);
454 if (timer->expires.sched < sum_exec_runtime) {
455 timer->expires.sched = 0;
456 } else {
457 timer->expires.sched -= sum_exec_runtime;
458 }
459 }
460}
461
462
463
464
465
466
467void posix_cpu_timers_exit(struct task_struct *tsk)
468{
469 cleanup_timers(tsk->cpu_timers,
470 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
471
472}
473void posix_cpu_timers_exit_group(struct task_struct *tsk)
474{
475 cleanup_timers(tsk->signal->cpu_timers,
476 cputime_add(tsk->utime, tsk->signal->utime),
477 cputime_add(tsk->stime, tsk->signal->stime),
478 tsk->se.sum_exec_runtime + tsk->signal->sum_sched_runtime);
479}
480
481
482
483
484
485
486static void process_timer_rebalance(struct task_struct *p,
487 unsigned int clock_idx,
488 union cpu_time_count expires,
489 union cpu_time_count val)
490{
491 cputime_t ticks, left;
492 unsigned long long ns, nsleft;
493 struct task_struct *t = p;
494 unsigned int nthreads = atomic_read(&p->signal->live);
495
496 if (!nthreads)
497 return;
498
499 switch (clock_idx) {
500 default:
501 BUG();
502 break;
503 case CPUCLOCK_PROF:
504 left = cputime_div_non_zero(cputime_sub(expires.cpu, val.cpu),
505 nthreads);
506 do {
507 if (likely(!(t->flags & PF_EXITING))) {
508 ticks = cputime_add(prof_ticks(t), left);
509 if (cputime_eq(t->it_prof_expires,
510 cputime_zero) ||
511 cputime_gt(t->it_prof_expires, ticks)) {
512 t->it_prof_expires = ticks;
513 }
514 }
515 t = next_thread(t);
516 } while (t != p);
517 break;
518 case CPUCLOCK_VIRT:
519 left = cputime_div_non_zero(cputime_sub(expires.cpu, val.cpu),
520 nthreads);
521 do {
522 if (likely(!(t->flags & PF_EXITING))) {
523 ticks = cputime_add(virt_ticks(t), left);
524 if (cputime_eq(t->it_virt_expires,
525 cputime_zero) ||
526 cputime_gt(t->it_virt_expires, ticks)) {
527 t->it_virt_expires = ticks;
528 }
529 }
530 t = next_thread(t);
531 } while (t != p);
532 break;
533 case CPUCLOCK_SCHED:
534 nsleft = expires.sched - val.sched;
535 do_div(nsleft, nthreads);
536 nsleft = max_t(unsigned long long, nsleft, 1);
537 do {
538 if (likely(!(t->flags & PF_EXITING))) {
539 ns = t->se.sum_exec_runtime + nsleft;
540 if (t->it_sched_expires == 0 ||
541 t->it_sched_expires > ns) {
542 t->it_sched_expires = ns;
543 }
544 }
545 t = next_thread(t);
546 } while (t != p);
547 break;
548 }
549}
550
551static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
552{
553
554
555
556
557 put_task_struct(timer->it.cpu.task);
558 timer->it.cpu.task = NULL;
559 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
560 timer->it.cpu.expires,
561 now);
562}
563
564
565
566
567
568
569static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
570{
571 struct task_struct *p = timer->it.cpu.task;
572 struct list_head *head, *listpos;
573 struct cpu_timer_list *const nt = &timer->it.cpu;
574 struct cpu_timer_list *next;
575 unsigned long i;
576
577 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
578 p->cpu_timers : p->signal->cpu_timers);
579 head += CPUCLOCK_WHICH(timer->it_clock);
580
581 BUG_ON(!irqs_disabled());
582 spin_lock(&p->sighand->siglock);
583
584 listpos = head;
585 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
586 list_for_each_entry(next, head, entry) {
587 if (next->expires.sched > nt->expires.sched)
588 break;
589 listpos = &next->entry;
590 }
591 } else {
592 list_for_each_entry(next, head, entry) {
593 if (cputime_gt(next->expires.cpu, nt->expires.cpu))
594 break;
595 listpos = &next->entry;
596 }
597 }
598 list_add(&nt->entry, listpos);
599
600 if (listpos == head) {
601
602
603
604
605
606
607 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
608 switch (CPUCLOCK_WHICH(timer->it_clock)) {
609 default:
610 BUG();
611 case CPUCLOCK_PROF:
612 if (cputime_eq(p->it_prof_expires,
613 cputime_zero) ||
614 cputime_gt(p->it_prof_expires,
615 nt->expires.cpu))
616 p->it_prof_expires = nt->expires.cpu;
617 break;
618 case CPUCLOCK_VIRT:
619 if (cputime_eq(p->it_virt_expires,
620 cputime_zero) ||
621 cputime_gt(p->it_virt_expires,
622 nt->expires.cpu))
623 p->it_virt_expires = nt->expires.cpu;
624 break;
625 case CPUCLOCK_SCHED:
626 if (p->it_sched_expires == 0 ||
627 p->it_sched_expires > nt->expires.sched)
628 p->it_sched_expires = nt->expires.sched;
629 break;
630 }
631 } else {
632
633
634
635
636 switch (CPUCLOCK_WHICH(timer->it_clock)) {
637 default:
638 BUG();
639 case CPUCLOCK_VIRT:
640 if (!cputime_eq(p->signal->it_virt_expires,
641 cputime_zero) &&
642 cputime_lt(p->signal->it_virt_expires,
643 timer->it.cpu.expires.cpu))
644 break;
645 goto rebalance;
646 case CPUCLOCK_PROF:
647 if (!cputime_eq(p->signal->it_prof_expires,
648 cputime_zero) &&
649 cputime_lt(p->signal->it_prof_expires,
650 timer->it.cpu.expires.cpu))
651 break;
652 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
653 if (i != RLIM_INFINITY &&
654 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
655 break;
656 goto rebalance;
657 case CPUCLOCK_SCHED:
658 rebalance:
659 process_timer_rebalance(
660 timer->it.cpu.task,
661 CPUCLOCK_WHICH(timer->it_clock),
662 timer->it.cpu.expires, now);
663 break;
664 }
665 }
666 }
667
668 spin_unlock(&p->sighand->siglock);
669}
670
671
672
673
674static void cpu_timer_fire(struct k_itimer *timer)
675{
676 if (unlikely(timer->sigq == NULL)) {
677
678
679
680
681 wake_up_process(timer->it_process);
682 timer->it.cpu.expires.sched = 0;
683 } else if (timer->it.cpu.incr.sched == 0) {
684
685
686
687 posix_timer_event(timer, 0);
688 timer->it.cpu.expires.sched = 0;
689 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
690
691
692
693
694
695
696 posix_cpu_timer_schedule(timer);
697 }
698}
699
700
701
702
703
704
705
706int posix_cpu_timer_set(struct k_itimer *timer, int flags,
707 struct itimerspec *new, struct itimerspec *old)
708{
709 struct task_struct *p = timer->it.cpu.task;
710 union cpu_time_count old_expires, new_expires, val;
711 int ret;
712
713 if (unlikely(p == NULL)) {
714
715
716
717 return -ESRCH;
718 }
719
720 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
721
722 read_lock(&tasklist_lock);
723
724
725
726
727
728 if (unlikely(p->signal == NULL)) {
729 read_unlock(&tasklist_lock);
730 put_task_struct(p);
731 timer->it.cpu.task = NULL;
732 return -ESRCH;
733 }
734
735
736
737
738 BUG_ON(!irqs_disabled());
739
740 ret = 0;
741 spin_lock(&p->sighand->siglock);
742 old_expires = timer->it.cpu.expires;
743 if (unlikely(timer->it.cpu.firing)) {
744 timer->it.cpu.firing = -1;
745 ret = TIMER_RETRY;
746 } else
747 list_del_init(&timer->it.cpu.entry);
748 spin_unlock(&p->sighand->siglock);
749
750
751
752
753
754
755
756
757
758 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
759 cpu_clock_sample(timer->it_clock, p, &val);
760 } else {
761 cpu_clock_sample_group(timer->it_clock, p, &val);
762 }
763
764 if (old) {
765 if (old_expires.sched == 0) {
766 old->it_value.tv_sec = 0;
767 old->it_value.tv_nsec = 0;
768 } else {
769
770
771
772
773
774
775
776
777
778
779 bump_cpu_timer(timer, val);
780 if (cpu_time_before(timer->it_clock, val,
781 timer->it.cpu.expires)) {
782 old_expires = cpu_time_sub(
783 timer->it_clock,
784 timer->it.cpu.expires, val);
785 sample_to_timespec(timer->it_clock,
786 old_expires,
787 &old->it_value);
788 } else {
789 old->it_value.tv_nsec = 1;
790 old->it_value.tv_sec = 0;
791 }
792 }
793 }
794
795 if (unlikely(ret)) {
796
797
798
799
800
801
802 read_unlock(&tasklist_lock);
803 goto out;
804 }
805
806 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
807 cpu_time_add(timer->it_clock, &new_expires, val);
808 }
809
810
811
812
813
814
815 timer->it.cpu.expires = new_expires;
816 if (new_expires.sched != 0 &&
817 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
818 cpu_time_before(timer->it_clock, val, new_expires)) {
819 arm_timer(timer, val);
820 }
821
822 read_unlock(&tasklist_lock);
823
824
825
826
827
828 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
829 &new->it_interval);
830
831
832
833
834
835
836 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
837 ~REQUEUE_PENDING;
838 timer->it_overrun_last = 0;
839 timer->it_overrun = -1;
840
841 if (new_expires.sched != 0 &&
842 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
843 !cpu_time_before(timer->it_clock, val, new_expires)) {
844
845
846
847
848
849 cpu_timer_fire(timer);
850 }
851
852 ret = 0;
853 out:
854 if (old) {
855 sample_to_timespec(timer->it_clock,
856 timer->it.cpu.incr, &old->it_interval);
857 }
858 return ret;
859}
860
861void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
862{
863 union cpu_time_count now;
864 struct task_struct *p = timer->it.cpu.task;
865 int clear_dead;
866
867
868
869
870 sample_to_timespec(timer->it_clock,
871 timer->it.cpu.incr, &itp->it_interval);
872
873 if (timer->it.cpu.expires.sched == 0) {
874 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
875 return;
876 }
877
878 if (unlikely(p == NULL)) {
879
880
881
882
883 dead:
884 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
885 &itp->it_value);
886 return;
887 }
888
889
890
891
892 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
893 cpu_clock_sample(timer->it_clock, p, &now);
894 clear_dead = p->exit_state;
895 } else {
896 read_lock(&tasklist_lock);
897 if (unlikely(p->signal == NULL)) {
898
899
900
901
902
903 put_task_struct(p);
904 timer->it.cpu.task = NULL;
905 timer->it.cpu.expires.sched = 0;
906 read_unlock(&tasklist_lock);
907 goto dead;
908 } else {
909 cpu_clock_sample_group(timer->it_clock, p, &now);
910 clear_dead = (unlikely(p->exit_state) &&
911 thread_group_empty(p));
912 }
913 read_unlock(&tasklist_lock);
914 }
915
916 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
917 if (timer->it.cpu.incr.sched == 0 &&
918 cpu_time_before(timer->it_clock,
919 timer->it.cpu.expires, now)) {
920
921
922
923
924 timer->it.cpu.expires.sched = 0;
925 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
926 return;
927 }
928
929
930
931
932 bump_cpu_timer(timer, now);
933 }
934
935 if (unlikely(clear_dead)) {
936
937
938
939
940
941 clear_dead_task(timer, now);
942 goto dead;
943 }
944
945 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
946 sample_to_timespec(timer->it_clock,
947 cpu_time_sub(timer->it_clock,
948 timer->it.cpu.expires, now),
949 &itp->it_value);
950 } else {
951
952
953
954
955 itp->it_value.tv_nsec = 1;
956 itp->it_value.tv_sec = 0;
957 }
958}
959
960
961
962
963
964
965static void check_thread_timers(struct task_struct *tsk,
966 struct list_head *firing)
967{
968 int maxfire;
969 struct list_head *timers = tsk->cpu_timers;
970
971 maxfire = 20;
972 tsk->it_prof_expires = cputime_zero;
973 while (!list_empty(timers)) {
974 struct cpu_timer_list *t = list_first_entry(timers,
975 struct cpu_timer_list,
976 entry);
977 if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
978 tsk->it_prof_expires = t->expires.cpu;
979 break;
980 }
981 t->firing = 1;
982 list_move_tail(&t->entry, firing);
983 }
984
985 ++timers;
986 maxfire = 20;
987 tsk->it_virt_expires = cputime_zero;
988 while (!list_empty(timers)) {
989 struct cpu_timer_list *t = list_first_entry(timers,
990 struct cpu_timer_list,
991 entry);
992 if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
993 tsk->it_virt_expires = t->expires.cpu;
994 break;
995 }
996 t->firing = 1;
997 list_move_tail(&t->entry, firing);
998 }
999
1000 ++timers;
1001 maxfire = 20;
1002 tsk->it_sched_expires = 0;
1003 while (!list_empty(timers)) {
1004 struct cpu_timer_list *t = list_first_entry(timers,
1005 struct cpu_timer_list,
1006 entry);
1007 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
1008 tsk->it_sched_expires = t->expires.sched;
1009 break;
1010 }
1011 t->firing = 1;
1012 list_move_tail(&t->entry, firing);
1013 }
1014}
1015
1016
1017
1018
1019
1020
1021static void check_process_timers(struct task_struct *tsk,
1022 struct list_head *firing)
1023{
1024 int maxfire;
1025 struct signal_struct *const sig = tsk->signal;
1026 cputime_t utime, stime, ptime, virt_expires, prof_expires;
1027 unsigned long long sum_sched_runtime, sched_expires;
1028 struct task_struct *t;
1029 struct list_head *timers = sig->cpu_timers;
1030
1031
1032
1033
1034 if (list_empty(&timers[CPUCLOCK_PROF]) &&
1035 cputime_eq(sig->it_prof_expires, cputime_zero) &&
1036 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1037 list_empty(&timers[CPUCLOCK_VIRT]) &&
1038 cputime_eq(sig->it_virt_expires, cputime_zero) &&
1039 list_empty(&timers[CPUCLOCK_SCHED]))
1040 return;
1041
1042
1043
1044
1045 utime = sig->utime;
1046 stime = sig->stime;
1047 sum_sched_runtime = sig->sum_sched_runtime;
1048 t = tsk;
1049 do {
1050 utime = cputime_add(utime, t->utime);
1051 stime = cputime_add(stime, t->stime);
1052 sum_sched_runtime += t->se.sum_exec_runtime;
1053 t = next_thread(t);
1054 } while (t != tsk);
1055 ptime = cputime_add(utime, stime);
1056
1057 maxfire = 20;
1058 prof_expires = cputime_zero;
1059 while (!list_empty(timers)) {
1060 struct cpu_timer_list *t = list_first_entry(timers,
1061 struct cpu_timer_list,
1062 entry);
1063 if (!--maxfire || cputime_lt(ptime, t->expires.cpu)) {
1064 prof_expires = t->expires.cpu;
1065 break;
1066 }
1067 t->firing = 1;
1068 list_move_tail(&t->entry, firing);
1069 }
1070
1071 ++timers;
1072 maxfire = 20;
1073 virt_expires = cputime_zero;
1074 while (!list_empty(timers)) {
1075 struct cpu_timer_list *t = list_first_entry(timers,
1076 struct cpu_timer_list,
1077 entry);
1078 if (!--maxfire || cputime_lt(utime, t->expires.cpu)) {
1079 virt_expires = t->expires.cpu;
1080 break;
1081 }
1082 t->firing = 1;
1083 list_move_tail(&t->entry, firing);
1084 }
1085
1086 ++timers;
1087 maxfire = 20;
1088 sched_expires = 0;
1089 while (!list_empty(timers)) {
1090 struct cpu_timer_list *t = list_first_entry(timers,
1091 struct cpu_timer_list,
1092 entry);
1093 if (!--maxfire || sum_sched_runtime < t->expires.sched) {
1094 sched_expires = t->expires.sched;
1095 break;
1096 }
1097 t->firing = 1;
1098 list_move_tail(&t->entry, firing);
1099 }
1100
1101
1102
1103
1104 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1105 if (cputime_ge(ptime, sig->it_prof_expires)) {
1106
1107 sig->it_prof_expires = sig->it_prof_incr;
1108 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1109 sig->it_prof_expires = cputime_add(
1110 sig->it_prof_expires, ptime);
1111 }
1112 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1113 }
1114 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1115 (cputime_eq(prof_expires, cputime_zero) ||
1116 cputime_lt(sig->it_prof_expires, prof_expires))) {
1117 prof_expires = sig->it_prof_expires;
1118 }
1119 }
1120 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1121 if (cputime_ge(utime, sig->it_virt_expires)) {
1122
1123 sig->it_virt_expires = sig->it_virt_incr;
1124 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1125 sig->it_virt_expires = cputime_add(
1126 sig->it_virt_expires, utime);
1127 }
1128 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1129 }
1130 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1131 (cputime_eq(virt_expires, cputime_zero) ||
1132 cputime_lt(sig->it_virt_expires, virt_expires))) {
1133 virt_expires = sig->it_virt_expires;
1134 }
1135 }
1136 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1137 unsigned long psecs = cputime_to_secs(ptime);
1138 cputime_t x;
1139 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1140
1141
1142
1143
1144 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1145 return;
1146 }
1147 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1148
1149
1150
1151 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1152 if (sig->rlim[RLIMIT_CPU].rlim_cur
1153 < sig->rlim[RLIMIT_CPU].rlim_max) {
1154 sig->rlim[RLIMIT_CPU].rlim_cur++;
1155 }
1156 }
1157 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1158 if (cputime_eq(prof_expires, cputime_zero) ||
1159 cputime_lt(x, prof_expires)) {
1160 prof_expires = x;
1161 }
1162 }
1163
1164 if (!cputime_eq(prof_expires, cputime_zero) ||
1165 !cputime_eq(virt_expires, cputime_zero) ||
1166 sched_expires != 0) {
1167
1168
1169
1170
1171
1172 cputime_t prof_left, virt_left, ticks;
1173 unsigned long long sched_left, sched;
1174 const unsigned int nthreads = atomic_read(&sig->live);
1175
1176 if (!nthreads)
1177 return;
1178
1179 prof_left = cputime_sub(prof_expires, utime);
1180 prof_left = cputime_sub(prof_left, stime);
1181 prof_left = cputime_div_non_zero(prof_left, nthreads);
1182 virt_left = cputime_sub(virt_expires, utime);
1183 virt_left = cputime_div_non_zero(virt_left, nthreads);
1184 if (sched_expires) {
1185 sched_left = sched_expires - sum_sched_runtime;
1186 do_div(sched_left, nthreads);
1187 sched_left = max_t(unsigned long long, sched_left, 1);
1188 } else {
1189 sched_left = 0;
1190 }
1191 t = tsk;
1192 do {
1193 if (unlikely(t->flags & PF_EXITING))
1194 continue;
1195
1196 ticks = cputime_add(cputime_add(t->utime, t->stime),
1197 prof_left);
1198 if (!cputime_eq(prof_expires, cputime_zero) &&
1199 (cputime_eq(t->it_prof_expires, cputime_zero) ||
1200 cputime_gt(t->it_prof_expires, ticks))) {
1201 t->it_prof_expires = ticks;
1202 }
1203
1204 ticks = cputime_add(t->utime, virt_left);
1205 if (!cputime_eq(virt_expires, cputime_zero) &&
1206 (cputime_eq(t->it_virt_expires, cputime_zero) ||
1207 cputime_gt(t->it_virt_expires, ticks))) {
1208 t->it_virt_expires = ticks;
1209 }
1210
1211 sched = t->se.sum_exec_runtime + sched_left;
1212 if (sched_expires && (t->it_sched_expires == 0 ||
1213 t->it_sched_expires > sched)) {
1214 t->it_sched_expires = sched;
1215 }
1216 } while ((t = next_thread(t)) != tsk);
1217 }
1218}
1219
1220
1221
1222
1223
1224void posix_cpu_timer_schedule(struct k_itimer *timer)
1225{
1226 struct task_struct *p = timer->it.cpu.task;
1227 union cpu_time_count now;
1228
1229 if (unlikely(p == NULL))
1230
1231
1232
1233 goto out;
1234
1235
1236
1237
1238 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1239 cpu_clock_sample(timer->it_clock, p, &now);
1240 bump_cpu_timer(timer, now);
1241 if (unlikely(p->exit_state)) {
1242 clear_dead_task(timer, now);
1243 goto out;
1244 }
1245 read_lock(&tasklist_lock);
1246 } else {
1247 read_lock(&tasklist_lock);
1248 if (unlikely(p->signal == NULL)) {
1249
1250
1251
1252
1253 put_task_struct(p);
1254 timer->it.cpu.task = p = NULL;
1255 timer->it.cpu.expires.sched = 0;
1256 goto out_unlock;
1257 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1258
1259
1260
1261
1262
1263 clear_dead_task(timer, now);
1264 goto out_unlock;
1265 }
1266 cpu_clock_sample_group(timer->it_clock, p, &now);
1267 bump_cpu_timer(timer, now);
1268
1269 }
1270
1271
1272
1273
1274 arm_timer(timer, now);
1275
1276out_unlock:
1277 read_unlock(&tasklist_lock);
1278
1279out:
1280 timer->it_overrun_last = timer->it_overrun;
1281 timer->it_overrun = -1;
1282 ++timer->it_requeue_pending;
1283}
1284
1285
1286
1287
1288
1289
1290void run_posix_cpu_timers(struct task_struct *tsk)
1291{
1292 LIST_HEAD(firing);
1293 struct k_itimer *timer, *next;
1294
1295 BUG_ON(!irqs_disabled());
1296
1297#define UNEXPIRED(clock) \
1298 (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
1299 cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
1300
1301 if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
1302 (tsk->it_sched_expires == 0 ||
1303 tsk->se.sum_exec_runtime < tsk->it_sched_expires))
1304 return;
1305
1306#undef UNEXPIRED
1307
1308
1309
1310
1311 read_lock(&tasklist_lock);
1312 if (likely(tsk->signal != NULL)) {
1313 spin_lock(&tsk->sighand->siglock);
1314
1315
1316
1317
1318
1319 check_thread_timers(tsk, &firing);
1320 check_process_timers(tsk, &firing);
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330 spin_unlock(&tsk->sighand->siglock);
1331 }
1332 read_unlock(&tasklist_lock);
1333
1334
1335
1336
1337
1338
1339
1340 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1341 int firing;
1342 spin_lock(&timer->it_lock);
1343 list_del_init(&timer->it.cpu.entry);
1344 firing = timer->it.cpu.firing;
1345 timer->it.cpu.firing = 0;
1346
1347
1348
1349
1350
1351 if (likely(firing >= 0)) {
1352 cpu_timer_fire(timer);
1353 }
1354 spin_unlock(&timer->it_lock);
1355 }
1356}
1357
1358
1359
1360
1361
1362
1363
1364
1365void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1366 cputime_t *newval, cputime_t *oldval)
1367{
1368 union cpu_time_count now;
1369 struct list_head *head;
1370
1371 BUG_ON(clock_idx == CPUCLOCK_SCHED);
1372 cpu_clock_sample_group_locked(clock_idx, tsk, &now);
1373
1374 if (oldval) {
1375 if (!cputime_eq(*oldval, cputime_zero)) {
1376 if (cputime_le(*oldval, now.cpu)) {
1377
1378 *oldval = jiffies_to_cputime(1);
1379 } else {
1380 *oldval = cputime_sub(*oldval, now.cpu);
1381 }
1382 }
1383
1384 if (cputime_eq(*newval, cputime_zero))
1385 return;
1386 *newval = cputime_add(*newval, now.cpu);
1387
1388
1389
1390
1391
1392 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1393 < cputime_to_secs(*newval))
1394 return;
1395 }
1396
1397
1398
1399
1400
1401 head = &tsk->signal->cpu_timers[clock_idx];
1402 if (list_empty(head) ||
1403 cputime_ge(list_first_entry(head,
1404 struct cpu_timer_list, entry)->expires.cpu,
1405 *newval)) {
1406
1407
1408
1409
1410 union cpu_time_count expires = { .sched = 0 };
1411 expires.cpu = *newval;
1412 process_timer_rebalance(tsk, clock_idx, expires, now);
1413 }
1414}
1415
1416static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1417 struct timespec *rqtp, struct itimerspec *it)
1418{
1419 struct k_itimer timer;
1420 int error;
1421
1422
1423
1424
1425 memset(&timer, 0, sizeof timer);
1426 spin_lock_init(&timer.it_lock);
1427 timer.it_clock = which_clock;
1428 timer.it_overrun = -1;
1429 error = posix_cpu_timer_create(&timer);
1430 timer.it_process = current;
1431 if (!error) {
1432 static struct itimerspec zero_it;
1433
1434 memset(it, 0, sizeof *it);
1435 it->it_value = *rqtp;
1436
1437 spin_lock_irq(&timer.it_lock);
1438 error = posix_cpu_timer_set(&timer, flags, it, NULL);
1439 if (error) {
1440 spin_unlock_irq(&timer.it_lock);
1441 return error;
1442 }
1443
1444 while (!signal_pending(current)) {
1445 if (timer.it.cpu.expires.sched == 0) {
1446
1447
1448
1449 spin_unlock_irq(&timer.it_lock);
1450 return 0;
1451 }
1452
1453
1454
1455
1456 __set_current_state(TASK_INTERRUPTIBLE);
1457 spin_unlock_irq(&timer.it_lock);
1458 schedule();
1459 spin_lock_irq(&timer.it_lock);
1460 }
1461
1462
1463
1464
1465 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1466 posix_cpu_timer_set(&timer, 0, &zero_it, it);
1467 spin_unlock_irq(&timer.it_lock);
1468
1469 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1470
1471
1472
1473 return 0;
1474 }
1475
1476 error = -ERESTART_RESTARTBLOCK;
1477 }
1478
1479 return error;
1480}
1481
1482int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1483 struct timespec *rqtp, struct timespec __user *rmtp)
1484{
1485 struct restart_block *restart_block =
1486 ¤t_thread_info()->restart_block;
1487 struct itimerspec it;
1488 int error;
1489
1490
1491
1492
1493 if (CPUCLOCK_PERTHREAD(which_clock) &&
1494 (CPUCLOCK_PID(which_clock) == 0 ||
1495 CPUCLOCK_PID(which_clock) == current->pid))
1496 return -EINVAL;
1497
1498 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1499
1500 if (error == -ERESTART_RESTARTBLOCK) {
1501
1502 if (flags & TIMER_ABSTIME)
1503 return -ERESTARTNOHAND;
1504
1505
1506
1507 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1508 return -EFAULT;
1509
1510 restart_block->fn = posix_cpu_nsleep_restart;
1511 restart_block->arg0 = which_clock;
1512 restart_block->arg1 = (unsigned long) rmtp;
1513 restart_block->arg2 = rqtp->tv_sec;
1514 restart_block->arg3 = rqtp->tv_nsec;
1515 }
1516 return error;
1517}
1518
1519long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1520{
1521 clockid_t which_clock = restart_block->arg0;
1522 struct timespec __user *rmtp;
1523 struct timespec t;
1524 struct itimerspec it;
1525 int error;
1526
1527 rmtp = (struct timespec __user *) restart_block->arg1;
1528 t.tv_sec = restart_block->arg2;
1529 t.tv_nsec = restart_block->arg3;
1530
1531 restart_block->fn = do_no_restart_syscall;
1532 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1533
1534 if (error == -ERESTART_RESTARTBLOCK) {
1535
1536
1537
1538 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1539 return -EFAULT;
1540
1541 restart_block->fn = posix_cpu_nsleep_restart;
1542 restart_block->arg0 = which_clock;
1543 restart_block->arg1 = (unsigned long) rmtp;
1544 restart_block->arg2 = t.tv_sec;
1545 restart_block->arg3 = t.tv_nsec;
1546 }
1547 return error;
1548
1549}
1550
1551
1552#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1553#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1554
1555static int process_cpu_clock_getres(const clockid_t which_clock,
1556 struct timespec *tp)
1557{
1558 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1559}
1560static int process_cpu_clock_get(const clockid_t which_clock,
1561 struct timespec *tp)
1562{
1563 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1564}
1565static int process_cpu_timer_create(struct k_itimer *timer)
1566{
1567 timer->it_clock = PROCESS_CLOCK;
1568 return posix_cpu_timer_create(timer);
1569}
1570static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1571 struct timespec *rqtp,
1572 struct timespec __user *rmtp)
1573{
1574 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1575}
1576static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1577{
1578 return -EINVAL;
1579}
1580static int thread_cpu_clock_getres(const clockid_t which_clock,
1581 struct timespec *tp)
1582{
1583 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1584}
1585static int thread_cpu_clock_get(const clockid_t which_clock,
1586 struct timespec *tp)
1587{
1588 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1589}
1590static int thread_cpu_timer_create(struct k_itimer *timer)
1591{
1592 timer->it_clock = THREAD_CLOCK;
1593 return posix_cpu_timer_create(timer);
1594}
1595static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
1596 struct timespec *rqtp, struct timespec __user *rmtp)
1597{
1598 return -EINVAL;
1599}
1600static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
1601{
1602 return -EINVAL;
1603}
1604
1605static __init int init_posix_cpu_timers(void)
1606{
1607 struct k_clock process = {
1608 .clock_getres = process_cpu_clock_getres,
1609 .clock_get = process_cpu_clock_get,
1610 .clock_set = do_posix_clock_nosettime,
1611 .timer_create = process_cpu_timer_create,
1612 .nsleep = process_cpu_nsleep,
1613 .nsleep_restart = process_cpu_nsleep_restart,
1614 };
1615 struct k_clock thread = {
1616 .clock_getres = thread_cpu_clock_getres,
1617 .clock_get = thread_cpu_clock_get,
1618 .clock_set = do_posix_clock_nosettime,
1619 .timer_create = thread_cpu_timer_create,
1620 .nsleep = thread_cpu_nsleep,
1621 .nsleep_restart = thread_cpu_nsleep_restart,
1622 };
1623
1624 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1625 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1626
1627 return 0;
1628}
1629__initcall(init_posix_cpu_timers);
1630