Introduce CABA helper process tree
Subsystems (7)
cover Introduce CABA helper process tree
Please see "Add CABA tree to task_struct" for deeper explanation, and "tests: Add CABA selftest" for a small test and an actual case for which we might need CABA.
Probably the original problem of restoring process tree with complex sessions can be resolved by allowing sessions copying, like we do for process group, but I'm not sure if that would be too secure to do it, and if there would not be another similar resource in future.
We can use CABA not only for CRIU for restoring processes, in normal life when processes detach CABA will help to understand from which place in process tree they were originally started from sshd/crond or something else.
Hope my idea is not completely insane =)
Pavel Tikhomirov (2):
Add CABA tree to task_struct
tests: Add CABA selftest
arch/ia64/kernel/mca.c | 3 + fs/exec.c | 1 + fs/proc/array.c | 18 + include/linux/sched.h | 7 + init/init_task.c | 3 + kernel/exit.c | 50 ++- kernel/fork.c | 4 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/caba/.gitignore | 1 + tools/testing/selftests/caba/Makefile | 7 + tools/testing/selftests/caba/caba_test.c | 501 +++++++++++++++++++++++ tools/testing/selftests/caba/config | 1 + 12 files changed, 591 insertions(+), 6 deletions(-) create mode 100644 tools/testing/selftests/caba/.gitignore create mode 100644 tools/testing/selftests/caba/Makefile create mode 100644 tools/testing/selftests/caba/caba_test.c create mode 100644 tools/testing/selftests/caba/config
Signature · 1 line
2.35.3
PATCH 1/2 Add CABA tree to task_struct
In linux after parent (father) process dies, children processes are moved (reparented) to a reaper process. Roughly speaking:
1) If father has other yet alive thread, this thread would be a reaper.
2) Else if there is father's ancestor (with no pidns level change in the middle), which has PR_SET_CHILD_SUBREAPER set, this ancestor would be a reaper.
3) Else father's pidns init would be a reaper for fathers children.
The problem with this for CRIU is that when CRIU comes to dump processes it does not know the order in which processes and their resources were created. And processes can have resources which a) can only be inherited when we clone processes, b) can only be created by specific processes and c) are shared between several processes (the example of such a resource is process session). For such resources CRIU restore would need to re-invent such order of process creation which at the same time creates the desired process tree topology and allows to inherit all resources right.
When process reparenting involves child-sub-reapers one can drastically mix processes in process tree so that it is not obvious how to restore everything right.
So this is what we came up with to help CRIU to overcome this problem:
CABA = Closest Alive Born Ancestor
CABD = Closest Alive Born Descendant
We want to put processes in one more tree - CABA tree. This tree is not affecting reparenting or process creation in any way except for providing a new information to CRIU so that it can understand from where the reparented child had reparented, though original father is already dead and probably a fathers father too, we can still have information about the process which is still alive and was originally a parent of process sequence (of already dead processes) which lead to us - CABA.
--- arch/ia64/kernel/mca.c | 3 +++ fs/exec.c | 1 + fs/proc/array.c | 18 +++++++++++++++ include/linux/sched.h | 7 ++++++ init/init_task.c | 3 +++ kernel/exit.c | 50 +++++++++++++++++++++++++++++++++++++----- kernel/fork.c | 4 ++++ 7 files changed, 80 insertions(+), 6 deletions(-)
arch/ia64/kernel/mca.c +3 -0
@@ -1793,6 +1793,9 @@ format_mca_init_stack(void *mca_data, unsigned long offset, p->parent = p->real_parent = p->group_leader = p;INIT_LIST_HEAD (&p->children);INIT_LIST_HEAD (&p->sibling); + p->caba = p->real_parent; +INIT_LIST_HEAD (&p->cabds); +INIT_LIST_HEAD (&p->cabd);strncpy (p->comm, type,sizeof (p->comm)-1 ); }
fs/exec.c +1 -0
@@ -1136,6 +1136,7 @@ static int de_thread(struct task_struct *tsk)list_replace_rcu (&leader->tasks, &tsk->tasks);list_replace_init (&leader->sibling, &tsk->sibling); +list_replace_init (&leader->cabd, &tsk->cabd); tsk->group_leader = tsk; leader->group_leader = tsk;
fs/proc/array.c +18 -0
@@ -151,11 +151,26 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,const struct cred *cred;pid_t ppid, tpid =0 , tgid, ngid;unsigned int max_fds =0 ; +struct task_struct *caba; +struct pid *caba_pid; +int caba_level =0 ; +pid_t caba_pids[MAX_PID_NS_LEVEL ] = {};rcu_read_lock (); ppid =pid_alive (p) ?task_tgid_nr_ns (rcu_dereference (p->real_parent), ns) :0 ; +#ifdef CONFIG_PID_NS + caba =rcu_dereference (p->caba); + caba_pid =get_task_pid (caba,PIDTYPE_PID ); +if (caba_pid) { + caba_level = caba_pid->level; +for (g = ns->level; g <= caba_level; g++) + caba_pids[g] =task_pid_nr_ns (caba, caba_pid->numbers[g].ns); +put_pid (caba_pid); + } +#endif + tracer =ptrace_parent (p);if (tracer) tpid =task_pid_nr_ns (tracer, ns);
@@ -214,6 +229,9 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,seq_puts (m,"\nNSsid:" );for (g = ns->level; g <= pid->level; g++)seq_put_decimal_ull (m,"\t" ,task_session_nr_ns (p, pid->numbers[g].ns)); +seq_puts (m,"\nNScaba:" ); +for (g = ns->level; g <= caba_level; g++) +seq_put_decimal_ull (m,"\t" , caba_pids[g]);#endif seq_putc (m,'\n' ); }
include/linux/sched.h +7 -0
@@ -973,6 +973,13 @@ struct task_struct {struct list_head sibling;struct task_struct *group_leader; +/* Closest Alive Born Ancestor process: */ +struct task_struct __rcu *caba; + +/* Closest Alive Born Descendants list: */ +struct list_head cabds; +struct list_head cabd; +/* * 'ptraced' is the list of tasks this task is using ptrace() on. *
init/init_task.c +3 -0
@@ -109,6 +109,9 @@ struct task_struct init_task .children = LIST_HEAD_INIT(init_task.children), .sibling = LIST_HEAD_INIT(init_task.sibling), .group_leader = &init_task, + .caba = &init_task, + .cabds = LIST_HEAD_INIT(init_task.cabds), + .cabd =LIST_HEAD_INIT (init_task.cabd),RCU_POINTER_INITIALIZER (real_cred, &init_cred),RCU_POINTER_INITIALIZER (cred, &init_cred), .comm = INIT_TASK_COMM,
kernel/exit.c +44 -6
90 lines not on this page · raw message
kernel/fork.c +4 -0
@@ -2123,6 +2123,8 @@ static __latent_entropy struct task_struct *copy_process( p->flags |=PF_FORKNOEXEC ;INIT_LIST_HEAD (&p->children);INIT_LIST_HEAD (&p->sibling); +INIT_LIST_HEAD (&p->cabds); +INIT_LIST_HEAD (&p->cabd);rcu_copy_process (p); p->vfork_done =NULL ;spin_lock_init (&p->alloc_lock);
@@ -2386,6 +2388,7 @@ static __latent_entropy struct task_struct *copy_process( p->parent_exec_id = current->self_exec_id; p->exit_signal = args->exit_signal; } + p->caba = p->real_parent;klp_copy_process (p);
@@ -2437,6 +2440,7 @@ static __latent_entropy struct task_struct *copy_process( p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || p->real_parent->signal->is_child_subreaper;list_add_tail (&p->sibling, &p->real_parent->children); +list_add_tail (&p->cabd, &p->caba->cabds);list_add_tail_rcu (&p->tasks, &init_task.tasks);attach_pid (p,PIDTYPE_TGID );attach_pid (p,PIDTYPE_PGID );
Signature · 1 line
2.35.3
kernel test robot
build botRe: [PATCH 1/2] Add CABA tree to task_struct
Thank you for the patch! Perhaps something to improve:
Hi Pavel,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on kees/for-next/execve tip/sched/core linus/master v5.19-rc1 next-20220610] [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/Pavel-Tikhomirov/Introduce-CABA-helper-process-tree/20220611-003433 base: https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next config: i386-randconfig-a001 (https://download.01.org/0day-ci/archive/20220611/[email protected]/config) compiler: gcc-11 (Debian 11.3.0-3) 11.3.0 reproduce (this is a W=1 build): # https://github.com/intel-lab-lkp/linux/commit/0875a2bed5ff95643c487dfcc28a550db06ea418 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Pavel-Tikhomirov/Introduce-CABA-helper-process-tree/20220611-003433 git checkout 0875a2bed5ff95643c487dfcc28a550db06ea418 # save the config file mkdir build_dir && cp config build_dir/.config make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash fs/proc/
If you fix the issue, kindly add following tag where applicable
All warnings (new ones prefixed by >>):
fs/proc/array.c: In function 'task_state':
>> fs/proc/array.c:157:15: warning: unused variable 'caba_pids' [-Wunused-variable]
157 | pid_t caba_pids[MAX_PID_NS_LEVEL] = {};
| ^~~~~~~~~
>> fs/proc/array.c:156:13: warning: unused variable 'caba_level' [-Wunused-variable]
156 | int caba_level = 0;
| ^~~~~~~~~~
>> fs/proc/array.c:155:21: warning: unused variable 'caba_pid' [-Wunused-variable]
155 | struct pid *caba_pid;
| ^~~~~~~~
>> fs/proc/array.c:154:29: warning: unused variable 'caba' [-Wunused-variable]
154 | struct task_struct *caba;
| ^~~~vim +/caba_pids +157 fs/proc/array.c
143
144 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
145 struct pid *pid, struct task_struct *p)
146 {
147 struct user_namespace *user_ns = seq_user_ns(m);
148 struct group_info *group_info;
149 int g, umask = -1;
150 struct task_struct *tracer;
151 const struct cred *cred;
152 pid_t ppid, tpid = 0, tgid, ngid;
153 unsigned int max_fds = 0;
> 154 struct task_struct *caba;
> 155 struct pid *caba_pid;
> 156 int caba_level = 0;
> 157 pid_t caba_pids[MAX_PID_NS_LEVEL] = {}; 158
159 rcu_read_lock();
160 ppid = pid_alive(p) ?
161 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
162
Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
PATCH 2/2 tests: Add CABA selftest
In reply to Pavel TikhomirovThis test creates a "tricky" example process tree where session leaders of two sessions are children of pid namespace init, also they have their own children, leader of session A has child with session B and leader from session B has child with session A.
We check that Closest Alive Born Ancestor tree is right for this case. This case illustrates how CABA tree helps to understand order of creation between sessions.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/caba/.gitignore | 1 + tools/testing/selftests/caba/Makefile | 7 + tools/testing/selftests/caba/caba_test.c | 501 +++++++++++++++++++++++ tools/testing/selftests/caba/config | 1 + 5 files changed, 511 insertions(+) create mode 100644 tools/testing/selftests/caba/.gitignore create mode 100644 tools/testing/selftests/caba/Makefile create mode 100644 tools/testing/selftests/caba/caba_test.c create mode 100644 tools/testing/selftests/caba/config
tools/testing/selftests/Makefile +1 -0
@@ -3,6 +3,7 @@ TARGETS += alsa TARGETS += arm64 TARGETS += bpf TARGETS += breakpoints +TARGETS += caba TARGETS += capabilities TARGETS += cgroup TARGETS += clone3
tools/testing/selftests/caba/.gitignore new file +1 -0
@@ -0,0 +1 @@ +caba_test
tools/testing/selftests/caba/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for caba selftests. +CFLAGS = -g -I../../../../usr/include/ -Wall -O2 + +TEST_GEN_FILES += caba_test + +include ../lib.mk
tools/testing/selftests/caba/caba_test.c new file +501 -0
501 lines not on this page · raw message
tools/testing/selftests/caba/config new file +1 -0
@@ -0,0 +1 @@ +CONFIG_PID_NS=y
Signature · 1 line
2.35.3
Quoted 57 lines · Pavel Tikhomirov
Please see "Add CABA tree to
task_struct" for deeper explanation, and "tests: Add CABA selftest" for a small test and an actual case for which we might need CABA.Probably the original problem of restoring process tree with complex sessions can be resolved by allowing sessions copying, like we do for process group, but I'm not sure if that would be too secure to do it, and if there would not be another similar resource in future.
We can use CABA not only for CRIU for restoring processes, in normal life when processes detach CABA will help to understand from which place in process tree they were originally started from sshd/crond or something else.
Hope my idea is not completely insane =)
CC Eric Biederman <[email protected]>CC Kees Cook <[email protected]>CC Alexander Viro <[email protected]>CC Ingo Molnar <[email protected]>CC Peter Zijlstra <[email protected]>CC Juri Lelli <[email protected]>CC Vincent Guittot <[email protected]>CC Dietmar Eggemann <[email protected]>CC Steven Rostedt <[email protected]>CC Ben Segall <[email protected]>CC Mel Gorman <[email protected]>CC Daniel Bristot de Oliveira <[email protected]>CC Valentin Schneider <[email protected]>CC Andrew Morton <[email protected]>Pavel Tikhomirov (2):
Add CABA tree totask_struct
tests: Add CABA selftestarch/ia64/kernel/mca.c | 3 + fs/exec.c | 1 + fs/proc/array.c | 18 + include/linux/sched.h | 7 + init/init_task.c | 3 + kernel/exit.c | 50 ++- kernel/fork.c | 4 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/caba/.gitignore | 1 + tools/testing/selftests/caba/Makefile | 7 + tools/testing/selftests/caba/caba_test.c | 501 +++++++++++++++++++++++ tools/testing/selftests/caba/config | 1 + 12 files changed, 591 insertions(+), 6 deletions(-)create mode 100644
tools/testing/selftests/caba/.gitignorecreate mode 100644tools/testing/selftests/caba/Makefilecreate mode 100644tools/testing/selftests/caba/caba_test.ccreate mode 100644tools/testing/selftests/caba/config
Signature · 2 lines
Best regards, Tikhomirov Pavel Software Developer, Virtuozzo.