| From eb183f26c3fb302d1d2a5f51056c8409b94e62e0 Mon Sep 17 00:00:00 2001 | |
| From: Sasha Levin <sashal@kernel.org> | |
| Date: Mon, 28 Sep 2026 00:55:17 -0700 | |
| Subject: perf test code-reading: Fix duplicate kernel maps and machine leak | |
| From: Ian Rogers <irogers@google.com> | |
| [ Upstream commit 58bf7b7af359d4c6f1af29fd8d2067bbcff1d8c7 ] | |
| In do_test_code_reading(), machine__new_host(&host_env) already calls | |
| machine__create_kernel_maps(machine). Calling | |
| machine__create_kernel_maps(machine) a second time immediately | |
| afterwards duplicates all kernel module maps in machine->kmaps, causing | |
| dso__load_kcore()'s module validation (do_validate_kcore_modules()) to | |
| fail and fall back to /proc/kallsyms. | |
| Furthermore, when try_kcore is true and !have_kcore, returning | |
| TEST_CODE_READING_NO_KCORE directly bypasses out_err, leaking the entire | |
| struct machine, its kernel/module maps and DSOs, all kallsyms symbols, | |
| and host_env. | |
| Remove the redundant machine__create_kernel_maps() call, check for | |
| !machine after machine__new_host(), and jump to out_err when returning | |
| TEST_CODE_READING_NO_KCORE. | |
| Fixes: 0fd4008ed755c52d ("perf test: Use machine__new_host in mmap thread code reading test") | |
| Assisted-by: Antigravity:gemini-3.1-pro | |
| Signed-off-by: Ian Rogers <irogers@google.com> | |
| Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> | |
| [ sashal: Dropped the host_env/perf_env__init parts that don't exist here, and on machine__new_host() failure used "return err" instead of "goto out_err" to avoid a NULL dereference in cleanup. ] | |
| Signed-off-by: Sasha Levin <sashal@kernel.org> | |
| --- | |
| tools/perf/tests/code-reading.c | 16 ++++++++-------- | |
| 1 file changed, 8 insertions(+), 8 deletions(-) | |
| diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c | |
| index 95feb6ef34a0a..b357abe6d7237 100644 | |
| --- a/tools/perf/tests/code-reading.c | |
| +++ b/tools/perf/tests/code-reading.c | |
| @@ -569,13 +569,11 @@ static int do_test_code_reading(bool try_kcore) | |
| pid = getpid(); | |
| machine = machine__new_host(); | |
| - machine->env = &perf_env; | |
| - | |
| - ret = machine__create_kernel_maps(machine); | |
| - if (ret < 0) { | |
| - pr_debug("machine__create_kernel_maps failed\n"); | |
| - goto out_err; | |
| + if (!machine) { | |
| + pr_debug("machine__new_host failed\n"); | |
| + return err; | |
| } | |
| + machine->env = &perf_env; | |
| /* Force the use of kallsyms instead of vmlinux to try kcore */ | |
| if (try_kcore) | |
| @@ -592,8 +590,10 @@ static int do_test_code_reading(bool try_kcore) | |
| have_kcore = dso__is_kcore(map->dso); | |
| /* 2nd time through we just try kcore */ | |
| - if (try_kcore && !have_kcore) | |
| - return TEST_CODE_READING_NO_KCORE; | |
| + if (try_kcore && !have_kcore) { | |
| + err = TEST_CODE_READING_NO_KCORE; | |
| + goto out_err; | |
| + } | |
| /* No point getting kernel events if there is no kernel object */ | |
| if (!have_vmlinux && !have_kcore) | |
| -- | |
| 2.53.0 | |