Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796167323; b=GQ2HRnoBmbhWBhyYhold/k0aQZizYDNVWWRWzzsNJvh3aje4Vg2zEO9Z9XVaNV7vZ1lb JSm4twoWoh3iOIyk9QeyxWpgl0D4VzRZOjFsloM9exomGZtnGZSy1Q8xR8G+TP3AgQXUJ I23rWS+WA5UpRgTKg4Z3Gq25ecJBrl9aQAHU/ytZAZLDBXwYQ3R04GfF/tnyjW6z1aBHf 31GE8D+F98gQXjzjgmFe4BzSg6cVUj7HXEa2OXQkaLdGh0KYrIQtWNBysw2+IbHtJ3pQZ lDJPs1EzLmz8NGkSr45LN6pFTeOq42hf34pquqWue6jXkkldkDPCcKg+w8DM8wNJ8iw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167323; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=rrQqkKOV4pYBkyE3oMGK4QmxNRkSuDqDrDGJYjSXqPk=; b=ByD2MkzlohQWBRFQQ82B8imOk8UGOaQYt3qPAe+sHK3RWGrnVfNxYV1OgsuLek+OAhgK nJPSXoGNfQf95XyexsssWmyPXKVGPueZtqWEvbLGCmRIGu9vc3OrO1LakmwJ+ZtE39yjs oM1liTiIDvyNjHojs6kFomJgBFg6pMY0xX/RgCZc+7Q07iF6YBeE0S1Hxw9i2P/MiZ8Ui SYb2oWXebw4Gcg86dfRbWPN9v+OAZQkKuTT/oSkZcoRVbwxV2FYZy+EbHcKq2N0dvc6LU vbnr3u/kuHoUN6VsFEQkPv8rsn3gwbMAAmLEGNkY12aWwcVd8nF8ocXtohQ6Se2diyA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=YwFgJAlw; arc=none smtp.remote-ip=209.85.210.70 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4987A606AFD8 for ; Tue, 1 Dec 2026 23:22:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4987A606AFD8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=YwFgJAlw Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-863475627f7so8924377a34.0 for ; Tue, 01 Dec 2026 15:22:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796167322; x=1796772122; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=rrQqkKOV4pYBkyE3oMGK4QmxNRkSuDqDrDGJYjSXqPk=; b=YwFgJAlwIOhe1fxCz+Pd/cLCkFVWObiFv48qpK8vjr9xdSZ7D0wcmFarSFgggtDDzd Ivg52d1WplV4vTlx8x11GSoTTYWwCgbnnbszbKZfib7pn2s/eZ3x83Z2glbE7PaSJUrz hAzUO1rcugUePuKstWvwE/v29FsuBhtkKDx9E7rXqmvzhy541UWTS5ZkaiJCLiWunXdn mlnndmDwkjKWLHXva1g5dqobU+NAjgWx1U95zaNAX8uAOdDPzsq+DBJl1Iok72RYSzj9 ifx3FTNsi/MRhVvZAJ3gS/SKE5gUHSiJOA/y0hwNU1aYS3yaBnluQHcFw9biz9yGlocx JpAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796167322; x=1796772122; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=rrQqkKOV4pYBkyE3oMGK4QmxNRkSuDqDrDGJYjSXqPk=; b=NdO377j0+tIhVMW+0a+7LeMgWskACPQS4tPbb83Av8l8/nU6jZpSiOV5MuhIIc9UI9 pdcz0WFHOAjiDa8uZHvopZiX2h8f+mSwiRnSLGe0RdYLxWAvOjZ5skOzSygJvoSlEHbs obqbTydp0Ue740o4z/xbKITJKE1XL/kPCx07R9zyZGprce46qTql3FTiUYs0CvxKbilc ziRVkTXPDlgpFtJqvDxJsXMYzj3W07EkuLHhsC9HiJIi57TAXIjhNU8raQlRhlmu5+41 ecj1LgGWijjD8VcBMmPoHjIoD/GhskdZiyfYx6GbYaTRNTmPJFa+PmDDVBBoaH3+MnMc LzHA== X-Forwarded-Encrypted: i=1; AL+hErHhLughB03Oar/PAHN3GHLw4m7LhHDi9nrYYH6zMyADqVgvn/G35mJ1vEe52DS5P+xD7iE=@vger.kernel.org X-Gm-Message-State: APuU4Z89Teon8uwD03rkW33WGG4Pfp21AKlTZaGFLi54wriWG6aORsNG aXFYkglC5WSQVn9fk+qn/xk2H1+f+LysH584LsLlUr2YJJsLrKgv2uZJytkyGNKlJddM6O+gUIh 1bU5NFM4f0A== X-Received: from otpv21.prod.google.com ([2002:a05:6830:1415:b0:82b:27e4:68be]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6830:2649:b0:862:b76e:3a73 with SMTP id 46e09a7af769-86315a6d398mr38463174a34.29.1796167321995; Tue, 01 Dec 2026 15:22:01 -0800 (PST) Date: Tue, 1 Dec 2026 15:21:16 -0800 In-Reply-To: <20261201232116.1988791-1-irogers@google.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201232116.1988791-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261201232116.1988791-17-irogers@google.com> Subject: [RFC PATCH 16/16] perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR tests From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Marc Zyngier , Alexei Starovoitov , linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" Add a dedicated host-ip-sym C test suite covering: - Automatic guest-to-host symbolization fallback across non-executable (PROT_READ) file-backed host MMAP2 mappings with non-zero pgoff, default host-only (!perf_guest) mode, perf kvm --guest (!perf_host) mode, guest symbol precedence when guest mappings are present, and build_id__mark_dso_hit() DSO hit marking via sample->host_ip. - Histogram collapsing, sorting, and formatting for host_dso, host_sym, and host_ip sort keys. - ASLR address anonymization of sampled host_ip values in aslr_tool. Signed-off-by: Ian Rogers --- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/host-ip-sym.c | 691 ++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 694 insertions(+) create mode 100644 tools/perf/tests/host-ip-sym.c diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 58858f3d2bf9..ace316b165a1 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -73,6 +73,7 @@ perf-test-y += hwmon_pmu.o perf-test-y += tool_pmu.o perf-test-y += subcmd-help.o perf-test-y += kallsyms-split.o +perf-test-y += host-ip-sym.o ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc riscv)) perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 0b8897fd42ca..f7388c5acf2b 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -177,6 +177,7 @@ static struct test_suite *generic_tests[] = { &suite__util, &suite__subcmd_help, &suite__kallsyms_split, + &suite__host_ip_sym, NULL, }; diff --git a/tools/perf/tests/host-ip-sym.c b/tools/perf/tests/host-ip-sym.c new file mode 100644 index 000000000000..b2acf08446e9 --- /dev/null +++ b/tools/perf/tests/host-ip-sym.c @@ -0,0 +1,691 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "perf.h" +#include "tests/tests.h" +#include "util/addr_location.h" +#include "util/aslr.h" +#include "util/build-id.h" +#include "util/debug.h" +#include "util/dso.h" +#include "util/event.h" +#include "util/evlist.h" +#include "util/evsel.h" +#include "util/hist.h" +#include "util/machine.h" +#include "util/map.h" +#include "util/parse-events.h" +#include "util/sample.h" +#include "util/sort.h" +#include "util/symbol.h" +#include "util/synthetic-events.h" +#include "util/thread.h" +#include "util/tool.h" + +#define TEST_HV_PID 4242 +#define TEST_VCPU_TID 4243 +#define TEST_DAX_START 0x7f0000200000ULL +#define TEST_DAX_LEN 0x10000ULL +#define TEST_DAX_PGOFF 0x4000ULL +#define TEST_DAX_SYM1_OFF 0x4100ULL +#define TEST_DAX_SYM2_OFF 0x4200ULL +#define TEST_DAX_SYM_LEN 0x80ULL +#define TEST_HOST_IP1 (TEST_DAX_START + (TEST_DAX_SYM1_OFF - TEST_DAX_PGOFF) + 0x20ULL) +#define TEST_HOST_IP2 (TEST_DAX_START + (TEST_DAX_SYM2_OFF - TEST_DAX_PGOFF) + 0x20ULL) +#define TEST_GUEST_IP 0x401120ULL + +static void cleanup_machines(struct machines *machines) +{ + struct rb_node *nd; + + while ((nd = rb_first_cached(&machines->guests)) != NULL) { + struct machine *guest = rb_entry(nd, struct machine, rb_node); + + rb_erase_cached(nd, &machines->guests); + machine__delete(guest); + } + machines__exit(machines); +} + +static void delete_all_hist_entries(struct hists *hists) +{ + struct rb_root_cached *root_in; + struct rb_root_cached *root_out = &hists->entries; + struct rb_node *node; + + if (hists__has(hists, need_collapse)) + root_in = &hists->entries_collapsed; + else + root_in = hists->entries_in; + + while (!RB_EMPTY_ROOT(&root_out->rb_root)) { + struct hist_entry *he; + + node = rb_first_cached(root_out); + he = rb_entry(node, struct hist_entry, rb_node); + rb_erase_cached(node, root_out); + rb_erase_cached(&he->rb_node_in, root_in); + hist_entry__delete(he); + } +} + +static int setup_host_dax_mapping(struct machine *host_machine) +{ + struct thread *thread; + struct dso *dso; + struct symbol *sym1, *sym2; + struct perf_sample mmap_sample = { + .pid = TEST_HV_PID, + .tid = TEST_HV_PID, + .cpumode = PERF_RECORD_MISC_USER, + }; + union perf_event mmap2_ev = { + .mmap2 = { + .header = { + .type = PERF_RECORD_MMAP2, + .misc = PERF_RECORD_MISC_USER | PERF_RECORD_MISC_MMAP_DATA, + .size = sizeof(struct perf_record_mmap2), + }, + .pid = TEST_HV_PID, + .tid = TEST_HV_PID, + .start = TEST_DAX_START, + .len = TEST_DAX_LEN, + .pgoff = TEST_DAX_PGOFF, + .maj = 8, + .min = 1, + .ino = 123456, + .ino_generation = 1, + .prot = PROT_READ, + .flags = MAP_SHARED, + }, + }; + + snprintf(mmap2_ev.mmap2.filename, sizeof(mmap2_ev.mmap2.filename), + "/mnt/dax/guest_app"); + + thread = machine__findnew_thread(host_machine, TEST_HV_PID, TEST_VCPU_TID); + if (!thread) + return -ENOMEM; + thread__set_comm(thread, "qemu-vcpu", 0); + thread__put(thread); + + if (machine__process_mmap2_event(host_machine, &mmap2_ev, &mmap_sample) < 0) + return -1; + + dso = machine__findnew_dso(host_machine, "/mnt/dax/guest_app"); + if (!dso) + return -ENOMEM; + + dso__set_loaded(dso); + + sym1 = symbol__new(TEST_DAX_SYM1_OFF, TEST_DAX_SYM_LEN, + STB_GLOBAL, STT_FUNC, "dax_guest_hot_fn"); + sym2 = symbol__new(TEST_DAX_SYM2_OFF, TEST_DAX_SYM_LEN, + STB_GLOBAL, STT_FUNC, "dax_guest_cold_fn"); + if (!sym1 || !sym2) { + if (sym1) + symbol__delete(sym1); + if (sym2) + symbol__delete(sym2); + dso__put(dso); + return -ENOMEM; + } + + symbols__insert(dso__symbols(dso), sym1); + symbols__insert(dso__symbols(dso), sym2); + dso__put(dso); + return 0; +} + +static int test__host_ip_fallback(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct machines machines = { 0 }; + struct machine *guest_machine; + struct addr_location al; + struct dso *guest_dso; + struct symbol *guest_sym; + bool saved_host = perf_host; + bool saved_guest = perf_guest; + int ret = TEST_FAIL; + struct perf_sample sample = { + .pid = TEST_HV_PID, + .tid = TEST_VCPU_TID, + .ip = TEST_GUEST_IP, + .host_ip = TEST_HOST_IP1, + .period = 100, + .cpumode = PERF_RECORD_MISC_GUEST_USER, + }; + struct perf_sample guest_mmap_sample = { + .pid = TEST_HV_PID, + .tid = TEST_VCPU_TID, + .cpumode = PERF_RECORD_MISC_GUEST_USER, + }; + union perf_event guest_mmap_ev = { + .mmap = { + .header = { + .type = PERF_RECORD_MMAP, + .misc = PERF_RECORD_MISC_GUEST_USER, + .size = sizeof(struct perf_record_mmap), + }, + .pid = TEST_HV_PID, + .tid = TEST_VCPU_TID, + .start = 0x401000ULL, + .len = 0x1000ULL, + .pgoff = 0, + }, + }; + + if (machines__init(&machines, /*session=*/NULL)) + return TEST_FAIL; + + perf_host = true; + perf_guest = true; + + if (setup_host_dax_mapping(&machines.host)) + goto out_machines; + + guest_machine = machines__find_guest(&machines, TEST_HV_PID); + if (!guest_machine) + goto out_machines; + + addr_location__init(&al); + + /* + * 1. Guest sample with no guest symbols automatically falls back to + * the non-executable DAX mapping on machines.host. + */ + if (machine__resolve(guest_machine, &al, &sample) < 0) + goto out_al; + + if (!al.map || !al.sym || strcmp(al.sym->name, "dax_guest_hot_fn") != 0) { + pr_debug("Expected fallback sym dax_guest_hot_fn, got %s\n", + al.sym ? al.sym->name : ""); + goto out_al; + } + if (al.addr != TEST_DAX_SYM1_OFF + 0x20ULL || al.level != 'u') { + pr_debug("Unexpected fallback addr=%#" PRIx64 " level=%c\n", + al.addr, al.level); + goto out_al; + } + if (!al.host_map || !al.host_sym || + strcmp(al.host_sym->name, "dax_guest_hot_fn") != 0 || + al.host_addr != TEST_DAX_SYM1_OFF + 0x20ULL || + al.raw_host_ip != TEST_HOST_IP1) { + pr_debug("Explicit host_map/host_sym mismatch\n"); + goto out_al; + } + addr_location__exit(&al); + + /* + * 2. In 'perf kvm --guest' mode (perf_host == false, perf_guest == true), + * host_ip lookup on machines.host must still succeed without setting + * HIST_FILTER__HOST. + */ + perf_host = false; + perf_guest = true; + addr_location__init(&al); + if (machine__resolve(guest_machine, &al, &sample) < 0) + goto out_al; + + if (al.filtered != 0 || !al.sym || + strcmp(al.sym->name, "dax_guest_hot_fn") != 0) { + pr_debug("perf kvm --guest mode failed: filtered=%#x sym=%s\n", + al.filtered, al.sym ? al.sym->name : ""); + goto out_al; + } + addr_location__exit(&al); + perf_host = true; + + /* + * 2b. In default perf report/script mode (perf_host == true, + * perf_guest == false), resolving a guest sample via machines.host + * must clear HIST_FILTER__GUEST when falling back to host_map. + */ + perf_host = true; + perf_guest = false; + addr_location__init(&al); + if (machine__resolve(&machines.host, &al, &sample) < 0) + goto out_al; + + if (al.filtered != 0 || al.level != 'u' || !al.sym || + strcmp(al.sym->name, "dax_guest_hot_fn") != 0) { + pr_debug("default host-only mode failed: filtered=%#x level=%c sym=%s\n", + al.filtered, al.level, al.sym ? al.sym->name : ""); + goto out_al; + } + addr_location__exit(&al); + perf_guest = true; + + /* + * 2c. build_id__mark_dso_hit() must mark the host DSO hit via + * sample.host_ip for guest samples so perf record --host-ip + * caches its build-id. + */ + { + struct perf_event_attr attr = { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_CPU_CYCLES, + .sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | + PERF_SAMPLE_HOST_IP, + }; + union perf_event sample_ev = { + .header = { + .type = PERF_RECORD_SAMPLE, + .misc = PERF_RECORD_MISC_GUEST_USER, + .size = sizeof(struct perf_event_header), + }, + }; + struct evsel *evsel = evsel__new(&attr); + struct dso *host_dso = machine__findnew_dso(&machines.host, + "/mnt/dax/guest_app"); + int hit_err; + + if (!evsel || !host_dso) { + evsel__put(evsel); + dso__put(host_dso); + goto out_machines; + } + if (dso__hit(host_dso)) { + pr_debug("host DSO unexpectedly marked hit before build_id__mark_dso_hit\n"); + evsel__put(evsel); + dso__put(host_dso); + goto out_machines; + } + sample.evsel = evsel; + hit_err = build_id__mark_dso_hit(/*tool=*/NULL, &sample_ev, + &sample, guest_machine); + sample.evsel = NULL; + if (hit_err < 0 || !dso__hit(host_dso)) { + pr_debug("build_id__mark_dso_hit failed to mark host DSO hit\n"); + evsel__put(evsel); + dso__put(host_dso); + goto out_machines; + } + evsel__put(evsel); + dso__put(host_dso); + } + + /* + * 3. When the guest machine DOES have a symbol for sample.ip, guest + * symbolization takes precedence for al.sym while al.host_sym still + * exposes the host symbol. + */ + snprintf(guest_mmap_ev.mmap.filename, sizeof(guest_mmap_ev.mmap.filename), + "/bin/guest_native"); + if (machine__process_mmap_event(guest_machine, &guest_mmap_ev, + &guest_mmap_sample) < 0) + goto out_machines; + + guest_dso = machine__findnew_dso(guest_machine, "/bin/guest_native"); + if (!guest_dso) + goto out_machines; + dso__set_loaded(guest_dso); + guest_sym = symbol__new(0x100ULL, 0x80ULL, STB_GLOBAL, STT_FUNC, + "guest_native_fn"); + if (!guest_sym) { + dso__put(guest_dso); + goto out_machines; + } + symbols__insert(dso__symbols(guest_dso), guest_sym); + dso__put(guest_dso); + + addr_location__init(&al); + if (machine__resolve(guest_machine, &al, &sample) < 0) + goto out_al; + + if (!al.sym || strcmp(al.sym->name, "guest_native_fn") != 0 || + !al.host_sym || strcmp(al.host_sym->name, "dax_guest_hot_fn") != 0) { + pr_debug("Guest symbol precedence failed: sym=%s host_sym=%s\n", + al.sym ? al.sym->name : "", + al.host_sym ? al.host_sym->name : ""); + goto out_al; + } + + ret = TEST_OK; +out_al: + addr_location__exit(&al); +out_machines: + perf_host = saved_host; + perf_guest = saved_guest; + cleanup_machines(&machines); + return ret; +} + +static int add_guest_hist_sample(struct evsel *evsel, struct machine *guest_machine, + u64 guest_ip, u64 host_ip) +{ + struct addr_location al; + struct perf_sample sample = { + .evsel = evsel, + .pid = TEST_HV_PID, + .tid = TEST_VCPU_TID, + .cpu = 0, + .ip = guest_ip, + .host_ip = host_ip, + .period = 100, + .cpumode = PERF_RECORD_MISC_GUEST_USER, + }; + struct hist_entry_iter iter = { + .sample = &sample, + .ops = &hist_iter_normal, + .hide_unresolved = false, + }; + int err; + + addr_location__init(&al); + err = machine__resolve(guest_machine, &al, &sample); + if (!err) + err = hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack, NULL); + addr_location__exit(&al); + return err; +} + +static int test__host_ip_hist_sort(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct machines machines = { 0 }; + struct machine *guest_machine; + struct evlist *evlist; + struct evsel *evsel; + struct hists *hists; + struct rb_node *node; + struct hist_entry *he; + char buf[512]; + struct perf_hpp hpp = { + .buf = buf, + .size = sizeof(buf), + }; + bool saved_host = perf_host; + bool saved_guest = perf_guest; + int ret = TEST_FAIL; + + evlist = evlist__new(); + if (!evlist) + return TEST_FAIL; + + if (parse_event(evlist, "cpu-clock")) + goto out_evlist; + + if (machines__init(&machines, /*session=*/NULL)) + goto out_evlist; + + perf_host = true; + perf_guest = true; + + if (setup_host_dax_mapping(&machines.host)) + goto out_machines; + + guest_machine = machines__find_guest(&machines, TEST_HV_PID); + if (!guest_machine) + goto out_machines; + + field_order = NULL; + sort_order = "host_dso,host_sym,host_ip"; + if (setup_sorting(/*evlist=*/NULL, machines.host.env) < 0) + goto out_machines; + perf_hpp__init(); + + evsel = evlist__first(evlist); + hists = evsel__hists(evsel); + + if (add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP, TEST_HOST_IP1) || + add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP + 4, TEST_HOST_IP1) || + add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP + 0x100, TEST_HOST_IP2)) + goto out_hists; + + hists__collapse_resort(hists, NULL); + evsel__output_resort(evsel, NULL); + + node = rb_first_cached(&hists->entries); + if (!node) + goto out_hists; + + he = rb_entry(node, struct hist_entry, rb_node); + if (he->stat.period != 200 || !he->host_ms.sym || + strcmp(he->host_ms.sym->name, "dax_guest_hot_fn") != 0 || + !he->host_ms.map || + strcmp(dso__short_name(map__dso(he->host_ms.map)), "guest_app") != 0 || + map__unmap_ip(he->host_ms.map, he->host_ip) != TEST_HOST_IP1) { + pr_debug("Unexpected top hist_entry for host sort keys\n"); + goto out_hists; + } + + hists__calc_col_len(hists, he); + { + struct perf_hpp_fmt *fmt; + + buf[0] = '\0'; + hists__for_each_format(hists, fmt) { + int printed = fmt->entry(fmt, &hpp, he); + + if (printed > 0) + advance_hpp(&hpp, printed); + } + } + if (!strstr(buf, "guest_app") || !strstr(buf, "dax_guest_hot_fn") || + !strstr(buf, "7f0000200120")) { + pr_debug("Formatted hist_entry missing expected host fields: '%s'\n", buf); + goto out_hists; + } + + node = rb_next(node); + if (!node) + goto out_hists; + he = rb_entry(node, struct hist_entry, rb_node); + if (he->stat.period != 100 || !he->host_ms.sym || + strcmp(he->host_ms.sym->name, "dax_guest_cold_fn") != 0) { + pr_debug("Unexpected second hist_entry for host sort keys\n"); + goto out_hists; + } + + /* + * Verify collapsing a sample with host_ip == 0 into an existing + * comm-sorted entry preserves valid host_ms.map/sym references. + */ + delete_all_hist_entries(hists); + reset_output_field(); + sort_order = "comm"; + if (setup_sorting(/*evlist=*/NULL, machines.host.env) < 0) + goto out_machines; + perf_hpp__init(); + + if (add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP, TEST_HOST_IP1) || + add_guest_hist_sample(evsel, guest_machine, TEST_GUEST_IP + 4, 0)) + goto out_hists; + + hists__collapse_resort(hists, NULL); + evsel__output_resort(evsel, NULL); + node = rb_first_cached(&hists->entries); + if (!node) + goto out_hists; + he = rb_entry(node, struct hist_entry, rb_node); + if (he->stat.period != 200 || !he->host_ms.map || !he->host_ms.sym || + strcmp(he->host_ms.sym->name, "dax_guest_hot_fn") != 0) { + pr_debug("Collapsing sample with host_ip=0 corrupted host_ms\n"); + goto out_hists; + } + + ret = TEST_OK; +out_hists: + delete_all_hist_entries(hists); + reset_output_field(); + sort_order = NULL; +out_machines: + perf_host = saved_host; + perf_guest = saved_guest; + cleanup_machines(&machines); +out_evlist: + evlist__put(evlist); + return ret; +} + +struct aslr_capture_tool { + struct perf_tool tool; + u64 remapped_host_ip[2]; + int count; +}; + +static int capture_sample_cb(const struct perf_tool *tool, + union perf_event *event __maybe_unused, + struct perf_sample *sample, + struct machine *machine __maybe_unused) +{ + struct aslr_capture_tool *cap = + container_of(tool, struct aslr_capture_tool, tool); + + if (cap->count < 2) + cap->remapped_host_ip[cap->count] = sample->host_ip; + cap->count++; + return 0; +} + +static int nop_mmap2_cb(const struct perf_tool *tool __maybe_unused, + union perf_event *event __maybe_unused, + struct perf_sample *sample __maybe_unused, + struct machine *machine __maybe_unused) +{ + return 0; +} + +static int test__host_ip_aslr(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct aslr_capture_tool cap = { .count = 0 }; + struct perf_tool *aslr; + struct machines machines = { 0 }; + struct machine *guest_machine; + struct perf_event_attr attr = { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_CPU_CYCLES, + .sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_HOST_IP, + }; + struct evsel *evsel = NULL; + union perf_event *ev = NULL; + union perf_event mmap2_ev = { + .mmap2 = { + .header = { + .type = PERF_RECORD_MMAP2, + .misc = PERF_RECORD_MISC_USER | PERF_RECORD_MISC_MMAP_DATA, + .size = sizeof(struct perf_record_mmap2), + }, + .pid = TEST_HV_PID, + .tid = TEST_HV_PID, + .start = TEST_DAX_START, + .len = TEST_DAX_LEN, + .pgoff = TEST_DAX_PGOFF, + .prot = PROT_READ, + .flags = MAP_SHARED, + }, + }; + struct perf_sample mmap_sample = { + .pid = TEST_HV_PID, + .tid = TEST_HV_PID, + .cpumode = PERF_RECORD_MISC_USER, + }; + struct perf_sample s1 = { + .ip = TEST_GUEST_IP, + .pid = TEST_HV_PID, + .tid = TEST_VCPU_TID, + .host_ip = TEST_HOST_IP1, + .cpumode = PERF_RECORD_MISC_GUEST_USER, + }; + struct perf_sample s2 = { + .ip = TEST_GUEST_IP, + .pid = TEST_HV_PID, + .tid = TEST_VCPU_TID, + .host_ip = 0, + .cpumode = PERF_RECORD_MISC_GUEST_USER, + }; + size_t sz; + int ret = TEST_FAIL; + + snprintf(mmap2_ev.mmap2.filename, sizeof(mmap2_ev.mmap2.filename), + "/mnt/dax/guest_app"); + + if (machines__init(&machines, /*session=*/NULL)) + return TEST_FAIL; + + guest_machine = machines__find_guest(&machines, TEST_HV_PID); + if (!guest_machine) + goto out_machines; + + perf_tool__init(&cap.tool, /*ordered_events=*/false); + cap.tool.sample = capture_sample_cb; + cap.tool.mmap2 = nop_mmap2_cb; + + aslr = aslr_tool__new(&cap.tool); + if (!aslr) + goto out_machines; + + evsel = evsel__new(&attr); + if (!evsel) + goto out_aslr; + evsel->sample_size = __evsel__sample_size(attr.sample_type); + + if (aslr->mmap2(aslr, &mmap2_ev, &mmap_sample, &machines.host) < 0) + goto out_evsel; + + sz = perf_event__sample_event_size(&s1, attr.sample_type, 0, 0); + ev = calloc(1, sz); + if (!ev) + goto out_evsel; + + ev->header.type = PERF_RECORD_SAMPLE; + ev->header.misc = PERF_RECORD_MISC_GUEST_USER; + ev->header.size = sz; + + s1.evsel = evsel; + if (perf_event__synthesize_sample(ev, attr.sample_type, 0, 0, &s1)) + goto out_ev; + if (aslr->sample(aslr, ev, &s1, guest_machine) < 0) + goto out_ev; + + s2.evsel = evsel; + if (perf_event__synthesize_sample(ev, attr.sample_type, 0, 0, &s2)) + goto out_ev; + if (aslr->sample(aslr, ev, &s2, guest_machine) < 0) + goto out_ev; + + if (cap.count != 2 || cap.remapped_host_ip[0] == 0 || + cap.remapped_host_ip[0] == TEST_HOST_IP1 || + (cap.remapped_host_ip[0] & 0xfffULL) != (TEST_HOST_IP1 & 0xfffULL) || + cap.remapped_host_ip[1] != 0) { + pr_debug("Unexpected ASLR host_ip remapping: %#" PRIx64 ", %#" PRIx64 "\n", + cap.remapped_host_ip[0], cap.remapped_host_ip[1]); + goto out_ev; + } + + ret = TEST_OK; +out_ev: + free(ev); +out_evsel: + evsel__put(evsel); +out_aslr: + aslr_tool__delete(aslr); +out_machines: + cleanup_machines(&machines); + return ret; +} + +static struct test_case tests__host_ip_sym[] = { + TEST_CASE("Guest fallback symbolization via host IP", host_ip_fallback), + TEST_CASE("Histogram sorting and formatting for host IP keys", host_ip_hist_sort), + TEST_CASE("ASLR anonymization of sampled host IP", host_ip_aslr), + { .name = NULL, } +}; + +struct test_suite suite__host_ip_sym = { + .desc = "PERF_SAMPLE_HOST_IP symbolization and sorting", + .test_cases = tests__host_ip_sym, +}; diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 20ae3609892b..3dce04124d6c 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -201,6 +201,7 @@ DECLARE_SUITE(util); DECLARE_SUITE(uncore_event_sorting); DECLARE_SUITE(subcmd_help); DECLARE_SUITE(kallsyms_split); +DECLARE_SUITE(host_ip_sym); /* * PowerPC and S390 do not support creation of instruction breakpoints using the -- 2.56.1.915.g69d5ec7298-goog