Re: [PATCH 2/2] ptrace: Convert ptrace_attach() to use lock guards

From: Oleg Nesterov
Date: Thu Nov 02 2023 - 11:19:26 EST


On 11/02, Peter Zijlstra wrote:
>
> Created as testing for the conditional guard infrastructure.

This patch scares me ;) I need to get used to guard/etc.
But looks correct.

Reviewed-by: Oleg Nesterov <oleg@xxxxxxxxxx>






> + /* SEIZE doesn't trap tracee on attach */
> + if (!seize)
> + send_sig_info(SIGSTOP, SEND_SIG_PRIV, task);

This is offtopic, but with or without this patch it is a bit
ugly to drop ->siglock and take it again right after that.
We can do (later) a minor cleanup on top of this change.


--- x/kernel/ptrace.c 2023-11-02 16:03:37.646838530 +0100
+++ x/kernel/ptrace.c 2023-11-02 16:05:52.171052506 +0100
@@ -386,10 +386,14 @@
return 0;
}

-static inline void ptrace_set_stopped(struct task_struct *task)
+static inline void ptrace_set_stopped(struct task_struct *task, bool seize)
{
guard(spinlock)(&task->sighand->siglock);

+ /* SEIZE doesn't trap tracee on attach */
+ if (!seize)
+ send_signal_locked(SIGSTOP, SEND_SIG_PRIV, task, PIDTYPE_PID);
+
/*
* If the task is already STOPPED, set JOBCTL_TRAP_STOP and
* TRAPPING, and kick it so that it transits to TRACED. TRAPPING
@@ -470,11 +474,6 @@
task->ptrace = flags;

ptrace_link(task, current);
-
- /* SEIZE doesn't trap tracee on attach */
- if (!seize)
- send_sig_info(SIGSTOP, SEND_SIG_PRIV, task);
-
ptrace_set_stopped(task);
}
}