Skip to contentSkip to conversation

selftests/mm: run_vmtests.sh: fix relative path handling

PATCH In review Sun Jian 4 participants 6 messages Jan 8 – Jan 10

Subsystems (3)
lore
Share & tools
lore

Sun Jian

author
#0 lore raw

PATCH 1/1 selftests/mm: run_vmtests.sh: fix relative path handling

run_vmtests.sh relies on being invoked from its own directory and uses relative paths to run tests.

Change to the script directory at startup so it can be run from any working directory without failing.

---
 tools/testing/selftests/mm/run_vmtests.sh | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/mm/run_vmtests.sh +4 -0
index d9173f2312b7..74c33fd07764 100755
@@ -5,6 +5,10 @@
 # Kselftest framework requirement - SKIP code is 4.
 ksft_skip=4
 
+# Ensure relative paths work regardless of caller's cwd.
+SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
+cd "$SCRIPT_DIR" || exit 1
+
 count_total=0
 count_pass=0
 count_fail=0
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 8 lines · On Thu, 8 Jan 2026 11:16:04 +0800 Sun Jian <[email protected]> wrote:

run_vmtests.sh relies on being invoked from its own directory and uses relative paths to run tests.

Change to the script directory at startup so it can be run from any working directory without failing.

...

hm, why? Is that a thing people actually do?

Is anyone going to actually test this feature?

tools/testing/selftests/mm/run_vmtests.sh line 11

Quoted 10 lines
tools/testing/selftests/mm/run_vmtests.sh +4 -0
@@ -5,6 +5,10 @@
 # Kselftest framework requirement - SKIP code is 4.
 ksft_skip=4
 
+# Ensure relative paths work regardless of caller's cwd.
+SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
+cd "$SCRIPT_DIR" || exit 1
+

Alternatively we could check that we're in the correct directory and error out if not.

author
#2 lore raw
Quoted 3 lines

hm, why? Is that a thing people actually do?

Is anyone going to actually test this feature?

Yes — invoking selftests directly from the kernel root can easily happen in practice, for example::

sudo tools/testing/selftests/mm/run_vmtests.sh

This currently results in false failures because relative paths being resolved against the caller's cwd instead of the script directory.

Quoted 3 lines

Alternatively we could check that we're in the correct directory and error out if not.

That would also be reasonable, but I slightly prefer auto-cd because it avoids an easy invocation pitfall and makes the runner more robust for wrappers/CI
where the cwd is not stable. That said, I'm happy to switch to a fail-fast cwd check if you prefer the behavior.

Regards,
Sun

#3 lore raw
Quoted 12 lines · sun jian
Quoted 3 lines

hm, why? Is that a thing people actually do?

Is anyone going to actually test this feature?

Yes — invoking selftests directly from the kernel root can easily happen in practice, for example::

sudo tools/testing/selftests/mm/run_vmtests.sh

This currently results in false failures because relative paths being resolved against the caller's cwd instead of the script directory.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

Alternatively we could check that we're in the correct directory and error out if not.

That would also be reasonable, but I slightly prefer auto-cd because it avoids an easy invocation pitfall and makes the runner more robust for wrappers/CI
where the cwd is not stable. That said, I'm happy to switch to a fail-fast cwd check if you prefer the behavior.

I'd prefer to just fail for the case that we never supported instead of adding support for it.

Signature · 3 lines
Cheers

David
author
#4 lore raw

PATCH v2 1/1 selftests/mm: run_vmtests.sh: fail if invoked from the wrong directory

In reply to Sun Jian

run_vmtests.sh assumes it is invoked from tools/testing/selftests/mm. When run from another working directory, relative paths can lead to confusing failures. Detect this case and abort with a clear message.

---
 tools/testing/selftests/mm/run_vmtests.sh | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/selftests/mm/run_vmtests.sh +7 -0
index d9173f2312b7..b7025afb56fd 100755
@@ -5,6 +5,13 @@
 # Kselftest framework requirement - SKIP code is 4.
 ksft_skip=4
 
+# Verify invocation from the script directory.
+SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
+if [ "$(pwd -P)" != "$SCRIPT_DIR" ]; then
+    echo "Please run this test from $SCRIPT_DIR" >&2
+    exit 1
+fi
+
 count_total=0
 count_pass=0
 count_fail=0
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#5 lore raw

Re: [PATCH v2] selftests/mm: run_vmtests.sh: fail if invoked from the wrong directory

Quoted 5 lines · On Sat, 10 Jan 2026 01:28:33 +0800 Sun Jian <[email protected]> wrote:

run_vmtests.sh assumes it is invoked from tools/testing/selftests/mm. When run from another working directory, relative paths can lead to confusing failures. Detect this case and abort with a clear message.

Thanks,
SJ

[...]