From: David Gow <david@davidgow.net>
To: Shuvam Pandey <shuvampandey1@gmail.com>, brendan.higgins@linux.dev
Cc: raemoar63@gmail.com, linux-kselftest@vger.kernel.org,
kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] kunit: tool: copy caller args in run_kernel to prevent mutation
Date: Fri, 27 Feb 2026 17:13:53 +0800 [thread overview]
Message-ID: <55be6364-ba3f-4b26-b2de-924ef49639fe@davidgow.net> (raw)
In-Reply-To: <20260226152910.34530-1-shuvampandey1@gmail.com>
Le 26/02/2026 à 11:29 PM, Shuvam Pandey a écrit :
> run_kernel() appended KUnit flags directly to the caller-provided args
> list. When exec_tests() calls run_kernel() repeatedly (e.g. with
> --run_isolated), each call mutated the same list, causing later runs
> to inherit stale filter_glob values and duplicate kunit.enable flags.
>
> Fix this by copying args at the start of run_kernel(). Add a regression
> test that calls run_kernel() twice with the same list and verifies the
> original remains unchanged.
>
> Fixes: ff9e09a3762f ("kunit: tool: support running each suite/test separately")
> Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
> ---
Nice catch!
Reviewed-by: David Gow <david@davidgow.net>
Thanks,
-- David
> tools/testing/kunit/kunit_kernel.py | 6 ++++--
> tools/testing/kunit/kunit_tool_test.py | 26 ++++++++++++++++++++++++++
> 2 files changed, 30 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/kunit/kunit_kernel.py b/tools/testing/kunit/kunit_kernel.py
> index 260d8d9aa1db..2998e1bc088b 100644
> --- a/tools/testing/kunit/kunit_kernel.py
> +++ b/tools/testing/kunit/kunit_kernel.py
> @@ -346,8 +346,10 @@ class LinuxSourceTree:
> return self.validate_config(build_dir)
>
> def run_kernel(self, args: Optional[List[str]]=None, build_dir: str='', filter_glob: str='', filter: str='', filter_action: Optional[str]=None, timeout: Optional[int]=None) -> Iterator[str]:
> - if not args:
> - args = []
> + # Copy to avoid mutating the caller-supplied list. exec_tests() reuses
> + # the same args across repeated run_kernel() calls (e.g. --run_isolated),
> + # so appending to the original would accumulate stale flags on each call.
> + args = list(args) if args else []
> if filter_glob:
> args.append('kunit.filter_glob=' + filter_glob)
> if filter:
> diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py
> index b67408147c1f..f6383884c599 100755
> --- a/tools/testing/kunit/kunit_tool_test.py
> +++ b/tools/testing/kunit/kunit_tool_test.py
> @@ -503,6 +503,32 @@ class LinuxSourceTreeTest(unittest.TestCase):
> with open(kunit_kernel.get_outfile_path(build_dir), 'rt') as outfile:
> self.assertEqual(outfile.read(), 'hi\nbye\n', msg='Missing some output')
>
> + def test_run_kernel_args_not_mutated(self):
> + """Verify run_kernel() copies args so callers can reuse them."""
> + start_calls = []
> +
> + def fake_start(start_args, unused_build_dir):
> + start_calls.append(list(start_args))
> + return subprocess.Popen(['printf', 'KTAP version 1\n'],
> + text=True, stdout=subprocess.PIPE)
> +
> + with tempfile.TemporaryDirectory('') as build_dir:
> + tree = kunit_kernel.LinuxSourceTree(build_dir,
> + kunitconfig_paths=[os.devnull])
> + with mock.patch.object(tree._ops, 'start', side_effect=fake_start), \
> + mock.patch.object(kunit_kernel.subprocess, 'call'):
> + kernel_args = ['mem=1G']
> + for _ in tree.run_kernel(args=kernel_args, build_dir=build_dir,
> + filter_glob='suite.test1'):
> + pass
> + for _ in tree.run_kernel(args=kernel_args, build_dir=build_dir,
> + filter_glob='suite.test2'):
> + pass
> + self.assertEqual(kernel_args, ['mem=1G'],
> + 'run_kernel() should not modify caller args')
> + self.assertIn('kunit.filter_glob=suite.test1', start_calls[0])
> + self.assertIn('kunit.filter_glob=suite.test2', start_calls[1])
> +
> def test_build_reconfig_no_config(self):
> with tempfile.TemporaryDirectory('') as build_dir:
> with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f:
>
> base-commit: f4d0ec0aa20d49f09dc01d82894ce80d72de0560
prev parent reply other threads:[~2026-02-27 9:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-26 15:29 Shuvam Pandey
2026-02-27 9:13 ` David Gow [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=55be6364-ba3f-4b26-b2de-924ef49639fe@davidgow.net \
--to=david@davidgow.net \
--cc=brendan.higgins@linux.dev \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=raemoar63@gmail.com \
--cc=shuvampandey1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®