Skip to contentSkip to conversation

clock_nanosleep() task_struct leak

PATCH Superseded Tommi Rantala 4 participants 13 messages Feb 1, 2013 – Feb 7, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

Hello,

Trinity discovered a task_struct leak with clock_nanosleep(), reproducible with:

-----8<-----8<-----8<-----
#include <time.h>

static const struct timespec req;

int main(void) {
        return clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID,
                        TIMER_ABSTIME, &req, NULL);
}

-----8<-----8<-----8<-----

Tommi

#1 lore raw
Quoted 7 lines · Tommi Rantala

Hello,

Trinity discovered a task_struct leak with clock_nanosleep(), reproducible with:

-----8<-----8<-----8<-----
#include <time.h>

Quoted 7 lines

static const struct timespec req;

int main(void) {
        return clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID,
                        TIMER_ABSTIME, &req, NULL);
}

-----8<-----8<-----8<-----

Groan. That code probably has more than that leak burried inside.

Thanks,

tglx

#2 lore raw
Quoted 10 lines · Thomas Gleixner
Quoted 7 lines · Tommi Rantala

Hello,

Trinity discovered a task_struct leak with clock_nanosleep(), reproducible with:

-----8<-----8<-----8<-----
#include <time.h>

Quoted 7 lines
Quoted 7 lines

static const struct timespec req;

int main(void) {
        return clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID,
                        TIMER_ABSTIME, &req, NULL);
}

-----8<-----8<-----8<-----

posix_cpu_timer_create()->get_task_struct() I guess...

Cough. I am not sure I ever understood this code, but now it certainly looks as if I never saw it before.

All I can say is: I'll try to take another look tomorrow :/

Quoted 1 line

Groan. That code probably has more than that leak burried inside.

Oh. yes... but if you meant something connected to this particular "test-case" please tell us ;)

Oleg.

#3 lore raw
Quoted 19 lines · Oleg Nesterov
Quoted 10 lines · Thomas Gleixner
Quoted 7 lines · Tommi Rantala

Hello,

Trinity discovered a task_struct leak with clock_nanosleep(), reproducible with:

-----8<-----8<-----8<-----
#include <time.h>

Quoted 7 lines
Quoted 7 lines

static const struct timespec req;

int main(void) {
        return clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID,
                        TIMER_ABSTIME, &req, NULL);
}

-----8<-----8<-----8<-----

Quoted 4 lines

posix_cpu_timer_create()->get_task_struct() I guess...

Cough. I am not sure I ever understood this code, but now it certainly looks as if I never saw it before.

Looks on do_cpu_nanosleep() we call posix_cpu_timer_create(), but we do not call posix_cpu_timer_del() at the end. Fix will not be super simple, since we need to care about error cases. I can cook a patch if nobody else want to do this.

Stanislaw

#4 lore raw
Quoted 22 lines · Stanislaw Gruszka
Quoted 21 lines · Oleg Nesterov
Quoted 10 lines · Thomas Gleixner
Quoted 7 lines · Tommi Rantala

Hello,

Trinity discovered a task_struct leak with clock_nanosleep(), reproducible with:

-----8<-----8<-----8<-----
#include <time.h>

Quoted 7 lines
Quoted 7 lines

static const struct timespec req;

int main(void) {
        return clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID,
                        TIMER_ABSTIME, &req, NULL);
}

-----8<-----8<-----8<-----

posix_cpu_timer_create()->get_task_struct() I guess...

Quoted 7 lines
Quoted 2 lines

Cough. I am not sure I ever understood this code, but now it certainly looks as if I never saw it before.

Looks on do_cpu_nanosleep() we call posix_cpu_timer_create(), but we do not call posix_cpu_timer_del() at the end. Fix will not be super simple, since we need to care about error cases. I can cook a patch if nobody else want to do this.

Would be much appreciated!

Thanks,

tglx

#5 lore raw
Quoted 26 lines · Thomas Gleixner
Quoted 25 lines · Stanislaw Gruszka
Quoted 23 lines · Oleg Nesterov
Quoted 10 lines · Thomas Gleixner
Quoted 7 lines · Tommi Rantala

Hello,

Trinity discovered a task_struct leak with clock_nanosleep(), reproducible with:

-----8<-----8<-----8<-----
#include <time.h>

Quoted 7 lines
Quoted 7 lines

static const struct timespec req;

int main(void) {
        return clock_nanosleep(CLOCK_PROCESS_CPUTIME_ID,
                        TIMER_ABSTIME, &req, NULL);
}

-----8<-----8<-----8<-----

posix_cpu_timer_create()->get_task_struct() I guess...

Cough. I am not sure I ever understood this code, but now it certainly looks as if I never saw it before.

Quoted 6 lines
Quoted 4 lines

Looks on do_cpu_nanosleep() we call posix_cpu_timer_create(), but we do not call posix_cpu_timer_del() at the end. Fix will not be super simple, since we need to care about error cases. I can cook a patch if nobody else want to do this.

Would be much appreciated!

Below is proposed fix. Error cases wasn't that bad since there are various limitations when timer could be fired (i.e. timer which already fired can not be fired again).

Tommi, please check if patch really fixes the problem. I tested it with signal interrupt and timeout scenarios, but I don't know how to confirm if it fix the leak or not.

kernel/posix-cpu-timers.c +10 -1
index 125cb67..07a38b6 100644
@@ -1424,6 +1424,7 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 				/*
 				 * Our timer fired and was reset.
 				 */
+				posix_cpu_timer_del(&timer);
 				spin_unlock_irq(&timer.it_lock);
 				return 0;
 			}
@@ -1441,9 +1442,17 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		 * We were interrupted by a signal.
 		 */
 		sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-		posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		if (!error)
+			posix_cpu_timer_del(&timer);
 		spin_unlock_irq(&timer.it_lock);
 
+		while (error == TIMER_RETRY) {
+			spin_lock_irq(&timer.it_lock);
+			error = posix_cpu_timer_del(&timer);
+			spin_unlock_irq(&timer.it_lock);
+		}
+
 		if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
 			/*
 			 * It actually did fire already.
#6 lore raw

2013/2/6 Stanislaw Gruszka <[email protected]>:

Quoted 7 lines

Below is proposed fix. Error cases wasn't that bad since there are various limitations when timer could be fired (i.e. timer which already fired can not be fired again).

Tommi, please check if patch really fixes the problem. I tested it with signal interrupt and timeout scenarios, but I don't know how to confirm if it fix the leak or not.

Hi, looks good, this patch fixes the leaks I'm seeing.

Without the patch, running the program from my earlier mail shows task_struct count growing:

{ttrantal@arkki ~}> uname -r
3.8.0-rc6+
{ttrantal@arkki ~}> sudo grep task_struct /proc/slabinfo
task_struct 93 123 8880 3 8 : tunables 0 0
0 : slabdata 41 41 0
{ttrantal@arkki ~}> for i in seq 1000 ; do ./leak ; done {ttrantal@arkki ~}> sudo grep task_struct /proc/slabinfo
task_struct 1089 1089 8880 3 8 : tunables 0 0
0 : slabdata 363 363 0
{ttrantal@arkki ~}> for i in seq 1000 ; do ./leak ; done {ttrantal@arkki ~}> sudo grep task_struct /proc/slabinfo
task_struct 2088 2088 8880 3 8 : tunables 0 0
0 : slabdata 696 696 0
{ttrantal@arkki ~}>

With the patch applied, the leak is gone:

{ttrantal@arkki ~}> sudo grep task_struct /proc/slabinfo
task_struct 92 108 8880 3 8 : tunables 0 0
0 : slabdata 36 36 0
{ttrantal@arkki ~}> for i in seq 1000 ; do ./leak ; done {ttrantal@arkki ~}> sudo grep task_struct /proc/slabinfo
task_struct 92 108 8880 3 8 : tunables 0 0
0 : slabdata 36 36 0
{ttrantal@arkki ~}> for i in seq 1000 ; do ./leak ; done {ttrantal@arkki ~}> sudo grep task_struct /proc/slabinfo
task_struct 92 108 8880 3 8 : tunables 0 0
0 : slabdata 36 36 0
{ttrantal@arkki ~}>

Running Trinity with kmemleak enabled also resulted to a lot of detected leaks, which are all gone now based on a quick run.

Quoted 31 lines
kernel/posix-cpu-timers.c +10 -1
index 125cb67..07a38b6 100644
@@ -1424,6 +1424,7 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
                                /*
                                 * Our timer fired and was reset.
                                 */
+                               posix_cpu_timer_del(&timer);
                                spin_unlock_irq(&timer.it_lock);
                                return 0;
                        }
@@ -1441,9 +1442,17 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
                 * We were interrupted by a signal.
                 */
                sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-               posix_cpu_timer_set(&timer, 0, &zero_it, it);
+               error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+               if (!error)
+                       posix_cpu_timer_del(&timer);
                spin_unlock_irq(&timer.it_lock);
 
+               while (error == TIMER_RETRY) {
+                       spin_lock_irq(&timer.it_lock);
+                       error = posix_cpu_timer_del(&timer);
+                       spin_unlock_irq(&timer.it_lock);
+               }
+
                if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
                        /*
                         * It actually did fire already.
#7 lore raw

PATCH 1/1 posix-cpu-timers: fix nanosleep task_struct leak

In do_cpu_nanosleep() we do posix_cpu_timer_create(), but forgot corresponding posix_cpu_timer_del(), what lead to task_struct leak.

---
 kernel/posix-cpu-timers.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/posix-cpu-timers.c +10 -1
index a278cad..a01caac 100644
@@ -1403,6 +1403,7 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 				/*
 				 * Our timer fired and was reset.
 				 */
+				posix_cpu_timer_del(&timer);
 				spin_unlock_irq(&timer.it_lock);
 				return 0;
 			}
@@ -1420,9 +1421,17 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		 * We were interrupted by a signal.
 		 */
 		sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-		posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		if (!error)
+			posix_cpu_timer_del(&timer);
 		spin_unlock_irq(&timer.it_lock);
 
+		while (error == TIMER_RETRY) {
+			spin_lock_irq(&timer.it_lock);
+			error = posix_cpu_timer_del(&timer);
+			spin_unlock_irq(&timer.it_lock);
+		}
+
 		if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
 			/*
 			 * It actually did fire already.
Signature · 1 line
1.7.11.7
#8 lore raw

Re: [PATCH] posix-cpu-timers: fix nanosleep task_struct leak

Stanislaw,

First of all, thank you so much. I knew it was a good idea to cc you ;)

And let me repeat that I forgot everything about this code.

Quoted 3 lines · Stanislaw Gruszka

In do_cpu_nanosleep() we do posix_cpu_timer_create(), but forgot corresponding posix_cpu_timer_del(), what lead to task_struct leak.

Plus, it seems we can leave the timer on ->cpu_timers list...

Quoted 11 lines
kernel/posix-cpu-timers.c +1 -0
@@ -1403,6 +1403,7 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 				/*
 				 * Our timer fired and was reset.
 				 */
+				posix_cpu_timer_del(&timer);
 				spin_unlock_irq(&timer.it_lock);
 				return 0;
 			}
@@ -1420,9 +1421,17 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		 * We were interrupted by a signal.
 		 */

kernel/posix-cpu-timers.c static int do_cpu_nanosleep(const clockid_t which_clock, int flags, · line 1431

Quoted 10 lines
kernel/posix-cpu-timers.c +6 -1
 		sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-		posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		if (!error)
+			posix_cpu_timer_del(&timer);
 		spin_unlock_irq(&timer.it_lock);
 
+		while (error == TIMER_RETRY) {
+			spin_lock_irq(&timer.it_lock);
+			error = posix_cpu_timer_del(&timer);

It is not clear to me why other posix_cpu_timer_del's above can't fail.. May be you can add a comment.

And I am not sure that TIMER_RETRY is the only error we should worry. And perhaps we need even more posix_cpu_timer_del's?

For example. Suppose that posix_cpu_timer_create() succeeds and does get_task_struct(p). But than p dies, and the first posix_cpu_timer_set() fails with -ESRCH. No?

Oleg.

#9 lore raw

Re: [PATCH] posix-cpu-timers: fix nanosleep task_struct leak

Quoted 1 line · Oleg Nesterov

First of all, thank you so much. I knew it was a good idea to cc you ;)

:-)

Quoted 25 lines · Stanislaw Gruszka
Quoted 3 lines

In do_cpu_nanosleep() we do posix_cpu_timer_create(), but forgot corresponding posix_cpu_timer_del(), what lead to task_struct leak.

Plus, it seems we can leave the timer on ->cpu_timers list...

Quoted 18 lines
kernel/posix-cpu-timers.c +4 -1
@@ -1403,6 +1403,7 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 				/*
 				 * Our timer fired and was reset.
 				 */
+				posix_cpu_timer_del(&timer);
 				spin_unlock_irq(&timer.it_lock);
 				return 0;
 			}
@@ -1420,9 +1421,17 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		 * We were interrupted by a signal.
 		 */
 		sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-		posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		if (!error)
+			posix_cpu_timer_del(&timer);
 		spin_unlock_irq(&timer.it_lock);
 
Quoted 6 lines

kernel/posix-cpu-timers.c static int do_cpu_nanosleep(const clockid_t which_clock, int flags, · line 1431

Quoted 3 lines
kernel/posix-cpu-timers.c +3 -0
+		while (error == TIMER_RETRY) {
+			spin_lock_irq(&timer.it_lock);
+			error = posix_cpu_timer_del(&timer);

It is not clear to me why other posix_cpu_timer_del's above can't fail.. May be you can add a comment.

Sure, I'll add more comments.

Once posix_cpu_timer_set(..., &zero_it, it) succeed with 0 return value, it's not possible to fire timer, so posix_cpu_timer_del() will not fail. Similar assumption is with first posix_cpu_timer_del() call I added in the patch.

Quoted 6 lines

And I am not sure that TIMER_RETRY is the only error we should worry. And perhaps we need even more posix_cpu_timer_del's?

For example. Suppose that posix_cpu_timer_create() succeeds and does get_task_struct(p). But than p dies, and the first posix_cpu_timer_set() fails with -ESRCH. No?

On second -ESRCH case posix_cpu_timer_set() internally call put_task_struct(). It does not remove from cpu_timers list, but that is done at exit(). First -ESRCH case, i.e. calling posix_cpu_timer_set() with timer->it.cpu.task == NULL, is not possible in our case.

BTW: I don't think we handle correctly case when traced process -
- timer->it.cpu.task will die. Tracing process - timer->it_process will probably not be woken up.

Stanislaw

#10 lore raw

Re: [PATCH] posix-cpu-timers: fix nanosleep task_struct leak

Quoted 7 lines · Stanislaw Gruszka
Quoted 3 lines · Oleg Nesterov

It is not clear to me why other posix_cpu_timer_del's above can't fail.. May be you can add a comment.

Sure, I'll add more comments.

Yes, please. This will help the next reader to understand that no, we do not forget to check the error code, just we do not need to do this.

Quoted 4 lines

Once posix_cpu_timer_set(..., &zero_it, it) succeed with 0 return value, it's not possible to fire timer, so posix_cpu_timer_del() will not fail. Similar assumption is with first posix_cpu_timer_del() call I added in the patch.

OK, I see.

Quoted 9 lines
Quoted 6 lines

And I am not sure that TIMER_RETRY is the only error we should worry. And perhaps we need even more posix_cpu_timer_del's?

For example. Suppose that posix_cpu_timer_create() succeeds and does get_task_struct(p). But than p dies, and the first posix_cpu_timer_set() fails with -ESRCH. No?

On second -ESRCH case posix_cpu_timer_set() internally call put_task_struct().

Ah, missed that, thanks.

Quoted 2 lines

It does not remove from cpu_timers list, but
that is done at exit().

Yes, posix_cpu_timers_exit().

OK, thanks for your explanations, I think the patch is fine.

Quoted 3 lines

BTW: I don't think we handle correctly case when traced process -
- timer->it.cpu.task will die. Tracing process - timer->it_process will probably not be woken up.

Probably... or perhaps we can treat this case as "timer never expires".

Oleg.

#11 lore raw

PATCH v2 1/1 posix-cpu-timers: fix nanosleep task_struct leak

In do_cpu_nanosleep() we do posic_cpu_timer_create(), but forgot corresponding posix_cpu_timer_del() what lead to task_struct leak.

---
v1 -> v2: add comments

 kernel/posix-cpu-timers.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/posix-cpu-timers.c +21 -2
index a278cad..942ca27 100644
@@ -1401,8 +1401,10 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		while (!signal_pending(current)) {
 			if (timer.it.cpu.expires.sched == 0) {
 				/*
-				 * Our timer fired and was reset.
+				 * Our timer fired and was reset, below
+				 * deletion can not fail.
 				 */
+				posix_cpu_timer_del(&timer);
 				spin_unlock_irq(&timer.it_lock);
 				return 0;
 			}
@@ -1420,9 +1422,26 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		 * We were interrupted by a signal.
 		 */
 		sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-		posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		if (!error) {
+			/*
+			 * Timer is now unarmed, deletion can not fail.
+			 */
+			posix_cpu_timer_del(&timer);
+		}
 		spin_unlock_irq(&timer.it_lock);
 
+		while (error == TIMER_RETRY) {
+			/*
+			 * We need to handle case when timer was or is in the
+			 * middle of firing. In other cases we already freed
+			 * resources.
+			 */
+			spin_lock_irq(&timer.it_lock);
+			error = posix_cpu_timer_del(&timer);
+			spin_unlock_irq(&timer.it_lock);
+		}
+
 		if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
 			/*
 			 * It actually did fire already.
Signature · 1 line
1.7.11.7
#12 lore raw

Re: [PATCH v2] posix-cpu-timers: fix nanosleep task_struct leak

Thanks Stanislaw.

I think this patch is fine.

Quoted 59 lines · Stanislaw Gruszka

In do_cpu_nanosleep() we do posic_cpu_timer_create(), but forgot corresponding posix_cpu_timer_del() what lead to task_struct leak.

---
v1 -> v2: add comments

 kernel/posix-cpu-timers.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
kernel/posix-cpu-timers.c +21 -2
index a278cad..942ca27 100644
@@ -1401,8 +1401,10 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		while (!signal_pending(current)) {
 			if (timer.it.cpu.expires.sched == 0) {
 				/*
-				 * Our timer fired and was reset.
+				 * Our timer fired and was reset, below
+				 * deletion can not fail.
 				 */
+				posix_cpu_timer_del(&timer);
 				spin_unlock_irq(&timer.it_lock);
 				return 0;
 			}
@@ -1420,9 +1422,26 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
 		 * We were interrupted by a signal.
 		 */
 		sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
-		posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
+		if (!error) {
+			/*
+			 * Timer is now unarmed, deletion can not fail.
+			 */
+			posix_cpu_timer_del(&timer);
+		}
 		spin_unlock_irq(&timer.it_lock);
 
+		while (error == TIMER_RETRY) {
+			/*
+			 * We need to handle case when timer was or is in the
+			 * middle of firing. In other cases we already freed
+			 * resources.
+			 */
+			spin_lock_irq(&timer.it_lock);
+			error = posix_cpu_timer_del(&timer);
+			spin_unlock_irq(&timer.it_lock);
+		}
+
 		if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
 			/*
 			 * It actually did fire already.
Signature · 1 line
1.7.11.7