Skip to contentSkip to conversation

posix-cpu-timers: fix memory leaks for task_struct

PATCH In review Qian Cai 3 participants 5 messages Mar 4, 2020 Posix clocks and timers

lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH 1/1 posix-cpu-timers: fix memory leaks for task_struct

The recent commit removed put_task_struct() in posix_cpu_timer_del() results in many memory leaks like this,

unreferenced object 0xc0000016d9b44480 (size 8192):
comm "timer_create01", pid 57749, jiffies 4295163733 (age 6159.670s) hex dump (first 32 bytes):

    02 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<0000000056aca129>] copy_process+0x26c/0x18e0
    alloc_task_struct_node at kernel/fork.c:169
    (inlined by) dup_task_struct at kernel/fork.c:877
    (inlined by) copy_process at kernel/fork.c:1929
    [<00000000bdbbf9f8>] _do_fork+0xac/0xb20
    [<00000000dcb1c445>] __do_sys_clone+0x98/0xe0
    __do_sys_clone at kernel/fork.c:2591
    [<000000006c059205>] ppc_clone+0x8/0xc
    ppc_clone at arch/powerpc/kernel/entry_64.S:479
Fixes 672ebe8eb017a5 ("posix-cpu-timers: Store a reference to a pid not a task")
---
 kernel/time/posix-cpu-timers.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +3 -1
index afd1e959a282..e0b580deb61a 100644
@@ -446,8 +446,10 @@ static int posix_cpu_timer_del(struct k_itimer *timer)
 
 out:
 	rcu_read_unlock();
-	if (!ret)
+	if (!ret) {
 		put_pid(ctmr->pid);
+		put_task_struct(p);
+	}
 
 	return ret;
 }
Signature · 1 line
2.21.0 (Apple Git-122.2)
#1 lore raw
Quoted 2 lines · Qian Cai

The recent commit removed put_task_struct() in posix_cpu_timer_del() results in many memory leaks like this,

Good spotting but no. The leak is in posix_cpu_timer_create. There is a strong likely hood but no guarantee that the task in posix_cpu_timer_del is the same as the task in posix_cpu_timer_create.

Plus the point of it all is to use pid references instead of task references.

Thank you very much for catching my braino.

Eric

Quoted 38 lines

unreferenced object 0xc0000016d9b44480 (size 8192):
comm "timer_create01", pid 57749, jiffies 4295163733 (age 6159.670s) hex dump (first 32 bytes):

    02 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<0000000056aca129>] copy_process+0x26c/0x18e0
    alloc_task_struct_node at kernel/fork.c:169
    (inlined by) dup_task_struct at kernel/fork.c:877
    (inlined by) copy_process at kernel/fork.c:1929
    [<00000000bdbbf9f8>] _do_fork+0xac/0xb20
    [<00000000dcb1c445>] __do_sys_clone+0x98/0xe0
    __do_sys_clone at kernel/fork.c:2591
    [<000000006c059205>] ppc_clone+0x8/0xc
    ppc_clone at arch/powerpc/kernel/entry_64.S:479
Fixes 672ebe8eb017a5 ("posix-cpu-timers: Store a reference to a pid not a task")
---
 kernel/time/posix-cpu-timers.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +3 -1
index afd1e959a282..e0b580deb61a 100644
@@ -446,8 +446,10 @@ static int posix_cpu_timer_del(struct k_itimer *timer)
 
 out:
 	rcu_read_unlock();
-	if (!ret)
+	if (!ret) {
 		put_pid(ctmr->pid);
+		put_task_struct(p);
+	}
 
 	return ret;
 }
#2 lore raw

PATCH 1/1 posix-cpu-timers: Put the task_struct in posix_cpu_timers_create

In reply to Qian Cai
Quoted 6 lines · Qian Cai

The recent commit removed put_task_struct() in posix_cpu_timer_del() results in many memory leaks like this,

unreferenced object 0xc0000016d9b44480 (size 8192):
comm "timer_create01", pid 57749, jiffies 4295163733 (age 6159.670s) hex dump (first 32 bytes):

Quoted 13 lines
    02 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<0000000056aca129>] copy_process+0x26c/0x18e0
    alloc_task_struct_node at kernel/fork.c:169
    (inlined by) dup_task_struct at kernel/fork.c:877
    (inlined by) copy_process at kernel/fork.c:1929
    [<00000000bdbbf9f8>] _do_fork+0xac/0xb20
    [<00000000dcb1c445>] __do_sys_clone+0x98/0xe0
    __do_sys_clone at kernel/fork.c:2591
    [<000000006c059205>] ppc_clone+0x8/0xc
    ppc_clone at arch/powerpc/kernel/entry_64.S:479

I forgot that get_task_for_clock called by posix_cpu_timer_create returns a reference to a task_struct. Put that reference to avoid the leak.

Fixes 672ebe8eb017a5 ("posix-cpu-timers: Store a reference to a pid not a task")
---
 kernel/time/posix-cpu-timers.c | 1 +
 1 file changed, 1 insertion(+)
kernel/time/posix-cpu-timers.c +1 -0
index 1c21f2fd3d9b..cd88c1217224 100644
@@ -405,6 +405,7 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)
 	new_timer->kclock = &clock_posix_cpu;
 	timerqueue_init(&new_timer->it.cpu.node);
 	new_timer->it.cpu.pid = get_task_pid(p, cpu_timer_pid_type(new_timer));
+	put_task_struct(p);
 	return 0;
 }
 
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH timers/core] posix-cpu-timers: Put the task_struct in posix_cpu_timers_create

Quoted 21 lines · [email protected] (Eric W. Biederman) writes:
Quoted 6 lines · Qian Cai

The recent commit removed put_task_struct() in posix_cpu_timer_del() results in many memory leaks like this,

unreferenced object 0xc0000016d9b44480 (size 8192):
comm "timer_create01", pid 57749, jiffies 4295163733 (age 6159.670s) hex dump (first 32 bytes):

Quoted 13 lines
    02 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<0000000056aca129>] copy_process+0x26c/0x18e0
    alloc_task_struct_node at kernel/fork.c:169
    (inlined by) dup_task_struct at kernel/fork.c:877
    (inlined by) copy_process at kernel/fork.c:1929
    [<00000000bdbbf9f8>] _do_fork+0xac/0xb20
    [<00000000dcb1c445>] __do_sys_clone+0x98/0xe0
    __do_sys_clone at kernel/fork.c:2591
    [<000000006c059205>] ppc_clone+0x8/0xc
    ppc_clone at arch/powerpc/kernel/entry_64.S:479
Quoted 3 lines

I forgot that get_task_for_clock called by posix_cpu_timer_create returns a reference to a task_struct. Put that reference to avoid the leak.

I took the liberty to fold this back into the affected commit and add a comment why this put_task_struct() is actually required.

Thanks,

tglx

#4 lore raw

Re: [PATCH timers/core] posix-cpu-timers: Put the task_struct in posix_cpu_timers_create

Quoted 23 lines · Thomas Gleixner
Quoted 21 lines · [email protected] (Eric W. Biederman) writes:
Quoted 19 lines · Qian Cai

The recent commit removed put_task_struct() in posix_cpu_timer_del() results in many memory leaks like this,

unreferenced object 0xc0000016d9b44480 (size 8192):
comm "timer_create01", pid 57749, jiffies 4295163733 (age 6159.670s) hex dump (first 32 bytes):

    02 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<0000000056aca129>] copy_process+0x26c/0x18e0
    alloc_task_struct_node at kernel/fork.c:169
    (inlined by) dup_task_struct at kernel/fork.c:877
    (inlined by) copy_process at kernel/fork.c:1929
    [<00000000bdbbf9f8>] _do_fork+0xac/0xb20
    [<00000000dcb1c445>] __do_sys_clone+0x98/0xe0
    __do_sys_clone at kernel/fork.c:2591
    [<000000006c059205>] ppc_clone+0x8/0xc
    ppc_clone at arch/powerpc/kernel/entry_64.S:479
Quoted 6 lines
Quoted 3 lines

I forgot that get_task_for_clock called by posix_cpu_timer_create returns a reference to a task_struct. Put that reference to avoid the leak.

I took the liberty to fold this back into the affected commit and add a comment why this put_task_struct() is actually required.

Good enough.

We should be able to use rcu and remove the reference entirely. But that is a change for another day.

Eric