diff options
author | Stanislaw Gruszka <sgruszka@redhat.com> | 2010-03-11 17:04:41 -0500 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2010-03-12 16:40:40 -0500 |
commit | 1f169f84d25a74fb2dc67274d31d082ce30c60fb (patch) | |
tree | 7affff2991652481d30c6c97328588f30ec976f3 /kernel | |
parent | ae1a78eecc45fe41215d9dbfd7079999455772d6 (diff) |
cpu-timers: Change SIGEV_NONE timer implementation
When user sets up a timer without associated signal and process does
not use any other cpu timers and does not exit, tsk->signal->cputimer
is enabled and running forever.
Avoid running the timer for no reason.
I used below program to check patch does not break current user space
visible behavior.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
void consume_cpu(void)
{
int i = 0;
int count = 0;
for(i=0; i<100000000; i++)
count++;
}
int main(void)
{
int i;
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec = { };
evt.sigev_notify = SIGEV_NONE;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 10;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
for (i = 0; i < 30; i++) {
consume_cpu();
memset(&spec, 0, sizeof(spec));
assert(timer_gettime(tid, &spec) == 0);
printf("%lu.%09lu\n",
(unsigned long) spec.it_value.tv_sec,
(unsigned long) spec.it_value.tv_nsec);
}
assert(timer_delete(tid) == 0);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/posix-cpu-timers.c | 28 |
1 files changed, 6 insertions, 22 deletions
diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c index cce2f0b2d406..7d9d0fab1651 100644 --- a/kernel/posix-cpu-timers.c +++ b/kernel/posix-cpu-timers.c | |||
@@ -615,7 +615,12 @@ static void arm_timer(struct k_itimer *timer) | |||
615 | */ | 615 | */ |
616 | static void cpu_timer_fire(struct k_itimer *timer) | 616 | static void cpu_timer_fire(struct k_itimer *timer) |
617 | { | 617 | { |
618 | if (unlikely(timer->sigq == NULL)) { | 618 | if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { |
619 | /* | ||
620 | * User don't want any signal. | ||
621 | */ | ||
622 | timer->it.cpu.expires.sched = 0; | ||
623 | } else if (unlikely(timer->sigq == NULL)) { | ||
619 | /* | 624 | /* |
620 | * This a special case for clock_nanosleep, | 625 | * This a special case for clock_nanosleep, |
621 | * not a normal timer from sys_timer_create. | 626 | * not a normal timer from sys_timer_create. |
@@ -784,7 +789,6 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags, | |||
784 | */ | 789 | */ |
785 | timer->it.cpu.expires = new_expires; | 790 | timer->it.cpu.expires = new_expires; |
786 | if (new_expires.sched != 0 && | 791 | if (new_expires.sched != 0 && |
787 | (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE && | ||
788 | cpu_time_before(timer->it_clock, val, new_expires)) { | 792 | cpu_time_before(timer->it_clock, val, new_expires)) { |
789 | arm_timer(timer); | 793 | arm_timer(timer); |
790 | } | 794 | } |
@@ -809,7 +813,6 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags, | |||
809 | timer->it_overrun = -1; | 813 | timer->it_overrun = -1; |
810 | 814 | ||
811 | if (new_expires.sched != 0 && | 815 | if (new_expires.sched != 0 && |
812 | (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE && | ||
813 | !cpu_time_before(timer->it_clock, val, new_expires)) { | 816 | !cpu_time_before(timer->it_clock, val, new_expires)) { |
814 | /* | 817 | /* |
815 | * The designated time already passed, so we notify | 818 | * The designated time already passed, so we notify |
@@ -883,25 +886,6 @@ void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp) | |||
883 | read_unlock(&tasklist_lock); | 886 | read_unlock(&tasklist_lock); |
884 | } | 887 | } |
885 | 888 | ||
886 | if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { | ||
887 | if (timer->it.cpu.incr.sched == 0 && | ||
888 | cpu_time_before(timer->it_clock, | ||
889 | timer->it.cpu.expires, now)) { | ||
890 | /* | ||
891 | * Do-nothing timer expired and has no reload, | ||
892 | * so it's as if it was never set. | ||
893 | */ | ||
894 | timer->it.cpu.expires.sched = 0; | ||
895 | itp->it_value.tv_sec = itp->it_value.tv_nsec = 0; | ||
896 | return; | ||
897 | } | ||
898 | /* | ||
899 | * Account for any expirations and reloads that should | ||
900 | * have happened. | ||
901 | */ | ||
902 | bump_cpu_timer(timer, now); | ||
903 | } | ||
904 | |||
905 | if (unlikely(clear_dead)) { | 889 | if (unlikely(clear_dead)) { |
906 | /* | 890 | /* |
907 | * We've noticed that the thread is dead, but | 891 | * We've noticed that the thread is dead, but |