ptrace: introduce the empty "struct ptrace_context"
Subsystems (5)
Oleg Nesterov
maintainerRFC PATCH v2 4/12 ptrace: introduce the empty "struct ptrace_context"
Suggested by Roland.
Introduce the new "struct ptrace_context" and add the pointer to task_struct. The next patches will move all ptrace-related fields from task_struct into this struct. From now, if the task was ever ptraced it has ->ptrace_ctx != NULL. Freed by free_task() along with task_struct itself.
With utrace based ptrace we can move this struct into utrace_engine->data.
include/linux/sched.h | 1 + include/linux/ptrace.h | 4 ++++ include/linux/tracehook.h | 34 ++++++++++++++++++++++++++++++++++ kernel/fork.c | 5 +++++ kernel/ptrace.c | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 1 deletion(-)
include/linux/sched.h renamed from include/linux/sched.h~3_STRUCT +1 -0
@@ -1201,6 +1201,7 @@ struct task_struct {struct list_head sibling;/* linkage in my parent's children list */ struct task_struct *group_leader;/* threadgroup leader */ +struct ptrace_context *ptrace_ctx;/* * ptraced is the list of tasks this task is using ptrace on. * This includes both natural children and PTRACE_ATTACH targets.
include/linux/ptrace.h renamed from include/linux/ptrace.h~3_STRUCT +4 -0
@@ -79,6 +79,10 @@#include <linux/compiler.h> /* For unlikely. */ #include <linux/sched.h> /* For struct task_struct. */ +struct ptrace_context { +}; + +extern int alloc_ptrace_context (struct task_struct *child);extern long arch_ptrace (struct task_struct *child,long request,long addr,long data);extern int ptrace_traceme (void );
include/linux/tracehook.h renamed from include/linux/tracehook.h~3_STRUCT +34 -0
@@ -243,6 +243,40 @@ static inline int tracehook_prepare_clon }/** +* tracehook_init_task - initialize the new task +* @task: the task to initialize +* @clone_flags: %CLONE_* flags from clone/fork/vfork system call +* @trace: return value from tracehook_prepare_clone() +* +* This is called immediately after dup_task_struct(). +* It must clear/reset any tracing state so that tracehook_free_task() +* will work safely if the task creation fails. If the task creation +* succeeds, a tracehook_finish_clone() call will follow with locks +* held, before @task starts or is accessible to anyone else. +* +* Called with no locks held. +*/ +static inline int tracehook_init_task (struct task_struct *task, +unsigned long clone_flags,int trace) +{ + task->ptrace_ctx =NULL ; +if (unlikely ((clone_flags &CLONE_PTRACE ) || trace)) +return alloc_ptrace_context (task); +return 0 ; +} + +/** +* tracehook_free_task - task is about to be freed +* @task: task that will be freed +* +* May be called from any context. +*/ +static inline void tracehook_free_task (struct task_struct *task) +{ +kfree (task->ptrace_ctx); +} + +/** * tracehook_finish_clone - new child created and being attached * @child: new child task * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
kernel/fork.c renamed from kernel/fork.c~3_STRUCT +5 -0
@@ -143,6 +143,7 @@ void free_task(struct task_struct *tsk)free_thread_info (tsk->stack);rt_mutex_debug_task_free (tsk);ftrace_graph_exit_task (tsk); +tracehook_free_task (tsk);free_task_struct (tsk); }EXPORT_SYMBOL (free_task);
@@ -982,6 +983,10 @@ static struct task_struct *copy_process(if (!p) goto fork_out; + retval =tracehook_init_task (p, clone_flags, trace); +if (retval) + goto bad_fork_free; +rt_mutex_init_task (p);#ifdef CONFIG_PROVE_LOCKING
kernel/ptrace.c renamed from kernel/ptrace.c~3_STRUCT +35 -1
@@ -174,6 +174,32 @@ bool ptrace_may_access(struct task_strucreturn !err; } +/* +* Check the task has ->ptrace_ctx or alloc the new one. Called +* from preemptible context when we are going to attach to this task. +* Once allocated, ->ptrace_ctx is never freed until free_task(). +*/ +int alloc_ptrace_context (struct task_struct *tsk) +{ +struct ptrace_context *ptrace_ctx; + +if (tsk->ptrace_ctx) +return 0 ; + + ptrace_ctx =kzalloc (sizeof (*ptrace_ctx),GFP_KERNEL ); +if (unlikely (!ptrace_ctx)) +return -ENOMEM ; + +task_lock (tsk); +if (likely (!tsk->ptrace_ctx)) + tsk->ptrace_ctx = ptrace_ctx; +else +kfree (ptrace_ctx); +task_unlock (tsk); + +return 0 ; +} +int ptrace_attach (struct task_struct *task) {int retval;
@@ -199,6 +225,10 @@ int ptrace_attach(struct task_struct *taif (retval) goto unlock_creds; + retval =alloc_ptrace_context (task); +if (unlikely (retval)) + goto unlock_creds; +write_lock_irq (&tasklist_lock); retval = -EPERM ;if (unlikely (task->exit_state))
@@ -230,8 +260,12 @@ out:*/ int ptrace_traceme (void ) { -int ret = -EPERM ; +int ret =alloc_ptrace_context (current); +if (unlikely (ret)) +return ret; + + ret = -EPERM ;write_lock_irq (&tasklist_lock);/* Are we already being traced? */ if (!task_ptrace (current)) {
kernel/ptrace.c bool ptrace_may_access(struct task_struc · line 198
Quoted 6 lines
kernel/ptrace.c renamed from kernel/ptrace.c~3_STRUCT +6
-0+task_lock (tsk); +if (likely (!tsk->ptrace_ctx)) + tsk->ptrace_ctx = ptrace_ctx; +else +kfree (ptrace_ctx); +task_unlock (tsk);
I don't think it's kosher call kfree with task_lock() held. The kfree case is a very unlikely path, so it can be:
task_lock(tsk);
if (likely(!tsk->ptrace_ctx)) {
tsk->ptrace_ctx = ptrace_ctx;
task_unlock(tsk);
return 0;
}
task_unlock(tsk);
kfree(ptrace_ctx);Thanks,
Roland
Oleg Nesterov
maintainerQuoted 9 lines · Roland McGrath
kernel/ptrace.c bool ptrace_may_access(struct task_struc · line 198
Quoted 7 lines
kernel/ptrace.c renamed from kernel/ptrace.c~3_STRUCT +6
-0+task_lock (tsk); +if (likely (!tsk->ptrace_ctx)) + tsk->ptrace_ctx = ptrace_ctx; +else +kfree (ptrace_ctx); +task_unlock (tsk);I don't think it's kosher call kfree with
task_lock()held.
Yes, it always makes sense to move as much as possible outside of spin_lock/etc, but
Quoted 1 line
The kfree case is a very unlikely path,
Exactly, this kfree() "never" runs under task_lock(), but the code looks a bit simpler and shorter.
Quoted 11 lines
so it can be:
task_lock(tsk); if (likely(!tsk->ptrace_ctx)) { tsk->ptrace_ctx = ptrace_ctx; task_unlock(tsk); return 0; } task_unlock(tsk); kfree(ptrace_ctx);
OK, will change.
Oleg.