Skip to contentSkip to conversation

throtl: fix IO block-size and race while submitting IO

PATCH Superseded Nilay Shroff 4 participants 9 messages Dec 17, 2024 – Dec 18, 2024 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover throtl: fix IO block-size and race while submitting IO

Hi,

The first patch fixes the IO block-size used for submitting IO depending on the throttle device max-sectors settings.

The second patch fixes the potential race condition between submitting IO and setting PID of the background IO process to cgroup.procs.

Nilay Shroff (2):
throtl/002: calculate block-size based on device max-sectors setting
throtl: fix the race between submitting IO and setting cgroup.procs

 tests/throtl/002 | 14 ++++++++++----
 tests/throtl/004 |  8 +++++++-
 tests/throtl/005 |  9 ++++++++-
 tests/throtl/rc  | 13 ++++++++++++-
 4 files changed, 37 insertions(+), 7 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/2 throtl/002: calculate block-size based on device max-sectors setting

The commit 60fa2e3ff3ab ("update max_sectors setting") added max-sectors while setting up throttle device. So now we should also calculate block- size which matches the wiops. Typically, size of each IO which is submitted to the block device depends on the max-sectors setting of the block device. For example setting max-sectors to 128 results into 64kb of max. IO size which should be used for sending read/write command to the device. So take into account the max-sectors-kb and wiops settings and calculate the appropriate block-size which is then used to issue IO to the block device. This change would result in always submitting 256 I/O read/write commands to block device.

Without this change on a system with 64k PAGE SIZE, using block-size of 1M would result in 16 I/O being submitted to the device and this operation may finish in a fraction of a section and result in test failure. However the intent of this test case is that we want to test submitting 256 I/O after setting wiops limit to 256.

---
 tests/throtl/002 | 14 ++++++++++----
 tests/throtl/rc  |  4 ++++
 2 files changed, 14 insertions(+), 4 deletions(-)
tests/throtl/002 +10 -4
index 185e66b..02b0969 100755
@@ -14,6 +14,9 @@ test() {
 	echo "Running ${TEST_NAME}"
 
 	local page_size max_secs
+	local io_size_kb block_size
+	local iops=256
+
 	page_size=$(getconf PAGE_SIZE)
 	max_secs=$((page_size / 512))
 
@@ -21,12 +24,15 @@ test() {
 		return 1;
 	fi
 
-	_throtl_set_limits wiops=256
-	_throtl_test_io write 1M 1
+	io_size_kb=$(($(_throtl_get_max_io_size) * 1024))
+	block_size=$((iops * io_size_kb))
+
+	_throtl_set_limits wiops="${iops}"
+	_throtl_test_io write "${block_size}" 1
 	_throtl_remove_limits
 
-	_throtl_set_limits riops=256
-	_throtl_test_io read 1M 1
+	_throtl_set_limits riops="${iops}"
+	_throtl_test_io read "${block_size}" 1
 	_throtl_remove_limits
 
 	_clean_up_throtl
tests/throtl/rc +4 -0
index 9c264bd..330e6b9 100644
@@ -71,6 +71,10 @@ _throtl_remove_limits() {
 		"$CGROUP2_DIR/$THROTL_DIR/io.max"
 }
 
+_throtl_get_max_io_size() {
+	cat "/sys/block/$THROTL_DEV/queue/max_sectors_kb"
+}
+
 _throtl_issue_io() {
 	local start_time
 	local end_time
Signature · 1 line
2.45.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH blktests 1/2] throtl/002: calculate block-size based on device max-sectors setting

Quoted 17 lines · Nilay Shroff

The commit 60fa2e3ff3ab ("update max_sectors setting") added max-sectors while setting up throttle device. So now we should also calculate block- size which matches the wiops. Typically, size of each IO which is submitted to the block device depends on the max-sectors setting of the block device. For example setting max-sectors to 128 results into 64kb of max. IO size which should be used for sending read/write command to the device. So take into account the max-sectors-kb and wiops settings and calculate the appropriate block-size which is then used to issue IO to the block device. This change would result in always submitting 256 I/O read/write commands to block device.

Without this change on a system with 64k PAGE SIZE, using block-size of 1M would result in 16 I/O being submitted to the device and this operation may finish in a fraction of a section and result in test failure. However the intent of this test case is that we want to test submitting 256 I/O after setting wiops limit to 256.

Quoted 6 lines
---
  tests/throtl/002 | 14 ++++++++++----
  tests/throtl/rc  |  4 ++++
  2 files changed, 14 insertions(+), 4 deletions(-)

LGTM

Quoted 50 lines
tests/throtl/002 +10 -4
index 185e66b..02b0969 100755
@@ -14,6 +14,9 @@ test() {
  	echo "Running ${TEST_NAME}"
 
  	local page_size max_secs
+	local io_size_kb block_size
+	local iops=256
+
  	page_size=$(getconf PAGE_SIZE)
  	max_secs=$((page_size / 512))
 
@@ -21,12 +24,15 @@ test() {
  		return 1;
  	fi
 
-	_throtl_set_limits wiops=256
-	_throtl_test_io write 1M 1
+	io_size_kb=$(($(_throtl_get_max_io_size) * 1024))
+	block_size=$((iops * io_size_kb))
+
+	_throtl_set_limits wiops="${iops}"
+	_throtl_test_io write "${block_size}" 1
  	_throtl_remove_limits
 
-	_throtl_set_limits riops=256
-	_throtl_test_io read 1M 1
+	_throtl_set_limits riops="${iops}"
+	_throtl_test_io read "${block_size}" 1
  	_throtl_remove_limits
 
  	_clean_up_throtl
tests/throtl/rc +4 -0
index 9c264bd..330e6b9 100644
@@ -71,6 +71,10 @@ _throtl_remove_limits() {
  		"$CGROUP2_DIR/$THROTL_DIR/io.max"
  }
 
+_throtl_get_max_io_size() {
+	cat "/sys/block/$THROTL_DEV/queue/max_sectors_kb"
+}
+
  _throtl_issue_io() {
  	local start_time
  	local end_time
#3 lore raw

PATCH 2/2 throtl: fix the race between submitting IO and setting cgroup.procs

In reply to Nilay Shroff

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.

This commit helps fix the above race condition by touching a temp file. The the existence of the temp file is then polled by the background process at regular interval. Until the temp file is created, the background process would not forward progress and starts submitting IO and from the main thread we'd touch temp file only after we write PID of the background process into cgroup.procs.

---
 tests/throtl/004 | 8 +++++++-
 tests/throtl/005 | 9 ++++++++-
 tests/throtl/rc  | 9 ++++++++-
 3 files changed, 23 insertions(+), 3 deletions(-)
tests/throtl/004 +7 -1
index 6e28612..44b33ec 100755
@@ -21,12 +21,18 @@ test() {
 	_throtl_set_limits wbps=$((1024 * 1024))
 
 	{
-		sleep 0.1
+                while true; do
+                        if [[ -e "$TMPDIR/test-io" ]]; then
+                                break
+                        fi
+                        sleep 0.1
+                done
 		_throtl_issue_io write 10M 1
 	} &
 
 	local pid=$!
 	echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
+	touch "$TMPDIR/test-io"
 
 	sleep 0.6
 	echo 0 > "/sys/kernel/config/nullb/$THROTL_DEV/power"
tests/throtl/005 +8 -1
index 0778258..4797ea3 100755
@@ -20,12 +20,19 @@ test() {
 	_throtl_set_limits wbps=$((512 * 1024))
 
 	{
-		sleep 0.1
+                while true; do
+                        if [[ -e "$TMPDIR/test-io" ]]; then
+                                break
+                        fi
+                        sleep 0.1
+                done
+
 		_throtl_issue_io write 1M 1
 	} &
 
 	local pid=$!
 	echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
+	touch "$TMPDIR/test-io"
 
 	sleep 1
 	_throtl_set_limits wbps=$((256 * 1024))
tests/throtl/rc +8 -1
index 330e6b9..a9c1889 100644
@@ -104,11 +104,18 @@ _throtl_test_io() {
 		local bs=$2
 		local count=$3
 
-		sleep 0.1
+		while true; do
+			if [[ -e "$TMPDIR/test-io" ]]; then
+				break
+			fi
+			sleep 0.1
+		done
 		_throtl_issue_io "$rw" "$bs" "$count"
 	} &
 
 	pid=$!
 	echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
+	touch "$TMPDIR/test-io"
 	wait $pid
+	rm "$TMPDIR/test-io"
 }
Signature · 1 line
2.45.2
#4 lore raw

Re: [PATCH blktests 2/2] throtl: fix the race between submitting IO and setting cgroup.procs

Hi,

Quoted 6 lines · Nilay Shroff

This commit helps fix the above race condition by touching a temp file. The the existence of the temp file is then polled by the background process at regular interval. Until the temp file is created, the background process would not forward progress and starts submitting IO and from the main thread we'd touch temp file only after we write PID of the background process into cgroup.procs.

It's right sleep 0.1 is not appropriate here, and this can work. However, I think reading cgroup.procs directly is better, something like following:

_throtl_test_io() {
- local pid
+ local pid="none"

        {
                local rw=$1
                local bs=$2
                local count=$3

- sleep 0.1
+ while ! cat $CGROUP2_DIR/$THROTL_DIR/cgroup.procs | grep $pid; do
+ sleep 0.1

                _throtl_issue_io "$rw" "$bs" "$count"
        } &

Thanks,
Kuai

#5 lore raw

Re: [PATCH blktests 2/2] throtl: fix the race between submitting IO and setting cgroup.procs

Quoted 6 lines · Yu Kuai

-               sleep 0.1
+               while ! cat $CGROUP2_DIR/$THROTL_DIR/cgroup.procs | grep $pid; do
+                       sleep 0.1
                _throtl_issue_io "$rw" "$bs" "$count"
        } &

If this approach is used, please add the options -q (quiet) and -w (match whole words only) to the grep command.

Thanks,

Bart.

#6 lore raw

Re: [PATCH blktests 2/2] throtl: fix the race between submitting IO and setting cgroup.procs

In reply to Yu Kuai
Quoted 23 lines · Yu Kuai

Hi,

Quoted 6 lines · Nilay Shroff

This commit helps fix the above race condition by touching a temp file. The the existence of the temp file is then polled by the background process at regular interval. Until the temp file is created, the background process would not forward progress and starts submitting IO and from the main thread we'd touch temp file only after we write PID of the background process into cgroup.procs.

It's right sleep 0.1 is not appropriate here, and this can work. However, I think reading cgroup.procs directly is better, something like following:

 _throtl_test_io() {
-       local pid
+       local pid="none"

        {
                local rw=$1
                local bs=$2
                local count=$3

Quoted 5 lines

-               sleep 0.1
+               while ! cat $CGROUP2_DIR/$THROTL_DIR/cgroup.procs | grep $pid; do +                       sleep 0.1
                _throtl_issue_io "$rw" "$bs" "$count"
        } &

Thank you for your review and suggestion!

However, IMO this may not work always because the issue here's that the @pid is local variable and when the shell starts the background job/process, typically, all local variables are copied to the new job. Henceforth, any update made to @pid in the parent shell would not be visible to the background job.
I think, for IPC between parent shell and background job, we may use temp file, named pipe or signals. As file is the easiest and simplest mechanism, for this simple test I preferred using file.

Thanks,
--Nilay

#7 lore raw

Re: [PATCH blktests 2/2] throtl: fix the race between submitting IO and setting cgroup.procs

Quoted 34 lines · Nilay Shroff
Quoted 23 lines · Yu Kuai

Hi,

Quoted 6 lines · Nilay Shroff

This commit helps fix the above race condition by touching a temp file. The the existence of the temp file is then polled by the background process at regular interval. Until the temp file is created, the background process would not forward progress and starts submitting IO and from the main thread we'd touch temp file only after we write PID of the background process into cgroup.procs.

It's right sleep 0.1 is not appropriate here, and this can work. However, I think reading cgroup.procs directly is better, something like following:

 _throtl_test_io() {
-       local pid
+       local pid="none"

        {
                local rw=$1
                local bs=$2
                local count=$3

Quoted 5 lines

-               sleep 0.1
+               while ! cat $CGROUP2_DIR/$THROTL_DIR/cgroup.procs | grep $pid; do +                       sleep 0.1
                _throtl_issue_io "$rw" "$bs" "$count"
        } &

Thank you for your review and suggestion!

Quoted 7 lines

However, IMO this may not work always because the issue here's that the @pid is local variable and when the shell starts the background job/process, typically, all local variables are copied to the new job. Henceforth, any update made to @pid in the parent shell would not be visible to the background job.
I think, for IPC between parent shell and background job, we may use temp file, named pipe or signals. As file is the easiest and simplest mechanism, for this simple test I preferred using file.

How about to refer to $BASHPID in the background job sub-shell? It shows the PID of the background job, so we don't need IPC to pass the PID.

I think the hunk like below can do the trick, hopefully. If it works, it would be cleaner to factor out the while loop to a helper function, like _throtl_wait_for_cgroup_procs() or something.

tests/throtl/004 +4 -0
index 6e28612..0a16764 100755
@@ -22,6 +22,10 @@ test() {
 
        {
                sleep 0.1
+               while ! grep --quiet --word-regexp "$BASHPID" \
+                       "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"; do
+                       sleep 0.1
+               done
                _throtl_issue_io write 10M 1
        } &
#8 lore raw

Re: [PATCH blktests 2/2] throtl: fix the race between submitting IO and setting cgroup.procs

Quoted 56 lines · Shinichiro Kawasaki
Quoted 34 lines · Nilay Shroff
Quoted 23 lines · Yu Kuai

Hi,

Quoted 6 lines · Nilay Shroff

This commit helps fix the above race condition by touching a temp file. The the existence of the temp file is then polled by the background process at regular interval. Until the temp file is created, the background process would not forward progress and starts submitting IO and from the main thread we'd touch temp file only after we write PID of the background process into cgroup.procs.

It's right sleep 0.1 is not appropriate here, and this can work. However, I think reading cgroup.procs directly is better, something like following:

 _throtl_test_io() {
-       local pid
+       local pid="none"

        {
                local rw=$1
                local bs=$2
                local count=$3

Quoted 5 lines

-               sleep 0.1
+               while ! cat $CGROUP2_DIR/$THROTL_DIR/cgroup.procs | grep $pid; do +                       sleep 0.1
                _throtl_issue_io "$rw" "$bs" "$count"
        } &

Thank you for your review and suggestion!

Quoted 7 lines

However, IMO this may not work always because the issue here's that the @pid is local variable and when the shell starts the background job/process, typically, all local variables are copied to the new job. Henceforth, any update made to @pid in the parent shell would not be visible to the background job.
I think, for IPC between parent shell and background job, we may use temp file, named pipe or signals. As file is the easiest and simplest mechanism, for this simple test I preferred using file.

How about to refer to $BASHPID in the background job sub-shell? It shows the PID of the background job, so we don't need IPC to pass the PID.

I think the hunk like below can do the trick, hopefully. If it works, it would be cleaner to factor out the while loop to a helper function, like _throtl_wait_for_cgroup_procs() or something.

tests/throtl/004 +0 -0
index 6e28612..0a16764 100755

tests/throtl/004 test() { · line 30

Quoted 9 lines
tests/throtl/004 +4 -0
@@ -22,6 +22,10 @@ test() {
 
        {
                sleep 0.1
+               while ! grep --quiet --word-regexp "$BASHPID" \
+                       "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"; do
+                       sleep 0.1
+               done
                _throtl_issue_io write 10M 1
        } &

I like the idea of using $BASHPID. I just tried it and it works!!

In fact, I have now further simplified the logic such that we don't need IPC between parent and child sub-shell:) Please find below the diff:

tests/throtl/004 +2 -11
index 44b33ec..d1461b9 100755
@@ -21,22 +21,13 @@ test() {
        _throtl_set_limits wbps=$((1024 * 1024))
 
        {
-                while true; do
-                        if [[ -e "$TMPDIR/test-io" ]]; then
-                                break
-                        fi
-                        sleep 0.1
-                done
+               echo "$BASHPID" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
                _throtl_issue_io write 10M 1
        } &
 
-       local pid=$!
-       echo "$pid" > "$CGROUP2_DIR/$THROTL_DIR/cgroup.procs"
-       touch "$TMPDIR/test-io"
-
        sleep 0.6
        echo 0 > "/sys/kernel/config/nullb/$THROTL_DEV/power"
-       wait "$pid"
+       wait $!
 
 

As shown above, we could now directly write the PID of the background job into the cgroup.procs and submit IO. So we don't require IPC between parent and child sub-shell.

I'd spin a new patch with the above change and submit.

Thanks,
--Nilay