Skip to contentSkip to conversation

cxl/memdev: Fix update-firmware --wait after a failed upload

PATCH New Unsung Lee 1 participant 4 messages Nov 21

Subsystems (2)
lore
Share & tools
lore

Unsung Lee

author
#0 lore raw

cover cxl/memdev: Fix update-firmware --wait after a failed upload

'cxl update-firmware --wait' does not return after an upload that was cancelled or failed, because it also waits for remaining_size to reach 0 and the kernel leaves remaining_size as it was on errors. Patch 2 waits on the loader status only and extends the cancel test to cover it. Patch 1 fixes the test helper that was meant to catch a hang like this but never times out. Patch 3 makes '-F <file> --wait' fail when the upload ended with data left, so a script can tell a failed update from a successful one.

Reproducer on a cxl_test or QEMU memdev:

  cxl update-firmware -F fw.bin mem0
  cxl update-firmware --cancel mem0      # while still transferring
  cxl update-firmware --wait mem0        # never returns before patch 2

Tested on a QEMU CXL type 3 memdev, 10 runs per case, each --wait under 'timeout 60' (exit code in parentheses):

  case                              v85         with this series
  normal upload with --wait         returns (0) returns (0)
  --cancel during transfer, --wait  timed out   returns (0)
  --wait alone after a cancel       timed out   returns (0)
  ->prepare() fails (busy), --wait  timed out   returns (1)

and with 'meson test cxl-update-firmware.sh' against cxl_test on lockdep and KASAN kernels: the series passes; the patched test with the v85 cxl/memdev.c fails at the new --wait check, and the test of patch 3 fails with the cxl/memdev.c of patch 2. The whole cxl test suite gives the same results with v85 and with this series.

An LLM-based tool was used to check that the experiments were safe to run and to drive the repeated test runs.

Patch 3 only sees failures that leave remaining_size non-zero, that is a ->prepare() failure or a failure or cancel during the transfer. A failure in the programming step leaves remaining_size at 0 and still exits 0. Reporting that needs the loader 'error' attribute ("programming:hw-error" etc.), which libcxl does not read; I can add an API such as cxl_memdev_fw_update_get_error() if that is the preferred direction. With several memdevs, the command still exits 0 when any of them succeeded, as for the other memdev commands.

Unsung Lee (3):
test/cxl-update-firmware: Make wait_complete() honor its timeout cxl/memdev: Stop waiting on remaining_size in update-firmware --wait cxl/memdev: Fail update-firmware --wait when the upload did not
complete

 Documentation/cxl/cxl-update-firmware.txt |  5 +++
 cxl/memdev.c                              | 15 ++++++--
 test/cxl-update-firmware.sh               | 42 +++++++++++++++++++----
 3 files changed, 54 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 test/cxl-update-firmware: Make wait_complete() honor its timeout

wait_complete() either sleeps and continues or breaks out of the loop, so the max_wait check after the if/else is never reached. A firmware update that never completes makes the test hang instead of failing.

Check the timeout before sleeping.

With the timeout enforced, the 15 second limit in test_nonblocking_update() leaves little margin: the 16 MiB upload there takes 9-10 seconds against cxl_test in a QEMU guest with lockdep, and 10.1-10.2 seconds with KASAN. Raise it to 30 seconds, about three times the measured time, which still catches a hang quickly.

Fixes 813809ed318f ("test/cxl-update-firmware: add a unit test for firmware update")
---
 test/cxl-update-firmware.sh | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
test/cxl-update-firmware.sh +4 -6
index f326868..100d15f 100755
@@ -61,17 +61,15 @@ wait_complete()
 	while true; do
 		json="$("$CXL" list -m "$mem" -F)"
 		in_prog="$(jq -r '.[].firmware.fw_update_in_progress' <<< "$json")"
-		if [[ $in_prog == "true" ]]; then
-			sleep 1
-			waited="$((waited + 1))"
-			continue
-		else
+		if [[ $in_prog != "true" ]]; then
 			break
 		fi
 		if (( waited == max_wait )); then
 			echo "completion timeout for $mem"
 			err "$LINENO"
 		fi
+		sleep 1
+		waited="$((waited + 1))"
 	done
 }
 
@@ -149,7 +147,7 @@ test_nonblocking_update()
 	mem="$(find_memdevs 1)"
 	json=$(do_update_fw -F "$file" "$mem")
 	validate_fw_update_in_progress "$json"
-	wait_complete "$mem" 15
+	wait_complete "$mem" 30
 	validate_fw_update_idle "$("$CXL" list -m "$mem" -F)"
 	check_sha "$mem" "$file"
 	rm "$file"
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/3 cxl/memdev: Stop waiting on remaining_size in update-firmware --wait

In reply to Unsung Lee

'cxl update-firmware --wait' polls until the firmware loader status is idle and remaining_size is 0. The kernel firmware upload core leaves remaining_size unmodified when an upload fails or is cancelled, "to provide additional information on errors" (fw_upload_main() in drivers/base/firmware_loader/sysfs_upload.c). After a failed or cancelled upload, status returns to idle while remaining_size stays non-zero until the next upload starts, so the loop never exits.

This is easy to hit: start an upload, cancel it with
'cxl update-firmware --cancel' while it is still transferring, then run 'cxl update-firmware --wait'. The same happens after a ->prepare() failure, where remaining_size is still the full image size, and with a plain '--wait' on a memdev whose previous upload failed.

Checking remaining_size is not needed to cover the start of an upload either. Writing 0 to 'loading' calls fw_upload_start(), which sets the status to receiving before the write returns, so the first poll after cxl_memdev_update_fw() cannot see a stale idle status.

Wait on the status only, and extend the cancel test to run --wait after the cancel.

Fixes 64ad46b4a147 ("cxl: add an update-firmware command")
---
 cxl/memdev.c                | 3 +--
 test/cxl-update-firmware.sh | 5 +++++
 2 files changed, 6 insertions(+), 2 deletions(-)
cxl/memdev.c +1 -2
index 6e44d15..7f0d021 100644
@@ -796,8 +796,7 @@ static int action_update_fw(struct cxl_memdev *memdev,
 	}
 
 	if (param.wait) {
-		while (cxl_memdev_fw_update_in_progress(memdev) ||
-		       cxl_memdev_fw_update_get_remaining(memdev) > 0)
+		while (cxl_memdev_fw_update_in_progress(memdev))
 			sleep(1);
 	}
 
test/cxl-update-firmware.sh +5 -0
index 100d15f..e93bb00 100755
@@ -181,6 +181,11 @@ test_cancel()
 	# cancellation is asynchronous, and the result looks the same as idle
 	wait_complete "$mem" 15
 	validate_fw_update_idle "$("$CXL" list -m "$mem" -F)"
+	# A cancel during the transfer leaves remaining_size non-zero,
+	# --wait must still return.
+	json="$(timeout 15 "$CXL" update-firmware -b "$CXL_TEST_BUS" --wait "$mem")" ||
+		err "$LINENO"
+	validate_fw_update_idle "$json"
 	# no need to check_sha
 	rm "$file"
 }
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/3 cxl/memdev: Fail update-firmware --wait when the upload did not complete

In reply to Unsung Lee

'cxl update-firmware -F <file> --wait' returns 0 even when the upload was cancelled or failed, so a script cannot tell a failed update from a successful one.

When an upload fails or is cancelled before all data is written, the kernel firmware upload core leaves remaining_size non-zero until the next upload starts (fw_upload_main() in drivers/base/firmware_loader/sysfs_upload.c). After waiting, report an error if remaining_size is still non-zero. This covers a ->prepare() failure, where remaining_size is the full image size, and a failure or cancel during the transfer. A failure after the transfer, in the programming step, still leaves remaining_size at 0 and is not detected; that would need the loader 'error' attribute, which libcxl does not read today.

Only check an upload started by the same command. With --wait alone the value may be left over from an earlier upload, and the command keeps returning 0 as before.

Add a test that cancels a blocking update and expects it to fail.

---
 Documentation/cxl/cxl-update-firmware.txt |  5 +++++
 cxl/memdev.c                              | 12 ++++++++++
 test/cxl-update-firmware.sh               | 27 +++++++++++++++++++++++
 3 files changed, 44 insertions(+)
Documentation/cxl/cxl-update-firmware.txt +5 -0
index b7342aa..5af6520 100644
@@ -76,6 +76,11 @@ include::bus-option.txt[]
 	it will initiate a wait on any current in-progress firmware
 	updates, and then return.
 
+	With a firmware-file, the command fails if the upload ends
+	before all of the firmware image was written to the device,
+	for example because it was cancelled or the device could not
+	start the transfer.
+
 include::verbose-option.txt[]
 
 include::../copyright.txt[]
cxl/memdev.c +12 -0
index 7f0d021..fe54258 100644
@@ -798,6 +798,18 @@ static int action_update_fw(struct cxl_memdev *memdev,
 	if (param.wait) {
 		while (cxl_memdev_fw_update_in_progress(memdev))
 			sleep(1);
+		/*
+		 * The kernel leaves remaining_size non-zero when an upload
+		 * fails or is cancelled before all data is written. Only check
+		 * an upload started here, a stale value may be from an earlier
+		 * one.
+		 */
+		if (param.fw_file && !rc &&
+		    cxl_memdev_fw_update_get_remaining(memdev) > 0) {
+			log_err(&ml, "%s: firmware update did not complete\n",
+				devname);
+			rc = -EIO;
+		}
 	}
 
 	flags = UTIL_JSON_FIRMWARE;
test/cxl-update-firmware.sh +27 -0
index e93bb00..d172994 100755
@@ -190,10 +190,37 @@ test_cancel()
 	rm "$file"
 }
 
+test_cancel_blocking()
+{
+	file="$(mk_fw_file 16)"
+	mem="$(find_memdevs 1)"
+	timeout 30 "$CXL" update-firmware -b "$CXL_TEST_BUS" -F "$file" --wait "$mem" &
+	pid=$!
+	waited=0
+	while [[ "$("$CXL" list -m "$mem" -F |
+		   jq -r '.[].firmware.fw_update_in_progress')" != "true" ]]; do
+		if (( waited == 10 )); then
+			err "$LINENO"
+		fi
+		sleep 1
+		waited="$((waited + 1))"
+	done
+	do_update_fw --cancel "$mem"
+	# the cancelled -F --wait must return, and must fail
+	wait_rc=0
+	wait "$pid" || wait_rc="$?"
+	if (( wait_rc == 0 || wait_rc == 124 )); then
+		err "$LINENO"
+	fi
+	validate_fw_update_idle "$("$CXL" list -m "$mem" -F)"
+	rm "$file"
+}
+
 test_blocking_update
 test_nonblocking_update
 test_multiple_memdev
 test_cancel
+test_cancel_blocking
 
 check_dmesg "$LINENO"
 modprobe -r cxl_test
Signature · 1 line
2.34.1