Skip to content

Commit 5e517ef

Browse files
shroffnikawasaki
authored andcommitted
throtl: fix the race between submitting IO and setting cgroup.procs
The throttle test cases uses _throtl_issue_io function to submit IO to the device. This function typically runs in the background process however before this function starts execution and submit IO, we need to set the PID of the background process into cgroup.procs. The current implementation adds sleep 0.1 before _throtl_issue_io and it's assumed that during this sleep time of 0.1 second, we shall be able to write the PID of the background process to cgroup.procs. However this may not be always true as background process might starts running after sleep of 0.1 seconds (and hence start submitting IO) before we could actually write the PID of background process into cgroup.procs from the parent shell. This commit helps fix the above race condition by writing pid of the background/child process using $BASHPID into cgroup.procs. The $BASHPID returns the pid of the current bash process. So we leverage $BASHPID to first write the pid of the background/child job/process into cgroup.procs from within the child sub-shell and then start submitting IO. This way we eliminate the need of any communication between parent shell and the background/child shell process and that helps avoid the race. Signed-off-by: Nilay Shroff <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Signed-off-by: Shin'ichiro Kawasaki <[email protected]>
1 parent 65ea613 commit 5e517ef

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

tests/throtl/004

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ test() {
2121
_throtl_set_limits wbps=$((1024 * 1024))
2222

2323
{
24-
sleep 0.1
24+
echo "$BASHPID" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
2525
_throtl_issue_io write 10M 1
2626
} &
2727

28-
local pid=$!
29-
echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
30-
3128
sleep 0.6
3229
echo 0 > "/sys/kernel/config/nullb/$THROTL_DEV/power"
33-
wait "$pid"
30+
wait $!
3431

3532
_clean_up_throtl
3633
echo "Test complete"

tests/throtl/005

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ test() {
2020
_throtl_set_limits wbps=$((512 * 1024))
2121

2222
{
23-
sleep 0.1
23+
echo "$BASHPID" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
2424
_throtl_issue_io write 1M 1
2525
} &
2626

27-
local pid=$!
28-
echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
29-
3027
sleep 1
3128
_throtl_set_limits wbps=$((256 * 1024))
32-
wait $pid
29+
wait $!
3330
_throtl_remove_limits
3431

3532
_clean_up_throtl

tests/throtl/rc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,15 @@ _throtl_issue_io() {
9797
# IO and then print time elapsed to the second, blk-throttle limits should be
9898
# set before this function.
9999
_throtl_test_io() {
100-
local pid
101100

102101
{
103102
local rw=$1
104103
local bs=$2
105104
local count=$3
106105

107-
sleep 0.1
106+
echo "$BASHPID" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
108107
_throtl_issue_io "$rw" "$bs" "$count"
109108
} &
110109

111-
pid=$!
112-
echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
113-
wait $pid
110+
wait $!
114111
}

0 commit comments

Comments
 (0)