#!/bin/bash # # Reproducers for two problems in "cgroup: don't hold cgroup_mutex for # cgroup.kill and cgroup.freeze": # # 1. A migration that fails before it commits makes the task walk of # cgroup.kill and cgroup.freeze skip a task. # 2. A pid namespace init that moves itself during a cgroup.kill written # from an ancestor pid namespace survives the kill. # # Needs a kernel with debug-delays.diff applied, cpuset enabled, and root. # Run it in a scratch VM. Each case has a control run that must behave. P=/sys/module/kernel/parameters CG=/run/cgtest mkdir -p $CG mount -t cgroup2 none $CG || exit 1 alive() { [ -d /proc/$1 ] && ! grep -q '^State:.*Z' /proc/$1/status; } utime() { awk '{print $14}' /proc/$1/stat; } runs() { local a=$(utime $1) sleep 1 local b=$(utime $1) [ "$b" -gt "$a" ] && echo "running" || echo "not running" } # D/e is a cgroup that every attach fails on with ENOSPC: its cpuset has no # effective CPUs because the sibling partition D/p took all of D's. echo +cpuset > $CG/cgroup.subtree_control mkdir $CG/D echo 0-1 > $CG/D/cpuset.cpus echo root > $CG/D/cpuset.cpus.partition echo +cpuset > $CG/D/cgroup.subtree_control mkdir $CG/D/p $CG/D/e echo 0-1 > $CG/D/p/cpuset.cpus echo root > $CG/D/p/cpuset.cpus.partition sleep 1000 & probe=$! if echo $probe > $CG/D/e/cgroup.procs 2>/dev/null; then echo "SETUP: attaching to D/e did not fail, case 1 is void" fi kill -9 $probe echo "=== 1a. cgroup.kill, failed migration during the task walk" for with_mig in 0 1; do mkdir -p $CG/S/x sleep 1000 & t=$! echo $t > $CG/S/x/cgroup.procs echo 2000 > $P/cgkill_dbg_iter_ms echo 1 > $CG/S/cgroup.kill & w=$! sleep 3 # the walk of S is done, the iterator of S/x has started [ $with_mig = 1 ] && echo $t > $CG/D/e/cgroup.procs 2>/dev/null wait $w echo 0 > $P/cgkill_dbg_iter_ms sleep 0.5 if alive $t; then echo "failed migration=$with_mig: task SURVIVED, S $(grep populated $CG/S/cgroup.events)" else echo "failed migration=$with_mig: task killed" fi kill -9 $t 2>/dev/null sleep 0.3 rmdir $CG/S/x $CG/S done echo "=== 1b. cgroup.freeze, failed migration during the task walk" for with_mig in 0 1; do mkdir -p $CG/G/x bash -c 'while :; do :; done' & t=$! echo $t > $CG/G/x/cgroup.procs echo 2000 > $P/cgfreeze_dbg_iter_ms echo 1 > $CG/G/cgroup.freeze & w=$! sleep 3 [ $with_mig = 1 ] && echo $t > $CG/D/e/cgroup.procs 2>/dev/null wait $w echo 0 > $P/cgfreeze_dbg_iter_ms sleep 5 echo "failed migration=$with_mig: freeze=$(cat $CG/G/cgroup.freeze), G $(grep frozen $CG/G/cgroup.events), task $(runs $t)" # freeze it fully, then the same for the thaw echo 0 > $CG/G/cgroup.freeze sleep 0.5 echo 1 > $CG/G/cgroup.freeze sleep 1 echo 2000 > $P/cgfreeze_dbg_iter_ms echo 0 > $CG/G/cgroup.freeze & w=$! sleep 3 [ $with_mig = 1 ] && echo $t > $CG/D/e/cgroup.procs 2>/dev/null wait $w echo 0 > $P/cgfreeze_dbg_iter_ms sleep 5 echo "failed migration=$with_mig: freeze=$(cat $CG/G/cgroup.freeze), G/x $(grep frozen $CG/G/x/cgroup.events), task $(runs $t)" kill -9 $t 2>/dev/null sleep 0.3 rmdir $CG/G/x $CG/G done echo "=== 2. cgroup.kill, pid namespace init moved during the kill" for mover in host init; do mkdir -p $CG/C/a $CG/C/b sleep 1000 & echo $! > $CG/C/a/cgroup.procs if [ $mover = init ]; then ( echo $BASHPID > $CG/C/b/cgroup.procs exec unshare -fp bash -c "sleep 4.5; echo 1 > $CG/C/a/cgroup.procs; sleep 1000; true" ) & else ( echo $BASHPID > $CG/C/b/cgroup.procs exec unshare -fp bash -c "sleep 1000; true" ) & fi sleep 0.5 init= for p in $(cat $CG/C/b/cgroup.procs); do grep -q -P '^NSpid:\t\d+\t1$' /proc/$p/status && init=$p done # sleep after each cgroup of the walk: C, then C/a, then C/b echo 3000 > $P/cgkill_dbg_cg_ms echo 1 > $CG/C/cgroup.kill & w=$! if [ $mover = host ]; then sleep 4 echo $init > $CG/C/a/cgroup.procs fi wait $w echo 0 > $P/cgkill_dbg_cg_ms sleep 0.5 if alive $init; then echo "moved by $mover: init SURVIVED in $(cat /proc/$init/cgroup), C $(grep populated $CG/C/cgroup.events)" else echo "moved by $mover: init killed" fi echo 1 > $CG/C/cgroup.kill sleep 0.5 rmdir $CG/C/a $CG/C/b $CG/C done