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

Sun Jian posted 1 patch 1 month ago
tools/testing/selftests/mm/run_vmtests.sh | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] selftests/mm: run_vmtests.sh: fix relative path handling
Posted by Sun Jian 1 month ago
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.

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---
 tools/testing/selftests/mm/run_vmtests.sh | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index d9173f2312b7..74c33fd07764 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -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
-- 
2.43.0
Re: [PATCH] selftests/mm: run_vmtests.sh: fix relative path handling
Posted by Andrew Morton 4 weeks, 1 day ago
On Thu,  8 Jan 2026 11:16:04 +0800 Sun Jian <sun.jian.kdev@gmail.com> 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?

> --- a/tools/testing/selftests/mm/run_vmtests.sh
> +++ b/tools/testing/selftests/mm/run_vmtests.sh
> @@ -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.
Re: [PATCH] selftests/mm: run_vmtests.sh: fix relative path handling
Posted by sun jian 4 weeks, 1 day ago
> 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.
>
> 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
Re: [PATCH] selftests/mm: run_vmtests.sh: fix relative path handling
Posted by David Hildenbrand (Red Hat) 4 weeks, 1 day ago
On 1/9/26 03:08, sun jian wrote:
>> 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.
>>
>> 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.

-- 
Cheers

David
[PATCH v2] selftests/mm: run_vmtests.sh: fail if invoked from the wrong directory
Posted by Sun Jian 4 weeks, 1 day ago
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.

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---
 tools/testing/selftests/mm/run_vmtests.sh | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index d9173f2312b7..b7025afb56fd 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -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
-- 
2.43.0
Re: [PATCH v2] selftests/mm: run_vmtests.sh: fail if invoked from the wrong directory
Posted by SeongJae Park 4 weeks ago
On Sat, 10 Jan 2026 01:28:33 +0800 Sun Jian <sun.jian.kdev@gmail.com> 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.
> 
> Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>

Reviewed-by: SeongJae Park <sj@kernel.org>


Thanks,
SJ

[...]