Skip to contentSkip to conversation

kernel/exit.c : Fix missing read_unlock

PATCH NAK Maninder Singh 4 participants 6 messages May 21, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/exit.c : Fix missing read_unlock

EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi,

From: Maninder Singh <[email protected]>

Subject: [PATCH 1/1] kernel/exit.c : Fix missing task_unlock

This patch adds missing read_unlock if do_wait_thread or ptrace_do_wait returns non zero.

---
 kernel/exit.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
kernel/exit.c +6 -2
index 22fcc05..31a061f 100644
@@ -1486,12 +1486,16 @@ repeat:
 	tsk = current;
 	do {
 		retval = do_wait_thread(wo, tsk);
-		if (retval)
+		if (retval) {
+			read_unlock(&tasklist_lock);
 			goto end;
+		}
 
 		retval = ptrace_do_wait(wo, tsk);
-		if (retval)
+		if (retval) {
+			read_unlock(&tasklist_lock);
 			goto end;
+		}
 
 		if (wo->wo_flags & __WNOTHREAD)
 			break;
Signature · 4 lines
1.7.1

Thanks 
Maninder Singhÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¥Š{±þG«�éÿŠ{ayºʇڙë,j­¢f£¢·hš�ï�êÿ‘êçz_è®(­éšŽŠÝ¢j"�ú¶m§ÿÿ¾«þG«�éÿ¢¸?™¨è­Ú&£ø§~�á¶iO•æ¬z·švØ^¶m§ÿÿÃÿ¶ìÿ¢¸?–I¥
#1 lore raw
Quoted 30 lines · Maninder Singh

EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi,

From: Maninder Singh <[email protected]>

Subject: [PATCH 1/1] kernel/exit.c : Fix missing task_unlock

This patch adds missing read_unlock if do_wait_thread or ptrace_do_wait returns non zero.

---
 kernel/exit.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
kernel/exit.c +3 -1
index 22fcc05..31a061f 100644
@@ -1486,12 +1486,16 @@ repeat:
 	tsk = current;
 	do {
 		retval = do_wait_thread(wo, tsk);
-		if (retval)
+		if (retval) {
+			read_unlock(&tasklist_lock);
 			goto end;
+		}

kernel/exit.c repeat: · line 1501

Quoted 10 lines
kernel/exit.c +3 -1
 
 		retval = ptrace_do_wait(wo, tsk);
-		if (retval)
+		if (retval) {
+			read_unlock(&tasklist_lock);
 			goto end;
+		}
 
 		if (wo->wo_flags & __WNOTHREAD)
 			break;

That's surprising and the changelog is lacking.

So the last time that code was touched upstream was 7 years ago:

commit 64a16caf5e3417ee32f670debcb5857b02a9e08e
Author: Oleg Nesterov <[email protected]>
Date: Wed Jun 17 16:27:40 2009 -0700

do_wait: simplify retval/tsk_result/notask_error mess

please explain whether what you fix is:

1) an ancient bug that somehow nobody ever triggered (plus analysis of why it wasn't triggered)

2) a new bug introduced by commit XYZ (plus analysis)

3) something else

Thanks,

Ingo

#2 lore raw
Quoted 41 lines · Ingo Molnar
Quoted 38 lines · Maninder Singh

EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi,

From: Maninder Singh <[email protected]>

Subject: [PATCH 1/1] kernel/exit.c : Fix missing task_unlock

This patch adds missing read_unlock if do_wait_thread or ptrace_do_wait returns non zero.

---
 kernel/exit.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
kernel/exit.c +6 -2
index 22fcc05..31a061f 100644
@@ -1486,12 +1486,16 @@ repeat:
      tsk = current;
      do {
              retval = do_wait_thread(wo, tsk);
-             if (retval)
+             if (retval) {
+                     read_unlock(&tasklist_lock);
                      goto end;
+             }
 
              retval = ptrace_do_wait(wo, tsk);
-             if (retval)
+             if (retval) {
+                     read_unlock(&tasklist_lock);
                      goto end;
+             }
 
Quoted 4 lines

kernel/exit.c repeat: · line 1501

Quoted 2 lines
kernel/exit.c +0 -0
              if (wo->wo_flags & __WNOTHREAD)
                      break;

That's surprising <snip>

Still it looks like it is a legitimate change. I don't see where the unlock would be done otherwise.

I do wonder if this would look nicer if the whole locked part would be pulled out into a separate (inline) function. That would render the repeated read_unlock()s unnecessary and possibly also prevent a goto/label mess if that were to be attempted in-line.

Frans

#3 lore raw
Quoted 42 lines · Frans Klaver
Quoted 41 lines · Ingo Molnar
Quoted 38 lines · Maninder Singh

EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi,

From: Maninder Singh <[email protected]>

Subject: [PATCH 1/1] kernel/exit.c : Fix missing task_unlock

This patch adds missing read_unlock if do_wait_thread or ptrace_do_wait returns non zero.

---
 kernel/exit.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
kernel/exit.c +6 -2
index 22fcc05..31a061f 100644
@@ -1486,12 +1486,16 @@ repeat:
      tsk = current;
      do {
              retval = do_wait_thread(wo, tsk);
-             if (retval)
+             if (retval) {
+                     read_unlock(&tasklist_lock);
                      goto end;
+             }
 
              retval = ptrace_do_wait(wo, tsk);
-             if (retval)
+             if (retval) {
+                     read_unlock(&tasklist_lock);
                      goto end;
+             }
 
Quoted 7 lines
Quoted 4 lines

kernel/exit.c repeat: · line 1501

Quoted 2 lines
kernel/exit.c +0 -0
              if (wo->wo_flags & __WNOTHREAD)
                      break;

That's surprising <snip>

Still it looks like it is a legitimate change. I don't see where the unlock would be done otherwise.

No, it does not look like a legitimate change, that's why I asked the questions. I think this patch breaks the kernel badly.

As it is explained in the comments as well, the various wait-loop functions (do_wait_thread(), ptrace_do_wait()) fundamentally unlock the tasklist_lock if they return an error.

NAK.

Thanks,

Ingo

#4 lore raw
Quoted 55 lines · Ingo Molnar
Quoted 1 line · Frans Klaver

On Thu, May 21, 2015 at 8:03 AM, Ingo Molnar <[email protected]>

Quoted 11 lines · wrote:
Quoted 11 lines · Maninder Singh
Quoted 9 lines

EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi,

From: Maninder Singh <[email protected]>

Subject: [PATCH 1/1] kernel/exit.c : Fix missing task_unlock

This patch adds missing read_unlock if do_wait_thread or

ptrace_do_wait

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

returns non zero.

---
 kernel/exit.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
kernel/exit.c +6 -2
index 22fcc05..31a061f 100644
@@ -1486,12 +1486,16 @@ repeat:
      tsk = current;
      do {
              retval = do_wait_thread(wo, tsk);
-             if (retval)
+             if (retval) {
+                     read_unlock(&tasklist_lock);
                      goto end;
+             }
 
              retval = ptrace_do_wait(wo, tsk);
-             if (retval)
+             if (retval) {
+                     read_unlock(&tasklist_lock);
                      goto end;
+             }
 
Quoted 7 lines
Quoted 4 lines

kernel/exit.c repeat: · line 1501

Quoted 2 lines
kernel/exit.c +0 -0
              if (wo->wo_flags & __WNOTHREAD)
                      break;

That's surprising <snip>

Still it looks like it is a legitimate change. I don't see where the unlock would be done otherwise.

Quoted 6 lines

No, it does not look like a legitimate change, that's why I asked the questions. I think this patch breaks the kernel badly.

As it is explained in the comments as well, the various wait-loop functions (do_wait_thread(), ptrace_do_wait()) fundamentally unlock the tasklist_lock if they return an error.

Ah, right. Given that I agree. I can imagine a static checker not seeing that either. Sorry for the noise here.

Thanks,
Frans

#5 lore raw
In reply to Maninder Singh
Quoted 10 lines · Maninder Singh

EP-F6AA0618C49C4AEDA73BFF1B39950BAB
Hi,

From: Maninder Singh <[email protected]>

Subject: [PATCH 1/1] kernel/exit.c : Fix missing task_unlock

This patch adds missing read_unlock if do_wait_thread or ptrace_do_wait returns non zero.

Confused...

wait_consider_task() should drop tasklist_lock if it returns non-zero?

Quoted 8 lines
kernel/exit.c +1 -1
@@ -1486,12 +1486,16 @@ repeat:
 	tsk = current;
 	do {
 		retval = do_wait_thread(wo, tsk);
-		if (retval)
+		if (retval) {

kernel/exit.c repeat: · line 1498

Quoted 10 lines
kernel/exit.c +5 -1
+			read_unlock(&tasklist_lock);
 			goto end;
+		}
 
 		retval = ptrace_do_wait(wo, tsk);
-		if (retval)
+		if (retval) {
+			read_unlock(&tasklist_lock);
 			goto end;
+		}

Well, the patch is obviously wrong. Because, again, tasklist_lock was already unlocked if (say) wait_task_zombie() reaps a child.

If you think there is a case which forgets to unlock, please tell us more.

Oleg.