From: Alison Schofield <alison.schofi...@intel.com>

The CXL kernel driver recently added support to inject and clear
poison in a region by specifying an HPA. Add a test case to the
existing cxl-poison unit test that demonstrates how to use the new
debugfs attributes. Use the kernel trace log to validate the round
trip address translations

SKIP, do not fail, if the new debugfs attributes are not present.

See the kernel ABI documentation for usage:
Documentation/ABI/testing/debugfs-cxl

Signed-off-by: Alison Schofield <alison.schofi...@intel.com>
---
 test/cxl-poison.sh | 80 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 69 insertions(+), 11 deletions(-)

diff --git a/test/cxl-poison.sh b/test/cxl-poison.sh
index 6ed890bc666c..f1668d000eb8 100644
--- a/test/cxl-poison.sh
+++ b/test/cxl-poison.sh
@@ -65,18 +65,45 @@ create_x2_region()
 
 inject_poison_sysfs()
 {
-       memdev="$1"
+       dev="$1"
        addr="$2"
 
-       echo "$addr" > /sys/kernel/debug/cxl/"$memdev"/inject_poison
+       echo "$addr" > /sys/kernel/debug/cxl/"$dev"/inject_poison
 }
 
 clear_poison_sysfs()
 {
-       memdev="$1"
+       dev="$1"
        addr="$2"
 
-       echo "$addr" > /sys/kernel/debug/cxl/"$memdev"/clear_poison
+       echo "$addr" > /sys/kernel/debug/cxl/"$dev"/clear_poison
+}
+
+check_trace_entry()
+{
+       expected_region="$1"
+       expected_hpa="$2"
+
+       trace_line=$(grep "cxl_poison" /sys/kernel/tracing/trace | tail -n 1)
+       if [[ -z "$trace_line" ]]; then
+               echo "No cxl_poison trace event found"
+               err "$LINENO"
+       fi
+
+       trace_region=$(echo "$trace_line" | grep -o 'region=[^ ]*' | cut -d= 
-f2)
+       trace_hpa=$(echo "$trace_line" | grep -o 'hpa=0x[0-9a-fA-F]\+' | cut 
-d= -f2)
+
+       if [[ "$trace_region" != "$expected_region" ]]; then
+               echo "Expected region $expected_region not found in trace"
+               echo "$trace_line"
+               err "$LINENO"
+       fi
+
+       if [[ "$trace_hpa" != "$expected_hpa" ]]; then
+               echo "Expected HPA $expected_hpa not found in trace"
+               echo "$trace_line"
+               err "$LINENO"
+       fi
 }
 
 validate_poison_found()
@@ -97,7 +124,7 @@ validate_poison_found()
        fi
 }
 
-test_poison_by_memdev()
+test_poison_by_memdev_by_dpa()
 {
        find_memdev
        inject_poison_sysfs "$memdev" "0x40000000"
@@ -113,7 +140,7 @@ test_poison_by_memdev()
        validate_poison_found "-m $memdev" 0
 }
 
-test_poison_by_region()
+test_poison_by_region_by_dpa()
 {
        create_x2_region
        inject_poison_sysfs "$mem0" "0x40000000"
@@ -123,15 +150,46 @@ test_poison_by_region()
        clear_poison_sysfs "$mem0" "0x40000000"
        clear_poison_sysfs "$mem1" "0x40000000"
        validate_poison_found "-r $region" 0
+
+       $CXL destroy-region -f -b cxl_test "$region"
+}
+
+test_poison_by_region_by_hpa()
+{
+       create_x2_region
+
+       [ -f "/sys/kernel/debug/cxl/$region/inject_poison" ] ||
+              do_skip "test case requires inject by region kernel support"
+
+       base=$(cat /sys/bus/cxl/devices/"$region"/resource)
+       gran=$(cat /sys/bus/cxl/devices/"$region"/interleave_granularity)
+
+       # Test two HPA addresses: base and base + granularity
+       # This hits the two memdevs in the region interleave.
+       hpa1=$(printf "0x%x" $((base)))
+       hpa2=$(printf "0x%x" $((base + gran)))
+
+       inject_poison_sysfs "$region" "$hpa1"
+       check_trace_entry "$region" "$hpa1"
+       inject_poison_sysfs "$region" "$hpa2"
+       check_trace_entry "$region" "$hpa2"
+       validate_poison_found "-r $region" 2
+
+       clear_poison_sysfs "$region" "$hpa1"
+       check_trace_entry "$region" "$hpa1"
+       clear_poison_sysfs "$region" "$hpa2"
+       check_trace_entry "$region" "$hpa2"
+       validate_poison_found "-r $region" 0
 }
 
-# Turn tracing on. Note that 'cxl list --media-errors' toggles the tracing.
-# Turning it on here allows the test user to also view inject and clear
-# trace events.
+# Clear old trace events, enable cxl_poison, enable global tracing
+echo "" > /sys/kernel/tracing/trace
 echo 1 > /sys/kernel/tracing/events/cxl/cxl_poison/enable
+echo 1 > /sys/kernel/tracing/tracing_on
 
-test_poison_by_memdev
-test_poison_by_region
+test_poison_by_memdev_by_dpa
+test_poison_by_region_by_dpa
+test_poison_by_region_by_hpa
 
 check_dmesg "$LINENO"
 
-- 
2.37.3


Reply via email to