Skip to contentSkip to conversation

ptrace-vs-PREEMPT_RT and freezer rewrite

PATCH In review Peter Zijlstra 14 participants 240 messages Apr 21, 2022 – Jul 16, 2022 1 message not in the archive Page 2 of 2

Subsystems (34)
lore
Share & tools
lore
#148 lore raw

Re: [PATCH v4 12/12] sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

In reply to Peter Zijlstra
Quoted 35 lines · Peter Zijlstra
Quoted 28 lines · Sven Schnelle
Quoted 8 lines
Quoted 1 line
[   86.218551] kill_chi-343805    6d.... 79990141us : ptrace_stop: JOBCTL_TRACED already set, state=0 <------ valid combination of flags?

Yeah, that's not supposed to be so. JOBCTL_TRACED is supposed to follow __TASK_TRACED for now. Set when __TASK_TRACED, cleared when TASK_RUNNING.

Specifically {ptrace_,}signal_wake_up() in signal.h clear JOBCTL_TRACED when they would wake a __TASK_TRACED task.

try_to_wake_up() clears TASK_TRACED in this case because a signal (SIGKILL) has to be delivered. As a test I put the following change on top, and it "fixes" the problem:

kernel/sched/core.c +3 -0
index da0bf6fe9ecd..f2e0f5e70e77 100644
@@ -4141,6 +4149,9 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
         * TASK_WAKING such that we can unlock p->pi_lock before doing the
         * enqueue, such as ttwu_queue_wakelist().
         */
+       if (p->__state & TASK_TRACED)
+               trace_printk("clearing TASK_TRACED 2\n");
+       p->jobctl &= ~JOBCTL_TRACED;
        WRITE_ONCE(p->__state, TASK_WAKING);
 
        /*
Quoted 2 lines

There are several places where the state is changed from TASK_TRACED to something else without clearing JOBCTL_TRACED.

I'm having difficulty spotting them; I find:

Quoted 7 lines

TASK_WAKEKILL: signal_wake_up()
__TASK_TRACED: ptrace_signal_wake_up(), ptrace_unfreeze_traced(), ptrace_resume()

And all those sites dutifully clear JOBCTL_TRACED.

I'd be most interested in the calstack for the 'clearing TASK_TRACED 2' events to see where we miss a spot.

The calltrace is:

[    9.863613] Call Trace:
[    9.863616]  [<00000000d3105f0e>] try_to_wake_up+0xae/0x620
[    9.863620] ([<00000000d3106164>] try_to_wake_up+0x304/0x620)
[    9.863623]  [<00000000d30d1e46>] ptrace_unfreeze_traced+0x9e/0xa8
[    9.863629]  [<00000000d30d2ef0>] __s390x_sys_ptrace+0xc0/0x160
[    9.863633]  [<00000000d3c5d8f4>] __do_syscall+0x1d4/0x200
[    9.863678]  [<00000000d3c6c332>] system_call+0x82/0xb0
[    9.863685] Last Breaking-Event-Address:
[    9.863686]  [<00000000d3106176>] try_to_wake_up+0x316/0x620
[    9.863688] ---[ end trace 0000000000000000 ]---

ptrace_unfreeze_traced() is:

static void ptrace_unfreeze_traced(struct task_struct *task)

{
        unsigned long flags;

        /*
         * The child may be awake and may have cleared
         * JOBCTL_PTRACE_FROZEN (see ptrace_resume).  The child will
         * not set JOBCTL_PTRACE_FROZEN or enter __TASK_TRACED anew.
         */
        if (lock_task_sighand(task, &flags)) {
                task->jobctl &= ~JOBCTL_PTRACE_FROZEN;
                if (__fatal_signal_pending(task)) {
                        task->jobctl &= ~TASK_TRACED;

Looking at this, shouldn't the line above read task->jobctl &= ~JOBCTL_TRACED?

                        wake_up_state(task, __TASK_TRACED);
                }
                unlock_task_sighand(task, &flags);
        }
}
author maintainer
#149 lore raw

Re: [PATCH v4 12/12] sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

Quoted 73 lines · Sven Schnelle
Quoted 35 lines · Peter Zijlstra
Quoted 28 lines · Sven Schnelle
Quoted 8 lines
Quoted 1 line
[   86.218551] kill_chi-343805    6d.... 79990141us : ptrace_stop: JOBCTL_TRACED already set, state=0 <------ valid combination of flags?

Yeah, that's not supposed to be so. JOBCTL_TRACED is supposed to follow __TASK_TRACED for now. Set when __TASK_TRACED, cleared when TASK_RUNNING.

Specifically {ptrace_,}signal_wake_up() in signal.h clear JOBCTL_TRACED when they would wake a __TASK_TRACED task.

try_to_wake_up() clears TASK_TRACED in this case because a signal (SIGKILL) has to be delivered. As a test I put the following change on top, and it "fixes" the problem:

kernel/sched/core.c +3 -0
index da0bf6fe9ecd..f2e0f5e70e77 100644
@@ -4141,6 +4149,9 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
         * TASK_WAKING such that we can unlock p->pi_lock before doing the
         * enqueue, such as ttwu_queue_wakelist().
         */
+       if (p->__state & TASK_TRACED)
+               trace_printk("clearing TASK_TRACED 2\n");
+       p->jobctl &= ~JOBCTL_TRACED;
        WRITE_ONCE(p->__state, TASK_WAKING);
 
        /*
Quoted 2 lines

There are several places where the state is changed from TASK_TRACED to something else without clearing JOBCTL_TRACED.

I'm having difficulty spotting them; I find:

Quoted 7 lines

TASK_WAKEKILL: signal_wake_up()
__TASK_TRACED: ptrace_signal_wake_up(), ptrace_unfreeze_traced(), ptrace_resume()

And all those sites dutifully clear JOBCTL_TRACED.

I'd be most interested in the calstack for the 'clearing TASK_TRACED 2' events to see where we miss a spot.

The calltrace is:

[    9.863613] Call Trace:
[    9.863616]  [<00000000d3105f0e>] try_to_wake_up+0xae/0x620
[    9.863620] ([<00000000d3106164>] try_to_wake_up+0x304/0x620)
[    9.863623]  [<00000000d30d1e46>] ptrace_unfreeze_traced+0x9e/0xa8
[    9.863629]  [<00000000d30d2ef0>] __s390x_sys_ptrace+0xc0/0x160
[    9.863633]  [<00000000d3c5d8f4>] __do_syscall+0x1d4/0x200
[    9.863678]  [<00000000d3c6c332>] system_call+0x82/0xb0
[    9.863685] Last Breaking-Event-Address:
[    9.863686]  [<00000000d3106176>] try_to_wake_up+0x316/0x620
[    9.863688] ---[ end trace 0000000000000000 ]---

ptrace_unfreeze_traced() is:

static void ptrace_unfreeze_traced(struct task_struct *task)

{
        unsigned long flags;

        /*
         * The child may be awake and may have cleared
         * JOBCTL_PTRACE_FROZEN (see ptrace_resume).  The child will
         * not set JOBCTL_PTRACE_FROZEN or enter __TASK_TRACED anew.
         */
        if (lock_task_sighand(task, &flags)) {
                task->jobctl &= ~JOBCTL_PTRACE_FROZEN;
                if (__fatal_signal_pending(task)) {
                        task->jobctl &= ~TASK_TRACED;
Quoted 1 line

Looking at this, shouldn't the line above read task->jobctl &= ~JOBCTL_TRACED?

YES! Absolutely.

Quoted 5 lines
                        wake_up_state(task, __TASK_TRACED);
                }
                unlock_task_sighand(task, &flags);
        }
}
maintainer
#150 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

In reply to Eric W. Biederman
Quoted 14 lines · Eric W. Biederman

Eric W. Biederman (11):
signal: Rename send_signal send_signal_locked
signal: Replace __group_send_sig_info with send_signal_locked ptrace/um: Replace PT_DTRACE with TIF_SINGLESTEP ptrace/xtensa: Replace PT_SINGLESTEP with TIF_SINGLESTEP
ptrace: Remove arch_ptrace_attach
signal: Use lockdep_assert_held instead of assert_spin_locked
ptrace: Reimplement PTRACE_KILL by always sending SIGKILL
ptrace: Document that wait_task_inactive can't fail
ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs
ptrace: Don't change __state
ptrace: Always take siglock in ptrace_resume

Quoted 2 lines

Peter Zijlstra (1):
sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

I can't comment 5/12. to be honest I didn't even try to look into arch/ia64/.

But other than that I see no problems in this version. However, I'd like to actually apply the whole series and read the changed code carefully, but sorry, I don't think I can do this before Monday.

Oleg.

#151 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

Quoted 12 lines · Oleg Nesterov
Quoted 11 lines · Eric W. Biederman

Eric W. Biederman (11): signal: Rename send_signal send_signal_locked
signal: Replace __group_send_sig_info with send_signal_locked ptrace/um: Replace PT_DTRACE with TIF_SINGLESTEP ptrace/xtensa:
Replace PT_SINGLESTEP with TIF_SINGLESTEP ptrace: Remove arch_ptrace_attach signal: Use lockdep_assert_held instead of assert_spin_locked ptrace: Reimplement PTRACE_KILL by always sending SIGKILL ptrace: Document that wait_task_inactive can't fail ptrace:
Admit ptrace_stop can generate spuriuos SIGTRAPs ptrace: Don't change __state ptrace: Always take siglock in ptrace_resume

Quoted 5 lines
Quoted 2 lines

Peter Zijlstra (1):
sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

I can't comment 5/12. to be honest I didn't even try to look into arch/ia64/.

I just looked at arch_ptrace_attach again and I spotted what looks like a fairly easy analysis that is mostly arch-generic code that shows this is dead code on ia64.

On ia64 arch_ptrace_attach is ptrace_attach_sync_user_rbs, and does nothing if __state is not TASK_STOPPED.

When arch_ptrace_attach is called after ptrace_traceme __state is TASK_RUNNING pretty much by definition as we are running in the child. Therefore ptrace_attach_sync_user_rbs does nothing in that case.

When arch_ptrace_attach is called after ptrace_attach __state there are two possibilities. If the tracee was already in TASK_STOPPED before the ptrace_attach, the tracee will be in TASK_TRACED. Otherwise the tracee will be in TASK_TRACED or on it's way to stopping in TASK_TRACED.

Unless I totally misread ptrace_attach. There is no way that after a successful ptrace_attach for the tracee to be in TASK_STOPPED. This makes ptrace_attach_sync_user_rbs a big noop, AKA dead code. So it can be removed.

Quoted 3 lines

But other than that I see no problems in this version. However, I'd like to actually apply the whole series and read the changed code carefully, but sorry, I don't think I can do this before Monday.

No rush. I don't expect the merge window will open for a while yet.

Eric

Tested-by on the v4 series
#152 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

In reply to Eric W. Biederman
Quoted 24 lines · Eric W. Biederman

The states TASK_STOPPED and TASK_TRACE are special in they can not handle spurious wake-ups. This plus actively depending upon and changing the value of tsk->__state causes problems for PREEMPT_RT and Peter's freezer rewrite.

There are a lot of details we have to get right to sort out the technical challenges and this is my parred back version of the changes that contains just those problems I see good solutions to that I believe are ready.

A couple of issues have been pointed but I think this parred back set of changes is still on the right track. The biggest change in v4 is the split of "ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs" into two patches because the dependency I thought exited between two different changes did not exist. The rest of the changes are minor tweaks to "ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs"; removing an always true branch, and adding an early test to see if the ptracer had gone, before TASK_TRAPPING was set.

This set of changes should support Peter's freezer rewrite, and with the addition of changing wait_task_inactive(TASK_TRACED) to be wait_task_inactive(0) in ptrace_check_attach I don't think there are any races or issues to be concerned about from the ptrace side.

Quoted 5 lines

More work is needed to support PREEMPT_RT, but these changes get things closer.

This set of changes continues to look like it will provide a firm foundation for solving the PREEMPT_RT and freezer challenges.

One of the more sensitive projects to changes around ptrace is rr (Robert and Kyle added to CC). I ran rr's selftests before/after this series and saw no changes. My failures remained the same; I assume they're due to missing CPU features (pkeys) or build configs (bpf), etc:

99% tests passed, 19 tests failed out of 2777

Total Test time (real) = 773.40 sec

The following tests FAILED:

         42 - bpf_map (Failed)
         43 - bpf_map-no-syscallbuf (Failed)
        414 - netfilter (Failed)
        415 - netfilter-no-syscallbuf (Failed)
        454 - x86/pkeys (Failed)
        455 - x86/pkeys-no-syscallbuf (Failed)
        1152 - ttyname (Failed)
        1153 - ttyname-no-syscallbuf (Failed)
        1430 - bpf_map-32 (Failed)
        1431 - bpf_map-32-no-syscallbuf (Failed)
        1502 - detach_sigkill-32 (Failed)
        1802 - netfilter-32 (Failed)
        1803 - netfilter-32-no-syscallbuf (Failed)
        1842 - x86/pkeys-32 (Failed)
        1843 - x86/pkeys-32-no-syscallbuf (Failed)
        2316 - crash_in_function-32 (Failed)
        2317 - crash_in_function-32-no-syscallbuf (Failed)
        2540 - ttyname-32 (Failed)
        2541 - ttyname-32-no-syscallbuf (Failed)

So, I guess:

:)

Signature · 1 line
Kees Cook
#153 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

Quoted 61 lines · Kees Cook
Quoted 24 lines · Eric W. Biederman

The states TASK_STOPPED and TASK_TRACE are special in they can not handle spurious wake-ups. This plus actively depending upon and changing the value of tsk->__state causes problems for PREEMPT_RT and Peter's freezer rewrite.

There are a lot of details we have to get right to sort out the technical challenges and this is my parred back version of the changes that contains just those problems I see good solutions to that I believe are ready.

A couple of issues have been pointed but I think this parred back set of changes is still on the right track. The biggest change in v4 is the split of "ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs" into two patches because the dependency I thought exited between two different changes did not exist. The rest of the changes are minor tweaks to "ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs"; removing an always true branch, and adding an early test to see if the ptracer had gone, before TASK_TRAPPING was set.

This set of changes should support Peter's freezer rewrite, and with the addition of changing wait_task_inactive(TASK_TRACED) to be wait_task_inactive(0) in ptrace_check_attach I don't think there are any races or issues to be concerned about from the ptrace side.

Quoted 5 lines

More work is needed to support PREEMPT_RT, but these changes get things closer.

This set of changes continues to look like it will provide a firm foundation for solving the PREEMPT_RT and freezer challenges.

One of the more sensitive projects to changes around ptrace is rr (Robert and Kyle added to CC). I ran rr's selftests before/after this series and saw no changes. My failures remained the same; I assume they're due to missing CPU features (pkeys) or build configs (bpf), etc:

99% tests passed, 19 tests failed out of 2777

Total Test time (real) = 773.40 sec

The following tests FAILED:

         42 - bpf_map (Failed)
         43 - bpf_map-no-syscallbuf (Failed)
        414 - netfilter (Failed)
        415 - netfilter-no-syscallbuf (Failed)
        454 - x86/pkeys (Failed)
        455 - x86/pkeys-no-syscallbuf (Failed)
        1152 - ttyname (Failed)
        1153 - ttyname-no-syscallbuf (Failed)
        1430 - bpf_map-32 (Failed)
        1431 - bpf_map-32-no-syscallbuf (Failed)
        1502 - detach_sigkill-32 (Failed)
        1802 - netfilter-32 (Failed)
        1803 - netfilter-32-no-syscallbuf (Failed)
        1842 - x86/pkeys-32 (Failed)
        1843 - x86/pkeys-32-no-syscallbuf (Failed)
        2316 - crash_in_function-32 (Failed)
        2317 - crash_in_function-32-no-syscallbuf (Failed)
        2540 - ttyname-32 (Failed)
        2541 - ttyname-32-no-syscallbuf (Failed)
Quoted 5 lines

So, I guess:

:)

Thank you. I was thinking it would be good to add the rr folks to the discussion.

Eric

maintainer Reviewed-by on the v4 series
#154 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

In reply to Eric W. Biederman
Quoted 14 lines · Eric W. Biederman

Eric W. Biederman (11):
signal: Rename send_signal send_signal_locked
signal: Replace __group_send_sig_info with send_signal_locked ptrace/um: Replace PT_DTRACE with TIF_SINGLESTEP ptrace/xtensa: Replace PT_SINGLESTEP with TIF_SINGLESTEP
ptrace: Remove arch_ptrace_attach
signal: Use lockdep_assert_held instead of assert_spin_locked
ptrace: Reimplement PTRACE_KILL by always sending SIGKILL
ptrace: Document that wait_task_inactive can't fail
ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs
ptrace: Don't change __state
ptrace: Always take siglock in ptrace_resume

Quoted 2 lines

Peter Zijlstra (1):
sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

OK, lgtm.

I still dislike you removed TASK_WAKEKILL from TASK_TRACED, but I can't find a good argument against it ;) and yes, this is subjective.

Oleg.

#155 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

Quoted 18 lines · Oleg Nesterov
Quoted 14 lines · Eric W. Biederman

Eric W. Biederman (11):
signal: Rename send_signal send_signal_locked
signal: Replace __group_send_sig_info with send_signal_locked ptrace/um: Replace PT_DTRACE with TIF_SINGLESTEP ptrace/xtensa: Replace PT_SINGLESTEP with TIF_SINGLESTEP
ptrace: Remove arch_ptrace_attach
signal: Use lockdep_assert_held instead of assert_spin_locked
ptrace: Reimplement PTRACE_KILL by always sending SIGKILL
ptrace: Document that wait_task_inactive can't fail
ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs
ptrace: Don't change __state
ptrace: Always take siglock in ptrace_resume

Quoted 2 lines

Peter Zijlstra (1):
sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

Quoted 7 lines

OK, lgtm.

I still dislike you removed TASK_WAKEKILL from TASK_TRACED, but I can't find a good argument against it ;) and yes, this is subjective.

Does anyone else have any comments on this patchset?

If not I am going to apply this to a branch and get it into linux-next.

Eric

#156 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

Quoted 3 lines · Eric W. Biederman

Does anyone else have any comments on this patchset?

If not I am going to apply this to a branch and get it into linux-next.

Looks good I guess.
Be aware that there will be clash due to
https://lore.kernel.org/all/[email protected]/

which sits currently in -akpm.

Quoted 1 line

Eric

Sebastian

#157 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

Quoted 10 lines · Sebastian Andrzej Siewior
Quoted 3 lines · Eric W. Biederman

Does anyone else have any comments on this patchset?

If not I am going to apply this to a branch and get it into linux-next.

Looks good I guess.
Be aware that there will be clash due to
https://lore.kernel.org/all/[email protected]/

which sits currently in -akpm.

Thanks for the heads up. That looks like the best kind of conflict. One where code just disappears.

Eric

#158 lore raw

Re: [PATCH v4 0/12] ptrace: cleaning up ptrace_stop

In reply to Eric W. Biederman
Quoted 25 lines · Eric W. Biederman
Quoted 23 lines · Oleg Nesterov
Quoted 14 lines · Eric W. Biederman

Eric W. Biederman (11):
signal: Rename send_signal send_signal_locked
signal: Replace __group_send_sig_info with send_signal_locked ptrace/um: Replace PT_DTRACE with TIF_SINGLESTEP ptrace/xtensa: Replace PT_SINGLESTEP with TIF_SINGLESTEP
ptrace: Remove arch_ptrace_attach
signal: Use lockdep_assert_held instead of assert_spin_locked
ptrace: Reimplement PTRACE_KILL by always sending SIGKILL
ptrace: Document that wait_task_inactive can't fail
ptrace: Admit ptrace_stop can generate spuriuos SIGTRAPs
ptrace: Don't change __state
ptrace: Always take siglock in ptrace_resume

Quoted 2 lines

Peter Zijlstra (1):
sched,signal,ptrace: Rework TASK_TRACED, TASK_STOPPED state

OK, lgtm.

Quoted 6 lines
Quoted 2 lines

I still dislike you removed TASK_WAKEKILL from TASK_TRACED, but I can't find a good argument against it ;) and yes, this is subjective.

Does anyone else have any comments on this patchset?

If not I am going to apply this to a branch and get it into linux-next.

Thank you all.

I have pushed this to my ptrace_stop-cleanup-for-v5.19 branch and placed the branch in linux-next.

Eric

#159 lore raw

cover ptrace: cleanups and calling do_cldstop with only siglock

In reply to Eric W. Biederman

For ptrace_stop to work on PREEMT_RT no spinlocks can be taken once ptrace_freeze_traced has completed successfully. Which fundamentally means the lock dance of dropping siglock and grabbing tasklist_lock does not work on PREEMPT_RT. So I have worked through what is necessary so that tasklist_lock does not need to be grabbed in ptrace_stop after siglock is dropped.

I have explored several alternate ways of getting there and along the way I found a lot of small bug fixes/cleanups that don't necessarily contribute to the final result but that or worthwhile on their own. So I have included those changes in this set of changes just so they don't get lost.

In addition I had a conversation with Thomas Gleixner recently that emphasized for me the need to reduce the hold times of tasklist_lock, and that made me realize that in principle it is possible.
https://lkml.kernel.org/r/[email protected]

Which is a long way of saying that not taking tasklist_lock in ptrace_stop is good not just for PREMPT_RT but also for improving the scalability of the kernel in general.

After this set of changes only cgroup_enter_frozen should remain a stumbling block for PREEMPT_RT in the ptrace_stop path.

Eric W. Biederman (16):
signal/alpha: Remove unused definition of TASK_REAL_PARENT signal/ia64: Remove unused definition of IA64_TASK_REAL_PARENT_OFFSET
kdb: Use real_parent when displaying a list of processes powerpc/xmon: Use real_parent when displaying a list of processes
ptrace: Remove dead code from __ptrace_detach
ptrace: Remove unnecessary locking in ptrace_(get|set)siginfo
signal: Wake up the designated parent
ptrace: Only populate last_siginfo from ptrace
ptrace: In ptrace_setsiginfo deal with invalid si_signo
ptrace: In ptrace_signal look at what the debugger did with siginfo
ptrace: Use si_sino as the signal number to resume with
ptrace: Stop protecting ptrace_set_signr with tasklist_lock
ptrace: Document why ptrace_setoptions does not need a lock
signal: Protect parent child relationships by childs siglock
ptrace: Use siglock instead of tasklist_lock in ptrace_check_attach
signal: Always call do_notify_parent_cldstop with siglock held

 arch/alpha/kernel/asm-offsets.c |   1 -
 arch/ia64/kernel/asm-offsets.c  |   1 -
 arch/powerpc/xmon/xmon.c        |   2 +-
 kernel/debug/kdb/kdb_main.c     |   2 +-
 kernel/exit.c                   |  23 +++-
 kernel/fork.c                   |  12 +-
 kernel/ptrace.c                 | 132 ++++++++----------
 kernel/signal.c                 | 296 ++++++++++++++++++++++++++--------------
 8 files changed, 279 insertions(+), 190 deletions(-)

Eric

#160 lore raw

PATCH 1/16 signal/alpha: Remove unused definition of TASK_REAL_PARENT

Rather than update this defition when I move tsk->real_parent into signal_struct remove it now.

Cc Richard Henderson <[email protected]>
Cc Ivan Kokshaysky <[email protected]>
Cc Matt Turner <[email protected]>
---
 arch/alpha/kernel/asm-offsets.c | 1 -
 1 file changed, 1 deletion(-)
arch/alpha/kernel/asm-offsets.c +0 -1
index 2e125e5c1508..0fca99dc5757 100644
@@ -21,7 +21,6 @@ void foo(void)
 
         DEFINE(TASK_BLOCKED, offsetof(struct task_struct, blocked));
         DEFINE(TASK_CRED, offsetof(struct task_struct, cred));
-        DEFINE(TASK_REAL_PARENT, offsetof(struct task_struct, real_parent));
         DEFINE(TASK_GROUP_LEADER, offsetof(struct task_struct, group_leader));
         DEFINE(TASK_TGID, offsetof(struct task_struct, tgid));
         BLANK();
Signature · 1 line
2.35.3
#161 lore raw

PATCH 2/16 signal/ia64: Remove unused definition of IA64_TASK_REAL_PARENT_OFFSET

In reply to Eric W. Biederman

Rather than update the unused definition of IA64_TASK_REAL_PARENT_OFFSENT when I move tsk->real_parent into signal_struct remove it now.

---
 arch/ia64/kernel/asm-offsets.c | 1 -
 1 file changed, 1 deletion(-)
arch/ia64/kernel/asm-offsets.c +0 -1
index be3b90fef2e9..245c4333ea30 100644
@@ -55,7 +55,6 @@ void foo(void)
 	DEFINE(IA64_PID_UPID_OFFSET, offsetof (struct pid, numbers[0]));
 	DEFINE(IA64_TASK_PENDING_OFFSET,offsetof (struct task_struct, pending));
 	DEFINE(IA64_TASK_PID_OFFSET, offsetof (struct task_struct, pid));
-	DEFINE(IA64_TASK_REAL_PARENT_OFFSET, offsetof (struct task_struct, real_parent));
 	DEFINE(IA64_TASK_SIGNAL_OFFSET,offsetof (struct task_struct, signal));
 	DEFINE(IA64_TASK_TGID_OFFSET, offsetof (struct task_struct, tgid));
 	DEFINE(IA64_TASK_THREAD_KSP_OFFSET, offsetof (struct task_struct, thread.ksp));
Signature · 1 line
2.35.3
#162 lore raw

PATCH 3/16 kdb: Use real_parent when displaying a list of processes

In reply to Eric W. Biederman

kdb has a bug that when using the ps command to display a list of processes, if a process is being debugged the debugger as the parent process.

This is silly, and I expect it never comes up in ptractice. As there is very little point in using gdb and kdb simultaneously. Update the code to use real_parent so that it is clear kdb does not want to display a debugger as the parent of a process.

Cc Jason Wessel <[email protected]>
Cc Daniel Thompson <[email protected]>
Cc Douglas Anderson <[email protected]>
Fixes 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)"
---
 kernel/debug/kdb/kdb_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/debug/kdb/kdb_main.c +1 -1
index 0852a537dad4..db49f1026eaa 100644
@@ -2306,7 +2306,7 @@ void kdb_ps1(const struct task_struct *p)
 
 	cpu = kdb_process_cpu(p);
 	kdb_printf("0x%px %8d %8d  %d %4d   %c  0x%px %c%s\n",
-		   (void *)p, p->pid, p->parent->pid,
+		   (void *)p, p->pid, p->real_parent->pid,
 		   kdb_task_has_cpu(p), kdb_process_cpu(p),
 		   kdb_task_state_char(p),
 		   (void *)(&p->thread),
Signature · 1 line
2.35.3
author maintainer
#163 lore raw

Re: [PATCH 03/16] kdb: Use real_parent when displaying a list of processes

Quoted 5 lines · Eric W. Biederman

kdb has a bug that when using the ps command to display a list of processes, if a process is being debugged the debugger as the parent process.

This is silly, and I expect it never comes up in ptractice. As there

                                                   ^^^^^^^^^

Lol, love the new word :-)

#164 lore raw

Re: [PATCH 03/16] kdb: Use real_parent when displaying a list of processes

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Eric W. Biederman

kdb has a bug that when using the ps command to display a list of processes, if a process is being debugged the debugger as the parent process.

This is silly, and I expect it never comes up in ptractice. As there

                                                   ^^^^^^^^^

Lol, love the new word :-)

It wasn't intentional but now I just might have to keep it.

Eric

reviewer
#165 lore raw

Re: [PATCH 03/16] kdb: Use real_parent when displaying a list of processes

In reply to Eric W. Biederman

Hi,

Quoted 9 lines · Eric W. Biederman

kdb has a bug that when using the ps command to display a list of processes, if a process is being debugged the debugger as the parent process.

This is silly, and I expect it never comes up in ptractice. As there is very little point in using gdb and kdb simultaneously. Update the code to use real_parent so that it is clear kdb does not want to display a debugger as the parent of a process.

So I would tend to defer to Daniel, but I'm not convinced that the behavior you describe for kdb today _is_ actually silly.

If I was in kdb and I was listing processes, I might actually want to see that a process's parent was set to gdb. Presumably that would tell me extra information that might be relevant to my debug session.

Personally, I'd rather add an extra piece of information into the list showing the real parent if it's not the same as the parent. Then you're not throwing away information.

-Doug

#166 lore raw

Re: [PATCH 03/16] kdb: Use real_parent when displaying a list of processes

Quoted 16 lines · Doug Anderson

Hi,

Quoted 9 lines · Eric W. Biederman

kdb has a bug that when using the ps command to display a list of processes, if a process is being debugged the debugger as the parent process.

This is silly, and I expect it never comes up in ptractice. As there is very little point in using gdb and kdb simultaneously. Update the code to use real_parent so that it is clear kdb does not want to display a debugger as the parent of a process.

So I would tend to defer to Daniel, but I'm not convinced that the behavior you describe for kdb today _is_ actually silly.

Quoted 7 lines

If I was in kdb and I was listing processes, I might actually want to see that a process's parent was set to gdb. Presumably that would tell me extra information that might be relevant to my debug session.

Personally, I'd rather add an extra piece of information into the list showing the real parent if it's not the same as the parent. Then you're not throwing away information.

The name of the field is confusing for anyone who isn't intimate with the implementation details. The function getppid returns tsk->real_parent->tgid.

If kdb wants information of what the tracer is that is fine, but I recommend putting that information in another field.

Given that the original description says give the information that ps gives my sense is that kdb is currently wrong. Especially as it does not give you the actual parentage anywhere.

I can certainly be convinced, but I do want some clarity. It looks very attractive to rename task->parent to task->ptracer and leave the field NULL when there is no tracer.

Eric

reviewer
#167 lore raw

Re: [PATCH 03/16] kdb: Use real_parent when displaying a list of processes

Hi,

Quoted 34 lines · Eric W. Biederman
Quoted 16 lines · Doug Anderson

Hi,

Quoted 9 lines · Eric W. Biederman

kdb has a bug that when using the ps command to display a list of processes, if a process is being debugged the debugger as the parent process.

This is silly, and I expect it never comes up in ptractice. As there is very little point in using gdb and kdb simultaneously. Update the code to use real_parent so that it is clear kdb does not want to display a debugger as the parent of a process.

So I would tend to defer to Daniel, but I'm not convinced that the behavior you describe for kdb today _is_ actually silly.

Quoted 7 lines

If I was in kdb and I was listing processes, I might actually want to see that a process's parent was set to gdb. Presumably that would tell me extra information that might be relevant to my debug session.

Personally, I'd rather add an extra piece of information into the list showing the real parent if it's not the same as the parent. Then you're not throwing away information.

The name of the field is confusing for anyone who isn't intimate with the implementation details. The function getppid returns tsk->real_parent->tgid.

If kdb wants information of what the tracer is that is fine, but I recommend putting that information in another field.

Quoted 7 lines

Given that the original description says give the information that ps gives my sense is that kdb is currently wrong. Especially as it does not give you the actual parentage anywhere.

I can certainly be convinced, but I do want some clarity. It looks very attractive to rename task->parent to task->ptracer and leave the field NULL when there is no tracer.

Fair enough. You can consider my objection rescinded.

Presumably, though, you're hoping for an Ack for your patch and you plan to take it with the rest of the series. That's going to need to come from Daniel anyway as he is the actual maintainer. I'm just the peanut gallery. ;-)

-Doug

#168 lore raw

PATCH 4/16 powerpc/xmon: Use real_parent when displaying a list of processes

In reply to Eric W. Biederman

xmon has a bug (copied from kdb) that when showing a list of processes the debugger is listed as the parent, if a processes is being debugged.

This is silly, and I expect it is rare enough no has noticed in practice. Update the code to use real_parent so that it is clear xmon does not want to display a debugger as the parent of a process.

Cc Douglas Miller <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
Fixes 6dfb54049f9a ("powerpc/xmon: Add xmon command to dump process/task similar to ps(1)")
---
 arch/powerpc/xmon/xmon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/xmon/xmon.c +1 -1
index fd72753e8ad5..b308ef9ce604 100644
@@ -3282,7 +3282,7 @@ static void show_task(struct task_struct *volatile tsk)
 
 	printf("%16px %16lx %16px %6d %6d %c %2d %s\n", tsk,
 		tsk->thread.ksp, tsk->thread.regs,
-		tsk->pid, rcu_dereference(tsk->parent)->pid,
+		tsk->pid, rcu_dereference(tsk->real_parent)->pid,
 		state, task_cpu(tsk),
 		tsk->comm);
 }
Signature · 1 line
2.35.3
#169 lore raw

PATCH 5/16 ptrace: Remove dead code from __ptrace_detach

In reply to Eric W. Biederman

Ever since commit 28d838cc4dfe ("Fix ptrace self-attach rule") it has been impossible to attach another thread in the same thread group.

Remove the code from __ptrace_detach that was trying to support detaching from a thread in the same thread group. The code is dead and I can not make sense of what it is trying to do.

---
 kernel/ptrace.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
kernel/ptrace.c +3 -21
index 328a34a99124..ca0e47691229 100644
@@ -526,19 +526,6 @@ static int ptrace_traceme(void)
 	return ret;
 }
 
-/*
- * Called with irqs disabled, returns true if childs should reap themselves.
- */
-static int ignoring_children(struct sighand_struct *sigh)
-{
-	int ret;
-	spin_lock(&sigh->siglock);
-	ret = (sigh->action[SIGCHLD-1].sa.sa_handler = SIG_IGN) ||
-	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
-	spin_unlock(&sigh->siglock);
-	return ret;
-}
-
 /*
  * Called with tasklist_lock held for writing.
  * Unlink a traced task, and clean it up if it was a traced zombie.
@@ -565,14 +552,9 @@ static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
 
 	dead = !thread_group_leader(p);
 
-	if (!dead && thread_group_empty(p)) {
-		if (!same_thread_group(p->real_parent, tracer))
-			dead = do_notify_parent(p, p->exit_signal);
-		else if (ignoring_children(tracer->sighand)) {
-			__wake_up_parent(p, tracer);
-			dead = true;
-		}
-	}
+	if (!dead && thread_group_empty(p))
+		dead = do_notify_parent(p, p->exit_signal);
+
 	/* Mark it as in the process of being reaped. */
 	if (dead)
 		p->exit_state = EXIT_DEAD;
Signature · 1 line
2.35.3
maintainer
#170 lore raw

Re: [PATCH 05/16] ptrace: Remove dead code from __ptrace_detach

Sorry for delay.

Quoted 6 lines · Eric W. Biederman

Ever since commit 28d838cc4dfe ("Fix ptrace self-attach rule") it has been impossible to attach another thread in the same thread group.

Remove the code from __ptrace_detach that was trying to support detaching from a thread in the same thread group.

may be I am totally confused, but I think you misunderstood this code and thus this patch is very wrong.

The same_thread_group() check does NOT try to check if debugger and tracee is in the same thread group, this is indeed impossible.

We need this check to know if the tracee was ptrace_reparented() before __ptrace_unlink() or not.

Quoted 9 lines
kernel/ptrace.c +0 -9
-static int ignoring_children(struct sighand_struct *sigh)
-{
-	int ret;
-	spin_lock(&sigh->siglock);
-	ret = (sigh->action[SIGCHLD-1].sa.sa_handler = SIG_IGN) ||
-	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
-	spin_unlock(&sigh->siglock);
-	return ret;
-}

...

kernel/ptrace.c static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) · line 575

Quoted 12 lines
kernel/ptrace.c +0 -8
@@ -565,14 +552,9 @@ static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
 
 	dead = !thread_group_leader(p);
 
-	if (!dead && thread_group_empty(p)) {
-		if (!same_thread_group(p->real_parent, tracer))
-			dead = do_notify_parent(p, p->exit_signal);
-		else if (ignoring_children(tracer->sighand)) {
-			__wake_up_parent(p, tracer);
-			dead = true;
-		}
-	}

So the code above does:

- if !same_thread_group(p->real_parent, tracer), then the tracee was ptrace_reparented(), and now we need to notify its natural parent to let it know it has a zombie child.

- otherwise, the tracee is our natural child, and it is actually dead. however, since we are going to reap this task, we need to wake up our sub-threads possibly sleeping on ->wait_chldexit wait_queue_head_t.

See?

kernel/ptrace.c static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) · line 556

Quoted 2 lines
kernel/ptrace.c +2 -0
+	if (!dead && thread_group_empty(p))
+		dead = do_notify_parent(p, p->exit_signal);

No, this looks wrong. Or I missed something?

Oleg.

maintainer
#171 lore raw

Re: [PATCH 05/16] ptrace: Remove dead code from __ptrace_detach

Quoted 56 lines · Oleg Nesterov

Sorry for delay.

Quoted 6 lines · Eric W. Biederman

Ever since commit 28d838cc4dfe ("Fix ptrace self-attach rule") it has been impossible to attach another thread in the same thread group.

Remove the code from __ptrace_detach that was trying to support detaching from a thread in the same thread group.

may be I am totally confused, but I think you misunderstood this code and thus this patch is very wrong.

The same_thread_group() check does NOT try to check if debugger and tracee is in the same thread group, this is indeed impossible.

We need this check to know if the tracee was ptrace_reparented() before __ptrace_unlink() or not.

Quoted 9 lines
kernel/ptrace.c +0 -9
-static int ignoring_children(struct sighand_struct *sigh)
-{
-	int ret;
-	spin_lock(&sigh->siglock);
-	ret = (sigh->action[SIGCHLD-1].sa.sa_handler = SIG_IGN) ||
-	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
-	spin_unlock(&sigh->siglock);
-	return ret;
-}

...

kernel/ptrace.c static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) · line 575

Quoted 12 lines
kernel/ptrace.c +0 -8
@@ -565,14 +552,9 @@ static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
 
 	dead = !thread_group_leader(p);
 
-	if (!dead && thread_group_empty(p)) {
-		if (!same_thread_group(p->real_parent, tracer))
-			dead = do_notify_parent(p, p->exit_signal);
-		else if (ignoring_children(tracer->sighand)) {
-			__wake_up_parent(p, tracer);
-			dead = true;
-		}
-	}

So the code above does:

- if !same_thread_group(p->real_parent, tracer), then the tracee was ptrace_reparented(), and now we need to notify its natural parent to let it know it has a zombie child.

- otherwise, the tracee is our natural child, and it is actually dead. however, since we are going to reap this task, we need to wake up our sub-threads possibly sleeping on ->wait_chldexit wait_queue_head_t.

Quoted 6 lines

See?

kernel/ptrace.c static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) · line 556

Quoted 2 lines
kernel/ptrace.c +2 -0
+	if (!dead && thread_group_empty(p))
+		dead = do_notify_parent(p, p->exit_signal);

No, this looks wrong. Or I missed something?

Yes, but...

That said, it seems that we do not need __wake_up_parent() if it was our natural child?

I'll recheck. Eric, I'll continue to read this series tomorrow, can't concentrate on ptrace today.

Oleg.

#172 lore raw

Re: [PATCH 05/16] ptrace: Remove dead code from __ptrace_detach

Quoted 64 lines · Oleg Nesterov
Quoted 56 lines · Oleg Nesterov

Sorry for delay.

Quoted 6 lines · Eric W. Biederman

Ever since commit 28d838cc4dfe ("Fix ptrace self-attach rule") it has been impossible to attach another thread in the same thread group.

Remove the code from __ptrace_detach that was trying to support detaching from a thread in the same thread group.

may be I am totally confused, but I think you misunderstood this code and thus this patch is very wrong.

The same_thread_group() check does NOT try to check if debugger and tracee is in the same thread group, this is indeed impossible.

We need this check to know if the tracee was ptrace_reparented() before __ptrace_unlink() or not.

Quoted 9 lines
kernel/ptrace.c +0 -9
-static int ignoring_children(struct sighand_struct *sigh)
-{
-	int ret;
-	spin_lock(&sigh->siglock);
-	ret = (sigh->action[SIGCHLD-1].sa.sa_handler = SIG_IGN) ||
-	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
-	spin_unlock(&sigh->siglock);
-	return ret;
-}

...

kernel/ptrace.c static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) · line 575

Quoted 12 lines
kernel/ptrace.c +0 -8
@@ -565,14 +552,9 @@ static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
 
 	dead = !thread_group_leader(p);
 
-	if (!dead && thread_group_empty(p)) {
-		if (!same_thread_group(p->real_parent, tracer))
-			dead = do_notify_parent(p, p->exit_signal);
-		else if (ignoring_children(tracer->sighand)) {
-			__wake_up_parent(p, tracer);
-			dead = true;
-		}
-	}

So the code above does:

- if !same_thread_group(p->real_parent, tracer), then the tracee was ptrace_reparented(), and now we need to notify its natural parent to let it know it has a zombie child.

- otherwise, the tracee is our natural child, and it is actually dead. however, since we are going to reap this task, we need to wake up our sub-threads possibly sleeping on ->wait_chldexit wait_queue_head_t.

Quoted 6 lines

See?

kernel/ptrace.c static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) · line 556

Quoted 2 lines
kernel/ptrace.c +2 -0
+	if (!dead && thread_group_empty(p))
+		dead = do_notify_parent(p, p->exit_signal);

No, this looks wrong. Or I missed something?

Quoted 4 lines

Yes, but...

That said, it seems that we do not need __wake_up_parent() if it was our natural child?

Agreed on both counts.

Hmm. I see where the logic comes from. The ignoring_children test and the __wake_up_parent are what do_notify_parent does when the parent ignores children. Hmm. I even see all of this document in the comment above __ptrace_detach.

So I am just going to drop this change.

Quoted 2 lines

I'll recheck. Eric, I'll continue to read this series tomorrow, can't concentrate on ptrace today.

No worries. This was entirely too close to the merge window so I dropped it all until today.

Eric

#173 lore raw

PATCH 6/16 ptrace: Remove unnecessary locking in ptrace_(get|set)siginfo

In reply to Eric W. Biederman

Since commit 9899d11f6544 ("ptrace: ensure arch_ptrace/ptrace_request can never race with SIGKILL") it has been unnecessary for ptrace_getsiginfo and ptrace_setsiginfo to use lock_task_sighand.

Having the code taking an unnecessary lock is confusing as it suggests that other parts of the code need to take the unnecessary lock as well.

So remove the unnecessary lock to make the code more efficient, simpler, and less confusing.

---
 kernel/ptrace.c | 30 ++++++++----------------------
 1 file changed, 8 insertions(+), 22 deletions(-)
kernel/ptrace.c +8 -22
index ca0e47691229..15e93eafa6f0 100644
@@ -677,34 +677,20 @@ static int ptrace_setoptions(struct task_struct *child, unsigned long data)
 
 static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info)
 {
-	unsigned long flags;
-	int error = -ESRCH;
+	if (unlikely(!child->last_siginfo))
+		return -EINVAL;
 
-	if (lock_task_sighand(child, &flags)) {
-		error = -EINVAL;
-		if (likely(child->last_siginfo != NULL)) {
-			copy_siginfo(info, child->last_siginfo);
-			error = 0;
-		}
-		unlock_task_sighand(child, &flags);
-	}
-	return error;
+	copy_siginfo(info, child->last_siginfo);
+	return 0;
 }
 
 static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info)
 {
-	unsigned long flags;
-	int error = -ESRCH;
+	if (unlikely(!child->last_siginfo))
+		return -EINVAL;
 
-	if (lock_task_sighand(child, &flags)) {
-		error = -EINVAL;
-		if (likely(child->last_siginfo != NULL)) {
-			copy_siginfo(child->last_siginfo, info);
-			error = 0;
-		}
-		unlock_task_sighand(child, &flags);
-	}
-	return error;
+	copy_siginfo(child->last_siginfo, info);
+	return 0;
 }
 
 static int ptrace_peek_siginfo(struct task_struct *child,
Signature · 1 line
2.35.3
maintainer
#174 lore raw

Re: [PATCH 06/16] ptrace: Remove unnecessary locking in ptrace_(get|set)siginfo

Quoted 4 lines · Eric W. Biederman

Since commit 9899d11f6544 ("ptrace: ensure arch_ptrace/ptrace_request can never race with SIGKILL") it has been unnecessary for ptrace_getsiginfo and ptrace_setsiginfo to use lock_task_sighand.

ACK

#175 lore raw

PATCH 7/16 signal: Wake up the designated parent

In reply to Eric W. Biederman

Today if a process is ptraced only the ptracer will ever be woken up in wait, if the parent is waiting with __WNOTHREAD. Update the code so that the real_parent can also be woken up with __WNOTHREAD even when the code is ptraced.

Fixes 75b95953a569 ("job control: Add @for_ptrace to do_notify_parent_cldstop()")
---
 kernel/exit.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
kernel/exit.c +14 -5
index f072959fcab7..0e26f73c49ac 100644
@@ -1421,26 +1421,35 @@ static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
 	return 0;
 }
 
+struct child_wait_info {
+	struct task_struct *p;
+	struct task_struct *parent;
+};
+
 static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
 				int sync, void *key)
 {
 	struct wait_opts *wo = container_of(wait, struct wait_opts,
 						child_wait);
-	struct task_struct *p = key;
+	struct child_wait_info *info = key;
 
-	if (!eligible_pid(wo, p))
+	if (!eligible_pid(wo, info->p))
 		return 0;
 
-	if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
-		return 0;
+	if ((wo->wo_flags & __WNOTHREAD) && (wait->private != info->parent))
+			return 0;
 
 	return default_wake_function(wait, mode, sync, key);
 }
 
 void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
 {
+	struct child_wait_info info = {
+		.p = p,
+		.parent = parent,
+	};
 	__wake_up_sync_key(&parent->signal->wait_chldexit,
-			   TASK_INTERRUPTIBLE, p);
+			   TASK_INTERRUPTIBLE, &info);
 }
 
 static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
Signature · 1 line
2.35.3
maintainer
#176 lore raw

Re: [PATCH 07/16] signal: Wake up the designated parent

I fail to understand this patch...

Quoted 3 lines · Eric W. Biederman

Today if a process is ptraced only the ptracer will ever be woken up in wait

and why is this wrong?

Quoted 1 line
Fixes 75b95953a569 ("job control: Add @for_ptrace to do_notify_parent_cldstop()")

how does this change fix 75b95953a569?

Quoted 6 lines

static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,

kernel/exit.c +0 -1
 				int sync, void *key)
 {
 	struct wait_opts *wo = container_of(wait, struct wait_opts,
 						child_wait);
-	struct task_struct *p = key;

kernel/exit.c static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk) · line 1440

Quoted 10 lines
kernel/exit.c +4 -3
+	struct child_wait_info *info = key;
 
-	if (!eligible_pid(wo, p))
+	if (!eligible_pid(wo, info->p))
 		return 0;
 
-	if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
-		return 0;
+	if ((wo->wo_flags & __WNOTHREAD) && (wait->private != info->parent))
+			return 0;

So. wait->private is the task T which sleeping on wait_chldexit.

Before the patch the logic is clear. T called do_wait(__WNOTHREAD) and we do not need to wake it up if it is not the "actual" parent of p.

After the patch we check it T is actual to the "parent" arg passed to __wake_up_parent(). Why??? This arg is only used to find the ->signal->wait_chldexit wait_queue_head, and this is fine.

As I said, I don't understand this patch. But at least this change is wrong in case when __wake_up_parent() is calles by __ptrace_detach(). (you removed it in 5/16 but this looks wrong too). Sure, we can change ptrace_detach() to use __wake_up_parent(p, p->parent), but for what?

I must have missed something.

Oleg.

maintainer
#177 lore raw

Re: [PATCH 07/16] signal: Wake up the designated parent

Quoted 5 lines · Oleg Nesterov

I fail to understand this patch...

Quoted 1 line · Eric W. Biederman
Quoted 8 lines
Quoted 2 lines

Today if a process is ptraced only the ptracer will ever be woken up in wait

and why is this wrong?

Quoted 1 line
Fixes 75b95953a569 ("job control: Add @for_ptrace to do_notify_parent_cldstop()")

how does this change fix 75b95953a569?

OK, I guess you mean the 2nd do_notify_parent_cldstop() in ptrace_stop(), the problematic case is current->ptrace = T. Right?

I dislike this patch anyway, but let me think more about it.

Oleg.

maintainer
#178 lore raw

Re: [PATCH 07/16] signal: Wake up the designated parent

Quoted 12 lines · Oleg Nesterov
Quoted 10 lines · Oleg Nesterov

I fail to understand this patch...

Quoted 3 lines · Eric W. Biederman

Today if a process is ptraced only the ptracer will ever be woken up in wait

and why is this wrong?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Fixes 75b95953a569 ("job control: Add @for_ptrace to do_notify_parent_cldstop()")

how does this change fix 75b95953a569?

OK, I guess you mean the 2nd do_notify_parent_cldstop() in ptrace_stop(), the problematic case is current->ptrace = T. Right?

I dislike this patch anyway, but let me think more about it.

OK, now that I understand the problem, the patch doesn't look bad to me, although I'd ask to make the changelog more clear.

After this change __wake_up_parent() can't accept any "parent" from p->parent thread group, but all callers look fine except ptrace_detach().

Oleg.

#179 lore raw

Re: [PATCH 07/16] signal: Wake up the designated parent

Quoted 17 lines · Oleg Nesterov
Quoted 16 lines · Oleg Nesterov
Quoted 13 lines · Oleg Nesterov

I fail to understand this patch...

Quoted 3 lines · Eric W. Biederman

Today if a process is ptraced only the ptracer will ever be woken up in wait

and why is this wrong?

Quoted 1 line
Fixes 75b95953a569 ("job control: Add @for_ptrace to do_notify_parent_cldstop()")

how does this change fix 75b95953a569?

Quoted 7 lines
Quoted 4 lines

OK, I guess you mean the 2nd do_notify_parent_cldstop() in ptrace_stop(), the problematic case is current->ptrace = T. Right?

I dislike this patch anyway, but let me think more about it.

OK, now that I understand the problem, the patch doesn't look bad to me, although I'd ask to make the changelog more clear.

I will see what I can do.

Quoted 3 lines

After this change __wake_up_parent() can't accept any "parent" from p->parent thread group, but all callers look fine except ptrace_detach().

Having looked at it a little more I think the change was too restrictive. For the !ptrace_reparented case there are possibly two threads of the parent process that wait_consider_task will allow to wait even with __WNOTHREAD specified. It is desirable to wake them both up.

Which if I have had enough sleep reduces this patch to just:

kernel/exit.c +4 -2
index f072959fcab7..c8156366b722 100644
@@ -1431,8 +1431,10 @@ static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
        if (!eligible_pid(wo, p))
                return 0;
 
-       if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
-               return 0;
+       if ((wo->wo_flags & __WNOTHREAD) &&
+           (wait->private != p->parent) &&
+           (wait->private != p->real_parent))
+                       return 0;
 
        return default_wake_function(wait, mode, sync, key);
 }

I think that solves the issue without missing wake-ups without adding any more.

For the same set of reasons it looks like the __wake_up_parent in __ptrace_detach is just simply dead code. I don't think there is a case where when !ptrace_reparented the thread that is the real_parent can sleep in do_wait when the thread that was calling ptrace could not.

That needs a very close look to confirm.

Eric

maintainer
#180 lore raw

Re: [PATCH 07/16] signal: Wake up the designated parent

Quoted 22 lines · Eric W. Biederman

Which if I have had enough sleep reduces this patch to just:

kernel/exit.c +4 -2
index f072959fcab7..c8156366b722 100644
@@ -1431,8 +1431,10 @@ static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
        if (!eligible_pid(wo, p))
                return 0;
 
-       if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
-               return 0;
+       if ((wo->wo_flags & __WNOTHREAD) &&
+           (wait->private != p->parent) &&
+           (wait->private != p->real_parent))
+                       return 0;
 
        return default_wake_function(wait, mode, sync, key);
 }
Quoted 2 lines

I think that solves the issue without missing wake-ups without adding any more.

Agreed, and looks much simpler.

Quoted 4 lines

For the same set of reasons it looks like the __wake_up_parent in __ptrace_detach is just simply dead code. I don't think there is a case where when !ptrace_reparented the thread that is the real_parent can sleep in do_wait when the thread that was calling ptrace could not.

Yes... this doesn't really differ from the case when one thread reaps a natural child and another thread sleep in do_wait().

Quoted 1 line

That needs a very close look to confirm.

Yes.

Oleg.

#181 lore raw

PATCH 8/16 ptrace: Only populate last_siginfo from ptrace

In reply to Eric W. Biederman

The code in ptrace_signal to populate siginfo if the signal number changed is buggy. If the tracer contined the tracee using ptrace_detach it is guaranteed to use the real_parent (or possibly a new tracer) but definitely not the origional tracer to populate si_pid and si_uid.

Fix this bug by only updating siginfo from the tracer so that the tracers pid and the tracers uid are always used.

If it happens that ptrace_resume or ptrace_detach don't have a signal to continue with clear siginfo.

This is a very old bug that has been fixable since commit 1669ce53e2ff ("Add PTRACE_GETSIGINFO and PTRACE_SETSIGINFO") when last_siginfo was introduced and the tracer could change siginfo.

Fixes: v2.1.68
History-Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git

---
 kernel/ptrace.c | 31 +++++++++++++++++++++++++++++--
 kernel/signal.c | 18 ------------------
 2 files changed, 29 insertions(+), 20 deletions(-)
kernel/ptrace.c +29 -2
index 15e93eafa6f0..a24eed725cec 100644
@@ -526,6 +526,33 @@ static int ptrace_traceme(void)
 	return ret;
 }
 
+static void ptrace_set_signr(struct task_struct *child, unsigned int signr)
+{
+	struct kernel_siginfo *info = child->last_siginfo;
+
+	child->exit_code = signr;
+	/*
+	 * Update the siginfo structure if the signal has
+	 * changed.  If the debugger wanted something
+	 * specific in the siginfo structure then it should
+	 * have updated *info via PTRACE_SETSIGINFO.
+	 */
+	if (info && (info->si_signo != signr)) {
+		clear_siginfo(info);
+
+		if (signr != 0) {
+			info->si_signo = signr;
+			info->si_errno = 0;
+			info->si_code = SI_USER;
+			rcu_read_lock();
+			info->si_pid = task_pid_nr_ns(current, task_active_pid_ns(child));
+			info->si_uid = from_kuid_munged(task_cred_xxx(child, user_ns),
+						current_uid());
+			rcu_read_unlock();
+		}
+	}
+}
+
 /*
  * Called with tasklist_lock held for writing.
  * Unlink a traced task, and clean it up if it was a traced zombie.
@@ -579,7 +606,7 @@ static int ptrace_detach(struct task_struct *child, unsigned int data)
 	 * tasklist_lock avoids the race with wait_task_stopped(), see
 	 * the comment in ptrace_resume().
 	 */
-	child->exit_code = data;
+	ptrace_set_signr(child, data);
 	__ptrace_detach(current, child);
 	write_unlock_irq(&tasklist_lock);
 
@@ -851,7 +878,7 @@ static int ptrace_resume(struct task_struct *child, long request,
 	 * wait_task_stopped() after resume.
 	 */
 	spin_lock_irq(&child->sighand->siglock);
-	child->exit_code = data;
+	ptrace_set_signr(child, data);
 	child->jobctl &= ~JOBCTL_TRACED;
 	wake_up_state(child, __TASK_TRACED);
 	spin_unlock_irq(&child->sighand->siglock);
kernel/signal.c +0 -18
index e782c2611b64..ff4a52352390 100644
@@ -2562,24 +2562,6 @@ static int ptrace_signal(int signr, kernel_siginfo_t *info, enum pid_type type)
 	if (signr = 0)
 		return signr;
 
-	/*
-	 * Update the siginfo structure if the signal has
-	 * changed.  If the debugger wanted something
-	 * specific in the siginfo structure then it should
-	 * have updated *info via PTRACE_SETSIGINFO.
-	 */
-	if (signr != info->si_signo) {
-		clear_siginfo(info);
-		info->si_signo = signr;
-		info->si_errno = 0;
-		info->si_code = SI_USER;
-		rcu_read_lock();
-		info->si_pid = task_pid_vnr(current->parent);
-		info->si_uid = from_kuid_munged(current_user_ns(),
-						task_uid(current->parent));
-		rcu_read_unlock();
-	}
-
 	/* If the (new) signal is now blocked, requeue it.  */
 	if (sigismember(&current->blocked, signr) ||
 	    fatal_signal_pending(current)) {
Signature · 1 line
2.35.3
maintainer
#182 lore raw

Re: [PATCH 08/16] ptrace: Only populate last_siginfo from ptrace

Quoted 6 lines · Eric W. Biederman

The code in ptrace_signal to populate siginfo if the signal number changed is buggy. If the tracer contined the tracee using ptrace_detach it is guaranteed to use the real_parent (or possibly a new tracer) but definitely not the origional tracer to populate si_pid and si_uid.

I guess nobody cares. As the comment says

If the debugger wanted something specific in the siginfo structure then it should have updated *info via PTRACE_SETSIGINFO.

otherwise I don't think si_pid/si_uid have any value.

However the patch looks fine to me, just the word "buggy" looks a bit too strong imo.

Oleg.

#183 lore raw

Re: [PATCH 08/16] ptrace: Only populate last_siginfo from ptrace

Quoted 8 lines · Oleg Nesterov
Quoted 6 lines · Eric W. Biederman

The code in ptrace_signal to populate siginfo if the signal number changed is buggy. If the tracer contined the tracee using ptrace_detach it is guaranteed to use the real_parent (or possibly a new tracer) but definitely not the origional tracer to populate si_pid and si_uid.

Quoted 7 lines

I guess nobody cares. As the comment says

If the debugger wanted something specific in the siginfo structure then it should have updated *info via PTRACE_SETSIGINFO.

otherwise I don't think si_pid/si_uid have any value.

No one has complained so it is clearly no one cares. So it is definitely not a regression. Or even anything that needs to be backported.

However si_pid and si_uid are defined with SI_USER are defined to be whomever sent the signal. So I would argue by definition those values are wrong.

Quoted 2 lines

However the patch looks fine to me, just the word "buggy" looks a bit too strong imo.

I guess I am in general agreement. Perhaps I can just say they values are wrong by definition?

Eric

maintainer
#184 lore raw

Re: [PATCH 08/16] ptrace: Only populate last_siginfo from ptrace

Quoted 8 lines · Eric W. Biederman
Quoted 2 lines · Oleg Nesterov

However the patch looks fine to me, just the word "buggy" looks a bit too strong imo.

I guess I am in general agreement. Perhaps I can just say they values are wrong by definition?

Up to you. I won't really argue with "buggy".

Oleg.

#185 lore raw

PATCH 9/16 ptrace: In ptrace_setsiginfo deal with invalid si_signo

In reply to Eric W. Biederman

If the tracer calls PTRACE_SETSIGINFO it only has an effect if the tracee is stopped in ptrace_signal.

When one of PTRACE_DETACH, PTRACE_SINGLESTEP, PTRACE_SINGLEBLOCK, PTRACE_SYSEMU, PTRACE_SYSEMU_SINGLESTEP, PTRACE_SYSCALL, or PTRACE_CONT pass in a signel number to continue with the kernel validates that signal number and the ptrace_signal verifies the signal number matches the si_signo, before the siginfo is used.

As the signal number to continue with is verified to be a valid signal number the signal number in si_signo must be a valid signal number.

Make this obvious and avoid needing checks later by immediately clearing siginfo if si_signo is not valid.

---
 kernel/ptrace.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/ptrace.c +3 -1
index a24eed725cec..a0a07d140751 100644
@@ -716,7 +716,9 @@ static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *
 	if (unlikely(!child->last_siginfo))
 		return -EINVAL;
 
-	copy_siginfo(child->last_siginfo, info);
+	clear_siginfo(child->last_siginfo);
+	if (valid_signal(info->si_signo))
+		copy_siginfo(child->last_siginfo, info);
 	return 0;
 }
 
Signature · 1 line
2.35.3
#186 lore raw

PATCH 10/16 ptrace: In ptrace_signal look at what the debugger did with siginfo

In reply to Eric W. Biederman

Now that siginfo is only modified by the tracer and that siginfo is cleared with the signal is canceled have ptrace_signal directly examine siginfo.

This makes the code a little simpler and handles the case when the tracer exits without calling ptrace_detach.

---
 kernel/signal.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/signal.c +2 -1
index ff4a52352390..3d955c23b13d 100644
@@ -2556,9 +2556,10 @@ static int ptrace_signal(int signr, kernel_siginfo_t *info, enum pid_type type)
 	 * comment in dequeue_signal().
 	 */
 	current->jobctl |= JOBCTL_STOP_DEQUEUED;
-	signr = ptrace_stop(signr, CLD_TRAPPED, 0, info);
+	ptrace_stop(signr, CLD_TRAPPED, 0, info);
 
 	/* We're back.  Did the debugger cancel the sig?  */
+	signr = info->si_signo;
 	if (signr = 0)
 		return signr;
 
Signature · 1 line
2.35.3
#187 lore raw

PATCH 11/16 ptrace: Use si_sino as the signal number to resume with

In reply to Eric W. Biederman

The signal number to resume with is already in si_signo. So instead of placing an extra copy in tsk->exit_code and later reading the extra copy from tsk->exit_code just read si_signo.

Read si_signo in ptrace_do_notify where it is easy as the siginfo is a local variable. Only ptrace_report_syscall cares about the signal to resume with from ptrace_stop and it calls ptrace_notify which calls ptrace_do_notify so moving the actual work into ptrace_do_notify where it is easier is not a problem.

With ptrace_stop not being involved in returning the signal to tracer asked the tracee to resume with remove the comment and the return code from ptrace_stop.

---
 kernel/ptrace.c |  1 -
 kernel/signal.c | 13 ++++---------
 2 files changed, 4 insertions(+), 10 deletions(-)
kernel/ptrace.c +0 -1
index a0a07d140751..e0ecb1536dfc 100644
@@ -530,7 +530,6 @@ static void ptrace_set_signr(struct task_struct *child, unsigned int signr)
 {
 	struct kernel_siginfo *info = child->last_siginfo;
 
-	child->exit_code = signr;
 	/*
 	 * Update the siginfo structure if the signal has
 	 * changed.  If the debugger wanted something
kernel/signal.c +4 -9
index 3d955c23b13d..2cc45e8448e2 100644
@@ -2186,12 +2186,8 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
  * We always set current->last_siginfo while stopped here.
  * That makes it a way to test a stopped process for
  * being ptrace-stopped vs being job-control-stopped.
- *
- * Returns the signal the ptracer requested the code resume
- * with.  If the code did not stop because the tracer is gone,
- * the stop signal remains unchanged unless clear_code.
  */
-static int ptrace_stop(int exit_code, int why, unsigned long message,
+static void ptrace_stop(int exit_code, int why, unsigned long message,
 		       kernel_siginfo_t *info)
 	__releases(&current->sighand->siglock)
 	__acquires(&current->sighand->siglock)
@@ -2219,7 +2215,7 @@ static int ptrace_stop(int exit_code, int why, unsigned long message,
 	 * signals here to prevent ptrace_stop sleeping in schedule.
 	 */
 	if (!current->ptrace || __fatal_signal_pending(current))
-		return exit_code;
+		return;
 
 	set_special_state(TASK_TRACED);
 	current->jobctl |= JOBCTL_TRACED;
@@ -2302,7 +2298,6 @@ static int ptrace_stop(int exit_code, int why, unsigned long message,
 	 * any signal-sending on another CPU that wants to examine it.
 	 */
 	spin_lock_irq(&current->sighand->siglock);
-	exit_code = current->exit_code;
 	current->last_siginfo = NULL;
 	current->ptrace_message = 0;
 	current->exit_code = 0;
@@ -2316,7 +2311,6 @@ static int ptrace_stop(int exit_code, int why, unsigned long message,
 	 * This sets TIF_SIGPENDING, but never clears it.
 	 */
 	recalc_sigpending_tsk(current);
-	return exit_code;
 }
 
 static int ptrace_do_notify(int signr, int exit_code, int why, unsigned long message)
@@ -2330,7 +2324,8 @@ static int ptrace_do_notify(int signr, int exit_code, int why, unsigned long mes
 	info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
 
 	/* Let the debugger run.  */
-	return ptrace_stop(exit_code, why, message, &info);
+	ptrace_stop(exit_code, why, message, &info);
+	return info.si_signo;
 }
 
 int ptrace_notify(int exit_code, unsigned long message)
Signature · 1 line
2.35.3
#188 lore raw

PATCH 12/16 ptrace: Stop protecting ptrace_set_signr with tasklist_lock

In reply to Eric W. Biederman

Now that ptrace_set_signr no longer sets task->exit_code the race documented in commit b72c186999e6 ("ptrace: fix race between ptrace_resume() and wait_task_stopped()") is no longer possible, as task->exit_code is only updated by wait during a ptrace_stop.

As there is no possibilty of a race and ptrace_freeze_traced is all of the protection ptrace_set_signr needs to operate without contention move ptrace_set_signr outside of tasklist_lock and remove the documentation about the race that is no more.

---
 kernel/ptrace.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
kernel/ptrace.c +4 -15
index e0ecb1536dfc..d0527b6e2b29 100644
@@ -595,17 +595,14 @@ static int ptrace_detach(struct task_struct *child, unsigned int data)
 	/* Architecture-specific hardware disable .. */
 	ptrace_disable(child);
 
+	ptrace_set_signr(child, data);
+
 	write_lock_irq(&tasklist_lock);
 	/*
 	 * We rely on ptrace_freeze_traced(). It can't be killed and
 	 * untraced by another thread, it can't be a zombie.
 	 */
 	WARN_ON(!child->ptrace || child->exit_state);
-	/*
-	 * tasklist_lock avoids the race with wait_task_stopped(), see
-	 * the comment in ptrace_resume().
-	 */
-	ptrace_set_signr(child, data);
 	__ptrace_detach(current, child);
 	write_unlock_irq(&tasklist_lock);
 
@@ -869,17 +866,9 @@ static int ptrace_resume(struct task_struct *child, long request,
 		user_disable_single_step(child);
 	}
 
-	/*
-	 * Change ->exit_code and ->state under siglock to avoid the race
-	 * with wait_task_stopped() in between; a non-zero ->exit_code will
-	 * wrongly look like another report from tracee.
-	 *
-	 * Note that we need siglock even if ->exit_code = data and/or this
-	 * status was not reported yet, the new status must not be cleared by
-	 * wait_task_stopped() after resume.
-	 */
-	spin_lock_irq(&child->sighand->siglock);
 	ptrace_set_signr(child, data);
+
+	spin_lock_irq(&child->sighand->siglock);
 	child->jobctl &= ~JOBCTL_TRACED;
 	wake_up_state(child, __TASK_TRACED);
 	spin_unlock_irq(&child->sighand->siglock);
Signature · 1 line
2.35.3
#189 lore raw

PATCH 13/16 ptrace: Document why ptrace_setoptions does not need a lock

In reply to Eric W. Biederman

The functions that change ->ptrace are: ptrace_attach, ptrace_traceme, ptrace_init_task, __ptrace_unlink, ptrace_setoptions.

Except for ptrace_setoptions all of the places where ->ptrace is modified hold tasklist_lock for write, and either the tracee or the tracer is modifies ->ptrace.

When ptrace_setoptions is called the tracee has been frozen with ptrace_freeze_traced, and most be explicitly unfrozen by the tracer before it can do anything. As ptrace_setoption is run in the tracer there can be no contention by the simple fact that the tracee can't run.

---
 kernel/ptrace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/ptrace.c +4 -1
index d0527b6e2b29..fbadd2f21f09 100644
@@ -689,7 +689,10 @@ static int ptrace_setoptions(struct task_struct *child, unsigned long data)
 	if (ret)
 		return ret;
 
-	/* Avoid intermediate state when all opts are cleared */
+	/*
+	 * With a frozen tracee, only the tracer modifies ->ptrace.
+	 * Avoid intermediate state when all opts are cleared.
+	 */
 	flags = child->ptrace;
 	flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
 	flags |= (data << PT_OPT_FLAG_SHIFT);
Signature · 1 line
2.35.3
#190 lore raw

PATCH 14/16 signal: Protect parent child relationships by childs siglock

In reply to Eric W. Biederman

The functions ptrace_stop and do_signal_stop have to drop siglock and grab tasklist_lock because the parent/child relation ship is guarded by tasklist_lock and not siglock.

Simplify things by additionally guarding the parent/child relationship with siglock. This just requires a little bit of code motion.

After this change tsk->parent, tsk->real_parent, tsk->ptracer_cred are all protected by tsk->siglock.

The fields tsk->sibling and tsk->ptrace_entry are mostly protected by tsk->siglock. The field tsk->ptrace_entry is not protected by siglock when tsk->ptrace_entry is reused as the dead task list. The field tsk->sibling is not protected by siglock when children are reparented because their original parent dies.

The field tsk->ptrace is protected by siglock except for the options which may change without siglock being held.

---
 kernel/exit.c   |  4 ++++
 kernel/fork.c   | 12 ++++++------
 kernel/ptrace.c |  9 +++++----
 3 files changed, 15 insertions(+), 10 deletions(-)
kernel/exit.c +4 -0
index 0e26f73c49ac..bad434b23c48 100644
@@ -643,11 +643,15 @@ static void forget_original_parent(struct task_struct *father,
 
 	reaper = find_new_reaper(father, reaper);
 	list_for_each_entry(p, &father->children, sibling) {
+		spin_lock(&p->sighand->siglock);
 		for_each_thread(p, t) {
 			RCU_INIT_POINTER(t->real_parent, reaper);
 			BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) = father));
 			if (likely(!t->ptrace))
 				t->parent = t->real_parent;
+		}
+		spin_unlock(&p->sighand->siglock);
+		for_each_thread(p, t) {
 			if (t->pdeath_signal)
 				group_send_sig_info(t->pdeath_signal,
 						    SEND_SIG_NOINFO, t,
kernel/fork.c +6 -6
index 9796897560ab..841021da69f3 100644
@@ -2367,6 +2367,12 @@ static __latent_entropy struct task_struct *copy_process(
 	 */
 	write_lock_irq(&tasklist_lock);
 
+	klp_copy_process(p);
+
+	sched_core_fork(p);
+
+	spin_lock(&current->sighand->siglock);
+
 	/* CLONE_PARENT re-uses the old parent */
 	if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
 		p->real_parent = current->real_parent;
@@ -2381,12 +2387,6 @@ static __latent_entropy struct task_struct *copy_process(
 		p->exit_signal = args->exit_signal;
 	}
 
-	klp_copy_process(p);
-
-	sched_core_fork(p);
-
-	spin_lock(&current->sighand->siglock);
-
 	/*
 	 * Copy seccomp details explicitly here, in case they were changed
 	 * before holding sighand lock.
kernel/ptrace.c +5 -4
index fbadd2f21f09..77dfdb3d1ced 100644
@@ -123,13 +123,12 @@ void __ptrace_unlink(struct task_struct *child)
 	clear_task_syscall_work(child, SYSCALL_EMU);
 #endif
 
+	spin_lock(&child->sighand->siglock);
 	child->parent = child->real_parent;
 	list_del_init(&child->ptrace_entry);
 	old_cred = child->ptracer_cred;
 	child->ptracer_cred = NULL;
 	put_cred(old_cred);
-
-	spin_lock(&child->sighand->siglock);
 	child->ptrace = 0;
 	/*
 	 * Clear all pending traps and TRAPPING.  TRAPPING should be
@@ -441,15 +440,15 @@ static int ptrace_attach(struct task_struct *task, long request,
 	if (task->ptrace)
 		goto unlock_tasklist;
 
+	spin_lock(&task->sighand->siglock);
 	task->ptrace = flags;
 
 	ptrace_link(task, current);
 
 	/* SEIZE doesn't trap tracee on attach */
 	if (!seize)
-		send_sig_info(SIGSTOP, SEND_SIG_PRIV, task);
+		send_signal_locked(SIGSTOP, SEND_SIG_PRIV, task, PIDTYPE_PID);
 
-	spin_lock(&task->sighand->siglock);
 
 	/*
 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
@@ -517,8 +516,10 @@ static int ptrace_traceme(void)
 		 * pretend ->real_parent untraces us right after return.
 		 */
 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
+			spin_lock(&current->sighand->siglock);
 			current->ptrace = PT_PTRACED;
 			ptrace_link(current, current->real_parent);
+			spin_unlock(&current->sighand->siglock);
 		}
 	}
 	write_unlock_irq(&tasklist_lock);
Signature · 1 line
2.35.3
#191 lore raw

PATCH 15/16 ptrace: Use siglock instead of tasklist_lock in ptrace_check_attach

In reply to Eric W. Biederman

Now that siglock protects tsk->parent and tsk->ptrace there is no need to grab tasklist_lock in ptrace_check_attach. The siglock can handle all of the locking needs of ptrace_check_attach.

---
 kernel/ptrace.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)
kernel/ptrace.c +9 -14
index 77dfdb3d1ced..fa65841bbdbe 100644
@@ -194,17 +194,14 @@ static bool ptrace_freeze_traced(struct task_struct *task)
 {
 	bool ret = false;
 
-	/* Lockless, nobody but us can set this flag */
 	if (task->jobctl & JOBCTL_LISTENING)
 		return ret;
 
-	spin_lock_irq(&task->sighand->siglock);
 	if (task_is_traced(task) && !looks_like_a_spurious_pid(task) &&
 	    !__fatal_signal_pending(task)) {
 		task->jobctl |= JOBCTL_PTRACE_FROZEN;
 		ret = true;
 	}
-	spin_unlock_irq(&task->sighand->siglock);
 
 	return ret;
 }
@@ -240,32 +237,30 @@ static void ptrace_unfreeze_traced(struct task_struct *task)
  * state.
  *
  * CONTEXT:
- * Grabs and releases tasklist_lock and @child->sighand->siglock.
+ * Grabs and releases @child->sighand->siglock.
  *
  * RETURNS:
  * 0 on success, -ESRCH if %child is not ready.
  */
 static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
 {
+	unsigned long flags;
 	int ret = -ESRCH;
 
 	/*
-	 * We take the read lock around doing both checks to close a
+	 * We take the siglock around doing both checks to close a
 	 * possible race where someone else was tracing our child and
 	 * detached between these two checks.  After this locked check,
 	 * we are sure that this is our traced child and that can only
 	 * be changed by us so it's not changing right after this.
 	 */
-	read_lock(&tasklist_lock);
-	if (child->ptrace && child->parent = current) {
-		/*
-		 * child->sighand can't be NULL, release_task()
-		 * does ptrace_unlink() before __exit_signal().
-		 */
-		if (ignore_state || ptrace_freeze_traced(child))
-			ret = 0;
+	if (lock_task_sighand(child, &flags)) {
+		if (child->ptrace && child->parent = current) {
+			if (ignore_state || ptrace_freeze_traced(child))
+				ret = 0;
+		}
+		unlock_task_sighand(child, &flags);
 	}
-	read_unlock(&tasklist_lock);
 
 	if (!ret && !ignore_state &&
 	    WARN_ON_ONCE(!wait_task_inactive(child, __TASK_TRACED)))
Signature · 1 line
2.35.3
#192 lore raw

PATCH 16/16 signal: Always call do_notify_parent_cldstop with siglock held

In reply to Eric W. Biederman

Now that siglock keeps tsk->parent and tsk->real_parent constant require that do_notify_parent_cldstop is called with tsk->siglock held instead of the tasklist_lock.

As all of the callers of do_notify_parent_cldstop had to drop the siglock and take tasklist_lock this simplifies all of it's callers.

This removes one reason for taking tasklist_lock.

This makes ptrace_stop so that it should reliably work correctly and reliably with PREEMPT_RT enabled and CONFIG_CGROUPS disabled. The remaining challenge is that cgroup_enter_frozen takes spin_lock after __state has been set to TASK_TRACED. Which on PREEMPT_RT means the code can sleep and change __state. Not only that but it means that wait_task_inactive could potentially detect the code scheduling away at that point and fail, causing ptrace_check_attach to fail.

---
 kernel/signal.c | 262 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 189 insertions(+), 73 deletions(-)
kernel/signal.c +189 -73
index 2cc45e8448e2..d4956be51939 100644
@@ -1994,6 +1994,129 @@ int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
 	return ret;
 }
 
+/**
+ * lock_parents_siglocks - Take current, real_parent, and parent's siglock
+ * @lock_tracer: The tracers siglock is needed.
+ *
+ * There is no natural ordering to these locks so they must be sorted
+ * before being taken.
+ *
+ * There are two complicating factors here:
+ * - The locks live in sighand and sighand can be arbitrarily shared
+ * - parent and real_parent can change when current's siglock is unlocked.
+ *
+ * To deal with this first the all of the sighand pointers are
+ * gathered under current's siglock, and the sighand pointers are
+ * sorted.  As siglock lives inside of sighand this also sorts the
+ * siglock's by address.
+ *
+ * Then the siglocks are taken in order dropping current's siglock if
+ * necessary.
+ *
+ * Finally if parent and real_parent have not changed return.
+ * If they either parent has changed drop their locks and try again.
+ *
+ * Changing sighand is an infrequent and somewhat expensive operation
+ * (unshare or exec) and so even in the worst case this loop
+ * should not loop too many times before all of the proper locks are
+ * taken in order.
+ *
+ * CONTEXT:
+ * Must be called with @current->sighand->siglock held
+ *
+ * RETURNS:
+ * current's, real_parent's, and parent's siglock held.
+ */
+static void lock_parents_siglocks(bool lock_tracer)
+	__releases(&current->sighand->siglock)
+	__acquires(&current->sighand->siglock)
+	__acquires(&current->real_parent->sighand->siglock)
+	__acquires(&current->parent->sighand->siglock)
+{
+	struct task_struct *me = current;
+	struct sighand_struct *m_sighand = me->sighand;
+
+	lockdep_assert_held(&m_sighand->siglock);
+
+	rcu_read_lock();
+	for (;;) {
+		struct task_struct *parent, *tracer;
+		struct sighand_struct *p_sighand, *t_sighand, *s1, *s2, *s3;
+
+		parent = me->real_parent;
+		tracer = ptrace_parent(me);
+		if (!tracer || !lock_tracer)
+			tracer = parent;
+
+		p_sighand = rcu_dereference(parent->sighand);
+		t_sighand = rcu_dereference(tracer->sighand);
+
+		/* Sort the sighands so that s1 >= s2 >= s3 */
+		s1 = m_sighand;
+		s2 = p_sighand;
+		s3 = t_sighand;
+		if (s1 > s2)
+			swap(s1, s2);
+		if (s1 > s3)
+			swap(s1, s3);
+		if (s2 > s3)
+			swap(s2, s3);
+
+		/* Take the locks in order */
+		if (s1 != m_sighand) {
+			spin_unlock(&m_sighand->siglock);
+			spin_lock(&s1->siglock);
+		}
+		if (s1 != s2)
+			spin_lock_nested(&s2->siglock, 1);
+		if (s2 != s3)
+			spin_lock_nested(&s3->siglock, 2);
+
+		/* Verify the proper locks are held */
+		if (likely((s1 = m_sighand) ||
+			   ((me->real_parent = parent) &&
+			    (me->parent = tracer) &&
+			    (parent->sighand = p_sighand) &&
+			    (tracer->sighand = t_sighand)))) {
+			break;
+		}
+
+		/* Drop all but current's siglock */
+		if (p_sighand != m_sighand)
+			spin_unlock(&p_sighand->siglock);
+		if (t_sighand != p_sighand)
+			spin_unlock(&t_sighand->siglock);
+
+		/*
+		 * Since [pt]_sighand will likely change if we go
+		 * around, and m_sighand is the only one held, make sure
+		 * it is subclass-0, since the above 's1 != m_sighand'
+		 * clause very much relies on that.
+		 */
+		lock_set_subclass(&m_sighand->siglock.dep_map, 0, _RET_IP_);
+	}
+	rcu_read_unlock();
+}
+
+static void unlock_parents_siglocks(bool unlock_tracer)
+	__releases(&current->real_parent->sighand->siglock)
+	__releases(&current->parent->sighand->siglock)
+{
+	struct task_struct *me = current;
+	struct task_struct *parent = me->real_parent;
+	struct task_struct *tracer = ptrace_parent(me);
+	struct sighand_struct *m_sighand = me->sighand;
+	struct sighand_struct *p_sighand = parent->sighand;
+
+	if (p_sighand != m_sighand)
+		spin_unlock(&p_sighand->siglock);
+	if (tracer && unlock_tracer) {
+		struct sighand_struct *t_sighand = tracer->sighand;
+		if (t_sighand != p_sighand)
+			spin_unlock(&t_sighand->siglock);
+	}
+}
+
 static void do_notify_pidfd(struct task_struct *task)
 {
 	struct pid *pid;
@@ -2125,11 +2248,12 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
 				     bool for_ptracer, int why)
 {
 	struct kernel_siginfo info;
-	unsigned long flags;
 	struct task_struct *parent;
 	struct sighand_struct *sighand;
 	u64 utime, stime;
 
+	lockdep_assert_held(&tsk->sighand->siglock);
+
 	if (for_ptracer) {
 		parent = tsk->parent;
 	} else {
@@ -2137,6 +2261,8 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
 		parent = tsk->real_parent;
 	}
 
+	lockdep_assert_held(&parent->sighand->siglock);
+
 	clear_siginfo(&info);
 	info.si_signo = SIGCHLD;
 	info.si_errno = 0;
@@ -2168,7 +2294,6 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
  	}
 
 	sighand = parent->sighand;
-	spin_lock_irqsave(&sighand->siglock, flags);
 	if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
 	    !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
 		send_signal_locked(SIGCHLD, &info, parent, PIDTYPE_TGID);
@@ -2176,7 +2301,6 @@ static void do_notify_parent_cldstop(struct task_struct *tsk,
 	 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
 	 */
 	__wake_up_parent(tsk, parent);
-	spin_unlock_irqrestore(&sighand->siglock, flags);
 }
 
 /*
@@ -2208,14 +2332,18 @@ static void ptrace_stop(int exit_code, int why, unsigned long message,
 		spin_lock_irq(&current->sighand->siglock);
 	}
 
+	lock_parents_siglocks(true);
 	/*
 	 * After this point ptrace_signal_wake_up or signal_wake_up
 	 * will clear TASK_TRACED if ptrace_unlink happens or a fatal
 	 * signal comes in.  Handle previous ptrace_unlinks and fatal
 	 * signals here to prevent ptrace_stop sleeping in schedule.
 	 */
-	if (!current->ptrace || __fatal_signal_pending(current))
+
+	if (!current->ptrace || __fatal_signal_pending(current)) {
+		unlock_parents_siglocks(true);
 		return;
+	}
 
 	set_special_state(TASK_TRACED);
 	current->jobctl |= JOBCTL_TRACED;
@@ -2254,16 +2382,6 @@ static void ptrace_stop(int exit_code, int why, unsigned long message,
 	if (why = CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
 		gstop_done = task_participate_group_stop(current);
 
-	/* any trap clears pending STOP trap, STOP trap clears NOTIFY */
-	task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
-	if (info && info->si_code >> 8 = PTRACE_EVENT_STOP)
-		task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
-
-	/* entering a trap, clear TRAPPING */
-	task_clear_jobctl_trapping(current);
-
-	spin_unlock_irq(&current->sighand->siglock);
-	read_lock(&tasklist_lock);
 	/*
 	 * Notify parents of the stop.
 	 *
@@ -2279,14 +2397,25 @@ static void ptrace_stop(int exit_code, int why, unsigned long message,
 	if (gstop_done && (!current->ptrace || ptrace_reparented(current)))
 		do_notify_parent_cldstop(current, false, why);
 
+	unlock_parents_siglocks(true);
+
+	/* any trap clears pending STOP trap, STOP trap clears NOTIFY */
+	task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
+	if (info && info->si_code >> 8 = PTRACE_EVENT_STOP)
+		task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
+
+	/* entering a trap, clear TRAPPING */
+	task_clear_jobctl_trapping(current);
+
 	/*
 	 * Don't want to allow preemption here, because
 	 * sys_ptrace() needs this task to be inactive.
 	 *
-	 * XXX: implement read_unlock_no_resched().
+	 * XXX: implement spin_unlock_no_resched().
 	 */
 	preempt_disable();
-	read_unlock(&tasklist_lock);
+	spin_unlock_irq(&current->sighand->siglock);
+
 	cgroup_enter_frozen();
 	preempt_enable_no_resched();
 	freezable_schedule();
@@ -2361,8 +2490,8 @@ int ptrace_notify(int exit_code, unsigned long message)
  * on %true return.
  *
  * RETURNS:
- * %false if group stop is already cancelled or ptrace trap is scheduled.
- * %true if participated in group stop.
+ * %false if group stop is already cancelled.
+ * %true otherwise (as lock_parents_siglocks may have dropped siglock).
  */
 static bool do_signal_stop(int signr)
 	__releases(&current->sighand->siglock)
@@ -2425,36 +2554,24 @@ static bool do_signal_stop(int signr)
 		}
 	}
 
+	lock_parents_siglocks(false);
+	/* Recheck JOBCTL_STOP_PENDING after unlock+lock of siglock */
+	if (unlikely(!(current->jobctl & JOBCTL_STOP_PENDING)))
+		goto out;
 	if (likely(!current->ptrace)) {
-		int notify = 0;
-
 		/*
 		 * If there are no other threads in the group, or if there
 		 * is a group stop in progress and we are the last to stop,
-		 * report to the parent.
+		 * report to the real_parent.
 		 */
 		if (task_participate_group_stop(current))
-			notify = CLD_STOPPED;
+			do_notify_parent_cldstop(current, false, CLD_STOPPED);
+		unlock_parents_siglocks(false);
 
 		current->jobctl |= JOBCTL_STOPPED;
 		set_special_state(TASK_STOPPED);
 		spin_unlock_irq(&current->sighand->siglock);
 
-		/*
-		 * Notify the parent of the group stop completion.  Because
-		 * we're not holding either the siglock or tasklist_lock
-		 * here, ptracer may attach inbetween; however, this is for
-		 * group stop and should always be delivered to the real
-		 * parent of the group leader.  The new ptracer will get
-		 * its notification when this task transitions into
-		 * TASK_TRACED.
-		 */
-		if (notify) {
-			read_lock(&tasklist_lock);
-			do_notify_parent_cldstop(current, false, notify);
-			read_unlock(&tasklist_lock);
-		}
-
 		/* Now we don't run again until woken by SIGCONT or SIGKILL */
 		cgroup_enter_frozen();
 		freezable_schedule();
@@ -2465,8 +2582,11 @@ static bool do_signal_stop(int signr)
 		 * Schedule it and let the caller deal with it.
 		 */
 		task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
-		return false;
 	}
+out:
+	unlock_parents_siglocks(false);
+	spin_unlock_irq(&current->sighand->siglock);
+	return true;
 }
 
 /**
@@ -2624,32 +2744,30 @@ bool get_signal(struct ksignal *ksig)
 	if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
 		int why;
 
-		if (signal->flags & SIGNAL_CLD_CONTINUED)
-			why = CLD_CONTINUED;
-		else
-			why = CLD_STOPPED;
+		lock_parents_siglocks(true);
+		/* Recheck signal->flags after unlock+lock of siglock */
+		if (likely(signal->flags & SIGNAL_CLD_MASK)) {
+			if (signal->flags & SIGNAL_CLD_CONTINUED)
+				why = CLD_CONTINUED;
+			else
+				why = CLD_STOPPED;
 
-		signal->flags &= ~SIGNAL_CLD_MASK;
+			signal->flags &= ~SIGNAL_CLD_MASK;
 
-		spin_unlock_irq(&sighand->siglock);
-
-		/*
-		 * Notify the parent that we're continuing.  This event is
-		 * always per-process and doesn't make whole lot of sense
-		 * for ptracers, who shouldn't consume the state via
-		 * wait(2) either, but, for backward compatibility, notify
-		 * the ptracer of the group leader too unless it's gonna be
-		 * a duplicate.
-		 */
-		read_lock(&tasklist_lock);
-		do_notify_parent_cldstop(current, false, why);
-
-		if (ptrace_reparented(current->group_leader))
-			do_notify_parent_cldstop(current->group_leader,
-						true, why);
-		read_unlock(&tasklist_lock);
-
-		goto relock;
+			/*
+			 * Notify the parent that we're continuing.  This event is
+			 * always per-process and doesn't make whole lot of sense
+			 * for ptracers, who shouldn't consume the state via
+			 * wait(2) either, but, for backward compatibility, notify
+			 * the ptracer of the group leader too unless it's gonna be
+			 * a duplicate.
+			 */
+			do_notify_parent_cldstop(current, false, why);
+			if (ptrace_reparented(current->group_leader))
+				do_notify_parent_cldstop(current->group_leader,
+							 true, why);
+		}
+		unlock_parents_siglocks(true);
 	}
 
 	for (;;) {
@@ -2906,7 +3024,6 @@ static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
 
 void exit_signals(struct task_struct *tsk)
 {
-	int group_stop = 0;
 	sigset_t unblocked;
 
 	/*
@@ -2937,21 +3054,20 @@ void exit_signals(struct task_struct *tsk)
 	signotset(&unblocked);
 	retarget_shared_pending(tsk, &unblocked);
 
-	if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
-	    task_participate_group_stop(tsk))
-		group_stop = CLD_STOPPED;
-out:
-	spin_unlock_irq(&tsk->sighand->siglock);
-
 	/*
 	 * If group stop has completed, deliver the notification.  This
 	 * should always go to the real parent of the group leader.
 	 */
-	if (unlikely(group_stop)) {
-		read_lock(&tasklist_lock);
-		do_notify_parent_cldstop(tsk, false, group_stop);
-		read_unlock(&tasklist_lock);
+	if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING)) {
+		lock_parents_siglocks(false);
+		/* Recheck JOBCTL_STOP_PENDING after unlock+lock of siglock */
+		if ((tsk->jobctl & JOBCTL_STOP_PENDING) &&
+		    task_participate_group_stop(tsk))
+			do_notify_parent_cldstop(tsk, false, CLD_STOPPED);
+		unlock_parents_siglocks(false);
 	}
+out:
+	spin_unlock_irq(&tsk->sighand->siglock);
 }
 
 /*
Signature · 1 line
2.35.3
#193 lore raw

Re: [PATCH 16/16] signal: Always call do_notify_parent_cldstop with siglock held

Thank you for the patch! Perhaps something to improve:

Hi "Eric,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20220518]
[cannot apply to linux/master powerpc/next wireless-next/main wireless/main linus/master v5.18-rc7 v5.18-rc6 v5.18-rc5 v5.18-rc7] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Eric-W-Biederman/signal-alpha-Remove-unused-definition-of-TASK_REAL_PARENT/20220519-065947
base:    736ee37e2e8eed7fe48d0a37ee5a709514d478b3
config: parisc-randconfig-s032-20220519 (https://download.01.org/0day-ci/archive/20220521/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 11.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-dirty
# https://github.com/intel-lab-lkp/linux/commit/4b66a617bf6d095d33fe43e9dbcfdf2e0de9fb29
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Eric-W-Biederman/signal-alpha-Remove-unused-definition-of-TASK_REAL_PARENT/20220519-065947 git checkout 4b66a617bf6d095d33fe43e9dbcfdf2e0de9fb29
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=parisc SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

sparse warnings: (new ones prefixed by >>)
kernel/signal.c: note: in included file (through arch/parisc/include/uapi/asm/signal.h, arch/parisc/include/asm/signal.h, include/uapi/linux/signal.h, ...):
include/uapi/asm-generic/signal-defs.h:83:29: sparse: sparse: multiple address spaces given
kernel/signal.c:195:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:195:31: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:195:31: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:198:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:198:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:198:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:480:9: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:480:9: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:480:9: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:484:34: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:484:34: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:484:34: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:542:53: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct k_sigaction *ka @@ got struct k_sigaction [noderef] __rcu * @@
kernel/signal.c:542:53: sparse: expected struct k_sigaction *ka
kernel/signal.c:542:53: sparse: got struct k_sigaction [noderef] __rcu *
include/uapi/asm-generic/signal-defs.h:83:29: sparse: sparse: multiple address spaces given
kernel/signal.c:1261:9: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: cannot dereference this type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: cannot dereference this type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: cannot dereference this type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: cannot dereference this type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: cannot dereference this type
kernel/signal.c:1267:29: sparse: sparse: no member 'ip' in struct pt_regs
kernel/signal.c:1267:29: sparse: sparse: cast from unknown type
kernel/signal.c:1267:29: sparse: sparse: incompatible types for 'case' statement
kernel/signal.c:1267:29: sparse: sparse: incompatible types for 'case' statement
kernel/signal.c:1267:29: sparse: sparse: incompatible types for 'case' statement
kernel/signal.c:1267:29: sparse: sparse: incompatible types for 'case' statement
kernel/signal.c:1328:9: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:1328:9: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:1328:9: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:1329:16: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct k_sigaction *action @@ got struct k_sigaction [noderef] __rcu * @@
kernel/signal.c:1329:16: sparse: expected struct k_sigaction *action
kernel/signal.c:1329:16: sparse: got struct k_sigaction [noderef] __rcu *
kernel/signal.c:1349:34: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:1349:34: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:1349:34: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:1938:36: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:1938:36: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:1938:36: sparse: got struct spinlock [noderef] __rcu *

>> kernel/signal.c:2048:46: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct sighand_struct *m_sighand @@     got struct sighand_struct [noderef] __rcu *sighand @@
   kernel/signal.c:2048:46: sparse:     expected struct sighand_struct *m_sighand
   kernel/signal.c:2048:46: sparse:     got struct sighand_struct [noderef] __rcu *sighand
   kernel/signal.c:2057:24: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct task_struct *parent @@     got struct task_struct [noderef] __rcu *real_parent @@
   kernel/signal.c:2057:24: sparse:     expected struct task_struct *parent
   kernel/signal.c:2057:24: sparse:     got struct task_struct [noderef] __rcu *real_parent
   kernel/signal.c:2087:21: sparse: sparse: incompatible types in comparison expression (different address spaces):
>> kernel/signal.c:2087:21: sparse:    struct task_struct [noderef] __rcu *
>> kernel/signal.c:2087:21: sparse:    struct task_struct *
>> kernel/signal.c:2117:40: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct task_struct *parent @@     got struct task_struct [noderef] __rcu *real_parent @@
   kernel/signal.c:2117:40: sparse:     expected struct task_struct *parent
   kernel/signal.c:2117:40: sparse:     got struct task_struct [noderef] __rcu *real_parent
   kernel/signal.c:2119:46: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct sighand_struct *m_sighand @@     got struct sighand_struct [noderef] __rcu *sighand @@
   kernel/signal.c:2119:46: sparse:     expected struct sighand_struct *m_sighand
   kernel/signal.c:2119:46: sparse:     got struct sighand_struct [noderef] __rcu *sighand
>> kernel/signal.c:2120:50: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct sighand_struct *p_sighand @@     got struct sighand_struct [noderef] __rcu *sighand @@
   kernel/signal.c:2120:50: sparse:     expected struct sighand_struct *p_sighand
   kernel/signal.c:2120:50: sparse:     got struct sighand_struct [noderef] __rcu *sighand
>> kernel/signal.c:2125:58: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct sighand_struct *t_sighand @@     got struct sighand_struct [noderef] __rcu *sighand @@

kernel/signal.c:2125:58: sparse: expected struct sighand_struct *t_sighand
kernel/signal.c:2125:58: sparse: got struct sighand_struct [noderef] __rcu *sighand
kernel/signal.c:2171:44: sparse: sparse: cast removes address space '__rcu' of expression
kernel/signal.c:2190:65: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *tsk @@ got struct task_struct [noderef] __rcu *parent @@
kernel/signal.c:2190:65: sparse: expected struct task_struct *tsk
kernel/signal.c:2190:65: sparse: got struct task_struct [noderef] __rcu *parent
kernel/signal.c:2191:40: sparse: sparse: cast removes address space '__rcu' of expression
kernel/signal.c:2209:14: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct sighand_struct *psig @@ got struct sighand_struct [noderef] __rcu *[noderef] __rcu sighand @@
kernel/signal.c:2209:14: sparse: expected struct sighand_struct *psig
kernel/signal.c:2209:14: sparse: got struct sighand_struct [noderef] __rcu *[noderef] __rcu sighand
kernel/signal.c:2238:53: sparse: sparse: incorrect type in argument 3 (different address spaces) @@ expected struct task_struct *t @@ got struct task_struct [noderef] __rcu *parent @@
kernel/signal.c:2238:53: sparse: expected struct task_struct *t
kernel/signal.c:2238:53: sparse: got struct task_struct [noderef] __rcu *parent
kernel/signal.c:2239:34: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct task_struct *parent @@ got struct task_struct [noderef] __rcu *parent @@
kernel/signal.c:2239:34: sparse: expected struct task_struct *parent
kernel/signal.c:2239:34: sparse: got struct task_struct [noderef] __rcu *parent
kernel/signal.c:2269:24: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct task_struct *parent @@ got struct task_struct [noderef] __rcu *parent @@
kernel/signal.c:2269:24: sparse: expected struct task_struct *parent
kernel/signal.c:2269:24: sparse: got struct task_struct [noderef] __rcu *parent
kernel/signal.c:2272:24: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct task_struct *parent @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/signal.c:2272:24: sparse: expected struct task_struct *parent
kernel/signal.c:2272:24: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/signal.c:2307:17: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct sighand_struct *sighand @@ got struct sighand_struct [noderef] __rcu *sighand @@
kernel/signal.c:2307:17: sparse: expected struct sighand_struct *sighand
kernel/signal.c:2307:17: sparse: got struct sighand_struct [noderef] __rcu *sighand
kernel/signal.c:2341:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2341:41: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2341:41: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2343:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2343:39: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2343:39: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2428:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2428:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2428:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2440:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2440:31: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2440:31: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2479:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2479:31: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2479:31: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2481:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2481:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2481:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2584:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2584:41: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2584:41: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2599:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2599:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2599:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2656:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2656:41: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2656:41: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2668:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:2668:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:2668:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:2726:49: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct sighand_struct *sighand @@ got struct sighand_struct [noderef] __rcu *sighand @@
kernel/signal.c:2726:49: sparse: expected struct sighand_struct *sighand
kernel/signal.c:2726:49: sparse: got struct sighand_struct [noderef] __rcu *sighand
kernel/signal.c:3052:27: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3052:27: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3052:27: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3081:29: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3081:29: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3081:29: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3138:27: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3138:27: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3138:27: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3140:29: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3140:29: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3140:29: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3291:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3291:31: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3291:31: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3294:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3294:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3294:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3683:27: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3683:27: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3683:27: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3695:37: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3695:37: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3695:37: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3700:35: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3700:35: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3700:35: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:3705:29: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:3705:29: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:3705:29: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:4159:31: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:4159:31: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:4159:31: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:4171:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:4171:33: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:4171:33: sparse: got struct spinlock [noderef] __rcu *
kernel/signal.c:4189:11: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct k_sigaction *k @@ got struct k_sigaction [noderef] __rcu * @@
kernel/signal.c:4189:11: sparse: expected struct k_sigaction *k
kernel/signal.c:4189:11: sparse: got struct k_sigaction [noderef] __rcu *
kernel/signal.c:4191:25: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/signal.c:4191:25: sparse: expected struct spinlock [usertype] *lock
kernel/signal.c:4191:25: sparse: got struct spinlock [noderef] __rcu *

vim +2048 kernel/signal.c

1934
1935 void sigqueue_free(struct sigqueue *q)
1936 {
1937 unsigned long flags;

> 1938		spinlock_t *lock = &current->sighand->siglock;
  1939	
  1940		BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1941		/*
  1942		 * We must hold ->siglock while testing q->list
  1943		 * to serialize with collect_signal() or with
  1944		 * __exit_signal()->flush_sigqueue().
  1945		 */
  1946		spin_lock_irqsave(lock, flags);
  1947		q->flags &= ~SIGQUEUE_PREALLOC;
  1948		/*
  1949		 * If it is queued it will be freed when dequeued,
  1950		 * like the "regular" sigqueue.
  1951		 */
  1952		if (!list_empty(&q->list))
  1953			q = NULL;
  1954		spin_unlock_irqrestore(lock, flags);
  1955	
  1956		if (q)
  1957			__sigqueue_free(q);
  1958	}
  1959	
  1960	int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
  1961	{
  1962		int sig = q->info.si_signo;
  1963		struct sigpending *pending;
  1964		struct task_struct *t;
  1965		unsigned long flags;
  1966		int ret, result;
  1967	
  1968		BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1969	
  1970		ret = -1;
  1971		rcu_read_lock();
  1972		t = pid_task(pid, type);
  1973		if (!t || !likely(lock_task_sighand(t, &flags)))
  1974			goto ret;
  1975	
  1976		ret = 1; /* the signal is ignored */
  1977		result = TRACE_SIGNAL_IGNORED;
  1978		if (!prepare_signal(sig, t, false))
  1979			goto out;
  1980	
  1981		ret = 0;
  1982		if (unlikely(!list_empty(&q->list))) {
  1983			/*
  1984			 * If an SI_TIMER entry is already queue just increment
  1985			 * the overrun count.
  1986			 */
  1987			BUG_ON(q->info.si_code != SI_TIMER);
  1988			q->info.si_overrun++;
  1989			result = TRACE_SIGNAL_ALREADY_PENDING;
  1990			goto out;
  1991		}
  1992		q->info.si_overrun = 0;
  1993	
  1994		signalfd_notify(t, sig);
  1995		pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
  1996		list_add_tail(&q->list, &pending->list);
  1997		sigaddset(&pending->signal, sig);
  1998		complete_signal(sig, t, type);
  1999		result = TRACE_SIGNAL_DELIVERED;
  2000	out:
  2001		trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
  2002		unlock_task_sighand(t, &flags);
  2003	ret:
  2004		rcu_read_unlock();
  2005		return ret;
  2006	}
  2007	
  2008	/**
  2009	 * lock_parents_siglocks - Take current, real_parent, and parent's siglock
  2010	 * @lock_tracer: The tracers siglock is needed.
  2011	 *
  2012	 * There is no natural ordering to these locks so they must be sorted
  2013	 * before being taken.
  2014	 *
  2015	 * There are two complicating factors here:
  2016	 * - The locks live in sighand and sighand can be arbitrarily shared
  2017	 * - parent and real_parent can change when current's siglock is unlocked.
  2018	 *
  2019	 * To deal with this first the all of the sighand pointers are
  2020	 * gathered under current's siglock, and the sighand pointers are
  2021	 * sorted.  As siglock lives inside of sighand this also sorts the
  2022	 * siglock's by address.
  2023	 *
  2024	 * Then the siglocks are taken in order dropping current's siglock if
  2025	 * necessary.
  2026	 *
  2027	 * Finally if parent and real_parent have not changed return.
  2028	 * If they either parent has changed drop their locks and try again.
  2029	 *
  2030	 * Changing sighand is an infrequent and somewhat expensive operation
  2031	 * (unshare or exec) and so even in the worst case this loop
  2032	 * should not loop too many times before all of the proper locks are
  2033	 * taken in order.
  2034	 *
  2035	 * CONTEXT:
  2036	 * Must be called with @current->sighand->siglock held
  2037	 *
  2038	 * RETURNS:
  2039	 * current's, real_parent's, and parent's siglock held.
  2040	 */
  2041	static void lock_parents_siglocks(bool lock_tracer)
  2042		__releases(&current->sighand->siglock)
  2043		__acquires(&current->sighand->siglock)
  2044		__acquires(&current->real_parent->sighand->siglock)
  2045		__acquires(&current->parent->sighand->siglock)
  2046	{
  2047		struct task_struct *me = current;
> 2048		struct sighand_struct *m_sighand = me->sighand;
  2049	
  2050		lockdep_assert_held(&m_sighand->siglock);
  2051	
  2052		rcu_read_lock();
  2053		for (;;) {
  2054			struct task_struct *parent, *tracer;
  2055			struct sighand_struct *p_sighand, *t_sighand, *s1, *s2, *s3;
  2056	
  2057			parent = me->real_parent;
  2058			tracer = ptrace_parent(me);
  2059			if (!tracer || !lock_tracer)
  2060				tracer = parent;
  2061	
  2062			p_sighand = rcu_dereference(parent->sighand);
  2063			t_sighand = rcu_dereference(tracer->sighand);
  2064	
  2065			/* Sort the sighands so that s1 >= s2 >= s3 */
  2066			s1 = m_sighand;
  2067			s2 = p_sighand;
  2068			s3 = t_sighand;
  2069			if (s1 > s2)
  2070				swap(s1, s2);
  2071			if (s1 > s3)
  2072				swap(s1, s3);
  2073			if (s2 > s3)
  2074				swap(s2, s3);
  2075	
  2076			/* Take the locks in order */
  2077			if (s1 != m_sighand) {
  2078				spin_unlock(&m_sighand->siglock);
  2079				spin_lock(&s1->siglock);
  2080			}
  2081			if (s1 != s2)
  2082				spin_lock_nested(&s2->siglock, 1);
  2083			if (s2 != s3)
  2084				spin_lock_nested(&s3->siglock, 2);
  2085	
  2086			/* Verify the proper locks are held */
> 2087			if (likely((s1 = m_sighand) ||
  2088				   ((me->real_parent = parent) &&
  2089				    (me->parent = tracer) &&
  2090				    (parent->sighand = p_sighand) &&
  2091				    (tracer->sighand = t_sighand)))) {
  2092				break;
  2093			}
  2094	
  2095			/* Drop all but current's siglock */
  2096			if (p_sighand != m_sighand)
  2097				spin_unlock(&p_sighand->siglock);
  2098			if (t_sighand != p_sighand)
  2099				spin_unlock(&t_sighand->siglock);
  2100	
  2101			/*
  2102			 * Since [pt]_sighand will likely change if we go
  2103			 * around, and m_sighand is the only one held, make sure
  2104			 * it is subclass-0, since the above 's1 != m_sighand'
  2105			 * clause very much relies on that.
  2106			 */
  2107			lock_set_subclass(&m_sighand->siglock.dep_map, 0, _RET_IP_);
  2108		}
  2109		rcu_read_unlock();
  2110	}
  2111	
  2112	static void unlock_parents_siglocks(bool unlock_tracer)
  2113		__releases(&current->real_parent->sighand->siglock)
  2114		__releases(&current->parent->sighand->siglock)
  2115	{
  2116		struct task_struct *me = current;
> 2117		struct task_struct *parent = me->real_parent;
  2118		struct task_struct *tracer = ptrace_parent(me);
  2119		struct sighand_struct *m_sighand = me->sighand;
> 2120		struct sighand_struct *p_sighand = parent->sighand;
  2121	
  2122		if (p_sighand != m_sighand)
  2123			spin_unlock(&p_sighand->siglock);
  2124		if (tracer && unlock_tracer) {
> 2125			struct sighand_struct *t_sighand = tracer->sighand;

2126 if (t_sighand != p_sighand)
2127 spin_unlock(&t_sighand->siglock);
2128 }
2129 }
2130

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp

A message that is not in the archive (sent off-list or not yet received): CALWUPBdFDLuT7JaNGSJ_UXbHf8y9uKdC-SkAqzd=FQC0MX4nNQ@mail.gmail.com

#196 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

Quoted 5 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Kyle Huey

Is there a git branch somewhere I can pull to test this? It doesn't apply cleanly to Linus's tip.

https://kernel.googlesource.com/pub/scm/linux/kernel/git/ebiederm/user-namespace.git ptrace_stop-cleanup-for-v5.19

Yes that is the branch this all applies to.

This is my second round of cleanups this cycle for this code. I just keep finding little things that deserve to be changed, when I am working on the more substantial issues.

Eric

#197 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

Quoted 9 lines · Eric W. Biederman
Quoted 5 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Kyle Huey

Is there a git branch somewhere I can pull to test this? It doesn't apply cleanly to Linus's tip.

https://kernel.googlesource.com/pub/scm/linux/kernel/git/ebiederm/user-namespace.git ptrace_stop-cleanup-for-v5.19

Quoted 7 lines

Yes that is the branch this all applies to.

This is my second round of cleanups this cycle for this code. I just keep finding little things that deserve to be changed, when I am working on the more substantial issues.

Eric

When running the rr test suite, I see hangs like this

[  812.151505] watchdog: BUG: soft lockup - CPU#3 stuck for 548s!
[condvar_stress-:12152]
[  812.151529] Modules linked in: snd_hda_codec_realtek

snd_hda_codec_generic ledtrig_audio rfcomm cmac algif_hash algif_skcipher af_alg bnep dm_crypt intel_rapl_msr mei_hdcp snd_hda_codec_
hdmi intel_rapl_common snd_hda_intel x86_pkg_temp_thermal snd_intel_dspcfg snd_intel_sdw_acpi nls_iso8859_1 intel_powerclamp snd_hda_codec coretemp snd_hda_core snd_hwdep snd_pcm rtl8723be btcoexist snd_seq_midi snd_seq_midi_event rtl8723_common kvm_intel rtl_pci snd_rawmidi rtlwifi btusb btrtl btbcm snd_seq kvm mac80211 btintel btmtk snd_seq_device rapl bluetooth snd_timer i ntel_cstate hp_wmi cfg80211 serio_raw snd platform_profile ecdh_generic mei_me sparse_keymap efi_pstore wmi_bmof ee1004 joydev input_leds ecc libarc4 soundcore mei acpi_pad mac_hid sch_fq_c odel ipmi_devintf ipmi_msghandler msr vhost_vsock vmw_vsock_virtio_transport_common vsock vhost_net vhost vhost_iotlb tap vhci_hcd usbip_core parport_pc ppdev lp parport ip_tables x_tables autofs4 btrfs blake2b_generic xor raid6_pq zstd_compress

[  812.151570]  libcrc32c hid_generic usbhid hid i915 drm_buddy

i2c_algo_bit ttm drm_dp_helper cec rc_core crct10dif_pclmul drm_kms_helper crc32_pclmul syscopyarea ghash_clmulni_intel sysfi llrect sysimgblt fb_sys_fops aesni_intel crypto_simd cryptd r8169 psmouse drm i2c_i801 realtek ahci i2c_smbus xhci_pci libahci xhci_pci_renesas wmi video

[  812.151584] CPU: 3 PID: 12152 Comm: condvar_stress- Tainted: G
    I  L    5.18.0-rc1+ #2
[  812.151586] Hardware name: HP 750-280st/2B4B, BIOS A0.11 02/24/2016
[  812.151587] RIP: 0010:_raw_spin_unlock_irq+0x15/0x40
[  812.151591] Code: df e8 3f 1f 4a ff 90 5b 5d c3 66 66 2e 0f 1f 84
00 00 00 00 00 0f 1f 44 00 00 55 48 89 e5 c6 07 00 0f 1f 00 fb 0f 1f
44 00 00 <bf> 01 00 00 00 e8 41 95 46 ff 65 8b 05 9
a c1 9a 5f 85 c0 74 02 5d
[  812.151593] RSP: 0018:ffffa863c246bd70 EFLAGS: 00000246
[  812.151594] RAX: ffff8bc0913f6400 RBX: ffff8bc0913f6400 RCX: 0000000000000000
[  812.151595] RDX: 0000000000000002 RSI: 00000000000a0013 RDI: ffff8bc089b63180
[  812.151596] RBP: ffffa863c246bd70 R08: ffff8bc0811d6b40 R09: ffff8bc089b63180
[  812.151597] R10: 0000000000000000 R11: 0000000000000004 R12: ffff8bc0913f6400
[  812.151597] R13: ffff8bc089b63180 R14: ffff8bc0913f6400 R15: ffffa863c246be68
[  812.151598] FS:  00007f612dda5700(0000) GS:ffff8bc7e24c0000(0000)
knlGS:0000000000000000
[  812.151599] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  812.151600] CR2: 000055e70715692e CR3: 000000010b4e8005 CR4: 00000000003706e4
[  812.151601] Call Trace:
[  812.151602]  <TASK>
[  812.151604]  do_signal_stop+0x228/0x260
[  812.151606]  get_signal+0x43a/0x8e0
[  812.151608]  arch_do_signal_or_restart+0x37/0x7d0
[  812.151610]  ? __this_cpu_preempt_check+0x13/0x20
[  812.151612]  ? __perf_event_task_sched_in+0x81/0x230
[  812.151616]  ? __this_cpu_preempt_check+0x13/0x20
[  812.151617]  exit_to_user_mode_prepare+0x130/0x1a0
[  812.151620]  syscall_exit_to_user_mode+0x26/0x40
[  812.151621]  ret_from_fork+0x15/0x30
[  812.151623] RIP: 0033:0x7f612dfcd125
[  812.151625] Code: 48 85 ff 74 3d 48 85 f6 74 38 48 83 ee 10 48 89
4e 08 48 89 3e 48 89 d7 4c 89 c2 4d 89 c8 4c 8b 54 24 08 b8 38 00 00
00 0f 05 <48> 85 c0 7c 13 74 01 c3 31 ed 58 5f ff d
0 48 89 c7 b8 3c 00 00 00
[  812.151626] RSP: 002b:00007f612dda4fb0 EFLAGS: 00000246 ORIG_RAX:
0000000000000038
[  812.151628] RAX: 0000000000000000 RBX: 00007f612dda5700 RCX: ffffffffffffffff
[  812.151628] RDX: 00007f612dda59d0 RSI: 00007f612dda4fb0 RDI: 00000000003d0f00
[  812.151629] RBP: 00007ffd59ad20b0 R08: 00007f612dda5700 R09: 00007f612dda5700
[  812.151630] R10: 00007f612dda59d0 R11: 0000000000000246 R12: 00007ffd59ad20ae
[  812.151631] R13: 00007ffd59ad20af R14: 00007ffd59ad20b0 R15: 00007f612dda4fc0
[  812.151632]  </TASK>

- Kyle

#198 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

Quoted 13 lines · Kyle Huey
Quoted 11 lines · Eric W. Biederman
Quoted 5 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Kyle Huey

Is there a git branch somewhere I can pull to test this? It doesn't apply cleanly to Linus's tip.

https://kernel.googlesource.com/pub/scm/linux/kernel/git/ebiederm/user-namespace.git ptrace_stop-cleanup-for-v5.19

Yes that is the branch this all applies to.

Quoted 7 lines
Quoted 5 lines

This is my second round of cleanups this cycle for this code. I just keep finding little things that deserve to be changed, when I am working on the more substantial issues.

Eric

When running the rr test suite, I see hangs like this

Thanks. I will dig into this.

Is there an easy way I can run the rr test suite to see if I can reproduce this myself?

Thanks,
Eric

Quoted 70 lines
[  812.151505] watchdog: BUG: soft lockup - CPU#3 stuck for 548s!
[condvar_stress-:12152]
[  812.151529] Modules linked in: snd_hda_codec_realtek

snd_hda_codec_generic ledtrig_audio rfcomm cmac algif_hash algif_skcipher af_alg bnep dm_crypt intel_rapl_msr mei_hdcp snd_hda_codec_
hdmi intel_rapl_common snd_hda_intel x86_pkg_temp_thermal snd_intel_dspcfg snd_intel_sdw_acpi nls_iso8859_1 intel_powerclamp snd_hda_codec coretemp snd_hda_core snd_hwdep snd_pcm rtl8723be btcoexist snd_seq_midi snd_seq_midi_event rtl8723_common kvm_intel rtl_pci snd_rawmidi rtlwifi btusb btrtl btbcm snd_seq kvm mac80211 btintel btmtk snd_seq_device rapl bluetooth snd_timer i ntel_cstate hp_wmi cfg80211 serio_raw snd platform_profile ecdh_generic mei_me sparse_keymap efi_pstore wmi_bmof ee1004 joydev input_leds ecc libarc4 soundcore mei acpi_pad mac_hid sch_fq_c odel ipmi_devintf ipmi_msghandler msr vhost_vsock vmw_vsock_virtio_transport_common vsock vhost_net vhost vhost_iotlb tap vhci_hcd usbip_core parport_pc ppdev lp parport ip_tables x_tables autofs4 btrfs blake2b_generic xor raid6_pq zstd_compress

[  812.151570]  libcrc32c hid_generic usbhid hid i915 drm_buddy

i2c_algo_bit ttm drm_dp_helper cec rc_core crct10dif_pclmul drm_kms_helper crc32_pclmul syscopyarea ghash_clmulni_intel sysfi llrect sysimgblt fb_sys_fops aesni_intel crypto_simd cryptd r8169 psmouse drm i2c_i801 realtek ahci i2c_smbus xhci_pci libahci xhci_pci_renesas wmi video

[  812.151584] CPU: 3 PID: 12152 Comm: condvar_stress- Tainted: G
    I  L    5.18.0-rc1+ #2
[  812.151586] Hardware name: HP 750-280st/2B4B, BIOS A0.11 02/24/2016
[  812.151587] RIP: 0010:_raw_spin_unlock_irq+0x15/0x40
[  812.151591] Code: df e8 3f 1f 4a ff 90 5b 5d c3 66 66 2e 0f 1f 84
00 00 00 00 00 0f 1f 44 00 00 55 48 89 e5 c6 07 00 0f 1f 00 fb 0f 1f
44 00 00 <bf> 01 00 00 00 e8 41 95 46 ff 65 8b 05 9
a c1 9a 5f 85 c0 74 02 5d
[  812.151593] RSP: 0018:ffffa863c246bd70 EFLAGS: 00000246
[  812.151594] RAX: ffff8bc0913f6400 RBX: ffff8bc0913f6400 RCX: 0000000000000000
[  812.151595] RDX: 0000000000000002 RSI: 00000000000a0013 RDI: ffff8bc089b63180
[  812.151596] RBP: ffffa863c246bd70 R08: ffff8bc0811d6b40 R09: ffff8bc089b63180
[  812.151597] R10: 0000000000000000 R11: 0000000000000004 R12: ffff8bc0913f6400
[  812.151597] R13: ffff8bc089b63180 R14: ffff8bc0913f6400 R15: ffffa863c246be68
[  812.151598] FS:  00007f612dda5700(0000) GS:ffff8bc7e24c0000(0000)
knlGS:0000000000000000
[  812.151599] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  812.151600] CR2: 000055e70715692e CR3: 000000010b4e8005 CR4: 00000000003706e4
[  812.151601] Call Trace:
[  812.151602]  <TASK>
[  812.151604]  do_signal_stop+0x228/0x260
[  812.151606]  get_signal+0x43a/0x8e0
[  812.151608]  arch_do_signal_or_restart+0x37/0x7d0
[  812.151610]  ? __this_cpu_preempt_check+0x13/0x20
[  812.151612]  ? __perf_event_task_sched_in+0x81/0x230
[  812.151616]  ? __this_cpu_preempt_check+0x13/0x20
[  812.151617]  exit_to_user_mode_prepare+0x130/0x1a0
[  812.151620]  syscall_exit_to_user_mode+0x26/0x40
[  812.151621]  ret_from_fork+0x15/0x30
[  812.151623] RIP: 0033:0x7f612dfcd125
[  812.151625] Code: 48 85 ff 74 3d 48 85 f6 74 38 48 83 ee 10 48 89
4e 08 48 89 3e 48 89 d7 4c 89 c2 4d 89 c8 4c 8b 54 24 08 b8 38 00 00
00 0f 05 <48> 85 c0 7c 13 74 01 c3 31 ed 58 5f ff d
0 48 89 c7 b8 3c 00 00 00
[  812.151626] RSP: 002b:00007f612dda4fb0 EFLAGS: 00000246 ORIG_RAX:
0000000000000038
[  812.151628] RAX: 0000000000000000 RBX: 00007f612dda5700 RCX: ffffffffffffffff
[  812.151628] RDX: 00007f612dda59d0 RSI: 00007f612dda4fb0 RDI: 00000000003d0f00
[  812.151629] RBP: 00007ffd59ad20b0 R08: 00007f612dda5700 R09: 00007f612dda5700
[  812.151630] R10: 00007f612dda59d0 R11: 0000000000000246 R12: 00007ffd59ad20ae
[  812.151631] R13: 00007ffd59ad20af R14: 00007ffd59ad20b0 R15: 00007f612dda4fc0
[  812.151632]  </TASK>

- Kyle

#199 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

Quoted 20 lines · Eric W. Biederman
Quoted 17 lines · Kyle Huey
Quoted 15 lines · Eric W. Biederman
Quoted 5 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Kyle Huey

Is there a git branch somewhere I can pull to test this? It doesn't apply cleanly to Linus's tip.

https://kernel.googlesource.com/pub/scm/linux/kernel/git/ebiederm/user-namespace.git ptrace_stop-cleanup-for-v5.19

Yes that is the branch this all applies to.

This is my second round of cleanups this cycle for this code. I just keep finding little things that deserve to be changed, when I am working on the more substantial issues.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Eric

When running the rr test suite, I see hangs like this

Thanks. I will dig into this.

Is there an easy way I can run the rr test suite to see if I can reproduce this myself?

It should be a straight forward
1. https://github.com/rr-debugger/rr.git
2. mkdir obj-rr && cd obj-rr
3. cmake ../rr
4. make -jN
5. make check

If you have trouble with it feel free to email me off list.

- Kyle

Quoted 73 lines

Thanks,
Eric

Quoted 70 lines
[  812.151505] watchdog: BUG: soft lockup - CPU#3 stuck for 548s!
[condvar_stress-:12152]
[  812.151529] Modules linked in: snd_hda_codec_realtek

snd_hda_codec_generic ledtrig_audio rfcomm cmac algif_hash algif_skcipher af_alg bnep dm_crypt intel_rapl_msr mei_hdcp snd_hda_codec_
hdmi intel_rapl_common snd_hda_intel x86_pkg_temp_thermal snd_intel_dspcfg snd_intel_sdw_acpi nls_iso8859_1 intel_powerclamp snd_hda_codec coretemp snd_hda_core snd_hwdep snd_pcm rtl8723be btcoexist snd_seq_midi snd_seq_midi_event rtl8723_common kvm_intel rtl_pci snd_rawmidi rtlwifi btusb btrtl btbcm snd_seq kvm mac80211 btintel btmtk snd_seq_device rapl bluetooth snd_timer i ntel_cstate hp_wmi cfg80211 serio_raw snd platform_profile ecdh_generic mei_me sparse_keymap efi_pstore wmi_bmof ee1004 joydev input_leds ecc libarc4 soundcore mei acpi_pad mac_hid sch_fq_c odel ipmi_devintf ipmi_msghandler msr vhost_vsock vmw_vsock_virtio_transport_common vsock vhost_net vhost vhost_iotlb tap vhci_hcd usbip_core parport_pc ppdev lp parport ip_tables x_tables autofs4 btrfs blake2b_generic xor raid6_pq zstd_compress

[  812.151570]  libcrc32c hid_generic usbhid hid i915 drm_buddy

i2c_algo_bit ttm drm_dp_helper cec rc_core crct10dif_pclmul drm_kms_helper crc32_pclmul syscopyarea ghash_clmulni_intel sysfi llrect sysimgblt fb_sys_fops aesni_intel crypto_simd cryptd r8169 psmouse drm i2c_i801 realtek ahci i2c_smbus xhci_pci libahci xhci_pci_renesas wmi video

[  812.151584] CPU: 3 PID: 12152 Comm: condvar_stress- Tainted: G
    I  L    5.18.0-rc1+ #2
[  812.151586] Hardware name: HP 750-280st/2B4B, BIOS A0.11 02/24/2016
[  812.151587] RIP: 0010:_raw_spin_unlock_irq+0x15/0x40
[  812.151591] Code: df e8 3f 1f 4a ff 90 5b 5d c3 66 66 2e 0f 1f 84
00 00 00 00 00 0f 1f 44 00 00 55 48 89 e5 c6 07 00 0f 1f 00 fb 0f 1f
44 00 00 <bf> 01 00 00 00 e8 41 95 46 ff 65 8b 05 9
a c1 9a 5f 85 c0 74 02 5d
[  812.151593] RSP: 0018:ffffa863c246bd70 EFLAGS: 00000246
[  812.151594] RAX: ffff8bc0913f6400 RBX: ffff8bc0913f6400 RCX: 0000000000000000
[  812.151595] RDX: 0000000000000002 RSI: 00000000000a0013 RDI: ffff8bc089b63180
[  812.151596] RBP: ffffa863c246bd70 R08: ffff8bc0811d6b40 R09: ffff8bc089b63180
[  812.151597] R10: 0000000000000000 R11: 0000000000000004 R12: ffff8bc0913f6400
[  812.151597] R13: ffff8bc089b63180 R14: ffff8bc0913f6400 R15: ffffa863c246be68
[  812.151598] FS:  00007f612dda5700(0000) GS:ffff8bc7e24c0000(0000)
knlGS:0000000000000000
[  812.151599] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  812.151600] CR2: 000055e70715692e CR3: 000000010b4e8005 CR4: 00000000003706e4
[  812.151601] Call Trace:
[  812.151602]  <TASK>
[  812.151604]  do_signal_stop+0x228/0x260
[  812.151606]  get_signal+0x43a/0x8e0
[  812.151608]  arch_do_signal_or_restart+0x37/0x7d0
[  812.151610]  ? __this_cpu_preempt_check+0x13/0x20
[  812.151612]  ? __perf_event_task_sched_in+0x81/0x230
[  812.151616]  ? __this_cpu_preempt_check+0x13/0x20
[  812.151617]  exit_to_user_mode_prepare+0x130/0x1a0
[  812.151620]  syscall_exit_to_user_mode+0x26/0x40
[  812.151621]  ret_from_fork+0x15/0x30
[  812.151623] RIP: 0033:0x7f612dfcd125
[  812.151625] Code: 48 85 ff 74 3d 48 85 f6 74 38 48 83 ee 10 48 89
4e 08 48 89 3e 48 89 d7 4c 89 c2 4d 89 c8 4c 8b 54 24 08 b8 38 00 00
00 0f 05 <48> 85 c0 7c 13 74 01 c3 31 ed 58 5f ff d
0 48 89 c7 b8 3c 00 00 00
[  812.151626] RSP: 002b:00007f612dda4fb0 EFLAGS: 00000246 ORIG_RAX:
0000000000000038
[  812.151628] RAX: 0000000000000000 RBX: 00007f612dda5700 RCX: ffffffffffffffff
[  812.151628] RDX: 00007f612dda59d0 RSI: 00007f612dda4fb0 RDI: 00000000003d0f00
[  812.151629] RBP: 00007ffd59ad20b0 R08: 00007f612dda5700 R09: 00007f612dda5700
[  812.151630] R10: 00007f612dda59d0 R11: 0000000000000246 R12: 00007ffd59ad20ae
[  812.151631] R13: 00007ffd59ad20af R14: 00007ffd59ad20b0 R15: 00007f612dda4fc0
[  812.151632]  </TASK>

- Kyle

#200 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

In reply to Eric W. Biederman
Quoted 7 lines · Eric W. Biederman

For ptrace_stop to work on PREEMT_RT no spinlocks can be taken once ptrace_freeze_traced has completed successfully. Which fundamentally means the lock dance of dropping siglock and grabbing tasklist_lock does not work on PREEMPT_RT. So I have worked through what is necessary so that tasklist_lock does not need to be grabbed in ptrace_stop after siglock is dropped.

…
It took me a while to realise that this is a follow-up I somehow assumed that you added a few patches on top. Might have been the yesterday's heat. b4 also refused to download this series because the v4 in this thread looked newer… Anyway. Both series applied:

| =============================
| WARNING: suspicious RCU usage
| 5.18.0-rc7+ #16 Not tainted
| -----------------------------
| include/linux/ptrace.h:120 suspicious rcu_dereference_check() usage!
|
| other info that might help us debug this:
|
| rcu_scheduler_active = 2, debug_locks = 1
| 2 locks held by ssdd/1734:
|  #0: ffff88800eaa6918 (&sighand->siglock){....}-{2:2}, at: lock_parents_siglocks+0xf0/0x3b0
|  #1: ffff88800eaa71d8 (&sighand->siglock/2){....}-{2:2}, at: lock_parents_siglocks+0x115/0x3b0
|
| stack backtrace:
| CPU: 2 PID: 1734 Comm: ssdd Not tainted 5.18.0-rc7+ #16
| Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.0-debian-1.16.0-4 04/01/2014
| Call Trace:
|  <TASK>
|  dump_stack_lvl+0x45/0x5a
|  unlock_parents_siglocks+0xb6/0xc0
|  ptrace_stop+0xb9/0x390
|  get_signal+0x51c/0x8d0
|  arch_do_signal_or_restart+0x31/0x750
|  exit_to_user_mode_prepare+0x157/0x220
|  irqentry_exit_to_user_mode+0x5/0x50
|  asm_sysvec_apic_timer_interrupt+0x12/0x20

That is ptrace_parent() in unlock_parents_siglocks().

Sebastian

#201 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

Quoted 40 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Eric W. Biederman

For ptrace_stop to work on PREEMT_RT no spinlocks can be taken once ptrace_freeze_traced has completed successfully. Which fundamentally means the lock dance of dropping siglock and grabbing tasklist_lock does not work on PREEMPT_RT. So I have worked through what is necessary so that tasklist_lock does not need to be grabbed in ptrace_stop after siglock is dropped.

…
It took me a while to realise that this is a follow-up I somehow assumed that you added a few patches on top. Might have been the yesterday's heat. b4 also refused to download this series because the v4 in this thread looked newer… Anyway. Both series applied:

| ==============> | WARNING: suspicious RCU usage
| 5.18.0-rc7+ #16 Not tainted
| -----------------------------
| include/linux/ptrace.h:120 suspicious rcu_dereference_check() usage!
|
| other info that might help us debug this:
|
| rcu_scheduler_active = 2, debug_locks = 1
| 2 locks held by ssdd/1734:
|  #0: ffff88800eaa6918 (&sighand->siglock){....}-{2:2}, at: lock_parents_siglocks+0xf0/0x3b0
|  #1: ffff88800eaa71d8 (&sighand->siglock/2){....}-{2:2}, at: lock_parents_siglocks+0x115/0x3b0
|
| stack backtrace:
| CPU: 2 PID: 1734 Comm: ssdd Not tainted 5.18.0-rc7+ #16
| Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.0-debian-1.16.0-4 04/01/2014
| Call Trace:
|  <TASK>
|  dump_stack_lvl+0x45/0x5a
|  unlock_parents_siglocks+0xb6/0xc0
|  ptrace_stop+0xb9/0x390
|  get_signal+0x51c/0x8d0
|  arch_do_signal_or_restart+0x31/0x750
|  exit_to_user_mode_prepare+0x157/0x220
|  irqentry_exit_to_user_mode+0x5/0x50
|  asm_sysvec_apic_timer_interrupt+0x12/0x20
Quoted 1 line

That is ptrace_parent() in unlock_parents_siglocks().

How odd. I thought I had the appropriate lockdep config options enabled in my test build to catch things like this. I guess not.

Now I am trying to think how to tell it that holding the appropriate iglock makes this ok.

Eric

author maintainer
#202 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

Quoted 42 lines · Eric W. Biederman
Quoted 40 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Eric W. Biederman

For ptrace_stop to work on PREEMT_RT no spinlocks can be taken once ptrace_freeze_traced has completed successfully. Which fundamentally means the lock dance of dropping siglock and grabbing tasklist_lock does not work on PREEMPT_RT. So I have worked through what is necessary so that tasklist_lock does not need to be grabbed in ptrace_stop after siglock is dropped.

…
It took me a while to realise that this is a follow-up I somehow assumed that you added a few patches on top. Might have been the yesterday's heat. b4 also refused to download this series because the v4 in this thread looked newer… Anyway. Both series applied:

| ==============> > | WARNING: suspicious RCU usage
| 5.18.0-rc7+ #16 Not tainted
| -----------------------------
| include/linux/ptrace.h:120 suspicious rcu_dereference_check() usage!
|
| other info that might help us debug this:
|
| rcu_scheduler_active = 2, debug_locks = 1
| 2 locks held by ssdd/1734:
|  #0: ffff88800eaa6918 (&sighand->siglock){....}-{2:2}, at: lock_parents_siglocks+0xf0/0x3b0
|  #1: ffff88800eaa71d8 (&sighand->siglock/2){....}-{2:2}, at: lock_parents_siglocks+0x115/0x3b0
|
| stack backtrace:
| CPU: 2 PID: 1734 Comm: ssdd Not tainted 5.18.0-rc7+ #16
| Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.0-debian-1.16.0-4 04/01/2014
| Call Trace:
|  <TASK>
|  dump_stack_lvl+0x45/0x5a
|  unlock_parents_siglocks+0xb6/0xc0
|  ptrace_stop+0xb9/0x390
|  get_signal+0x51c/0x8d0
|  arch_do_signal_or_restart+0x31/0x750
|  exit_to_user_mode_prepare+0x157/0x220
|  irqentry_exit_to_user_mode+0x5/0x50
|  asm_sysvec_apic_timer_interrupt+0x12/0x20
Quoted 7 lines
Quoted 1 line

That is ptrace_parent() in unlock_parents_siglocks().

How odd. I thought I had the appropriate lockdep config options enabled in my test build to catch things like this. I guess not.

Now I am trying to think how to tell it that holding the appropriate iglock makes this ok.

The typical annotation is something like:

rcu_dereference_protected(foo, lockdep_is_held(&bar))

Except in this case I think the problem is that bar depends on foo in non-trivial ways. That is, foo is 'task->parent' and bar is 'task->parent->sighand->siglock' or something.

The other option is to use rcu_dereference_raw() in this one instance and have a comment that explains the situation.

#203 lore raw

Re: [PATCH 00/16] ptrace: cleanups and calling do_cldstop with only siglock

In reply to Eric W. Biederman
Quoted 2 lines · Eric W. Biederman

After this set of changes only cgroup_enter_frozen should remain a stumbling block for PREEMPT_RT in the ptrace_stop path.

Yes, I can confirm that. I have no systemd-less system at hand which means I can't boot a kernel without CGROUP support. But after removing cgroup_{enter|leave}_frozen() in ptrace_stop() I don't see the problems I saw earlier.

Sebastian

#204 lore raw

cover ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

In reply to Eric W. Biederman

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

Keeping SIGKILL working for anything after the process has been killed is also a real pain from an implementation point of view.

So I am attempting to remove this wart in the userspace API and see if anyone cares.

Eric W. Biederman (3):
signal: Ensure SIGNAL_GROUP_EXIT gets set in do_group_exit
signal: Guarantee that SIGNAL_GROUP_EXIT is set on process exit
signal: Drop signals received after a fatal signal has been processed

 fs/coredump.c                |  2 +-
 include/linux/sched/signal.h |  1 +
 kernel/exit.c                | 20 +++++++++++++++++++-
 kernel/fork.c                |  2 ++
 kernel/signal.c              |  3 ++-
 5 files changed, 25 insertions(+), 3 deletions(-)

Eric

#205 lore raw

PATCH 1/3 signal: Ensure SIGNAL_GROUP_EXIT gets set in do_group_exit

The function do_group_exit has an optimization that avoids taking siglock and doing the work to find other threads in the signal group and shutting them down.

It is very desirable for SIGNAL_GROUP_EXIT to always been set whenever it is decided for the process to exit. That ensures only a single place needs to be tested, and a single bit of state needs to be looked at. This makes the optimization in do_group_exit counter productive.

Make the code and maintenance simpler by removing this unnecessary option.

---
 kernel/exit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/exit.c +1 -1
index f072959fcab7..96e4b12edea8 100644
@@ -905,7 +905,7 @@ do_group_exit(int exit_code)
 		exit_code = sig->group_exit_code;
 	else if (sig->group_exec_task)
 		exit_code = 0;
-	else if (!thread_group_empty(current)) {
+	else {
 		struct sighand_struct *const sighand = current->sighand;
 
 		spin_lock_irq(&sighand->siglock);
Signature · 1 line
2.35.3
#206 lore raw

PATCH 2/3 signal: Guarantee that SIGNAL_GROUP_EXIT is set on process exit

In reply to Eric W. Biederman

Track how many threads have not started exiting and when the last thread starts exiting set SIGNAL_GROUP_EXIT.

This guarantees that SIGNAL_GROUP_EXIT will get set when a process exits. In practice this achieves nothing as glibc's implementation of _exit calls sys_group_exit then sys_exit. While glibc's implemenation of pthread_exit calls exit (which cleansup and calls _exit) if it is the last thread and sys_exit if it is the last thread.

This means the only way the kernel might observe a process that does not set call exit_group is if the language runtime does not use glibc.

With more cleanups I hope to move the decrement of quick_threads earlier.

---
 include/linux/sched/signal.h |  1 +
 kernel/exit.c                | 18 ++++++++++++++++++
 kernel/fork.c                |  2 ++
 3 files changed, 21 insertions(+)
include/linux/sched/signal.h +1 -0
index cafbe03eed01..20099268fa25 100644
@@ -94,6 +94,7 @@ struct signal_struct {
 	refcount_t		sigcnt;
 	atomic_t		live;
 	int			nr_threads;
+	int			quick_threads;
 	struct list_head	thread_head;
 
 	wait_queue_head_t	wait_chldexit;	/* for wait4() */
kernel/exit.c +18 -0
index 96e4b12edea8..beaedb867bd3 100644
@@ -733,11 +733,29 @@ static void check_stack_usage(void)
 static inline void check_stack_usage(void) {}
 #endif
 
+static void synchronize_group_exit(struct task_struct *tsk, long code)
+{
+	struct sighand_struct *sighand = tsk->sighand;
+	struct signal_struct *signal = tsk->signal;
+
+	spin_lock_irq(&sighand->siglock);
+	signal->quick_threads--;
+	if ((signal->quick_threads == 0) &&
+	    !(signal->flags & SIGNAL_GROUP_EXIT)) {
+		signal->flags = SIGNAL_GROUP_EXIT;
+		signal->group_exit_code = code;
+		signal->group_stop_count = 0;
+	}
+	spin_unlock_irq(&sighand->siglock);
+}
+
 void __noreturn do_exit(long code)
 {
 	struct task_struct *tsk = current;
 	int group_dead;
 
+	synchronize_group_exit(tsk, code);
+
 	WARN_ON(tsk->plug);
 
 	kcov_task_exit(tsk);
kernel/fork.c +2 -0
index 9d44f2d46c69..67813b25a567 100644
@@ -1692,6 +1692,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
 		return -ENOMEM;
 
 	sig->nr_threads = 1;
+	sig->quick_threads = 1;
 	atomic_set(&sig->live, 1);
 	refcount_set(&sig->sigcnt, 1);
 
@@ -2444,6 +2445,7 @@ static __latent_entropy struct task_struct *copy_process(
 			__this_cpu_inc(process_counts);
 		} else {
 			current->signal->nr_threads++;
+			current->signal->quick_threads++;
 			atomic_inc(&current->signal->live);
 			refcount_inc(&current->signal->sigcnt);
 			task_join_group_stop(p);
Signature · 1 line
2.35.3
#207 lore raw

Re: [PATCH 2/3] signal: Guarantee that SIGNAL_GROUP_EXIT is set on process exit

Thank you for the patch! Perhaps something to improve:

Hi "Eric,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.19-rc3 next-20220622]
[cannot apply to kees/for-next/pstore]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Eric-W-Biederman/signal-Ensure-SIGNAL_GROUP_EXIT-gets-set-in-do_group_exit/20220623-014543
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 3abc3ae553c7ed73365b385b9a4cffc5176aae45
config: nios2-randconfig-s032-20220622 (https://download.01.org/0day-ci/archive/20220623/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.4-31-g4880bd19-dirty
# https://github.com/intel-lab-lkp/linux/commit/0e1cfa4e4efe9c701f3ef5665c22310ac7aa7e7e
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Eric-W-Biederman/signal-Ensure-SIGNAL_GROUP_EXIT-gets-set-in-do_group_exit/20220623-014543 git checkout 0e1cfa4e4efe9c701f3ef5665c22310ac7aa7e7e
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=nios2 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

sparse warnings: (new ones prefixed by >>)
kernel/exit.c:281:37: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *tsk @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/exit.c:281:37: sparse: expected struct task_struct *tsk
kernel/exit.c:281:37: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/exit.c:284:32: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *task @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/exit.c:284:32: sparse: expected struct task_struct *task
kernel/exit.c:284:32: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/exit.c:285:35: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *task @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/exit.c:285:35: sparse: expected struct task_struct *task
kernel/exit.c:285:35: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/exit.c:330:24: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct task_struct *parent @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/exit.c:330:24: sparse: expected struct task_struct *parent
kernel/exit.c:330:24: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/exit.c:357:27: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:357:27: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:357:27: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:360:29: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:360:29: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:360:29: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:583:29: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct task_struct *reaper @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/exit.c:583:29: sparse: expected struct task_struct *reaper
kernel/exit.c:583:29: sparse: got struct task_struct [noderef] __rcu *real_parent
kernel/exit.c:585:29: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct task_struct *reaper @@ got struct task_struct [noderef] __rcu *real_parent @@
kernel/exit.c:585:29: sparse: expected struct task_struct *reaper
kernel/exit.c:585:29: sparse: got struct task_struct [noderef] __rcu *real_parent

>> kernel/exit.c:738:45: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected struct sighand_struct *sighand @@     got struct sighand_struct [noderef] __rcu *sighand @@

kernel/exit.c:738:45: sparse: expected struct sighand_struct *sighand
kernel/exit.c:738:45: sparse: got struct sighand_struct [noderef] __rcu *sighand
kernel/exit.c:927:63: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct sighand_struct *const sighand @@ got struct sighand_struct [noderef] __rcu *sighand @@
kernel/exit.c:927:63: sparse: expected struct sighand_struct *const sighand
kernel/exit.c:927:63: sparse: got struct sighand_struct [noderef] __rcu *sighand
kernel/exit.c:1082:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1082:39: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1082:39: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1107:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1107:41: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1107:41: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1196:25: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1196:25: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1196:25: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1211:27: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1211:27: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1211:27: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1262:25: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1262:25: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1262:25: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1265:35: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1265:35: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1265:35: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1271:27: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct spinlock [usertype] *lock @@ got struct spinlock [noderef] __rcu * @@
kernel/exit.c:1271:27: sparse: expected struct spinlock [usertype] *lock
kernel/exit.c:1271:27: sparse: got struct spinlock [noderef] __rcu *
kernel/exit.c:1452:59: sparse: sparse: incompatible types in comparison expression (different base types):
kernel/exit.c:1452:59: sparse: void *
kernel/exit.c:1452:59: sparse: struct task_struct [noderef] __rcu *
kernel/exit.c:1468:25: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected struct task_struct *parent @@ got struct task_struct [noderef] __rcu * @@
kernel/exit.c:1468:25: sparse: expected struct task_struct *parent
kernel/exit.c:1468:25: sparse: got struct task_struct [noderef] __rcu *
kernel/exit.c: note: in included file (through arch/nios2/include/uapi/asm/elf.h, arch/nios2/include/asm/elf.h, include/linux/elf.h, ...):
include/linux/ptrace.h:92:40: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *p1 @@ got struct task_struct [noderef] __rcu *real_parent @@
include/linux/ptrace.h:92:40: sparse: expected struct task_struct *p1
include/linux/ptrace.h:92:40: sparse: got struct task_struct [noderef] __rcu *real_parent
include/linux/ptrace.h:92:60: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct task_struct *p2 @@ got struct task_struct [noderef] __rcu *parent @@
include/linux/ptrace.h:92:60: sparse: expected struct task_struct *p2
include/linux/ptrace.h:92:60: sparse: got struct task_struct [noderef] __rcu *parent
include/linux/ptrace.h:92:40: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *p1 @@ got struct task_struct [noderef] __rcu *real_parent @@
include/linux/ptrace.h:92:40: sparse: expected struct task_struct *p1
include/linux/ptrace.h:92:40: sparse: got struct task_struct [noderef] __rcu *real_parent
include/linux/ptrace.h:92:60: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct task_struct *p2 @@ got struct task_struct [noderef] __rcu *parent @@
include/linux/ptrace.h:92:60: sparse: expected struct task_struct *p2
include/linux/ptrace.h:92:60: sparse: got struct task_struct [noderef] __rcu *parent
kernel/exit.c: note: in included file (through include/linux/sched/signal.h, include/linux/rcuwait.h, include/linux/percpu-rwsem.h, ...):
include/linux/sched/task.h:110:21: sparse: sparse: context imbalance in 'wait_task_zombie' - unexpected unlock include/linux/sched/task.h:110:21: sparse: sparse: context imbalance in 'wait_task_stopped' - unexpected unlock include/linux/sched/task.h:110:21: sparse: sparse: context imbalance in 'wait_task_continued' - unexpected unlock
kernel/exit.c: note: in included file (through arch/nios2/include/uapi/asm/elf.h, arch/nios2/include/asm/elf.h, include/linux/elf.h, ...):
include/linux/ptrace.h:92:40: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct task_struct *p1 @@ got struct task_struct [noderef] __rcu *real_parent @@
include/linux/ptrace.h:92:40: sparse: expected struct task_struct *p1
include/linux/ptrace.h:92:40: sparse: got struct task_struct [noderef] __rcu *real_parent
include/linux/ptrace.h:92:60: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct task_struct *p2 @@ got struct task_struct [noderef] __rcu *parent @@
include/linux/ptrace.h:92:60: sparse: expected struct task_struct *p2
include/linux/ptrace.h:92:60: sparse: got struct task_struct [noderef] __rcu *parent
kernel/exit.c: note: in included file (through include/linux/thread_info.h, include/asm-generic/preempt.h, arch/nios2/include/generated/asm/preempt.h, ...):
arch/nios2/include/asm/thread_info.h:62:9: sparse: sparse: context imbalance in 'do_wait' - wrong count at exit

vim +738 kernel/exit.c

735
736 static void synchronize_group_exit(struct task_struct *tsk, long code)
737 {

 > 738		struct sighand_struct *sighand = tsk->sighand;

739 struct signal_struct *signal = tsk->signal;
740
741 spin_lock_irq(&sighand->siglock);
742 signal->quick_threads--;
743 if ((signal->quick_threads == 0) &&
744 !(signal->flags & SIGNAL_GROUP_EXIT)) {
745 signal->flags = SIGNAL_GROUP_EXIT;
746 signal->group_exit_code = code;
747 signal->group_stop_count = 0;
748 }
749 spin_unlock_irq(&sighand->siglock);
750 }
751

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#208 lore raw

PATCH 3/3 signal: Drop signals received after a fatal signal has been processed

In reply to Eric W. Biederman

In 403bad72b67d ("coredump: only SIGKILL should interrupt the coredumping task") Oleg modified the kernel to drop all signals that come in during a coredump except SIGKILL, and suggested that it might be a good idea to generalize that to other cases after the process has received a fatal signal.

Semantically it does not make sense to perform any signal delivery after the process has already been killed.

When a signal is sent while a process is dying today the signal is placed in the signal queue by __send_signal and a single task of the process is woken up with signal_wake_up, if there are any tasks that have not set PF_EXITING.

Take things one step farther and have prepare_signal report that all signals that come after a process has been killed should be ignored. While retaining the historical exception of allowing SIGKILL to interrupt coredumps.

Update the comment in fs/coredump.c to make it clear coredumps are special in being able to receive SIGKILL.

This changes things so that a process stopped in PTRACE_EVENT_EXIT can not be made to escape it's ptracer and finish exiting by sending it SIGKILL. That a process can be made to leave PTRACE_EVENT_EXIT and escape it's tracer by sending the process a SIGKILL has been complicating tracer's for no apparent advantage. If the process needs to be made to leave PTRACE_EVENT_EXIT all that needs to happen is to kill the proceses's tracer. This differs from the coredump code where there is no other mechanism besides honoring SIGKILL to expedite the end of coredumping.

---
 fs/coredump.c   | 2 +-
 kernel/signal.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
fs/coredump.c +1 -1
index ebc43f960b64..b836948c9543 100644
@@ -354,7 +354,7 @@ static int zap_process(struct task_struct *start, int exit_code)
 	struct task_struct *t;
 	int nr = 0;
 
-	/* ignore all signals except SIGKILL, see prepare_signal() */
+	/* Allow SIGKILL, see prepare_signal() */
 	start->signal->flags = SIGNAL_GROUP_EXIT;
 	start->signal->group_exit_code = exit_code;
 	start->signal->group_stop_count = 0;
kernel/signal.c +2 -1
index edb1dc9b00dc..369d65b06025 100644
@@ -913,8 +913,9 @@ static bool prepare_signal(int sig, struct task_struct *p, bool force)
 		if (signal->core_state)
 			return sig == SIGKILL;
 		/*
-		 * The process is in the middle of dying, nothing to do.
+		 * The process is in the middle of dying, drop the signal.
 		 */
+		return false;
 	} else if (sig_kernel_stop(sig)) {
 		/*
 		 * This is a stop signal.  Remove SIGCONT from all queues.
Signature · 1 line
2.35.3
#209 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

In reply to Eric W. Biederman
Quoted 9 lines · Eric W. Biederman

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

Keeping SIGKILL working for anything after the process has been killed is also a real pain from an implementation point of view.

So I am attempting to remove this wart in the userspace API and see if anyone cares.

Hi Eric,

With this series s390 hits the warning exactly same way. Is that expected?

Thanks!

#210 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

Quoted 8 lines · Alexander Gordeev
Quoted 7 lines · Eric W. Biederman

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

Keeping SIGKILL working for anything after the process has been killed is also a real pain from an implementation point of view.

Quoted 6 lines
Quoted 2 lines

So I am attempting to remove this wart in the userspace API and see if anyone cares.

Hi Eric,

With this series s390 hits the warning exactly same way. Is that expected?

Yes. I was working on this before I got your mysterious bug report. I included you because I am including everyone I know who deals with the userspace side of this since I am very deliberately changing the user visible behavior of PTRACE_EVENT_EXIT.

I am going to start seeing if I can find any possible explanation for your regression report. Since I don't have much to go on I expect I will have to revert the last change in my ptrace_stop series that apparently triggers the WARN_ON you reported. I really would have expected the WARN_ON to be triggered in the patch in which it was introduced, not the final patch in the series.

To the best of my knowledge changing PTRACE_EVENT_EXIT is both desirable from a userspace semantics standpoint and from a kernel implementation standpoint. If someone knows any differently and depends upon sending SIGKILL to processes in PTRACE_EVENT_EXIT to steal the process away from the tracer I would love to hear about that case.

Eric

#211 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

In reply to Eric W. Biederman
Quoted 15 lines · Eric W. Biederman

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

Keeping SIGKILL working for anything after the process has been killed is also a real pain from an implementation point of view.

So I am attempting to remove this wart in the userspace API and see if anyone cares.

Eric W. Biederman (3):
signal: Ensure SIGNAL_GROUP_EXIT gets set in do_group_exit
signal: Guarantee that SIGNAL_GROUP_EXIT is set on process exit
signal: Drop signals received after a fatal signal has been processed

Quoted 6 lines
 fs/coredump.c                |  2 +-
 include/linux/sched/signal.h |  1 +
 kernel/exit.c                | 20 +++++++++++++++++++-
 kernel/fork.c                |  2 ++
 kernel/signal.c              |  3 ++-
 5 files changed, 25 insertions(+), 3 deletions(-)

RR folks any comments?

Did I properly understand what Keno Fischer was asking for when we talked in person?

Eric

#212 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

Hi Eric,

Quoted 9 lines · Eric W. Biederman
Quoted 4 lines

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

RR folks any comments?

Did I properly understand what Keno Fischer was asking for when we talked in person?

Yes, this is indeed what I had in mind. I have not yet had the opportunity to try out your patch series (sorry), but from visual inspection, it does indeed do what I wanted, which is to make sure that a tracee stays in PTRACE_EVENT_EXIT for the tracer to inspect, even if there is another SIGKILL incoming simultaneously (since otherwise it may be impossible for the tracer to observe the PTRACE_EVENT_EXIT if two SIGKILLs come in rapid succession). I will try to take this series for a proper spin shortly.

Keno

#213 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

Quoted 13 lines · Keno Fischer

Hi Eric,

Quoted 9 lines · Eric W. Biederman
Quoted 4 lines

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

RR folks any comments?

Did I properly understand what Keno Fischer was asking for when we talked in person?

Quoted 8 lines

Yes, this is indeed what I had in mind. I have not yet had the opportunity to try out your patch series (sorry), but from visual inspection, it does indeed do what I wanted, which is to make sure that a tracee stays in PTRACE_EVENT_EXIT for the tracer to inspect, even if there is another SIGKILL incoming simultaneously (since otherwise it may be impossible for the tracer to observe the PTRACE_EVENT_EXIT if two SIGKILLs come in rapid succession). I will try to take this series for a proper spin shortly.

Thanks,

I haven't yet figured out how to get the rr test suite to run successfully. Something about my test machine and lack of perf counters seems to be causing problems. So if you can perform the testing on your side that would be fantastic.

Eric

#214 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

Quoted 24 lines · Eric W. Biederman
Quoted 13 lines · Keno Fischer

Hi Eric,

Quoted 9 lines · Eric W. Biederman
Quoted 4 lines

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

RR folks any comments?

Did I properly understand what Keno Fischer was asking for when we talked in person?

Quoted 8 lines

Yes, this is indeed what I had in mind. I have not yet had the opportunity to try out your patch series (sorry), but from visual inspection, it does indeed do what I wanted, which is to make sure that a tracee stays in PTRACE_EVENT_EXIT for the tracer to inspect, even if there is another SIGKILL incoming simultaneously (since otherwise it may be impossible for the tracer to observe the PTRACE_EVENT_EXIT if two SIGKILLs come in rapid succession). I will try to take this series for a proper spin shortly.

Quoted 6 lines

Thanks,

I haven't yet figured out how to get the rr test suite to run successfully. Something about my test machine and lack of perf counters seems to be causing problems. So if you can perform the testing on your side that would be fantastic.

Ok. I finally found a machine where I can run rr and the rr test suite.

It looks like there are a couple of the rr 5.5.0 test that fail on Linus's lastest kernel simply because of changes in kernel behavior. In particular clone_cleartid_coredump, and fcntl_rw_hints. The clone_cleartid_coredump appears to fail because SIGSEGV no longer kills all processes that share an mm. Which was a deliberate change.

With the lastest development version of rr, only detach_sigkill appears to be failing on Linus's latest. That failure appears to be independent of the patches in question as well. When run manually the detach_sigkill test succeeds so I am not quite certain what is going on, any thoughts?

As for my patchset it looks like it does not cause any new test failures for rr so I will plan on getting it into linux-next shortly.

Eric

#215 lore raw

Re: [PATCH 0/3] ptrace: Stop supporting SIGKILL for PTRACE_EVENT_EXIT

Quoted 34 lines · Eric W. Biederman
Quoted 24 lines · Eric W. Biederman
Quoted 13 lines · Keno Fischer

Hi Eric,

Quoted 9 lines · Eric W. Biederman
Quoted 4 lines

Recently I had a conversation where it was pointed out to me that SIGKILL sent to a tracee stropped in PTRACE_EVENT_EXIT is quite difficult for a tracer to handle.

RR folks any comments?

Did I properly understand what Keno Fischer was asking for when we talked in person?

Quoted 8 lines

Yes, this is indeed what I had in mind. I have not yet had the opportunity to try out your patch series (sorry), but from visual inspection, it does indeed do what I wanted, which is to make sure that a tracee stays in PTRACE_EVENT_EXIT for the tracer to inspect, even if there is another SIGKILL incoming simultaneously (since otherwise it may be impossible for the tracer to observe the PTRACE_EVENT_EXIT if two SIGKILLs come in rapid succession). I will try to take this series for a proper spin shortly.

Quoted 6 lines

Thanks,

I haven't yet figured out how to get the rr test suite to run successfully. Something about my test machine and lack of perf counters seems to be causing problems. So if you can perform the testing on your side that would be fantastic.

Quoted 7 lines

Ok. I finally found a machine where I can run rr and the rr test suite.

It looks like there are a couple of the rr 5.5.0 test that fail on Linus's lastest kernel simply because of changes in kernel behavior. In particular clone_cleartid_coredump, and fcntl_rw_hints. The clone_cleartid_coredump appears to fail because SIGSEGV no longer kills all processes that share an mm. Which was a deliberate change.

Yeah, we changed to handle this in
https://github.com/rr-debugger/rr/commit/04bbacdbaba1cc496e92060014442bd1fd26b41d
and https://github.com/rr-debugger/rr/commit/1a3b389c2956e1844c0d07bf4297398bb6c561ea.

Quoted 5 lines

With the lastest development version of rr, only detach_sigkill appears to be failing on Linus's latest. That failure appears to be independent of the patches in question as well. When run manually the detach_sigkill test succeeds so I am not quite certain what is going on, any thoughts?

If it fails before your changes I wouldn't worry about it too much, there's been some other failures in that test lately.

- Kyle

Quoted 5 lines

As for my patchset it looks like it does not cause any new test failures for rr so I will plan on getting it into linux-next shortly.

Eric

maintainer
#216 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

In reply to Peter Zijlstra

kernel/signal.c static void do_notify_parent_cldstop(str · line 2197

Quoted 5 lines · Peter Zijlstra
kernel/signal.c +4 -0
+static void clear_traced_quiesce(void)
+{
+	spin_lock_irq(&current->sighand->siglock);
+	WARN_ON_ONCE(!(current->jobctl & JOBCTL_TRACED_QUIESCE));

This WARN_ON_ONCE() doesn't look right, the task can be killed right after ptrace_stop() sets JOBCTL_TRACED | JOBCTL_TRACED_QUIESCE and drops siglock.

Quoted 13 lines
kernel/signal.c +3 -3
@@ -2290,14 +2303,26 @@ static int ptrace_stop(int exit_code, in
 		/*
 		 * Don't want to allow preemption here, because
 		 * sys_ptrace() needs this task to be inactive.
-		 *
-		 * XXX: implement read_unlock_no_resched().
 		 */
 		preempt_disable();
 		read_unlock(&tasklist_lock);
-		cgroup_enter_frozen();
+		cgroup_enter_frozen(); // XXX broken on PREEMPT_RT !!!
+
+		/*

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2321

Quoted 10 lines
kernel/signal.c +10 -0
+		 * JOBCTL_TRACE_QUIESCE bridges the gap between
+		 * set_current_state(TASK_TRACED) above and schedule() below.
+		 * There must not be any blocking (specifically anything that
+		 * touched ->saved_state on PREEMPT_RT) between here and
+		 * schedule().
+		 *
+		 * ptrace_check_attach() relies on this with its
+		 * wait_task_inactive() usage.
+		 */
+		clear_traced_quiesce();

Well, I think it should be called earlier under tasklist_lock, before preempt_disable() above.

We need tasklist_lock to protect ->parent, debugger can be killed and go away right after read_unlock(&tasklist_lock).

Still trying to convince myself everything is right with JOBCTL_STOPPED/TRACED ...

Oleg.

author maintainer
#217 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 10 lines · Oleg Nesterov

kernel/signal.c static void do_notify_parent_cldstop(str · line 2197

Quoted 5 lines · Peter Zijlstra
kernel/signal.c +4 -0
+static void clear_traced_quiesce(void)
+{
+	spin_lock_irq(&current->sighand->siglock);
+	WARN_ON_ONCE(!(current->jobctl & JOBCTL_TRACED_QUIESCE));

This WARN_ON_ONCE() doesn't look right, the task can be killed right after ptrace_stop() sets JOBCTL_TRACED | JOBCTL_TRACED_QUIESCE and drops siglock.

OK, will look at that.

Quoted 27 lines
Quoted 13 lines
kernel/signal.c +3 -3
@@ -2290,14 +2303,26 @@ static int ptrace_stop(int exit_code, in
 		/*
 		 * Don't want to allow preemption here, because
 		 * sys_ptrace() needs this task to be inactive.
-		 *
-		 * XXX: implement read_unlock_no_resched().
 		 */
 		preempt_disable();
 		read_unlock(&tasklist_lock);
-		cgroup_enter_frozen();
+		cgroup_enter_frozen(); // XXX broken on PREEMPT_RT !!!
+
+		/*

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2321

Quoted 10 lines
kernel/signal.c +10 -0
+		 * JOBCTL_TRACE_QUIESCE bridges the gap between
+		 * set_current_state(TASK_TRACED) above and schedule() below.
+		 * There must not be any blocking (specifically anything that
+		 * touched ->saved_state on PREEMPT_RT) between here and
+		 * schedule().
+		 *
+		 * ptrace_check_attach() relies on this with its
+		 * wait_task_inactive() usage.
+		 */
+		clear_traced_quiesce();

Well, I think it should be called earlier under tasklist_lock, before preempt_disable() above.

Quoted 5 lines

We need tasklist_lock to protect ->parent, debugger can be killed and go away right after read_unlock(&tasklist_lock).

Still trying to convince myself everything is right with JOBCTL_STOPPED/TRACED ...

Can't do it earlier, since cgroup_enter_frozen() can do spinlock (eg. use ->saved_state).

#218 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 41 lines · Peter Zijlstra
Quoted 10 lines · Oleg Nesterov

kernel/signal.c static void do_notify_parent_cldstop(str · line 2197

Quoted 5 lines · Peter Zijlstra
kernel/signal.c +4 -0
+static void clear_traced_quiesce(void)
+{
+	spin_lock_irq(&current->sighand->siglock);
+	WARN_ON_ONCE(!(current->jobctl & JOBCTL_TRACED_QUIESCE));

This WARN_ON_ONCE() doesn't look right, the task can be killed right after ptrace_stop() sets JOBCTL_TRACED | JOBCTL_TRACED_QUIESCE and drops siglock.

OK, will look at that.

Quoted 27 lines
Quoted 13 lines
kernel/signal.c +3 -3
@@ -2290,14 +2303,26 @@ static int ptrace_stop(int exit_code, in
 		/*
 		 * Don't want to allow preemption here, because
 		 * sys_ptrace() needs this task to be inactive.
-		 *
-		 * XXX: implement read_unlock_no_resched().
 		 */
 		preempt_disable();
 		read_unlock(&tasklist_lock);
-		cgroup_enter_frozen();
+		cgroup_enter_frozen(); // XXX broken on PREEMPT_RT !!!
+
+		/*

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2321

Quoted 10 lines
kernel/signal.c +10 -0
+		 * JOBCTL_TRACE_QUIESCE bridges the gap between
+		 * set_current_state(TASK_TRACED) above and schedule() below.
+		 * There must not be any blocking (specifically anything that
+		 * touched ->saved_state on PREEMPT_RT) between here and
+		 * schedule().
+		 *
+		 * ptrace_check_attach() relies on this with its
+		 * wait_task_inactive() usage.
+		 */
+		clear_traced_quiesce();

Well, I think it should be called earlier under tasklist_lock, before preempt_disable() above.

Quoted 8 lines
Quoted 5 lines

We need tasklist_lock to protect ->parent, debugger can be killed and go away right after read_unlock(&tasklist_lock).

Still trying to convince myself everything is right with JOBCTL_STOPPED/TRACED ...

Can't do it earlier, since cgroup_enter_frozen() can do spinlock (eg. use ->saved_state).

There are some other issues in this part of ptrace_stop().

I don't see JOBCTL_TRACED_QUIESCE being cleared "if (!current->ptrace)".

Currently in ptrace_check_attach a parameter of __TASK_TRACED is passed so that wait_task_inactive cane fail if the "!current->ptrace" branch of ptrace_stop is take and ptrace_stop does not stop. With the TASK_FROZEN state it appears that "!current->ptrace" branch can continue and freeze somewhere else and wait_task_inactive could decided it was fine.

I have to run, but hopefully tommorrow I will post the patches that remove the "!current->ptrace" case altogether and basically remove the need for quiesce and wait_task_inactive detecting which branch is taken.

The spinlock in cgroup_enter_frozen remains an issue for PREEMPT_RT. But the rest of the issues are cleared up by using siglock instead of tasklist_lock. Plus the code is just easier to read and understand.

Eric

maintainer
#219 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 2 lines · Eric W. Biederman

I don't see JOBCTL_TRACED_QUIESCE being cleared "if (!current->ptrace)".

As Peter explained, in this case we can rely on __ptrace_unlink() which should clear this flag.

Oleg.

#220 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 6 lines · Oleg Nesterov
Quoted 2 lines · Eric W. Biederman

I don't see JOBCTL_TRACED_QUIESCE being cleared "if (!current->ptrace)".

As Peter explained, in this case we can rely on __ptrace_unlink() which should clear this flag.

I had missed that that signal_wake_up_state was clearing JOBCTL_TRACED_QUIESCE.

Relying on __ptrace_unlink assumes the __ptrace_unlink happens after siglock is taken before calling ptrace_stop. Especially with the ptrace_notify in signal_delivered that does not look guaranteed.

The __ptrace_unlink could also happen during arch_ptrace_stop.

Relying on siglock is sufficient because __ptrace_unlink holds siglock over clearing task->ptrace. Which means that the simple fix for this is to just test task->ptrace before we set JOBCTL_TRACED_QUEIESCE.

Eric

maintainer
#221 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 10 lines · Eric W. Biederman

Relying on __ptrace_unlink assumes the __ptrace_unlink happens after siglock is taken before calling ptrace_stop. Especially with the ptrace_notify in signal_delivered that does not look guaranteed.

The __ptrace_unlink could also happen during arch_ptrace_stop.

Relying on siglock is sufficient because __ptrace_unlink holds siglock over clearing task->ptrace. Which means that the simple fix for this is to just test task->ptrace before we set JOBCTL_TRACED_QUEIESCE.

Or simply clear _QUEIESCE along with _TRACED/DELAY_WAKEKILL before return?

Oleg.

maintainer
#222 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

In reply to Peter Zijlstra

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2242

Quoted 8 lines · Peter Zijlstra
kernel/signal.c +1 -1
@@ -2225,7 +2238,7 @@ static int ptrace_stop(int exit_code, in
 	 * schedule() will not sleep if there is a pending signal that
 	 * can awaken the task.
 	 */
-	current->jobctl |= JOBCTL_TRACED;
+	current->jobctl |= JOBCTL_TRACED | JOBCTL_TRACED_QUIESCE;
 	set_special_state(TASK_TRACED);

OK, this looks wrong. I actually mean the previous patch which sets JOBCTL_TRACED.

The problem is that the tracee can be already killed, so that fatal_signal_pending(current) is true. In this case we can't rely on signal_wake_up_state() which should clear JOBCTL_TRACED, or the callers of ptrace_signal_wake_up/etc which clear this flag by hand.

In this case schedule() won't block and ptrace_stop() will leak JOBCTL_TRACED. Unless I missed something.

We could check fatal_signal_pending() and damn! this is what I think ptrace_stop() should have done from the very beginning. But for now I'd suggest to simply clear this flag before return, along with DELAY_WAKEKILL and LISTENING.

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2363

Quoted 2 lines
kernel/signal.c +1 -0
 	current->jobctl &= ~JOBCTL_LISTENING;
+	current->jobctl &= ~JOBCTL_DELAY_WAKEKILL;
	current->jobctl &=
		~(~JOBCTL_TRACED | JOBCTL_DELAY_WAKEKILL | JOBCTL_LISTENING);

Oleg.

#223 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 18 lines · Oleg Nesterov

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2242

Quoted 8 lines · Peter Zijlstra
kernel/signal.c +1 -1
@@ -2225,7 +2238,7 @@ static int ptrace_stop(int exit_code, in
 	 * schedule() will not sleep if there is a pending signal that
 	 * can awaken the task.
 	 */
-	current->jobctl |= JOBCTL_TRACED;
+	current->jobctl |= JOBCTL_TRACED | JOBCTL_TRACED_QUIESCE;
 	set_special_state(TASK_TRACED);

OK, this looks wrong. I actually mean the previous patch which sets JOBCTL_TRACED.

The problem is that the tracee can be already killed, so that fatal_signal_pending(current) is true. In this case we can't rely on signal_wake_up_state() which should clear JOBCTL_TRACED, or the callers of ptrace_signal_wake_up/etc which clear this flag by hand.

Quoted 7 lines

In this case schedule() won't block and ptrace_stop() will leak JOBCTL_TRACED. Unless I missed something.

We could check fatal_signal_pending() and damn! this is what I think ptrace_stop() should have done from the very beginning. But for now I'd suggest to simply clear this flag before return, along with DELAY_WAKEKILL and LISTENING.

Oh. That is an interesting case for JOBCTL_TRACED. The scheduler refuses to stop if signal_pending_state(TASK_TRACED, p) returns true.

The ptrace_stop code used to handle this explicitly and in commit 7d613f9f72ec ("signal: Remove the bogus sigkill_pending in ptrace_stop") I actually removed the test. As the test was somewhat wrong and redundant, and in slightly the wrong location.

But doing:

	/* Don't stop if the task is dying */
	if (unlikely(__fatal_signal_pending(current)))
		return exit_code;

Should work.

Quoted 6 lines

kernel/signal.c static int ptrace_stop(int exit_code, in · line 2363

Quoted 3 lines
kernel/signal.c +1 -0
 	current->jobctl &= ~JOBCTL_LISTENING;
+	current->jobctl &= ~JOBCTL_DELAY_WAKEKILL;
	current->jobctl &=
		~(~JOBCTL_TRACED | JOBCTL_DELAY_WAKEKILL | JOBCTL_LISTENING);

I presume you meant:

	current->jobctl &=

~(JOBCTL_TRACED | JOBCTL_DELAY_WAKEKILL | JOBCTL_LISTENING);

I don't think we want to do that. For the case you are worried about it is a valid fix.

In general this is the wrong approach as we want the waker to clear JOBCTL_TRACED. If the waker does not it is possible that ptrace_freeze_traced might attempt to freeze a process whose state is not appropriate for attach, because the code is past the call to schedule().

In fact I think clearing JOBCTL_TRACED at the end of ptrace_stop will allow ptrace_freeze_traced to come in while siglock is dropped, expect the process to stop, and have the process not stop. Of course wait_task_inactive coming first that might not be a problem.

This is a minor problem with the patchset I just posted. I thought the only reason wait_task_inactive could fail was if ptrace_stop() hit the !current->ptrace case. Thinking about any it any SIGKILL coming in before tracee stops in schedule will trigger this, so it is not as safe as I thought to not pass a state into wait_task_inactive.

It is time for me to shut down today. I will sleep on that and see what I can see tomorrow.

Eric

author maintainer
#224 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 7 lines · Eric W. Biederman

But doing:

	/* Don't stop if the task is dying */
	if (unlikely(__fatal_signal_pending(current)))
		return exit_code;

Should work.

Something like so then...

---
Subject: signal,ptrace: Don't stop dying tasks
From: Peter Zijlstra <[email protected]>
Date: Thu Apr 28 22:17:56 CEST 2022

Oleg pointed out that the tracee can already be killed such that fatal_signal_pending() is true. In that case signal_wake_up_state() cannot be relied upon to be responsible for the wakeup -- something we're going to want to rely on.

As such, explicitly handle this case.

---
 kernel/signal.c |    4 ++++
 1 file changed, 4 insertions(+)
kernel/signal.c +4 -0
@@ -2226,6 +2226,10 @@ static int ptrace_stop(int exit_code, in
 		spin_lock_irq(&current->sighand->siglock);
 	}
 
+	/* Don't stop if the task is dying. */
+	if (unlikely(__fatal_signal_pending(current)))
+		return exit_code;
+
 	/*
 	 * schedule() will not sleep if there is a pending signal that
 	 * can awaken the task.
maintainer
#225 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 5 lines · Peter Zijlstra

Oleg pointed out that the tracee can already be killed such that fatal_signal_pending() is true. In that case signal_wake_up_state() cannot be relied upon to be responsible for the wakeup -- something we're going to want to rely on.

Peter, I am all confused...

If this patch is against the current tree, we don't need it.

If it is on top of JOBCTL_TRACED/DELAY_WAKEKILL changes (yours or Eric's), then it can't help - SIGKILL can come right after the tracee drops siglock and calls schedule().

Perhaps I missed something, but let me repeat the 3rd time: I'd suggest to simply clear JOBCTL_TRACED along with LISTENING/DELAY_WAKEKILL before return to close this race.

Oleg.

Quoted 14 lines
kernel/signal.c +4 -0
@@ -2226,6 +2226,10 @@ static int ptrace_stop(int exit_code, in
 		spin_lock_irq(&current->sighand->siglock);
 	}
 
+	/* Don't stop if the task is dying. */
+	if (unlikely(__fatal_signal_pending(current)))
+		return exit_code;
+
 	/*
 	 * schedule() will not sleep if there is a pending signal that
 	 * can awaken the task.
author maintainer
#226 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 7 lines · Oleg Nesterov
Quoted 5 lines · Peter Zijlstra

Oleg pointed out that the tracee can already be killed such that fatal_signal_pending() is true. In that case signal_wake_up_state() cannot be relied upon to be responsible for the wakeup -- something we're going to want to rely on.

Quoted 7 lines

Peter, I am all confused...

If this patch is against the current tree, we don't need it.

If it is on top of JOBCTL_TRACED/DELAY_WAKEKILL changes (yours or Eric's), then it can't help - SIGKILL can come right after the tracee drops siglock and calls schedule().

But by that time it will already have set TRACED and signal_wake_up() wil clear it, no?

Quoted 3 lines

Perhaps I missed something, but let me repeat the 3rd time: I'd suggest to simply clear JOBCTL_TRACED along with LISTENING/DELAY_WAKEKILL before return to close this race.

I think Eric convinced me there was a problem with that, but I'll go over it all again in the morning, perhaps I'll reach a different conclusion :-)

maintainer
#227 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Peter, you know, it is very difficult to me to discuss the changes in the 2 unfinished series and not loose the context ;) Plus I am already sleeping. But I'll try to reply anyway.

Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Oleg Nesterov

If it is on top of JOBCTL_TRACED/DELAY_WAKEKILL changes (yours or Eric's), then it can't help - SIGKILL can come right after the tracee drops siglock and calls schedule().

But by that time it will already have set TRACED and signal_wake_up() wil clear it, no?

No. JOBCTL_DELAY_WAKEKILL is already set, this means that signal_wake_up() will remove TASK_WAKEKILL from the "state" passed to signal_wake_up_state() and this is fine and correct, this mean thats ttwu() won't change ->__state.

But this also mean that wake_up_state() will return false, and in this case

	signal_wake_up_state:

		if (wake_up_state(t, state | TASK_INTERRUPTIBLE))
			t->jobctl &= ~(JOBCTL_STOPPED | JOBCTL_TRACED | JOBCTL_TRACED_QUIESCE);

won't clear these flags. And this is nice too.

But. fatal_signal_pending() is true! And once we change freeze_traced() to not abuse p->__state, schedule() won't block because it will check signal_pending_state(TASK_TRACED == TASK_WAKEKILL | __TASK_TRACED) and __fatal_signal_pending() == T.

In this case ptrace_stop() will leak JOBCTL_TRACED, so we simply need to clear it before return along with LISTENING | DELAY_WAKEKILL.

Quoted 3 lines

I'll go
over it all again in the morning, perhaps I'll reach a different conclusion :-)

Same here ;)

Oleg.

maintainer
#228 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

In reply to Peter Zijlstra

kernel/ptrace.c SYSCALL_DEFINE4(ptrace, long, request, l · line 1340

Quoted 8 lines · Peter Zijlstra
kernel/ptrace.c +1 -2
@@ -1329,8 +1337,7 @@ SYSCALL_DEFINE4(ptrace, long, request, l
 		goto out_put_task_struct;
 
 	ret = arch_ptrace(child, request, addr, data);
-	if (ret || request != PTRACE_DETACH)
-		ptrace_unfreeze_traced(child);
+	ptrace_unfreeze_traced(child);

Forgot to mention... whatever we do this doesn't look right.

ptrace_unfreeze_traced() must not be called if the tracee was untraced, anothet debugger can come after that. I agree, the current code looks a bit confusing, perhaps it makes sense to re-write it:

	if (request == PTRACE_DETACH && ret == 0)
		; /* nothing to do, no longer traced by us */
	else
		ptrace_unfreeze_traced(child);

Oleg.

#229 lore raw

Re: [PATCH v2 2/5] sched,ptrace: Fix ptrace_check_attach() vs PREEMPT_RT

Quoted 12 lines · Oleg Nesterov

kernel/ptrace.c SYSCALL_DEFINE4(ptrace, long, request, l · line 1340

Quoted 8 lines · Peter Zijlstra
kernel/ptrace.c +1 -2
@@ -1329,8 +1337,7 @@ SYSCALL_DEFINE4(ptrace, long, request, l
 		goto out_put_task_struct;
 
 	ret = arch_ptrace(child, request, addr, data);
-	if (ret || request != PTRACE_DETACH)
-		ptrace_unfreeze_traced(child);
+	ptrace_unfreeze_traced(child);

Forgot to mention... whatever we do this doesn't look right.

Quoted 8 lines

ptrace_unfreeze_traced() must not be called if the tracee was untraced, anothet debugger can come after that. I agree, the current code looks a bit confusing, perhaps it makes sense to re-write it:

	if (request == PTRACE_DETACH && ret == 0)
		; /* nothing to do, no longer traced by us */
	else
		ptrace_unfreeze_traced(child);

This was a bug in my original JOBCTL_DELAY_WAITKILL patch and it was just cut and pasted here. I thought it made sense when I was throwing things together but when I looked more closely I realized that it is not safe.

Eric

author maintainer
#230 lore raw

PATCH v2 3/5 freezer: Have {,un}lock_system_sleep() save/restore flags

In reply to Peter Zijlstra

Rafael explained that the reason for having both PF_NOFREEZE and PF_FREEZER_SKIP is that {,un}lock_system_sleep() is callable from kthread context that has previously called set_freezable().

In preparation of merging the flags, have {,un}lock_system_slee() save and restore current->flags.

---
 drivers/acpi/x86/s2idle.c         |   12 ++++++++----
 drivers/scsi/scsi_transport_spi.c |    7 ++++---
 include/linux/suspend.h           |    8 ++++----
 kernel/power/hibernate.c          |   35 ++++++++++++++++++++++-------------
 kernel/power/main.c               |   16 ++++++++++------
 kernel/power/suspend.c            |   12 ++++++++----
 kernel/power/user.c               |   24 ++++++++++++++----------
 7 files changed, 70 insertions(+), 44 deletions(-)
drivers/acpi/x86/s2idle.c +8 -4
@@ -538,12 +538,14 @@ void acpi_s2idle_setup(void)
 
 int acpi_register_lps0_dev(struct acpi_s2idle_dev_ops *arg)
 {
+	unsigned int sleep_flags;
+
 	if (!lps0_device_handle || sleep_no_lps0)
 		return -ENODEV;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 	list_add(&arg->list_node, &lps0_s2idle_devops_head);
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return 0;
 }
@@ -551,12 +553,14 @@ EXPORT_SYMBOL_GPL(acpi_register_lps0_dev
 
 void acpi_unregister_lps0_dev(struct acpi_s2idle_dev_ops *arg)
 {
+	unsigned int sleep_flags;
+
 	if (!lps0_device_handle || sleep_no_lps0)
 		return;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 	list_del(&arg->list_node);
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 }
 EXPORT_SYMBOL_GPL(acpi_unregister_lps0_dev);
 
drivers/scsi/scsi_transport_spi.c +4 -3
@@ -998,8 +998,9 @@ void
 spi_dv_device(struct scsi_device *sdev)
 {
 	struct scsi_target *starget = sdev->sdev_target;
-	u8 *buffer;
 	const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
+	unsigned int sleep_flags;
+	u8 *buffer;
 
 	/*
 	 * Because this function and the power management code both call
@@ -1007,7 +1008,7 @@ spi_dv_device(struct scsi_device *sdev)
 	 * while suspend or resume is in progress. Hence the
 	 * lock/unlock_system_sleep() calls.
 	 */
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	if (scsi_autopm_get_device(sdev))
 		goto unlock_system_sleep;
@@ -1058,7 +1059,7 @@ spi_dv_device(struct scsi_device *sdev)
 	scsi_autopm_put_device(sdev);
 
 unlock_system_sleep:
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 }
 EXPORT_SYMBOL(spi_dv_device);
 
include/linux/suspend.h +4 -4
@@ -510,8 +510,8 @@ extern bool pm_save_wakeup_count(unsigne
 extern void pm_wakep_autosleep_enabled(bool set);
 extern void pm_print_active_wakeup_sources(void);
 
-extern void lock_system_sleep(void);
-extern void unlock_system_sleep(void);
+extern unsigned int lock_system_sleep(void);
+extern void unlock_system_sleep(unsigned int);
 
 #else /* !CONFIG_PM_SLEEP */
 
@@ -534,8 +534,8 @@ static inline void pm_system_wakeup(void
 static inline void pm_wakeup_clear(bool reset) {}
 static inline void pm_system_irq_wakeup(unsigned int irq_number) {}
 
-static inline void lock_system_sleep(void) {}
-static inline void unlock_system_sleep(void) {}
+static inline unsigned int lock_system_sleep(void) { return 0; }
+static inline void unlock_system_sleep(unsigned int flags) {}
 
 #endif /* !CONFIG_PM_SLEEP */
 
kernel/power/hibernate.c +22 -13
@@ -92,20 +92,24 @@ bool hibernation_available(void)
  */
 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
 {
+	unsigned int sleep_flags;
+
 	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
 	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore
 	    && ops->restore_cleanup && ops->leave)) {
 		WARN_ON(1);
 		return;
 	}
-	lock_system_sleep();
+
+	sleep_flags = lock_system_sleep();
+
 	hibernation_ops = ops;
 	if (ops)
 		hibernation_mode = HIBERNATION_PLATFORM;
 	else if (hibernation_mode == HIBERNATION_PLATFORM)
 		hibernation_mode = HIBERNATION_SHUTDOWN;
 
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 }
 EXPORT_SYMBOL_GPL(hibernation_set_ops);
 
@@ -713,6 +717,7 @@ static int load_image_and_restore(void)
 int hibernate(void)
 {
 	bool snapshot_test = false;
+	unsigned int sleep_flags;
 	int error;
 
 	if (!hibernation_available()) {
@@ -720,7 +725,7 @@ int hibernate(void)
 		return -EPERM;
 	}
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 	/* The snapshot device should not be opened while we're running */
 	if (!hibernate_acquire()) {
 		error = -EBUSY;
@@ -794,7 +799,7 @@ int hibernate(void)
 	pm_restore_console();
 	hibernate_release();
  Unlock:
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 	pr_info("hibernation exit\n");
 
 	return error;
@@ -809,9 +814,10 @@ int hibernate(void)
  */
 int hibernate_quiet_exec(int (*func)(void *data), void *data)
 {
+	unsigned int sleep_flags;
 	int error;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	if (!hibernate_acquire()) {
 		error = -EBUSY;
@@ -891,7 +897,7 @@ int hibernate_quiet_exec(int (*func)(voi
 	hibernate_release();
 
 unlock:
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return error;
 }
@@ -1100,11 +1106,12 @@ static ssize_t disk_show(struct kobject
 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
 			  const char *buf, size_t n)
 {
+	int mode = HIBERNATION_INVALID;
+	unsigned int sleep_flags;
 	int error = 0;
-	int i;
 	int len;
 	char *p;
-	int mode = HIBERNATION_INVALID;
+	int i;
 
 	if (!hibernation_available())
 		return -EPERM;
@@ -1112,7 +1119,7 @@ static ssize_t disk_store(struct kobject
 	p = memchr(buf, '\n', n);
 	len = p ? p - buf : n;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
 		if (len == strlen(hibernation_modes[i])
 		    && !strncmp(buf, hibernation_modes[i], len)) {
@@ -1142,7 +1149,7 @@ static ssize_t disk_store(struct kobject
 	if (!error)
 		pm_pr_dbg("Hibernation mode set to '%s'\n",
 			       hibernation_modes[mode]);
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 	return error ? error : n;
 }
 
@@ -1158,9 +1165,10 @@ static ssize_t resume_show(struct kobjec
 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
 			    const char *buf, size_t n)
 {
-	dev_t res;
+	unsigned int sleep_flags;
 	int len = n;
 	char *name;
+	dev_t res;
 
 	if (len && buf[len-1] == '\n')
 		len--;
@@ -1173,9 +1181,10 @@ static ssize_t resume_store(struct kobje
 	if (!res)
 		return -EINVAL;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 	swsusp_resume_device = res;
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
+
 	pm_pr_dbg("Configured hibernation resume from disk to %u\n",
 		  swsusp_resume_device);
 	noresume = 0;
kernel/power/main.c +10 -6
@@ -21,14 +21,16 @@
 
 #ifdef CONFIG_PM_SLEEP
 
-void lock_system_sleep(void)
+unsigned int lock_system_sleep(void)
 {
+	unsigned int flags = current->flags;
 	current->flags |= PF_FREEZER_SKIP;
 	mutex_lock(&system_transition_mutex);
+	return flags;
 }
 EXPORT_SYMBOL_GPL(lock_system_sleep);
 
-void unlock_system_sleep(void)
+void unlock_system_sleep(unsigned int flags)
 {
 	/*
 	 * Don't use freezer_count() because we don't want the call to
@@ -46,7 +48,8 @@ void unlock_system_sleep(void)
 	 * Which means, if we use try_to_freeze() here, it would make them
 	 * enter the refrigerator, thus causing hibernation to lockup.
 	 */
-	current->flags &= ~PF_FREEZER_SKIP;
+	if (!(flags & PF_FREEZER_SKIP))
+		current->flags &= ~PF_FREEZER_SKIP;
 	mutex_unlock(&system_transition_mutex);
 }
 EXPORT_SYMBOL_GPL(unlock_system_sleep);
@@ -260,16 +263,17 @@ static ssize_t pm_test_show(struct kobje
 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
 				const char *buf, size_t n)
 {
+	unsigned int sleep_flags;
 	const char * const *s;
+	int error = -EINVAL;
 	int level;
 	char *p;
 	int len;
-	int error = -EINVAL;
 
 	p = memchr(buf, '\n', n);
 	len = p ? p - buf : n;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	level = TEST_FIRST;
 	for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
@@ -279,7 +283,7 @@ static ssize_t pm_test_store(struct kobj
 			break;
 		}
 
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return error ? error : n;
 }
kernel/power/suspend.c +8 -4
@@ -75,9 +75,11 @@ EXPORT_SYMBOL_GPL(pm_suspend_default_s2i
 
 void s2idle_set_ops(const struct platform_s2idle_ops *ops)
 {
-	lock_system_sleep();
+	unsigned int sleep_flags;
+
+	sleep_flags = lock_system_sleep();
 	s2idle_ops = ops;
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 }
 
 static void s2idle_begin(void)
@@ -200,7 +202,9 @@ __setup("mem_sleep_default=", mem_sleep_
  */
 void suspend_set_ops(const struct platform_suspend_ops *ops)
 {
-	lock_system_sleep();
+	unsigned int sleep_flags;
+
+	sleep_flags = lock_system_sleep();
 
 	suspend_ops = ops;
 
@@ -216,7 +220,7 @@ void suspend_set_ops(const struct platfo
 			mem_sleep_current = PM_SUSPEND_MEM;
 	}
 
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 }
 EXPORT_SYMBOL_GPL(suspend_set_ops);
 
kernel/power/user.c +14 -10
@@ -46,12 +46,13 @@ int is_hibernate_resume_dev(dev_t dev)
 static int snapshot_open(struct inode *inode, struct file *filp)
 {
 	struct snapshot_data *data;
+	unsigned int sleep_flags;
 	int error;
 
 	if (!hibernation_available())
 		return -EPERM;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	if (!hibernate_acquire()) {
 		error = -EBUSY;
@@ -97,7 +98,7 @@ static int snapshot_open(struct inode *i
 	data->dev = 0;
 
  Unlock:
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return error;
 }
@@ -105,8 +106,9 @@ static int snapshot_open(struct inode *i
 static int snapshot_release(struct inode *inode, struct file *filp)
 {
 	struct snapshot_data *data;
+	unsigned int sleep_flags;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	swsusp_free();
 	data = filp->private_data;
@@ -123,7 +125,7 @@ static int snapshot_release(struct inode
 			PM_POST_HIBERNATION : PM_POST_RESTORE);
 	hibernate_release();
 
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return 0;
 }
@@ -131,11 +133,12 @@ static int snapshot_release(struct inode
 static ssize_t snapshot_read(struct file *filp, char __user *buf,
                              size_t count, loff_t *offp)
 {
+	loff_t pg_offp = *offp & ~PAGE_MASK;
 	struct snapshot_data *data;
+	unsigned int sleep_flags;
 	ssize_t res;
-	loff_t pg_offp = *offp & ~PAGE_MASK;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	data = filp->private_data;
 	if (!data->ready) {
@@ -156,7 +159,7 @@ static ssize_t snapshot_read(struct file
 		*offp += res;
 
  Unlock:
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return res;
 }
@@ -164,11 +167,12 @@ static ssize_t snapshot_read(struct file
 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
                               size_t count, loff_t *offp)
 {
+	loff_t pg_offp = *offp & ~PAGE_MASK;
 	struct snapshot_data *data;
+	unsigned int sleep_flags;
 	ssize_t res;
-	loff_t pg_offp = *offp & ~PAGE_MASK;
 
-	lock_system_sleep();
+	sleep_flags = lock_system_sleep();
 
 	data = filp->private_data;
 
@@ -190,7 +194,7 @@ static ssize_t snapshot_write(struct fil
 	if (res > 0)
 		*offp += res;
 unlock:
-	unlock_system_sleep();
+	unlock_system_sleep(sleep_flags);
 
 	return res;
 }
author maintainer
#231 lore raw

PATCH v2 4/5 freezer,umh: Clean up freezer/initrd interaction

In reply to Peter Zijlstra

handle_initrd() marks itself as PF_FREEZER_SKIP in order to ensure that the UMH, which is going to freeze the system, doesn't indefinitely wait for it's caller.

Rework things by adding UMH_FREEZABLE to indicate the completion is freezable.

---
 include/linux/umh.h     |    9 +++++----
 init/do_mounts_initrd.c |   10 +---------
 kernel/umh.c            |    8 ++++++++
 3 files changed, 14 insertions(+), 13 deletions(-)
include/linux/umh.h +5 -4
@@ -11,10 +11,11 @@
 struct cred;
 struct file;
 
-#define UMH_NO_WAIT	0	/* don't wait at all */
-#define UMH_WAIT_EXEC	1	/* wait for the exec, but not the process */
-#define UMH_WAIT_PROC	2	/* wait for the process to complete */
-#define UMH_KILLABLE	4	/* wait for EXEC/PROC killable */
+#define UMH_NO_WAIT	0x00	/* don't wait at all */
+#define UMH_WAIT_EXEC	0x01	/* wait for the exec, but not the process */
+#define UMH_WAIT_PROC	0x02	/* wait for the process to complete */
+#define UMH_KILLABLE	0x04	/* wait for EXEC/PROC killable */
+#define UMH_FREEZABLE	0x08	/* wait for EXEC/PROC freezable */
 
 struct subprocess_info {
 	struct work_struct work;
init/do_mounts_initrd.c +1 -9
@@ -79,19 +79,11 @@ static void __init handle_initrd(void)
 	init_mkdir("/old", 0700);
 	init_chdir("/old");
 
-	/*
-	 * In case that a resume from disk is carried out by linuxrc or one of
-	 * its children, we need to tell the freezer not to wait for us.
-	 */
-	current->flags |= PF_FREEZER_SKIP;
-
 	info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
 					 GFP_KERNEL, init_linuxrc, NULL, NULL);
 	if (!info)
 		return;
-	call_usermodehelper_exec(info, UMH_WAIT_PROC);
-
-	current->flags &= ~PF_FREEZER_SKIP;
+	call_usermodehelper_exec(info, UMH_WAIT_PROC|UMH_FREEZABLE);
 
 	/* move initrd to rootfs' /old */
 	init_mount("..", ".", NULL, MS_MOVE, NULL);
kernel/umh.c +8 -0
@@ -28,6 +28,7 @@
 #include <linux/async.h>
 #include <linux/uaccess.h>
 #include <linux/initrd.h>
+#include <linux/freezer.h>
 
 #include <trace/events/module.h>
 
@@ -436,6 +437,9 @@ int call_usermodehelper_exec(struct subp
 	if (wait == UMH_NO_WAIT)	/* task has freed sub_info */
 		goto unlock;
 
+	if (wait & UMH_FREEZABLE)
+		freezer_do_not_count();
+
 	if (wait & UMH_KILLABLE) {
 		retval = wait_for_completion_killable(&done);
 		if (!retval)
@@ -448,6 +452,10 @@ int call_usermodehelper_exec(struct subp
 	}
 
 	wait_for_completion(&done);
+
+	if (wait & UMH_FREEZABLE)
+		freezer_count();
+
 wait_done:
 	retval = sub_info->retval;
 out:
author maintainer
#232 lore raw

PATCH v2 5/5 freezer,sched: Rewrite core freezer logic

In reply to Peter Zijlstra

Rewrite the core freezer to behave better wrt thawing and be simpler in general.

By replacing PF_FROZEN with TASK_FROZEN, a special block state, it is ensured frozen tasks stay frozen until thawed and don't randomly wake up early, as is currently possible.

As such, it does away with PF_FROZEN and PF_FREEZER_SKIP, freeing up two PF_flags (yay).

Specifically; the current scheme works a little like:

	freezer_do_not_count();
	schedule();
	freezer_count();

And either the task is blocked, or it lands in try_to_freezer() through freezer_count(). Now, when it is blocked, the freezer considers it frozen and continues.

However, on thawing, once pm_freezing is cleared, freezer_count() stops working, and any random/spurious wakeup will let a task run before its time.

That is, thawing tries to thaw things in explicit order; kernel threads and workqueues before doing bringing SMP back before userspace etc.. However due to the above mentioned races it is entirely possible for userspace tasks to thaw (by accident) before SMP is back.

This can be a fatal problem in asymmetric ISA architectures (eg ARMv9) where the userspace task requires a special CPU to run.

---
 drivers/android/binder.c       |    4 
 drivers/media/pci/pt3/pt3.c    |    4 
 fs/cifs/inode.c                |    4 
 fs/cifs/transport.c            |    5 
 fs/coredump.c                  |    5 
 fs/nfs/file.c                  |    3 
 fs/nfs/inode.c                 |   12 --
 fs/nfs/nfs3proc.c              |    3 
 fs/nfs/nfs4proc.c              |   14 +-
 fs/nfs/nfs4state.c             |    3 
 fs/nfs/pnfs.c                  |    4 
 fs/xfs/xfs_trans_ail.c         |    8 -
 include/linux/completion.h     |    1 
 include/linux/freezer.h        |  244 +----------------------------------------
 include/linux/sched.h          |   41 +++---
 include/linux/sunrpc/sched.h   |    7 -
 include/linux/wait.h           |   40 +++++-
 kernel/cgroup/legacy_freezer.c |   23 +--
 kernel/exit.c                  |    4 
 kernel/fork.c                  |    5 
 kernel/freezer.c               |  137 ++++++++++++++++-------
 kernel/futex/waitwake.c        |    8 -
 kernel/hung_task.c             |    4 
 kernel/power/main.c            |    6 -
 kernel/power/process.c         |   10 -
 kernel/ptrace.c                |    2 
 kernel/sched/completion.c      |    9 +
 kernel/sched/core.c            |   19 ++-
 kernel/signal.c                |   14 +-
 kernel/time/hrtimer.c          |    4 
 kernel/umh.c                   |   20 +--
 mm/khugepaged.c                |    4 
 net/sunrpc/sched.c             |   12 --
 net/unix/af_unix.c             |    8 -
 34 files changed, 281 insertions(+), 410 deletions(-)
drivers/android/binder.c +1 -3
@@ -4034,10 +4034,9 @@ static int binder_wait_for_work(struct b
 	struct binder_proc *proc = thread->proc;
 	int ret = 0;
 
-	freezer_do_not_count();
 	binder_inner_proc_lock(proc);
 	for (;;) {
-		prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE);
+		prepare_to_wait(&thread->wait, &wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE);
 		if (binder_has_work_ilocked(thread, do_proc_work))
 			break;
 		if (do_proc_work)
@@ -4054,7 +4053,6 @@ static int binder_wait_for_work(struct b
 	}
 	finish_wait(&thread->wait, &wait);
 	binder_inner_proc_unlock(proc);
-	freezer_count();
 
 	return ret;
 }
drivers/media/pci/pt3/pt3.c +2 -2
@@ -445,8 +445,8 @@ static int pt3_fetch_thread(void *data)
 		pt3_proc_dma(adap);
 
 		delay = ktime_set(0, PT3_FETCH_DELAY * NSEC_PER_MSEC);
-		set_current_state(TASK_UNINTERRUPTIBLE);
-		freezable_schedule_hrtimeout_range(&delay,
+		set_current_state(TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
+		schedule_hrtimeout_range(&delay,
 					PT3_FETCH_DELAY_DELTA * NSEC_PER_MSEC,
 					HRTIMER_MODE_REL);
 	}
fs/cifs/inode.c +2 -2
@@ -2286,7 +2286,7 @@ cifs_invalidate_mapping(struct inode *in
 static int
 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
 {
-	freezable_schedule_unsafe();
+	schedule();
 	if (signal_pending_state(mode, current))
 		return -ERESTARTSYS;
 	return 0;
@@ -2304,7 +2304,7 @@ cifs_revalidate_mapping(struct inode *in
 		return 0;
 
 	rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
-				     TASK_KILLABLE);
+				     TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 	if (rc)
 		return rc;
 
fs/cifs/transport.c +3 -2
@@ -760,8 +760,9 @@ wait_for_response(struct TCP_Server_Info
 {
 	int error;
 
-	error = wait_event_freezekillable_unsafe(server->response_q,
-				    midQ->mid_state != MID_REQUEST_SUBMITTED);
+	error = wait_event_state(server->response_q,
+				 midQ->mid_state != MID_REQUEST_SUBMITTED,
+				 (TASK_KILLABLE|TASK_FREEZABLE_UNSAFE));
 	if (error < 0)
 		return -ERESTARTSYS;
 
fs/coredump.c +2 -3
@@ -402,9 +402,8 @@ static int coredump_wait(int exit_code,
 	if (core_waiters > 0) {
 		struct core_thread *ptr;
 
-		freezer_do_not_count();
-		wait_for_completion(&core_state->startup);
-		freezer_count();
+		wait_for_completion_state(&core_state->startup,
+					  TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
 		/*
 		 * Wait for all the threads to become inactive, so that
 		 * all the thread context (extended register state, like
fs/nfs/file.c +2 -1
@@ -565,7 +565,8 @@ static vm_fault_t nfs_vm_page_mkwrite(st
 	}
 
 	wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
-			nfs_wait_bit_killable, TASK_KILLABLE);
+			   nfs_wait_bit_killable,
+			   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 
 	lock_page(page);
 	mapping = page_file_mapping(page);
fs/nfs/inode.c +4 -8
@@ -72,18 +72,13 @@ nfs_fattr_to_ino_t(struct nfs_fattr *fat
 	return nfs_fileid_to_ino_t(fattr->fileid);
 }
 
-static int nfs_wait_killable(int mode)
+int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
 {
-	freezable_schedule_unsafe();
+	schedule();
 	if (signal_pending_state(mode, current))
 		return -ERESTARTSYS;
 	return 0;
 }
-
-int nfs_wait_bit_killable(struct wait_bit_key *key, int mode)
-{
-	return nfs_wait_killable(mode);
-}
 EXPORT_SYMBOL_GPL(nfs_wait_bit_killable);
 
 /**
@@ -1331,7 +1326,8 @@ int nfs_clear_invalid_mapping(struct add
 	 */
 	for (;;) {
 		ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING,
-					 nfs_wait_bit_killable, TASK_KILLABLE);
+					 nfs_wait_bit_killable,
+					 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 		if (ret)
 			goto out;
 		spin_lock(&inode->i_lock);
fs/nfs/nfs3proc.c +2 -1
@@ -36,7 +36,8 @@ nfs3_rpc_wrapper(struct rpc_clnt *clnt,
 		res = rpc_call_sync(clnt, msg, flags);
 		if (res != -EJUKEBOX)
 			break;
-		freezable_schedule_timeout_killable_unsafe(NFS_JUKEBOX_RETRY_TIME);
+		__set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
+		schedule_timeout(NFS_JUKEBOX_RETRY_TIME);
 		res = -ERESTARTSYS;
 	} while (!fatal_signal_pending(current));
 	return res;
fs/nfs/nfs4proc.c +7 -7
@@ -408,8 +408,8 @@ static int nfs4_delay_killable(long *tim
 {
 	might_sleep();
 
-	freezable_schedule_timeout_killable_unsafe(
-		nfs4_update_delay(timeout));
+	__set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
+	schedule_timeout(nfs4_update_delay(timeout));
 	if (!__fatal_signal_pending(current))
 		return 0;
 	return -EINTR;
@@ -419,7 +419,8 @@ static int nfs4_delay_interruptible(long
 {
 	might_sleep();
 
-	freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
+	__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
+	schedule_timeout(nfs4_update_delay(timeout));
 	if (!signal_pending(current))
 		return 0;
 	return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
@@ -7363,7 +7364,8 @@ nfs4_retry_setlk_simple(struct nfs4_stat
 		status = nfs4_proc_setlk(state, cmd, request);
 		if ((status != -EAGAIN) || IS_SETLK(cmd))
 			break;
-		freezable_schedule_timeout_interruptible(timeout);
+		__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
+		schedule_timeout(timeout);
 		timeout *= 2;
 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
 		status = -ERESTARTSYS;
@@ -7431,10 +7433,8 @@ nfs4_retry_setlk(struct nfs4_state *stat
 			break;
 
 		status = -ERESTARTSYS;
-		freezer_do_not_count();
-		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
+		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
 			   NFS4_LOCK_MAXTIMEOUT);
-		freezer_count();
 	} while (!signalled());
 
 	remove_wait_queue(q, &waiter.wait);
fs/nfs/nfs4state.c +2 -1
@@ -1314,7 +1314,8 @@ int nfs4_wait_clnt_recover(struct nfs_cl
 
 	refcount_inc(&clp->cl_count);
 	res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
-				 nfs_wait_bit_killable, TASK_KILLABLE);
+				 nfs_wait_bit_killable,
+				 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 	if (res)
 		goto out;
 	if (clp->cl_cons_state < 0)
fs/nfs/pnfs.c +2 -2
@@ -1907,7 +1907,7 @@ static int pnfs_prepare_to_retry_layoutg
 	pnfs_layoutcommit_inode(lo->plh_inode, false);
 	return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
 				   nfs_wait_bit_killable,
-				   TASK_KILLABLE);
+				   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 }
 
 static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
@@ -3182,7 +3182,7 @@ pnfs_layoutcommit_inode(struct inode *in
 		status = wait_on_bit_lock_action(&nfsi->flags,
 				NFS_INO_LAYOUTCOMMITTING,
 				nfs_wait_bit_killable,
-				TASK_KILLABLE);
+				TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 		if (status)
 			goto out;
 	}
fs/xfs/xfs_trans_ail.c +4 -4
@@ -602,9 +602,9 @@ xfsaild(
 
 	while (1) {
 		if (tout && tout <= 20)
-			set_current_state(TASK_KILLABLE);
+			set_current_state(TASK_KILLABLE|TASK_FREEZABLE);
 		else
-			set_current_state(TASK_INTERRUPTIBLE);
+			set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
 
 		/*
 		 * Check kthread_should_stop() after we set the task state to
@@ -653,14 +653,14 @@ xfsaild(
 		    ailp->ail_target == ailp->ail_target_prev &&
 		    list_empty(&ailp->ail_buf_list)) {
 			spin_unlock(&ailp->ail_lock);
-			freezable_schedule();
+			schedule();
 			tout = 0;
 			continue;
 		}
 		spin_unlock(&ailp->ail_lock);
 
 		if (tout)
-			freezable_schedule_timeout(msecs_to_jiffies(tout));
+			schedule_timeout(msecs_to_jiffies(tout));
 
 		__set_current_state(TASK_RUNNING);
 
include/linux/completion.h +1 -0
@@ -103,6 +103,7 @@ extern void wait_for_completion(struct c
 extern void wait_for_completion_io(struct completion *);
 extern int wait_for_completion_interruptible(struct completion *x);
 extern int wait_for_completion_killable(struct completion *x);
+extern int wait_for_completion_state(struct completion *x, unsigned int state);
 extern unsigned long wait_for_completion_timeout(struct completion *x,
 						   unsigned long timeout);
 extern unsigned long wait_for_completion_io_timeout(struct completion *x,
include/linux/freezer.h +10 -234

291 lines not on this page · raw message

include/linux/sched.h +23 -18

62 lines not on this page · raw message

include/linux/sunrpc/sched.h +1 -6
@@ -252,7 +252,7 @@ int		rpc_malloc(struct rpc_task *);
 void		rpc_free(struct rpc_task *);
 int		rpciod_up(void);
 void		rpciod_down(void);
-int		__rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *);
+int		rpc_wait_for_completion_task(struct rpc_task *task);
 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
 struct net;
 void		rpc_show_tasks(struct net *);
@@ -264,11 +264,6 @@ extern struct workqueue_struct *xprtiod_
 void		rpc_prepare_task(struct rpc_task *task);
 gfp_t		rpc_task_gfp_mask(void);
 
-static inline int rpc_wait_for_completion_task(struct rpc_task *task)
-{
-	return __rpc_wait_for_completion_task(task, NULL);
-}
-
 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
 static inline const char * rpc_qname(const struct rpc_wait_queue *q)
 {
include/linux/wait.h +34 -6
@@ -361,8 +361,8 @@ do {										\
 } while (0)
 
 #define __wait_event_freezable(wq_head, condition)				\
-	___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0,		\
-			    freezable_schedule())
+	___wait_event(wq_head, condition, (TASK_INTERRUPTIBLE|TASK_FREEZABLE),	\
+			0, 0, schedule())
 
 /**
  * wait_event_freezable - sleep (or freeze) until a condition gets true
@@ -420,8 +420,8 @@ do {										\
 
 #define __wait_event_freezable_timeout(wq_head, condition, timeout)		\
 	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
-		      TASK_INTERRUPTIBLE, 0, timeout,				\
-		      __ret = freezable_schedule_timeout(__ret))
+		      (TASK_INTERRUPTIBLE|TASK_FREEZABLE), 0, timeout,		\
+		      __ret = schedule_timeout(__ret))
 
 /*
  * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
@@ -641,8 +641,8 @@ do {										\
 
 
 #define __wait_event_freezable_exclusive(wq, condition)				\
-	___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0,			\
-			freezable_schedule())
+	___wait_event(wq, condition, (TASK_INTERRUPTIBLE|TASK_FREEZABLE), 1, 0,\
+			schedule())
 
 #define wait_event_freezable_exclusive(wq, condition)				\
 ({										\
@@ -931,6 +931,34 @@ extern int do_wait_intr_irq(wait_queue_h
 	__ret;									\
 })
 
+#define __wait_event_state(wq, condition, state)				\
+	___wait_event(wq, condition, state, 0, 0, schedule())
+
+/**
+ * wait_event_state - sleep until a condition gets true
+ * @wq_head: the waitqueue to wait on
+ * @condition: a C expression for the event to wait for
+ * @state: state to sleep in
+ *
+ * The process is put to sleep (@state) until the @condition evaluates to true
+ * or a signal is received.  The @condition is checked each time the waitqueue
+ * @wq_head is woken up.
+ *
+ * wake_up() has to be called after changing any variable that could
+ * change the result of the wait condition.
+ *
+ * The function will return -ERESTARTSYS if it was interrupted by a
+ * signal and 0 if @condition evaluated to true.
+ */
+#define wait_event_state(wq_head, condition, state)				\
+({										\
+	int __ret = 0;								\
+	might_sleep();								\
+	if (!(condition))							\
+		__ret = __wait_event_state(wq_head, condition, state);		\
+	__ret;									\
+})
+
 #define __wait_event_killable_timeout(wq_head, condition, timeout)		\
 	___wait_event(wq_head, ___wait_cond_timeout(condition),			\
 		      TASK_KILLABLE, 0, timeout,				\
kernel/cgroup/legacy_freezer.c +8 -15
@@ -113,7 +113,7 @@ static int freezer_css_online(struct cgr
 
 	if (parent && (parent->state & CGROUP_FREEZING)) {
 		freezer->state |= CGROUP_FREEZING_PARENT | CGROUP_FROZEN;
-		atomic_inc(&system_freezing_cnt);
+		static_branch_inc(&freezer_active);
 	}
 
 	mutex_unlock(&freezer_mutex);
@@ -134,7 +134,7 @@ static void freezer_css_offline(struct c
 	mutex_lock(&freezer_mutex);
 
 	if (freezer->state & CGROUP_FREEZING)
-		atomic_dec(&system_freezing_cnt);
+		static_branch_dec(&freezer_active);
 
 	freezer->state = 0;
 
@@ -179,6 +179,7 @@ static void freezer_attach(struct cgroup
 			__thaw_task(task);
 		} else {
 			freeze_task(task);
+
 			/* clear FROZEN and propagate upwards */
 			while (freezer && (freezer->state & CGROUP_FROZEN)) {
 				freezer->state &= ~CGROUP_FROZEN;
@@ -271,16 +272,8 @@ static void update_if_frozen(struct cgro
 	css_task_iter_start(css, 0, &it);
 
 	while ((task = css_task_iter_next(&it))) {
-		if (freezing(task)) {
-			/*
-			 * freezer_should_skip() indicates that the task
-			 * should be skipped when determining freezing
-			 * completion.  Consider it frozen in addition to
-			 * the usual frozen condition.
-			 */
-			if (!frozen(task) && !freezer_should_skip(task))
-				goto out_iter_end;
-		}
+		if (freezing(task) && !frozen(task))
+			goto out_iter_end;
 	}
 
 	freezer->state |= CGROUP_FROZEN;
@@ -357,7 +350,7 @@ static void freezer_apply_state(struct f
 
 	if (freeze) {
 		if (!(freezer->state & CGROUP_FREEZING))
-			atomic_inc(&system_freezing_cnt);
+			static_branch_inc(&freezer_active);
 		freezer->state |= state;
 		freeze_cgroup(freezer);
 	} else {
@@ -366,9 +359,9 @@ static void freezer_apply_state(struct f
 		freezer->state &= ~state;
 
 		if (!(freezer->state & CGROUP_FREEZING)) {
-			if (was_freezing)
-				atomic_dec(&system_freezing_cnt);
 			freezer->state &= ~CGROUP_FROZEN;
+			if (was_freezing)
+				static_branch_dec(&freezer_active);
 			unfreeze_cgroup(freezer);
 		}
 	}
kernel/exit.c +2 -2
@@ -374,10 +374,10 @@ static void coredump_task_exit(struct ta
 			complete(&core_state->startup);
 
 		for (;;) {
-			set_current_state(TASK_UNINTERRUPTIBLE);
+			set_current_state(TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
 			if (!self.task) /* see coredump_finish() */
 				break;
-			freezable_schedule();
+			schedule();
 		}
 		__set_current_state(TASK_RUNNING);
 	}
kernel/fork.c +2 -3
@@ -1417,13 +1417,12 @@ static void complete_vfork_done(struct t
 static int wait_for_vfork_done(struct task_struct *child,
 				struct completion *vfork)
 {
+	unsigned int state = TASK_UNINTERRUPTIBLE|TASK_KILLABLE|TASK_FREEZABLE;
 	int killed;
 
-	freezer_do_not_count();
 	cgroup_enter_frozen();
-	killed = wait_for_completion_killable(vfork);
+	killed = wait_for_completion_state(vfork, state);
 	cgroup_leave_frozen(false);
-	freezer_count();
 
 	if (killed) {
 		task_lock(child);
kernel/freezer.c +100 -37

203 lines not on this page · raw message

kernel/futex/waitwake.c +4 -4
@@ -334,7 +334,7 @@ void futex_wait_queue(struct futex_hash_
 	 * futex_queue() calls spin_unlock() upon completion, both serializing
 	 * access to the hash list and forcing another memory barrier.
 	 */
-	set_current_state(TASK_INTERRUPTIBLE);
+	set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
 	futex_queue(q, hb);
 
 	/* Arm the timer */
@@ -352,7 +352,7 @@ void futex_wait_queue(struct futex_hash_
 		 * is no timeout, or if it has yet to expire.
 		 */
 		if (!timeout || timeout->task)
-			freezable_schedule();
+			schedule();
 	}
 	__set_current_state(TASK_RUNNING);
 }
@@ -430,7 +430,7 @@ static int futex_wait_multiple_setup(str
 			return ret;
 	}
 
-	set_current_state(TASK_INTERRUPTIBLE);
+	set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
 
 	for (i = 0; i < count; i++) {
 		u32 __user *uaddr = (u32 __user *)(unsigned long)vs[i].w.uaddr;
@@ -504,7 +504,7 @@ static void futex_sleep_multiple(struct
 			return;
 	}
 
-	freezable_schedule();
+	schedule();
 }
 
 /**
kernel/hung_task.c +2 -2
@@ -95,8 +95,8 @@ static void check_hung_task(struct task_
 	 * Ensure the task is not frozen.
 	 * Also, skip vfork and any other user process that freezer should skip.
 	 */
-	if (unlikely(t->flags & (PF_FROZEN | PF_FREEZER_SKIP)))
-	    return;
+	if (unlikely(READ_ONCE(t->__state) & (TASK_FREEZABLE | TASK_FROZEN)))
+		return;
 
 	/*
 	 * When a freshly created task is scheduled once, changes its state to
kernel/power/main.c +3 -3
@@ -24,7 +24,7 @@
 unsigned int lock_system_sleep(void)
 {
 	unsigned int flags = current->flags;
-	current->flags |= PF_FREEZER_SKIP;
+	current->flags |= PF_NOFREEZE;
 	mutex_lock(&system_transition_mutex);
 	return flags;
 }
@@ -48,8 +48,8 @@ void unlock_system_sleep(unsigned int fl
 	 * Which means, if we use try_to_freeze() here, it would make them
 	 * enter the refrigerator, thus causing hibernation to lockup.
 	 */
-	if (!(flags & PF_FREEZER_SKIP))
-		current->flags &= ~PF_FREEZER_SKIP;
+	if (!(flags & PF_NOFREEZE))
+		current->flags &= ~PF_NOFREEZE;
 	mutex_unlock(&system_transition_mutex);
 }
 EXPORT_SYMBOL_GPL(unlock_system_sleep);
kernel/power/process.c +4 -6
@@ -53,8 +53,7 @@ static int try_to_freeze_tasks(bool user
 			if (p == current || !freeze_task(p))
 				continue;
 
-			if (!freezer_should_skip(p))
-				todo++;
+			todo++;
 		}
 		read_unlock(&tasklist_lock);
 
@@ -99,8 +98,7 @@ static int try_to_freeze_tasks(bool user
 		if (!wakeup || pm_debug_messages_on) {
 			read_lock(&tasklist_lock);
 			for_each_process_thread(g, p) {
-				if (p != current && !freezer_should_skip(p)
-				    && freezing(p) && !frozen(p))
+				if (p != current && freezing(p) && !frozen(p))
 					sched_show_task(p);
 			}
 			read_unlock(&tasklist_lock);
@@ -132,7 +130,7 @@ int freeze_processes(void)
 	current->flags |= PF_SUSPEND_TASK;
 
 	if (!pm_freezing)
-		atomic_inc(&system_freezing_cnt);
+		static_branch_inc(&freezer_active);
 
 	pm_wakeup_clear(0);
 	pr_info("Freezing user space processes ... ");
@@ -193,7 +191,7 @@ void thaw_processes(void)
 
 	trace_suspend_resume(TPS("thaw_processes"), 0, true);
 	if (pm_freezing)
-		atomic_dec(&system_freezing_cnt);
+		static_branch_dec(&freezer_active);
 	pm_freezing = false;
 	pm_nosig_freezing = false;
 
kernel/ptrace.c +1 -1
@@ -288,7 +288,7 @@ static int ptrace_check_attach(struct ta
 	}
 	__set_current_state(TASK_RUNNING);
 
-	if (!wait_task_inactive(child, TASK_TRACED) ||
+	if (!wait_task_inactive(child, TASK_TRACED|TASK_FREEZABLE) ||
 	    !ptrace_freeze_traced(child))
 		return -ESRCH;
 
kernel/sched/completion.c +9 -0
@@ -247,6 +247,15 @@ int __sched wait_for_completion_killable
 }
 EXPORT_SYMBOL(wait_for_completion_killable);
 
+int __sched wait_for_completion_state(struct completion *x, unsigned int state)
+{
+	long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, state);
+	if (t == -ERESTARTSYS)
+		return t;
+	return 0;
+}
+EXPORT_SYMBOL(wait_for_completion_state);
+
 /**
  * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
  * @x:  holds the state of this particular completion
kernel/sched/core.c +16 -3
@@ -3260,6 +3260,19 @@ int migrate_swap(struct task_struct *cur
 }
 #endif /* CONFIG_NUMA_BALANCING */
 
+static inline bool __wti_match(struct task_struct *p, unsigned int match_state)
+{
+	unsigned int state = READ_ONCE(p->__state);
+
+	if ((match_state & TASK_FREEZABLE) && state == TASK_FROZEN)
+		return true;
+
+	if (state == (match_state & ~TASK_FREEZABLE))
+		return true;
+
+	return false;
+}
+
 /*
  * wait_task_inactive - wait for a thread to unschedule.
  *
@@ -3304,7 +3317,7 @@ unsigned long wait_task_inactive(struct
 		 * is actually now running somewhere else!
 		 */
 		while (task_running(rq, p)) {
-			if (match_state && unlikely(READ_ONCE(p->__state) != match_state))
+			if (match_state && !__wti_match(p, match_state))
 				return 0;
 			cpu_relax();
 		}
@@ -3319,7 +3332,7 @@ unsigned long wait_task_inactive(struct
 		running = task_running(rq, p);
 		queued = task_on_rq_queued(p);
 		ncsw = 0;
-		if (!match_state || READ_ONCE(p->__state) == match_state)
+		if (!match_state || __wti_match(p, match_state))
 			ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
 		task_rq_unlock(rq, p, &rf);
 
@@ -6320,7 +6333,7 @@ static void __sched notrace __schedule(u
 			prev->sched_contributes_to_load =
 				(prev_state & TASK_UNINTERRUPTIBLE) &&
 				!(prev_state & TASK_NOLOAD) &&
-				!(prev->flags & PF_FROZEN);
+				!(prev_state & TASK_FROZEN);
 
 			if (prev->sched_contributes_to_load)
 				rq->nr_uninterruptible++;
kernel/signal.c +7 -7
@@ -2321,7 +2321,7 @@ static int ptrace_stop(int exit_code, in
 		clear_traced_quiesce();
 
 		preempt_enable_no_resched();
-		freezable_schedule();
+		schedule();
 
 		cgroup_leave_frozen(true);
 	} else {
@@ -2514,7 +2514,7 @@ static bool do_signal_stop(int signr)
 
 		/* Now we don't run again until woken by SIGCONT or SIGKILL */
 		cgroup_enter_frozen();
-		freezable_schedule();
+		schedule();
 		return true;
 	} else {
 		/*
@@ -2589,11 +2589,11 @@ static void do_freezer_trap(void)
 	 * immediately (if there is a non-fatal signal pending), and
 	 * put the task into sleep.
 	 */
-	__set_current_state(TASK_INTERRUPTIBLE);
+	__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
 	clear_thread_flag(TIF_SIGPENDING);
 	spin_unlock_irq(&current->sighand->siglock);
 	cgroup_enter_frozen();
-	freezable_schedule();
+	schedule();
 }
 
 static int ptrace_signal(int signr, kernel_siginfo_t *info, enum pid_type type)
@@ -3639,9 +3639,9 @@ static int do_sigtimedwait(const sigset_
 		recalc_sigpending();
 		spin_unlock_irq(&tsk->sighand->siglock);
 
-		__set_current_state(TASK_INTERRUPTIBLE);
-		ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
-							 HRTIMER_MODE_REL);
+		__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
+		ret = schedule_hrtimeout_range(to, tsk->timer_slack_ns,
+					       HRTIMER_MODE_REL);
 		spin_lock_irq(&tsk->sighand->siglock);
 		__set_task_blocked(tsk, &tsk->real_blocked);
 		sigemptyset(&tsk->real_blocked);
kernel/time/hrtimer.c +2 -2
@@ -2037,11 +2037,11 @@ static int __sched do_nanosleep(struct h
 	struct restart_block *restart;
 
 	do {
-		set_current_state(TASK_INTERRUPTIBLE);
+		set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
 		hrtimer_sleeper_start_expires(t, mode);
 
 		if (likely(t->task))
-			freezable_schedule();
+			schedule();
 
 		hrtimer_cancel(&t->timer);
 		mode = HRTIMER_MODE_ABS;
kernel/umh.c +9 -11
@@ -404,6 +404,7 @@ EXPORT_SYMBOL(call_usermodehelper_setup)
  */
 int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
 {
+	unsigned int state = TASK_UNINTERRUPTIBLE;
 	DECLARE_COMPLETION_ONSTACK(done);
 	int retval = 0;
 
@@ -437,25 +438,22 @@ int call_usermodehelper_exec(struct subp
 	if (wait == UMH_NO_WAIT)	/* task has freed sub_info */
 		goto unlock;
 
+	if (wait & UMH_KILLABLE)
+		state |= TASK_KILLABLE;
+
 	if (wait & UMH_FREEZABLE)
-		freezer_do_not_count();
+		state |= TASK_FREEZABLE;
 
-	if (wait & UMH_KILLABLE) {
-		retval = wait_for_completion_killable(&done);
-		if (!retval)
-			goto wait_done;
+	retval = wait_for_completion_state(&done, state);
+	if (!retval)
+		goto wait_done;
 
+	if (wait & UMH_KILLABLE) {
 		/* umh_complete() will see NULL and free sub_info */
 		if (xchg(&sub_info->complete, NULL))
 			goto unlock;
-		/* fallthrough, umh_complete() was already called */
 	}
 
-	wait_for_completion(&done);
-
-	if (wait & UMH_FREEZABLE)
-		freezer_count();
-
 wait_done:
 	retval = sub_info->retval;
 out:
mm/khugepaged.c +2 -2
@@ -780,8 +780,8 @@ static void khugepaged_alloc_sleep(void)
 	DEFINE_WAIT(wait);
 
 	add_wait_queue(&khugepaged_wait, &wait);
-	freezable_schedule_timeout_interruptible(
-		msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
+	__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
+	schedule_timeout(msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
 	remove_wait_queue(&khugepaged_wait, &wait);
 }
 
net/sunrpc/sched.c +5 -7
@@ -268,7 +268,7 @@ EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue
 
 static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode)
 {
-	freezable_schedule_unsafe();
+	schedule();
 	if (signal_pending_state(mode, current))
 		return -ERESTARTSYS;
 	return 0;
@@ -332,14 +332,12 @@ static int rpc_complete_task(struct rpc_
  * to enforce taking of the wq->lock and hence avoid races with
  * rpc_complete_task().
  */
-int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action)
+int rpc_wait_for_completion_task(struct rpc_task *task)
 {
-	if (action == NULL)
-		action = rpc_wait_bit_killable;
 	return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
-			action, TASK_KILLABLE);
+			rpc_wait_bit_killable, TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
 }
-EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
+EXPORT_SYMBOL_GPL(rpc_wait_for_completion_task);
 
 /*
  * Make an RPC task runnable.
@@ -963,7 +961,7 @@ static void __rpc_execute(struct rpc_tas
 		trace_rpc_task_sync_sleep(task, task->tk_action);
 		status = out_of_line_wait_on_bit(&task->tk_runstate,
 				RPC_TASK_QUEUED, rpc_wait_bit_killable,
-				TASK_KILLABLE);
+				TASK_KILLABLE|TASK_FREEZABLE);
 		if (status < 0) {
 			/*
 			 * When a sync task receives a signal, it exits with
net/unix/af_unix.c +3 -5
@@ -2530,13 +2530,14 @@ static long unix_stream_data_wait(struct
 				  struct sk_buff *last, unsigned int last_len,
 				  bool freezable)
 {
+	unsigned int state = TASK_INTERRUPTIBLE | freezable * TASK_FREEZABLE;
 	struct sk_buff *tail;
 	DEFINE_WAIT(wait);
 
 	unix_state_lock(sk);
 
 	for (;;) {
-		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
+		prepare_to_wait(sk_sleep(sk), &wait, state);
 
 		tail = skb_peek_tail(&sk->sk_receive_queue);
 		if (tail != last ||
@@ -2549,10 +2550,7 @@ static long unix_stream_data_wait(struct
 
 		sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
 		unix_state_unlock(sk);
-		if (freezable)
-			timeo = freezable_schedule_timeout(timeo);
-		else
-			timeo = schedule_timeout(timeo);
+		timeo = schedule_timeout(timeo);
 		unix_state_lock(sk);
 
 		if (sock_flag(sk, SOCK_DEAD))
#233 lore raw

Re: [PATCH v2 5/5] freezer,sched: Rewrite core freezer logic

kernel/ptrace.c static int ptrace_check_attach(struct ta · line 293

Quoted 10 lines · Peter Zijlstra
kernel/ptrace.c +1 -1
@@ -288,7 +288,7 @@ static int ptrace_check_attach(struct ta
 	}
 	__set_current_state(TASK_RUNNING);
 
-	if (!wait_task_inactive(child, TASK_TRACED) ||
+	if (!wait_task_inactive(child, TASK_TRACED|TASK_FREEZABLE) ||
 	    !ptrace_freeze_traced(child))
 		return -ESRCH;

Do we mind that this is going to fail if the child is frozen during ptrace_check_attach?

I think to avoid that we need to safely get this to wait_task_inactive(child, 0), like the coredump code uses.

I would like to say that we can do without the wait_task_inactive, but it looks like it is necessary to ensure that all of the userspace registers are saved where the tracer can get at them.

Eric

maintainer
#234 lore raw

Re: [PATCH v2 5/5] freezer,sched: Rewrite core freezer logic

Quoted 4 lines · Eric W. Biederman

I would like to say that we can do without the wait_task_inactive, but it looks like it is necessary to ensure that all of the userspace registers are saved where the tracer can get at them.

Yes, for example, fpu regs.

But there are more problems. For example, if debugger changes TIF_BLOCKSTEP we need to ensure the child is already inactive and it will do another switch_to() after that.

Oleg.

author maintainer
#235 lore raw

Re: [PATCH v2 5/5] freezer,sched: Rewrite core freezer logic

In reply to Eric W. Biederman
Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Peter Zijlstra
kernel/ptrace.c +0 -0
@@ -288,7 +288,7 @@ static int ptrace_check_attach(struct ta
 	}
 	__set_current_state(TASK_RUNNING);
 
Quoted 7 lines

kernel/ptrace.c static int ptrace_check_attach(struct ta · line 293

Quoted 4 lines
kernel/ptrace.c +1 -1
-	if (!wait_task_inactive(child, TASK_TRACED) ||
+	if (!wait_task_inactive(child, TASK_TRACED|TASK_FREEZABLE) ||
 	    !ptrace_freeze_traced(child))
 		return -ESRCH;

Do we mind that this is going to fail if the child is frozen during ptrace_check_attach?

Why should this fail? wait_task_inactive() will in fact succeed if it is frozen due to the added TASK_FREEZABLE and some wait_task_inactive() changes elsewhere in this patch.

And I don't see why ptrace_freeze_traced() should fail. It'll warn though, I should extend/remove that WARN_ON_ONCE() looking at __state, but it should work.

author maintainer
#236 lore raw

Re: [PATCH v2 5/5] freezer,sched: Rewrite core freezer logic

Quoted 17 lines · Peter Zijlstra
Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Peter Zijlstra
kernel/ptrace.c +0 -0
@@ -288,7 +288,7 @@ static int ptrace_check_attach(struct ta
 	}
 	__set_current_state(TASK_RUNNING);
 
Quoted 7 lines

kernel/ptrace.c static int ptrace_check_attach(struct ta · line 293

Quoted 4 lines
kernel/ptrace.c +1 -1
-	if (!wait_task_inactive(child, TASK_TRACED) ||
+	if (!wait_task_inactive(child, TASK_TRACED|TASK_FREEZABLE) ||
 	    !ptrace_freeze_traced(child))
 		return -ESRCH;

Do we mind that this is going to fail if the child is frozen during ptrace_check_attach?

Quoted 3 lines

Why should this fail? wait_task_inactive() will in fact succeed if it is frozen due to the added TASK_FREEZABLE and some wait_task_inactive() changes elsewhere in this patch.

These:

kernel/sched/core.c +15 -2
@@ -3260,6 +3260,19 @@ int migrate_swap(struct task_struct *cur
 }
 #endif /* CONFIG_NUMA_BALANCING */
 
+static inline bool __wti_match(struct task_struct *p, unsigned int match_state)
+{
+	unsigned int state = READ_ONCE(p->__state);
+
+	if ((match_state & TASK_FREEZABLE) && state == TASK_FROZEN)
+		return true;
+
+	if (state == (match_state & ~TASK_FREEZABLE))
+		return true;
+
+	return false;
+}
+
 /*
  * wait_task_inactive - wait for a thread to unschedule.
  *
@@ -3304,7 +3317,7 @@ unsigned long wait_task_inactive(struct
 		 * is actually now running somewhere else!
 		 */
 		while (task_running(rq, p)) {
-			if (match_state && unlikely(READ_ONCE(p->__state) != match_state))
+			if (match_state && !__wti_match(p, match_state))
 				return 0;
 			cpu_relax();
 		}
@@ -3319,7 +3332,7 @@ unsigned long wait_task_inactive(struct
 		running = task_running(rq, p);
 		queued = task_on_rq_queued(p);
 		ncsw = 0;
-		if (!match_state || READ_ONCE(p->__state) == match_state)
+		if (!match_state || __wti_match(p, match_state))
 			ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
 		task_rq_unlock(rq, p, &rf);
 
Quoted 3 lines

And I don't see why ptrace_freeze_traced() should fail. It'll warn though, I should extend/remove that WARN_ON_ONCE() looking at __state, but it should work.

And that looks like (after removal of the one WARN):

static bool ptrace_freeze_traced(struct task_struct *task)

{
	unsigned long flags;
	bool ret = false;

	/* Lockless, nobody but us can set this flag */
	if (task->jobctl & JOBCTL_LISTENING)
		return ret;

	if (!lock_task_sighand(task, &flags))
		return ret;

	if (task_is_traced(task) &&
	    !looks_like_a_spurious_pid(task) &&
	    !__fatal_signal_pending(task)) {
		WARN_ON_ONCE(task->jobctl & JOBCTL_DELAY_WAKEKILL);
		task->jobctl |= JOBCTL_DELAY_WAKEKILL;
		ret = true;
	}
	unlock_task_sighand(task, &flags);

	return ret;
}

And nothing there cares about ->__state.

#237 lore raw

Re: [PATCH v2 5/5] freezer,sched: Rewrite core freezer logic

Quoted 18 lines · Peter Zijlstra
Quoted 17 lines · Peter Zijlstra
Quoted 15 lines · Eric W. Biederman

kernel/ptrace.c static int ptrace_check_attach(struct ta · line 293

Quoted 10 lines · Peter Zijlstra
kernel/ptrace.c +1 -1
@@ -288,7 +288,7 @@ static int ptrace_check_attach(struct ta
 	}
 	__set_current_state(TASK_RUNNING);
 
-	if (!wait_task_inactive(child, TASK_TRACED) ||
+	if (!wait_task_inactive(child, TASK_TRACED|TASK_FREEZABLE) ||
 	    !ptrace_freeze_traced(child))
 		return -ESRCH;

Do we mind that this is going to fail if the child is frozen during ptrace_check_attach?

Quoted 5 lines
Quoted 3 lines

Why should this fail? wait_task_inactive() will in fact succeed if it is frozen due to the added TASK_FREEZABLE and some wait_task_inactive() changes elsewhere in this patch.

These:

I had missed that change to wait_task_inactive.

Still that change to wait_task_inactive fundamentally depends upon the fact that we don't care about the state we are passing into wait_task_inactive. So I think it would be better to simply have a precursor patch that changes wait_task_inactive(child, TASK_TRACED) to wait_task_inactive(child, 0) and say so explicitly.

Eric

#238 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra

Find here a new posting of the ptrace and freezer patches :-)

The majority of the changes are in patch 2, which with much feedback from Oleg and Eric has changed lots.

I'm hoping we're converging on something agreeable.

I tested this under RT (had to remove the preempt-disable section in ptrace_stop()) with ssdd [0]. It forks a few tasks and then PTRACE_SINGLESTEPs them for a few iterations.

The following failures were reported by that tool:
| forktest#27/3790: EXITING, ERROR: wait on PTRACE_ATTACH saw a SIGCHLD count of 0, should be 1
| forktest#225/40029: EXITING, ERROR: wait on PTRACE_SINGLESTEP #22241: no SIGCHLD seen (signal count == 0), signo 5

very rarely. Then I managed to figure out that the latter error triggers if I compile something large with a RT priority. Sadly it also happens with my old ptrace hack (but I just noticed it). It didn't happen with without RT (just the 5 patches applied).

I also managed to trigger this backtrace with RT:

|WARNING: CPU: 1 PID: 3748 at kernel/signal.c:2237 ptrace_stop+0x356/0x370
|Modules linked in:
|CPU: 1 PID: 3748 Comm: ssdd Not tainted 5.18.0-rc3-rt1+ #1
|Hardware name: Intel Corporation S2600CP/S2600CP, BIOS SE5C600.86B.02.03.0003.041920141333 04/19/2014
|RIP: 0010:ptrace_stop+0x356/0x370
|RSP: 0000:ffffc9000d277d98 EFLAGS: 00010246
|RAX: ffff888116d1e100 RBX: ffff888116d1e100 RCX: 0000000000000001
|RDX: 0000000000000001 RSI: 000000000000002e RDI: ffffffff822bdcc3
|RBP: ffff888116d1e100 R08: ffff88811ca99870 R09: 0000000000000001
|R10: ffff88811ca99910 R11: ffff88852ade2680 R12: ffffc9000d277e90
|R13: 0000000000000004 R14: ffff888116d1ed48 R15: 0000000000000000
|FS:  00007f0afdad4580(0000) GS:ffff88852aa40000(0000) knlGS:0000000000000000
|CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
|CR2: 00007f0afdad4508 CR3: 0000000558198006 CR4: 00000000000606e0
|Call Trace:
| <TASK>
| get_signal+0x553/0x870
| arch_do_signal_or_restart+0x31/0x7b0
| exit_to_user_mode_prepare+0xe4/0x110
| irqentry_exit_to_user_mode+0x5/0x20
| noist_exc_debug+0xe0/0x120
| asm_exc_debug+0x2b/0x30
|RSP: 002b:00007fffae964b70 EFLAGS: 00000346
|RAX: 0000000000000000 RBX: 00000000000000fc RCX: 00007f0afd9c0d35
|RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000001
|RBP: 00007fffae964e38 R08: 0000000000000000 R09: 00007fffae962a82
|R10: 00007f0afdad4850 R11: 0000000000000246 R12: 0000000000000000
|R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
| </TASK>

which is the WARN_ON_ONCE() in clear_traced_quiesce().

[0] https://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git/tree/src/ssdd/ssdd.c

Sebastian

#239 lore raw
Quoted 11 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Peter Zijlstra

Find here a new posting of the ptrace and freezer patches :-)

The majority of the changes are in patch 2, which with much feedback from Oleg and Eric has changed lots.

I'm hoping we're converging on something agreeable.

I tested this under RT (had to remove the preempt-disable section in ptrace_stop()) with ssdd [0]. It forks a few tasks and then PTRACE_SINGLESTEPs them for a few iterations.

Out of curiosity why did you need to remove the preempt_disable section on PREEMPT_RT? It should have lasted for just a moment until schedule was called.

Eric

#240 lore raw
Quoted 10 lines · Eric W. Biederman
Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Peter Zijlstra

Find here a new posting of the ptrace and freezer patches :-)

The majority of the changes are in patch 2, which with much feedback from Oleg and Eric has changed lots.

I'm hoping we're converging on something agreeable.

Quoted 7 lines
Quoted 3 lines

I tested this under RT (had to remove the preempt-disable section in ptrace_stop()) with ssdd [0]. It forks a few tasks and then PTRACE_SINGLESTEPs them for a few iterations.

Out of curiosity why did you need to remove the preempt_disable section on PREEMPT_RT? It should have lasted for just a moment until schedule was called.

within that section spinlock_t locks are acquired. These locks are sleeping locks on PREEMPT_RT and must not be acquired within a preempt-disable section. (A spinlock_t lock does not disable preemption on PREEMPT_RT.)

Quoted 1 line

Eric

Sebastian