Linux Perf Users
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	James Clark <james.clark@linaro.org>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Clark Williams <williams@redhat.com>,
	linux-perf-users@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Athira Rajeev <atrajeev@linux.ibm.com>
Subject: [PATCH v3 01/18] perf kmem: Use the allocation order when matching page frees
Date: Wed,  2 Dec 2026 09:14:17 -0300	[thread overview]
Message-ID: <20261202121434.2224-2-acme@kernel.org> (raw)
In-Reply-To: <20261202121434.2224-1-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

The page alloc tree is keyed by the values recorded at allocation
time, but the free path looks it up with the order from the free
event while taking the other key fields from the live page record.
When a page is split or reused the orders differ, the lookup misses
and the whole stat fails with a misleading -ENOMEM.

Use the order from the live page record, as is already done for the
gfp flags, migrate type and callsite, so the lookup finds the entry
created at allocation time.  Subtract the allocation's size when a
live entry goes away, not the free event's, and refresh the live
record on a new allocation of the same page, so a free that follows
a lost free is matched to this allocation.

Fixes: 0d68bc92c481 ("perf kmem: Analyze page allocator events also")
Reviewed-by: Athira Rajeev <atrajeev@linux.ibm.com>
Tested-by: Athira Rajeev <atrajeev@linux.ibm.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-kmem.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 87869c717266815c..a73e2586922ad732 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -873,9 +873,17 @@ static int evsel__process_page_alloc_event(struct perf_sample *sample)
 	if (pstat == NULL)
 		return -ENOMEM;
 
+	/*
+	 * A page whose earlier free was not captured is still in the
+	 * live tree: refresh its key with this allocation, so the next
+	 * free is matched to the entry created here.
+	 */
+	pstat->order = order;
+	pstat->gfp_flags = gfp_flags;
+	pstat->migrate_type = migrate_type;
+	pstat->callsite = callsite;
 	pstat->nr_alloc++;
 	pstat->alloc_bytes += bytes;
-	pstat->callsite = callsite;
 
 	if (!live_page) {
 		pstat = page_stat__findnew_alloc(&this);
@@ -905,6 +913,7 @@ static int evsel__process_page_free_event(struct perf_sample *sample)
 	u64 page;
 	unsigned int order = perf_sample__intval(sample, "order");
 	u64 bytes = kmem_page_size << order;
+	u64 alloc_size;
 	struct page_stat *pstat;
 	struct page_stat this = {
 		.order = order,
@@ -935,9 +944,16 @@ static int evsel__process_page_free_event(struct perf_sample *sample)
 		return 0;
 	}
 
+	/*
+	 * The alloc tree is keyed by the values recorded at allocation
+	 * time.  Use the order from the live page as well, otherwise a
+	 * free of a split/reused page looks up the wrong entry.
+	 */
+	this.order = pstat->order;
 	this.gfp_flags = pstat->gfp_flags;
 	this.migrate_type = pstat->migrate_type;
 	this.callsite = pstat->callsite;
+	alloc_size = kmem_page_size << this.order;
 
 	rb_erase(&pstat->node, &page_live_tree);
 	free(pstat);
@@ -961,8 +977,12 @@ static int evsel__process_page_free_event(struct perf_sample *sample)
 	pstat->free_bytes += bytes;
 
 	if (live_page) {
+		/*
+		 * The entry was credited with the allocation's size, a
+		 * split or coalesced free must not change that.
+		 */
 		pstat->nr_alloc--;
-		pstat->alloc_bytes -= bytes;
+		pstat->alloc_bytes -= alloc_size;
 
 		if (pstat->nr_alloc == 0) {
 			rb_erase(&pstat->node, &page_caller_tree);
-- 
2.56.0


  reply	other threads:[~2026-12-02 12:14 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:14 [PATCH v3 00/18] perf tests: Add coverage for the untested perf subcommands Arnaldo Carvalho de Melo
2026-12-02 12:14 ` Arnaldo Carvalho de Melo [this message]
2026-12-02 12:14 ` [PATCH v3 02/18] perf tests top: Pin the ring buffer size Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 03/18] perf tests sched: Exercise the sched views and options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 04/18] perf tests help: Add a basic perf help test Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 05/18] perf tests config: Exercise the config file and list options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 06/18] perf tests script: Exercise the list, fields and dump options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 07/18] perf tests sort: Exercise the report and diff sort keys Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 08/18] perf tests diff: Exercise the output options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 09/18] perf tests annotate: Exercise the output and filter options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 10/18] perf tests report: Exercise the output and format options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 11/18] perf tests record: Exercise more record options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 12/18] perf tests trace: Exercise the tracing options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 13/18] perf tests timechart: Exercise the report options Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 14/18] perf tests kmem: Add a basic perf kmem test Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 15/18] perf tests bench: Add smoke tests for perf bench Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 16/18] perf tests lock: Exercise perf lock report and info Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 17/18] perf tests probe: Exercise the variable and line listing Arnaldo Carvalho de Melo
2026-12-02 12:14 ` [PATCH v3 18/18] perf tests report: Exercise the TUI browsers Arnaldo Carvalho de Melo
2026-12-02 12:30 ` [PATCH v3 00/18] perf tests: Add coverage for the untested perf subcommands sashiko-bot

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=20261202121434.2224-2-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=atrajeev@linux.ibm.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=williams@redhat.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